talloc: use atexit() again instead of a library destructor
[garming/samba-autobuild/.git] / source3 / libads / sitename_cache.c
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
24 /****************************************************************************
25  Store and fetch the AD client sitename.
26 ****************************************************************************/
27
28 #define SITENAME_KEY    "AD_SITENAME/DOMAIN/%s"
29
30 static char *sitename_key(const char *realm)
31 {
32         char *keystr;
33
34         if (asprintf_strupper_m(&keystr, SITENAME_KEY, realm) == -1) {
35                 return NULL;
36         }
37
38         return keystr;
39 }
40
41
42 /****************************************************************************
43  Store the AD client sitename.
44  We store indefinately as every new CLDAP query will re-write this.
45 ****************************************************************************/
46
47 bool sitename_store(const char *realm, const char *sitename)
48 {
49         time_t expire;
50         bool ret = False;
51         char *key;
52
53         if (!realm || (strlen(realm) == 0)) {
54                 DEBUG(0,("sitename_store: no realm\n"));
55                 return False;
56         }
57
58         key = sitename_key(realm);
59
60         if (!sitename || (sitename && !*sitename)) {
61                 DEBUG(5,("sitename_store: deleting empty sitename!\n"));
62                 ret = gencache_del(key);
63                 SAFE_FREE(key);
64                 return ret;
65         }
66
67         expire = get_time_t_max(); /* Store indefinately. */
68
69         DEBUG(10,("sitename_store: realm = [%s], sitename = [%s], expire = [%u]\n",
70                 realm, sitename, (unsigned int)expire ));
71
72         ret = gencache_set( key, sitename, expire );
73         SAFE_FREE(key);
74         return ret;
75 }
76
77 /****************************************************************************
78  Fetch the AD client sitename.
79  Caller must free.
80 ****************************************************************************/
81
82 char *sitename_fetch(TALLOC_CTX *mem_ctx, const char *realm)
83 {
84         char *sitename = NULL;
85         time_t timeout;
86         bool ret = False;
87         const char *query_realm;
88         char *key;
89
90         if (!realm || (strlen(realm) == 0)) {
91                 query_realm = lp_realm();
92         } else {
93                 query_realm = realm;
94         }
95
96         key = sitename_key(query_realm);
97
98         ret = gencache_get( key, mem_ctx, &sitename, &timeout );
99         SAFE_FREE(key);
100         if ( !ret ) {
101                 DBG_INFO("No stored sitename for realm '%s'\n", query_realm);
102         } else {
103                 DBG_INFO("Returning sitename for realm '%s': \"%s\"\n",
104                          query_realm, sitename);
105         }
106         return sitename;
107 }
108
109 /****************************************************************************
110  Did the sitename change ?
111 ****************************************************************************/
112
113 bool stored_sitename_changed(const char *realm, const char *sitename)
114 {
115         bool ret = False;
116
117         char *new_sitename;
118
119         if (!realm || (strlen(realm) == 0)) {
120                 DEBUG(0,("stored_sitename_changed: no realm\n"));
121                 return False;
122         }
123
124         new_sitename = sitename_fetch(talloc_tos(), realm);
125
126         if (sitename && new_sitename && !strequal(sitename, new_sitename)) {
127                 ret = True;
128         } else if ((sitename && !new_sitename) ||
129                         (!sitename && new_sitename)) {
130                 ret = True;
131         }
132         TALLOC_FREE(new_sitename);
133         return ret;
134 }
135