s4-dsdb: pass parent request to dsdb_module_*() functions
[samba.git] / source4 / dsdb / samdb / ldb_modules / descriptor.c
index f9992e3c9ef118929fdd45c6aefcf06fa6f0235e..dfbfdf6dec2605bc28279f0423819db4f3f5e44b 100644 (file)
  */
 
 #include "includes.h"
-#include "ldb_module.h"
-#include "dlinklist.h"
+#include <ldb_module.h>
+#include "util/dlinklist.h"
 #include "dsdb/samdb/samdb.h"
 #include "librpc/ndr/libndr.h"
 #include "librpc/gen_ndr/ndr_security.h"
 #include "libcli/security/security.h"
+#include "dsdb/samdb/ldb_modules/schema.h"
 #include "auth/auth.h"
 #include "param/param.h"
+#include "util.h"
 
 struct descriptor_data {
        int _dummy;
@@ -49,45 +51,36 @@ struct descriptor_data {
 struct descriptor_context {
        struct ldb_module *module;
        struct ldb_request *req;
+       struct ldb_message *msg;
        struct ldb_reply *search_res;
        struct ldb_reply *search_oc_res;
        struct ldb_val *parentsd_val;
+       struct ldb_message_element *sd_element;
        struct ldb_val *sd_val;
        int (*step_fn)(struct descriptor_context *);
 };
 
-static const struct dsdb_class * get_last_structural_class(const struct dsdb_schema *schema, struct ldb_message_element *element)
-{
-       const struct dsdb_class *last_class = NULL;
-       int i;
-       for (i = 0; i < element->num_values; i++){
-               if (!last_class) {
-                       last_class = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &element->values[i]);
-               } else {
-                       const struct dsdb_class *tmp_class = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &element->values[i]);
-                       if (tmp_class->subClass_order > last_class->subClass_order)
-                               last_class = tmp_class;
-               }
-       }
-       return last_class;
-}
-
 struct dom_sid *get_default_ag(TALLOC_CTX *mem_ctx,
                               struct ldb_dn *dn,
                               struct security_token *token,
                               struct ldb_context *ldb)
 {
        TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
-       struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
-       struct ldb_dn *schema_base_dn = ldb_get_schema_basedn(ldb);
-       struct ldb_dn *config_base_dn = ldb_get_config_basedn(ldb);
        const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
        struct dom_sid *da_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_ADMINS);
        struct dom_sid *ea_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_ENTERPRISE_ADMINS);
        struct dom_sid *sa_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_SCHEMA_ADMINS);
        struct dom_sid *dag_sid;
+       struct ldb_dn *nc_root;
+       int ret;
+
+       ret = dsdb_find_nc_root(ldb, tmp_ctx, dn, &nc_root);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return NULL;
+       }
 
