r25026: Move param/param.h out of includes.h
[gd/samba-autobuild/.git] / source4 / nbt_server / wins / winsdb.c
index ff3c787eb036f64abdb9de274d60859e781fcc18..cfaee05020ddfafc96c6cce54d6e1435e0534860 100644 (file)
@@ -4,10 +4,11 @@
    WINS database routines
 
    Copyright (C) Andrew Tridgell       2005
-   
+   Copyright (C) Stefan Metzmacher     2005
+      
    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,
@@ -16,8 +17,7 @@
    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"
 #include "nbt_server/wins/winsdb.h"
 #include "lib/ldb/include/ldb.h"
 #include "lib/ldb/include/ldb_errors.h"
-#include "db_wrap.h"
+#include "librpc/gen_ndr/ndr_nbt.h"
 #include "system/time.h"
+#include "db_wrap.h"
+#include "system/network.h"
+#include "lib/socket/netif.h"
+#include "param/param.h"
+
+uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
+{
+       int ret;
+       struct ldb_context *ldb = h->ldb;
+       struct ldb_dn *dn;
+       struct ldb_result *res = NULL;
+       TALLOC_CTX *tmp_ctx = talloc_new(ldb);
+       uint64_t maxVersion = 0;
+
+       dn = ldb_dn_new(tmp_ctx, ldb, "CN=VERSION");
+       if (!dn) goto failed;
+
+       /* find the record in the WINS database */
+       ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, 
+                        NULL, NULL, &res);
+       if (ret != LDB_SUCCESS) goto failed;
+       talloc_steal(tmp_ctx, res);
+       if (res->count > 1) goto failed;
+
+       if (res->count == 1) {
+               maxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
+       }
+
+failed:
+       talloc_free(tmp_ctx);
+       return maxVersion;
+}
 
 /*
-  return the new maxVersion and save it
+ if newVersion == 0 return the old maxVersion + 1 and save it
+ if newVersion > 0 return MAX(oldMaxVersion, newMaxVersion) and save it
 */
