CVE-2018-1057: s4:dsdb/acl: only call dsdb_acl_debug() if we checked the acl in acl_c...
[Samba.git] / source4 / dsdb / samdb / ldb_modules / acl.c
blobedc6733c7abade0bde80c4cd6819b271b6e83fc6
1 /*
2 ldb database library
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/>.
23 * Name: ldb
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
34 #include "includes.h"
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"
43 #include "system/kerberos.h"
44 #include "auth/kerberos/kerberos.h"
46 struct extended_access_check_attribute {
47 const char *oa_name;
48 const uint32_t requires_rights;
51 struct acl_private {
52 bool acl_search;
53 const char **password_attrs;
54 void *cached_schema_ptr;
55 uint64_t cached_schema_metadata_usn;
56 uint64_t cached_schema_loaded_usn;
57 const char **confidential_attrs;
58 bool userPassword_support;
61 struct acl_context {
62 struct ldb_module *module;
63 struct ldb_request *req;
64 bool am_system;
65 bool am_administrator;
66 bool modify_search;
67 bool constructed_attrs;
68 bool allowedAttributes;
69 bool allowedAttributesEffective;
70 bool allowedChildClasses;
71 bool allowedChildClassesEffective;
72 bool sDRightsEffective;
73 bool userPassword;
74 const char * const *attrs;
75 struct dsdb_schema *schema;
78 static int acl_module_init(struct ldb_module *module)
80 struct ldb_context *ldb;
81 struct acl_private *data;
82 int ret;
83 unsigned int i, n, j;
84 TALLOC_CTX *mem_ctx;
85 static const char * const attrs[] = { "passwordAttribute", NULL };
86 static const char * const secret_attrs[] = {
87 DSDB_SECRET_ATTRIBUTES
89 struct ldb_result *res;
90 struct ldb_message *msg;
91 struct ldb_message_element *password_attributes;
93 ldb = ldb_module_get_ctx(module);
95 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
96 if (ret != LDB_SUCCESS) {
97 ldb_debug(ldb, LDB_DEBUG_ERROR,
98 "acl_module_init: Unable to register control with rootdse!\n");
99 return ldb_operr(ldb);
102 data = talloc_zero(module, struct acl_private);
103 if (data == NULL) {
104 return ldb_oom(ldb);
107 data->acl_search = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
108 NULL, "acl", "search", true);
109 ldb_module_set_private(module, data);
111 data->userPassword_support = dsdb_user_password_support(module, module, NULL);
113 mem_ctx = talloc_new(module);
114 if (!mem_ctx) {
115 return ldb_oom(ldb);
118 ret = dsdb_module_search_dn(module, mem_ctx, &res,
119 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
120 attrs,
121 DSDB_FLAG_NEXT_MODULE |
122 DSDB_FLAG_AS_SYSTEM,
123 NULL);
124 if (ret != LDB_SUCCESS) {
125 goto done;
127 if (res->count == 0) {
128 goto done;
131 if (res->count > 1) {
132 talloc_free(mem_ctx);
133 return LDB_ERR_CONSTRAINT_VIOLATION;
136 msg = res->msgs[0];
138 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
139 if (!password_attributes) {
140 goto done;
142 data->password_attrs = talloc_array(data, const char *,
143 password_attributes->num_values +
144 ARRAY_SIZE(secret_attrs) + 1);
145 if (!data->password_attrs) {
146 talloc_free(mem_ctx);
147 return ldb_oom(ldb);
150 n = 0;
151 for (i=0; i < password_attributes->num_values; i++) {
152 data->password_attrs[n] = (const char *)password_attributes->values[i].data;
153 talloc_steal(data->password_attrs, password_attributes->values[i].data);
154 n++;
157 for (i=0; i < ARRAY_SIZE(secret_attrs); i++) {
158 bool found = false;
160 for (j=0; j < n; j++) {
161 if (strcasecmp(data->password_attrs[j], secret_attrs[i]) == 0) {
162 found = true;
163 break;
167 if (found) {
168 continue;
171 data->password_attrs[n] = talloc_strdup(data->password_attrs,
172 secret_attrs[i]);
173 if (data->password_attrs[n] == NULL) {
174 talloc_free(mem_ctx);
175 return ldb_oom(ldb);
177 n++;
179 data->password_attrs[n] = NULL;
181 done:
182 talloc_free(mem_ctx);
183 return ldb_next_init(module);
186 static int acl_allowedAttributes(struct ldb_module *module,
187 const struct dsdb_schema *schema,
188 struct ldb_message *sd_msg,
189 struct ldb_message *msg,
190 struct acl_context *ac)
192 struct ldb_message_element *oc_el;
193 struct ldb_context *ldb = ldb_module_get_ctx(module);
194 TALLOC_CTX *mem_ctx;
195 const char **attr_list;
196 int i, ret;
197 const struct dsdb_class *objectclass;
199 /* If we don't have a schema yet, we can't do anything... */
200 if (schema == NULL) {
201 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
202 return LDB_ERR_OPERATIONS_ERROR;
205 /* Must remove any existing attribute */
206 if (ac->allowedAttributes) {
207 ldb_msg_remove_attr(msg, "allowedAttributes");
210 mem_ctx = talloc_new(msg);
211 if (!mem_ctx) {
212 return ldb_oom(ldb);
215 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
216 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
217 if (!attr_list) {
218 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
219 talloc_free(mem_ctx);
220 return LDB_ERR_OPERATIONS_ERROR;
224 * Get the top-most structural object class for the ACL check
226 objectclass = dsdb_get_last_structural_class(ac->schema,
227 oc_el);
228 if (objectclass == NULL) {
229 ldb_asprintf_errstring(ldb, "acl_read: Failed to find a structural class for %s",
230 ldb_dn_get_linearized(sd_msg->dn));
231 talloc_free(mem_ctx);
232 return LDB_ERR_OPERATIONS_ERROR;
235 if (ac->allowedAttributes) {
236 for (i=0; attr_list && attr_list[i]; i++) {
237 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
240 if (ac->allowedAttributesEffective) {
241 struct security_descriptor *sd;
242 struct dom_sid *sid = NULL;
243 struct ldb_control *as_system = ldb_request_get_control(ac->req,
244 LDB_CONTROL_AS_SYSTEM_OID);
246 if (as_system != NULL) {
247 as_system->critical = 0;
250 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
251 if (ac->am_system || as_system) {
252 for (i=0; attr_list && attr_list[i]; i++) {
253 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
255 return LDB_SUCCESS;
258 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
260 if (ret != LDB_SUCCESS) {
261 return ret;
264 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
265 for (i=0; attr_list && attr_list[i]; i++) {
266 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
267 attr_list[i]);
268 if (!attr) {
269 return ldb_operr(ldb);
271 /* remove constructed attributes */
272 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
273 || attr->systemOnly
274 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
275 continue;
277 ret = acl_check_access_on_attribute(module,
278 msg,
280 sid,
281 SEC_ADS_WRITE_PROP,
282 attr,
283 objectclass);
284 if (ret == LDB_SUCCESS) {
285 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
289 return LDB_SUCCESS;
292 static int acl_childClasses(struct ldb_module *module,
293 const struct dsdb_schema *schema,
294 struct ldb_message *sd_msg,
295 struct ldb_message *msg,
296 const char *attrName)
298 struct ldb_message_element *oc_el;
299 struct ldb_message_element *allowedClasses;
300 const struct dsdb_class *sclass;
301 unsigned int i, j;
302 int ret;
304 /* If we don't have a schema yet, we can't do anything... */
305 if (schema == NULL) {
306 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
307 return LDB_ERR_OPERATIONS_ERROR;
310 /* Must remove any existing attribute, or else confusion reins */
311 ldb_msg_remove_attr(msg, attrName);
312 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
313 if (ret != LDB_SUCCESS) {
314 return ret;
317 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
319 for (i=0; oc_el && i < oc_el->num_values; i++) {
320 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
321 if (!sclass) {
322 /* We don't know this class? what is going on? */
323 continue;
326 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
327 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
330 if (allowedClasses->num_values > 1) {
331 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
332 for (i=1 ; i < allowedClasses->num_values; i++) {
333 struct ldb_val *val1 = &allowedClasses->values[i-1];
334 struct ldb_val *val2 = &allowedClasses->values[i];
335 if (data_blob_cmp(val1, val2) == 0) {
336 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
337 allowedClasses->num_values--;
338 i--;
343 return LDB_SUCCESS;
346 static int acl_childClassesEffective(struct ldb_module *module,
347 const struct dsdb_schema *schema,
348 struct ldb_message *sd_msg,
349 struct ldb_message *msg,
350 struct acl_context *ac)
352 struct ldb_message_element *oc_el;
353 struct ldb_message_element *allowedClasses = NULL;
354 const struct dsdb_class *sclass;
355 struct security_descriptor *sd;
356 struct ldb_control *as_system = ldb_request_get_control(ac->req,
357 LDB_CONTROL_AS_SYSTEM_OID);
358 struct dom_sid *sid = NULL;
359 unsigned int i, j;
360 int ret;
362 if (as_system != NULL) {
363 as_system->critical = 0;
366 if (ac->am_system || as_system) {
367 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
370 /* If we don't have a schema yet, we can't do anything... */
371 if (schema == NULL) {
372 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
373 return LDB_ERR_OPERATIONS_ERROR;
376 /* Must remove any existing attribute, or else confusion reins */
377 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
379 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
380 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
381 if (ret != LDB_SUCCESS) {
382 return ret;
385 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
386 for (i=0; oc_el && i < oc_el->num_values; i++) {
387 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
388 if (!sclass) {
389 /* We don't know this class? what is going on? */
390 continue;
393 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
394 const struct dsdb_class *sc;
396 sc = dsdb_class_by_lDAPDisplayName(schema,
397 sclass->possibleInferiors[j]);
398 if (!sc) {
399 /* We don't know this class? what is going on? */
400 continue;
403 ret = acl_check_access_on_objectclass(module, ac,
404 sd, sid,
405 SEC_ADS_CREATE_CHILD,
406 sc);
407 if (ret == LDB_SUCCESS) {
408 ldb_msg_add_string(msg, "allowedChildClassesEffective",
409 sclass->possibleInferiors[j]);
413 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
414 if (!allowedClasses) {
415 return LDB_SUCCESS;
418 if (allowedClasses->num_values > 1) {
419 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
420 for (i=1 ; i < allowedClasses->num_values; i++) {
421 struct ldb_val *val1 = &allowedClasses->values[i-1];
422 struct ldb_val *val2 = &allowedClasses->values[i];
423 if (data_blob_cmp(val1, val2) == 0) {
424 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
425 allowedClasses->num_values--;
426 i--;
430 return LDB_SUCCESS;
433 static int acl_sDRightsEffective(struct ldb_module *module,
434 struct ldb_message *sd_msg,
435 struct ldb_message *msg,
436 struct acl_context *ac)
438 struct ldb_context *ldb = ldb_module_get_ctx(module);
439 struct ldb_message_element *rightsEffective;
440 int ret;
441 struct security_descriptor *sd;
442 struct ldb_control *as_system = ldb_request_get_control(ac->req,
443 LDB_CONTROL_AS_SYSTEM_OID);
444 struct dom_sid *sid = NULL;
445 uint32_t flags = 0;
447 if (as_system != NULL) {
448 as_system->critical = 0;
451 /* Must remove any existing attribute, or else confusion reins */
452 ldb_msg_remove_attr(msg, "sDRightsEffective");
453 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
454 if (ret != LDB_SUCCESS) {
455 return ret;
457 if (ac->am_system || as_system) {
458 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
459 } else {
460 const struct dsdb_class *objectclass;
461 const struct dsdb_attribute *attr;
463 objectclass = dsdb_get_structural_oc_from_msg(ac->schema, sd_msg);
464 if (objectclass == NULL) {
465 return ldb_operr(ldb);
468 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
469 "nTSecurityDescriptor");
470 if (attr == NULL) {
471 return ldb_operr(ldb);
474 /* Get the security descriptor from the message */
475 ret = dsdb_get_sd_from_ldb_message(ldb, msg, sd_msg, &sd);
476 if (ret != LDB_SUCCESS) {
477 return ret;
479 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
480 ret = acl_check_access_on_attribute(module,
481 msg,
483 sid,
484 SEC_STD_WRITE_OWNER,
485 attr,
486 objectclass);
487 if (ret == LDB_SUCCESS) {
488 flags |= SECINFO_OWNER | SECINFO_GROUP;
490 ret = acl_check_access_on_attribute(module,
491 msg,
493 sid,
494 SEC_STD_WRITE_DAC,
495 attr,
496 objectclass);
497 if (ret == LDB_SUCCESS) {
498 flags |= SECINFO_DACL;
500 ret = acl_check_access_on_attribute(module,
501 msg,
503 sid,
504 SEC_FLAG_SYSTEM_SECURITY,
505 attr,
506 objectclass);
507 if (ret == LDB_SUCCESS) {
508 flags |= SECINFO_SACL;
511 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
512 "sDRightsEffective", flags);
515 static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
516 struct ldb_context *ldb,
517 const char *spn_value,
518 uint32_t userAccountControl,
519 const char *samAccountName,
520 const char *dnsHostName,
521 const char *netbios_name,
522 const char *ntds_guid)
524 int ret, princ_size;
525 krb5_context krb_ctx;
526 krb5_error_code kerr;
527 krb5_principal principal;
528 char *instanceName;
529 char *serviceType;
530 char *serviceName;
531 const char *forest_name = samdb_forest_name(ldb, mem_ctx);
532 const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
533 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
534 struct loadparm_context);
535 bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
536 (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
538 if (strcasecmp_m(spn_value, samAccountName) == 0) {
539 /* MacOS X sets this value, and setting an SPN of your
540 * own samAccountName is both pointless and safe */
541 return LDB_SUCCESS;
544 kerr = smb_krb5_init_context_basic(mem_ctx,
545 lp_ctx,
546 &krb_ctx);
547 if (kerr != 0) {
548 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
549 "Could not initialize kerberos context.");
552 ret = krb5_parse_name(krb_ctx, spn_value, &principal);
553 if (ret) {
554 krb5_free_context(krb_ctx);
555 return LDB_ERR_CONSTRAINT_VIOLATION;
558 princ_size = krb5_princ_size(krb_ctx, principal);
559 if (princ_size < 2) {
560 DBG_WARNING("princ_size=%d\n", princ_size);
561 goto fail;
564 instanceName = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
565 principal, 1);
566 serviceType = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
567 principal, 0);
568 if (krb5_princ_size(krb_ctx, principal) == 3) {
569 serviceName = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
570 principal, 2);
571 } else {
572 serviceName = NULL;
575 if (serviceName) {
576 if (!is_dc) {
577 DBG_WARNING("is_dc=false, serviceName=%s,"
578 "serviceType=%s\n", serviceName,
579 serviceType);
580 goto fail;
582 if (strcasecmp(serviceType, "ldap") == 0) {
583 if (strcasecmp(serviceName, netbios_name) != 0 &&
584 strcasecmp(serviceName, forest_name) != 0) {
585 DBG_WARNING("serviceName=%s\n", serviceName);
586 goto fail;
589 } else if (strcasecmp(serviceType, "gc") == 0) {
590 if (strcasecmp(serviceName, forest_name) != 0) {
591 DBG_WARNING("serviceName=%s\n", serviceName);
592 goto fail;
594 } else {
595 if (strcasecmp(serviceName, base_domain) != 0 &&
596 strcasecmp(serviceName, netbios_name) != 0) {
597 DBG_WARNING("serviceType=%s, "
598 "serviceName=%s\n",
599 serviceType, serviceName);
600 goto fail;
604 /* instanceName can be samAccountName without $ or dnsHostName
605 * or "ntds_guid._msdcs.forest_domain for DC objects */
606 if (strlen(instanceName) == (strlen(samAccountName) - 1)
607 && strncasecmp(instanceName, samAccountName,
608 strlen(samAccountName) - 1) == 0) {
609 goto success;
611 if ((dnsHostName != NULL) &&
612 (strcasecmp(instanceName, dnsHostName) == 0)) {
613 goto success;
615 if (is_dc) {
616 const char *guid_str;
617 guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
618 ntds_guid,
619 forest_name);
620 if (strcasecmp(instanceName, guid_str) == 0) {
621 goto success;
625 fail:
626 krb5_free_principal(krb_ctx, principal);
627 krb5_free_context(krb_ctx);
628 ldb_debug_set(ldb, LDB_DEBUG_WARNING,
629 "acl: spn validation failed for "
630 "spn[%s] uac[0x%x] account[%s] hostname[%s] "
631 "nbname[%s] ntds[%s] forest[%s] domain[%s]\n",
632 spn_value, (unsigned)userAccountControl,
633 samAccountName, dnsHostName,
634 netbios_name, ntds_guid,
635 forest_name, base_domain);
636 return LDB_ERR_CONSTRAINT_VIOLATION;
638 success:
639 krb5_free_principal(krb_ctx, principal);
640 krb5_free_context(krb_ctx);
641 return LDB_SUCCESS;
644 static int acl_check_spn(TALLOC_CTX *mem_ctx,
645 struct ldb_module *module,
646 struct ldb_request *req,
647 struct security_descriptor *sd,
648 struct dom_sid *sid,
649 const struct dsdb_attribute *attr,
650 const struct dsdb_class *objectclass)
652 int ret;
653 unsigned int i;
654 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
655 struct ldb_context *ldb = ldb_module_get_ctx(module);
656 struct ldb_result *acl_res;
657 struct ldb_result *netbios_res;
658 struct ldb_message_element *el;
659 struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
660 uint32_t userAccountControl;
661 const char *samAccountName;
662 const char *dnsHostName;
663 const char *netbios_name;
664 struct GUID ntds;
665 char *ntds_guid = NULL;
667 static const char *acl_attrs[] = {
668 "samAccountName",
669 "dnsHostName",
670 "userAccountControl",
671 NULL
673 static const char *netbios_attrs[] = {
674 "nETBIOSName",
675 NULL
678 /* if we have wp, we can do whatever we like */
679 if (acl_check_access_on_attribute(module,
680 tmp_ctx,
682 sid,
683 SEC_ADS_WRITE_PROP,
684 attr, objectclass) == LDB_SUCCESS) {
685 talloc_free(tmp_ctx);
686 return LDB_SUCCESS;
689 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
690 GUID_DRS_VALIDATE_SPN,
691 SEC_ADS_SELF_WRITE,
692 sid);
694 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
695 dsdb_acl_debug(sd, acl_user_token(module),
696 req->op.mod.message->dn,
697 true,
698 10);
699 talloc_free(tmp_ctx);
700 return ret;
703 ret = dsdb_module_search_dn(module, tmp_ctx,
704 &acl_res, req->op.mod.message->dn,
705 acl_attrs,
706 DSDB_FLAG_NEXT_MODULE |
707 DSDB_FLAG_AS_SYSTEM |
708 DSDB_SEARCH_SHOW_RECYCLED,
709 req);
710 if (ret != LDB_SUCCESS) {
711 talloc_free(tmp_ctx);
712 return ret;
715 userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
716 dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
717 samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
719 ret = dsdb_module_search(module, tmp_ctx,
720 &netbios_res, partitions_dn,
721 LDB_SCOPE_ONELEVEL,
722 netbios_attrs,
723 DSDB_FLAG_NEXT_MODULE |
724 DSDB_FLAG_AS_SYSTEM,
725 req,
726 "(ncName=%s)",
727 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
729 netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
731 el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
732 if (!el) {
733 talloc_free(tmp_ctx);
734 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
735 "Error finding element for servicePrincipalName.");
738 /* NTDSDSA objectGuid of object we are checking SPN for */
739 if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
740 ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
741 req->op.mod.message->dn, &ntds, req);
742 if (ret != LDB_SUCCESS) {
743 ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
744 ldb_dn_get_linearized(req->op.mod.message->dn),
745 ldb_strerror(ret));
746 talloc_free(tmp_ctx);
747 return LDB_ERR_OPERATIONS_ERROR;
749 ntds_guid = GUID_string(tmp_ctx, &ntds);
752 for (i=0; i < el->num_values; i++) {
753 ret = acl_validate_spn_value(tmp_ctx,
754 ldb,
755 (char *)el->values[i].data,
756 userAccountControl,
757 samAccountName,
758 dnsHostName,
759 netbios_name,
760 ntds_guid);
761 if (ret != LDB_SUCCESS) {
762 talloc_free(tmp_ctx);
763 return ret;
766 talloc_free(tmp_ctx);
767 return LDB_SUCCESS;
770 static int acl_add(struct ldb_module *module, struct ldb_request *req)
772 int ret;
773 struct ldb_dn *parent;
774 struct ldb_context *ldb;
775 const struct dsdb_schema *schema;
776 const struct dsdb_class *objectclass;
777 struct ldb_control *as_system;
778 struct ldb_message_element *el;
779 unsigned int instanceType = 0;
781 if (ldb_dn_is_special(req->op.add.message->dn)) {
782 return ldb_next_request(module, req);
785 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
786 if (as_system != NULL) {
787 as_system->critical = 0;
790 if (dsdb_module_am_system(module) || as_system) {
791 return ldb_next_request(module, req);
794 ldb = ldb_module_get_ctx(module);
796 parent = ldb_dn_get_parent(req, req->op.add.message->dn);
797 if (parent == NULL) {
798 return ldb_oom(ldb);
801 schema = dsdb_get_schema(ldb, req);
802 if (!schema) {
803 return ldb_operr(ldb);
806 objectclass = dsdb_get_structural_oc_from_msg(schema, req->op.add.message);
807 if (!objectclass) {
808 ldb_asprintf_errstring(ldb_module_get_ctx(module),
809 "acl: unable to find or validate structural objectClass on %s\n",
810 ldb_dn_get_linearized(req->op.add.message->dn));
811 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
814 el = ldb_msg_find_element(req->op.add.message, "instanceType");
815 if ((el != NULL) && (el->num_values != 1)) {
816 ldb_set_errstring(ldb, "acl: the 'instanceType' attribute is single-valued!");
817 return LDB_ERR_UNWILLING_TO_PERFORM;
820 instanceType = ldb_msg_find_attr_as_uint(req->op.add.message,
821 "instanceType", 0);
822 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
823 static const char *no_attrs[] = { NULL };
824 struct ldb_result *partition_res;
825 struct ldb_dn *partitions_dn;
827 partitions_dn = samdb_partitions_dn(ldb, req);
828 if (!partitions_dn) {
829 ldb_set_errstring(ldb, "acl: CN=partitions dn could not be generated!");
830 return LDB_ERR_UNWILLING_TO_PERFORM;
833 ret = dsdb_module_search(module, req, &partition_res,
834 partitions_dn, LDB_SCOPE_ONELEVEL,
835 no_attrs,
836 DSDB_FLAG_NEXT_MODULE |
837 DSDB_FLAG_AS_SYSTEM |
838 DSDB_SEARCH_ONE_ONLY |
839 DSDB_SEARCH_SHOW_RECYCLED,
840 req,
841 "(&(nCName=%s)(objectClass=crossRef))",
842 ldb_dn_get_linearized(req->op.add.message->dn));
844 if (ret == LDB_SUCCESS) {
845 /* Check that we can write to the crossRef object MS-ADTS 3.1.1.5.2.8.2 */
846 ret = dsdb_module_check_access_on_dn(module, req, partition_res->msgs[0]->dn,
847 SEC_ADS_WRITE_PROP,
848 &objectclass->schemaIDGUID, req);
849 if (ret != LDB_SUCCESS) {
850 ldb_asprintf_errstring(ldb_module_get_ctx(module),
851 "acl: ACL check failed on crossRef object %s: %s\n",
852 ldb_dn_get_linearized(partition_res->msgs[0]->dn),
853 ldb_errstring(ldb));
854 return ret;
858 * TODO: Remaining checks, like if we are
859 * the naming master etc need to be handled
860 * in the instanceType module
862 return ldb_next_request(module, req);
865 /* Check that we can create a crossRef object MS-ADTS 3.1.1.5.2.8.2 */
866 ret = dsdb_module_check_access_on_dn(module, req, partitions_dn,
867 SEC_ADS_CREATE_CHILD,
868 &objectclass->schemaIDGUID, req);
869 if (ret == LDB_ERR_NO_SUCH_OBJECT &&
870 ldb_request_get_control(req, LDB_CONTROL_RELAX_OID))
872 /* Allow provision bootstrap */
873 ret = LDB_SUCCESS;
875 if (ret != LDB_SUCCESS) {
876 ldb_asprintf_errstring(ldb_module_get_ctx(module),
877 "acl: ACL check failed on CN=Partitions crossRef container %s: %s\n",
878 ldb_dn_get_linearized(partitions_dn), ldb_errstring(ldb));
879 return ret;
883 * TODO: Remaining checks, like if we are the naming
884 * master and adding the crossRef object need to be
885 * handled in the instanceType module
887 return ldb_next_request(module, req);
890 ret = dsdb_module_check_access_on_dn(module, req, parent,
891 SEC_ADS_CREATE_CHILD,
892 &objectclass->schemaIDGUID, req);
893 if (ret != LDB_SUCCESS) {
894 ldb_asprintf_errstring(ldb_module_get_ctx(module),
895 "acl: unable to get access to %s\n",
896 ldb_dn_get_linearized(req->op.add.message->dn));
897 return ret;
899 return ldb_next_request(module, req);
902 /* ckecks if modifications are allowed on "Member" attribute */
903 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
904 struct ldb_module *module,
905 struct ldb_request *req,
906 struct security_descriptor *sd,
907 struct dom_sid *sid,
908 const struct dsdb_attribute *attr,
909 const struct dsdb_class *objectclass)
911 int ret;
912 unsigned int i;
913 struct ldb_context *ldb = ldb_module_get_ctx(module);
914 struct ldb_dn *user_dn;
915 struct ldb_message_element *member_el;
916 /* if we have wp, we can do whatever we like */
917 if (acl_check_access_on_attribute(module,
918 mem_ctx,
920 sid,
921 SEC_ADS_WRITE_PROP,
922 attr, objectclass) == LDB_SUCCESS) {
923 return LDB_SUCCESS;
925 /* if we are adding/deleting ourselves, check for self membership */
926 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
927 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
928 &user_dn);
929 if (ret != LDB_SUCCESS) {
930 return ret;
932 member_el = ldb_msg_find_element(req->op.mod.message, "member");
933 if (!member_el) {
934 return ldb_operr(ldb);
936 /* user can only remove oneself */
937 if (member_el->num_values == 0) {
938 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
940 for (i = 0; i < member_el->num_values; i++) {
941 if (strcasecmp((const char *)member_el->values[i].data,
942 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
943 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
946 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
947 GUID_DRS_SELF_MEMBERSHIP,
948 SEC_ADS_SELF_WRITE,
949 sid);
950 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
951 dsdb_acl_debug(sd, acl_user_token(module),
952 req->op.mod.message->dn,
953 true,
954 10);
956 return ret;
959 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
960 struct ldb_module *module,
961 struct ldb_request *req,
962 struct security_descriptor *sd,
963 struct dom_sid *sid,
964 const struct dsdb_class *objectclass,
965 bool userPassword)
967 int ret = LDB_SUCCESS;
968 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
969 struct ldb_message_element *el;
970 struct ldb_message *msg;
971 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
972 "unicodePwd", "dBCSPwd", NULL }, **l;
973 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
975 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
976 if (msg == NULL) {
977 return ldb_module_oom(module);
979 for (l = passwordAttrs; *l != NULL; l++) {
980 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
981 continue;
984 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
985 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
986 ++del_attr_cnt;
988 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
989 ++add_attr_cnt;
991 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
992 ++rep_attr_cnt;
994 ldb_msg_remove_element(msg, el);
998 /* single deletes will be handled by the "password_hash" LDB module
999 * later in the stack, so we let it though here */
1000 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
1001 talloc_free(tmp_ctx);
1002 return LDB_SUCCESS;
1005 if (ldb_request_get_control(req,
1006 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
1007 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
1008 * have a user password change and not a set as the message
1009 * looks like. In it's value blob it contains the NT and/or LM
1010 * hash of the old password specified by the user.
1011 * This control is used by the SAMR and "kpasswd" password
1012 * change mechanisms. */
1013 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
1014 GUID_DRS_USER_CHANGE_PASSWORD,
1015 SEC_ADS_CONTROL_ACCESS,
1016 sid);
1017 goto checked;
1019 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
1020 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
1021 GUID_DRS_FORCE_CHANGE_PASSWORD,
1022 SEC_ADS_CONTROL_ACCESS,
1023 sid);
1024 goto checked;
1026 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
1027 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
1028 GUID_DRS_USER_CHANGE_PASSWORD,
1029 SEC_ADS_CONTROL_ACCESS,
1030 sid);
1031 /* Very strange, but we get constraint violation in this case */
1032 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
1033 ret = LDB_ERR_CONSTRAINT_VIOLATION;
1035 goto checked;
1038 talloc_free(tmp_ctx);
1039 return LDB_SUCCESS;
1041 checked:
1042 if (ret != LDB_SUCCESS) {
1043 dsdb_acl_debug(sd, acl_user_token(module),
1044 req->op.mod.message->dn,
1045 true,
1046 10);
1048 talloc_free(tmp_ctx);
1049 return ret;
1053 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
1055 int ret;
1056 struct ldb_context *ldb = ldb_module_get_ctx(module);
1057 const struct dsdb_schema *schema;
1058 unsigned int i;
1059 const struct dsdb_class *objectclass;
1060 struct ldb_result *acl_res;
1061 struct security_descriptor *sd;
1062 struct dom_sid *sid = NULL;
1063 struct ldb_control *as_system;
1064 struct ldb_control *is_undelete;
1065 bool userPassword;
1066 TALLOC_CTX *tmp_ctx;
1067 const struct ldb_message *msg = req->op.mod.message;
1068 static const char *acl_attrs[] = {
1069 "nTSecurityDescriptor",
1070 "objectClass",
1071 "objectSid",
1072 NULL
1075 if (ldb_dn_is_special(msg->dn)) {
1076 return ldb_next_request(module, req);
1079 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1080 if (as_system != NULL) {
1081 as_system->critical = 0;
1084 is_undelete = ldb_request_get_control(req, DSDB_CONTROL_RESTORE_TOMBSTONE_OID);
1086 /* Don't print this debug statement if elements[0].name is going to be NULL */
1087 if (msg->num_elements > 0) {
1088 DEBUG(10, ("ldb:acl_modify: %s\n", msg->elements[0].name));
1090 if (dsdb_module_am_system(module) || as_system) {
1091 return ldb_next_request(module, req);
1094 tmp_ctx = talloc_new(req);
1095 if (tmp_ctx == NULL) {
1096 return ldb_oom(ldb);
1099 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, msg->dn,
1100 acl_attrs,
1101 DSDB_FLAG_NEXT_MODULE |
1102 DSDB_FLAG_AS_SYSTEM |
1103 DSDB_SEARCH_SHOW_RECYCLED,
1104 req);
1106 if (ret != LDB_SUCCESS) {
1107 goto fail;
1110 userPassword = dsdb_user_password_support(module, req, req);
1112 schema = dsdb_get_schema(ldb, tmp_ctx);
1113 if (!schema) {
1114 talloc_free(tmp_ctx);
1115 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1116 "acl_modify: Error obtaining schema.");
1119 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
1120 if (ret != LDB_SUCCESS) {
1121 talloc_free(tmp_ctx);
1122 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1123 "acl_modify: Error retrieving security descriptor.");
1125 /* Theoretically we pass the check if the object has no sd */
1126 if (!sd) {
1127 goto success;
1130 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1131 if (!objectclass) {
1132 talloc_free(tmp_ctx);
1133 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1134 "acl_modify: Error retrieving object class for GUID.");
1136 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1137 for (i=0; i < msg->num_elements; i++) {
1138 const struct ldb_message_element *el = &msg->elements[i];
1139 const struct dsdb_attribute *attr;
1142 * This basic attribute existence check with the right errorcode
1143 * is needed since this module is the first one which requests
1144 * schema attribute information.
1145 * The complete attribute checking is done in the
1146 * "objectclass_attrs" module behind this one.
1148 * NOTE: "clearTextPassword" is not defined in the schema.
1150 attr = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
1151 if (!attr && ldb_attr_cmp("clearTextPassword", el->name) != 0) {
1152 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' "
1153 "on entry '%s' was not found in the schema!",
1154 req->op.mod.message->elements[i].name,
1155 ldb_dn_get_linearized(req->op.mod.message->dn));
1156 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1157 goto fail;
1160 if (ldb_attr_cmp("nTSecurityDescriptor", el->name) == 0) {
1161 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
1162 uint32_t access_mask = 0;
1164 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
1165 access_mask |= SEC_STD_WRITE_OWNER;
1167 if (sd_flags & SECINFO_DACL) {
1168 access_mask |= SEC_STD_WRITE_DAC;
1170 if (sd_flags & SECINFO_SACL) {
1171 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
1174 ret = acl_check_access_on_attribute(module,
1175 tmp_ctx,
1177 sid,
1178 access_mask,
1179 attr,
1180 objectclass);
1181 if (ret != LDB_SUCCESS) {
1182 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1183 "Object %s has no write dacl access\n",
1184 ldb_dn_get_linearized(msg->dn));
1185 dsdb_acl_debug(sd,
1186 acl_user_token(module),
1187 msg->dn,
1188 true,
1189 10);
1190 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1191 goto fail;
1193 } else if (ldb_attr_cmp("member", el->name) == 0) {
1194 ret = acl_check_self_membership(tmp_ctx,
1195 module,
1196 req,
1198 sid,
1199 attr,
1200 objectclass);
1201 if (ret != LDB_SUCCESS) {
1202 goto fail;
1204 } else if (ldb_attr_cmp("dBCSPwd", el->name) == 0) {
1205 /* this one is not affected by any rights, we should let it through
1206 so that passwords_hash returns the correct error */
1207 continue;
1208 } else if (ldb_attr_cmp("unicodePwd", el->name) == 0 ||
1209 (userPassword && ldb_attr_cmp("userPassword", el->name) == 0) ||
1210 ldb_attr_cmp("clearTextPassword", el->name) == 0) {
1211 ret = acl_check_password_rights(tmp_ctx,
1212 module,
1213 req,
1215 sid,
1216 objectclass,
1217 userPassword);
1218 if (ret != LDB_SUCCESS) {
1219 goto fail;
1221 } else if (ldb_attr_cmp("servicePrincipalName", el->name) == 0) {
1222 ret = acl_check_spn(tmp_ctx,
1223 module,
1224 req,
1226 sid,
1227 attr,
1228 objectclass);
1229 if (ret != LDB_SUCCESS) {
1230 goto fail;
1232 } else if (is_undelete != NULL && (ldb_attr_cmp("isDeleted", el->name) == 0)) {
1234 * in case of undelete op permissions on
1235 * isDeleted are irrelevant and
1236 * distinguishedName is removed by the
1237 * tombstone_reanimate module
1239 continue;
1240 } else {
1241 ret = acl_check_access_on_attribute(module,
1242 tmp_ctx,
1244 sid,
1245 SEC_ADS_WRITE_PROP,
1246 attr,
1247 objectclass);
1248 if (ret != LDB_SUCCESS) {
1249 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1250 "Object %s has no write property access\n",
1251 ldb_dn_get_linearized(msg->dn));
1252 dsdb_acl_debug(sd,
1253 acl_user_token(module),
1254 msg->dn,
1255 true,
1256 10);
1257 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1258 goto fail;
1263 success:
1264 talloc_free(tmp_ctx);
1265 return ldb_next_request(module, req);
1266 fail:
1267 talloc_free(tmp_ctx);
1268 return ret;
1271 /* similar to the modify for the time being.
1272 * We need to consider the special delete tree case, though - TODO */
1273 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1275 int ret;
1276 struct ldb_dn *parent;
1277 struct ldb_context *ldb;
1278 struct ldb_dn *nc_root;
1279 struct ldb_control *as_system;
1280 const struct dsdb_schema *schema;
1281 const struct dsdb_class *objectclass;
1282 struct security_descriptor *sd = NULL;
1283 struct dom_sid *sid = NULL;
1284 struct ldb_result *acl_res;
1285 static const char *acl_attrs[] = {
1286 "nTSecurityDescriptor",
1287 "objectClass",
1288 "objectSid",
1289 NULL
1292 if (ldb_dn_is_special(req->op.del.dn)) {
1293 return ldb_next_request(module, req);
1296 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1297 if (as_system != NULL) {
1298 as_system->critical = 0;
1301 if (dsdb_module_am_system(module) || as_system) {
1302 return ldb_next_request(module, req);
1305 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1307 ldb = ldb_module_get_ctx(module);
1309 parent = ldb_dn_get_parent(req, req->op.del.dn);
1310 if (parent == NULL) {
1311 return ldb_oom(ldb);
1314 /* Make sure we aren't deleting a NC */
1316 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1317 if (ret != LDB_SUCCESS) {
1318 return ret;
1320 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1321 talloc_free(nc_root);
1322 DEBUG(10,("acl:deleting a NC\n"));
1323 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1324 return ldb_module_done(req, NULL, NULL,
1325 LDB_ERR_UNWILLING_TO_PERFORM);
1327 talloc_free(nc_root);
1329 ret = dsdb_module_search_dn(module, req, &acl_res,
1330 req->op.del.dn, acl_attrs,
1331 DSDB_FLAG_NEXT_MODULE |
1332 DSDB_FLAG_AS_SYSTEM |
1333 DSDB_SEARCH_SHOW_RECYCLED, req);
1334 /* we sould be able to find the parent */
1335 if (ret != LDB_SUCCESS) {
1336 DEBUG(10,("acl: failed to find object %s\n",
1337 ldb_dn_get_linearized(req->op.rename.olddn)));
1338 return ret;
1341 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1342 if (ret != LDB_SUCCESS) {
1343 return ldb_operr(ldb);
1345 if (!sd) {
1346 return ldb_operr(ldb);
1349 schema = dsdb_get_schema(ldb, req);
1350 if (!schema) {
1351 return ldb_operr(ldb);
1354 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1356 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1357 if (!objectclass) {
1358 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1359 "acl_modify: Error retrieving object class for GUID.");
1362 if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID)) {
1363 ret = acl_check_access_on_objectclass(module, req, sd, sid,
1364 SEC_ADS_DELETE_TREE,
1365 objectclass);
1366 if (ret != LDB_SUCCESS) {
1367 return ret;
1370 return ldb_next_request(module, req);
1373 /* First check if we have delete object right */
1374 ret = acl_check_access_on_objectclass(module, req, sd, sid,
1375 SEC_STD_DELETE,
1376 objectclass);
1377 if (ret == LDB_SUCCESS) {
1378 return ldb_next_request(module, req);
1381 /* Nope, we don't have delete object. Lets check if we have delete
1382 * child on the parent */
1383 ret = dsdb_module_check_access_on_dn(module, req, parent,
1384 SEC_ADS_DELETE_CHILD,
1385 &objectclass->schemaIDGUID,
1386 req);
1387 if (ret != LDB_SUCCESS) {
1388 return ret;
1391 return ldb_next_request(module, req);
1393 static int acl_check_reanimate_tombstone(TALLOC_CTX *mem_ctx,
1394 struct ldb_module *module,
1395 struct ldb_request *req,
1396 struct ldb_dn *nc_root)
1398 int ret;
1399 struct ldb_result *acl_res;
1400 struct security_descriptor *sd = NULL;
1401 struct dom_sid *sid = NULL;
1402 static const char *acl_attrs[] = {
1403 "nTSecurityDescriptor",
1404 "objectClass",
1405 "objectSid",
1406 NULL
1409 ret = dsdb_module_search_dn(module, mem_ctx, &acl_res,
1410 nc_root, acl_attrs,
1411 DSDB_FLAG_NEXT_MODULE |
1412 DSDB_FLAG_AS_SYSTEM |
1413 DSDB_SEARCH_SHOW_RECYCLED, req);
1414 if (ret != LDB_SUCCESS) {
1415 DEBUG(10,("acl: failed to find object %s\n",
1416 ldb_dn_get_linearized(nc_root)));
1417 return ret;
1420 ret = dsdb_get_sd_from_ldb_message(mem_ctx, req, acl_res->msgs[0], &sd);
1421 sid = samdb_result_dom_sid(mem_ctx, acl_res->msgs[0], "objectSid");
1422 if (ret != LDB_SUCCESS || !sd) {
1423 return ldb_operr(ldb_module_get_ctx(module));
1425 return acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
1426 GUID_DRS_REANIMATE_TOMBSTONE,
1427 SEC_ADS_CONTROL_ACCESS, sid);
1430 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1432 int ret;
1433 struct ldb_dn *oldparent;
1434 struct ldb_dn *newparent;
1435 const struct dsdb_schema *schema;
1436 const struct dsdb_class *objectclass;
1437 const struct dsdb_attribute *attr = NULL;
1438 struct ldb_context *ldb;
1439 struct security_descriptor *sd = NULL;
1440 struct dom_sid *sid = NULL;
1441 struct ldb_result *acl_res;
1442 struct ldb_dn *nc_root;
1443 struct ldb_control *as_system;
1444 struct ldb_control *is_undelete;
1445 TALLOC_CTX *tmp_ctx;
1446 const char *rdn_name;
1447 static const char *acl_attrs[] = {
1448 "nTSecurityDescriptor",
1449 "objectClass",
1450 "objectSid",
1451 NULL
1454 if (ldb_dn_is_special(req->op.rename.olddn)) {
1455 return ldb_next_request(module, req);
1458 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1459 if (as_system != NULL) {
1460 as_system->critical = 0;
1463 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1464 if (dsdb_module_am_system(module) || as_system) {
1465 return ldb_next_request(module, req);
1468 ldb = ldb_module_get_ctx(module);
1470 tmp_ctx = talloc_new(req);
1471 if (tmp_ctx == NULL) {
1472 return ldb_oom(ldb);
1475 oldparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.olddn);
1476 if (oldparent == NULL) {
1477 return ldb_oom(ldb);
1479 newparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.newdn);
1480 if (newparent == NULL) {
1481 return ldb_oom(ldb);
1484 /* Make sure we aren't renaming/moving a NC */
1486 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1487 if (ret != LDB_SUCCESS) {
1488 return ret;
1490 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1491 talloc_free(nc_root);
1492 DEBUG(10,("acl:renaming/moving a NC\n"));
1493 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1494 return ldb_module_done(req, NULL, NULL,
1495 LDB_ERR_UNWILLING_TO_PERFORM);
1498 /* special check for undelete operation */
1499 is_undelete = ldb_request_get_control(req, DSDB_CONTROL_RESTORE_TOMBSTONE_OID);
1500 if (is_undelete != NULL) {
1501 is_undelete->critical = 0;
1502 ret = acl_check_reanimate_tombstone(tmp_ctx, module, req, nc_root);
1503 if (ret != LDB_SUCCESS) {
1504 talloc_free(tmp_ctx);
1505 return ret;
1508 talloc_free(nc_root);
1510 /* Look for the parent */
1512 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1513 req->op.rename.olddn, acl_attrs,
1514 DSDB_FLAG_NEXT_MODULE |
1515 DSDB_FLAG_AS_SYSTEM |
1516 DSDB_SEARCH_SHOW_RECYCLED, req);
1517 /* we sould be able to find the parent */
1518 if (ret != LDB_SUCCESS) {
1519 DEBUG(10,("acl: failed to find object %s\n",
1520 ldb_dn_get_linearized(req->op.rename.olddn)));
1521 talloc_free(tmp_ctx);
1522 return ret;
1525 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1526 if (ret != LDB_SUCCESS) {
1527 talloc_free(tmp_ctx);
1528 return ldb_operr(ldb);
1530 if (!sd) {
1531 talloc_free(tmp_ctx);
1532 return ldb_operr(ldb);
1535 schema = dsdb_get_schema(ldb, acl_res);
1536 if (!schema) {
1537 talloc_free(tmp_ctx);
1538 return ldb_operr(ldb);
1541 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1543 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1544 if (!objectclass) {
1545 talloc_free(tmp_ctx);
1546 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1547 "acl_modify: Error retrieving object class for GUID.");
1550 attr = dsdb_attribute_by_lDAPDisplayName(schema, "name");
1551 if (attr == NULL) {
1552 talloc_free(tmp_ctx);
1553 return ldb_operr(ldb);
1556 ret = acl_check_access_on_attribute(module, tmp_ctx, sd, sid,
1557 SEC_ADS_WRITE_PROP,
1558 attr, objectclass);
1559 if (ret != LDB_SUCCESS) {
1560 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1561 "Object %s has no wp on %s\n",
1562 ldb_dn_get_linearized(req->op.rename.olddn),
1563 attr->lDAPDisplayName);
1564 dsdb_acl_debug(sd,
1565 acl_user_token(module),
1566 req->op.rename.olddn,
1567 true,
1568 10);
1569 talloc_free(tmp_ctx);
1570 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1573 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1574 if (rdn_name == NULL) {
1575 talloc_free(tmp_ctx);
1576 return ldb_operr(ldb);
1579 attr = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
1580 if (attr == NULL) {
1581 talloc_free(tmp_ctx);
1582 return ldb_operr(ldb);
1585 ret = acl_check_access_on_attribute(module, tmp_ctx, sd, sid,
1586 SEC_ADS_WRITE_PROP,
1587 attr, objectclass);
1588 if (ret != LDB_SUCCESS) {
1589 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1590 "Object %s has no wp on %s\n",
1591 ldb_dn_get_linearized(req->op.rename.olddn),
1592 attr->lDAPDisplayName);
1593 dsdb_acl_debug(sd,
1594 acl_user_token(module),
1595 req->op.rename.olddn,
1596 true,
1597 10);
1598 talloc_free(tmp_ctx);
1599 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1602 if (ldb_dn_compare(oldparent, newparent) == 0) {
1603 /* regular rename, not move, nothing more to do */
1604 talloc_free(tmp_ctx);
1605 return ldb_next_request(module, req);
1608 /* new parent should have create child */
1609 ret = dsdb_module_check_access_on_dn(module, req, newparent,
1610 SEC_ADS_CREATE_CHILD,
1611 &objectclass->schemaIDGUID, req);
1612 if (ret != LDB_SUCCESS) {
1613 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1614 "acl:access_denied renaming %s",
1615 ldb_dn_get_linearized(req->op.rename.olddn));
1616 talloc_free(tmp_ctx);
1617 return ret;
1620 /* do we have delete object on the object? */
1621 /* this access is not necessary for undelete ops */
1622 if (is_undelete == NULL) {
1623 ret = acl_check_access_on_objectclass(module, tmp_ctx, sd, sid,
1624 SEC_STD_DELETE,
1625 objectclass);
1626 if (ret == LDB_SUCCESS) {
1627 talloc_free(tmp_ctx);
1628 return ldb_next_request(module, req);
1630 /* what about delete child on the current parent */
1631 ret = dsdb_module_check_access_on_dn(module, req, oldparent,
1632 SEC_ADS_DELETE_CHILD,
1633 &objectclass->schemaIDGUID,
1634 req);
1635 if (ret != LDB_SUCCESS) {
1636 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1637 "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1638 talloc_free(tmp_ctx);
1639 return ldb_module_done(req, NULL, NULL, ret);
1642 talloc_free(tmp_ctx);
1644 return ldb_next_request(module, req);
1647 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1648 struct acl_private *data)
1650 struct dsdb_attribute *a;
1651 uint32_t n = 0;
1653 if (data->acl_search) {
1655 * If acl:search is activated, the acl_read module
1656 * protects confidential attributes.
1658 return LDB_SUCCESS;
1661 if ((ac->schema == data->cached_schema_ptr) &&
1662 (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1664 return LDB_SUCCESS;
1667 data->cached_schema_ptr = NULL;
1668 data->cached_schema_loaded_usn = 0;
1669 data->cached_schema_metadata_usn = 0;
1670 TALLOC_FREE(data->confidential_attrs);
1672 if (ac->schema == NULL) {
1673 return LDB_SUCCESS;
1676 for (a = ac->schema->attributes; a; a = a->next) {
1677 const char **attrs = data->confidential_attrs;
1679 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1680 continue;
1683 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1684 if (attrs == NULL) {
1685 TALLOC_FREE(data->confidential_attrs);
1686 return ldb_module_oom(ac->module);
1689 attrs[n] = a->lDAPDisplayName;
1690 attrs[n+1] = NULL;
1691 n++;
1693 data->confidential_attrs = attrs;
1696 data->cached_schema_ptr = ac->schema;
1697 data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1699 return LDB_SUCCESS;
1702 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1704 struct acl_context *ac;
1705 struct acl_private *data;
1706 struct ldb_result *acl_res;
1707 static const char *acl_attrs[] = {
1708 "objectClass",
1709 "nTSecurityDescriptor",
1710 "objectSid",
1711 NULL
1713 int ret;
1714 unsigned int i;
1716 ac = talloc_get_type(req->context, struct acl_context);
1717 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1718 if (!ares) {
1719 return ldb_module_done(ac->req, NULL, NULL,
1720 LDB_ERR_OPERATIONS_ERROR);
1722 if (ares->error != LDB_SUCCESS) {
1723 return ldb_module_done(ac->req, ares->controls,
1724 ares->response, ares->error);
1727 switch (ares->type) {
1728 case LDB_REPLY_ENTRY:
1729 if (ac->constructed_attrs) {
1730 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1731 acl_attrs,
1732 DSDB_FLAG_NEXT_MODULE |
1733 DSDB_FLAG_AS_SYSTEM |
1734 DSDB_SEARCH_SHOW_RECYCLED,
1735 req);
1736 if (ret != LDB_SUCCESS) {
1737 return ldb_module_done(ac->req, NULL, NULL, ret);
1741 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1742 ret = acl_allowedAttributes(ac->module, ac->schema,
1743 acl_res->msgs[0],
1744 ares->message, ac);
1745 if (ret != LDB_SUCCESS) {
1746 return ldb_module_done(ac->req, NULL, NULL, ret);
1750 if (ac->allowedChildClasses) {
1751 ret = acl_childClasses(ac->module, ac->schema,
1752 acl_res->msgs[0],
1753 ares->message,
1754 "allowedChildClasses");
1755 if (ret != LDB_SUCCESS) {
1756 return ldb_module_done(ac->req, NULL, NULL, ret);
1760 if (ac->allowedChildClassesEffective) {
1761 ret = acl_childClassesEffective(ac->module, ac->schema,
1762 acl_res->msgs[0],
1763 ares->message, ac);
1764 if (ret != LDB_SUCCESS) {
1765 return ldb_module_done(ac->req, NULL, NULL, ret);
1769 if (ac->sDRightsEffective) {
1770 ret = acl_sDRightsEffective(ac->module,
1771 acl_res->msgs[0],
1772 ares->message, ac);
1773 if (ret != LDB_SUCCESS) {
1774 return ldb_module_done(ac->req, NULL, NULL, ret);
1778 if (data == NULL) {
1779 return ldb_module_send_entry(ac->req, ares->message,
1780 ares->controls);
1783 if (ac->am_system) {
1784 return ldb_module_send_entry(ac->req, ares->message,
1785 ares->controls);
1788 if (data->password_attrs != NULL) {
1789 for (i = 0; data->password_attrs[i]; i++) {
1790 if ((!ac->userPassword) &&
1791 (ldb_attr_cmp(data->password_attrs[i],
1792 "userPassword") == 0))
1794 continue;
1797 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1801 if (ac->am_administrator) {
1802 return ldb_module_send_entry(ac->req, ares->message,
1803 ares->controls);
1806 ret = acl_search_update_confidential_attrs(ac, data);
1807 if (ret != LDB_SUCCESS) {
1808 return ret;
1811 if (data->confidential_attrs != NULL) {
1812 for (i = 0; data->confidential_attrs[i]; i++) {
1813 ldb_msg_remove_attr(ares->message,
1814 data->confidential_attrs[i]);
1818 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1820 case LDB_REPLY_REFERRAL:
1821 return ldb_module_send_referral(ac->req, ares->referral);
1823 case LDB_REPLY_DONE:
1824 return ldb_module_done(ac->req, ares->controls,
1825 ares->response, LDB_SUCCESS);
1828 return LDB_SUCCESS;
1831 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1833 struct ldb_context *ldb;
1834 struct acl_context *ac;
1835 struct ldb_parse_tree *down_tree;
1836 struct ldb_request *down_req;
1837 struct acl_private *data;
1838 int ret;
1839 unsigned int i;
1841 if (ldb_dn_is_special(req->op.search.base)) {
1842 return ldb_next_request(module, req);
1845 ldb = ldb_module_get_ctx(module);
1847 ac = talloc_zero(req, struct acl_context);
1848 if (ac == NULL) {
1849 return ldb_oom(ldb);
1851 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1853 ac->module = module;
1854 ac->req = req;
1855 ac->am_system = dsdb_module_am_system(module);
1856 ac->am_administrator = dsdb_module_am_administrator(module);
1857 ac->constructed_attrs = false;
1858 ac->modify_search = true;
1859 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1860 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1861 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1862 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1863 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1864 ac->userPassword = true;
1865 ac->schema = dsdb_get_schema(ldb, ac);
1867 ac->constructed_attrs |= ac->allowedAttributes;
1868 ac->constructed_attrs |= ac->allowedChildClasses;
1869 ac->constructed_attrs |= ac->allowedChildClassesEffective;
1870 ac->constructed_attrs |= ac->allowedAttributesEffective;
1871 ac->constructed_attrs |= ac->sDRightsEffective;
1873 if (data == NULL) {
1874 ac->modify_search = false;
1876 if (ac->am_system) {
1877 ac->modify_search = false;
1880 if (!ac->constructed_attrs && !ac->modify_search) {
1881 talloc_free(ac);
1882 return ldb_next_request(module, req);
1885 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1886 if (data != NULL) {
1887 ac->userPassword = data->userPassword_support;
1890 ret = acl_search_update_confidential_attrs(ac, data);
1891 if (ret != LDB_SUCCESS) {
1892 return ret;
1895 down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1896 if (down_tree == NULL) {
1897 return ldb_oom(ldb);
1900 if (!ac->am_system && data->password_attrs) {
1901 for (i = 0; data->password_attrs[i]; i++) {
1902 if ((!ac->userPassword) &&
1903 (ldb_attr_cmp(data->password_attrs[i],
1904 "userPassword") == 0))
1906 continue;
1909 ldb_parse_tree_attr_replace(down_tree,
1910 data->password_attrs[i],
1911 "kludgeACLredactedattribute");
1915 if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1916 for (i = 0; data->confidential_attrs[i]; i++) {
1917 ldb_parse_tree_attr_replace(down_tree,
1918 data->confidential_attrs[i],
1919 "kludgeACLredactedattribute");
1923 ret = ldb_build_search_req_ex(&down_req,
1924 ldb, ac,
1925 req->op.search.base,
1926 req->op.search.scope,
1927 down_tree,
1928 req->op.search.attrs,
1929 req->controls,
1930 ac, acl_search_callback,
1931 req);
1932 LDB_REQ_SET_LOCATION(down_req);
1933 if (ret != LDB_SUCCESS) {
1934 return ret;
1936 /* perform the search */
1937 return ldb_next_request(module, down_req);
1940 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1942 struct ldb_context *ldb = ldb_module_get_ctx(module);
1943 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1945 /* allow everybody to read the sequence number */
1946 if (strcmp(req->op.extended.oid,
1947 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1948 return ldb_next_request(module, req);
1951 if (dsdb_module_am_system(module) ||
1952 dsdb_module_am_administrator(module) || as_system) {
1953 return ldb_next_request(module, req);
1954 } else {
1955 ldb_asprintf_errstring(ldb,
1956 "acl_extended: "
1957 "attempted database modify not permitted. "
1958 "User %s is not SYSTEM or an administrator",
1959 acl_user_name(req, module));
1960 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1964 static const struct ldb_module_ops ldb_acl_module_ops = {
1965 .name = "acl",
1966 .search = acl_search,
1967 .add = acl_add,
1968 .modify = acl_modify,
1969 .del = acl_delete,
1970 .rename = acl_rename,
1971 .extended = acl_extended,
1972 .init_context = acl_module_init
1975 int ldb_acl_module_init(const char *version)
1977 LDB_MODULE_CHECK_VERSION(version);
1978 return ldb_register_module(&ldb_acl_module_ops);