dsdb-acl: use dsdb_get_structural_oc_from_msg() rather than class_schemaid_guid_by_lD...
[Samba/gebeck_regimport.git] / source4 / dsdb / samdb / ldb_modules / acl.c
blob2a1a8538ac4e39e81db37834f88423f2d8a867f1
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;
60 struct acl_context {
61 struct ldb_module *module;
62 struct ldb_request *req;
63 bool am_system;
64 bool am_administrator;
65 bool modify_search;
66 bool constructed_attrs;
67 bool allowedAttributes;
68 bool allowedAttributesEffective;
69 bool allowedChildClasses;
70 bool allowedChildClassesEffective;
71 bool sDRightsEffective;
72 bool userPassword;
73 const char * const *attrs;
74 struct dsdb_schema *schema;
77 static int acl_module_init(struct ldb_module *module)
79 struct ldb_context *ldb;
80 struct acl_private *data;
81 int ret;
82 unsigned int i, n, j;
83 TALLOC_CTX *mem_ctx;
84 static const char * const attrs[] = { "passwordAttribute", NULL };
85 static const char * const secret_attrs[] = {
86 DSDB_SECRET_ATTRIBUTES
88 struct ldb_result *res;
89 struct ldb_message *msg;
90 struct ldb_message_element *password_attributes;
92 ldb = ldb_module_get_ctx(module);
94 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
95 if (ret != LDB_SUCCESS) {
96 ldb_debug(ldb, LDB_DEBUG_ERROR,
97 "acl_module_init: Unable to register control with rootdse!\n");
98 return ldb_operr(ldb);
101 data = talloc_zero(module, struct acl_private);
102 if (data == NULL) {
103 return ldb_oom(ldb);
106 data->acl_search = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
107 NULL, "acl", "search", true);
108 ldb_module_set_private(module, data);
110 mem_ctx = talloc_new(module);
111 if (!mem_ctx) {
112 return ldb_oom(ldb);
115 ret = dsdb_module_search_dn(module, mem_ctx, &res,
116 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
117 attrs,
118 DSDB_FLAG_NEXT_MODULE |
119 DSDB_FLAG_AS_SYSTEM,
120 NULL);
121 if (ret != LDB_SUCCESS) {
122 goto done;
124 if (res->count == 0) {
125 goto done;
128 if (res->count > 1) {
129 talloc_free(mem_ctx);
130 return LDB_ERR_CONSTRAINT_VIOLATION;
133 msg = res->msgs[0];
135 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
136 if (!password_attributes) {
137 goto done;
139 data->password_attrs = talloc_array(data, const char *,
140 password_attributes->num_values +
141 ARRAY_SIZE(secret_attrs) + 1);
142 if (!data->password_attrs) {
143 talloc_free(mem_ctx);
144 return ldb_oom(ldb);
147 n = 0;
148 for (i=0; i < password_attributes->num_values; i++) {
149 data->password_attrs[n] = (const char *)password_attributes->values[i].data;
150 talloc_steal(data->password_attrs, password_attributes->values[i].data);
151 n++;
154 for (i=0; i < ARRAY_SIZE(secret_attrs); i++) {
155 bool found = false;
157 for (j=0; j < n; j++) {
158 if (strcasecmp(data->password_attrs[j], secret_attrs[i]) == 0) {
159 found = true;
160 break;
164 if (found) {
165 continue;
168 data->password_attrs[n] = talloc_strdup(data->password_attrs,
169 secret_attrs[i]);
170 if (data->password_attrs[n] == NULL) {
171 talloc_free(mem_ctx);
172 return ldb_oom(ldb);
174 n++;
176 data->password_attrs[n] = NULL;
178 done:
179 talloc_free(mem_ctx);
180 return ldb_next_init(module);
183 static int acl_allowedAttributes(struct ldb_module *module,
184 const struct dsdb_schema *schema,
185 struct ldb_message *sd_msg,
186 struct ldb_message *msg,
187 struct acl_context *ac)
189 struct ldb_message_element *oc_el;
190 struct ldb_context *ldb = ldb_module_get_ctx(module);
191 TALLOC_CTX *mem_ctx;
192 const char **attr_list;
193 int i, ret;
195 /* If we don't have a schema yet, we can't do anything... */
196 if (schema == NULL) {
197 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
198 return LDB_ERR_OPERATIONS_ERROR;
201 /* Must remove any existing attribute */
202 if (ac->allowedAttributes) {
203 ldb_msg_remove_attr(msg, "allowedAttributes");
206 mem_ctx = talloc_new(msg);
207 if (!mem_ctx) {
208 return ldb_oom(ldb);
211 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
212 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
213 if (!attr_list) {
214 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
215 talloc_free(mem_ctx);
216 return LDB_ERR_OPERATIONS_ERROR;
218 if (ac->allowedAttributes) {
219 for (i=0; attr_list && attr_list[i]; i++) {
220 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
223 if (ac->allowedAttributesEffective) {
224 struct security_descriptor *sd;
225 struct dom_sid *sid = NULL;
226 struct ldb_control *as_system = ldb_request_get_control(ac->req,
227 LDB_CONTROL_AS_SYSTEM_OID);
229 if (as_system != NULL) {
230 as_system->critical = 0;
233 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
234 if (ac->am_system || as_system) {
235 for (i=0; attr_list && attr_list[i]; i++) {
236 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
238 return LDB_SUCCESS;
241 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
243 if (ret != LDB_SUCCESS) {
244 return ret;
247 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
248 for (i=0; attr_list && attr_list[i]; i++) {
249 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
250 attr_list[i]);
251 if (!attr) {
252 return ldb_operr(ldb);
254 /* remove constructed attributes */
255 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
256 || attr->systemOnly
257 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
258 continue;
260 ret = acl_check_access_on_attribute(module,
261 msg,
263 sid,
264 SEC_ADS_WRITE_PROP,
265 attr);
266 if (ret == LDB_SUCCESS) {
267 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
271 return LDB_SUCCESS;
274 static int acl_childClasses(struct ldb_module *module,
275 const struct dsdb_schema *schema,
276 struct ldb_message *sd_msg,
277 struct ldb_message *msg,
278 const char *attrName)
280 struct ldb_message_element *oc_el;
281 struct ldb_message_element *allowedClasses;
282 const struct dsdb_class *sclass;
283 unsigned int i, j;
284 int ret;
286 /* If we don't have a schema yet, we can't do anything... */
287 if (schema == NULL) {
288 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
289 return LDB_ERR_OPERATIONS_ERROR;
292 /* Must remove any existing attribute, or else confusion reins */
293 ldb_msg_remove_attr(msg, attrName);
294 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
295 if (ret != LDB_SUCCESS) {
296 return ret;
299 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
301 for (i=0; oc_el && i < oc_el->num_values; i++) {
302 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
303 if (!sclass) {
304 /* We don't know this class? what is going on? */
305 continue;
308 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
309 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
312 if (allowedClasses->num_values > 1) {
313 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
314 for (i=1 ; i < allowedClasses->num_values; i++) {
315 struct ldb_val *val1 = &allowedClasses->values[i-1];
316 struct ldb_val *val2 = &allowedClasses->values[i];
317 if (data_blob_cmp(val1, val2) == 0) {
318 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
319 allowedClasses->num_values--;
320 i--;
325 return LDB_SUCCESS;
328 static int acl_check_access_on_class(struct ldb_module *module,
329 const struct dsdb_schema *schema,
330 TALLOC_CTX *mem_ctx,
331 struct security_descriptor *sd,
332 struct security_token *token,
333 struct dom_sid *rp_sid,
334 uint32_t access_mask,
335 const char *class_name)
337 int ret;
338 NTSTATUS status;
339 uint32_t access_granted;
340 struct object_tree *root = NULL;
341 struct object_tree *new_node = NULL;
342 const struct GUID *guid;
344 if (class_name != NULL) {
345 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
346 if (!guid) {
347 DEBUG(10, ("acl_search: cannot find class %s\n",
348 class_name));
349 goto fail;
351 if (!insert_in_object_tree(mem_ctx,
352 guid, access_mask,
353 &root, &new_node)) {
354 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
355 goto fail;
359 status = sec_access_check_ds(sd, token,
360 access_mask,
361 &access_granted,
362 root,
363 rp_sid);
364 if (!NT_STATUS_IS_OK(status)) {
365 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
366 } else {
367 ret = LDB_SUCCESS;
369 return ret;
370 fail:
371 return ldb_operr(ldb_module_get_ctx(module));
374 static int acl_childClassesEffective(struct ldb_module *module,
375 const struct dsdb_schema *schema,
376 struct ldb_message *sd_msg,
377 struct ldb_message *msg,
378 struct acl_context *ac)
380 struct ldb_message_element *oc_el;
381 struct ldb_message_element *allowedClasses = NULL;
382 const struct dsdb_class *sclass;
383 struct security_descriptor *sd;
384 struct ldb_control *as_system = ldb_request_get_control(ac->req,
385 LDB_CONTROL_AS_SYSTEM_OID);
386 struct dom_sid *sid = NULL;
387 unsigned int i, j;
388 int ret;
390 if (as_system != NULL) {
391 as_system->critical = 0;
394 if (ac->am_system || as_system) {
395 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
398 /* If we don't have a schema yet, we can't do anything... */
399 if (schema == NULL) {
400 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
401 return LDB_ERR_OPERATIONS_ERROR;
404 /* Must remove any existing attribute, or else confusion reins */
405 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
407 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
408 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
409 if (ret != LDB_SUCCESS) {
410 return ret;
413 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
414 for (i=0; oc_el && i < oc_el->num_values; i++) {
415 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
416 if (!sclass) {
417 /* We don't know this class? what is going on? */
418 continue;
421 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
422 ret = acl_check_access_on_class(module,
423 schema,
424 msg,
426 acl_user_token(module),
427 sid,
428 SEC_ADS_CREATE_CHILD,
429 sclass->possibleInferiors[j]);
430 if (ret == LDB_SUCCESS) {
431 ldb_msg_add_string(msg, "allowedChildClassesEffective",
432 sclass->possibleInferiors[j]);
436 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
437 if (!allowedClasses) {
438 return LDB_SUCCESS;
441 if (allowedClasses->num_values > 1) {
442 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
443 for (i=1 ; i < allowedClasses->num_values; i++) {
444 struct ldb_val *val1 = &allowedClasses->values[i-1];
445 struct ldb_val *val2 = &allowedClasses->values[i];
446 if (data_blob_cmp(val1, val2) == 0) {
447 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
448 allowedClasses->num_values--;
449 i--;
453 return LDB_SUCCESS;
456 static int acl_sDRightsEffective(struct ldb_module *module,
457 struct ldb_message *sd_msg,
458 struct ldb_message *msg,
459 struct acl_context *ac)
461 struct ldb_context *ldb = ldb_module_get_ctx(module);
462 struct ldb_message_element *rightsEffective;
463 int ret;
464 struct security_descriptor *sd;
465 struct ldb_control *as_system = ldb_request_get_control(ac->req,
466 LDB_CONTROL_AS_SYSTEM_OID);
467 struct dom_sid *sid = NULL;
468 uint32_t flags = 0;
470 if (as_system != NULL) {
471 as_system->critical = 0;
474 /* Must remove any existing attribute, or else confusion reins */
475 ldb_msg_remove_attr(msg, "sDRightsEffective");
476 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
477 if (ret != LDB_SUCCESS) {
478 return ret;
480 if (ac->am_system || as_system) {
481 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
483 else {
484 const struct dsdb_attribute *attr;
486 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
487 "nTSecurityDescriptor");
488 if (attr == NULL) {
489 return ldb_operr(ldb);
492 /* Get the security descriptor from the message */
493 ret = dsdb_get_sd_from_ldb_message(ldb, msg, sd_msg, &sd);
494 if (ret != LDB_SUCCESS) {
495 return ret;
497 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
498 ret = acl_check_access_on_attribute(module,
499 msg,
501 sid,
502 SEC_STD_WRITE_OWNER,
503 attr);
504 if (ret == LDB_SUCCESS) {
505 flags |= SECINFO_OWNER | SECINFO_GROUP;
507 ret = acl_check_access_on_attribute(module,
508 msg,
510 sid,
511 SEC_STD_WRITE_DAC,
512 attr);
513 if (ret == LDB_SUCCESS) {
514 flags |= SECINFO_DACL;
516 ret = acl_check_access_on_attribute(module,
517 msg,
519 sid,
520 SEC_FLAG_SYSTEM_SECURITY,
521 attr);
522 if (ret == LDB_SUCCESS) {
523 flags |= SECINFO_SACL;
526 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
527 "sDRightsEffective", flags);
530 static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
531 struct ldb_context *ldb,
532 const char *spn_value,
533 uint32_t userAccountControl,
534 const char *samAccountName,
535 const char *dnsHostName,
536 const char *netbios_name,
537 const char *ntds_guid)
539 int ret;
540 krb5_context krb_ctx;
541 krb5_error_code kerr;
542 krb5_principal principal;
543 char *instanceName;
544 char *serviceType;
545 char *serviceName;
546 const char *forest_name = samdb_forest_name(ldb, mem_ctx);
547 const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
548 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
549 struct loadparm_context);
550 bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
551 (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
553 if (strcasecmp_m(spn_value, samAccountName) == 0) {
554 /* MacOS X sets this value, and setting an SPN of your
555 * own samAccountName is both pointless and safe */
556 return LDB_SUCCESS;
559 kerr = smb_krb5_init_context_basic(mem_ctx,
560 lp_ctx,
561 &krb_ctx);
562 if (kerr != 0) {
563 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
564 "Could not initialize kerberos context.");
567 ret = krb5_parse_name(krb_ctx, spn_value, &principal);
568 if (ret) {
569 krb5_free_context(krb_ctx);
570 return LDB_ERR_CONSTRAINT_VIOLATION;
573 if (principal->name.name_string.len < 2) {
574 goto fail;
577 instanceName = principal->name.name_string.val[1];
578 serviceType = principal->name.name_string.val[0];
579 if (principal->name.name_string.len == 3) {
580 serviceName = principal->name.name_string.val[2];
581 } else {
582 serviceName = NULL;
585 if (serviceName) {
586 if (!is_dc) {
587 goto fail;
589 if (strcasecmp(serviceType, "ldap") == 0) {
590 if (strcasecmp(serviceName, netbios_name) != 0 &&
591 strcasecmp(serviceName, forest_name) != 0) {
592 goto fail;
595 } else if (strcasecmp(serviceType, "gc") == 0) {
596 if (strcasecmp(serviceName, forest_name) != 0) {
597 goto fail;
599 } else {
600 if (strcasecmp(serviceName, base_domain) != 0 &&
601 strcasecmp(serviceName, netbios_name) != 0) {
602 goto fail;
606 /* instanceName can be samAccountName without $ or dnsHostName
607 * or "ntds_guid._msdcs.forest_domain for DC objects */
608 if (strlen(instanceName) == (strlen(samAccountName) - 1)
609 && strncasecmp(instanceName, samAccountName, strlen(samAccountName) - 1) == 0) {
610 goto success;
611 } else if (dnsHostName != NULL && strcasecmp(instanceName, dnsHostName) == 0) {
612 goto success;
613 } else if (is_dc) {
614 const char *guid_str;
615 guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
616 ntds_guid,
617 forest_name);
618 if (strcasecmp(instanceName, guid_str) == 0) {
619 goto success;
623 fail:
624 krb5_free_principal(krb_ctx, principal);
625 krb5_free_context(krb_ctx);
626 return LDB_ERR_CONSTRAINT_VIOLATION;
628 success:
629 krb5_free_principal(krb_ctx, principal);
630 krb5_free_context(krb_ctx);
631 return LDB_SUCCESS;
634 static int acl_check_spn(TALLOC_CTX *mem_ctx,
635 struct ldb_module *module,
636 struct ldb_request *req,
637 struct security_descriptor *sd,
638 struct dom_sid *sid,
639 const struct GUID *oc_guid,
640 const struct dsdb_attribute *attr)
642 int ret;
643 unsigned int i;
644 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
645 struct ldb_context *ldb = ldb_module_get_ctx(module);
646 struct ldb_result *acl_res;
647 struct ldb_result *netbios_res;
648 struct ldb_message_element *el;
649 struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
650 uint32_t userAccountControl;
651 const char *samAccountName;
652 const char *dnsHostName;
653 const char *netbios_name;
654 struct GUID ntds;
655 char *ntds_guid = NULL;
657 static const char *acl_attrs[] = {
658 "samAccountName",
659 "dnsHostName",
660 "userAccountControl",
661 NULL
663 static const char *netbios_attrs[] = {
664 "nETBIOSName",
665 NULL
668 /* if we have wp, we can do whatever we like */
669 if (acl_check_access_on_attribute(module,
670 tmp_ctx,
672 sid,
673 SEC_ADS_WRITE_PROP,
674 attr) == LDB_SUCCESS) {
675 talloc_free(tmp_ctx);
676 return LDB_SUCCESS;
679 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
680 GUID_DRS_VALIDATE_SPN,
681 SEC_ADS_SELF_WRITE,
682 sid);
684 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
685 dsdb_acl_debug(sd, acl_user_token(module),
686 req->op.mod.message->dn,
687 true,
688 10);
689 talloc_free(tmp_ctx);
690 return ret;
693 ret = dsdb_module_search_dn(module, tmp_ctx,
694 &acl_res, req->op.mod.message->dn,
695 acl_attrs,
696 DSDB_FLAG_NEXT_MODULE |
697 DSDB_FLAG_AS_SYSTEM |
698 DSDB_SEARCH_SHOW_RECYCLED,
699 req);
700 if (ret != LDB_SUCCESS) {
701 talloc_free(tmp_ctx);
702 return ret;
705 userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
706 dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
707 samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
709 ret = dsdb_module_search(module, tmp_ctx,
710 &netbios_res, partitions_dn,
711 LDB_SCOPE_ONELEVEL,
712 netbios_attrs,
713 DSDB_FLAG_NEXT_MODULE |
714 DSDB_FLAG_AS_SYSTEM,
715 req,
716 "(ncName=%s)",
717 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
719 netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
721 el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
722 if (!el) {
723 talloc_free(tmp_ctx);
724 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
725 "Error finding element for servicePrincipalName.");
728 /* NTDSDSA objectGuid of object we are checking SPN for */
729 if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
730 ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
731 req->op.mod.message->dn, &ntds, req);
732 if (ret != LDB_SUCCESS) {
733 ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
734 ldb_dn_get_linearized(req->op.mod.message->dn),
735 ldb_strerror(ret));
736 talloc_free(tmp_ctx);
737 return LDB_ERR_OPERATIONS_ERROR;
739 ntds_guid = GUID_string(tmp_ctx, &ntds);
742 for (i=0; i < el->num_values; i++) {
743 ret = acl_validate_spn_value(tmp_ctx,
744 ldb,
745 (char *)el->values[i].data,
746 userAccountControl,
747 samAccountName,
748 dnsHostName,
749 netbios_name,
750 ntds_guid);
751 if (ret != LDB_SUCCESS) {
752 talloc_free(tmp_ctx);
753 return ret;
756 talloc_free(tmp_ctx);
757 return LDB_SUCCESS;
760 static int acl_add(struct ldb_module *module, struct ldb_request *req)
762 int ret;
763 struct ldb_dn *parent;
764 struct ldb_context *ldb;
765 const struct dsdb_schema *schema;
766 const struct dsdb_class *objectclass;
767 struct ldb_dn *nc_root;
768 struct ldb_control *as_system;
770 if (ldb_dn_is_special(req->op.add.message->dn)) {
771 return ldb_next_request(module, req);
774 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
775 if (as_system != NULL) {
776 as_system->critical = 0;
779 if (dsdb_module_am_system(module) || as_system) {
780 return ldb_next_request(module, req);
783 ldb = ldb_module_get_ctx(module);
785 parent = ldb_dn_get_parent(req, req->op.add.message->dn);
786 if (parent == NULL) {
787 return ldb_oom(ldb);
790 /* Creating an NC. There is probably something we should do here,
791 * but we will establish that later */
793 ret = dsdb_find_nc_root(ldb, req, req->op.add.message->dn, &nc_root);
794 if (ret != LDB_SUCCESS) {
795 return ret;
797 if (ldb_dn_compare(nc_root, req->op.add.message->dn) == 0) {
798 talloc_free(nc_root);
799 return ldb_next_request(module, req);
801 talloc_free(nc_root);
803 schema = dsdb_get_schema(ldb, req);
804 if (!schema) {
805 return ldb_operr(ldb);
808 objectclass = dsdb_get_structural_oc_from_msg(schema, req->op.add.message);
809 if (!objectclass) {
810 ldb_asprintf_errstring(ldb_module_get_ctx(module),
811 "acl: unable to find or validate structrual objectClass on %s\n",
812 ldb_dn_get_linearized(req->op.add.message->dn));
813 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
816 ret = dsdb_module_check_access_on_dn(module, req, parent,
817 SEC_ADS_CREATE_CHILD,
818 &objectclass->schemaIDGUID, req);
819 if (ret != LDB_SUCCESS) {
820 return ret;
822 return ldb_next_request(module, req);
825 /* ckecks if modifications are allowed on "Member" attribute */
826 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
827 struct ldb_module *module,
828 struct ldb_request *req,
829 struct security_descriptor *sd,
830 struct dom_sid *sid,
831 const struct GUID *oc_guid,
832 const struct dsdb_attribute *attr)
834 int ret;
835 unsigned int i;
836 struct ldb_context *ldb = ldb_module_get_ctx(module);
837 struct ldb_dn *user_dn;
838 struct ldb_message_element *member_el;
839 /* if we have wp, we can do whatever we like */
840 if (acl_check_access_on_attribute(module,
841 mem_ctx,
843 sid,
844 SEC_ADS_WRITE_PROP,
845 attr) == LDB_SUCCESS) {
846 return LDB_SUCCESS;
848 /* if we are adding/deleting ourselves, check for self membership */
849 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
850 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
851 &user_dn);
852 if (ret != LDB_SUCCESS) {
853 return ret;
855 member_el = ldb_msg_find_element(req->op.mod.message, "member");
856 if (!member_el) {
857 return ldb_operr(ldb);
859 /* user can only remove oneself */
860 if (member_el->num_values == 0) {
861 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
863 for (i = 0; i < member_el->num_values; i++) {
864 if (strcasecmp((const char *)member_el->values[i].data,
865 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
866 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
869 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
870 GUID_DRS_SELF_MEMBERSHIP,
871 SEC_ADS_SELF_WRITE,
872 sid);
873 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
874 dsdb_acl_debug(sd, acl_user_token(module),
875 req->op.mod.message->dn,
876 true,
877 10);
879 return ret;
882 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
883 struct ldb_module *module,
884 struct ldb_request *req,
885 struct security_descriptor *sd,
886 struct dom_sid *sid,
887 const struct GUID *oc_guid,
888 bool userPassword)
890 int ret = LDB_SUCCESS;
891 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
892 struct ldb_message_element *el;
893 struct ldb_message *msg;
894 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
895 "unicodePwd", "dBCSPwd", NULL }, **l;
896 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
898 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
899 if (msg == NULL) {
900 return ldb_module_oom(module);
902 for (l = passwordAttrs; *l != NULL; l++) {
903 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
904 continue;
907 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
908 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
909 ++del_attr_cnt;
911 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
912 ++add_attr_cnt;
914 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
915 ++rep_attr_cnt;
917 ldb_msg_remove_element(msg, el);
921 /* single deletes will be handled by the "password_hash" LDB module
922 * later in the stack, so we let it though here */
923 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
924 talloc_free(tmp_ctx);
925 return LDB_SUCCESS;
928 if (ldb_request_get_control(req,
929 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
930 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
931 * have a user password change and not a set as the message
932 * looks like. In it's value blob it contains the NT and/or LM
933 * hash of the old password specified by the user.
934 * This control is used by the SAMR and "kpasswd" password
935 * change mechanisms. */
936 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
937 GUID_DRS_USER_CHANGE_PASSWORD,
938 SEC_ADS_CONTROL_ACCESS,
939 sid);
941 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
942 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
943 GUID_DRS_FORCE_CHANGE_PASSWORD,
944 SEC_ADS_CONTROL_ACCESS,
945 sid);
947 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
948 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
949 GUID_DRS_USER_CHANGE_PASSWORD,
950 SEC_ADS_CONTROL_ACCESS,
951 sid);
952 /* Very strange, but we get constraint violation in this case */
953 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
954 ret = LDB_ERR_CONSTRAINT_VIOLATION;
957 if (ret != LDB_SUCCESS) {
958 dsdb_acl_debug(sd, acl_user_token(module),
959 req->op.mod.message->dn,
960 true,
961 10);
963 talloc_free(tmp_ctx);
964 return ret;
967 static const struct GUID *get_oc_guid_from_message(const struct dsdb_schema *schema,
968 struct ldb_message *msg)
970 struct ldb_message_element *oc_el;
971 const struct dsdb_class *object_class;
973 oc_el = ldb_msg_find_element(msg, "objectClass");
974 if (!oc_el) {
975 return NULL;
978 object_class = dsdb_get_last_structural_class(schema, oc_el);
979 if (object_class == NULL) {
980 return NULL;
983 return &object_class->schemaIDGUID;
987 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
989 int ret;
990 struct ldb_context *ldb = ldb_module_get_ctx(module);
991 const struct dsdb_schema *schema;
992 unsigned int i;
993 const struct dsdb_class *objectclass;
994 uint32_t access_granted;
995 NTSTATUS status;
996 struct ldb_result *acl_res;
997 struct security_descriptor *sd;
998 struct dom_sid *sid = NULL;
999 struct ldb_control *as_system;
1000 bool userPassword;
1001 TALLOC_CTX *tmp_ctx;
1002 const struct ldb_message *msg = req->op.mod.message;
1003 static const char *acl_attrs[] = {
1004 "nTSecurityDescriptor",
1005 "objectClass",
1006 "objectSid",
1007 NULL
1010 if (ldb_dn_is_special(msg->dn)) {
1011 return ldb_next_request(module, req);
1014 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1015 if (as_system != NULL) {
1016 as_system->critical = 0;
1019 /* Don't print this debug statement if elements[0].name is going to be NULL */
1020 if (msg->num_elements > 0) {
1021 DEBUG(10, ("ldb:acl_modify: %s\n", msg->elements[0].name));
1023 if (dsdb_module_am_system(module) || as_system) {
1024 return ldb_next_request(module, req);
1027 tmp_ctx = talloc_new(req);
1028 if (tmp_ctx == NULL) {
1029 return ldb_oom(ldb);
1032 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, msg->dn,
1033 acl_attrs,
1034 DSDB_FLAG_NEXT_MODULE |
1035 DSDB_FLAG_AS_SYSTEM |
1036 DSDB_SEARCH_SHOW_RECYCLED,
1037 req);
1039 if (ret != LDB_SUCCESS) {
1040 goto fail;
1043 userPassword = dsdb_user_password_support(module, req, req);
1045 schema = dsdb_get_schema(ldb, tmp_ctx);
1046 if (!schema) {
1047 talloc_free(tmp_ctx);
1048 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1049 "acl_modify: Error obtaining schema.");
1052 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
1053 if (ret != LDB_SUCCESS) {
1054 talloc_free(tmp_ctx);
1055 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1056 "acl_modify: Error retrieving security descriptor.");
1058 /* Theoretically we pass the check if the object has no sd */
1059 if (!sd) {
1060 goto success;
1063 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1064 if (!objectclass) {
1065 talloc_free(tmp_ctx);
1066 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1067 "acl_modify: Error retrieving object class for GUID.");
1069 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1070 for (i=0; i < msg->num_elements; i++) {
1071 const struct ldb_message_element *el = &msg->elements[i];
1072 const struct dsdb_attribute *attr;
1075 * This basic attribute existence check with the right errorcode
1076 * is needed since this module is the first one which requests
1077 * schema attribute information.
1078 * The complete attribute checking is done in the
1079 * "objectclass_attrs" module behind this one.
1081 * NOTE: "clearTextPassword" is not defined in the schema.
1083 attr = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
1084 if (!attr && ldb_attr_cmp("clearTextPassword", el->name) != 0) {
1085 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' "
1086 "on entry '%s' was not found in the schema!",
1087 req->op.mod.message->elements[i].name,
1088 ldb_dn_get_linearized(req->op.mod.message->dn));
1089 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1090 goto fail;
1093 if (ldb_attr_cmp("nTSecurityDescriptor", el->name) == 0) {
1094 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
1095 uint32_t access_mask = 0;
1097 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
1098 access_mask |= SEC_STD_WRITE_OWNER;
1100 if (sd_flags & SECINFO_DACL) {
1101 access_mask |= SEC_STD_WRITE_DAC;
1103 if (sd_flags & SECINFO_SACL) {
1104 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
1107 status = sec_access_check_ds(sd, acl_user_token(module),
1108 access_mask,
1109 &access_granted,
1110 NULL,
1111 sid);
1113 if (!NT_STATUS_IS_OK(status)) {
1114 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1115 "Object %s has no write dacl access\n",
1116 ldb_dn_get_linearized(msg->dn));
1117 dsdb_acl_debug(sd,
1118 acl_user_token(module),
1119 msg->dn,
1120 true,
1121 10);
1122 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1123 goto fail;
1125 } else if (ldb_attr_cmp("member", el->name) == 0) {
1126 ret = acl_check_self_membership(tmp_ctx,
1127 module,
1128 req,
1130 sid,
1131 &objectclass->schemaIDGUID,
1132 attr);
1133 if (ret != LDB_SUCCESS) {
1134 goto fail;
1136 } else if (ldb_attr_cmp("dBCSPwd", el->name) == 0) {
1137 /* this one is not affected by any rights, we should let it through
1138 so that passwords_hash returns the correct error */
1139 continue;
1140 } else if (ldb_attr_cmp("unicodePwd", el->name) == 0 ||
1141 (userPassword && ldb_attr_cmp("userPassword", el->name) == 0) ||
1142 ldb_attr_cmp("clearTextPassword", el->name) == 0) {
1143 ret = acl_check_password_rights(tmp_ctx,
1144 module,
1145 req,
1147 sid,
1148 &objectclass->schemaIDGUID,
1149 userPassword);
1150 if (ret != LDB_SUCCESS) {
1151 goto fail;
1153 } else if (ldb_attr_cmp("servicePrincipalName", el->name) == 0) {
1154 ret = acl_check_spn(tmp_ctx,
1155 module,
1156 req,
1158 sid,
1159 &objectclass->schemaIDGUID,
1160 attr);
1161 if (ret != LDB_SUCCESS) {
1162 goto fail;
1164 } else {
1165 struct object_tree *root = NULL;
1166 struct object_tree *new_node = NULL;
1168 if (!insert_in_object_tree(tmp_ctx,
1169 &objectclass->schemaIDGUID,
1170 SEC_ADS_WRITE_PROP,
1171 &root, &new_node)) {
1172 talloc_free(tmp_ctx);
1173 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1174 "acl_modify: Error adding new node in object tree.");
1177 if (!insert_in_object_tree(tmp_ctx,
1178 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
1179 &new_node, &new_node)) {
1180 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1181 "acl_modify: cannot add to object tree securityGUID\n");
1182 ret = LDB_ERR_OPERATIONS_ERROR;
1183 goto fail;
1186 if (!insert_in_object_tree(tmp_ctx,
1187 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
1188 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1189 "acl_modify: cannot add to object tree attributeGUID\n");
1190 ret = LDB_ERR_OPERATIONS_ERROR;
1191 goto fail;
1194 status = sec_access_check_ds(sd, acl_user_token(module),
1195 SEC_ADS_WRITE_PROP,
1196 &access_granted,
1197 root,
1198 sid);
1199 if (!NT_STATUS_IS_OK(status)) {
1200 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1201 "Object %s has no write property access\n",
1202 ldb_dn_get_linearized(msg->dn));
1203 dsdb_acl_debug(sd,
1204 acl_user_token(module),
1205 msg->dn,
1206 true,
1207 10);
1208 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1209 goto fail;
1214 success:
1215 talloc_free(tmp_ctx);
1216 return ldb_next_request(module, req);
1217 fail:
1218 talloc_free(tmp_ctx);
1219 return ret;
1222 /* similar to the modify for the time being.
1223 * We need to consider the special delete tree case, though - TODO */
1224 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1226 int ret;
1227 struct ldb_dn *parent;
1228 struct ldb_context *ldb;
1229 struct ldb_dn *nc_root;
1230 struct ldb_control *as_system;
1232 if (ldb_dn_is_special(req->op.del.dn)) {
1233 return ldb_next_request(module, req);
1236 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1237 if (as_system != NULL) {
1238 as_system->critical = 0;
1241 if (dsdb_module_am_system(module) || as_system) {
1242 return ldb_next_request(module, req);
1245 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1247 ldb = ldb_module_get_ctx(module);
1249 parent = ldb_dn_get_parent(req, req->op.del.dn);
1250 if (parent == NULL) {
1251 return ldb_oom(ldb);
1254 /* Make sure we aren't deleting a NC */
1256 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1257 if (ret != LDB_SUCCESS) {
1258 return ret;
1260 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1261 talloc_free(nc_root);
1262 DEBUG(10,("acl:deleting a NC\n"));
1263 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1264 return ldb_module_done(req, NULL, NULL,
1265 LDB_ERR_UNWILLING_TO_PERFORM);
1267 talloc_free(nc_root);
1269 if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID)) {
1270 ret = dsdb_module_check_access_on_dn(module, req,
1271 req->op.del.dn,
1272 SEC_ADS_DELETE_TREE, NULL,
1273 req);
1274 if (ret != LDB_SUCCESS) {
1275 return ret;
1278 return ldb_next_request(module, req);
1281 /* First check if we have delete object right */
1282 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn,
1283 SEC_STD_DELETE, NULL, req);
1284 if (ret == LDB_SUCCESS) {
1285 return ldb_next_request(module, req);
1288 /* Nope, we don't have delete object. Lets check if we have delete
1289 * child on the parent */
1290 ret = dsdb_module_check_access_on_dn(module, req, parent,
1291 SEC_ADS_DELETE_CHILD, NULL, req);
1292 if (ret != LDB_SUCCESS) {
1293 return ret;
1296 return ldb_next_request(module, req);
1299 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1301 int ret;
1302 struct ldb_dn *oldparent;
1303 struct ldb_dn *newparent;
1304 const struct dsdb_schema *schema;
1305 const struct dsdb_class *objectclass;
1306 struct ldb_context *ldb;
1307 struct security_descriptor *sd = NULL;
1308 struct dom_sid *sid = NULL;
1309 struct ldb_result *acl_res;
1310 const struct GUID *guid;
1311 struct ldb_dn *nc_root;
1312 struct object_tree *root = NULL;
1313 struct object_tree *new_node = NULL;
1314 struct ldb_control *as_system;
1315 TALLOC_CTX *tmp_ctx;
1316 NTSTATUS status;
1317 uint32_t access_granted;
1318 const char *rdn_name;
1319 static const char *acl_attrs[] = {
1320 "nTSecurityDescriptor",
1321 "objectClass",
1322 "objectSid",
1323 NULL
1326 if (ldb_dn_is_special(req->op.rename.olddn)) {
1327 return ldb_next_request(module, req);
1330 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1331 if (as_system != NULL) {
1332 as_system->critical = 0;
1335 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1336 if (dsdb_module_am_system(module) || as_system) {
1337 return ldb_next_request(module, req);
1340 ldb = ldb_module_get_ctx(module);
1342 tmp_ctx = talloc_new(req);
1343 if (tmp_ctx == NULL) {
1344 return ldb_oom(ldb);
1347 oldparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.olddn);
1348 if (oldparent == NULL) {
1349 return ldb_oom(ldb);
1351 newparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.newdn);
1352 if (newparent == NULL) {
1353 return ldb_oom(ldb);
1356 /* Make sure we aren't renaming/moving a NC */
1358 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1359 if (ret != LDB_SUCCESS) {
1360 return ret;
1362 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1363 talloc_free(nc_root);
1364 DEBUG(10,("acl:renaming/moving a NC\n"));
1365 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1366 return ldb_module_done(req, NULL, NULL,
1367 LDB_ERR_UNWILLING_TO_PERFORM);
1369 talloc_free(nc_root);
1371 /* Look for the parent */
1373 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1374 req->op.rename.olddn, acl_attrs,
1375 DSDB_FLAG_NEXT_MODULE |
1376 DSDB_FLAG_AS_SYSTEM |
1377 DSDB_SEARCH_SHOW_RECYCLED, req);
1378 /* we sould be able to find the parent */
1379 if (ret != LDB_SUCCESS) {
1380 DEBUG(10,("acl: failed to find object %s\n",
1381 ldb_dn_get_linearized(req->op.rename.olddn)));
1382 talloc_free(tmp_ctx);
1383 return ret;
1386 schema = dsdb_get_schema(ldb, acl_res);
1387 if (!schema) {
1388 talloc_free(tmp_ctx);
1389 return ldb_operr(ldb);
1392 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1393 if (!objectclass) {
1394 talloc_free(tmp_ctx);
1395 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1396 "acl_modify: Error retrieving object class for GUID.");
1399 if (!insert_in_object_tree(tmp_ctx, &objectclass->schemaIDGUID, SEC_ADS_WRITE_PROP,
1400 &root, &new_node)) {
1401 talloc_free(tmp_ctx);
1402 return ldb_operr(ldb);
1405 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1406 "name");
1407 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1408 &new_node, &new_node)) {
1409 talloc_free(tmp_ctx);
1410 return ldb_operr(ldb);
1413 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1414 if (rdn_name == NULL) {
1415 talloc_free(tmp_ctx);
1416 return ldb_operr(ldb);
1418 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1419 rdn_name);
1420 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1421 &new_node, &new_node)) {
1422 talloc_free(tmp_ctx);
1423 return ldb_operr(ldb);
1426 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1428 if (ret != LDB_SUCCESS) {
1429 talloc_free(tmp_ctx);
1430 return ldb_operr(ldb);
1432 /* Theoretically we pass the check if the object has no sd */
1433 if (!sd) {
1434 talloc_free(tmp_ctx);
1435 return LDB_SUCCESS;
1437 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1438 status = sec_access_check_ds(sd, acl_user_token(module),
1439 SEC_ADS_WRITE_PROP,
1440 &access_granted,
1441 root,
1442 sid);
1444 if (!NT_STATUS_IS_OK(status)) {
1445 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1446 "Object %s has no wp on name\n",
1447 ldb_dn_get_linearized(req->op.rename.olddn));
1448 dsdb_acl_debug(sd,
1449 acl_user_token(module),
1450 req->op.rename.olddn,
1451 true,
1452 10);
1453 talloc_free(tmp_ctx);
1454 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1457 if (ldb_dn_compare(oldparent, newparent) == 0) {
1458 /* regular rename, not move, nothing more to do */
1459 talloc_free(tmp_ctx);
1460 return ldb_next_request(module, req);
1463 /* new parent should have create child */
1464 root = NULL;
1465 new_node = NULL;
1467 ret = dsdb_module_check_access_on_dn(module, req, newparent,
1468 SEC_ADS_CREATE_CHILD,
1469 &objectclass->schemaIDGUID, req);
1470 if (ret != LDB_SUCCESS) {
1471 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1472 "acl:access_denied renaming %s",
1473 ldb_dn_get_linearized(req->op.rename.olddn));
1474 talloc_free(tmp_ctx);
1475 return ret;
1477 /* do we have delete object on the object? */
1479 status = sec_access_check_ds(sd, acl_user_token(module),
1480 SEC_STD_DELETE,
1481 &access_granted,
1482 NULL,
1483 sid);
1485 if (NT_STATUS_IS_OK(status)) {
1486 talloc_free(tmp_ctx);
1487 return ldb_next_request(module, req);
1489 /* what about delete child on the current parent */
1490 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL, req);
1491 if (ret != LDB_SUCCESS) {
1492 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1493 "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1494 talloc_free(tmp_ctx);
1495 return ldb_module_done(req, NULL, NULL, ret);
1498 talloc_free(tmp_ctx);
1500 return ldb_next_request(module, req);
1503 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1504 struct acl_private *data)
1506 struct dsdb_attribute *a;
1507 uint32_t n = 0;
1509 if (data->acl_search) {
1511 * If acl:search is activated, the acl_read module
1512 * protects confidential attributes.
1514 return LDB_SUCCESS;
1517 if ((ac->schema == data->cached_schema_ptr) &&
1518 (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
1519 (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1521 return LDB_SUCCESS;
1524 data->cached_schema_ptr = NULL;
1525 data->cached_schema_loaded_usn = 0;
1526 data->cached_schema_metadata_usn = 0;
1527 TALLOC_FREE(data->confidential_attrs);
1529 if (ac->schema == NULL) {
1530 return LDB_SUCCESS;
1533 for (a = ac->schema->attributes; a; a = a->next) {
1534 const char **attrs = data->confidential_attrs;
1536 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1537 continue;
1540 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1541 if (attrs == NULL) {
1542 TALLOC_FREE(data->confidential_attrs);
1543 return ldb_module_oom(ac->module);
1546 attrs[n] = a->lDAPDisplayName;
1547 attrs[n+1] = NULL;
1548 n++;
1550 data->confidential_attrs = attrs;
1553 data->cached_schema_ptr = ac->schema;
1554 data->cached_schema_loaded_usn = ac->schema->loaded_usn;
1555 data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1557 return LDB_SUCCESS;
1560 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1562 struct acl_context *ac;
1563 struct acl_private *data;
1564 struct ldb_result *acl_res;
1565 static const char *acl_attrs[] = {
1566 "objectClass",
1567 "nTSecurityDescriptor",
1568 "objectSid",
1569 NULL
1571 int ret;
1572 unsigned int i;
1574 ac = talloc_get_type(req->context, struct acl_context);
1575 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1576 if (!ares) {
1577 return ldb_module_done(ac->req, NULL, NULL,
1578 LDB_ERR_OPERATIONS_ERROR);
1580 if (ares->error != LDB_SUCCESS) {
1581 return ldb_module_done(ac->req, ares->controls,
1582 ares->response, ares->error);
1585 switch (ares->type) {
1586 case LDB_REPLY_ENTRY:
1587 if (ac->constructed_attrs) {
1588 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1589 acl_attrs,
1590 DSDB_FLAG_NEXT_MODULE |
1591 DSDB_FLAG_AS_SYSTEM |
1592 DSDB_SEARCH_SHOW_RECYCLED,
1593 req);
1594 if (ret != LDB_SUCCESS) {
1595 return ldb_module_done(ac->req, NULL, NULL, ret);
1599 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1600 ret = acl_allowedAttributes(ac->module, ac->schema,
1601 acl_res->msgs[0],
1602 ares->message, ac);
1603 if (ret != LDB_SUCCESS) {
1604 return ldb_module_done(ac->req, NULL, NULL, ret);
1608 if (ac->allowedChildClasses) {
1609 ret = acl_childClasses(ac->module, ac->schema,
1610 acl_res->msgs[0],
1611 ares->message,
1612 "allowedChildClasses");
1613 if (ret != LDB_SUCCESS) {
1614 return ldb_module_done(ac->req, NULL, NULL, ret);
1618 if (ac->allowedChildClassesEffective) {
1619 ret = acl_childClassesEffective(ac->module, ac->schema,
1620 acl_res->msgs[0],
1621 ares->message, ac);
1622 if (ret != LDB_SUCCESS) {
1623 return ldb_module_done(ac->req, NULL, NULL, ret);
1627 if (ac->sDRightsEffective) {
1628 ret = acl_sDRightsEffective(ac->module,
1629 acl_res->msgs[0],
1630 ares->message, ac);
1631 if (ret != LDB_SUCCESS) {
1632 return ldb_module_done(ac->req, NULL, NULL, ret);
1636 if (data == NULL) {
1637 return ldb_module_send_entry(ac->req, ares->message,
1638 ares->controls);
1641 if (ac->am_system) {
1642 return ldb_module_send_entry(ac->req, ares->message,
1643 ares->controls);
1646 if (data->password_attrs != NULL) {
1647 for (i = 0; data->password_attrs[i]; i++) {
1648 if ((!ac->userPassword) &&
1649 (ldb_attr_cmp(data->password_attrs[i],
1650 "userPassword") == 0))
1652 continue;
1655 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1659 if (ac->am_administrator) {
1660 return ldb_module_send_entry(ac->req, ares->message,
1661 ares->controls);
1664 ret = acl_search_update_confidential_attrs(ac, data);
1665 if (ret != LDB_SUCCESS) {
1666 return ret;
1669 if (data->confidential_attrs != NULL) {
1670 for (i = 0; data->confidential_attrs[i]; i++) {
1671 ldb_msg_remove_attr(ares->message,
1672 data->confidential_attrs[i]);
1676 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1678 case LDB_REPLY_REFERRAL:
1679 return ldb_module_send_referral(ac->req, ares->referral);
1681 case LDB_REPLY_DONE:
1682 return ldb_module_done(ac->req, ares->controls,
1683 ares->response, LDB_SUCCESS);
1686 return LDB_SUCCESS;
1689 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1691 struct ldb_context *ldb;
1692 struct acl_context *ac;
1693 struct ldb_parse_tree *down_tree;
1694 struct ldb_request *down_req;
1695 struct acl_private *data;
1696 int ret;
1697 unsigned int i;
1699 if (ldb_dn_is_special(req->op.search.base)) {
1700 return ldb_next_request(module, req);
1703 ldb = ldb_module_get_ctx(module);
1705 ac = talloc_zero(req, struct acl_context);
1706 if (ac == NULL) {
1707 return ldb_oom(ldb);
1709 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1711 ac->module = module;
1712 ac->req = req;
1713 ac->am_system = dsdb_module_am_system(module);
1714 ac->am_administrator = dsdb_module_am_administrator(module);
1715 ac->constructed_attrs = false;
1716 ac->modify_search = true;
1717 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1718 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1719 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1720 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1721 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1722 ac->userPassword = true;
1723 ac->schema = dsdb_get_schema(ldb, ac);
1725 ac->constructed_attrs |= ac->allowedAttributes;
1726 ac->constructed_attrs |= ac->allowedChildClasses;
1727 ac->constructed_attrs |= ac->allowedChildClassesEffective;
1728 ac->constructed_attrs |= ac->allowedAttributesEffective;
1729 ac->constructed_attrs |= ac->sDRightsEffective;
1731 if (data == NULL) {
1732 ac->modify_search = false;
1734 if (ac->am_system) {
1735 ac->modify_search = false;
1738 if (!ac->constructed_attrs && !ac->modify_search) {
1739 talloc_free(ac);
1740 return ldb_next_request(module, req);
1743 if (!ac->am_system) {
1744 ac->userPassword = dsdb_user_password_support(module, ac, req);
1747 ret = acl_search_update_confidential_attrs(ac, data);
1748 if (ret != LDB_SUCCESS) {
1749 return ret;
1752 down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1753 if (down_tree == NULL) {
1754 return ldb_oom(ldb);
1757 if (!ac->am_system && data->password_attrs) {
1758 for (i = 0; data->password_attrs[i]; i++) {
1759 if ((!ac->userPassword) &&
1760 (ldb_attr_cmp(data->password_attrs[i],
1761 "userPassword") == 0))
1763 continue;
1766 ldb_parse_tree_attr_replace(down_tree,
1767 data->password_attrs[i],
1768 "kludgeACLredactedattribute");
1772 if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1773 for (i = 0; data->confidential_attrs[i]; i++) {
1774 ldb_parse_tree_attr_replace(down_tree,
1775 data->confidential_attrs[i],
1776 "kludgeACLredactedattribute");
1780 ret = ldb_build_search_req_ex(&down_req,
1781 ldb, ac,
1782 req->op.search.base,
1783 req->op.search.scope,
1784 down_tree,
1785 req->op.search.attrs,
1786 req->controls,
1787 ac, acl_search_callback,
1788 req);
1789 LDB_REQ_SET_LOCATION(down_req);
1790 if (ret != LDB_SUCCESS) {
1791 return ret;
1793 /* perform the search */
1794 return ldb_next_request(module, down_req);
1797 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1799 struct ldb_context *ldb = ldb_module_get_ctx(module);
1800 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1802 /* allow everybody to read the sequence number */
1803 if (strcmp(req->op.extended.oid,
1804 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1805 return ldb_next_request(module, req);
1808 if (dsdb_module_am_system(module) ||
1809 dsdb_module_am_administrator(module) || as_system) {
1810 return ldb_next_request(module, req);
1811 } else {
1812 ldb_asprintf_errstring(ldb,
1813 "acl_extended: "
1814 "attempted database modify not permitted. "
1815 "User %s is not SYSTEM or an administrator",
1816 acl_user_name(req, module));
1817 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1821 static const struct ldb_module_ops ldb_acl_module_ops = {
1822 .name = "acl",
1823 .search = acl_search,
1824 .add = acl_add,
1825 .modify = acl_modify,
1826 .del = acl_delete,
1827 .rename = acl_rename,
1828 .extended = acl_extended,
1829 .init_context = acl_module_init
1832 int ldb_acl_module_init(const char *version)
1834 LDB_MODULE_CHECK_VERSION(version);
1835 return ldb_register_module(&ldb_acl_module_ops);