s4:dsdb/acl_read: enable acl checking on search by default (bug #8620)
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / acl_read.c
index 3a0f6f34eb2983a9d5cff7316129def260e2e3a0..92744f28ba7701cade4eb0f1110ab5a6e433d1c3 100644 (file)
@@ -44,146 +44,224 @@ struct aclread_context {
        struct ldb_request *req;
        const char * const *attrs;
        const struct dsdb_schema *schema;
+       uint32_t sd_flags;
+       bool sd;
+       bool instance_type;
+       bool object_sid;
+       bool indirsync;
 };
 
 struct aclread_private {
        bool enabled;
 };
 
+static void aclread_mark_inaccesslible(struct ldb_message_element *el) {
+       el->flags |= LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE;
+}
+
+static bool aclread_is_inaccessible(struct ldb_message_element *el) {
+       return el->flags & LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE;
+}
+
 static int aclread_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
-        struct ldb_context *ldb;
-        struct aclread_context *ac;
-        struct ldb_result *acl_res;
-        struct ldb_message_element *parent;
-        static const char *acl_attrs[] = {
-                "nTSecurityDescriptor",
-                "objectSid",
-                "parentGUID",
-                NULL
-        };
-        int ret;
-        unsigned int i;
-        struct security_descriptor *sd;
-        struct dom_sid *sid = NULL;
-        TALLOC_CTX *tmp_ctx;
-        ac = talloc_get_type(req->context, struct aclread_context);
-        ldb = ldb_module_get_ctx(ac->module);
-        if (!ares) {
-                return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR );
-        }
-        if (ares->error != LDB_SUCCESS) {
-                return ldb_module_done(ac->req, ares->controls,
-                                       ares->response, ares->error);
-        }
-        tmp_ctx = talloc_new(ac);
-        switch (ares->type) {
-        case LDB_REPLY_ENTRY:
-                ret = dsdb_module_search_dn(ac->module, tmp_ctx, &acl_res, ares->message->dn,
-                                            acl_attrs,
-                                            DSDB_FLAG_NEXT_MODULE |
-                                            DSDB_SEARCH_SHOW_DELETED);
-                if (ret != LDB_SUCCESS) {
-                        goto fail;
-                }
-                ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
-                if (ret != LDB_SUCCESS) {
-                        DEBUG(10, ("acl_read: cannot get descriptor\n"));
-                        ret = LDB_ERR_OPERATIONS_ERROR;
-                        goto fail;
-                }
-                sid = samdb_result_dom_sid(tmp_ctx, acl_res->msgs[0], "objectSid");
-                /* get the parent guid */
-                parent = ldb_msg_find_element(acl_res->msgs[0], "parentGUID");
-                if (parent) {
-                        /* the object has a parent, so we have to check for visibility */
-                        struct GUID parent_guid = samdb_result_guid(acl_res->msgs[0], "parentGUID");
-                        ret = dsdb_module_check_access_on_guid(ac->module,
-                                                               tmp_ctx,
-                                                               &parent_guid,
-                                                               SEC_ADS_LIST,
-                                                               NULL);
-                        if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
-                                talloc_free(tmp_ctx);
-                                return LDB_SUCCESS;
-                        } else if (ret != LDB_SUCCESS) {
-                                goto fail;
-                        }
-                }
-                /* for every element in the message check RP */
-                i = 0;
-                while (i < ares->message->num_elements) {
-                        char *p, *new_attr;
-                        const struct dsdb_attribute *attr;
-                        p = strchr(ares->message->elements[i].name, ';');
-                        if (!p) {
-                                attr =  dsdb_attribute_by_lDAPDisplayName(ac->schema,
-                                                                          ares->message->elements[i].name);
-                        } else {
-                                new_attr = talloc_strndup(tmp_ctx,
-                                                          ares->message->elements[i].name,
-                                                          (size_t)(p -ares->message->elements[i].name));
-                                if (!new_attr) {
-                                        ldb_oom(ldb);
-                                        ret = LDB_ERR_OPERATIONS_ERROR;
-                                        goto fail;
-                                }
-                                attr =  dsdb_attribute_by_lDAPDisplayName(ac->schema,
-                                                                          new_attr);
-                                talloc_free(new_attr);
-                        }
-
-                        if (!attr) {
-                                DEBUG(2, ("acl_read: cannot find attribute %s in schema\n",
-                                          ares->message->elements[i].name));
-                                ret = LDB_ERR_OPERATIONS_ERROR;
-                                goto fail;
-                        }
-                        /* nTSecurityDescriptor is a special case */
-                        if (ldb_attr_cmp("nTSecurityDescriptor",
-                                         ares->message->elements[i].name) == 0) {
-                                ret = acl_check_access_on_attribute(ac->module,
-                                                                    tmp_ctx,
-                                                                    sd,
-                                                                    sid,
-                                                                    SEC_FLAG_SYSTEM_SECURITY|SEC_STD_READ_CONTROL,
-                                                                    attr);
-                        } else {
-                                ret = acl_check_access_on_attribute(ac->module,
-                                                                    tmp_ctx,
-                                                                    sd,
-                                                                    sid,
-                                                                    SEC_ADS_READ_PROP,
-                                                                    attr);
-                        }
-                        if (ret == LDB_SUCCESS) {
-                                i++;
-                        } else if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
-                                /* do not return this entry if attribute is
-                                           part of the search filter */
-                                if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
-                                                            ares->message->elements[i].name)) {
-                                        talloc_free(tmp_ctx);
-                                        return LDB_SUCCESS;
-                                }
-                                ldb_msg_remove_attr(ares->message, ares->message->elements[i].name);
-                        } else {
-                                goto fail;
-                        }
-                }
-                talloc_free(tmp_ctx);
-                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,
+       struct ldb_context *ldb;
+       struct aclread_context *ac;
+       struct ldb_message *ret_msg;
+       struct ldb_message *msg;
+       int ret, num_of_attrs = 0;
+       unsigned int i, k = 0;
+       struct security_descriptor *sd;
+       struct dom_sid *sid = NULL;
+       TALLOC_CTX *tmp_ctx;
+       uint32_t instanceType;
+
+       ac = talloc_get_type(req->context, struct aclread_context);
+       ldb = ldb_module_get_ctx(ac->module);
+       if (!ares) {
+               return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR );
+       }
+       if (ares->error != LDB_SUCCESS) {
+               return ldb_module_done(ac->req, ares->controls,
+                                      ares->response, ares->error);
+       }
+       tmp_ctx = talloc_new(ac);
+       switch (ares->type) {
+       case LDB_REPLY_ENTRY:
+               msg = ares->message;
+               ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, msg, &sd);
+               if (ret != LDB_SUCCESS || sd == NULL ) {
+                       DEBUG(10, ("acl_read: cannot get descriptor\n"));
+                       ret = LDB_ERR_OPERATIONS_ERROR;
+                       goto fail;
+               }
+               sid = samdb_result_dom_sid(tmp_ctx, msg, "objectSid");
+               /* get the object instance type */
+               instanceType = ldb_msg_find_attr_as_uint(msg,
+                                                        "instanceType", 0);
+               if (!ldb_dn_is_null(msg->dn) && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
+               {
+                       /* the object has a parent, so we have to check for visibility */
+                       struct ldb_dn *parent_dn = ldb_dn_get_parent(tmp_ctx, msg->dn);
+
+                       ret = dsdb_module_check_access_on_dn(ac->module,
+                                                            tmp_ctx,
+                                                            parent_dn,
+                                                            SEC_ADS_LIST,
+                                                            NULL, req);
+                       if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
+                               talloc_free(tmp_ctx);
+                               return LDB_SUCCESS;
+                       } else if (ret != LDB_SUCCESS) {
+                               goto fail;
+                       }
+               }
+               /* for every element in the message check RP */
+               for (i=0; i < msg->num_elements; i++) {
+                       const struct dsdb_attribute *attr;
+                       bool is_sd, is_objectsid, is_instancetype;
+                       uint32_t access_mask;
+                       attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
+                                                                msg->elements[i].name);
+                       if (!attr) {
+                               DEBUG(2, ("acl_read: cannot find attribute %s in schema\n",
+                                          msg->elements[i].name));
+                               ret = LDB_ERR_OPERATIONS_ERROR;
+                               goto fail;
+                       }
+                       is_sd = ldb_attr_cmp("nTSecurityDescriptor",
+                                             msg->elements[i].name) == 0;
+                       is_objectsid = ldb_attr_cmp("objectSid",
+                                                   msg->elements[i].name) == 0;
+                       is_instancetype = ldb_attr_cmp("instanceType",
+                                                      msg->elements[i].name) == 0;
+                       /* these attributes were added to perform access checks and must be removed */
+                       if (is_objectsid && ac->object_sid) {
+                               aclread_mark_inaccesslible(&msg->elements[i]);
+                               continue;
+                       }
+                       if (is_instancetype && ac->instance_type) {
+                               aclread_mark_inaccesslible(&msg->elements[i]);
+                               continue;
+                       }
+                       if (is_sd && ac->sd) {
+                               aclread_mark_inaccesslible(&msg->elements[i]);
+                               continue;
+                       }
+                       /* nTSecurityDescriptor is a special case */
+                       if (is_sd) {
+                               access_mask = 0;
+
+                               if (ac->sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
+                                       access_mask |= SEC_STD_READ_CONTROL;
+                               }
+                               if (ac->sd_flags & SECINFO_DACL) {
+                                       access_mask |= SEC_STD_READ_CONTROL;
+                               }
+                               if (ac->sd_flags & SECINFO_SACL) {
+                                       access_mask |= SEC_FLAG_SYSTEM_SECURITY;
+                               }
+                       } else {
+                               access_mask = SEC_ADS_READ_PROP;
+                       }
+
+                       if (attr->searchFlags & SEARCH_FLAG_CONFIDENTIAL) {
+                               access_mask |= SEC_ADS_CONTROL_ACCESS;
+                       }
+
+                       if (access_mask == 0) {
+                               aclread_mark_inaccesslible(&msg->elements[i]);
+                               continue;
+                       }
+
+                       ret = acl_check_access_on_attribute(ac->module,
+                                                           tmp_ctx,
+                                                           sd,
+                                                           sid,
+                                                           access_mask,
+                                                           attr);
+
+                       /*
+                        * Dirsync control needs the replpropertymetadata attribute
+                        * so return it as it will be removed by the control
+                        * in anycase.
+                        */
+                       if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
+                               if (!ac->indirsync) {
+                                       /*
+                                        * do not return this entry if attribute is
+                                        * part of the search filter
+                                        */
+                                       if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
+                                                               msg->elements[i].name)) {
+                                               talloc_free(tmp_ctx);
+                                               return LDB_SUCCESS;
+                                       }
+                                       aclread_mark_inaccesslible(&msg->elements[i]);
+                               } else {
+                                       /*
+                                        * We are doing dirysnc answers
+                                        * and the object shouldn't be returned (normally)
+                                        * but we will return it without replPropertyMetaData
+                                        * so that the dirysync module will do what is needed
+                                        * (remove the object if it is not deleted, or return
+                                        * just the objectGUID if it's deleted).
+                                        */
+                                       if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
+                                                               msg->elements[i].name)) {
+                                               ldb_msg_remove_attr(msg, "replPropertyMetaData");
+                                               break;
+                                       } else {
+                                               aclread_mark_inaccesslible(&msg->elements[i]);
+                                       }
+                               }
+                       } else if (ret != LDB_SUCCESS) {
+                               goto fail;
+                       }
+               }
+               for (i=0; i < msg->num_elements; i++) {
+                       if (!aclread_is_inaccessible(&msg->elements[i])) {
+                               num_of_attrs++;
+                       }
+               }
+               /*create a new message to return*/
+               ret_msg = ldb_msg_new(ac->req);
+               ret_msg->dn = msg->dn;
+               talloc_steal(ret_msg, msg->dn);
+               ret_msg->num_elements = num_of_attrs;
+               if (num_of_attrs > 0) {
+                       ret_msg->elements = talloc_array(ret_msg,
+                                                        struct ldb_message_element,
+                                                        num_of_attrs);
+                       if (ret_msg->elements == NULL) {
+                               return ldb_oom(ldb);
+                       }
+                       for (i=0; i < msg->num_elements; i++) {
+                               bool to_remove = aclread_is_inaccessible(&msg->elements[i]);
+                               if (!to_remove) {
+                                       ret_msg->elements[k] = msg->elements[i];
+                                       talloc_steal(ret_msg->elements, msg->elements[i].name);
+                                       talloc_steal(ret_msg->elements, msg->elements[i].values);
+                                       k++;
+                               }
+                       }
+               } else {
+                       ret_msg->elements = NULL;
+               }
+               talloc_free(tmp_ctx);
+
+               return ldb_module_send_entry(ac->req, ret_msg, 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_SUCCESS;
+       }
+       return LDB_SUCCESS;
 fail:
