s4:dsdb/samldb: avoid nested unindexed searches in samldb_member_check()
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / samldb.c
index 44c8fee29f0d91c42b6f36d736092c098d0f831e..d3d074159edcd20d973d034b835fe717d04cb372 100644 (file)
@@ -177,7 +177,10 @@ 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)",
@@ -186,6 +189,9 @@ static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
                return ldb_operr(ldb);
        }
        if (ret == 1) {
+               ldb_asprintf_errstring(ldb,
+                                      "samldb: Account name (sAMAccountName) '%s' already in use!",
+                                      name);
                return LDB_ERR_ENTRY_ALREADY_EXISTS;
        }
 
@@ -750,6 +756,53 @@ 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)
  *
@@ -785,9 +838,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;
        }
 
@@ -1002,41 +1054,17 @@ static int samldb_prim_group_change(struct samldb_ctx *ac)
        uint32_t rid;
        struct dom_sid *sid;
        struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
-       unsigned int i;
        int ret;
 
-       /* We've to walk over all modification entries and consider the
-        * "primaryGroupID" 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.
-        */
-       el = NULL;
-       for (i = 0; i < ac->msg->num_elements; i++) {
-               if (ldb_attr_cmp(ac->msg->elements[i].name,
-                                "primaryGroupID") != 0) {
-                       continue;
-               }
-
-               el = &ac->msg->elements[i];
-               if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
-                       return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
-               }
-               if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
-                       return LDB_ERR_UNWILLING_TO_PERFORM;
-               }
+       ret = samldb_get_single_valued_attr(ac, "primaryGroupID", &el);
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
        if (el == NULL) {
+               /* we are not affected */
                return LDB_SUCCESS;
        }
 
-       /* Okay, now for sure we are performing a "primaryGroupID" replace */
-
        /* Fetch informations from the existing object */
 
        ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
@@ -1044,6 +1072,9 @@ static int samldb_prim_group_change(struct samldb_ctx *ac)
        if (ret != LDB_SUCCESS) {
                return ret;
        }
+       if (res->count != 1) {
+               return ldb_operr(ldb);
+       }
 
        /* Finds out the DN of the old primary group */
 
@@ -1109,7 +1140,7 @@ static int samldb_prim_group_change(struct samldb_ctx *ac)
                }
 
                /* Remove the "member" attribute on the new primary group */
-               msg = talloc_zero(ac, struct ldb_message);
+               msg = ldb_msg_new(ac->msg);
                if (msg == NULL) {
                        return ldb_module_oom(ac->module);
                }
@@ -1125,9 +1156,10 @@ static int samldb_prim_group_change(struct samldb_ctx *ac)
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
+               talloc_free(msg);
 
                /* Add a "member" attribute for the previous primary group */
-               msg = talloc_zero(ac, struct ldb_message);
+               msg = ldb_msg_new(ac->msg);
                if (msg == NULL) {
                        return ldb_module_oom(ac->module);
                }
@@ -1143,6 +1175,7 @@ static int samldb_prim_group_change(struct samldb_ctx *ac)
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
+               talloc_free(msg);
        }
 
        talloc_free(res);
@@ -1163,15 +1196,264 @@ static int samldb_prim_group_trigger(struct samldb_ctx *ac)
        return ret;
 }
 
