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 return ldb_next_request(module, req);
630 schema = dsdb_get_schema(ldb, req);
632 return ldb_operr(ldb);
635 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
636 if (!oc_el || oc_el->num_values == 0) {
637 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
638 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
641 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
642 (char *)oc_el->values[oc_el->num_values-1].data);
643 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
644 if (ret != LDB_SUCCESS) {
647 return ldb_next_request(module, req);
650 /* checks for validated writes */
651 static int acl_check_extended_right(TALLOC_CTX *mem_ctx,
652 struct security_descriptor *sd,
653 struct security_token *token,
654 const char *ext_right,
660 uint32_t access_granted;
661 struct object_tree *root = NULL;
662 struct object_tree *new_node = NULL;
663 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
665 GUID_from_string(ext_right, &right);
667 if (!insert_in_object_tree(tmp_ctx, &right, right_type,
669 DEBUG(10, ("acl_ext_right: cannot add to object tree\n"));
670 talloc_free(tmp_ctx);
671 return LDB_ERR_OPERATIONS_ERROR;
673 status = sec_access_check_ds(sd, token,
679 if (!NT_STATUS_IS_OK(status)) {
680 talloc_free(tmp_ctx);
681 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
683 talloc_free(tmp_ctx);
688 /* ckecks if modifications are allowed on "Member" attribute */
689 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
690 struct ldb_module *module,
691 struct ldb_request *req,
692 struct security_descriptor *sd,
694 const struct GUID *oc_guid,
695 const struct dsdb_attribute *attr)
699 struct ldb_context *ldb = ldb_module_get_ctx(module);
700 struct ldb_dn *user_dn;
701 struct ldb_message_element *member_el;
702 /* if we have wp, we can do whatever we like */
703 if (acl_check_access_on_attribute(module,
708 attr) == LDB_SUCCESS) {
711 /* if we are adding/deleting ourselves, check for self membership */
712 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
713 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
715 if (ret != LDB_SUCCESS) {
718 member_el = ldb_msg_find_element(req->op.mod.message, "member");
720 return ldb_operr(ldb);
722 /* user can only remove oneself */
723 if (member_el->num_values == 0) {
724 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
726 for (i = 0; i < member_el->num_values; i++) {
727 if (strcasecmp((const char *)member_el->values[i].data,
728 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
729 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
732 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
733 GUID_DRS_SELF_MEMBERSHIP,
736 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
737 dsdb_acl_debug(sd, acl_user_token(module),
738 req->op.mod.message->dn,
745 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
746 struct ldb_module *module,
747 struct ldb_request *req,
748 struct security_descriptor *sd,
750 const struct GUID *oc_guid)
752 int ret = LDB_SUCCESS;
753 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
754 struct ldb_message_element *el;
755 struct ldb_message *msg;
756 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
757 "unicodePwd", "dBCSPwd", NULL }, **l;
758 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
760 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
762 return ldb_module_oom(module);
764 for (l = passwordAttrs; *l != NULL; l++) {
765 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
766 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
769 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
772 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
775 ldb_msg_remove_element(msg, el);
778 /* a single delete will be handled by password hash
779 later in the stack, so we let it though here */
780 if (del_attr_cnt > 0 && add_attr_cnt == 0) {
781 talloc_free(tmp_ctx);
785 if (ldb_request_get_control(req,
786 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
787 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
788 * have a user password change and not a set as the message
789 * looks like. In it's value blob it contains the NT and/or LM
790 * hash of the old password specified by the user.
791 * This control is used by the SAMR and "kpasswd" password
792 * change mechanisms. */
793 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
794 GUID_DRS_USER_CHANGE_PASSWORD,
795 SEC_ADS_CONTROL_ACCESS,
798 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
799 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
800 GUID_DRS_FORCE_CHANGE_PASSWORD,
801 SEC_ADS_CONTROL_ACCESS,
804 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
805 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
806 GUID_DRS_USER_CHANGE_PASSWORD,
807 SEC_ADS_CONTROL_ACCESS,
809 /* Very strange, but we get constraint violation in this case */
810 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
811 ret = LDB_ERR_CONSTRAINT_VIOLATION;
814 if (ret != LDB_SUCCESS) {
815 dsdb_acl_debug(sd, acl_user_token(module),
816 req->op.mod.message->dn,
820 talloc_free(tmp_ctx);
824 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
827 struct ldb_context *ldb = ldb_module_get_ctx(module);
828 const struct dsdb_schema *schema;
830 const struct GUID *guid;
831 uint32_t access_granted;
832 struct object_tree *root = NULL;
833 struct object_tree *new_node = NULL;
835 struct ldb_result *acl_res;
836 struct security_descriptor *sd;
837 struct dom_sid *sid = NULL;
838 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
839 TALLOC_CTX *tmp_ctx = talloc_new(req);
840 static const char *acl_attrs[] = {
841 "nTSecurityDescriptor",
847 if (as_system != NULL) {
848 as_system->critical = 0;
851 /* Don't print this debug statement if elements[0].name is going to be NULL */
852 if(req->op.mod.message->num_elements > 0)
854 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
856 if (dsdb_module_am_system(module) || as_system) {
857 return ldb_next_request(module, req);
859 if (ldb_dn_is_special(req->op.mod.message->dn)) {
860 return ldb_next_request(module, req);
862 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
864 DSDB_FLAG_NEXT_MODULE);
866 if (ret != LDB_SUCCESS) {
870 schema = dsdb_get_schema(ldb, tmp_ctx);
872 ret = LDB_ERR_OPERATIONS_ERROR;
876 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
877 if (ret != LDB_SUCCESS) {
878 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
881 /* Theoretically we pass the check if the object has no sd */
886 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
888 DEBUG(10, ("acl_modify: cannot get guid\n"));
891 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
892 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
894 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
897 for (i=0; i < req->op.mod.message->num_elements; i++){
898 const struct dsdb_attribute *attr;
899 attr = dsdb_attribute_by_lDAPDisplayName(schema,
900 req->op.mod.message->elements[i].name);
902 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
903 status = sec_access_check_ds(sd, acl_user_token(module),
909 if (!NT_STATUS_IS_OK(status)) {
910 DEBUG(10, ("Object %s has no write dacl access\n",
911 ldb_dn_get_linearized(req->op.mod.message->dn)));
913 acl_user_token(module),
914 req->op.mod.message->dn,
917 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
921 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
922 ret = acl_check_self_membership(tmp_ctx,
929 if (ret != LDB_SUCCESS) {
933 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
934 /* this one is not affected by any rights, we should let it through
935 so that passwords_hash returns the correct error */
938 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
939 ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0 ||
940 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
941 ret = acl_check_password_rights(tmp_ctx,
947 if (ret != LDB_SUCCESS) {
952 /* This basic attribute existence check with the right errorcode
953 * is needed since this module is the first one which requests
954 * schema attribute informations.
955 * The complete attribute checking is done in the
956 * "objectclass_attrs" module behind this one.
959 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
960 req->op.mod.message->elements[i].name,
961 ldb_dn_get_linearized(req->op.mod.message->dn));
962 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
965 if (!insert_in_object_tree(tmp_ctx,
966 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
967 &new_node, &new_node)) {
968 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
969 ret = LDB_ERR_OPERATIONS_ERROR;
973 if (!insert_in_object_tree(tmp_ctx,
974 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
975 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
976 ret = LDB_ERR_OPERATIONS_ERROR;
982 if (root->num_of_children > 0) {
983 status = sec_access_check_ds(sd, acl_user_token(module),
989 if (!NT_STATUS_IS_OK(status)) {
990 DEBUG(10, ("Object %s has no write property access\n",
991 ldb_dn_get_linearized(req->op.mod.message->dn)));
993 acl_user_token(module),
994 req->op.mod.message->dn,
997 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1003 talloc_free(tmp_ctx);
1004 return ldb_next_request(module, req);
1006 talloc_free(tmp_ctx);
1010 /* similar to the modify for the time being.
1011 * We need to concider the special delete tree case, though - TODO */
1012 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1015 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
1016 struct ldb_context *ldb;
1017 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1019 if (as_system != NULL) {
1020 as_system->critical = 0;
1023 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1024 if (dsdb_module_am_system(module) || as_system) {
1025 return ldb_next_request(module, req);
1028 if (ldb_dn_is_special(req->op.del.dn)) {
1029 return ldb_next_request(module, req);
1031 ldb = ldb_module_get_ctx(module);
1032 /* first check if we have delete object right */
1033 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
1034 if (ret == LDB_SUCCESS) {
1035 return ldb_next_request(module, req);
1038 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
1039 /* No parent, so check fails */
1040 /* FIXME: this has to be made dynamic at some point */
1041 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
1042 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
1043 (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0)) {
1044 DEBUG(10,("acl:deleting an NC\n"));
1045 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
1048 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
1049 if (ret != LDB_SUCCESS) {
1052 return ldb_next_request(module, req);
1055 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1058 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
1059 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
1060 const struct dsdb_schema *schema;
1061 struct ldb_context *ldb;
1062 struct security_descriptor *sd = NULL;
1063 struct dom_sid *sid = NULL;
1064 struct ldb_result *acl_res;
1065 const struct GUID *guid;
1066 struct object_tree *root = NULL;
1067 struct object_tree *new_node = NULL;
1068 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1069 TALLOC_CTX *tmp_ctx = talloc_new(req);
1071 uint32_t access_granted;
1072 const char *rdn_name;
1073 static const char *acl_attrs[] = {
1074 "nTSecurityDescriptor",
1080 if (as_system != NULL) {
1081 as_system->critical = 0;
1084 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1085 if (dsdb_module_am_system(module) || as_system) {
1086 return ldb_next_request(module, req);
1088 if (ldb_dn_is_special(req->op.rename.olddn)) {
1089 return ldb_next_request(module, req);
1091 ldb = ldb_module_get_ctx(module);
1093 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
1095 DSDB_FLAG_NEXT_MODULE |
1096 DSDB_SEARCH_SHOW_DELETED);
1097 /* we sould be able to find the parent */
1098 if (ret != LDB_SUCCESS) {
1099 DEBUG(10,("acl: failed to find object %s\n",
1100 ldb_dn_get_linearized(req->op.rename.olddn)));
1104 schema = dsdb_get_schema(ldb, acl_res);
1106 talloc_free(acl_res);
1107 return ldb_operr(ldb);
1110 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1111 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1112 &root, &new_node)) {
1113 return ldb_operr(ldb);
1116 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1118 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1119 &new_node, &new_node)) {
1120 return ldb_operr(ldb);
1123 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1124 if (rdn_name == NULL) {
1125 return ldb_operr(ldb);
1127 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1129 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1130 &new_node, &new_node)) {
1131 return ldb_operr(ldb);
1134 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1136 if (ret != LDB_SUCCESS) {
1137 return ldb_operr(ldb);
1139 /* Theoretically we pass the check if the object has no sd */
1143 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1144 status = sec_access_check_ds(sd, acl_user_token(module),
1150 if (!NT_STATUS_IS_OK(status)) {
1151 DEBUG(10, ("Object %s has no wp on name\n",
1152 ldb_dn_get_linearized(req->op.rename.olddn)));
1154 acl_user_token(module),
1155 req->op.rename.olddn,
1158 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1161 if (ldb_dn_compare(oldparent, newparent) == 0) {
1162 /* regular rename, not move, nothing more to do */
1163 return ldb_next_request(module, req);
1166 /* What exactly to do in this case? It would fail anyway.. */
1167 /* FIXME: this has to be made dynamic at some point */
1168 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
1169 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
1170 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0)) {
1171 DEBUG(10,("acl:moving as an NC\n"));
1172 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1174 /* new parent should have create child */
1175 talloc_free(tmp_ctx);
1176 tmp_ctx = talloc_new(req);
1179 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1181 DEBUG(10,("acl:renamed object has no object class\n"));
1182 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1185 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
1186 if (ret != LDB_SUCCESS) {
1187 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1190 /* do we have delete object on the object? */
1192 status = sec_access_check_ds(sd, acl_user_token(module),
1198 if (NT_STATUS_IS_OK(status)) {
1199 return ldb_next_request(module, req);
1201 /* what about delete child on the current parent */
1202 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1203 if (ret != LDB_SUCCESS) {
1204 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1205 return ldb_module_done(req, NULL, NULL, ret);
1207 return ldb_next_request(module, req);
1210 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1212 struct ldb_context *ldb;
1213 struct acl_context *ac;
1214 struct acl_private *data;
1215 struct ldb_result *acl_res;
1216 static const char *acl_attrs[] = {
1218 "nTSecurityDescriptor",
1224 ac = talloc_get_type(req->context, struct acl_context);
1225 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1226 ldb = ldb_module_get_ctx(ac->module);
1229 return ldb_module_done(ac->req, NULL, NULL,
1230 LDB_ERR_OPERATIONS_ERROR);
1232 if (ares->error != LDB_SUCCESS) {
1233 return ldb_module_done(ac->req, ares->controls,
1234 ares->response, ares->error);
1237 switch (ares->type) {
1238 case LDB_REPLY_ENTRY:
1239 if (ac->allowedAttributes
1240 || ac->allowedChildClasses
1241 || ac->allowedChildClassesEffective
1242 || ac->allowedAttributesEffective
1243 || ac->sDRightsEffective) {
1244 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1246 DSDB_FLAG_NEXT_MODULE);
1247 if (ret != LDB_SUCCESS) {
1248 return ldb_module_done(ac->req, NULL, NULL, ret);
1250 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1251 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1252 if (ret != LDB_SUCCESS) {
1253 return ldb_module_done(ac->req, NULL, NULL, ret);
1256 if (ac->allowedChildClasses) {
1257 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1258 ares->message, "allowedChildClasses");
1259 if (ret != LDB_SUCCESS) {
1260 return ldb_module_done(ac->req, NULL, NULL, ret);
1263 if (ac->allowedChildClassesEffective) {
1264 ret = acl_childClassesEffective(ac->module, ac->schema,
1265 acl_res->msgs[0], ares->message, ac);
1266 if (ret != LDB_SUCCESS) {
1267 return ldb_module_done(ac->req, NULL, NULL, ret);
1270 if (ac->sDRightsEffective) {
1271 ret = acl_sDRightsEffective(ac->module,
1272 acl_res->msgs[0], ares->message, ac);
1273 if (ret != LDB_SUCCESS) {
1274 return ldb_module_done(ac->req, NULL, NULL, ret);
1278 if (data && data->password_attrs) {
1279 if (!ac->am_system) {
1280 for (i = 0; data->password_attrs[i]; i++) {
1281 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1285 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1287 case LDB_REPLY_REFERRAL:
1288 return ldb_module_send_referral(ac->req, ares->referral);
1290 case LDB_REPLY_DONE:
1291 return ldb_module_done(ac->req, ares->controls,
1292 ares->response, LDB_SUCCESS);
1298 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1300 struct ldb_context *ldb;
1301 struct acl_context *ac;
1302 struct ldb_request *down_req;
1303 struct acl_private *data;
1306 ldb = ldb_module_get_ctx(module);
1308 ac = talloc_zero(req, struct acl_context);
1310 return ldb_oom(ldb);
1312 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1314 ac->module = module;
1316 ac->am_system = dsdb_module_am_system(module);
1317 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1318 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1319 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1320 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1321 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1322 ac->schema = dsdb_get_schema(ldb, ac);
1324 /* replace any attributes in the parse tree that are private,
1325 so we don't allow a search for 'userPassword=penguin',
1326 just as we would not allow that attribute to be returned */
1327 if (ac->am_system) {
1328 /* FIXME: We should copy the tree and keep the original unmodified. */
1329 /* remove password attributes */
1330 if (data && data->password_attrs) {
1331 for (i = 0; data->password_attrs[i]; i++) {
1332 ldb_parse_tree_attr_replace(req->op.search.tree,
1333 data->password_attrs[i],
1334 "kludgeACLredactedattribute");
1338 ret = ldb_build_search_req_ex(&down_req,
1340 req->op.search.base,
1341 req->op.search.scope,
1342 req->op.search.tree,
1343 req->op.search.attrs,
1345 ac, acl_search_callback,
1347 if (ret != LDB_SUCCESS) {
1350 /* perform the search */
1351 return ldb_next_request(module, down_req);
1354 static const char *acl_user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module)
1356 struct ldb_context *ldb = ldb_module_get_ctx(module);
1357 struct auth_session_info *session_info
1358 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
1359 if (!session_info) {
1360 return "UNKNOWN (NULL)";
1363 return talloc_asprintf(mem_ctx, "%s\\%s",
1364 session_info->server_info->domain_name,
1365 session_info->server_info->account_name);
1368 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1370 struct ldb_context *ldb = ldb_module_get_ctx(module);
1371 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1373 /* allow everybody to read the sequence number */
1374 if (strcmp(req->op.extended.oid,
1375 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1376 return ldb_next_request(module, req);
1379 if (dsdb_module_am_system(module) ||
1380 dsdb_module_am_administrator(module) || as_system) {
1381 return ldb_next_request(module, req);
1383 ldb_asprintf_errstring(ldb,
1385 "attempted database modify not permitted. "
1386 "User %s is not SYSTEM or an administrator",
1387 acl_user_name(req, module));
1388 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1392 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1394 .search = acl_search,
1396 .modify = acl_modify,
1398 .rename = acl_rename,
1399 .extended = acl_extended,
1400 .init_context = acl_module_init