-        talloc_free(tmp_ctx);
-        return ldb_module_done(ac->req, NULL, NULL, ret);
+       talloc_free(tmp_ctx);
+       return ldb_module_done(ac->req, NULL, NULL, ret);
 }
 
 
@@ -191,18 +269,18 @@ static int aclread_search(struct ldb_module *module, struct ldb_request *req)
 {
        struct ldb_context *ldb;
        int ret;
-       bool block_anonymous;
        struct aclread_context *ac;
        struct ldb_request *down_req;
        struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
-       struct auth_session_info *session_info;
+       uint32_t flags = ldb_req_get_custom_flags(req);
        struct ldb_result *res;
-       struct ldb_message_element *parent;
        struct aclread_private *p;
        bool is_untrusted = ldb_req_is_untrusted(req);
+       const char * const *attrs = NULL;
+       uint32_t instanceType;
        static const char *acl_attrs[] = {
-                "parentGUID",
-                NULL
+               "instanceType",
+               NULL
        };
 
        ldb = ldb_module_get_ctx(module);
@@ -219,40 +297,30 @@ static int aclread_search(struct ldb_module *module, struct ldb_request *req)
        if (ldb_dn_is_special(req->op.search.base)) {
                return ldb_next_request(module, req);
        }
-       /* allow all access to rootDSE */
-       if (req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base)) {
-               return ldb_next_request(module, req);
-       }
-
-       session_info = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
-       if (session_info && security_token_is_anonymous(session_info->security_token)) {
-               block_anonymous = dsdb_block_anonymous_ops(module);
-               if (block_anonymous) {
-                       return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
-                                        "This request is not allowed to an anonymous connection.");
-               }
-       }
 
        /* check accessibility of base */
        if (!ldb_dn_is_null(req->op.search.base)) {
                ret = dsdb_module_search_dn(module, req, &res, req->op.search.base,
                                            acl_attrs,
                                            DSDB_FLAG_NEXT_MODULE |
-                                           DSDB_SEARCH_SHOW_DELETED);
+                                           DSDB_FLAG_AS_SYSTEM |
+                                           DSDB_SEARCH_SHOW_RECYCLED,
+                                           req);
                if (ret != LDB_SUCCESS) {
                        return ldb_error(ldb, ret,
-                                        "acl_read: Error retrieving SD for base.");
+                                       "acl_read: Error retrieving instanceType for base.");
                }
