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