Move DRSUAPI per-attribute decryption into a common file
[samba.git] / source3 / libnet / libnet_dssync.c
index 068491e6bb0f4491c626c7183b65105b79ae9e47..59feac249569e41d7b397bc51f498248904c75dc 100644 (file)
@@ -3,6 +3,7 @@
 
    Copyright (C) Stefan (metze) Metzmacher 2005
    Copyright (C) Guenther Deschner 2008
+   Copyright (C) Michael Adam 2008
 
    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
@@ -21,6 +22,7 @@
 
 #include "includes.h"
 #include "libnet/libnet.h"
+#include "../libcli/drsuapi/drsuapi.h"
 
 /****************************************************************
 ****************************************************************/
@@ -50,6 +52,7 @@ NTSTATUS libnet_dssync_init_context(TALLOC_CTX *mem_ctx,
        NT_STATUS_HAVE_NO_MEMORY(ctx);
 
        talloc_set_destructor(ctx, libnet_dssync_free_context);
+       ctx->clean_old_entries = false;
 
        *ctx_p = ctx;
 
@@ -59,49 +62,6 @@ NTSTATUS libnet_dssync_init_context(TALLOC_CTX *mem_ctx,
 /****************************************************************
 ****************************************************************/
 
-static DATA_BLOB *decrypt_attr_val(TALLOC_CTX *mem_ctx,
-                                  DATA_BLOB *session_key,
-                                  uint32_t rid,
-                                  enum drsuapi_DsAttributeId id,
-                                  DATA_BLOB *raw_data)
-{
-       bool rcrypt = false;
-       DATA_BLOB out_data;
-
-       ZERO_STRUCT(out_data);
-
-       switch (id) {
-               case DRSUAPI_ATTRIBUTE_dBCSPwd:
-               case DRSUAPI_ATTRIBUTE_unicodePwd:
-               case DRSUAPI_ATTRIBUTE_ntPwdHistory:
-               case DRSUAPI_ATTRIBUTE_lmPwdHistory:
-                       rcrypt  = true;
-                       break;
-               case DRSUAPI_ATTRIBUTE_supplementalCredentials:
-               case DRSUAPI_ATTRIBUTE_priorValue:
-               case DRSUAPI_ATTRIBUTE_currentValue:
-               case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
-               case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
-               case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
-               case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
-                       break;
-               default:
-                       return raw_data;
-       }
-
-       out_data = decrypt_drsuapi_blob(mem_ctx, session_key, rcrypt,
-                                         rid, raw_data);
-
-       if (out_data.length) {
-               return (DATA_BLOB *)talloc_memdup(mem_ctx, &out_data, sizeof(DATA_BLOB));
-       }
-
-       return raw_data;
-}
-
-/****************************************************************
-****************************************************************/
-
 static void parse_obj_identifier(struct drsuapi_DsReplicaObjectIdentifier *id,
                                 uint32_t *rid)
 {
@@ -119,30 +79,6 @@ static void parse_obj_identifier(struct drsuapi_DsReplicaObjectIdentifier *id,
 /****************************************************************
 ****************************************************************/
 
-static void parse_obj_attribute(TALLOC_CTX *mem_ctx,
-                               DATA_BLOB *session_key,
-                               uint32_t rid,
-                               struct drsuapi_DsReplicaAttribute *attr)
-{
-       int i = 0;
-
-       for (i=0; i<attr->value_ctr.num_values; i++) {
-
-               DATA_BLOB *plain_data = NULL;
-
-               plain_data = decrypt_attr_val(mem_ctx,
-                                             session_key,
-                                             rid,
-                                             attr->attid,
-                                             attr->value_ctr.values[i].blob);
-
-               attr->value_ctr.values[i].blob = plain_data;
-       }
-}
-
-/****************************************************************
-****************************************************************/
-
 static void libnet_dssync_decrypt_attributes(TALLOC_CTX *mem_ctx,
                                             DATA_BLOB *session_key,
                                             struct drsuapi_DsReplicaObjectListItemEx *cur)
@@ -168,10 +104,10 @@ static void libnet_dssync_decrypt_attributes(TALLOC_CTX *mem_ctx,
                                continue;
                        }
 
-                       parse_obj_attribute(mem_ctx,
-                                           session_key,
-                                           rid,
-                                           attr);
+                       drsuapi_decrypt_attribute(mem_ctx,
+                                                 session_key,
+                                                 rid,
+                                                 attr);
                }
        }
 }
@@ -295,7 +231,7 @@ static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
        req.req1.count          = 1;
        req.req1.names          = names;
        req.req1.format_flags   = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
-       req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_UKNOWN;
+       req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_UNKNOWN;
        req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
 
        status = rpccli_drsuapi_DsCrackNames(ctx->cli, mem_ctx,
@@ -306,7 +242,7 @@ static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
                                             &ctr,
                                             &werr);
        if (!NT_STATUS_IS_OK(status)) {
-               ctx->error_message = talloc_asprintf(mem_ctx,
+               ctx->error_message = talloc_asprintf(ctx,
                        "Failed to lookup DN for domain name: %s",
                        get_friendly_werror_msg(werr));
                return status;
@@ -363,11 +299,12 @@ static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
                                            struct dssync_context *ctx,
                                            const char *dn,
                                            struct replUpToDateVectorBlob *utdv,
-                                           int32_t level,
+                                           int32_t *plevel,
                                            union drsuapi_DsGetNCChangesRequest *preq)
 {
        NTSTATUS status;
        uint32_t count;
+       int32_t level;
        union drsuapi_DsGetNCChangesRequest req;
        struct dom_sid null_sid;
        enum drsuapi_DsExtendedOperation extended_op;
@@ -383,6 +320,14 @@ static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(null_sid);
        ZERO_STRUCT(req);
 
+       if (ctx->remote_info28.supported_extensions
+           & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8)
+       {
+               level = 8;
+       } else {
+               level = 5;
+       }
+
        nc = TALLOC_ZERO_P(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
        if (!nc) {
                status = NT_STATUS_NO_MEMORY;
@@ -392,7 +337,9 @@ static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
        nc->guid = GUID_zero();
        nc->sid = null_sid;
 
-       if (!ctx->single && !ctx->repl_nodiff && utdv) {
+       if (!ctx->single_object_replication &&
+           !ctx->force_full_replication && utdv)
+       {
                cursors = TALLOC_ZERO_P(mem_ctx,
                                         struct drsuapi_DsReplicaCursorCtrEx);
                if (!cursors) {
@@ -424,7 +371,7 @@ static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
                }
        }
 
-       if (ctx->single) {
+       if (ctx->single_object_replication) {
                extended_op = DRSUAPI_EXOP_REPL_OBJ;
        } else {
                extended_op = DRSUAPI_EXOP_NONE;
@@ -449,6 +396,10 @@ static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
                goto fail;
        }
 
+       if (plevel) {
+               *plevel = level;
+       }
+
        if (preq) {
                *preq = req;
        }
@@ -461,81 +412,54 @@ fail:
        return status;
 }
 
-static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
-                                     struct dssync_context *ctx)
+static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
+                                          struct dssync_context *ctx,
+                                          int32_t level,
+                                          union drsuapi_DsGetNCChangesRequest *req,
+                                          struct replUpToDateVectorBlob **pnew_utdv)
 {
        NTSTATUS status;
        WERROR werr;
-
-       int32_t level;
-       int32_t level_out = 0;
-       union drsuapi_DsGetNCChangesRequest req;
        union drsuapi_DsGetNCChangesCtr ctr;
-
        struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
        struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
-       struct replUpToDateVectorBlob *old_utdv = NULL;
-       struct replUpToDateVectorBlob new_utdv;
-       struct replUpToDateVectorBlob *pnew_utdv = NULL;
+       struct replUpToDateVectorBlob *new_utdv = NULL;
+       int32_t level_out = 0;
        int32_t out_level = 0;
        int y;
        bool last_query;
-       const char *dn;
-
-       status = ctx->ops->startup(ctx, mem_ctx, &old_utdv);
-       if (!NT_STATUS_IS_OK(status)) {
-               ctx->error_message = talloc_asprintf(mem_ctx,
-                       "Failed to call startup operation: %s",
-                       nt_errstr(status));
-               goto out;
-       }
-
-       if (ctx->remote_info28.supported_extensions
-           & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8)
-       {
-               level = 8;
-       } else {
-               level = 5;
-       }
-
-       if (ctx->single && ctx->object_dn) {
-               dn = ctx->object_dn;
-       } else {
-               dn = ctx->nc_dn;
-       }
 
-       status = libnet_dssync_build_request(mem_ctx, ctx, dn, old_utdv, level,
-                                            &req);
-       if (!NT_STATUS_IS_OK(status)) {
-               ctx->error_message = talloc_asprintf(mem_ctx,
-                       "Failed to build DsGetNCChanges request: %s",
-                       nt_errstr(status));
-               goto out;
+       if (!ctx->single_object_replication) {
+               new_utdv = TALLOC_ZERO_P(mem_ctx, struct replUpToDateVectorBlob);
+               if (!new_utdv) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
        }
 
        for (y=0, last_query = false; !last_query; y++) {
-               struct drsuapi_DsReplicaObjectListItemEx *first_object;
-               struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr;
+               struct drsuapi_DsReplicaObjectListItemEx *first_object = NULL;
+               struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr = NULL;
 
                if (level == 8) {
                        DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
-                               (long long)req.req8.highwatermark.tmp_highest_usn,
-                               (long long)req.req8.highwatermark.highest_usn));
+                               (long long)req->req8.highwatermark.tmp_highest_usn,
+                               (long long)req->req8.highwatermark.highest_usn));
                } else if (level == 5) {
                        DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
-                               (long long)req.req5.highwatermark.tmp_highest_usn,
-                               (long long)req.req5.highwatermark.highest_usn));
+                               (long long)req->req5.highwatermark.tmp_highest_usn,
+                               (long long)req->req5.highwatermark.highest_usn));
                }
 
                status = rpccli_drsuapi_DsGetNCChanges(ctx->cli, mem_ctx,
                                                       &ctx->bind_handle,
                                                       level,
-                                                      &req,
+                                                      req,
                                                       &level_out,
                                                       &ctr,
                                                       &werr);
                if (!NT_STATUS_IS_OK(status)) {
-                       ctx->error_message = talloc_asprintf(mem_ctx,
+                       ctx->error_message = talloc_asprintf(ctx,
                                "Failed to get NC Changes: %s",
                                get_friendly_werror_msg(werr));
                        goto out;
@@ -549,17 +473,24 @@ static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
                if (level_out == 1) {
                        out_level = 1;
                        ctr1 = &ctr.ctr1;
-               } else if (level_out == 2) {
+               } else if (level_out == 2 && ctr.ctr2.mszip1.ts) {
                        out_level = 1;
-                       ctr1 = ctr.ctr2.ctr.mszip1.ctr1;
+                       ctr1 = &ctr.ctr2.mszip1.ts->ctr1;
                } else if (level_out == 6) {
                        out_level = 6;
                        ctr6 = &ctr.ctr6;
                } else if (level_out == 7
                           && ctr.ctr7.level == 6
-                          && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP) {
+                          && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
+                          && ctr.ctr7.ctr.mszip6.ts) {
+                       out_level = 6;
+                       ctr6 = &ctr.ctr7.ctr.mszip6.ts->ctr6;
+               } else if (level_out == 7
+                          && ctr.ctr7.level == 6
+                          && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
+                          && ctr.ctr7.ctr.xpress6.ts) {
                        out_level = 6;
-                       ctr6 = ctr.ctr7.ctr.mszip6.ctr6;
+                       ctr6 = &ctr.ctr7.ctr.xpress6.ts->ctr6;
                }
 
                if (out_level == 1) {
@@ -571,15 +502,16 @@ static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
                        mapping_ctr = &ctr1->mapping_ctr;
 
                        if (ctr1->more_data) {
-                               req.req5.highwatermark = ctr1->new_highwatermark;
+                               req->req5.highwatermark = ctr1->new_highwatermark;
                        } else {
                                last_query = true;
-                               ZERO_STRUCT(new_utdv);
-                               new_utdv.version = 1;
-                               if (ctr1->uptodateness_vector) {
-                                       new_utdv.ctr.ctr1.count =
+                               if (ctr1->uptodateness_vector &&
+                                   !ctx->single_object_replication)
+                               {
+                                       new_utdv->version = 1;
+                                       new_utdv->ctr.ctr1.count =
                                                ctr1->uptodateness_vector->count;
-                                       new_utdv.ctr.ctr1.cursors =
+                                       new_utdv->ctr.ctr1.cursors =
                                                ctr1->uptodateness_vector->cursors;
                                }
                        }
@@ -592,15 +524,16 @@ static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
                        mapping_ctr = &ctr6->mapping_ctr;
 
                        if (ctr6->more_data) {
-                               req.req8.highwatermark = ctr6->new_highwatermark;
+                               req->req8.highwatermark = ctr6->new_highwatermark;
                        } else {
                                last_query = true;
-                               ZERO_STRUCT(new_utdv);
-                               new_utdv.version = 2;
-                               if (ctr6->uptodateness_vector) {
-                                       new_utdv.ctr.ctr2.count =
+                               if (ctr6->uptodateness_vector &&
+                                   !ctx->single_object_replication)
+                               {
+                                       new_utdv->version = 2;
+                                       new_utdv->ctr.ctr2.count =
                                                ctr6->uptodateness_vector->count;
-                                       new_utdv.ctr.ctr2.cursors =
+                                       new_utdv->ctr.ctr2.cursors =
                                                ctr6->uptodateness_vector->cursors;
                                }
                        }
@@ -608,7 +541,7 @@ static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
 
                status = cli_get_session_key(mem_ctx, ctx->cli, &ctx->session_key);
                if (!NT_STATUS_IS_OK(status)) {
-                       ctx->error_message = talloc_asprintf(mem_ctx,
+                       ctx->error_message = talloc_asprintf(ctx,
                                "Failed to get Session Key: %s",
                                nt_errstr(status));
                        goto out;
@@ -623,7 +556,7 @@ static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
                                                           first_object,
                                                           mapping_ctr);
                        if (!NT_STATUS_IS_OK(status)) {
-                               ctx->error_message = talloc_asprintf(mem_ctx,
+                               ctx->error_message = talloc_asprintf(ctx,
                                        "Failed to call processing function: %s",
                                        nt_errstr(status));
                                goto out;
@@ -631,16 +564,70 @@ static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
                }
        }
 
-       if (!ctx->single) {
-               pnew_utdv = &new_utdv;
+       *pnew_utdv = new_utdv;
+
+out:
+       return status;
+}
+
+static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
+                                     struct dssync_context *ctx)
+{
+       NTSTATUS status;
+
+       int32_t level = 0;
+       union drsuapi_DsGetNCChangesRequest req;
+       struct replUpToDateVectorBlob *old_utdv = NULL;
+       struct replUpToDateVectorBlob *pnew_utdv = NULL;
+       const char **dns;
+       uint32_t dn_count;
+       uint32_t count;
+
+       if (ctx->ops->startup) {
+               status = ctx->ops->startup(ctx, mem_ctx, &old_utdv);
+               if (!NT_STATUS_IS_OK(status)) {
+                       ctx->error_message = talloc_asprintf(ctx,
+                               "Failed to call startup operation: %s",
+                               nt_errstr(status));
+                       goto out;
+               }
        }
 
-       status = ctx->ops->finish(ctx, mem_ctx, pnew_utdv);
-       if (!NT_STATUS_IS_OK(status)) {
-               ctx->error_message = talloc_asprintf(mem_ctx,
-                       "Failed to call finishing operation: %s",
-                       nt_errstr(status));
-               goto out;
+       if (ctx->single_object_replication && ctx->object_dns) {
+               dns = ctx->object_dns;
+               dn_count = ctx->object_count;
+       } else {
+               dns = &ctx->nc_dn;
+               dn_count = 1;
+       }
+
+       for (count=0; count < dn_count; count++) {
+               status = libnet_dssync_build_request(mem_ctx, ctx,
+                                                    dns[count],
+                                                    old_utdv, &level,
+                                                    &req);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto out;
+               }
+
+               status = libnet_dssync_getncchanges(mem_ctx, ctx, level, &req,
+                                                   &pnew_utdv);
+               if (!NT_STATUS_IS_OK(status)) {
+                       ctx->error_message = talloc_asprintf(ctx,
+                               "Failed to call DsGetNCCHanges: %s",
+                               nt_errstr(status));
+                       goto out;
+               }
+       }
+
+       if (ctx->ops->finish) {
+               status = ctx->ops->finish(ctx, mem_ctx, pnew_utdv);
+               if (!NT_STATUS_IS_OK(status)) {
+                       ctx->error_message = talloc_asprintf(ctx,
+                               "Failed to call finishing operation: %s",
+                               nt_errstr(status));
+                       goto out;
+               }
        }
 
  out:
@@ -654,18 +641,25 @@ NTSTATUS libnet_dssync(TALLOC_CTX *mem_ctx,
                       struct dssync_context *ctx)
 {
        NTSTATUS status;
+       TALLOC_CTX *tmp_ctx;
+
+       tmp_ctx = talloc_new(mem_ctx);
+       if (!tmp_ctx) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
-       status = libnet_dssync_init(mem_ctx, ctx);
+       status = libnet_dssync_init(tmp_ctx, ctx);
        if (!NT_STATUS_IS_OK(status)) {
                goto out;
        }
 
-       status = libnet_dssync_process(mem_ctx, ctx);
+       status = libnet_dssync_process(tmp_ctx, ctx);
        if (!NT_STATUS_IS_OK(status)) {
                goto out;
        }
 
  out:
+       TALLOC_FREE(tmp_ctx);
        return status;
 }