dsdb-acl: dsdb_attribute_by_lDAPDisplayName() is needed for all attributes
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / acl.c
index 2f123145dbae22b4c3e1a97010036c4cf0083deb..b8fab552e198d2797c26da032792b74844864de6 100644 (file)
@@ -1,22 +1,22 @@
 /*
-   ldb database library
+  ldb database library
 
-   Copyright (C) Simo Sorce 2006-2008
-   Copyright (C) Nadezhda Ivanova 2009
-   Copyright (C) Anatoliy Atanasov  2009
+  Copyright (C) Simo Sorce 2006-2008
+  Copyright (C) Nadezhda Ivanova 2009
+  Copyright (C) Anatoliy Atanasov  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
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
+  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
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
 #include "ldb_module.h"
 #include "auth/auth.h"
 #include "libcli/security/security.h"
-#include "librpc/gen_ndr/ndr_security.h"
 #include "dsdb/samdb/samdb.h"
 #include "librpc/gen_ndr/ndr_security.h"
 #include "param/param.h"
-
-/* acl_search helper */
-struct acl_context {
-
-       struct ldb_module *module;
-       struct ldb_request *req;
-       struct ldb_request *down_req;
-
-       /*needed if we have to identify if this is SYSTEM_USER*/
-       enum security_user_level user_type;
-
-       uint32_t access_needed;
-       struct ldb_dn * dn_to_check;
-
-       /* set to true when we need to process the request as a SYSTEM_USER, regardless
-        * of the user's actual rights - for example when we need to retrieve the
-        * ntSecurityDescriptor */
-       bool ignore_security;
-       struct security_token *token;
-       /*needed to identify if we have requested these attributes*/
-       bool nTSecurityDescriptor;
-       bool objectClass;
-       int sec_result;
-};
+#include "dsdb/samdb/ldb_modules/util.h"
+#include "lib/util/tsort.h"
+#include "system/kerberos.h"
+#include "auth/kerberos/kerberos.h"
 
 struct extended_access_check_attribute {
        const char *oa_name;
        const uint32_t requires_rights;
 };
 
