2 Unix SMB/CIFS implementation.
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/>.
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(TALLOC_CTX
*mem_ctx
, const char *realm
)
33 char *keystr
= talloc_asprintf_strupper_m(
34 mem_ctx
, SITENAME_KEY
, realm
);
39 /****************************************************************************
40 Store the AD client sitename.
41 We store indefinitely as every new CLDAP query will re-write this.
42 ****************************************************************************/
44 bool sitename_store(const char *realm
, const char *sitename
)
50 if (!realm
|| (strlen(realm
) == 0)) {
51 DEBUG(0,("sitename_store: no realm\n"));
55 key
= sitename_key(talloc_tos(), realm
);
57 if (!sitename
|| (sitename
&& !*sitename
)) {
58 DEBUG(5,("sitename_store: deleting empty sitename!\n"));
59 ret
= gencache_del(key
);
64 expire
= get_time_t_max(); /* Store indefinitely. */
66 DEBUG(10,("sitename_store: realm = [%s], sitename = [%s], expire = [%u]\n",
67 realm
, sitename
, (unsigned int)expire
));
69 ret
= gencache_set( key
, sitename
, expire
);
74 /****************************************************************************
75 Fetch the AD client sitename.
77 ****************************************************************************/
79 char *sitename_fetch(TALLOC_CTX
*mem_ctx
, const char *realm
)
81 char *sitename
= NULL
;
84 const char *query_realm
;
87 if (!realm
|| (strlen(realm
) == 0)) {
88 query_realm
= lp_realm();
93 key
= sitename_key(talloc_tos(), query_realm
);
95 ret
= gencache_get( key
, mem_ctx
, &sitename
, &timeout
);
98 DBG_INFO("No stored sitename for realm '%s'\n", query_realm
);
100 DBG_INFO("Returning sitename for realm '%s': \"%s\"\n",
101 query_realm
, sitename
);
106 /****************************************************************************
107 Did the sitename change ?
108 ****************************************************************************/
110 bool stored_sitename_changed(const char *realm
, const char *sitename
)
116 if (!realm
|| (strlen(realm
) == 0)) {
117 DEBUG(0,("stored_sitename_changed: no realm\n"));
121 new_sitename
= sitename_fetch(talloc_tos(), realm
);
123 if (sitename
&& new_sitename
&& !strequal(sitename
, new_sitename
)) {
125 } else if ((sitename
&& !new_sitename
) ||
126 (!sitename
&& new_sitename
)) {
129 TALLOC_FREE(new_sitename
);