s4:lib/tevent: rename structs
[gd/samba-autobuild/.git] / source4 / nbt_server / wins / winsdb.c
index 748b489c67bb221f1ad59f2f8a59bea112ecd88a..945914ccdfb4b2c1cd0cd89e6c4739fd01b0cfae 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"
 #include "lib/ldb/include/ldb_errors.h"
 #include "librpc/gen_ndr/ndr_nbt.h"
 #include "system/time.h"
-#include "db_wrap.h"
+#include "ldb_wrap.h"
 #include "system/network.h"
 #include "lib/socket/netif.h"
+#include "param/param.h"
 
 uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
 {
@@ -45,10 +45,8 @@ uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
        if (!dn) goto failed;
 
        /* find the record in the WINS database */
-       ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, 
-                        NULL, NULL, &res);
+       ret = ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
        if (ret != LDB_SUCCESS) goto failed;
-       talloc_steal(tmp_ctx, res);
        if (res->count > 1) goto failed;
 
        if (res->count == 1) {
@@ -82,14 +80,10 @@ uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion)
        if (!dn) goto failed;
 
        /* find the record in the WINS database */
-       ret = ldb_search(wins_db, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
-
+       ret = ldb_search(wins_db, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
        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_attr_as_uint64(res->msgs[0], "maxVersion", 0);
        }
@@ -143,10 +137,8 @@ uint64_t winsdb_get_seqnumber(struct winsdb_handle *h)
        if (!dn) goto failed;
 
        /* find the record in the WINS database */
-       ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, 
-                        NULL, NULL, &res);
+       ret = ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
        if (ret != LDB_SUCCESS) goto failed;
-       talloc_steal(tmp_ctx, res);
        if (res->count > 1) goto failed;
 
        if (res->count == 1) {
@@ -366,8 +358,8 @@ static int winsdb_addr_sort_list (struct winsdb_addr **p1, struct winsdb_addr **
        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;
+       bool a1_owned = false;
+       bool a2_owned = false;
 
        /*
         * first the owned addresses with the newest to the oldest address
@@ -378,11 +370,11 @@ static int winsdb_addr_sort_list (struct winsdb_addr **p1, struct winsdb_addr **
        }
 
        if (strcmp(a2->wins_owner, h->local_owner) == 0) {
-               a2_owned = True;
+               a2_owned = true;
        }
 
        if (strcmp(a1->wins_owner, h->local_owner) == 0) {
-               a1_owned = True;
+               a1_owned = true;
        }
 
        return a2_owned - a1_owned;
@@ -391,12 +383,12 @@ static int winsdb_addr_sort_list (struct winsdb_addr **p1, struct winsdb_addr **
 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)
+                                         bool is_name_registration)
 {
        struct winsdb_addr *old_addr = NULL;
        size_t len = 0;
        size_t i;
-       BOOL found_old_replica = False;
+       bool found_old_replica = false;
 
        /*
         * count the addresses and maybe
@@ -444,10 +436,10 @@ struct winsdb_addr **winsdb_addr_list_add(struct winsdb_handle *h, const struct
         * record at all, find the oldest owned address
         */
        for (i=0; addresses[i]; i++) {
-               BOOL cur_is_replica = False;
+               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;
+                       cur_is_replica = true;
                }
 
                /*
@@ -461,7 +453,7 @@ struct winsdb_addr **winsdb_addr_list_add(struct winsdb_handle *h, const struct
                 * that would be replaced
                 */
                if (!found_old_replica && cur_is_replica) {
-                       found_old_replica = True;
+                       found_old_replica = true;
                        old_addr = addresses[i];
                        continue;
                }
@@ -589,10 +581,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, wins_db, name), LDB_SCOPE_BASE, 
-                        NULL, NULL, &res);
+       ret = ldb_search(wins_db, tmp_ctx, &res,
+                        winsdb_dn(tmp_ctx, wins_db, name),
+                        LDB_SCOPE_BASE, NULL, NULL);
 