-struct acl_private{
-       bool perform_check;
+struct acl_private {
+       bool acl_search;
+       const char **password_attrs;
+       void *cached_schema_ptr;
+       uint64_t cached_schema_metadata_usn;
+       uint64_t cached_schema_loaded_usn;
+       const char **confidential_attrs;
 };
 
-static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares);
-
-/*FIXME: Perhaps this should go in the .idl file*/
-#define SEC_GENERIC_ACCESS_NEVER_GRANTED ( 0xFFFFFFFF )
-
-/*Contains a part of the attributes - the ones that have predefined required rights*/
-static const struct extended_access_check_attribute extended_access_checks_table[] =
-{
-       {
-               .oa_name = "nTSecurityDescriptor",
-               .requires_rights = SEC_FLAG_SYSTEM_SECURITY & SEC_STD_READ_CONTROL,
-       },
-       {
-                .oa_name = "pekList",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-        },
-       {
-                .oa_name = "currentValue",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "dBCSPwd",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "unicodePwd",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "ntPwdHistory",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "priorValue",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "supplementalCredentials",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "trustAuthIncoming",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "trustAuthOutgoing",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "ImPwdHistory",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "initialAuthIncoming",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "initialAuthOutgoing",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
-       {
-                .oa_name = "msDS-ExecuteScriptPassword",
-                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
-       },
+struct acl_context {
+       struct ldb_module *module;
+       struct ldb_request *req;
+       bool am_system;
+       bool am_administrator;
+       bool modify_search;
+       bool constructed_attrs;
+       bool allowedAttributes;
+       bool allowedAttributesEffective;
+       bool allowedChildClasses;
+       bool allowedChildClassesEffective;
+       bool sDRightsEffective;
+       bool userPassword;
+       const char * const *attrs;
+       struct dsdb_schema *schema;
 };
 
-static NTSTATUS extended_access_check(const char *attribute_name, const int access_rights, uint32_t searchFlags)
+static int acl_module_init(struct ldb_module *module)
 {
-       int i = 0;
-       if (access_rights == SEC_GENERIC_ACCESS_NEVER_GRANTED) {
-               return NT_STATUS_ACCESS_DENIED;
-       }
-
-       /*Check if the attribute is in the table first*/
-       for ( i = 0; extended_access_checks_table[i].oa_name; i++ ) {
-               if (ldb_attr_cmp(extended_access_checks_table[i].oa_name, attribute_name) == 0) {
-                       if ((access_rights & extended_access_checks_table[i].requires_rights) == access_rights) {
-                               return NT_STATUS_OK;
-                       } else {
-                               return NT_STATUS_ACCESS_DENIED;
-                       }
-               }
-       }
+       struct ldb_context *ldb;
+       struct acl_private *data;
+       int ret;
+       unsigned int i, n, j;
+       TALLOC_CTX *mem_ctx;
+       static const char * const attrs[] = { "passwordAttribute", NULL };
+       static const char * const secret_attrs[] = {
+               DSDB_SECRET_ATTRIBUTES
+       };
+       struct ldb_result *res;
+       struct ldb_message *msg;
+       struct ldb_message_element *password_attributes;
 
-       /*Check for attribute whose attributeSchema has 0x80 set in searchFlags*/
-       if ((searchFlags & SEARCH_FLAG_CONFIDENTIAL) == SEARCH_FLAG_CONFIDENTIAL) {
-               if (((SEC_ADS_READ_PROP & SEC_ADS_CONTROL_ACCESS) & access_rights) == access_rights) {
-                       return NT_STATUS_OK;
-               } else {
-                       return NT_STATUS_ACCESS_DENIED;
-               }
-       }
+       ldb = ldb_module_get_ctx(module);
 
-       /*Check attributes with *special* behaviour*/
-       if (ldb_attr_cmp("msDS-QuotaEffective", attribute_name) == 0 || ldb_attr_cmp("msDS-QuotaUsed", attribute_name) == 0){
-               /*Rights required:
-                *
-                *(RIGHT_DS_READ_PROPERTY on the Quotas container or
-                *((the client is querying the quota for the security principal it is authenticated as) and
-                *(DS-Query-Self-Quota control access right on the Quotas container))
-                */
+       ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
+       if (ret != LDB_SUCCESS) {
+               ldb_debug(ldb, LDB_DEBUG_ERROR,
+                         "acl_module_init: Unable to register control with rootdse!\n");
+               return ldb_operr(ldb);
        }
 
-        if (ldb_attr_cmp("userPassword", attribute_name) == 0) {
-               /*When the dSHeuristics.fUserPwdSupport flag is false, the requester must be granted RIGHT_DS_READ_PROPERTY.
-                *When the dSHeuristics.fUserPwdSupport flag is true, access is never granted.
-                */
+       data = talloc_zero(module, struct acl_private);
+       if (data == NULL) {
+               return ldb_oom(ldb);
        }
 
-       if (ldb_attr_cmp("sDRightsEffective", attribute_name) == 0) {
-               /*FIXME:3.1.1.4.5.4 in MS-ADTS*/
+       data->acl_search = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
+                                       NULL, "acl", "search", true);
+       ldb_module_set_private(module, data);
+
+       mem_ctx = talloc_new(module);
+       if (!mem_ctx) {
+               return ldb_oom(ldb);
        }
 
-       if (ldb_attr_cmp("allowedChildClassesEffective", attribute_name) == 0) {
-               /*FIXME:3.1.1.4.5.5 in MS-ADTS*/
-        }
+       ret = dsdb_module_search_dn(module, mem_ctx, &res,
+                                   ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
+                                   attrs,
+                                   DSDB_FLAG_NEXT_MODULE |
+                                   DSDB_FLAG_AS_SYSTEM,
+                                   NULL);
+       if (ret != LDB_SUCCESS) {
+               goto done;
+       }
+       if (res->count == 0) {
+               goto done;
+       }
 
-       if (ldb_attr_cmp("allowedAttributesEffective", attribute_name) == 0) {
-               /*FIXME:3.1.1.4.5.7 in MS-ADTS*/
-        }
+       if (res->count > 1) {
+               talloc_free(mem_ctx);
+               return LDB_ERR_CONSTRAINT_VIOLATION;
+       }
 
-       if (ldb_attr_cmp("msDS-Approx-Immed-Subordinates", attribute_name) == 0) {
-               /*FIXME:3.1.1.4.5.15 in MS-ADTS*/
-        }
+       msg = res->msgs[0];
 
-       if (ldb_attr_cmp("msDS-QuotaEffective", attribute_name) == 0) {
-               /*FIXME:3.1.1.4.5.22 in MS-ADTS*/
-        }
+       password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
+       if (!password_attributes) {
+               goto done;
+       }
+       data->password_attrs = talloc_array(data, const char *,
+                       password_attributes->num_values +
+                       ARRAY_SIZE(secret_attrs) + 1);
+       if (!data->password_attrs) {
+               talloc_free(mem_ctx);
+               return ldb_oom(ldb);
+       }
 
-       if (ldb_attr_cmp("msDS-ReplAttributeMetaData", attribute_name) == 0 || ldb_attr_cmp("msDS-ReplAttributeMetaData", attribute_name) == 0) {
-               /*The security context of the requester must be granted the following rights on the replPropertyMetaData attribute:
-                *(RIGHT_DS_READ_PROPERTY)or (DS-Replication-Manage-Topology by ON!nTSecurityDescriptor)
-                */
-        }
+       n = 0;
+       for (i=0; i < password_attributes->num_values; i++) {
+               data->password_attrs[n] = (const char *)password_attributes->values[i].data;
+               talloc_steal(data->password_attrs, password_attributes->values[i].data);
+               n++;
+       }
 
-       if (ldb_attr_cmp("msDS-NCReplInboundNeighbors", attribute_name) == 0) {
-               /*The security context of the requester must be granted the following rights on repsFrom:
-                *(RIGHT_DS_READ_PROPERTY) or (DS-Replication-Manage-Topology) or (DS-Replication-Monitor-Topology)
-                */
-        }
+       for (i=0; i < ARRAY_SIZE(secret_attrs); i++) {
+               bool found = false;
 
-       if (ldb_attr_cmp("msDS-NCReplOutboundNeighbors", attribute_name) == 0) {
-               /*The security context of the requester must be granted the following rights on repsTo:
-                *(RIGHT_DS_READ_PROPERTY) or (DS-Replication-Manage-Topology) or (DS-Replication-Monitor-Topology)
-                */
-        }
+               for (j=0; j < n; j++) {
+                       if (strcasecmp(data->password_attrs[j], secret_attrs[i]) == 0) {
+                               found = true;
+                               break;
+                       }
+               }
 
-       if (ldb_attr_cmp("msDS-NCReplCursors", attribute_name) == 0) {
-               /*The security context of the requester must be granted the following rights on replUpToDateVector: (RIGHT_DS_READ_PROPERTY)
-                *or (DS-Replication-Manage-Topology) or (DS-Replication-Monitor-Topology)
-                */
-        }
+               if (found) {
+                       continue;
+               }
 
-       if (ldb_attr_cmp("msDS-IsUserCachableAtRodc", attribute_name) == 0) {
-               /*The security context of the requester must be granted
-                *the DS-Replication-Secrets-Synchronize control access right on the root of the default NC.
-                */
-        }
+               data->password_attrs[n] = talloc_strdup(data->password_attrs,
+                                                       secret_attrs[i]);
+               if (data->password_attrs[n] == NULL) {
+                       talloc_free(mem_ctx);
+                       return ldb_oom(ldb);
+               }
+               n++;
+       }
+       data->password_attrs[n] = NULL;
 
-       return NT_STATUS_OK;
+done:
+       talloc_free(mem_ctx);
+       return ldb_next_init(module);
 }
 
-/* Builds an object tree for object specific access checks */
-static struct object_tree * build_object_tree_form_attr_list(TALLOC_CTX *mem_ctx,   /* Todo this context or separate? */
-                                                            struct ldb_context *ldb,
-                                                            const char ** attr_names,
-                                                            int num_attrs,
-                                                            const char * object_class,
-                                                            uint32_t init_access)
+static int acl_allowedAttributes(struct ldb_module *module,
+                                const struct dsdb_schema *schema,
+                                struct ldb_message *sd_msg,
+                                struct ldb_message *msg,
+                                struct acl_context *ac)
 {
-       const struct dsdb_schema *schema = dsdb_get_schema(ldb);
-       const struct GUID *oc_guid = class_schemaid_guid_by_lDAPDisplayName(schema, object_class);
-       struct object_tree *tree;
-       int i;
+       struct ldb_message_element *oc_el;
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       TALLOC_CTX *mem_ctx;
+       const char **attr_list;
+       int i, ret;
 
-       if (!oc_guid)
-               return NULL;
+       /* If we don't have a schema yet, we can't do anything... */
+       if (schema == NULL) {
+               ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-       tree = insert_in_object_tree(mem_ctx, oc_guid, NULL, init_access, NULL);
-       if (attr_names){
-               for (i=0; i < num_attrs; i++){
-                       const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema,attr_names[i]);
-                       if (attribute)
-                               insert_in_object_tree(mem_ctx,
-                                                     &attribute->schemaIDGUID,
-                                                     &attribute->attributeSecurityGUID,
-                                                     init_access,
-                                                     tree);
+       /* Must remove any existing attribute */
+       if (ac->allowedAttributes) {
+               ldb_msg_remove_attr(msg, "allowedAttributes");
+       }
+
+       mem_ctx = talloc_new(msg);
+       if (!mem_ctx) {
+               return ldb_oom(ldb);
+       }
+
+       oc_el = ldb_msg_find_element(sd_msg, "objectClass");
+       attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
+       if (!attr_list) {
+               ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
+               talloc_free(mem_ctx);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       if (ac->allowedAttributes) {
+               for (i=0; attr_list && attr_list[i]; i++) {
+                       ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
                }
        }
-       return tree;
-}
+       if (ac->allowedAttributesEffective) {
+               struct security_descriptor *sd;
+               struct dom_sid *sid = NULL;
+               struct ldb_control *as_system = ldb_request_get_control(ac->req,
+                                                                       LDB_CONTROL_AS_SYSTEM_OID);
+
+               if (as_system != NULL) {
+                       as_system->critical = 0;
+               }
 
-bool is_root_base_dn(struct ldb_context *ldb, struct ldb_dn *dn_to_check)
-{
-       int result;
-       struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
-       result = ldb_dn_compare(root_base_dn,dn_to_check);
-       return (result==0);
+               ldb_msg_remove_attr(msg, "allowedAttributesEffective");
+               if (ac->am_system || as_system) {
+                       for (i=0; attr_list && attr_list[i]; i++) {
+                               ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
+                       }
+                       return LDB_SUCCESS;
+               }
+
+               ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
+
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+
+               sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
+               for (i=0; attr_list && attr_list[i]; i++) {
+                       const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
+                                                                                       attr_list[i]);
+                       if (!attr) {
+                               return ldb_operr(ldb);
+                       }
+                       /* remove constructed attributes */
+                       if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
+                           || attr->systemOnly
+                           || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
+                               continue;
+                       }
+                       ret = acl_check_access_on_attribute(module,
+                                                           msg,
+                                                           sd,
+                                                           sid,
+                                                           SEC_ADS_WRITE_PROP,
+                                                           attr);
+                       if (ret == LDB_SUCCESS) {
+                               ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
+                       }
+               }
+       }
+       return LDB_SUCCESS;
 }
 
-static int acl_op_callback(struct ldb_request *req, struct ldb_reply *ares)
+static int acl_childClasses(struct ldb_module *module,
+                           const struct dsdb_schema *schema,
+                           struct ldb_message *sd_msg,
+                           struct ldb_message *msg,
+                           const char *attrName)
 {
-       struct acl_context *ac;
-
-       ac = talloc_get_type(req->context, struct acl_context);
+       struct ldb_message_element *oc_el;
+       struct ldb_message_element *allowedClasses;
+       const struct dsdb_class *sclass;
+       unsigned int i, j;
+       int ret;
 
-       if (!ares) {
-               return ldb_module_done(ac->req, NULL, NULL,
-                                       LDB_ERR_OPERATIONS_ERROR);
+       /* If we don't have a schema yet, we can't do anything... */
+       if (schema == NULL) {
+               ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
+               return LDB_ERR_OPERATIONS_ERROR;
        }
-       if (ares->error != LDB_SUCCESS) {
-               return ldb_module_done(ac->req, ares->controls,
-                                       ares->response, ares->error);
+
+       /* Must remove any existing attribute, or else confusion reins */
+       ldb_msg_remove_attr(msg, attrName);
+       ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
 
-       if (ares->type != LDB_REPLY_DONE) {
-               talloc_free(ares);
-               return ldb_module_done(ac->req, NULL, NULL,
-                                       LDB_ERR_OPERATIONS_ERROR);
+       oc_el = ldb_msg_find_element(sd_msg, "objectClass");
+
+       for (i=0; oc_el && i < oc_el->num_values; i++) {
+               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;
+               }
+
+               for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
+                       ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
+               }
+       }
+       if (allowedClasses->num_values > 1) {
+               TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
+               for (i=1 ; i < allowedClasses->num_values; i++) {
+                       struct ldb_val *val1 = &allowedClasses->values[i-1];
+                       struct ldb_val *val2 = &allowedClasses->values[i];
+                       if (data_blob_cmp(val1, val2) == 0) {
+                               memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
+                               allowedClasses->num_values--;
+                               i--;
+                       }
+               }
        }
 
-       return ldb_module_done(ac->req, ares->controls,
-                               ares->response, ares->error);
+       return LDB_SUCCESS;
 }
 
-
-static int acl_access_check_add(struct ldb_reply *ares,
-                               struct acl_context *ac,
-                               struct security_descriptor *sd)
+static int acl_check_access_on_class(struct ldb_module *module,
+                                    const struct dsdb_schema *schema,
+                                    TALLOC_CTX *mem_ctx,
+                                    struct security_descriptor *sd,
+                                    struct security_token *token,
+                                    struct dom_sid *rp_sid,
+                                    uint32_t access_mask,
+                                    const char *class_name)
 {
-       uint32_t access_granted = 0;
+       int ret;
        NTSTATUS status;
-       struct ldb_dn *parent;
-       struct ldb_dn *grandparent;
-       struct object_tree *tree = NULL;
-
-       parent = ldb_dn_get_parent(ac->req, ac->req->op.add.message->dn);
-       grandparent = ldb_dn_get_parent(ac->req, parent);
-       if (ldb_dn_compare(ares->message->dn, grandparent) == 0)
-               status = sec_access_check_ds(sd, ac->token,
-                                            SEC_ADS_LIST,
-                                            &access_granted,
-                                            NULL);
-       else if (ldb_dn_compare(ares->message->dn, parent) == 0){
-               struct ldb_message_element *oc_el;
-               struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
-               const struct dsdb_schema *schema = dsdb_get_schema(ldb);
-               int i;
-
-               oc_el = ldb_msg_find_element(ares->message, "objectClass");
-               if (!oc_el || oc_el->num_values == 0)
-                       return LDB_SUCCESS;
-               for (i = 0; i < oc_el->num_values; i++){
-                       const struct GUID *guid = class_schemaid_guid_by_lDAPDisplayName(schema,
-                                                                                         oc_el->values[i].data);
-                       ac->sec_result = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
-                       tree = insert_in_object_tree(ac->req, guid, NULL, SEC_ADS_CREATE_CHILD,
-                                                    tree);
-                       status = sec_access_check_ds(sd, ac->token, SEC_ADS_CREATE_CHILD,&access_granted, tree);
-                       if (NT_STATUS_IS_OK(status))
-                               ac->sec_result = LDB_SUCCESS;
+       uint32_t access_granted;
+       struct object_tree *root = NULL;
+       struct object_tree *new_node = NULL;
+       const struct GUID *guid;
+
+       if (class_name != NULL) {
+               guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
+               if (!guid) {
+                       DEBUG(10, ("acl_search: cannot find class %s\n",
+                                  class_name));
+                       goto fail;
+               }
+               if (!insert_in_object_tree(mem_ctx,
+                                          guid, access_mask,
+                                          &root, &new_node)) {
+                       DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
+                       goto fail;
                }
        }
-       else
-               return LDB_SUCCESS;
 
-       return ac->sec_result;
+       status = sec_access_check_ds(sd, token,
+                                    access_mask,
+                                    &access_granted,
+                                    root,
+                                    rp_sid);
+       if (!NT_STATUS_IS_OK(status)) {
+               ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
+       } else {
+               ret = LDB_SUCCESS;
+       }
+       return ret;
+fail:
+       return ldb_operr(ldb_module_get_ctx(module));
 }
 
-static int acl_access_check_modify(struct ldb_reply *ares, struct acl_context *ac,
-                                  struct security_descriptor *sd)
+static int acl_childClassesEffective(struct ldb_module *module,
+                                    const struct dsdb_schema *schema,
+                                    struct ldb_message *sd_msg,
+                                    struct ldb_message *msg,
+                                    struct acl_context *ac)
 {
-       uint32_t access_granted = 0;
-       NTSTATUS status;
-       struct ldb_dn *parent;
-       struct object_tree *tree = NULL;
-
-       parent = ldb_dn_get_parent(ac->req, ac->req->op.add.message->dn);
-       if (ldb_dn_compare(ares->message->dn, parent) == 0)
-               status = sec_access_check_ds(sd, ac->token, SEC_ADS_LIST,&access_granted, NULL);
-       else if (ldb_dn_compare(ares->message->dn, ac->req->op.add.message->dn) == 0){
-               struct ldb_message_element *oc_el;
-               struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
-               const struct dsdb_schema *schema = dsdb_get_schema(ldb);
-               int i;
-               struct GUID *guid;
-               oc_el = ldb_msg_find_element(ares->message, "objectClass");
-               if (!oc_el || oc_el->num_values == 0)
-                       return LDB_SUCCESS;
+       struct ldb_message_element *oc_el;
+       struct ldb_message_element *allowedClasses = NULL;
+       const struct dsdb_class *sclass;
+       struct security_descriptor *sd;
+       struct ldb_control *as_system = ldb_request_get_control(ac->req,
+                                                               LDB_CONTROL_AS_SYSTEM_OID);
+       struct dom_sid *sid = NULL;
+       unsigned int i, j;
+       int ret;
 
-               guid = class_schemaid_guid_by_lDAPDisplayName(schema,
-                                                             oc_el->values[oc_el->num_values-1].data);
-               tree = insert_in_object_tree(ac->req, guid, NULL, SEC_ADS_WRITE_PROP,
-                                                    tree);
-               for (i=0; i < ac->req->op.mod.message->num_elements; i++){
-                       const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
-                                                                                             ac->req->op.mod.message->elements[i].name);
-                       if (!attr)
-                               return LDB_ERR_OPERATIONS_ERROR; /* What should we actually return here? */
-                       insert_in_object_tree(ac, &attr->schemaIDGUID,
-                                             &attr->attributeSecurityGUID, ac->access_needed, tree);
+       if (as_system != NULL) {
+               as_system->critical = 0;
+       }
+
+       if (ac->am_system || as_system) {
+               return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
+       }
+
+       /* If we don't have a schema yet, we can't do anything... */
+       if (schema == NULL) {
+               ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       /* Must remove any existing attribute, or else confusion reins */
+       ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
+
+       oc_el = ldb_msg_find_element(sd_msg, "objectClass");
+       ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+
+       sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
+       for (i=0; oc_el && i < oc_el->num_values; i++) {
+               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;
+               }
+
+               for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
+                       ret = acl_check_access_on_class(module,
+                                                       schema,
+                                                       msg,
+                                                       sd,
+                                                       acl_user_token(module),
+                                                       sid,
+                                                       SEC_ADS_CREATE_CHILD,
+                                                       sclass->possibleInferiors[j]);
+                       if (ret == LDB_SUCCESS) {
+                               ldb_msg_add_string(msg, "allowedChildClassesEffective",
+                                                  sclass->possibleInferiors[j]);
+                       }
                }
-               status = sec_access_check_ds(sd, ac->token, SEC_ADS_WRITE_PROP ,&access_granted, tree);
-               if (!NT_STATUS_IS_OK(status))
-                       ac->sec_result = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
        }
-       else
+       allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
+       if (!allowedClasses) {
                return LDB_SUCCESS;
-       return ac->sec_result;
-}
-/*TODO*/
-static int acl_access_check_rename(struct ldb_reply *ares, struct acl_context *ac,
-                                  struct security_descriptor *sd)
-{
-       return ac->sec_result;
+       }
+
+       if (allowedClasses->num_values > 1) {
+               TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
+               for (i=1 ; i < allowedClasses->num_values; i++) {
+                       struct ldb_val *val1 = &allowedClasses->values[i-1];
+                       struct ldb_val *val2 = &allowedClasses->values[i];
+                       if (data_blob_cmp(val1, val2) == 0) {
+                               memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
+                               allowedClasses->num_values--;
+                               i--;
+                       }
+               }
+       }
+       return LDB_SUCCESS;
 }
 
-static int acl_access_check_delete(struct ldb_reply *ares, struct acl_context *ac,
-                                  struct security_descriptor *sd)
+static int acl_sDRightsEffective(struct ldb_module *module,
+                                struct ldb_message *sd_msg,
+                                struct ldb_message *msg,
+                                struct acl_context *ac)
 {
-       uint32_t access_granted = 0;
-       NTSTATUS status;
-       struct ldb_dn *parent;
-       struct object_tree *tree = NULL;
-
-       parent = ldb_dn_get_parent(ac->req, ac->req->op.del.dn);
-       if (ldb_dn_compare(ares->message->dn, parent) == 0){
-               status = sec_access_check_ds(sd, ac->token, SEC_ADS_LIST,&access_granted, NULL);
-               if (!NT_STATUS_IS_OK(status)){
-                       ac->sec_result = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
-                       return ac->sec_result;
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct ldb_message_element *rightsEffective;
+       int ret;
+       struct security_descriptor *sd;
+       struct ldb_control *as_system = ldb_request_get_control(ac->req,
+                                                               LDB_CONTROL_AS_SYSTEM_OID);
+       struct dom_sid *sid = NULL;
+       uint32_t flags = 0;
+
+       if (as_system != NULL) {
+               as_system->critical = 0;
+       }
+
+       /* Must remove any existing attribute, or else confusion reins */
+       ldb_msg_remove_attr(msg, "sDRightsEffective");
+       ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (ac->am_system || as_system) {
+               flags = SECINFO_OWNER | SECINFO_GROUP |  SECINFO_SACL |  SECINFO_DACL;
+       }
+       else {
+               const struct dsdb_attribute *attr;
+
+               attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
+                                                        "nTSecurityDescriptor");
+               if (attr == NULL) {
+                       return ldb_operr(ldb);
+               }
+
+               /* Get the security descriptor from the message */
+               ret = dsdb_get_sd_from_ldb_message(ldb, msg, sd_msg, &sd);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
                }
-               status = sec_access_check_ds(sd, ac->token, SEC_ADS_DELETE_CHILD,&access_granted, NULL);
-               if (NT_STATUS_IS_OK(status)){
-                       ac->sec_result = LDB_SUCCESS;
-                       return ac->sec_result;
+               sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
+               ret = acl_check_access_on_attribute(module,
+                                                   msg,
+                                                   sd,
+                                                   sid,
+                                                   SEC_STD_WRITE_OWNER,
+                                                   attr);
+               if (ret == LDB_SUCCESS) {
+                       flags |= SECINFO_OWNER | SECINFO_GROUP;
+               }
+               ret = acl_check_access_on_attribute(module,
+                                                   msg,
+                                                   sd,
+                                                   sid,
+                                                   SEC_STD_WRITE_DAC,
+                                                   attr);
+               if (ret == LDB_SUCCESS) {
+                       flags |= SECINFO_DACL;
+               }
+               ret = acl_check_access_on_attribute(module,
+                                                   msg,
+                                                   sd,
+                                                   sid,
+                                                   SEC_FLAG_SYSTEM_SECURITY,
+                                                   attr);
+               if (ret == LDB_SUCCESS) {
+                       flags |= SECINFO_SACL;
                }
        }
-       else if (ldb_dn_compare(ares->message->dn, ac->req->op.del.dn) == 0){
-               status = sec_access_check_ds(sd, ac->token, SEC_STD_DELETE, &access_granted, NULL);
-               if (!NT_STATUS_IS_OK(status))
-                       ac->sec_result = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
-       }
-       return ac->sec_result;
+       return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
+                                 "sDRightsEffective", flags);
 }
 
-static int acl_access_check_search(struct ldb_reply *ares, struct acl_context *ac,
-                                  struct security_descriptor *sd)
+static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
+                                 struct ldb_context *ldb,
+                                 const char *spn_value,
+                                 uint32_t userAccountControl,
+                                 const char *samAccountName,
+                                 const char *dnsHostName,
+                                 const char *netbios_name,
+                                 const char *ntds_guid)
 {
-       uint32_t access_granted;
-       NTSTATUS status;
-       struct ldb_dn *parent;
+       int ret;
+       krb5_context krb_ctx;
+       krb5_error_code kerr;
+       krb5_principal principal;
+       char *instanceName;
+       char *serviceType;
+       char *serviceName;
+       const char *forest_name = samdb_forest_name(ldb, mem_ctx);
+       const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
+       struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
+                                                         struct loadparm_context);
+       bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
+               (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
+
+       if (strcasecmp_m(spn_value, samAccountName) == 0) {
+               /* MacOS X sets this value, and setting an SPN of your
+                * own samAccountName is both pointless and safe */
+               return LDB_SUCCESS;
+       }
+
+       kerr = smb_krb5_init_context_basic(mem_ctx,
+                                          lp_ctx,
+                                          &krb_ctx);
+       if (kerr != 0) {
+               return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
+                                "Could not initialize kerberos context.");
+       }
+
+       ret = krb5_parse_name(krb_ctx, spn_value, &principal);
+       if (ret) {
+               krb5_free_context(krb_ctx);
+               return LDB_ERR_CONSTRAINT_VIOLATION;
+       }
+
+       if (principal->name.name_string.len < 2) {
+               goto fail;
+       }
 
-       if (ac->user_type == SECURITY_SYSTEM || ac->user_type == SECURITY_ANONYMOUS) {
-               return LDB_SUCCESS;/*FIXME: we have anonymous access*/
+       instanceName = principal->name.name_string.val[1];
+       serviceType = principal->name.name_string.val[0];
+       if (principal->name.name_string.len == 3) {
+               serviceName = principal->name.name_string.val[2];
+       } else {
+               serviceName = NULL;
        }
 
-       parent = ldb_dn_get_parent(ac->req, ac->dn_to_check);
-       ac->sec_result = LDB_SUCCESS;
-       if (ldb_dn_compare(ares->message->dn, parent) == 0) {
-               status = sec_access_check_ds(sd, ac->token, SEC_ADS_LIST,&access_granted, NULL);
-               if (!NT_STATUS_IS_OK(status)) {
-                       ac->sec_result = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
+       if (serviceName) {
+               if (!is_dc) {
+                       goto fail;
+               }
+               if (strcasecmp(serviceType, "ldap") == 0) {
+                       if (strcasecmp(serviceName, netbios_name) != 0 &&
+                           strcasecmp(serviceName, forest_name) != 0) {
+                               goto fail;
+                       }
+
+               } else if (strcasecmp(serviceType, "gc") == 0) {
+                       if (strcasecmp(serviceName, forest_name) != 0) {
+                               goto fail;
+                       }
+               } else {
+                       if (strcasecmp(serviceName, base_domain) != 0 &&
+                           strcasecmp(serviceName, netbios_name) != 0) {
+                               goto fail;
+                       }
                }
        }
+       /* instanceName can be samAccountName without $ or dnsHostName
+        * or "ntds_guid._msdcs.forest_domain for DC objects */
+       if (strlen(instanceName) == (strlen(samAccountName) - 1)
+           && strncasecmp(instanceName, samAccountName, strlen(samAccountName) - 1) == 0) {
+               goto success;
+       } else if (dnsHostName != NULL && strcasecmp(instanceName, dnsHostName) == 0) {
+               goto success;
+       } else if (is_dc) {
+               const char *guid_str;
+               guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
+                                          ntds_guid,
+                                          forest_name);
+               if (strcasecmp(instanceName, guid_str) == 0) {
+                       goto success;
+               }
+       }
+
+fail:
+       krb5_free_principal(krb_ctx, principal);
+       krb5_free_context(krb_ctx);
+       return LDB_ERR_CONSTRAINT_VIOLATION;
 
-       return ac->sec_result;
+success:
+       krb5_free_principal(krb_ctx, principal);
+       krb5_free_context(krb_ctx);
+       return LDB_SUCCESS;
 }
 
-static int acl_perform_access_check(struct ldb_request *req, struct ldb_reply *ares,
-                                   struct acl_context *ac)
+static int acl_check_spn(TALLOC_CTX *mem_ctx,
+                        struct ldb_module *module,
+                        struct ldb_request *req,
+                        struct security_descriptor *sd,
+                        struct dom_sid *sid,
+                        const struct GUID *oc_guid,
+                        const struct dsdb_attribute *attr)
 {
-       struct ldb_message_element *oc_el;
-       struct security_descriptor *sd;
-       enum ndr_err_code ndr_err;
-
-       oc_el = ldb_msg_find_element(ares->message, "ntSecurityDescriptor");
-       if (!oc_el || oc_el->num_values == 0)
+       int ret;
+       unsigned int i;
+       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct ldb_result *acl_res;
+       struct ldb_result *netbios_res;
+       struct ldb_message_element *el;
+       struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
+       uint32_t userAccountControl;
+       const char *samAccountName;
+       const char *dnsHostName;
+       const char *netbios_name;
+       struct GUID ntds;
+       char *ntds_guid = NULL;
+
+       static const char *acl_attrs[] = {
+               "samAccountName",
+               "dnsHostName",
+               "userAccountControl",
+               NULL
+       };
+       static const char *netbios_attrs[] = {
+               "nETBIOSName",
+               NULL
+       };
+
+       /* if we have wp, we can do whatever we like */
+       if (acl_check_access_on_attribute(module,
+                                         tmp_ctx,
+                                         sd,
+                                         sid,
+                                         SEC_ADS_WRITE_PROP,
+                                         attr) == LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
                return LDB_SUCCESS;
+       }
 
-       sd = talloc(ac, struct security_descriptor);
-       if(!sd) {
-               return ldb_module_done(ac->req, ares->controls,
-                                      ares->response, LDB_ERR_OPERATIONS_ERROR);
+       ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
+                                      GUID_DRS_VALIDATE_SPN,
+                                      SEC_ADS_SELF_WRITE,
+                                      sid);
+
+       if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
+               dsdb_acl_debug(sd, acl_user_token(module),
+                              req->op.mod.message->dn,
+                              true,
+                              10);
+               talloc_free(tmp_ctx);
+               return ret;
        }
-       ndr_err = ndr_pull_struct_blob(&oc_el->values[0], sd, NULL, sd,
-                                      (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
 
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err))
-               return ldb_module_done(ac->req, ares->controls,
-                                      ares->response, LDB_ERR_OPERATIONS_ERROR);
-       switch (ac->req->operation) {
-       case LDB_SEARCH:
-               return acl_access_check_search(ares, ac, sd);
-       case LDB_ADD:
-               return acl_access_check_add(ares, ac, sd);
-       case LDB_MODIFY:
-               return acl_access_check_modify(ares, ac, sd);
-       case LDB_DELETE:
-               return acl_access_check_delete(ares, ac, sd);
-       case LDB_RENAME:
-               return acl_access_check_rename(ares, ac, sd);
-       default:
-               return ldb_module_done(ac->req, ares->controls,
-                                      ares->response, LDB_ERR_OPERATIONS_ERROR);
+       ret = dsdb_module_search_dn(module, tmp_ctx,
+                                   &acl_res, req->op.mod.message->dn,
+                                   acl_attrs,
+                                   DSDB_FLAG_NEXT_MODULE |
+                                   DSDB_FLAG_AS_SYSTEM |
+                                   DSDB_SEARCH_SHOW_RECYCLED,
+                                   req);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return ret;
+       }
+
+       userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
+       dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
+       samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
+
+       ret = dsdb_module_search(module, tmp_ctx,
+                                &netbios_res, partitions_dn,
+                                LDB_SCOPE_ONELEVEL,
+                                netbios_attrs,
+                                DSDB_FLAG_NEXT_MODULE |
+                                DSDB_FLAG_AS_SYSTEM,
+                                req,
+                                "(ncName=%s)",
+                                ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
+
+       netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
+
+       el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
+       if (!el) {
+               talloc_free(tmp_ctx);
+               return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
+                                        "Error finding element for servicePrincipalName.");
        }
+
+       /* NTDSDSA objectGuid of object we are checking SPN for */
+       if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
+               ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
+                                                            req->op.mod.message->dn, &ntds, req);
+               if (ret != LDB_SUCCESS) {
+                       ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
+                                              ldb_dn_get_linearized(req->op.mod.message->dn),
+                                              ldb_strerror(ret));
+                       talloc_free(tmp_ctx);
+                       return LDB_ERR_OPERATIONS_ERROR;
+               }
+               ntds_guid = GUID_string(tmp_ctx, &ntds);
+       }
+
+       for (i=0; i < el->num_values; i++) {
+               ret = acl_validate_spn_value(tmp_ctx,
+                                            ldb,
+                                            (char *)el->values[i].data,
+                                            userAccountControl,
+                                            samAccountName,
+                                            dnsHostName,
+                                            netbios_name,
+                                            ntds_guid);
+               if (ret != LDB_SUCCESS) {
+                       talloc_free(tmp_ctx);
+                       return ret;
+               }
+       }
+       talloc_free(tmp_ctx);
        return LDB_SUCCESS;
 }
 
-static int acl_forward_add(struct ldb_reply *ares,
-                          struct acl_context *ac)
+static int acl_add(struct ldb_module *module, struct ldb_request *req)
 {
-  struct ldb_request *newreq;
-       struct ldb_context *ldb;
        int ret;
+       struct ldb_dn *parent;
+       struct ldb_context *ldb;
+       const struct dsdb_schema *schema;
+       struct ldb_message_element *oc_el;
+       const struct GUID *guid;
+       struct ldb_dn *nc_root;
+       struct ldb_control *as_system;
 
-       ldb = ldb_module_get_ctx(ac->module);
-       ret = ldb_build_add_req(&newreq,ldb,
-                               ac,
-                               ac->req->op.add.message,
-                               ac->req->controls,
-                               ac,
-                               acl_op_callback,
-                               ac->req);
-       if (ret != LDB_SUCCESS)
-               return ldb_module_done(ac->req, ares->controls,
-                                      ares->response, LDB_ERR_OPERATIONS_ERROR);
-       return ldb_next_request(ac->module, newreq);
-}
+       if (ldb_dn_is_special(req->op.add.message->dn)) {
+               return ldb_next_request(module, req);
+       }
 
-static int acl_forward_modify(struct ldb_reply *ares,
-                             struct acl_context *ac)
-{
-  struct ldb_request *newreq;
-       struct ldb_context *ldb;
-       int ret;
+       as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
+       if (as_system != NULL) {
+               as_system->critical = 0;
+       }
 
-       ldb = ldb_module_get_ctx(ac->module);
-       ret = ldb_build_mod_req(&newreq,ldb,
-                                   ac,
-                                   ac->req->op.mod.message,
-                                   ac->req->controls,
-                                   ac,
-                                   acl_op_callback,
-                                   ac->req);
-       if (ret != LDB_SUCCESS)
-               return ldb_module_done(ac->req, ares->controls,
-                                      ares->response, LDB_ERR_OPERATIONS_ERROR);
-       return ldb_next_request(ac->module, newreq);
-}
+       if (dsdb_module_am_system(module) || as_system) {
+               return ldb_next_request(module, req);
+       }
 
-static int acl_forward_delete(struct ldb_reply *ares,
-                             struct acl_context *ac)
-{
-       struct ldb_request *newreq;
-       struct ldb_context *ldb;
-       int ret;
+       ldb = ldb_module_get_ctx(module);
 
-       ldb = ldb_module_get_ctx(ac->module);
-       ret = ldb_build_del_req(&newreq, ldb,
-                                   ac,
-                                   ac->req->op.del.dn,
-                                   ac->req->controls,
-                                   ac,
-                                   acl_op_callback,
-                                   ac->req);
-       if (ret != LDB_SUCCESS)
-               return ldb_module_done(ac->req, ares->controls,
-                                      ares->response, LDB_ERR_OPERATIONS_ERROR);
-       return ldb_next_request(ac->module, newreq);
+       parent = ldb_dn_get_parent(req, req->op.add.message->dn);
+       if (parent == NULL) {
+               return ldb_oom(ldb);
+       }
+
+       /* Creating an NC. There is probably something we should do here,
+        * but we will establish that later */
+
+       ret = dsdb_find_nc_root(ldb, req, req->op.add.message->dn, &nc_root);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (ldb_dn_compare(nc_root, req->op.add.message->dn) == 0) {
+               talloc_free(nc_root);
+               return ldb_next_request(module, req);
+       }
+       talloc_free(nc_root);
+
+       schema = dsdb_get_schema(ldb, req);
+       if (!schema) {
+               return ldb_operr(ldb);
+       }
+
+       oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
+       if (!oc_el || oc_el->num_values == 0) {
+               ldb_asprintf_errstring(ldb_module_get_ctx(module),
+                                      "acl: unable to find objectClass on %s\n",
+                                      ldb_dn_get_linearized(req->op.add.message->dn));
+               return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
+       }
+
+       guid = class_schemaid_guid_by_lDAPDisplayName(schema,
+                                                     (char *)oc_el->values[oc_el->num_values-1].data);
+       ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid, req);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       return ldb_next_request(module, req);
 }
 
-static int acl_forward_rename(struct ldb_reply *ares,
-                             struct acl_context *ac)
+/* ckecks if modifications are allowed on "Member" attribute */
+static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
+                                    struct ldb_module *module,
+                                    struct ldb_request *req,
+                                    struct security_descriptor *sd,
+                                    struct dom_sid *sid,
+                                    const struct GUID *oc_guid,
+                                    const struct dsdb_attribute *attr)
 {
-       return LDB_SUCCESS;
+       int ret;
+       unsigned int i;
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct ldb_dn *user_dn;
+       struct ldb_message_element *member_el;
+       /* if we have wp, we can do whatever we like */
+       if (acl_check_access_on_attribute(module,
+                                         mem_ctx,
+                                         sd,
+                                         sid,
+                                         SEC_ADS_WRITE_PROP,
+                                         attr) == LDB_SUCCESS) {
+               return LDB_SUCCESS;
+       }
+       /* if we are adding/deleting ourselves, check for self membership */
+       ret = dsdb_find_dn_by_sid(ldb, mem_ctx, 
+                                 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX], 
+                                 &user_dn);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       member_el = ldb_msg_find_element(req->op.mod.message, "member");
+       if (!member_el) {
+               return ldb_operr(ldb);
+       }
+       /* user can only remove oneself */
+       if (member_el->num_values == 0) {
+               return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
+       }
+       for (i = 0; i < member_el->num_values; i++) {
+               if (strcasecmp((const char *)member_el->values[i].data,
+                              ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
+                       return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
+               }
+       }
+       ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
+                                      GUID_DRS_SELF_MEMBERSHIP,
+                                      SEC_ADS_SELF_WRITE,
+                                      sid);
+       if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
+               dsdb_acl_debug(sd, acl_user_token(module),
+                              req->op.mod.message->dn,
+                              true,
+                              10);
+       }
+       return ret;
 }
 
