s4:dsdb/common/util.c - provide a call which returns the forest function level
[amitay/samba.git] / source4 / dsdb / common / util.c
index 63870278ceb3f75029723458d6dac6c0bc71c085..7c5fd8a1b48ef000c5bbd510a2f989ccaff8311e 100644 (file)
@@ -24,6 +24,7 @@
 #include "includes.h"
 #include "events/events.h"
 #include "ldb.h"
+#include "ldb_module.h"
 #include "ldb_errors.h"
 #include "../lib/util/util_ldb.h"
 #include "../lib/crypto/crypto.h"
@@ -385,7 +386,7 @@ struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, const struct ldb_messa
        if (sid == NULL) {
                return NULL;
        }
-       ndr_err = ndr_pull_struct_blob(v, sid, NULL, sid,
+       ndr_err = ndr_pull_struct_blob(v, sid, sid,
                                       (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                talloc_free(sid);
@@ -520,8 +521,10 @@ NTTIME samdb_result_force_password_change(struct ldb_context *sam_ldb,
                                          struct ldb_dn *domain_dn, 
                                          struct ldb_message *msg)
 {
-       uint64_t attr_time = samdb_result_uint64(msg, "pwdLastSet", 0);
-       uint32_t userAccountControl = samdb_result_uint64(msg, "userAccountControl", 0);
+       int64_t attr_time = samdb_result_int64(msg, "pwdLastSet", 0);
+       uint32_t userAccountControl = ldb_msg_find_attr_as_uint(msg,
+                                                               "userAccountControl",
+                                                               0);
        int64_t maxPwdAge;
 
        /* Machine accounts don't expire, and there is a flag for 'no expiry' */
@@ -533,8 +536,12 @@ NTTIME samdb_result_force_password_change(struct ldb_context *sam_ldb,
        if (attr_time == 0) {
                return 0;
        }
+       if (attr_time == -1) {
+               return 0x7FFFFFFFFFFFFFFFULL;
+       }
 
-       maxPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "maxPwdAge", NULL);
+       maxPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn,
+                                      "maxPwdAge", NULL);
        if (maxPwdAge == 0) {
                return 0x7FFFFFFFFFFFFFFFULL;
        } else {
@@ -588,7 +595,7 @@ unsigned int samdb_result_hashes(TALLOC_CTX *mem_ctx, const struct ldb_message *
        return count;
 }
 
-NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct ldb_message *msg, 
+NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct ldb_message *msg,
                                struct samr_Password **lm_pwd, struct samr_Password **nt_pwd) 
 {
        struct samr_Password *lmPwdHash, *ntPwdHash;
@@ -607,7 +614,7 @@ NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx, struct loadparm_context *lp
                /* Ensure that if we have turned off LM
                 * authentication, that we never use the LM hash, even
                 * if we store it */
-               if (lp_lanman_auth(lp_ctx)) {
+               if (lpcfg_lanman_auth(lp_ctx)) {
                        unsigned int num_lm;
                        num_lm = samdb_result_hashes(mem_ctx, msg, "dBCSPwd", &lmPwdHash);
                        if (num_lm == 0) {
@@ -801,7 +808,7 @@ int samdb_msg_add_string(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struc
        char *s = talloc_strdup(mem_ctx, str);
        char *a = talloc_strdup(mem_ctx, attr_name);
        if (s == NULL || a == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(sam_ldb);
        }
        return ldb_msg_add_string(msg, a, s);
 }
@@ -816,11 +823,10 @@ int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, stru
        enum ndr_err_code ndr_err;
 
        ndr_err = ndr_push_struct_blob(&v, mem_ctx, 
-                                      lp_iconv_convenience(ldb_get_opaque(sam_ldb, "loadparm")),
                                       sid,
                                       (ndr_push_flags_fn_t)ndr_push_dom_sid);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(sam_ldb);
        }
        return ldb_msg_add_value(msg, attr_name, &v, NULL);
 }
@@ -838,52 +844,114 @@ int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struc
 }
 
 /*
-  add a add attribute value to a message
+  add an add attribute value to a message or enhance an existing attribute
+  which has the same name and the add flag set.
 */
-int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
-                        const char *attr_name, const char *value)
+int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
+                        struct ldb_message *msg, const char *attr_name,
+                        const char *value)
 {
        struct ldb_message_element *el;
-       char *a, *v;
+       struct ldb_val val, *vals;
+       char *v;
+       unsigned int i;
+       bool found = false;
        int ret;
-       a = talloc_strdup(mem_ctx, attr_name);
-       if (a == NULL)
-               return LDB_ERR_OPERATIONS_ERROR;
+
        v = talloc_strdup(mem_ctx, value);
-       if (v == NULL)
-               return LDB_ERR_OPERATIONS_ERROR;
-       ret = ldb_msg_add_string(msg, a, v);
-       if (ret != 0)
-               return ret;
-       el = ldb_msg_find_element(msg, a);
-       if (el == NULL)
-               return LDB_ERR_OPERATIONS_ERROR;
-       el->flags = LDB_FLAG_MOD_ADD;
+       if (v == NULL) {
+               return ldb_oom(sam_ldb);
+       }
+
+       val.data = (uint8_t *) v;
+       val.length = strlen(v);
+
+       if (val.length == 0) {
+               /* allow empty strings as non-existent attributes */
+               return LDB_SUCCESS;
+       }
+
+       for (i = 0; i < msg->num_elements; i++) {
+               el = &msg->elements[i];
+               if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
+                   (el->flags == LDB_FLAG_MOD_ADD)) {
+                       found = true;
+                       break;
+               }
+       }
+       if (!found) {
+               ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_ADD,
+                                       &el);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+       }
+
+       vals = talloc_realloc(msg, el->values, struct ldb_val,
+                             el->num_values + 1);
+       if (vals == NULL) {
+               return ldb_oom(sam_ldb);
+       }
+       el->values = vals;
+       el->values[el->num_values] = val;
+       ++(el->num_values);
+
        return LDB_SUCCESS;
 }
 
 /*
-  add a delete attribute value to a message
+  add a delete attribute value to a message or enhance an existing attribute
+  which has the same name and the delete flag set.
 */
