s4-dsdb: some more attribuutes that we should only give if asked for
[ira/wip.git] / source4 / dsdb / kcc / kcc_periodic.c
index 649efd51caab2fde4815592582de9456b5fc70f9..097fff41ecf5a083f8470db59f4b2deda8d4e024 100644 (file)
 #include "param/param.h"
 
 /*
- * add a repsFrom to all our partitions
+ * see if a repsFromToBlob is in a list
  */
+static bool reps_in_list(struct repsFromToBlob *r, struct repsFromToBlob *reps, uint32_t count)
+{
+       int i;
+       for (i=0; i<count; i++) {
+               if (strcmp(r->ctr.ctr1.other_info->dns_name, 
+                          reps[i].ctr.ctr1.other_info->dns_name) == 0 &&
+                   GUID_compare(&r->ctr.ctr1.source_dsa_obj_guid, 
+                                &reps[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
+                       return true;
+               }
+       }
+       return false;
+}
 
 
+/*
+ * add any missing repsFrom structures to our partitions
+ */
+static NTSTATUS kccsrv_add_repsFrom(struct kccsrv_service *s, TALLOC_CTX *mem_ctx,
+                                   struct repsFromToBlob *reps, uint32_t count)
+{
+       struct kccsrv_partition *p;
+
+       /* update the repsFrom on all partitions */
+       for (p=s->partitions; p; p=p->next) {
+               struct repsFromToBlob *old_reps;
+               uint32_t old_count;
+               WERROR werr;
+               int i;
+               bool modified = false;
+
+               werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsFrom", &old_reps, &old_count);
+               if (!W_ERROR_IS_OK(werr)) {
+                       DEBUG(0,(__location__ ": Failed to load repsFrom from %s - %s\n", 
+                                ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
+                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               }
+
+               /* add any new ones */
+               for (i=0; i<count; i++) {
+                       if (!reps_in_list(&reps[i], old_reps, old_count)) {
+                               old_reps = talloc_realloc(mem_ctx, old_reps, struct repsFromToBlob, old_count+1);
+                               NT_STATUS_HAVE_NO_MEMORY(old_reps);
+                               old_reps[old_count] = reps[i];
+                               old_count++;
+                               modified = true;
+                       }
+               }
+
+               /* remove any stale ones */
+               for (i=0; i<old_count; i++) {
+                       if (!reps_in_list(&old_reps[i], reps, count)) {
+                               memmove(&old_reps[i], &old_reps[i+1], (old_count-(i+1))*sizeof(old_reps[0]));
+                               old_count--;
+                               i--;
+                               modified = true;
+                       }
+               }
+               
+               if (modified) {
+                       werr = dsdb_savereps(s->samdb, mem_ctx, p->dn, "repsFrom", old_reps, old_count);
+                       if (!W_ERROR_IS_OK(werr)) {
+                               DEBUG(0,(__location__ ": Failed to save repsFrom to %s - %s\n", 
+                                        ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
+                               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+                       }
+               }
+       }
+
+       return NT_STATUS_OK;
+
+}
+
 /*
   this is the core of our initial simple KCC
   We just add a repsFrom entry for all DCs we find that have nTDSDSA
   objects, except for ourselves
  */
-static NTSTATUS kccsrv_simple_update(struct kccsrv_service *s, TALLOC_CTX *mem_ctx)
+NTSTATUS kccsrv_simple_update(struct kccsrv_service *s, TALLOC_CTX *mem_ctx)
 {
        struct ldb_result *res;
        int ret, i;
        const char *attrs[] = { "objectGUID", "invocationID", NULL };
-       struct ldb_message *msg;
-       struct ldb_message_element *el;
-       struct kccsrv_partition *p;
+       struct repsFromToBlob *reps = NULL;
+       uint32_t count = 0;
 
        ret = ldb_search(s->samdb, mem_ctx, &res, s->config_dn, LDB_SCOPE_SUBTREE, 
                         attrs, "objectClass=nTDSDSA");
@@ -60,21 +130,9 @@ static NTSTATUS kccsrv_simple_update(struct kccsrv_service *s, TALLOC_CTX *mem_c
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
        }
 
-       msg = ldb_msg_new(mem_ctx);
-       NT_STATUS_HAVE_NO_MEMORY(msg);
-
-       ret = ldb_msg_add_empty(msg, "repsFrom", LDB_FLAG_MOD_REPLACE, &el);
-       if (ret != LDB_SUCCESS) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
        for (i=0; i<res->count; i++) {
-               struct repsFromToBlob r;
                struct repsFromTo1 *r1;
-               struct repsFromTo1OtherInfo oi;
                struct GUID ntds_guid, invocation_id;
-               struct ldb_val v;
-               enum ndr_err_code ndr_err;
 
                ntds_guid = samdb_result_guid(res->msgs[i], "objectGUID");
                if (GUID_compare(&ntds_guid, &s->ntds_guid) == 0) {
@@ -84,15 +142,17 @@ static NTSTATUS kccsrv_simple_update(struct kccsrv_service *s, TALLOC_CTX *mem_c
 
                invocation_id = samdb_result_guid(res->msgs[i], "invocationID");
 
-               ZERO_STRUCT(r);
-               ZERO_STRUCT(oi);
-               r.version = 1;
-               r1 = &r.ctr.ctr1;
+               reps = talloc_realloc(mem_ctx, reps, struct repsFromToBlob, count+1);
+               NT_STATUS_HAVE_NO_MEMORY(reps);
+
+               ZERO_STRUCT(reps[count]);
+               reps[count].version = 1;
+               r1 = &reps[count].ctr.ctr1;
 
-               oi.dns_name                  = talloc_asprintf(mem_ctx, "%s._msdcs.%s",
+               r1->other_info               = talloc_zero(reps, struct repsFromTo1OtherInfo);
+               r1->other_info->dns_name     = talloc_asprintf(r1->other_info, "%s._msdcs.%s",
                                                               GUID_string(mem_ctx, &ntds_guid),
-                                                              lp_realm(s->task->lp_ctx));
-               r1->other_info               = &oi;
+                                                              lp_dnsdomain(s->task->lp_ctx));
                r1->source_dsa_obj_guid      = ntds_guid;
                r1->source_dsa_invocation_id = invocation_id;
                r1->replica_flags            = 
@@ -100,35 +160,10 @@ static NTSTATUS kccsrv_simple_update(struct kccsrv_service *s, TALLOC_CTX *mem_c
                        DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP | 
                        DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS;
                memset(r1->schedule, 0x11, sizeof(r1->schedule));
-
-
-               ndr_err = ndr_push_struct_blob(&v, mem_ctx, 
-                                              lp_iconv_convenience(s->task->lp_ctx),
-                                              &r,
-                                              (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
-               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-                       DEBUG(0,(__location__ ": Failed tp push repsFrom blob\n"));
-                       return NT_STATUS_INTERNAL_ERROR;
-               }
-
-               el->values = talloc_realloc(msg, el->values, struct ldb_val, el->num_values+1);
-               NT_STATUS_HAVE_NO_MEMORY(el->values);
-               el->values[el->num_values] = v;
-               el->num_values++;
+               count++;
        }
 
-       /* replace the repsFrom on all partitions */
-       for (p=s->partitions; p; p=p->next) {
-               msg->dn = p->dn;
-               ret = ldb_modify(s->samdb, msg);
-               if (ret != LDB_SUCCESS) {
-                       DEBUG(0,(__location__ ": Failed to store repsFrom for %s - %s\n",
-                                ldb_dn_get_linearized(msg->dn), ldb_errstring(s->samdb)));
-                       return NT_STATUS_INTERNAL_ERROR;
-               }
-       }
-
-       return NT_STATUS_OK;
+       return kccsrv_add_repsFrom(s, mem_ctx, reps, count);
 }
 
 
@@ -146,7 +181,7 @@ static void kccsrv_periodic_handler_te(struct tevent_context *ev, struct tevent_
 
        status = kccsrv_periodic_schedule(service, service->periodic.interval);
        if (!W_ERROR_IS_OK(status)) {
-               task_server_terminate(service->task, win_errstr(status));
+               task_server_terminate(service->task, win_errstr(status), true);
                return;
        }
 }