-static int acl_forward_search(struct acl_context *ac)
+static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
+                                    struct ldb_module *module,
+                                    struct ldb_request *req,
+                                    struct security_descriptor *sd,
+                                    struct dom_sid *sid,
+                                    const struct GUID *oc_guid,
+                                    bool userPassword)
 {
-       int ret;
-       const char * const *attrs;
-       struct ldb_control *sd_control;
-       struct ldb_control **sd_saved_controls;
-       struct ldb_context *ldb;
-       struct ldb_request *newreq;
-
-       ldb = ldb_module_get_ctx(ac->module);
-       attrs = ac->req->op.search.attrs;
-       if (attrs) {
-               ac->nTSecurityDescriptor = false;
-               ac->objectClass = false;
-               if (!ldb_attr_in_list(ac->req->op.search.attrs, "nTSecurityDescriptor")) {
-                       attrs = ldb_attr_list_copy_add(ac, attrs, "nTSecurityDescriptor");
-                       ac->nTSecurityDescriptor = true;
+       int ret = LDB_SUCCESS;
+       unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
+       struct ldb_message_element *el;
+       struct ldb_message *msg;
+       const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
+                                       "unicodePwd", "dBCSPwd", NULL }, **l;
+       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
+
+       msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
+       if (msg == NULL) {
+               return ldb_module_oom(module);
+       }
+       for (l = passwordAttrs; *l != NULL; l++) {
+               if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
+                       continue;
                }
-               if (!ldb_attr_in_list(ac->req->op.search.attrs, "objectClass")) {
-                       attrs = ldb_attr_list_copy_add(ac, attrs, "objectClass");
-                       ac->objectClass = true;
+
+               while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
+                       if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
+                               ++del_attr_cnt;
+                       }
+                       if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
+                               ++add_attr_cnt;
+                       }
+                       if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
+                               ++rep_attr_cnt;
+                       }
+                       ldb_msg_remove_element(msg, el);
                }
        }
