Use the repl_meta_data module by default.
[samba.git] / source4 / dsdb / samdb / ldb_modules / repl_meta_data.c
index 51b6612236a79deb8014cba1ce945d570bd84a1a..5100b7cb7c89ea2e6a4bb9521b52980fd2b5c743 100644 (file)
@@ -4,7 +4,7 @@
    Copyright (C) Simo Sorce  2004-2006
    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
    Copyright (C) Andrew Tridgell 2005
-   Copyright (C) Stefan Metzmacher 2007
+   Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
 
      ** NOTE! The following LGPL license applies to the ldb
      ** library. This does NOT imply that all of Samba is released
@@ -13,7 +13,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -21,8 +21,7 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -48,6 +47,7 @@
 #include "librpc/gen_ndr/ndr_misc.h"
 #include "librpc/gen_ndr/ndr_drsuapi.h"
 #include "librpc/gen_ndr/ndr_drsblobs.h"
+#include "param/param.h"
 
 struct replmd_replicated_request {
        struct ldb_module *module;
@@ -240,12 +240,9 @@ static int replmd_prepare_originating(struct ldb_module *module, struct ldb_requ
                                      struct ldb_dn *dn, const char *fn_name,
                                      int (*fn)(struct ldb_module *,
                                                struct ldb_request *,
-                                               const struct dsdb_schema *,
-                                               const struct dsdb_control_current_partition *))
+                                               const struct dsdb_schema *))
 {
        const struct dsdb_schema *schema;
-       const struct ldb_control *partition_ctrl;
-       const struct dsdb_control_current_partition *partition;
  
        /* do not manipulate our control entries */
        if (ldb_dn_is_special(dn)) {
@@ -260,46 +257,16 @@ static int replmd_prepare_originating(struct ldb_module *module, struct ldb_requ
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
 
-       partition_ctrl = get_control_from_list(req->controls, DSDB_CONTROL_CURRENT_PARTITION_OID);
-       if (!partition_ctrl) {
-               ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "%s: no current partition control found",
-                             fn_name);
-               return LDB_ERR_CONSTRAINT_VIOLATION;
-       }
-
-       partition = talloc_get_type(partition_ctrl->data,
-                                   struct dsdb_control_current_partition);
-       if (!partition) {
-               ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "%s: current partition control contains invalid data",
-                             fn_name);
-               return LDB_ERR_CONSTRAINT_VIOLATION;
-       }
-
-       if (partition->version != DSDB_CONTROL_CURRENT_PARTITION_VERSION) {
-               ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "%s: current partition control contains invalid version [%u != %u]\n",
-                             fn_name, partition->version, DSDB_CONTROL_CURRENT_PARTITION_VERSION);
-               return LDB_ERR_CONSTRAINT_VIOLATION;
-       }
-
-       return fn(module, req, schema, partition);
+       return fn(module, req, schema);
 }
 
 static int replmd_add_originating(struct ldb_module *module,
                                  struct ldb_request *req,
-                                 const struct dsdb_schema *schema,
-                                 const struct dsdb_control_current_partition *partition)
+                                 const struct dsdb_schema *schema)
 {
-       NTSTATUS nt_status;
+       enum ndr_err_code ndr_err;
        struct ldb_request *down_req;
        struct ldb_message *msg;
-       uint32_t instance_type;
-       struct ldb_dn *new_dn;
-       const char *rdn_name;
-       const char *rdn_name_upper;
-       const struct ldb_val *rdn_value = NULL;
        const struct dsdb_attribute *rdn_attr = NULL;
        struct GUID guid;
        struct ldb_val guid_value;
@@ -321,12 +288,6 @@ static int replmd_add_originating(struct ldb_module *module,
                return LDB_ERR_UNWILLING_TO_PERFORM;
        }
 
-       if (ldb_msg_find_element(req->op.add.message, "instanceType")) {
-               ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
-                             "replmd_add_originating: it's not allowed to add an object with instanceType\n");
-               return LDB_ERR_UNWILLING_TO_PERFORM;
-       }
-
        /* Get a sequence number from the backend */
        ret = ldb_sequence_number(module->ldb, LDB_SEQ_NEXT, &seq_num);
        if (ret != LDB_SUCCESS) {
@@ -368,102 +329,24 @@ static int replmd_add_originating(struct ldb_module *module,
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       /*
-        * get details of the rdn name
-        */
-       rdn_name        = ldb_dn_get_rdn_name(msg->dn);
-       if (!rdn_name) {
-               talloc_free(down_req);
-               ldb_oom(module->ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       rdn_attr        = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
-       if (!rdn_attr) {
-               talloc_free(down_req);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       rdn_value       = ldb_dn_get_rdn_val(msg->dn);
-       if (!rdn_value) {
-               talloc_free(down_req);
-               ldb_oom(module->ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
        /* 
         * remove autogenerated attributes
         */
-       ldb_msg_remove_attr(msg, rdn_name);
-       ldb_msg_remove_attr(msg, "name");
        ldb_msg_remove_attr(msg, "whenCreated");
        ldb_msg_remove_attr(msg, "whenChanged");
        ldb_msg_remove_attr(msg, "uSNCreated");
        ldb_msg_remove_attr(msg, "uSNChanged");
        ldb_msg_remove_attr(msg, "replPropertyMetaData");
 
-       /*
-        * TODO: construct a new DN out of:
-        *       - the parent DN
-        *       - the upper case of rdn_attr->LDAPDisplayName
-        *       - rdn_value
-        */
-       new_dn = ldb_dn_copy(msg, msg->dn);
-       if (!new_dn) {
-               talloc_free(down_req);
-               ldb_oom(module->ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       rdn_name_upper = strupper_talloc(msg, rdn_attr->lDAPDisplayName);
-       if (!rdn_name_upper) {
-               talloc_free(down_req);
-               ldb_oom(module->ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       ret = ldb_dn_set_component(new_dn, 0, rdn_name_upper, *rdn_value);
-       if (ret != LDB_SUCCESS) {
-               talloc_free(down_req);
-               ldb_oom(module->ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       msg->dn = new_dn;
-
-       /*
-        * TODO: calculate correct instance type
-        */
-       instance_type = INSTANCE_TYPE_WRITE;
-       if (ldb_dn_compare(partition->dn, msg->dn) == 0) {
-               instance_type |= INSTANCE_TYPE_IS_NC_HEAD;
-               if (ldb_dn_compare(msg->dn, samdb_base_dn(module->ldb)) != 0) {
-                       instance_type |= INSTANCE_TYPE_NC_ABOVE;
-               }
-       }
-
        /*
         * readd replicated attributes
         */
-       ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
-       if (ret != LDB_SUCCESS) {
-               talloc_free(down_req);
-               ldb_oom(module->ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       ret = ldb_msg_add_value(msg, "name", rdn_value, NULL);
-       if (ret != LDB_SUCCESS) {
-               talloc_free(down_req);
-               ldb_oom(module->ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
        ret = ldb_msg_add_string(msg, "whenCreated", time_str);
        if (ret != LDB_SUCCESS) {
                talloc_free(down_req);
                ldb_oom(module->ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
-       ret = ldb_msg_add_fmt(msg, "instanceType", "%u", instance_type);
-       if (ret != LDB_SUCCESS) {
-               talloc_free(down_req);
-               ldb_oom(module->ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
 
        /* build the replication meta_data */
        ZERO_STRUCT(nmd);
@@ -483,6 +366,8 @@ static int replmd_add_originating(struct ldb_module *module,
                struct replPropertyMetaData1 *m = &nmd.ctr.ctr1.array[ni];
                const struct dsdb_attribute *sa;
 
+               if (e->name[0] == '@') continue;
+
                sa = dsdb_attribute_by_lDAPDisplayName(schema, e->name);
                if (!sa) {
                        ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
@@ -492,18 +377,24 @@ static int replmd_add_originating(struct ldb_module *module,
                        return LDB_ERR_NO_SUCH_ATTRIBUTE;
                }
 
-               if (sa->systemFlags & 0x00000001) {
-                       /* attribute is not replicated so it has no meta data */
+               if ((sa->systemFlags & 0x00000001) || (sa->systemFlags & 0x00000004)) {
+                       /* if the attribute is not replicated (0x00000001)
+                        * or constructed (0x00000004) it has no metadata
+                        */
                        continue;
                }
 
                m->attid                        = sa->attributeID_id;
                m->version                      = 1;
-               m->orginating_time              = now;
-               m->orginating_invocation_id     = *our_invocation_id;
-               m->orginating_usn               = seq_num;
+               m->originating_change_time      = now;
+               m->originating_invocation_id    = *our_invocation_id;
+               m->originating_usn              = seq_num;
                m->local_usn                    = seq_num;
                ni++;
+
+               if (ldb_attr_cmp(e->name, ldb_dn_get_rdn_name(msg->dn))) {
+                       rdn_attr = sa;
+               }
        }
 
        /* fix meta data count */
@@ -515,16 +406,19 @@ static int replmd_add_originating(struct ldb_module *module,
        replmd_replPropertyMetaDataCtr1_sort(&nmd.ctr.ctr1, &rdn_attr->attributeID_id);
 
        /* generated NDR encoded values */
-       nt_status = ndr_push_struct_blob(&guid_value, msg, &guid, 
-                                        (ndr_push_flags_fn_t)ndr_push_GUID);
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               talloc_free(down_req);
+       ndr_err = ndr_push_struct_blob(&guid_value, msg, 
+                                      NULL,
+                                      &guid,
+                                      (ndr_push_flags_fn_t)ndr_push_GUID);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                ldb_oom(module->ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
-       nt_status = ndr_push_struct_blob(&nmd_value, msg, &nmd,
-                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
-       if (!NT_STATUS_IS_OK(nt_status)) {
+       ndr_err = ndr_push_struct_blob(&nmd_value, msg, 
+                                      lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
+                                      &nmd,
+                                      (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                talloc_free(down_req);
                ldb_oom(module->ldb);
                return LDB_ERR_OPERATIONS_ERROR;
@@ -591,8 +485,7 @@ static int replmd_add(struct ldb_module *module, struct ldb_request *req)
 
 static int replmd_modify_originating(struct ldb_module *module,
                                     struct ldb_request *req,
-                                    const struct dsdb_schema *schema,
-                                    const struct dsdb_control_current_partition *partition)
+                                    const struct dsdb_schema *schema)
 {
        struct ldb_request *down_req;
        struct ldb_message *msg;
@@ -698,7 +591,7 @@ static int replmd_replicated_apply_add_callback(struct ldb_context *ldb,
                                                void *private_data,
                                                struct ldb_reply *ares)
 {
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        struct replmd_replicated_request *ar = talloc_get_type(private_data,
                                               struct replmd_replicated_request);
 
@@ -720,7 +613,7 @@ static int replmd_replicated_apply_add_callback(struct ldb_context *ldb,
 
 static int replmd_replicated_apply_add(struct replmd_replicated_request *ar)
 {
-       NTSTATUS nt_status;
+       enum ndr_err_code ndr_err;
        struct ldb_message *msg;
        struct replPropertyMetaDataBlob *md;
        struct ldb_val md_value;
@@ -771,9 +664,12 @@ static int replmd_replicated_apply_add(struct replmd_replicated_request *ar)
        for (i=0; i < md->ctr.ctr1.count; i++) {
                md->ctr.ctr1.array[i].local_usn = seq_num;
        }
-       nt_status = ndr_push_struct_blob(&md_value, msg, md,
-                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
-       if (!NT_STATUS_IS_OK(nt_status)) {
+       ndr_err = ndr_push_struct_blob(&md_value, msg, 
+                                      lp_iconv_convenience(ldb_get_opaque(ar->module->ldb, "loadparm")),
+                                      md,
+                                      (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
                return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
        }
        ret = ldb_msg_add_value(msg, "replPropertyMetaData", &md_value, NULL);
@@ -792,14 +688,20 @@ static int replmd_replicated_apply_add(struct replmd_replicated_request *ar)
                                replmd_replicated_apply_add_callback);
        if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
 
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        return ldb_next_request(ar->module, ar->sub.change_req);
 #else
        ret = ldb_next_request(ar->module, ar->sub.change_req);
-       if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
+       if (ret != LDB_SUCCESS) {
+               ldb_asprintf_errstring(ar->module->ldb, "Failed to add replicated object %s: %s", ldb_dn_get_linearized(ar->sub.change_req->op.add.message->dn), 
+                                      ldb_errstring(ar->module->ldb));
+               return replmd_replicated_request_error(ar, ret);
+       }
 
-       ar->sub.change_ret = ldb_wait(ar->sub.search_req->handle, LDB_WAIT_ALL);
+       ar->sub.change_ret = ldb_wait(ar->sub.change_req->handle, LDB_WAIT_ALL);
        if (ar->sub.change_ret != LDB_SUCCESS) {
+               ldb_asprintf_errstring(ar->module->ldb, "Failed while waiting on add replicated object %s: %s", ldb_dn_get_linearized(ar->sub.change_req->op.add.message->dn), 
+                                      ldb_errstring(ar->module->ldb));
                return replmd_replicated_request_error(ar, ar->sub.change_ret);
        }
 
@@ -821,23 +723,23 @@ static int replmd_replPropertyMetaData1_conflict_compare(struct replPropertyMeta
                return m1->version - m2->version;
        }
 
-       if (m1->orginating_time != m2->orginating_time) {
-               return m1->orginating_time - m2->orginating_time;
+       if (m1->originating_change_time != m2->originating_change_time) {
+               return m1->originating_change_time - m2->originating_change_time;
        }
 
-       ret = GUID_compare(&m1->orginating_invocation_id, &m2->orginating_invocation_id);
+       ret = GUID_compare(&m1->originating_invocation_id, &m2->originating_invocation_id);
        if (ret != 0) {
                return ret;
        }
 
-       return m1->orginating_usn - m2->orginating_usn;
+       return m1->originating_usn - m2->originating_usn;
 }
 
 static int replmd_replicated_apply_merge_callback(struct ldb_context *ldb,
                                                  void *private_data,
                                                  struct ldb_reply *ares)
 {
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        struct replmd_replicated_request *ar = talloc_get_type(private_data,
                                               struct replmd_replicated_request);
 
@@ -862,7 +764,7 @@ static int replmd_replicated_apply_merge_callback(struct ldb_context *ldb,
 
 static int replmd_replicated_apply_merge(struct replmd_replicated_request *ar)
 {
-       NTSTATUS nt_status;
+       enum ndr_err_code ndr_err;
        struct ldb_message *msg;
        struct replPropertyMetaDataBlob *rmd;
        struct replPropertyMetaDataBlob omd;
@@ -899,9 +801,11 @@ static int replmd_replicated_apply_merge(struct replmd_replicated_request *ar)
        /* find existing meta data */
        omd_value = ldb_msg_find_ldb_val(ar->sub.search_msg, "replPropertyMetaData");
        if (omd_value) {
-               nt_status = ndr_pull_struct_blob(omd_value, ar->sub.mem_ctx, &omd,
-                                                (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
-               if (!NT_STATUS_IS_OK(nt_status)) {
+               ndr_err = ndr_pull_struct_blob(omd_value, ar->sub.mem_ctx, 
+                                              lp_iconv_convenience(ldb_get_opaque(ar->module->ldb, "loadparm")), &omd,
+                                              (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
+               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                       NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
                        return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
                }
 
@@ -981,9 +885,12 @@ static int replmd_replicated_apply_merge(struct replmd_replicated_request *ar)
        }
 
        /* create the meta data value */
-       nt_status = ndr_push_struct_blob(&nmd_value, msg, &nmd,
-                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
-       if (!NT_STATUS_IS_OK(nt_status)) {
+       ndr_err = ndr_push_struct_blob(&nmd_value, msg, 
+                                      lp_iconv_convenience(ldb_get_opaque(ar->module->ldb, "loadparm")),
+                                      &nmd,
+                                      (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
                return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
        }
 
@@ -1032,13 +939,13 @@ static int replmd_replicated_apply_merge(struct replmd_replicated_request *ar)
                                replmd_replicated_apply_merge_callback);
        if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
 
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        return ldb_next_request(ar->module, ar->sub.change_req);
 #else
        ret = ldb_next_request(ar->module, ar->sub.change_req);
        if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
 
-       ar->sub.change_ret = ldb_wait(ar->sub.search_req->handle, LDB_WAIT_ALL);
+       ar->sub.change_ret = ldb_wait(ar->sub.change_req->handle, LDB_WAIT_ALL);
        if (ar->sub.change_ret != LDB_SUCCESS) {
                return replmd_replicated_request_error(ar, ar->sub.change_ret);
        }
@@ -1075,7 +982,7 @@ static int replmd_replicated_apply_search_callback(struct ldb_context *ldb,
 
        talloc_free(ares);
 
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        if (is_done) {
                ar->sub.search_ret = ldb_wait(ar->sub.search_req->handle, LDB_WAIT_ALL);
                if (ar->sub.search_ret != LDB_SUCCESS) {
@@ -1115,14 +1022,14 @@ static int replmd_replicated_apply_search(struct replmd_replicated_request *ar)
                                   replmd_replicated_apply_search_callback);
        if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
 
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        return ldb_next_request(ar->module, ar->sub.search_req);
 #else
        ret = ldb_next_request(ar->module, ar->sub.search_req);
        if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
 
        ar->sub.search_ret = ldb_wait(ar->sub.search_req->handle, LDB_WAIT_ALL);
-       if (ar->sub.search_ret != LDB_SUCCESS) {
+       if (ar->sub.search_ret != LDB_SUCCESS && ar->sub.search_ret != LDB_ERR_NO_SUCH_OBJECT) {
                return replmd_replicated_request_error(ar, ar->sub.search_ret);
        }
        if (ar->sub.search_msg) {
@@ -1135,7 +1042,7 @@ static int replmd_replicated_apply_search(struct replmd_replicated_request *ar)
 
 static int replmd_replicated_apply_next(struct replmd_replicated_request *ar)
 {
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        if (ar->index_current >= ar->objs->num_objects) {
                return replmd_replicated_uptodate_vector(ar);
        }
@@ -1151,7 +1058,7 @@ static int replmd_replicated_uptodate_modify_callback(struct ldb_context *ldb,
                                                      void *private_data,
                                                      struct ldb_reply *ares)
 {
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        struct replmd_replicated_request *ar = talloc_get_type(private_data,
                                               struct replmd_replicated_request);
 
@@ -1177,7 +1084,7 @@ static int replmd_drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplic
 
 static int replmd_replicated_uptodate_modify(struct replmd_replicated_request *ar)
 {
-       NTSTATUS nt_status;
+       enum ndr_err_code ndr_err;
        struct ldb_message *msg;
        struct replUpToDateVectorBlob ouv;
        const struct ldb_val *ouv_value;
@@ -1220,9 +1127,11 @@ static int replmd_replicated_uptodate_modify(struct replmd_replicated_request *a
         */
        ouv_value = ldb_msg_find_ldb_val(ar->sub.search_msg, "replUpToDateVector");
        if (ouv_value) {
-               nt_status = ndr_pull_struct_blob(ouv_value, ar->sub.mem_ctx, &ouv,
-                                                (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
-               if (!NT_STATUS_IS_OK(nt_status)) {
+               ndr_err = ndr_pull_struct_blob(ouv_value, ar->sub.mem_ctx, 
+                                              lp_iconv_convenience(ldb_get_opaque(ar->module->ldb, "loadparm")), &ouv,
+                                              (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
+               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                       NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
                        return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
                }
 
@@ -1344,9 +1253,12 @@ static int replmd_replicated_uptodate_modify(struct replmd_replicated_request *a
        if (!msg) return replmd_replicated_request_werror(ar, WERR_NOMEM);
        msg->dn = ar->sub.search_msg->dn;
 
-       nt_status = ndr_push_struct_blob(&nuv_value, msg, &nuv,
-                                        (ndr_push_flags_fn_t)ndr_push_replUpToDateVectorBlob);
-       if (!NT_STATUS_IS_OK(nt_status)) {
+       ndr_err = ndr_push_struct_blob(&nuv_value, msg, 
+                                      lp_iconv_convenience(ldb_get_opaque(ar->module->ldb, "loadparm")), 
+                                      &nuv,
+                                      (ndr_push_flags_fn_t)ndr_push_replUpToDateVectorBlob);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
                return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
        }
        ret = ldb_msg_add_value(msg, "replUpToDateVector", &nuv_value, &nuv_el);
@@ -1380,9 +1292,10 @@ static int replmd_replicated_uptodate_modify(struct replmd_replicated_request *a
                        trf = talloc(ar->sub.mem_ctx, struct repsFromToBlob);
                        if (!trf) return replmd_replicated_request_werror(ar, WERR_NOMEM);
 
-                       nt_status = ndr_pull_struct_blob(&orf_el->values[i], trf, trf,
-                                                        (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
-                       if (!NT_STATUS_IS_OK(nt_status)) {
+                       ndr_err = ndr_pull_struct_blob(&orf_el->values[i], trf, lp_iconv_convenience(ldb_get_opaque(ar->module->ldb, "loadparm")), trf,
+                                                      (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
+                       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                               NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
                                return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
                        }
 
@@ -1429,9 +1342,12 @@ static int replmd_replicated_uptodate_modify(struct replmd_replicated_request *a
        }
 
        /* we now fill the value which is already attached to ldb_message */
-       nt_status = ndr_push_struct_blob(nrf_value, msg, &nrf,
-                                        (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
-       if (!NT_STATUS_IS_OK(nt_status)) {
+       ndr_err = ndr_push_struct_blob(nrf_value, msg, 
+                                      lp_iconv_convenience(ldb_get_opaque(ar->module->ldb, "loadparm")),
+                                      &nrf,
+                                      (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
                return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
        }
 
@@ -1451,7 +1367,7 @@ static int replmd_replicated_uptodate_modify(struct replmd_replicated_request *a
                                replmd_replicated_uptodate_modify_callback);
        if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
 
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        return ldb_next_request(ar->module, ar->sub.change_req);
 #else
        ret = ldb_next_request(ar->module, ar->sub.change_req);
@@ -1491,7 +1407,7 @@ static int replmd_replicated_uptodate_search_callback(struct ldb_context *ldb,
 
        talloc_free(ares);
 
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        if (is_done) {
                ar->sub.search_ret = ldb_wait(ar->sub.search_req->handle, LDB_WAIT_ALL);
                if (ar->sub.search_ret != LDB_SUCCESS) {
@@ -1528,7 +1444,7 @@ static int replmd_replicated_uptodate_search(struct replmd_replicated_request *a
                                   replmd_replicated_uptodate_search_callback);
        if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
 
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        return ldb_next_request(ar->module, ar->sub.search_req);
 #else
        ret = ldb_next_request(ar->module, ar->sub.search_req);
@@ -1578,7 +1494,7 @@ static int replmd_extended_replicated_objects(struct ldb_module *module, struct
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-#ifdef REPLMD_FULL_ASYNC /* TODO: active this code when ldb support full async code */ 
+#ifdef REPLMD_FULL_ASYNC /* TODO: activate this code when ldb support full async code */ 
        return replmd_replicated_apply_next(ar);
 #else
        while (ar->index_current < ar->objs->num_objects &&