build: moved libds/common/flag_mapping.c into a common subsystem
[sfrench/samba-autobuild/.git] / source4 / dsdb / samdb / ldb_modules / samldb.c
index b91ca3e83b6c357056330343850edcddc993c766..b11fc554b55be1d9434763caa4a6473c4f8f13bf 100644 (file)
@@ -39,6 +39,7 @@
 #include "librpc/gen_ndr/ndr_security.h"
 #include "ldb_wrap.h"
 #include "param/param.h"
+#include "libds/common/flag_mapping.h"
 
 struct samldb_ctx;
 
@@ -167,6 +168,8 @@ static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
        struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
        const char *name;
        int ret;
+       struct ldb_result *res;
+       const char *noattrs[] = { NULL };
 
        if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
                ret = samldb_generate_sAMAccountName(ldb, ac->msg);
@@ -177,20 +180,29 @@ static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
 
        name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
        if (name == NULL) {
-               return ldb_operr(ldb);
+               /* The "sAMAccountName" cannot be nothing */
+               ldb_set_errstring(ldb,
+                                 "samldb: Empty account names aren't allowed!");
+               return LDB_ERR_CONSTRAINT_VIOLATION;
        }
 
-       ret = samdb_search_count(ldb, ac, NULL, "(sAMAccountName=%s)",
+       ret = dsdb_module_search(ac->module, ac, &res,
+                                NULL, LDB_SCOPE_SUBTREE, noattrs,
+                                DSDB_FLAG_NEXT_MODULE,
+                                ac->req,
+                                "(sAMAccountName=%s)",
                                 ldb_binary_encode_string(ac, name));
-       if ((ret < 0) || (ret > 1)) {
-               return ldb_operr(ldb);
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
-       if (ret == 1) {
+       if (res->count != 0) {
                ldb_asprintf_errstring(ldb,
                                       "samldb: Account name (sAMAccountName) '%s' already in use!",
                                       name);
+               talloc_free(res);
                return LDB_ERR_ENTRY_ALREADY_EXISTS;
        }
+       talloc_free(res);
 
        return samldb_next_step(ac);
 }
@@ -220,7 +232,7 @@ static int samldb_allocate_sid(struct samldb_ctx *ac)
        struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
        int ret;
 
-       ret = ridalloc_allocate_rid(ac->module, &rid);
+       ret = ridalloc_allocate_rid(ac->module, &rid, ac->req);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
@@ -251,6 +263,7 @@ static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac,
        ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL,
                                 LDB_SCOPE_SUBTREE, no_attrs,
                                 DSDB_FLAG_NEXT_MODULE,
+                                ac->req,
                                 "(msDC-SecondaryKrbTgtNumber=%u)",
                                 krbtgt_number);
        if (ret == LDB_SUCCESS && res->count == 0) {
@@ -268,6 +281,7 @@ static int samldb_rodc_add(struct samldb_ctx *ac)
        uint32_t krbtgt_number, i_start, i;
        int ret;
        char *newpass;
+       struct ldb_val newpass_utf16;
 
        /* find a unused msDC-SecondaryKrbTgtNumber */
        i_start = generate_random() & 0xFFFF;
@@ -317,7 +331,17 @@ found:
                return ldb_operr(ldb);
        }
 
-       ret = ldb_msg_add_steal_string(ac->msg, "clearTextPassword", newpass);
+       if (!convert_string_talloc(ac,
+                                  CH_UNIX, CH_UTF16,
+                                  newpass, strlen(newpass),
+                                  (void *)&newpass_utf16.data,
+                                  &newpass_utf16.length, false)) {
+               ldb_asprintf_errstring(ldb,
+                                      "samldb_rodc_add: "
+                                      "failed to generate UTF16 password from random password");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       ret = ldb_msg_add_steal_value(ac->msg, "clearTextPassword", &newpass_utf16);
        if (ret != LDB_SUCCESS) {
                return ldb_operr(ldb);
        }
@@ -338,6 +362,7 @@ static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
                                 ac->dn, LDB_SCOPE_BASE, no_attrs,
                                 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
                                 | DSDB_FLAG_NEXT_MODULE,
+                                ac->req,
                                 "(objectClass=classSchema)");
        if (ret == LDB_ERR_NO_SUCH_OBJECT) {
                /* Don't be pricky when the DN doesn't exist if we have the */
@@ -420,6 +445,7 @@ static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
                                         &ldb_res,
                                         schema_dn, LDB_SCOPE_ONELEVEL, NULL,
                                         DSDB_FLAG_NEXT_MODULE,
+                                        ac->req,
                                         "(msDS-IntId=%d)", msds_intid);
                if (ret != LDB_SUCCESS) {
                        ldb_debug_set(ldb, LDB_DEBUG_ERROR,
@@ -572,6 +598,9 @@ static int samldb_fill_object(struct samldb_ctx *ac)
                }
 
                rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
+               if (rdn_value == NULL) {
+                       return ldb_operr(ldb);
+               }
                if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
                        /* the RDN has prefix "CN" */
                        ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
@@ -635,6 +664,9 @@ static int samldb_fill_object(struct samldb_ctx *ac)
        } else if (strcmp(ac->type, "attributeSchema") == 0) {
                const struct ldb_val *rdn_value;
                rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
+               if (rdn_value == NULL) {
+                       return ldb_operr(ldb);
+               }
                if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
                        /* the RDN has prefix "CN" */
                        ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
@@ -689,6 +721,7 @@ static int samldb_fill_object(struct samldb_ctx *ac)
 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
 {
        struct ldb_context *ldb;
+       const struct ldb_val *rdn_value;
        struct dom_sid *sid;
        int ret;
 
@@ -696,8 +729,12 @@ static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
 
        sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
        if (sid == NULL) {
+               rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
+               if (rdn_value == NULL) {
+                       return ldb_operr(ldb);
+               }
                sid = dom_sid_parse_talloc(ac->msg,
-                                          (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
+                                          (const char *)rdn_value->data);
                if (sid == NULL) {
                        ldb_set_errstring(ldb,
                                          "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
@@ -742,7 +779,7 @@ static int samldb_schema_info_update(struct samldb_ctx *ac)
        }
 
        ret = dsdb_module_schema_info_update(ac->module, schema,
-                                            DSDB_FLAG_NEXT_MODULE);
+                                            DSDB_FLAG_NEXT_MODULE, ac->req);
        if (ret != LDB_SUCCESS) {
                ldb_asprintf_errstring(ldb,
                                       "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
@@ -753,53 +790,6 @@ static int samldb_schema_info_update(struct samldb_ctx *ac)
        return LDB_SUCCESS;
 }
 
-/*
- * Gets back a single-valued attribute by the rules of the SAM triggers when
- * performing a modify operation
- */
-static int samldb_get_single_valued_attr(struct samldb_ctx *ac,
-                                        const char *attr_name,
-                                        struct ldb_message_element **attr)
-{
-       struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
-       struct ldb_message_element *el = NULL;
-       unsigned int i;
-
-       /* We've to walk over all modification entries and consider the
-        * "attr_name" ones.
-        *
-        * 1.) Add operations aren't allowed and there is returned
-        *     "ATTRIBUTE_OR_VALUE_EXISTS".
-        * 2.) Replace operations are allowed but the last one is taken
-        * 3.) Delete operations are also not allowed and there is returned
-        *     "UNWILLING_TO_PERFORM".
-        *
-        * If "el" is afterwards NULL then that means we've nothing to do here.
-        */
-       for (i = 0; i < ac->msg->num_elements; i++) {
-               if (ldb_attr_cmp(ac->msg->elements[i].name, attr_name) != 0) {
-                       continue;
-               }
-
-               el = &ac->msg->elements[i];
-               if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
-                       ldb_asprintf_errstring(ldb,
-                                              "samldb: attribute '%s' already exists!",
-                                              attr_name);
-                       return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
-               }
-               if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
-                       ldb_asprintf_errstring(ldb,
-                                              "samldb: attribute '%s' cannot be deleted!",
-                                              attr_name);
-                       return LDB_ERR_UNWILLING_TO_PERFORM;
-               }
-       }
-
-       *attr = el;
-       return LDB_SUCCESS;
-}
-
 /*
  * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
  *
@@ -816,7 +806,6 @@ static int samldb_objectclass_trigger(struct samldb_ctx *ac)
        struct ldb_message_element *el, *el2;
        enum sid_generator sid_generator;
        struct dom_sid *sid;
-       const char *tempstr;
        int ret;
 
        /* make sure that "sAMAccountType" is not specified */
@@ -835,9 +824,8 @@ static int samldb_objectclass_trigger(struct samldb_ctx *ac)
        sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
        if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
            (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
-               ldb_asprintf_errstring(ldb,
-                                      "samldb: no SID may be specified in user/group modifications for %s",
-                                      ldb_dn_get_linearized(ac->msg->dn));
+               ldb_set_errstring(ldb,
+                                 "samldb: objectSid must not be specified!");
                return LDB_ERR_UNWILLING_TO_PERFORM;
        }
 
@@ -851,11 +839,14 @@ static int samldb_objectclass_trigger(struct samldb_ctx *ac)
        }
 
        if (strcmp(ac->type, "user") == 0) {
+               bool uac_generated = false;
+
                /* Step 1.2: Default values */
-               tempstr = talloc_asprintf(ac->msg, "%d", UF_NORMAL_ACCOUNT);
-               if (tempstr == NULL) return ldb_operr(ldb);
                ret = samdb_find_or_add_attribute(ldb, ac->msg,
-                       "userAccountControl", tempstr);
+                       "accountExpires", "9223372036854775807");
+               if (ret != LDB_SUCCESS) return ret;
+               ret = samdb_find_or_add_attribute(ldb, ac->msg,
+                       "badPasswordTime", "0");
                if (ret != LDB_SUCCESS) return ret;
                ret = samdb_find_or_add_attribute(ldb, ac->msg,
                        "badPwdCount", "0");
@@ -866,9 +857,6 @@ static int samldb_objectclass_trigger(struct samldb_ctx *ac)
                ret = samdb_find_or_add_attribute(ldb, ac->msg,
                        "countryCode", "0");
                if (ret != LDB_SUCCESS) return ret;
-               ret = samdb_find_or_add_attribute(ldb, ac->msg,
-                       "badPasswordTime", "0");
-               if (ret != LDB_SUCCESS) return ret;
                ret = samdb_find_or_add_attribute(ldb, ac->msg,
                        "lastLogoff", "0");
                if (ret != LDB_SUCCESS) return ret;
@@ -876,15 +864,25 @@ static int samldb_objectclass_trigger(struct samldb_ctx *ac)
                        "lastLogon", "0");
                if (ret != LDB_SUCCESS) return ret;
                ret = samdb_find_or_add_attribute(ldb, ac->msg,
-                       "pwdLastSet", "0");
-               if (ret != LDB_SUCCESS) return ret;
-               ret = samdb_find_or_add_attribute(ldb, ac->msg,
-                       "accountExpires", "9223372036854775807");
+                       "logonCount", "0");
                if (ret != LDB_SUCCESS) return ret;
                ret = samdb_find_or_add_attribute(ldb, ac->msg,
-                       "logonCount", "0");
+                       "pwdLastSet", "0");
                if (ret != LDB_SUCCESS) return ret;
 
