lib: util: Add sys_valid_io_range()
[Samba.git] / source3 / libads / sitename_cache.c
blob549d5890354b2bf01700f53e16962de9d12066e6
1 /*
2 Unix SMB/CIFS implementation.
3 DNS utility library
4 Copyright (C) Gerald (Jerry) Carter 2006.
5 Copyright (C) Jeremy Allison 2007.
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.
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.
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/>.
21 #include "includes.h"
22 #include "libads/sitename_cache.h"
23 #include "lib/gencache.h"
25 /****************************************************************************
26 Store and fetch the AD client sitename.
27 ****************************************************************************/
29 #define SITENAME_KEY "AD_SITENAME/DOMAIN/%s"
31 static char *sitename_key(const char *realm)
33 char *keystr;
35 if (asprintf_strupper_m(&keystr, SITENAME_KEY, realm) == -1) {
36 return NULL;
39 return keystr;
43 /****************************************************************************
44 Store the AD client sitename.
45 We store indefinately as every new CLDAP query will re-write this.
46 ****************************************************************************/
48 bool sitename_store(const char *realm, const char *sitename)
50 time_t expire;
51 bool ret = False;
52 char *key;
54 if (!realm || (strlen(realm) == 0)) {
55 DEBUG(0,("sitename_store: no realm\n"));
56 return False;
59 key = sitename_key(realm);
61 if (!sitename || (sitename && !*sitename)) {
62 DEBUG(5,("sitename_store: deleting empty sitename!\n"));
63 ret = gencache_del(key);
64 SAFE_FREE(key);
65 return ret;
68 expire = get_time_t_max(); /* Store indefinately. */
70 DEBUG(10,("sitename_store: realm = [%s], sitename = [%s], expire = [%u]\n",
71 realm, sitename, (unsigned int)expire ));
73 ret = gencache_set( key, sitename, expire );
74 SAFE_FREE(key);
75 return ret;
78 /****************************************************************************
79 Fetch the AD client sitename.
80 Caller must free.
81 ****************************************************************************/
83 char *sitename_fetch(TALLOC_CTX *mem_ctx, const char *realm)
85 char *sitename = NULL;
86 time_t timeout;
87 bool ret = False;
88 const char *query_realm;
89 char *key;
91 if (!realm || (strlen(realm) == 0)) {
92 query_realm = lp_realm();
93 } else {
94 query_realm = realm;
97 key = sitename_key(query_realm);
99 ret = gencache_get( key, mem_ctx, &sitename, &timeout );
100 SAFE_FREE(key);
101 if ( !ret ) {
102 DBG_INFO("No stored sitename for realm '%s'\n", query_realm);
103 } else {
104 DBG_INFO("Returning sitename for realm '%s': \"%s\"\n",
105 query_realm, sitename);
107 return sitename;
110 /****************************************************************************
111 Did the sitename change ?
112 ****************************************************************************/
114 bool stored_sitename_changed(const char *realm, const char *sitename)
116 bool ret = False;
118 char *new_sitename;
120 if (!realm || (strlen(realm) == 0)) {
121 DEBUG(0,("stored_sitename_changed: no realm\n"));
122 return False;
125 new_sitename = sitename_fetch(talloc_tos(), realm);
127 if (sitename && new_sitename && !strequal(sitename, new_sitename)) {
128 ret = True;
129 } else if ((sitename && !new_sitename) ||
130 (!sitename && new_sitename)) {
131 ret = True;
133 TALLOC_FREE(new_sitename);
134 return ret;