+static int samldb_user_account_control_change(struct samldb_ctx *ac)
+{
+       struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
+       uint32_t user_account_control, account_type;
+       struct ldb_message_element *el;
+       struct ldb_message *tmp_msg;
+       int ret;
+
+       ret = samldb_get_single_valued_attr(ac, "userAccountControl", &el);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (el == NULL) {
+               /* we are not affected */
+               return LDB_SUCCESS;
+       }
+
+       /* Create a temporary message for fetching the "userAccountControl" */
+       tmp_msg = ldb_msg_new(ac->msg);
+       if (tmp_msg == NULL) {
+               return ldb_module_oom(ac->module);
+       }
+       ret = ldb_msg_add(tmp_msg, el, 0);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       user_account_control = ldb_msg_find_attr_as_uint(tmp_msg,
+                                                        "userAccountControl",
+                                                        0);
+       talloc_free(tmp_msg);
+
+       /* Temporary duplicate accounts aren't allowed */
+       if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
+               return LDB_ERR_OTHER;
+       }
+
+       account_type = ds_uf2atype(user_account_control);
+       if (account_type == 0) {
+               ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
+               return LDB_ERR_UNWILLING_TO_PERFORM;
+       }
+       ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
+                                account_type);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       el = ldb_msg_find_element(ac->msg, "sAMAccountType");
+       el->flags = LDB_FLAG_MOD_REPLACE;
+
+       if (user_account_control
+           & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
+               ret = samdb_msg_add_string(ldb, ac->msg, ac->msg,
+                                          "isCriticalSystemObject", "TRUE");
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+               el = ldb_msg_find_element(ac->msg,
+                                          "isCriticalSystemObject");
+               el->flags = LDB_FLAG_MOD_REPLACE;
+       }
+
+       if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
+               uint32_t rid = ds_uf2prim_group_rid(user_account_control);
+               ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
+                                        "primaryGroupID", rid);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+               el = ldb_msg_find_element(ac->msg,
+                                          "primaryGroupID");
+               el->flags = LDB_FLAG_MOD_REPLACE;
+       }
+
+       return LDB_SUCCESS;
+}
+
+static int samldb_group_type_change(struct samldb_ctx *ac)
+{
+       struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
+       uint32_t group_type, old_group_type, account_type;
+       struct ldb_message_element *el;
+       struct ldb_message *tmp_msg;
+       int ret;
+
+       ret = samldb_get_single_valued_attr(ac, "groupType", &el);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (el == NULL) {
+               /* we are not affected */
+               return LDB_SUCCESS;
+       }
+
+       /* Create a temporary message for fetching the "groupType" */
+       tmp_msg = ldb_msg_new(ac->msg);
+       if (tmp_msg == NULL) {
+               return ldb_module_oom(ac->module);
+       }
+       ret = ldb_msg_add(tmp_msg, el, 0);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       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);
+       if (old_group_type == 0) {
+               return ldb_operr(ldb);
+       }
+
+       /* Group type switching isn't so easy as it seems: We can only
+        * change in this directions: global <-> universal <-> local
+        * 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)) {
+                       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;
+       }
+
+       account_type =  ds_gtype2atype(group_type);
+       if (account_type == 0) {
+               ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
+               return LDB_ERR_UNWILLING_TO_PERFORM;
+       }
+       ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
+                                account_type);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       el = ldb_msg_find_element(ac->msg, "sAMAccountType");
+       el->flags = LDB_FLAG_MOD_REPLACE;
+
+       return LDB_SUCCESS;
+}
+
+static int samldb_sam_accountname_check(struct samldb_ctx *ac)
+{
+       struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
+       const char *no_attrs[] = { NULL };
+       struct ldb_result *res;
+       const char *sam_accountname, *enc_str;
+       struct ldb_message_element *el;
+       struct ldb_message *tmp_msg;
+       int ret;
+
+       ret = samldb_get_single_valued_attr(ac, "sAMAccountName", &el);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (el == NULL) {
+               /* we are not affected */
+               return LDB_SUCCESS;
+       }
+
+       /* Create a temporary message for fetching the "sAMAccountName" */
+       tmp_msg = ldb_msg_new(ac->msg);
+       if (tmp_msg == NULL) {
+               return ldb_module_oom(ac->module);
+       }
+       ret = ldb_msg_add(tmp_msg, el, 0);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       sam_accountname = talloc_steal(ac,
+                                      ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
+       talloc_free(tmp_msg);
+
+       if (sam_accountname == NULL) {
+               /* 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);
+       if (enc_str == NULL) {
+               return ldb_module_oom(ac->module);
+       }
+
+       /* 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);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (res->count > 1) {
+               return ldb_operr(ldb);
+       } else if (res->count == 1) {
+               if (ldb_dn_compare(res->msgs[0]->dn, ac->msg->dn) != 0) {
+                       ldb_asprintf_errstring(ldb,
+                                              "samldb: Account name (sAMAccountName) '%s' already in use!",
+                                              sam_accountname);
+                       return LDB_ERR_ENTRY_ALREADY_EXISTS;
+               }
+       }
+       talloc_free(res);
+
+       return LDB_SUCCESS;
+}
+
 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;
+       struct ldb_dn *member_dn;
        uint32_t prim_group_rid;
        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 = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
+                        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. */
@@ -1182,6 +1464,8 @@ 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;
+
                        member_dn = ldb_dn_from_ldb_val(ac, ldb,
                                                        &el->values[j]);
                        if (!ldb_dn_validate(member_dn)) {
@@ -1201,12 +1485,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;
@@ -1237,19 +1523,223 @@ 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 (dom_sid_equal(group_sid, sid)) {
+                               return LDB_ERR_ENTRY_ALREADY_EXISTS;
                        }
+               }
+       }
 
-                       if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
-                               return LDB_ERR_ENTRY_ALREADY_EXISTS;
+       return LDB_SUCCESS;
+}
+
+/* This trigger adapts the "servicePrincipalName" attributes if the
+ * "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, *el2 = NULL;
+       struct ldb_message *msg;
+       const char *attrs[] = { "servicePrincipalName", NULL };
+       struct ldb_result *res;
+       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) &&
+                   (LDB_FLAG_MOD_TYPE(ac->msg->elements[i].flags)
+                                      != LDB_FLAG_MOD_DELETE)) {
+                       el = &ac->msg->elements[i];
+               }
+               if ((ldb_attr_cmp(ac->msg->elements[i].name,
+                                 "sAMAccountName") == 0) &&
+                   (LDB_FLAG_MOD_TYPE(ac->msg->elements[i].flags)
+                                      != LDB_FLAG_MOD_DELETE)) {
+                       el2 = &ac->msg->elements[i];
+               }
+       }
+       if ((el == NULL) && (el2 == NULL)) {
+               /* we are not affected */
+               return LDB_SUCCESS;
+       }
+
+       /* Create a temporary message for fetching the "dNSHostName" */
+       if (el != 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);
+
+               old_dns_hostname = samdb_search_string(ldb, ac, ac->msg->dn,
+                                                      "dNSHostName", NULL);
+       }
+
+       /* Create a temporary message for fetching the "sAMAccountName" */
+       if (el2 != NULL) {
+               char *tempstr, *tempstr2;
+
+               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);
+
+               tempstr2 = talloc_strdup(ac,
+                                        samdb_search_string(ldb, ac, ac->msg->dn, "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;
+       }
+
+       if (old_dns_hostname == NULL) {
+               /* we cannot change when the old name is unknown */
+               dns_hostname = NULL;
+       }
+       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;
+       }
+
+       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;
+       }
+
+       /* Potential "servicePrincipalName" changes in the same request have to
+        * be handled before the update (Windows behaviour). */
+       el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
+       if (el != NULL) {
+               msg = ldb_msg_new(ac->msg);
+               if (msg == NULL) {
+                       return ldb_module_oom(ac->module);
+               }
+               msg->dn = ac->msg->dn;
+
+               do {
+                       ret = ldb_msg_add(msg, el, el->flags);
+                       if (ret != LDB_SUCCESS) {
+                               return ret;
                        }
+
+                       ldb_msg_remove_element(ac->msg, el);
+
+                       el = ldb_msg_find_element(ac->msg,
+                                                 "servicePrincipalName");
+               } while (el != NULL);
+
+               ret = dsdb_module_modify(ac->module, msg,
+                                        DSDB_FLAG_NEXT_MODULE);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
                }
