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 "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"
45 struct descriptor_data {
48 struct descriptor_context {
49 struct ldb_module *module;
50 struct ldb_request *req;
51 struct ldb_reply *search_res;
52 int (*step_fn)(struct descriptor_context *);
55 static const struct dsdb_class * get_last_structural_class(const struct dsdb_schema *schema, struct ldb_message_element *element)
57 const struct dsdb_class *last_class = NULL;
59 for (i = 0; i < element->num_values; i++){
61 last_class = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &element->values[i]);
63 const struct dsdb_class *tmp_class = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &element->values[i]);
64 if (tmp_class->subClass_order > last_class->subClass_order)
65 last_class = tmp_class;
71 struct dom_sid *get_default_ag(TALLOC_CTX *mem_ctx,
73 struct security_token *token,
74 struct ldb_context *ldb)
76 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
77 struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
78 struct ldb_dn *schema_base_dn = ldb_get_schema_basedn(ldb);
79 struct ldb_dn *config_base_dn = ldb_get_config_basedn(ldb);
80 const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
81 struct dom_sid *da_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_ADMINS);
82 struct dom_sid *ea_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_ENTERPRISE_ADMINS);
83 struct dom_sid *sa_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_SCHEMA_ADMINS);
84 struct dom_sid *dag_sid;
86 if (ldb_dn_compare_base(schema_base_dn, dn) == 0){
87 if (security_token_has_sid(token, sa_sid))
88 dag_sid = dom_sid_dup(mem_ctx, sa_sid);
89 else if (security_token_has_sid(token, ea_sid))
90 dag_sid = dom_sid_dup(mem_ctx, ea_sid);
91 else if (security_token_has_sid(token, da_sid))
92 dag_sid = dom_sid_dup(mem_ctx, da_sid);
96 else if (ldb_dn_compare_base(config_base_dn, dn) == 0){
97 if (security_token_has_sid(token, ea_sid))
98 dag_sid = dom_sid_dup(mem_ctx, ea_sid);
99 else if (security_token_has_sid(token, da_sid))
100 dag_sid = dom_sid_dup(mem_ctx, da_sid);
104 else if (ldb_dn_compare_base(root_base_dn, dn) == 0){
105 if (security_token_has_sid(token, da_sid))
106 dag_sid = dom_sid_dup(mem_ctx, da_sid);
107 else if (security_token_has_sid(token, ea_sid))
108 dag_sid = dom_sid_dup(mem_ctx, ea_sid);
115 talloc_free(tmp_ctx);
119 static struct security_descriptor *get_sd_unpacked(struct ldb_module *module, TALLOC_CTX *mem_ctx,
120 const struct dsdb_class *objectclass)
122 struct ldb_context *ldb = ldb_module_get_ctx(module);
123 struct security_descriptor *sd;
124 const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
126 if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
130 sd = sddl_decode(mem_ctx,
131 objectclass->defaultSecurityDescriptor,
136 static struct dom_sid *get_default_group(TALLOC_CTX *mem_ctx,
137 struct ldb_context *ldb,
140 int *domainFunctionality;
142 domainFunctionality = talloc_get_type(
143 ldb_get_opaque(ldb, "domainFunctionality"), int);
145 if (*domainFunctionality
146 && (*domainFunctionality >= DS_DOMAIN_FUNCTION_2008)) {
153 static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
156 const struct dsdb_class *objectclass,
157 const struct ldb_val *parent,
158 struct ldb_val *object)
160 struct security_descriptor *user_descriptor = NULL, *parent_descriptor = NULL;
161 struct security_descriptor *new_sd;
162 DATA_BLOB *linear_sd;
163 enum ndr_err_code ndr_err;
164 struct ldb_context *ldb = ldb_module_get_ctx(module);
165 struct auth_session_info *session_info
166 = ldb_get_opaque(ldb, "sessionInfo");
167 const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
169 struct dom_sid *default_owner;
170 struct dom_sid *default_group;
173 user_descriptor = talloc(mem_ctx, struct security_descriptor);
176 ndr_err = ndr_pull_struct_blob(object, user_descriptor, NULL,
178 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
180 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)){
181 talloc_free(user_descriptor);
186 user_descriptor = get_sd_unpacked(module, mem_ctx, objectclass);
189 parent_descriptor = talloc(mem_ctx, struct security_descriptor);
190 if(!parent_descriptor)
192 ndr_err = ndr_pull_struct_blob(parent, parent_descriptor, NULL,
194 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
196 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)){
197 talloc_free(parent_descriptor);
201 default_owner = get_default_ag(mem_ctx, dn,
202 session_info->security_token, ldb);
203 default_group = get_default_group(mem_ctx, ldb, default_owner);
204 new_sd = create_security_descriptor(mem_ctx, parent_descriptor, user_descriptor, true,
205 NULL, SEC_DACL_AUTO_INHERIT|SEC_SACL_AUTO_INHERIT,
206 session_info->security_token,
207 default_owner, default_group,
208 map_generic_rights_ds);
213 sddl_sd = sddl_encode(mem_ctx, new_sd, domain_sid);
214 DEBUG(10, ("Object %s created with desriptor %s", ldb_dn_get_linearized(dn), sddl_sd));
216 linear_sd = talloc(mem_ctx, DATA_BLOB);
221 ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
222 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
224 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
225 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
232 static struct descriptor_context *descriptor_init_context(struct ldb_module *module,
233 struct ldb_request *req)
235 struct ldb_context *ldb;
236 struct descriptor_context *ac;
238 ldb = ldb_module_get_ctx(module);
240 ac = talloc_zero(req, struct descriptor_context);
242 ldb_set_errstring(ldb, "Out of Memory");
251 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
253 struct ldb_context *ldb;
254 struct descriptor_context *ac;
257 ac = talloc_get_type(req->context, struct descriptor_context);
258 ldb = ldb_module_get_ctx(ac->module);
261 return ldb_module_done(ac->req, NULL, NULL,
262 LDB_ERR_OPERATIONS_ERROR);
264 if (ares->error != LDB_SUCCESS &&
265 ares->error != LDB_ERR_NO_SUCH_OBJECT) {
266 return ldb_module_done(ac->req, ares->controls,
267 ares->response, ares->error);
270 switch (ares->type) {
271 case LDB_REPLY_ENTRY:
272 if (ac->search_res != NULL) {
273 ldb_set_errstring(ldb, "Too many results");
275 return ldb_module_done(ac->req, NULL, NULL,
276 LDB_ERR_OPERATIONS_ERROR);
279 ac->search_res = talloc_steal(ac, ares);
282 case LDB_REPLY_REFERRAL:
289 ret = ac->step_fn(ac);
290 if (ret != LDB_SUCCESS) {
291 return ldb_module_done(ac->req, NULL, NULL, ret);
298 static int descriptor_op_callback(struct ldb_request *req, struct ldb_reply *ares)
300 struct descriptor_context *ac;
302 ac = talloc_get_type(req->context, struct descriptor_context);
305 return ldb_module_done(ac->req, NULL, NULL,
306 LDB_ERR_OPERATIONS_ERROR);
308 if (ares->error != LDB_SUCCESS) {
309 return ldb_module_done(ac->req, ares->controls,
310 ares->response, ares->error);
313 if (ares->type != LDB_REPLY_DONE) {
315 return ldb_module_done(ac->req, NULL, NULL,
316 LDB_ERR_OPERATIONS_ERROR);
319 return ldb_module_done(ac->req, ares->controls,
320 ares->response, ares->error);
323 static int descriptor_do_add(struct descriptor_context *ac)
325 struct ldb_context *ldb;
326 const struct dsdb_schema *schema;
327 struct ldb_request *add_req;
328 struct ldb_message_element *objectclass_element, *sd_element = NULL;
329 struct ldb_message *msg;
332 struct ldb_val *sd_val = NULL;
333 const struct ldb_val *parentsd_val = NULL;
335 const struct dsdb_class *objectclass;
337 ldb = ldb_module_get_ctx(ac->module);
338 schema = dsdb_get_schema(ldb);
340 mem_ctx = talloc_new(ac);
341 if (mem_ctx == NULL) {
342 return LDB_ERR_OPERATIONS_ERROR;
345 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
347 /* get the security descriptor values*/
348 sd_element = ldb_msg_find_element(msg, "nTSecurityDescriptor");
349 objectclass_element = ldb_msg_find_element(msg, "objectClass");
350 objectclass = get_last_structural_class(schema, objectclass_element);
353 return LDB_ERR_OPERATIONS_ERROR;
356 sd_val = &sd_element->values[0];
357 /* NC's have no parent */
358 if ((ldb_dn_compare(msg->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
359 (ldb_dn_compare(msg->dn, (ldb_get_config_basedn(ldb))) == 0) ||
360 (ldb_dn_compare(msg->dn, (ldb_get_root_basedn(ldb))) == 0))
362 else if (ac->search_res != NULL)
363 parentsd_val = ldb_msg_find_ldb_val(ac->search_res->message, "nTSecurityDescriptor");
366 /* get the parent descriptor and the one provided. If not provided, get the default.*/
367 /* convert to security descriptor and calculate */
368 sd = get_new_descriptor(ac->module, msg->dn, mem_ctx, objectclass,
369 parentsd_val, sd_val);
371 ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
374 talloc_free(mem_ctx);
375 ret = ldb_msg_sanity_check(ldb, msg);
377 if (ret != LDB_SUCCESS) {
381 ret = ldb_build_add_req(&add_req, ldb, ac,
384 ac, descriptor_op_callback,
386 if (ret != LDB_SUCCESS) {
390 /* perform the add */
391 return ldb_next_request(ac->module, add_req);
394 static int descriptor_add(struct ldb_module *module, struct ldb_request *req)
396 struct ldb_context *ldb;
397 struct ldb_request *search_req;
398 struct descriptor_context *ac;
399 struct ldb_dn *parent_dn;
401 struct descriptor_data *data;
402 static const char * const descr_attrs[] = { "nTSecurityDescriptor", NULL };
404 data = talloc_get_type(ldb_module_get_private(module), struct descriptor_data);
405 ldb = ldb_module_get_ctx(module);
407 ldb_debug(ldb, LDB_DEBUG_TRACE, "descriptor_add\n");
409 if (ldb_dn_is_special(req->op.add.message->dn)) {
410 return ldb_next_request(module, req);
413 ac = descriptor_init_context(module, req);
415 return LDB_ERR_OPERATIONS_ERROR;
418 /* If there isn't a parent, just go on to the add processing */
419 if (ldb_dn_get_comp_num(ac->req->op.add.message->dn) == 1) {
420 return descriptor_do_add(ac);
423 /* get copy of parent DN */
424 parent_dn = ldb_dn_get_parent(ac, ac->req->op.add.message->dn);
425 if (parent_dn == NULL) {
427 return LDB_ERR_OPERATIONS_ERROR;
430 ret = ldb_build_search_req(&search_req, ldb,
431 ac, parent_dn, LDB_SCOPE_BASE,
432 "(objectClass=*)", descr_attrs,
434 ac, get_search_callback,
436 if (ret != LDB_SUCCESS) {
439 talloc_steal(search_req, parent_dn);
441 ac->step_fn = descriptor_do_add;
443 return ldb_next_request(ac->module, search_req);
446 static int descriptor_modify(struct ldb_module *module, struct ldb_request *req)
448 struct ldb_context *ldb = ldb_module_get_ctx(module);
449 ldb_debug(ldb, LDB_DEBUG_TRACE, "descriptor_modify\n");
450 return ldb_next_request(module, req);
453 static int descriptor_rename(struct ldb_module *module, struct ldb_request *req)
455 struct ldb_context *ldb = ldb_module_get_ctx(module);
456 ldb_debug(ldb, LDB_DEBUG_TRACE, "descriptor_rename\n");
457 return ldb_next_request(module, req);
460 static int descriptor_init(struct ldb_module *module)
462 struct ldb_context *ldb;
463 struct descriptor_data *data;
465 ldb = ldb_module_get_ctx(module);
466 data = talloc(module, struct descriptor_data);
469 return LDB_ERR_OPERATIONS_ERROR;
472 ldb_module_set_private(module, data);
473 return ldb_next_init(module);
477 _PUBLIC_ const struct ldb_module_ops ldb_descriptor_module_ops = {
478 .name = "descriptor",
479 .add = descriptor_add,
480 .modify = descriptor_modify,
481 .rename = descriptor_rename,
482 .init_context = descriptor_init