-int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
-                        const char *attr_name, const char *value)
+int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
+                        struct ldb_message *msg, const char *attr_name,
+                        const char *value)
 {
        struct ldb_message_element *el;
-       char *a, *v;
+       struct ldb_val val, *vals;
+       char *v;
+       unsigned int i;
+       bool found = false;
        int ret;
-       a = talloc_strdup(mem_ctx, attr_name);
-       if (a == NULL)
-               return LDB_ERR_OPERATIONS_ERROR;
+
        v = talloc_strdup(mem_ctx, value);
-       if (v == NULL)
-               return LDB_ERR_OPERATIONS_ERROR;
-       ret = ldb_msg_add_string(msg, a, v);
-       if (ret != 0)
-               return ret;
-       el = ldb_msg_find_element(msg, a);
-       if (el == NULL)
-               return LDB_ERR_OPERATIONS_ERROR;
-       el->flags = LDB_FLAG_MOD_DELETE;
+       if (v == NULL) {
+               return ldb_oom(sam_ldb);
+       }
+
+       val.data = (uint8_t *) v;
+       val.length = strlen(v);
+
+       if (val.length == 0) {
+               /* allow empty strings as non-existent attributes */
+               return LDB_SUCCESS;
+       }
+
+       for (i = 0; i < msg->num_elements; i++) {
+               el = &msg->elements[i];
+               if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
+                   (el->flags == LDB_FLAG_MOD_DELETE)) {
+                       found = true;
+                       break;
+               }
+       }
+       if (!found) {
+               ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_DELETE,
+                                       &el);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+       }
+
+       vals = talloc_realloc(msg, el->values, struct ldb_val,
+                             el->num_values + 1);
+       if (vals == NULL) {
+               return ldb_oom(sam_ldb);
+       }
+       el->values = vals;
+       el->values[el->num_values] = val;
+       ++(el->num_values);
+
        return LDB_SUCCESS;
 }
 