+               talloc_free(msg);
        }
 
+       /* Fetch the "servicePrincipalName"s if any */
+       ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
+                        NULL);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if ((res->count != 1) || (res->msgs[0]->num_elements > 1)) {
+               return ldb_operr(ldb);
+       }
+
+       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/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++) {
+                       char *old_str, *new_str, *pos;
+                       const char *tok;
+
+                       old_str = (char *)
+                               res->msgs[0]->elements[0].values[i].data;
+
+                       new_str = talloc_strdup(ac->msg,
+                                               strtok_r(old_str, "/", &pos));
+                       if (new_str == NULL) {
+                               return ldb_module_oom(ac->module);
+                       }
+
+                       while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
+                               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);
+                               if (new_str == NULL) {
+                                       return ldb_module_oom(ac->module);
+                               }
+                       }
+
+                       ret = ldb_msg_add_string(ac->msg,
+                                                "servicePrincipalName",
+                                                new_str);
+                       if (ret != LDB_SUCCESS) {
+                               return ret;
+                       }
+               }
+
+               el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
+               el->flags = LDB_FLAG_MOD_REPLACE;
+       }
+
+       talloc_free(res);
+
        return LDB_SUCCESS;
 }
 
@@ -1357,7 +1847,6 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
        struct ldb_message_element *el, *el2;
        bool modified = false;
        int ret;
