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