@@ -934,7 +1002,7 @@ int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct
        struct ldb_val val;
        val.data = talloc_memdup(mem_ctx, hash->hash, 16);
        if (!val.data) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(sam_ldb);
        }
        val.length = 16;
        return ldb_msg_add_value(msg, attr_name, &val, NULL);
@@ -943,7 +1011,8 @@ int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct
 /*
   add a samr_Password array to a message
 */
-int samdb_msg_add_hashes(TALLOC_CTX *mem_ctx, struct ldb_message *msg,
+int samdb_msg_add_hashes(struct ldb_context *ldb,
+                        TALLOC_CTX *mem_ctx, struct ldb_message *msg,
                         const char *attr_name, struct samr_Password *hashes,
                         unsigned int count)
 {
@@ -952,7 +1021,7 @@ int samdb_msg_add_hashes(TALLOC_CTX *mem_ctx, struct ldb_message *msg,
        val.data = talloc_array_size(mem_ctx, 16, count);
        val.length = count*16;
        if (!val.data) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
        for (i=0;i<count;i++) {
                memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
@@ -1762,7 +1831,7 @@ bool samdb_is_pdc(struct ldb_context *ldb)
        }
 
        ret = ldb_search(ldb, tmp_ctx, &dom_res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, dom_attrs, NULL);
-       if (ret) {
+       if (ret != LDB_SUCCESS) {
                DEBUG(1,("Searching for fSMORoleOwner in %s failed: %s\n", 
                         ldb_dn_get_linearized(ldb_get_default_basedn(ldb)), 
                         ldb_errstring(ldb)));
@@ -1808,7 +1877,7 @@ bool samdb_is_gc(struct ldb_context *ldb)
 
        /* Query cn=ntds settings,.... */
        ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
-       if (ret) {
+       if (ret != LDB_SUCCESS) {
                talloc_free(tmp_ctx);
                return false;
        }
@@ -1834,11 +1903,11 @@ int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
        TALLOC_CTX *local_ctx;
        struct ldb_dn *sdn = dn;
        struct ldb_result *res = NULL;
-       int ret = 0;
+       int ret = LDB_SUCCESS;
        const char *attrs[] = { NULL };
 
        local_ctx = talloc_new(mem_ctx);
-       if (local_ctx == NULL) return LDB_ERR_OPERATIONS_ERROR;
+       if (local_ctx == NULL) return ldb_oom(ldb);
 
        while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
                ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
@@ -2281,7 +2350,7 @@ struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
                                            domain_ref_attrs,
                                            "(&(nETBIOSName=%s)(objectclass=crossRef))", 
                                            escaped_domain);
-       if (ret_domain != 0) {
+       if (ret_domain != LDB_SUCCESS) {
                return NULL;
        }
 
@@ -2292,7 +2361,7 @@ struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
                                                LDB_SCOPE_BASE,
                                                domain_ref2_attrs,
                                                "(objectclass=domain)");
-               if (ret_domain != 0) {
+               if (ret_domain != LDB_SUCCESS) {
                        return NULL;
                }
 
@@ -2326,7 +2395,7 @@ int dsdb_find_dn_by_guid(struct ldb_context *ldb,
        char *guid_str = GUID_string(mem_ctx, guid);
 
        if (!guid_str) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
@@ -2453,6 +2522,37 @@ int dsdb_find_sid_by_dn(struct ldb_context *ldb,
        return LDB_SUCCESS;
 }
 
+/*
+  use a SID to find a DN
+ */
+int dsdb_find_dn_by_sid(struct ldb_context *ldb,
+                       TALLOC_CTX *mem_ctx,
+                       struct dom_sid *sid, struct ldb_dn **dn)
+{
+       int ret;
+       struct ldb_result *res;
+       const char *attrs[] = { NULL };
+       char *sid_str = dom_sid_string(mem_ctx, sid);
+
+       if (!sid_str) {
+               return ldb_operr(ldb);
+       }
+
+       ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
+                         DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
+                         DSDB_SEARCH_SHOW_EXTENDED_DN |
+                         DSDB_SEARCH_ONE_ONLY,
+                         "objectSID=%s", sid_str);
+       talloc_free(sid_str);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+
+       *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
+       talloc_free(res);
+
+       return LDB_SUCCESS;
+}
 
 /*
   load a repsFromTo blob list for a given partition GUID
@@ -2494,7 +2594,7 @@ WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ld
        for (i=0; i<(*count); i++) {
                enum ndr_err_code ndr_err;
                ndr_err = ndr_pull_struct_blob(&el->values[i], 
-                                              mem_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
+                                              mem_ctx, 
                                               &(*r)[i], 
                                               (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
@@ -2535,7 +2635,7 @@ WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ld
                struct ldb_val v;
                enum ndr_err_code ndr_err;
 
-               ndr_err = ndr_push_struct_blob(&v, tmp_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
+               ndr_err = ndr_push_struct_blob(&v, tmp_ctx, 
                                               &r[i], 
                                               (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
@@ -2577,7 +2677,7 @@ int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
        res = talloc_zero(tmp_ctx, struct ldb_result);
        if (!res) {
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        ret = ldb_build_search_req(&req, ldb, tmp_ctx,
@@ -2594,13 +2694,12 @@ int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
 
        p_ctrl = talloc(req, struct dsdb_control_current_partition);
        if (p_ctrl == NULL) {
-               talloc_free(res);
-               return LDB_ERR_OPERATIONS_ERROR;
+               talloc_free(tmp_ctx);
+               return ldb_oom(ldb);
        }
        p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
        p_ctrl->dn = dn;
        
-
        ret = ldb_request_add_control(req,
                                      DSDB_CONTROL_CURRENT_PARTITION_OID,
                                      false, p_ctrl);
@@ -2678,7 +2777,7 @@ int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bo
        config_dn = ldb_get_config_basedn(sam_ctx);
        if (!config_dn) {
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(sam_ctx);
        }
 
        ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
@@ -2724,7 +2823,7 @@ int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
 
        objectGUID = samdb_ntds_objectGUID(sam_ctx);
        if (!objectGUID) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(sam_ctx);
        }
 
        ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
@@ -2734,19 +2833,48 @@ int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
 
        cached = talloc(sam_ctx, bool);
        if (cached == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(sam_ctx);
        }
        *cached = *am_rodc;
 
        ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
        if (ret != LDB_SUCCESS) {
                talloc_free(cached);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(sam_ctx);
        }
 
        return LDB_SUCCESS;
 }
 
+bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
+{
+       TALLOC_CTX *tmp_ctx;
+       bool *cached;
+
+       tmp_ctx = talloc_new(ldb);
+       if (tmp_ctx == NULL) {
+               goto failed;
+       }
+
+       cached = talloc(tmp_ctx, bool);
+       if (!cached) {
+               goto failed;
+       }
+
+       *cached = am_rodc;
+       if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
+               goto failed;
+       }
+
+       talloc_steal(ldb, cached);
+       talloc_free(tmp_ctx);
+       return true;
+
+failed:
+       DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
+       talloc_free(tmp_ctx);
+       return false;
+}
 
 
 /*
@@ -2767,7 +2895,7 @@ int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
        }
 
        ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
-       if (ret) {
+       if (ret != LDB_SUCCESS) {
                goto failed;
        }
 
@@ -2794,7 +2922,7 @@ const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *
        struct ldb_result *res;
 
        ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
-       if (ret) {
+       if (ret != LDB_SUCCESS) {
                goto failed;
        }
 
@@ -2837,8 +2965,7 @@ const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
 }
 
 /*
-  return domain functional level
-  returns DS_DOMAIN_FUNCTION_*
+ * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
  */
 int dsdb_functional_level(struct ldb_context *ldb)
 {
@@ -2851,6 +2978,20 @@ int dsdb_functional_level(struct ldb_context *ldb)
        return *domainFunctionality;
 }
 
+/*
+ * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
+ */
+int dsdb_forest_functional_level(struct ldb_context *ldb)
+{
+       int *forestFunctionality =
+               talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
+       if (!forestFunctionality) {
+               DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
+               return DS_DOMAIN_FUNCTION_2000;
+       }
+       return *forestFunctionality;
+}
+
 /*
   set a GUID in an extended DN structure
  */
@@ -2952,7 +3093,7 @@ NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const
 
        tmp_ctx = talloc_new(NULL);
 
-       ndr_err = ndr_pull_struct_blob_all(sid_blob, tmp_ctx, NULL, sid,
+       ndr_err = ndr_pull_struct_blob_all(sid_blob, tmp_ctx, sid,
                                           (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
@@ -2984,7 +3125,7 @@ uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
   return RMD_FLAGS directly from a ldb_val for a DN
   returns 0 if RMD_FLAGS is not found
  */
-uint32_t dsdb_dn_val_rmd_flags(struct ldb_val *val)
+uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
 {
        const char *p;
        uint32_t flags;
@@ -3008,7 +3149,7 @@ uint32_t dsdb_dn_val_rmd_flags(struct ldb_val *val)
 /*
   return true if a ldb_val containing a DN in storage form is deleted
  */
-bool dsdb_dn_is_deleted_val(struct ldb_val *val)
+bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
 {
        return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
 }
@@ -3040,7 +3181,7 @@ int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
                            wk_guid, ldb_dn_get_linearized(nc_root));
        if (!wkguid_dn) {
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(samdb);
        }
 
        ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs, DSDB_SEARCH_SHOW_DELETED);
@@ -3071,17 +3212,17 @@ int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb
        int ret;
        struct ldb_message_element *el;
        struct ldb_result *root_res;
-       int i;
+       unsigned int i;
        struct ldb_dn **nc_dns;
 
        tmp_ctx = talloc_new(samdb);
        if (tmp_ctx == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(samdb);
        }
 
        ret = ldb_search(samdb, tmp_ctx, &root_res,
                         ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
-       if (ret) {
+       if (ret != LDB_SUCCESS) {
                DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
                talloc_free(tmp_ctx);
                return ret;
@@ -3098,14 +3239,14 @@ int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb
        nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
        if (!nc_dns) {
               talloc_free(tmp_ctx);
-              return LDB_ERR_OPERATIONS_ERROR;
+              return ldb_oom(samdb);
        }
 
        for (i=0; i<el->num_values; i++) {
               nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
               if (nc_dns[i] == NULL) {
                       talloc_free(tmp_ctx);
-                      return LDB_ERR_OPERATIONS_ERROR;
+                      return ldb_operr(samdb);
               }
        }
 
@@ -3157,13 +3298,13 @@ int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
        }
        dn = ldb_dn_copy(ldb, dn);
        if (!dn) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
        /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
         be a wellknown GUID for this */
        if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
                talloc_free(dn);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
@@ -3214,8 +3355,7 @@ int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *m
                enum ndr_err_code ndr_err;
                struct replUpToDateVectorBlob ouv;
 
-               ndr_err = ndr_pull_struct_blob(ouv_value, r,
-                                              lp_iconv_convenience(ldb_get_opaque(samdb, "loadparm")), &ouv,
+               ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
                                               (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                        talloc_free(r);
@@ -3241,7 +3381,7 @@ int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *m
        if (!our_invocation_id) {
                DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
                talloc_free(*cursors);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(samdb);
        }
 
        ret = dsdb_load_partition_usn(samdb, dn, &highest_usn, NULL);
@@ -3262,7 +3402,7 @@ int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *m
 
        (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
        if (! *cursors) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(samdb);
        }
 
        (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
@@ -3283,7 +3423,7 @@ int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *m
                     struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
 {
        struct drsuapi_DsReplicaCursor2 *v2;
-       unsigned int i;
+       uint32_t i;
        int ret;
 
        ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
@@ -3300,7 +3440,7 @@ int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *m
        *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
        if (*cursors == NULL) {
                talloc_free(v2);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(samdb);
        }
 
        for (i=0; i<*count; i++) {
@@ -3390,9 +3530,46 @@ int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
                }
        }
 
+       if (dsdb_flags & DSDB_TREE_DELETE) {
+               ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+       }
+
        return LDB_SUCCESS;
 }
 
+/*
+  an add with a set of controls
+*/
+int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
+            uint32_t dsdb_flags)
+{
+       struct ldb_request *req;
+       int ret;
+
+       ret = ldb_build_add_req(&req, ldb, ldb,
+                               message,
+                               NULL,
+                               NULL,
+                               ldb_op_default_callback,
+                               NULL);
+
+       if (ret != LDB_SUCCESS) return ret;
+
+       ret = dsdb_request_add_controls(req, dsdb_flags);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(req);
+               return ret;
+       }
+
+       ret = dsdb_autotransaction_request(ldb, req);
+
+       talloc_free(req);
+       return ret;
+}
+
 /*
   a modify with a set of controls
 */
@@ -3456,7 +3633,7 @@ int dsdb_search_dn(struct ldb_context *ldb,
 
        res = talloc_zero(mem_ctx, struct ldb_result);
        if (!res) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        ret = ldb_build_search_req(&req, ldb, res,
@@ -3516,7 +3693,7 @@ int dsdb_search(struct ldb_context *ldb,
        res = talloc_zero(tmp_ctx, struct ldb_result);
        if (!res) {
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        if (exp_fmt) {
@@ -3526,7 +3703,7 @@ int dsdb_search(struct ldb_context *ldb,
 
                if (!expression) {
                        talloc_free(tmp_ctx);
-                       return LDB_ERR_OPERATIONS_ERROR;
+                       return ldb_oom(ldb);
                }
        }
 
@@ -3602,7 +3779,7 @@ int dsdb_search_one(struct ldb_context *ldb,
        res = talloc_zero(tmp_ctx, struct ldb_result);
        if (!res) {
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        if (exp_fmt) {
@@ -3612,12 +3789,15 @@ int dsdb_search_one(struct ldb_context *ldb,
 
                if (!expression) {
                        talloc_free(tmp_ctx);
-                       return LDB_ERR_OPERATIONS_ERROR;
+                       return ldb_oom(ldb);
                }
+               ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
+                                 dsdb_flags, "%s", expression);
+       } else {
+               ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
+                                 dsdb_flags, NULL);
        }
 
-       ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
-                         dsdb_flags, "%s", expression);
        if (ret != LDB_SUCCESS) {
                talloc_free(tmp_ctx);
                return ret;
@@ -3685,13 +3865,13 @@ int dsdb_validate_dsa_guid(struct ldb_context *ldb,
                DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
                         GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
        dn = msg->dn;
 
        if (!ldb_dn_remove_child_components(dn, 1)) {
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
@@ -3701,7 +3881,7 @@ int dsdb_validate_dsa_guid(struct ldb_context *ldb,
                DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
                         GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
@@ -3709,7 +3889,7 @@ int dsdb_validate_dsa_guid(struct ldb_context *ldb,
                DEBUG(1,(__location__ ": Failed to find account_dn for DSA with objectGUID %s, sid %s\n",
                         GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
@@ -3717,7 +3897,7 @@ int dsdb_validate_dsa_guid(struct ldb_context *ldb,
                DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
                         GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        if (!dom_sid_equal(sid, &sid2)) {
@@ -3727,7 +3907,7 @@ int dsdb_validate_dsa_guid(struct ldb_context *ldb,
                         dom_sid_string(tmp_ctx, sid),
                         dom_sid_string(tmp_ctx, &sid2)));
                talloc_free(tmp_ctx);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        talloc_free(tmp_ctx);