-
-               parent = ldb_msg_find_element(res->msgs[0], "parentGUID");
-               if (parent) {
+               instanceType = ldb_msg_find_attr_as_uint(res->msgs[0],
+                                                       "instanceType", 0);
+               if (instanceType != 0 && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
+               {
                        /* the object has a parent, so we have to check for visibility */
-                       struct GUID parent_guid = samdb_result_guid(res->msgs[0], "parentGUID");
-                       ret = dsdb_module_check_access_on_guid(module,
-                                                              req,
-                                                              &parent_guid,
-                                                              SEC_ADS_LIST,
-                                                              NULL);
+                       struct ldb_dn *parent_dn = ldb_dn_get_parent(req, req->op.search.base);
+                       ret = dsdb_module_check_access_on_dn(module,
+                                                            req,
+                                                            parent_dn,
+                                                            SEC_ADS_LIST,
+                                                            NULL, req);
                        if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
                                return ldb_module_done(req, NULL, NULL, LDB_ERR_NO_SUCH_OBJECT);
                        } else if (ret != LDB_SUCCESS) {
@@ -267,16 +335,50 @@ static int aclread_search(struct ldb_module *module, struct ldb_request *req)
        ac->module = module;
        ac->req = req;
        ac->schema = dsdb_get_schema(ldb, req);
+       if (flags & DSDB_ACL_CHECKS_DIRSYNC_FLAG) {
+               ac->indirsync = true;
+       } else {
+               ac->indirsync = false;
+       }
        if (!ac->schema) {
                return ldb_operr(ldb);
        }
+       /*
+        * In theory we should also check for the SD control but control verification is
+        * expensive so we'd better had the ntsecuritydescriptor to the list of
+        * searched attribute and then remove it !
+        */
+       ac->sd_flags = dsdb_request_sd_flags(ac->req, NULL);
 
+       ac->sd = !(ldb_attr_in_list(req->op.search.attrs, "nTSecurityDescriptor"));
+       if (req->op.search.attrs && !ldb_attr_in_list(req->op.search.attrs, "*")) {
+               if (!ldb_attr_in_list(req->op.search.attrs, "instanceType")) {
+                       ac->instance_type = true;
+                       attrs = ldb_attr_list_copy_add(ac, req->op.search.attrs, "instanceType");
+               } else {
+                       attrs = req->op.search.attrs;
+               }
+               if (!ldb_attr_in_list(req->op.search.attrs, "objectSid")) {
+                       ac->object_sid = true;
+                       attrs = ldb_attr_list_copy_add(ac, attrs, "objectSid");
+               }
+       }
+
+       if (ac->sd) {
+               /* avoid replacing all attributes with nTSecurityDescriptor
+                * if attribute list is empty */
+               if (!attrs) {
+                       attrs = ldb_attr_list_copy_add(ac, req->op.search.attrs, "*");
+               }
+               attrs = ldb_attr_list_copy_add(ac, attrs, "nTSecurityDescriptor");
+       }
+       ac->attrs = req->op.search.attrs;
        ret = ldb_build_search_req_ex(&down_req,
                                      ldb, ac,
                                      req->op.search.base,
                                      req->op.search.scope,
                                      req->op.search.tree,
-                                     req->op.search.attrs,
+                                     attrs,
                                      req->controls,
                                      ac, aclread_callback,
                                      req);
@@ -295,7 +397,7 @@ static int aclread_init(struct ldb_module *module)
        if (p == NULL) {
                return ldb_module_oom(module);
        }
-       p->enabled = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"), NULL, "acl", "search", false);
+       p->enabled = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"), NULL, "acl", "search", true);
        ldb_module_set_private(module, p);
        return ldb_next_init(module);
 }