[745] | 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 | * Generates a key for netbios name lookups on basis of
|
---|
| 30 | * netbios name and type.
|
---|
| 31 | * The caller must free returned key string when finished.
|
---|
| 32 | *
|
---|
| 33 | * @param name netbios name string (case insensitive)
|
---|
| 34 | * @param name_type netbios type of the name being looked up
|
---|
| 35 | *
|
---|
| 36 | * @return string consisted of uppercased name and appended
|
---|
| 37 | * type number
|
---|
| 38 | */
|
---|
| 39 |
|
---|
| 40 | static char* namecache_key(const char *name,
|
---|
| 41 | int name_type)
|
---|
| 42 | {
|
---|
| 43 | char *keystr;
|
---|
| 44 | asprintf_strupper_m(&keystr, NBTKEY_FMT, name, name_type);
|
---|
| 45 |
|
---|
| 46 | return keystr;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Store a name(s) in the name cache
|
---|
| 51 | *
|
---|
| 52 | * @param name netbios names array
|
---|
| 53 | * @param name_type integer netbios name type
|
---|
| 54 | * @param num_names number of names being stored
|
---|
| 55 | * @param ip_list array of in_addr structures containing
|
---|
| 56 | * ip addresses being stored
|
---|
| 57 | **/
|
---|
| 58 |
|
---|
| 59 | bool namecache_store(const char *name,
|
---|
| 60 | int name_type,
|
---|
| 61 | int num_names,
|
---|
| 62 | struct ip_service *ip_list)
|
---|
| 63 | {
|
---|
| 64 | time_t expiry;
|
---|
| 65 | char *key, *value_string;
|
---|
| 66 | int i;
|
---|
| 67 | bool ret;
|
---|
| 68 |
|
---|
| 69 | if (name_type > 255) {
|
---|
| 70 | return False; /* Don't store non-real name types. */
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | if ( DEBUGLEVEL >= 5 ) {
|
---|
| 74 | TALLOC_CTX *ctx = talloc_stackframe();
|
---|
| 75 | char *addr = NULL;
|
---|
| 76 |
|
---|
| 77 | DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
|
---|
| 78 | num_names, num_names == 1 ? "": "es", name, name_type));
|
---|
| 79 |
|
---|
| 80 | for (i = 0; i < num_names; i++) {
|
---|
| 81 | addr = print_canonical_sockaddr(ctx,
|
---|
| 82 | &ip_list[i].ss);
|
---|
| 83 | if (!addr) {
|
---|
| 84 | continue;
|
---|
| 85 | }
|
---|
| 86 | DEBUGADD(5, ("%s%s", addr,
|
---|
| 87 | (i == (num_names - 1) ? "" : ",")));
|
---|
| 88 |
|
---|
| 89 | }
|
---|
| 90 | DEBUGADD(5, ("\n"));
|
---|
| 91 | TALLOC_FREE(ctx);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | key = namecache_key(name, name_type);
|
---|
| 95 | if (!key) {
|
---|
| 96 | return False;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | expiry = time(NULL) + lp_name_cache_timeout();
|
---|
| 100 |
|
---|
| 101 | /*
|
---|
| 102 | * Generate string representation of ip addresses list
|
---|
| 103 | * First, store the number of ip addresses and then
|
---|
| 104 | * place each single ip
|
---|
| 105 | */
|
---|
| 106 | if (!ipstr_list_make(&value_string, ip_list, num_names)) {
|
---|
| 107 | SAFE_FREE(key);
|
---|
| 108 | SAFE_FREE(value_string);
|
---|
| 109 | return false;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /* set the entry */
|
---|
| 113 | ret = gencache_set(key, value_string, expiry);
|
---|
| 114 | SAFE_FREE(key);
|
---|
| 115 | SAFE_FREE(value_string);
|
---|
| 116 | return ret;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Look up a name in the cache.
|
---|
| 121 | *
|
---|
| 122 | * @param name netbios name to look up for
|
---|
| 123 | * @param name_type netbios name type of @param name
|
---|
| 124 | * @param ip_list mallocated list of IP addresses if found in the cache,
|
---|
| 125 | * NULL otherwise
|
---|
| 126 | * @param num_names number of entries found
|
---|
| 127 | *
|
---|
| 128 | * @return true upon successful fetch or
|
---|
| 129 | * false if name isn't found in the cache or has expired
|
---|
| 130 | **/
|
---|
| 131 |
|
---|
| 132 | bool namecache_fetch(const char *name,
|
---|
| 133 | int name_type,
|
---|
| 134 | struct ip_service **ip_list,
|
---|
| 135 | int *num_names)
|
---|
| 136 | {
|
---|
| 137 | char *key, *value;
|
---|
| 138 | time_t timeout;
|
---|
| 139 |
|
---|
| 140 | /* exit now if null pointers were passed as they're required further */
|
---|
| 141 | if (!ip_list || !num_names) {
|
---|
| 142 | return False;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (name_type > 255) {
|
---|
| 146 | return False; /* Don't fetch non-real name types. */
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | *num_names = 0;
|
---|
| 150 |
|
---|
| 151 | /*
|
---|
| 152 | * Use gencache interface - lookup the key
|
---|
| 153 | */
|
---|
| 154 | key = namecache_key(name, name_type);
|
---|
| 155 | if (!key) {
|
---|
| 156 | return False;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | if (!gencache_get(key, &value, &timeout)) {
|
---|
| 160 | DEBUG(5, ("no entry for %s#%02X found.\n", name, name_type));
|
---|
| 161 | SAFE_FREE(key);
|
---|
| 162 | return False;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | DEBUG(5, ("name %s#%02X found.\n", name, name_type));
|
---|
| 166 |
|
---|
| 167 | /*
|
---|
| 168 | * Split up the stored value into the list of IP adresses
|
---|
| 169 | */
|
---|
| 170 | *num_names = ipstr_list_parse(value, ip_list);
|
---|
| 171 |
|
---|
| 172 | SAFE_FREE(key);
|
---|
| 173 | SAFE_FREE(value);
|
---|
| 174 |
|
---|
| 175 | return *num_names > 0; /* true only if some ip has been fetched */
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * Remove a namecache entry. Needed for site support.
|
---|
| 180 | *
|
---|
| 181 | **/
|
---|
| 182 |
|
---|
| 183 | bool namecache_delete(const char *name, int name_type)
|
---|
| 184 | {
|
---|
| 185 | bool ret;
|
---|
| 186 | char *key;
|
---|
| 187 |
|
---|
| 188 | if (name_type > 255) {
|
---|
| 189 | return False; /* Don't fetch non-real name types. */
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | key = namecache_key(name, name_type);
|
---|
| 193 | if (!key) {
|
---|
| 194 | return False;
|
---|
| 195 | }
|
---|
| 196 | ret = gencache_del(key);
|
---|
| 197 | SAFE_FREE(key);
|
---|
| 198 | return ret;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /**
|
---|
| 202 | * Delete single namecache entry. Look at the
|
---|
| 203 | * gencache_iterate definition.
|
---|
| 204 | *
|
---|
| 205 | **/
|
---|
| 206 |
|
---|
| 207 | static void flush_netbios_name(const char *key,
|
---|
| 208 | const char *value,
|
---|
| 209 | time_t timeout,
|
---|
| 210 | void *dptr)
|
---|
| 211 | {
|
---|
| 212 | gencache_del(key);
|
---|
| 213 | DEBUG(5, ("Deleting entry %s\n", key));
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /**
|
---|
| 217 | * Flush all names from the name cache.
|
---|
| 218 | * It's done by gencache_iterate()
|
---|
| 219 | *
|
---|
| 220 | * @return true upon successful deletion or
|
---|
| 221 | * false in case of an error
|
---|
| 222 | **/
|
---|
| 223 |
|
---|
| 224 | void namecache_flush(void)
|
---|
| 225 | {
|
---|
| 226 | /*
|
---|
| 227 | * iterate through each NBT cache's entry and flush it
|
---|
| 228 | * by flush_netbios_name function
|
---|
| 229 | */
|
---|
| 230 | gencache_iterate(flush_netbios_name, NULL, "NBT/*");
|
---|
| 231 | DEBUG(5, ("Namecache flushed\n"));
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /* Construct a name status record key. */
|
---|
| 235 |
|
---|
| 236 | static char *namecache_status_record_key(const char *name,
|
---|
| 237 | int name_type1,
|
---|
| 238 | int name_type2,
|
---|
| 239 | const struct sockaddr_storage *keyip)
|
---|
| 240 | {
|
---|
| 241 | char addr[INET6_ADDRSTRLEN];
|
---|
| 242 | char *keystr;
|
---|
| 243 |
|
---|
| 244 | print_sockaddr(addr, sizeof(addr), keyip);
|
---|
| 245 | asprintf_strupper_m(&keystr, "NBT/%s#%02X.%02X.%s", name,
|
---|
| 246 | name_type1, name_type2, addr);
|
---|
| 247 | return keystr;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /* Store a name status record. */
|
---|
| 251 |
|
---|
| 252 | bool namecache_status_store(const char *keyname, int keyname_type,
|
---|
| 253 | int name_type, const struct sockaddr_storage *keyip,
|
---|
| 254 | const char *srvname)
|
---|
| 255 | {
|
---|
| 256 | char *key;
|
---|
| 257 | time_t expiry;
|
---|
| 258 | bool ret;
|
---|
| 259 |
|
---|
| 260 | key = namecache_status_record_key(keyname, keyname_type,
|
---|
| 261 | name_type, keyip);
|
---|
| 262 | if (!key)
|
---|
| 263 | return False;
|
---|
| 264 |
|
---|
| 265 | expiry = time(NULL) + lp_name_cache_timeout();
|
---|
| 266 | ret = gencache_set(key, srvname, expiry);
|
---|
| 267 |
|
---|
| 268 | if (ret) {
|
---|
| 269 | DEBUG(5, ("namecache_status_store: entry %s -> %s\n",
|
---|
| 270 | key, srvname ));
|
---|
| 271 | } else {
|
---|
| 272 | DEBUG(5, ("namecache_status_store: entry %s store failed.\n",
|
---|
| 273 | key ));
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | SAFE_FREE(key);
|
---|
| 277 | return ret;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /* Fetch a name status record. */
|
---|
| 281 |
|
---|
| 282 | bool namecache_status_fetch(const char *keyname,
|
---|
| 283 | int keyname_type,
|
---|
| 284 | int name_type,
|
---|
| 285 | const struct sockaddr_storage *keyip,
|
---|
| 286 | char *srvname_out)
|
---|
| 287 | {
|
---|
| 288 | char *key = NULL;
|
---|
| 289 | char *value = NULL;
|
---|
| 290 | time_t timeout;
|
---|
| 291 |
|
---|
| 292 | key = namecache_status_record_key(keyname, keyname_type,
|
---|
| 293 | name_type, keyip);
|
---|
| 294 | if (!key)
|
---|
| 295 | return False;
|
---|
| 296 |
|
---|
| 297 | if (!gencache_get(key, &value, &timeout)) {
|
---|
| 298 | DEBUG(5, ("namecache_status_fetch: no entry for %s found.\n",
|
---|
| 299 | key));
|
---|
| 300 | SAFE_FREE(key);
|
---|
| 301 | return False;
|
---|
| 302 | } else {
|
---|
| 303 | DEBUG(5, ("namecache_status_fetch: key %s -> %s\n",
|
---|
| 304 | key, value ));
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | strlcpy(srvname_out, value, 16);
|
---|
| 308 | SAFE_FREE(key);
|
---|
| 309 | SAFE_FREE(value);
|
---|
| 310 | return True;
|
---|
| 311 | }
|
---|