LCOV - code coverage report
Current view: top level - source3/libads - sitename_cache.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 36 51 70.6 %
Date: 2024-04-21 15:09:00 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    DNS utility library
       4             :    Copyright (C) Gerald (Jerry) Carter           2006.
       5             :    Copyright (C) Jeremy Allison                  2007.
       6             : 
       7             :    This program is free software; you can redistribute it and/or modify
       8             :    it under the terms of the GNU General Public License as published by
       9             :    the Free Software Foundation; either version 3 of the License, or
      10             :    (at your option) any later version.
      11             : 
      12             :    This program is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU General Public License
      18             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : #include "includes.h"
      22             : #include "libads/sitename_cache.h"
      23             : #include "lib/gencache.h"
      24             : 
      25             : /****************************************************************************
      26             :  Store and fetch the AD client sitename.
      27             : ****************************************************************************/
      28             : 
      29             : #define SITENAME_KEY    "AD_SITENAME/DOMAIN/%s"
      30             : 
      31       11327 : static char *sitename_key(TALLOC_CTX *mem_ctx, const char *realm)
      32             : {
      33       11327 :         char *keystr = talloc_asprintf_strupper_m(
      34             :                 mem_ctx, SITENAME_KEY, realm);
      35       11327 :         return keystr;
      36             : }
      37             : 
      38             : 
      39             : /****************************************************************************
      40             :  Store the AD client sitename.
      41             :  We store indefinitely as every new CLDAP query will re-write this.
      42             : ****************************************************************************/
      43             : 
      44        1262 : bool sitename_store(const char *realm, const char *sitename)
      45             : {
      46           0 :         time_t expire;
      47        1262 :         bool ret = False;
      48           0 :         char *key;
      49             : 
      50        1262 :         if (!realm || (strlen(realm) == 0)) {
      51           0 :                 DEBUG(0,("sitename_store: no realm\n"));
      52           0 :                 return False;
      53             :         }
      54             : 
      55        1262 :         key = sitename_key(talloc_tos(), realm);
      56             : 
      57        1262 :         if (!sitename || (sitename && !*sitename)) {
      58           0 :                 DEBUG(5,("sitename_store: deleting empty sitename!\n"));
      59           0 :                 ret = gencache_del(key);
      60           0 :                 TALLOC_FREE(key);
      61           0 :                 return ret;
      62             :         }
      63             : 
      64        1262 :         expire = get_time_t_max(); /* Store indefinitely. */
      65             : 
      66        1262 :         DEBUG(10,("sitename_store: realm = [%s], sitename = [%s], expire = [%u]\n",
      67             :                 realm, sitename, (unsigned int)expire ));
      68             : 
      69        1262 :         ret = gencache_set( key, sitename, expire );
      70        1262 :         TALLOC_FREE(key);
      71        1262 :         return ret;
      72             : }
      73             : 
      74             : /****************************************************************************
      75             :  Fetch the AD client sitename.
      76             :  Caller must free.
      77             : ****************************************************************************/
      78             : 
      79       10065 : char *sitename_fetch(TALLOC_CTX *mem_ctx, const char *realm)
      80             : {
      81       10065 :         char *sitename = NULL;
      82           0 :         time_t timeout;
      83       10065 :         bool ret = False;
      84           0 :         const char *query_realm;
      85           0 :         char *key;
      86             : 
      87       10065 :         if (!realm || (strlen(realm) == 0)) {
      88        5914 :                 query_realm = lp_realm();
      89             :         } else {
      90        4151 :                 query_realm = realm;
      91             :         }
      92             : 
      93       10065 :         key = sitename_key(talloc_tos(), query_realm);
      94             : 
      95       10065 :         ret = gencache_get( key, mem_ctx, &sitename, &timeout );
      96       10065 :         TALLOC_FREE(key);
      97       10065 :         if ( !ret ) {
      98        7840 :                 DBG_INFO("No stored sitename for realm '%s'\n", query_realm);
      99             :         } else {
     100        2225 :                 DBG_INFO("Returning sitename for realm '%s': \"%s\"\n",
     101             :                          query_realm, sitename);
     102             :         }
     103       10065 :         return sitename;
     104             : }
     105             : 
     106             : /****************************************************************************
     107             :  Did the sitename change ?
     108             : ****************************************************************************/
     109             : 
     110         182 : bool stored_sitename_changed(const char *realm, const char *sitename)
     111             : {
     112         182 :         bool ret = False;
     113             : 
     114           0 :         char *new_sitename;
     115             : 
     116         182 :         if (!realm || (strlen(realm) == 0)) {
     117           0 :                 DEBUG(0,("stored_sitename_changed: no realm\n"));
     118           0 :                 return False;
     119             :         }
     120             : 
     121         182 :         new_sitename = sitename_fetch(talloc_tos(), realm);
     122             : 
     123         182 :         if (sitename && new_sitename && !strequal(sitename, new_sitename)) {
     124           0 :                 ret = True;
     125         182 :         } else if ((sitename && !new_sitename) ||
     126           2 :                         (!sitename && new_sitename)) {
     127           2 :                 ret = True;
     128             :         }
     129         182 :         TALLOC_FREE(new_sitename);
     130         182 :         return ret;
     131             : }
     132             : 

Generated by: LCOV version 1.14