s4:dsdb/descriptor: make use of dsdb_request_sd_flags()
[Samba/gebeck_regimport.git] / source4 / dsdb / samdb / ldb_modules / descriptor.c
bloba764d69aaff711ecd31aba13f06855d17bd5886a
1 /*
2 ldb database library
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/>.
23 * Name: ldb
25 * Component: DS Security descriptor module
27 * Description:
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
35 #include "includes.h"
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 "auth/auth.h"
43 #include "param/param.h"
44 #include "dsdb/samdb/ldb_modules/util.h"
46 struct descriptor_data {
47 int _dummy;
50 struct descriptor_context {
51 struct ldb_module *module;
52 struct ldb_request *req;
53 struct ldb_message *msg;
54 struct ldb_reply *search_res;
55 struct ldb_reply *search_oc_res;
56 struct ldb_val *parentsd_val;
57 struct ldb_message_element *sd_element;
58 struct ldb_val *sd_val;
59 uint32_t sd_flags;
60 int (*step_fn)(struct descriptor_context *);
63 static struct dom_sid *get_default_ag(TALLOC_CTX *mem_ctx,
64 struct ldb_dn *dn,
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;
75 int ret;
77 ret = dsdb_find_nc_root(ldb, tmp_ctx, dn, &nc_root);
78 if (ret != LDB_SUCCESS) {
79 talloc_free(tmp_ctx);
80 return NULL;
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);
90 } else {
91 dag_sid = NULL;
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);
98 } else {
99 dag_sid = NULL;
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);
106 } else {
107 dag_sid = NULL;
109 } else {
110 dag_sid = NULL;
113 talloc_free(tmp_ctx);
114 return dag_sid;
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) {
125 return NULL;
128 sd = sddl_decode(mem_ctx,
129 objectclass->defaultSecurityDescriptor,
130 domain_sid);
131 return sd;
134 static struct dom_sid *get_default_group(TALLOC_CTX *mem_ctx,
135 struct ldb_context *ldb,
136 struct dom_sid *dag)
138 if (dsdb_functional_level(ldb) >= DS_DOMAIN_FUNCTION_2008) {
139 return dag;
142 return NULL;
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,
148 uint32_t sd_flags)
150 struct security_descriptor *final_sd;
151 /* if there is no control or control == 0 modify everything */
152 if (!sd_flags) {
153 return new_sd;
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;
164 else if (old_sd) {
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;
173 else if (old_sd) {
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;
209 return final_sd;
212 static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
213 struct ldb_dn *dn,
214 TALLOC_CTX *mem_ctx,
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,
219 uint32_t sd_flags)
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);
230 char *sddl_sd;
231 struct dom_sid *default_owner;
232 struct dom_sid *default_group;
234 if (object) {
235 user_descriptor = talloc(mem_ctx, struct security_descriptor);
236 if (!user_descriptor) {
237 return NULL;
239 ndr_err = ndr_pull_struct_blob(object, user_descriptor,
240 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);
245 return NULL;
247 } else {
248 user_descriptor = get_sd_unpacked(module, mem_ctx, objectclass);
251 if (old_sd) {
252 old_descriptor = talloc(mem_ctx, struct security_descriptor);
253 if (!old_descriptor) {
254 return NULL;
256 ndr_err = ndr_pull_struct_blob(old_sd, old_descriptor,
257 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);
262 return NULL;
266 if (parent) {
267 parent_descriptor = talloc(mem_ctx, struct security_descriptor);
268 if (!parent_descriptor) {
269 return NULL;
271 ndr_err = ndr_pull_struct_blob(parent, parent_descriptor,
272 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);
277 return NULL;
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);
289 if (!new_sd) {
290 return NULL;
292 final_sd = descr_handle_sd_flags(mem_ctx, new_sd, old_descriptor, sd_flags);
294 if (!final_sd) {
295 return NULL;
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);
309 if (!linear_sd) {
310 return NULL;
313 ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
314 final_sd,
315 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
316 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
317 return NULL;
320 return linear_sd;
323 static DATA_BLOB *descr_get_descriptor_to_show(struct ldb_module *module,
324 TALLOC_CTX *mem_ctx,
325 struct ldb_val *sd,
326 uint32_t sd_flags)
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);
333 if (!old_sd) {
334 return NULL;
336 ndr_err = ndr_pull_struct_blob(sd, old_sd,
337 old_sd,
338 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
340 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
341 talloc_free(old_sd);
342 return NULL;
345 final_sd = descr_handle_sd_flags(mem_ctx, old_sd, NULL, sd_flags);
347 if (!final_sd) {
348 return NULL;
351 linear_sd = talloc(mem_ctx, DATA_BLOB);
352 if (!linear_sd) {
353 return NULL;
356 ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
357 final_sd,
358 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
359 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
360 return NULL;
363 return linear_sd;
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);
375 if (ac == NULL) {
376 ldb_set_errstring(ldb, "Out of Memory");
377 return NULL;
380 ac->module = module;
381 ac->req = req;
382 return ac;
385 static int descriptor_search_callback(struct ldb_request *req, struct ldb_reply *ares)
387 struct descriptor_context *ac;
388 struct ldb_val *sd_val = NULL;
389 struct ldb_message_element *sd_el;
390 DATA_BLOB *show_sd;
391 int ret;
393 ac = talloc_get_type(req->context, struct descriptor_context);
395 if (!ares) {
396 ret = LDB_ERR_OPERATIONS_ERROR;
397 goto fail;
399 if (ares->error != LDB_SUCCESS) {
400 return ldb_module_done(ac->req, ares->controls,
401 ares->response, ares->error);
404 switch (ares->type) {
405 case LDB_REPLY_ENTRY:
406 sd_el = ldb_msg_find_element(ares->message, "nTSecurityDescriptor");
407 if (sd_el) {
408 sd_val = sd_el->values;
411 if (sd_val) {
412 show_sd = descr_get_descriptor_to_show(ac->module, ac->req,
413 sd_val, ac->sd_flags);
414 if (!show_sd) {
415 ret = LDB_ERR_OPERATIONS_ERROR;
416 goto fail;
418 ldb_msg_remove_attr(ares->message, "nTSecurityDescriptor");
419 ret = ldb_msg_add_steal_value(ares->message, "nTSecurityDescriptor", show_sd);
420 if (ret != LDB_SUCCESS) {
421 goto fail;
424 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
426 case LDB_REPLY_REFERRAL:
427 return ldb_module_send_referral(ac->req, ares->referral);
429 case LDB_REPLY_DONE:
430 return ldb_module_done(ac->req, ares->controls,
431 ares->response, ares->error);
434 fail:
435 talloc_free(ares);
436 return ldb_module_done(ac->req, NULL, NULL, ret);
439 static int descriptor_add(struct ldb_module *module, struct ldb_request *req)
441 struct ldb_context *ldb;
442 struct ldb_request *add_req;
443 struct ldb_message *msg;
444 struct ldb_result *parent_res;
445 const struct ldb_val *parent_sd = NULL;
446 const struct ldb_val *user_sd;
447 struct ldb_dn *parent_dn, *dn, *nc_root;
448 struct ldb_message_element *objectclass_element, *sd_element;
449 int ret;
450 const struct dsdb_schema *schema;
451 DATA_BLOB *sd;
452 const struct dsdb_class *objectclass;
453 static const char * const parent_attrs[] = { "nTSecurityDescriptor", NULL };
454 uint32_t instanceType;
455 bool isNC = false;
456 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
458 ldb = ldb_module_get_ctx(module);
459 dn = req->op.add.message->dn;
460 user_sd = ldb_msg_find_ldb_val(req->op.add.message, "nTSecurityDescriptor");
461 sd_element = ldb_msg_find_element(req->op.add.message, "nTSecurityDescriptor");
462 /* nTSecurityDescriptor without a value is an error, letting through so it is handled */
463 if (user_sd == NULL && sd_element) {
464 return ldb_next_request(module, req);
467 ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_add: %s\n", ldb_dn_get_linearized(dn));
469 /* do not manipulate our control entries */
470 if (ldb_dn_is_special(dn)) {
471 return ldb_next_request(module, req);
474 instanceType = ldb_msg_find_attr_as_uint(req->op.add.message, "instanceType", 0);
476 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
477 isNC = true;
480 if (!isNC) {
481 ret = dsdb_find_nc_root(ldb, req, dn, &nc_root);
482 if (ret != LDB_SUCCESS) {
483 ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_add: Could not find NC root for %s\n",
484 ldb_dn_get_linearized(dn));
485 return ret;
488 if (ldb_dn_compare(dn, nc_root) == 0) {
489 DEBUG(0, ("Found DN %s being a NC by the old method\n", ldb_dn_get_linearized(dn)));
490 isNC = true;
494 if (isNC) {
495 DEBUG(2, ("DN: %s is a NC\n", ldb_dn_get_linearized(dn)));
497 if (!isNC) {
498 /* if the object has a parent, retrieve its SD to
499 * use for calculation. Unfortunately we do not yet have
500 * instanceType, so we use dsdb_find_nc_root. */
502 parent_dn = ldb_dn_get_parent(req, dn);
503 if (parent_dn == NULL) {
504 return ldb_oom(ldb);
507 /* we aren't any NC */
508 ret = dsdb_module_search_dn(module, req, &parent_res, parent_dn,
509 parent_attrs,
510 DSDB_FLAG_NEXT_MODULE |
511 DSDB_FLAG_AS_SYSTEM |
512 DSDB_SEARCH_SHOW_RECYCLED,
513 req);
514 if (ret != LDB_SUCCESS) {
515 ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_add: Could not find SD for %s\n",
516 ldb_dn_get_linearized(parent_dn));
517 return ret;
519 if (parent_res->count != 1) {
520 return ldb_operr(ldb);
522 parent_sd = ldb_msg_find_ldb_val(parent_res->msgs[0], "nTSecurityDescriptor");
525 schema = dsdb_get_schema(ldb, req);
527 objectclass_element = ldb_msg_find_element(req->op.add.message, "objectClass");
528 if (objectclass_element == NULL) {
529 return ldb_operr(ldb);
532 objectclass = dsdb_get_last_structural_class(schema,
533 objectclass_element);
534 if (objectclass == NULL) {
535 return ldb_operr(ldb);
538 sd = get_new_descriptor(module, dn, req,
539 objectclass, parent_sd,
540 user_sd, NULL, 0);
541 msg = ldb_msg_copy_shallow(req, req->op.add.message);
542 if (sd != NULL) {
543 if (sd_element != NULL) {
544 sd_element->values[0] = *sd;
545 } else {
546 ret = ldb_msg_add_steal_value(msg,
547 "nTSecurityDescriptor",
548 sd);
549 if (ret != LDB_SUCCESS) {
550 return ret;
555 ret = ldb_build_add_req(&add_req, ldb, req,
556 msg,
557 req->controls,
558 req, dsdb_next_callback,
559 req);
560 LDB_REQ_SET_LOCATION(add_req);
561 if (ret != LDB_SUCCESS) {
562 return ldb_error(ldb, ret,
563 "descriptor_add: Error creating new add request.");
566 return ldb_next_request(module, add_req);
569 static int descriptor_modify(struct ldb_module *module, struct ldb_request *req)
571 struct ldb_context *ldb;
572 struct ldb_control *sd_recalculate_control;
573 struct ldb_request *mod_req;
574 struct ldb_message *msg;
575 struct ldb_result *current_res, *parent_res;
576 const struct ldb_val *old_sd = NULL;
577 const struct ldb_val *parent_sd = NULL;
578 const struct ldb_val *user_sd;
579 struct ldb_dn *parent_dn, *dn;
580 struct ldb_message_element *objectclass_element;
581 int ret;
582 uint32_t instanceType;
583 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
584 const struct dsdb_schema *schema;
585 DATA_BLOB *sd;
586 const struct dsdb_class *objectclass;
587 static const char * const parent_attrs[] = { "nTSecurityDescriptor", NULL };
588 static const char * const current_attrs[] = { "nTSecurityDescriptor",
589 "instanceType",
590 "objectClass", NULL };
591 ldb = ldb_module_get_ctx(module);
592 dn = req->op.mod.message->dn;
593 user_sd = ldb_msg_find_ldb_val(req->op.mod.message, "nTSecurityDescriptor");
594 /* This control forces the recalculation of the SD also when
595 * no modification is performed. */
596 sd_recalculate_control = ldb_request_get_control(req,
597 LDB_CONTROL_RECALCULATE_SD_OID);
598 if (!user_sd && !sd_recalculate_control) {
599 return ldb_next_request(module, req);
602 ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_modify: %s\n", ldb_dn_get_linearized(dn));
604 /* do not manipulate our control entries */
605 if (ldb_dn_is_special(dn)) {
606 return ldb_next_request(module, req);
609 ret = dsdb_module_search_dn(module, req, &current_res, dn,
610 current_attrs,
611 DSDB_FLAG_NEXT_MODULE |
612 DSDB_FLAG_AS_SYSTEM |
613 DSDB_SEARCH_SHOW_RECYCLED,
614 req);
615 if (ret != LDB_SUCCESS) {
616 ldb_debug(ldb, LDB_DEBUG_ERROR,"descriptor_modify: Could not find %s\n",
617 ldb_dn_get_linearized(dn));
618 return ret;
621 instanceType = ldb_msg_find_attr_as_uint(current_res->msgs[0],
622 "instanceType", 0);
623 /* if the object has a parent, retrieve its SD to
624 * use for calculation */
625 if (!ldb_dn_is_null(current_res->msgs[0]->dn) &&
626 !(instanceType & INSTANCE_TYPE_IS_NC_HEAD)) {
627 parent_dn = ldb_dn_get_parent(req, dn);
628 if (parent_dn == NULL) {
629 return ldb_oom(ldb);
631 ret = dsdb_module_search_dn(module, req, &parent_res, parent_dn,
632 parent_attrs,
633 DSDB_FLAG_NEXT_MODULE |
634 DSDB_FLAG_AS_SYSTEM |
635 DSDB_SEARCH_SHOW_RECYCLED,
636 req);
637 if (ret != LDB_SUCCESS) {
638 ldb_debug(ldb, LDB_DEBUG_ERROR, "descriptor_modify: Could not find SD for %s\n",
639 ldb_dn_get_linearized(parent_dn));
640 return ret;
642 if (parent_res->count != 1) {
643 return ldb_operr(ldb);
645 parent_sd = ldb_msg_find_ldb_val(parent_res->msgs[0], "nTSecurityDescriptor");
648 schema = dsdb_get_schema(ldb, req);
650 objectclass_element = ldb_msg_find_element(current_res->msgs[0], "objectClass");
651 if (objectclass_element == NULL) {
652 return ldb_operr(ldb);
655 objectclass = dsdb_get_last_structural_class(schema,
656 objectclass_element);
657 if (objectclass == NULL) {
658 return ldb_operr(ldb);
661 old_sd = ldb_msg_find_ldb_val(current_res->msgs[0], "nTSecurityDescriptor");
663 sd = get_new_descriptor(module, dn, req,
664 objectclass, parent_sd,
665 user_sd, old_sd, sd_flags);
666 msg = ldb_msg_copy_shallow(req, req->op.mod.message);
667 if (sd != NULL) {
668 struct ldb_message_element *sd_element;
669 if (user_sd != NULL) {
670 sd_element = ldb_msg_find_element(msg,
671 "nTSecurityDescriptor");
672 sd_element->values[0] = *sd;
673 } else if (sd_recalculate_control != NULL) {
674 /* In this branch we really do force the recalculation
675 * of the SD */
676 ldb_msg_remove_attr(msg, "nTSecurityDescriptor");
678 ret = ldb_msg_add_steal_value(msg,
679 "nTSecurityDescriptor",
680 sd);
681 if (ret != LDB_SUCCESS) {
682 return ldb_error(ldb, ret,
683 "descriptor_modify: Could not replace SD value in message.");
685 sd_element = ldb_msg_find_element(msg,
686 "nTSecurityDescriptor");
687 sd_element->flags = LDB_FLAG_MOD_REPLACE;
691 /* mark the controls as non-critical since we've handled them */
692 if (sd_recalculate_control != NULL) {
693 sd_recalculate_control->critical = 0;
696 ret = ldb_build_mod_req(&mod_req, ldb, req,
697 msg,
698 req->controls,
699 req,
700 dsdb_next_callback,
701 req);
702 LDB_REQ_SET_LOCATION(mod_req);
703 if (ret != LDB_SUCCESS) {
704 return ret;
707 return ldb_next_request(module, mod_req);
710 static int descriptor_search(struct ldb_module *module, struct ldb_request *req)
712 int ret;
713 struct ldb_context *ldb;
714 struct ldb_request *down_req;
715 struct descriptor_context *ac;
716 bool explicit_sd_flags = false;
717 uint32_t sd_flags = dsdb_request_sd_flags(req, &explicit_sd_flags);
718 bool show_sd = explicit_sd_flags;
720 if (!show_sd &&
721 ldb_attr_in_list(req->op.search.attrs, "nTSecurityDescriptor"))
723 show_sd = true;
726 if (!show_sd) {
727 return ldb_next_request(module, req);
730 ldb = ldb_module_get_ctx(module);
731 ac = descriptor_init_context(module, req);
732 if (ac == NULL) {
733 return ldb_operr(ldb);
735 ac->sd_flags = sd_flags;
737 ret = ldb_build_search_req_ex(&down_req, ldb, ac,
738 req->op.search.base,
739 req->op.search.scope,
740 req->op.search.tree,
741 req->op.search.attrs,
742 req->controls,
743 ac, descriptor_search_callback,
744 ac->req);
745 LDB_REQ_SET_LOCATION(down_req);
746 if (ret != LDB_SUCCESS) {
747 return ret;
750 return ldb_next_request(ac->module, down_req);
752 /* TODO */
753 static int descriptor_rename(struct ldb_module *module, struct ldb_request *req)
755 struct ldb_context *ldb = ldb_module_get_ctx(module);
756 ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn));
758 /* do not manipulate our control entries */
759 if (ldb_dn_is_special(req->op.rename.olddn)) {
760 return ldb_next_request(module, req);
763 return ldb_next_request(module, req);
766 static int descriptor_init(struct ldb_module *module)
768 int ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
769 struct ldb_context *ldb = ldb_module_get_ctx(module);
770 if (ret != LDB_SUCCESS) {
771 ldb_debug(ldb, LDB_DEBUG_ERROR,
772 "descriptor: Unable to register control with rootdse!\n");
773 return ldb_operr(ldb);
775 return ldb_next_init(module);
779 static const struct ldb_module_ops ldb_descriptor_module_ops = {
780 .name = "descriptor",
781 .search = descriptor_search,
782 .add = descriptor_add,
783 .modify = descriptor_modify,
784 .rename = descriptor_rename,
785 .init_context = descriptor_init
788 int ldb_descriptor_module_init(const char *version)
790 LDB_MODULE_CHECK_VERSION(version);
791 return ldb_register_module(&ldb_descriptor_module_ops);