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 int acl_module_init(struct ldb_module *module)
69 struct ldb_context *ldb;
70 struct acl_private *data;
73 static const char *attrs[] = { "passwordAttribute", NULL };
74 struct ldb_result *res;
75 struct ldb_message *msg;
76 struct ldb_message_element *password_attributes;
78 ldb = ldb_module_get_ctx(module);
80 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
81 if (ret != LDB_SUCCESS) {
82 ldb_debug(ldb, LDB_DEBUG_ERROR,
83 "acl_module_init: Unable to register control with rootdse!\n");
84 return ldb_operr(ldb);
87 data = talloc(module, struct acl_private);
92 data->password_attrs = NULL;
93 data->acl_perform = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
94 NULL, "acl", "perform", false);
95 ldb_module_set_private(module, data);
97 mem_ctx = talloc_new(module);
102 ret = dsdb_module_search_dn(module, mem_ctx, &res,
103 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
105 DSDB_FLAG_NEXT_MODULE);
106 if (ret != LDB_SUCCESS) {
109 if (res->count == 0) {
113 if (res->count > 1) {
114 talloc_free(mem_ctx);
115 return LDB_ERR_CONSTRAINT_VIOLATION;
120 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
121 if (!password_attributes) {
124 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
125 if (!data->password_attrs) {
126 talloc_free(mem_ctx);
129 for (i=0; i < password_attributes->num_values; i++) {
130 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
131 talloc_steal(data->password_attrs, password_attributes->values[i].data);
133 data->password_attrs[i] = NULL;
136 talloc_free(mem_ctx);
137 return ldb_next_init(module);
140 static int acl_allowedAttributes(struct ldb_module *module,
141 const struct dsdb_schema *schema,
142 struct ldb_message *sd_msg,
143 struct ldb_message *msg,
144 struct acl_context *ac)
146 struct ldb_message_element *oc_el;
147 struct ldb_context *ldb = ldb_module_get_ctx(module);
149 const char **attr_list;
152 /* If we don't have a schema yet, we can't do anything... */
153 if (schema == NULL) {
154 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
155 return LDB_ERR_OPERATIONS_ERROR;
158 /* Must remove any existing attribute */
159 if (ac->allowedAttributes) {
160 ldb_msg_remove_attr(msg, "allowedAttributes");
163 mem_ctx = talloc_new(msg);
168 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
169 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
171 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
172 talloc_free(mem_ctx);
173 return LDB_ERR_OPERATIONS_ERROR;
175 if (ac->allowedAttributes) {
176 for (i=0; attr_list && attr_list[i]; i++) {
177 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
180 if (ac->allowedAttributesEffective) {
181 struct security_descriptor *sd;
182 struct dom_sid *sid = NULL;
183 struct ldb_control *as_system = ldb_request_get_control(ac->req,
184 LDB_CONTROL_AS_SYSTEM_OID);
186 if (as_system != NULL) {
187 as_system->critical = 0;
190 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
191 if (ac->am_system || as_system) {
192 for (i=0; attr_list && attr_list[i]; i++) {
193 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
198 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
200 if (ret != LDB_SUCCESS) {
204 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
205 for (i=0; attr_list && attr_list[i]; i++) {
206 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
209 return ldb_operr(ldb);
211 /* remove constructed attributes */
212 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
214 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
217 ret = acl_check_access_on_attribute(module,
223 if (ret == LDB_SUCCESS) {
224 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
231 static int acl_childClasses(struct ldb_module *module,
232 const struct dsdb_schema *schema,
233 struct ldb_message *sd_msg,
234 struct ldb_message *msg,
235 const char *attrName)
237 struct ldb_message_element *oc_el;
238 struct ldb_message_element *allowedClasses;
239 const struct dsdb_class *sclass;
243 /* If we don't have a schema yet, we can't do anything... */
244 if (schema == NULL) {
245 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
246 return LDB_ERR_OPERATIONS_ERROR;
249 /* Must remove any existing attribute, or else confusion reins */
250 ldb_msg_remove_attr(msg, attrName);
251 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
252 if (ret != LDB_SUCCESS) {
256 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
258 for (i=0; oc_el && i < oc_el->num_values; i++) {
259 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
261 /* We don't know this class? what is going on? */
265 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
266 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
269 if (allowedClasses->num_values > 1) {
270 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
271 for (i=1 ; i < allowedClasses->num_values; i++) {
272 struct ldb_val *val1 = &allowedClasses->values[i-1];
273 struct ldb_val *val2 = &allowedClasses->values[i];
274 if (data_blob_cmp(val1, val2) == 0) {
275 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
276 allowedClasses->num_values--;
285 static int acl_childClassesEffective(struct ldb_module *module,
286 const struct dsdb_schema *schema,
287 struct ldb_message *sd_msg,
288 struct ldb_message *msg,
289 struct acl_context *ac)
291 struct ldb_message_element *oc_el;
292 struct ldb_message_element *allowedClasses = NULL;
293 const struct dsdb_class *sclass;
294 struct security_descriptor *sd;
295 struct ldb_control *as_system = ldb_request_get_control(ac->req,
296 LDB_CONTROL_AS_SYSTEM_OID);
297 struct dom_sid *sid = NULL;
301 if (as_system != NULL) {
302 as_system->critical = 0;
305 if (ac->am_system || as_system) {
306 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
309 /* If we don't have a schema yet, we can't do anything... */
310 if (schema == NULL) {
311 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
312 return LDB_ERR_OPERATIONS_ERROR;
315 /* Must remove any existing attribute, or else confusion reins */
316 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
318 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
319 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
320 if (ret != LDB_SUCCESS) {
324 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
325 for (i=0; oc_el && i < oc_el->num_values; i++) {
326 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
328 /* We don't know this class? what is going on? */
332 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
333 ret = acl_check_access_on_class(module,
338 SEC_ADS_CREATE_CHILD,
339 sclass->possibleInferiors[j]);
340 if (ret == LDB_SUCCESS) {
341 ldb_msg_add_string(msg, "allowedChildClassesEffective",
342 sclass->possibleInferiors[j]);
346 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
347 if (!allowedClasses) {
351 if (allowedClasses->num_values > 1) {
352 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
353 for (i=1 ; i < allowedClasses->num_values; i++) {
354 struct ldb_val *val1 = &allowedClasses->values[i-1];
355 struct ldb_val *val2 = &allowedClasses->values[i];
356 if (data_blob_cmp(val1, val2) == 0) {
357 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
358 allowedClasses->num_values--;
366 static int acl_sDRightsEffective(struct ldb_module *module,
367 struct ldb_message *sd_msg,
368 struct ldb_message *msg,
369 struct acl_context *ac)
371 struct ldb_message_element *rightsEffective;
373 struct security_descriptor *sd;
374 struct ldb_control *as_system = ldb_request_get_control(ac->req,
375 LDB_CONTROL_AS_SYSTEM_OID);
376 struct dom_sid *sid = NULL;
379 if (as_system != NULL) {
380 as_system->critical = 0;
383 /* Must remove any existing attribute, or else confusion reins */
384 ldb_msg_remove_attr(msg, "sDRightsEffective");
385 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
386 if (ret != LDB_SUCCESS) {
389 if (ac->am_system || as_system) {
390 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
393 /* Get the security descriptor from the message */
394 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
395 if (ret != LDB_SUCCESS) {
398 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
399 ret = acl_check_access_on_attribute(module,
405 if (ret == LDB_SUCCESS) {
406 flags |= SECINFO_OWNER | SECINFO_GROUP;
408 ret = acl_check_access_on_attribute(module,
414 if (ret == LDB_SUCCESS) {
415 flags |= SECINFO_DACL;
417 ret = acl_check_access_on_attribute(module,
421 SEC_FLAG_SYSTEM_SECURITY,
423 if (ret == LDB_SUCCESS) {
424 flags |= SECINFO_SACL;
427 ldb_msg_add_fmt(msg, "sDRightsEffective", "%u", flags);
431 static int acl_add(struct ldb_module *module, struct ldb_request *req)
434 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
435 struct ldb_context *ldb;
436 const struct dsdb_schema *schema;
437 struct ldb_message_element *oc_el;
438 const struct GUID *guid;
439 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
441 if (as_system != NULL) {
442 as_system->critical = 0;
445 if (dsdb_module_am_system(module) || as_system) {
446 return ldb_next_request(module, req);
449 if (ldb_dn_is_special(req->op.add.message->dn)) {
450 return ldb_next_request(module, req);
452 ldb = ldb_module_get_ctx(module);
453 /* Creating an NC. There is probably something we should do here,
454 * but we will establish that later */
455 /* FIXME: this has to be made dynamic at some point */
456 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
457 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
458 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_default_basedn(ldb))) == 0)) {
459 return ldb_next_request(module, req);
462 schema = dsdb_get_schema(ldb, req);
464 return ldb_operr(ldb);
467 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
468 if (!oc_el || oc_el->num_values == 0) {
469 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
470 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
473 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
474 (char *)oc_el->values[oc_el->num_values-1].data);
475 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
476 if (ret != LDB_SUCCESS) {
479 return ldb_next_request(module, req);
482 /* ckecks if modifications are allowed on "Member" attribute */
483 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
484 struct ldb_module *module,
485 struct ldb_request *req,
486 struct security_descriptor *sd,
488 const struct GUID *oc_guid,
489 const struct dsdb_attribute *attr)
493 struct ldb_context *ldb = ldb_module_get_ctx(module);
494 struct ldb_dn *user_dn;
495 struct ldb_message_element *member_el;
496 /* if we have wp, we can do whatever we like */
497 if (acl_check_access_on_attribute(module,
502 attr) == LDB_SUCCESS) {
505 /* if we are adding/deleting ourselves, check for self membership */
506 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
507 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
509 if (ret != LDB_SUCCESS) {
512 member_el = ldb_msg_find_element(req->op.mod.message, "member");
514 return ldb_operr(ldb);
516 /* user can only remove oneself */
517 if (member_el->num_values == 0) {
518 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
520 for (i = 0; i < member_el->num_values; i++) {
521 if (strcasecmp((const char *)member_el->values[i].data,
522 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
523 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
526 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
527 GUID_DRS_SELF_MEMBERSHIP,
530 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
531 dsdb_acl_debug(sd, acl_user_token(module),
532 req->op.mod.message->dn,
539 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
540 struct ldb_module *module,
541 struct ldb_request *req,
542 struct security_descriptor *sd,
544 const struct GUID *oc_guid)
546 int ret = LDB_SUCCESS;
547 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
548 struct ldb_message_element *el;
549 struct ldb_message *msg;
550 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
551 "unicodePwd", "dBCSPwd", NULL }, **l;
552 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
554 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
556 return ldb_module_oom(module);
558 for (l = passwordAttrs; *l != NULL; l++) {
559 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
560 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
563 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
566 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
569 ldb_msg_remove_element(msg, el);
572 /* a single delete will be handled by password hash
573 later in the stack, so we let it though here */
574 if (del_attr_cnt > 0 && add_attr_cnt == 0) {
575 talloc_free(tmp_ctx);
579 if (ldb_request_get_control(req,
580 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
581 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
582 * have a user password change and not a set as the message
583 * looks like. In it's value blob it contains the NT and/or LM
584 * hash of the old password specified by the user.
585 * This control is used by the SAMR and "kpasswd" password
586 * change mechanisms. */
587 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
588 GUID_DRS_USER_CHANGE_PASSWORD,
589 SEC_ADS_CONTROL_ACCESS,
592 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
593 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
594 GUID_DRS_FORCE_CHANGE_PASSWORD,
595 SEC_ADS_CONTROL_ACCESS,
598 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
599 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
600 GUID_DRS_USER_CHANGE_PASSWORD,
601 SEC_ADS_CONTROL_ACCESS,
603 /* Very strange, but we get constraint violation in this case */
604 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
605 ret = LDB_ERR_CONSTRAINT_VIOLATION;
608 if (ret != LDB_SUCCESS) {
609 dsdb_acl_debug(sd, acl_user_token(module),
610 req->op.mod.message->dn,
614 talloc_free(tmp_ctx);
618 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
621 struct ldb_context *ldb = ldb_module_get_ctx(module);
622 const struct dsdb_schema *schema;
624 const struct GUID *guid;
625 uint32_t access_granted;
626 struct object_tree *root = NULL;
627 struct object_tree *new_node = NULL;
629 struct ldb_result *acl_res;
630 struct security_descriptor *sd;
631 struct dom_sid *sid = NULL;
632 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
633 TALLOC_CTX *tmp_ctx = talloc_new(req);
634 static const char *acl_attrs[] = {
635 "nTSecurityDescriptor",
641 if (as_system != NULL) {
642 as_system->critical = 0;
645 /* Don't print this debug statement if elements[0].name is going to be NULL */
646 if(req->op.mod.message->num_elements > 0)
648 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
650 if (dsdb_module_am_system(module) || as_system) {
651 return ldb_next_request(module, req);
653 if (ldb_dn_is_special(req->op.mod.message->dn)) {
654 return ldb_next_request(module, req);
656 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
658 DSDB_FLAG_NEXT_MODULE);
660 if (ret != LDB_SUCCESS) {
664 schema = dsdb_get_schema(ldb, tmp_ctx);
666 ret = LDB_ERR_OPERATIONS_ERROR;
670 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
671 if (ret != LDB_SUCCESS) {
672 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
675 /* Theoretically we pass the check if the object has no sd */
680 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
682 DEBUG(10, ("acl_modify: cannot get guid\n"));
685 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
686 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
688 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
691 for (i=0; i < req->op.mod.message->num_elements; i++){
692 const struct dsdb_attribute *attr;
693 attr = dsdb_attribute_by_lDAPDisplayName(schema,
694 req->op.mod.message->elements[i].name);
696 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
697 status = sec_access_check_ds(sd, acl_user_token(module),
703 if (!NT_STATUS_IS_OK(status)) {
704 DEBUG(10, ("Object %s has no write dacl access\n",
705 ldb_dn_get_linearized(req->op.mod.message->dn)));
707 acl_user_token(module),
708 req->op.mod.message->dn,
711 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
715 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
716 ret = acl_check_self_membership(tmp_ctx,
723 if (ret != LDB_SUCCESS) {
727 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
728 /* this one is not affected by any rights, we should let it through
729 so that passwords_hash returns the correct error */
732 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
733 ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0 ||
734 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
735 ret = acl_check_password_rights(tmp_ctx,
741 if (ret != LDB_SUCCESS) {
746 /* This basic attribute existence check with the right errorcode
747 * is needed since this module is the first one which requests
748 * schema attribute informations.
749 * The complete attribute checking is done in the
750 * "objectclass_attrs" module behind this one.
753 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
754 req->op.mod.message->elements[i].name,
755 ldb_dn_get_linearized(req->op.mod.message->dn));
756 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
759 if (!insert_in_object_tree(tmp_ctx,
760 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
761 &new_node, &new_node)) {
762 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
763 ret = LDB_ERR_OPERATIONS_ERROR;
767 if (!insert_in_object_tree(tmp_ctx,
768 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
769 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
770 ret = LDB_ERR_OPERATIONS_ERROR;
776 if (root->num_of_children > 0) {
777 status = sec_access_check_ds(sd, acl_user_token(module),
783 if (!NT_STATUS_IS_OK(status)) {
784 DEBUG(10, ("Object %s has no write property access\n",
785 ldb_dn_get_linearized(req->op.mod.message->dn)));
787 acl_user_token(module),
788 req->op.mod.message->dn,
791 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
797 talloc_free(tmp_ctx);
798 return ldb_next_request(module, req);
800 talloc_free(tmp_ctx);
804 /* similar to the modify for the time being.
805 * We need to concider the special delete tree case, though - TODO */
806 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
809 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
810 struct ldb_context *ldb;
811 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
813 if (as_system != NULL) {
814 as_system->critical = 0;
817 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
818 if (dsdb_module_am_system(module) || as_system) {
819 return ldb_next_request(module, req);
822 if (ldb_dn_is_special(req->op.del.dn)) {
823 return ldb_next_request(module, req);
825 ldb = ldb_module_get_ctx(module);
826 /* first check if we have delete object right */
827 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
828 if (ret == LDB_SUCCESS) {
829 return ldb_next_request(module, req);
832 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
833 /* No parent, so check fails */
834 /* FIXME: this has to be made dynamic at some point */
835 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
836 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
837 (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0)) {
838 DEBUG(10,("acl:deleting an NC\n"));
839 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
842 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
843 if (ret != LDB_SUCCESS) {
846 return ldb_next_request(module, req);
849 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
852 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
853 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
854 const struct dsdb_schema *schema;
855 struct ldb_context *ldb;
856 struct security_descriptor *sd = NULL;
857 struct dom_sid *sid = NULL;
858 struct ldb_result *acl_res;
859 const struct GUID *guid;
860 struct object_tree *root = NULL;
861 struct object_tree *new_node = NULL;
862 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
863 TALLOC_CTX *tmp_ctx = talloc_new(req);
865 uint32_t access_granted;
866 const char *rdn_name;
867 static const char *acl_attrs[] = {
868 "nTSecurityDescriptor",
874 if (as_system != NULL) {
875 as_system->critical = 0;
878 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
879 if (dsdb_module_am_system(module) || as_system) {
880 return ldb_next_request(module, req);
882 if (ldb_dn_is_special(req->op.rename.olddn)) {
883 return ldb_next_request(module, req);
885 ldb = ldb_module_get_ctx(module);
887 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
889 DSDB_FLAG_NEXT_MODULE |
890 DSDB_SEARCH_SHOW_DELETED);
891 /* we sould be able to find the parent */
892 if (ret != LDB_SUCCESS) {
893 DEBUG(10,("acl: failed to find object %s\n",
894 ldb_dn_get_linearized(req->op.rename.olddn)));
898 schema = dsdb_get_schema(ldb, acl_res);
900 talloc_free(acl_res);
901 return ldb_operr(ldb);
904 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
905 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
907 return ldb_operr(ldb);
910 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
912 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
913 &new_node, &new_node)) {
914 return ldb_operr(ldb);
917 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
918 if (rdn_name == NULL) {
919 return ldb_operr(ldb);
921 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
923 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
924 &new_node, &new_node)) {
925 return ldb_operr(ldb);
928 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
930 if (ret != LDB_SUCCESS) {
931 return ldb_operr(ldb);
933 /* Theoretically we pass the check if the object has no sd */
937 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
938 status = sec_access_check_ds(sd, acl_user_token(module),
944 if (!NT_STATUS_IS_OK(status)) {
945 DEBUG(10, ("Object %s has no wp on name\n",
946 ldb_dn_get_linearized(req->op.rename.olddn)));
948 acl_user_token(module),
949 req->op.rename.olddn,
952 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
955 if (ldb_dn_compare(oldparent, newparent) == 0) {
956 /* regular rename, not move, nothing more to do */
957 return ldb_next_request(module, req);
960 /* What exactly to do in this case? It would fail anyway.. */
961 /* FIXME: this has to be made dynamic at some point */
962 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
963 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
964 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0)) {
965 DEBUG(10,("acl:moving as an NC\n"));
966 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
968 /* new parent should have create child */
969 talloc_free(tmp_ctx);
970 tmp_ctx = talloc_new(req);
973 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
975 DEBUG(10,("acl:renamed object has no object class\n"));
976 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
979 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
980 if (ret != LDB_SUCCESS) {
981 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
984 /* do we have delete object on the object? */
986 status = sec_access_check_ds(sd, acl_user_token(module),
992 if (NT_STATUS_IS_OK(status)) {
993 return ldb_next_request(module, req);
995 /* what about delete child on the current parent */
996 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
997 if (ret != LDB_SUCCESS) {
998 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
999 return ldb_module_done(req, NULL, NULL, ret);
1001 return ldb_next_request(module, req);
1004 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1006 struct ldb_context *ldb;
1007 struct acl_context *ac;
1008 struct acl_private *data;
1009 struct ldb_result *acl_res;
1010 static const char *acl_attrs[] = {
1012 "nTSecurityDescriptor",
1018 ac = talloc_get_type(req->context, struct acl_context);
1019 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1020 ldb = ldb_module_get_ctx(ac->module);
1023 return ldb_module_done(ac->req, NULL, NULL,
1024 LDB_ERR_OPERATIONS_ERROR);
1026 if (ares->error != LDB_SUCCESS) {
1027 return ldb_module_done(ac->req, ares->controls,
1028 ares->response, ares->error);
1031 switch (ares->type) {
1032 case LDB_REPLY_ENTRY:
1033 if (ac->allowedAttributes
1034 || ac->allowedChildClasses
1035 || ac->allowedChildClassesEffective
1036 || ac->allowedAttributesEffective
1037 || ac->sDRightsEffective) {
1038 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1040 DSDB_FLAG_NEXT_MODULE);
1041 if (ret != LDB_SUCCESS) {
1042 return ldb_module_done(ac->req, NULL, NULL, ret);
1044 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1045 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1046 if (ret != LDB_SUCCESS) {
1047 return ldb_module_done(ac->req, NULL, NULL, ret);
1050 if (ac->allowedChildClasses) {
1051 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1052 ares->message, "allowedChildClasses");
1053 if (ret != LDB_SUCCESS) {
1054 return ldb_module_done(ac->req, NULL, NULL, ret);
1057 if (ac->allowedChildClassesEffective) {
1058 ret = acl_childClassesEffective(ac->module, ac->schema,
1059 acl_res->msgs[0], ares->message, ac);
1060 if (ret != LDB_SUCCESS) {
1061 return ldb_module_done(ac->req, NULL, NULL, ret);
1064 if (ac->sDRightsEffective) {
1065 ret = acl_sDRightsEffective(ac->module,
1066 acl_res->msgs[0], ares->message, ac);
1067 if (ret != LDB_SUCCESS) {
1068 return ldb_module_done(ac->req, NULL, NULL, ret);
1072 if (data && data->password_attrs) {
1073 if (!ac->am_system) {
1074 for (i = 0; data->password_attrs[i]; i++) {
1075 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1079 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1081 case LDB_REPLY_REFERRAL:
1082 return ldb_module_send_referral(ac->req, ares->referral);
1084 case LDB_REPLY_DONE:
1085 return ldb_module_done(ac->req, ares->controls,
1086 ares->response, LDB_SUCCESS);
1092 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1094 struct ldb_context *ldb;
1095 struct acl_context *ac;
1096 struct ldb_request *down_req;
1097 struct acl_private *data;
1100 ldb = ldb_module_get_ctx(module);
1102 ac = talloc_zero(req, struct acl_context);
1104 return ldb_oom(ldb);
1106 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1108 ac->module = module;
1110 ac->am_system = dsdb_module_am_system(module);
1111 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1112 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1113 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1114 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1115 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1116 ac->schema = dsdb_get_schema(ldb, ac);
1118 /* replace any attributes in the parse tree that are private,
1119 so we don't allow a search for 'userPassword=penguin',
1120 just as we would not allow that attribute to be returned */
1121 if (ac->am_system) {
1122 /* FIXME: We should copy the tree and keep the original unmodified. */
1123 /* remove password attributes */
1124 if (data && data->password_attrs) {
1125 for (i = 0; data->password_attrs[i]; i++) {
1126 ldb_parse_tree_attr_replace(req->op.search.tree,
1127 data->password_attrs[i],
1128 "kludgeACLredactedattribute");
1132 ret = ldb_build_search_req_ex(&down_req,
1134 req->op.search.base,
1135 req->op.search.scope,
1136 req->op.search.tree,
1137 req->op.search.attrs,
1139 ac, acl_search_callback,
1141 LDB_REQ_SET_LOCATION(down_req);
1142 if (ret != LDB_SUCCESS) {
1145 /* perform the search */
1146 return ldb_next_request(module, down_req);
1149 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1151 struct ldb_context *ldb = ldb_module_get_ctx(module);
1152 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1154 /* allow everybody to read the sequence number */
1155 if (strcmp(req->op.extended.oid,
1156 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1157 return ldb_next_request(module, req);
1160 if (dsdb_module_am_system(module) ||
1161 dsdb_module_am_administrator(module) || as_system) {
1162 return ldb_next_request(module, req);
1164 ldb_asprintf_errstring(ldb,
1166 "attempted database modify not permitted. "
1167 "User %s is not SYSTEM or an administrator",
1168 acl_user_name(req, module));
1169 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1173 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1175 .search = acl_search,
1177 .modify = acl_modify,
1179 .rename = acl_rename,
1180 .extended = acl_extended,
1181 .init_context = acl_module_init