s4-dsdb: enable initial replication of partitions via DsReplicaSync
[ira/wip.git] / source4 / dsdb / repl / drepl_partitions.c
index 470a88f76de5d9d91b76faf638e87e9b589c7407..2aa2d7c8d68eb649576022f091c493ce8aaeb27d 100644 (file)
 #include "librpc/gen_ndr/ndr_drsblobs.h"
 #include "libcli/security/security.h"
 #include "param/param.h"
+#include "dsdb/common/util.h"
 
 WERROR dreplsrv_load_partitions(struct dreplsrv_service *s)
 {
        WERROR status;
-       static const char *attrs[] = { "namingContexts", NULL };
+       static const char *attrs[] = { "hasMasterNCs", "hasPartialReplicaNCs", NULL };
        unsigned int i;
        int ret;
        TALLOC_CTX *tmp_ctx;
        struct ldb_result *res;
        struct ldb_message_element *el;
+       struct ldb_dn *ntds_dn;
 
        tmp_ctx = talloc_new(s);
        W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
 
-       ret = ldb_search(s->samdb, tmp_ctx, &res,
-                        ldb_dn_new(tmp_ctx, s->samdb, ""), LDB_SCOPE_BASE, attrs, NULL);
+       ntds_dn = samdb_ntds_settings_dn(s->samdb);
+       if (!ntds_dn) {
+               DEBUG(1,(__location__ ": Unable to find ntds_dn: %s\n", ldb_errstring(s->samdb)));
+               talloc_free(tmp_ctx);
+               return WERR_DS_DRA_INTERNAL_ERROR;
+       }
+
+       ret = dsdb_search_dn(s->samdb, tmp_ctx, &res, ntds_dn, attrs, DSDB_SEARCH_SHOW_EXTENDED_DN);
        if (ret != LDB_SUCCESS) {
-               DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(s->samdb)));
+               DEBUG(1,("Searching for hasMasterNCs in NTDS DN failed: %s\n", ldb_errstring(s->samdb)));
+               talloc_free(tmp_ctx);
+               return WERR_DS_DRA_INTERNAL_ERROR;
+       }
+
+       el = ldb_msg_find_element(res->msgs[0], "hasMasterNCs");
+       if (!el) {
+               DEBUG(1,("Finding hasMasterNCs element in root_res failed: %s\n",
+                        ldb_errstring(s->samdb)));
                talloc_free(tmp_ctx);
                return WERR_DS_DRA_INTERNAL_ERROR;
-       }
+       }
+
+       for (i=0; i<el->num_values; i++) {
+               struct ldb_dn *pdn;
+               struct dreplsrv_partition *p;
 
-       el = ldb_msg_find_element(res->msgs[0], "namingContexts");
-       if (!el) {
-               DEBUG(1,("Finding namingContexts element in root_res failed: %s\n",
-                       ldb_errstring(s->samdb)));
-              talloc_free(tmp_ctx);
-              return WERR_DS_DRA_INTERNAL_ERROR;
-       }
+               pdn = ldb_dn_from_ldb_val(tmp_ctx, s->samdb, &el->values[i]);
+               if (pdn == NULL) {
+                       talloc_free(tmp_ctx);
+                       return WERR_DS_DRA_INTERNAL_ERROR;
+               }
+               if (!ldb_dn_validate(pdn)) {
+                       return WERR_DS_DRA_INTERNAL_ERROR;
+               }
+
+               p = talloc_zero(s, struct dreplsrv_partition);
+               W_ERROR_HAVE_NO_MEMORY(p);
+
+               p->dn = talloc_steal(p, pdn);
+               p->service = s;
+
+               DLIST_ADD(s->partitions, p);
 
-       for (i=0; i<el->num_values; i++) {
-              struct ldb_dn *pdn;
-              struct dreplsrv_partition *p;
+               DEBUG(2, ("dreplsrv_partition[%s] loaded\n", ldb_dn_get_linearized(p->dn)));
+       }
+
+       el = ldb_msg_find_element(res->msgs[0], "hasPartialReplicaNCs");
+
+       for (i=0; el && i<el->num_values; i++) {
+               struct ldb_dn *pdn;
+               struct dreplsrv_partition *p;
 
-              pdn = ldb_dn_from_ldb_val(tmp_ctx, s->samdb, &el->values[i]);
-              if (pdn == NULL) {
-                      talloc_free(tmp_ctx);
-                      return WERR_DS_DRA_INTERNAL_ERROR;
-              }
-              if (!ldb_dn_validate(pdn)) {
-                      return WERR_DS_DRA_INTERNAL_ERROR;
-              }
+               pdn = ldb_dn_from_ldb_val(tmp_ctx, s->samdb, &el->values[i]);
+               if (pdn == NULL) {
+                       talloc_free(tmp_ctx);
+                       return WERR_DS_DRA_INTERNAL_ERROR;
+               }
+               if (!ldb_dn_validate(pdn)) {
+                       return WERR_DS_DRA_INTERNAL_ERROR;
+               }
 
-              p = talloc_zero(s, struct dreplsrv_partition);
-              W_ERROR_HAVE_NO_MEMORY(p);
+               p = talloc_zero(s, struct dreplsrv_partition);
+               W_ERROR_HAVE_NO_MEMORY(p);
 
-              p->dn = talloc_steal(p, pdn);
+               p->dn = talloc_steal(p, pdn);
+               p->partial_replica = true;
+               p->service = s;
 
-              DLIST_ADD(s->partitions, p);
+               DLIST_ADD(s->partitions, p);
 
-              DEBUG(2, ("dreplsrv_partition[%s] loaded\n", ldb_dn_get_linearized(p->dn)));
+               DEBUG(2, ("dreplsrv_partition[%s] loaded (partial replica)\n", ldb_dn_get_linearized(p->dn)));
        }
 
        talloc_free(tmp_ctx);
@@ -107,6 +143,7 @@ NTSTATUS dreplsrv_get_target_principal(struct dreplsrv_service *s,
        int ret;
        const char *hostname;
        struct ldb_dn *dn;
+       struct ldb_dn *forest_dn;
 
        *target_principal = NULL;
 
@@ -148,9 +185,15 @@ NTSTATUS dreplsrv_get_target_principal(struct dreplsrv_service *s,
         * on it's record, it must also have GC/hostname/realm
         * servicePrincipalName */
 
+       forest_dn = ldb_get_root_basedn(s->samdb);
+       if (forest_dn == NULL) {
+               talloc_free(tmp_ctx);
+               return NT_STATUS_OK;
+       }
+
        *target_principal = talloc_asprintf(mem_ctx, "GC/%s/%s",
                                            hostname,
-                                           lpcfg_dnsdomain(s->task->lp_ctx));
+                                           samdb_dn_to_dns_domain(tmp_ctx, forest_dn));
        talloc_free(tmp_ctx);
        return NT_STATUS_OK;
 }
@@ -362,6 +405,48 @@ WERROR dreplsrv_partition_source_dsa_by_dns(const struct dreplsrv_partition *p,
 }
 
 
+/*
+  create a temporary dsa structure for a replication. This is needed
+  for the initial replication of a new partition, such as when a new
+  domain NC is created and we are a global catalog server
+ */
+WERROR dreplsrv_partition_source_dsa_temporary(struct dreplsrv_partition *p,
+                                              TALLOC_CTX *mem_ctx,
+                                              const struct GUID *dsa_guid,
+                                              struct dreplsrv_partition_source_dsa **_dsa)
+{
+       struct dreplsrv_partition_source_dsa *dsa;
+       WERROR werr;
+
+       dsa = talloc_zero(mem_ctx, struct dreplsrv_partition_source_dsa);
+       W_ERROR_HAVE_NO_MEMORY(dsa);
+
+       dsa->partition = p;
+       dsa->repsFrom1 = &dsa->_repsFromBlob.ctr.ctr1;
+       dsa->repsFrom1->replica_flags = 0;
+       dsa->repsFrom1->source_dsa_obj_guid = *dsa_guid;
+
+       dsa->repsFrom1->other_info = talloc_zero(dsa, struct repsFromTo1OtherInfo);
+       W_ERROR_HAVE_NO_MEMORY(dsa->repsFrom1->other_info);
+
+       dsa->repsFrom1->other_info->dns_name = samdb_ntds_msdcs_dns_name(p->service->samdb,
+                                                                        dsa->repsFrom1->other_info, dsa_guid);
+       W_ERROR_HAVE_NO_MEMORY(dsa->repsFrom1->other_info->dns_name);
+
+       werr = dreplsrv_out_connection_attach(p->service, dsa->repsFrom1, &dsa->conn);
+       if (!W_ERROR_IS_OK(werr)) {
+               DEBUG(0,(__location__ ": Failed to attach connection to %s\n",
+                        ldb_dn_get_linearized(p->dn)));
+               talloc_free(dsa);
+               return werr;
+       }
+
+       *_dsa = dsa;
+
+       return WERR_OK;
+}
+
+
 static WERROR dreplsrv_refresh_partition(struct dreplsrv_service *s,
                                         struct dreplsrv_partition *p)
 {