dummy commit
[metze/old/v3-2-winbind-ndr.git] / source / libsmb / namecache.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NetBIOS name cache module on top of gencache mechanism.
5    
6    Copyright (C) Tim Potter         2002
7    Copyright (C) Rafal Szczesniak   2002
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24
25 #define NBTKEY_FMT  "NBT/%s#%02X"
26
27
28 /**
29  * Initialise namecache system. Function calls gencache
30  * initialisation function to perform necessary actions
31  * 
32  * @return true upon successful initialisation of the cache or
33  *         false on failure
34  **/
35
36 BOOL namecache_enable(void)
37 {
38         /*
39          * Check if name caching disabled by setting the name cache
40          * timeout to zero.
41          */ 
42
43         if (lp_name_cache_timeout() == 0) {
44                 DEBUG(5, ("namecache_enable: disabling netbios name cache\n"));
45                 return False;
46         }
47
48         /* Init namecache by calling gencache initialisation */
49
50         if (!gencache_init()) {
51                 DEBUG(2, ("namecache_enable: Couldn't initialise namecache on top of gencache.\n"));
52                 return False;
53         }
54
55         /* I leave it for now, though I don't think we really need this (mimir, 27.09.2002) */
56         DEBUG(5, ("namecache_enable: enabling netbios namecache, timeout %d "
57                   "seconds\n", lp_name_cache_timeout()));
58
59         return True;
60 }
61
62
63 /**
64  * Shutdown namecache. Routine calls gencache close function
65  * to safely close gencache file.
66  *
67  * @return true upon successful shutdown of the cache or
68  *         false on failure
69  **/
70  
71 BOOL namecache_shutdown(void)
72 {
73         if (!gencache_shutdown()) {
74                 DEBUG(2, ("namecache_shutdown: Couldn't close namecache on top of gencache.\n"));
75                 return False;
76         }
77         
78         DEBUG(5, ("namecache_shutdown: netbios namecache closed successfully.\n"));
79         return True;
80 }
81
82
83 /**
84  * Generates a key for netbios name lookups on basis of
85  * netbios name and type.
86  * The caller must free returned key string when finished.
87  *
88  * @param name netbios name string (case insensitive)
89  * @param name_type netbios type of the name being looked up
90  *
91  * @return string consisted of uppercased name and appended
92  *         type number
93  */
94
95 static char* namecache_key(const char *name, int name_type)
96 {
97         char *keystr;
98         asprintf(&keystr, NBTKEY_FMT, strupper_static(name), name_type);
99
100         return keystr;
101 }
102
103
104 /**
105  * Store a name(s) in the name cache
106  *
107  * @param name netbios names array
108  * @param name_type integer netbios name type
109  * @param num_names number of names being stored
110  * @param ip_list array of in_addr structures containing
111  *        ip addresses being stored
112  **/
113
114 BOOL namecache_store(const char *name, int name_type,
115                      int num_names, struct ip_service *ip_list)
116 {
117         time_t expiry;
118         char *key, *value_string;
119         int i;
120         BOOL ret;
121
122         /*
123          * we use gecache call to avoid annoying debug messages about
124          * initialised namecache again and again...
125          */
126         if (!gencache_init()) return False;
127
128         if (name_type > 255) {
129                 return False; /* Don't store non-real name types. */
130         }
131
132         if ( DEBUGLEVEL >= 5 ) {
133                 DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
134                         num_names, num_names == 1 ? "": "es", name, name_type));
135
136                 for (i = 0; i < num_names; i++) 
137                         DEBUGADD(5, ("%s:%d%s", inet_ntoa(ip_list[i].ip), 
138                                 ip_list[i].port, (i == (num_names - 1) ? "" : ",")));
139                         
140                 DEBUGADD(5, ("\n"));
141         }
142         
143         key = namecache_key(name, name_type);
144         if (!key) {
145                 return False;
146         }
147
148         expiry = time(NULL) + lp_name_cache_timeout();
149
150         /*
151          * Generate string representation of ip addresses list
152          * First, store the number of ip addresses and then
153          * place each single ip
154          */
155         if (!ipstr_list_make(&value_string, ip_list, num_names)) {
156                 SAFE_FREE(key);
157                 SAFE_FREE(value_string);
158                 return False;
159         }
160         
161         /* set the entry */
162         ret = gencache_set(key, value_string, expiry);
163         SAFE_FREE(key);
164         SAFE_FREE(value_string);
165         return ret;
166 }
167
168
169 /**
170  * Look up a name in the cache.
171  *
172  * @param name netbios name to look up for
173  * @param name_type netbios name type of @param name
174  * @param ip_list mallocated list of IP addresses if found in the cache,
175  *        NULL otherwise
176  * @param num_names number of entries found
177  *
178  * @return true upon successful fetch or
179  *         false if name isn't found in the cache or has expired
180  **/
181
182 BOOL namecache_fetch(const char *name, int name_type, struct ip_service **ip_list,
183                      int *num_names)
184 {
185         char *key, *value;
186         time_t timeout;
187
188         /* exit now if null pointers were passed as they're required further */
189         if (!ip_list || !num_names) return False;
190
191         if (!gencache_init())
192                 return False;
193
194         if (name_type > 255) {
195                 return False; /* Don't fetch non-real name types. */
196         }
197
198         *num_names = 0;
199
200         /* 
201          * Use gencache interface - lookup the key
202          */
203         key = namecache_key(name, name_type);
204         if (!key) {
205                 return False;
206         }
207
208         if (!gencache_get(key, &value, &timeout)) {
209                 DEBUG(5, ("no entry for %s#%02X found.\n", name, name_type));
210                 SAFE_FREE(key);
211                 return False;
212         } else {
213                 DEBUG(5, ("name %s#%02X found.\n", name, name_type));
214         }
215         
216         /*
217          * Split up the stored value into the list of IP adresses
218          */
219         *num_names = ipstr_list_parse(value, ip_list);
220         
221         SAFE_FREE(key);
222         SAFE_FREE(value);
223                          
224         return *num_names > 0;          /* true only if some ip has been fetched */
225 }
226
227 /**
228  * Remove a namecache entry. Needed for site support.
229  *
230  **/
231
232 BOOL namecache_delete(const char *name, int name_type)
233 {
234         BOOL ret;
235         char *key;
236
237         if (!gencache_init())
238                 return False;
239
240         if (name_type > 255) {
241                 return False; /* Don't fetch non-real name types. */
242         }
243
244         key = namecache_key(name, name_type);
245         if (!key) {
246                 return False;
247         }
248         ret = gencache_del(key);
249         SAFE_FREE(key);
250         return ret;
251 }
252
253 /**
254  * Delete single namecache entry. Look at the
255  * gencache_iterate definition.
256  *
257  **/
258
259 static void flush_netbios_name(const char* key, const char *value, time_t timeout, void* dptr)
260 {
261         gencache_del(key);
262         DEBUG(5, ("Deleting entry %s\n", key));
263 }
264
265
266 /**
267  * Flush all names from the name cache.
268  * It's done by gencache_iterate()
269  *
270  * @return True upon successful deletion or
271  *         False in case of an error
272  **/
273
274 void namecache_flush(void)
275 {
276         if (!gencache_init())
277                 return;
278
279         /* 
280          * iterate through each NBT cache's entry and flush it
281          * by flush_netbios_name function
282          */
283         gencache_iterate(flush_netbios_name, NULL, "NBT/*");
284         DEBUG(5, ("Namecache flushed\n"));
285 }
286
287 /* Construct a name status record key. */
288
289 static char *namecache_status_record_key(const char *name, int name_type1,
290                                 int name_type2, struct in_addr keyip)
291 {
292         char *keystr;
293
294         asprintf(&keystr, "NBT/%s#%02X.%02X.%s",
295                         strupper_static(name), name_type1, name_type2, inet_ntoa(keyip));
296         return keystr;
297 }
298
299 /* Store a name status record. */
300
301 BOOL namecache_status_store(const char *keyname, int keyname_type,
302                 int name_type, struct in_addr keyip,
303                 const char *srvname)
304 {
305         char *key;
306         time_t expiry;
307         BOOL ret;
308
309         if (!gencache_init())
310                 return False;
311
312         key = namecache_status_record_key(keyname, keyname_type, name_type, keyip);
313         if (!key)
314                 return False;
315
316         expiry = time(NULL) + lp_name_cache_timeout();
317         ret = gencache_set(key, srvname, expiry);
318
319         if (ret)
320                 DEBUG(5, ("namecache_status_store: entry %s -> %s\n", key, srvname ));
321         else
322                 DEBUG(5, ("namecache_status_store: entry %s store failed.\n", key ));
323
324         SAFE_FREE(key);
325         return ret;
326 }
327
328 /* Fetch a name status record. */
329
330 BOOL namecache_status_fetch(const char *keyname, int keyname_type,
331                         int name_type, struct in_addr keyip, char *srvname_out)
332 {
333         char *key = NULL;
334         char *value = NULL;
335         time_t timeout;
336
337         if (!gencache_init())
338                 return False;
339
340         key = namecache_status_record_key(keyname, keyname_type, name_type, keyip);
341         if (!key)
342                 return False;
343
344         if (!gencache_get(key, &value, &timeout)) {
345                 DEBUG(5, ("namecache_status_fetch: no entry for %s found.\n", key));
346                 SAFE_FREE(key);
347                 return False;
348         } else {
349                 DEBUG(5, ("namecache_status_fetch: key %s -> %s\n", key, value ));
350         }
351
352         strlcpy(srvname_out, value, 16);
353         SAFE_FREE(key);
354         SAFE_FREE(value);
355         return True;
356 }