-       ret = ldb_build_search_req_ex(&newreq,ldb,
-                                     ac,
-                                     ac->req->op.search.base,
-                                     ac->req->op.search.scope,
-                                     ac->req->op.search.tree,
-                                     attrs,
-                                     ac->req->controls,
-                                     ac, acl_search_callback,
-                                     ac->req);
-       if (ret != LDB_SUCCESS) {
-                return LDB_ERR_OPERATIONS_ERROR;
-       }
-       /* check if there's an SD_FLAGS control */
-       sd_control = ldb_request_get_control(newreq, LDB_CONTROL_SD_FLAGS_OID);
-       if (sd_control) {
-               /* 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 (!save_controls(sd_control, newreq, &sd_saved_controls)) {
-                       return LDB_ERR_OPERATIONS_ERROR;
+
+       /* single deletes will be handled by the "password_hash" LDB module
+        * later in the stack, so we let it though here */
+       if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
+               talloc_free(tmp_ctx);
+               return LDB_SUCCESS;
+       }
+
+       if (ldb_request_get_control(req,
+                                   DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
+               /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
+                * have a user password change and not a set as the message
+                * looks like. In it's value blob it contains the NT and/or LM
+                * hash of the old password specified by the user.
+                * This control is used by the SAMR and "kpasswd" password
+                * change mechanisms. */
+               ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
+                                              GUID_DRS_USER_CHANGE_PASSWORD,
+                                              SEC_ADS_CONTROL_ACCESS,
+                                              sid);
+       }
+       else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
+               ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
+                                              GUID_DRS_FORCE_CHANGE_PASSWORD,
+                                              SEC_ADS_CONTROL_ACCESS,
+                                              sid);
+       }
+       else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
+               ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
+                                              GUID_DRS_USER_CHANGE_PASSWORD,
+                                              SEC_ADS_CONTROL_ACCESS,
+                                              sid);
+               /* Very strange, but we get constraint violation in this case */
+               if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
+                       ret = LDB_ERR_CONSTRAINT_VIOLATION;
                }
        }
