dsdb-acl: introduce a 'el' helper variable to acl_modify()
[Samba/gebeck_regimport.git] / source4 / dsdb / samdb / ldb_modules / acl.c
blobec21db35b68247115cbbd2e554eef85578bf3d3c
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 struct ldb_message_element *oc_el;
767 const struct GUID *guid;
768 struct ldb_dn *nc_root;
769 struct ldb_control *as_system;
771 if (ldb_dn_is_special(req->op.add.message->dn)) {
772 return ldb_next_request(module, req);
775 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
776 if (as_system != NULL) {
777 as_system->critical = 0;
780 if (dsdb_module_am_system(module) || as_system) {
781 return ldb_next_request(module, req);
784 ldb = ldb_module_get_ctx(module);
786 parent = ldb_dn_get_parent(req, req->op.add.message->dn);
787 if (parent == NULL) {
788 return ldb_oom(ldb);
791 /* Creating an NC. There is probably something we should do here,
792 * but we will establish that later */
794 ret = dsdb_find_nc_root(ldb, req, req->op.add.message->dn, &nc_root);
795 if (ret != LDB_SUCCESS) {
796 return ret;
798 if (ldb_dn_compare(nc_root, req->op.add.message->dn) == 0) {
799 talloc_free(nc_root);
800 return ldb_next_request(module, req);
802 talloc_free(nc_root);
804 schema = dsdb_get_schema(ldb, req);
805 if (!schema) {
806 return ldb_operr(ldb);
809 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
810 if (!oc_el || oc_el->num_values == 0) {
811 ldb_asprintf_errstring(ldb_module_get_ctx(module),
812 "acl: unable to find objectClass on %s\n",
813 ldb_dn_get_linearized(req->op.add.message->dn));
814 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
817 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
818 (char *)oc_el->values[oc_el->num_values-1].data);
819 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid, req);
820 if (ret != LDB_SUCCESS) {
821 return ret;
823 return ldb_next_request(module, req);
826 /* ckecks if modifications are allowed on "Member" attribute */
827 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
828 struct ldb_module *module,
829 struct ldb_request *req,
830 struct security_descriptor *sd,
831 struct dom_sid *sid,
832 const struct GUID *oc_guid,
833 const struct dsdb_attribute *attr)
835 int ret;
836 unsigned int i;
837 struct ldb_context *ldb = ldb_module_get_ctx(module);
838 struct ldb_dn *user_dn;
839 struct ldb_message_element *member_el;
840 /* if we have wp, we can do whatever we like */
841 if (acl_check_access_on_attribute(module,
842 mem_ctx,
844 sid,
845 SEC_ADS_WRITE_PROP,
846 attr) == LDB_SUCCESS) {
847 return LDB_SUCCESS;
849 /* if we are adding/deleting ourselves, check for self membership */
850 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
851 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
852 &user_dn);
853 if (ret != LDB_SUCCESS) {
854 return ret;
856 member_el = ldb_msg_find_element(req->op.mod.message, "member");
857 if (!member_el) {
858 return ldb_operr(ldb);
860 /* user can only remove oneself */
861 if (member_el->num_values == 0) {
862 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
864 for (i = 0; i < member_el->num_values; i++) {
865 if (strcasecmp((const char *)member_el->values[i].data,
866 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
867 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
870 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
871 GUID_DRS_SELF_MEMBERSHIP,
872 SEC_ADS_SELF_WRITE,
873 sid);
874 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
875 dsdb_acl_debug(sd, acl_user_token(module),
876 req->op.mod.message->dn,
877 true,
878 10);
880 return ret;
883 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
884 struct ldb_module *module,
885 struct ldb_request *req,
886 struct security_descriptor *sd,
887 struct dom_sid *sid,
888 const struct GUID *oc_guid,
889 bool userPassword)
891 int ret = LDB_SUCCESS;
892 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
893 struct ldb_message_element *el;
894 struct ldb_message *msg;
895 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
896 "unicodePwd", "dBCSPwd", NULL }, **l;
897 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
899 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
900 if (msg == NULL) {
901 return ldb_module_oom(module);
903 for (l = passwordAttrs; *l != NULL; l++) {
904 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
905 continue;
908 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
909 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
910 ++del_attr_cnt;
912 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
913 ++add_attr_cnt;
915 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
916 ++rep_attr_cnt;
918 ldb_msg_remove_element(msg, el);
922 /* single deletes will be handled by the "password_hash" LDB module
923 * later in the stack, so we let it though here */
924 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
925 talloc_free(tmp_ctx);
926 return LDB_SUCCESS;
929 if (ldb_request_get_control(req,
930 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
931 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
932 * have a user password change and not a set as the message
933 * looks like. In it's value blob it contains the NT and/or LM
934 * hash of the old password specified by the user.
935 * This control is used by the SAMR and "kpasswd" password
936 * change mechanisms. */
937 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
938 GUID_DRS_USER_CHANGE_PASSWORD,
939 SEC_ADS_CONTROL_ACCESS,
940 sid);
942 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
943 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
944 GUID_DRS_FORCE_CHANGE_PASSWORD,
945 SEC_ADS_CONTROL_ACCESS,
946 sid);
948 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
949 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
950 GUID_DRS_USER_CHANGE_PASSWORD,
951 SEC_ADS_CONTROL_ACCESS,
952 sid);
953 /* Very strange, but we get constraint violation in this case */
954 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
955 ret = LDB_ERR_CONSTRAINT_VIOLATION;
958 if (ret != LDB_SUCCESS) {
959 dsdb_acl_debug(sd, acl_user_token(module),
960 req->op.mod.message->dn,
961 true,
962 10);
964 talloc_free(tmp_ctx);
965 return ret;
968 static const struct GUID *get_oc_guid_from_message(const struct dsdb_schema *schema,
969 struct ldb_message *msg)
971 struct ldb_message_element *oc_el;
972 const struct dsdb_class *object_class;
974 oc_el = ldb_msg_find_element(msg, "objectClass");
975 if (!oc_el) {
976 return NULL;
979 object_class = dsdb_get_last_structural_class(schema, oc_el);
980 if (object_class == NULL) {
981 return NULL;
984 return &object_class->schemaIDGUID;
988 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
990 int ret;
991 struct ldb_context *ldb = ldb_module_get_ctx(module);
992 const struct dsdb_schema *schema;
993 unsigned int i;
994 const struct GUID *guid;
995 uint32_t access_granted;
996 NTSTATUS status;
997 struct ldb_result *acl_res;
998 struct security_descriptor *sd;
999 struct dom_sid *sid = NULL;
1000 struct ldb_control *as_system;
1001 bool userPassword;
1002 TALLOC_CTX *tmp_ctx;
1003 const struct ldb_message *msg = req->op.mod.message;
1004 static const char *acl_attrs[] = {
1005 "nTSecurityDescriptor",
1006 "objectClass",
1007 "objectSid",
1008 NULL
1011 if (ldb_dn_is_special(msg->dn)) {
1012 return ldb_next_request(module, req);
1015 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1016 if (as_system != NULL) {
1017 as_system->critical = 0;
1020 /* Don't print this debug statement if elements[0].name is going to be NULL */
1021 if (msg->num_elements > 0) {
1022 DEBUG(10, ("ldb:acl_modify: %s\n", msg->elements[0].name));
1024 if (dsdb_module_am_system(module) || as_system) {
1025 return ldb_next_request(module, req);
1028 tmp_ctx = talloc_new(req);
1029 if (tmp_ctx == NULL) {
1030 return ldb_oom(ldb);
1033 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, msg->dn,
1034 acl_attrs,
1035 DSDB_FLAG_NEXT_MODULE |
1036 DSDB_FLAG_AS_SYSTEM |
1037 DSDB_SEARCH_SHOW_RECYCLED,
1038 req);
1040 if (ret != LDB_SUCCESS) {
1041 goto fail;
1044 userPassword = dsdb_user_password_support(module, req, req);
1046 schema = dsdb_get_schema(ldb, tmp_ctx);
1047 if (!schema) {
1048 talloc_free(tmp_ctx);
1049 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1050 "acl_modify: Error obtaining schema.");
1053 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
1054 if (ret != LDB_SUCCESS) {
1055 talloc_free(tmp_ctx);
1056 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1057 "acl_modify: Error retrieving security descriptor.");
1059 /* Theoretically we pass the check if the object has no sd */
1060 if (!sd) {
1061 goto success;
1064 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1065 if (!guid) {
1066 talloc_free(tmp_ctx);
1067 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1068 "acl_modify: Error retrieving object class GUID.");
1070 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1071 for (i=0; i < msg->num_elements; i++) {
1072 const struct ldb_message_element *el = &msg->elements[i];
1073 const struct dsdb_attribute *attr;
1075 attr = dsdb_attribute_by_lDAPDisplayName(schema,
1076 el->name);
1078 if (ldb_attr_cmp("nTSecurityDescriptor", el->name) == 0) {
1079 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
1080 uint32_t access_mask = 0;
1082 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
1083 access_mask |= SEC_STD_WRITE_OWNER;
1085 if (sd_flags & SECINFO_DACL) {
1086 access_mask |= SEC_STD_WRITE_DAC;
1088 if (sd_flags & SECINFO_SACL) {
1089 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
1092 status = sec_access_check_ds(sd, acl_user_token(module),
1093 access_mask,
1094 &access_granted,
1095 NULL,
1096 sid);
1098 if (!NT_STATUS_IS_OK(status)) {
1099 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1100 "Object %s has no write dacl access\n",
1101 ldb_dn_get_linearized(msg->dn));
1102 dsdb_acl_debug(sd,
1103 acl_user_token(module),
1104 msg->dn,
1105 true,
1106 10);
1107 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1108 goto fail;
1110 } else if (ldb_attr_cmp("member", el->name) == 0) {
1111 ret = acl_check_self_membership(tmp_ctx,
1112 module,
1113 req,
1115 sid,
1116 guid,
1117 attr);
1118 if (ret != LDB_SUCCESS) {
1119 goto fail;
1121 } else if (ldb_attr_cmp("dBCSPwd", el->name) == 0) {
1122 /* this one is not affected by any rights, we should let it through
1123 so that passwords_hash returns the correct error */
1124 continue;
1125 } else if (ldb_attr_cmp("unicodePwd", el->name) == 0 ||
1126 (userPassword && ldb_attr_cmp("userPassword", el->name) == 0) ||
1127 ldb_attr_cmp("clearTextPassword", el->name) == 0) {
1128 ret = acl_check_password_rights(tmp_ctx,
1129 module,
1130 req,
1132 sid,
1133 guid,
1134 userPassword);
1135 if (ret != LDB_SUCCESS) {
1136 goto fail;
1138 } else if (ldb_attr_cmp("servicePrincipalName", el->name) == 0) {
1139 ret = acl_check_spn(tmp_ctx,
1140 module,
1141 req,
1143 sid,
1144 guid,
1145 attr);
1146 if (ret != LDB_SUCCESS) {
1147 goto fail;
1149 } else {
1150 struct object_tree *root = NULL;
1151 struct object_tree *new_node = NULL;
1153 /* This basic attribute existence check with the right errorcode
1154 * is needed since this module is the first one which requests
1155 * schema attribute information.
1156 * The complete attribute checking is done in the
1157 * "objectclass_attrs" module behind this one.
1159 if (!attr) {
1160 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
1161 el->name,
1162 ldb_dn_get_linearized(msg->dn));
1163 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1164 goto fail;
1167 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1168 &root, &new_node)) {
1169 talloc_free(tmp_ctx);
1170 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1171 "acl_modify: Error adding new node in object tree.");
1174 if (!insert_in_object_tree(tmp_ctx,
1175 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
1176 &new_node, &new_node)) {
1177 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1178 "acl_modify: cannot add to object tree securityGUID\n");
1179 ret = LDB_ERR_OPERATIONS_ERROR;
1180 goto fail;
1183 if (!insert_in_object_tree(tmp_ctx,
1184 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
1185 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1186 "acl_modify: cannot add to object tree attributeGUID\n");
1187 ret = LDB_ERR_OPERATIONS_ERROR;
1188 goto fail;
1191 status = sec_access_check_ds(sd, acl_user_token(module),
1192 SEC_ADS_WRITE_PROP,
1193 &access_granted,
1194 root,
1195 sid);
1196 if (!NT_STATUS_IS_OK(status)) {
1197 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1198 "Object %s has no write property access\n",
1199 ldb_dn_get_linearized(msg->dn));
1200 dsdb_acl_debug(sd,
1201 acl_user_token(module),
1202 msg->dn,
1203 true,
1204 10);
1205 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1206 goto fail;
1211 success:
1212 talloc_free(tmp_ctx);
1213 return ldb_next_request(module, req);
1214 fail:
1215 talloc_free(tmp_ctx);
1216 return ret;
1219 /* similar to the modify for the time being.
1220 * We need to consider the special delete tree case, though - TODO */
1221 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1223 int ret;
1224 struct ldb_dn *parent;
1225 struct ldb_context *ldb;
1226 struct ldb_dn *nc_root;
1227 struct ldb_control *as_system;
1229 if (ldb_dn_is_special(req->op.del.dn)) {
1230 return ldb_next_request(module, req);
1233 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1234 if (as_system != NULL) {
1235 as_system->critical = 0;
1238 if (dsdb_module_am_system(module) || as_system) {
1239 return ldb_next_request(module, req);
1242 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1244 ldb = ldb_module_get_ctx(module);
1246 parent = ldb_dn_get_parent(req, req->op.del.dn);
1247 if (parent == NULL) {
1248 return ldb_oom(ldb);
1251 /* Make sure we aren't deleting a NC */
1253 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1254 if (ret != LDB_SUCCESS) {
1255 return ret;
1257 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1258 talloc_free(nc_root);
1259 DEBUG(10,("acl:deleting a NC\n"));
1260 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1261 return ldb_module_done(req, NULL, NULL,
1262 LDB_ERR_UNWILLING_TO_PERFORM);
1264 talloc_free(nc_root);
1266 if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID)) {
1267 ret = dsdb_module_check_access_on_dn(module, req,
1268 req->op.del.dn,
1269 SEC_ADS_DELETE_TREE, NULL,
1270 req);
1271 if (ret != LDB_SUCCESS) {
1272 return ret;
1275 return ldb_next_request(module, req);
1278 /* First check if we have delete object right */
1279 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn,
1280 SEC_STD_DELETE, NULL, req);
1281 if (ret == LDB_SUCCESS) {
1282 return ldb_next_request(module, req);
1285 /* Nope, we don't have delete object. Lets check if we have delete
1286 * child on the parent */
1287 ret = dsdb_module_check_access_on_dn(module, req, parent,
1288 SEC_ADS_DELETE_CHILD, NULL, req);
1289 if (ret != LDB_SUCCESS) {
1290 return ret;
1293 return ldb_next_request(module, req);
1296 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1298 int ret;
1299 struct ldb_dn *oldparent;
1300 struct ldb_dn *newparent;
1301 const struct dsdb_schema *schema;
1302 struct ldb_context *ldb;
1303 struct security_descriptor *sd = NULL;
1304 struct dom_sid *sid = NULL;
1305 struct ldb_result *acl_res;
1306 const struct GUID *guid;
1307 struct ldb_dn *nc_root;
1308 struct object_tree *root = NULL;
1309 struct object_tree *new_node = NULL;
1310 struct ldb_control *as_system;
1311 TALLOC_CTX *tmp_ctx;
1312 NTSTATUS status;
1313 uint32_t access_granted;
1314 const char *rdn_name;
1315 static const char *acl_attrs[] = {
1316 "nTSecurityDescriptor",
1317 "objectClass",
1318 "objectSid",
1319 NULL
1322 if (ldb_dn_is_special(req->op.rename.olddn)) {
1323 return ldb_next_request(module, req);
1326 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1327 if (as_system != NULL) {
1328 as_system->critical = 0;
1331 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1332 if (dsdb_module_am_system(module) || as_system) {
1333 return ldb_next_request(module, req);
1336 ldb = ldb_module_get_ctx(module);
1338 tmp_ctx = talloc_new(req);
1339 if (tmp_ctx == NULL) {
1340 return ldb_oom(ldb);
1343 oldparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.olddn);
1344 if (oldparent == NULL) {
1345 return ldb_oom(ldb);
1347 newparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.newdn);
1348 if (newparent == NULL) {
1349 return ldb_oom(ldb);
1352 /* Make sure we aren't renaming/moving a NC */
1354 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1355 if (ret != LDB_SUCCESS) {
1356 return ret;
1358 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1359 talloc_free(nc_root);
1360 DEBUG(10,("acl:renaming/moving a NC\n"));
1361 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1362 return ldb_module_done(req, NULL, NULL,
1363 LDB_ERR_UNWILLING_TO_PERFORM);
1365 talloc_free(nc_root);
1367 /* Look for the parent */
1369 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1370 req->op.rename.olddn, acl_attrs,
1371 DSDB_FLAG_NEXT_MODULE |
1372 DSDB_FLAG_AS_SYSTEM |
1373 DSDB_SEARCH_SHOW_RECYCLED, req);
1374 /* we sould be able to find the parent */
1375 if (ret != LDB_SUCCESS) {
1376 DEBUG(10,("acl: failed to find object %s\n",
1377 ldb_dn_get_linearized(req->op.rename.olddn)));
1378 talloc_free(tmp_ctx);
1379 return ret;
1382 schema = dsdb_get_schema(ldb, acl_res);
1383 if (!schema) {
1384 talloc_free(tmp_ctx);
1385 return ldb_operr(ldb);
1388 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1389 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1390 &root, &new_node)) {
1391 talloc_free(tmp_ctx);
1392 return ldb_operr(ldb);
1395 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1396 "name");
1397 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1398 &new_node, &new_node)) {
1399 talloc_free(tmp_ctx);
1400 return ldb_operr(ldb);
1403 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1404 if (rdn_name == NULL) {
1405 talloc_free(tmp_ctx);
1406 return ldb_operr(ldb);
1408 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1409 rdn_name);
1410 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1411 &new_node, &new_node)) {
1412 talloc_free(tmp_ctx);
1413 return ldb_operr(ldb);
1416 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1418 if (ret != LDB_SUCCESS) {
1419 talloc_free(tmp_ctx);
1420 return ldb_operr(ldb);
1422 /* Theoretically we pass the check if the object has no sd */
1423 if (!sd) {
1424 talloc_free(tmp_ctx);
1425 return LDB_SUCCESS;
1427 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1428 status = sec_access_check_ds(sd, acl_user_token(module),
1429 SEC_ADS_WRITE_PROP,
1430 &access_granted,
1431 root,
1432 sid);
1434 if (!NT_STATUS_IS_OK(status)) {
1435 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1436 "Object %s has no wp on name\n",
1437 ldb_dn_get_linearized(req->op.rename.olddn));
1438 dsdb_acl_debug(sd,
1439 acl_user_token(module),
1440 req->op.rename.olddn,
1441 true,
1442 10);
1443 talloc_free(tmp_ctx);
1444 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1447 if (ldb_dn_compare(oldparent, newparent) == 0) {
1448 /* regular rename, not move, nothing more to do */
1449 talloc_free(tmp_ctx);
1450 return ldb_next_request(module, req);
1453 /* new parent should have create child */
1454 root = NULL;
1455 new_node = NULL;
1456 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1457 if (!guid) {
1458 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1459 "acl:renamed object has no object class\n");
1460 talloc_free(tmp_ctx);
1461 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1464 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid, req);
1465 if (ret != LDB_SUCCESS) {
1466 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1467 "acl:access_denied renaming %s",
1468 ldb_dn_get_linearized(req->op.rename.olddn));
1469 talloc_free(tmp_ctx);
1470 return ret;
1472 /* do we have delete object on the object? */
1474 status = sec_access_check_ds(sd, acl_user_token(module),
1475 SEC_STD_DELETE,
1476 &access_granted,
1477 NULL,
1478 sid);
1480 if (NT_STATUS_IS_OK(status)) {
1481 talloc_free(tmp_ctx);
1482 return ldb_next_request(module, req);
1484 /* what about delete child on the current parent */
1485 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL, req);
1486 if (ret != LDB_SUCCESS) {
1487 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1488 "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1489 talloc_free(tmp_ctx);
1490 return ldb_module_done(req, NULL, NULL, ret);
1493 talloc_free(tmp_ctx);
1495 return ldb_next_request(module, req);
1498 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1499 struct acl_private *data)
1501 struct dsdb_attribute *a;
1502 uint32_t n = 0;
1504 if (data->acl_search) {
1506 * If acl:search is activated, the acl_read module
1507 * protects confidential attributes.
1509 return LDB_SUCCESS;
1512 if ((ac->schema == data->cached_schema_ptr) &&
1513 (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
1514 (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1516 return LDB_SUCCESS;
1519 data->cached_schema_ptr = NULL;
1520 data->cached_schema_loaded_usn = 0;
1521 data->cached_schema_metadata_usn = 0;
1522 TALLOC_FREE(data->confidential_attrs);
1524 if (ac->schema == NULL) {
1525 return LDB_SUCCESS;
1528 for (a = ac->schema->attributes; a; a = a->next) {
1529 const char **attrs = data->confidential_attrs;
1531 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1532 continue;
1535 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1536 if (attrs == NULL) {
1537 TALLOC_FREE(data->confidential_attrs);
1538 return ldb_module_oom(ac->module);
1541 attrs[n] = a->lDAPDisplayName;
1542 attrs[n+1] = NULL;
1543 n++;
1545 data->confidential_attrs = attrs;
1548 data->cached_schema_ptr = ac->schema;
1549 data->cached_schema_loaded_usn = ac->schema->loaded_usn;
1550 data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1552 return LDB_SUCCESS;
1555 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1557 struct acl_context *ac;
1558 struct acl_private *data;
1559 struct ldb_result *acl_res;
1560 static const char *acl_attrs[] = {
1561 "objectClass",
1562 "nTSecurityDescriptor",
1563 "objectSid",
1564 NULL
1566 int ret;
1567 unsigned int i;
1569 ac = talloc_get_type(req->context, struct acl_context);
1570 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1571 if (!ares) {
1572 return ldb_module_done(ac->req, NULL, NULL,
1573 LDB_ERR_OPERATIONS_ERROR);
1575 if (ares->error != LDB_SUCCESS) {
1576 return ldb_module_done(ac->req, ares->controls,
1577 ares->response, ares->error);
1580 switch (ares->type) {
1581 case LDB_REPLY_ENTRY:
1582 if (ac->constructed_attrs) {
1583 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1584 acl_attrs,
1585 DSDB_FLAG_NEXT_MODULE |
1586 DSDB_FLAG_AS_SYSTEM |
1587 DSDB_SEARCH_SHOW_RECYCLED,
1588 req);
1589 if (ret != LDB_SUCCESS) {
1590 return ldb_module_done(ac->req, NULL, NULL, ret);
1594 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1595 ret = acl_allowedAttributes(ac->module, ac->schema,
1596 acl_res->msgs[0],
1597 ares->message, ac);
1598 if (ret != LDB_SUCCESS) {
1599 return ldb_module_done(ac->req, NULL, NULL, ret);
1603 if (ac->allowedChildClasses) {
1604 ret = acl_childClasses(ac->module, ac->schema,
1605 acl_res->msgs[0],
1606 ares->message,
1607 "allowedChildClasses");
1608 if (ret != LDB_SUCCESS) {
1609 return ldb_module_done(ac->req, NULL, NULL, ret);
1613 if (ac->allowedChildClassesEffective) {
1614 ret = acl_childClassesEffective(ac->module, ac->schema,
1615 acl_res->msgs[0],
1616 ares->message, ac);
1617 if (ret != LDB_SUCCESS) {
1618 return ldb_module_done(ac->req, NULL, NULL, ret);
1622 if (ac->sDRightsEffective) {
1623 ret = acl_sDRightsEffective(ac->module,
1624 acl_res->msgs[0],
1625 ares->message, ac);
1626 if (ret != LDB_SUCCESS) {
1627 return ldb_module_done(ac->req, NULL, NULL, ret);
1631 if (data == NULL) {
1632 return ldb_module_send_entry(ac->req, ares->message,
1633 ares->controls);
1636 if (ac->am_system) {
1637 return ldb_module_send_entry(ac->req, ares->message,
1638 ares->controls);
1641 if (data->password_attrs != NULL) {
1642 for (i = 0; data->password_attrs[i]; i++) {
1643 if ((!ac->userPassword) &&
1644 (ldb_attr_cmp(data->password_attrs[i],
1645 "userPassword") == 0))
1647 continue;
1650 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1654 if (ac->am_administrator) {
1655 return ldb_module_send_entry(ac->req, ares->message,
1656 ares->controls);
1659 ret = acl_search_update_confidential_attrs(ac, data);
1660 if (ret != LDB_SUCCESS) {
1661 return ret;
1664 if (data->confidential_attrs != NULL) {
1665 for (i = 0; data->confidential_attrs[i]; i++) {
1666 ldb_msg_remove_attr(ares->message,
1667 data->confidential_attrs[i]);
1671 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1673 case LDB_REPLY_REFERRAL:
1674 return ldb_module_send_referral(ac->req, ares->referral);
1676 case LDB_REPLY_DONE:
1677 return ldb_module_done(ac->req, ares->controls,
1678 ares->response, LDB_SUCCESS);
1681 return LDB_SUCCESS;
1684 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1686 struct ldb_context *ldb;
1687 struct acl_context *ac;
1688 struct ldb_parse_tree *down_tree;
1689 struct ldb_request *down_req;
1690 struct acl_private *data;
1691 int ret;
1692 unsigned int i;
1694 if (ldb_dn_is_special(req->op.search.base)) {
1695 return ldb_next_request(module, req);
1698 ldb = ldb_module_get_ctx(module);
1700 ac = talloc_zero(req, struct acl_context);
1701 if (ac == NULL) {
1702 return ldb_oom(ldb);
1704 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1706 ac->module = module;
1707 ac->req = req;
1708 ac->am_system = dsdb_module_am_system(module);
1709 ac->am_administrator = dsdb_module_am_administrator(module);
1710 ac->constructed_attrs = false;
1711 ac->modify_search = true;
1712 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1713 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1714 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1715 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1716 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1717 ac->userPassword = true;
1718 ac->schema = dsdb_get_schema(ldb, ac);
1720 ac->constructed_attrs |= ac->allowedAttributes;
1721 ac->constructed_attrs |= ac->allowedChildClasses;
1722 ac->constructed_attrs |= ac->allowedChildClassesEffective;
1723 ac->constructed_attrs |= ac->allowedAttributesEffective;
1724 ac->constructed_attrs |= ac->sDRightsEffective;
1726 if (data == NULL) {
1727 ac->modify_search = false;
1729 if (ac->am_system) {
1730 ac->modify_search = false;
1733 if (!ac->constructed_attrs && !ac->modify_search) {
1734 talloc_free(ac);
1735 return ldb_next_request(module, req);
1738 if (!ac->am_system) {
1739 ac->userPassword = dsdb_user_password_support(module, ac, req);
1742 ret = acl_search_update_confidential_attrs(ac, data);
1743 if (ret != LDB_SUCCESS) {
1744 return ret;
1747 down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1748 if (down_tree == NULL) {
1749 return ldb_oom(ldb);
1752 if (!ac->am_system && data->password_attrs) {
1753 for (i = 0; data->password_attrs[i]; i++) {
1754 if ((!ac->userPassword) &&
1755 (ldb_attr_cmp(data->password_attrs[i],
1756 "userPassword") == 0))
1758 continue;
1761 ldb_parse_tree_attr_replace(down_tree,
1762 data->password_attrs[i],
1763 "kludgeACLredactedattribute");
1767 if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1768 for (i = 0; data->confidential_attrs[i]; i++) {
1769 ldb_parse_tree_attr_replace(down_tree,
1770 data->confidential_attrs[i],
1771 "kludgeACLredactedattribute");
1775 ret = ldb_build_search_req_ex(&down_req,
1776 ldb, ac,
1777 req->op.search.base,
1778 req->op.search.scope,
1779 down_tree,
1780 req->op.search.attrs,
1781 req->controls,
1782 ac, acl_search_callback,
1783 req);
1784 LDB_REQ_SET_LOCATION(down_req);
1785 if (ret != LDB_SUCCESS) {
1786 return ret;
1788 /* perform the search */
1789 return ldb_next_request(module, down_req);
1792 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1794 struct ldb_context *ldb = ldb_module_get_ctx(module);
1795 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1797 /* allow everybody to read the sequence number */
1798 if (strcmp(req->op.extended.oid,
1799 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1800 return ldb_next_request(module, req);
1803 if (dsdb_module_am_system(module) ||
1804 dsdb_module_am_administrator(module) || as_system) {
1805 return ldb_next_request(module, req);
1806 } else {
1807 ldb_asprintf_errstring(ldb,
1808 "acl_extended: "
1809 "attempted database modify not permitted. "
1810 "User %s is not SYSTEM or an administrator",
1811 acl_user_name(req, module));
1812 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1816 static const struct ldb_module_ops ldb_acl_module_ops = {
1817 .name = "acl",
1818 .search = acl_search,
1819 .add = acl_add,
1820 .modify = acl_modify,
1821 .del = acl_delete,
1822 .rename = acl_rename,
1823 .extended = acl_extended,
1824 .init_context = acl_module_init
1827 int ldb_acl_module_init(const char *version)
1829 LDB_MODULE_CHECK_VERSION(version);
1830 return ldb_register_module(&ldb_acl_module_ops);