4 Copyright (C) Simo Sorce 2006-2008
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2007
6 Copyright (C) Nadezhda Ivanova 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: DS Security descriptor module
28 * - Calculate the security descriptor of a newly created object
29 * - Perform sd recalculation on a move operation
30 * - Handle sd modification invariants
32 * Author: Nadezhda Ivanova
36 #include <ldb_module.h>
37 #include "util/dlinklist.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "librpc/ndr/libndr.h"
40 #include "librpc/gen_ndr/ndr_security.h"
41 #include "libcli/security/security.h"
42 #include "dsdb/samdb/ldb_modules/schema.h"
43 #include "auth/auth.h"
44 #include "param/param.h"
45 #include "dsdb/samdb/ldb_modules/util.h"
47 struct descriptor_data
{
51 struct descriptor_context
{
52 struct ldb_module
*module
;
53 struct ldb_request
*req
;
54 struct ldb_message
*msg
;
55 struct ldb_reply
*search_res
;
56 struct ldb_reply
*search_oc_res
;
57 struct ldb_val
*parentsd_val
;
58 struct ldb_message_element
*sd_element
;
59 struct ldb_val
*sd_val
;
60 int (*step_fn
)(struct descriptor_context
*);
63 static struct dom_sid
*get_default_ag(TALLOC_CTX
*mem_ctx
,
65 struct security_token
*token
,
66 struct ldb_context
*ldb
)
68 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
69 const struct dom_sid
*domain_sid
= samdb_domain_sid(ldb
);
70 struct dom_sid
*da_sid
= dom_sid_add_rid(tmp_ctx
, domain_sid
, DOMAIN_RID_ADMINS
);
71 struct dom_sid
*ea_sid
= dom_sid_add_rid(tmp_ctx
, domain_sid
, DOMAIN_RID_ENTERPRISE_ADMINS
);
72 struct dom_sid
*sa_sid
= dom_sid_add_rid(tmp_ctx
, domain_sid
, DOMAIN_RID_SCHEMA_ADMINS
);
73 struct dom_sid
*dag_sid
;
74 struct ldb_dn
*nc_root
;
77 ret
= dsdb_find_nc_root(ldb
, tmp_ctx
, dn
, &nc_root
);
78 if (ret
!= LDB_SUCCESS
) {
83 if (ldb_dn_compare(nc_root
, ldb_get_schema_basedn(ldb
)) == 0) {
84 if (security_token_has_sid(token
, sa_sid
)) {
85 dag_sid
= dom_sid_dup(mem_ctx
, sa_sid
);
86 } else if (security_token_has_sid(token
, ea_sid
)) {
87 dag_sid
= dom_sid_dup(mem_ctx
, ea_sid
);
88 } else if (security_token_has_sid(token
, da_sid
)) {
89 dag_sid
= dom_sid_dup(mem_ctx
, da_sid
);
93 } else if (ldb_dn_compare(nc_root
, ldb_get_config_basedn(ldb
)) == 0) {
94 if (security_token_has_sid(token
, ea_sid
)) {
95 dag_sid
= dom_sid_dup(mem_ctx
, ea_sid
);
96 } else if (security_token_has_sid(token
, da_sid
)) {
97 dag_sid
= dom_sid_dup(mem_ctx
, da_sid
);
101 } else if (ldb_dn_compare(nc_root
, ldb_get_default_basedn(ldb
)) == 0) {
102 if (security_token_has_sid(token
, da_sid
)) {
103 dag_sid
= dom_sid_dup(mem_ctx
, da_sid
);
104 } else if (security_token_has_sid(token
, ea_sid
)) {
105 dag_sid
= dom_sid_dup(mem_ctx
, ea_sid
);
113 talloc_free(tmp_ctx
);
117 static struct security_descriptor
*get_sd_unpacked(struct ldb_module
*module
, TALLOC_CTX
*mem_ctx
,
118 const struct dsdb_class
*objectclass
)
120 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
121 struct security_descriptor
*sd
;
122 const struct dom_sid
*domain_sid
= samdb_domain_sid(ldb
);
124 if (!objectclass
->defaultSecurityDescriptor
|| !domain_sid
) {
128 sd
= sddl_decode(mem_ctx
,
129 objectclass
->defaultSecurityDescriptor
,
134 static struct dom_sid
*get_default_group(TALLOC_CTX
*mem_ctx
,
135 struct ldb_context
*ldb
,
138 if (dsdb_functional_level(ldb
) >= DS_DOMAIN_FUNCTION_2008
) {
145 static struct security_descriptor
*descr_handle_sd_flags(TALLOC_CTX
*mem_ctx
,
146 struct security_descriptor
*new_sd
,
147 struct security_descriptor
*old_sd
,
150 struct security_descriptor
*final_sd
;
151 /* if there is no control or control == 0 modify everything */
156 final_sd
= talloc_zero(mem_ctx
, struct security_descriptor
);
157 final_sd
->revision
= SECURITY_DESCRIPTOR_REVISION_1
;
158 final_sd
->type
= SEC_DESC_SELF_RELATIVE
;
160 if (sd_flags
& (SECINFO_OWNER
)) {
161 final_sd
->owner_sid
= talloc_memdup(mem_ctx
, new_sd
->owner_sid
, sizeof(struct dom_sid
));
162 final_sd
->type
|= new_sd
->type
& SEC_DESC_OWNER_DEFAULTED
;
165 final_sd
->owner_sid
= talloc_memdup(mem_ctx
, old_sd
->owner_sid
, sizeof(struct dom_sid
));
166 final_sd
->type
|= old_sd
->type
& SEC_DESC_OWNER_DEFAULTED
;
169 if (sd_flags
& (SECINFO_GROUP
)) {
170 final_sd
->group_sid
= talloc_memdup(mem_ctx
, new_sd
->group_sid
, sizeof(struct dom_sid
));
171 final_sd
->type
|= new_sd
->type
& SEC_DESC_GROUP_DEFAULTED
;
174 final_sd
->group_sid
= talloc_memdup(mem_ctx
, old_sd
->group_sid
, sizeof(struct dom_sid
));
175 final_sd
->type
|= old_sd
->type
& SEC_DESC_GROUP_DEFAULTED
;
178 if (sd_flags
& (SECINFO_SACL
)) {
179 final_sd
->sacl
= security_acl_dup(mem_ctx
,new_sd
->sacl
);
180 final_sd
->type
|= new_sd
->type
& (SEC_DESC_SACL_PRESENT
|
181 SEC_DESC_SACL_DEFAULTED
|SEC_DESC_SACL_AUTO_INHERIT_REQ
|
182 SEC_DESC_SACL_AUTO_INHERITED
|SEC_DESC_SACL_PROTECTED
|
183 SEC_DESC_SERVER_SECURITY
);
185 else if (old_sd
&& old_sd
->sacl
) {
186 final_sd
->sacl
= security_acl_dup(mem_ctx
,old_sd
->sacl
);
187 final_sd
->type
|= old_sd
->type
& (SEC_DESC_SACL_PRESENT
|
188 SEC_DESC_SACL_DEFAULTED
|SEC_DESC_SACL_AUTO_INHERIT_REQ
|
189 SEC_DESC_SACL_AUTO_INHERITED
|SEC_DESC_SACL_PROTECTED
|
190 SEC_DESC_SERVER_SECURITY
);
193 if (sd_flags
& (SECINFO_DACL
)) {
194 final_sd
->dacl
= security_acl_dup(mem_ctx
,new_sd
->dacl
);
195 final_sd
->type
|= new_sd
->type
& (SEC_DESC_DACL_PRESENT
|
196 SEC_DESC_DACL_DEFAULTED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
|
197 SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_PROTECTED
|
198 SEC_DESC_DACL_TRUSTED
);
200 else if (old_sd
&& old_sd
->dacl
) {
201 final_sd
->dacl
= security_acl_dup(mem_ctx
,old_sd
->dacl
);
202 final_sd
->type
|= old_sd
->type
& (SEC_DESC_DACL_PRESENT
|
203 SEC_DESC_DACL_DEFAULTED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
|
204 SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_PROTECTED
|
205 SEC_DESC_DACL_TRUSTED
);
207 /* not so sure about this */
208 final_sd
->type
|= new_sd
->type
& SEC_DESC_RM_CONTROL_VALID
;
212 static DATA_BLOB
*get_new_descriptor(struct ldb_module
*module
,
215 const struct dsdb_class
*objectclass
,
216 const struct ldb_val
*parent
,
217 const struct ldb_val
*object
,
218 const struct ldb_val
*old_sd
,
221 struct security_descriptor
*user_descriptor
= NULL
, *parent_descriptor
= NULL
;
222 struct security_descriptor
*old_descriptor
= NULL
;
223 struct security_descriptor
*new_sd
, *final_sd
;
224 DATA_BLOB
*linear_sd
;
225 enum ndr_err_code ndr_err
;
226 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
227 struct auth_session_info
*session_info
228 = ldb_get_opaque(ldb
, "sessionInfo");
229 const struct dom_sid
*domain_sid
= samdb_domain_sid(ldb
);
231 struct dom_sid
*default_owner
;
232 struct dom_sid
*default_group
;
235 user_descriptor
= talloc(mem_ctx
, struct security_descriptor
);
236 if (!user_descriptor
) {
239 ndr_err
= ndr_pull_struct_blob(object
, user_descriptor
,
241 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
243 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
244 talloc_free(user_descriptor
);
248 user_descriptor
= get_sd_unpacked(module
, mem_ctx
, objectclass
);
252 old_descriptor
= talloc(mem_ctx
, struct security_descriptor
);
253 if (!old_descriptor
) {
256 ndr_err
= ndr_pull_struct_blob(old_sd
, old_descriptor
,
258 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
260 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
261 talloc_free(old_descriptor
);
267 parent_descriptor
= talloc(mem_ctx
, struct security_descriptor
);
268 if (!parent_descriptor
) {
271 ndr_err
= ndr_pull_struct_blob(parent
, parent_descriptor
,
273 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
275 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
276 talloc_free(parent_descriptor
);
281 default_owner
= get_default_ag(mem_ctx
, dn
,
282 session_info
->security_token
, ldb
);
283 default_group
= get_default_group(mem_ctx
, ldb
, default_owner
);
284 new_sd
= create_security_descriptor(mem_ctx
, parent_descriptor
, user_descriptor
, true,
285 NULL
, SEC_DACL_AUTO_INHERIT
|SEC_SACL_AUTO_INHERIT
,
286 session_info
->security_token
,
287 default_owner
, default_group
,
288 map_generic_rights_ds
);
292 final_sd
= descr_handle_sd_flags(mem_ctx
, new_sd
, old_descriptor
, sd_flags
);
298 if (final_sd
->dacl
) {
299 final_sd
->dacl
->revision
= SECURITY_ACL_REVISION_ADS
;
301 if (final_sd
->sacl
) {
302 final_sd
->sacl
->revision
= SECURITY_ACL_REVISION_ADS
;
305 sddl_sd
= sddl_encode(mem_ctx
, final_sd
, domain_sid
);
306 DEBUG(10, ("Object %s created with desriptor %s\n\n", ldb_dn_get_linearized(dn
), sddl_sd
));
308 linear_sd
= talloc(mem_ctx
, DATA_BLOB
);
313 ndr_err
= ndr_push_struct_blob(linear_sd
, mem_ctx
,
315 (ndr_push_flags_fn_t
)ndr_push_security_descriptor
);
316 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
323 static DATA_BLOB
*descr_get_descriptor_to_show(struct ldb_module
*module
,
328 struct security_descriptor
*old_sd
, *final_sd
;
329 DATA_BLOB
*linear_sd
;
330 enum ndr_err_code ndr_err
;
332 old_sd
= talloc(mem_ctx
, struct security_descriptor
);
336 ndr_err
= ndr_pull_struct_blob(sd
, old_sd
,
338 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
340 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
345 final_sd
= descr_handle_sd_flags(mem_ctx
, old_sd
, NULL
, sd_flags
);
351 linear_sd
= talloc(mem_ctx
, DATA_BLOB
);
356 ndr_err
= ndr_push_struct_blob(linear_sd
, mem_ctx
,
358 (ndr_push_flags_fn_t
)ndr_push_security_descriptor
);
359 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
366 static struct descriptor_context
*descriptor_init_context(struct ldb_module
*module
,
367 struct ldb_request
*req
)
369 struct ldb_context
*ldb
;
370 struct descriptor_context
*ac
;
372 ldb
= ldb_module_get_ctx(module
);
374 ac
= talloc_zero(req
, struct descriptor_context
);
376 ldb_set_errstring(ldb
, "Out of Memory");
385 static int descriptor_search_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
387 struct descriptor_context
*ac
;
388 struct ldb_control
*sd_control
;
389 struct ldb_val
*sd_val
= NULL
;
390 struct ldb_message_element
*sd_el
;
393 uint32_t sd_flags
= 0;
395 ac
= talloc_get_type(req
->context
, struct descriptor_context
);
398 ret
= LDB_ERR_OPERATIONS_ERROR
;
401 if (ares
->error
!= LDB_SUCCESS
) {
402 return ldb_module_done(ac
->req
, ares
->controls
,
403 ares
->response
, ares
->error
);
406 sd_control
= ldb_request_get_control(ac
->req
, LDB_CONTROL_SD_FLAGS_OID
);
408 struct ldb_sd_flags_control
*sdctr
= (struct ldb_sd_flags_control
*)sd_control
->data
;
409 sd_flags
= sdctr
->secinfo_flags
;
410 /* we only care for the last 4 bits */
411 sd_flags
= sd_flags
& 0x0000000F;
413 /* MS-ADTS 3.1.1.3.4.1.11 says that no bits
419 switch (ares
->type
) {
420 case LDB_REPLY_ENTRY
:
422 sd_el
= ldb_msg_find_element(ares
->message
, "nTSecurityDescriptor");
424 sd_val
= sd_el
->values
;
428 show_sd
= descr_get_descriptor_to_show(ac
->module
, ac
->req
,
431 ret
= LDB_ERR_OPERATIONS_ERROR
;
434 ldb_msg_remove_attr(ares
->message
, "nTSecurityDescriptor");
435 ret
= ldb_msg_add_steal_value(ares
->message
, "nTSecurityDescriptor", show_sd
);
436 if (ret
!= LDB_SUCCESS
) {
440 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
442 case LDB_REPLY_REFERRAL
:
443 return ldb_module_send_referral(ac
->req
, ares
->referral
);
446 return ldb_module_done(ac
->req
, ares
->controls
,
447 ares
->response
, ares
->error
);
452 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
455 static int descriptor_add(struct ldb_module
*module
, struct ldb_request
*req
)
457 struct ldb_context
*ldb
;
458 struct ldb_request
*add_req
;
459 struct ldb_message
*msg
;
460 struct ldb_result
*parent_res
;
461 const struct ldb_val
*parent_sd
= NULL
;
462 const struct ldb_val
*user_sd
;
463 struct ldb_dn
*parent_dn
, *dn
, *nc_root
;
464 struct ldb_message_element
*objectclass_element
, *sd_element
;
466 const struct dsdb_schema
*schema
;
468 const struct dsdb_class
*objectclass
;
469 static const char * const parent_attrs
[] = { "nTSecurityDescriptor", NULL
};
470 uint32_t instanceType
;
473 ldb
= ldb_module_get_ctx(module
);
474 dn
= req
->op
.add
.message
->dn
;
475 user_sd
= ldb_msg_find_ldb_val(req
->op
.add
.message
, "nTSecurityDescriptor");
476 sd_element
= ldb_msg_find_element(req
->op
.add
.message
, "nTSecurityDescriptor");
477 /* nTSecurityDescriptor without a value is an error, letting through so it is handled */
478 if (user_sd
== NULL
&& sd_element
) {
479 return ldb_next_request(module
, req
);
482 ldb_debug(ldb
, LDB_DEBUG_TRACE
,"descriptor_add: %s\n", ldb_dn_get_linearized(dn
));
484 /* do not manipulate our control entries */
485 if (ldb_dn_is_special(dn
)) {
486 return ldb_next_request(module
, req
);
489 instanceType
= ldb_msg_find_attr_as_uint(req
->op
.add
.message
, "instanceType", 0);
491 if (instanceType
& INSTANCE_TYPE_IS_NC_HEAD
) {
496 ret
= dsdb_find_nc_root(ldb
, req
, dn
, &nc_root
);
497 if (ret
!= LDB_SUCCESS
) {
498 ldb_debug(ldb
, LDB_DEBUG_TRACE
,"descriptor_add: Could not find NC root for %s\n",
499 ldb_dn_get_linearized(dn
));
503 if (ldb_dn_compare(dn
, nc_root
) == 0) {
504 DEBUG(0, ("Found DN %s being a NC by the old method\n", ldb_dn_get_linearized(dn
)));
510 DEBUG(2, ("DN: %s is a NC\n", ldb_dn_get_linearized(dn
)));
513 /* if the object has a parent, retrieve its SD to
514 * use for calculation. Unfortunately we do not yet have
515 * instanceType, so we use dsdb_find_nc_root. */
517 parent_dn
= ldb_dn_get_parent(req
, dn
);
518 if (parent_dn
== NULL
) {
522 /* we aren't any NC */
523 ret
= dsdb_module_search_dn(module
, req
, &parent_res
, parent_dn
,
525 DSDB_FLAG_NEXT_MODULE
,
527 if (ret
!= LDB_SUCCESS
) {
528 ldb_debug(ldb
, LDB_DEBUG_TRACE
,"descriptor_add: Could not find SD for %s\n",
529 ldb_dn_get_linearized(parent_dn
));
532 if (parent_res
->count
!= 1) {
533 return ldb_operr(ldb
);
535 parent_sd
= ldb_msg_find_ldb_val(parent_res
->msgs
[0], "nTSecurityDescriptor");
538 schema
= dsdb_get_schema(ldb
, req
);
540 objectclass_element
= ldb_msg_find_element(req
->op
.add
.message
, "objectClass");
541 if (objectclass_element
== NULL
) {
542 return ldb_operr(ldb
);
545 objectclass
= get_last_structural_class(schema
, objectclass_element
, req
);
546 if (objectclass
== NULL
) {
547 return ldb_operr(ldb
);
550 sd
= get_new_descriptor(module
, dn
, req
,
551 objectclass
, parent_sd
,
553 msg
= ldb_msg_copy_shallow(req
, req
->op
.add
.message
);
555 if (sd_element
!= NULL
) {
556 sd_element
->values
[0] = *sd
;
558 ret
= ldb_msg_add_steal_value(msg
,
559 "nTSecurityDescriptor",
561 if (ret
!= LDB_SUCCESS
) {
567 ret
= ldb_build_add_req(&add_req
, ldb
, req
,
570 req
, dsdb_next_callback
,
572 LDB_REQ_SET_LOCATION(add_req
);
573 if (ret
!= LDB_SUCCESS
) {
574 return ldb_error(ldb
, ret
,
575 "descriptor_add: Error creating new add request.");
578 return ldb_next_request(module
, add_req
);
581 static int descriptor_modify(struct ldb_module
*module
, struct ldb_request
*req
)
583 struct ldb_context
*ldb
;
584 struct ldb_control
*sd_recalculate_control
, *sd_flags_control
;
585 struct ldb_request
*mod_req
;
586 struct ldb_message
*msg
;
587 struct ldb_result
*current_res
, *parent_res
;
588 const struct ldb_val
*old_sd
= NULL
;
589 const struct ldb_val
*parent_sd
= NULL
;
590 const struct ldb_val
*user_sd
;
591 struct ldb_dn
*parent_dn
, *dn
;
592 struct ldb_message_element
*objectclass_element
;
594 uint32_t instanceType
, sd_flags
= 0;
595 const struct dsdb_schema
*schema
;
597 const struct dsdb_class
*objectclass
;
598 static const char * const parent_attrs
[] = { "nTSecurityDescriptor", NULL
};
599 static const char * const current_attrs
[] = { "nTSecurityDescriptor",
601 "objectClass", NULL
};
602 ldb
= ldb_module_get_ctx(module
);
603 dn
= req
->op
.mod
.message
->dn
;
604 user_sd
= ldb_msg_find_ldb_val(req
->op
.mod
.message
, "nTSecurityDescriptor");
605 /* This control forces the recalculation of the SD also when
606 * no modification is performed. */
607 sd_recalculate_control
= ldb_request_get_control(req
,
608 LDB_CONTROL_RECALCULATE_SD_OID
);
609 if (!user_sd
&& !sd_recalculate_control
) {
610 return ldb_next_request(module
, req
);
613 ldb_debug(ldb
, LDB_DEBUG_TRACE
,"descriptor_modify: %s\n", ldb_dn_get_linearized(dn
));
615 /* do not manipulate our control entries */
616 if (ldb_dn_is_special(dn
)) {
617 return ldb_next_request(module
, req
);
620 ret
= dsdb_module_search_dn(module
, req
, ¤t_res
, dn
,
622 DSDB_FLAG_NEXT_MODULE
,
624 if (ret
!= LDB_SUCCESS
) {
625 ldb_debug(ldb
, LDB_DEBUG_ERROR
,"descriptor_modify: Could not find %s\n",
626 ldb_dn_get_linearized(dn
));
630 instanceType
= ldb_msg_find_attr_as_uint(current_res
->msgs
[0],
632 /* if the object has a parent, retrieve its SD to
633 * use for calculation */
634 if (!ldb_dn_is_null(current_res
->msgs
[0]->dn
) &&
635 !(instanceType
& INSTANCE_TYPE_IS_NC_HEAD
)) {
636 parent_dn
= ldb_dn_get_parent(req
, dn
);
637 if (parent_dn
== NULL
) {
640 ret
= dsdb_module_search_dn(module
, req
, &parent_res
, parent_dn
,
642 DSDB_FLAG_NEXT_MODULE
,
644 if (ret
!= LDB_SUCCESS
) {
645 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "descriptor_modify: Could not find SD for %s\n",
646 ldb_dn_get_linearized(parent_dn
));
649 if (parent_res
->count
!= 1) {
650 return ldb_operr(ldb
);
652 parent_sd
= ldb_msg_find_ldb_val(parent_res
->msgs
[0], "nTSecurityDescriptor");
654 sd_flags_control
= ldb_request_get_control(req
, LDB_CONTROL_SD_FLAGS_OID
);
656 schema
= dsdb_get_schema(ldb
, req
);
658 objectclass_element
= ldb_msg_find_element(current_res
->msgs
[0], "objectClass");
659 if (objectclass_element
== NULL
) {
660 return ldb_operr(ldb
);
663 objectclass
= get_last_structural_class(schema
, objectclass_element
, req
);
664 if (objectclass
== NULL
) {
665 return ldb_operr(ldb
);
668 if (sd_flags_control
) {
669 struct ldb_sd_flags_control
*sdctr
= (struct ldb_sd_flags_control
*)sd_flags_control
->data
;
670 sd_flags
= sdctr
->secinfo_flags
;
671 /* we only care for the last 4 bits */
672 sd_flags
= sd_flags
& 0x0000000F;
675 old_sd
= ldb_msg_find_ldb_val(current_res
->msgs
[0], "nTSecurityDescriptor");
678 sd
= get_new_descriptor(module
, dn
, req
,
679 objectclass
, parent_sd
,
680 user_sd
, old_sd
, sd_flags
);
681 msg
= ldb_msg_copy_shallow(req
, req
->op
.mod
.message
);
683 struct ldb_message_element
*sd_element
;
684 if (user_sd
!= NULL
) {
685 sd_element
= ldb_msg_find_element(msg
,
686 "nTSecurityDescriptor");
687 sd_element
->values
[0] = *sd
;
688 } else if (sd_recalculate_control
!= NULL
) {
689 /* In this branch we really do force the recalculation
691 ldb_msg_remove_attr(msg
, "nTSecurityDescriptor");
693 ret
= ldb_msg_add_steal_value(msg
,
694 "nTSecurityDescriptor",
696 if (ret
!= LDB_SUCCESS
) {
697 return ldb_error(ldb
, ret
,
698 "descriptor_modify: Could not replace SD value in message.");
700 sd_element
= ldb_msg_find_element(msg
,
701 "nTSecurityDescriptor");
702 sd_element
->flags
= LDB_FLAG_MOD_REPLACE
;
706 /* mark the controls as non-critical since we've handled them */
707 if (sd_flags_control
!= NULL
) {
708 sd_flags_control
->critical
= 0;
710 if (sd_recalculate_control
!= NULL
) {
711 sd_recalculate_control
->critical
= 0;
714 ret
= ldb_build_mod_req(&mod_req
, ldb
, req
,
720 LDB_REQ_SET_LOCATION(mod_req
);
721 if (ret
!= LDB_SUCCESS
) {
725 return ldb_next_request(module
, mod_req
);
728 static int descriptor_search(struct ldb_module
*module
, struct ldb_request
*req
)
731 struct ldb_context
*ldb
;
732 struct ldb_control
*sd_control
;
733 struct ldb_request
*down_req
;
734 struct descriptor_context
*ac
;
736 sd_control
= ldb_request_get_control(req
, LDB_CONTROL_SD_FLAGS_OID
);
738 return ldb_next_request(module
, req
);
741 ldb
= ldb_module_get_ctx(module
);
742 ac
= descriptor_init_context(module
, req
);
744 return ldb_operr(ldb
);
747 ret
= ldb_build_search_req_ex(&down_req
, ldb
, ac
,
749 req
->op
.search
.scope
,
751 req
->op
.search
.attrs
,
753 ac
, descriptor_search_callback
,
755 LDB_REQ_SET_LOCATION(down_req
);
756 if (ret
!= LDB_SUCCESS
) {
759 /* mark it as handled */
761 sd_control
->critical
= 0;
764 return ldb_next_request(ac
->module
, down_req
);
767 static int descriptor_rename(struct ldb_module
*module
, struct ldb_request
*req
)
769 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
770 ldb_debug(ldb
, LDB_DEBUG_TRACE
,"descriptor_rename: %s\n", ldb_dn_get_linearized(req
->op
.rename
.olddn
));
772 /* do not manipulate our control entries */
773 if (ldb_dn_is_special(req
->op
.rename
.olddn
)) {
774 return ldb_next_request(module
, req
);
777 return ldb_next_request(module
, req
);
780 static int descriptor_init(struct ldb_module
*module
)
782 int ret
= ldb_mod_register_control(module
, LDB_CONTROL_SD_FLAGS_OID
);
783 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
784 if (ret
!= LDB_SUCCESS
) {
785 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
786 "descriptor: Unable to register control with rootdse!\n");
787 return ldb_operr(ldb
);
789 return ldb_next_init(module
);
793 static const struct ldb_module_ops ldb_descriptor_module_ops
= {
794 .name
= "descriptor",
795 .search
= descriptor_search
,
796 .add
= descriptor_add
,
797 .modify
= descriptor_modify
,
798 .rename
= descriptor_rename
,
799 .init_context
= descriptor_init
802 int ldb_descriptor_module_init(const char *version
)
804 LDB_MODULE_CHECK_VERSION(version
);
805 return ldb_register_module(&ldb_descriptor_module_ops
);