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 *attrs
[] = { "passwordAttribute", NULL
};
85 struct ldb_result
*res
;
86 struct ldb_message
*msg
;
87 struct ldb_message_element
*password_attributes
;
89 ldb
= ldb_module_get_ctx(module
);
91 ret
= ldb_mod_register_control(module
, LDB_CONTROL_SD_FLAGS_OID
);
92 if (ret
!= LDB_SUCCESS
) {
93 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
94 "acl_module_init: Unable to register control with rootdse!\n");
95 return ldb_operr(ldb
);
98 data
= talloc_zero(module
, struct acl_private
);
103 data
->acl_search
= lpcfg_parm_bool(ldb_get_opaque(ldb
, "loadparm"),
104 NULL
, "acl", "search", false);
105 ldb_module_set_private(module
, data
);
107 mem_ctx
= talloc_new(module
);
112 ret
= dsdb_module_search_dn(module
, mem_ctx
, &res
,
113 ldb_dn_new(mem_ctx
, ldb
, "@KLUDGEACL"),
115 DSDB_FLAG_NEXT_MODULE
|
118 if (ret
!= LDB_SUCCESS
) {
121 if (res
->count
== 0) {
125 if (res
->count
> 1) {
126 talloc_free(mem_ctx
);
127 return LDB_ERR_CONSTRAINT_VIOLATION
;
132 password_attributes
= ldb_msg_find_element(msg
, "passwordAttribute");
133 if (!password_attributes
) {
136 data
->password_attrs
= talloc_array(data
, const char *, password_attributes
->num_values
+ 1);
137 if (!data
->password_attrs
) {
138 talloc_free(mem_ctx
);
141 for (i
=0; i
< password_attributes
->num_values
; i
++) {
142 data
->password_attrs
[i
] = (const char *)password_attributes
->values
[i
].data
;
143 talloc_steal(data
->password_attrs
, password_attributes
->values
[i
].data
);
145 data
->password_attrs
[i
] = NULL
;
148 talloc_free(mem_ctx
);
149 return ldb_next_init(module
);
152 static int acl_allowedAttributes(struct ldb_module
*module
,
153 const struct dsdb_schema
*schema
,
154 struct ldb_message
*sd_msg
,
155 struct ldb_message
*msg
,
156 struct acl_context
*ac
)
158 struct ldb_message_element
*oc_el
;
159 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
161 const char **attr_list
;
164 /* If we don't have a schema yet, we can't do anything... */
165 if (schema
== NULL
) {
166 ldb_asprintf_errstring(ldb
, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg
->dn
));
167 return LDB_ERR_OPERATIONS_ERROR
;
170 /* Must remove any existing attribute */
171 if (ac
->allowedAttributes
) {
172 ldb_msg_remove_attr(msg
, "allowedAttributes");
175 mem_ctx
= talloc_new(msg
);
180 oc_el
= ldb_msg_find_element(sd_msg
, "objectClass");
181 attr_list
= dsdb_full_attribute_list(mem_ctx
, schema
, oc_el
, DSDB_SCHEMA_ALL
);
183 ldb_asprintf_errstring(ldb
, "acl: Failed to get list of attributes");
184 talloc_free(mem_ctx
);
185 return LDB_ERR_OPERATIONS_ERROR
;
187 if (ac
->allowedAttributes
) {
188 for (i
=0; attr_list
&& attr_list
[i
]; i
++) {
189 ldb_msg_add_string(msg
, "allowedAttributes", attr_list
[i
]);
192 if (ac
->allowedAttributesEffective
) {
193 struct security_descriptor
*sd
;
194 struct dom_sid
*sid
= NULL
;
195 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
196 LDB_CONTROL_AS_SYSTEM_OID
);
198 if (as_system
!= NULL
) {
199 as_system
->critical
= 0;
202 ldb_msg_remove_attr(msg
, "allowedAttributesEffective");
203 if (ac
->am_system
|| as_system
) {
204 for (i
=0; attr_list
&& attr_list
[i
]; i
++) {
205 ldb_msg_add_string(msg
, "allowedAttributesEffective", attr_list
[i
]);
210 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module
), mem_ctx
, sd_msg
, &sd
);
212 if (ret
!= LDB_SUCCESS
) {
216 sid
= samdb_result_dom_sid(mem_ctx
, sd_msg
, "objectSid");
217 for (i
=0; attr_list
&& attr_list
[i
]; i
++) {
218 const struct dsdb_attribute
*attr
= dsdb_attribute_by_lDAPDisplayName(schema
,
221 return ldb_operr(ldb
);
223 /* remove constructed attributes */
224 if (attr
->systemFlags
& DS_FLAG_ATTR_IS_CONSTRUCTED
226 || (attr
->linkID
!= 0 && attr
->linkID
% 2 != 0 )) {
229 ret
= acl_check_access_on_attribute(module
,
235 if (ret
== LDB_SUCCESS
) {
236 ldb_msg_add_string(msg
, "allowedAttributesEffective", attr_list
[i
]);
243 static int acl_childClasses(struct ldb_module
*module
,
244 const struct dsdb_schema
*schema
,
245 struct ldb_message
*sd_msg
,
246 struct ldb_message
*msg
,
247 const char *attrName
)
249 struct ldb_message_element
*oc_el
;
250 struct ldb_message_element
*allowedClasses
;
251 const struct dsdb_class
*sclass
;
255 /* If we don't have a schema yet, we can't do anything... */
256 if (schema
== NULL
) {
257 ldb_asprintf_errstring(ldb_module_get_ctx(module
), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg
->dn
));
258 return LDB_ERR_OPERATIONS_ERROR
;
261 /* Must remove any existing attribute, or else confusion reins */
262 ldb_msg_remove_attr(msg
, attrName
);
263 ret
= ldb_msg_add_empty(msg
, attrName
, 0, &allowedClasses
);
264 if (ret
!= LDB_SUCCESS
) {
268 oc_el
= ldb_msg_find_element(sd_msg
, "objectClass");
270 for (i
=0; oc_el
&& i
< oc_el
->num_values
; i
++) {
271 sclass
= dsdb_class_by_lDAPDisplayName_ldb_val(schema
, &oc_el
->values
[i
]);
273 /* We don't know this class? what is going on? */
277 for (j
=0; sclass
->possibleInferiors
&& sclass
->possibleInferiors
[j
]; j
++) {
278 ldb_msg_add_string(msg
, attrName
, sclass
->possibleInferiors
[j
]);
281 if (allowedClasses
->num_values
> 1) {
282 TYPESAFE_QSORT(allowedClasses
->values
, allowedClasses
->num_values
, data_blob_cmp
);
283 for (i
=1 ; i
< allowedClasses
->num_values
; i
++) {
284 struct ldb_val
*val1
= &allowedClasses
->values
[i
-1];
285 struct ldb_val
*val2
= &allowedClasses
->values
[i
];
286 if (data_blob_cmp(val1
, val2
) == 0) {
287 memmove(val1
, val2
, (allowedClasses
->num_values
- i
) * sizeof(struct ldb_val
));
288 allowedClasses
->num_values
--;
297 static int acl_check_access_on_class(struct ldb_module
*module
,
298 const struct dsdb_schema
*schema
,
300 struct security_descriptor
*sd
,
301 struct security_token
*token
,
302 struct dom_sid
*rp_sid
,
303 uint32_t access_mask
,
304 const char *class_name
)
308 uint32_t access_granted
;
309 struct object_tree
*root
= NULL
;
310 struct object_tree
*new_node
= NULL
;
311 const struct GUID
*guid
;
313 if (class_name
!= NULL
) {
314 guid
= class_schemaid_guid_by_lDAPDisplayName(schema
, class_name
);
316 DEBUG(10, ("acl_search: cannot find class %s\n",
320 if (!insert_in_object_tree(mem_ctx
,
323 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
328 status
= sec_access_check_ds(sd
, token
,
333 if (!NT_STATUS_IS_OK(status
)) {
334 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
340 return ldb_operr(ldb_module_get_ctx(module
));
343 static int acl_childClassesEffective(struct ldb_module
*module
,
344 const struct dsdb_schema
*schema
,
345 struct ldb_message
*sd_msg
,
346 struct ldb_message
*msg
,
347 struct acl_context
*ac
)
349 struct ldb_message_element
*oc_el
;
350 struct ldb_message_element
*allowedClasses
= NULL
;
351 const struct dsdb_class
*sclass
;
352 struct security_descriptor
*sd
;
353 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
354 LDB_CONTROL_AS_SYSTEM_OID
);
355 struct dom_sid
*sid
= NULL
;
359 if (as_system
!= NULL
) {
360 as_system
->critical
= 0;
363 if (ac
->am_system
|| as_system
) {
364 return acl_childClasses(module
, schema
, sd_msg
, msg
, "allowedChildClassesEffective");
367 /* If we don't have a schema yet, we can't do anything... */
368 if (schema
== NULL
) {
369 ldb_asprintf_errstring(ldb_module_get_ctx(module
), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg
->dn
));
370 return LDB_ERR_OPERATIONS_ERROR
;
373 /* Must remove any existing attribute, or else confusion reins */
374 ldb_msg_remove_attr(msg
, "allowedChildClassesEffective");
376 oc_el
= ldb_msg_find_element(sd_msg
, "objectClass");
377 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module
), msg
, sd_msg
, &sd
);
378 if (ret
!= LDB_SUCCESS
) {
382 sid
= samdb_result_dom_sid(msg
, sd_msg
, "objectSid");
383 for (i
=0; oc_el
&& i
< oc_el
->num_values
; i
++) {
384 sclass
= dsdb_class_by_lDAPDisplayName_ldb_val(schema
, &oc_el
->values
[i
]);
386 /* We don't know this class? what is going on? */
390 for (j
=0; sclass
->possibleInferiors
&& sclass
->possibleInferiors
[j
]; j
++) {
391 ret
= acl_check_access_on_class(module
,
395 acl_user_token(module
),
397 SEC_ADS_CREATE_CHILD
,
398 sclass
->possibleInferiors
[j
]);
399 if (ret
== LDB_SUCCESS
) {
400 ldb_msg_add_string(msg
, "allowedChildClassesEffective",
401 sclass
->possibleInferiors
[j
]);
405 allowedClasses
= ldb_msg_find_element(msg
, "allowedChildClassesEffective");
406 if (!allowedClasses
) {
410 if (allowedClasses
->num_values
> 1) {
411 TYPESAFE_QSORT(allowedClasses
->values
, allowedClasses
->num_values
, data_blob_cmp
);
412 for (i
=1 ; i
< allowedClasses
->num_values
; i
++) {
413 struct ldb_val
*val1
= &allowedClasses
->values
[i
-1];
414 struct ldb_val
*val2
= &allowedClasses
->values
[i
];
415 if (data_blob_cmp(val1
, val2
) == 0) {
416 memmove(val1
, val2
, (allowedClasses
->num_values
- i
) * sizeof( struct ldb_val
));
417 allowedClasses
->num_values
--;
425 static int acl_sDRightsEffective(struct ldb_module
*module
,
426 struct ldb_message
*sd_msg
,
427 struct ldb_message
*msg
,
428 struct acl_context
*ac
)
430 struct ldb_message_element
*rightsEffective
;
432 struct security_descriptor
*sd
;
433 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
434 LDB_CONTROL_AS_SYSTEM_OID
);
435 struct dom_sid
*sid
= NULL
;
438 if (as_system
!= NULL
) {
439 as_system
->critical
= 0;
442 /* Must remove any existing attribute, or else confusion reins */
443 ldb_msg_remove_attr(msg
, "sDRightsEffective");
444 ret
= ldb_msg_add_empty(msg
, "sDRightsEffective", 0, &rightsEffective
);
445 if (ret
!= LDB_SUCCESS
) {
448 if (ac
->am_system
|| as_system
) {
449 flags
= SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_SACL
| SECINFO_DACL
;
452 /* Get the security descriptor from the message */
453 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module
), msg
, sd_msg
, &sd
);
454 if (ret
!= LDB_SUCCESS
) {
457 sid
= samdb_result_dom_sid(msg
, sd_msg
, "objectSid");
458 ret
= acl_check_access_on_attribute(module
,
464 if (ret
== LDB_SUCCESS
) {
465 flags
|= SECINFO_OWNER
| SECINFO_GROUP
;
467 ret
= acl_check_access_on_attribute(module
,
473 if (ret
== LDB_SUCCESS
) {
474 flags
|= SECINFO_DACL
;
476 ret
= acl_check_access_on_attribute(module
,
480 SEC_FLAG_SYSTEM_SECURITY
,
482 if (ret
== LDB_SUCCESS
) {
483 flags
|= SECINFO_SACL
;
486 return samdb_msg_add_uint(ldb_module_get_ctx(module
), msg
, msg
,
487 "sDRightsEffective", flags
);
490 static int acl_validate_spn_value(TALLOC_CTX
*mem_ctx
,
491 struct ldb_context
*ldb
,
492 const char *spn_value
,
493 uint32_t userAccountControl
,
494 const char *samAccountName
,
495 const char *dnsHostName
,
496 const char *netbios_name
,
497 const char *ntds_guid
)
500 krb5_context krb_ctx
;
501 krb5_error_code kerr
;
502 krb5_principal principal
;
506 const char *forest_name
= samdb_forest_name(ldb
, mem_ctx
);
507 const char *base_domain
= samdb_default_domain_name(ldb
, mem_ctx
);
508 struct loadparm_context
*lp_ctx
= talloc_get_type(ldb_get_opaque(ldb
, "loadparm"),
509 struct loadparm_context
);
510 bool is_dc
= (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) ||
511 (userAccountControl
& UF_PARTIAL_SECRETS_ACCOUNT
);
513 if (strcasecmp_m(spn_value
, samAccountName
) == 0) {
514 /* MacOS X sets this value, and setting an SPN of your
515 * own samAccountName is both pointless and safe */
519 kerr
= smb_krb5_init_context_basic(mem_ctx
,
523 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
524 "Could not initialize kerberos context.");
527 ret
= krb5_parse_name(krb_ctx
, spn_value
, &principal
);
529 krb5_free_context(krb_ctx
);
530 return LDB_ERR_CONSTRAINT_VIOLATION
;
533 if (principal
->name
.name_string
.len
< 2) {
537 instanceName
= principal
->name
.name_string
.val
[1];
538 serviceType
= principal
->name
.name_string
.val
[0];
539 if (principal
->name
.name_string
.len
== 3) {
540 serviceName
= principal
->name
.name_string
.val
[2];
549 if (strcasecmp(serviceType
, "ldap") == 0) {
550 if (strcasecmp(serviceName
, netbios_name
) != 0 &&
551 strcasecmp(serviceName
, forest_name
) != 0) {
555 } else if (strcasecmp(serviceType
, "gc") == 0) {
556 if (strcasecmp(serviceName
, forest_name
) != 0) {
560 if (strcasecmp(serviceName
, base_domain
) != 0 &&
561 strcasecmp(serviceName
, netbios_name
) != 0) {
566 /* instanceName can be samAccountName without $ or dnsHostName
567 * or "ntds_guid._msdcs.forest_domain for DC objects */
568 if (strlen(instanceName
) == (strlen(samAccountName
) - 1)
569 && strncasecmp(instanceName
, samAccountName
, strlen(samAccountName
) - 1) == 0) {
571 } else if (dnsHostName
!= NULL
&& strcasecmp(instanceName
, dnsHostName
) == 0) {
574 const char *guid_str
;
575 guid_str
= talloc_asprintf(mem_ctx
,"%s._msdcs.%s",
578 if (strcasecmp(instanceName
, guid_str
) == 0) {
584 krb5_free_principal(krb_ctx
, principal
);
585 krb5_free_context(krb_ctx
);
586 return LDB_ERR_CONSTRAINT_VIOLATION
;
589 krb5_free_principal(krb_ctx
, principal
);
590 krb5_free_context(krb_ctx
);
594 static int acl_check_spn(TALLOC_CTX
*mem_ctx
,
595 struct ldb_module
*module
,
596 struct ldb_request
*req
,
597 struct security_descriptor
*sd
,
599 const struct GUID
*oc_guid
,
600 const struct dsdb_attribute
*attr
)
604 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
605 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
606 struct ldb_result
*acl_res
;
607 struct ldb_result
*netbios_res
;
608 struct ldb_message_element
*el
;
609 struct ldb_dn
*partitions_dn
= samdb_partitions_dn(ldb
, tmp_ctx
);
610 uint32_t userAccountControl
;
611 const char *samAccountName
;
612 const char *dnsHostName
;
613 const char *netbios_name
;
615 char *ntds_guid
= NULL
;
617 static const char *acl_attrs
[] = {
620 "userAccountControl",
623 static const char *netbios_attrs
[] = {
628 /* if we have wp, we can do whatever we like */
629 if (acl_check_access_on_attribute(module
,
634 attr
) == LDB_SUCCESS
) {
635 talloc_free(tmp_ctx
);
639 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
640 GUID_DRS_VALIDATE_SPN
,
644 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
645 dsdb_acl_debug(sd
, acl_user_token(module
),
646 req
->op
.mod
.message
->dn
,
649 talloc_free(tmp_ctx
);
653 ret
= dsdb_module_search_dn(module
, tmp_ctx
,
654 &acl_res
, req
->op
.mod
.message
->dn
,
656 DSDB_FLAG_NEXT_MODULE
|
657 DSDB_FLAG_AS_SYSTEM
|
658 DSDB_SEARCH_SHOW_RECYCLED
,
660 if (ret
!= LDB_SUCCESS
) {
661 talloc_free(tmp_ctx
);
665 userAccountControl
= ldb_msg_find_attr_as_uint(acl_res
->msgs
[0], "userAccountControl", 0);
666 dnsHostName
= ldb_msg_find_attr_as_string(acl_res
->msgs
[0], "dnsHostName", NULL
);
667 samAccountName
= ldb_msg_find_attr_as_string(acl_res
->msgs
[0], "samAccountName", NULL
);
669 ret
= dsdb_module_search(module
, tmp_ctx
,
670 &netbios_res
, partitions_dn
,
673 DSDB_FLAG_NEXT_MODULE
|
677 ldb_dn_get_linearized(ldb_get_default_basedn(ldb
)));
679 netbios_name
= ldb_msg_find_attr_as_string(netbios_res
->msgs
[0], "nETBIOSName", NULL
);
681 el
= ldb_msg_find_element(req
->op
.mod
.message
, "servicePrincipalName");
683 talloc_free(tmp_ctx
);
684 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
685 "Error finding element for servicePrincipalName.");
688 /* NTDSDSA objectGuid of object we are checking SPN for */
689 if (userAccountControl
& (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
690 ret
= dsdb_module_find_ntdsguid_for_computer(module
, tmp_ctx
,
691 req
->op
.mod
.message
->dn
, &ntds
, req
);
692 if (ret
!= LDB_SUCCESS
) {
693 ldb_asprintf_errstring(ldb
, "Failed to find NTDSDSA objectGuid for %s: %s",
694 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
),
696 talloc_free(tmp_ctx
);
697 return LDB_ERR_OPERATIONS_ERROR
;
699 ntds_guid
= GUID_string(tmp_ctx
, &ntds
);
702 for (i
=0; i
< el
->num_values
; i
++) {
703 ret
= acl_validate_spn_value(tmp_ctx
,
705 (char *)el
->values
[i
].data
,
711 if (ret
!= LDB_SUCCESS
) {
712 talloc_free(tmp_ctx
);
716 talloc_free(tmp_ctx
);
720 static int acl_add(struct ldb_module
*module
, struct ldb_request
*req
)
723 struct ldb_dn
*parent
= ldb_dn_get_parent(req
, req
->op
.add
.message
->dn
);
724 struct ldb_context
*ldb
;
725 const struct dsdb_schema
*schema
;
726 struct ldb_message_element
*oc_el
;
727 const struct GUID
*guid
;
728 struct ldb_dn
*nc_root
;
729 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
731 if (as_system
!= NULL
) {
732 as_system
->critical
= 0;
735 if (dsdb_module_am_system(module
) || as_system
) {
736 return ldb_next_request(module
, req
);
738 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
739 return ldb_next_request(module
, req
);
742 ldb
= ldb_module_get_ctx(module
);
744 /* Creating an NC. There is probably something we should do here,
745 * but we will establish that later */
747 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.add
.message
->dn
, &nc_root
);
748 if (ret
!= LDB_SUCCESS
) {
751 if (ldb_dn_compare(nc_root
, req
->op
.add
.message
->dn
) == 0) {
752 talloc_free(nc_root
);
753 return ldb_next_request(module
, req
);
755 talloc_free(nc_root
);
757 schema
= dsdb_get_schema(ldb
, req
);
759 return ldb_operr(ldb
);
762 oc_el
= ldb_msg_find_element(req
->op
.add
.message
, "objectClass");
763 if (!oc_el
|| oc_el
->num_values
== 0) {
764 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
765 "acl: unable to find objectClass on %s\n",
766 ldb_dn_get_linearized(req
->op
.add
.message
->dn
));
767 return ldb_module_done(req
, NULL
, NULL
, LDB_ERR_OPERATIONS_ERROR
);
770 guid
= class_schemaid_guid_by_lDAPDisplayName(schema
,
771 (char *)oc_el
->values
[oc_el
->num_values
-1].data
);
772 ret
= dsdb_module_check_access_on_dn(module
, req
, parent
, SEC_ADS_CREATE_CHILD
, guid
, req
);
773 if (ret
!= LDB_SUCCESS
) {
776 return ldb_next_request(module
, req
);
779 /* ckecks if modifications are allowed on "Member" attribute */
780 static int acl_check_self_membership(TALLOC_CTX
*mem_ctx
,
781 struct ldb_module
*module
,
782 struct ldb_request
*req
,
783 struct security_descriptor
*sd
,
785 const struct GUID
*oc_guid
,
786 const struct dsdb_attribute
*attr
)
790 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
791 struct ldb_dn
*user_dn
;
792 struct ldb_message_element
*member_el
;
793 /* if we have wp, we can do whatever we like */
794 if (acl_check_access_on_attribute(module
,
799 attr
) == LDB_SUCCESS
) {
802 /* if we are adding/deleting ourselves, check for self membership */
803 ret
= dsdb_find_dn_by_sid(ldb
, mem_ctx
,
804 &acl_user_token(module
)->sids
[PRIMARY_USER_SID_INDEX
],
806 if (ret
!= LDB_SUCCESS
) {
809 member_el
= ldb_msg_find_element(req
->op
.mod
.message
, "member");
811 return ldb_operr(ldb
);
813 /* user can only remove oneself */
814 if (member_el
->num_values
== 0) {
815 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
817 for (i
= 0; i
< member_el
->num_values
; i
++) {
818 if (strcasecmp((const char *)member_el
->values
[i
].data
,
819 ldb_dn_get_extended_linearized(mem_ctx
, user_dn
, 1)) != 0) {
820 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
823 ret
= acl_check_extended_right(mem_ctx
, sd
, acl_user_token(module
),
824 GUID_DRS_SELF_MEMBERSHIP
,
827 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
828 dsdb_acl_debug(sd
, acl_user_token(module
),
829 req
->op
.mod
.message
->dn
,
836 static int acl_check_password_rights(TALLOC_CTX
*mem_ctx
,
837 struct ldb_module
*module
,
838 struct ldb_request
*req
,
839 struct security_descriptor
*sd
,
841 const struct GUID
*oc_guid
,
844 int ret
= LDB_SUCCESS
;
845 unsigned int del_attr_cnt
= 0, add_attr_cnt
= 0, rep_attr_cnt
= 0;
846 struct ldb_message_element
*el
;
847 struct ldb_message
*msg
;
848 const char *passwordAttrs
[] = { "userPassword", "clearTextPassword",
849 "unicodePwd", "dBCSPwd", NULL
}, **l
;
850 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
852 msg
= ldb_msg_copy_shallow(tmp_ctx
, req
->op
.mod
.message
);
854 return ldb_module_oom(module
);
856 for (l
= passwordAttrs
; *l
!= NULL
; l
++) {
857 if ((!userPassword
) && (ldb_attr_cmp(*l
, "userPassword") == 0)) {
861 while ((el
= ldb_msg_find_element(msg
, *l
)) != NULL
) {
862 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
) {
865 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_ADD
) {
868 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_REPLACE
) {
871 ldb_msg_remove_element(msg
, el
);
875 /* single deletes will be handled by the "password_hash" LDB module
876 * later in the stack, so we let it though here */
877 if ((del_attr_cnt
> 0) && (add_attr_cnt
== 0) && (rep_attr_cnt
== 0)) {
878 talloc_free(tmp_ctx
);
882 if (ldb_request_get_control(req
,
883 DSDB_CONTROL_PASSWORD_CHANGE_OID
) != NULL
) {
884 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
885 * have a user password change and not a set as the message
886 * looks like. In it's value blob it contains the NT and/or LM
887 * hash of the old password specified by the user.
888 * This control is used by the SAMR and "kpasswd" password
889 * change mechanisms. */
890 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
891 GUID_DRS_USER_CHANGE_PASSWORD
,
892 SEC_ADS_CONTROL_ACCESS
,
895 else if (rep_attr_cnt
> 0 || (add_attr_cnt
!= del_attr_cnt
)) {
896 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
897 GUID_DRS_FORCE_CHANGE_PASSWORD
,
898 SEC_ADS_CONTROL_ACCESS
,
901 else if (add_attr_cnt
== 1 && del_attr_cnt
== 1) {
902 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
903 GUID_DRS_USER_CHANGE_PASSWORD
,
904 SEC_ADS_CONTROL_ACCESS
,
906 /* Very strange, but we get constraint violation in this case */
907 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
908 ret
= LDB_ERR_CONSTRAINT_VIOLATION
;
911 if (ret
!= LDB_SUCCESS
) {
912 dsdb_acl_debug(sd
, acl_user_token(module
),
913 req
->op
.mod
.message
->dn
,
917 talloc_free(tmp_ctx
);
921 static const struct GUID
*get_oc_guid_from_message(const struct dsdb_schema
*schema
,
922 struct ldb_message
*msg
)
924 struct ldb_message_element
*oc_el
;
925 const struct dsdb_class
*object_class
;
927 oc_el
= ldb_msg_find_element(msg
, "objectClass");
932 object_class
= dsdb_get_last_structural_class(schema
, oc_el
);
933 if (object_class
== NULL
) {
937 return &object_class
->schemaIDGUID
;
941 static int acl_modify(struct ldb_module
*module
, struct ldb_request
*req
)
944 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
945 const struct dsdb_schema
*schema
;
947 const struct GUID
*guid
;
948 uint32_t access_granted
;
949 struct object_tree
*root
= NULL
;
950 struct object_tree
*new_node
= NULL
;
952 struct ldb_result
*acl_res
;
953 struct security_descriptor
*sd
;
954 struct dom_sid
*sid
= NULL
;
955 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
956 bool userPassword
= dsdb_user_password_support(module
, req
, req
);
957 TALLOC_CTX
*tmp_ctx
= talloc_new(req
);
958 static const char *acl_attrs
[] = {
959 "nTSecurityDescriptor",
965 if (as_system
!= NULL
) {
966 as_system
->critical
= 0;
969 /* Don't print this debug statement if elements[0].name is going to be NULL */
970 if(req
->op
.mod
.message
->num_elements
> 0)
972 DEBUG(10, ("ldb:acl_modify: %s\n", req
->op
.mod
.message
->elements
[0].name
));
974 if (dsdb_module_am_system(module
) || as_system
) {
975 return ldb_next_request(module
, req
);
977 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
978 return ldb_next_request(module
, req
);
980 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &acl_res
, req
->op
.mod
.message
->dn
,
982 DSDB_FLAG_NEXT_MODULE
|
983 DSDB_FLAG_AS_SYSTEM
|
984 DSDB_SEARCH_SHOW_RECYCLED
,
987 if (ret
!= LDB_SUCCESS
) {
991 schema
= dsdb_get_schema(ldb
, tmp_ctx
);
993 ret
= LDB_ERR_OPERATIONS_ERROR
;
997 ret
= dsdb_get_sd_from_ldb_message(ldb
, tmp_ctx
, acl_res
->msgs
[0], &sd
);
998 if (ret
!= LDB_SUCCESS
) {
999 talloc_free(tmp_ctx
);
1000 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1001 "acl_modify: Error retrieving security descriptor.");
1003 /* Theoretically we pass the check if the object has no sd */
1008 guid
= get_oc_guid_from_message(schema
, acl_res
->msgs
[0]);
1010 talloc_free(tmp_ctx
);
1011 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1012 "acl_modify: Error retrieving object class GUID.");
1014 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1015 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1016 &root
, &new_node
)) {
1017 talloc_free(tmp_ctx
);
1018 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1019 "acl_modify: Error adding new node in object tree.");
1021 for (i
=0; i
< req
->op
.mod
.message
->num_elements
; i
++){
1022 const struct dsdb_attribute
*attr
;
1023 attr
= dsdb_attribute_by_lDAPDisplayName(schema
,
1024 req
->op
.mod
.message
->elements
[i
].name
);
1026 if (ldb_attr_cmp("nTSecurityDescriptor", req
->op
.mod
.message
->elements
[i
].name
) == 0) {
1027 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1033 if (!NT_STATUS_IS_OK(status
)) {
1034 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1035 "Object %s has no write dacl access\n",
1036 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
));
1038 acl_user_token(module
),
1039 req
->op
.mod
.message
->dn
,
1042 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1046 else if (ldb_attr_cmp("member", req
->op
.mod
.message
->elements
[i
].name
) == 0) {
1047 ret
= acl_check_self_membership(tmp_ctx
,
1054 if (ret
!= LDB_SUCCESS
) {
1058 else if (ldb_attr_cmp("dBCSPwd", req
->op
.mod
.message
->elements
[i
].name
) == 0) {
1059 /* this one is not affected by any rights, we should let it through
1060 so that passwords_hash returns the correct error */
1063 else if (ldb_attr_cmp("unicodePwd", req
->op
.mod
.message
->elements
[i
].name
) == 0 ||
1064 (userPassword
&& ldb_attr_cmp("userPassword", req
->op
.mod
.message
->elements
[i
].name
) == 0) ||
1065 ldb_attr_cmp("clearTextPassword", req
->op
.mod
.message
->elements
[i
].name
) == 0) {
1066 ret
= acl_check_password_rights(tmp_ctx
,
1073 if (ret
!= LDB_SUCCESS
) {
1076 } else if (ldb_attr_cmp("servicePrincipalName", req
->op
.mod
.message
->elements
[i
].name
) == 0) {
1077 ret
= acl_check_spn(tmp_ctx
,
1084 if (ret
!= LDB_SUCCESS
) {
1089 /* This basic attribute existence check with the right errorcode
1090 * is needed since this module is the first one which requests
1091 * schema attribute information.
1092 * The complete attribute checking is done in the
1093 * "objectclass_attrs" module behind this one.
1096 ldb_asprintf_errstring(ldb
, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
1097 req
->op
.mod
.message
->elements
[i
].name
,
1098 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
));
1099 ret
= LDB_ERR_NO_SUCH_ATTRIBUTE
;
1102 if (!insert_in_object_tree(tmp_ctx
,
1103 &attr
->attributeSecurityGUID
, SEC_ADS_WRITE_PROP
,
1104 &new_node
, &new_node
)) {
1105 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1106 "acl_modify: cannot add to object tree securityGUID\n");
1107 ret
= LDB_ERR_OPERATIONS_ERROR
;
1111 if (!insert_in_object_tree(tmp_ctx
,
1112 &attr
->schemaIDGUID
, SEC_ADS_WRITE_PROP
, &new_node
, &new_node
)) {
1113 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1114 "acl_modify: cannot add to object tree attributeGUID\n");
1115 ret
= LDB_ERR_OPERATIONS_ERROR
;
1121 if (root
->num_of_children
> 0) {
1122 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1128 if (!NT_STATUS_IS_OK(status
)) {
1129 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1130 "Object %s has no write property access\n",
1131 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
));
1133 acl_user_token(module
),
1134 req
->op
.mod
.message
->dn
,
1137 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1143 talloc_free(tmp_ctx
);
1144 return ldb_next_request(module
, req
);
1146 talloc_free(tmp_ctx
);
1150 /* similar to the modify for the time being.
1151 * We need to consider the special delete tree case, though - TODO */
1152 static int acl_delete(struct ldb_module
*module
, struct ldb_request
*req
)
1155 struct ldb_dn
*parent
= ldb_dn_get_parent(req
, req
->op
.del
.dn
);
1156 struct ldb_context
*ldb
;
1157 struct ldb_dn
*nc_root
;
1158 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1160 if (as_system
!= NULL
) {
1161 as_system
->critical
= 0;
1164 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req
->op
.del
.dn
)));
1165 if (dsdb_module_am_system(module
) || as_system
) {
1166 return ldb_next_request(module
, req
);
1168 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
1169 return ldb_next_request(module
, req
);
1172 ldb
= ldb_module_get_ctx(module
);
1174 /* Make sure we aren't deleting a NC */
1176 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.del
.dn
, &nc_root
);
1177 if (ret
!= LDB_SUCCESS
) {
1180 if (ldb_dn_compare(nc_root
, req
->op
.del
.dn
) == 0) {
1181 talloc_free(nc_root
);
1182 DEBUG(10,("acl:deleting a NC\n"));
1183 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1184 return ldb_module_done(req
, NULL
, NULL
,
1185 LDB_ERR_UNWILLING_TO_PERFORM
);
1187 talloc_free(nc_root
);
1189 /* First check if we have delete object right */
1190 ret
= dsdb_module_check_access_on_dn(module
, req
, req
->op
.del
.dn
,
1191 SEC_STD_DELETE
, NULL
, req
);
1192 if (ret
== LDB_SUCCESS
) {
1193 return ldb_next_request(module
, req
);
1196 /* Nope, we don't have delete object. Lets check if we have delete
1197 * child on the parent */
1198 ret
= dsdb_module_check_access_on_dn(module
, req
, parent
,
1199 SEC_ADS_DELETE_CHILD
, NULL
, req
);
1200 if (ret
!= LDB_SUCCESS
) {
1204 return ldb_next_request(module
, req
);
1207 static int acl_rename(struct ldb_module
*module
, struct ldb_request
*req
)
1210 struct ldb_dn
*oldparent
= ldb_dn_get_parent(req
, req
->op
.rename
.olddn
);
1211 struct ldb_dn
*newparent
= ldb_dn_get_parent(req
, req
->op
.rename
.newdn
);
1212 const struct dsdb_schema
*schema
;
1213 struct ldb_context
*ldb
;
1214 struct security_descriptor
*sd
= NULL
;
1215 struct dom_sid
*sid
= NULL
;
1216 struct ldb_result
*acl_res
;
1217 const struct GUID
*guid
;
1218 struct ldb_dn
*nc_root
;
1219 struct object_tree
*root
= NULL
;
1220 struct object_tree
*new_node
= NULL
;
1221 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1222 TALLOC_CTX
*tmp_ctx
= talloc_new(req
);
1224 uint32_t access_granted
;
1225 const char *rdn_name
;
1226 static const char *acl_attrs
[] = {
1227 "nTSecurityDescriptor",
1233 if (as_system
!= NULL
) {
1234 as_system
->critical
= 0;
1237 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1238 if (dsdb_module_am_system(module
) || as_system
) {
1239 return ldb_next_request(module
, req
);
1241 if (ldb_dn_is_special(req
->op
.rename
.olddn
)) {
1242 return ldb_next_request(module
, req
);
1245 ldb
= ldb_module_get_ctx(module
);
1247 /* Make sure we aren't renaming/moving a NC */
1249 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.rename
.olddn
, &nc_root
);
1250 if (ret
!= LDB_SUCCESS
) {
1253 if (ldb_dn_compare(nc_root
, req
->op
.rename
.olddn
) == 0) {
1254 talloc_free(nc_root
);
1255 DEBUG(10,("acl:renaming/moving a NC\n"));
1256 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1257 return ldb_module_done(req
, NULL
, NULL
,
1258 LDB_ERR_UNWILLING_TO_PERFORM
);
1260 talloc_free(nc_root
);
1262 /* Look for the parent */
1264 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &acl_res
,
1265 req
->op
.rename
.olddn
, acl_attrs
,
1266 DSDB_FLAG_NEXT_MODULE
|
1267 DSDB_FLAG_AS_SYSTEM
|
1268 DSDB_SEARCH_SHOW_RECYCLED
, req
);
1269 /* we sould be able to find the parent */
1270 if (ret
!= LDB_SUCCESS
) {
1271 DEBUG(10,("acl: failed to find object %s\n",
1272 ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1273 talloc_free(tmp_ctx
);
1277 schema
= dsdb_get_schema(ldb
, acl_res
);
1279 talloc_free(tmp_ctx
);
1280 return ldb_operr(ldb
);
1283 guid
= get_oc_guid_from_message(schema
, acl_res
->msgs
[0]);
1284 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1285 &root
, &new_node
)) {
1286 talloc_free(tmp_ctx
);
1287 return ldb_operr(ldb
);
1290 guid
= attribute_schemaid_guid_by_lDAPDisplayName(schema
,
1292 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1293 &new_node
, &new_node
)) {
1294 talloc_free(tmp_ctx
);
1295 return ldb_operr(ldb
);
1298 rdn_name
= ldb_dn_get_rdn_name(req
->op
.rename
.olddn
);
1299 if (rdn_name
== NULL
) {
1300 talloc_free(tmp_ctx
);
1301 return ldb_operr(ldb
);
1303 guid
= attribute_schemaid_guid_by_lDAPDisplayName(schema
,
1305 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1306 &new_node
, &new_node
)) {
1307 talloc_free(tmp_ctx
);
1308 return ldb_operr(ldb
);
1311 ret
= dsdb_get_sd_from_ldb_message(ldb
, req
, acl_res
->msgs
[0], &sd
);
1313 if (ret
!= LDB_SUCCESS
) {
1314 talloc_free(tmp_ctx
);
1315 return ldb_operr(ldb
);
1317 /* Theoretically we pass the check if the object has no sd */
1319 talloc_free(tmp_ctx
);
1322 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1323 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1329 if (!NT_STATUS_IS_OK(status
)) {
1330 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1331 "Object %s has no wp on name\n",
1332 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1334 acl_user_token(module
),
1335 req
->op
.rename
.olddn
,
1338 talloc_free(tmp_ctx
);
1339 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1342 if (ldb_dn_compare(oldparent
, newparent
) == 0) {
1343 /* regular rename, not move, nothing more to do */
1344 talloc_free(tmp_ctx
);
1345 return ldb_next_request(module
, req
);
1348 /* new parent should have create child */
1351 guid
= get_oc_guid_from_message(schema
, acl_res
->msgs
[0]);
1353 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1354 "acl:renamed object has no object class\n");
1355 talloc_free(tmp_ctx
);
1356 return ldb_module_done(req
, NULL
, NULL
, LDB_ERR_OPERATIONS_ERROR
);
1359 ret
= dsdb_module_check_access_on_dn(module
, req
, newparent
, SEC_ADS_CREATE_CHILD
, guid
, req
);
1360 if (ret
!= LDB_SUCCESS
) {
1361 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1362 "acl:access_denied renaming %s",
1363 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1364 talloc_free(tmp_ctx
);
1367 /* do we have delete object on the object? */
1369 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1375 if (NT_STATUS_IS_OK(status
)) {
1376 talloc_free(tmp_ctx
);
1377 return ldb_next_request(module
, req
);
1379 /* what about delete child on the current parent */
1380 ret
= dsdb_module_check_access_on_dn(module
, req
, oldparent
, SEC_ADS_DELETE_CHILD
, NULL
, req
);
1381 if (ret
!= LDB_SUCCESS
) {
1382 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1383 "acl:access_denied renaming %s", ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1384 talloc_free(tmp_ctx
);
1385 return ldb_module_done(req
, NULL
, NULL
, ret
);
1388 talloc_free(tmp_ctx
);
1390 return ldb_next_request(module
, req
);
1393 static int acl_search_update_confidential_attrs(struct acl_context
*ac
,
1394 struct acl_private
*data
)
1396 struct dsdb_attribute
*a
;
1399 if (data
->acl_search
) {
1401 * If acl:search is activated, the acl_read module
1402 * protects confidential attributes.
1407 if ((ac
->schema
== data
->cached_schema_ptr
) &&
1408 (ac
->schema
->loaded_usn
== data
->cached_schema_loaded_usn
) &&
1409 (ac
->schema
->metadata_usn
== data
->cached_schema_metadata_usn
))
1414 data
->cached_schema_ptr
= NULL
;
1415 data
->cached_schema_loaded_usn
= 0;
1416 data
->cached_schema_metadata_usn
= 0;
1417 TALLOC_FREE(data
->confidential_attrs
);
1419 if (ac
->schema
== NULL
) {
1423 for (a
= ac
->schema
->attributes
; a
; a
= a
->next
) {
1424 const char **attrs
= data
->confidential_attrs
;
1426 if (!(a
->searchFlags
& SEARCH_FLAG_CONFIDENTIAL
)) {
1430 attrs
= talloc_realloc(data
, attrs
, const char *, n
+ 2);
1431 if (attrs
== NULL
) {
1432 TALLOC_FREE(data
->confidential_attrs
);
1433 return ldb_module_oom(ac
->module
);
1436 attrs
[n
] = a
->lDAPDisplayName
;
1440 data
->confidential_attrs
= attrs
;
1443 data
->cached_schema_ptr
= ac
->schema
;
1444 data
->cached_schema_loaded_usn
= ac
->schema
->loaded_usn
;
1445 data
->cached_schema_metadata_usn
= ac
->schema
->metadata_usn
;
1450 static int acl_search_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1452 struct acl_context
*ac
;
1453 struct acl_private
*data
;
1454 struct ldb_result
*acl_res
;
1455 static const char *acl_attrs
[] = {
1457 "nTSecurityDescriptor",
1464 ac
= talloc_get_type(req
->context
, struct acl_context
);
1465 data
= talloc_get_type(ldb_module_get_private(ac
->module
), struct acl_private
);
1467 return ldb_module_done(ac
->req
, NULL
, NULL
,
1468 LDB_ERR_OPERATIONS_ERROR
);
1470 if (ares
->error
!= LDB_SUCCESS
) {
1471 return ldb_module_done(ac
->req
, ares
->controls
,
1472 ares
->response
, ares
->error
);
1475 switch (ares
->type
) {
1476 case LDB_REPLY_ENTRY
:
1477 if (ac
->constructed_attrs
) {
1478 ret
= dsdb_module_search_dn(ac
->module
, ac
, &acl_res
, ares
->message
->dn
,
1480 DSDB_FLAG_NEXT_MODULE
|
1481 DSDB_FLAG_AS_SYSTEM
|
1482 DSDB_SEARCH_SHOW_RECYCLED
,
1484 if (ret
!= LDB_SUCCESS
) {
1485 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1489 if (ac
->allowedAttributes
|| ac
->allowedAttributesEffective
) {
1490 ret
= acl_allowedAttributes(ac
->module
, ac
->schema
,
1493 if (ret
!= LDB_SUCCESS
) {
1494 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1498 if (ac
->allowedChildClasses
) {
1499 ret
= acl_childClasses(ac
->module
, ac
->schema
,
1502 "allowedChildClasses");
1503 if (ret
!= LDB_SUCCESS
) {
1504 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1508 if (ac
->allowedChildClassesEffective
) {
1509 ret
= acl_childClassesEffective(ac
->module
, ac
->schema
,
1512 if (ret
!= LDB_SUCCESS
) {
1513 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1517 if (ac
->sDRightsEffective
) {
1518 ret
= acl_sDRightsEffective(ac
->module
,
1521 if (ret
!= LDB_SUCCESS
) {
1522 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1527 return ldb_module_send_entry(ac
->req
, ares
->message
,
1531 if (ac
->am_system
) {
1532 return ldb_module_send_entry(ac
->req
, ares
->message
,
1536 if (data
->password_attrs
!= NULL
) {
1537 for (i
= 0; data
->password_attrs
[i
]; i
++) {
1538 if ((!ac
->userPassword
) &&
1539 (ldb_attr_cmp(data
->password_attrs
[i
],
1540 "userPassword") == 0))
1545 ldb_msg_remove_attr(ares
->message
, data
->password_attrs
[i
]);
1549 if (ac
->am_administrator
) {
1550 return ldb_module_send_entry(ac
->req
, ares
->message
,
1554 ret
= acl_search_update_confidential_attrs(ac
, data
);
1555 if (ret
!= LDB_SUCCESS
) {
1559 if (data
->confidential_attrs
!= NULL
) {
1560 for (i
= 0; data
->confidential_attrs
[i
]; i
++) {
1561 ldb_msg_remove_attr(ares
->message
,
1562 data
->confidential_attrs
[i
]);
1566 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
1568 case LDB_REPLY_REFERRAL
:
1569 return ldb_module_send_referral(ac
->req
, ares
->referral
);
1571 case LDB_REPLY_DONE
:
1572 return ldb_module_done(ac
->req
, ares
->controls
,
1573 ares
->response
, LDB_SUCCESS
);
1579 static int acl_search(struct ldb_module
*module
, struct ldb_request
*req
)
1581 struct ldb_context
*ldb
;
1582 struct acl_context
*ac
;
1583 struct ldb_parse_tree
*down_tree
;
1584 struct ldb_request
*down_req
;
1585 struct acl_private
*data
;
1589 ldb
= ldb_module_get_ctx(module
);
1591 ac
= talloc_zero(req
, struct acl_context
);
1593 return ldb_oom(ldb
);
1595 data
= talloc_get_type(ldb_module_get_private(module
), struct acl_private
);
1597 ac
->module
= module
;
1599 ac
->am_system
= dsdb_module_am_system(module
);
1600 ac
->am_administrator
= dsdb_module_am_administrator(module
);
1601 ac
->constructed_attrs
= false;
1602 ac
->modify_search
= true;
1603 ac
->allowedAttributes
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedAttributes");
1604 ac
->allowedAttributesEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedAttributesEffective");
1605 ac
->allowedChildClasses
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedChildClasses");
1606 ac
->allowedChildClassesEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedChildClassesEffective");
1607 ac
->sDRightsEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "sDRightsEffective");
1608 ac
->userPassword
= dsdb_user_password_support(module
, ac
, req
);
1609 ac
->schema
= dsdb_get_schema(ldb
, ac
);
1611 ac
->constructed_attrs
|= ac
->allowedAttributes
;
1612 ac
->constructed_attrs
|= ac
->allowedChildClasses
;
1613 ac
->constructed_attrs
|= ac
->allowedChildClassesEffective
;
1614 ac
->constructed_attrs
|= ac
->allowedAttributesEffective
;
1615 ac
->constructed_attrs
|= ac
->sDRightsEffective
;
1618 ac
->modify_search
= false;
1620 if (ac
->am_system
) {
1621 ac
->modify_search
= false;
1624 if (!ac
->constructed_attrs
&& !ac
->modify_search
) {
1625 return ldb_next_request(module
, req
);
1628 ret
= acl_search_update_confidential_attrs(ac
, data
);
1629 if (ret
!= LDB_SUCCESS
) {
1633 down_tree
= ldb_parse_tree_copy_shallow(ac
, req
->op
.search
.tree
);
1634 if (down_tree
== NULL
) {
1635 return ldb_oom(ldb
);
1638 if (!ac
->am_system
&& data
->password_attrs
) {
1639 for (i
= 0; data
->password_attrs
[i
]; i
++) {
1640 if ((!ac
->userPassword
) &&
1641 (ldb_attr_cmp(data
->password_attrs
[i
],
1642 "userPassword") == 0))
1647 ldb_parse_tree_attr_replace(down_tree
,
1648 data
->password_attrs
[i
],
1649 "kludgeACLredactedattribute");
1653 if (!ac
->am_system
&& !ac
->am_administrator
&& data
->confidential_attrs
) {
1654 for (i
= 0; data
->confidential_attrs
[i
]; i
++) {
1655 ldb_parse_tree_attr_replace(down_tree
,
1656 data
->confidential_attrs
[i
],
1657 "kludgeACLredactedattribute");
1661 ret
= ldb_build_search_req_ex(&down_req
,
1663 req
->op
.search
.base
,
1664 req
->op
.search
.scope
,
1666 req
->op
.search
.attrs
,
1668 ac
, acl_search_callback
,
1670 LDB_REQ_SET_LOCATION(down_req
);
1671 if (ret
!= LDB_SUCCESS
) {
1674 /* perform the search */
1675 return ldb_next_request(module
, down_req
);
1678 static int acl_extended(struct ldb_module
*module
, struct ldb_request
*req
)
1680 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
1681 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1683 /* allow everybody to read the sequence number */
1684 if (strcmp(req
->op
.extended
.oid
,
1685 LDB_EXTENDED_SEQUENCE_NUMBER
) == 0) {
1686 return ldb_next_request(module
, req
);
1689 if (dsdb_module_am_system(module
) ||
1690 dsdb_module_am_administrator(module
) || as_system
) {
1691 return ldb_next_request(module
, req
);
1693 ldb_asprintf_errstring(ldb
,
1695 "attempted database modify not permitted. "
1696 "User %s is not SYSTEM or an administrator",
1697 acl_user_name(req
, module
));
1698 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1702 static const struct ldb_module_ops ldb_acl_module_ops
= {
1704 .search
= acl_search
,
1706 .modify
= acl_modify
,
1708 .rename
= acl_rename
,
1709 .extended
= acl_extended
,
1710 .init_context
= acl_module_init
1713 int ldb_acl_module_init(const char *version
)
1715 LDB_MODULE_CHECK_VERSION(version
);
1716 return ldb_register_module(&ldb_acl_module_ops
);