-       uint32_t account_type;
 
        if (ldb_dn_is_special(req->op.mod.message->dn)) {
                /* do not manipulate our control entries */
@@ -1366,6 +1855,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) {
@@ -1406,75 +1902,6 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
                return ldb_operr(ldb);
        }
 
-       el = ldb_msg_find_element(ac->msg, "groupType");
-       if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE)
-           && el->num_values == 1) {
-               uint32_t group_type, old_group_type;
-
-               modified = true;
-
-               group_type = ldb_msg_find_attr_as_uint(ac->msg, "groupType", 0);
-               old_group_type = samdb_search_uint(ldb, ac, 0, ac->msg->dn,
-                                                  "groupType", NULL);
-               if (old_group_type == 0) {
-                       return ldb_operr(ldb);
-               }
-
-               /* Group type switching isn't so easy as it seems: We can only
-                * change in this directions: global <-> universal <-> local
-                * 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)) {
-                               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;
-               }
-
-               account_type =  ds_gtype2atype(group_type);
-               if (account_type == 0) {
-                       ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
-                       return LDB_ERR_UNWILLING_TO_PERFORM;
-               }
-               ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
-                                        "sAMAccountType",
-                                        account_type);
-               if (ret != LDB_SUCCESS) {
-                       return ret;
-               }
-               el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
-               el2->flags = LDB_FLAG_MOD_REPLACE;
-       }
-       el = ldb_msg_find_element(ac->msg, "groupType");
-       if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
-               return LDB_ERR_UNWILLING_TO_PERFORM;
-       }
-
        el = ldb_msg_find_element(ac->msg, "primaryGroupID");
        if (el != NULL) {
                ret = samldb_prim_group_change(ac);
@@ -1484,64 +1911,30 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
        }
 
        el = ldb_msg_find_element(ac->msg, "userAccountControl");
-       if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE)
-           && el->num_values == 1) {
-               uint32_t user_account_control;
-
+       if (el != NULL) {
                modified = true;
-
-               user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
-                                                                "userAccountControl",
-                                                                0);
-
-               /* Temporary duplicate accounts aren't allowed */
-               if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
-                       return LDB_ERR_OTHER;
-               }
-
-               account_type = ds_uf2atype(user_account_control);
-               if (account_type == 0) {
-                       ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
-                       return LDB_ERR_UNWILLING_TO_PERFORM;
-               }
-               ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
-                                        "sAMAccountType",
-                                        account_type);
+               ret = samldb_user_account_control_change(ac);
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
-               el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
-               el2->flags = LDB_FLAG_MOD_REPLACE;
+       }
 
-               if (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
-                       ret = samdb_msg_add_string(ldb, ac->msg, ac->msg,
-                                                  "isCriticalSystemObject",
-                                                  "TRUE");
-                       if (ret != LDB_SUCCESS) {
-                               return ret;
-                       }
-                       el2 = ldb_msg_find_element(ac->msg,
-                                                  "isCriticalSystemObject");
-                       el2->flags = LDB_FLAG_MOD_REPLACE;
+       el = ldb_msg_find_element(ac->msg, "groupType");
+       if (el != NULL) {
+               modified = true;
+               ret = samldb_group_type_change(ac);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
                }
+       }
 
-               if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
-                       uint32_t rid = ds_uf2prim_group_rid(user_account_control);
-
-                       ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
-                                                "primaryGroupID", rid);
-                       if (ret != LDB_SUCCESS) {
-                               return ret;
-                       }
-                       el2 = ldb_msg_find_element(ac->msg,
-                                                  "primaryGroupID");
-                       el2->flags = LDB_FLAG_MOD_REPLACE;
+       el = ldb_msg_find_element(ac->msg, "sAMAccountName");
+       if (el != NULL) {
+               ret = samldb_sam_accountname_check(ac);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
                }
        }
-       el = ldb_msg_find_element(ac->msg, "userAccountControl");
-       if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
-               return LDB_ERR_UNWILLING_TO_PERFORM;
-       }
 
        el = ldb_msg_find_element(ac->msg, "member");
        if (el != NULL) {
@@ -1551,6 +1944,16 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
                }
        }
 
+       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) {
+                       return ret;
+               }
+       }
+
        if (modified) {
                struct ldb_request *child_req;
 
@@ -1675,7 +2078,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,
@@ -1683,3 +2086,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);
+}