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 "dsdb/samdb/ldb_modules/schema.h"
43 #include "lib/util/tsort.h"
45 struct extended_access_check_attribute {
47 const uint32_t requires_rights;
52 const char **password_attrs;
56 struct ldb_module *module;
57 struct ldb_request *req;
59 bool allowedAttributes;
60 bool allowedAttributesEffective;
61 bool allowedChildClasses;
62 bool allowedChildClassesEffective;
63 bool sDRightsEffective;
65 const char * const *attrs;
66 struct dsdb_schema *schema;
69 static int acl_module_init(struct ldb_module *module)
71 struct ldb_context *ldb;
72 struct acl_private *data;
75 static const char *attrs[] = { "passwordAttribute", NULL };
76 struct ldb_result *res;
77 struct ldb_message *msg;
78 struct ldb_message_element *password_attributes;
80 ldb = ldb_module_get_ctx(module);
82 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
83 if (ret != LDB_SUCCESS) {
84 ldb_debug(ldb, LDB_DEBUG_ERROR,
85 "acl_module_init: Unable to register control with rootdse!\n");
86 return ldb_operr(ldb);
89 data = talloc(module, struct acl_private);
94 data->password_attrs = NULL;
95 data->acl_perform = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
96 NULL, "acl", "perform", false);
97 ldb_module_set_private(module, data);
99 mem_ctx = talloc_new(module);
104 ret = dsdb_module_search_dn(module, mem_ctx, &res,
105 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
107 DSDB_FLAG_NEXT_MODULE);
108 if (ret != LDB_SUCCESS) {
111 if (res->count == 0) {
115 if (res->count > 1) {
116 talloc_free(mem_ctx);
117 return LDB_ERR_CONSTRAINT_VIOLATION;
122 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
123 if (!password_attributes) {
126 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
127 if (!data->password_attrs) {
128 talloc_free(mem_ctx);
131 for (i=0; i < password_attributes->num_values; i++) {
132 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
133 talloc_steal(data->password_attrs, password_attributes->values[i].data);
135 data->password_attrs[i] = NULL;
138 talloc_free(mem_ctx);
139 return ldb_next_init(module);
142 static int acl_allowedAttributes(struct ldb_module *module,
143 const struct dsdb_schema *schema,
144 struct ldb_message *sd_msg,
145 struct ldb_message *msg,
146 struct acl_context *ac)
148 struct ldb_message_element *oc_el;
149 struct ldb_context *ldb = ldb_module_get_ctx(module);
151 const char **attr_list;
154 /* If we don't have a schema yet, we can't do anything... */
155 if (schema == NULL) {
156 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
157 return LDB_ERR_OPERATIONS_ERROR;
160 /* Must remove any existing attribute */
161 if (ac->allowedAttributes) {
162 ldb_msg_remove_attr(msg, "allowedAttributes");
165 mem_ctx = talloc_new(msg);
170 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
171 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
173 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
174 talloc_free(mem_ctx);
175 return LDB_ERR_OPERATIONS_ERROR;
177 if (ac->allowedAttributes) {
178 for (i=0; attr_list && attr_list[i]; i++) {
179 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
182 if (ac->allowedAttributesEffective) {
183 struct security_descriptor *sd;
184 struct dom_sid *sid = NULL;
185 struct ldb_control *as_system = ldb_request_get_control(ac->req,
186 LDB_CONTROL_AS_SYSTEM_OID);
188 if (as_system != NULL) {
189 as_system->critical = 0;
192 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
193 if (ac->am_system || as_system) {
194 for (i=0; attr_list && attr_list[i]; i++) {
195 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
200 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
202 if (ret != LDB_SUCCESS) {
206 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
207 for (i=0; attr_list && attr_list[i]; i++) {
208 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
211 return ldb_operr(ldb);
213 /* remove constructed attributes */
214 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
216 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
219 ret = acl_check_access_on_attribute(module,
225 if (ret == LDB_SUCCESS) {
226 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
233 static int acl_childClasses(struct ldb_module *module,
234 const struct dsdb_schema *schema,
235 struct ldb_message *sd_msg,
236 struct ldb_message *msg,
237 const char *attrName)
239 struct ldb_message_element *oc_el;
240 struct ldb_message_element *allowedClasses;
241 const struct dsdb_class *sclass;
245 /* If we don't have a schema yet, we can't do anything... */
246 if (schema == NULL) {
247 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
248 return LDB_ERR_OPERATIONS_ERROR;
251 /* Must remove any existing attribute, or else confusion reins */
252 ldb_msg_remove_attr(msg, attrName);
253 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
254 if (ret != LDB_SUCCESS) {
258 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
260 for (i=0; oc_el && i < oc_el->num_values; i++) {
261 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
263 /* We don't know this class? what is going on? */
267 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
268 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
271 if (allowedClasses->num_values > 1) {
272 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
273 for (i=1 ; i < allowedClasses->num_values; i++) {
274 struct ldb_val *val1 = &allowedClasses->values[i-1];
275 struct ldb_val *val2 = &allowedClasses->values[i];
276 if (data_blob_cmp(val1, val2) == 0) {
277 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
278 allowedClasses->num_values--;
287 static int acl_childClassesEffective(struct ldb_module *module,
288 const struct dsdb_schema *schema,
289 struct ldb_message *sd_msg,
290 struct ldb_message *msg,
291 struct acl_context *ac)
293 struct ldb_message_element *oc_el;
294 struct ldb_message_element *allowedClasses = NULL;
295 const struct dsdb_class *sclass;
296 struct security_descriptor *sd;
297 struct ldb_control *as_system = ldb_request_get_control(ac->req,
298 LDB_CONTROL_AS_SYSTEM_OID);
299 struct dom_sid *sid = NULL;
303 if (as_system != NULL) {
304 as_system->critical = 0;
307 if (ac->am_system || as_system) {
308 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
311 /* If we don't have a schema yet, we can't do anything... */
312 if (schema == NULL) {
313 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
314 return LDB_ERR_OPERATIONS_ERROR;
317 /* Must remove any existing attribute, or else confusion reins */
318 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
320 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
321 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
322 if (ret != LDB_SUCCESS) {
326 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
327 for (i=0; oc_el && i < oc_el->num_values; i++) {
328 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
330 /* We don't know this class? what is going on? */
334 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
335 ret = acl_check_access_on_class(module,
340 SEC_ADS_CREATE_CHILD,
341 sclass->possibleInferiors[j]);
342 if (ret == LDB_SUCCESS) {
343 ldb_msg_add_string(msg, "allowedChildClassesEffective",
344 sclass->possibleInferiors[j]);
348 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
349 if (!allowedClasses) {
353 if (allowedClasses->num_values > 1) {
354 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
355 for (i=1 ; i < allowedClasses->num_values; i++) {
356 struct ldb_val *val1 = &allowedClasses->values[i-1];
357 struct ldb_val *val2 = &allowedClasses->values[i];
358 if (data_blob_cmp(val1, val2) == 0) {
359 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
360 allowedClasses->num_values--;
368 static int acl_sDRightsEffective(struct ldb_module *module,
369 struct ldb_message *sd_msg,
370 struct ldb_message *msg,
371 struct acl_context *ac)
373 struct ldb_message_element *rightsEffective;
375 struct security_descriptor *sd;
376 struct ldb_control *as_system = ldb_request_get_control(ac->req,
377 LDB_CONTROL_AS_SYSTEM_OID);
378 struct dom_sid *sid = NULL;
381 if (as_system != NULL) {
382 as_system->critical = 0;
385 /* Must remove any existing attribute, or else confusion reins */
386 ldb_msg_remove_attr(msg, "sDRightsEffective");
387 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
388 if (ret != LDB_SUCCESS) {
391 if (ac->am_system || as_system) {
392 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
395 /* Get the security descriptor from the message */
396 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
397 if (ret != LDB_SUCCESS) {
400 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
401 ret = acl_check_access_on_attribute(module,
407 if (ret == LDB_SUCCESS) {
408 flags |= SECINFO_OWNER | SECINFO_GROUP;
410 ret = acl_check_access_on_attribute(module,
416 if (ret == LDB_SUCCESS) {
417 flags |= SECINFO_DACL;
419 ret = acl_check_access_on_attribute(module,
423 SEC_FLAG_SYSTEM_SECURITY,
425 if (ret == LDB_SUCCESS) {
426 flags |= SECINFO_SACL;
429 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
430 "sDRightsEffective", flags);
433 static int acl_add(struct ldb_module *module, struct ldb_request *req)
436 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
437 struct ldb_context *ldb;
438 const struct dsdb_schema *schema;
439 struct ldb_message_element *oc_el;
440 const struct GUID *guid;
441 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
443 if (as_system != NULL) {
444 as_system->critical = 0;
447 if (dsdb_module_am_system(module) || as_system) {
448 return ldb_next_request(module, req);
451 if (ldb_dn_is_special(req->op.add.message->dn)) {
452 return ldb_next_request(module, req);
454 ldb = ldb_module_get_ctx(module);
455 /* Creating an NC. There is probably something we should do here,
456 * but we will establish that later */
457 /* FIXME: this has to be made dynamic at some point */
458 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
459 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
460 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_default_basedn(ldb))) == 0)) {
461 return ldb_next_request(module, req);
464 schema = dsdb_get_schema(ldb, req);
466 return ldb_operr(ldb);
469 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
470 if (!oc_el || oc_el->num_values == 0) {
471 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
472 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
475 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
476 (char *)oc_el->values[oc_el->num_values-1].data);
477 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
478 if (ret != LDB_SUCCESS) {
481 return ldb_next_request(module, req);
484 /* ckecks if modifications are allowed on "Member" attribute */
485 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
486 struct ldb_module *module,
487 struct ldb_request *req,
488 struct security_descriptor *sd,
490 const struct GUID *oc_guid,
491 const struct dsdb_attribute *attr)
495 struct ldb_context *ldb = ldb_module_get_ctx(module);
496 struct ldb_dn *user_dn;
497 struct ldb_message_element *member_el;
498 /* if we have wp, we can do whatever we like */
499 if (acl_check_access_on_attribute(module,
504 attr) == LDB_SUCCESS) {
507 /* if we are adding/deleting ourselves, check for self membership */
508 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
509 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
511 if (ret != LDB_SUCCESS) {
514 member_el = ldb_msg_find_element(req->op.mod.message, "member");
516 return ldb_operr(ldb);
518 /* user can only remove oneself */
519 if (member_el->num_values == 0) {
520 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
522 for (i = 0; i < member_el->num_values; i++) {
523 if (strcasecmp((const char *)member_el->values[i].data,
524 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
525 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
528 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
529 GUID_DRS_SELF_MEMBERSHIP,
532 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
533 dsdb_acl_debug(sd, acl_user_token(module),
534 req->op.mod.message->dn,
541 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
542 struct ldb_module *module,
543 struct ldb_request *req,
544 struct security_descriptor *sd,
546 const struct GUID *oc_guid,
549 int ret = LDB_SUCCESS;
550 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
551 struct ldb_message_element *el;
552 struct ldb_message *msg;
553 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
554 "unicodePwd", "dBCSPwd", NULL }, **l;
555 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
557 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
559 return ldb_module_oom(module);
561 for (l = passwordAttrs; *l != NULL; l++) {
562 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
566 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
567 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
570 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
573 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
576 ldb_msg_remove_element(msg, el);
580 /* single deletes will be handled by the "password_hash" LDB module
581 * later in the stack, so we let it though here */
582 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
583 talloc_free(tmp_ctx);
587 if (ldb_request_get_control(req,
588 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
589 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
590 * have a user password change and not a set as the message
591 * looks like. In it's value blob it contains the NT and/or LM
592 * hash of the old password specified by the user.
593 * This control is used by the SAMR and "kpasswd" password
594 * change mechanisms. */
595 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
596 GUID_DRS_USER_CHANGE_PASSWORD,
597 SEC_ADS_CONTROL_ACCESS,
600 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
601 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
602 GUID_DRS_FORCE_CHANGE_PASSWORD,
603 SEC_ADS_CONTROL_ACCESS,
606 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
607 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
608 GUID_DRS_USER_CHANGE_PASSWORD,
609 SEC_ADS_CONTROL_ACCESS,
611 /* Very strange, but we get constraint violation in this case */
612 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
613 ret = LDB_ERR_CONSTRAINT_VIOLATION;
616 if (ret != LDB_SUCCESS) {
617 dsdb_acl_debug(sd, acl_user_token(module),
618 req->op.mod.message->dn,
622 talloc_free(tmp_ctx);
626 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
629 struct ldb_context *ldb = ldb_module_get_ctx(module);
630 const struct dsdb_schema *schema;
632 const struct GUID *guid;
633 uint32_t access_granted;
634 struct object_tree *root = NULL;
635 struct object_tree *new_node = NULL;
637 struct ldb_result *acl_res;
638 struct security_descriptor *sd;
639 struct dom_sid *sid = NULL;
640 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
641 bool userPassword = dsdb_user_password_support(module, req);
642 TALLOC_CTX *tmp_ctx = talloc_new(req);
643 static const char *acl_attrs[] = {
644 "nTSecurityDescriptor",
650 if (as_system != NULL) {
651 as_system->critical = 0;
654 /* Don't print this debug statement if elements[0].name is going to be NULL */
655 if(req->op.mod.message->num_elements > 0)
657 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
659 if (dsdb_module_am_system(module) || as_system) {
660 return ldb_next_request(module, req);
662 if (ldb_dn_is_special(req->op.mod.message->dn)) {
663 return ldb_next_request(module, req);
665 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
667 DSDB_FLAG_NEXT_MODULE);
669 if (ret != LDB_SUCCESS) {
673 schema = dsdb_get_schema(ldb, tmp_ctx);
675 ret = LDB_ERR_OPERATIONS_ERROR;
679 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
680 if (ret != LDB_SUCCESS) {
681 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
684 /* Theoretically we pass the check if the object has no sd */
689 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
691 DEBUG(10, ("acl_modify: cannot get guid\n"));
694 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
695 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
697 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
700 for (i=0; i < req->op.mod.message->num_elements; i++){
701 const struct dsdb_attribute *attr;
702 attr = dsdb_attribute_by_lDAPDisplayName(schema,
703 req->op.mod.message->elements[i].name);
705 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
706 status = sec_access_check_ds(sd, acl_user_token(module),
712 if (!NT_STATUS_IS_OK(status)) {
713 DEBUG(10, ("Object %s has no write dacl access\n",
714 ldb_dn_get_linearized(req->op.mod.message->dn)));
716 acl_user_token(module),
717 req->op.mod.message->dn,
720 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
724 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
725 ret = acl_check_self_membership(tmp_ctx,
732 if (ret != LDB_SUCCESS) {
736 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
737 /* this one is not affected by any rights, we should let it through
738 so that passwords_hash returns the correct error */
741 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
742 (userPassword && ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0) ||
743 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
744 ret = acl_check_password_rights(tmp_ctx,
751 if (ret != LDB_SUCCESS) {
756 /* This basic attribute existence check with the right errorcode
757 * is needed since this module is the first one which requests
758 * schema attribute informations.
759 * The complete attribute checking is done in the
760 * "objectclass_attrs" module behind this one.
763 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
764 req->op.mod.message->elements[i].name,
765 ldb_dn_get_linearized(req->op.mod.message->dn));
766 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
769 if (!insert_in_object_tree(tmp_ctx,
770 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
771 &new_node, &new_node)) {
772 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
773 ret = LDB_ERR_OPERATIONS_ERROR;
777 if (!insert_in_object_tree(tmp_ctx,
778 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
779 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
780 ret = LDB_ERR_OPERATIONS_ERROR;
786 if (root->num_of_children > 0) {
787 status = sec_access_check_ds(sd, acl_user_token(module),
793 if (!NT_STATUS_IS_OK(status)) {
794 DEBUG(10, ("Object %s has no write property access\n",
795 ldb_dn_get_linearized(req->op.mod.message->dn)));
797 acl_user_token(module),
798 req->op.mod.message->dn,
801 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
807 talloc_free(tmp_ctx);
808 return ldb_next_request(module, req);
810 talloc_free(tmp_ctx);
814 /* similar to the modify for the time being.
815 * We need to concider the special delete tree case, though - TODO */
816 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
819 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
820 struct ldb_context *ldb;
821 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
823 if (as_system != NULL) {
824 as_system->critical = 0;
827 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
828 if (dsdb_module_am_system(module) || as_system) {
829 return ldb_next_request(module, req);
832 if (ldb_dn_is_special(req->op.del.dn)) {
833 return ldb_next_request(module, req);
835 ldb = ldb_module_get_ctx(module);
836 /* first check if we have delete object right */
837 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
838 if (ret == LDB_SUCCESS) {
839 return ldb_next_request(module, req);
842 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
843 /* No parent, so check fails */
844 /* FIXME: this has to be made dynamic at some point */
845 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
846 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
847 (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0)) {
848 DEBUG(10,("acl:deleting an NC\n"));
849 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
852 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
853 if (ret != LDB_SUCCESS) {
856 return ldb_next_request(module, req);
859 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
862 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
863 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
864 const struct dsdb_schema *schema;
865 struct ldb_context *ldb;
866 struct security_descriptor *sd = NULL;
867 struct dom_sid *sid = NULL;
868 struct ldb_result *acl_res;
869 const struct GUID *guid;
870 struct object_tree *root = NULL;
871 struct object_tree *new_node = NULL;
872 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
873 TALLOC_CTX *tmp_ctx = talloc_new(req);
875 uint32_t access_granted;
876 const char *rdn_name;
877 static const char *acl_attrs[] = {
878 "nTSecurityDescriptor",
884 if (as_system != NULL) {
885 as_system->critical = 0;
888 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
889 if (dsdb_module_am_system(module) || as_system) {
890 return ldb_next_request(module, req);
892 if (ldb_dn_is_special(req->op.rename.olddn)) {
893 return ldb_next_request(module, req);
895 ldb = ldb_module_get_ctx(module);
897 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
899 DSDB_FLAG_NEXT_MODULE |
900 DSDB_SEARCH_SHOW_RECYCLED);
901 /* we sould be able to find the parent */
902 if (ret != LDB_SUCCESS) {
903 DEBUG(10,("acl: failed to find object %s\n",
904 ldb_dn_get_linearized(req->op.rename.olddn)));
908 schema = dsdb_get_schema(ldb, acl_res);
910 talloc_free(acl_res);
911 return ldb_operr(ldb);
914 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
915 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
917 return ldb_operr(ldb);
920 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
922 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
923 &new_node, &new_node)) {
924 return ldb_operr(ldb);
927 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
928 if (rdn_name == NULL) {
929 return ldb_operr(ldb);
931 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
933 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
934 &new_node, &new_node)) {
935 return ldb_operr(ldb);
938 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
940 if (ret != LDB_SUCCESS) {
941 return ldb_operr(ldb);
943 /* Theoretically we pass the check if the object has no sd */
947 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
948 status = sec_access_check_ds(sd, acl_user_token(module),
954 if (!NT_STATUS_IS_OK(status)) {
955 DEBUG(10, ("Object %s has no wp on name\n",
956 ldb_dn_get_linearized(req->op.rename.olddn)));
958 acl_user_token(module),
959 req->op.rename.olddn,
962 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
965 if (ldb_dn_compare(oldparent, newparent) == 0) {
966 /* regular rename, not move, nothing more to do */
967 return ldb_next_request(module, req);
970 /* What exactly to do in this case? It would fail anyway.. */
971 /* FIXME: this has to be made dynamic at some point */
972 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
973 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
974 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0)) {
975 DEBUG(10,("acl:moving as an NC\n"));
976 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
978 /* new parent should have create child */
979 talloc_free(tmp_ctx);
980 tmp_ctx = talloc_new(req);
983 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
985 DEBUG(10,("acl:renamed object has no object class\n"));
986 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
989 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
990 if (ret != LDB_SUCCESS) {
991 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
994 /* do we have delete object on the object? */
996 status = sec_access_check_ds(sd, acl_user_token(module),
1002 if (NT_STATUS_IS_OK(status)) {
1003 return ldb_next_request(module, req);
1005 /* what about delete child on the current parent */
1006 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1007 if (ret != LDB_SUCCESS) {
1008 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1009 return ldb_module_done(req, NULL, NULL, ret);
1011 return ldb_next_request(module, req);
1014 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1016 struct ldb_context *ldb;
1017 struct acl_context *ac;
1018 struct acl_private *data;
1019 struct ldb_result *acl_res;
1020 static const char *acl_attrs[] = {
1022 "nTSecurityDescriptor",
1028 ac = talloc_get_type(req->context, struct acl_context);
1029 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1030 ldb = ldb_module_get_ctx(ac->module);
1033 return ldb_module_done(ac->req, NULL, NULL,
1034 LDB_ERR_OPERATIONS_ERROR);
1036 if (ares->error != LDB_SUCCESS) {
1037 return ldb_module_done(ac->req, ares->controls,
1038 ares->response, ares->error);
1041 switch (ares->type) {
1042 case LDB_REPLY_ENTRY:
1043 if (ac->allowedAttributes
1044 || ac->allowedChildClasses
1045 || ac->allowedChildClassesEffective
1046 || ac->allowedAttributesEffective
1047 || ac->sDRightsEffective) {
1048 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1050 DSDB_FLAG_NEXT_MODULE);
1051 if (ret != LDB_SUCCESS) {
1052 return ldb_module_done(ac->req, NULL, NULL, ret);
1054 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1055 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1056 if (ret != LDB_SUCCESS) {
1057 return ldb_module_done(ac->req, NULL, NULL, ret);
1060 if (ac->allowedChildClasses) {
1061 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1062 ares->message, "allowedChildClasses");
1063 if (ret != LDB_SUCCESS) {
1064 return ldb_module_done(ac->req, NULL, NULL, ret);
1067 if (ac->allowedChildClassesEffective) {
1068 ret = acl_childClassesEffective(ac->module, ac->schema,
1069 acl_res->msgs[0], ares->message, ac);
1070 if (ret != LDB_SUCCESS) {
1071 return ldb_module_done(ac->req, NULL, NULL, ret);
1074 if (ac->sDRightsEffective) {
1075 ret = acl_sDRightsEffective(ac->module,
1076 acl_res->msgs[0], ares->message, ac);
1077 if (ret != LDB_SUCCESS) {
1078 return ldb_module_done(ac->req, NULL, NULL, ret);
1082 if (data && data->password_attrs) {
1083 if (!ac->am_system) {
1084 for (i = 0; data->password_attrs[i]; i++) {
1085 if ((!ac->userPassword) &&
1086 (ldb_attr_cmp(data->password_attrs[i],
1087 "userPassword") == 0))
1090 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1094 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1096 case LDB_REPLY_REFERRAL:
1097 return ldb_module_send_referral(ac->req, ares->referral);
1099 case LDB_REPLY_DONE:
1100 return ldb_module_done(ac->req, ares->controls,
1101 ares->response, LDB_SUCCESS);
1107 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1109 struct ldb_context *ldb;
1110 struct acl_context *ac;
1111 struct ldb_request *down_req;
1112 struct acl_private *data;
1115 ldb = ldb_module_get_ctx(module);
1117 ac = talloc_zero(req, struct acl_context);
1119 return ldb_oom(ldb);
1121 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1123 ac->module = module;
1125 ac->am_system = dsdb_module_am_system(module);
1126 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1127 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1128 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1129 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1130 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1131 ac->userPassword = dsdb_user_password_support(module, ac);
1132 ac->schema = dsdb_get_schema(ldb, ac);
1134 /* replace any attributes in the parse tree that are private,
1135 so we don't allow a search for 'userPassword=penguin',
1136 just as we would not allow that attribute to be returned */
1137 if (ac->am_system) {
1138 /* FIXME: We should copy the tree and keep the original unmodified. */
1139 /* remove password attributes */
1140 if (data && data->password_attrs) {
1141 for (i = 0; data->password_attrs[i]; i++) {
1142 if ((!ac->userPassword) &&
1143 (ldb_attr_cmp(data->password_attrs[i],
1144 "userPassword") == 0))
1147 ldb_parse_tree_attr_replace(req->op.search.tree,
1148 data->password_attrs[i],
1149 "kludgeACLredactedattribute");
1153 ret = ldb_build_search_req_ex(&down_req,
1155 req->op.search.base,
1156 req->op.search.scope,
1157 req->op.search.tree,
1158 req->op.search.attrs,
1160 ac, acl_search_callback,
1162 LDB_REQ_SET_LOCATION(down_req);
1163 if (ret != LDB_SUCCESS) {
1166 /* perform the search */
1167 return ldb_next_request(module, down_req);
1170 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1172 struct ldb_context *ldb = ldb_module_get_ctx(module);
1173 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1175 /* allow everybody to read the sequence number */
1176 if (strcmp(req->op.extended.oid,
1177 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1178 return ldb_next_request(module, req);
1181 if (dsdb_module_am_system(module) ||
1182 dsdb_module_am_administrator(module) || as_system) {
1183 return ldb_next_request(module, req);
1185 ldb_asprintf_errstring(ldb,
1187 "attempted database modify not permitted. "
1188 "User %s is not SYSTEM or an administrator",
1189 acl_user_name(req, module));
1190 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1194 static const struct ldb_module_ops ldb_acl_module_ops = {
1196 .search = acl_search,
1198 .modify = acl_modify,
1200 .rename = acl_rename,
1201 .extended = acl_extended,
1202 .init_context = acl_module_init
1205 int ldb_acl_module_init(const char *version)
1207 LDB_MODULE_CHECK_VERSION(version);
1208 return ldb_register_module(&ldb_acl_module_ops);