werror: replace WERR_NO_SUCH_SERVICE with WERR_SERVICE_DOES_NOT_EXIST in source3...
[samba.git] / source3 / libnet / libnet_dssync.c
index 81866c85ba590624929b63209b181f1b1f08a957..267709e3c1176975b2a6bd305e10a6267895f509 100644 (file)
 
 
 #include "includes.h"
-#include "libnet/libnet.h"
+#include "libnet/libnet_dssync.h"
+#include "rpc_client/cli_pipe.h"
+#include "../libcli/drsuapi/drsuapi.h"
+#include "../librpc/gen_ndr/ndr_drsuapi_c.h"
 
 /****************************************************************
 ****************************************************************/
 
 static int libnet_dssync_free_context(struct dssync_context *ctx)
 {
+       WERROR result;
+       struct dcerpc_binding_handle *b;
+
        if (!ctx) {
                return 0;
        }
 
        if (is_valid_policy_hnd(&ctx->bind_handle) && ctx->cli) {
-               rpccli_drsuapi_DsUnbind(ctx->cli, ctx, &ctx->bind_handle, NULL);
+               b = ctx->cli->binding_handle;
+               dcerpc_drsuapi_DsUnbind(b, ctx, &ctx->bind_handle, &result);
        }
 
        return 0;
@@ -47,7 +54,7 @@ NTSTATUS libnet_dssync_init_context(TALLOC_CTX *mem_ctx,
 {
        struct dssync_context *ctx;
 
-       ctx = TALLOC_ZERO_P(mem_ctx, struct dssync_context);
+       ctx = talloc_zero(mem_ctx, struct dssync_context);
        NT_STATUS_HAVE_NO_MEMORY(ctx);
 
        talloc_set_destructor(ctx, libnet_dssync_free_context);
@@ -61,49 +68,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)
 {
@@ -121,30 +85,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)
@@ -170,10 +110,11 @@ 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,
+                                                 0,
+                                                 attr);
                }
        }
 }