+               /* On add operations we might need to generate a
+                * "userAccountControl" (if it isn't specified). */
+               el = ldb_msg_find_element(ac->msg, "userAccountControl");
+               if ((el == NULL) && (ac->req->operation == LDB_ADD)) {
+                       ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
+                                                "userAccountControl",
+                                                UF_NORMAL_ACCOUNT);
+                       if (ret != LDB_SUCCESS) {
+                               return ret;
+                       }
+                       uac_generated = true;
+               }
+
                el = ldb_msg_find_element(ac->msg, "userAccountControl");
                if (el != NULL) {
                        uint32_t user_account_control, account_type;
@@ -940,8 +938,10 @@ static int samldb_objectclass_trigger(struct samldb_ctx *ac)
                        }
 
                        /* Step 1.5: Add additional flags when needed */
-                       if ((user_account_control & UF_NORMAL_ACCOUNT) &&
-                           (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
+                       /* Obviously this is done when the "userAccountControl"
+                        * has been generated here (tested against Windows
+                        * Server) */
+                       if (uac_generated) {
                                user_account_control |= UF_ACCOUNTDISABLE;
                                user_account_control |= UF_PASSWD_NOTREQD;
 
@@ -955,6 +955,8 @@ static int samldb_objectclass_trigger(struct samldb_ctx *ac)
                }
 
        } else if (strcmp(ac->type, "group") == 0) {
+               const char *tempstr;
+
                /* Step 2.2: Default values */
                tempstr = talloc_asprintf(ac->msg, "%d",
                                          GTYPE_SECURITY_GLOBAL_GROUP);
@@ -1010,9 +1012,11 @@ static int samldb_objectclass_trigger(struct samldb_ctx *ac)
 static int samldb_prim_group_set(struct samldb_ctx *ac)
 {
        struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
-       struct ldb_dn *prim_group_dn;
        uint32_t rid;
        struct dom_sid *sid;
+       struct ldb_result *res;
+       int ret;
+       const char *noattrs[] = { NULL };
 
        rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
        if (rid == (uint32_t) -1) {
@@ -1030,14 +1034,22 @@ static int samldb_prim_group_set(struct samldb_ctx *ac)
                return ldb_operr(ldb);
        }
 
-       prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
-                                       ldap_encode_ndr_dom_sid(ac, sid));
-       if (prim_group_dn == NULL) {
+       ret = dsdb_module_search(ac->module, ac, &res, NULL, LDB_SCOPE_SUBTREE,
+                                noattrs, DSDB_FLAG_NEXT_MODULE,
+                                ac->req,
+                                "(objectSid=%s)",
+                                ldap_encode_ndr_dom_sid(ac, sid));
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (res->count != 1) {
+               talloc_free(res);
                ldb_asprintf_errstring(ldb,
                                       "Failed to find primary group with RID %u!",
                                       rid);
                return LDB_ERR_UNWILLING_TO_PERFORM;
        }
+       talloc_free(res);
 
        return LDB_SUCCESS;
 }
@@ -1046,18 +1058,17 @@ static int samldb_prim_group_change(struct samldb_ctx *ac)
 {
        struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
        const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
-       struct ldb_result *res;
+       struct ldb_result *res, *group_res;
        struct ldb_message_element *el;
        struct ldb_message *msg;
-       uint32_t rid;
-       struct dom_sid *sid;
+       uint32_t prev_rid, new_rid;
+       struct dom_sid *prev_sid, *new_sid;
        struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
        int ret;
+       const char *noattrs[] = { NULL };
 
-       ret = samldb_get_single_valued_attr(ac, "primaryGroupID", &el);
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
+       el = dsdb_get_single_valued_attr(ac->msg, "primaryGroupID",
+                                        ac->req->operation);
        if (el == NULL) {
                /* we are not affected */
                return LDB_SUCCESS;
@@ -1065,8 +1076,8 @@ static int samldb_prim_group_change(struct samldb_ctx *ac)
 
        /* Fetch informations from the existing object */
 
-       ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
-                        NULL);
+       ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
+                                DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
@@ -1076,22 +1087,17 @@ static int samldb_prim_group_change(struct samldb_ctx *ac)
 
        /* Finds out the DN of the old primary group */
 
-       rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
-       if (rid == (uint32_t) -1) {
+       prev_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID",
+                                            (uint32_t) -1);
+       if (prev_rid == (uint32_t) -1) {
                /* User objects do always have a mandatory "primaryGroupID"
                 * attribute. If this doesn't exist then the object is of the
                 * wrong type. This is the exact Windows error code */
                return LDB_ERR_OBJECT_CLASS_VIOLATION;
        }
 
-       sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
-       if (sid == NULL) {
-               return ldb_operr(ldb);
-       }
-
-       prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
-                                            ldap_encode_ndr_dom_sid(ac, sid));
-       if (prev_prim_group_dn == NULL) {
+       prev_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), prev_rid);
+       if (prev_sid == NULL) {
                return ldb_operr(ldb);
        }
 
@@ -1107,76 +1113,95 @@ static int samldb_prim_group_change(struct samldb_ctx *ac)
        if (ret != LDB_SUCCESS) {
                return ret;
        }
-       rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
+       new_rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
        talloc_free(msg);
-       if (rid == (uint32_t) -1) {
+       if (new_rid == (uint32_t) -1) {
                /* we aren't affected of any primary group change */
                return LDB_SUCCESS;
        }
 
-       sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
-       if (sid == NULL) {
+       if (prev_rid == new_rid) {
+               return LDB_SUCCESS;
+       }
+
+       ret = dsdb_module_search(ac->module, ac, &group_res, NULL, LDB_SCOPE_SUBTREE,
+                                noattrs, DSDB_FLAG_NEXT_MODULE,
+                                ac->req,
+                                "(objectSid=%s)",
+                                ldap_encode_ndr_dom_sid(ac, prev_sid));
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (group_res->count != 1) {
                return ldb_operr(ldb);
        }
+       prev_prim_group_dn = group_res->msgs[0]->dn;
 
-       new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
-                                           ldap_encode_ndr_dom_sid(ac, sid));
-       if (new_prim_group_dn == NULL) {
+       new_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), new_rid);
+       if (new_sid == NULL) {
+               return ldb_operr(ldb);
+       }
+
+       ret = dsdb_module_search(ac->module, ac, &group_res, NULL, LDB_SCOPE_SUBTREE,
+                                noattrs, DSDB_FLAG_NEXT_MODULE,
+                                ac->req,
+                                "(objectSid=%s)",
+                                ldap_encode_ndr_dom_sid(ac, new_sid));
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (group_res->count != 1) {
                /* Here we know if the specified new primary group candidate is
                 * valid or not. */
                return LDB_ERR_UNWILLING_TO_PERFORM;
        }
+       new_prim_group_dn = group_res->msgs[0]->dn;
 
-       /* Only update the "member" attributes when we really do have a change */
-       if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
-               /* We need to be already a normal member of the new primary
-                * group in order to be successful. */
-               el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
-                                         ldb_dn_get_linearized(new_prim_group_dn));
-               if (el == NULL) {
-                       return LDB_ERR_UNWILLING_TO_PERFORM;
-               }
-
-               /* Remove the "member" attribute on the new primary group */
-               msg = ldb_msg_new(ac->msg);
-               if (msg == NULL) {
-                       return ldb_module_oom(ac->module);
-               }
-               msg->dn = new_prim_group_dn;
+       /* We need to be already a normal member of the new primary
+        * group in order to be successful. */
+       el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
+                                 ldb_dn_get_linearized(new_prim_group_dn));
+       if (el == NULL) {
+               return LDB_ERR_UNWILLING_TO_PERFORM;
+       }
 