-       return ldb_next_request(ac->module, newreq);
+       if (ret != LDB_SUCCESS) {
+               dsdb_acl_debug(sd, acl_user_token(module),
+                              req->op.mod.message->dn,
+                              true,
+                              10);
+       }
+       talloc_free(tmp_ctx);
+       return ret;
 }
 
-static int acl_forward_request(struct ldb_reply *ares,
-                              struct acl_context *ac)
+static const struct GUID *get_oc_guid_from_message(const struct dsdb_schema *schema,
+                                                  struct ldb_message *msg)
 {
-       switch (ac->req->operation) {
-       case LDB_SEARCH:
-               return acl_forward_search(ac);
-       case LDB_ADD:
-               return acl_forward_add(ares,ac);
-       case LDB_MODIFY:
-               return acl_forward_modify(ares,ac);
-       case LDB_DELETE:
-               return acl_forward_delete(ares,ac);
-       case LDB_RENAME:
-               return acl_forward_rename(ares,ac);
-       default:
-               return ldb_module_done(ac->req, ares->controls,
-                                      ares->response, LDB_ERR_OPERATIONS_ERROR);
+       struct ldb_message_element *oc_el;
+       const struct dsdb_class *object_class;
+
+       oc_el = ldb_msg_find_element(msg, "objectClass");
+       if (!oc_el) {
+               return NULL;
        }
-       return LDB_SUCCESS;
+
+       object_class = dsdb_get_last_structural_class(schema, oc_el);
+       if (object_class == NULL) {
+               return NULL;
+       }
+
+       return &object_class->schemaIDGUID;
 }
 
-static int acl_visible_callback(struct ldb_request *req, struct ldb_reply *ares)
+
+static int acl_modify(struct ldb_module *module, struct ldb_request *req)
 {
-       struct acl_context *ac;
+       int ret;
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       const struct dsdb_schema *schema;
+       unsigned int i;
+       const struct GUID *guid;
+       uint32_t access_granted;
+       NTSTATUS status;
+       struct ldb_result *acl_res;
+       struct security_descriptor *sd;
+       struct dom_sid *sid = NULL;
+       struct ldb_control *as_system;
+       bool userPassword;
+       TALLOC_CTX *tmp_ctx;
+       const struct ldb_message *msg = req->op.mod.message;
+       static const char *acl_attrs[] = {
+               "nTSecurityDescriptor",
+               "objectClass",
+               "objectSid",
+               NULL
+       };
+
+       if (ldb_dn_is_special(msg->dn)) {
+               return ldb_next_request(module, req);
+       }
 
-       ac = talloc_get_type(req->context, struct acl_context);
+       as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
+       if (as_system != NULL) {
+               as_system->critical = 0;
+       }
 
-       if (!ares) {
-               return ldb_module_done(ac->req, NULL, NULL,
-                                       LDB_ERR_OPERATIONS_ERROR);
+       /* Don't print this debug statement if elements[0].name is going to be NULL */
+       if (msg->num_elements > 0) {
+               DEBUG(10, ("ldb:acl_modify: %s\n", msg->elements[0].name));
+       }
+       if (dsdb_module_am_system(module) || as_system) {
+               return ldb_next_request(module, req);
        }
 
-       if (ares->error != LDB_SUCCESS) {
-               return ldb_module_done(ac->req, ares->controls,
-                                       ares->response, ares->error);
+       tmp_ctx = talloc_new(req);
+       if (tmp_ctx == NULL) {
+               return ldb_oom(ldb);
        }
 
-       switch (ares->type) {
-       case LDB_REPLY_ENTRY:
-               return acl_perform_access_check(req, ares, ac);
-       case LDB_REPLY_REFERRAL:
-               return ldb_module_send_referral(ac->req, ares->referral); /* what to do here actually? */
-       case LDB_REPLY_DONE:
-               if (ac->sec_result != LDB_SUCCESS) {
-                       return ldb_module_done(ac->req, ares->controls,
-                                               ares->response, ac->sec_result);
+       ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, msg->dn,
+                                   acl_attrs,
+                                   DSDB_FLAG_NEXT_MODULE |
+                                   DSDB_FLAG_AS_SYSTEM |
+                                   DSDB_SEARCH_SHOW_RECYCLED,
+                                   req);
+
+       if (ret != LDB_SUCCESS) {
+               goto fail;
+       }
+
+       userPassword = dsdb_user_password_support(module, req, req);
+
+       schema = dsdb_get_schema(ldb, tmp_ctx);
+       if (!schema) {
+               talloc_free(tmp_ctx);
+               return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
+                                "acl_modify: Error obtaining schema.");
+       }
+
+       ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
+                                "acl_modify: Error retrieving security descriptor.");
+       }
+       /* Theoretically we pass the check if the object has no sd */
+       if (!sd) {
+               goto success;
+       }
+
+       guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
+       if (!guid) {
+               talloc_free(tmp_ctx);
+               return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
+                                "acl_modify: Error retrieving object class GUID.");
+       }
+       sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
+       for (i=0; i < msg->num_elements; i++) {
+               const struct ldb_message_element *el = &msg->elements[i];
+               const struct dsdb_attribute *attr;
+
+               /*
+                * This basic attribute existence check with the right errorcode
+                * is needed since this module is the first one which requests
+                * schema attribute information.
+                * The complete attribute checking is done in the
+                * "objectclass_attrs" module behind this one.
+                *
+                * NOTE: "clearTextPassword" is not defined in the schema.
+                */
+               attr = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
+               if (!attr && ldb_attr_cmp("clearTextPassword", el->name) != 0) {
+                       ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' "
+                                              "on entry '%s' was not found in the schema!",
+                                              req->op.mod.message->elements[i].name,
+                                      ldb_dn_get_linearized(req->op.mod.message->dn));
+                       ret =  LDB_ERR_NO_SUCH_ATTRIBUTE;
+                       goto fail;
+               }
+
+               if (ldb_attr_cmp("nTSecurityDescriptor", el->name) == 0) {
+                       uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
+                       uint32_t access_mask = 0;
+
+                       if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
+                               access_mask |= SEC_STD_WRITE_OWNER;
+                       }
+                       if (sd_flags & SECINFO_DACL) {
+                               access_mask |= SEC_STD_WRITE_DAC;
+                       }
+                       if (sd_flags & SECINFO_SACL) {
+                               access_mask |= SEC_FLAG_SYSTEM_SECURITY;
+                       }
+
+                       status = sec_access_check_ds(sd, acl_user_token(module),
+                                            access_mask,
+                                            &access_granted,
+                                            NULL,
+                                            sid);
+
+                       if (!NT_STATUS_IS_OK(status)) {
+                               ldb_asprintf_errstring(ldb_module_get_ctx(module),
+                                                      "Object %s has no write dacl access\n",
+                                                      ldb_dn_get_linearized(msg->dn));
+                               dsdb_acl_debug(sd,
+                                              acl_user_token(module),
+                                              msg->dn,
+                                              true,
+                                              10);
+                               ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
+                               goto fail;
+                       }
+               } else if (ldb_attr_cmp("member", el->name) == 0) {
+                       ret = acl_check_self_membership(tmp_ctx,
+                                                       module,
+                                                       req,
+                                                       sd,
+                                                       sid,
+                                                       guid,
+                                                       attr);
+                       if (ret != LDB_SUCCESS) {
+                               goto fail;
+                       }
+               } else if (ldb_attr_cmp("dBCSPwd", el->name) == 0) {
+                       /* this one is not affected by any rights, we should let it through
+                          so that passwords_hash returns the correct error */
+                       continue;
+               } else if (ldb_attr_cmp("unicodePwd", el->name) == 0 ||
+                          (userPassword && ldb_attr_cmp("userPassword", el->name) == 0) ||
+                          ldb_attr_cmp("clearTextPassword", el->name) == 0) {
+                       ret = acl_check_password_rights(tmp_ctx,
+                                                       module,
+                                                       req,
+                                                       sd,
+                                                       sid,
+                                                       guid,
+                                                       userPassword);
+                       if (ret != LDB_SUCCESS) {
+                               goto fail;
+                       }
+               } else if (ldb_attr_cmp("servicePrincipalName", el->name) == 0) {
+                       ret = acl_check_spn(tmp_ctx,
+                                           module,
+                                           req,
+                                           sd,
+                                           sid,
+                                           guid,
+                                           attr);
+                       if (ret != LDB_SUCCESS) {
+                               goto fail;
+                       }
+               } else {
+                       struct object_tree *root = NULL;
+                       struct object_tree *new_node = NULL;
+
+                       if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
+                                                  &root, &new_node)) {
+                               talloc_free(tmp_ctx);
+                               return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
+                                                "acl_modify: Error adding new node in object tree.");
+                       }
+
+                       if (!insert_in_object_tree(tmp_ctx,
+                                                  &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
+                                                  &new_node, &new_node)) {
+                               ldb_asprintf_errstring(ldb_module_get_ctx(module),
+                                                      "acl_modify: cannot add to object tree securityGUID\n");
+                               ret = LDB_ERR_OPERATIONS_ERROR;
+                               goto fail;
+                       }
+
+                       if (!insert_in_object_tree(tmp_ctx,
+                                                  &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
+                               ldb_asprintf_errstring(ldb_module_get_ctx(module),
+                                                      "acl_modify: cannot add to object tree attributeGUID\n");
+                               ret = LDB_ERR_OPERATIONS_ERROR;
+                               goto fail;
+                       }
+
+                       status = sec_access_check_ds(sd, acl_user_token(module),
+                                                    SEC_ADS_WRITE_PROP,
+                                                    &access_granted,
+                                                    root,
+                                                    sid);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               ldb_asprintf_errstring(ldb_module_get_ctx(module),
+                                                      "Object %s has no write property access\n",
+                                                      ldb_dn_get_linearized(msg->dn));
+                               dsdb_acl_debug(sd,
+                                              acl_user_token(module),
+                                              msg->dn,
+                                              true,
+                                              10);
+                               ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
+                               goto fail;
+                       }
                }
