-/*
+/*
Unix SMB/CIFS implementation.
- NetBIOS name cache module.
+ NetBIOS name cache module on top of gencache mechanism.
+
+ Copyright (C) Tim Potter 2002
+ Copyright (C) Rafal Szczesniak 2002
+ Copyright (C) Jeremy Allison 2007
- Copyright (C) Tim Potter, 2002
-
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
-
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-
+
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
-static BOOL done_namecache_init;
-static BOOL enable_namecache;
-static TDB_CONTEXT *namecache_tdb;
+#define NBTKEY_FMT "NBT/%s#%02X"
-struct nc_value {
- time_t expiry; /* When entry expires */
- int count; /* Number of addresses */
- struct in_addr ip_list[1]; /* Address list */
-};
+/**
+ * Initialise namecache system. Function calls gencache
+ * initialisation function to perform necessary actions
+ *
+ * @return true upon successful initialisation of the cache or
+ * false on failure
+ **/
-/* Initialise namecache system */
-
-BOOL namecache_enable(void)
+bool namecache_enable(void)
{
- /* Check if we have been here before, or name caching disabled
- by setting the name cache timeout to zero. */
-
- if (done_namecache_init)
- return False;
-
- done_namecache_init = True;
+ /*
+ * Check if name caching disabled by setting the name cache
+ * timeout to zero.
+ */
if (lp_name_cache_timeout() == 0) {
- DEBUG(5, ("namecache_init: disabling netbios name cache\n"));
+ DEBUG(5, ("namecache_enable: disabling netbios name cache\n"));
return False;
}
- /* Open namecache tdb in read/write or readonly mode */
-
- namecache_tdb = tdb_open_log(
- lock_path("namecache.tdb"), 0,
- TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
+ /* Init namecache by calling gencache initialisation */
- if (!namecache_tdb) {
- DEBUG(5, ("namecache_init: could not open %s\n",
- lock_path("namecache.tdb")));
+ if (!gencache_init()) {
+ DEBUG(2, ("namecache_enable: "
+ "Couldn't initialise namecache on top of gencache.\n"));
return False;
}
- DEBUG(5, ("namecache_init: enabling netbios namecache, timeout %d "
+ /* I leave it for now, though I don't think we really
+ * need this (mimir, 27.09.2002) */
+ DEBUG(5, ("namecache_enable: enabling netbios namecache, timeout %d "
"seconds\n", lp_name_cache_timeout()));
- enable_namecache = True;
-
return True;
}
-/* Return a key for a name and name type. The caller must free
- retval.dptr when finished. */
+/**
+ * Shutdown namecache. Routine calls gencache close function
+ * to safely close gencache file.
+ *
+ * @return true upon successful shutdown of the cache or
+ * false on failure
+ **/
-static TDB_DATA namecache_key(const char *name, int name_type)
+bool namecache_shutdown(void)
{
- TDB_DATA retval;
- char *keystr;
-
- asprintf(&keystr, "%s#%02X", strupper_static(name), name_type);
-
- retval.dsize = strlen(keystr) + 1;
- retval.dptr = keystr;
+ if (!gencache_shutdown()) {
+ DEBUG(2, ("namecache_shutdown: "
+ "Couldn't close namecache on top of gencache.\n"));
+ return False;
+ }
- return retval;
+ DEBUG(5, ("namecache_shutdown: "
+ "netbios namecache closed successfully.\n"));
+ return True;
}
-/* Return a data value for an IP list. The caller must free
- retval.dptr when finished. */
-
-static TDB_DATA namecache_value(struct in_addr *ip_list, int num_names,
- time_t expiry)
+/**
+ * Generates a key for netbios name lookups on basis of
+ * netbios name and type.
+ * The caller must free returned key string when finished.
+ *
+ * @param name netbios name string (case insensitive)
+ * @param name_type netbios type of the name being looked up
+ *
+ * @return string consisted of uppercased name and appended
+ * type number
+ */
+
+static char* namecache_key(const char *name,
+ int name_type)
{
- TDB_DATA retval;
- struct nc_value *value;
- int size = sizeof(struct nc_value);
-
- if (num_names > 0)
- size += sizeof(struct in_addr) * (num_names-1);
-
- value = (struct nc_value *)malloc(size);
-
- memset(value, 0, size);
-
- value->expiry = expiry;
- value->count = num_names;
-
- if (ip_list)
- memcpy(value->ip_list, ip_list, sizeof(struct in_addr) * num_names);
-
- retval.dptr = (char *)value;
- retval.dsize = size;
+ char *keystr;
+ asprintf_strupper_m(&keystr, NBTKEY_FMT, name, name_type);
- return retval;
+ return keystr;
}
-/* Store a name in the name cache */
-
-void namecache_store(const char *name, int name_type,
- int num_names, struct in_addr *ip_list)
+/**
+ * Store a name(s) in the name cache
+ *
+ * @param name netbios names array
+ * @param name_type integer netbios name type
+ * @param num_names number of names being stored
+ * @param ip_list array of in_addr structures containing
+ * ip addresses being stored
+ **/
+
+bool namecache_store(const char *name,
+ int name_type,
+ int num_names,
+ struct ip_service *ip_list)
{
- TDB_DATA key, value;
time_t expiry;
+ char *key, *value_string;
int i;
+ bool ret;
- if (!enable_namecache)
- return;
-
- DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
- num_names, num_names == 1 ? "": "es", name, name_type));
+ /*
+ * we use gecache call to avoid annoying debug messages about
+ * initialised namecache again and again...
+ */
+ if (!gencache_init()) {
+ return False;
+ }
- for (i = 0; i < num_names; i++)
- DEBUGADD(5, ("%s%s", inet_ntoa(ip_list[i]),
- i == (num_names - 1) ? "" : ", "));
+ if (name_type > 255) {
+ return False; /* Don't store non-real name types. */
+ }
- DEBUGADD(5, ("\n"));
+ if ( DEBUGLEVEL >= 5 ) {
+ TALLOC_CTX *ctx = talloc_stackframe();
+ char *addr = NULL;
+
+ DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
+ num_names, num_names == 1 ? "": "es", name, name_type));
+
+ for (i = 0; i < num_names; i++) {
+ addr = print_canonical_sockaddr(ctx,
+ &ip_list[i].ss);
+ if (!addr) {
+ continue;
+ }
+ DEBUGADD(5, ("%s%s", addr,
+ (i == (num_names - 1) ? "" : ",")));
+
+ }
+ DEBUGADD(5, ("\n"));
+ TALLOC_FREE(ctx);
+ }
key = namecache_key(name, name_type);
+ if (!key) {
+ return False;
+ }
- /* Cache pdc location or dc lists for only a little while
- otherwise if we lock on to a bad DC we can potentially be
- out of action for the entire cache timeout time! */
-
- if (name_type != 0x1b || name_type != 0x1c)
- expiry = time(NULL) + 10;
- else
- expiry = time(NULL) + lp_name_cache_timeout();
-
- value = namecache_value(ip_list, num_names, expiry);
-
- tdb_store(namecache_tdb, key, value, TDB_REPLACE);
+ expiry = time(NULL) + lp_name_cache_timeout();
+
+ /*
+ * Generate string representation of ip addresses list
+ * First, store the number of ip addresses and then
+ * place each single ip
+ */
+ if (!ipstr_list_make(&value_string, ip_list, num_names)) {
+ SAFE_FREE(key);
+ SAFE_FREE(value_string);
+ return false;
+ }
- free(key.dptr);
- free(value.dptr);
+ /* set the entry */
+ ret = gencache_set(key, value_string, expiry);
+ SAFE_FREE(key);
+ SAFE_FREE(value_string);
+ return ret;
}
-/* Look up a name in the name cache. Return a mallocated list of IP
- addresses if the name is contained in the cache. */
-
-BOOL namecache_fetch(const char *name, int name_type, struct in_addr **ip_list,
- int *num_names)
+/**
+ * Look up a name in the cache.
+ *
+ * @param name netbios name to look up for
+ * @param name_type netbios name type of @param name
+ * @param ip_list mallocated list of IP addresses if found in the cache,
+ * NULL otherwise
+ * @param num_names number of entries found
+ *
+ * @return true upon successful fetch or
+ * false if name isn't found in the cache or has expired
+ **/
+
+bool namecache_fetch(const char *name,
+ int name_type,
+ struct ip_service **ip_list,
+ int *num_names)
{
- TDB_DATA key, value;
- struct nc_value *data;
- time_t now;
- int i;
+ char *key, *value;
+ time_t timeout;
- *ip_list = NULL;
- *num_names = 0;
+ /* exit now if null pointers were passed as they're required further */
+ if (!ip_list || !num_names) {
+ return False;
+ }
- if (!enable_namecache)
+ if (!gencache_init()) {
return False;
+ }
- /* Read value */
+ if (name_type > 255) {
+ return False; /* Don't fetch non-real name types. */
+ }
+
+ *num_names = 0;
+ /*
+ * Use gencache interface - lookup the key
+ */
key = namecache_key(name, name_type);
+ if (!key) {
+ return False;
+ }
- value = tdb_fetch(namecache_tdb, key);
-
- if (!value.dptr) {
- DEBUG(5, ("namecache_fetch: %s#%02x not found\n",
- name, name_type));
- goto done;
+ if (!gencache_get(key, &value, &timeout)) {
+ DEBUG(5, ("no entry for %s#%02X found.\n", name, name_type));
+ SAFE_FREE(key);
+ return False;
+ } else {
+ DEBUG(5, ("name %s#%02X found.\n", name, name_type));
}
- data = (struct nc_value *)value.dptr;
+ /*
+ * Split up the stored value into the list of IP adresses
+ */
+ *num_names = ipstr_list_parse(value, ip_list);
- /* Check expiry time */
+ SAFE_FREE(key);
+ SAFE_FREE(value);
- now = time(NULL);
+ return *num_names > 0; /* true only if some ip has been fetched */
+}
- if (now > data->expiry) {
+/**
+ * Remove a namecache entry. Needed for site support.
+ *
+ **/
- DEBUG(5, ("namecache_fetch: entry for %s#%02x expired\n",
- name, name_type));
+bool namecache_delete(const char *name, int name_type)
+{
+ bool ret;
+ char *key;
- tdb_delete(namecache_tdb, key);
+ if (!gencache_init())
+ return False;
- SAFE_FREE(value.dptr);
- value = tdb_null;
+ if (name_type > 255) {
+ return False; /* Don't fetch non-real name types. */
+ }
- goto done;
+ key = namecache_key(name, name_type);
+ if (!key) {
+ return False;
}
+ ret = gencache_del(key);
+ SAFE_FREE(key);
+ return ret;
+}
- if ((data->expiry - now) > lp_name_cache_timeout()) {
+/**
+ * Delete single namecache entry. Look at the
+ * gencache_iterate definition.
+ *
+ **/
- /* Someone may have changed the system time on us */
+static void flush_netbios_name(const char *key,
+ const char *value,
+ time_t timeout,
+ void *dptr)
+{
+ gencache_del(key);
+ DEBUG(5, ("Deleting entry %s\n", key));
+}
- DEBUG(5, ("namecache_fetch: entry for %s#%02x has bad expiry\n",
- name, name_type));
+/**
+ * Flush all names from the name cache.
+ * It's done by gencache_iterate()
+ *
+ * @return true upon successful deletion or
+ * false in case of an error
+ **/
- tdb_delete(namecache_tdb, key);
+void namecache_flush(void)
+{
+ if (!gencache_init()) {
+ return;
+ }
- SAFE_FREE(value.dptr);
- value = tdb_null;
+ /*
+ * iterate through each NBT cache's entry and flush it
+ * by flush_netbios_name function
+ */
+ gencache_iterate(flush_netbios_name, NULL, "NBT/*");
+ DEBUG(5, ("Namecache flushed\n"));
+}
- goto done;
- }
+/* Construct a name status record key. */
- /* Extract and return namelist */
+static char *namecache_status_record_key(const char *name,
+ int name_type1,
+ int name_type2,
+ const struct sockaddr_storage *keyip)
+{
+ char addr[INET6_ADDRSTRLEN];
+ char *keystr;
- DEBUG(5, ("namecache_fetch: returning %d address%s for %s#%02x: ",
- data->count, data->count == 1 ? "" : "es", name, name_type));
+ print_sockaddr(addr, sizeof(addr), keyip);
+ asprintf_strupper_m(&keystr, "NBT/%s#%02X.%02X.%s", name,
+ name_type1, name_type2, addr);
+ return keystr;
+}
- if (data->count) {
+/* Store a name status record. */
- *ip_list = (struct in_addr *)malloc(
- sizeof(struct in_addr) * data->count);
-
- memcpy(*ip_list, data->ip_list, sizeof(struct in_addr) * data->count);
-
- *num_names = data->count;
-
- for (i = 0; i < *num_names; i++)
- DEBUGADD(5, ("%s%s", inet_ntoa((*ip_list)[i]),
- i == (*num_names - 1) ? "" : ", "));
+bool namecache_status_store(const char *keyname, int keyname_type,
+ int name_type, const struct sockaddr_storage *keyip,
+ const char *srvname)
+{
+ char *key;
+ time_t expiry;
+ bool ret;
+ if (!gencache_init()) {
+ return False;
}
- DEBUGADD(5, ("\n"));
+ key = namecache_status_record_key(keyname, keyname_type,
+ name_type, keyip);
+ if (!key)
+ return False;
+
+ expiry = time(NULL) + lp_name_cache_timeout();
+ ret = gencache_set(key, srvname, expiry);
-done:
- SAFE_FREE(key.dptr);
- SAFE_FREE(value.dptr);
+ if (ret) {
+ DEBUG(5, ("namecache_status_store: entry %s -> %s\n",
+ key, srvname ));
+ } else {
+ DEBUG(5, ("namecache_status_store: entry %s store failed.\n",
+ key ));
+ }
- return value.dsize > 0;
+ SAFE_FREE(key);
+ return ret;
}
-/* Flush all names from the name cache */
+/* Fetch a name status record. */
-void namecache_flush(void)
+bool namecache_status_fetch(const char *keyname,
+ int keyname_type,
+ int name_type,
+ const struct sockaddr_storage *keyip,
+ char *srvname_out)
{
- int result;
+ char *key = NULL;
+ char *value = NULL;
+ time_t timeout;
- if (!namecache_tdb)
- return;
+ if (!gencache_init())
+ return False;
- result = tdb_traverse(namecache_tdb, tdb_traverse_delete_fn, NULL);
+ key = namecache_status_record_key(keyname, keyname_type,
+ name_type, keyip);
+ if (!key)
+ return False;
- if (result == -1)
- DEBUG(5, ("namecache_flush: error deleting cache entries\n"));
- else
- DEBUG(5, ("namecache_flush: deleted %d cache entr%s\n",
- result, result == 1 ? "y" : "ies"));
+ if (!gencache_get(key, &value, &timeout)) {
+ DEBUG(5, ("namecache_status_fetch: no entry for %s found.\n",
+ key));
+ SAFE_FREE(key);
+ return False;
+ } else {
+ DEBUG(5, ("namecache_status_fetch: key %s -> %s\n",
+ key, value ));
+ }
+
+ strlcpy(srvname_out, value, 16);
+ SAFE_FREE(key);
+ SAFE_FREE(value);
+ return True;
}