s4:objectclass LDB module - Prevent write operations on constructed attributes
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / objectclass.c
index 7d00851792c04053c2b5cb3628eda2d4ce8bcc81..82b8835b0b700c5df4de675c9af44ae15cddbd35 100644 (file)
@@ -2,7 +2,7 @@
    ldb database library
 
    Copyright (C) Simo Sorce  2006-2008
-   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2007
+   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 
 #include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_errors.h"
-#include "ldb/include/ldb_private.h"
+#include "ldb_module.h"
+#include "dlinklist.h"
 #include "dsdb/samdb/samdb.h"
-#include "../lib/util/dlinklist.h"
 #include "librpc/ndr/libndr.h"
 #include "librpc/gen_ndr/ndr_security.h"
 #include "libcli/security/security.h"
 #include "auth/auth.h"
 #include "param/param.h"
+#include "../libds/common/flags.h"
 
 struct oc_context {
 
@@ -63,11 +62,14 @@ struct class_list {
 static struct oc_context *oc_init_context(struct ldb_module *module,
                                          struct ldb_request *req)
 {
+       struct ldb_context *ldb;
        struct oc_context *ac;
 
+       ldb = ldb_module_get_ctx(module);
+
        ac = talloc_zero(req, struct oc_context);
        if (ac == NULL) {
-               ldb_set_errstring(module->ldb, "Out of Memory");
+               ldb_set_errstring(ldb, "Out of Memory");
                return NULL;
        }
 
@@ -89,11 +91,14 @@ static int objectclass_sort(struct ldb_module *module,
                            struct ldb_message_element *objectclass_element,
                            struct class_list **sorted_out) 
 {
+       struct ldb_context *ldb;
        int i;
        int layer;
        struct class_list *sorted = NULL, *parent_class = NULL,
                *subclass = NULL, *unsorted = NULL, *current, *poss_subclass, *poss_parent, *new_parent;
 
+       ldb = ldb_module_get_ctx(module);
+
        /* DESIGN:
         *
         * We work on 4 different 'bins' (implemented here as linked lists):
@@ -127,13 +132,20 @@ static int objectclass_sort(struct ldb_module *module,
        for (i=0; i < objectclass_element->num_values; i++) {
                current = talloc(mem_ctx, struct class_list);
                if (!current) {
-                       ldb_oom(module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
-               current->objectclass = dsdb_class_by_lDAPDisplayName(schema, (const char *)objectclass_element->values[i].data);
+               current->objectclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &objectclass_element->values[i]);
                if (!current->objectclass) {
-                       ldb_asprintf_errstring(module->ldb, "objectclass %s is not a valid objectClass in schema", (const char *)objectclass_element->values[i].data);
-                       return LDB_ERR_OBJECT_CLASS_VIOLATION;
+                       ldb_asprintf_errstring(ldb, "objectclass %.*s is not a valid objectClass in schema", 
+                                              (int)objectclass_element->values[i].length, (const char *)objectclass_element->values[i].data);
+                       /* This looks weird, but windows apparently returns this for invalid objectClass values */
+                       return LDB_ERR_NO_SUCH_ATTRIBUTE;
+               } else if (current->objectclass->isDefunct) {
+                       ldb_asprintf_errstring(ldb, "objectclass %.*s marked as isDefunct objectClass in schema - not valid for new objects", 
+                                              (int)objectclass_element->values[i].length, (const char *)objectclass_element->values[i].data);
+                       /* This looks weird, but windows apparently returns this for invalid objectClass values */
+                       return LDB_ERR_NO_SUCH_ATTRIBUTE;
                }
 
                /* this is the root of the tree.  We will start
@@ -222,58 +234,18 @@ static int objectclass_sort(struct ldb_module *module,
         * was no 'top', a conflict in the objectClasses or some other
         * schema error?
         */
-       ldb_asprintf_errstring(module->ldb, "objectclass %s is not a valid objectClass in objectClass chain", unsorted->objectclass->lDAPDisplayName);
+       ldb_asprintf_errstring(ldb, "objectclass %s is not a valid objectClass in objectClass chain", unsorted->objectclass->lDAPDisplayName);
        return LDB_ERR_OBJECT_CLASS_VIOLATION;
 }
 
-static DATA_BLOB *get_sd(struct ldb_module *module, TALLOC_CTX *mem_ctx, 
-                        const struct dsdb_class *objectclass) 
-{
-       enum ndr_err_code ndr_err;
-       DATA_BLOB *linear_sd;
-       struct auth_session_info *session_info
-               = ldb_get_opaque(module->ldb, "sessionInfo");
-       struct security_descriptor *sd;
-       const struct dom_sid *domain_sid = samdb_domain_sid(module->ldb);
-
-       if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
-               return NULL;
-       }
-       
-       sd = sddl_decode(mem_ctx, 
-                        objectclass->defaultSecurityDescriptor,
-                        domain_sid);
-
-       if (!sd || !session_info || !session_info->security_token) {
-               return NULL;
-       }
-       
-       sd->owner_sid = session_info->security_token->user_sid;
-       sd->group_sid = session_info->security_token->group_sid;
-       
-       linear_sd = talloc(mem_ctx, DATA_BLOB);
-       if (!linear_sd) {
-               return NULL;
-       }
-
-       ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx, 
-                                       lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
-                                      sd,
-                                      (ndr_push_flags_fn_t)ndr_push_security_descriptor);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               return NULL;
-       }
-       
-       return linear_sd;
-
-}
-
 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