-               return acl_forward_request(ares,ac);
-       default:
-               break;
        }
-       return LDB_SUCCESS;
-}
 
-static enum security_user_level what_is_user(struct ldb_module *module)
-{
-       struct ldb_context *ldb = ldb_module_get_ctx(module);
-       struct auth_session_info *session_info
-               = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
-       return security_session_user_level(session_info);
+success:
+       talloc_free(tmp_ctx);
+       return ldb_next_request(module, req);
+fail:
+       talloc_free(tmp_ctx);
+       return ret;
 }
 
-static struct security_token * user_token(struct ldb_module *module)
+/* similar to the modify for the time being.
+ * We need to consider the special delete tree case, though - TODO */
+static int acl_delete(struct ldb_module *module, struct ldb_request *req)
 {
-       struct ldb_context *ldb = ldb_module_get_ctx(module);
-       struct auth_session_info *session_info
-               = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
-       if(!session_info) {
-               return NULL;
+       int ret;
+       struct ldb_dn *parent;
+       struct ldb_context *ldb;
+       struct ldb_dn *nc_root;
+       struct ldb_control *as_system;
+
+       if (ldb_dn_is_special(req->op.del.dn)) {
+               return ldb_next_request(module, req);
        }
-       return session_info->security_token;
-}
 
+       as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
+       if (as_system != NULL) {
+               as_system->critical = 0;
+       }
 
-static int make_req_access_check(struct ldb_module *module, struct ldb_request *req,
-                                struct acl_context *ac, const char *filter)
-{
-       struct ldb_context *ldb;
-       int ret;
-       const char **attrs = talloc_array(ac, const char *, 3);
-       struct ldb_parse_tree *tree = ldb_parse_tree(req, filter);
+       if (dsdb_module_am_system(module) || as_system) {
+               return ldb_next_request(module, req);
+       }
 
-       attrs[0] = talloc_strdup(attrs, "ntSecurityDescriptor");
-       attrs[1] = talloc_strdup(attrs, "objectClass");
-       attrs[2] = NULL;
+       DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
 
        ldb = ldb_module_get_ctx(module);
-       ret = ldb_build_search_req_ex(&ac->down_req,
-                                     ldb, ac,
-                                     ac->dn_to_check,
-                                     LDB_SCOPE_SUBTREE,
-                                     tree,
-                                     attrs,
-                                     NULL,
-                                     ac, acl_visible_callback,
-                                     req);
-       return ret;
-}
 
-static const char *user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module)
-{
-       struct ldb_context *ldb = ldb_module_get_ctx(module);
-       struct auth_session_info *session_info
-               = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
-       if (!session_info) {
-               return "UNKNOWN (NULL)";
+       parent = ldb_dn_get_parent(req, req->op.del.dn);
+       if (parent == NULL) {
+               return ldb_oom(ldb);
        }
 
-       return talloc_asprintf(mem_ctx, "%s\\%s",
-                              session_info->server_info->domain_name,
-                              session_info->server_info->account_name);
-}
+       /* Make sure we aren't deleting a NC */
 
-static int acl_module_init(struct ldb_module *module)
-{
-       struct ldb_context *ldb;
-       struct acl_private *data;
-       int ret;
+       ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
+               talloc_free(nc_root);
+               DEBUG(10,("acl:deleting a NC\n"));
+               /* Windows returns "ERR_UNWILLING_TO_PERFORM */
+               return ldb_module_done(req, NULL, NULL,
+                                      LDB_ERR_UNWILLING_TO_PERFORM);
+       }
+       talloc_free(nc_root);
+
+       if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID)) {
+               ret = dsdb_module_check_access_on_dn(module, req,
+                                                    req->op.del.dn,
+                                                    SEC_ADS_DELETE_TREE, NULL,
+                                                    req);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
 
-       ldb = ldb_module_get_ctx(module);
+               return ldb_next_request(module, req);
+       }
 
-       ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
-       if (ret != LDB_SUCCESS) {
-               ldb_debug(ldb, LDB_DEBUG_ERROR,
-                         "acl_module_init: Unable to register control with rootdse!\n");
-               return LDB_ERR_OPERATIONS_ERROR;
+       /* First check if we have delete object right */
+       ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn,
+                                            SEC_STD_DELETE, NULL, req);
+       if (ret == LDB_SUCCESS) {
+               return ldb_next_request(module, req);
        }
 
-       data = talloc(module, struct acl_private);
-       data->perform_check = lp_parm_bool(ldb_get_opaque(ldb, "loadparm"),
-                                 NULL, "acl", "perform", false);
-       ldb_module_set_private(module, data);
+       /* Nope, we don't have delete object. Lets check if we have delete
+        * child on the parent */
+       ret = dsdb_module_check_access_on_dn(module, req, parent,
+                                            SEC_ADS_DELETE_CHILD, NULL, req);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
 
-       return ldb_next_init(module);
+       return ldb_next_request(module, req);
 }
 
-static int acl_add(struct ldb_module *module, struct ldb_request *req)
+static int acl_rename(struct ldb_module *module, struct ldb_request *req)
 {
        int ret;
-       struct acl_context *ac;
-       struct ldb_dn * parent = ldb_dn_get_parent(req, req->op.add.message->dn);
-       char * filter;
+       struct ldb_dn *oldparent;
+       struct ldb_dn *newparent;
+       const struct dsdb_schema *schema;
        struct ldb_context *ldb;
-       struct acl_private *data;
-
-       ldb = ldb_module_get_ctx(module);
-       data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
-
-       if (!data->perform_check)
+       struct security_descriptor *sd = NULL;
+       struct dom_sid *sid = NULL;
+       struct ldb_result *acl_res;
+       const struct GUID *guid;
+       struct ldb_dn *nc_root;
+       struct object_tree *root = NULL;
+       struct object_tree *new_node = NULL;
+       struct ldb_control *as_system;
+       TALLOC_CTX *tmp_ctx;
+       NTSTATUS status;
+       uint32_t access_granted;
+       const char *rdn_name;
+       static const char *acl_attrs[] = {
+               "nTSecurityDescriptor",
+               "objectClass",
+               "objectSid",
+               NULL
+       };
+
+       if (ldb_dn_is_special(req->op.rename.olddn)) {
                return ldb_next_request(module, req);
+       }
 
-       ac = talloc(req, struct acl_context);
-       if (ac == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
+       as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
+       if (as_system != NULL) {
+               as_system->critical = 0;
        }
 
-       if (what_is_user(module) == SECURITY_SYSTEM)
+       DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
+       if (dsdb_module_am_system(module) || as_system) {
                return ldb_next_request(module, req);
+       }
 
-       ac->module = module;
-       ac->req = req;
-       ac->ignore_security = true;
-       ac->dn_to_check = ldb_dn_get_parent(req, parent);
-       ac->token = user_token(module);
-       ac->user_type = what_is_user(module);
-       ac->sec_result = LDB_SUCCESS;
-       if (!is_root_base_dn(ldb, req->op.add.message->dn) && parent && !is_root_base_dn(ldb, parent)){
-               filter = talloc_asprintf(req,"(&(objectClass=*)(|(%s=%s)(%s=%s))))",
-                                        ldb_dn_get_component_name(parent,0),
-                                        ldb_dn_get_component_val(parent,0)->data,
-                                        ldb_dn_get_component_name(ac->dn_to_check,0),
-                                        ldb_dn_get_component_val(ac->dn_to_check,0)->data);
-
-               ret = make_req_access_check(module, req, ac, filter);
-               if (ret != LDB_SUCCESS){
-                       return ret;
-               }
-               return ldb_next_request(module, ac->down_req);
+       ldb = ldb_module_get_ctx(module);
+
+       tmp_ctx = talloc_new(req);
+       if (tmp_ctx == NULL) {
+               return ldb_oom(ldb);
        }
-       return ldb_next_request(module, req);
-}
 
-static int acl_modify(struct ldb_module *module, struct ldb_request *req)
-{
-       int ret;
-       struct acl_context *ac;
-       struct ldb_dn * parent = ldb_dn_get_parent(req, req->op.mod.message->dn);
-       char * filter;
-       struct ldb_context *ldb;
-       struct acl_private *data;
+       oldparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.olddn);
+       if (oldparent == NULL) {
+               return ldb_oom(ldb);
+       }
+       newparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.newdn);
+       if (newparent == NULL) {
+               return ldb_oom(ldb);
+       }
 
-       ldb = ldb_module_get_ctx(module);
-       data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
+       /* Make sure we aren't renaming/moving a NC */
 
-       if (!data->perform_check)
-               return ldb_next_request(module, req);
+       ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
+               talloc_free(nc_root);
+               DEBUG(10,("acl:renaming/moving a NC\n"));
+               /* Windows returns "ERR_UNWILLING_TO_PERFORM */
+               return ldb_module_done(req, NULL, NULL,
+                                      LDB_ERR_UNWILLING_TO_PERFORM);
+       }
+       talloc_free(nc_root);
 
-       ac = talloc(req, struct acl_context);
-       if (ac == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
+       /* Look for the parent */
+
+       ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
+                                   req->op.rename.olddn, acl_attrs,
+                                   DSDB_FLAG_NEXT_MODULE |
+                                   DSDB_FLAG_AS_SYSTEM |
+                                   DSDB_SEARCH_SHOW_RECYCLED, req);
+       /* we sould be able to find the parent */
+       if (ret != LDB_SUCCESS) {
+               DEBUG(10,("acl: failed to find object %s\n",
+                         ldb_dn_get_linearized(req->op.rename.olddn)));
+               talloc_free(tmp_ctx);
+               return ret;
        }
 
-/* TODO Is this really right? */
-/*     if (what_is_user(module) == SECURITY_SYSTEM) */
-               return ldb_next_request(module, req);
+       schema = dsdb_get_schema(ldb, acl_res);
+       if (!schema) {
+               talloc_free(tmp_ctx);
+               return ldb_operr(ldb);
+       }
 