-       talloc_steal(tmp_ctx, res);
        if (ret != LDB_SUCCESS || res->count > 1) {
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
                goto failed;
@@ -621,7 +613,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;
+       bool we_are_owner = false;
 
        rec = talloc(mem_ctx, struct winsdb_record);
        if (rec == NULL) {
@@ -683,7 +675,7 @@ NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_
        }
 
        if (strcmp(rec->wins_owner, h->local_owner) == 0) {
-               we_are_owner = True;
+               we_are_owner = true;
        }
 
        /* 
@@ -850,7 +842,7 @@ uint8_t winsdb_add(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t
        trans = ldb_transaction_commit(wins_db);
        if (trans != LDB_SUCCESS) goto failed;
 
-       wins_hook(h, rec, WINS_HOOK_ADD);
+       wins_hook(h, rec, WINS_HOOK_ADD, h->hook_script);
 
        talloc_free(tmp_ctx);
        return NBT_RCODE_OK;
@@ -899,7 +891,7 @@ uint8_t winsdb_modify(struct winsdb_handle *h, struct winsdb_record *rec, uint32
        trans = ldb_transaction_commit(wins_db);
        if (trans != LDB_SUCCESS) goto failed;
 
-       wins_hook(h, rec, WINS_HOOK_MODIFY);
+       wins_hook(h, rec, WINS_HOOK_MODIFY, h->hook_script);
 
        talloc_free(tmp_ctx);
        return NBT_RCODE_OK;
@@ -934,7 +926,7 @@ uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec)
        trans = ldb_transaction_commit(wins_db);
        if (trans != LDB_SUCCESS) goto failed;
 
-       wins_hook(h, rec, WINS_HOOK_DELETE);
+       wins_hook(h, rec, WINS_HOOK_DELETE, h->hook_script);
 
        talloc_free(tmp_ctx);
        return NBT_RCODE_OK;
@@ -945,7 +937,8 @@ failed:
        return NBT_RCODE_SVR;
 }
 
-static BOOL winsdb_check_or_add_module_list(struct winsdb_handle *h)
+static bool winsdb_check_or_add_module_list(struct tevent_context *ev_ctx, 
+                                           struct loadparm_context *lp_ctx, struct winsdb_handle *h)
 {
        int trans;
        int ret;
@@ -963,9 +956,8 @@ static BOOL winsdb_check_or_add_module_list(struct winsdb_handle *h)
        if (!dn) goto failed;
 
        /* find the record in the WINS database */
-       ret = ldb_search(h->ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
+       ret = ldb_search(h->ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
        if (ret != LDB_SUCCESS) goto failed;
-       talloc_steal(tmp_ctx, res);
 
        if (res->count > 0) goto skip;
 
@@ -988,59 +980,58 @@ 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(lp_ctx, 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, ev_ctx, lp_ctx, lock_path(h, lp_ctx, lp_wins_url(lp_ctx)),
                                  NULL, NULL, flags, NULL);
        if (!h->ldb) goto failed;
 
        talloc_free(tmp_ctx);
-       return True;
+       return true;
 
 skip:
        if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
        talloc_free(tmp_ctx);
-       return True;
+       return true;
 
 failed:
        if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
        talloc_free(tmp_ctx);
-       return False;
+       return false;
 }
 
-struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx, enum winsdb_handle_caller caller)
+struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx, 
+                                    struct tevent_context *ev_ctx,
+                                    struct loadparm_context *lp_ctx,
+                                    const char *owner,
+                                    enum winsdb_handle_caller caller)
 {
        struct winsdb_handle *h = NULL;
-       const char *owner;
        unsigned int flags = 0;
-       BOOL ret;
+       bool ret;
        int ldb_err;
 
        h = talloc(mem_ctx, struct winsdb_handle);
        if (!h) return NULL;
 
-       if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
+       if (lp_parm_bool(lp_ctx, 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, ev_ctx, lp_ctx, lock_path(h, lp_ctx, lp_wins_url(lp_ctx)),
                                  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->hook_script = lp_wins_hook(lp_ctx);
 
        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);
+       ret = winsdb_check_or_add_module_list(ev_ctx, lp_ctx, h);
        if (!ret) goto failed;
 
        ldb_err = ldb_set_opaque(h->ldb, "winsdb_handle", h);