-               ret = samdb_msg_add_delval(ldb, msg, msg, "member",
-                                          ldb_dn_get_linearized(ac->msg->dn));
-               if (ret != LDB_SUCCESS) {
-                       return ret;
-               }
+       /* Remove the "member" attribute on the new primary group */
+       msg = ldb_msg_new(ac->msg);
+       if (msg == NULL) {
+               return ldb_module_oom(ac->module);
+       }
+       msg->dn = new_prim_group_dn;
 
-               ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
-               if (ret != LDB_SUCCESS) {
-                       return ret;
-               }
-               talloc_free(msg);
+       ret = samdb_msg_add_delval(ldb, msg, msg, "member",
+                                  ldb_dn_get_linearized(ac->msg->dn));
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
 
-               /* Add a "member" attribute for the previous primary group */
-               msg = ldb_msg_new(ac->msg);
-               if (msg == NULL) {
-                       return ldb_module_oom(ac->module);
-               }
-               msg->dn = prev_prim_group_dn;
+       ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       talloc_free(msg);
 
-               ret = samdb_msg_add_addval(ldb, msg, msg, "member",
-                                          ldb_dn_get_linearized(ac->msg->dn));
-               if (ret != LDB_SUCCESS) {
-                       return ret;
-               }
+       /* Add a "member" attribute for the previous primary group */
+       msg = ldb_msg_new(ac->msg);
+       if (msg == NULL) {
+               return ldb_module_oom(ac->module);
+       }
+       msg->dn = prev_prim_group_dn;
 
