r25446: Merge some changes I made on the way home from SFO:
[jelmer/samba4-debian.git] / source / wrepl_server / wrepl_server.c
index 2682ca6721b51da56579e5d0f3c9e933a486f493..f4b48ae916edab1024e806df4cdb342fa8a457aa 100644 (file)
@@ -7,7 +7,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,
    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 "dlinklist.h"
+#include "lib/util/dlinklist.h"
 #include "smbd/service_task.h"
+#include "smbd/service.h"
 #include "lib/messaging/irpc.h"
-#include "librpc/gen_ndr/ndr_winsrepl.h"
+#include "librpc/gen_ndr/winsrepl.h"
 #include "wrepl_server/wrepl_server.h"
 #include "nbt_server/wins/winsdb.h"
 #include "ldb/include/ldb.h"
 #include "ldb/include/ldb_errors.h"
 #include "auth/auth.h"
+#include "db_wrap.h"
+#include "param/param.h"
 
 static struct ldb_context *wins_config_db_connect(TALLOC_CTX *mem_ctx)
 {
-       return ldb_wrap_connect(mem_ctx, private_path(mem_ctx, lp_wins_config_url()),
+       return ldb_wrap_connect(mem_ctx, global_loadparm, private_path(mem_ctx, 
+                               global_loadparm, 
+                               lp_wins_config_url(global_loadparm)),
                                system_session(mem_ctx), NULL, 0, NULL);
 }
 
+static uint64_t wins_config_db_get_seqnumber(struct ldb_context *ldb)
+{
+       int ret;
+       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;
+}
+
 /*
   open winsdb
 */
 static NTSTATUS wreplsrv_open_winsdb(struct wreplsrv_service *service)
 {
-       service->wins_db     = winsdb_connect(service);
+       service->wins_db     = winsdb_connect(service, WINSDB_HANDLE_CALLER_WREPL);
        if (!service->wins_db) {
                return NT_STATUS_INTERNAL_DB_ERROR;
        }
@@ -53,26 +84,26 @@ static NTSTATUS wreplsrv_open_winsdb(struct wreplsrv_service *service)
        }
 
        /* the default renew interval is 6 days */
-       service->config.renew_interval    = lp_parm_int(-1,"wreplsrv","renew_interval", 6*24*60*60);
+       service->config.renew_interval    = lp_parm_int(global_loadparm, NULL,"wreplsrv","renew_interval", 6*24*60*60);
 
        /* the default tombstone (extinction) interval is 6 days */
-       service->config.tombstone_interval= lp_parm_int(-1,"wreplsrv","tombstone_interval", 6*24*60*60);
+       service->config.tombstone_interval= lp_parm_int(global_loadparm, NULL,"wreplsrv","tombstone_interval", 6*24*60*60);
 
        /* the default tombstone (extinction) timeout is 1 day */
-       service->config.tombstone_timeout = lp_parm_int(-1,"wreplsrv","tombstone_timeout", 1*24*60*60);
+       service->config.tombstone_timeout = lp_parm_int(global_loadparm, NULL,"wreplsrv","tombstone_timeout", 1*24*60*60);
 
        /* the default tombstone extra timeout is 3 days */
-       service->config.tombstone_extra_timeout = lp_parm_int(-1,"wreplsrv","tombstone_extra_timeout", 3*24*60*60);
+       service->config.tombstone_extra_timeout = lp_parm_int(global_loadparm, NULL,"wreplsrv","tombstone_extra_timeout", 3*24*60*60);
 
        /* the default verify interval is 24 days */
-       service->config.verify_interval   = lp_parm_int(-1,"wreplsrv","verify_interval", 24*24*60*60);
+       service->config.verify_interval   = lp_parm_int(global_loadparm, NULL,"wreplsrv","verify_interval", 24*24*60*60);
 
        /* the default scavenging interval is 'renew_interval/2' */
-       service->config.scavenging_interval=lp_parm_int(-1,"wreplsrv","scavenging_interval",
+       service->config.scavenging_interval=lp_parm_int(global_loadparm, NULL,"wreplsrv","scavenging_interval",
                                                        service->config.renew_interval/2);
 
        /* the maximun interval to the next periodic processing event */
-       service->config.periodic_interval = lp_parm_int(-1,"wreplsrv","periodic_interval", 60);
+       service->config.periodic_interval = lp_parm_int(global_loadparm, NULL,"wreplsrv","periodic_interval", 15);
 
        return NT_STATUS_OK;
 }
@@ -93,51 +124,88 @@ struct wreplsrv_partner *wreplsrv_find_partner(struct wreplsrv_service *service,
 /*
   load our replication partners
 */
-static NTSTATUS wreplsrv_load_partners(struct wreplsrv_service *service)
+NTSTATUS wreplsrv_load_partners(struct wreplsrv_service *service)
 {
+       struct wreplsrv_partner *partner;
        struct ldb_result *res = NULL;
        int ret;
-       TALLOC_CTX *tmp_ctx = talloc_new(service);
+       TALLOC_CTX *tmp_ctx;
        int i;
+       uint64_t new_seqnumber;
+
+       new_seqnumber = wins_config_db_get_seqnumber(service->config.ldb);
+
+       /* if it's not the first run and nothing changed we're done */
+       if (service->config.seqnumber != 0 && service->config.seqnumber == new_seqnumber) {
+               return NT_STATUS_OK;
+       }
+
+       tmp_ctx = talloc_new(service);
+       NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
+
+       service->config.seqnumber = new_seqnumber;
 
        /* find the record in the WINS database */
-       ret = ldb_search(service->config.ldb, ldb_dn_explode(tmp_ctx, "CN=PARTNERS"), LDB_SCOPE_SUBTREE,
+       ret = ldb_search(service->config.ldb, ldb_dn_new(tmp_ctx, service->config.ldb, "CN=PARTNERS"), LDB_SCOPE_SUBTREE,
                         "(objectClass=wreplPartner)", NULL, &res);
        if (ret != LDB_SUCCESS) goto failed;
        talloc_steal(tmp_ctx, res);
-       if (res->count == 0) goto done;
+
+       /* first disable all existing partners */
+       for (partner=service->partners; partner; partner = partner->next) {
+               partner->type = WINSREPL_PARTNER_NONE;
+       }
 
        for (i=0; i < res->count; i++) {
-               struct wreplsrv_partner *partner;
+               const char *address;
 
-               partner = talloc_zero(service, struct wreplsrv_partner);
-               if (partner == NULL) goto failed;
+               address = ldb_msg_find_attr_as_string(res->msgs[i], "address", NULL);
+               if (!address) {
+                       goto failed;
+               }
 
-               partner->service                = service;
-               partner->address                = ldb_msg_find_string(res->msgs[i], "address", NULL);
-               if (!partner->address) goto failed;
-               partner->name                   = ldb_msg_find_string(res->msgs[i], "name", partner->address);
-               partner->type                   = ldb_msg_find_uint(res->msgs[i], "type", WINSREPL_PARTNER_BOTH);
-               partner->pull.interval          = ldb_msg_find_uint(res->msgs[i], "pullInterval",
-                                                                   WINSREPL_DEFAULT_PULL_INTERVAL);
-               partner->pull.retry_interval    = ldb_msg_find_uint(res->msgs[i], "pullRetryInterval",
-                                                                   WINSREPL_DEFAULT_PULL_RETRY_INTERVAL);
-               partner->our_address            = ldb_msg_find_string(res->msgs[i], "ourAddress", NULL);
-               partner->push.change_count      = ldb_msg_find_uint(res->msgs[i], "pushChangeCount",
-                                                                   WINSREPL_DEFAULT_PUSH_CHANGE_COUNT);
-               partner->push.use_inform        = ldb_msg_find_uint(res->msgs[i], "pushUseInform", False);
+               partner = wreplsrv_find_partner(service, address);
+               if (partner) {
+                       if (partner->name != partner->address) {
+                               talloc_free(discard_const(partner->name));
+                       }
+                       partner->name = NULL;
+                       talloc_free(discard_const(partner->our_address));
+                       partner->our_address = NULL;
+
+                       /* force rescheduling of pulling */
+                       partner->pull.next_run = timeval_zero();
+               } else {
+                       partner = talloc_zero(service, struct wreplsrv_partner);
+                       if (partner == NULL) goto failed;
+
+                       partner->service = service;
+                       partner->address = address;
+                       talloc_steal(partner, partner->address);
+
+                       DLIST_ADD_END(service->partners, partner, struct wreplsrv_partner *);
+               }
 
-               talloc_steal(partner, partner->address);
+               partner->name                   = ldb_msg_find_attr_as_string(res->msgs[i], "name", partner->address);
                talloc_steal(partner, partner->name);
+               partner->our_address            = ldb_msg_find_attr_as_string(res->msgs[i], "ourAddress", NULL);
                talloc_steal(partner, partner->our_address);
 
-               DLIST_ADD(service->partners, partner);
+               partner->type                   = ldb_msg_find_attr_as_uint(res->msgs[i], "type", WINSREPL_PARTNER_BOTH);
+               partner->pull.interval          = ldb_msg_find_attr_as_uint(res->msgs[i], "pullInterval",
+                                                                   WINSREPL_DEFAULT_PULL_INTERVAL);
+               partner->pull.retry_interval    = ldb_msg_find_attr_as_uint(res->msgs[i], "pullRetryInterval",
+                                                                   WINSREPL_DEFAULT_PULL_RETRY_INTERVAL);
+               partner->push.change_count      = ldb_msg_find_attr_as_uint(res->msgs[i], "pushChangeCount",
+                                                                   WINSREPL_DEFAULT_PUSH_CHANGE_COUNT);
+               partner->push.use_inform        = ldb_msg_find_attr_as_uint(res->msgs[i], "pushUseInform", False);
 
                DEBUG(3,("wreplsrv_load_partners: found partner: %s type: 0x%X\n",
                        partner->address, partner->type));
        }
-done:
-       DEBUG(1,("wreplsrv_load_partners: %u partners found\n", res->count));
+
+       DEBUG(2,("wreplsrv_load_partners: %u partners found: wins_config_db seqnumber %llu\n",
+               res->count, (unsigned long long)service->config.seqnumber));
 
        talloc_free(tmp_ctx);
        return NT_STATUS_OK;
@@ -146,106 +214,74 @@ failed:
        return NT_STATUS_FOOBAR;
 }
 
-BOOL wreplsrv_is_our_address(struct wreplsrv_service *service, const char *address)
-{
-       const char *our_address;
-
-       if (lp_interfaces() && lp_bind_interfaces_only()) {
-               int num_interfaces = iface_count();
-               int i;
-               for(i = 0; i < num_interfaces; i++) {
-                       our_address = iface_n_ip(i);
-                       if (strcasecmp(our_address, address) == 0) {
-                               return True;
-                       }
-               }
-       } else {
-               our_address = lp_socket_address();
-               if (strcasecmp(our_address, address) == 0) {
-                       return True;
-               }
-       }
-
-       return False;
-}
-
-uint64_t wreplsrv_local_max_version(struct wreplsrv_service *service)
-{
-       int ret;
-       struct ldb_context *ldb = service->wins_db->ldb;
-       struct ldb_dn *dn;
-       struct ldb_result *res = NULL;
-       TALLOC_CTX *tmp_ctx = talloc_new(service);
-       uint64_t maxVersion = 0;
-
-       dn = ldb_dn_explode(tmp_ctx, "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_uint64(res->msgs[0], "maxVersion", 0);
-       }
-
-failed:
-       talloc_free(tmp_ctx);
-       return maxVersion;
-}
-
 NTSTATUS wreplsrv_fill_wrepl_table(struct wreplsrv_service *service,
                                   TALLOC_CTX *mem_ctx,
                                   struct wrepl_table *table_out,
-                                  const char *our_ip,
                                   const char *initiator,
                                   BOOL full_table)
 {
        struct wreplsrv_owner *cur;
-       uint64_t local_max_version;
        uint32_t i = 0;
 
        table_out->partner_count        = 0;
        table_out->partners             = NULL;
        table_out->initiator            = initiator;
 
-       local_max_version = wreplsrv_local_max_version(service);
-       if (local_max_version > 0) {
-               table_out->partner_count++;
-       }
+       for (cur = service->table; cur; cur = cur->next) {
+               if (full_table) {
+                       table_out->partner_count++;
+                       continue;
+               }
+
+               if (strcmp(initiator, cur->owner.address) != 0) continue;
 
-       for (cur = service->table; full_table && cur; cur = cur->next) {
                table_out->partner_count++;
+               break;
        }
 
        table_out->partners = talloc_array(mem_ctx, struct wrepl_wins_owner, table_out->partner_count);
        NT_STATUS_HAVE_NO_MEMORY(table_out->partners);
 
-       if (local_max_version > 0) {
-               table_out->partners[i].address          = our_ip;
-               table_out->partners[i].min_version      = 0;
-               table_out->partners[i].max_version      = local_max_version;
-               table_out->partners[i].type             = 1;
-               i++;
-       }
+       for (cur = service->table; cur && i < table_out->partner_count; cur = cur->next) {
+               /*
+                * if it's our local entry
+                * update the max version
+                */
+               if (cur == service->owner) {
+                       cur->owner.max_version = winsdb_get_maxVersion(service->wins_db);
+               }
+
+               if (full_table) {
+                       table_out->partners[i] = cur->owner;
+                       i++;
+                       continue;
+               }
+
+               if (strcmp(initiator, cur->owner.address) != 0) continue;
 
-       for (cur = service->table; full_table && cur; cur = cur->next) {
                table_out->partners[i] = cur->owner;
                i++;
+               break;
        }
 
        return NT_STATUS_OK;
 }
 
-struct wreplsrv_owner *wreplsrv_find_owner(struct wreplsrv_owner *table, const char *wins_owner)
+struct wreplsrv_owner *wreplsrv_find_owner(struct wreplsrv_service *service,
+                                          struct wreplsrv_owner *table,
+                                          const char *wins_owner)
 {
        struct wreplsrv_owner *cur;
 
        for (cur = table; cur; cur = cur->next) {
                if (strcmp(cur->owner.address, wins_owner) == 0) {
+                       /*
+                        * if it's our local entry
+                        * update the max version
+                        */
+                       if (cur == service->owner) {
+                               cur->owner.max_version = winsdb_get_maxVersion(service->wins_db);
+                       }
                        return cur;
                }
        }
@@ -264,11 +300,11 @@ NTSTATUS wreplsrv_add_table(struct wreplsrv_service *service,
        struct wreplsrv_owner *table = *_table;
        struct wreplsrv_owner *cur;
 
-       if (strcmp(WINSDB_OWNER_LOCAL, wins_owner) == 0) {
-               return NT_STATUS_OK;
+       if (!wins_owner || strcmp(wins_owner, "0.0.0.0") == 0) {
+               wins_owner = service->wins_db->local_owner;
        }
 
-       cur = wreplsrv_find_owner(table, wins_owner);
+       cur = wreplsrv_find_owner(service, table, wins_owner);
 
        /* if it doesn't exists yet, create one */
        if (!cur) {
@@ -283,15 +319,26 @@ NTSTATUS wreplsrv_add_table(struct wreplsrv_service *service,
 
                cur->partner            = wreplsrv_find_partner(service, wins_owner);
 
-               DLIST_ADD(table, cur);
+               DLIST_ADD_END(table, cur, struct wreplsrv_owner *);
                *_table = table;
        }
 
        /* the min_version is always 0 here, and won't be updated */
 
-       /* if the given version is higher the then current nax_version, update */
+       /* if the given version is higher than the current max_version, update */
        if (cur->owner.max_version < version) {
                cur->owner.max_version = version;
+               /* if it's for our local db, we need to update the wins.ldb too */
+               if (cur == service->owner) {
+                       uint64_t ret;
+                       ret = winsdb_set_maxVersion(service->wins_db, cur->owner.max_version);
+                       if (ret != cur->owner.max_version) {
+                               DEBUG(0,("winsdb_set_maxVersion(%llu) failed: %llu\n",
+                                        (unsigned long long)cur->owner.max_version, 
+                                        (unsigned long long)ret));
+                               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+                       }
+               }
        }
 
        return NT_STATUS_OK;
@@ -308,6 +355,7 @@ static NTSTATUS wreplsrv_load_table(struct wreplsrv_service *service)
        TALLOC_CTX *tmp_ctx = talloc_new(service);
        struct ldb_context *ldb = service->wins_db->ldb;
        int i;
+       struct wreplsrv_owner *local_owner;
        const char *wins_owner;
        uint64_t version;
        const char * const attrs[] = {
@@ -316,29 +364,54 @@ static NTSTATUS wreplsrv_load_table(struct wreplsrv_service *service)
                NULL
        };
 
+       /*
+        * make sure we have our local entry in the list,
+        * but we set service->owner when we're done
+        * to avoid to many calls to wreplsrv_local_max_version()
+        */
+       status = wreplsrv_add_table(service,
+                                   service, &service->table,
+                                   service->wins_db->local_owner, 0);
+       if (!NT_STATUS_IS_OK(status)) goto failed;
+       local_owner = wreplsrv_find_owner(service, service->table, service->wins_db->local_owner);
+       if (!local_owner) {
+               status = NT_STATUS_INTERNAL_ERROR;
+               goto failed;
+       }
+
        /* find the record in the WINS database */
        ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE,
                         "(objectClass=winsRecord)", attrs, &res);
        status = NT_STATUS_INTERNAL_DB_CORRUPTION;
        if (ret != LDB_SUCCESS) goto failed;
        talloc_steal(tmp_ctx, res);
-       if (res->count == 0) goto done;
 
        for (i=0; i < res->count; i++) {
-               wins_owner     = ldb_msg_find_string(res->msgs[i], "winsOwner", NULL);
-               version        = ldb_msg_find_uint64(res->msgs[i], "versionID", 0);
-
-               if (wins_owner) { 
-                       status = wreplsrv_add_table(service,
-                                                   service, &service->table,
-                                                   wins_owner, version);
-                       if (!NT_STATUS_IS_OK(status)) goto failed;
-               }
-               talloc_free(res->msgs[i]);
+               wins_owner     = ldb_msg_find_attr_as_string(res->msgs[i], "winsOwner", NULL);
+               version        = ldb_msg_find_attr_as_uint64(res->msgs[i], "versionID", 0);
 
-               /* TODO: what's abut the per address owners? */
+               status = wreplsrv_add_table(service,
+                                           service, &service->table,
+                                           wins_owner, version);
+               if (!NT_STATUS_IS_OK(status)) goto failed;
+               talloc_free(res->msgs[i]);
        }
-done:
+
+       /*
+        * this makes sure we call wreplsrv_local_max_version() before returning in
+        * wreplsrv_find_owner()
+        */
+       service->owner = local_owner;
+
+       /*
+        * this makes sure the maxVersion in the database is updated,
+        * with the highest version we found, if this is higher than the current stored one
+        */
+       status = wreplsrv_add_table(service,
+                                   service, &service->table,
+                                   service->wins_db->local_owner, local_owner->owner.max_version);
+       if (!NT_STATUS_IS_OK(status)) goto failed;
+
        talloc_free(tmp_ctx);
        return NT_STATUS_OK;
 failed:
@@ -370,6 +443,8 @@ static void wreplsrv_task_init(struct task_server *task)
        NTSTATUS status;
        struct wreplsrv_service *service;
 
+       task_server_set_title(task, "task[wreplsrv]");
+
        service = talloc_zero(task, struct wreplsrv_service);
        if (!service) {
                task_server_terminate(task, "wreplsrv_task_init: out of memory");
@@ -421,7 +496,7 @@ static void wreplsrv_task_init(struct task_server *task)
  */
 static NTSTATUS wreplsrv_init(struct event_context *event_ctx, const struct model_ops *model_ops)
 {
-       if (!lp_wins_support()) {
+       if (!lp_wins_support(global_loadparm)) {
                return NT_STATUS_OK;
        }