s4-dsdb: pass parent request to dsdb_module_*() functions
[samba.git] / source4 / dsdb / samdb / ldb_modules / objectclass.c
index d2b4f10838b65fe3fca24b41c4b1d1d15508c4b5..b72b9bb8e7c9d2a8e21deebd67f795f8166aef7b 100644 (file)
@@ -319,14 +319,18 @@ static int fix_dn(struct ldb_context *ldb,
        char *upper_rdn_attr;
        const struct ldb_val *rdn_val;
 
-       /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
+       /* Fix up the DN to be in the standard form, taking particular care to
+        * match the parent DN */
        *fixed_dn = ldb_dn_copy(mem_ctx, parent_dn);
+       if (*fixed_dn == NULL) {
+               return ldb_oom(ldb);
+       }
 
        /* We need the attribute name in upper case */
        upper_rdn_attr = strupper_talloc(*fixed_dn, 
                                         ldb_dn_get_rdn_name(newdn));
-       if (!upper_rdn_attr) {
-               return ldb_operr(ldb);
+       if (upper_rdn_attr == NULL) {
+               return ldb_oom(ldb);
        }
 
        /* Create a new child */
@@ -364,7 +368,6 @@ static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
        struct oc_context *ac;
        struct ldb_dn *parent_dn;
        const struct ldb_val *val;
-       char *value;
        int ret;
        static const char * const parent_attrs[] = { "objectClass", NULL };
 
@@ -385,6 +388,7 @@ static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
                instanceType = ldb_msg_find_attr_as_uint(req->op.add.message,
                                                         "instanceType", 0);
                if (!(instanceType & INSTANCE_TYPE_IS_NC_HEAD)) {
+                       char *referral_uri;
                        /* When we are trying to readd the root basedn then
                         * this is denied, but with an interesting mechanism:
                         * there is generated a referral with the last
@@ -394,13 +398,13 @@ static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
                        if (val == NULL) {
                                return ldb_operr(ldb);
                        }
-                       value = talloc_asprintf(req, "ldap://%s/%s", val->data,
-                                               ldb_dn_get_linearized(req->op.add.message->dn));
-                       if (value == NULL) {
-                               return ldb_oom(ldb);
+                       referral_uri = talloc_asprintf(req, "ldap://%s/%s", val->data,
+                                                      ldb_dn_get_linearized(req->op.add.message->dn));
+                       if (referral_uri == NULL) {
+                               return ldb_module_oom(module);
                        }
 
-                       return ldb_module_send_referral(req, value);
+                       return ldb_module_send_referral(req, referral_uri);
                }
        }
 
@@ -417,7 +421,7 @@ static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
        /* get copy of parent DN */
        parent_dn = ldb_dn_get_parent(ac, ac->req->op.add.message->dn);
        if (parent_dn == NULL) {
-               return ldb_oom(ldb);
+               return ldb_operr(ldb);
        }
 
        ret = ldb_build_search_req(&search_req, ldb,
@@ -459,7 +463,7 @@ static bool check_rodc_ntdsdsa_add(struct oc_context *ac,
 
 static int objectclass_do_add(struct oc_context *ac)
 {
-       struct ldb_context *ldb;
+       struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
        struct ldb_request *add_req;
        struct ldb_message_element *objectclass_element, *el;
        struct ldb_message *msg;
@@ -474,9 +478,10 @@ static int objectclass_do_add(struct oc_context *ac)
        bool found;
        int ret;
 
-       ldb = ldb_module_get_ctx(ac->module);
-
        msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
+       if (msg == NULL) {
+               return ldb_module_oom(ac->module);
+       }
 
        /* Check if we have a valid parent - this check is needed since
         * we don't get a LDB_ERR_NO_SUCH_OBJECT error. */
@@ -485,8 +490,8 @@ static int objectclass_do_add(struct oc_context *ac)
 
                /* An add operation on partition DNs without "NC-add" operation
                 * isn't allowed. */
-               instanceType = ldb_msg_find_attr_as_uint(ac->req->op.add.message,
-                                                        "instanceType", 0);
+               instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType",
+                                                        0);
                if (!(instanceType & INSTANCE_TYPE_IS_NC_HEAD)) {
                        ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not exist!", 
                                               ldb_dn_get_linearized(msg->dn));
@@ -509,26 +514,24 @@ static int objectclass_do_add(struct oc_context *ac)
                }
        }
 
-       mem_ctx = talloc_new(ac);
-       if (mem_ctx == NULL) {
-               return ldb_oom(ldb);
-       }
-
        if (ac->schema != NULL) {
                objectclass_element = ldb_msg_find_element(msg, "objectClass");
                if (!objectclass_element) {
                        ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, no objectclass specified!",
                                               ldb_dn_get_linearized(msg->dn));
-                       talloc_free(mem_ctx);
                        return LDB_ERR_OBJECT_CLASS_VIOLATION;
                }
                if (objectclass_element->num_values == 0) {
                        ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, at least one (structural) objectclass has to be specified!",
                                               ldb_dn_get_linearized(msg->dn));
-                       talloc_free(mem_ctx);
                        return LDB_ERR_CONSTRAINT_VIOLATION;
                }
 
+               mem_ctx = talloc_new(ac);
+               if (mem_ctx == NULL) {
+                       return ldb_module_oom(ac->module);
+               }
+
                /* Here we do now get the "objectClass" list from the
                 * database. */
                ret = objectclass_sort(ac->module, ac->schema, mem_ctx,
@@ -560,13 +563,40 @@ static int objectclass_do_add(struct oc_context *ac)
 
                /* Move from the linked list back into an ldb msg */
                for (current = sorted; current; current = current->next) {
-                       value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
-                       if (value == NULL) {
+                       const char *objectclass_name = current->objectclass->lDAPDisplayName;
+
+                       /* LSA-specific objectclasses per default not
+                        * allowed to be created over LDAP, so we need
+                        * to tell if this connection is LDAP (ie
+                        * marked as untrusted), and if the client is
+                        * adding these particular objectClass values
+                        * we must reject */
+
+                       /* Hongwei Sun from Microsoft explians:
+                          The constraint in 3.1.1.5.2.2 MS-ADTS means that the TDO
+                          cannot be added through LDAP interface, instead it can only be
+                          created through LSA Policy API.  This is also explained in
+                          7.1.6.9.7 MS-ADTS as follows:
+
+                          "Despite being replicated normally between peer DCs in a domain,
+                          the process of creating or manipulating TDOs is specifically
+                          restricted to the LSA Policy APIs, as detailed in [MS-LSAD] section
+                          3.1.1.5. Unlike other objects in the DS, TDOs may not be created or
+                          manipulated by client machines over the LDAPv3 transport."
+                       */
+
+                       if (ldb_req_is_untrusted(ac->req) &&
+                           ((strcasecmp(objectclass_name, "secret") == 0) ||
+                            (strcasecmp(objectclass_name, "trustedDomain") == 0))) {
+                               ldb_asprintf_errstring(ldb,
+                                                      "objectclass: object class '%s' is LSA-specific, rejecting creation of '%s' over LDAP!",
+                                                      objectclass_name,
+                                                      ldb_dn_get_linearized(msg->dn));
                                talloc_free(mem_ctx);
-                               return ldb_oom(ldb);
+                               return LDB_ERR_UNWILLING_TO_PERFORM;
                        }
 
-                       ret = ldb_msg_add_string(msg, "objectClass", value);
+                       ret = ldb_msg_add_string(msg, "objectClass", objectclass_name);
                        if (ret != LDB_SUCCESS) {
                                ldb_set_errstring(ldb,
                                                  "objectclass: could not re-add sorted "
@@ -583,7 +613,7 @@ static int objectclass_do_add(struct oc_context *ac)
 
                /* Make sure its valid to add an object of this type */
                objectclass = get_last_structural_class(ac->schema,
-                                                       objectclass_element);
+                                                       objectclass_element, ac->req);
                if(objectclass == NULL) {
                        ldb_asprintf_errstring(ldb,
                                               "Failed to find a structural class for %s",
@@ -617,16 +647,10 @@ static int objectclass_do_add(struct oc_context *ac)
                if (objectclass->systemOnly &&
                    !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
                    !check_rodc_ntdsdsa_add(ac, objectclass)) {
-                       ldb_asprintf_errstring(ldb, "objectClass %s is systemOnly, rejecting creation of %s",
-                                               objectclass->lDAPDisplayName, ldb_dn_get_linearized(msg->dn));
-                       return LDB_ERR_UNWILLING_TO_PERFORM;
-               }
-
-               if (((strcmp(objectclass->lDAPDisplayName, "secret") == 0) ||
-                    (strcmp(objectclass->lDAPDisplayName, "trustedDomain") == 0)) &&
-                    !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
-                       ldb_asprintf_errstring(ldb, "objectClass %s is LSA-specific, rejecting creation of %s",
-                                               objectclass->lDAPDisplayName, ldb_dn_get_linearized(msg->dn));
+                       ldb_asprintf_errstring(ldb,
+                                              "objectclass: object class '%s' is system-only, rejecting creation of '%s'!",
+                                              objectclass->lDAPDisplayName,
+                                              ldb_dn_get_linearized(msg->dn));
                        return LDB_ERR_UNWILLING_TO_PERFORM;
                }
 
@@ -676,7 +700,7 @@ static int objectclass_do_add(struct oc_context *ac)
                                                      objectclass->defaultObjectCategory);
                        }
                        if (value == NULL) {
-                               return ldb_oom(ldb);
+                               return ldb_module_oom(ac->module);
                        }
 
                        ret = ldb_msg_add_string(msg, "objectCategory", value);
@@ -829,7 +853,7 @@ static int objectclass_modify(struct ldb_module *module, struct ldb_request *req
 
        msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
        if (msg == NULL) {
-               return ldb_operr(ldb);
+               return ldb_module_oom(ac->module);
        }
 
        /* For now change everything except the objectclasses */
@@ -840,6 +864,29 @@ static int objectclass_modify(struct ldb_module *module, struct ldb_request *req
                oc_changes = true;
        }
 
+       /* MS-ADTS 3.1.1.5.3.5 - on a forest level < 2003 we do allow updates
+        * only on application NCs - not on the standard DCs */
+       if (oc_changes &&
+           (dsdb_forest_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003)) {
+               struct ldb_dn *nc_root;
+
+               ret = dsdb_find_nc_root(ldb, ac, req->op.mod.message->dn,
+                                       &nc_root);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+
+               if ((ldb_dn_compare(nc_root, ldb_get_default_basedn(ldb)) == 0) ||
+                   (ldb_dn_compare(nc_root, ldb_get_config_basedn(ldb)) == 0) ||
+                   (ldb_dn_compare(nc_root, ldb_get_schema_basedn(ldb)) == 0)) {
+                       ldb_set_errstring(ldb,
+                                         "objectclass: object class changes on objects under the standard name contexts not allowed!");
+                       return LDB_ERR_UNWILLING_TO_PERFORM;
+               }
+
+               talloc_free(nc_root);
+       }
+
        ret = ldb_build_mod_req(&down_req, ldb, ac,
                                msg,
                                req->controls, ac,
@@ -921,7 +968,7 @@ static int objectclass_do_mod(struct oc_context *ac)
        TALLOC_CTX *mem_ctx;
        struct class_list *sorted, *current;
        const struct dsdb_class *objectclass;
-       unsigned int i, j;
+       unsigned int i, j, k;
        bool found, replace = false;
        int ret;
 
@@ -939,215 +986,224 @@ static int objectclass_do_mod(struct oc_context *ac)
                return ldb_operr(ldb);
        }
 
-       oc_el_change = ldb_msg_find_element(ac->req->op.mod.message,
-                                           "objectClass");
-       if (oc_el_change == NULL) {
-               /* we should have an objectclass change operation */
-               return ldb_operr(ldb);
-       }
-
        /* use a new message structure */
        msg = ldb_msg_new(ac);
        if (msg == NULL) {
-               return ldb_oom(ldb);
+               return ldb_module_oom(ac->module);
        }
 
        msg->dn = ac->req->op.mod.message->dn;
 
        mem_ctx = talloc_new(ac);
        if (mem_ctx == NULL) {
-               return ldb_oom(ldb);
+               return ldb_module_oom(ac->module);
        }
 
-       switch (oc_el_change->flags & LDB_FLAG_MOD_MASK) {
-       case LDB_FLAG_MOD_ADD:
-               /* Merge the two message elements */
-               for (i = 0; i < oc_el_change->num_values; i++) {
-                       for (j = 0; j < oc_el_entry->num_values; j++) {
-                               if (strcasecmp((char *)oc_el_change->values[i].data,
-                                              (char *)oc_el_entry->values[j].data) == 0) {
-                                       /* we cannot add an already existing object class */
+       /* We've to walk over all "objectClass" message elements */
+       for (k = 0; k < ac->req->op.mod.message->num_elements; k++) {
+               if (ldb_attr_cmp(ac->req->op.mod.message->elements[k].name,
+                                "objectClass") != 0) {
+                       continue;
+               }
+
+               oc_el_change = &ac->req->op.mod.message->elements[k];
+
+               switch (oc_el_change->flags & LDB_FLAG_MOD_MASK) {
+               case LDB_FLAG_MOD_ADD:
+                       /* Merge the two message elements */
+                       for (i = 0; i < oc_el_change->num_values; i++) {
+                               for (j = 0; j < oc_el_entry->num_values; j++) {
+                                       if (ldb_attr_cmp((char *)oc_el_change->values[i].data,
+                                                        (char *)oc_el_entry->values[j].data) == 0) {
+                                               ldb_asprintf_errstring(ldb,
+                                                                      "objectclass: cannot re-add an existing objectclass: '%.*s'!",
+                                                                      (int)oc_el_change->values[i].length,
+                                                                      (const char *)oc_el_change->values[i].data);
+                                               talloc_free(mem_ctx);
+                                               return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
+                                       }
+                               }
+                               /* append the new object class value - code was
+                                * copied from "ldb_msg_add_value" */
+                               vals = talloc_realloc(oc_el_entry, oc_el_entry->values,
+                                                     struct ldb_val,
+                                                     oc_el_entry->num_values + 1);
+                               if (vals == NULL) {
                                        talloc_free(mem_ctx);
-                                       return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
+                                       return ldb_module_oom(ac->module);
                                }
+                               oc_el_entry->values = vals;
+                               oc_el_entry->values[oc_el_entry->num_values] =
+                                                       oc_el_change->values[i];
+                               ++(oc_el_entry->num_values);
                        }
-                       /* append the new object class value - code was copied
-                        * from "ldb_msg_add_value" */
-                       vals = talloc_realloc(oc_el_entry, oc_el_entry->values,
-                                             struct ldb_val,
-                                             oc_el_entry->num_values + 1);
-                       if (vals == NULL) {
+
+                       objectclass = get_last_structural_class(ac->schema,
+                                                               oc_el_change, ac->req);
+                       if (objectclass != NULL) {
+                               ldb_asprintf_errstring(ldb,
+                                                      "objectclass: cannot add a new top-most structural objectclass '%s'!",
+                                                      objectclass->lDAPDisplayName);
                                talloc_free(mem_ctx);
-                               return ldb_oom(ldb);
+                               return LDB_ERR_OBJECT_CLASS_VIOLATION;
                        }
-                       oc_el_entry->values = vals;
-                       oc_el_entry->values[oc_el_entry->num_values] =
-                               oc_el_change->values[i];
-                       ++(oc_el_entry->num_values);
-               }
 
-               objectclass = get_last_structural_class(ac->schema,
-                                                       oc_el_change);
-               if (objectclass != NULL) {
-                       /* we cannot add a new structural object class */
-                       talloc_free(mem_ctx);
-                       return LDB_ERR_OBJECT_CLASS_VIOLATION;
-               }
+                       /* Now do the sorting */
+                       ret = objectclass_sort(ac->module, ac->schema, mem_ctx,
+                                              oc_el_entry, &sorted);
+                       if (ret != LDB_SUCCESS) {
+                               talloc_free(mem_ctx);
+                               return ret;
+                       }
 
-               /* Now do the sorting */
-               ret = objectclass_sort(ac->module, ac->schema, mem_ctx,
-                                      oc_el_entry, &sorted);
-               if (ret != LDB_SUCCESS) {
-                       talloc_free(mem_ctx);
-                       return ret;
-               }
+                       break;
 
-               break;
+               case LDB_FLAG_MOD_REPLACE:
+                       /* Do the sorting for the change message element */
+                       ret = objectclass_sort(ac->module, ac->schema, mem_ctx,
+                                              oc_el_change, &sorted);
+                       if (ret != LDB_SUCCESS) {
+                               talloc_free(mem_ctx);
+                               return ret;
+                       }
 
-       case LDB_FLAG_MOD_REPLACE:
-               /* Do the sorting for the change message element */
-               ret = objectclass_sort(ac->module, ac->schema, mem_ctx,
-                                      oc_el_change, &sorted);
-               if (ret != LDB_SUCCESS) {
-                       talloc_free(mem_ctx);
-                       return ret;
-               }
+                       /* this is a replace */
+                       replace = true;
 
-               /* this is a replace */
-               replace = true;
+                       break;
 
-               break;
+               case LDB_FLAG_MOD_DELETE:
+                       /* get the actual top-most structural objectclass */
+                       objectclass = get_last_structural_class(ac->schema,
+                                                               oc_el_entry, ac->req);
+                       if (objectclass == NULL) {
+                               /* no structural objectclass? */
+                               talloc_free(mem_ctx);
+                               return ldb_operr(ldb);
+                       }
 
-       case LDB_FLAG_MOD_DELETE:
-               /* get the actual top-most structural objectclass */
-               objectclass = get_last_structural_class(ac->schema,
-                                                       oc_el_entry);
-               if (objectclass == NULL) {
-                       talloc_free(mem_ctx);
-                       return ldb_operr(ldb);
-               }
+                       /* Merge the two message elements */
+                       for (i = 0; i < oc_el_change->num_values; i++) {
+                               found = false;
+                               for (j = 0; j < oc_el_entry->num_values; j++) {
+                                       if (ldb_attr_cmp((char *)oc_el_change->values[i].data,
+                                                        (char *)oc_el_entry->values[j].data) == 0) {
+                                               found = true;
+                                               /* delete the object class value
+                                                * - code was copied from
+                                                * "ldb_msg_remove_element" */
+                                               if (j != oc_el_entry->num_values - 1) {
+                                                       memmove(&oc_el_entry->values[j],
+                                                               &oc_el_entry->values[j+1],
+                                                               ((oc_el_entry->num_values-1) - j)*sizeof(struct ldb_val));
+                                               }
+                                               --(oc_el_entry->num_values);
+                                               break;
+                                       }
+                               }
+                               if (!found) {
+                                       /* we cannot delete a not existing
+                                        * object class */
+                                       ldb_asprintf_errstring(ldb,
+                                                              "objectclass: cannot delete this objectclass: '%.*s'!",
+                                                              (int)oc_el_change->values[i].length,
+                                                              (const char *)oc_el_change->values[i].data);
+                                       talloc_free(mem_ctx);
+                                       return LDB_ERR_NO_SUCH_ATTRIBUTE;
+                               }
+                       }
 
-               /* Merge the two message elements */
-               for (i = 0; i < oc_el_change->num_values; i++) {
+                       /* Make sure that the top-most structural object class
+                        * hasn't been deleted */
                        found = false;
-                       for (j = 0; j < oc_el_entry->num_values; j++) {
-                               if (strcasecmp((char *)oc_el_change->values[i].data,
-                                              (char *)oc_el_entry->values[j].data) == 0) {
+                       for (i = 0; i < oc_el_entry->num_values; i++) {
+                               if (ldb_attr_cmp(objectclass->lDAPDisplayName,
+                                                (char *)oc_el_entry->values[i].data) == 0) {
                                        found = true;
-                                       /* delete the object class value -
-                                        * code was copied from
-                                        * "ldb_msg_remove_element" */
-                                       if (j != oc_el_entry->num_values - 1) {
-                                               memmove(&oc_el_entry->values[j],
-                                                       &oc_el_entry->values[j+1],
-                                                       ((oc_el_entry->num_values-1) - j)*sizeof(struct ldb_val));
-                                       }
-                                       --(oc_el_entry->num_values);
                                        break;
                                }
                        }
                        if (!found) {
-                               /* we cannot delete a not existing object class */
-                               ldb_asprintf_errstring(ldb, "Cannot delete this %.*s ",
-                                              (int)oc_el_change->values[i].length, (const char *)oc_el_change->values[i].data);
-
+                               ldb_asprintf_errstring(ldb,
+                                                      "objectclass: cannot delete the top-most structural objectclass '%s'!",
+                                                      objectclass->lDAPDisplayName);
                                talloc_free(mem_ctx);
-                               return LDB_ERR_NO_SUCH_ATTRIBUTE;
+                               return LDB_ERR_OBJECT_CLASS_VIOLATION;
                        }
-               }
 
-               /* Make sure that the top-most structural objectclass wasn't
-                * deleted */
-               found = false;
-               for (i = 0; i < oc_el_entry->num_values; i++) {
-                       if (strcasecmp(objectclass->lDAPDisplayName,
-                           (char *)oc_el_entry->values[i].data) == 0) {
-                               found = true; break;
+                       /* Now do the sorting */
+                       ret = objectclass_sort(ac->module, ac->schema, mem_ctx,
+                                              oc_el_entry, &sorted);
+                       if (ret != LDB_SUCCESS) {
+                               talloc_free(mem_ctx);
+                               return ret;
                        }
-               }
-               if (!found) {
-                       talloc_free(mem_ctx);
-                       return LDB_ERR_OBJECT_CLASS_VIOLATION;
-               }
 
+                       break;
+               }
 
-               /* Now do the sorting */
-               ret = objectclass_sort(ac->module, ac->schema, mem_ctx,
-                                      oc_el_entry, &sorted);
+               /* (Re)-add an empty "objectClass" attribute on the object
+                * classes change message "msg". */
+               ldb_msg_remove_attr(msg, "objectClass");
+               ret = ldb_msg_add_empty(msg, "objectClass",
+                                       LDB_FLAG_MOD_REPLACE, &oc_el_change);
                if (ret != LDB_SUCCESS) {
                        talloc_free(mem_ctx);
                        return ret;
                }
 
-               break;
-       }
-
-       /* Only one "objectclass" attribute change element per modify request
-        * allowed! */
-       for (i = 0; i < ac->req->op.mod.message->num_elements; i++) {
-               if (ldb_attr_cmp(ac->req->op.mod.message->elements[i].name,
-                                "objectClass") != 0) continue;
-
-               if (ldb_msg_element_compare(&ac->req->op.mod.message->elements[i],
-                                           oc_el_change) != 0) {
-                       ldb_set_errstring(ldb,
-                                         "objectclass: only one 'objectClass' attribute change per modify request allowed!");
-                       talloc_free(mem_ctx);
-                       return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
+               /* Move from the linked list back into an ldb msg */
+               for (current = sorted; current; current = current->next) {
+                       value = talloc_strdup(msg,
+                                             current->objectclass->lDAPDisplayName);
+                       if (value == NULL) {
+                               talloc_free(mem_ctx);
+                               return ldb_module_oom(ac->module);
+                       }
+                       ret = ldb_msg_add_string(msg, "objectClass", value);
+                       if (ret != LDB_SUCCESS) {
+                               ldb_set_errstring(ldb,
+                                                 "objectclass: could not re-add sorted objectclasses!");
+                               talloc_free(mem_ctx);
+                               return ret;
+                       }
                }
-       }
-
-       ret = ldb_msg_add_empty(msg, "objectClass",
-                               LDB_FLAG_MOD_REPLACE, &oc_el_change);
-       if (ret != LDB_SUCCESS) {
-               talloc_free(mem_ctx);
-               return ldb_oom(ldb);
-       }
 
-       /* Move from the linked list back into an ldb msg */
-       for (current = sorted; current; current = current->next) {
-               value = talloc_strdup(msg,
-                                     current->objectclass->lDAPDisplayName);
-               if (value == NULL) {
-                       talloc_free(mem_ctx);
-                       return ldb_oom(ldb);
-               }
-               ret = ldb_msg_add_string(msg, "objectClass", value);
-               if (ret != LDB_SUCCESS) {
-                       ldb_set_errstring(ldb, "objectclass: could not re-add sorted objectclass to modify msg");
-                       talloc_free(mem_ctx);
-                       return ret;
+               if (replace) {
+                       /* Well, on replace we are nearly done: we have to test
+                        * if the change and entry message element are identical
+                        * ly. We can use "ldb_msg_element_compare" since now
+                        * the specified objectclasses match for sure in case.
+                        */
+                       ret = ldb_msg_element_compare(oc_el_entry,
+                                                     oc_el_change);
+                       if (ret == 0) {
+                               ret = ldb_msg_element_compare(oc_el_change,
+                                                             oc_el_entry);
+                       }
+                       if (ret == 0) {
+                               /* they are the same so we are done in this
+                                * case */
+                               talloc_free(mem_ctx);
+                               return ldb_module_done(ac->req, NULL, NULL,
+                                                      LDB_SUCCESS);
+                       } else {
+                               ldb_set_errstring(ldb,
+                                                 "objectclass: the specified objectclasses are not exactly the same as on the entry!");
+                               talloc_free(mem_ctx);
+                               return LDB_ERR_OBJECT_CLASS_VIOLATION;
+                       }
                }
-       }
-
-       talloc_free(mem_ctx);
 
-       if (replace) {
-               /* Well, on replace we are nearly done: we have to test if
-                * the change and entry message element are identically. We
-                * can use "ldb_msg_element_compare" since now the specified
-                * objectclasses match for sure in case. */
-               ret = ldb_msg_element_compare(oc_el_entry, oc_el_change);
-               if (ret == 0) {
-                       ret = ldb_msg_element_compare(oc_el_change,
-                                                     oc_el_entry);
-               }
-               if (ret == 0) {
-                       /* they are the same so we are done in this case */
-                       return ldb_module_done(ac->req, NULL, NULL,
-                                              LDB_SUCCESS);
-               } else {
-                       /* they're not exactly the same */
-                       return LDB_ERR_OBJECT_CLASS_VIOLATION;
-               }
+               /* Now we've applied all changes from "oc_el_change" to
+                * "oc_el_entry" therefore the new "oc_el_entry" will be
+                * "oc_el_change". */
+               oc_el_entry = oc_el_change;
        }
 
-       /* in the other cases we have the real change left to do */
+       talloc_free(mem_ctx);
 
-       ret = ldb_msg_sanity_check(ldb, msg);
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
+       /* Now we have the real and definitive change left to do */
 
        ret = ldb_build_mod_req(&mod_req, ldb, ac,
                                msg,
@@ -1178,7 +1234,7 @@ static int objectclass_rename(struct ldb_module *module, struct ldb_request *req
        ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_rename\n");
 
        /* do not manipulate our control entries */
-       if (ldb_dn_is_special(req->op.rename.newdn)) {
+       if (ldb_dn_is_special(req->op.rename.olddn)) {
                return ldb_next_request(module, req);
        }
 
@@ -1296,7 +1352,7 @@ static int objectclass_do_rename2(struct oc_context *ac)
                        /* existing entry without a valid object class? */
                        return ldb_operr(ldb);
                }
-               objectclass = get_last_structural_class(ac->schema, oc_el_entry);
+               objectclass = get_last_structural_class(ac->schema, oc_el_entry, ac->req);
                if (objectclass == NULL) {
                        /* existing entry without a valid object class? */
                        return ldb_operr(ldb);