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