4 Copyright (C) Simo Sorce 2006-2008
5 Copyright (C) Nadezhda Ivanova 2009
6 Copyright (C) Anatoliy Atanasov 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Component: ldb ACL module
27 * Description: Module that performs authorisation access checks based on the
28 * account's security context and the DACL of the object being polled.
29 * Only DACL checks implemented at this point
31 * Authors: Nadezhda Ivanova, Anatoliy Atanasov
35 #include "ldb_module.h"
36 #include "auth/auth.h"
37 #include "libcli/security/security.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "librpc/gen_ndr/ndr_security.h"
40 #include "param/param.h"
41 #include "dsdb/samdb/ldb_modules/util.h"
42 #include "lib/util/tsort.h"
43 #include "system/kerberos.h"
44 #include "auth/kerberos/kerberos.h"
46 struct extended_access_check_attribute
{
48 const uint32_t requires_rights
;
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
;
61 struct ldb_module
*module
;
62 struct ldb_request
*req
;
64 bool am_administrator
;
66 bool constructed_attrs
;
67 bool allowedAttributes
;
68 bool allowedAttributesEffective
;
69 bool allowedChildClasses
;
70 bool allowedChildClassesEffective
;
71 bool sDRightsEffective
;
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
;
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
);
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
);
115 ret
= dsdb_module_search_dn(module
, mem_ctx
, &res
,
116 ldb_dn_new(mem_ctx
, ldb
, "@KLUDGEACL"),
118 DSDB_FLAG_NEXT_MODULE
|
121 if (ret
!= LDB_SUCCESS
) {
124 if (res
->count
== 0) {
128 if (res
->count
> 1) {
129 talloc_free(mem_ctx
);
130 return LDB_ERR_CONSTRAINT_VIOLATION
;
135 password_attributes
= ldb_msg_find_element(msg
, "passwordAttribute");
136 if (!password_attributes
) {
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
);
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
);
154 for (i
=0; i
< ARRAY_SIZE(secret_attrs
); i
++) {
157 for (j
=0; j
< n
; j
++) {
158 if (strcasecmp(data
->password_attrs
[j
], secret_attrs
[i
]) == 0) {
168 data
->password_attrs
[n
] = talloc_strdup(data
->password_attrs
,
170 if (data
->password_attrs
[n
] == NULL
) {
171 talloc_free(mem_ctx
);
176 data
->password_attrs
[n
] = NULL
;
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
);
192 const char **attr_list
;
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
);
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
);
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
]);
241 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module
), mem_ctx
, sd_msg
, &sd
);
243 if (ret
!= LDB_SUCCESS
) {
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
,
252 return ldb_operr(ldb
);
254 /* remove constructed attributes */
255 if (attr
->systemFlags
& DS_FLAG_ATTR_IS_CONSTRUCTED
257 || (attr
->linkID
!= 0 && attr
->linkID
% 2 != 0 )) {
260 ret
= acl_check_access_on_attribute(module
,
266 if (ret
== LDB_SUCCESS
) {
267 ldb_msg_add_string(msg
, "allowedAttributesEffective", attr_list
[i
]);
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
;
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
) {
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
]);
304 /* We don't know this class? what is going on? */
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
--;
328 static int acl_check_access_on_class(struct ldb_module
*module
,
329 const struct dsdb_schema
*schema
,
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
)
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
);
347 DEBUG(10, ("acl_search: cannot find class %s\n",
351 if (!insert_in_object_tree(mem_ctx
,
354 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
359 status
= sec_access_check_ds(sd
, token
,
364 if (!NT_STATUS_IS_OK(status
)) {
365 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
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
;
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
) {
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
]);
417 /* We don't know this class? what is going on? */
421 for (j
=0; sclass
->possibleInferiors
&& sclass
->possibleInferiors
[j
]; j
++) {
422 ret
= acl_check_access_on_class(module
,
426 acl_user_token(module
),
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
) {
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
--;
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_message_element
*rightsEffective
;
463 struct security_descriptor
*sd
;
464 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
465 LDB_CONTROL_AS_SYSTEM_OID
);
466 struct dom_sid
*sid
= NULL
;
469 if (as_system
!= NULL
) {
470 as_system
->critical
= 0;
473 /* Must remove any existing attribute, or else confusion reins */
474 ldb_msg_remove_attr(msg
, "sDRightsEffective");
475 ret
= ldb_msg_add_empty(msg
, "sDRightsEffective", 0, &rightsEffective
);
476 if (ret
!= LDB_SUCCESS
) {
479 if (ac
->am_system
|| as_system
) {
480 flags
= SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_SACL
| SECINFO_DACL
;
483 /* Get the security descriptor from the message */
484 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module
), msg
, sd_msg
, &sd
);
485 if (ret
!= LDB_SUCCESS
) {
488 sid
= samdb_result_dom_sid(msg
, sd_msg
, "objectSid");
489 ret
= acl_check_access_on_attribute(module
,
495 if (ret
== LDB_SUCCESS
) {
496 flags
|= SECINFO_OWNER
| SECINFO_GROUP
;
498 ret
= acl_check_access_on_attribute(module
,
504 if (ret
== LDB_SUCCESS
) {
505 flags
|= SECINFO_DACL
;
507 ret
= acl_check_access_on_attribute(module
,
511 SEC_FLAG_SYSTEM_SECURITY
,
513 if (ret
== LDB_SUCCESS
) {
514 flags
|= SECINFO_SACL
;
517 return samdb_msg_add_uint(ldb_module_get_ctx(module
), msg
, msg
,
518 "sDRightsEffective", flags
);
521 static int acl_validate_spn_value(TALLOC_CTX
*mem_ctx
,
522 struct ldb_context
*ldb
,
523 const char *spn_value
,
524 uint32_t userAccountControl
,
525 const char *samAccountName
,
526 const char *dnsHostName
,
527 const char *netbios_name
,
528 const char *ntds_guid
)
531 krb5_context krb_ctx
;
532 krb5_error_code kerr
;
533 krb5_principal principal
;
537 const char *forest_name
= samdb_forest_name(ldb
, mem_ctx
);
538 const char *base_domain
= samdb_default_domain_name(ldb
, mem_ctx
);
539 struct loadparm_context
*lp_ctx
= talloc_get_type(ldb_get_opaque(ldb
, "loadparm"),
540 struct loadparm_context
);
541 bool is_dc
= (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) ||
542 (userAccountControl
& UF_PARTIAL_SECRETS_ACCOUNT
);
544 if (strcasecmp_m(spn_value
, samAccountName
) == 0) {
545 /* MacOS X sets this value, and setting an SPN of your
546 * own samAccountName is both pointless and safe */
550 kerr
= smb_krb5_init_context_basic(mem_ctx
,
554 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
555 "Could not initialize kerberos context.");
558 ret
= krb5_parse_name(krb_ctx
, spn_value
, &principal
);
560 krb5_free_context(krb_ctx
);
561 return LDB_ERR_CONSTRAINT_VIOLATION
;
564 if (principal
->name
.name_string
.len
< 2) {
568 instanceName
= principal
->name
.name_string
.val
[1];
569 serviceType
= principal
->name
.name_string
.val
[0];
570 if (principal
->name
.name_string
.len
== 3) {
571 serviceName
= principal
->name
.name_string
.val
[2];
580 if (strcasecmp(serviceType
, "ldap") == 0) {
581 if (strcasecmp(serviceName
, netbios_name
) != 0 &&
582 strcasecmp(serviceName
, forest_name
) != 0) {
586 } else if (strcasecmp(serviceType
, "gc") == 0) {
587 if (strcasecmp(serviceName
, forest_name
) != 0) {
591 if (strcasecmp(serviceName
, base_domain
) != 0 &&
592 strcasecmp(serviceName
, netbios_name
) != 0) {
597 /* instanceName can be samAccountName without $ or dnsHostName
598 * or "ntds_guid._msdcs.forest_domain for DC objects */
599 if (strlen(instanceName
) == (strlen(samAccountName
) - 1)
600 && strncasecmp(instanceName
, samAccountName
, strlen(samAccountName
) - 1) == 0) {
602 } else if (dnsHostName
!= NULL
&& strcasecmp(instanceName
, dnsHostName
) == 0) {
605 const char *guid_str
;
606 guid_str
= talloc_asprintf(mem_ctx
,"%s._msdcs.%s",
609 if (strcasecmp(instanceName
, guid_str
) == 0) {
615 krb5_free_principal(krb_ctx
, principal
);
616 krb5_free_context(krb_ctx
);
617 return LDB_ERR_CONSTRAINT_VIOLATION
;
620 krb5_free_principal(krb_ctx
, principal
);
621 krb5_free_context(krb_ctx
);
625 static int acl_check_spn(TALLOC_CTX
*mem_ctx
,
626 struct ldb_module
*module
,
627 struct ldb_request
*req
,
628 struct security_descriptor
*sd
,
630 const struct GUID
*oc_guid
,
631 const struct dsdb_attribute
*attr
)
635 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
636 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
637 struct ldb_result
*acl_res
;
638 struct ldb_result
*netbios_res
;
639 struct ldb_message_element
*el
;
640 struct ldb_dn
*partitions_dn
= samdb_partitions_dn(ldb
, tmp_ctx
);
641 uint32_t userAccountControl
;
642 const char *samAccountName
;
643 const char *dnsHostName
;
644 const char *netbios_name
;
646 char *ntds_guid
= NULL
;
648 static const char *acl_attrs
[] = {
651 "userAccountControl",
654 static const char *netbios_attrs
[] = {
659 /* if we have wp, we can do whatever we like */
660 if (acl_check_access_on_attribute(module
,
665 attr
) == LDB_SUCCESS
) {
666 talloc_free(tmp_ctx
);
670 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
671 GUID_DRS_VALIDATE_SPN
,
675 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
676 dsdb_acl_debug(sd
, acl_user_token(module
),
677 req
->op
.mod
.message
->dn
,
680 talloc_free(tmp_ctx
);
684 ret
= dsdb_module_search_dn(module
, tmp_ctx
,
685 &acl_res
, req
->op
.mod
.message
->dn
,
687 DSDB_FLAG_NEXT_MODULE
|
688 DSDB_FLAG_AS_SYSTEM
|
689 DSDB_SEARCH_SHOW_RECYCLED
,
691 if (ret
!= LDB_SUCCESS
) {
692 talloc_free(tmp_ctx
);
696 userAccountControl
= ldb_msg_find_attr_as_uint(acl_res
->msgs
[0], "userAccountControl", 0);
697 dnsHostName
= ldb_msg_find_attr_as_string(acl_res
->msgs
[0], "dnsHostName", NULL
);
698 samAccountName
= ldb_msg_find_attr_as_string(acl_res
->msgs
[0], "samAccountName", NULL
);
700 ret
= dsdb_module_search(module
, tmp_ctx
,
701 &netbios_res
, partitions_dn
,
704 DSDB_FLAG_NEXT_MODULE
|
708 ldb_dn_get_linearized(ldb_get_default_basedn(ldb
)));
710 netbios_name
= ldb_msg_find_attr_as_string(netbios_res
->msgs
[0], "nETBIOSName", NULL
);
712 el
= ldb_msg_find_element(req
->op
.mod
.message
, "servicePrincipalName");
714 talloc_free(tmp_ctx
);
715 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
716 "Error finding element for servicePrincipalName.");
719 /* NTDSDSA objectGuid of object we are checking SPN for */
720 if (userAccountControl
& (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
721 ret
= dsdb_module_find_ntdsguid_for_computer(module
, tmp_ctx
,
722 req
->op
.mod
.message
->dn
, &ntds
, req
);
723 if (ret
!= LDB_SUCCESS
) {
724 ldb_asprintf_errstring(ldb
, "Failed to find NTDSDSA objectGuid for %s: %s",
725 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
),
727 talloc_free(tmp_ctx
);
728 return LDB_ERR_OPERATIONS_ERROR
;
730 ntds_guid
= GUID_string(tmp_ctx
, &ntds
);
733 for (i
=0; i
< el
->num_values
; i
++) {
734 ret
= acl_validate_spn_value(tmp_ctx
,
736 (char *)el
->values
[i
].data
,
742 if (ret
!= LDB_SUCCESS
) {
743 talloc_free(tmp_ctx
);
747 talloc_free(tmp_ctx
);
751 static int acl_add(struct ldb_module
*module
, struct ldb_request
*req
)
754 struct ldb_dn
*parent
= ldb_dn_get_parent(req
, req
->op
.add
.message
->dn
);
755 struct ldb_context
*ldb
;
756 const struct dsdb_schema
*schema
;
757 struct ldb_message_element
*oc_el
;
758 const struct GUID
*guid
;
759 struct ldb_dn
*nc_root
;
760 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
762 if (as_system
!= NULL
) {
763 as_system
->critical
= 0;
766 if (dsdb_module_am_system(module
) || as_system
) {
767 return ldb_next_request(module
, req
);
769 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
770 return ldb_next_request(module
, req
);
773 ldb
= ldb_module_get_ctx(module
);
775 /* Creating an NC. There is probably something we should do here,
776 * but we will establish that later */
778 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.add
.message
->dn
, &nc_root
);
779 if (ret
!= LDB_SUCCESS
) {
782 if (ldb_dn_compare(nc_root
, req
->op
.add
.message
->dn
) == 0) {
783 talloc_free(nc_root
);
784 return ldb_next_request(module
, req
);
786 talloc_free(nc_root
);
788 schema
= dsdb_get_schema(ldb
, req
);
790 return ldb_operr(ldb
);
793 oc_el
= ldb_msg_find_element(req
->op
.add
.message
, "objectClass");
794 if (!oc_el
|| oc_el
->num_values
== 0) {
795 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
796 "acl: unable to find objectClass on %s\n",
797 ldb_dn_get_linearized(req
->op
.add
.message
->dn
));
798 return ldb_module_done(req
, NULL
, NULL
, LDB_ERR_OPERATIONS_ERROR
);
801 guid
= class_schemaid_guid_by_lDAPDisplayName(schema
,
802 (char *)oc_el
->values
[oc_el
->num_values
-1].data
);
803 ret
= dsdb_module_check_access_on_dn(module
, req
, parent
, SEC_ADS_CREATE_CHILD
, guid
, req
);
804 if (ret
!= LDB_SUCCESS
) {
807 return ldb_next_request(module
, req
);
810 /* ckecks if modifications are allowed on "Member" attribute */
811 static int acl_check_self_membership(TALLOC_CTX
*mem_ctx
,
812 struct ldb_module
*module
,
813 struct ldb_request
*req
,
814 struct security_descriptor
*sd
,
816 const struct GUID
*oc_guid
,
817 const struct dsdb_attribute
*attr
)
821 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
822 struct ldb_dn
*user_dn
;
823 struct ldb_message_element
*member_el
;
824 /* if we have wp, we can do whatever we like */
825 if (acl_check_access_on_attribute(module
,
830 attr
) == LDB_SUCCESS
) {
833 /* if we are adding/deleting ourselves, check for self membership */
834 ret
= dsdb_find_dn_by_sid(ldb
, mem_ctx
,
835 &acl_user_token(module
)->sids
[PRIMARY_USER_SID_INDEX
],
837 if (ret
!= LDB_SUCCESS
) {
840 member_el
= ldb_msg_find_element(req
->op
.mod
.message
, "member");
842 return ldb_operr(ldb
);
844 /* user can only remove oneself */
845 if (member_el
->num_values
== 0) {
846 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
848 for (i
= 0; i
< member_el
->num_values
; i
++) {
849 if (strcasecmp((const char *)member_el
->values
[i
].data
,
850 ldb_dn_get_extended_linearized(mem_ctx
, user_dn
, 1)) != 0) {
851 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
854 ret
= acl_check_extended_right(mem_ctx
, sd
, acl_user_token(module
),
855 GUID_DRS_SELF_MEMBERSHIP
,
858 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
859 dsdb_acl_debug(sd
, acl_user_token(module
),
860 req
->op
.mod
.message
->dn
,
867 static int acl_check_password_rights(TALLOC_CTX
*mem_ctx
,
868 struct ldb_module
*module
,
869 struct ldb_request
*req
,
870 struct security_descriptor
*sd
,
872 const struct GUID
*oc_guid
,
875 int ret
= LDB_SUCCESS
;
876 unsigned int del_attr_cnt
= 0, add_attr_cnt
= 0, rep_attr_cnt
= 0;
877 struct ldb_message_element
*el
;
878 struct ldb_message
*msg
;
879 const char *passwordAttrs
[] = { "userPassword", "clearTextPassword",
880 "unicodePwd", "dBCSPwd", NULL
}, **l
;
881 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
883 msg
= ldb_msg_copy_shallow(tmp_ctx
, req
->op
.mod
.message
);
885 return ldb_module_oom(module
);
887 for (l
= passwordAttrs
; *l
!= NULL
; l
++) {
888 if ((!userPassword
) && (ldb_attr_cmp(*l
, "userPassword") == 0)) {
892 while ((el
= ldb_msg_find_element(msg
, *l
)) != NULL
) {
893 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
) {
896 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_ADD
) {
899 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_REPLACE
) {
902 ldb_msg_remove_element(msg
, el
);
906 /* single deletes will be handled by the "password_hash" LDB module
907 * later in the stack, so we let it though here */
908 if ((del_attr_cnt
> 0) && (add_attr_cnt
== 0) && (rep_attr_cnt
== 0)) {
909 talloc_free(tmp_ctx
);
913 if (ldb_request_get_control(req
,
914 DSDB_CONTROL_PASSWORD_CHANGE_OID
) != NULL
) {
915 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
916 * have a user password change and not a set as the message
917 * looks like. In it's value blob it contains the NT and/or LM
918 * hash of the old password specified by the user.
919 * This control is used by the SAMR and "kpasswd" password
920 * change mechanisms. */
921 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
922 GUID_DRS_USER_CHANGE_PASSWORD
,
923 SEC_ADS_CONTROL_ACCESS
,
926 else if (rep_attr_cnt
> 0 || (add_attr_cnt
!= del_attr_cnt
)) {
927 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
928 GUID_DRS_FORCE_CHANGE_PASSWORD
,
929 SEC_ADS_CONTROL_ACCESS
,
932 else if (add_attr_cnt
== 1 && del_attr_cnt
== 1) {
933 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
934 GUID_DRS_USER_CHANGE_PASSWORD
,
935 SEC_ADS_CONTROL_ACCESS
,
937 /* Very strange, but we get constraint violation in this case */
938 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
939 ret
= LDB_ERR_CONSTRAINT_VIOLATION
;
942 if (ret
!= LDB_SUCCESS
) {
943 dsdb_acl_debug(sd
, acl_user_token(module
),
944 req
->op
.mod
.message
->dn
,
948 talloc_free(tmp_ctx
);
952 static const struct GUID
*get_oc_guid_from_message(const struct dsdb_schema
*schema
,
953 struct ldb_message
*msg
)
955 struct ldb_message_element
*oc_el
;
956 const struct dsdb_class
*object_class
;
958 oc_el
= ldb_msg_find_element(msg
, "objectClass");
963 object_class
= dsdb_get_last_structural_class(schema
, oc_el
);
964 if (object_class
== NULL
) {
968 return &object_class
->schemaIDGUID
;
972 static int acl_modify(struct ldb_module
*module
, struct ldb_request
*req
)
975 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
976 const struct dsdb_schema
*schema
;
978 const struct GUID
*guid
;
979 uint32_t access_granted
;
980 struct object_tree
*root
= NULL
;
981 struct object_tree
*new_node
= NULL
;
983 struct ldb_result
*acl_res
;
984 struct security_descriptor
*sd
;
985 struct dom_sid
*sid
= NULL
;
986 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
987 bool userPassword
= dsdb_user_password_support(module
, req
, req
);
988 TALLOC_CTX
*tmp_ctx
= talloc_new(req
);
989 static const char *acl_attrs
[] = {
990 "nTSecurityDescriptor",
996 if (as_system
!= NULL
) {
997 as_system
->critical
= 0;
1000 /* Don't print this debug statement if elements[0].name is going to be NULL */
1001 if(req
->op
.mod
.message
->num_elements
> 0)
1003 DEBUG(10, ("ldb:acl_modify: %s\n", req
->op
.mod
.message
->elements
[0].name
));
1005 if (dsdb_module_am_system(module
) || as_system
) {
1006 return ldb_next_request(module
, req
);
1008 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
1009 return ldb_next_request(module
, req
);
1011 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &acl_res
, req
->op
.mod
.message
->dn
,
1013 DSDB_FLAG_NEXT_MODULE
|
1014 DSDB_FLAG_AS_SYSTEM
|
1015 DSDB_SEARCH_SHOW_RECYCLED
,
1018 if (ret
!= LDB_SUCCESS
) {
1022 schema
= dsdb_get_schema(ldb
, tmp_ctx
);
1024 ret
= LDB_ERR_OPERATIONS_ERROR
;
1028 ret
= dsdb_get_sd_from_ldb_message(ldb
, tmp_ctx
, acl_res
->msgs
[0], &sd
);
1029 if (ret
!= LDB_SUCCESS
) {
1030 talloc_free(tmp_ctx
);
1031 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1032 "acl_modify: Error retrieving security descriptor.");
1034 /* Theoretically we pass the check if the object has no sd */
1039 guid
= get_oc_guid_from_message(schema
, acl_res
->msgs
[0]);
1041 talloc_free(tmp_ctx
);
1042 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1043 "acl_modify: Error retrieving object class GUID.");
1045 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1046 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1047 &root
, &new_node
)) {
1048 talloc_free(tmp_ctx
);
1049 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1050 "acl_modify: Error adding new node in object tree.");
1052 for (i
=0; i
< req
->op
.mod
.message
->num_elements
; i
++){
1053 const struct dsdb_attribute
*attr
;
1054 attr
= dsdb_attribute_by_lDAPDisplayName(schema
,
1055 req
->op
.mod
.message
->elements
[i
].name
);
1057 if (ldb_attr_cmp("nTSecurityDescriptor", req
->op
.mod
.message
->elements
[i
].name
) == 0) {
1058 uint32_t sd_flags
= dsdb_request_sd_flags(req
, NULL
);
1059 uint32_t access_mask
= 0;
1061 if (sd_flags
& (SECINFO_OWNER
|SECINFO_GROUP
)) {
1062 access_mask
|= SEC_STD_WRITE_OWNER
;
1064 if (sd_flags
& SECINFO_DACL
) {
1065 access_mask
|= SEC_STD_WRITE_DAC
;
1067 if (sd_flags
& SECINFO_SACL
) {
1068 access_mask
|= SEC_FLAG_SYSTEM_SECURITY
;
1071 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1077 if (!NT_STATUS_IS_OK(status
)) {
1078 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1079 "Object %s has no write dacl access\n",
1080 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
));
1082 acl_user_token(module
),
1083 req
->op
.mod
.message
->dn
,
1086 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1090 else if (ldb_attr_cmp("member", req
->op
.mod
.message
->elements
[i
].name
) == 0) {
1091 ret
= acl_check_self_membership(tmp_ctx
,
1098 if (ret
!= LDB_SUCCESS
) {
1102 else if (ldb_attr_cmp("dBCSPwd", req
->op
.mod
.message
->elements
[i
].name
) == 0) {
1103 /* this one is not affected by any rights, we should let it through
1104 so that passwords_hash returns the correct error */
1107 else if (ldb_attr_cmp("unicodePwd", req
->op
.mod
.message
->elements
[i
].name
) == 0 ||
1108 (userPassword
&& ldb_attr_cmp("userPassword", req
->op
.mod
.message
->elements
[i
].name
) == 0) ||
1109 ldb_attr_cmp("clearTextPassword", req
->op
.mod
.message
->elements
[i
].name
) == 0) {
1110 ret
= acl_check_password_rights(tmp_ctx
,
1117 if (ret
!= LDB_SUCCESS
) {
1120 } else if (ldb_attr_cmp("servicePrincipalName", req
->op
.mod
.message
->elements
[i
].name
) == 0) {
1121 ret
= acl_check_spn(tmp_ctx
,
1128 if (ret
!= LDB_SUCCESS
) {
1133 /* This basic attribute existence check with the right errorcode
1134 * is needed since this module is the first one which requests
1135 * schema attribute information.
1136 * The complete attribute checking is done in the
1137 * "objectclass_attrs" module behind this one.
1140 ldb_asprintf_errstring(ldb
, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
1141 req
->op
.mod
.message
->elements
[i
].name
,
1142 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
));
1143 ret
= LDB_ERR_NO_SUCH_ATTRIBUTE
;
1146 if (!insert_in_object_tree(tmp_ctx
,
1147 &attr
->attributeSecurityGUID
, SEC_ADS_WRITE_PROP
,
1148 &new_node
, &new_node
)) {
1149 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1150 "acl_modify: cannot add to object tree securityGUID\n");
1151 ret
= LDB_ERR_OPERATIONS_ERROR
;
1155 if (!insert_in_object_tree(tmp_ctx
,
1156 &attr
->schemaIDGUID
, SEC_ADS_WRITE_PROP
, &new_node
, &new_node
)) {
1157 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1158 "acl_modify: cannot add to object tree attributeGUID\n");
1159 ret
= LDB_ERR_OPERATIONS_ERROR
;
1165 if (root
->num_of_children
> 0) {
1166 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1172 if (!NT_STATUS_IS_OK(status
)) {
1173 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1174 "Object %s has no write property access\n",
1175 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
));
1177 acl_user_token(module
),
1178 req
->op
.mod
.message
->dn
,
1181 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1187 talloc_free(tmp_ctx
);
1188 return ldb_next_request(module
, req
);
1190 talloc_free(tmp_ctx
);
1194 /* similar to the modify for the time being.
1195 * We need to consider the special delete tree case, though - TODO */
1196 static int acl_delete(struct ldb_module
*module
, struct ldb_request
*req
)
1199 struct ldb_dn
*parent
= ldb_dn_get_parent(req
, req
->op
.del
.dn
);
1200 struct ldb_context
*ldb
;
1201 struct ldb_dn
*nc_root
;
1202 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1204 if (as_system
!= NULL
) {
1205 as_system
->critical
= 0;
1208 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req
->op
.del
.dn
)));
1209 if (dsdb_module_am_system(module
) || as_system
) {
1210 return ldb_next_request(module
, req
);
1212 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
1213 return ldb_next_request(module
, req
);
1216 ldb
= ldb_module_get_ctx(module
);
1218 /* Make sure we aren't deleting a NC */
1220 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.del
.dn
, &nc_root
);
1221 if (ret
!= LDB_SUCCESS
) {
1224 if (ldb_dn_compare(nc_root
, req
->op
.del
.dn
) == 0) {
1225 talloc_free(nc_root
);
1226 DEBUG(10,("acl:deleting a NC\n"));
1227 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1228 return ldb_module_done(req
, NULL
, NULL
,
1229 LDB_ERR_UNWILLING_TO_PERFORM
);
1231 talloc_free(nc_root
);
1233 /* First check if we have delete object right */
1234 ret
= dsdb_module_check_access_on_dn(module
, req
, req
->op
.del
.dn
,
1235 SEC_STD_DELETE
, NULL
, req
);
1236 if (ret
== LDB_SUCCESS
) {
1237 return ldb_next_request(module
, req
);
1240 /* Nope, we don't have delete object. Lets check if we have delete
1241 * child on the parent */
1242 ret
= dsdb_module_check_access_on_dn(module
, req
, parent
,
1243 SEC_ADS_DELETE_CHILD
, NULL
, req
);
1244 if (ret
!= LDB_SUCCESS
) {
1248 return ldb_next_request(module
, req
);
1251 static int acl_rename(struct ldb_module
*module
, struct ldb_request
*req
)
1254 struct ldb_dn
*oldparent
= ldb_dn_get_parent(req
, req
->op
.rename
.olddn
);
1255 struct ldb_dn
*newparent
= ldb_dn_get_parent(req
, req
->op
.rename
.newdn
);
1256 const struct dsdb_schema
*schema
;
1257 struct ldb_context
*ldb
;
1258 struct security_descriptor
*sd
= NULL
;
1259 struct dom_sid
*sid
= NULL
;
1260 struct ldb_result
*acl_res
;
1261 const struct GUID
*guid
;
1262 struct ldb_dn
*nc_root
;
1263 struct object_tree
*root
= NULL
;
1264 struct object_tree
*new_node
= NULL
;
1265 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1266 TALLOC_CTX
*tmp_ctx
= talloc_new(req
);
1268 uint32_t access_granted
;
1269 const char *rdn_name
;
1270 static const char *acl_attrs
[] = {
1271 "nTSecurityDescriptor",
1277 if (as_system
!= NULL
) {
1278 as_system
->critical
= 0;
1281 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1282 if (dsdb_module_am_system(module
) || as_system
) {
1283 return ldb_next_request(module
, req
);
1285 if (ldb_dn_is_special(req
->op
.rename
.olddn
)) {
1286 return ldb_next_request(module
, req
);
1289 ldb
= ldb_module_get_ctx(module
);
1291 /* Make sure we aren't renaming/moving a NC */
1293 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.rename
.olddn
, &nc_root
);
1294 if (ret
!= LDB_SUCCESS
) {
1297 if (ldb_dn_compare(nc_root
, req
->op
.rename
.olddn
) == 0) {
1298 talloc_free(nc_root
);
1299 DEBUG(10,("acl:renaming/moving a NC\n"));
1300 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1301 return ldb_module_done(req
, NULL
, NULL
,
1302 LDB_ERR_UNWILLING_TO_PERFORM
);
1304 talloc_free(nc_root
);
1306 /* Look for the parent */
1308 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &acl_res
,
1309 req
->op
.rename
.olddn
, acl_attrs
,
1310 DSDB_FLAG_NEXT_MODULE
|
1311 DSDB_FLAG_AS_SYSTEM
|
1312 DSDB_SEARCH_SHOW_RECYCLED
, req
);
1313 /* we sould be able to find the parent */
1314 if (ret
!= LDB_SUCCESS
) {
1315 DEBUG(10,("acl: failed to find object %s\n",
1316 ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1317 talloc_free(tmp_ctx
);
1321 schema
= dsdb_get_schema(ldb
, acl_res
);
1323 talloc_free(tmp_ctx
);
1324 return ldb_operr(ldb
);
1327 guid
= get_oc_guid_from_message(schema
, acl_res
->msgs
[0]);
1328 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1329 &root
, &new_node
)) {
1330 talloc_free(tmp_ctx
);
1331 return ldb_operr(ldb
);
1334 guid
= attribute_schemaid_guid_by_lDAPDisplayName(schema
,
1336 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1337 &new_node
, &new_node
)) {
1338 talloc_free(tmp_ctx
);
1339 return ldb_operr(ldb
);
1342 rdn_name
= ldb_dn_get_rdn_name(req
->op
.rename
.olddn
);
1343 if (rdn_name
== NULL
) {
1344 talloc_free(tmp_ctx
);
1345 return ldb_operr(ldb
);
1347 guid
= attribute_schemaid_guid_by_lDAPDisplayName(schema
,
1349 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1350 &new_node
, &new_node
)) {
1351 talloc_free(tmp_ctx
);
1352 return ldb_operr(ldb
);
1355 ret
= dsdb_get_sd_from_ldb_message(ldb
, req
, acl_res
->msgs
[0], &sd
);
1357 if (ret
!= LDB_SUCCESS
) {
1358 talloc_free(tmp_ctx
);
1359 return ldb_operr(ldb
);
1361 /* Theoretically we pass the check if the object has no sd */
1363 talloc_free(tmp_ctx
);
1366 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1367 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1373 if (!NT_STATUS_IS_OK(status
)) {
1374 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1375 "Object %s has no wp on name\n",
1376 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1378 acl_user_token(module
),
1379 req
->op
.rename
.olddn
,
1382 talloc_free(tmp_ctx
);
1383 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1386 if (ldb_dn_compare(oldparent
, newparent
) == 0) {
1387 /* regular rename, not move, nothing more to do */
1388 talloc_free(tmp_ctx
);
1389 return ldb_next_request(module
, req
);
1392 /* new parent should have create child */
1395 guid
= get_oc_guid_from_message(schema
, acl_res
->msgs
[0]);
1397 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1398 "acl:renamed object has no object class\n");
1399 talloc_free(tmp_ctx
);
1400 return ldb_module_done(req
, NULL
, NULL
, LDB_ERR_OPERATIONS_ERROR
);
1403 ret
= dsdb_module_check_access_on_dn(module
, req
, newparent
, SEC_ADS_CREATE_CHILD
, guid
, req
);
1404 if (ret
!= LDB_SUCCESS
) {
1405 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1406 "acl:access_denied renaming %s",
1407 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1408 talloc_free(tmp_ctx
);
1411 /* do we have delete object on the object? */
1413 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1419 if (NT_STATUS_IS_OK(status
)) {
1420 talloc_free(tmp_ctx
);
1421 return ldb_next_request(module
, req
);
1423 /* what about delete child on the current parent */
1424 ret
= dsdb_module_check_access_on_dn(module
, req
, oldparent
, SEC_ADS_DELETE_CHILD
, NULL
, req
);
1425 if (ret
!= LDB_SUCCESS
) {
1426 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1427 "acl:access_denied renaming %s", ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1428 talloc_free(tmp_ctx
);
1429 return ldb_module_done(req
, NULL
, NULL
, ret
);
1432 talloc_free(tmp_ctx
);
1434 return ldb_next_request(module
, req
);
1437 static int acl_search_update_confidential_attrs(struct acl_context
*ac
,
1438 struct acl_private
*data
)
1440 struct dsdb_attribute
*a
;
1443 if (data
->acl_search
) {
1445 * If acl:search is activated, the acl_read module
1446 * protects confidential attributes.
1451 if ((ac
->schema
== data
->cached_schema_ptr
) &&
1452 (ac
->schema
->loaded_usn
== data
->cached_schema_loaded_usn
) &&
1453 (ac
->schema
->metadata_usn
== data
->cached_schema_metadata_usn
))
1458 data
->cached_schema_ptr
= NULL
;
1459 data
->cached_schema_loaded_usn
= 0;
1460 data
->cached_schema_metadata_usn
= 0;
1461 TALLOC_FREE(data
->confidential_attrs
);
1463 if (ac
->schema
== NULL
) {
1467 for (a
= ac
->schema
->attributes
; a
; a
= a
->next
) {
1468 const char **attrs
= data
->confidential_attrs
;
1470 if (!(a
->searchFlags
& SEARCH_FLAG_CONFIDENTIAL
)) {
1474 attrs
= talloc_realloc(data
, attrs
, const char *, n
+ 2);
1475 if (attrs
== NULL
) {
1476 TALLOC_FREE(data
->confidential_attrs
);
1477 return ldb_module_oom(ac
->module
);
1480 attrs
[n
] = a
->lDAPDisplayName
;
1484 data
->confidential_attrs
= attrs
;
1487 data
->cached_schema_ptr
= ac
->schema
;
1488 data
->cached_schema_loaded_usn
= ac
->schema
->loaded_usn
;
1489 data
->cached_schema_metadata_usn
= ac
->schema
->metadata_usn
;
1494 static int acl_search_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1496 struct acl_context
*ac
;
1497 struct acl_private
*data
;
1498 struct ldb_result
*acl_res
;
1499 static const char *acl_attrs
[] = {
1501 "nTSecurityDescriptor",
1508 ac
= talloc_get_type(req
->context
, struct acl_context
);
1509 data
= talloc_get_type(ldb_module_get_private(ac
->module
), struct acl_private
);
1511 return ldb_module_done(ac
->req
, NULL
, NULL
,
1512 LDB_ERR_OPERATIONS_ERROR
);
1514 if (ares
->error
!= LDB_SUCCESS
) {
1515 return ldb_module_done(ac
->req
, ares
->controls
,
1516 ares
->response
, ares
->error
);
1519 switch (ares
->type
) {
1520 case LDB_REPLY_ENTRY
:
1521 if (ac
->constructed_attrs
) {
1522 ret
= dsdb_module_search_dn(ac
->module
, ac
, &acl_res
, ares
->message
->dn
,
1524 DSDB_FLAG_NEXT_MODULE
|
1525 DSDB_FLAG_AS_SYSTEM
|
1526 DSDB_SEARCH_SHOW_RECYCLED
,
1528 if (ret
!= LDB_SUCCESS
) {
1529 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1533 if (ac
->allowedAttributes
|| ac
->allowedAttributesEffective
) {
1534 ret
= acl_allowedAttributes(ac
->module
, ac
->schema
,
1537 if (ret
!= LDB_SUCCESS
) {
1538 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1542 if (ac
->allowedChildClasses
) {
1543 ret
= acl_childClasses(ac
->module
, ac
->schema
,
1546 "allowedChildClasses");
1547 if (ret
!= LDB_SUCCESS
) {
1548 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1552 if (ac
->allowedChildClassesEffective
) {
1553 ret
= acl_childClassesEffective(ac
->module
, ac
->schema
,
1556 if (ret
!= LDB_SUCCESS
) {
1557 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1561 if (ac
->sDRightsEffective
) {
1562 ret
= acl_sDRightsEffective(ac
->module
,
1565 if (ret
!= LDB_SUCCESS
) {
1566 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1571 return ldb_module_send_entry(ac
->req
, ares
->message
,
1575 if (ac
->am_system
) {
1576 return ldb_module_send_entry(ac
->req
, ares
->message
,
1580 if (data
->password_attrs
!= NULL
) {
1581 for (i
= 0; data
->password_attrs
[i
]; i
++) {
1582 if ((!ac
->userPassword
) &&
1583 (ldb_attr_cmp(data
->password_attrs
[i
],
1584 "userPassword") == 0))
1589 ldb_msg_remove_attr(ares
->message
, data
->password_attrs
[i
]);
1593 if (ac
->am_administrator
) {
1594 return ldb_module_send_entry(ac
->req
, ares
->message
,
1598 ret
= acl_search_update_confidential_attrs(ac
, data
);
1599 if (ret
!= LDB_SUCCESS
) {
1603 if (data
->confidential_attrs
!= NULL
) {
1604 for (i
= 0; data
->confidential_attrs
[i
]; i
++) {
1605 ldb_msg_remove_attr(ares
->message
,
1606 data
->confidential_attrs
[i
]);
1610 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
1612 case LDB_REPLY_REFERRAL
:
1613 return ldb_module_send_referral(ac
->req
, ares
->referral
);
1615 case LDB_REPLY_DONE
:
1616 return ldb_module_done(ac
->req
, ares
->controls
,
1617 ares
->response
, LDB_SUCCESS
);
1623 static int acl_search(struct ldb_module
*module
, struct ldb_request
*req
)
1625 struct ldb_context
*ldb
;
1626 struct acl_context
*ac
;
1627 struct ldb_parse_tree
*down_tree
;
1628 struct ldb_request
*down_req
;
1629 struct acl_private
*data
;
1633 ldb
= ldb_module_get_ctx(module
);
1635 ac
= talloc_zero(req
, struct acl_context
);
1637 return ldb_oom(ldb
);
1639 data
= talloc_get_type(ldb_module_get_private(module
), struct acl_private
);
1641 ac
->module
= module
;
1643 ac
->am_system
= dsdb_module_am_system(module
);
1644 ac
->am_administrator
= dsdb_module_am_administrator(module
);
1645 ac
->constructed_attrs
= false;
1646 ac
->modify_search
= true;
1647 ac
->allowedAttributes
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedAttributes");
1648 ac
->allowedAttributesEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedAttributesEffective");
1649 ac
->allowedChildClasses
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedChildClasses");
1650 ac
->allowedChildClassesEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedChildClassesEffective");
1651 ac
->sDRightsEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "sDRightsEffective");
1652 ac
->userPassword
= dsdb_user_password_support(module
, ac
, req
);
1653 ac
->schema
= dsdb_get_schema(ldb
, ac
);
1655 ac
->constructed_attrs
|= ac
->allowedAttributes
;
1656 ac
->constructed_attrs
|= ac
->allowedChildClasses
;
1657 ac
->constructed_attrs
|= ac
->allowedChildClassesEffective
;
1658 ac
->constructed_attrs
|= ac
->allowedAttributesEffective
;
1659 ac
->constructed_attrs
|= ac
->sDRightsEffective
;
1662 ac
->modify_search
= false;
1664 if (ac
->am_system
) {
1665 ac
->modify_search
= false;
1668 if (!ac
->constructed_attrs
&& !ac
->modify_search
) {
1669 return ldb_next_request(module
, req
);
1672 ret
= acl_search_update_confidential_attrs(ac
, data
);
1673 if (ret
!= LDB_SUCCESS
) {
1677 down_tree
= ldb_parse_tree_copy_shallow(ac
, req
->op
.search
.tree
);
1678 if (down_tree
== NULL
) {
1679 return ldb_oom(ldb
);
1682 if (!ac
->am_system
&& data
->password_attrs
) {
1683 for (i
= 0; data
->password_attrs
[i
]; i
++) {
1684 if ((!ac
->userPassword
) &&
1685 (ldb_attr_cmp(data
->password_attrs
[i
],
1686 "userPassword") == 0))
1691 ldb_parse_tree_attr_replace(down_tree
,
1692 data
->password_attrs
[i
],
1693 "kludgeACLredactedattribute");
1697 if (!ac
->am_system
&& !ac
->am_administrator
&& data
->confidential_attrs
) {
1698 for (i
= 0; data
->confidential_attrs
[i
]; i
++) {
1699 ldb_parse_tree_attr_replace(down_tree
,
1700 data
->confidential_attrs
[i
],
1701 "kludgeACLredactedattribute");
1705 ret
= ldb_build_search_req_ex(&down_req
,
1707 req
->op
.search
.base
,
1708 req
->op
.search
.scope
,
1710 req
->op
.search
.attrs
,
1712 ac
, acl_search_callback
,
1714 LDB_REQ_SET_LOCATION(down_req
);
1715 if (ret
!= LDB_SUCCESS
) {
1718 /* perform the search */
1719 return ldb_next_request(module
, down_req
);
1722 static int acl_extended(struct ldb_module
*module
, struct ldb_request
*req
)
1724 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
1725 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1727 /* allow everybody to read the sequence number */
1728 if (strcmp(req
->op
.extended
.oid
,
1729 LDB_EXTENDED_SEQUENCE_NUMBER
) == 0) {
1730 return ldb_next_request(module
, req
);
1733 if (dsdb_module_am_system(module
) ||
1734 dsdb_module_am_administrator(module
) || as_system
) {
1735 return ldb_next_request(module
, req
);
1737 ldb_asprintf_errstring(ldb
,
1739 "attempted database modify not permitted. "
1740 "User %s is not SYSTEM or an administrator",
1741 acl_user_name(req
, module
));
1742 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1746 static const struct ldb_module_ops ldb_acl_module_ops
= {
1748 .search
= acl_search
,
1750 .modify
= acl_modify
,
1752 .rename
= acl_rename
,
1753 .extended
= acl_extended
,
1754 .init_context
= acl_module_init
1757 int ldb_acl_module_init(const char *version
)
1759 LDB_MODULE_CHECK_VERSION(version
);
1760 return ldb_register_module(&ldb_acl_module_ops
);