source: vendor/3.6.23/source3/libsmb/trustdom_cache.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 7.7 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Trusted domain names cache on top of gencache.
5
6 Copyright (C) Rafal Szczesniak 2002
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23#include "../libcli/security/security.h"
24
25#undef DBGC_CLASS
26#define DBGC_CLASS DBGC_ALL /* there's no proper class yet */
27
28#define TDOMKEY_FMT "TDOM/%s"
29#define TDOMTSKEY "TDOMCACHE/TIMESTAMP"
30
31
32/**
33 * @file trustdom_cache.c
34 *
35 * Implementation of trusted domain names cache useful when
36 * samba acts as domain member server. In such case, caching
37 * domain names currently trusted gives a performance gain
38 * because there's no need to query PDC each time we need
39 * list of trusted domains
40 **/
41
42/**
43 * Initialise trustdom name caching system. Call gencache
44 * initialisation routine to perform necessary activities.
45 *
46 * @return true upon successful cache initialisation or
47 * false if cache init failed
48 **/
49
50bool trustdom_cache_enable(void)
51{
52 return True;
53}
54
55
56/**
57 * Shutdown trustdom name caching system. Calls gencache
58 * shutdown function.
59 *
60 * @return true upon successful cache close or
61 * false if it failed
62 **/
63
64bool trustdom_cache_shutdown(void)
65{
66 return True;
67}
68
69
70/**
71 * Form up trustdom name key. It is based only
72 * on domain name now.
73 *
74 * @param name trusted domain name
75 * @return cache key for use in gencache mechanism
76 **/
77
78static char* trustdom_cache_key(const char* name)
79{
80 char* keystr = NULL;
81 asprintf_strupper_m(&keystr, TDOMKEY_FMT, name);
82
83 return keystr;
84}
85
86
87/**
88 * Store trusted domain in gencache as the domain name (key)
89 * and trusted domain's SID (value)
90 *
91 * @param name trusted domain name
92 * @param alt_name alternative trusted domain name (used in ADS domains)
93 * @param sid trusted domain's SID
94 * @param timeout cache entry expiration time
95 * @return true upon successful value storing or
96 * false if store attempt failed
97 **/
98
99bool trustdom_cache_store(char* name, char* alt_name, const struct dom_sid *sid,
100 time_t timeout)
101{
102 char *key, *alt_key;
103 fstring sid_string;
104 bool ret;
105
106 DEBUG(5, ("trustdom_store: storing SID %s of domain %s\n",
107 sid_string_dbg(sid), name));
108
109 key = trustdom_cache_key(name);
110 alt_key = alt_name ? trustdom_cache_key(alt_name) : NULL;
111
112 /* Generate string representation domain SID */
113 sid_to_fstring(sid_string, sid);
114
115 /*
116 * try to put the names in the cache
117 */
118 if (alt_key) {
119 ret = gencache_set(alt_key, sid_string, timeout);
120 if ( ret ) {
121 ret = gencache_set(key, sid_string, timeout);
122 }
123 SAFE_FREE(alt_key);
124 SAFE_FREE(key);
125 return ret;
126 }
127
128 ret = gencache_set(key, sid_string, timeout);
129 SAFE_FREE(key);
130 return ret;
131}
132
133
134/**
135 * Fetch trusted domain's SID from the gencache.
136 * This routine can also be used to check whether given
137 * domain is currently trusted one.
138 *
139 * @param name trusted domain name
140 * @param sid trusted domain's SID to be returned
141 * @return true if entry is found or
142 * false if has expired/doesn't exist
143 **/
144
145bool trustdom_cache_fetch(const char* name, struct dom_sid* sid)
146{
147 char *key = NULL, *value = NULL;
148 time_t timeout;
149
150 /* exit now if null pointers were passed as they're required further */
151 if (!sid)
152 return False;
153
154 /* prepare a key and get the value */
155 key = trustdom_cache_key(name);
156 if (!key)
157 return False;
158
159 if (!gencache_get(key, &value, &timeout)) {
160 DEBUG(5, ("no entry for trusted domain %s found.\n", name));
161 SAFE_FREE(key);
162 return False;
163 } else {
164 SAFE_FREE(key);
165 DEBUG(5, ("trusted domain %s found (%s)\n", name, value));
166 }
167
168 /* convert sid string representation into struct dom_sid structure */
169 if(! string_to_sid(sid, value)) {
170 sid = NULL;
171 SAFE_FREE(value);
172 return False;
173 }
174
175 SAFE_FREE(value);
176 return True;
177}
178
179
180/*******************************************************************
181 fetch the timestamp from the last update
182*******************************************************************/
183
184uint32 trustdom_cache_fetch_timestamp( void )
185{
186 char *value = NULL;
187 time_t timeout;
188 uint32 timestamp;
189
190 if (!gencache_get(TDOMTSKEY, &value, &timeout)) {
191 DEBUG(5, ("no timestamp for trusted domain cache located.\n"));
192 SAFE_FREE(value);
193 return 0;
194 }
195
196 timestamp = atoi(value);
197
198 SAFE_FREE(value);
199 return timestamp;
200}
201
202/*******************************************************************
203 store the timestamp from the last update
204*******************************************************************/
205
206bool trustdom_cache_store_timestamp( uint32 t, time_t timeout )
207{
208 fstring value;
209
210 fstr_sprintf(value, "%d", t );
211
212 if (!gencache_set(TDOMTSKEY, value, timeout)) {
213 DEBUG(5, ("failed to set timestamp for trustdom_cache\n"));
214 return False;
215 }
216
217 return True;
218}
219
220
221/**
222 * Delete single trustdom entry. Look at the
223 * gencache_iterate definition.
224 *
225 **/
226
227static void flush_trustdom_name(const char* key, const char *value, time_t timeout, void* dptr)
228{
229 gencache_del(key);
230 DEBUG(5, ("Deleting entry %s\n", key));
231}
232
233
234/**
235 * Flush all the trusted domains entries from the cache.
236 **/
237
238void trustdom_cache_flush(void)
239{
240 /*
241 * iterate through each TDOM cache's entry and flush it
242 * by flush_trustdom_name function
243 */
244 gencache_iterate(flush_trustdom_name, NULL, trustdom_cache_key("*"));
245 DEBUG(5, ("Trusted domains cache flushed\n"));
246}
247
248/********************************************************************
249 update the trustdom_cache if needed
250********************************************************************/
251#define TRUSTDOM_UPDATE_INTERVAL 600
252
253void update_trustdom_cache( void )
254{
255 char **domain_names;
256 struct dom_sid *dom_sids;
257 uint32 num_domains;
258 uint32 last_check;
259 int time_diff;
260 TALLOC_CTX *mem_ctx = NULL;
261 time_t now = time(NULL);
262 int i;
263
264 /* get the timestamp. We have to initialise it if the last timestamp == 0 */
265 if ( (last_check = trustdom_cache_fetch_timestamp()) == 0 )
266 trustdom_cache_store_timestamp(0, now+TRUSTDOM_UPDATE_INTERVAL);
267
268 time_diff = (int) (now - last_check);
269
270 if ( (time_diff > 0) && (time_diff < TRUSTDOM_UPDATE_INTERVAL) ) {
271 DEBUG(10,("update_trustdom_cache: not time to update trustdom_cache yet\n"));
272 return;
273 }
274
275 /* note that we don't lock the timestamp. This prevents this
276 smbd from blocking all other smbd daemons while we
277 enumerate the trusted domains */
278 trustdom_cache_store_timestamp(now, now+TRUSTDOM_UPDATE_INTERVAL);
279
280 if ( !(mem_ctx = talloc_init("update_trustdom_cache")) ) {
281 DEBUG(0,("update_trustdom_cache: talloc_init() failed!\n"));
282 goto done;
283 }
284
285 /* get the domains and store them */
286
287 if ( enumerate_domain_trusts(mem_ctx, lp_workgroup(), &domain_names,
288 &num_domains, &dom_sids)) {
289 for ( i=0; i<num_domains; i++ ) {
290 trustdom_cache_store( domain_names[i], NULL, &dom_sids[i],
291 now+TRUSTDOM_UPDATE_INTERVAL);
292 }
293 } else {
294 /* we failed to fetch the list of trusted domains - restore the old
295 timestamp */
296 trustdom_cache_store_timestamp(last_check,
297 last_check+TRUSTDOM_UPDATE_INTERVAL);
298 }
299
300done:
301 talloc_destroy( mem_ctx );
302
303 return;
304}
Note: See TracBrowser for help on using the repository browser.