+       struct ldb_context *ldb;
        struct oc_context *ac;
        int ret;
 
        ac = talloc_get_type(req->context, struct oc_context);
+       ldb = ldb_module_get_ctx(ac->module);
 
        if (!ares) {
                return ldb_module_done(ac->req, NULL, NULL,
@@ -285,10 +257,12 @@ static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
                                        ares->response, ares->error);
        }
 
+       ldb_reset_err_string(ldb);
+
        switch (ares->type) {
        case LDB_REPLY_ENTRY:
                if (ac->search_res != NULL) {
-                       ldb_set_errstring(ac->module->ldb, "Too many results");
+                       ldb_set_errstring(ldb, "Too many results");
                        talloc_free(ares);
                        return ldb_module_done(ac->req, NULL, NULL,
                                                LDB_ERR_OPERATIONS_ERROR);
@@ -356,6 +330,8 @@ static int fix_dn(TALLOC_CTX *mem_ctx,
                  struct ldb_dn **fixed_dn) 
 {
        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 */
        *fixed_dn = ldb_dn_copy(mem_ctx, parent_dn);
 
@@ -365,21 +341,37 @@ static int fix_dn(TALLOC_CTX *mem_ctx,
        if (!upper_rdn_attr) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
-                                              
+
        /* Create a new child */
        if (ldb_dn_add_child_fmt(*fixed_dn, "X=X") == false) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
+
+       rdn_val = ldb_dn_get_rdn_val(newdn);
+
+#if 0
+       /* the rules for rDN length constraints are more complex than
+       this. Until we understand them we need to leave this
+       constraint out. Otherwise we break replication, as windows
+       does sometimes send us rDNs longer than 64 */
+       if (!rdn_val || rdn_val->length > 64) {
+               DEBUG(2,(__location__ ": WARNING: rDN longer than 64 limit for '%s'\n", ldb_dn_get_linearized(newdn)));
+       }
+#endif
+
+
        /* And replace it with CN=foo (we need the attribute in upper case */
-       return ldb_dn_set_component(*fixed_dn, 0, upper_rdn_attr,
-                                   *ldb_dn_get_rdn_val(newdn));
+       return ldb_dn_set_component(*fixed_dn, 0, upper_rdn_attr, *rdn_val);
 }
 
 /* Fix all attribute names to be in the correct case, and check they are all valid per the schema */
-static int fix_attributes(struct ldb_context *ldb, const struct dsdb_schema *schema, struct ldb_message *msg) 
+static int fix_check_attributes(struct ldb_context *ldb,
+                               const struct dsdb_schema *schema,
+                               struct ldb_message *msg,
+                               enum ldb_request_type op)
 {
-       int i;
+       unsigned int i;
        for (i=0; i < msg->num_elements; i++) {
                const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema, msg->elements[i].name);
                /* Add in a very special case for 'clearTextPassword',
@@ -388,10 +380,21 @@ static int fix_attributes(struct ldb_context *ldb, const struct dsdb_schema *sch
                if (!attribute) {
                        if (strcasecmp(msg->elements[i].name, "clearTextPassword") != 0) {
                                ldb_asprintf_errstring(ldb, "attribute %s is not a valid attribute in schema", msg->elements[i].name);
-                               return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
+                               /* Apparently Windows sends exactly this behaviour */
+                               return LDB_ERR_NO_SUCH_ATTRIBUTE;
                        }
                } else {
                        msg->elements[i].name = attribute->lDAPDisplayName;
+
+                       /* We have to deny write operations on constructed attributes */
+                       if ((attribute->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED) != 0) {
+                               if (op == LDB_ADD) {
+                                       return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
+                               } else {
+                                       return LDB_ERR_CONSTRAINT_VIOLATION;
+                               }
+                       }
+
                }
        }
 
@@ -402,22 +405,26 @@ static int objectclass_do_add(struct oc_context *ac);
 
 static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
 {
+       struct ldb_context *ldb;
        struct ldb_request *search_req;
        struct oc_context *ac;
        struct ldb_dn *parent_dn;
        int ret;
-       
-       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
+       static const char * const parent_attrs[] = { "objectGUID", "objectClass", NULL };
+
+       ldb = ldb_module_get_ctx(module);
+
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
 
        /* do not manipulate our control entries */
        if (ldb_dn_is_special(req->op.add.message->dn)) {
                return ldb_next_request(module, req);
        }
 
-       /* Need to object to this, but cn=rootdse doesn't have an objectClass... */
+       /* the objectClass must be specified on add */
        if (ldb_msg_find_element(req->op.add.message, 
                                 "objectClass") == NULL) {
-               return ldb_next_request(module, req);
+               return LDB_ERR_OBJECT_CLASS_VIOLATION;
        }
 
        ac = oc_init_context(module, req);
@@ -433,13 +440,13 @@ 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) {
-               ldb_oom(module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       ret = ldb_build_search_req(&search_req, module->ldb,
+       ret = ldb_build_search_req(&search_req, ldb,
                                   ac, parent_dn, LDB_SCOPE_BASE,
-                                  "(objectClass=*)", NULL,
+                                  "(objectClass=*)", parent_attrs,
                                   NULL,
                                   ac, get_search_callback,
                                   req);
@@ -455,6 +462,7 @@ static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
 
 static int objectclass_do_add(struct oc_context *ac)
 {
+       struct ldb_context *ldb;
        const struct dsdb_schema *schema;
        struct ldb_request *add_req;
        char *value;
@@ -464,7 +472,8 @@ static int objectclass_do_add(struct oc_context *ac)
        struct class_list *sorted, *current;
        int ret;
 
-       schema = dsdb_get_schema(ac->module->ldb);
+       ldb = ldb_module_get_ctx(ac->module);
+       schema = dsdb_get_schema(ldb);
 
        mem_ctx = talloc_new(ac);
        if (mem_ctx == NULL) {
@@ -475,20 +484,20 @@ static int objectclass_do_add(struct oc_context *ac)
 
        /* Check we have a valid parent */
        if (ac->search_res == NULL) {
-               if (ldb_dn_compare(ldb_get_root_basedn(ac->module->ldb),
+               if (ldb_dn_compare(ldb_get_root_basedn(ldb),
                                                                msg->dn) == 0) {
                        /* Allow the tree to be started */
                        
                        /* but don't keep any error string, it's meaningless */
-                       ldb_set_errstring(ac->module->ldb, NULL);
+                       ldb_set_errstring(ldb, NULL);
                } else {
-                       ldb_asprintf_errstring(ac->module->ldb, "objectclass: Cannot add %s, parent does not exist!", 
+                       ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not exist!", 
                                               ldb_dn_get_linearized(msg->dn));
                        talloc_free(mem_ctx);
-                       return LDB_ERR_UNWILLING_TO_PERFORM;
+                       return LDB_ERR_NO_SUCH_OBJECT;
                }
        } else {
-               
+
                /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
                ret = fix_dn(msg, 
                             ac->req->op.add.message->dn,
@@ -496,20 +505,15 @@ static int objectclass_do_add(struct oc_context *ac)
                             &msg->dn);
 
                if (ret != LDB_SUCCESS) {
-                       ldb_asprintf_errstring(ac->module->ldb, "Could not munge DN %s into normal form", 
+                       ldb_asprintf_errstring(ldb, "Could not munge DN %s into normal form", 
                                               ldb_dn_get_linearized(ac->req->op.add.message->dn));
                        talloc_free(mem_ctx);
                        return ret;
                }
 
-               /* TODO: Check this is a valid child to this parent,
-                * by reading the allowedChildClasses and
-                * allowedChildClasssesEffective attributes */
-
        }
-
        if (schema) {
-               ret = fix_attributes(ac->module->ldb, schema, msg);
+               ret = fix_check_attributes(ldb, schema, msg, ac->req->operation);
                if (ret != LDB_SUCCESS) {
                        talloc_free(mem_ctx);
                        return ret;
@@ -517,7 +521,7 @@ static int objectclass_do_add(struct oc_context *ac)
 
                /* This is now the objectClass list from the database */
                objectclass_element = ldb_msg_find_element(msg, "objectClass");
-               
+
                if (!objectclass_element) {
                        /* Where did it go?  bail now... */
                        talloc_free(mem_ctx);
@@ -536,21 +540,21 @@ static int objectclass_do_add(struct oc_context *ac)
                        talloc_free(mem_ctx);
                        return ret;
                }
-               
+
                /* We must completely replace the existing objectClass entry,
                 * because we need it sorted */
-               
+
                /* 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) {
-                               ldb_oom(ac->module->ldb);
+                               ldb_oom(ldb);
                                talloc_free(mem_ctx);
                                return LDB_ERR_OPERATIONS_ERROR;
                        }
                        ret = ldb_msg_add_string(msg, "objectClass", value);
                        if (ret != LDB_SUCCESS) {
-                               ldb_set_errstring(ac->module->ldb, 
+                               ldb_set_errstring(ldb,
                                                  "objectclass: could not re-add sorted "
                                                  "objectclass to modify msg");
                                talloc_free(mem_ctx);
@@ -560,25 +564,80 @@ static int objectclass_do_add(struct oc_context *ac)
                        if (!current->next) {
                                struct ldb_message_element *el;
                                int32_t systemFlags = 0;
+                               const char *rdn_name = ldb_dn_get_rdn_name(msg->dn);
+                               if (current->objectclass->rDNAttID
+                                   && ldb_attr_cmp(rdn_name, current->objectclass->rDNAttID) != 0) {
+                                       ldb_asprintf_errstring(ldb,
+                                                              "RDN %s is not correct for most specific structural objectclass %s, should be %s",
+                                                              rdn_name, current->objectclass->lDAPDisplayName, current->objectclass->rDNAttID);
+                                       return LDB_ERR_NAMING_VIOLATION;
+                               }
+
+                               if (ac->search_res && ac->search_res->message) {
+                                       struct ldb_message_element *oc_el
+                                               = ldb_msg_find_element(ac->search_res->message, "objectClass");
+
+                                       bool allowed_class = false;
+                                       int i, j;
+                                       for (i=0; allowed_class == false && oc_el && i < oc_el->num_values; i++) {
+                                               const struct dsdb_class *sclass;
+
+                                               sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
+                                               if (!sclass) {
+                                                       /* We don't know this class?  what is going on? */
+                                                       continue;
+                                               }
+                                               if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
+                                                       for (j=0; sclass->systemPossibleInferiors && sclass->systemPossibleInferiors[j]; j++) {
+                                                               if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, sclass->systemPossibleInferiors[j]) == 0) {
+                                                                       allowed_class = true;
+                                                                       break;
+                                                               }
+                                                       }
+                                               } else {
+                                                       for (j=0; sclass->systemPossibleInferiors && sclass->systemPossibleInferiors[j]; j++) {
+                                                               if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, sclass->systemPossibleInferiors[j]) == 0) {
+                                                                       allowed_class = true;
+                                                                       break;
+                                                               }
+                                                       }
+                                               }
+                                       }
+
+                                       if (!allowed_class) {
+                                               ldb_asprintf_errstring(ldb, "structural objectClass %s is not a valid child class for %s",
+                                                              current->objectclass->lDAPDisplayName, ldb_dn_get_linearized(ac->search_res->message->dn));
+                                               return LDB_ERR_NAMING_VIOLATION;
+                                       }
+                               }
+
+                               if (current->objectclass->systemOnly && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
+                                       ldb_asprintf_errstring(ldb, "objectClass %s is systemOnly, rejecting creation of %s",
+                                                              current->objectclass->lDAPDisplayName, ldb_dn_get_linearized(msg->dn));
+                                       return LDB_ERR_UNWILLING_TO_PERFORM;
+                               }
+
                                if (!ldb_msg_find_element(msg, "objectCategory")) {
-                                       value = talloc_strdup(msg, current->objectclass->defaultObjectCategory);
+                                       struct dsdb_extended_dn_store_format *dn_format = talloc_get_type(ldb_module_get_private(ac->module), struct dsdb_extended_dn_store_format);
+                                       if (dn_format && dn_format->store_extended_dn_in_ldb == false) {
+                                               /* Strip off extended components */
+                                               struct ldb_dn *dn = ldb_dn_new(msg, ldb, current->objectclass->defaultObjectCategory);
+                                               value = ldb_dn_alloc_linearized(msg, dn);
+                                               talloc_free(dn);
+                                       } else {
+                                               value = talloc_strdup(msg, current->objectclass->defaultObjectCategory);
+                                       }
                                        if (value == NULL) {
-                                               ldb_oom(ac->module->ldb);
+                                               ldb_oom(ldb);
                                                talloc_free(mem_ctx);
                                                return LDB_ERR_OPERATIONS_ERROR;
                                        }
                                        ldb_msg_add_string(msg, "objectCategory", value);
                                }
                                if (!ldb_msg_find_element(msg, "showInAdvancedViewOnly") && (current->objectclass->defaultHidingValue == true)) {
-                                       ldb_msg_add_string(msg, "showInAdvancedViewOnly", 
+                                       ldb_msg_add_string(msg, "showInAdvancedViewOnly",
                                                           "TRUE");
                                }
-                               if (!ldb_msg_find_element(msg, "nTSecurityDescriptor")) {
-                                       DATA_BLOB *sd = get_sd(ac->module, mem_ctx, current->objectclass);
-                                       if (sd) {
-                                               ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
-                                       }
-                               }
 
                                /* There are very special rules for systemFlags, see MS-ADTS 3.1.1.5.2.4 */
                                el = ldb_msg_find_element(msg, "systemFlags");
@@ -590,7 +649,7 @@ static int objectclass_do_add(struct oc_context *ac)
                                        /* systemFlags &= ( SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_MOVE | SYSTEM_FLAG_CONFIG_LIMITED_MOVE); */
                                        ldb_msg_remove_element(msg, el);
                                }
-                               
+
                                /* This flag is only allowed on attributeSchema objects */
                                if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "attributeSchema") == 0) {
                                        systemFlags &= ~SYSTEM_FLAG_ATTR_IS_RDN;
@@ -603,7 +662,7 @@ static int objectclass_do_add(struct oc_context *ac)
                                           || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "ntDSDSA") == 0) {
                                        systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
 
-                               } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLink") == 0 
+                               } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLink") == 0
                                           || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLinkBridge") == 0
                                           || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "nTDSConnection") == 0) {
                                        systemFlags |= (int32_t)(SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
@@ -612,21 +671,21 @@ static int objectclass_do_add(struct oc_context *ac)
                                /* TODO: If parent object is site or subnet, also add (SYSTEM_FLAG_CONFIG_ALLOW_RENAME) */
 
                                if (el || systemFlags != 0) {
-                                       samdb_msg_add_int(ac->module->ldb, msg, msg, "systemFlags", systemFlags);
+                                       samdb_msg_add_int(ldb, msg, msg, "systemFlags", systemFlags);
                                }
                        }
                }
        }
 
        talloc_free(mem_ctx);