-static uint64_t winsdb_allocate_version(struct wins_server *winssrv)
+uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion)
 {
+       int trans;
        int ret;
-       struct ldb_context *ldb = winssrv->wins_db;
        struct ldb_dn *dn;
-       struct ldb_message **res = NULL;
+       struct ldb_result *res = NULL;
        struct ldb_message *msg = NULL;
-       TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
-       uint64_t maxVersion = 0;
+       struct ldb_context *wins_db = h->ldb;
+       TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
+       uint64_t oldMaxVersion = 0;
+
+       trans = ldb_transaction_start(wins_db);
+       if (trans != LDB_SUCCESS) goto failed;
 
-       dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
+       dn = ldb_dn_new(tmp_ctx, wins_db, "CN=VERSION");
        if (!dn) goto failed;
 
-       ret |= ldb_msg_add_string(msg, "objectClass", "winsEntry");
-       ret |= ldb_msg_add_fmt(msg, "minVersion", "%llu", winssrv->min_version);
-       ret |= ldb_msg_add_fmt(msg, "maxVersion", "%llu", winssrv->max_version);
-       if (ret != 0) goto failed;
+       /* find the record in the WINS database */
+       ret = ldb_search(wins_db, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
+
+       if (ret != LDB_SUCCESS) goto failed;
+       talloc_steal(tmp_ctx, res);
+       if (res->count > 1) goto failed;
 
-       if (ret == 1) {
-               maxVersion = ldb_msg_find_uint64(res[0], "maxVersion", 0);
+       talloc_steal(tmp_ctx, res);
+
+       if (res->count == 1) {
+               oldMaxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
+       }
+
+       if (newMaxVersion == 0) {
+               newMaxVersion = oldMaxVersion + 1;
+       } else {
+               newMaxVersion = MAX(oldMaxVersion, newMaxVersion);
        }
-       maxVersion++;
 
        msg = ldb_msg_new(tmp_ctx);
        if (!msg) goto failed;
        msg->dn = dn;
 
 
-       ret = ldb_msg_add_empty(ldb, msg, "maxVersion", LDB_FLAG_MOD_REPLACE);
+       ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
+       if (ret != 0) goto failed;
+       ret = ldb_msg_add_string(msg, "objectClass", "winsMaxVersion");
        if (ret != 0) goto failed;
-       ret = ldb_msg_add_fmt(ldb, msg, "maxVersion", "%llu", maxVersion);
+       ret = ldb_msg_add_empty(msg, "maxVersion", LDB_FLAG_MOD_REPLACE, NULL);
+       if (ret != 0) goto failed;
+       ret = ldb_msg_add_fmt(msg, "maxVersion", "%llu", (long long)newMaxVersion);
        if (ret != 0) goto failed;
 
-       ret = ldb_modify(ldb, msg);
-       if (ret != 0) ret = ldb_add(ldb, msg);
+       ret = ldb_modify(wins_db, msg);
+       if (ret != 0) ret = ldb_add(wins_db, msg);
        if (ret != 0) goto failed;
 
+       trans = ldb_transaction_commit(wins_db);
+       if (trans != LDB_SUCCESS) goto failed;
+
        talloc_free(tmp_ctx);
-       return maxVersion;
+       return newMaxVersion;
 
 failed:
+       if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
        talloc_free(tmp_ctx);
        return 0;
 }
 
+uint64_t winsdb_get_seqnumber(struct winsdb_handle *h)
+{
+       int ret;
+       struct ldb_context *ldb = h->ldb;
+       struct ldb_dn *dn;
+       struct ldb_result *res = NULL;
+       TALLOC_CTX *tmp_ctx = talloc_new(ldb);
+       uint64_t seqnumber = 0;
+
+       dn = ldb_dn_new(tmp_ctx, ldb, "@BASEINFO");
+       if (!dn) goto failed;
+
+       /* find the record in the WINS database */
+       ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, 
+                        NULL, NULL, &res);
+       if (ret != LDB_SUCCESS) goto failed;
+       talloc_steal(tmp_ctx, res);
+       if (res->count > 1) goto failed;
+
+       if (res->count == 1) {
+               seqnumber = ldb_msg_find_attr_as_uint64(res->msgs[0], "sequenceNumber", 0);
+       }
+
+failed:
+       talloc_free(tmp_ctx);
+       return seqnumber;
+}
+
 /*
   return a DN for a nbt_name
 */
-static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
+static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct nbt_name *name)
 {
        struct ldb_dn *dn;
 
-       dn = ldb_dn_string_compose(mem_ctx, NULL, "type=0x%02X", name->type);
-       if (dn && name->name && *name->name) {
-               dn = ldb_dn_string_compose(mem_ctx, dn, "name=%s", name->name);
+       dn = ldb_dn_new_fmt(mem_ctx, ldb, "type=0x%02X", name->type);
+       if (ldb_dn_is_valid(dn) && name->name && *name->name) {
+               ldb_dn_add_child_fmt(dn, "name=%s", name->name);
        }
-       if (dn && name->scope && *name->scope) {
-               dn = ldb_dn_string_compose(mem_ctx, dn, "scope=%s", name->scope);
+       if (ldb_dn_is_valid(dn) && name->scope && *name->scope) {
+               ldb_dn_add_child_fmt(dn, "scope=%s", name->scope);
        }
        return dn;
 }
@@ -97,6 +179,7 @@ static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct n
 {
        NTSTATUS status;
        struct nbt_name *name;
+       unsigned int comp_num;
        uint32_t cur = 0;
 
        name = talloc(mem_ctx, struct nbt_name);
@@ -105,20 +188,22 @@ static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct n
                goto failed;
        }
 
-       if (dn->comp_num > 3) {
+       comp_num = ldb_dn_get_comp_num(dn);
+
+       if (comp_num > 3) {
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
                goto failed;
        }
 
-       if (dn->comp_num > cur && strcasecmp("scope", dn->components[cur].name) == 0) {
-               name->scope     = talloc_steal(name, dn->components[cur].value.data);
+       if (comp_num > cur && strcasecmp("scope", ldb_dn_get_component_name(dn, cur)) == 0) {
+               name->scope     = (const char *)talloc_strdup(name, (char *)ldb_dn_get_component_val(dn, cur)->data);
                cur++;
        } else {
                name->scope     = NULL;
        }
 
-       if (dn->comp_num > cur && strcasecmp("name", dn->components[cur].name) == 0) {
-               name->name      = talloc_steal(name, dn->components[cur].value.data);
+       if (comp_num > cur && strcasecmp("name", ldb_dn_get_component_name(dn, cur)) == 0) {
+               name->name      = (const char *)talloc_strdup(name, (char *)ldb_dn_get_component_val(dn, cur)->data);
                cur++;
        } else {
                name->name      = talloc_strdup(name, "");
@@ -128,8 +213,8 @@ static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct n
                }
        }
 
-       if (dn->comp_num > cur && strcasecmp("type", dn->components[cur].name) == 0) {
-               name->type      = strtoul((char *)dn->components[cur].value.data, NULL, 0);
+       if (comp_num > cur && strcasecmp("type", ldb_dn_get_component_name(dn, cur)) == 0) {
+               name->type      = strtoul((char *)ldb_dn_get_component_val(dn, cur)->data, NULL, 0);
                cur++;
        } else {
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
@@ -149,119 +234,14 @@ failed:
  "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
  are valid records
 */
-static BOOL winsdb_remove_version(struct wins_server *winssrv, uint64_t version)
-{
-       if (version == winssrv->min_version) {
-               winssrv->min_version++;
-               return winsdb_save_version(winssrv);
-       }
-
-       return True;
-}
-
-/*
- encode the winsdb_addr("address") attribute like this:
- "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z"
-*/
-static int ldb_msg_add_winsdb_addr(struct ldb_context *ldb, struct ldb_message *msg, 
-                                  const char *attr_name, struct winsdb_addr *addr)
-{
-       struct ldb_val val;
-       const char *str;
-
-       dn = ldb_dn_string_compose(mem_ctx, NULL, "type=%02x", name->type);
-
-       addresses[len]->address = talloc_strdup(addresses[len], address);
-       if (!addresses[len]->address) {
-               talloc_free(addresses);
-               return NULL;
-       }
-
-       addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
-       if (!addresses[len]->wins_owner) {
-               talloc_free(addresses);
-               return NULL;
-       }
-
-       addresses[len]->expire_time = expire_time;
-
-       addresses[len+1] = NULL;
-
-       return addresses;
-}
-
-void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
-{
-       size_t i;
-
-       for (i=0; addresses[i]; i++) {
-               if (strcmp(addresses[i]->address, address) == 0) {
-                       break;
-               }
-       }
-       if (!addresses[i]) return;
-
-       for (; addresses[i]; i++) {
-               addresses[i] = addresses[i+1];
-       }
-
-       return;
-}
-
-struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
-{
-       size_t i;
-
-       for (i=0; addresses[i]; i++) {
-               if (strcmp(addresses[i]->address, address) == 0) {
-                       return addresses[i];
-               }
-       }
-
-       return NULL;
-}
-
-size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
-{
-       size_t i;
-       for (i=0; addresses[i]; i++);
-       return i;
-}
-
-const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
-{
-       size_t len = winsdb_addr_list_length(addresses);
-       const char **str_list;
-       size_t i;
-
-       str_list = talloc_array(mem_ctx, const char *, len + 1);
-       if (!str_list) return NULL;
-
-       for (i=0; i < len; i++) {
-               str_list[i] = talloc_strdup(str_list, addresses[i]->address);
-               if (!str_list[i]) {
-                       talloc_free(str_list);
-                       return NULL;
-               }
-       }
-
-       str_list[len] = NULL;
-       return str_list;
-}
-
-/*
- decode the winsdb_addr("address") attribute:
- "172.31.1.1" or 
- "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z"
- are valid records
-*/
-static struct winsdb_addr *winsdb_addr_decode(struct winsdb_record *rec, TALLOC_CTX *mem_ctx, struct ldb_val *val)
+static NTSTATUS winsdb_addr_decode(struct winsdb_handle *h, struct winsdb_record *rec, struct ldb_val *val,
+                                  TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
 {
        NTSTATUS status;
        struct winsdb_addr *addr;
-       char *address;
-       char *wins_owner;
-       char *expire_time;
+       const char *address;
+       const char *wins_owner;
+       const char *expire_time;
        char *p;
 
        addr = talloc(mem_ctx, struct winsdb_addr);
@@ -275,8 +255,12 @@ static struct winsdb_addr *winsdb_addr_decode(struct winsdb_record *rec, TALLOC_
        p = strchr(address, ';');
        if (!p) {
                /* support old entries, with only the address */
-               addr->address           = talloc_steal(addr, val->data);
-               addr->wins_owner        = rec->wins_owner;
+               addr->address           = (const char *)talloc_steal(addr, val->data);
+               addr->wins_owner        = talloc_reference(addr, rec->wins_owner);
+               if (!addr->wins_owner) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto failed;
+               }
                addr->expire_time       = rec->expire_time;
                *_addr = addr;
                return NT_STATUS_OK;
@@ -301,6 +285,9 @@ static struct winsdb_addr *winsdb_addr_decode(struct winsdb_record *rec, TALLOC_
        }
 
        *p = '\0';p++;
+       if (strcmp(wins_owner, "0.0.0.0") == 0) {
+               wins_owner = h->local_owner;
+       }
        addr->wins_owner = talloc_strdup(addr, wins_owner);
        if (!addr->wins_owner) {
                status = NT_STATUS_NO_MEMORY;
@@ -320,7 +307,7 @@ static struct winsdb_addr *winsdb_addr_decode(struct winsdb_record *rec, TALLOC_
        }
 
        *p = '\0';p++;
-       addr->expire_time = ldap_string_to_time(expire_time);
+       addr->expire_time = ldb_string_to_time(expire_time);
 
        *_addr = addr;
        return NT_STATUS_OK;
@@ -331,23 +318,35 @@ failed:
 
 /*
  encode the winsdb_addr("address") attribute like this:
+ non-static record:
  "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
+ static record:
+ "172.31.1.1"
 */
-static int ldb_msg_add_winsdb_addr(struct ldb_context *ldb, struct ldb_message *msg, 
+static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, struct winsdb_record *rec,
                                   const char *attr_name, struct winsdb_addr *addr)
 {
        struct ldb_val val;
        const char *str;
 
-       str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
-                             addr->address, addr->wins_owner,
-                             ldap_timestring(msg, addr->expire_time));
-       if (!str) return -1;
+       if (rec->is_static) {
+               str = talloc_strdup(msg, addr->address);
+               if (!str) return -1;
+       } else {
+               char *expire_time;
+               expire_time = ldb_timestring(msg, addr->expire_time);
+               if (!expire_time) return -1;
+               str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
+                                     addr->address, addr->wins_owner,
+                                     expire_time);
+               talloc_free(expire_time);
+               if (!str) return -1;
+       }
 
        val.data = discard_const_p(uint8_t, str);
        val.length = strlen(str);
 
-       return ldb_msg_add_value(ldb, msg, attr_name, &val);
+       return ldb_msg_add_value(msg, attr_name, &val, NULL);
 }
 
 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
@@ -362,11 +361,134 @@ struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
        return addresses;
 }
 
-struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const char *address,
-                                         const char *wins_owner, time_t expire_time)
+static int winsdb_addr_sort_list (struct winsdb_addr **p1, struct winsdb_addr **p2, void *opaque)
 {
-       size_t len = winsdb_addr_list_length(addresses);
+       struct winsdb_addr *a1 = talloc_get_type(*p1, struct winsdb_addr);
+       struct winsdb_addr *a2 = talloc_get_type(*p2, struct winsdb_addr);
+       struct winsdb_handle *h= talloc_get_type(opaque, struct winsdb_handle);
+       BOOL a1_owned = False;
+       BOOL a2_owned = False;
+
+       /*
+        * first the owned addresses with the newest to the oldest address
+        * then the replica addresses with the newest to the oldest address
+        */
+       if (a2->expire_time != a1->expire_time) {
+               return a2->expire_time - a1->expire_time;
+       }
+
+       if (strcmp(a2->wins_owner, h->local_owner) == 0) {
+               a2_owned = True;
+       }
+
+       if (strcmp(a1->wins_owner, h->local_owner) == 0) {
+               a1_owned = True;
+       }
+
+       return a2_owned - a1_owned;
+}
+
+struct winsdb_addr **winsdb_addr_list_add(struct winsdb_handle *h, const struct winsdb_record *rec,
+                                         struct winsdb_addr **addresses, const char *address,
+                                         const char *wins_owner, time_t expire_time,
+                                         BOOL is_name_registration)
+{
+       struct winsdb_addr *old_addr = NULL;
+       size_t len = 0;
+       size_t i;
+       BOOL found_old_replica = False;
+
+       /*
+        * count the addresses and maybe
+        * find an old entry for the new address
+        */
+       for (i=0; addresses[i]; i++) {
+               if (old_addr) continue;
+               if (strcmp(addresses[i]->address, address) == 0) {
+                       old_addr = addresses[i];
+               }
+       }
+       len = i;
+
+       /*
+        * the address is already there
+        * and we can replace it
+        */
+       if (old_addr) {
+               goto remove_old_addr;
+       }
+
+       /*
+        * if we don't have 25 addresses already,
+        * we can just add the new address
+        */
+       if (len < 25) {
+               goto add_new_addr;
+       }
 
+       /*
+        * if we haven't found the address,
+        * and we have already have 25 addresses
+        * if so then we need to do the following:
+        * - if it isn't a name registration, then just ignore the new address
+        * - if it is a name registration, then first search for 
+        *   the oldest replica and if there's no replica address
+        *   search the oldest owned address
+        */
+       if (!is_name_registration) {
+               return addresses;
+       }
+
+       /*
+        * find the oldest replica address, if there's no replica
+        * record at all, find the oldest owned address
+        */
+       for (i=0; addresses[i]; i++) {
+               BOOL cur_is_replica = False;
+               /* find out if the current address is a replica */
+               if (strcmp(addresses[i]->wins_owner, h->local_owner) != 0) {
+                       cur_is_replica = True;
+               }
+
+               /*
+                * if we already found a replica address and the current address
+                * is not a replica, then skip it
+                */
+               if (found_old_replica && !cur_is_replica) continue;
+
+               /*
+                * if we found the first replica address, reset the address
+                * that would be replaced
+                */
+               if (!found_old_replica && cur_is_replica) {
+                       found_old_replica = True;
+                       old_addr = addresses[i];
+                       continue;
+               }
+
+               /*
+                * if the first address isn't a replica, just start with 
+                * the first one
+                */
+               if (!old_addr) {
+                       old_addr = addresses[i];
+                       continue;
+               }
+
+               /*
+                * see if we find an older address
+                */
+               if (addresses[i]->expire_time < old_addr->expire_time) {
+                       old_addr = addresses[i];
+                       continue;
+               }
+       }
+
+remove_old_addr:
+       winsdb_addr_list_remove(addresses, old_addr->address);
+       len --;
+
+add_new_addr:
        addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
        if (!addresses) return NULL;
 
@@ -392,6 +514,8 @@ struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const
 
        addresses[len+1] = NULL;
 
+       ldb_qsort(addresses, len+1 , sizeof(addresses[0]), h, (ldb_qsort_cmp_fn_t)winsdb_addr_sort_list);
+
        return addresses;
 }
 
@@ -404,7 +528,6 @@ void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address
                        break;
                }
        }
-       if (!addresses[i]) return;
 
        for (; addresses[i]; i++) {
                addresses[i] = addresses[i+1];
@@ -436,63 +559,51 @@ size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
 {
        size_t len = winsdb_addr_list_length(addresses);
-       const char **str_list;
+       const char **str_list=NULL;
        size_t i;
 
-       str_list = talloc_array(mem_ctx, const char *, len + 1);
-       if (!str_list) return NULL;
-
        for (i=0; i < len; i++) {
-               str_list[i] = talloc_strdup(str_list, addresses[i]->address);
+               str_list = str_list_add(str_list, addresses[i]->address);
                if (!str_list[i]) {
-                       talloc_free(str_list);
                        return NULL;
                }
        }
-
-       str_list[len] = NULL;
+       talloc_steal(mem_ctx, str_list);
        return str_list;
 }
 
 /*
   load a WINS entry from the database
 */
-NTSTATUS winsdb_lookup(struct ldb_context *wins_db
+NTSTATUS winsdb_lookup(struct winsdb_handle *h
                       struct nbt_name *name,
                       TALLOC_CTX *mem_ctx,
                       struct winsdb_record **_rec)
 {
        NTSTATUS status;
-       struct ldb_message **res = NULL;
+       struct ldb_result *res = NULL;
        int ret;
        struct winsdb_record *rec;
+       struct ldb_context *wins_db = h->ldb;
        TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
+       time_t now = time(NULL);
 
        /* find the record in the WINS database */
-       ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE, 
+       ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, wins_db, name), LDB_SCOPE_BASE, 
                         NULL, NULL, &res);
-       if (res != NULL) {
-               talloc_steal(tmp_ctx, res);
-       }
-       if (ret == 0) {
-               status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
-               goto failed;
-       } else if (ret != 1) {
+
+       talloc_steal(tmp_ctx, res);
+       if (ret != LDB_SUCCESS || res->count > 1) {
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
                goto failed;
+       } else if (res->count== 0) {
+               status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
+               goto failed;
        }
 
-       status = winsdb_record(res[0], name, tmp_ctx, &rec);
+       status = winsdb_record(h, res->msgs[0], tmp_ctx, now, &rec);
        if (!NT_STATUS_IS_OK(status)) goto failed;
 
-       /* see if it has already expired */
-       if (rec->state == WINS_REC_ACTIVE &&
-           rec->expire_time <= time(NULL)) {
-               DEBUG(5,("WINS: expiring name %s (expired at %s)\n", 
-                        nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
-               rec->state = WINS_REC_RELEASED;
-       }
-
        talloc_steal(mem_ctx, rec);
        talloc_free(tmp_ctx);
        *_rec = rec;
@@ -503,12 +614,14 @@ failed:
        return status;
 }
 
-NTSTATUS winsdb_record(struct ldb_message *msg, struct nbt_name *name, TALLOC_CTX *mem_ctx, struct winsdb_record **_rec)
+NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_CTX *mem_ctx, time_t now, struct winsdb_record **_rec)
 {
        NTSTATUS status;
        struct winsdb_record *rec;
        struct ldb_message_element *el;
-       uint32_t i;
+       struct nbt_name *name;
+       uint32_t i, j, num_values;
+       BOOL we_are_owner = False;
 
        rec = talloc(mem_ctx, struct winsdb_record);
        if (rec == NULL) {
@@ -516,46 +629,129 @@ NTSTATUS winsdb_record(struct ldb_message *msg, struct nbt_name *name, TALLOC_CT
                goto failed;
        }
 
-       if (!name) {
-               status = winsdb_nbt_name(rec, msg->dn, &name);
-               if (!NT_STATUS_IS_OK(status)) goto failed;
+       status = winsdb_nbt_name(rec, msg->dn, &name);
+       if (!NT_STATUS_IS_OK(status)) goto failed;
+
+       if (strlen(name->name) > 15) {
+               status = NT_STATUS_INTERNAL_DB_CORRUPTION;
+               goto failed;
+       }
+       if (name->scope && strlen(name->scope) > 238) {
+               status = NT_STATUS_INTERNAL_DB_CORRUPTION;
+               goto failed;
        }
 
        /* parse it into a more convenient winsdb_record structure */
-       rec->name           = name;
-       rec->state          = ldb_msg_find_int(res[0], "active", WINS_REC_RELEASED);
-       rec->nb_flags       = ldb_msg_find_int(res[0], "nbFlags", 0);
-       rec->expire_time    = ldb_string_to_time(ldb_msg_find_string(res[0], "expires", NULL));
-       rec->registered_by  = ldb_msg_find_string(res[0], "registeredBy", NULL);
-       rec->version        = ldb_msg_find_uint64(res[0], "version", 0);
+       rec->name               = name;
+       rec->type               = ldb_msg_find_attr_as_int(msg, "recordType", WREPL_TYPE_UNIQUE);
+       rec->state              = ldb_msg_find_attr_as_int(msg, "recordState", WREPL_STATE_RELEASED);
+       rec->node               = ldb_msg_find_attr_as_int(msg, "nodeType", WREPL_NODE_B);
+       rec->is_static          = ldb_msg_find_attr_as_int(msg, "isStatic", 0);
+       rec->expire_time        = ldb_string_to_time(ldb_msg_find_attr_as_string(msg, "expireTime", NULL));
+       rec->version            = ldb_msg_find_attr_as_uint64(msg, "versionID", 0);
+       rec->wins_owner         = ldb_msg_find_attr_as_string(msg, "winsOwner", NULL);
+       rec->registered_by      = ldb_msg_find_attr_as_string(msg, "registeredBy", NULL);
        talloc_steal(rec, rec->wins_owner);
        talloc_steal(rec, rec->registered_by);
 
-       if (!rec->wins_owner) rec->wins_owner = WINSDB_OWNER_LOCAL;
+       if (!rec->wins_owner || strcmp(rec->wins_owner, "0.0.0.0") == 0) {
+               rec->wins_owner = h->local_owner;
+       }
 
        el = ldb_msg_find_element(msg, "address");
-       if (el == NULL) {
+       if (el) {
+               num_values = el->num_values;
+       } else {
+               num_values = 0;
+       }
+
+       if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
+               if (num_values != 1) {
+                       status = NT_STATUS_INTERNAL_DB_CORRUPTION;
+                       goto failed;
+               }
+       }
+       if (rec->state == WREPL_STATE_ACTIVE) {
+               if (num_values < 1) {
+                       status = NT_STATUS_INTERNAL_DB_CORRUPTION;
+                       goto failed;
+               }
+       }
+       if (num_values > 25) {
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
                goto failed;
        }
 
-       rec->addresses     = talloc_array(rec, struct winsdb_addr *, el->num_values+1);
+       if (strcmp(rec->wins_owner, h->local_owner) == 0) {
+               we_are_owner = True;
+       }
+
+       /* 
+        * see if it has already expired
+        * 
+        * NOTE: only expire owned records this way!
+        *       w2k3 resolves expired replicas
+        *       which are in active state
+        */
+       if (!rec->is_static &&
+           rec->expire_time <= now &&
+           rec->state == WREPL_STATE_ACTIVE &&
+           we_are_owner) {
+               DEBUG(5,("WINS: expiring name %s (expired at %s)\n", 
+                        nbt_name_string(mem_ctx, rec->name), timestring(mem_ctx, rec->expire_time)));
+               rec->state = WREPL_STATE_RELEASED;
+       }
+
+       rec->addresses     = talloc_array(rec, struct winsdb_addr *, num_values+1);
        if (rec->addresses == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto failed;
        }
 
-       for (i=0;i<el->num_values;i++) {
-               status = winsdb_addr_decode(rec, &el->values[i], rec->addresses, &rec->addresses[i]);
+       for (i=0,j=0;i<num_values;i++) {
+               status = winsdb_addr_decode(h, rec, &el->values[i], rec->addresses, &rec->addresses[j]);
                if (!NT_STATUS_IS_OK(status)) goto failed;
+
+               /*
+                * the record isn't static and is active
+                * then don't add the address if it's expired
+                */
+               if (!rec->is_static &&
+                   rec->addresses[j]->expire_time <= now &&
+                   rec->state == WREPL_STATE_ACTIVE &&
+                   we_are_owner) {
+                       DEBUG(5,("WINS: expiring name addr %s of %s (expired at %s)\n", 
+                                rec->addresses[j]->address, nbt_name_string(rec->addresses[j], rec->name),
+                                timestring(rec->addresses[j], rec->addresses[j]->expire_time)));
+                       talloc_free(rec->addresses[j]);
+                       rec->addresses[j] = NULL;
+                       continue;
+               }
+               j++;
+       }
+       rec->addresses[j] = NULL;
+       num_values = j;
+
+       if (rec->is_static && rec->state == WREPL_STATE_ACTIVE) {
+               rec->expire_time = get_time_t_max();
+               for (i=0;rec->addresses[i];i++) {
+                       rec->addresses[i]->expire_time = rec->expire_time;
+               }
+       }
+
+       if (rec->state == WREPL_STATE_ACTIVE) {
+               if (num_values < 1) {
+                       DEBUG(5,("WINS: expiring name %s (because it has no active addresses)\n", 
+                                nbt_name_string(mem_ctx, rec->name)));
+                       rec->state = WREPL_STATE_RELEASED;
+               }
        }
-       rec->addresses[i] = NULL;
 
        *_rec = rec;
        return NT_STATUS_OK;
 failed:
        if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
-               DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
+               DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_get_linearized(msg->dn)));
        }
        talloc_free(rec);
        return status;
@@ -568,22 +764,54 @@ struct ldb_message *winsdb_message(struct ldb_context *ldb,
                                   struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
 {
        int i, ret=0;
+       size_t addr_count;
+       const char *expire_time;
        struct ldb_message *msg = ldb_msg_new(mem_ctx);
        if (msg == NULL) goto failed;
 
-       msg->dn = winsdb_dn(msg, rec->name);
+       /* make sure we don't put in corrupted records */
+       addr_count = winsdb_addr_list_length(rec->addresses);
+       if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
+               rec->state = WREPL_STATE_RELEASED;
+       }
+       if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
+               rec->type = WREPL_TYPE_MHOMED;
+       }
+
+       expire_time = ldb_timestring(msg, rec->expire_time);
+       if (!expire_time) {
+               goto failed;
+       }
+
+       msg->dn = winsdb_dn(msg, ldb, rec->name);
        if (msg->dn == NULL) goto failed;
-       ret |= ldb_msg_add_fmt(msg, "objectClass", "wins");
-       ret |= ldb_msg_add_fmt(msg, "active", "%u", rec->state);
-       ret |= ldb_msg_add_fmt(msg, "nbFlags", "0x%04x", rec->nb_flags);
-       ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
-       ret |= ldb_msg_add_string(msg, "expires", 
-                                 ldb_timestring(msg, rec->expire_time));
-       ret |= ldb_msg_add_fmt(msg, "version", "%llu", rec->version);
+       ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
+       if (rec->name->name && *rec->name->name) {
+               ret |= ldb_msg_add_string(msg, "name", rec->name->name);
+       }
+       if (rec->name->scope && *rec->name->scope) {
+               ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
+       }
+       ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
+       ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
+       ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
+       ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
+       ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
+       ret |= ldb_msg_add_empty(msg, "expireTime", 0, NULL);
+       if (!(rec->is_static && rec->state == WREPL_STATE_ACTIVE)) {
+               ret |= ldb_msg_add_string(msg, "expireTime", expire_time);
+       }
+       ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
+       ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
+       ret |= ldb_msg_add_empty(msg, "address", 0, NULL);
        for (i=0;rec->addresses[i];i++) {
-               ret |= ldb_msg_add_string(msg, "address", rec->addresses[i]);
+               ret |= ldb_msg_add_winsdb_addr(msg, rec, "address", rec->addresses[i]);
+       }
+       ret |= ldb_msg_add_empty(msg, "registeredBy", 0, NULL);
+       if (rec->registered_by) {
+               ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
+               if (ret != 0) goto failed;
        }
-       if (ret != 0) goto failed;
        return msg;
 
 failed:
@@ -594,34 +822,41 @@ failed:
 /*
   save a WINS record into the database
 */
-uint8_t winsdb_add(struct wins_server *winssrv, struct winsdb_record *rec)
+uint8_t winsdb_add(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
 {
-       struct ldb_context *ldb = winssrv->wins_db;
        struct ldb_message *msg;
-       TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
+       struct ldb_context *wins_db = h->ldb;
+       TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
        int trans = -1;
        int ret = 0;
 
-
-       trans = ldb_transaction_start(ldb);
+       trans = ldb_transaction_start(wins_db);
        if (trans != LDB_SUCCESS) goto failed;
 
-       rec->version = winsdb_allocate_version(winssrv);
-       if (rec->version == 0) goto failed;
+       if (flags & WINSDB_FLAG_ALLOC_VERSION) {
+               /* passing '0' means auto-allocate a new one */
+               rec->version = winsdb_set_maxVersion(h, 0);
+               if (rec->version == 0) goto failed;
+       }
+       if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
+               rec->wins_owner = h->local_owner;
+       }
 
-       msg = winsdb_message(winssrv->wins_db, rec, tmp_ctx);
+       msg = winsdb_message(wins_db, rec, tmp_ctx);
        if (msg == NULL) goto failed;
-       ret = ldb_add(ldb, msg);
+       ret = ldb_add(wins_db, msg);
        if (ret != 0) goto failed;
 
-       trans = ldb_transaction_commit(ldb);
+       trans = ldb_transaction_commit(wins_db);
        if (trans != LDB_SUCCESS) goto failed;
 
+       wins_hook(h, rec, WINS_HOOK_ADD);
+
        talloc_free(tmp_ctx);
        return NBT_RCODE_OK;
 
 failed:
-       if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
+       if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
        talloc_free(tmp_ctx);
        return NBT_RCODE_SVR;
 }
@@ -630,40 +865,47 @@ failed:
 /*
   modify a WINS record in the database
 */
-uint8_t winsdb_modify(struct wins_server *winssrv, struct winsdb_record *rec)
+uint8_t winsdb_modify(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
 {
-       struct ldb_context *ldb = winssrv->wins_db;
        struct ldb_message *msg;
-       TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
+       struct ldb_context *wins_db = h->ldb;
+       TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
        int trans;
        int ret;
        int i;
 
-       trans = ldb_transaction_start(ldb);
+       trans = ldb_transaction_start(wins_db);
        if (trans != LDB_SUCCESS) goto failed;
 
-       rec->version = winsdb_allocate_version(winssrv);
-       if (rec->version == 0) goto failed;
-       rec->wins_owner = WINSDB_OWNER_LOCAL;
+       if (flags & WINSDB_FLAG_ALLOC_VERSION) {
+               /* passing '0' means auto-allocate a new one */
+               rec->version = winsdb_set_maxVersion(h, 0);
+               if (rec->version == 0) goto failed;
+       }
+       if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
+               rec->wins_owner = h->local_owner;
+       }
 
-       msg = winsdb_message(winssrv->wins_db, rec, tmp_ctx);
+       msg = winsdb_message(wins_db, rec, tmp_ctx);
        if (msg == NULL) goto failed;
 
        for (i=0;i<msg->num_elements;i++) {
                msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
        }
 
-       ret = ldb_modify(ldb, msg);
+       ret = ldb_modify(wins_db, msg);
        if (ret != 0) goto failed;
 
-       trans = ldb_transaction_commit(ldb);
+       trans = ldb_transaction_commit(wins_db);
        if (trans != LDB_SUCCESS) goto failed;
 
+       wins_hook(h, rec, WINS_HOOK_MODIFY);
+
        talloc_free(tmp_ctx);
        return NBT_RCODE_OK;
 
 failed:
-       if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
+       if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
        talloc_free(tmp_ctx);
        return NBT_RCODE_SVR;
 }
@@ -672,39 +914,140 @@ failed:
 /*
   delete a WINS record from the database
 */
-uint8_t winsdb_delete(struct wins_server *winssrv, struct winsdb_record *rec)
+uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec)
 {
-       struct ldb_context *ldb = winssrv->wins_db;
-       TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
-       const struct ldb_dn *dn;
+       struct ldb_context *wins_db = h->ldb;
+       TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
+       struct ldb_dn *dn;
        int trans;
        int ret;
 
-       trans = ldb_transaction_start(ldb);
+       trans = ldb_transaction_start(wins_db);
        if (trans != LDB_SUCCESS) goto failed;
 
-       if(!winsdb_remove_version(winssrv, rec->version))
-               goto failed;
-
-       dn = winsdb_dn(tmp_ctx, rec->name);
+       dn = winsdb_dn(tmp_ctx, wins_db, rec->name);
        if (dn == NULL) goto failed;
 
-       ret = ldb_delete(ldb, dn);
+       ret = ldb_delete(wins_db, dn);
        if (ret != 0) goto failed;
 
-       trans = ldb_transaction_commit(ldb);
+       trans = ldb_transaction_commit(wins_db);
        if (trans != LDB_SUCCESS) goto failed;
 
+       wins_hook(h, rec, WINS_HOOK_DELETE);
+
        talloc_free(tmp_ctx);
        return NBT_RCODE_OK;
 
 failed:
-       if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
+       if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
        talloc_free(tmp_ctx);
        return NBT_RCODE_SVR;
 }
 
-struct ldb_context *winsdb_connect(TALLOC_CTX *mem_ctx)
+static BOOL winsdb_check_or_add_module_list(struct winsdb_handle *h)
+{
+       int trans;
+       int ret;
+       struct ldb_dn *dn;
+       struct ldb_result *res = NULL;
+       struct ldb_message *msg = NULL;
+       TALLOC_CTX *tmp_ctx = talloc_new(h);
+       unsigned int flags = 0;
+
+       trans = ldb_transaction_start(h->ldb);
+       if (trans != LDB_SUCCESS) goto failed;
+
+       /* check if we have a special @MODULES record already */
+       dn = ldb_dn_new(tmp_ctx, h->ldb, "@MODULES");
+       if (!dn) goto failed;
+
+       /* find the record in the WINS database */
+       ret = ldb_search(h->ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
+       if (ret != LDB_SUCCESS) goto failed;
+       talloc_steal(tmp_ctx, res);
+
+       if (res->count > 0) goto skip;
+
+       /* if there's no record, add one */
+       msg = ldb_msg_new(tmp_ctx);
+       if (!msg) goto failed;
+       msg->dn = dn;
+
+       ret = ldb_msg_add_string(msg, "@LIST", "wins_ldb");
+       if (ret != 0) goto failed;
+
+       ret = ldb_add(h->ldb, msg);
+       if (ret != 0) goto failed;
+
+       trans = ldb_transaction_commit(h->ldb);
+       if (trans != LDB_SUCCESS) goto failed;
+
+       /* close and reopen the database, with the modules */
+       trans = LDB_ERR_OTHER;
+       talloc_free(h->ldb);
+       h->ldb = NULL;
+
+       if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
+               flags |= LDB_FLG_NOSYNC;
+       }
+
+       h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
+                                 NULL, NULL, flags, NULL);
+       if (!h->ldb) goto failed;
+
+       talloc_free(tmp_ctx);
+       return True;
+
+skip:
+       if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
+       talloc_free(tmp_ctx);
+       return True;
+
+failed:
+       if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
+       talloc_free(tmp_ctx);
+       return False;
+}
+
+struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx, enum winsdb_handle_caller caller)
 {
-       return ldb_wrap_connect(mem_ctx, lp_wins_url(), 0, NULL);
+       struct winsdb_handle *h = NULL;
+       const char *owner;
+       unsigned int flags = 0;
+       BOOL ret;
+       int ldb_err;
+
+       h = talloc(mem_ctx, struct winsdb_handle);
+       if (!h) return NULL;
+
+       if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
+               flags |= LDB_FLG_NOSYNC;
+       }
+
+       h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
+                                 NULL, NULL, flags, NULL);
+       if (!h->ldb) goto failed;       
+
+       h->caller = caller;
+
+       owner = lp_parm_string(-1, "winsdb", "local_owner");
+       if (!owner) {
+               owner = iface_n_ip(0);
+       }
+
+       h->local_owner = talloc_strdup(h, owner);
+       if (!h->local_owner) goto failed;
+
+       /* make sure the module list is available and used */
+       ret = winsdb_check_or_add_module_list(h);
+       if (!ret) goto failed;
+
+       ldb_err = ldb_set_opaque(h->ldb, "winsdb_handle", h);
+       if (ldb_err != LDB_SUCCESS) goto failed;
+
+       return h;
+failed:
+       talloc_free(h);
+       return NULL;
 }