-       ac->module = module;
-       ac->req = req;
-       ac->ignore_security = true;
-       ac->dn_to_check = req->op.mod.message->dn;
-       ac->token = user_token(module);
-       ac->user_type = what_is_user(module);
-       ac->sec_result = LDB_SUCCESS;
-       if (!is_root_base_dn(ldb, req->op.mod.message->dn) && parent && !is_root_base_dn(ldb, parent)){
-               filter = talloc_asprintf(req,"(&(objectClass=*)(|(%s=%s)(%s=%s))))",
-                                  ldb_dn_get_component_name(parent,0),
-                                  ldb_dn_get_component_val(parent,0)->data,
-                                  ldb_dn_get_component_name(req->op.mod.message->dn,0),
-                                  ldb_dn_get_component_val(req->op.mod.message->dn,0)->data);
-
-               ret = make_req_access_check(module, req, ac, filter);
-               if (ret != LDB_SUCCESS){
-                       return ret;
-               }
-               return ldb_next_request(module, ac->down_req);
+       guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
+       if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
+                                  &root, &new_node)) {
+               talloc_free(tmp_ctx);
+               return ldb_operr(ldb);
+       };
+
+       guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
+                                                         "name");
+       if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
+                                  &new_node, &new_node)) {
+               talloc_free(tmp_ctx);
+               return ldb_operr(ldb);
+       };
+
+       rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
+       if (rdn_name == NULL) {
+               talloc_free(tmp_ctx);
+               return ldb_operr(ldb);
        }
-       return ldb_next_request(module, req);
-}
+       guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
+                                                         rdn_name);
+       if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
+                                  &new_node, &new_node)) {
+               talloc_free(tmp_ctx);
+               return ldb_operr(ldb);
+       };
 
-/* similar to the modify for the time being.
- * We need to concider the special delete tree case, though - TODO */
-static int acl_delete(struct ldb_module *module, struct ldb_request *req)
-{
-       int ret;
-       struct acl_context *ac;
-       struct ldb_dn * parent = ldb_dn_get_parent(req, req->op.del.dn);
-       char * filter;
-       struct ldb_context *ldb;
-       struct acl_private *data;
+       ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
 
-       ldb = ldb_module_get_ctx(module);
-       data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return ldb_operr(ldb);
+       }
+       /* Theoretically we pass the check if the object has no sd */
+       if (!sd) {
+               talloc_free(tmp_ctx);
+               return LDB_SUCCESS;
+       }
+       sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
+       status = sec_access_check_ds(sd, acl_user_token(module),
+                                    SEC_ADS_WRITE_PROP,
+                                    &access_granted,
+                                    root,
+                                    sid);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               ldb_asprintf_errstring(ldb_module_get_ctx(module),
+                                      "Object %s has no wp on name\n",
+                                      ldb_dn_get_linearized(req->op.rename.olddn));
+               dsdb_acl_debug(sd,
+                         acl_user_token(module),
+                         req->op.rename.olddn,
+                         true,
+                         10);
+               talloc_free(tmp_ctx);
+               return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
+       }
 
-       if (!data->perform_check)
+       if (ldb_dn_compare(oldparent, newparent) == 0) {
+               /* regular rename, not move, nothing more to do */
+               talloc_free(tmp_ctx);
                return ldb_next_request(module, req);
+       }
 
-       ac = talloc(req, struct acl_context);
-       if (ac == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
+       /* new parent should have create child */
+       root = NULL;
+       new_node = NULL;
+       guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
+       if (!guid) {
+               ldb_asprintf_errstring(ldb_module_get_ctx(module),
+                                      "acl:renamed object has no object class\n");
+               talloc_free(tmp_ctx);
+               return ldb_module_done(req, NULL, NULL,  LDB_ERR_OPERATIONS_ERROR);
        }
 
-       if (ac->user_type == SECURITY_SYSTEM)
-               return ldb_next_request(module, req);
+       ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid, req);
+       if (ret != LDB_SUCCESS) {
+               ldb_asprintf_errstring(ldb_module_get_ctx(module),
+                                      "acl:access_denied renaming %s",
+                                      ldb_dn_get_linearized(req->op.rename.olddn));
+               talloc_free(tmp_ctx);
+               return ret;
+       }
+       /* do we have delete object on the object? */
 
-       ac->module = module;
-       ac->req = req;
-       ac->ignore_security = true;
-       ac->dn_to_check = req->op.del.dn;
-       ac->token = user_token(module);
-       ac->user_type = what_is_user(module);
-       ac->sec_result = LDB_SUCCESS;
-       if (parent) {
-               filter = talloc_asprintf(req,"(&(objectClass=*)(|(%s=%s)(%s=%s))))",
-                                        ldb_dn_get_component_name(parent,0),
-                                        ldb_dn_get_component_val(parent,0)->data,
-                                        ldb_dn_get_component_name(req->op.del.dn,0),
-                                        ldb_dn_get_component_val(req->op.del.dn,0)->data);
-               ret = make_req_access_check(module, req, ac, filter);
-
-               if (ret != LDB_SUCCESS){
-                       return ret;
-                }
-               return ldb_next_request(module, ac->down_req);
+       status = sec_access_check_ds(sd, acl_user_token(module),
+                                    SEC_STD_DELETE,
+                                    &access_granted,
+                                    NULL,
+                                    sid);
+
+       if (NT_STATUS_IS_OK(status)) {
+               talloc_free(tmp_ctx);
+               return ldb_next_request(module, req);
+       }
+       /* what about delete child on the current parent */
+       ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL, req);
+       if (ret != LDB_SUCCESS) {
+               ldb_asprintf_errstring(ldb_module_get_ctx(module),
+                                      "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
+               talloc_free(tmp_ctx);
+               return ldb_module_done(req, NULL, NULL, ret);
        }
 
+       talloc_free(tmp_ctx);
+
        return ldb_next_request(module, req);
 }
 
