4 Copyright (C) Simo Sorce 2006-2008
5 Copyright (C) Nadezhda Ivanova 2009
6 Copyright (C) Anatoliy Atanasov 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Component: ldb ACL module
27 * Description: Module that performs authorisation access checks based on the
28 * account's security context and the DACL of the object being polled.
29 * Only DACL checks implemented at this point
31 * Authors: Nadezhda Ivanova, Anatoliy Atanasov
35 #include "ldb_module.h"
36 #include "auth/auth.h"
37 #include "libcli/security/security.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "librpc/gen_ndr/ndr_security.h"
40 #include "param/param.h"
41 #include "dsdb/samdb/ldb_modules/util.h"
42 #include "lib/util/tsort.h"
44 struct extended_access_check_attribute {
46 const uint32_t requires_rights;
51 const char **password_attrs;
55 struct ldb_module *module;
56 struct ldb_request *req;
58 bool allowedAttributes;
59 bool allowedAttributesEffective;
60 bool allowedChildClasses;
61 bool allowedChildClassesEffective;
62 bool sDRightsEffective;
63 const char * const *attrs;
64 struct dsdb_schema *schema;
67 bool is_root_base_dn(struct ldb_context *ldb, struct ldb_dn *dn_to_check)
70 struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
71 result = ldb_dn_compare(root_base_dn,dn_to_check);
75 static struct security_token *acl_user_token(struct ldb_module *module)
77 struct ldb_context *ldb = ldb_module_get_ctx(module);
78 struct auth_session_info *session_info
79 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
83 return session_info->security_token;
86 /* performs an access check from inside the module stack
87 * given the dn of the object to be checked, the required access
88 * guid is either the guid of the extended right, or NULL
91 int dsdb_module_check_access_on_dn(struct ldb_module *module,
95 const struct GUID *guid)
98 struct ldb_result *acl_res;
99 static const char *acl_attrs[] = {
100 "nTSecurityDescriptor",
104 struct ldb_context *ldb = ldb_module_get_ctx(module);
105 struct auth_session_info *session_info
106 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
108 return LDB_ERR_OPERATIONS_ERROR;
110 ret = dsdb_module_search_dn(module, mem_ctx, &acl_res, dn,
111 acl_attrs, DSDB_SEARCH_SHOW_DELETED);
112 if (ret != LDB_SUCCESS) {
113 DEBUG(10,("access_check: failed to find object %s\n", ldb_dn_get_linearized(dn)));
116 return dsdb_check_access_on_dn_internal(acl_res,
118 session_info->security_token,
125 static int acl_module_init(struct ldb_module *module)
127 struct ldb_context *ldb;
128 struct acl_private *data;
130 TALLOC_CTX *mem_ctx = talloc_new(module);
131 static const char *attrs[] = { "passwordAttribute", NULL };
132 struct ldb_result *res;
133 struct ldb_message *msg;
134 struct ldb_message_element *password_attributes;
136 ldb = ldb_module_get_ctx(module);
138 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
139 if (ret != LDB_SUCCESS) {
140 ldb_debug(ldb, LDB_DEBUG_ERROR,
141 "acl_module_init: Unable to register control with rootdse!\n");
142 return LDB_ERR_OPERATIONS_ERROR;
145 data = talloc(module, struct acl_private);
148 return LDB_ERR_OPERATIONS_ERROR;
151 data->password_attrs = NULL;
152 data->acl_perform = lp_parm_bool(ldb_get_opaque(ldb, "loadparm"),
153 NULL, "acl", "perform", false);
154 ldb_module_set_private(module, data);
158 return LDB_ERR_OPERATIONS_ERROR;
161 ret = dsdb_module_search_dn(module, mem_ctx, &res,
162 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
164 if (ret != LDB_SUCCESS) {
167 if (res->count == 0) {
171 if (res->count > 1) {
172 talloc_free(mem_ctx);
173 return LDB_ERR_CONSTRAINT_VIOLATION;
178 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
179 if (!password_attributes) {
182 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
183 if (!data->password_attrs) {
184 talloc_free(mem_ctx);
186 return LDB_ERR_OPERATIONS_ERROR;
188 for (i=0; i < password_attributes->num_values; i++) {
189 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
190 talloc_steal(data->password_attrs, password_attributes->values[i].data);
192 data->password_attrs[i] = NULL;
195 talloc_free(mem_ctx);
196 return ldb_next_init(module);
199 static const struct GUID *get_oc_guid_from_message(struct ldb_module *module,
200 const struct dsdb_schema *schema,
201 struct ldb_message *msg)
203 struct ldb_message_element *oc_el;
205 oc_el = ldb_msg_find_element(msg, "objectClass");
210 return class_schemaid_guid_by_lDAPDisplayName(schema,
211 (char *)oc_el->values[oc_el->num_values-1].data);
214 static int acl_check_access_on_attribute(struct ldb_module *module,
216 struct security_descriptor *sd,
217 struct dom_sid *rp_sid,
219 const struct dsdb_attribute *attr)
223 uint32_t access_granted;
224 struct object_tree *root = NULL;
225 struct object_tree *new_node = NULL;
226 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
227 struct security_token *token = acl_user_token(module);
229 if (!GUID_all_zero(&attr->attributeSecurityGUID)) {
230 if (!insert_in_object_tree(tmp_ctx,
231 &attr->attributeSecurityGUID, access,
233 DEBUG(10, ("acl_search: cannot add to object tree securityGUID\n"));
237 if (!insert_in_object_tree(tmp_ctx,
238 &attr->schemaIDGUID, access, &new_node, &new_node)) {
239 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
244 if (!insert_in_object_tree(tmp_ctx,
245 &attr->schemaIDGUID, access, &root, &new_node)) {
246 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
251 status = sec_access_check_ds(sd, token,
256 if (!NT_STATUS_IS_OK(status)) {
257 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
264 return LDB_ERR_OPERATIONS_ERROR;
267 static int acl_check_access_on_class(struct ldb_module *module,
268 const struct dsdb_schema *schema,
270 struct security_descriptor *sd,
271 struct dom_sid *rp_sid,
273 const char *class_name)
277 uint32_t access_granted;
278 struct object_tree *root = NULL;
279 struct object_tree *new_node = NULL;
280 const struct GUID *guid;
281 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
282 struct security_token *token = acl_user_token(module);
284 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
286 DEBUG(10, ("acl_search: cannot find class %s\n",
290 if (!insert_in_object_tree(tmp_ctx,
293 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
297 status = sec_access_check_ds(sd, token,
302 if (!NT_STATUS_IS_OK(status)) {
303 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
310 return LDB_ERR_OPERATIONS_ERROR;
313 static int acl_allowedAttributes(struct ldb_module *module,
314 const struct dsdb_schema *schema,
315 struct ldb_message *sd_msg,
316 struct ldb_message *msg,
317 struct acl_context *ac)
319 struct ldb_message_element *oc_el;
320 struct ldb_context *ldb = ldb_module_get_ctx(module);
322 const char **attr_list;
325 /* If we don't have a schema yet, we can't do anything... */
326 if (schema == NULL) {
327 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
328 return LDB_ERR_OPERATIONS_ERROR;
331 /* Must remove any existing attribute */
332 if (ac->allowedAttributes) {
333 ldb_msg_remove_attr(msg, "allowedAttributes");
336 mem_ctx = talloc_new(msg);
339 return LDB_ERR_OPERATIONS_ERROR;
342 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
343 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
345 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
346 talloc_free(mem_ctx);
347 return LDB_ERR_OPERATIONS_ERROR;
349 if (ac->allowedAttributes) {
350 for (i=0; attr_list && attr_list[i]; i++) {
351 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
354 if (ac->allowedAttributesEffective) {
355 struct security_descriptor *sd;
356 struct dom_sid *sid = NULL;
357 struct ldb_control *as_system = ldb_request_get_control(ac->req,
358 LDB_CONTROL_AS_SYSTEM_OID);
360 if (as_system != NULL) {
361 as_system->critical = 0;
364 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
365 if (ac->am_system || as_system) {
366 for (i=0; attr_list && attr_list[i]; i++) {
367 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
372 ret = dsdb_get_sd_from_ldb_message(mem_ctx, sd_msg, &sd);
374 if (ret != LDB_SUCCESS) {
378 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
379 for (i=0; attr_list && attr_list[i]; i++) {
380 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
383 return LDB_ERR_OPERATIONS_ERROR;
385 /* remove constructed attributes */
386 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
388 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
391 ret = acl_check_access_on_attribute(module,
397 if (ret == LDB_SUCCESS) {
398 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
405 static int acl_childClasses(struct ldb_module *module,
406 const struct dsdb_schema *schema,
407 struct ldb_message *sd_msg,
408 struct ldb_message *msg,
409 const char *attrName)
411 struct ldb_message_element *oc_el;
412 struct ldb_message_element *allowedClasses;
413 const struct dsdb_class *sclass;
416 /* If we don't have a schema yet, we can't do anything... */
417 if (schema == NULL) {
418 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
419 return LDB_ERR_OPERATIONS_ERROR;
422 /* Must remove any existing attribute, or else confusion reins */
423 ldb_msg_remove_attr(msg, attrName);
424 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
425 if (ret != LDB_SUCCESS) {
429 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
431 for (i=0; oc_el && i < oc_el->num_values; i++) {
432 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
434 /* We don't know this class? what is going on? */
438 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
439 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
442 if (allowedClasses->num_values > 1) {
443 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
444 for (i=1 ; i < allowedClasses->num_values; i++) {
445 struct ldb_val *val1 = &allowedClasses->values[i-1];
446 struct ldb_val *val2 = &allowedClasses->values[i];
447 if (data_blob_cmp(val1, val2) == 0) {
448 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
449 allowedClasses->num_values--;
458 static int acl_childClassesEffective(struct ldb_module *module,
459 const struct dsdb_schema *schema,
460 struct ldb_message *sd_msg,
461 struct ldb_message *msg,
462 struct acl_context *ac)
464 struct ldb_message_element *oc_el;
465 struct ldb_message_element *allowedClasses = NULL;
466 const struct dsdb_class *sclass;
467 struct security_descriptor *sd;
468 struct ldb_control *as_system = ldb_request_get_control(ac->req,
469 LDB_CONTROL_AS_SYSTEM_OID);
470 struct dom_sid *sid = NULL;
473 if (as_system != NULL) {
474 as_system->critical = 0;
477 if (ac->am_system || as_system) {
478 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
481 /* If we don't have a schema yet, we can't do anything... */
482 if (schema == NULL) {
483 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
484 return LDB_ERR_OPERATIONS_ERROR;
487 /* Must remove any existing attribute, or else confusion reins */
488 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
490 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
491 ret = dsdb_get_sd_from_ldb_message(msg, sd_msg, &sd);
492 if (ret != LDB_SUCCESS) {
496 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
497 for (i=0; oc_el && i < oc_el->num_values; i++) {
498 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
500 /* We don't know this class? what is going on? */
504 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
505 ret = acl_check_access_on_class(module,
510 SEC_ADS_CREATE_CHILD,
511 sclass->possibleInferiors[j]);
512 if (ret == LDB_SUCCESS) {
513 ldb_msg_add_string(msg, "allowedChildClassesEffective",
514 sclass->possibleInferiors[j]);
518 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
519 if (!allowedClasses) {
523 if (allowedClasses->num_values > 1) {
524 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
525 for (i=1 ; i < allowedClasses->num_values; i++) {
526 struct ldb_val *val1 = &allowedClasses->values[i-1];
527 struct ldb_val *val2 = &allowedClasses->values[i];
528 if (data_blob_cmp(val1, val2) == 0) {
529 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
530 allowedClasses->num_values--;
538 static int acl_sDRightsEffective(struct ldb_module *module,
539 struct ldb_message *sd_msg,
540 struct ldb_message *msg,
541 struct acl_context *ac)
543 struct ldb_message_element *rightsEffective;
545 struct security_descriptor *sd;
546 struct ldb_control *as_system = ldb_request_get_control(ac->req,
547 LDB_CONTROL_AS_SYSTEM_OID);
548 struct dom_sid *sid = NULL;
551 if (as_system != NULL) {
552 as_system->critical = 0;
555 /* Must remove any existing attribute, or else confusion reins */
556 ldb_msg_remove_attr(msg, "sDRightsEffective");
557 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
558 if (ret != LDB_SUCCESS) {
561 if (ac->am_system || as_system) {
562 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
565 /* Get the security descriptor from the message */
566 ret = dsdb_get_sd_from_ldb_message(msg, sd_msg, &sd);
567 if (ret != LDB_SUCCESS) {
570 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
571 ret = acl_check_access_on_attribute(module,
577 if (ret == LDB_SUCCESS) {
578 flags |= SECINFO_OWNER | SECINFO_GROUP;
580 ret = acl_check_access_on_attribute(module,
586 if (ret == LDB_SUCCESS) {
587 flags |= SECINFO_DACL;
589 ret = acl_check_access_on_attribute(module,
593 SEC_FLAG_SYSTEM_SECURITY,
595 if (ret == LDB_SUCCESS) {
596 flags |= SECINFO_SACL;
599 ldb_msg_add_fmt(msg, "sDRightsEffective", "%u", flags);
603 static int acl_add(struct ldb_module *module, struct ldb_request *req)
606 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
607 struct ldb_context *ldb;
608 const struct dsdb_schema *schema;
609 struct ldb_message_element *oc_el;
610 const struct GUID *guid;
611 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
613 if (as_system != NULL) {
614 as_system->critical = 0;
617 if (dsdb_module_am_system(module) || as_system) {
618 return ldb_next_request(module, req);
621 if (ldb_dn_is_special(req->op.add.message->dn)) {
622 return ldb_next_request(module, req);
624 ldb = ldb_module_get_ctx(module);
625 /* Creating an NC. There is probably something we should do here,
626 * but we will establish that later */
627 /* FIXME: this has to be made dynamic at some point */
628 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
629 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
630 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_default_basedn(ldb))) == 0) ||
631 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_root_basedn(ldb))) == 0)) {
632 return ldb_next_request(module, req);
635 schema = dsdb_get_schema(ldb, req);
637 return LDB_ERR_OPERATIONS_ERROR;
640 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
641 if (!oc_el || oc_el->num_values == 0) {
642 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
643 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
646 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
647 (char *)oc_el->values[oc_el->num_values-1].data);
648 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
649 if (ret != LDB_SUCCESS) {
652 return ldb_next_request(module, req);
655 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
658 struct ldb_context *ldb = ldb_module_get_ctx(module);
659 const struct dsdb_schema *schema;
661 bool modify_sd = false;
662 const struct GUID *guid;
663 uint32_t access_granted;
664 struct object_tree *root = NULL;
665 struct object_tree *new_node = NULL;
667 struct ldb_result *acl_res;
668 struct security_descriptor *sd;
669 struct dom_sid *sid = NULL;
670 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
671 TALLOC_CTX *tmp_ctx = talloc_new(req);
672 static const char *acl_attrs[] = {
673 "nTSecurityDescriptor",
679 if (as_system != NULL) {
680 as_system->critical = 0;
683 /* Don't print this debug statement if elements[0].name is going to be NULL */
684 if(req->op.mod.message->num_elements > 0)
686 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
688 if (dsdb_module_am_system(module) || as_system) {
689 return ldb_next_request(module, req);
691 if (ldb_dn_is_special(req->op.mod.message->dn)) {
692 return ldb_next_request(module, req);
694 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.mod.message->dn,
697 if (ret != LDB_SUCCESS) {
701 schema = dsdb_get_schema(ldb, acl_res);
703 talloc_free(acl_res);
704 return LDB_ERR_OPERATIONS_ERROR;
707 ret = dsdb_get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
708 if (ret != LDB_SUCCESS) {
709 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
712 /* Theoretically we pass the check if the object has no sd */
717 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
719 DEBUG(10, ("acl_modify: cannot get guid\n"));
722 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
723 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
725 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
728 for (i=0; i < req->op.mod.message->num_elements; i++){
729 const struct dsdb_attribute *attr;
730 /* clearTextPassword is not in schema */
731 if (strcmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
732 attr = dsdb_attribute_by_lDAPDisplayName(schema, "unicodePwd");
734 attr = dsdb_attribute_by_lDAPDisplayName(schema,
735 req->op.mod.message->elements[i].name);
737 if (strcmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
742 DEBUG(10, ("acl_modify: cannot find attribute %s\n",
743 req->op.mod.message->elements[i].name));
746 if (!insert_in_object_tree(tmp_ctx,
747 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
748 &new_node, &new_node)) {
749 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
753 if (!insert_in_object_tree(tmp_ctx,
754 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
755 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
761 if (root->num_of_children > 0) {
762 status = sec_access_check_ds(sd, acl_user_token(module),
768 if (!NT_STATUS_IS_OK(status)) {
769 DEBUG(10, ("Object %s nas no write property access\n",
770 ldb_dn_get_linearized(req->op.mod.message->dn)));
772 acl_user_token(module),
773 req->op.mod.message->dn,
776 talloc_free(tmp_ctx);
777 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
781 status = sec_access_check_ds(sd, acl_user_token(module),
787 if (!NT_STATUS_IS_OK(status)) {
788 DEBUG(10, ("Object %s nas no write dacl access\n",
789 ldb_dn_get_linearized(req->op.mod.message->dn)));
791 acl_user_token(module),
792 req->op.mod.message->dn,
795 talloc_free(tmp_ctx);
796 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
800 talloc_free(tmp_ctx);
801 return ldb_next_request(module, req);
803 talloc_free(tmp_ctx);
804 return LDB_ERR_OPERATIONS_ERROR;
807 /* similar to the modify for the time being.
808 * We need to concider the special delete tree case, though - TODO */
809 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
812 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
813 struct ldb_context *ldb;
814 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
816 if (as_system != NULL) {
817 as_system->critical = 0;
820 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
821 if (dsdb_module_am_system(module) || as_system) {
822 return ldb_next_request(module, req);
825 if (ldb_dn_is_special(req->op.del.dn)) {
826 return ldb_next_request(module, req);
828 ldb = ldb_module_get_ctx(module);
829 /* first check if we have delete object right */
830 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
831 if (ret == LDB_SUCCESS) {
832 return ldb_next_request(module, req);
835 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
836 /* No parent, so check fails */
837 /* FIXME: this has to be made dynamic at some point */
838 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
839 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
840 (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0) ||
841 (ldb_dn_compare(req->op.del.dn, (ldb_get_root_basedn(ldb))) == 0)) {
842 DEBUG(10,("acl:deleting an NC\n"));
843 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
846 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
847 if (ret != LDB_SUCCESS) {
850 return ldb_next_request(module, req);
853 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
856 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
857 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
858 const struct dsdb_schema *schema;
859 struct ldb_context *ldb;
860 struct security_descriptor *sd = NULL;
861 struct dom_sid *sid = NULL;
862 struct ldb_result *acl_res;
863 const struct GUID *guid;
864 struct object_tree *root = NULL;
865 struct object_tree *new_node = NULL;
866 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
867 TALLOC_CTX *tmp_ctx = talloc_new(req);
869 uint32_t access_granted;
870 const char *rdn_name;
871 static const char *acl_attrs[] = {
872 "nTSecurityDescriptor",
878 if (as_system != NULL) {
879 as_system->critical = 0;
882 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
883 if (dsdb_module_am_system(module) || as_system) {
884 return ldb_next_request(module, req);
886 if (ldb_dn_is_special(req->op.rename.olddn)) {
887 return ldb_next_request(module, req);
889 ldb = ldb_module_get_ctx(module);
891 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
892 acl_attrs, DSDB_SEARCH_SHOW_DELETED);
893 /* we sould be able to find the parent */
894 if (ret != LDB_SUCCESS) {
895 DEBUG(10,("acl: failed to find object %s\n",
896 ldb_dn_get_linearized(req->op.rename.olddn)));
900 schema = dsdb_get_schema(ldb, acl_res);
902 talloc_free(acl_res);
903 return LDB_ERR_OPERATIONS_ERROR;
906 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
907 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
909 return LDB_ERR_OPERATIONS_ERROR;
912 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
914 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
915 &new_node, &new_node)) {
916 return LDB_ERR_OPERATIONS_ERROR;
919 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
920 if (rdn_name == NULL) {
921 return LDB_ERR_OPERATIONS_ERROR;
923 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
925 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
926 &new_node, &new_node)) {
927 return LDB_ERR_OPERATIONS_ERROR;
930 ret = dsdb_get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
932 if (ret != LDB_SUCCESS) {
933 return LDB_ERR_OPERATIONS_ERROR;
935 /* Theoretically we pass the check if the object has no sd */
939 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
940 status = sec_access_check_ds(sd, acl_user_token(module),
946 if (!NT_STATUS_IS_OK(status)) {
947 DEBUG(10, ("Object %s nas no wp on name\n",
948 ldb_dn_get_linearized(req->op.rename.olddn)));
950 acl_user_token(module),
951 req->op.rename.olddn,
954 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
957 if (ldb_dn_compare(oldparent, newparent) == 0) {
958 /* regular rename, not move, nothing more to do */
959 return ldb_next_request(module, req);
962 /* What exactly to do in this case? It would fail anyway.. */
963 /* FIXME: this has to be made dynamic at some point */
964 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
965 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
966 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0) ||
967 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_root_basedn(ldb))) == 0)) {
968 DEBUG(10,("acl:moving as an NC\n"));
969 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
971 /* new parent should have create child */
972 talloc_free(tmp_ctx);
973 tmp_ctx = talloc_new(req);
976 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
978 DEBUG(10,("acl:renamed object has no object class\n"));
979 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
982 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
983 if (ret != LDB_SUCCESS) {
984 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
987 /* do we have delete object on the object? */
989 status = sec_access_check_ds(sd, acl_user_token(module),
995 if (NT_STATUS_IS_OK(status)) {
996 return ldb_next_request(module, req);
998 /* what about delete child on the current parent */
999 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1000 if (ret != LDB_SUCCESS) {
1001 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1002 return ldb_module_done(req, NULL, NULL, ret);
1004 return ldb_next_request(module, req);
1007 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1009 struct ldb_context *ldb;
1010 struct acl_context *ac;
1011 struct acl_private *data;
1012 struct ldb_result *acl_res;
1013 static const char *acl_attrs[] = {
1015 "nTSecurityDescriptor",
1021 ac = talloc_get_type(req->context, struct acl_context);
1022 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1023 ldb = ldb_module_get_ctx(ac->module);
1026 return ldb_module_done(ac->req, NULL, NULL,
1027 LDB_ERR_OPERATIONS_ERROR);
1029 if (ares->error != LDB_SUCCESS) {
1030 return ldb_module_done(ac->req, ares->controls,
1031 ares->response, ares->error);
1034 switch (ares->type) {
1035 case LDB_REPLY_ENTRY:
1036 if (ac->allowedAttributes
1037 || ac->allowedChildClasses
1038 || ac->allowedChildClassesEffective
1039 || ac->allowedAttributesEffective
1040 || ac->sDRightsEffective) {
1041 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1043 if (ret != LDB_SUCCESS) {
1044 return ldb_module_done(ac->req, NULL, NULL, ret);
1046 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1047 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1048 if (ret != LDB_SUCCESS) {
1049 return ldb_module_done(ac->req, NULL, NULL, ret);
1052 if (ac->allowedChildClasses) {
1053 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1054 ares->message, "allowedChildClasses");
1055 if (ret != LDB_SUCCESS) {
1056 return ldb_module_done(ac->req, NULL, NULL, ret);
1059 if (ac->allowedChildClassesEffective) {
1060 ret = acl_childClassesEffective(ac->module, ac->schema,
1061 acl_res->msgs[0], ares->message, ac);
1062 if (ret != LDB_SUCCESS) {
1063 return ldb_module_done(ac->req, NULL, NULL, ret);
1066 if (ac->sDRightsEffective) {
1067 ret = acl_sDRightsEffective(ac->module,
1068 acl_res->msgs[0], ares->message, ac);
1069 if (ret != LDB_SUCCESS) {
1070 return ldb_module_done(ac->req, NULL, NULL, ret);
1074 if (data && data->password_attrs) {
1075 if (!ac->am_system) {
1076 for (i = 0; data->password_attrs[i]; i++) {
1077 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1081 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1083 case LDB_REPLY_REFERRAL:
1084 return ldb_module_send_referral(ac->req, ares->referral);
1086 case LDB_REPLY_DONE:
1087 return ldb_module_done(ac->req, ares->controls,
1088 ares->response, LDB_SUCCESS);
1094 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1096 struct ldb_context *ldb;
1097 struct acl_context *ac;
1098 struct ldb_request *down_req;
1099 struct acl_private *data;
1102 ldb = ldb_module_get_ctx(module);
1104 ac = talloc_zero(req, struct acl_context);
1107 return LDB_ERR_OPERATIONS_ERROR;
1109 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1111 ac->module = module;
1113 ac->am_system = dsdb_module_am_system(module);
1114 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1115 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1116 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1117 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1118 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1119 ac->schema = dsdb_get_schema(ldb, ac);
1121 /* replace any attributes in the parse tree that are private,
1122 so we don't allow a search for 'userPassword=penguin',
1123 just as we would not allow that attribute to be returned */
1124 if (ac->am_system) {
1125 /* FIXME: We should copy the tree and keep the original unmodified. */
1126 /* remove password attributes */
1127 if (data && data->password_attrs) {
1128 for (i = 0; data->password_attrs[i]; i++) {
1129 ldb_parse_tree_attr_replace(req->op.search.tree,
1130 data->password_attrs[i],
1131 "kludgeACLredactedattribute");
1135 ret = ldb_build_search_req_ex(&down_req,
1137 req->op.search.base,
1138 req->op.search.scope,
1139 req->op.search.tree,
1140 req->op.search.attrs,
1142 ac, acl_search_callback,
1144 if (ret != LDB_SUCCESS) {
1147 /* perform the search */
1148 return ldb_next_request(module, down_req);
1151 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1153 .search = acl_search,
1155 .modify = acl_modify,
1157 .rename = acl_rename,
1158 .init_context = acl_module_init