@@ -189,6 +130,7 @@ static NTSTATUS libnet_dssync_bind(TALLOC_CTX *mem_ctx,
        struct GUID bind_guid;
        struct drsuapi_DsBindInfoCtr bind_info;
        struct drsuapi_DsBindInfo28 info28;
+       struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
 
        ZERO_STRUCT(info28);
 
@@ -229,7 +171,7 @@ static NTSTATUS libnet_dssync_bind(TALLOC_CTX *mem_ctx,
        bind_info.length = 28;
        bind_info.info.info28 = info28;
 
-       status = rpccli_drsuapi_DsBind(ctx->cli, mem_ctx,
+       status = dcerpc_drsuapi_DsBind(b, mem_ctx,
                                       &bind_guid,
                                       &bind_info,
                                       &ctx->bind_handle,
@@ -254,9 +196,19 @@ static NTSTATUS libnet_dssync_bind(TALLOC_CTX *mem_ctx,
                ctx->remote_info28.repl_epoch           = 0;
                break;
        }
-       case 28:
+       case 28: {
                ctx->remote_info28 = bind_info.info.info28;
                break;
+       }
+       case 32: {
+               struct drsuapi_DsBindInfo32 *info32;
+               info32 = &bind_info.info.info32;
+               ctx->remote_info28.site_guid            = info32->site_guid;
+               ctx->remote_info28.supported_extensions = info32->supported_extensions;
+               ctx->remote_info28.pid                  = info32->pid;
+               ctx->remote_info28.repl_epoch           = info32->repl_epoch;
+               break;
+       }
        case 48: {
                struct drsuapi_DsBindInfo48 *info48;
                info48 = &bind_info.info.info48;
@@ -266,6 +218,15 @@ static NTSTATUS libnet_dssync_bind(TALLOC_CTX *mem_ctx,
                ctx->remote_info28.repl_epoch           = info48->repl_epoch;
                break;
        }
+       case 52: {
+               struct drsuapi_DsBindInfo52 *info52;
+               info52 = &bind_info.info.info52;
+               ctx->remote_info28.site_guid            = info52->site_guid;
+               ctx->remote_info28.supported_extensions = info52->supported_extensions;
+               ctx->remote_info28.pid                  = info52->pid;
+               ctx->remote_info28.repl_epoch           = info52->repl_epoch;
+               break;
+       }
        default:
                DEBUG(1, ("Warning: invalid info length in bind info: %d\n",
                          bind_info.length));
@@ -283,11 +244,12 @@ static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
 {
        NTSTATUS status;
        WERROR werr;
-       int32_t level = 1;
+       uint32_t level = 1;
        union drsuapi_DsNameRequest req;
-       int32_t level_out;
+       uint32_t level_out;
        struct drsuapi_DsNameString names[1];
        union drsuapi_DsNameCtr ctr;
+       struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
 
        names[0].str = talloc_asprintf(mem_ctx, "%s\\", ctx->domain_name);
        NT_STATUS_HAVE_NO_MEMORY(names[0].str);
@@ -300,7 +262,7 @@ static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
        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,
+       status = dcerpc_drsuapi_DsCrackNames(b, mem_ctx,
                                             &ctx->bind_handle,
                                             level,
                                             &req,
@@ -310,11 +272,14 @@ static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
        if (!NT_STATUS_IS_OK(status)) {
                ctx->error_message = talloc_asprintf(ctx,
                        "Failed to lookup DN for domain name: %s",
-                       get_friendly_werror_msg(werr));
+                       get_friendly_nt_error_msg(status));
                return status;
        }
 
        if (!W_ERROR_IS_OK(werr)) {
+               ctx->error_message = talloc_asprintf(ctx,
+                       "Failed to lookup DN for domain name: %s",
+                       get_friendly_werror_msg(werr));
                return werror_to_ntstatus(werr);
        }
 
@@ -365,23 +330,23 @@ static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
                                            struct dssync_context *ctx,
                                            const char *dn,
                                            struct replUpToDateVectorBlob *utdv,
-                                           int32_t *plevel,
+                                           uint32_t *plevel,
                                            union drsuapi_DsGetNCChangesRequest *preq)
 {
        NTSTATUS status;
        uint32_t count;
-       int32_t level;
+       uint32_t level;
        union drsuapi_DsGetNCChangesRequest req;
        struct dom_sid null_sid;
        enum drsuapi_DsExtendedOperation extended_op;
        struct drsuapi_DsReplicaObjectIdentifier *nc = NULL;
        struct drsuapi_DsReplicaCursorCtrEx *cursors = NULL;
 
-       uint32_t replica_flags  = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE |
-                                 DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP |
-                                 DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS |
-                                 DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS |
-                                 DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED;
+       uint32_t replica_flags  = DRSUAPI_DRS_WRIT_REP |
+                                 DRSUAPI_DRS_INIT_SYNC |
+                                 DRSUAPI_DRS_PER_SYNC |
+                                 DRSUAPI_DRS_GET_ANC |
+                                 DRSUAPI_DRS_NEVER_SYNCED;
 
        ZERO_STRUCT(null_sid);
        ZERO_STRUCT(req);
@@ -394,7 +359,7 @@ static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
                level = 5;
        }
 
-       nc = TALLOC_ZERO_P(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
+       nc = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
        if (!nc) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -406,7 +371,7 @@ static NTSTATUS libnet_dssync_build_request(TALLOC_CTX *mem_ctx,
        if (!ctx->single_object_replication &&
            !ctx->force_full_replication && utdv)
        {
-               cursors = TALLOC_ZERO_P(mem_ctx,
+               cursors = talloc_zero(mem_ctx,
                                         struct drsuapi_DsReplicaCursorCtrEx);
                if (!cursors) {
                        status = NT_STATUS_NO_MEMORY;
@@ -480,7 +445,7 @@ fail:
 
 static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
                                           struct dssync_context *ctx,
-                                          int32_t level,
+                                          uint32_t level,
                                           union drsuapi_DsGetNCChangesRequest *req,
                                           struct replUpToDateVectorBlob **pnew_utdv)
 {
@@ -490,13 +455,14 @@ static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
        struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
        struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
        struct replUpToDateVectorBlob *new_utdv = NULL;
-       int32_t level_out = 0;
-       int32_t out_level = 0;
+       uint32_t level_out = 0;
+       uint32_t out_level = 0;
        int y;
        bool last_query;
+       struct dcerpc_binding_handle *b = ctx->cli->binding_handle;
 
        if (!ctx->single_object_replication) {
-               new_utdv = TALLOC_ZERO_P(mem_ctx, struct replUpToDateVectorBlob);
+               new_utdv = talloc_zero(mem_ctx, struct replUpToDateVectorBlob);
                if (!new_utdv) {
                        status = NT_STATUS_NO_MEMORY;
                        goto out;
@@ -506,6 +472,8 @@ static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
        for (y=0, last_query = false; !last_query; y++) {
                struct drsuapi_DsReplicaObjectListItemEx *first_object = NULL;
                struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr = NULL;
+               uint32_t linked_attributes_count = 0;
+               struct drsuapi_DsReplicaLinkedAttribute *linked_attributes = NULL;
 
                if (level == 8) {
                        DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
@@ -517,7 +485,7 @@ static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
                                (long long)req->req5.highwatermark.highest_usn));
                }
 
-               status = rpccli_drsuapi_DsGetNCChanges(ctx->cli, mem_ctx,
+               status = dcerpc_drsuapi_DsGetNCChanges(b, mem_ctx,
                                                       &ctx->bind_handle,
                                                       level,
                                                       req,
@@ -527,12 +495,15 @@ static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
                if (!NT_STATUS_IS_OK(status)) {
                        ctx->error_message = talloc_asprintf(ctx,
                                "Failed to get NC Changes: %s",
-                               get_friendly_werror_msg(werr));
+                               get_friendly_nt_error_msg(status));
                        goto out;
                }
 
                if (!W_ERROR_IS_OK(werr)) {
                        status = werror_to_ntstatus(werr);
+                       ctx->error_message = talloc_asprintf(ctx,
+                               "Failed to get NC Changes: %s",
+                               get_friendly_werror_msg(werr));
                        goto out;
                }
 
@@ -589,6 +560,9 @@ static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
                        first_object = ctr6->first_object;
                        mapping_ctr = &ctr6->mapping_ctr;
 
+                       linked_attributes = ctr6->linked_attributes;
+                       linked_attributes_count = ctr6->linked_attributes_count;
+
                        if (ctr6->more_data) {
                                req->req8.highwatermark = ctr6->new_highwatermark;
                        } else {
@@ -628,6 +602,23 @@ static NTSTATUS libnet_dssync_getncchanges(TALLOC_CTX *mem_ctx,
                                goto out;
                        }
                }
+
+               if (linked_attributes_count == 0) {
+                       continue;
+               }
+
+               if (ctx->ops->process_links) {
+                       status = ctx->ops->process_links(ctx, mem_ctx,
+                                                        linked_attributes_count,
+                                                        linked_attributes,
+                                                        mapping_ctr);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               ctx->error_message = talloc_asprintf(ctx,
+                                       "Failed to call processing function: %s",
+                                       nt_errstr(status));
+                               goto out;
+                       }
+               }
        }
 
        *pnew_utdv = new_utdv;
@@ -641,7 +632,7 @@ static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
 {
        NTSTATUS status;
 
-       int32_t level = 0;
+       uint32_t level = 0;
        union drsuapi_DsGetNCChangesRequest req;
        struct replUpToDateVectorBlob *old_utdv = NULL;
        struct replUpToDateVectorBlob *pnew_utdv = NULL;
@@ -649,12 +640,14 @@ static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
        uint32_t dn_count;
        uint32_t count;
 
-       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;
+       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;
+               }
        }
 
        if (ctx->single_object_replication && ctx->object_dns) {
@@ -665,6 +658,8 @@ static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
                dn_count = 1;
        }
 
+       status = NT_STATUS_OK;
+
        for (count=0; count < dn_count; count++) {
                status = libnet_dssync_build_request(mem_ctx, ctx,
                                                     dns[count],
@@ -677,19 +672,23 @@ static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
                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));
+                       if (!ctx->error_message) {
+                               ctx->error_message = talloc_asprintf(ctx,
+                                       "Failed to call DsGetNCCHanges: %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(ctx,
-                       "Failed to call finishing operation: %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: