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
;
58 bool userPassword_support
;
62 struct ldb_module
*module
;
63 struct ldb_request
*req
;
65 bool am_administrator
;
67 bool constructed_attrs
;
68 bool allowedAttributes
;
69 bool allowedAttributesEffective
;
70 bool allowedChildClasses
;
71 bool allowedChildClassesEffective
;
72 bool sDRightsEffective
;
74 const char * const *attrs
;
75 struct dsdb_schema
*schema
;
78 static int acl_module_init(struct ldb_module
*module
)
80 struct ldb_context
*ldb
;
81 struct acl_private
*data
;
85 static const char * const attrs
[] = { "passwordAttribute", NULL
};
86 static const char * const secret_attrs
[] = {
87 DSDB_SECRET_ATTRIBUTES
89 struct ldb_result
*res
;
90 struct ldb_message
*msg
;
91 struct ldb_message_element
*password_attributes
;
93 ldb
= ldb_module_get_ctx(module
);
95 ret
= ldb_mod_register_control(module
, LDB_CONTROL_SD_FLAGS_OID
);
96 if (ret
!= LDB_SUCCESS
) {
97 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
98 "acl_module_init: Unable to register control with rootdse!\n");
99 return ldb_operr(ldb
);
102 data
= talloc_zero(module
, struct acl_private
);
107 data
->acl_search
= lpcfg_parm_bool(ldb_get_opaque(ldb
, "loadparm"),
108 NULL
, "acl", "search", true);
109 ldb_module_set_private(module
, data
);
111 data
->userPassword_support
= dsdb_user_password_support(module
, module
, NULL
);
113 mem_ctx
= talloc_new(module
);
118 ret
= dsdb_module_search_dn(module
, mem_ctx
, &res
,
119 ldb_dn_new(mem_ctx
, ldb
, "@KLUDGEACL"),
121 DSDB_FLAG_NEXT_MODULE
|
124 if (ret
!= LDB_SUCCESS
) {
127 if (res
->count
== 0) {
131 if (res
->count
> 1) {
132 talloc_free(mem_ctx
);
133 return LDB_ERR_CONSTRAINT_VIOLATION
;
138 password_attributes
= ldb_msg_find_element(msg
, "passwordAttribute");
139 if (!password_attributes
) {
142 data
->password_attrs
= talloc_array(data
, const char *,
143 password_attributes
->num_values
+
144 ARRAY_SIZE(secret_attrs
) + 1);
145 if (!data
->password_attrs
) {
146 talloc_free(mem_ctx
);
151 for (i
=0; i
< password_attributes
->num_values
; i
++) {
152 data
->password_attrs
[n
] = (const char *)password_attributes
->values
[i
].data
;
153 talloc_steal(data
->password_attrs
, password_attributes
->values
[i
].data
);
157 for (i
=0; i
< ARRAY_SIZE(secret_attrs
); i
++) {
160 for (j
=0; j
< n
; j
++) {
161 if (strcasecmp(data
->password_attrs
[j
], secret_attrs
[i
]) == 0) {
171 data
->password_attrs
[n
] = talloc_strdup(data
->password_attrs
,
173 if (data
->password_attrs
[n
] == NULL
) {
174 talloc_free(mem_ctx
);
179 data
->password_attrs
[n
] = NULL
;
182 talloc_free(mem_ctx
);
183 return ldb_next_init(module
);
186 static int acl_allowedAttributes(struct ldb_module
*module
,
187 const struct dsdb_schema
*schema
,
188 struct ldb_message
*sd_msg
,
189 struct ldb_message
*msg
,
190 struct acl_context
*ac
)
192 struct ldb_message_element
*oc_el
;
193 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
195 const char **attr_list
;
197 const struct dsdb_class
*objectclass
;
199 /* If we don't have a schema yet, we can't do anything... */
200 if (schema
== NULL
) {
201 ldb_asprintf_errstring(ldb
, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg
->dn
));
202 return LDB_ERR_OPERATIONS_ERROR
;
205 /* Must remove any existing attribute */
206 if (ac
->allowedAttributes
) {
207 ldb_msg_remove_attr(msg
, "allowedAttributes");
210 mem_ctx
= talloc_new(msg
);
215 oc_el
= ldb_msg_find_element(sd_msg
, "objectClass");
216 attr_list
= dsdb_full_attribute_list(mem_ctx
, schema
, oc_el
, DSDB_SCHEMA_ALL
);
218 ldb_asprintf_errstring(ldb
, "acl: Failed to get list of attributes");
219 talloc_free(mem_ctx
);
220 return LDB_ERR_OPERATIONS_ERROR
;
224 * Get the top-most structural object class for the ACL check
226 objectclass
= dsdb_get_last_structural_class(ac
->schema
,
228 if (objectclass
== NULL
) {
229 ldb_asprintf_errstring(ldb
, "acl_read: Failed to find a structural class for %s",
230 ldb_dn_get_linearized(sd_msg
->dn
));
231 talloc_free(mem_ctx
);
232 return LDB_ERR_OPERATIONS_ERROR
;
235 if (ac
->allowedAttributes
) {
236 for (i
=0; attr_list
&& attr_list
[i
]; i
++) {
237 ldb_msg_add_string(msg
, "allowedAttributes", attr_list
[i
]);
240 if (ac
->allowedAttributesEffective
) {
241 struct security_descriptor
*sd
;
242 struct dom_sid
*sid
= NULL
;
243 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
244 LDB_CONTROL_AS_SYSTEM_OID
);
246 if (as_system
!= NULL
) {
247 as_system
->critical
= 0;
250 ldb_msg_remove_attr(msg
, "allowedAttributesEffective");
251 if (ac
->am_system
|| as_system
) {
252 for (i
=0; attr_list
&& attr_list
[i
]; i
++) {
253 ldb_msg_add_string(msg
, "allowedAttributesEffective", attr_list
[i
]);
258 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module
), mem_ctx
, sd_msg
, &sd
);
260 if (ret
!= LDB_SUCCESS
) {
264 sid
= samdb_result_dom_sid(mem_ctx
, sd_msg
, "objectSid");
265 for (i
=0; attr_list
&& attr_list
[i
]; i
++) {
266 const struct dsdb_attribute
*attr
= dsdb_attribute_by_lDAPDisplayName(schema
,
269 return ldb_operr(ldb
);
271 /* remove constructed attributes */
272 if (attr
->systemFlags
& DS_FLAG_ATTR_IS_CONSTRUCTED
274 || (attr
->linkID
!= 0 && attr
->linkID
% 2 != 0 )) {
277 ret
= acl_check_access_on_attribute(module
,
284 if (ret
== LDB_SUCCESS
) {
285 ldb_msg_add_string(msg
, "allowedAttributesEffective", attr_list
[i
]);
292 static int acl_childClasses(struct ldb_module
*module
,
293 const struct dsdb_schema
*schema
,
294 struct ldb_message
*sd_msg
,
295 struct ldb_message
*msg
,
296 const char *attrName
)
298 struct ldb_message_element
*oc_el
;
299 struct ldb_message_element
*allowedClasses
;
300 const struct dsdb_class
*sclass
;
304 /* If we don't have a schema yet, we can't do anything... */
305 if (schema
== NULL
) {
306 ldb_asprintf_errstring(ldb_module_get_ctx(module
), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg
->dn
));
307 return LDB_ERR_OPERATIONS_ERROR
;
310 /* Must remove any existing attribute, or else confusion reins */
311 ldb_msg_remove_attr(msg
, attrName
);
312 ret
= ldb_msg_add_empty(msg
, attrName
, 0, &allowedClasses
);
313 if (ret
!= LDB_SUCCESS
) {
317 oc_el
= ldb_msg_find_element(sd_msg
, "objectClass");
319 for (i
=0; oc_el
&& i
< oc_el
->num_values
; i
++) {
320 sclass
= dsdb_class_by_lDAPDisplayName_ldb_val(schema
, &oc_el
->values
[i
]);
322 /* We don't know this class? what is going on? */
326 for (j
=0; sclass
->possibleInferiors
&& sclass
->possibleInferiors
[j
]; j
++) {
327 ldb_msg_add_string(msg
, attrName
, sclass
->possibleInferiors
[j
]);
330 if (allowedClasses
->num_values
> 1) {
331 TYPESAFE_QSORT(allowedClasses
->values
, allowedClasses
->num_values
, data_blob_cmp
);
332 for (i
=1 ; i
< allowedClasses
->num_values
; i
++) {
333 struct ldb_val
*val1
= &allowedClasses
->values
[i
-1];
334 struct ldb_val
*val2
= &allowedClasses
->values
[i
];
335 if (data_blob_cmp(val1
, val2
) == 0) {
336 memmove(val1
, val2
, (allowedClasses
->num_values
- i
) * sizeof(struct ldb_val
));
337 allowedClasses
->num_values
--;
346 static int acl_childClassesEffective(struct ldb_module
*module
,
347 const struct dsdb_schema
*schema
,
348 struct ldb_message
*sd_msg
,
349 struct ldb_message
*msg
,
350 struct acl_context
*ac
)
352 struct ldb_message_element
*oc_el
;
353 struct ldb_message_element
*allowedClasses
= NULL
;
354 const struct dsdb_class
*sclass
;
355 struct security_descriptor
*sd
;
356 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
357 LDB_CONTROL_AS_SYSTEM_OID
);
358 struct dom_sid
*sid
= NULL
;
362 if (as_system
!= NULL
) {
363 as_system
->critical
= 0;
366 if (ac
->am_system
|| as_system
) {
367 return acl_childClasses(module
, schema
, sd_msg
, msg
, "allowedChildClassesEffective");
370 /* If we don't have a schema yet, we can't do anything... */
371 if (schema
== NULL
) {
372 ldb_asprintf_errstring(ldb_module_get_ctx(module
), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg
->dn
));
373 return LDB_ERR_OPERATIONS_ERROR
;
376 /* Must remove any existing attribute, or else confusion reins */
377 ldb_msg_remove_attr(msg
, "allowedChildClassesEffective");
379 oc_el
= ldb_msg_find_element(sd_msg
, "objectClass");
380 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module
), msg
, sd_msg
, &sd
);
381 if (ret
!= LDB_SUCCESS
) {
385 sid
= samdb_result_dom_sid(msg
, sd_msg
, "objectSid");
386 for (i
=0; oc_el
&& i
< oc_el
->num_values
; i
++) {
387 sclass
= dsdb_class_by_lDAPDisplayName_ldb_val(schema
, &oc_el
->values
[i
]);
389 /* We don't know this class? what is going on? */
393 for (j
=0; sclass
->possibleInferiors
&& sclass
->possibleInferiors
[j
]; j
++) {
394 const struct dsdb_class
*sc
;
396 sc
= dsdb_class_by_lDAPDisplayName(schema
,
397 sclass
->possibleInferiors
[j
]);
399 /* We don't know this class? what is going on? */
403 ret
= acl_check_access_on_objectclass(module
, ac
,
405 SEC_ADS_CREATE_CHILD
,
407 if (ret
== LDB_SUCCESS
) {
408 ldb_msg_add_string(msg
, "allowedChildClassesEffective",
409 sclass
->possibleInferiors
[j
]);
413 allowedClasses
= ldb_msg_find_element(msg
, "allowedChildClassesEffective");
414 if (!allowedClasses
) {
418 if (allowedClasses
->num_values
> 1) {
419 TYPESAFE_QSORT(allowedClasses
->values
, allowedClasses
->num_values
, data_blob_cmp
);
420 for (i
=1 ; i
< allowedClasses
->num_values
; i
++) {
421 struct ldb_val
*val1
= &allowedClasses
->values
[i
-1];
422 struct ldb_val
*val2
= &allowedClasses
->values
[i
];
423 if (data_blob_cmp(val1
, val2
) == 0) {
424 memmove(val1
, val2
, (allowedClasses
->num_values
- i
) * sizeof( struct ldb_val
));
425 allowedClasses
->num_values
--;
433 static int acl_sDRightsEffective(struct ldb_module
*module
,
434 struct ldb_message
*sd_msg
,
435 struct ldb_message
*msg
,
436 struct acl_context
*ac
)
438 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
439 struct ldb_message_element
*rightsEffective
;
441 struct security_descriptor
*sd
;
442 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
443 LDB_CONTROL_AS_SYSTEM_OID
);
444 struct dom_sid
*sid
= NULL
;
447 if (as_system
!= NULL
) {
448 as_system
->critical
= 0;
451 /* Must remove any existing attribute, or else confusion reins */
452 ldb_msg_remove_attr(msg
, "sDRightsEffective");
453 ret
= ldb_msg_add_empty(msg
, "sDRightsEffective", 0, &rightsEffective
);
454 if (ret
!= LDB_SUCCESS
) {
457 if (ac
->am_system
|| as_system
) {
458 flags
= SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_SACL
| SECINFO_DACL
;
460 const struct dsdb_class
*objectclass
;
461 const struct dsdb_attribute
*attr
;
463 objectclass
= dsdb_get_structural_oc_from_msg(ac
->schema
, sd_msg
);
464 if (objectclass
== NULL
) {
465 return ldb_operr(ldb
);
468 attr
= dsdb_attribute_by_lDAPDisplayName(ac
->schema
,
469 "nTSecurityDescriptor");
471 return ldb_operr(ldb
);
474 /* Get the security descriptor from the message */
475 ret
= dsdb_get_sd_from_ldb_message(ldb
, msg
, sd_msg
, &sd
);
476 if (ret
!= LDB_SUCCESS
) {
479 sid
= samdb_result_dom_sid(msg
, sd_msg
, "objectSid");
480 ret
= acl_check_access_on_attribute(module
,
487 if (ret
== LDB_SUCCESS
) {
488 flags
|= SECINFO_OWNER
| SECINFO_GROUP
;
490 ret
= acl_check_access_on_attribute(module
,
497 if (ret
== LDB_SUCCESS
) {
498 flags
|= SECINFO_DACL
;
500 ret
= acl_check_access_on_attribute(module
,
504 SEC_FLAG_SYSTEM_SECURITY
,
507 if (ret
== LDB_SUCCESS
) {
508 flags
|= SECINFO_SACL
;
511 return samdb_msg_add_uint(ldb_module_get_ctx(module
), msg
, msg
,
512 "sDRightsEffective", flags
);
515 static int acl_validate_spn_value(TALLOC_CTX
*mem_ctx
,
516 struct ldb_context
*ldb
,
517 const char *spn_value
,
518 uint32_t userAccountControl
,
519 const char *samAccountName
,
520 const char *dnsHostName
,
521 const char *netbios_name
,
522 const char *ntds_guid
)
525 krb5_context krb_ctx
;
526 krb5_error_code kerr
;
527 krb5_principal principal
;
531 const char *forest_name
= samdb_forest_name(ldb
, mem_ctx
);
532 const char *base_domain
= samdb_default_domain_name(ldb
, mem_ctx
);
533 struct loadparm_context
*lp_ctx
= talloc_get_type(ldb_get_opaque(ldb
, "loadparm"),
534 struct loadparm_context
);
535 bool is_dc
= (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) ||
536 (userAccountControl
& UF_PARTIAL_SECRETS_ACCOUNT
);
538 if (strcasecmp_m(spn_value
, samAccountName
) == 0) {
539 /* MacOS X sets this value, and setting an SPN of your
540 * own samAccountName is both pointless and safe */
544 kerr
= smb_krb5_init_context_basic(mem_ctx
,
548 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
549 "Could not initialize kerberos context.");
552 ret
= krb5_parse_name(krb_ctx
, spn_value
, &principal
);
554 krb5_free_context(krb_ctx
);
555 return LDB_ERR_CONSTRAINT_VIOLATION
;
558 princ_size
= krb5_princ_size(krb_ctx
, principal
);
559 if (princ_size
< 2) {
560 DBG_WARNING("princ_size=%d\n", princ_size
);
564 instanceName
= smb_krb5_principal_get_comp_string(mem_ctx
, krb_ctx
,
566 serviceType
= smb_krb5_principal_get_comp_string(mem_ctx
, krb_ctx
,
568 if (krb5_princ_size(krb_ctx
, principal
) == 3) {
569 serviceName
= smb_krb5_principal_get_comp_string(mem_ctx
, krb_ctx
,
577 DBG_WARNING("is_dc=false, serviceName=%s,"
578 "serviceType=%s\n", serviceName
,
582 if (strcasecmp(serviceType
, "ldap") == 0) {
583 if (strcasecmp(serviceName
, netbios_name
) != 0 &&
584 strcasecmp(serviceName
, forest_name
) != 0) {
585 DBG_WARNING("serviceName=%s\n", serviceName
);
589 } else if (strcasecmp(serviceType
, "gc") == 0) {
590 if (strcasecmp(serviceName
, forest_name
) != 0) {
591 DBG_WARNING("serviceName=%s\n", serviceName
);
595 if (strcasecmp(serviceName
, base_domain
) != 0 &&
596 strcasecmp(serviceName
, netbios_name
) != 0) {
597 DBG_WARNING("serviceType=%s, "
599 serviceType
, serviceName
);
604 /* instanceName can be samAccountName without $ or dnsHostName
605 * or "ntds_guid._msdcs.forest_domain for DC objects */
606 if (strlen(instanceName
) == (strlen(samAccountName
) - 1)
607 && strncasecmp(instanceName
, samAccountName
,
608 strlen(samAccountName
) - 1) == 0) {
611 if ((dnsHostName
!= NULL
) &&
612 (strcasecmp(instanceName
, dnsHostName
) == 0)) {
616 const char *guid_str
;
617 guid_str
= talloc_asprintf(mem_ctx
,"%s._msdcs.%s",
620 if (strcasecmp(instanceName
, guid_str
) == 0) {
626 krb5_free_principal(krb_ctx
, principal
);
627 krb5_free_context(krb_ctx
);
628 ldb_debug_set(ldb
, LDB_DEBUG_WARNING
,
629 "acl: spn validation failed for "
630 "spn[%s] uac[0x%x] account[%s] hostname[%s] "
631 "nbname[%s] ntds[%s] forest[%s] domain[%s]\n",
632 spn_value
, (unsigned)userAccountControl
,
633 samAccountName
, dnsHostName
,
634 netbios_name
, ntds_guid
,
635 forest_name
, base_domain
);
636 return LDB_ERR_CONSTRAINT_VIOLATION
;
639 krb5_free_principal(krb_ctx
, principal
);
640 krb5_free_context(krb_ctx
);
644 static int acl_check_spn(TALLOC_CTX
*mem_ctx
,
645 struct ldb_module
*module
,
646 struct ldb_request
*req
,
647 struct security_descriptor
*sd
,
649 const struct dsdb_attribute
*attr
,
650 const struct dsdb_class
*objectclass
)
654 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
655 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
656 struct ldb_result
*acl_res
;
657 struct ldb_result
*netbios_res
;
658 struct ldb_message_element
*el
;
659 struct ldb_dn
*partitions_dn
= samdb_partitions_dn(ldb
, tmp_ctx
);
660 uint32_t userAccountControl
;
661 const char *samAccountName
;
662 const char *dnsHostName
;
663 const char *netbios_name
;
665 char *ntds_guid
= NULL
;
667 static const char *acl_attrs
[] = {
670 "userAccountControl",
673 static const char *netbios_attrs
[] = {
678 /* if we have wp, we can do whatever we like */
679 if (acl_check_access_on_attribute(module
,
684 attr
, objectclass
) == LDB_SUCCESS
) {
685 talloc_free(tmp_ctx
);
689 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
690 GUID_DRS_VALIDATE_SPN
,
694 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
695 dsdb_acl_debug(sd
, acl_user_token(module
),
696 req
->op
.mod
.message
->dn
,
699 talloc_free(tmp_ctx
);
703 ret
= dsdb_module_search_dn(module
, tmp_ctx
,
704 &acl_res
, req
->op
.mod
.message
->dn
,
706 DSDB_FLAG_NEXT_MODULE
|
707 DSDB_FLAG_AS_SYSTEM
|
708 DSDB_SEARCH_SHOW_RECYCLED
,
710 if (ret
!= LDB_SUCCESS
) {
711 talloc_free(tmp_ctx
);
715 userAccountControl
= ldb_msg_find_attr_as_uint(acl_res
->msgs
[0], "userAccountControl", 0);
716 dnsHostName
= ldb_msg_find_attr_as_string(acl_res
->msgs
[0], "dnsHostName", NULL
);
717 samAccountName
= ldb_msg_find_attr_as_string(acl_res
->msgs
[0], "samAccountName", NULL
);
719 ret
= dsdb_module_search(module
, tmp_ctx
,
720 &netbios_res
, partitions_dn
,
723 DSDB_FLAG_NEXT_MODULE
|
727 ldb_dn_get_linearized(ldb_get_default_basedn(ldb
)));
729 netbios_name
= ldb_msg_find_attr_as_string(netbios_res
->msgs
[0], "nETBIOSName", NULL
);
731 el
= ldb_msg_find_element(req
->op
.mod
.message
, "servicePrincipalName");
733 talloc_free(tmp_ctx
);
734 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
735 "Error finding element for servicePrincipalName.");
738 /* NTDSDSA objectGuid of object we are checking SPN for */
739 if (userAccountControl
& (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
740 ret
= dsdb_module_find_ntdsguid_for_computer(module
, tmp_ctx
,
741 req
->op
.mod
.message
->dn
, &ntds
, req
);
742 if (ret
!= LDB_SUCCESS
) {
743 ldb_asprintf_errstring(ldb
, "Failed to find NTDSDSA objectGuid for %s: %s",
744 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
),
746 talloc_free(tmp_ctx
);
747 return LDB_ERR_OPERATIONS_ERROR
;
749 ntds_guid
= GUID_string(tmp_ctx
, &ntds
);
752 for (i
=0; i
< el
->num_values
; i
++) {
753 ret
= acl_validate_spn_value(tmp_ctx
,
755 (char *)el
->values
[i
].data
,
761 if (ret
!= LDB_SUCCESS
) {
762 talloc_free(tmp_ctx
);
766 talloc_free(tmp_ctx
);
770 static int acl_add(struct ldb_module
*module
, struct ldb_request
*req
)
773 struct ldb_dn
*parent
;
774 struct ldb_context
*ldb
;
775 const struct dsdb_schema
*schema
;
776 const struct dsdb_class
*objectclass
;
777 struct ldb_control
*as_system
;
778 struct ldb_message_element
*el
;
779 unsigned int instanceType
= 0;
781 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
782 return ldb_next_request(module
, req
);
785 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
786 if (as_system
!= NULL
) {
787 as_system
->critical
= 0;
790 if (dsdb_module_am_system(module
) || as_system
) {
791 return ldb_next_request(module
, req
);
794 ldb
= ldb_module_get_ctx(module
);
796 parent
= ldb_dn_get_parent(req
, req
->op
.add
.message
->dn
);
797 if (parent
== NULL
) {
801 schema
= dsdb_get_schema(ldb
, req
);
803 return ldb_operr(ldb
);
806 objectclass
= dsdb_get_structural_oc_from_msg(schema
, req
->op
.add
.message
);
808 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
809 "acl: unable to find or validate structural objectClass on %s\n",
810 ldb_dn_get_linearized(req
->op
.add
.message
->dn
));
811 return ldb_module_done(req
, NULL
, NULL
, LDB_ERR_OPERATIONS_ERROR
);
814 el
= ldb_msg_find_element(req
->op
.add
.message
, "instanceType");
815 if ((el
!= NULL
) && (el
->num_values
!= 1)) {
816 ldb_set_errstring(ldb
, "acl: the 'instanceType' attribute is single-valued!");
817 return LDB_ERR_UNWILLING_TO_PERFORM
;
820 instanceType
= ldb_msg_find_attr_as_uint(req
->op
.add
.message
,
822 if (instanceType
& INSTANCE_TYPE_IS_NC_HEAD
) {
823 static const char *no_attrs
[] = { NULL
};
824 struct ldb_result
*partition_res
;
825 struct ldb_dn
*partitions_dn
;
827 partitions_dn
= samdb_partitions_dn(ldb
, req
);
828 if (!partitions_dn
) {
829 ldb_set_errstring(ldb
, "acl: CN=partitions dn could not be generated!");
830 return LDB_ERR_UNWILLING_TO_PERFORM
;
833 ret
= dsdb_module_search(module
, req
, &partition_res
,
834 partitions_dn
, LDB_SCOPE_ONELEVEL
,
836 DSDB_FLAG_NEXT_MODULE
|
837 DSDB_FLAG_AS_SYSTEM
|
838 DSDB_SEARCH_ONE_ONLY
|
839 DSDB_SEARCH_SHOW_RECYCLED
,
841 "(&(nCName=%s)(objectClass=crossRef))",
842 ldb_dn_get_linearized(req
->op
.add
.message
->dn
));
844 if (ret
== LDB_SUCCESS
) {
845 /* Check that we can write to the crossRef object MS-ADTS 3.1.1.5.2.8.2 */
846 ret
= dsdb_module_check_access_on_dn(module
, req
, partition_res
->msgs
[0]->dn
,
848 &objectclass
->schemaIDGUID
, req
);
849 if (ret
!= LDB_SUCCESS
) {
850 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
851 "acl: ACL check failed on crossRef object %s: %s\n",
852 ldb_dn_get_linearized(partition_res
->msgs
[0]->dn
),
858 * TODO: Remaining checks, like if we are
859 * the naming master etc need to be handled
860 * in the instanceType module
862 return ldb_next_request(module
, req
);
865 /* Check that we can create a crossRef object MS-ADTS 3.1.1.5.2.8.2 */
866 ret
= dsdb_module_check_access_on_dn(module
, req
, partitions_dn
,
867 SEC_ADS_CREATE_CHILD
,
868 &objectclass
->schemaIDGUID
, req
);
869 if (ret
== LDB_ERR_NO_SUCH_OBJECT
&&
870 ldb_request_get_control(req
, LDB_CONTROL_RELAX_OID
))
872 /* Allow provision bootstrap */
875 if (ret
!= LDB_SUCCESS
) {
876 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
877 "acl: ACL check failed on CN=Partitions crossRef container %s: %s\n",
878 ldb_dn_get_linearized(partitions_dn
), ldb_errstring(ldb
));
883 * TODO: Remaining checks, like if we are the naming
884 * master and adding the crossRef object need to be
885 * handled in the instanceType module
887 return ldb_next_request(module
, req
);
890 ret
= dsdb_module_check_access_on_dn(module
, req
, parent
,
891 SEC_ADS_CREATE_CHILD
,
892 &objectclass
->schemaIDGUID
, req
);
893 if (ret
!= LDB_SUCCESS
) {
894 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
895 "acl: unable to get access to %s\n",
896 ldb_dn_get_linearized(req
->op
.add
.message
->dn
));
899 return ldb_next_request(module
, req
);
902 /* ckecks if modifications are allowed on "Member" attribute */
903 static int acl_check_self_membership(TALLOC_CTX
*mem_ctx
,
904 struct ldb_module
*module
,
905 struct ldb_request
*req
,
906 struct security_descriptor
*sd
,
908 const struct dsdb_attribute
*attr
,
909 const struct dsdb_class
*objectclass
)
913 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
914 struct ldb_dn
*user_dn
;
915 struct ldb_message_element
*member_el
;
916 /* if we have wp, we can do whatever we like */
917 if (acl_check_access_on_attribute(module
,
922 attr
, objectclass
) == LDB_SUCCESS
) {
925 /* if we are adding/deleting ourselves, check for self membership */
926 ret
= dsdb_find_dn_by_sid(ldb
, mem_ctx
,
927 &acl_user_token(module
)->sids
[PRIMARY_USER_SID_INDEX
],
929 if (ret
!= LDB_SUCCESS
) {
932 member_el
= ldb_msg_find_element(req
->op
.mod
.message
, "member");
934 return ldb_operr(ldb
);
936 /* user can only remove oneself */
937 if (member_el
->num_values
== 0) {
938 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
940 for (i
= 0; i
< member_el
->num_values
; i
++) {
941 if (strcasecmp((const char *)member_el
->values
[i
].data
,
942 ldb_dn_get_extended_linearized(mem_ctx
, user_dn
, 1)) != 0) {
943 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
946 ret
= acl_check_extended_right(mem_ctx
, sd
, acl_user_token(module
),
947 GUID_DRS_SELF_MEMBERSHIP
,
950 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
951 dsdb_acl_debug(sd
, acl_user_token(module
),
952 req
->op
.mod
.message
->dn
,
959 static int acl_check_password_rights(TALLOC_CTX
*mem_ctx
,
960 struct ldb_module
*module
,
961 struct ldb_request
*req
,
962 struct security_descriptor
*sd
,
964 const struct dsdb_class
*objectclass
,
967 int ret
= LDB_SUCCESS
;
968 unsigned int del_attr_cnt
= 0, add_attr_cnt
= 0, rep_attr_cnt
= 0;
969 unsigned int del_val_cnt
= 0, add_val_cnt
= 0, rep_val_cnt
= 0;
970 struct ldb_message_element
*el
;
971 struct ldb_message
*msg
;
972 struct ldb_control
*c
= NULL
;
973 const char *passwordAttrs
[] = { "userPassword", "clearTextPassword",
974 "unicodePwd", NULL
}, **l
;
975 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
976 struct dsdb_control_password_acl_validation
*pav
= NULL
;
978 if (tmp_ctx
== NULL
) {
979 return LDB_ERR_OPERATIONS_ERROR
;
982 pav
= talloc_zero(req
, struct dsdb_control_password_acl_validation
);
984 talloc_free(tmp_ctx
);
985 return LDB_ERR_OPERATIONS_ERROR
;
988 c
= ldb_request_get_control(req
, DSDB_CONTROL_PASSWORD_CHANGE_OID
);
990 pav
->pwd_reset
= false;
993 * The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
994 * have a user password change and not a set as the message
995 * looks like. In it's value blob it contains the NT and/or LM
996 * hash of the old password specified by the user. This control
997 * is used by the SAMR and "kpasswd" password change mechanisms.
999 * This control can't be used by real LDAP clients,
1000 * the only caller is samdb_set_password_internal(),
1001 * so we don't have to strict verification of the input.
1003 ret
= acl_check_extended_right(tmp_ctx
,
1005 acl_user_token(module
),
1006 GUID_DRS_USER_CHANGE_PASSWORD
,
1007 SEC_ADS_CONTROL_ACCESS
,
1012 c
= ldb_request_get_control(req
, DSDB_CONTROL_PASSWORD_HASH_VALUES_OID
);
1014 pav
->pwd_reset
= true;
1017 * The "DSDB_CONTROL_PASSWORD_HASH_VALUES_OID" control, without
1018 * "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
1019 * have a force password set.
1020 * This control is used by the SAMR/NETLOGON/LSA password
1023 * This control can't be used by real LDAP clients,
1024 * the only caller is samdb_set_password_internal(),
1025 * so we don't have to strict verification of the input.
1027 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
1028 GUID_DRS_FORCE_CHANGE_PASSWORD
,
1029 SEC_ADS_CONTROL_ACCESS
,
1034 el
= ldb_msg_find_element(req
->op
.mod
.message
, "dBCSPwd");
1037 * dBCSPwd is only allowed with a control.
1039 talloc_free(tmp_ctx
);
1040 return LDB_ERR_UNWILLING_TO_PERFORM
;
1043 msg
= ldb_msg_copy_shallow(tmp_ctx
, req
->op
.mod
.message
);
1045 return ldb_module_oom(module
);
1047 for (l
= passwordAttrs
; *l
!= NULL
; l
++) {
1048 if ((!userPassword
) && (ldb_attr_cmp(*l
, "userPassword") == 0)) {
1052 while ((el
= ldb_msg_find_element(msg
, *l
)) != NULL
) {
1053 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
) {
1055 del_val_cnt
+= el
->num_values
;
1057 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_ADD
) {
1059 add_val_cnt
+= el
->num_values
;
1061 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_REPLACE
) {
1063 rep_val_cnt
+= el
->num_values
;
1065 ldb_msg_remove_element(msg
, el
);
1069 /* single deletes will be handled by the "password_hash" LDB module
1070 * later in the stack, so we let it though here */
1071 if ((del_attr_cnt
> 0) && (add_attr_cnt
== 0) && (rep_attr_cnt
== 0)) {
1072 talloc_free(tmp_ctx
);
1077 if (rep_attr_cnt
> 0) {
1078 pav
->pwd_reset
= true;
1080 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
1081 GUID_DRS_FORCE_CHANGE_PASSWORD
,
1082 SEC_ADS_CONTROL_ACCESS
,
1087 if (add_attr_cnt
!= del_attr_cnt
) {
1088 pav
->pwd_reset
= true;
1090 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
1091 GUID_DRS_FORCE_CHANGE_PASSWORD
,
1092 SEC_ADS_CONTROL_ACCESS
,
1097 if (add_val_cnt
== 1 && del_val_cnt
== 1) {
1098 pav
->pwd_reset
= false;
1100 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
1101 GUID_DRS_USER_CHANGE_PASSWORD
,
1102 SEC_ADS_CONTROL_ACCESS
,
1104 /* Very strange, but we get constraint violation in this case */
1105 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
1106 ret
= LDB_ERR_CONSTRAINT_VIOLATION
;
1111 if (add_val_cnt
== 1 && del_val_cnt
== 0) {
1112 pav
->pwd_reset
= true;
1114 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
1115 GUID_DRS_FORCE_CHANGE_PASSWORD
,
1116 SEC_ADS_CONTROL_ACCESS
,
1118 /* Very strange, but we get constraint violation in this case */
1119 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
1120 ret
= LDB_ERR_CONSTRAINT_VIOLATION
;
1126 * Everything else is handled by the password_hash module where it will
1127 * fail, but with the correct error code when the module is again
1128 * checking the attributes. As the change request will lack the
1129 * DSDB_CONTROL_PASSWORD_ACL_VALIDATION_OID control, we can be sure that
1130 * any modification attempt that went this way will be rejected.
1133 talloc_free(tmp_ctx
);
1137 if (ret
!= LDB_SUCCESS
) {
1138 dsdb_acl_debug(sd
, acl_user_token(module
),
1139 req
->op
.mod
.message
->dn
,
1142 talloc_free(tmp_ctx
);
1146 ret
= ldb_request_add_control(req
,
1147 DSDB_CONTROL_PASSWORD_ACL_VALIDATION_OID
, false, pav
);
1148 if (ret
!= LDB_SUCCESS
) {
1149 ldb_debug(ldb_module_get_ctx(module
), LDB_DEBUG_ERROR
,
1150 "Unable to register ACL validation control!\n");
1156 static int acl_modify(struct ldb_module
*module
, struct ldb_request
*req
)
1159 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
1160 const struct dsdb_schema
*schema
;
1162 const struct dsdb_class
*objectclass
;
1163 struct ldb_result
*acl_res
;
1164 struct security_descriptor
*sd
;
1165 struct dom_sid
*sid
= NULL
;
1166 struct ldb_control
*as_system
;
1167 struct ldb_control
*is_undelete
;
1169 bool password_rights_checked
= false;
1170 TALLOC_CTX
*tmp_ctx
;
1171 const struct ldb_message
*msg
= req
->op
.mod
.message
;
1172 static const char *acl_attrs
[] = {
1173 "nTSecurityDescriptor",
1179 if (ldb_dn_is_special(msg
->dn
)) {
1180 return ldb_next_request(module
, req
);
1183 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1184 if (as_system
!= NULL
) {
1185 as_system
->critical
= 0;
1188 is_undelete
= ldb_request_get_control(req
, DSDB_CONTROL_RESTORE_TOMBSTONE_OID
);
1190 /* Don't print this debug statement if elements[0].name is going to be NULL */
1191 if (msg
->num_elements
> 0) {
1192 DEBUG(10, ("ldb:acl_modify: %s\n", msg
->elements
[0].name
));
1194 if (dsdb_module_am_system(module
) || as_system
) {
1195 return ldb_next_request(module
, req
);
1198 tmp_ctx
= talloc_new(req
);
1199 if (tmp_ctx
== NULL
) {
1200 return ldb_oom(ldb
);
1203 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &acl_res
, msg
->dn
,
1205 DSDB_FLAG_NEXT_MODULE
|
1206 DSDB_FLAG_AS_SYSTEM
|
1207 DSDB_SEARCH_SHOW_RECYCLED
,
1210 if (ret
!= LDB_SUCCESS
) {
1214 userPassword
= dsdb_user_password_support(module
, req
, req
);
1216 schema
= dsdb_get_schema(ldb
, tmp_ctx
);
1218 talloc_free(tmp_ctx
);
1219 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1220 "acl_modify: Error obtaining schema.");
1223 ret
= dsdb_get_sd_from_ldb_message(ldb
, tmp_ctx
, acl_res
->msgs
[0], &sd
);
1224 if (ret
!= LDB_SUCCESS
) {
1225 talloc_free(tmp_ctx
);
1226 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1227 "acl_modify: Error retrieving security descriptor.");
1229 /* Theoretically we pass the check if the object has no sd */
1234 objectclass
= dsdb_get_structural_oc_from_msg(schema
, acl_res
->msgs
[0]);
1236 talloc_free(tmp_ctx
);
1237 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1238 "acl_modify: Error retrieving object class for GUID.");
1240 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1241 for (i
=0; i
< msg
->num_elements
; i
++) {
1242 const struct ldb_message_element
*el
= &msg
->elements
[i
];
1243 const struct dsdb_attribute
*attr
;
1246 * This basic attribute existence check with the right errorcode
1247 * is needed since this module is the first one which requests
1248 * schema attribute information.
1249 * The complete attribute checking is done in the
1250 * "objectclass_attrs" module behind this one.
1252 * NOTE: "clearTextPassword" is not defined in the schema.
1254 attr
= dsdb_attribute_by_lDAPDisplayName(schema
, el
->name
);
1255 if (!attr
&& ldb_attr_cmp("clearTextPassword", el
->name
) != 0) {
1256 ldb_asprintf_errstring(ldb
, "acl_modify: attribute '%s' "
1257 "on entry '%s' was not found in the schema!",
1258 req
->op
.mod
.message
->elements
[i
].name
,
1259 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
));
1260 ret
= LDB_ERR_NO_SUCH_ATTRIBUTE
;
1264 if (ldb_attr_cmp("nTSecurityDescriptor", el
->name
) == 0) {
1265 uint32_t sd_flags
= dsdb_request_sd_flags(req
, NULL
);
1266 uint32_t access_mask
= 0;
1268 if (sd_flags
& (SECINFO_OWNER
|SECINFO_GROUP
)) {
1269 access_mask
|= SEC_STD_WRITE_OWNER
;
1271 if (sd_flags
& SECINFO_DACL
) {
1272 access_mask
|= SEC_STD_WRITE_DAC
;
1274 if (sd_flags
& SECINFO_SACL
) {
1275 access_mask
|= SEC_FLAG_SYSTEM_SECURITY
;
1278 ret
= acl_check_access_on_attribute(module
,
1285 if (ret
!= LDB_SUCCESS
) {
1286 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1287 "Object %s has no write dacl access\n",
1288 ldb_dn_get_linearized(msg
->dn
));
1290 acl_user_token(module
),
1294 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1297 } else if (ldb_attr_cmp("member", el
->name
) == 0) {
1298 ret
= acl_check_self_membership(tmp_ctx
,
1305 if (ret
!= LDB_SUCCESS
) {
1308 } else if (ldb_attr_cmp("dBCSPwd", el
->name
) == 0) {
1309 /* this one is not affected by any rights, we should let it through
1310 so that passwords_hash returns the correct error */
1312 } else if (ldb_attr_cmp("unicodePwd", el
->name
) == 0 ||
1313 (userPassword
&& ldb_attr_cmp("userPassword", el
->name
) == 0) ||
1314 ldb_attr_cmp("clearTextPassword", el
->name
) == 0) {
1315 if (password_rights_checked
) {
1318 ret
= acl_check_password_rights(tmp_ctx
,
1325 if (ret
!= LDB_SUCCESS
) {
1328 password_rights_checked
= true;
1329 } else if (ldb_attr_cmp("servicePrincipalName", el
->name
) == 0) {
1330 ret
= acl_check_spn(tmp_ctx
,
1337 if (ret
!= LDB_SUCCESS
) {
1340 } else if (is_undelete
!= NULL
&& (ldb_attr_cmp("isDeleted", el
->name
) == 0)) {
1342 * in case of undelete op permissions on
1343 * isDeleted are irrelevant and
1344 * distinguishedName is removed by the
1345 * tombstone_reanimate module
1349 ret
= acl_check_access_on_attribute(module
,
1356 if (ret
!= LDB_SUCCESS
) {
1357 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1358 "Object %s has no write property access\n",
1359 ldb_dn_get_linearized(msg
->dn
));
1361 acl_user_token(module
),
1365 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1372 talloc_free(tmp_ctx
);
1373 return ldb_next_request(module
, req
);
1375 talloc_free(tmp_ctx
);
1379 /* similar to the modify for the time being.
1380 * We need to consider the special delete tree case, though - TODO */
1381 static int acl_delete(struct ldb_module
*module
, struct ldb_request
*req
)
1384 struct ldb_dn
*parent
;
1385 struct ldb_context
*ldb
;
1386 struct ldb_dn
*nc_root
;
1387 struct ldb_control
*as_system
;
1388 const struct dsdb_schema
*schema
;
1389 const struct dsdb_class
*objectclass
;
1390 struct security_descriptor
*sd
= NULL
;
1391 struct dom_sid
*sid
= NULL
;
1392 struct ldb_result
*acl_res
;
1393 static const char *acl_attrs
[] = {
1394 "nTSecurityDescriptor",
1400 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
1401 return ldb_next_request(module
, req
);
1404 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1405 if (as_system
!= NULL
) {
1406 as_system
->critical
= 0;
1409 if (dsdb_module_am_system(module
) || as_system
) {
1410 return ldb_next_request(module
, req
);
1413 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req
->op
.del
.dn
)));
1415 ldb
= ldb_module_get_ctx(module
);
1417 parent
= ldb_dn_get_parent(req
, req
->op
.del
.dn
);
1418 if (parent
== NULL
) {
1419 return ldb_oom(ldb
);
1422 /* Make sure we aren't deleting a NC */
1424 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.del
.dn
, &nc_root
);
1425 if (ret
!= LDB_SUCCESS
) {
1428 if (ldb_dn_compare(nc_root
, req
->op
.del
.dn
) == 0) {
1429 talloc_free(nc_root
);
1430 DEBUG(10,("acl:deleting a NC\n"));
1431 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1432 return ldb_module_done(req
, NULL
, NULL
,
1433 LDB_ERR_UNWILLING_TO_PERFORM
);
1435 talloc_free(nc_root
);
1437 ret
= dsdb_module_search_dn(module
, req
, &acl_res
,
1438 req
->op
.del
.dn
, acl_attrs
,
1439 DSDB_FLAG_NEXT_MODULE
|
1440 DSDB_FLAG_AS_SYSTEM
|
1441 DSDB_SEARCH_SHOW_RECYCLED
, req
);
1442 /* we sould be able to find the parent */
1443 if (ret
!= LDB_SUCCESS
) {
1444 DEBUG(10,("acl: failed to find object %s\n",
1445 ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1449 ret
= dsdb_get_sd_from_ldb_message(ldb
, req
, acl_res
->msgs
[0], &sd
);
1450 if (ret
!= LDB_SUCCESS
) {
1451 return ldb_operr(ldb
);
1454 return ldb_operr(ldb
);
1457 schema
= dsdb_get_schema(ldb
, req
);
1459 return ldb_operr(ldb
);
1462 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1464 objectclass
= dsdb_get_structural_oc_from_msg(schema
, acl_res
->msgs
[0]);
1466 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1467 "acl_modify: Error retrieving object class for GUID.");
1470 if (ldb_request_get_control(req
, LDB_CONTROL_TREE_DELETE_OID
)) {
1471 ret
= acl_check_access_on_objectclass(module
, req
, sd
, sid
,
1472 SEC_ADS_DELETE_TREE
,
1474 if (ret
!= LDB_SUCCESS
) {
1478 return ldb_next_request(module
, req
);
1481 /* First check if we have delete object right */
1482 ret
= acl_check_access_on_objectclass(module
, req
, sd
, sid
,
1485 if (ret
== LDB_SUCCESS
) {
1486 return ldb_next_request(module
, req
);
1489 /* Nope, we don't have delete object. Lets check if we have delete
1490 * child on the parent */
1491 ret
= dsdb_module_check_access_on_dn(module
, req
, parent
,
1492 SEC_ADS_DELETE_CHILD
,
1493 &objectclass
->schemaIDGUID
,
1495 if (ret
!= LDB_SUCCESS
) {
1499 return ldb_next_request(module
, req
);
1501 static int acl_check_reanimate_tombstone(TALLOC_CTX
*mem_ctx
,
1502 struct ldb_module
*module
,
1503 struct ldb_request
*req
,
1504 struct ldb_dn
*nc_root
)
1507 struct ldb_result
*acl_res
;
1508 struct security_descriptor
*sd
= NULL
;
1509 struct dom_sid
*sid
= NULL
;
1510 static const char *acl_attrs
[] = {
1511 "nTSecurityDescriptor",
1517 ret
= dsdb_module_search_dn(module
, mem_ctx
, &acl_res
,
1519 DSDB_FLAG_NEXT_MODULE
|
1520 DSDB_FLAG_AS_SYSTEM
|
1521 DSDB_SEARCH_SHOW_RECYCLED
, req
);
1522 if (ret
!= LDB_SUCCESS
) {
1523 DEBUG(10,("acl: failed to find object %s\n",
1524 ldb_dn_get_linearized(nc_root
)));
1528 ret
= dsdb_get_sd_from_ldb_message(mem_ctx
, req
, acl_res
->msgs
[0], &sd
);
1529 sid
= samdb_result_dom_sid(mem_ctx
, acl_res
->msgs
[0], "objectSid");
1530 if (ret
!= LDB_SUCCESS
|| !sd
) {
1531 return ldb_operr(ldb_module_get_ctx(module
));
1533 return acl_check_extended_right(mem_ctx
, sd
, acl_user_token(module
),
1534 GUID_DRS_REANIMATE_TOMBSTONE
,
1535 SEC_ADS_CONTROL_ACCESS
, sid
);
1538 static int acl_rename(struct ldb_module
*module
, struct ldb_request
*req
)
1541 struct ldb_dn
*oldparent
;
1542 struct ldb_dn
*newparent
;
1543 const struct dsdb_schema
*schema
;
1544 const struct dsdb_class
*objectclass
;
1545 const struct dsdb_attribute
*attr
= NULL
;
1546 struct ldb_context
*ldb
;
1547 struct security_descriptor
*sd
= NULL
;
1548 struct dom_sid
*sid
= NULL
;
1549 struct ldb_result
*acl_res
;
1550 struct ldb_dn
*nc_root
;
1551 struct ldb_control
*as_system
;
1552 struct ldb_control
*is_undelete
;
1553 TALLOC_CTX
*tmp_ctx
;
1554 const char *rdn_name
;
1555 static const char *acl_attrs
[] = {
1556 "nTSecurityDescriptor",
1562 if (ldb_dn_is_special(req
->op
.rename
.olddn
)) {
1563 return ldb_next_request(module
, req
);
1566 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1567 if (as_system
!= NULL
) {
1568 as_system
->critical
= 0;
1571 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1572 if (dsdb_module_am_system(module
) || as_system
) {
1573 return ldb_next_request(module
, req
);
1576 ldb
= ldb_module_get_ctx(module
);
1578 tmp_ctx
= talloc_new(req
);
1579 if (tmp_ctx
== NULL
) {
1580 return ldb_oom(ldb
);
1583 oldparent
= ldb_dn_get_parent(tmp_ctx
, req
->op
.rename
.olddn
);
1584 if (oldparent
== NULL
) {
1585 return ldb_oom(ldb
);
1587 newparent
= ldb_dn_get_parent(tmp_ctx
, req
->op
.rename
.newdn
);
1588 if (newparent
== NULL
) {
1589 return ldb_oom(ldb
);
1592 /* Make sure we aren't renaming/moving a NC */
1594 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.rename
.olddn
, &nc_root
);
1595 if (ret
!= LDB_SUCCESS
) {
1598 if (ldb_dn_compare(nc_root
, req
->op
.rename
.olddn
) == 0) {
1599 talloc_free(nc_root
);
1600 DEBUG(10,("acl:renaming/moving a NC\n"));
1601 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1602 return ldb_module_done(req
, NULL
, NULL
,
1603 LDB_ERR_UNWILLING_TO_PERFORM
);
1606 /* special check for undelete operation */
1607 is_undelete
= ldb_request_get_control(req
, DSDB_CONTROL_RESTORE_TOMBSTONE_OID
);
1608 if (is_undelete
!= NULL
) {
1609 is_undelete
->critical
= 0;
1610 ret
= acl_check_reanimate_tombstone(tmp_ctx
, module
, req
, nc_root
);
1611 if (ret
!= LDB_SUCCESS
) {
1612 talloc_free(tmp_ctx
);
1616 talloc_free(nc_root
);
1618 /* Look for the parent */
1620 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &acl_res
,
1621 req
->op
.rename
.olddn
, acl_attrs
,
1622 DSDB_FLAG_NEXT_MODULE
|
1623 DSDB_FLAG_AS_SYSTEM
|
1624 DSDB_SEARCH_SHOW_RECYCLED
, req
);
1625 /* we sould be able to find the parent */
1626 if (ret
!= LDB_SUCCESS
) {
1627 DEBUG(10,("acl: failed to find object %s\n",
1628 ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1629 talloc_free(tmp_ctx
);
1633 ret
= dsdb_get_sd_from_ldb_message(ldb
, req
, acl_res
->msgs
[0], &sd
);
1634 if (ret
!= LDB_SUCCESS
) {
1635 talloc_free(tmp_ctx
);
1636 return ldb_operr(ldb
);
1639 talloc_free(tmp_ctx
);
1640 return ldb_operr(ldb
);
1643 schema
= dsdb_get_schema(ldb
, acl_res
);
1645 talloc_free(tmp_ctx
);
1646 return ldb_operr(ldb
);
1649 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1651 objectclass
= dsdb_get_structural_oc_from_msg(schema
, acl_res
->msgs
[0]);
1653 talloc_free(tmp_ctx
);
1654 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1655 "acl_modify: Error retrieving object class for GUID.");
1658 attr
= dsdb_attribute_by_lDAPDisplayName(schema
, "name");
1660 talloc_free(tmp_ctx
);
1661 return ldb_operr(ldb
);
1664 ret
= acl_check_access_on_attribute(module
, tmp_ctx
, sd
, sid
,
1667 if (ret
!= LDB_SUCCESS
) {
1668 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1669 "Object %s has no wp on %s\n",
1670 ldb_dn_get_linearized(req
->op
.rename
.olddn
),
1671 attr
->lDAPDisplayName
);
1673 acl_user_token(module
),
1674 req
->op
.rename
.olddn
,
1677 talloc_free(tmp_ctx
);
1678 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1681 rdn_name
= ldb_dn_get_rdn_name(req
->op
.rename
.olddn
);
1682 if (rdn_name
== NULL
) {
1683 talloc_free(tmp_ctx
);
1684 return ldb_operr(ldb
);
1687 attr
= dsdb_attribute_by_lDAPDisplayName(schema
, rdn_name
);
1689 talloc_free(tmp_ctx
);
1690 return ldb_operr(ldb
);
1693 ret
= acl_check_access_on_attribute(module
, tmp_ctx
, sd
, sid
,
1696 if (ret
!= LDB_SUCCESS
) {
1697 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1698 "Object %s has no wp on %s\n",
1699 ldb_dn_get_linearized(req
->op
.rename
.olddn
),
1700 attr
->lDAPDisplayName
);
1702 acl_user_token(module
),
1703 req
->op
.rename
.olddn
,
1706 talloc_free(tmp_ctx
);
1707 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1710 if (ldb_dn_compare(oldparent
, newparent
) == 0) {
1711 /* regular rename, not move, nothing more to do */
1712 talloc_free(tmp_ctx
);
1713 return ldb_next_request(module
, req
);
1716 /* new parent should have create child */
1717 ret
= dsdb_module_check_access_on_dn(module
, req
, newparent
,
1718 SEC_ADS_CREATE_CHILD
,
1719 &objectclass
->schemaIDGUID
, req
);
1720 if (ret
!= LDB_SUCCESS
) {
1721 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1722 "acl:access_denied renaming %s",
1723 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1724 talloc_free(tmp_ctx
);
1728 /* do we have delete object on the object? */
1729 /* this access is not necessary for undelete ops */
1730 if (is_undelete
== NULL
) {
1731 ret
= acl_check_access_on_objectclass(module
, tmp_ctx
, sd
, sid
,
1734 if (ret
== LDB_SUCCESS
) {
1735 talloc_free(tmp_ctx
);
1736 return ldb_next_request(module
, req
);
1738 /* what about delete child on the current parent */
1739 ret
= dsdb_module_check_access_on_dn(module
, req
, oldparent
,
1740 SEC_ADS_DELETE_CHILD
,
1741 &objectclass
->schemaIDGUID
,
1743 if (ret
!= LDB_SUCCESS
) {
1744 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1745 "acl:access_denied renaming %s", ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1746 talloc_free(tmp_ctx
);
1747 return ldb_module_done(req
, NULL
, NULL
, ret
);
1750 talloc_free(tmp_ctx
);
1752 return ldb_next_request(module
, req
);
1755 static int acl_search_update_confidential_attrs(struct acl_context
*ac
,
1756 struct acl_private
*data
)
1758 struct dsdb_attribute
*a
;
1761 if (data
->acl_search
) {
1763 * If acl:search is activated, the acl_read module
1764 * protects confidential attributes.
1769 if ((ac
->schema
== data
->cached_schema_ptr
) &&
1770 (ac
->schema
->metadata_usn
== data
->cached_schema_metadata_usn
))
1775 data
->cached_schema_ptr
= NULL
;
1776 data
->cached_schema_loaded_usn
= 0;
1777 data
->cached_schema_metadata_usn
= 0;
1778 TALLOC_FREE(data
->confidential_attrs
);
1780 if (ac
->schema
== NULL
) {
1784 for (a
= ac
->schema
->attributes
; a
; a
= a
->next
) {
1785 const char **attrs
= data
->confidential_attrs
;
1787 if (!(a
->searchFlags
& SEARCH_FLAG_CONFIDENTIAL
)) {
1791 attrs
= talloc_realloc(data
, attrs
, const char *, n
+ 2);
1792 if (attrs
== NULL
) {
1793 TALLOC_FREE(data
->confidential_attrs
);
1794 return ldb_module_oom(ac
->module
);
1797 attrs
[n
] = a
->lDAPDisplayName
;
1801 data
->confidential_attrs
= attrs
;
1804 data
->cached_schema_ptr
= ac
->schema
;
1805 data
->cached_schema_metadata_usn
= ac
->schema
->metadata_usn
;
1810 static int acl_search_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1812 struct acl_context
*ac
;
1813 struct acl_private
*data
;
1814 struct ldb_result
*acl_res
;
1815 static const char *acl_attrs
[] = {
1817 "nTSecurityDescriptor",
1824 ac
= talloc_get_type(req
->context
, struct acl_context
);
1825 data
= talloc_get_type(ldb_module_get_private(ac
->module
), struct acl_private
);
1827 return ldb_module_done(ac
->req
, NULL
, NULL
,
1828 LDB_ERR_OPERATIONS_ERROR
);
1830 if (ares
->error
!= LDB_SUCCESS
) {
1831 return ldb_module_done(ac
->req
, ares
->controls
,
1832 ares
->response
, ares
->error
);
1835 switch (ares
->type
) {
1836 case LDB_REPLY_ENTRY
:
1837 if (ac
->constructed_attrs
) {
1838 ret
= dsdb_module_search_dn(ac
->module
, ac
, &acl_res
, ares
->message
->dn
,
1840 DSDB_FLAG_NEXT_MODULE
|
1841 DSDB_FLAG_AS_SYSTEM
|
1842 DSDB_SEARCH_SHOW_RECYCLED
,
1844 if (ret
!= LDB_SUCCESS
) {
1845 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1849 if (ac
->allowedAttributes
|| ac
->allowedAttributesEffective
) {
1850 ret
= acl_allowedAttributes(ac
->module
, ac
->schema
,
1853 if (ret
!= LDB_SUCCESS
) {
1854 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1858 if (ac
->allowedChildClasses
) {
1859 ret
= acl_childClasses(ac
->module
, ac
->schema
,
1862 "allowedChildClasses");
1863 if (ret
!= LDB_SUCCESS
) {
1864 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1868 if (ac
->allowedChildClassesEffective
) {
1869 ret
= acl_childClassesEffective(ac
->module
, ac
->schema
,
1872 if (ret
!= LDB_SUCCESS
) {
1873 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1877 if (ac
->sDRightsEffective
) {
1878 ret
= acl_sDRightsEffective(ac
->module
,
1881 if (ret
!= LDB_SUCCESS
) {
1882 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1887 return ldb_module_send_entry(ac
->req
, ares
->message
,
1891 if (ac
->am_system
) {
1892 return ldb_module_send_entry(ac
->req
, ares
->message
,
1896 if (data
->password_attrs
!= NULL
) {
1897 for (i
= 0; data
->password_attrs
[i
]; i
++) {
1898 if ((!ac
->userPassword
) &&
1899 (ldb_attr_cmp(data
->password_attrs
[i
],
1900 "userPassword") == 0))
1905 ldb_msg_remove_attr(ares
->message
, data
->password_attrs
[i
]);
1909 if (ac
->am_administrator
) {
1910 return ldb_module_send_entry(ac
->req
, ares
->message
,
1914 ret
= acl_search_update_confidential_attrs(ac
, data
);
1915 if (ret
!= LDB_SUCCESS
) {
1919 if (data
->confidential_attrs
!= NULL
) {
1920 for (i
= 0; data
->confidential_attrs
[i
]; i
++) {
1921 ldb_msg_remove_attr(ares
->message
,
1922 data
->confidential_attrs
[i
]);
1926 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
1928 case LDB_REPLY_REFERRAL
:
1929 return ldb_module_send_referral(ac
->req
, ares
->referral
);
1931 case LDB_REPLY_DONE
:
1932 return ldb_module_done(ac
->req
, ares
->controls
,
1933 ares
->response
, LDB_SUCCESS
);
1939 static int acl_search(struct ldb_module
*module
, struct ldb_request
*req
)
1941 struct ldb_context
*ldb
;
1942 struct acl_context
*ac
;
1943 struct ldb_parse_tree
*down_tree
;
1944 struct ldb_request
*down_req
;
1945 struct acl_private
*data
;
1949 if (ldb_dn_is_special(req
->op
.search
.base
)) {
1950 return ldb_next_request(module
, req
);
1953 ldb
= ldb_module_get_ctx(module
);
1955 ac
= talloc_zero(req
, struct acl_context
);
1957 return ldb_oom(ldb
);
1959 data
= talloc_get_type(ldb_module_get_private(module
), struct acl_private
);
1961 ac
->module
= module
;
1963 ac
->am_system
= dsdb_module_am_system(module
);
1964 ac
->am_administrator
= dsdb_module_am_administrator(module
);
1965 ac
->constructed_attrs
= false;
1966 ac
->modify_search
= true;
1967 ac
->allowedAttributes
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedAttributes");
1968 ac
->allowedAttributesEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedAttributesEffective");
1969 ac
->allowedChildClasses
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedChildClasses");
1970 ac
->allowedChildClassesEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedChildClassesEffective");
1971 ac
->sDRightsEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "sDRightsEffective");
1972 ac
->userPassword
= true;
1973 ac
->schema
= dsdb_get_schema(ldb
, ac
);
1975 ac
->constructed_attrs
|= ac
->allowedAttributes
;
1976 ac
->constructed_attrs
|= ac
->allowedChildClasses
;
1977 ac
->constructed_attrs
|= ac
->allowedChildClassesEffective
;
1978 ac
->constructed_attrs
|= ac
->allowedAttributesEffective
;
1979 ac
->constructed_attrs
|= ac
->sDRightsEffective
;
1982 ac
->modify_search
= false;
1984 if (ac
->am_system
) {
1985 ac
->modify_search
= false;
1988 if (!ac
->constructed_attrs
&& !ac
->modify_search
) {
1990 return ldb_next_request(module
, req
);
1993 data
= talloc_get_type(ldb_module_get_private(ac
->module
), struct acl_private
);
1995 ac
->userPassword
= data
->userPassword_support
;
1998 ret
= acl_search_update_confidential_attrs(ac
, data
);
1999 if (ret
!= LDB_SUCCESS
) {
2003 down_tree
= ldb_parse_tree_copy_shallow(ac
, req
->op
.search
.tree
);
2004 if (down_tree
== NULL
) {
2005 return ldb_oom(ldb
);
2008 if (!ac
->am_system
&& data
->password_attrs
) {
2009 for (i
= 0; data
->password_attrs
[i
]; i
++) {
2010 if ((!ac
->userPassword
) &&
2011 (ldb_attr_cmp(data
->password_attrs
[i
],
2012 "userPassword") == 0))
2017 ldb_parse_tree_attr_replace(down_tree
,
2018 data
->password_attrs
[i
],
2019 "kludgeACLredactedattribute");
2023 if (!ac
->am_system
&& !ac
->am_administrator
&& data
->confidential_attrs
) {
2024 for (i
= 0; data
->confidential_attrs
[i
]; i
++) {
2025 ldb_parse_tree_attr_replace(down_tree
,
2026 data
->confidential_attrs
[i
],
2027 "kludgeACLredactedattribute");
2031 ret
= ldb_build_search_req_ex(&down_req
,
2033 req
->op
.search
.base
,
2034 req
->op
.search
.scope
,
2036 req
->op
.search
.attrs
,
2038 ac
, acl_search_callback
,
2040 LDB_REQ_SET_LOCATION(down_req
);
2041 if (ret
!= LDB_SUCCESS
) {
2044 /* perform the search */
2045 return ldb_next_request(module
, down_req
);
2048 static int acl_extended(struct ldb_module
*module
, struct ldb_request
*req
)
2050 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
2051 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
2053 /* allow everybody to read the sequence number */
2054 if (strcmp(req
->op
.extended
.oid
,
2055 LDB_EXTENDED_SEQUENCE_NUMBER
) == 0) {
2056 return ldb_next_request(module
, req
);
2059 if (dsdb_module_am_system(module
) ||
2060 dsdb_module_am_administrator(module
) || as_system
) {
2061 return ldb_next_request(module
, req
);
2063 ldb_asprintf_errstring(ldb
,
2065 "attempted database modify not permitted. "
2066 "User %s is not SYSTEM or an administrator",
2067 acl_user_name(req
, module
));
2068 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
2072 static const struct ldb_module_ops ldb_acl_module_ops
= {
2074 .search
= acl_search
,
2076 .modify
= acl_modify
,
2078 .rename
= acl_rename
,
2079 .extended
= acl_extended
,
2080 .init_context
= acl_module_init
2083 int ldb_acl_module_init(const char *version
)
2085 LDB_MODULE_CHECK_VERSION(version
);
2086 return ldb_register_module(&ldb_acl_module_ops
);