r25446: Merge some changes I made on the way home from SFO:
[jelmer/samba4-debian.git] / source / nbt_server / wins / winsdb.c
index a0b65bec2583a114bc70a6db9fd9bf78255ad239..2789873c59faa8c194f8547b915c4ae45c232620 100644 (file)
@@ -8,7 +8,7 @@
       
    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,
@@ -17,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"
@@ -30,7 +29,8 @@
 #include "system/time.h"
 #include "db_wrap.h"
 #include "system/network.h"
-#include "netif/netif.h"
+#include "lib/socket/netif.h"
+#include "param/param.h"
 
 uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
 {
@@ -41,7 +41,7 @@ uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
        TALLOC_CTX *tmp_ctx = talloc_new(ldb);
        uint64_t maxVersion = 0;
 
-       dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
+       dn = ldb_dn_new(tmp_ctx, ldb, "CN=VERSION");
        if (!dn) goto failed;
 
        /* find the record in the WINS database */
@@ -52,7 +52,7 @@ uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
        if (res->count > 1) goto failed;
 
        if (res->count == 1) {
-               maxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
+               maxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
        }
 
 failed:
@@ -78,19 +78,20 @@ uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion)
        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;
 
        /* 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;
 
        talloc_steal(tmp_ctx, res);
 
        if (res->count == 1) {
-               oldMaxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
+               oldMaxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
        }
 
        if (newMaxVersion == 0) {
@@ -104,11 +105,11 @@ uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion)
        msg->dn = dn;
 
 
-       ret = ldb_msg_add_empty(msg, "objectClass", 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_empty(msg, "maxVersion", LDB_FLAG_MOD_REPLACE);
+       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;
@@ -138,7 +139,7 @@ uint64_t winsdb_get_seqnumber(struct winsdb_handle *h)
        TALLOC_CTX *tmp_ctx = talloc_new(ldb);
        uint64_t seqnumber = 0;
 
-       dn = ldb_dn_explode(tmp_ctx, "@BASEINFO");
+       dn = ldb_dn_new(tmp_ctx, ldb, "@BASEINFO");
        if (!dn) goto failed;
 
        /* find the record in the WINS database */
@@ -149,7 +150,7 @@ uint64_t winsdb_get_seqnumber(struct winsdb_handle *h)
        if (res->count > 1) goto failed;
 
        if (res->count == 1) {
-               seqnumber = ldb_msg_find_uint64(res->msgs[0], "sequenceNumber", 0);
+               seqnumber = ldb_msg_find_attr_as_uint64(res->msgs[0], "sequenceNumber", 0);
        }
 
 failed:
@@ -160,16 +161,16 @@ failed:
 /*
   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;
 }
@@ -178,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);
@@ -186,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, "");
@@ -209,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;
@@ -251,7 +255,7 @@ static NTSTATUS winsdb_addr_decode(struct winsdb_handle *h, struct winsdb_record
        p = strchr(address, ';');
        if (!p) {
                /* support old entries, with only the address */
-               addr->address           = talloc_steal(addr, val->data);
+               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;
@@ -342,7 +346,7 @@ static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, struct winsdb_record
        val.data = discard_const_p(uint8_t, str);
        val.length = strlen(str);
 
-       return ldb_msg_add_value(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)
@@ -585,9 +589,10 @@ NTSTATUS winsdb_lookup(struct winsdb_handle *h,
        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);
 
+       talloc_steal(tmp_ctx, res);
        if (ret != LDB_SUCCESS || res->count > 1) {
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
                goto failed;
@@ -596,8 +601,6 @@ NTSTATUS winsdb_lookup(struct winsdb_handle *h,
                goto failed;
        }
 
-       talloc_steal(tmp_ctx, res);
-
        status = winsdb_record(h, res->msgs[0], tmp_ctx, now, &rec);
        if (!NT_STATUS_IS_OK(status)) goto failed;
 
@@ -618,6 +621,7 @@ NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_
        struct ldb_message_element *el;
        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) {
@@ -639,14 +643,14 @@ NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_
 
        /* parse it into a more convenient winsdb_record structure */
        rec->name               = name;
-       rec->type               = ldb_msg_find_int(msg, "recordType", WREPL_TYPE_UNIQUE);
-       rec->state              = ldb_msg_find_int(msg, "recordState", WREPL_STATE_RELEASED);
-       rec->node               = ldb_msg_find_int(msg, "nodeType", WREPL_NODE_B);
-       rec->is_static          = ldb_msg_find_int(msg, "isStatic", 0);
-       rec->expire_time        = ldb_string_to_time(ldb_msg_find_string(msg, "expireTime", NULL));
-       rec->version            = ldb_msg_find_uint64(msg, "versionID", 0);
-       rec->wins_owner         = ldb_msg_find_string(msg, "winsOwner", NULL);
-       rec->registered_by      = ldb_msg_find_string(msg, "registeredBy", NULL);
+       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);
 
