s4:dsdb/samdb: optimize samldb_prim_group_change()
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / samldb.c
index 13b173a6bcdc75edb191f3e8148143bf415e92c1..ee43d84398f120d813cc896ce25c05c2ba77893d 100644 (file)
@@ -756,53 +756,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)
  *
@@ -1051,15 +1004,12 @@ static int samldb_prim_group_change(struct samldb_ctx *ac)
        struct ldb_result *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;
 
-       ret = samldb_get_single_valued_attr(ac, "primaryGroupID", &el);
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
+       el = dsdb_get_single_valued_attr(ac->msg, "primaryGroupID");
        if (el == NULL) {
                /* we are not affected */
                return LDB_SUCCESS;
@@ -1078,22 +1028,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);
        }
 
@@ -1109,76 +1054,81 @@ 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;
+       }
+
+       prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
+                                            ldap_encode_ndr_dom_sid(ac, prev_sid));
+       if (prev_prim_group_dn == NULL) {
+               return ldb_operr(ldb);
+       }
+
+       new_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), new_rid);
+       if (new_sid == NULL) {
                return ldb_operr(ldb);
        }
 
        new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
-                                           ldap_encode_ndr_dom_sid(ac, sid));
+                                           ldap_encode_ndr_dom_sid(ac, new_sid));
        if (new_prim_group_dn == NULL) {
                /* Here we know if the specified new primary group candidate is
                 * valid or not. */
                return LDB_ERR_UNWILLING_TO_PERFORM;
        }
 
-       /* 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);
+       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);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       talloc_free(msg);
 
        return LDB_SUCCESS;
 }
@@ -1204,10 +1154,7 @@ 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");
        if (el == NULL) {
                /* we are not affected */
                return LDB_SUCCESS;
@@ -1280,10 +1227,7 @@ static int samldb_group_type_change(struct samldb_ctx *ac)
        struct ldb_message *tmp_msg;
        int ret;
 
-       ret = samldb_get_single_valued_attr(ac, "groupType", &el);
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
+       el = dsdb_get_single_valued_attr(ac->msg, "groupType");
        if (el == NULL) {
                /* we are not affected */
                return LDB_SUCCESS;
@@ -1369,10 +1313,7 @@ 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");
        if (el == NULL) {
                /* we are not affected */
                return LDB_SUCCESS;
@@ -1427,13 +1368,33 @@ 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;
+       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. */
@@ -1444,6 +1405,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)) {
@@ -1463,12 +1426,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;
@@ -1499,14 +1464,7 @@ 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;
                        }
                }
@@ -1529,27 +1487,8 @@ static int samldb_service_principal_names_change(struct samldb_ctx *ac)
        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];
-               }
-       }
+       el = dsdb_get_single_valued_attr(ac->msg, "dNSHostName");
+       el2 = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName");
        if ((el == NULL) && (el2 == NULL)) {
                /* we are not affected */
                return LDB_SUCCESS;