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_operr(ldb);
110 ret = dsdb_module_search_dn(module, mem_ctx, &acl_res, dn,
112 DSDB_FLAG_NEXT_MODULE |
113 DSDB_SEARCH_SHOW_DELETED);
114 if (ret != LDB_SUCCESS) {
115 DEBUG(10,("access_check: failed to find object %s\n", ldb_dn_get_linearized(dn)));
118 return dsdb_check_access_on_dn_internal(ldb, acl_res,
120 session_info->security_token,
127 static int acl_module_init(struct ldb_module *module)
129 struct ldb_context *ldb;
130 struct acl_private *data;
132 TALLOC_CTX *mem_ctx = talloc_new(module);
133 static const char *attrs[] = { "passwordAttribute", NULL };
134 struct ldb_result *res;
135 struct ldb_message *msg;
136 struct ldb_message_element *password_attributes;
138 ldb = ldb_module_get_ctx(module);
140 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
141 if (ret != LDB_SUCCESS) {
142 ldb_debug(ldb, LDB_DEBUG_ERROR,
143 "acl_module_init: Unable to register control with rootdse!\n");
144 return ldb_operr(ldb);
147 data = talloc(module, struct acl_private);
152 data->password_attrs = NULL;
153 data->acl_perform = lp_parm_bool(ldb_get_opaque(ldb, "loadparm"),
154 NULL, "acl", "perform", false);
155 ldb_module_set_private(module, data);
161 ret = dsdb_module_search_dn(module, mem_ctx, &res,
162 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
164 DSDB_FLAG_NEXT_MODULE);
165 if (ret != LDB_SUCCESS) {
168 if (res->count == 0) {
172 if (res->count > 1) {
173 talloc_free(mem_ctx);
174 return LDB_ERR_CONSTRAINT_VIOLATION;
179 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
180 if (!password_attributes) {
183 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
184 if (!data->password_attrs) {
185 talloc_free(mem_ctx);
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;
262 talloc_free(tmp_ctx);
265 talloc_free(tmp_ctx);
266 return ldb_operr(ldb_module_get_ctx(module));
269 static int acl_check_access_on_class(struct ldb_module *module,
270 const struct dsdb_schema *schema,
272 struct security_descriptor *sd,
273 struct dom_sid *rp_sid,
275 const char *class_name)
279 uint32_t access_granted;
280 struct object_tree *root = NULL;
281 struct object_tree *new_node = NULL;
282 const struct GUID *guid;
283 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
284 struct security_token *token = acl_user_token(module);
286 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
288 DEBUG(10, ("acl_search: cannot find class %s\n",
292 if (!insert_in_object_tree(tmp_ctx,
295 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
299 status = sec_access_check_ds(sd, token,
304 if (!NT_STATUS_IS_OK(status)) {
305 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
312 return ldb_operr(ldb_module_get_ctx(module));
315 static int acl_allowedAttributes(struct ldb_module *module,
316 const struct dsdb_schema *schema,
317 struct ldb_message *sd_msg,
318 struct ldb_message *msg,
319 struct acl_context *ac)
321 struct ldb_message_element *oc_el;
322 struct ldb_context *ldb = ldb_module_get_ctx(module);
324 const char **attr_list;
327 /* If we don't have a schema yet, we can't do anything... */
328 if (schema == NULL) {
329 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
330 return LDB_ERR_OPERATIONS_ERROR;
333 /* Must remove any existing attribute */
334 if (ac->allowedAttributes) {
335 ldb_msg_remove_attr(msg, "allowedAttributes");
338 mem_ctx = talloc_new(msg);
343 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
344 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
346 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
347 talloc_free(mem_ctx);
348 return LDB_ERR_OPERATIONS_ERROR;
350 if (ac->allowedAttributes) {
351 for (i=0; attr_list && attr_list[i]; i++) {
352 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
355 if (ac->allowedAttributesEffective) {
356 struct security_descriptor *sd;
357 struct dom_sid *sid = NULL;
358 struct ldb_control *as_system = ldb_request_get_control(ac->req,
359 LDB_CONTROL_AS_SYSTEM_OID);
361 if (as_system != NULL) {
362 as_system->critical = 0;
365 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
366 if (ac->am_system || as_system) {
367 for (i=0; attr_list && attr_list[i]; i++) {
368 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
373 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
375 if (ret != LDB_SUCCESS) {
379 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
380 for (i=0; attr_list && attr_list[i]; i++) {
381 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
384 return ldb_operr(ldb);
386 /* remove constructed attributes */
387 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
389 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
392 ret = acl_check_access_on_attribute(module,
398 if (ret == LDB_SUCCESS) {
399 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
406 static int acl_childClasses(struct ldb_module *module,
407 const struct dsdb_schema *schema,
408 struct ldb_message *sd_msg,
409 struct ldb_message *msg,
410 const char *attrName)
412 struct ldb_message_element *oc_el;
413 struct ldb_message_element *allowedClasses;
414 const struct dsdb_class *sclass;
418 /* If we don't have a schema yet, we can't do anything... */
419 if (schema == NULL) {
420 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
421 return LDB_ERR_OPERATIONS_ERROR;
424 /* Must remove any existing attribute, or else confusion reins */
425 ldb_msg_remove_attr(msg, attrName);
426 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
427 if (ret != LDB_SUCCESS) {
431 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
433 for (i=0; oc_el && i < oc_el->num_values; i++) {
434 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
436 /* We don't know this class? what is going on? */
440 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
441 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
444 if (allowedClasses->num_values > 1) {
445 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
446 for (i=1 ; i < allowedClasses->num_values; i++) {
447 struct ldb_val *val1 = &allowedClasses->values[i-1];
448 struct ldb_val *val2 = &allowedClasses->values[i];
449 if (data_blob_cmp(val1, val2) == 0) {
450 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
451 allowedClasses->num_values--;
460 static int acl_childClassesEffective(struct ldb_module *module,
461 const struct dsdb_schema *schema,
462 struct ldb_message *sd_msg,
463 struct ldb_message *msg,
464 struct acl_context *ac)
466 struct ldb_message_element *oc_el;
467 struct ldb_message_element *allowedClasses = NULL;
468 const struct dsdb_class *sclass;
469 struct security_descriptor *sd;
470 struct ldb_control *as_system = ldb_request_get_control(ac->req,
471 LDB_CONTROL_AS_SYSTEM_OID);
472 struct dom_sid *sid = NULL;
476 if (as_system != NULL) {
477 as_system->critical = 0;
480 if (ac->am_system || as_system) {
481 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
484 /* If we don't have a schema yet, we can't do anything... */
485 if (schema == NULL) {
486 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
487 return LDB_ERR_OPERATIONS_ERROR;
490 /* Must remove any existing attribute, or else confusion reins */
491 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
493 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
494 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
495 if (ret != LDB_SUCCESS) {
499 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
500 for (i=0; oc_el && i < oc_el->num_values; i++) {
501 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
503 /* We don't know this class? what is going on? */
507 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
508 ret = acl_check_access_on_class(module,
513 SEC_ADS_CREATE_CHILD,
514 sclass->possibleInferiors[j]);
515 if (ret == LDB_SUCCESS) {
516 ldb_msg_add_string(msg, "allowedChildClassesEffective",
517 sclass->possibleInferiors[j]);
521 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
522 if (!allowedClasses) {
526 if (allowedClasses->num_values > 1) {
527 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
528 for (i=1 ; i < allowedClasses->num_values; i++) {
529 struct ldb_val *val1 = &allowedClasses->values[i-1];
530 struct ldb_val *val2 = &allowedClasses->values[i];
531 if (data_blob_cmp(val1, val2) == 0) {
532 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
533 allowedClasses->num_values--;
541 static int acl_sDRightsEffective(struct ldb_module *module,
542 struct ldb_message *sd_msg,
543 struct ldb_message *msg,
544 struct acl_context *ac)
546 struct ldb_message_element *rightsEffective;
548 struct security_descriptor *sd;
549 struct ldb_control *as_system = ldb_request_get_control(ac->req,
550 LDB_CONTROL_AS_SYSTEM_OID);
551 struct dom_sid *sid = NULL;
554 if (as_system != NULL) {
555 as_system->critical = 0;
558 /* Must remove any existing attribute, or else confusion reins */
559 ldb_msg_remove_attr(msg, "sDRightsEffective");
560 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
561 if (ret != LDB_SUCCESS) {
564 if (ac->am_system || as_system) {
565 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
568 /* Get the security descriptor from the message */
569 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
570 if (ret != LDB_SUCCESS) {
573 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
574 ret = acl_check_access_on_attribute(module,
580 if (ret == LDB_SUCCESS) {
581 flags |= SECINFO_OWNER | SECINFO_GROUP;
583 ret = acl_check_access_on_attribute(module,
589 if (ret == LDB_SUCCESS) {
590 flags |= SECINFO_DACL;
592 ret = acl_check_access_on_attribute(module,
596 SEC_FLAG_SYSTEM_SECURITY,
598 if (ret == LDB_SUCCESS) {
599 flags |= SECINFO_SACL;
602 ldb_msg_add_fmt(msg, "sDRightsEffective", "%u", flags);
606 static int acl_add(struct ldb_module *module, struct ldb_request *req)
609 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
610 struct ldb_context *ldb;
611 const struct dsdb_schema *schema;
612 struct ldb_message_element *oc_el;
613 const struct GUID *guid;
614 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
616 if (as_system != NULL) {
617 as_system->critical = 0;
620 if (dsdb_module_am_system(module) || as_system) {
621 return ldb_next_request(module, req);
624 if (ldb_dn_is_special(req->op.add.message->dn)) {
625 return ldb_next_request(module, req);
627 ldb = ldb_module_get_ctx(module);
628 /* Creating an NC. There is probably something we should do here,
629 * but we will establish that later */
630 /* FIXME: this has to be made dynamic at some point */
631 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
632 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
633 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_default_basedn(ldb))) == 0) ||
634 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_root_basedn(ldb))) == 0)) {
635 return ldb_next_request(module, req);
638 schema = dsdb_get_schema(ldb, req);
640 return ldb_operr(ldb);
643 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
644 if (!oc_el || oc_el->num_values == 0) {
645 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
646 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
649 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
650 (char *)oc_el->values[oc_el->num_values-1].data);
651 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
652 if (ret != LDB_SUCCESS) {
655 return ldb_next_request(module, req);
658 /* checks for validated writes */
659 static int acl_check_extended_right(TALLOC_CTX *mem_ctx,
660 struct security_descriptor *sd,
661 struct security_token *token,
662 const char *ext_right,
668 uint32_t access_granted;
669 struct object_tree *root = NULL;
670 struct object_tree *new_node = NULL;
671 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
673 GUID_from_string(ext_right, &right);
675 if (!insert_in_object_tree(tmp_ctx, &right, right_type,
677 DEBUG(10, ("acl_ext_right: cannot add to object tree\n"));
678 talloc_free(tmp_ctx);
679 return LDB_ERR_OPERATIONS_ERROR;
681 status = sec_access_check_ds(sd, token,
687 if (!NT_STATUS_IS_OK(status)) {
688 talloc_free(tmp_ctx);
689 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
691 talloc_free(tmp_ctx);
696 /* ckecks if modifications are allowed on "Member" attribute */
697 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
698 struct ldb_module *module,
699 struct ldb_request *req,
700 struct security_descriptor *sd,
702 const struct GUID *oc_guid,
703 const struct dsdb_attribute *attr)
707 struct ldb_context *ldb = ldb_module_get_ctx(module);
708 struct ldb_dn *user_dn;
709 struct ldb_message_element *member_el;
710 /* if we have wp, we can do whatever we like */
711 if (acl_check_access_on_attribute(module,
716 attr) == LDB_SUCCESS) {
719 /* if we are adding/deleting ourselves, check for self membership */
720 ret = dsdb_find_dn_by_sid(ldb, mem_ctx, acl_user_token(module)->user_sid, &user_dn);
721 if (ret != LDB_SUCCESS) {
724 member_el = ldb_msg_find_element(req->op.mod.message, "member");
726 return ldb_operr(ldb);
728 /* user can only remove oneself */
729 if (member_el->num_values == 0) {
730 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
732 for (i = 0; i < member_el->num_values; i++) {
733 if (strcasecmp((const char *)member_el->values[i].data,
734 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
735 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
738 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
739 GUID_DRS_SELF_MEMBERSHIP,
742 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
743 dsdb_acl_debug(sd, acl_user_token(module),
744 req->op.mod.message->dn,
751 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
752 struct ldb_module *module,
753 struct ldb_request *req,
754 struct security_descriptor *sd,
756 const struct GUID *oc_guid)
758 int ret = LDB_SUCCESS;
759 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
760 struct ldb_message_element *el;
761 struct ldb_message *msg;
762 const char *passwordAttrs[] = { "userPassword", "unicodePwd",
763 "clearTextPassword", NULL }, **l;
764 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
766 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
768 return ldb_module_oom(module);
770 for (l = passwordAttrs; *l != NULL; l++) {
771 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
772 if (el->flags == LDB_FLAG_MOD_DELETE) {
775 if (el->flags == LDB_FLAG_MOD_ADD) {
778 if (el->flags == LDB_FLAG_MOD_REPLACE) {
781 ldb_msg_remove_element(msg, el);
784 /* a single delete will be handled by password hash
785 later in the stack, so we let it though here */
786 if (del_attr_cnt > 0 && add_attr_cnt == 0) {
787 talloc_free(tmp_ctx);
790 if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
791 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
792 GUID_DRS_FORCE_CHANGE_PASSWORD,
793 SEC_ADS_CONTROL_ACCESS,
796 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
797 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
798 GUID_DRS_USER_CHANGE_PASSWORD,
799 SEC_ADS_CONTROL_ACCESS,
801 /* Very strange, but we get constraint violation in this case */
802 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
803 ret = LDB_ERR_CONSTRAINT_VIOLATION;
806 if (ret != LDB_SUCCESS) {
807 dsdb_acl_debug(sd, acl_user_token(module),
808 req->op.mod.message->dn,
812 talloc_free(tmp_ctx);
816 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
819 struct ldb_context *ldb = ldb_module_get_ctx(module);
820 const struct dsdb_schema *schema;
822 const struct GUID *guid;
823 uint32_t access_granted;
824 struct object_tree *root = NULL;
825 struct object_tree *new_node = NULL;
827 struct ldb_result *acl_res;
828 struct security_descriptor *sd;
829 struct dom_sid *sid = NULL;
830 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
831 TALLOC_CTX *tmp_ctx = talloc_new(req);
832 static const char *acl_attrs[] = {
833 "nTSecurityDescriptor",
839 if (as_system != NULL) {
840 as_system->critical = 0;
843 /* Don't print this debug statement if elements[0].name is going to be NULL */
844 if(req->op.mod.message->num_elements > 0)
846 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
848 if (dsdb_module_am_system(module) || as_system) {
849 return ldb_next_request(module, req);
851 if (ldb_dn_is_special(req->op.mod.message->dn)) {
852 return ldb_next_request(module, req);
854 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
856 DSDB_FLAG_NEXT_MODULE);
858 if (ret != LDB_SUCCESS) {
862 schema = dsdb_get_schema(ldb, tmp_ctx);
864 ret = LDB_ERR_OPERATIONS_ERROR;
868 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
869 if (ret != LDB_SUCCESS) {
870 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
873 /* Theoretically we pass the check if the object has no sd */
878 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
880 DEBUG(10, ("acl_modify: cannot get guid\n"));
883 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
884 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
886 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
889 for (i=0; i < req->op.mod.message->num_elements; i++){
890 const struct dsdb_attribute *attr;
891 attr = dsdb_attribute_by_lDAPDisplayName(schema,
892 req->op.mod.message->elements[i].name);
894 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
895 status = sec_access_check_ds(sd, acl_user_token(module),
901 if (!NT_STATUS_IS_OK(status)) {
902 DEBUG(10, ("Object %s has no write dacl access\n",
903 ldb_dn_get_linearized(req->op.mod.message->dn)));
905 acl_user_token(module),
906 req->op.mod.message->dn,
909 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
913 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
914 ret = acl_check_self_membership(tmp_ctx,
921 if (ret != LDB_SUCCESS) {
925 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
926 /* this one is not affected by any rights, we should let it through
927 so that passwords_hash returns the correct error */
930 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
931 ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0 ||
932 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
933 ret = acl_check_password_rights(tmp_ctx,
939 if (ret != LDB_SUCCESS) {
944 /* This basic attribute existence check with the right errorcode
945 * is needed since this module is the first one which requests
946 * schema attribute informations.
947 * The complete attribute checking is done in the
948 * "objectclass_attrs" module behind this one.
951 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
952 req->op.mod.message->elements[i].name,
953 ldb_dn_get_linearized(req->op.mod.message->dn));
954 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
957 if (!insert_in_object_tree(tmp_ctx,
958 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
959 &new_node, &new_node)) {
960 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
961 ret = LDB_ERR_OPERATIONS_ERROR;
965 if (!insert_in_object_tree(tmp_ctx,
966 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
967 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
968 ret = LDB_ERR_OPERATIONS_ERROR;
974 if (root->num_of_children > 0) {
975 status = sec_access_check_ds(sd, acl_user_token(module),
981 if (!NT_STATUS_IS_OK(status)) {
982 DEBUG(10, ("Object %s has no write property access\n",
983 ldb_dn_get_linearized(req->op.mod.message->dn)));
985 acl_user_token(module),
986 req->op.mod.message->dn,
989 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
995 talloc_free(tmp_ctx);
996 return ldb_next_request(module, req);
998 talloc_free(tmp_ctx);
1002 /* similar to the modify for the time being.
1003 * We need to concider the special delete tree case, though - TODO */
1004 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1007 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
1008 struct ldb_context *ldb;
1009 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1011 if (as_system != NULL) {
1012 as_system->critical = 0;
1015 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1016 if (dsdb_module_am_system(module) || as_system) {
1017 return ldb_next_request(module, req);
1020 if (ldb_dn_is_special(req->op.del.dn)) {
1021 return ldb_next_request(module, req);
1023 ldb = ldb_module_get_ctx(module);
1024 /* first check if we have delete object right */
1025 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
1026 if (ret == LDB_SUCCESS) {
1027 return ldb_next_request(module, req);
1030 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
1031 /* No parent, so check fails */
1032 /* FIXME: this has to be made dynamic at some point */
1033 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
1034 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
1035 (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0) ||
1036 (ldb_dn_compare(req->op.del.dn, (ldb_get_root_basedn(ldb))) == 0)) {
1037 DEBUG(10,("acl:deleting an NC\n"));
1038 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
1041 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
1042 if (ret != LDB_SUCCESS) {
1045 return ldb_next_request(module, req);
1048 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1051 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
1052 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
1053 const struct dsdb_schema *schema;
1054 struct ldb_context *ldb;
1055 struct security_descriptor *sd = NULL;
1056 struct dom_sid *sid = NULL;
1057 struct ldb_result *acl_res;
1058 const struct GUID *guid;
1059 struct object_tree *root = NULL;
1060 struct object_tree *new_node = NULL;
1061 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1062 TALLOC_CTX *tmp_ctx = talloc_new(req);
1064 uint32_t access_granted;
1065 const char *rdn_name;
1066 static const char *acl_attrs[] = {
1067 "nTSecurityDescriptor",
1073 if (as_system != NULL) {
1074 as_system->critical = 0;
1077 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1078 if (dsdb_module_am_system(module) || as_system) {
1079 return ldb_next_request(module, req);
1081 if (ldb_dn_is_special(req->op.rename.olddn)) {
1082 return ldb_next_request(module, req);
1084 ldb = ldb_module_get_ctx(module);
1086 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
1088 DSDB_FLAG_NEXT_MODULE |
1089 DSDB_SEARCH_SHOW_DELETED);
1090 /* we sould be able to find the parent */
1091 if (ret != LDB_SUCCESS) {
1092 DEBUG(10,("acl: failed to find object %s\n",
1093 ldb_dn_get_linearized(req->op.rename.olddn)));
1097 schema = dsdb_get_schema(ldb, acl_res);
1099 talloc_free(acl_res);
1100 return ldb_operr(ldb);
1103 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1104 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1105 &root, &new_node)) {
1106 return ldb_operr(ldb);
1109 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1111 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1112 &new_node, &new_node)) {
1113 return ldb_operr(ldb);
1116 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1117 if (rdn_name == NULL) {
1118 return ldb_operr(ldb);
1120 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1122 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1123 &new_node, &new_node)) {
1124 return ldb_operr(ldb);
1127 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1129 if (ret != LDB_SUCCESS) {
1130 return ldb_operr(ldb);
1132 /* Theoretically we pass the check if the object has no sd */
1136 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1137 status = sec_access_check_ds(sd, acl_user_token(module),
1143 if (!NT_STATUS_IS_OK(status)) {
1144 DEBUG(10, ("Object %s has no wp on name\n",
1145 ldb_dn_get_linearized(req->op.rename.olddn)));
1147 acl_user_token(module),
1148 req->op.rename.olddn,
1151 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1154 if (ldb_dn_compare(oldparent, newparent) == 0) {
1155 /* regular rename, not move, nothing more to do */
1156 return ldb_next_request(module, req);
1159 /* What exactly to do in this case? It would fail anyway.. */
1160 /* FIXME: this has to be made dynamic at some point */
1161 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
1162 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
1163 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0) ||
1164 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_root_basedn(ldb))) == 0)) {
1165 DEBUG(10,("acl:moving as an NC\n"));
1166 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1168 /* new parent should have create child */
1169 talloc_free(tmp_ctx);
1170 tmp_ctx = talloc_new(req);
1173 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1175 DEBUG(10,("acl:renamed object has no object class\n"));
1176 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1179 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
1180 if (ret != LDB_SUCCESS) {
1181 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1184 /* do we have delete object on the object? */
1186 status = sec_access_check_ds(sd, acl_user_token(module),
1192 if (NT_STATUS_IS_OK(status)) {
1193 return ldb_next_request(module, req);
1195 /* what about delete child on the current parent */
1196 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1197 if (ret != LDB_SUCCESS) {
1198 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1199 return ldb_module_done(req, NULL, NULL, ret);
1201 return ldb_next_request(module, req);
1204 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1206 struct ldb_context *ldb;
1207 struct acl_context *ac;
1208 struct acl_private *data;
1209 struct ldb_result *acl_res;
1210 static const char *acl_attrs[] = {
1212 "nTSecurityDescriptor",
1218 ac = talloc_get_type(req->context, struct acl_context);
1219 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1220 ldb = ldb_module_get_ctx(ac->module);
1223 return ldb_module_done(ac->req, NULL, NULL,
1224 LDB_ERR_OPERATIONS_ERROR);
1226 if (ares->error != LDB_SUCCESS) {
1227 return ldb_module_done(ac->req, ares->controls,
1228 ares->response, ares->error);
1231 switch (ares->type) {
1232 case LDB_REPLY_ENTRY:
1233 if (ac->allowedAttributes
1234 || ac->allowedChildClasses
1235 || ac->allowedChildClassesEffective
1236 || ac->allowedAttributesEffective
1237 || ac->sDRightsEffective) {
1238 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1240 DSDB_FLAG_NEXT_MODULE);
1241 if (ret != LDB_SUCCESS) {
1242 return ldb_module_done(ac->req, NULL, NULL, ret);
1244 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1245 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1246 if (ret != LDB_SUCCESS) {
1247 return ldb_module_done(ac->req, NULL, NULL, ret);
1250 if (ac->allowedChildClasses) {
1251 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1252 ares->message, "allowedChildClasses");
1253 if (ret != LDB_SUCCESS) {
1254 return ldb_module_done(ac->req, NULL, NULL, ret);
1257 if (ac->allowedChildClassesEffective) {
1258 ret = acl_childClassesEffective(ac->module, ac->schema,
1259 acl_res->msgs[0], ares->message, ac);
1260 if (ret != LDB_SUCCESS) {
1261 return ldb_module_done(ac->req, NULL, NULL, ret);
1264 if (ac->sDRightsEffective) {
1265 ret = acl_sDRightsEffective(ac->module,
1266 acl_res->msgs[0], ares->message, ac);
1267 if (ret != LDB_SUCCESS) {
1268 return ldb_module_done(ac->req, NULL, NULL, ret);
1272 if (data && data->password_attrs) {
1273 if (!ac->am_system) {
1274 for (i = 0; data->password_attrs[i]; i++) {
1275 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1279 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1281 case LDB_REPLY_REFERRAL:
1282 return ldb_module_send_referral(ac->req, ares->referral);
1284 case LDB_REPLY_DONE:
1285 return ldb_module_done(ac->req, ares->controls,
1286 ares->response, LDB_SUCCESS);
1292 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1294 struct ldb_context *ldb;
1295 struct acl_context *ac;
1296 struct ldb_request *down_req;
1297 struct acl_private *data;
1300 ldb = ldb_module_get_ctx(module);
1302 ac = talloc_zero(req, struct acl_context);
1304 return ldb_oom(ldb);
1306 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1308 ac->module = module;
1310 ac->am_system = dsdb_module_am_system(module);
1311 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1312 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1313 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1314 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1315 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1316 ac->schema = dsdb_get_schema(ldb, ac);
1318 /* replace any attributes in the parse tree that are private,
1319 so we don't allow a search for 'userPassword=penguin',
1320 just as we would not allow that attribute to be returned */
1321 if (ac->am_system) {
1322 /* FIXME: We should copy the tree and keep the original unmodified. */
1323 /* remove password attributes */
1324 if (data && data->password_attrs) {
1325 for (i = 0; data->password_attrs[i]; i++) {
1326 ldb_parse_tree_attr_replace(req->op.search.tree,
1327 data->password_attrs[i],
1328 "kludgeACLredactedattribute");
1332 ret = ldb_build_search_req_ex(&down_req,
1334 req->op.search.base,
1335 req->op.search.scope,
1336 req->op.search.tree,
1337 req->op.search.attrs,
1339 ac, acl_search_callback,
1341 if (ret != LDB_SUCCESS) {
1344 /* perform the search */
1345 return ldb_next_request(module, down_req);
1348 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1350 .search = acl_search,
1352 .modify = acl_modify,
1354 .rename = acl_rename,
1355 .init_context = acl_module_init