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 static struct security_token *acl_user_token(struct ldb_module *module)
69 struct ldb_context *ldb = ldb_module_get_ctx(module);
70 struct auth_session_info *session_info
71 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
75 return session_info->security_token;
78 /* performs an access check from inside the module stack
79 * given the dn of the object to be checked, the required access
80 * guid is either the guid of the extended right, or NULL
83 int dsdb_module_check_access_on_dn(struct ldb_module *module,
87 const struct GUID *guid)
90 struct ldb_result *acl_res;
91 static const char *acl_attrs[] = {
92 "nTSecurityDescriptor",
96 struct ldb_context *ldb = ldb_module_get_ctx(module);
97 struct auth_session_info *session_info
98 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
100 return ldb_operr(ldb);
102 ret = dsdb_module_search_dn(module, mem_ctx, &acl_res, dn,
104 DSDB_FLAG_NEXT_MODULE |
105 DSDB_SEARCH_SHOW_DELETED);
106 if (ret != LDB_SUCCESS) {
107 DEBUG(10,("access_check: failed to find object %s\n", ldb_dn_get_linearized(dn)));
110 return dsdb_check_access_on_dn_internal(ldb, acl_res,
112 session_info->security_token,
119 static int acl_module_init(struct ldb_module *module)
121 struct ldb_context *ldb;
122 struct acl_private *data;
125 static const char *attrs[] = { "passwordAttribute", NULL };
126 struct ldb_result *res;
127 struct ldb_message *msg;
128 struct ldb_message_element *password_attributes;
130 ldb = ldb_module_get_ctx(module);
132 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
133 if (ret != LDB_SUCCESS) {
134 ldb_debug(ldb, LDB_DEBUG_ERROR,
135 "acl_module_init: Unable to register control with rootdse!\n");
136 return ldb_operr(ldb);
139 data = talloc(module, struct acl_private);
144 data->password_attrs = NULL;
145 data->acl_perform = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
146 NULL, "acl", "perform", false);
147 ldb_module_set_private(module, data);
149 mem_ctx = talloc_new(module);
154 ret = dsdb_module_search_dn(module, mem_ctx, &res,
155 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
157 DSDB_FLAG_NEXT_MODULE);
158 if (ret != LDB_SUCCESS) {
161 if (res->count == 0) {
165 if (res->count > 1) {
166 talloc_free(mem_ctx);
167 return LDB_ERR_CONSTRAINT_VIOLATION;
172 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
173 if (!password_attributes) {
176 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
177 if (!data->password_attrs) {
178 talloc_free(mem_ctx);
181 for (i=0; i < password_attributes->num_values; i++) {
182 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
183 talloc_steal(data->password_attrs, password_attributes->values[i].data);
185 data->password_attrs[i] = NULL;
188 talloc_free(mem_ctx);
189 return ldb_next_init(module);
192 static const struct GUID *get_oc_guid_from_message(struct ldb_module *module,
193 const struct dsdb_schema *schema,
194 struct ldb_message *msg)
196 struct ldb_message_element *oc_el;
198 oc_el = ldb_msg_find_element(msg, "objectClass");
203 return class_schemaid_guid_by_lDAPDisplayName(schema,
204 (char *)oc_el->values[oc_el->num_values-1].data);
207 static int acl_check_access_on_attribute(struct ldb_module *module,
209 struct security_descriptor *sd,
210 struct dom_sid *rp_sid,
212 const struct dsdb_attribute *attr)
216 uint32_t access_granted;
217 struct object_tree *root = NULL;
218 struct object_tree *new_node = NULL;
219 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
220 struct security_token *token = acl_user_token(module);
222 if (!GUID_all_zero(&attr->attributeSecurityGUID)) {
223 if (!insert_in_object_tree(tmp_ctx,
224 &attr->attributeSecurityGUID, access,
226 DEBUG(10, ("acl_search: cannot add to object tree securityGUID\n"));
230 if (!insert_in_object_tree(tmp_ctx,
231 &attr->schemaIDGUID, access, &new_node, &new_node)) {
232 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
237 if (!insert_in_object_tree(tmp_ctx,
238 &attr->schemaIDGUID, access, &root, &new_node)) {
239 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
244 status = sec_access_check_ds(sd, token,
249 if (!NT_STATUS_IS_OK(status)) {
250 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
255 talloc_free(tmp_ctx);
258 talloc_free(tmp_ctx);
259 return ldb_operr(ldb_module_get_ctx(module));
262 static int acl_check_access_on_class(struct ldb_module *module,
263 const struct dsdb_schema *schema,
265 struct security_descriptor *sd,
266 struct dom_sid *rp_sid,
268 const char *class_name)
272 uint32_t access_granted;
273 struct object_tree *root = NULL;
274 struct object_tree *new_node = NULL;
275 const struct GUID *guid;
276 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
277 struct security_token *token = acl_user_token(module);
279 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
281 DEBUG(10, ("acl_search: cannot find class %s\n",
285 if (!insert_in_object_tree(tmp_ctx,
288 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
292 status = sec_access_check_ds(sd, token,
297 if (!NT_STATUS_IS_OK(status)) {
298 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
305 return ldb_operr(ldb_module_get_ctx(module));
308 static int acl_allowedAttributes(struct ldb_module *module,
309 const struct dsdb_schema *schema,
310 struct ldb_message *sd_msg,
311 struct ldb_message *msg,
312 struct acl_context *ac)
314 struct ldb_message_element *oc_el;
315 struct ldb_context *ldb = ldb_module_get_ctx(module);
317 const char **attr_list;
320 /* If we don't have a schema yet, we can't do anything... */
321 if (schema == NULL) {
322 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
323 return LDB_ERR_OPERATIONS_ERROR;
326 /* Must remove any existing attribute */
327 if (ac->allowedAttributes) {
328 ldb_msg_remove_attr(msg, "allowedAttributes");
331 mem_ctx = talloc_new(msg);
336 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
337 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
339 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
340 talloc_free(mem_ctx);
341 return LDB_ERR_OPERATIONS_ERROR;
343 if (ac->allowedAttributes) {
344 for (i=0; attr_list && attr_list[i]; i++) {
345 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
348 if (ac->allowedAttributesEffective) {
349 struct security_descriptor *sd;
350 struct dom_sid *sid = NULL;
351 struct ldb_control *as_system = ldb_request_get_control(ac->req,
352 LDB_CONTROL_AS_SYSTEM_OID);
354 if (as_system != NULL) {
355 as_system->critical = 0;
358 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
359 if (ac->am_system || as_system) {
360 for (i=0; attr_list && attr_list[i]; i++) {
361 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
366 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
368 if (ret != LDB_SUCCESS) {
372 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
373 for (i=0; attr_list && attr_list[i]; i++) {
374 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
377 return ldb_operr(ldb);
379 /* remove constructed attributes */
380 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
382 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
385 ret = acl_check_access_on_attribute(module,
391 if (ret == LDB_SUCCESS) {
392 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
399 static int acl_childClasses(struct ldb_module *module,
400 const struct dsdb_schema *schema,
401 struct ldb_message *sd_msg,
402 struct ldb_message *msg,
403 const char *attrName)
405 struct ldb_message_element *oc_el;
406 struct ldb_message_element *allowedClasses;
407 const struct dsdb_class *sclass;
411 /* If we don't have a schema yet, we can't do anything... */
412 if (schema == NULL) {
413 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
414 return LDB_ERR_OPERATIONS_ERROR;
417 /* Must remove any existing attribute, or else confusion reins */
418 ldb_msg_remove_attr(msg, attrName);
419 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
420 if (ret != LDB_SUCCESS) {
424 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
426 for (i=0; oc_el && i < oc_el->num_values; i++) {
427 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
429 /* We don't know this class? what is going on? */
433 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
434 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
437 if (allowedClasses->num_values > 1) {
438 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
439 for (i=1 ; i < allowedClasses->num_values; i++) {
440 struct ldb_val *val1 = &allowedClasses->values[i-1];
441 struct ldb_val *val2 = &allowedClasses->values[i];
442 if (data_blob_cmp(val1, val2) == 0) {
443 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
444 allowedClasses->num_values--;
453 static int acl_childClassesEffective(struct ldb_module *module,
454 const struct dsdb_schema *schema,
455 struct ldb_message *sd_msg,
456 struct ldb_message *msg,
457 struct acl_context *ac)
459 struct ldb_message_element *oc_el;
460 struct ldb_message_element *allowedClasses = NULL;
461 const struct dsdb_class *sclass;
462 struct security_descriptor *sd;
463 struct ldb_control *as_system = ldb_request_get_control(ac->req,
464 LDB_CONTROL_AS_SYSTEM_OID);
465 struct dom_sid *sid = NULL;
469 if (as_system != NULL) {
470 as_system->critical = 0;
473 if (ac->am_system || as_system) {
474 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
477 /* If we don't have a schema yet, we can't do anything... */
478 if (schema == NULL) {
479 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
480 return LDB_ERR_OPERATIONS_ERROR;
483 /* Must remove any existing attribute, or else confusion reins */
484 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
486 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
487 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
488 if (ret != LDB_SUCCESS) {
492 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
493 for (i=0; oc_el && i < oc_el->num_values; i++) {
494 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
496 /* We don't know this class? what is going on? */
500 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
501 ret = acl_check_access_on_class(module,
506 SEC_ADS_CREATE_CHILD,
507 sclass->possibleInferiors[j]);
508 if (ret == LDB_SUCCESS) {
509 ldb_msg_add_string(msg, "allowedChildClassesEffective",
510 sclass->possibleInferiors[j]);
514 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
515 if (!allowedClasses) {
519 if (allowedClasses->num_values > 1) {
520 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
521 for (i=1 ; i < allowedClasses->num_values; i++) {
522 struct ldb_val *val1 = &allowedClasses->values[i-1];
523 struct ldb_val *val2 = &allowedClasses->values[i];
524 if (data_blob_cmp(val1, val2) == 0) {
525 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
526 allowedClasses->num_values--;
534 static int acl_sDRightsEffective(struct ldb_module *module,
535 struct ldb_message *sd_msg,
536 struct ldb_message *msg,
537 struct acl_context *ac)
539 struct ldb_message_element *rightsEffective;
541 struct security_descriptor *sd;
542 struct ldb_control *as_system = ldb_request_get_control(ac->req,
543 LDB_CONTROL_AS_SYSTEM_OID);
544 struct dom_sid *sid = NULL;
547 if (as_system != NULL) {
548 as_system->critical = 0;
551 /* Must remove any existing attribute, or else confusion reins */
552 ldb_msg_remove_attr(msg, "sDRightsEffective");
553 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
554 if (ret != LDB_SUCCESS) {
557 if (ac->am_system || as_system) {
558 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
561 /* Get the security descriptor from the message */
562 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
563 if (ret != LDB_SUCCESS) {
566 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
567 ret = acl_check_access_on_attribute(module,
573 if (ret == LDB_SUCCESS) {
574 flags |= SECINFO_OWNER | SECINFO_GROUP;
576 ret = acl_check_access_on_attribute(module,
582 if (ret == LDB_SUCCESS) {
583 flags |= SECINFO_DACL;
585 ret = acl_check_access_on_attribute(module,
589 SEC_FLAG_SYSTEM_SECURITY,
591 if (ret == LDB_SUCCESS) {
592 flags |= SECINFO_SACL;
595 ldb_msg_add_fmt(msg, "sDRightsEffective", "%u", flags);
599 static int acl_add(struct ldb_module *module, struct ldb_request *req)
602 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
603 struct ldb_context *ldb;
604 const struct dsdb_schema *schema;
605 struct ldb_message_element *oc_el;
606 const struct GUID *guid;
607 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
609 if (as_system != NULL) {
610 as_system->critical = 0;
613 if (dsdb_module_am_system(module) || as_system) {
614 return ldb_next_request(module, req);
617 if (ldb_dn_is_special(req->op.add.message->dn)) {
618 return ldb_next_request(module, req);
620 ldb = ldb_module_get_ctx(module);
621 /* Creating an NC. There is probably something we should do here,
622 * but we will establish that later */
623 /* FIXME: this has to be made dynamic at some point */
624 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
625 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
626 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_default_basedn(ldb))) == 0) ||
627 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_root_basedn(ldb))) == 0)) {
628 return ldb_next_request(module, req);
631 schema = dsdb_get_schema(ldb, req);
633 return ldb_operr(ldb);
636 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
637 if (!oc_el || oc_el->num_values == 0) {
638 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
639 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
642 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
643 (char *)oc_el->values[oc_el->num_values-1].data);
644 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
645 if (ret != LDB_SUCCESS) {
648 return ldb_next_request(module, req);
651 /* checks for validated writes */
652 static int acl_check_extended_right(TALLOC_CTX *mem_ctx,
653 struct security_descriptor *sd,
654 struct security_token *token,
655 const char *ext_right,
661 uint32_t access_granted;
662 struct object_tree *root = NULL;
663 struct object_tree *new_node = NULL;
664 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
666 GUID_from_string(ext_right, &right);
668 if (!insert_in_object_tree(tmp_ctx, &right, right_type,
670 DEBUG(10, ("acl_ext_right: cannot add to object tree\n"));
671 talloc_free(tmp_ctx);
672 return LDB_ERR_OPERATIONS_ERROR;
674 status = sec_access_check_ds(sd, token,
680 if (!NT_STATUS_IS_OK(status)) {
681 talloc_free(tmp_ctx);
682 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
684 talloc_free(tmp_ctx);
689 /* ckecks if modifications are allowed on "Member" attribute */
690 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
691 struct ldb_module *module,
692 struct ldb_request *req,
693 struct security_descriptor *sd,
695 const struct GUID *oc_guid,
696 const struct dsdb_attribute *attr)
700 struct ldb_context *ldb = ldb_module_get_ctx(module);
701 struct ldb_dn *user_dn;
702 struct ldb_message_element *member_el;
703 /* if we have wp, we can do whatever we like */
704 if (acl_check_access_on_attribute(module,
709 attr) == LDB_SUCCESS) {
712 /* if we are adding/deleting ourselves, check for self membership */
713 ret = dsdb_find_dn_by_sid(ldb, mem_ctx, acl_user_token(module)->user_sid, &user_dn);
714 if (ret != LDB_SUCCESS) {
717 member_el = ldb_msg_find_element(req->op.mod.message, "member");
719 return ldb_operr(ldb);
721 /* user can only remove oneself */
722 if (member_el->num_values == 0) {
723 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
725 for (i = 0; i < member_el->num_values; i++) {
726 if (strcasecmp((const char *)member_el->values[i].data,
727 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
728 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
731 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
732 GUID_DRS_SELF_MEMBERSHIP,
735 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
736 dsdb_acl_debug(sd, acl_user_token(module),
737 req->op.mod.message->dn,
744 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
745 struct ldb_module *module,
746 struct ldb_request *req,
747 struct security_descriptor *sd,
749 const struct GUID *oc_guid)
751 int ret = LDB_SUCCESS;
752 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
753 struct ldb_message_element *el;
754 struct ldb_message *msg;
755 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
756 "unicodePwd", "dBCSPwd", NULL }, **l;
757 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
759 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
761 return ldb_module_oom(module);
763 for (l = passwordAttrs; *l != NULL; l++) {
764 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
765 if (el->flags == LDB_FLAG_MOD_DELETE) {
768 if (el->flags == LDB_FLAG_MOD_ADD) {
771 if (el->flags == LDB_FLAG_MOD_REPLACE) {
774 ldb_msg_remove_element(msg, el);
777 /* a single delete will be handled by password hash
778 later in the stack, so we let it though here */
779 if (del_attr_cnt > 0 && add_attr_cnt == 0) {
780 talloc_free(tmp_ctx);
783 if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
784 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
785 GUID_DRS_FORCE_CHANGE_PASSWORD,
786 SEC_ADS_CONTROL_ACCESS,
789 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
790 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
791 GUID_DRS_USER_CHANGE_PASSWORD,
792 SEC_ADS_CONTROL_ACCESS,
794 /* Very strange, but we get constraint violation in this case */
795 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
796 ret = LDB_ERR_CONSTRAINT_VIOLATION;
799 if (ret != LDB_SUCCESS) {
800 dsdb_acl_debug(sd, acl_user_token(module),
801 req->op.mod.message->dn,
805 talloc_free(tmp_ctx);
809 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
812 struct ldb_context *ldb = ldb_module_get_ctx(module);
813 const struct dsdb_schema *schema;
815 const struct GUID *guid;
816 uint32_t access_granted;
817 struct object_tree *root = NULL;
818 struct object_tree *new_node = NULL;
820 struct ldb_result *acl_res;
821 struct security_descriptor *sd;
822 struct dom_sid *sid = NULL;
823 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
824 TALLOC_CTX *tmp_ctx = talloc_new(req);
825 static const char *acl_attrs[] = {
826 "nTSecurityDescriptor",
832 if (as_system != NULL) {
833 as_system->critical = 0;
836 /* Don't print this debug statement if elements[0].name is going to be NULL */
837 if(req->op.mod.message->num_elements > 0)
839 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
841 if (dsdb_module_am_system(module) || as_system) {
842 return ldb_next_request(module, req);
844 if (ldb_dn_is_special(req->op.mod.message->dn)) {
845 return ldb_next_request(module, req);
847 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
849 DSDB_FLAG_NEXT_MODULE);
851 if (ret != LDB_SUCCESS) {
855 schema = dsdb_get_schema(ldb, tmp_ctx);
857 ret = LDB_ERR_OPERATIONS_ERROR;
861 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
862 if (ret != LDB_SUCCESS) {
863 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
866 /* Theoretically we pass the check if the object has no sd */
871 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
873 DEBUG(10, ("acl_modify: cannot get guid\n"));
876 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
877 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
879 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
882 for (i=0; i < req->op.mod.message->num_elements; i++){
883 const struct dsdb_attribute *attr;
884 attr = dsdb_attribute_by_lDAPDisplayName(schema,
885 req->op.mod.message->elements[i].name);
887 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
888 status = sec_access_check_ds(sd, acl_user_token(module),
894 if (!NT_STATUS_IS_OK(status)) {
895 DEBUG(10, ("Object %s has no write dacl access\n",
896 ldb_dn_get_linearized(req->op.mod.message->dn)));
898 acl_user_token(module),
899 req->op.mod.message->dn,
902 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
906 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
907 ret = acl_check_self_membership(tmp_ctx,
914 if (ret != LDB_SUCCESS) {
918 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
919 /* this one is not affected by any rights, we should let it through
920 so that passwords_hash returns the correct error */
923 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
924 ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0 ||
925 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
926 ret = acl_check_password_rights(tmp_ctx,
932 if (ret != LDB_SUCCESS) {
937 /* This basic attribute existence check with the right errorcode
938 * is needed since this module is the first one which requests
939 * schema attribute informations.
940 * The complete attribute checking is done in the
941 * "objectclass_attrs" module behind this one.
944 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
945 req->op.mod.message->elements[i].name,
946 ldb_dn_get_linearized(req->op.mod.message->dn));
947 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
950 if (!insert_in_object_tree(tmp_ctx,
951 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
952 &new_node, &new_node)) {
953 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
954 ret = LDB_ERR_OPERATIONS_ERROR;
958 if (!insert_in_object_tree(tmp_ctx,
959 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
960 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
961 ret = LDB_ERR_OPERATIONS_ERROR;
967 if (root->num_of_children > 0) {
968 status = sec_access_check_ds(sd, acl_user_token(module),
974 if (!NT_STATUS_IS_OK(status)) {
975 DEBUG(10, ("Object %s has no write property access\n",
976 ldb_dn_get_linearized(req->op.mod.message->dn)));
978 acl_user_token(module),
979 req->op.mod.message->dn,
982 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
988 talloc_free(tmp_ctx);
989 return ldb_next_request(module, req);
991 talloc_free(tmp_ctx);
995 /* similar to the modify for the time being.
996 * We need to concider the special delete tree case, though - TODO */
997 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1000 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
1001 struct ldb_context *ldb;
1002 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1004 if (as_system != NULL) {
1005 as_system->critical = 0;
1008 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1009 if (dsdb_module_am_system(module) || as_system) {
1010 return ldb_next_request(module, req);
1013 if (ldb_dn_is_special(req->op.del.dn)) {
1014 return ldb_next_request(module, req);
1016 ldb = ldb_module_get_ctx(module);
1017 /* first check if we have delete object right */
1018 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
1019 if (ret == LDB_SUCCESS) {
1020 return ldb_next_request(module, req);
1023 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
1024 /* No parent, so check fails */
1025 /* FIXME: this has to be made dynamic at some point */
1026 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
1027 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
1028 (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0) ||
1029 (ldb_dn_compare(req->op.del.dn, (ldb_get_root_basedn(ldb))) == 0)) {
1030 DEBUG(10,("acl:deleting an NC\n"));
1031 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
1034 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
1035 if (ret != LDB_SUCCESS) {
1038 return ldb_next_request(module, req);
1041 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1044 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
1045 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
1046 const struct dsdb_schema *schema;
1047 struct ldb_context *ldb;
1048 struct security_descriptor *sd = NULL;
1049 struct dom_sid *sid = NULL;
1050 struct ldb_result *acl_res;
1051 const struct GUID *guid;
1052 struct object_tree *root = NULL;
1053 struct object_tree *new_node = NULL;
1054 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1055 TALLOC_CTX *tmp_ctx = talloc_new(req);
1057 uint32_t access_granted;
1058 const char *rdn_name;
1059 static const char *acl_attrs[] = {
1060 "nTSecurityDescriptor",
1066 if (as_system != NULL) {
1067 as_system->critical = 0;
1070 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1071 if (dsdb_module_am_system(module) || as_system) {
1072 return ldb_next_request(module, req);
1074 if (ldb_dn_is_special(req->op.rename.olddn)) {
1075 return ldb_next_request(module, req);
1077 ldb = ldb_module_get_ctx(module);
1079 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
1081 DSDB_FLAG_NEXT_MODULE |
1082 DSDB_SEARCH_SHOW_DELETED);
1083 /* we sould be able to find the parent */
1084 if (ret != LDB_SUCCESS) {
1085 DEBUG(10,("acl: failed to find object %s\n",
1086 ldb_dn_get_linearized(req->op.rename.olddn)));
1090 schema = dsdb_get_schema(ldb, acl_res);
1092 talloc_free(acl_res);
1093 return ldb_operr(ldb);
1096 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1097 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1098 &root, &new_node)) {
1099 return ldb_operr(ldb);
1102 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1104 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1105 &new_node, &new_node)) {
1106 return ldb_operr(ldb);
1109 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1110 if (rdn_name == NULL) {
1111 return ldb_operr(ldb);
1113 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1115 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1116 &new_node, &new_node)) {
1117 return ldb_operr(ldb);
1120 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1122 if (ret != LDB_SUCCESS) {
1123 return ldb_operr(ldb);
1125 /* Theoretically we pass the check if the object has no sd */
1129 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1130 status = sec_access_check_ds(sd, acl_user_token(module),
1136 if (!NT_STATUS_IS_OK(status)) {
1137 DEBUG(10, ("Object %s has no wp on name\n",
1138 ldb_dn_get_linearized(req->op.rename.olddn)));
1140 acl_user_token(module),
1141 req->op.rename.olddn,
1144 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1147 if (ldb_dn_compare(oldparent, newparent) == 0) {
1148 /* regular rename, not move, nothing more to do */
1149 return ldb_next_request(module, req);
1152 /* What exactly to do in this case? It would fail anyway.. */
1153 /* FIXME: this has to be made dynamic at some point */
1154 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
1155 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
1156 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0) ||
1157 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_root_basedn(ldb))) == 0)) {
1158 DEBUG(10,("acl:moving as an NC\n"));
1159 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1161 /* new parent should have create child */
1162 talloc_free(tmp_ctx);
1163 tmp_ctx = talloc_new(req);
1166 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1168 DEBUG(10,("acl:renamed object has no object class\n"));
1169 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1172 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
1173 if (ret != LDB_SUCCESS) {
1174 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1177 /* do we have delete object on the object? */
1179 status = sec_access_check_ds(sd, acl_user_token(module),
1185 if (NT_STATUS_IS_OK(status)) {
1186 return ldb_next_request(module, req);
1188 /* what about delete child on the current parent */
1189 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1190 if (ret != LDB_SUCCESS) {
1191 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1192 return ldb_module_done(req, NULL, NULL, ret);
1194 return ldb_next_request(module, req);
1197 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1199 struct ldb_context *ldb;
1200 struct acl_context *ac;
1201 struct acl_private *data;
1202 struct ldb_result *acl_res;
1203 static const char *acl_attrs[] = {
1205 "nTSecurityDescriptor",
1211 ac = talloc_get_type(req->context, struct acl_context);
1212 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1213 ldb = ldb_module_get_ctx(ac->module);
1216 return ldb_module_done(ac->req, NULL, NULL,
1217 LDB_ERR_OPERATIONS_ERROR);
1219 if (ares->error != LDB_SUCCESS) {
1220 return ldb_module_done(ac->req, ares->controls,
1221 ares->response, ares->error);
1224 switch (ares->type) {
1225 case LDB_REPLY_ENTRY:
1226 if (ac->allowedAttributes
1227 || ac->allowedChildClasses
1228 || ac->allowedChildClassesEffective
1229 || ac->allowedAttributesEffective
1230 || ac->sDRightsEffective) {
1231 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1233 DSDB_FLAG_NEXT_MODULE);
1234 if (ret != LDB_SUCCESS) {
1235 return ldb_module_done(ac->req, NULL, NULL, ret);
1237 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1238 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1239 if (ret != LDB_SUCCESS) {
1240 return ldb_module_done(ac->req, NULL, NULL, ret);
1243 if (ac->allowedChildClasses) {
1244 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1245 ares->message, "allowedChildClasses");
1246 if (ret != LDB_SUCCESS) {
1247 return ldb_module_done(ac->req, NULL, NULL, ret);
1250 if (ac->allowedChildClassesEffective) {
1251 ret = acl_childClassesEffective(ac->module, ac->schema,
1252 acl_res->msgs[0], ares->message, ac);
1253 if (ret != LDB_SUCCESS) {
1254 return ldb_module_done(ac->req, NULL, NULL, ret);
1257 if (ac->sDRightsEffective) {
1258 ret = acl_sDRightsEffective(ac->module,
1259 acl_res->msgs[0], ares->message, ac);
1260 if (ret != LDB_SUCCESS) {
1261 return ldb_module_done(ac->req, NULL, NULL, ret);
1265 if (data && data->password_attrs) {
1266 if (!ac->am_system) {
1267 for (i = 0; data->password_attrs[i]; i++) {
1268 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1272 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1274 case LDB_REPLY_REFERRAL:
1275 return ldb_module_send_referral(ac->req, ares->referral);
1277 case LDB_REPLY_DONE:
1278 return ldb_module_done(ac->req, ares->controls,
1279 ares->response, LDB_SUCCESS);
1285 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1287 struct ldb_context *ldb;
1288 struct acl_context *ac;
1289 struct ldb_request *down_req;
1290 struct acl_private *data;
1293 ldb = ldb_module_get_ctx(module);
1295 ac = talloc_zero(req, struct acl_context);
1297 return ldb_oom(ldb);
1299 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1301 ac->module = module;
1303 ac->am_system = dsdb_module_am_system(module);
1304 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1305 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1306 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1307 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1308 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1309 ac->schema = dsdb_get_schema(ldb, ac);
1311 /* replace any attributes in the parse tree that are private,
1312 so we don't allow a search for 'userPassword=penguin',
1313 just as we would not allow that attribute to be returned */
1314 if (ac->am_system) {
1315 /* FIXME: We should copy the tree and keep the original unmodified. */
1316 /* remove password attributes */
1317 if (data && data->password_attrs) {
1318 for (i = 0; data->password_attrs[i]; i++) {
1319 ldb_parse_tree_attr_replace(req->op.search.tree,
1320 data->password_attrs[i],
1321 "kludgeACLredactedattribute");
1325 ret = ldb_build_search_req_ex(&down_req,
1327 req->op.search.base,
1328 req->op.search.scope,
1329 req->op.search.tree,
1330 req->op.search.attrs,
1332 ac, acl_search_callback,
1334 if (ret != LDB_SUCCESS) {
1337 /* perform the search */
1338 return ldb_next_request(module, down_req);
1341 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1343 .search = acl_search,
1345 .modify = acl_modify,
1347 .rename = acl_rename,
1348 .init_context = acl_module_init