-static int acl_rename(struct ldb_module *module, struct ldb_request *req)
+static int acl_search_update_confidential_attrs(struct acl_context *ac,
+                                               struct acl_private *data)
 {
-       struct ldb_dn *source_parent;
-       struct ldb_dn *dest_parent;
-       int ret;
-       struct acl_context *ac;
-       char * filter;
-       struct ldb_context *ldb;
-       struct acl_private *data;
+       struct dsdb_attribute *a;
+       uint32_t n = 0;
 
-       ldb = ldb_module_get_ctx(module);
-       data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
+       if (data->acl_search) {
+               /*
+                * If acl:search is activated, the acl_read module
+                * protects confidential attributes.
+                */
+               return LDB_SUCCESS;
+       }
 
-       if (!data->perform_check)
-               return ldb_next_request(module, req);
+       if ((ac->schema == data->cached_schema_ptr) &&
+           (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
+           (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
+       {
+               return LDB_SUCCESS;
+       }
 
-       ac = talloc(req, struct acl_context);
-       if (ac == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
+       data->cached_schema_ptr = NULL;
+       data->cached_schema_loaded_usn = 0;
+       data->cached_schema_metadata_usn = 0;
+       TALLOC_FREE(data->confidential_attrs);
+
+       if (ac->schema == NULL) {
+               return LDB_SUCCESS;
        }
 
-       if (ac->user_type == SECURITY_SYSTEM)
-               return ldb_next_request(module, req);
+       for (a = ac->schema->attributes; a; a = a->next) {
+               const char **attrs = data->confidential_attrs;
 
-       ac->module = module;
-       ac->req = req;
-       ac->ignore_security = true;
-       ac->token = user_token(module);
-       ac->user_type = what_is_user(module);
-       ac->sec_result = LDB_SUCCESS;
-
-       /* We need to know if it is a simple rename or a move operation */
-       source_parent = ldb_dn_get_parent(req, req->op.rename.olddn);
-       dest_parent = ldb_dn_get_parent(req, req->op.rename.newdn);
-
-       if (ldb_dn_compare(source_parent, dest_parent) == 0){
-               /*Not a move, just rename*/
-               filter = talloc_asprintf(req,"(&(objectClass=*)(|(%s=%s)(%s=%s))))",
-                                        ldb_dn_get_component_name(dest_parent,0),
-                                        ldb_dn_get_component_val(dest_parent,0)->data,
-                                        ldb_dn_get_component_name(req->op.rename.olddn,0),
-                                        ldb_dn_get_component_val(req->op.rename.olddn,0)->data);
-       }
-       else{
-               filter = talloc_asprintf(req,"(&(objectClass=*)(|(%s=%s)(%s=%s))))",
-                                        ldb_dn_get_component_name(dest_parent,0),
-                                        ldb_dn_get_component_val(dest_parent,0)->data,
-                                        ldb_dn_get_component_name(source_parent,0),
-                                        ldb_dn_get_component_val(source_parent,0)->data);
-       }
-
-       ret = make_req_access_check(module, req, ac, filter);
-
-       if (ret != LDB_SUCCESS){
-               return ret;
+               if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
+                       continue;
+               }
+
+               attrs = talloc_realloc(data, attrs, const char *, n + 2);
+               if (attrs == NULL) {
+                       TALLOC_FREE(data->confidential_attrs);
+                       return ldb_module_oom(ac->module);
                }
-       return ldb_next_request(module, ac->down_req);
+
+               attrs[n] = a->lDAPDisplayName;
+               attrs[n+1] = NULL;
+               n++;
+
+               data->confidential_attrs = attrs;
+       }
+
+       data->cached_schema_ptr = ac->schema;
+       data->cached_schema_loaded_usn = ac->schema->loaded_usn;
+       data->cached_schema_metadata_usn = ac->schema->metadata_usn;
+
+       return LDB_SUCCESS;
 }
 
 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
-       struct ldb_context *ldb;
        struct acl_context *ac;
-       struct security_descriptor *sd;
-       uint32_t searchFlags;
-       uint32_t access_mask;
-       struct object_tree *ot;
-       int i, ret;
-       NTSTATUS status;
-       struct ldb_message_element *element_security_descriptor;
-       struct ldb_message_element *element_object_class;
-       const struct dsdb_attribute *attr;
-       const struct dsdb_schema *schema;
-       struct GUID *oc_guid;
+       struct acl_private *data;
+       struct ldb_result *acl_res;
+       static const char *acl_attrs[] = {
+               "objectClass",
+               "nTSecurityDescriptor",
+               "objectSid",
+               NULL
+       };
+       int ret;
+       unsigned int i;
 
        ac = talloc_get_type(req->context, struct acl_context);
-       ldb = ldb_module_get_ctx(ac->module);
-       schema = dsdb_get_schema(ldb);
+       data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
        if (!ares) {
-               return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
+               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);
+               return ldb_module_done(ac->req, ares->controls,
+                                      ares->response, ares->error);
        }
 
        switch (ares->type) {
        case LDB_REPLY_ENTRY:
-               switch (ac->user_type) {
-               case SECURITY_SYSTEM:
-               case SECURITY_ANONYMOUS:/*FIXME: should we let anonymous have system access*/
-                       break;
-               default:
-                       /* Access checks
-                        *
-                        * 0. If we do not have nTSecurityDescriptor, we do not have an object in the response,
-                        *    so check the parent dn.
-                        * 1. Call sec_access_check on empty tree
-                        * 2. For each attribute call extended_access_check
-                        * 3. For each attribute call build_object_tree_form_attr_list and then check with sec_access_check
-                        *
-                        */
-                       element_security_descriptor = ldb_msg_find_element(ares->message, "nTSecurityDescriptor");
-                       element_object_class = ldb_msg_find_element(ares->message, "objectClass");
-                       if (!element_security_descriptor || !element_object_class)
-                               break;
+               if (ac->constructed_attrs) {
+                       ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn, 
+                                                   acl_attrs,
+                                                   DSDB_FLAG_NEXT_MODULE |
+                                                   DSDB_FLAG_AS_SYSTEM |
+                                                   DSDB_SEARCH_SHOW_RECYCLED,
+                                                   req);
+                       if (ret != LDB_SUCCESS) {
+                               return ldb_module_done(ac->req, NULL, NULL, ret);
+                       }
+               }
 
-                       sd = talloc(ldb, struct security_descriptor);
-                       if(!sd) {
-                         return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
+               if (ac->allowedAttributes || ac->allowedAttributesEffective) {
+                       ret = acl_allowedAttributes(ac->module, ac->schema,
+                                                   acl_res->msgs[0],
+                                                   ares->message, ac);
+                       if (ret != LDB_SUCCESS) {
+                               return ldb_module_done(ac->req, NULL, NULL, ret);
                        }
-                       if(!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&element_security_descriptor->values[0],
-                                                                        ldb,
-                                                                        NULL,
-                                                                        sd,
-                                                                        (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
-                               DEBUG(0, ("acl_search_callback: Error parsing security descriptor\n"));
-                               return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
+               }
+
+               if (ac->allowedChildClasses) {
+                       ret = acl_childClasses(ac->module, ac->schema,
+                                              acl_res->msgs[0],
+                                              ares->message,
+                                              "allowedChildClasses");
+                       if (ret != LDB_SUCCESS) {
+                               return ldb_module_done(ac->req, NULL, NULL, ret);
                        }
+               }
 
-                       oc_guid = class_schemaid_guid_by_lDAPDisplayName(schema, element_object_class->values[0].data);
-                       for (i=0; i<ares->message->num_elements; i++) {
-                               attr = dsdb_attribute_by_lDAPDisplayName(schema, ares->message->elements[i].name);
-                               if (attr) {
-                                       searchFlags = attr->searchFlags;
-                               } else {
-                                       searchFlags = 0x0;
-                               }
+               if (ac->allowedChildClassesEffective) {
+                       ret = acl_childClassesEffective(ac->module, ac->schema,
+                                                       acl_res->msgs[0],
+                                                       ares->message, ac);
+                       if (ret != LDB_SUCCESS) {
+                               return ldb_module_done(ac->req, NULL, NULL, ret);
+                       }
+               }
 
-                               /*status = extended_access_check(ares->message->elements[i].name, access_mask, searchFlags); */ /* Todo FIXME */
-                               ac->access_needed = SEC_ADS_READ_PROP;
-                               if (NT_STATUS_IS_OK(status)) {
-                                       ot = insert_in_object_tree(req, oc_guid, NULL, ac->access_needed, NULL);
+               if (ac->sDRightsEffective) {
+                       ret = acl_sDRightsEffective(ac->module,
+                                                   acl_res->msgs[0],
+                                                   ares->message, ac);
+                       if (ret != LDB_SUCCESS) {
+                               return ldb_module_done(ac->req, NULL, NULL, ret);
+                       }
+               }
 
-                                       insert_in_object_tree(req,
-                                                             &attr->schemaIDGUID,
-                                                             &attr->attributeSecurityGUID,
-                                                             ac->access_needed,
-                                                             ot);
+               if (data == NULL) {
+                       return ldb_module_send_entry(ac->req, ares->message,
+                                                    ares->controls);
+               }
 
-                                       status = sec_access_check_ds(sd,
-                                                                    ac->token,
-                                                                    ac->access_needed,
-                                                                    &access_mask,
-                                                                    ot);
+               if (ac->am_system) {
+                       return ldb_module_send_entry(ac->req, ares->message,
+                                                    ares->controls);
+               }
 
-                                       if (NT_STATUS_IS_OK(status)) {
+               if (data->password_attrs != NULL) {
+                       for (i = 0; data->password_attrs[i]; i++) {
+                               if ((!ac->userPassword) &&
+                                   (ldb_attr_cmp(data->password_attrs[i],
+                                                 "userPassword") == 0))
+                               {
                                                continue;
-                                       }
                                }
-                               ldb_msg_remove_attr(ares->message, ares->message->elements[i].name);
+
+                               ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
                        }
-                       break;
                }
-               if (ac->nTSecurityDescriptor) {
-                       ldb_msg_remove_attr(ares->message, "nTSecurityDescriptor");
-               } else if (ac->objectClass) {
-                       ldb_msg_remove_attr(ares->message, "objectClass");
+
+               if (ac->am_administrator) {
+                       return ldb_module_send_entry(ac->req, ares->message,
+                                                    ares->controls);
+               }
+
+               ret = acl_search_update_confidential_attrs(ac, data);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+
+               if (data->confidential_attrs != NULL) {
+                       for (i = 0; data->confidential_attrs[i]; i++) {
+                               ldb_msg_remove_attr(ares->message,
+                                                   data->confidential_attrs[i]);
+                       }
                }
 
                return ldb_module_send_entry(ac->req, ares->message, ares->controls);
+
        case LDB_REPLY_REFERRAL:
                return ldb_module_send_referral(ac->req, ares->referral);
 
        case LDB_REPLY_DONE:
-               return ldb_module_done(ac->req, ares->controls,ares->response, LDB_SUCCESS);
-       }
+               return ldb_module_done(ac->req, ares->controls,
+                                      ares->response, LDB_SUCCESS);
 
-        return LDB_SUCCESS;
+       }
+       return LDB_SUCCESS;
 }
 
 static int acl_search(struct ldb_module *module, struct ldb_request *req)
 {
-       int ret;
        struct ldb_context *ldb;
        struct acl_context *ac;
-       const char **attrs;
-       struct ldb_control *sd_control;
-       struct ldb_control **sd_saved_controls;
-       struct ldb_dn * parent;
+       struct ldb_parse_tree *down_tree;
+       struct ldb_request *down_req;
        struct acl_private *data;
+       int ret;
+       unsigned int i;
+
+       if (ldb_dn_is_special(req->op.search.base)) {
+               return ldb_next_request(module, req);
+       }
 
        ldb = ldb_module_get_ctx(module);
+
+       ac = talloc_zero(req, struct acl_context);
+       if (ac == NULL) {
+               return ldb_oom(ldb);
+       }
        data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
 
-       if (!data || !data->perform_check)
-               return ldb_next_request(module, req);
+       ac->module = module;
+       ac->req = req;
+       ac->am_system = dsdb_module_am_system(module);
+       ac->am_administrator = dsdb_module_am_administrator(module);
+       ac->constructed_attrs = false;
+       ac->modify_search = true;
+       ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
+       ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
+       ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
+       ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
+       ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
+       ac->userPassword = true;
+       ac->schema = dsdb_get_schema(ldb, ac);
+
+       ac->constructed_attrs |= ac->allowedAttributes;
+       ac->constructed_attrs |= ac->allowedChildClasses;
+       ac->constructed_attrs |= ac->allowedChildClassesEffective;
+       ac->constructed_attrs |= ac->allowedAttributesEffective;
+       ac->constructed_attrs |= ac->sDRightsEffective;
+
+       if (data == NULL) {
+               ac->modify_search = false;
+       }
+       if (ac->am_system) {
+               ac->modify_search = false;
+       }
 
-       if (what_is_user(module) == SECURITY_SYSTEM)
+       if (!ac->constructed_attrs && !ac->modify_search) {
+               talloc_free(ac);
                return ldb_next_request(module, req);
+       }
 
-       ac = talloc_get_type(req->context, struct acl_context);
-       if ( ac == NULL ) {
-               ac = talloc(req, struct acl_context);
-               if (ac == NULL) {
-                       ldb_oom(ldb);
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
-               ac->module = module;
-               ac->req = req;
-               ac->ignore_security = false;
-               ac->user_type = what_is_user(module);
-               ac->token = user_token(module);
-               ac->dn_to_check = req->op.search.base;
-               ac->sec_result = LDB_SUCCESS;
-
-               attrs = talloc_array(ac, const char*, 2);
-               attrs[0] = talloc_strdup(attrs, "nTSecurityDescriptor");
-               attrs[1] = NULL;
-               parent = ldb_dn_get_parent(req, ac->dn_to_check);
-               if (!is_root_base_dn(ldb, req->op.search.base) && parent && !is_root_base_dn(ldb, parent)) {
-                       /*we have parent so check for visibility*/
-                       ret = ldb_build_search_req(&ac->down_req,
-                                                  ldb, ac,
-                                                  parent,
-                                                  LDB_SCOPE_BASE,
-                                                  "(objectClass=*)",
-                                                  attrs,
-                                                  req->controls,
-                                                  ac, acl_visible_callback,
-                                                  req);
-                       if (ret != LDB_SUCCESS) {
-                               return ret;
+       if (!ac->am_system) {
+               ac->userPassword = dsdb_user_password_support(module, ac, req);
+       }
+
+       ret = acl_search_update_confidential_attrs(ac, data);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+
+       down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
+       if (down_tree == NULL) {
+               return ldb_oom(ldb);
+       }
+
+       if (!ac->am_system && data->password_attrs) {
+               for (i = 0; data->password_attrs[i]; i++) {
+                       if ((!ac->userPassword) &&
+                           (ldb_attr_cmp(data->password_attrs[i],
+                                         "userPassword") == 0))
+                       {
+                               continue;
                        }
-                       return ldb_next_request(module, ac->down_req);
-               } else {
-                       return acl_forward_search(ac);
+
+                       ldb_parse_tree_attr_replace(down_tree,
+                                                   data->password_attrs[i],
+                                                   "kludgeACLredactedattribute");
                }
        }
 
-       return ldb_next_request(module, req);
+       if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
+               for (i = 0; data->confidential_attrs[i]; i++) {
+                       ldb_parse_tree_attr_replace(down_tree,
+                                                   data->confidential_attrs[i],
+                                                   "kludgeACLredactedattribute");
+               }
+       }
+
+       ret = ldb_build_search_req_ex(&down_req,
+                                     ldb, ac,
+                                     req->op.search.base,
+                                     req->op.search.scope,
+                                     down_tree,
+                                     req->op.search.attrs,
+                                     req->controls,
+                                     ac, acl_search_callback,
+                                     req);
+       LDB_REQ_SET_LOCATION(down_req);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       /* perform the search */
+       return ldb_next_request(module, down_req);
 }
 
 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
 {
        struct ldb_context *ldb = ldb_module_get_ctx(module);
-       enum security_user_level user_type;
-       struct acl_private *data;
-
-       data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
-
-       if (!data->perform_check)
-               return ldb_next_request(module, req);
+       struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
 
        /* allow everybody to read the sequence number */
-       if (strcmp(req->op.extended.oid, LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
+       if (strcmp(req->op.extended.oid,
+                  LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
                return ldb_next_request(module, req);
        }
 
-       user_type = what_is_user(module);
-       switch (user_type) {
-       case SECURITY_SYSTEM:
-       case SECURITY_ADMINISTRATOR:
+       if (dsdb_module_am_system(module) ||
+           dsdb_module_am_administrator(module) || as_system) {
                return ldb_next_request(module, req);
-       default:
+       } else {
                ldb_asprintf_errstring(ldb,
-                                      "acl_extended: attempted database modify not permitted."
-                                      "User %s is not SYSTEM or an Administrator",
-                                      user_name(req, module));
+                                      "acl_extended: "
+                                      "attempted database modify not permitted. "
+                                      "User %s is not SYSTEM or an administrator",
+                                      acl_user_name(req, module));
                return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
        }
 }
 
-_PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
+static const struct ldb_module_ops ldb_acl_module_ops = {
        .name              = "acl",
        .search            = acl_search,
        .add               = acl_add,
@@ -1150,3 +1825,9 @@ _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
        .extended          = acl_extended,
        .init_context      = acl_module_init
 };
+
+int ldb_acl_module_init(const char *version)
+{
+       LDB_MODULE_CHECK_VERSION(version);
+       return ldb_register_module(&ldb_acl_module_ops);
+}