-       if (ldb_dn_compare_base(schema_base_dn, dn) == 0){
+       if (ldb_dn_compare(nc_root, ldb_get_schema_basedn(ldb)) == 0) {
                if (security_token_has_sid(token, sa_sid))
                        dag_sid = dom_sid_dup(mem_ctx, sa_sid);
                else if (security_token_has_sid(token, ea_sid))
@@ -96,25 +89,23 @@ struct dom_sid *get_default_ag(TALLOC_CTX *mem_ctx,
                        dag_sid = dom_sid_dup(mem_ctx, da_sid);
                else
                        dag_sid = NULL;
-       }
-       else if (ldb_dn_compare_base(config_base_dn, dn) == 0){
+       } else if (ldb_dn_compare(nc_root, ldb_get_config_basedn(ldb)) == 0) {
                if (security_token_has_sid(token, ea_sid))
                        dag_sid = dom_sid_dup(mem_ctx, ea_sid);
                else if (security_token_has_sid(token, da_sid))
                        dag_sid = dom_sid_dup(mem_ctx, da_sid);
                else
                        dag_sid = NULL;
-       }
-       else if (ldb_dn_compare_base(root_base_dn, dn) == 0){
+       } else if (ldb_dn_compare(nc_root, ldb_get_default_basedn(ldb)) == 0) {
                if (security_token_has_sid(token, da_sid))
                        dag_sid = dom_sid_dup(mem_ctx, da_sid);
                else if (security_token_has_sid(token, ea_sid))
                                dag_sid = dom_sid_dup(mem_ctx, ea_sid);
                else
                        dag_sid = NULL;
-       }
-       else
+       } else {
                dag_sid = NULL;
+       }
 
        talloc_free(tmp_ctx);
        return dag_sid;
@@ -141,13 +132,7 @@ static struct dom_sid *get_default_group(TALLOC_CTX *mem_ctx,
                                         struct ldb_context *ldb,
                                         struct dom_sid *dag)
 {
-       int *domainFunctionality;
-
-       domainFunctionality = talloc_get_type(
-               ldb_get_opaque(ldb, "domainFunctionality"), int);
-
-       if (*domainFunctionality
-                       && (*domainFunctionality >= DS_DOMAIN_FUNCTION_2008)) {
+       if (dsdb_functional_level(ldb) >= DS_DOMAIN_FUNCTION_2008) {
                return dag;
        }
 
@@ -194,7 +179,7 @@ static struct security_descriptor *descr_handle_sd_flags(TALLOC_CTX *mem_ctx,
                        SEC_DESC_SACL_AUTO_INHERITED|SEC_DESC_SACL_PROTECTED |
                        SEC_DESC_SERVER_SECURITY);
        } 
-       else if (old_sd) {
+       else if (old_sd && old_sd->sacl) {
                final_sd->sacl = security_acl_dup(mem_ctx,old_sd->sacl);
                final_sd->type |= old_sd->type & (SEC_DESC_SACL_PRESENT |
                        SEC_DESC_SACL_DEFAULTED|SEC_DESC_SACL_AUTO_INHERIT_REQ |
@@ -209,7 +194,7 @@ static struct security_descriptor *descr_handle_sd_flags(TALLOC_CTX *mem_ctx,
                        SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_PROTECTED |
                        SEC_DESC_DACL_TRUSTED);
        } 
-       else if (old_sd) {
+       else if (old_sd && old_sd->dacl) {
                final_sd->dacl = security_acl_dup(mem_ctx,old_sd->dacl);
                final_sd->type |= old_sd->type & (SEC_DESC_DACL_PRESENT |
                        SEC_DESC_DACL_DEFAULTED|SEC_DESC_DACL_AUTO_INHERIT_REQ |
@@ -248,7 +233,7 @@ static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
                if (!user_descriptor) {
                        return NULL;
                }
-               ndr_err = ndr_pull_struct_blob(object, user_descriptor, NULL,
+               ndr_err = ndr_pull_struct_blob(object, user_descriptor, 
                                               user_descriptor,
                                               (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
 
@@ -265,7 +250,7 @@ static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
                if (!old_descriptor) {
                        return NULL;
                }
-               ndr_err = ndr_pull_struct_blob(old_sd, old_descriptor, NULL,
+               ndr_err = ndr_pull_struct_blob(old_sd, old_descriptor, 
                                               old_descriptor,
                                               (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
 
@@ -280,7 +265,7 @@ static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
                if (!parent_descriptor) {
                        return NULL;
                }
-               ndr_err = ndr_pull_struct_blob(parent, parent_descriptor, NULL,
+               ndr_err = ndr_pull_struct_blob(parent, parent_descriptor, 
                                               parent_descriptor,
                                               (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
 
@@ -306,6 +291,14 @@ static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
        if (!final_sd) {
                return NULL;
        }
+
+       if (final_sd->dacl) {
+               final_sd->dacl->revision = SECURITY_ACL_REVISION_ADS;
+       }
+       if (final_sd->sacl) {
+               final_sd->sacl->revision = SECURITY_ACL_REVISION_ADS;
+       }
+
        sddl_sd = sddl_encode(mem_ctx, final_sd, domain_sid);
        DEBUG(10, ("Object %s created with desriptor %s\n\n", ldb_dn_get_linearized(dn), sddl_sd));
 
@@ -315,7 +308,6 @@ static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
        }
 
        ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
-                                      lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
                                       final_sd,
                                       (ndr_push_flags_fn_t)ndr_push_security_descriptor);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
@@ -333,13 +325,12 @@ static DATA_BLOB *descr_get_descriptor_to_show(struct ldb_module *module,
        struct security_descriptor *old_sd, *final_sd;
        DATA_BLOB *linear_sd;
        enum ndr_err_code ndr_err;
-       struct ldb_context *ldb = ldb_module_get_ctx(module);
 
        old_sd = talloc(mem_ctx, struct security_descriptor);
        if (!old_sd) {
                return NULL;
        }
-       ndr_err = ndr_pull_struct_blob(sd, old_sd, NULL,
+       ndr_err = ndr_pull_struct_blob(sd, old_sd, 
                                       old_sd,
                                       (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
 
@@ -360,7 +351,6 @@ static DATA_BLOB *descr_get_descriptor_to_show(struct ldb_module *module,
        }
 
        ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
-                                      lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
                                       final_sd,
                                       (ndr_push_flags_fn_t)ndr_push_security_descriptor);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
@@ -412,12 +402,12 @@ static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
 
        switch (ares->type) {
        case LDB_REPLY_ENTRY:
-                       if (ac->search_res != NULL) {
+               if (ac->search_res != NULL) {
                        ldb_set_errstring(ldb, "Too many results");
                        talloc_free(ares);
                        return ldb_module_done(ac->req, NULL, NULL,
                                                LDB_ERR_OPERATIONS_ERROR);
-                                               }
+               }
 
                ac->search_res = talloc_steal(ac, ares);
                break;
@@ -462,12 +452,12 @@ static int get_search_oc_callback(struct ldb_request *req, struct ldb_reply *are
 
        switch (ares->type) {
        case LDB_REPLY_ENTRY:
-                       if (ac->search_oc_res != NULL) {
+               if (ac->search_oc_res != NULL) {
                        ldb_set_errstring(ldb, "Too many results");
                        talloc_free(ares);
                        return ldb_module_done(ac->req, NULL, NULL,
                                                LDB_ERR_OPERATIONS_ERROR);
-                                               }
+               }
 
                ac->search_oc_res = talloc_steal(ac, ares);
                break;
@@ -489,32 +479,6 @@ static int get_search_oc_callback(struct ldb_request *req, struct ldb_reply *are
        return LDB_SUCCESS;
 }
 
-
-static int descriptor_op_callback(struct ldb_request *req, struct ldb_reply *ares)
-{
-       struct descriptor_context *ac;
-
-       ac = talloc_get_type(req->context, struct descriptor_context);
-
-       if (!ares) {
-               return ldb_module_done(ac->req, NULL, NULL,
-                                       LDB_ERR_OPERATIONS_ERROR);
-       }
-       if (ares->error != LDB_SUCCESS) {
-               return ldb_module_done(ac->req, ares->controls,
-                                       ares->response, ares->error);
-       }
-
-       if (ares->type != LDB_REPLY_DONE) {
-               talloc_free(ares);
-               return ldb_module_done(ac->req, NULL, NULL,
-                                       LDB_ERR_OPERATIONS_ERROR);
-       }
-
-       return ldb_module_done(ac->req, ares->controls,
-                               ares->response, ares->error);
-}
-
 static int descriptor_search_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
        struct descriptor_context *ac;
@@ -542,6 +506,11 @@ static int descriptor_search_callback(struct ldb_request *req, struct ldb_reply
                sd_flags = sdctr->secinfo_flags;
                /* we only care for the last 4 bits */
                sd_flags = sd_flags & 0x0000000F;
+               if (sd_flags == 0) {
+                       /* MS-ADTS 3.1.1.3.4.1.11 says that no bits
+                          equals all 4 bits */
+                       sd_flags = 0xF;
+               }
        }
 
        switch (ares->type) {
@@ -568,17 +537,15 @@ static int descriptor_search_callback(struct ldb_request *req, struct ldb_reply
                return ldb_module_send_entry(ac->req, ares->message, ares->controls);
 
        case LDB_REPLY_REFERRAL:
-               /* ignore referrals */
-               break;
+               return ldb_module_send_referral(ac->req, ares->referral);
 
        case LDB_REPLY_DONE:
                return ldb_module_done(ac->req, ares->controls,
                                        ares->response, ares->error);
        }
 
-       talloc_free(ares);
-       return LDB_SUCCESS;
 fail:
+       talloc_free(ares);
        return ldb_module_done(ac->req, NULL, NULL, ret);
 }
 
@@ -587,30 +554,32 @@ static int descriptor_do_mod(struct descriptor_context *ac)
        struct ldb_context *ldb;
        const struct dsdb_schema *schema;
        struct ldb_request *mod_req;
-       struct ldb_message_element *objectclass_element, *tmp_element, *oldsd_el;
+       struct ldb_message_element *objectclass_element, *oldsd_el;
        struct ldb_val *oldsd_val = NULL;
        int ret;
        DATA_BLOB *sd;
        const struct dsdb_class *objectclass;
-       struct ldb_message *msg;
        struct ldb_control *sd_control;
-       struct ldb_control **saved_controls;
-       int flags = 0;
+       struct ldb_control *sd_control2;
        uint32_t sd_flags = 0;
 
        ldb = ldb_module_get_ctx(ac->module);
-       schema = dsdb_get_schema(ldb);
+       schema = dsdb_get_schema(ldb, ac);
 
-       msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
-       objectclass_element = ldb_msg_find_element(ac->search_oc_res->message, "objectClass");
-       objectclass = get_last_structural_class(schema, objectclass_element);
+       objectclass_element = ldb_msg_find_element(ac->search_oc_res->message,
+                                                  "objectClass");
+       if (objectclass_element == NULL) {
+               return ldb_operr(ldb);
+       }
 
-       if (!objectclass) {
-               ldb_asprintf_errstring(ldb, "No last structural objectclass found on %s",
-                                      ldb_dn_get_linearized(ac->search_oc_res->message->dn));
-               return LDB_ERR_OPERATIONS_ERROR;
+       objectclass = get_last_structural_class(schema, objectclass_element, ac->req);
+       if (objectclass == NULL) {
+               return ldb_operr(ldb);
        }
+
        sd_control = ldb_request_get_control(ac->req, LDB_CONTROL_SD_FLAGS_OID);
+       sd_control2 = ldb_request_get_control(ac->req,
+                                             LDB_CONTROL_RECALCULATE_SD_OID);
        if (sd_control) {
                struct ldb_sd_flags_control *sdctr = (struct ldb_sd_flags_control *)sd_control->data;
                sd_flags = sdctr->secinfo_flags;
@@ -618,43 +587,53 @@ static int descriptor_do_mod(struct descriptor_context *ac)
                sd_flags = sd_flags & 0x0000000F;
        }
        if (sd_flags != 0) {
-               oldsd_el = ldb_msg_find_element(ac->search_oc_res->message, "nTSecurityDescriptor");
+               oldsd_el = ldb_msg_find_element(ac->search_oc_res->message,
+                                               "nTSecurityDescriptor");
                if (oldsd_el) {
                        oldsd_val = oldsd_el->values;
                }
        }
-       sd = get_new_descriptor(ac->module, msg->dn, ac, objectclass,
-                               ac->parentsd_val, ac->sd_val, oldsd_val, sd_flags);
-       if (ac->sd_val) {
-               tmp_element = ldb_msg_find_element(msg, "ntSecurityDescriptor");
-               flags = tmp_element->flags;
-               ldb_msg_remove_attr(msg, "nTSecurityDescriptor");
-       }
 
-       if (sd) {
-               ret = ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
-               if (ret != LDB_SUCCESS) {
-                       return ret;
+       sd = get_new_descriptor(ac->module, ac->msg->dn, ac,
+                               objectclass, ac->parentsd_val,
+                               ac->sd_val, oldsd_val, sd_flags);
+       if (sd != NULL) {
+               if (ac->sd_val != NULL) {
+                       ac->sd_element->values[0] = *sd;
+               } else if (sd_control2 != NULL) {
+                       /* In this branch we really do force the recalculation
+                        * of the SD */
+                       ldb_msg_remove_attr(ac->msg, "nTSecurityDescriptor");
+
+                       ret = ldb_msg_add_steal_value(ac->msg,
+                                                     "nTSecurityDescriptor",
+                                                     sd);
+                       if (ret != LDB_SUCCESS) {
+                               return ret;
+                       }
+                       ac->sd_element = ldb_msg_find_element(ac->msg,
+                                                             "nTSecurityDescriptor");
+                       ac->sd_element->flags = LDB_FLAG_MOD_REPLACE;
                }
-               tmp_element = ldb_msg_find_element(msg, "ntSecurityDescriptor");
-               tmp_element->flags = flags;
        }
+
+       /* mark the controls as non-critical since we've handled them */
+       if (sd_control != NULL) {
+               sd_control->critical = 0;
+       }
+       if (sd_control2 != NULL) {
+               sd_control2->critical = 0;
+       }
+
        ret = ldb_build_mod_req(&mod_req, ldb, ac,
-                               msg,
+                               ac->msg,
                                ac->req->controls,
-                               ac, descriptor_op_callback,
+                               ac->req, dsdb_next_callback,
                                ac->req);
+       LDB_REQ_SET_LOCATION(mod_req);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
-       /* save it locally and remove it from the list */
-       /* we do not need to replace them later as we
-        * are keeping the original req intact */
-       if (sd_control) {
-               if (!save_controls(sd_control, mod_req, &saved_controls)) {
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
-       }
 
        return ldb_next_request(ac->module, mod_req);
 }
@@ -664,9 +643,7 @@ static int descriptor_do_add(struct descriptor_context *ac)
        struct ldb_context *ldb;
        const struct dsdb_schema *schema;
        struct ldb_request *add_req;
-       struct ldb_message_element *objectclass_element, *sd_element = NULL;
-       struct ldb_message *msg;
-       TALLOC_CTX *mem_ctx;
+       struct ldb_message_element *objectclass_element;
        int ret;
        DATA_BLOB *sd;
        const struct dsdb_class *objectclass;
@@ -674,89 +651,110 @@ static int descriptor_do_add(struct descriptor_context *ac)
        struct ldb_request *search_req;
 
        ldb = ldb_module_get_ctx(ac->module);
-       schema = dsdb_get_schema(ldb);
-       mem_ctx = talloc_new(ac);
-       if (mem_ctx == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
+       schema = dsdb_get_schema(ldb, ac);
 
        switch (ac->req->operation) {
        case LDB_ADD:
-               msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
-               objectclass_element = ldb_msg_find_element(msg, "objectClass");
-               objectclass = get_last_structural_class(schema, objectclass_element);
+               ac->msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
+               if (ac->msg == NULL) {
+                       return ldb_module_oom(ac->module);
+               }
 
-               if (!objectclass) {
-                       ldb_asprintf_errstring(ldb, "No last structural objectclass found on %s", ldb_dn_get_linearized(msg->dn));
-                       return LDB_ERR_OPERATIONS_ERROR;
+               objectclass_element = ldb_msg_find_element(ac->msg,
+                                                          "objectClass");
+               if (objectclass_element == NULL) {
+                       return ldb_operr(ldb);
+               }
+
+               objectclass = get_last_structural_class(schema,
+                                                       objectclass_element, ac->req);
+               if (objectclass == NULL) {
+                       return ldb_operr(ldb);
                }
                break;
        case LDB_MODIFY:
-               msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
+               ac->msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
+               if (ac->msg == NULL) {
+                       return ldb_module_oom(ac->module);
+               }
                break;
        default:
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
-
-       /* get the security descriptor values*/
-       sd_element = ldb_msg_find_element(msg, "nTSecurityDescriptor");
-       if (sd_element) {
-               ac->sd_val = talloc_memdup(ac, &sd_element->values[0], sizeof(struct ldb_val));
-       }
-       /* NC's have no parent */
-       if ((ldb_dn_compare(msg->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
-           (ldb_dn_compare(msg->dn, (ldb_get_config_basedn(ldb))) == 0) ||
-           (ldb_dn_compare(msg->dn, (ldb_get_root_basedn(ldb))) == 0)) {
-               ac->parentsd_val = NULL;
-       } else if (ac->search_res != NULL) {
-               struct ldb_message_element *parent_element = ldb_msg_find_element(ac->search_res->message, "nTSecurityDescriptor");
-               if (parent_element) {
-                       ac->parentsd_val = talloc_memdup(ac, &parent_element->values[0], sizeof(struct ldb_val));
-               }
+       /* Check if there is a valid security descriptor provided */
+       ac->sd_element = dsdb_get_single_valued_attr(ac->msg,
+                                                    "nTSecurityDescriptor",
+                                                    ac->req->operation);
+       if ((ac->sd_element != NULL) && (ac->sd_element->num_values == 1)) {
+               ac->sd_val = talloc_memdup(ac,
+                                          &ac->sd_element->values[0],
+                                          sizeof(struct ldb_val));
        }
 
-       if (ac->req->operation == LDB_ADD) {
-       /* get the parent descriptor and the one provided. If not provided, get the default.*/
-       /* convert to security descriptor and calculate */
-               sd = get_new_descriptor(ac->module, msg->dn, mem_ctx, objectclass,
-                                       ac->parentsd_val, ac->sd_val, NULL, 0);
-               if (ac->sd_val) {
-                       ldb_msg_remove_attr(msg, "nTSecurityDescriptor");
+       /* If we do have a parent, then please fetch it's security descriptor.
+        * But have in mind: NCs don't have any parents! That means
+        * "CN=Configuration,DC=example,DC=com" has no parent
+        * "DC=example,DC=com" since this is located under another NC! */
+       if (ac->search_res != NULL) {
+               struct ldb_message_element *parent_element = NULL;
+               struct ldb_dn *nc_root;
+
+               ret = dsdb_find_nc_root(ldb, ac, ac->msg->dn, &nc_root);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
                }
 
-               if (sd) {
-                       ret = ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
-                       if (ret != LDB_SUCCESS) {
-                               return ret;
+               if (ldb_dn_compare(ac->msg->dn, nc_root) != 0) {
+                       /* we aren't any NC */
+                       parent_element = ldb_msg_find_element(ac->search_res->message,
+                                                             "nTSecurityDescriptor");
+                       if (parent_element != NULL) {
+                               ac->parentsd_val = talloc_memdup(ac,
+                                                                &parent_element->values[0],
+                                                                sizeof(struct ldb_val));
                        }
                }
+       }
 
-               talloc_free(mem_ctx);
-               ret = ldb_msg_sanity_check(ldb, msg);
-
-               if (ret != LDB_SUCCESS) {
-                       ldb_asprintf_errstring(ldb, "No last structural objectclass found on %s",
-                                              ldb_dn_get_linearized(msg->dn));
-                       return ret;
+       if (ac->req->operation == LDB_ADD) {
+               /* Get the parent descriptor and the one provided. If not
+                * provided, get the default. Convert it to a security
+                * descriptor and calculate the permissions. */
+               sd = get_new_descriptor(ac->module, ac->msg->dn, ac,
+                                       objectclass, ac->parentsd_val,
+                                       ac->sd_val, NULL, 0);
+               if (sd != NULL) {
+                       if (ac->sd_val != NULL) {
+                               ac->sd_element->values[0] = *sd;
+                       } else if (ac->sd_element == NULL) {
+                               ret = ldb_msg_add_steal_value(ac->msg,
+                                                             "nTSecurityDescriptor",
+                                                             sd);
+                               if (ret != LDB_SUCCESS) {
+                                       return ret;
+                               }
+                       }
                }
 
                ret = ldb_build_add_req(&add_req, ldb, ac,
-                                       msg,
+                                       ac->msg,
                                        ac->req->controls,
-                                       ac, descriptor_op_callback,
+                                       ac->req, dsdb_next_callback,
                                        ac->req);
+               LDB_REQ_SET_LOCATION(add_req);
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
                return ldb_next_request(ac->module, add_req);
        } else {
-               ret = ldb_build_search_req(&search_req, ldb,
-                                  ac, msg->dn, LDB_SCOPE_BASE,
-                                  "(objectClass=*)", attrs,
-                                  NULL,
-                                  ac, get_search_oc_callback,
-                                  ac->req);
+               ret = ldb_build_search_req(&search_req, ldb, ac,
+                                          ac->msg->dn, LDB_SCOPE_BASE,
+                                          "(objectClass=*)", attrs,
+                                          NULL,
+                                          ac, get_search_oc_callback,
+                                          ac->req);
+               LDB_REQ_SET_LOCATION(search_req);
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
@@ -768,6 +766,7 @@ static int descriptor_do_add(struct descriptor_context *ac)
 static int descriptor_change(struct ldb_module *module, struct ldb_request *req)
 {
        struct ldb_context *ldb;
+       struct ldb_control *sd_control;
        struct ldb_request *search_req;
        struct descriptor_context *ac;
        struct ldb_dn *parent_dn, *dn;
@@ -783,23 +782,29 @@ static int descriptor_change(struct ldb_module *module, struct ldb_request *req)
                break;
        case LDB_MODIFY:
                dn = req->op.mod.message->dn;
-               sd_element = ldb_msg_find_element(req->op.mod.message, "nTSecurityDescriptor");
-               if (!sd_element) {
+               sd_element = ldb_msg_find_element(req->op.mod.message,
+                                                 "nTSecurityDescriptor");
+               /* This control forces the recalculation of the SD also when
+                * no modification is performed. */
+               sd_control = ldb_request_get_control(req,
+                                                    LDB_CONTROL_RECALCULATE_SD_OID);
+               if (!sd_element && !sd_control) {
                        return ldb_next_request(module, req);
                }
                break;
        default:
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
        ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_change: %s\n", ldb_dn_get_linearized(dn));
 
+       /* do not manipulate our control entries */
        if (ldb_dn_is_special(dn)) {
                return ldb_next_request(module, req);
        }
 
        ac = descriptor_init_context(module, req);
        if (ac == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        /* If there isn't a parent, just go on to the add processing */
@@ -810,8 +815,7 @@ static int descriptor_change(struct ldb_module *module, struct ldb_request *req)
        /* get copy of parent DN */
        parent_dn = ldb_dn_get_parent(ac, dn);
        if (parent_dn == NULL) {
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        ret = ldb_build_search_req(&search_req, ldb,
@@ -820,6 +824,7 @@ static int descriptor_change(struct ldb_module *module, struct ldb_request *req)
                                   NULL,
                                   ac, get_search_callback,
                                   req);
+       LDB_REQ_SET_LOCATION(search_req);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
@@ -835,7 +840,6 @@ static int descriptor_search(struct ldb_module *module, struct ldb_request *req)
        int ret;
        struct ldb_context *ldb;
        struct ldb_control *sd_control;
-       struct ldb_control **saved_controls;
        struct ldb_request *down_req;
        struct descriptor_context *ac;
 
@@ -847,7 +851,7 @@ static int descriptor_search(struct ldb_module *module, struct ldb_request *req)
        ldb = ldb_module_get_ctx(module);
        ac = descriptor_init_context(module, req);
        if (ac == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        ret = ldb_build_search_req_ex(&down_req, ldb, ac,
@@ -858,16 +862,13 @@ static int descriptor_search(struct ldb_module *module, struct ldb_request *req)
                                      req->controls,
                                      ac, descriptor_search_callback,
                                      ac->req);
+       LDB_REQ_SET_LOCATION(down_req);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
-       /* save it locally and remove it from the list */
-       /* we do not need to replace them later as we
-        * are keeping the original req intact */
+       /* mark it as handled */
        if (sd_control) {
-               if (!save_controls(sd_control, down_req, &saved_controls)) {
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
+               sd_control->critical = 0;
        }
 
        return ldb_next_request(ac->module, down_req);
@@ -877,6 +878,12 @@ static int descriptor_rename(struct ldb_module *module, struct ldb_request *req)
 {
        struct ldb_context *ldb = ldb_module_get_ctx(module);
        ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn));
+
+       /* do not manipulate our control entries */
+       if (ldb_dn_is_special(req->op.rename.olddn)) {
+               return ldb_next_request(module, req);
+       }
+
        return ldb_next_request(module, req);
 }
 
@@ -887,13 +894,13 @@ static int descriptor_init(struct ldb_module *module)
        if (ret != LDB_SUCCESS) {
                ldb_debug(ldb, LDB_DEBUG_ERROR,
                        "descriptor: Unable to register control with rootdse!\n");
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
        return ldb_next_init(module);
 }
 
 
-_PUBLIC_ const struct ldb_module_ops ldb_descriptor_module_ops = {
+static const struct ldb_module_ops ldb_descriptor_module_ops = {
        .name          = "descriptor",
        .search        = descriptor_search,
        .add           = descriptor_change,
@@ -902,4 +909,8 @@ _PUBLIC_ const struct ldb_module_ops ldb_descriptor_module_ops = {
        .init_context  = descriptor_init
 };
 
-
+int ldb_descriptor_module_init(const char *version)
+{
+       LDB_MODULE_CHECK_VERSION(version);
+       return ldb_register_module(&ldb_descriptor_module_ops);
+}