-               ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
-               if (ret != LDB_SUCCESS) {
-                       return ret;
-               }
-               talloc_free(msg);
+       ret = samdb_msg_add_addval(ldb, msg, msg, "member",
+                                  ldb_dn_get_linearized(ac->msg->dn));
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
 
-       talloc_free(res);
+       ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       talloc_free(msg);
 
        return LDB_SUCCESS;
 }
@@ -1202,10 +1227,8 @@ static int samldb_user_account_control_change(struct samldb_ctx *ac)
        struct ldb_message *tmp_msg;
        int ret;
 
-       ret = samldb_get_single_valued_attr(ac, "userAccountControl", &el);
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
+       el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl",
+                                        ac->req->operation);
        if (el == NULL) {
                /* we are not affected */
                return LDB_SUCCESS;
@@ -1277,11 +1300,11 @@ static int samldb_group_type_change(struct samldb_ctx *ac)
        struct ldb_message_element *el;
        struct ldb_message *tmp_msg;
        int ret;
+       struct ldb_result *res;
+       const char *attrs[] = { "groupType", NULL };
 
-       ret = samldb_get_single_valued_attr(ac, "groupType", &el);
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
+       el = dsdb_get_single_valued_attr(ac->msg, "groupType",
+                                        ac->req->operation);
        if (el == NULL) {
                /* we are not affected */
                return LDB_SUCCESS;
@@ -1299,8 +1322,12 @@ static int samldb_group_type_change(struct samldb_ctx *ac)
        group_type = ldb_msg_find_attr_as_uint(tmp_msg, "groupType", 0);
        talloc_free(tmp_msg);
 
-       old_group_type = samdb_search_uint(ldb, ac, 0, ac->msg->dn,
-                                          "groupType", NULL);
+       ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
+                                   DSDB_FLAG_NEXT_MODULE, ac->req);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       old_group_type = ldb_msg_find_attr_as_uint(res->msgs[0], "groupType", 0);
        if (old_group_type == 0) {
                return ldb_operr(ldb);
        }
@@ -1310,35 +1337,40 @@ static int samldb_group_type_change(struct samldb_ctx *ac)
         * On each step also the group type itself
         * (security/distribution) is variable. */
 
-       switch (group_type) {
-       case GTYPE_SECURITY_GLOBAL_GROUP:
-       case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
-               /* change to "universal" allowed */
-               if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
-                   (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
-                       return LDB_ERR_UNWILLING_TO_PERFORM;
-               }
-       break;
-
-       case GTYPE_SECURITY_UNIVERSAL_GROUP:
-       case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
-               /* each change allowed */
-       break;
-
-       case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
-       case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
-               /* change to "universal" allowed */
-               if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
-                   (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
+       if (ldb_request_get_control(ac->req, LDB_CONTROL_PROVISION_OID) == NULL) {
+               switch (group_type) {
+               case GTYPE_SECURITY_GLOBAL_GROUP:
+               case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
+                       /* change to "universal" allowed */
+                       if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
+                       (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
+                               ldb_set_errstring(ldb,
+                                       "samldb: Change from security/distribution local group forbidden!");
+                               return LDB_ERR_UNWILLING_TO_PERFORM;
+                       }
+               break;
+
+               case GTYPE_SECURITY_UNIVERSAL_GROUP:
+               case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
+                       /* each change allowed */
+               break;
+               case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
+               case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
+                       /* change to "universal" allowed */
+                       if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
+                       (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
+                               ldb_set_errstring(ldb,
+                                       "samldb: Change from security/distribution global group forbidden!");
+                               return LDB_ERR_UNWILLING_TO_PERFORM;
+                       }
+               break;
+
+               case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
+               default:
+                       /* we don't allow this "groupType" values */
                        return LDB_ERR_UNWILLING_TO_PERFORM;
+               break;
                }
-       break;
-
-       case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
-       default:
-               /* we don't allow this "groupType" values */
-               return LDB_ERR_UNWILLING_TO_PERFORM;
-       break;
        }
 
        account_type =  ds_gtype2atype(group_type);
@@ -1367,10 +1399,8 @@ static int samldb_sam_accountname_check(struct samldb_ctx *ac)
        struct ldb_message *tmp_msg;
        int ret;
 
-       ret = samldb_get_single_valued_attr(ac, "sAMAccountName", &el);
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
+       el = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
+                                        ac->req->operation);
        if (el == NULL) {
                /* we are not affected */
                return LDB_SUCCESS;
@@ -1385,12 +1415,15 @@ static int samldb_sam_accountname_check(struct samldb_ctx *ac)
        if (ret != LDB_SUCCESS) {
                return ret;
        }
-       sam_accountname = ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName",
-                                                     NULL);
+       sam_accountname = talloc_steal(ac,
+                                      ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
        talloc_free(tmp_msg);
 
        if (sam_accountname == NULL) {
-               return ldb_operr(ldb);
+               /* The "sAMAccountName" cannot be nothing */
+               ldb_set_errstring(ldb,
+                                 "samldb: Empty account names aren't allowed!");
+               return LDB_ERR_UNWILLING_TO_PERFORM;
        }
 
        enc_str = ldb_binary_encode_string(ac, sam_accountname);
@@ -1400,8 +1433,9 @@ static int samldb_sam_accountname_check(struct samldb_ctx *ac)
 
        /* Make sure that a "sAMAccountName" is only used once */
 
-       ret = ldb_search(ldb, ac, &res, NULL, LDB_SCOPE_SUBTREE, no_attrs,
-                        "(sAMAccountName=%s)", enc_str);
+       ret = dsdb_module_search(ac->module, ac, &res, NULL, LDB_SCOPE_SUBTREE, no_attrs,
+                                DSDB_FLAG_NEXT_MODULE, ac->req,
+                                "(sAMAccountName=%s)", enc_str);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
@@ -1422,13 +1456,32 @@ static int samldb_sam_accountname_check(struct samldb_ctx *ac)
 
 static int samldb_member_check(struct samldb_ctx *ac)
 {
+       static const char * const attrs[] = { "objectSid", "member", NULL };
        struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
        struct ldb_message_element *el;
-       struct ldb_dn *member_dn, *group_dn;
-       uint32_t prim_group_rid;
+       struct ldb_dn *member_dn;
        struct dom_sid *sid;
+       struct ldb_result *res;
+       struct dom_sid *group_sid;
        unsigned int i, j;
        int cnt;
+       int ret;
+
+       /* Fetch informations from the existing object */
+
+       ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
+                                DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (res->count != 1) {
+               return ldb_operr(ldb);
+       }
+
+       group_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
+       if (group_sid == NULL) {
+               return ldb_operr(ldb);
+       }
 
        /* We've to walk over all modification entries and consider the "member"
         * ones. */
@@ -1439,6 +1492,11 @@ static int samldb_member_check(struct samldb_ctx *ac)
 
                el = &ac->msg->elements[i];
                for (j = 0; j < el->num_values; j++) {
+                       struct ldb_message_element *mo;
+                       struct ldb_result *group_res;
+                       const char *group_attrs[] = { "primaryGroupID" , NULL };
+                       uint32_t prim_group_rid;
+
                        member_dn = ldb_dn_from_ldb_val(ac, ldb,
                                                        &el->values[j]);
                        if (!ldb_dn_validate(member_dn)) {
@@ -1458,12 +1516,14 @@ static int samldb_member_check(struct samldb_ctx *ac)
                         *   ERR_NO_SUCH_ATTRIBUTE!)
                         * - primary group check
                         */
-                       cnt = samdb_search_count(ldb, ac, ac->msg->dn,
-                                                "(member=%s)",
-                                                ldb_dn_get_linearized(member_dn));
-                       if (cnt < 0) {
-                               return ldb_operr(ldb);
+                       mo = samdb_find_attribute(ldb, res->msgs[0], "member",
+                                                 ldb_dn_get_linearized(member_dn));
+                       if (mo == NULL) {
+                               cnt = 0;
+                       } else {
+                               cnt = 1;
                        }
+
                        if ((cnt > 0) && (LDB_FLAG_MOD_TYPE(el->flags)
                            == LDB_FLAG_MOD_ADD)) {
                                return LDB_ERR_ENTRY_ALREADY_EXISTS;
@@ -1477,11 +1537,17 @@ static int samldb_member_check(struct samldb_ctx *ac)
                         * ones for them - in this case return
                         * ERR_ENTRY_ALREADY_EXISTS. */
 
-                       prim_group_rid = samdb_search_uint(ldb, ac,
-                                                          (uint32_t) -1,
-                                                          member_dn,
-                                                          "primaryGroupID",
-                                                          NULL);
+                       ret = dsdb_module_search_dn(ac->module, ac, &group_res,
+                                                   member_dn, group_attrs,
+                                                   DSDB_FLAG_NEXT_MODULE, ac->req);
+                       if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+                               /* member DN doesn't exist yet */
+                               continue;
+                       }
+                       if (ret != LDB_SUCCESS) {
+                               return ret;
+                       }
+                       prim_group_rid = ldb_msg_find_attr_as_uint(group_res->msgs[0], "primaryGroupID", (uint32_t)-1);
                        if (prim_group_rid == (uint32_t) -1) {
                                /* the member hasn't to be a user account ->
                                 * therefore no check needed in this case. */
@@ -1494,77 +1560,165 @@ static int samldb_member_check(struct samldb_ctx *ac)
                                return ldb_operr(ldb);
                        }
 
-                       group_dn = samdb_search_dn(ldb, ac, NULL,
-                                                  "(objectSid=%s)",
-                                                  ldap_encode_ndr_dom_sid(ac, sid));
-                       if (group_dn == NULL) {
-                               return ldb_operr(ldb);
-                       }
-
-                       if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
+                       if (dom_sid_equal(group_sid, sid)) {
                                return LDB_ERR_ENTRY_ALREADY_EXISTS;
                        }
                }
        }
 
+       talloc_free(res);
+
+       return LDB_SUCCESS;
+}
+
+/* SAM objects have special rules regarding the "description" attribute on
+ * modify operations. */
+static int samldb_description_check(struct samldb_ctx *ac, bool *modified)
+{
+       struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
+       const char * const attrs[] = { "objectClass", "description", NULL };
+       struct ldb_result *res;
+       unsigned int i;
+       int ret;
+
+       /* Fetch informations from the existing object */
+       ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
+                                DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req,
+                                "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
+       if (ret != LDB_SUCCESS) {
+               /* don't treat it specially ... let normal error codes
+                  happen from other places */
+               ldb_reset_err_string(ldb);
+               return LDB_SUCCESS;
+       }
+       if (res->count == 0) {
+               /* we didn't match the filter */
+               talloc_free(res);
+               return LDB_SUCCESS;
+       }
+
+       /* We've to walk over all modification entries and consider the
+        * "description" ones. */
+       for (i = 0; i < ac->msg->num_elements; i++) {
+               if (ldb_attr_cmp(ac->msg->elements[i].name, "description") == 0) {
+                       ac->msg->elements[i].flags |= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK;
+                       *modified = true;
+               }
+       }
+
+       talloc_free(res);
+
        return LDB_SUCCESS;
 }
 
 /* This trigger adapts the "servicePrincipalName" attributes if the
- * "dNSHostName" attribute changes */
+ * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
 static int samldb_service_principal_names_change(struct samldb_ctx *ac)
 {
        struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
-       struct ldb_message_element *el = NULL;
+       struct ldb_message_element *el = NULL, *el2 = NULL;
        struct ldb_message *msg;
        const char *attrs[] = { "servicePrincipalName", NULL };
        struct ldb_result *res;
-       const char *dns_hostname, *old_dns_hostname;
+       const char *dns_hostname = NULL, *old_dns_hostname = NULL,
+                  *sam_accountname = NULL, *old_sam_accountname = NULL;
        unsigned int i;
        int ret;
 
-       /* Here it's not the same logic as with "samldb_get_single_valued_attr".
-        * We need to:
-        *
-        * - consider "add" and "replace" operations - the last value we take
-        * - ignore "delete" operations - obviously this attribute isn't
-        *   write protected
-        */
-       for (i = 0; i < ac->msg->num_elements; i++) {
-               if (ldb_attr_cmp(ac->msg->elements[i].name,
-                                "dNSHostName") != 0) {
-                       continue;
+       el = dsdb_get_single_valued_attr(ac->msg, "dNSHostName",
+                                        ac->req->operation);
+       el2 = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
+                                         ac->req->operation);
+       if ((el == NULL) && (el2 == NULL)) {
+               /* we are not affected */
+               return LDB_SUCCESS;
+       }
+
+       /* Create a temporary message for fetching the "dNSHostName" */
+       if (el != NULL) {
+               const char *dns_attrs[] = { "dNSHostName", NULL };
+               msg = ldb_msg_new(ac->msg);
+               if (msg == NULL) {
+                       return ldb_module_oom(ac->module);
                }
+               ret = ldb_msg_add(msg, el, 0);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+               dns_hostname = talloc_steal(ac,
+                                           ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL));
+               talloc_free(msg);
 
-               if (LDB_FLAG_MOD_TYPE(ac->msg->elements[i].flags)
-                   != LDB_FLAG_MOD_DELETE) {
-                       el = &ac->msg->elements[i];
+               ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn,
+                                           dns_attrs, DSDB_FLAG_NEXT_MODULE, ac->req);
+               if (ret == LDB_SUCCESS) {
+                       old_dns_hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
                }
        }
-       if (el == NULL) {
-               /* we are not affected */
-               return LDB_SUCCESS;
+
+       /* Create a temporary message for fetching the "sAMAccountName" */
+       if (el2 != NULL) {
+               char *tempstr, *tempstr2;
+               const char *acct_attrs[] = { "sAMAccountName", NULL };
+
+               msg = ldb_msg_new(ac->msg);
+               if (msg == NULL) {
+                       return ldb_module_oom(ac->module);
+               }
+               ret = ldb_msg_add(msg, el2, 0);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+               tempstr = talloc_strdup(ac,
+                                       ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
+               talloc_free(msg);
+
+               ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, acct_attrs,
+                                           DSDB_FLAG_NEXT_MODULE, ac->req);
+               if (ret == LDB_SUCCESS) {
+                       tempstr2 = talloc_strdup(ac,
+                                                ldb_msg_find_attr_as_string(res->msgs[0],
+                                                                            "sAMAccountName", NULL));
+               }
+
+
+               /* The "sAMAccountName" needs some additional trimming: we need
+                * to remove the trailing "$"s if they exist. */
+               if ((tempstr != NULL) && (tempstr[0] != '\0') &&
+                   (tempstr[strlen(tempstr) - 1] == '$')) {
+                       tempstr[strlen(tempstr) - 1] = '\0';
+               }
+               if ((tempstr2 != NULL) && (tempstr2[0] != '\0') &&
+                   (tempstr2[strlen(tempstr2) - 1] == '$')) {
+                       tempstr2[strlen(tempstr2) - 1] = '\0';
+               }
+               sam_accountname = tempstr;
+               old_sam_accountname = tempstr2;
        }
 
-       /* Create a temporary message for fetching the "dNSHostName" */
-       msg = ldb_msg_new(ac->msg);
-       if (msg == NULL) {
-               return ldb_module_oom(ac->module);
+       if (old_dns_hostname == NULL) {
+               /* we cannot change when the old name is unknown */
+               dns_hostname = NULL;
        }
-       ret = ldb_msg_add(msg, el, 0);
-       if (ret != LDB_SUCCESS) {
-               return ret;
+       if ((old_dns_hostname != NULL) && (dns_hostname != NULL) &&
+           (strcasecmp(old_dns_hostname, dns_hostname) == 0)) {
+               /* The "dNSHostName" didn't change */
+               dns_hostname = NULL;
+       }
+
+       if (old_sam_accountname == NULL) {
+               /* we cannot change when the old name is unknown */
+               sam_accountname = NULL;
+       }
+       if ((old_sam_accountname != NULL) && (sam_accountname != NULL) &&
+           (strcasecmp(old_sam_accountname, sam_accountname) == 0)) {
+               /* The "sAMAccountName" didn't change */
+               sam_accountname = NULL;
        }
-       dns_hostname = ldb_msg_find_attr_as_string(msg, "dNSHostName", "");
-       talloc_free(msg);
 
-       old_dns_hostname = samdb_search_string(ldb, ac, ac->msg->dn,
-                                              "dNSHostName", NULL);
-       if ((old_dns_hostname == NULL) || (strcasecmp(old_dns_hostname,
-                                                     dns_hostname) == 0)) {
-               /* Well, if there's no old DNS hostname then we cannot do this.
-                * And if old and new DNS name do match we are also finished
-                * here. */
+       if ((dns_hostname == NULL) && (sam_accountname == NULL)) {
+               /* Well, there are informations missing (old name(s)) or the
+                * names didn't change. We've nothing to do and can exit here */
                return LDB_SUCCESS;
        }
 
@@ -1591,7 +1745,7 @@ static int samldb_service_principal_names_change(struct samldb_ctx *ac)
                } while (el != NULL);
 
                ret = dsdb_module_modify(ac->module, msg,
-                                        DSDB_FLAG_NEXT_MODULE);
+                                        DSDB_FLAG_NEXT_MODULE, ac->req);
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
@@ -1599,8 +1753,8 @@ static int samldb_service_principal_names_change(struct samldb_ctx *ac)
        }
 
        /* Fetch the "servicePrincipalName"s if any */
-       ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
-                        NULL);
+       ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
+                                DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
@@ -1611,7 +1765,8 @@ static int samldb_service_principal_names_change(struct samldb_ctx *ac)
        if (res->msgs[0]->num_elements == 1) {
                /* Yes, we do have "servicePrincipalName"s. First we update them
                 * locally, that means we do always substitute the current
-                * "dNSHostName" with the new one and then we append this to the
+                * "dNSHostName" with the new one and/or "sAMAccountName"
+                * without "$" with the new one and then we append this to the
                 * modification request (Windows behaviour). */
 
                for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
@@ -1628,9 +1783,14 @@ static int samldb_service_principal_names_change(struct samldb_ctx *ac)
                        }
 
                        while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
-                               if (strcasecmp(tok, old_dns_hostname) == 0) {
+                               if ((dns_hostname != NULL) &&
+                                   (strcasecmp(tok, old_dns_hostname) == 0)) {
                                        tok = dns_hostname;
                                }
+                               if ((sam_accountname != NULL) &&
+                                   (strcasecmp(tok, old_sam_accountname) == 0)) {
+                                       tok = sam_accountname;
+                               }
 
                                new_str = talloc_asprintf(ac->msg, "%s/%s",
                                                          new_str, tok);
@@ -1757,7 +1917,7 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
 {
        struct ldb_context *ldb;
        struct samldb_ctx *ac;
-       struct ldb_message_element *el;
+       struct ldb_message_element *el, *el2;
        bool modified = false;
        int ret;
 
@@ -1768,6 +1928,13 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
 
        ldb = ldb_module_get_ctx(module);
 
+       /* make sure that "objectSid" is not specified */
+       el = ldb_msg_find_element(req->op.mod.message, "objectSid");
+       if (el != NULL) {
+               ldb_set_errstring(ldb,
+                                 "samldb: objectSid must not be specified!");
+               return LDB_ERR_UNWILLING_TO_PERFORM;
+       }
        /* make sure that "sAMAccountType" is not specified */
        el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
        if (el != NULL) {
@@ -1850,8 +2017,17 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
                }
        }
 
-       el = ldb_msg_find_element(ac->msg, "dNSHostName");
+       el = ldb_msg_find_element(ac->msg, "description");
        if (el != NULL) {
+               ret = samldb_description_check(ac, &modified);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+       }
+
+       el = ldb_msg_find_element(ac->msg, "dNSHostName");
+       el2 = ldb_msg_find_element(ac->msg, "sAMAccountName");
+       if ((el != NULL) || (el2 != NULL)) {
                modified = true;
                ret = samldb_service_principal_names_change(ac);
                if (ret != LDB_SUCCESS) {
@@ -1890,13 +2066,20 @@ static int samldb_prim_group_users_check(struct samldb_ctx *ac)
        struct dom_sid *sid;
        uint32_t rid;
        NTSTATUS status;
-       int count;
+       int ret;
+       struct ldb_result *res;
+       const char *attrs[] = { "objectSid", NULL };
+       const char *noattrs[] = { NULL };
 
        ldb = ldb_module_get_ctx(ac->module);
 
        /* Finds out the SID/RID of the SAM object */
-       sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSid",
-                                  NULL);
+       ret = dsdb_module_search_dn(ac->module, ac, &res, ac->req->op.del.dn, attrs, DSDB_FLAG_NEXT_MODULE, ac->req);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+
+       sid = samdb_result_dom_sid(ac, res->msgs[0], "objectSid");
        if (sid == NULL) {
                /* No SID - it might not be a SAM object - therefore ok */
                return LDB_SUCCESS;
@@ -1911,13 +2094,14 @@ static int samldb_prim_group_users_check(struct samldb_ctx *ac)
        }
 
        /* Deny delete requests from groups which are primary ones */
-       count = samdb_search_count(ldb, ac, NULL,
-                                  "(&(primaryGroupID=%u)(objectClass=user))",
-                                  rid);
-       if (count < 0) {
-               return ldb_operr(ldb);
+       ret = dsdb_module_search(ac->module, ac, &res, NULL, LDB_SCOPE_SUBTREE, noattrs,
+                                DSDB_FLAG_NEXT_MODULE,
+                                ac->req,
+                                "(&(primaryGroupID=%u)(objectClass=user))", rid);
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
-       if (count > 0) {
+       if (res->count > 0) {
                return LDB_ERR_ENTRY_ALREADY_EXISTS;
        }
 
@@ -1965,7 +2149,7 @@ static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct l
                return LDB_ERR_PROTOCOL_ERROR;
        }
 
-       ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
+       ret = ridalloc_allocate_rid_pool_fsmo(module, exop, req);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
@@ -1983,7 +2167,7 @@ static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
 }
 
 
-_PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
+static const struct ldb_module_ops ldb_samldb_module_ops = {
        .name          = "samldb",
        .add           = samldb_add,
        .modify        = samldb_modify,
@@ -1991,3 +2175,9 @@ _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
        .extended      = samldb_extended
 };
 
+
+int ldb_samldb_module_init(const char *version)
+{
+       LDB_MODULE_CHECK_VERSION(version);
+       return ldb_register_module(&ldb_samldb_module_ops);
+}