fix a few segfaults
[Samba.git] / source / libsmb / namecache.c
blobf05f76a22be2b994205e7a700693a145be85668e
1 /*
2 Unix SMB/CIFS implementation.
4 NetBIOS name cache module.
6 Copyright (C) Tim Potter, 2002
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.
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.
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.
23 #include "includes.h"
25 static BOOL done_namecache_init;
26 static BOOL enable_namecache;
27 static TDB_CONTEXT *namecache_tdb;
29 struct nc_value {
30 time_t expiry; /* When entry expires */
31 int count; /* Number of addresses */
32 struct in_addr ip_list[1]; /* Address list */
35 /* Initialise namecache system */
37 void namecache_enable(void)
39 /* Check if we have been here before, or name caching disabled
40 by setting the name cache timeout to zero. */
42 if (done_namecache_init)
43 return;
45 done_namecache_init = True;
47 if (lp_name_cache_timeout() == 0) {
48 DEBUG(5, ("namecache_init: disabling netbios name cache\n"));
49 return;
52 /* Open namecache tdb in read/write or readonly mode */
54 namecache_tdb = tdb_open_log(
55 lock_path("namecache.tdb"), 0,
56 TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
58 if (!namecache_tdb) {
59 DEBUG(5, ("namecache_init: could not open %s\n",
60 lock_path("namecache.tdb")));
61 return;
64 DEBUG(5, ("namecache_init: enabling netbios namecache, timeout %d "
65 "seconds\n", lp_name_cache_timeout()));
67 enable_namecache = True;
70 /* Return a key for a name and name type. The caller must free
71 retval.dptr when finished. */
73 static TDB_DATA namecache_key(const char *name, int name_type)
75 TDB_DATA retval;
76 char *keystr;
78 asprintf(&keystr, "%s#%02X", strupper_static(name), name_type);
80 retval.dsize = strlen(keystr) + 1;
81 retval.dptr = keystr;
83 return retval;
86 /* Return a data value for an IP list. The caller must free
87 retval.dptr when finished. */
89 static TDB_DATA namecache_value(struct in_addr *ip_list, int num_names,
90 time_t expiry)
92 TDB_DATA retval;
93 struct nc_value *value;
94 int size;
96 size = sizeof(struct nc_value) + sizeof(struct in_addr) *
97 (num_names-1);
99 value = (struct nc_value *)malloc(size);
101 value->expiry = expiry;
102 value->count = num_names;
104 memcpy(value->ip_list, ip_list, sizeof(*ip_list));
106 retval.dptr = (char *)value;
107 retval.dsize = size;
109 return retval;
112 /* Store a name in the name cache */
114 void namecache_store(const char *name, int name_type,
115 int num_names, struct in_addr *ip_list)
117 TDB_DATA key, value;
118 time_t expiry;
119 int i;
121 if (!enable_namecache)
122 return;
124 DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
125 num_names, num_names == 1 ? "": "es", name, name_type));
127 for (i = 0; i < num_names; i++)
128 DEBUGADD(5, ("%s%s", inet_ntoa(ip_list[i]),
129 i == (num_names - 1) ? "" : ", "));
131 DEBUGADD(5, ("\n"));
133 key = namecache_key(name, name_type);
135 /* Cache pdc location or dc lists for only a little while
136 otherwise if we lock on to a bad DC we can potentially be
137 out of action for the entire cache timeout time! */
139 if (name_type != 0x1b || name_type != 0x1c)
140 expiry = time(NULL) + 10;
141 else
142 expiry = time(NULL) + lp_name_cache_timeout();
144 value = namecache_value(ip_list, num_names, expiry);
146 tdb_store(namecache_tdb, key, value, TDB_REPLACE);
148 free(key.dptr);
149 free(value.dptr);
152 /* Look up a name in the name cache. Return a mallocated list of IP
153 addresses if the name is contained in the cache. */
155 BOOL namecache_fetch(const char *name, int name_type, struct in_addr **ip_list,
156 int *num_names)
158 TDB_DATA key, value;
159 struct nc_value *data;
160 time_t now;
161 int i;
163 if (!enable_namecache)
164 return False;
166 /* Read value */
168 key = namecache_key(name, name_type);
170 value = tdb_fetch(namecache_tdb, key);
172 if (!value.dptr) {
173 DEBUG(5, ("namecache_fetch: %s#%02x not found\n",
174 name, name_type));
175 goto done;
178 data = (struct nc_value *)value.dptr;
180 /* Check expiry time */
182 now = time(NULL);
184 if (now > data->expiry) {
186 DEBUG(5, ("namecache_fetch: entry for %s#%02x expired\n",
187 name, name_type));
189 tdb_delete(namecache_tdb, key);
191 value = tdb_null;
193 goto done;
196 if ((data->expiry - now) > lp_name_cache_timeout()) {
198 /* Someone may have changed the system time on us */
200 DEBUG(5, ("namecache_fetch: entry for %s#%02x has bad expiry\n",
201 name, name_type));
203 tdb_delete(namecache_tdb, key);
205 value = tdb_null;
207 goto done;
210 /* Extract and return namelist */
212 *ip_list = (struct in_addr *)malloc(
213 sizeof(struct in_addr) * (data->count-1));
215 memcpy(*ip_list, data->ip_list, sizeof(struct in_addr) *
216 (data->count-1));
218 *num_names = data->count;
220 DEBUG(5, ("namecache_fetch: returning %d address%s for %s#%02x: ",
221 *num_names, *num_names == 1 ? "" : "es", name, name_type));
223 for (i = 0; i < *num_names; i++)
224 DEBUGADD(5, ("%s%s", inet_ntoa((*ip_list)[i]),
225 i == (*num_names - 1) ? "" : ", "));
227 DEBUGADD(5, ("\n"));
229 done:
230 SAFE_FREE(key.dptr);
231 SAFE_FREE(value.dptr);
233 return value.dsize > 0;
236 /* Flush all names from the name cache */
238 void namecache_flush(void)
240 int result;
242 if (!namecache_tdb)
243 return;
245 result = tdb_traverse(namecache_tdb, tdb_traverse_delete_fn, NULL);
247 if (result == -1)
248 DEBUG(5, ("namecache_flush: error deleting cache entries\n"));
249 else
250 DEBUG(5, ("namecache_flush: deleted %d cache entr%s\n",
251 result, result == 1 ? "y" : "ies"));