convert snprintf() calls using pstrings & fstrings
[nivanova/samba-autobuild/.git] / source3 / libsmb / trustdom_cache.c
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.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 /**
44  * Initialise trustdom name caching system. Call gencache
45  * initialisation routine to perform necessary activities.
46  *
47  * @return true upon successful cache initialisation or
48  *         false if cache init failed
49  **/
50  
51 BOOL trustdom_cache_enable(void)
52 {
53         /* Init trustdom cache by calling gencache initialisation */
54         if (!gencache_init()) {
55                 DEBUG(2, ("trustdomcache_enable: Couldn't initialise trustdom cache on top of gencache.\n"));
56                 return False;
57         }
58
59         return True;
60 }
61
62
63 /**
64  * Shutdown trustdom name caching system. Calls gencache
65  * shutdown function.
66  *
67  * @return true upon successful cache close or
68  *         false if it failed
69  **/
70  
71 BOOL trustdom_cache_shutdown(void)
72 {
73         /* Close trustdom cache by calling gencache shutdown */
74         if (!gencache_shutdown()) {
75                 DEBUG(2, ("trustdomcache_shutdown: Couldn't shutdown trustdom cache on top of gencache.\n"));
76                 return False;
77         }
78         
79         return True;
80 }
81
82
83 /**
84  * Form up trustdom name key. It is based only
85  * on domain name now.
86  *
87  * @param name trusted domain name
88  * @return cache key for use in gencache mechanism
89  **/
90
91 static char* trustdom_cache_key(const char* name)
92 {
93         char* keystr = NULL;
94         asprintf(&keystr, TDOMKEY_FMT, strupper_static(name));
95         
96         return keystr;
97 }
98
99
100 /**
101  * Store trusted domain in gencache as the domain name (key)
102  * and ip address of domain controller (value)
103  *
104  * @param name trusted domain name
105  * @param alt_name alternative trusted domain name (used in ADS domains)
106  * @param sid trusted domain's SID
107  * @param timeout cache entry expiration time
108  * @return true upon successful value storing or
109  *         false if store attempt failed
110  **/
111  
112 BOOL trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
113                           time_t timeout)
114 {
115         char *key, *alt_key;
116         fstring sid_string;
117
118         /*
119          * we use gecache call to avoid annoying debug messages
120          * about initialised trustdom 
121          */
122         if (!gencache_init()) return False;
123
124         DEBUG(5, ("trustdom_store: storing SID %s of domain %s\n",
125                   sid_string_static(sid), name));
126
127         key = trustdom_cache_key(name);
128         alt_key = alt_name ? trustdom_cache_key(alt_name) : NULL;
129
130         /* Generate string representation domain SID */
131         sid_to_string(sid_string, sid);
132
133         /*
134          * try to put the names in the cache
135          */
136         if (alt_key) {
137                 return (gencache_set(alt_key, sid_string, timeout)
138                         && gencache_set(key, sid_string, timeout));
139         }
140                  
141         return gencache_set(key, sid_string, timeout);
142 }
143
144
145 /**
146  * Fetch trusted domain's dc from the gencache.
147  * This routine can also be used to check whether given
148  * domain is currently trusted one.
149  *
150  * @param name trusted domain name
151  * @param sid trusted domain's SID to be returned
152  * @return true if entry is found or
153  *         false if has expired/doesn't exist
154  **/
155  
156 BOOL trustdom_cache_fetch(const char* name, DOM_SID* sid)
157 {
158         char *key, *value;
159         time_t timeout;
160
161         /* init the cache */
162         if (!gencache_init()) return False;
163         
164         /* exit now if null pointers were passed as they're required further */
165         if (!sid) return False;
166
167         /* prepare a key and get the value */
168         key = trustdom_cache_key(name);
169         if (!key) return False;
170         
171         if (!gencache_get(key, &value, &timeout)) {
172                 DEBUG(5, ("no entry for trusted domain %s found.\n", name));
173                 SAFE_FREE(key);
174                 return False;
175         } else {
176                 SAFE_FREE(key);
177                 DEBUG(5, ("trusted domain %s found (%s)\n", name, value));
178         }
179
180         /* convert ip string representation into in_addr structure */
181         if(! string_to_sid(sid, value)) {
182                 sid = NULL;
183                 return False;
184         }
185         
186         return True;
187 }
188
189
190 /*******************************************************************
191  fetch the timestamp from the last update 
192 *******************************************************************/
193
194 uint32 trustdom_cache_fetch_timestamp( void )
195 {
196         char *value;
197         time_t timeout;
198         uint32 timestamp;
199
200         /* init the cache */
201         if (!gencache_init()) 
202                 return False;
203                 
204         if (!gencache_get(TDOMTSKEY, &value, &timeout)) {
205                 DEBUG(5, ("no timestamp for trusted domain cache located.\n"));
206                 return 0;
207         } 
208
209         timestamp = atoi(value);
210                 
211         return timestamp;
212 }
213
214 /*******************************************************************
215  store the timestamp from the last update 
216 *******************************************************************/
217
218 BOOL trustdom_cache_store_timestamp( uint32 t, time_t timeout )
219 {
220         fstring value;
221
222         /* init the cache */
223         if (!gencache_init()) 
224                 return False;
225                 
226         fstr_sprintf(value, "%d", t );
227                 
228         if (!gencache_set(TDOMTSKEY, value, timeout)) {
229                 DEBUG(5, ("failed to set timestamp for trustdom_cache\n"));
230                 return False;
231         } 
232
233         return True;
234 }
235
236
237 /*******************************************************************
238  lock the timestamp entry in the trustdom_cache
239 *******************************************************************/
240
241 BOOL trustdom_cache_lock_timestamp( void )
242 {
243         return gencache_lock_entry( TDOMTSKEY ) != -1;
244 }
245
246 /*******************************************************************
247  unlock the timestamp entry in the trustdom_cache
248 *******************************************************************/
249
250 void trustdom_cache_unlock_timestamp( void )
251 {
252         gencache_unlock_entry( TDOMTSKEY );
253 }
254
255 /**
256  * Delete single trustdom entry. Look at the
257  * gencache_iterate definition.
258  *
259  **/
260
261 static void flush_trustdom_name(const char* key, const char *value, time_t timeout, void* dptr)
262 {
263         gencache_del(key);
264         DEBUG(5, ("Deleting entry %s\n", key));
265 }
266
267
268 /**
269  * Flush all the trusted domains entries from the cache.
270  **/
271
272 void trustdom_cache_flush(void)
273 {
274         if (!gencache_init())
275                 return;
276
277         /* 
278          * iterate through each TDOM cache's entry and flush it
279          * by flush_trustdom_name function
280          */
281         gencache_iterate(flush_trustdom_name, NULL, trustdom_cache_key("*"));
282         DEBUG(5, ("Trusted domains cache flushed\n"));
283 }
284
285 /********************************************************************
286  update the trustdom_cache if needed 
287 ********************************************************************/
288 #define TRUSTDOM_UPDATE_INTERVAL        600
289
290 void update_trustdom_cache( void )
291 {
292         char **domain_names;
293         DOM_SID *dom_sids;
294         uint32 num_domains;
295         uint32 last_check;
296         int time_diff;
297         TALLOC_CTX *mem_ctx = NULL;
298         time_t now = time(NULL);
299         int i;
300         
301         /* get the timestamp.  We have to initialise it if the last timestamp == 0 */
302         
303         if ( (last_check = trustdom_cache_fetch_timestamp()) == 0 ) 
304                 trustdom_cache_store_timestamp(0, now+TRUSTDOM_UPDATE_INTERVAL);
305
306         time_diff = now - last_check;
307         
308         if ( (time_diff > 0) && (time_diff < TRUSTDOM_UPDATE_INTERVAL) ) {
309                 DEBUG(10,("update_trustdom_cache: not time to update trustdom_cache yet\n"));
310                 return;
311         }
312                 
313         /* lock the timestamp */
314         if ( !trustdom_cache_lock_timestamp() )
315                 return;
316         
317         if ( !(mem_ctx = talloc_init("update_trustdom_cache")) ) {
318                 DEBUG(0,("update_trustdom_cache: talloc_init() failed!\n"));
319                 goto done;
320         }
321
322         /* get the domains and store them */
323         
324         if ( enumerate_domain_trusts(mem_ctx, lp_workgroup(), &domain_names, 
325                 &num_domains, &dom_sids) ) 
326         {
327                 for ( i=0; i<num_domains; i++ ) {
328                         trustdom_cache_store( domain_names[i], NULL, &dom_sids[i], 
329                                 now+TRUSTDOM_UPDATE_INTERVAL);
330                 }
331                 
332                 trustdom_cache_store_timestamp( now, now+TRUSTDOM_UPDATE_INTERVAL );
333         }
334
335 done:   
336         /* unlock and we're done */
337         trustdom_cache_unlock_timestamp();
338         
339         talloc_destroy( mem_ctx );
340         
341         return;
342 }