@@ -678,10 +682,21 @@ NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_
                goto failed;
        }
 
-       /* see if it has already expired */
+       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) {
+           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;
@@ -703,7 +718,8 @@ NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_
                 */
                if (!rec->is_static &&
                    rec->addresses[j]->expire_time <= now &&
-                   rec->state == WREPL_STATE_ACTIVE) {
+                   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)));
@@ -735,7 +751,7 @@ NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_
        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;
@@ -767,7 +783,7 @@ struct ldb_message *winsdb_message(struct ldb_context *ldb,
                goto failed;
        }
 
-       msg->dn = winsdb_dn(msg, rec->name);
+       msg->dn = winsdb_dn(msg, ldb, rec->name);
        if (msg->dn == NULL) goto failed;
        ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
        if (rec->name->name && *rec->name->name) {
@@ -781,17 +797,17 @@ struct ldb_message *winsdb_message(struct ldb_context *ldb,
        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);
+       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);
+       ret |= ldb_msg_add_empty(msg, "address", 0, NULL);
        for (i=0;rec->addresses[i];i++) {
                ret |= ldb_msg_add_winsdb_addr(msg, rec, "address", rec->addresses[i]);
        }
-       ret |= ldb_msg_add_empty(msg, "registeredBy", 0);
+       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;
@@ -902,14 +918,14 @@ uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec)
 {
        struct ldb_context *wins_db = h->ldb;
        TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
-       const struct ldb_dn *dn;
+       struct ldb_dn *dn;
        int trans;
        int ret;
 
        trans = ldb_transaction_start(wins_db);
        if (trans != LDB_SUCCESS) 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(wins_db, dn);
@@ -943,7 +959,7 @@ static BOOL winsdb_check_or_add_module_list(struct winsdb_handle *h)
        if (trans != LDB_SUCCESS) goto failed;
 
        /* check if we have a special @MODULES record already */
-       dn = ldb_dn_explode(tmp_ctx, "@MODULES");
+       dn = ldb_dn_new(tmp_ctx, h->ldb, "@MODULES");
        if (!dn) goto failed;
 
        /* find the record in the WINS database */
@@ -972,11 +988,11 @@ static BOOL winsdb_check_or_add_module_list(struct winsdb_handle *h)
        talloc_free(h->ldb);
        h->ldb = NULL;
 
-       if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
+       if (lp_parm_bool(global_loadparm, NULL,"winsdb", "nosync", false)) {
                flags |= LDB_FLG_NOSYNC;
        }
 
-       h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
+       h->ldb = ldb_wrap_connect(h, global_loadparm, lock_path(h, global_loadparm, lp_wins_url(global_loadparm)),
                                  NULL, NULL, flags, NULL);
        if (!h->ldb) goto failed;
 
@@ -1005,17 +1021,17 @@ struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx, enum winsdb_handle_cal
        h = talloc(mem_ctx, struct winsdb_handle);
        if (!h) return NULL;
 
-       if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
+       if (lp_parm_bool(global_loadparm, NULL,"winsdb", "nosync", false)) {
                flags |= LDB_FLG_NOSYNC;
        }
 
-       h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
+       h->ldb = ldb_wrap_connect(h, global_loadparm, lock_path(h, global_loadparm, lp_wins_url(global_loadparm)),
                                  NULL, NULL, flags, NULL);
        if (!h->ldb) goto failed;       
 
        h->caller = caller;
 
-       owner = lp_parm_string(-1, "winsdb", "local_owner");
+       owner = lp_parm_string(global_loadparm, NULL, "winsdb", "local_owner");
        if (!owner) {
                owner = iface_n_ip(0);
        }