-       ret = ldb_msg_sanity_check(ac->module->ldb, msg);
+       ret = ldb_msg_sanity_check(ldb, msg);
 
 
        if (ret != LDB_SUCCESS) {
                return ret;
        }
 
-       ret = ldb_build_add_req(&add_req, ac->module->ldb, ac,
+       ret = ldb_build_add_req(&add_req, ldb, ac,
                                msg,
                                ac->req->controls,
                                ac, oc_op_callback,
@@ -645,9 +704,10 @@ static int objectclass_do_mod(struct oc_context *ac);
 
 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
 {
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
        struct ldb_message_element *objectclass_element;
        struct ldb_message *msg;
-       const struct dsdb_schema *schema = dsdb_get_schema(module->ldb);
+       const struct dsdb_schema *schema = dsdb_get_schema(ldb);
        struct class_list *sorted, *current;
        struct ldb_request *down_req;
        struct oc_context *ac;
@@ -655,7 +715,7 @@ static int objectclass_modify(struct ldb_module *module, struct ldb_request *req
        char *value;
        int ret;
 
-       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
 
        /* do not manipulate our control entries */
        if (ldb_dn_is_special(req->op.mod.message->dn)) {
@@ -666,7 +726,13 @@ static int objectclass_modify(struct ldb_module *module, struct ldb_request *req
        if (!schema) {
                return ldb_next_request(module, req);
        }
-       objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
+
+       /* As with the "real" AD we don't accept empty messages */
+       if (req->op.mod.message->num_elements == 0) {
+               ldb_set_errstring(ldb, "objectclass: modify message must have "
+                                      "elements/attributes!");
+               return LDB_ERR_UNWILLING_TO_PERFORM;
+       }
 
        ac = oc_init_context(module, req);
        if (ac == NULL) {
@@ -675,6 +741,7 @@ static int objectclass_modify(struct ldb_module *module, struct ldb_request *req
 
        /* If no part of this touches the objectClass, then we don't
         * need to make any changes.  */
+       objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
 
        /* If the only operation is the deletion of the objectClass
         * then go on with just fixing the attribute case */
@@ -684,12 +751,12 @@ static int objectclass_modify(struct ldb_module *module, struct ldb_request *req
                        return LDB_ERR_OPERATIONS_ERROR;
                }
                
-               ret = fix_attributes(module->ldb, schema, msg);
+               ret = fix_check_attributes(ldb, schema, msg, req->operation);
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
 
-               ret = ldb_build_mod_req(&down_req, module->ldb, ac,
+               ret = ldb_build_mod_req(&down_req, ldb, ac,
                                        msg,
                                        req->controls,
                                        ac, oc_op_callback,
@@ -721,7 +788,7 @@ static int objectclass_modify(struct ldb_module *module, struct ldb_request *req
                        return LDB_ERR_OPERATIONS_ERROR;
                }
 
-               ret = fix_attributes(module->ldb, schema, msg);
+               ret = fix_check_attributes(ldb, schema, msg, req->operation);
                if (ret != LDB_SUCCESS) {
                        talloc_free(mem_ctx);
                        return ret;
@@ -752,13 +819,13 @@ static int objectclass_modify(struct ldb_module *module, struct ldb_request *req
                        value = talloc_strdup(msg,
                                        current->objectclass->lDAPDisplayName);
                        if (value == NULL) {
-                               ldb_oom(module->ldb);
+                               ldb_oom(ldb);
                                talloc_free(mem_ctx);
                                return LDB_ERR_OPERATIONS_ERROR;
                        }
                        ret = ldb_msg_add_string(msg, "objectClass", value);
                        if (ret != LDB_SUCCESS) {
-                               ldb_set_errstring(module->ldb,
+                               ldb_set_errstring(ldb,
                                        "objectclass: could not re-add sorted "
                                        "objectclass to modify msg");
                                talloc_free(mem_ctx);
@@ -768,12 +835,12 @@ static int objectclass_modify(struct ldb_module *module, struct ldb_request *req
                
                talloc_free(mem_ctx);
 
-               ret = ldb_msg_sanity_check(module->ldb, msg);
+               ret = ldb_msg_sanity_check(ldb, msg);
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
 
-               ret = ldb_build_mod_req(&down_req, module->ldb, ac,
+               ret = ldb_build_mod_req(&down_req, ldb, ac,
                                        msg,
                                        req->controls,
                                        ac, oc_op_callback,
@@ -793,17 +860,17 @@ 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) {
-               ldb_oom(module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       ret = fix_attributes(module->ldb, schema, msg);
+       ret = fix_check_attributes(ldb, schema, msg, req->operation);
        if (ret != LDB_SUCCESS) {
-               ldb_oom(ac->module->ldb);
+               ldb_oom(ldb);
                return ret;
        }
 
-       ret = ldb_build_mod_req(&down_req, module->ldb, ac,
+       ret = ldb_build_mod_req(&down_req, ldb, ac,
                                msg,
                                req->controls,
                                ac, oc_modify_callback,
@@ -817,12 +884,14 @@ static int objectclass_modify(struct ldb_module *module, struct ldb_request *req
 
 static int oc_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
+       struct ldb_context *ldb;
        static const char * const attrs[] = { "objectClass", NULL };
        struct ldb_request *search_req;
        struct oc_context *ac;
        int ret;
 
        ac = talloc_get_type(req->context, struct oc_context);
+       ldb = ldb_module_get_ctx(ac->module);
 
        if (!ares) {
                return ldb_module_done(ac->req, NULL, NULL,
@@ -839,7 +908,9 @@ static int oc_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
                                        LDB_ERR_OPERATIONS_ERROR);
        }
 
-       ret = ldb_build_search_req(&search_req, ac->module->ldb, ac,
+       talloc_free(ares);
+
+       ret = ldb_build_search_req(&search_req, ldb, ac,
                                   ac->req->op.mod.message->dn, LDB_SCOPE_BASE,
                                   "(objectClass=*)",
                                   attrs, NULL, 
@@ -860,7 +931,7 @@ static int oc_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
 
 static int objectclass_do_mod(struct oc_context *ac)
 {
-
+       struct ldb_context *ldb;
        const struct dsdb_schema *schema;
        struct ldb_request *mod_req;
        char *value;
@@ -870,10 +941,12 @@ static int objectclass_do_mod(struct oc_context *ac)
        struct class_list *sorted, *current;
        int ret;
 
+       ldb = ldb_module_get_ctx(ac->module);
+
        if (ac->search_res == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
-       schema = dsdb_get_schema(ac->module->ldb);
+       schema = dsdb_get_schema(ldb);
 
        mem_ctx = talloc_new(ac);
        if (mem_ctx == NULL) {
@@ -883,7 +956,7 @@ static int objectclass_do_mod(struct oc_context *ac)
        /* use a new message structure */
        msg = ldb_msg_new(ac);
        if (msg == NULL) {
-               ldb_set_errstring(ac->module->ldb,
+               ldb_set_errstring(ldb,
                        "objectclass: could not create new modify msg");
                talloc_free(mem_ctx);
                return LDB_ERR_OPERATIONS_ERROR;
@@ -912,7 +985,7 @@ static int objectclass_do_mod(struct oc_context *ac)
 
        ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
        if (ret != LDB_SUCCESS) {
-               ldb_set_errstring(ac->module->ldb, "objectclass: could not clear objectclass in modify msg");
+               ldb_set_errstring(ldb, "objectclass: could not clear objectclass in modify msg");
                talloc_free(mem_ctx);
                return ret;
        }
@@ -921,24 +994,24 @@ static int objectclass_do_mod(struct oc_context *ac)
        for (current = sorted; current; current = current->next) {
                value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
                if (value == NULL) {
-                       ldb_oom(ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
                ret = ldb_msg_add_string(msg, "objectClass", value);
                if (ret != LDB_SUCCESS) {
-                       ldb_set_errstring(ac->module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
+                       ldb_set_errstring(ldb, "objectclass: could not re-add sorted objectclass to modify msg");
                        talloc_free(mem_ctx);
                        return ret;
                }
        }
 
-       ret = ldb_msg_sanity_check(ac->module->ldb, msg);
+       ret = ldb_msg_sanity_check(ldb, msg);
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
                return ret;
        }
 
-       ret = ldb_build_mod_req(&mod_req, ac->module->ldb, ac,
+       ret = ldb_build_mod_req(&mod_req, ldb, ac,
                                msg,
                                ac->req->controls,
                                ac, oc_op_callback,
@@ -958,13 +1031,15 @@ static int objectclass_do_rename(struct oc_context *ac);
 static int objectclass_rename(struct ldb_module *module, struct ldb_request *req)
 {
        static const char * const attrs[] = { NULL };
-
+       struct ldb_context *ldb;
        struct ldb_request *search_req;
        struct oc_context *ac;
        struct ldb_dn *parent_dn;
        int ret;
 
-       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_rename\n");
+       ldb = ldb_module_get_ctx(module);
+
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_rename\n");
 
        if (ldb_dn_is_special(req->op.rename.newdn)) { /* do not manipulate our control entries */
                return ldb_next_request(module, req);
@@ -973,7 +1048,7 @@ static int objectclass_rename(struct ldb_module *module, struct ldb_request *req
        /* Firstly ensure we are not trying to rename it to be a child of itself */
        if ((ldb_dn_compare_base(req->op.rename.olddn, req->op.rename.newdn) == 0) 
            && (ldb_dn_compare(req->op.rename.olddn, req->op.rename.newdn) != 0)) {
-               ldb_asprintf_errstring(module->ldb, "Cannot rename %s to be a child of itself",
+               ldb_asprintf_errstring(ldb, "Cannot rename %s to be a child of itself",
                                       ldb_dn_get_linearized(req->op.rename.olddn));
                return LDB_ERR_UNWILLING_TO_PERFORM;
        }
@@ -985,33 +1060,52 @@ static int objectclass_rename(struct ldb_module *module, struct ldb_request *req
 
        parent_dn = ldb_dn_get_parent(ac, req->op.rename.newdn);
        if (parent_dn == NULL) {
-               ldb_oom(module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
-       ret = ldb_build_search_req(&search_req, module->ldb,
+
+       /*
+         it makes a search request, looking for the parent DN to fix up the new DN
+         to a standard one, at objectclass_do_rename()
+        */
+       ret = ldb_build_search_req(&search_req, ldb,
                                   ac, parent_dn, LDB_SCOPE_BASE,
                                   "(objectClass=*)",
-                                  attrs, NULL, 
+                                  attrs, NULL,
                                   ac, get_search_callback,
                                   req);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
 
+       /* we have to add the show deleted control, as otherwise DRS
+          deletes will be refused as we will think the target parent
+          does not exist */
+       ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID, false, NULL);
+
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+
        ac->step_fn = objectclass_do_rename;
 
        return ldb_next_request(ac->module, search_req);
+
+
 }
 
 static int objectclass_do_rename(struct oc_context *ac)
 {
+       struct ldb_context *ldb;
        struct ldb_request *rename_req;
        struct ldb_dn *fixed_dn;
        int ret;
 
+       ldb = ldb_module_get_ctx(ac->module);
+
        /* Check we have a valid parent */
        if (ac->search_res == NULL) {
-               ldb_asprintf_errstring(ac->module->ldb, "objectclass: Cannot rename %s, parent does not exist!", 
+               ldb_asprintf_errstring(ldb, "objectclass: Cannot rename %s, parent does not exist!", 
                                       ldb_dn_get_linearized(ac->req->op.rename.newdn));
                return LDB_ERR_UNWILLING_TO_PERFORM;
        }
@@ -1030,7 +1124,7 @@ static int objectclass_do_rename(struct oc_context *ac)
         * by reading the allowedChildClasses and
         * allowedChildClasssesEffective attributes */
 
-       ret = ldb_build_rename_req(&rename_req, ac->module->ldb, ac,
+       ret = ldb_build_rename_req(&rename_req, ldb, ac,
                                   ac->req->op.rename.olddn, fixed_dn,
                                   ac->req->controls,
                                   ac, oc_op_callback,
@@ -1043,9 +1137,26 @@ static int objectclass_do_rename(struct oc_context *ac)
        return ldb_next_request(ac->module, rename_req);
 }
 
+static int objectclass_init(struct ldb_module *module)
+{
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       int ret;
+       /* Init everything else */
+       ret = ldb_next_init(module);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       
+       /* Look for the opaque to indicate we might have to cut down the DN of defaultObjectCategory */
+       ldb_module_set_private(module, ldb_get_opaque(ldb, DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME));
+
+       return ldb_next_init(module);
+}
+
 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_module_ops = {
        .name              = "objectclass",
        .add           = objectclass_add,
        .modify        = objectclass_modify,
        .rename        = objectclass_rename,
+       .init_context  = objectclass_init
 };