Disable descriptor module unless enabled in smb.conf
[samba.git] / source4 / dsdb / samdb / ldb_modules / descriptor.c
1 /*
2    ldb database library
3
4    Copyright (C) Simo Sorce  2006-2008
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2007
6    Copyright (C) Nadezhda Ivanova  2009
7
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.
12
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.
17
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/>.
20 */
21
22 /*
23  *  Name: ldb
24  *
25  *  Component: DS Security descriptor module
26  *
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
31  *
32  *  Author: Nadezhda Ivanova
33  */
34
35 #include "includes.h"
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"
44
45 struct descriptor_data {
46         bool inherit;
47 };
48
49 struct descriptor_context {
50                 struct ldb_module *module;
51                 struct ldb_request *req;
52                 struct ldb_reply *search_res;
53                 int (*step_fn)(struct descriptor_context *);
54 };
55
56 static const struct dsdb_class * get_last_structural_class(const struct dsdb_schema *schema, struct ldb_message_element *element)
57 {
58         const struct dsdb_class *last_class = NULL;
59         int i;
60         for (i = 0; i < element->num_values; i++){
61                 if (!last_class)
62                         last_class = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &element->values[i]);
63                 else {
64                         const struct dsdb_class *tmp_class = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &element->values[i]);
65                         if (tmp_class->subClass_order > last_class->subClass_order)
66                                 last_class = tmp_class;
67                 }
68         }
69         return last_class;
70 }
71
72 struct dom_sid *get_default_ag(TALLOC_CTX *mem_ctx,
73                                struct ldb_dn *dn,
74                                struct security_token *token,
75                                struct ldb_context *ldb)
76 {
77         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
78         struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
79         struct ldb_dn *schema_base_dn = ldb_get_schema_basedn(ldb);
80         struct ldb_dn *config_base_dn = ldb_get_config_basedn(ldb);
81         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
82         struct dom_sid *da_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_ADMINS);
83         struct dom_sid *ea_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_ENTERPRISE_ADMINS);
84         struct dom_sid *sa_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_SCHEMA_ADMINS);
85         struct dom_sid *dag_sid;
86
87         if (ldb_dn_compare_base(schema_base_dn, dn) == 0){
88                 if (security_token_has_sid(token, sa_sid))
89                         dag_sid = dom_sid_dup(mem_ctx, sa_sid);
90                 else if (security_token_has_sid(token, ea_sid))
91                         dag_sid = dom_sid_dup(mem_ctx, ea_sid);
92                 else if (security_token_has_sid(token, da_sid))
93                         dag_sid = dom_sid_dup(mem_ctx, da_sid);
94                 else
95                         dag_sid = NULL;
96         }
97         else if (ldb_dn_compare_base(config_base_dn, dn) == 0){
98                 if (security_token_has_sid(token, ea_sid))
99                         dag_sid = dom_sid_dup(mem_ctx, ea_sid);
100                 else if (security_token_has_sid(token, da_sid))
101                         dag_sid = dom_sid_dup(mem_ctx, da_sid);
102                 else
103                         dag_sid = NULL;
104         }
105         else if (ldb_dn_compare_base(root_base_dn, dn) == 0){
106                 if (security_token_has_sid(token, da_sid))
107                         dag_sid = dom_sid_dup(mem_ctx, da_sid);
108                 else if (security_token_has_sid(token, ea_sid))
109                                 dag_sid = dom_sid_dup(mem_ctx, ea_sid);
110                 else
111                         dag_sid = NULL;
112         }
113         else
114                 dag_sid = NULL;
115
116         talloc_free(tmp_ctx);
117         return dag_sid;
118 }
119
120 static struct security_descriptor *get_sd_unpacked(struct ldb_module *module, TALLOC_CTX *mem_ctx,
121                                             const struct dsdb_class *objectclass)
122 {
123         struct ldb_context *ldb = ldb_module_get_ctx(module);
124         struct security_descriptor *sd;
125         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
126
127         if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
128                 return NULL;
129         }
130
131         sd = sddl_decode(mem_ctx,
132                          objectclass->defaultSecurityDescriptor,
133                          domain_sid);
134         return sd;
135 }
136
137 static struct dom_sid *get_default_group(TALLOC_CTX *mem_ctx,
138                                          struct ldb_context *ldb,
139                                          struct dom_sid *dag)
140 {
141         int *domainFunctionality;
142
143         domainFunctionality = talloc_get_type(
144                 ldb_get_opaque(ldb, "domainFunctionality"), int);
145
146         if (*domainFunctionality
147                         && (*domainFunctionality >= DS_DOMAIN_FUNCTION_2008)) {
148                 return dag;
149         }
150
151         return NULL;
152 }
153
154 static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
155                                      struct ldb_dn *dn,
156                                      TALLOC_CTX *mem_ctx,
157                                      const struct dsdb_class *objectclass,
158                                      const struct ldb_val *parent,
159                                      struct ldb_val *object)
160 {
161         struct security_descriptor *user_descriptor = NULL, *parent_descriptor = NULL;
162         struct security_descriptor *new_sd;
163         DATA_BLOB *linear_sd;
164         enum ndr_err_code ndr_err;
165         struct ldb_context *ldb = ldb_module_get_ctx(module);
166         struct auth_session_info *session_info
167                 = ldb_get_opaque(ldb, "sessionInfo");
168         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
169         char *sddl_sd;
170         struct dom_sid *default_owner;
171         struct dom_sid *default_group;
172
173         if (object){
174                 user_descriptor = talloc(mem_ctx, struct security_descriptor);
175                 if(!user_descriptor)
176                         return NULL;
177                 ndr_err = ndr_pull_struct_blob(object, user_descriptor, NULL,
178                                                user_descriptor,
179                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
180
181                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)){
182                         talloc_free(user_descriptor);
183                         return NULL;
184                 }
185         }
186         else
187                 user_descriptor = get_sd_unpacked(module, mem_ctx, objectclass);
188
189         if (parent){
190                 parent_descriptor = talloc(mem_ctx, struct security_descriptor);
191                 if(!parent_descriptor)
192                         return NULL;
193                 ndr_err = ndr_pull_struct_blob(parent, parent_descriptor, NULL,
194                                                parent_descriptor,
195                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
196
197                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)){
198                         talloc_free(parent_descriptor);
199                         return NULL;
200                 }
201         }
202         default_owner = get_default_ag(mem_ctx, dn,
203                                        session_info->security_token, ldb);
204         default_group = get_default_group(mem_ctx, ldb, default_owner);
205         new_sd = create_security_descriptor(mem_ctx, parent_descriptor, user_descriptor, true,
206                                             NULL, SEC_DACL_AUTO_INHERIT|SEC_SACL_AUTO_INHERIT,
207                                             session_info->security_token,
208                                             default_owner, default_group,
209                                             map_generic_rights_ds);
210         if (!new_sd)
211                 return NULL;
212
213
214         sddl_sd = sddl_encode(mem_ctx, new_sd, domain_sid);
215         DEBUG(10, ("Object %s created with desriptor %s", ldb_dn_get_linearized(dn), sddl_sd));
216
217         linear_sd = talloc(mem_ctx, DATA_BLOB);
218         if (!linear_sd) {
219                 return NULL;
220         }
221
222         ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
223                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
224                                        new_sd,
225                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
226         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
227                 return NULL;
228         }
229
230         return linear_sd;
231 }
232
233 static struct descriptor_context *descriptor_init_context(struct ldb_module *module,
234                                                           struct ldb_request *req)
235 {
236         struct ldb_context *ldb;
237         struct descriptor_context *ac;
238
239         ldb = ldb_module_get_ctx(module);
240
241         ac = talloc_zero(req, struct descriptor_context);
242         if (ac == NULL) {
243                 ldb_set_errstring(ldb, "Out of Memory");
244                 return NULL;
245         }
246
247         ac->module = module;
248         ac->req = req;
249         return ac;
250 }
251
252 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
253 {
254         struct ldb_context *ldb;
255         struct descriptor_context *ac;
256         int ret;
257
258         ac = talloc_get_type(req->context, struct descriptor_context);
259         ldb = ldb_module_get_ctx(ac->module);
260
261         if (!ares) {
262                 return ldb_module_done(ac->req, NULL, NULL,
263                                         LDB_ERR_OPERATIONS_ERROR);
264         }
265         if (ares->error != LDB_SUCCESS &&
266             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
267                 return ldb_module_done(ac->req, ares->controls,
268                                         ares->response, ares->error);
269         }
270
271         switch (ares->type) {
272         case LDB_REPLY_ENTRY:
273                 if (ac->search_res != NULL) {
274                         ldb_set_errstring(ldb, "Too many results");
275                         talloc_free(ares);
276                         return ldb_module_done(ac->req, NULL, NULL,
277                                                 LDB_ERR_OPERATIONS_ERROR);
278                 }
279
280                 ac->search_res = talloc_steal(ac, ares);
281                 break;
282
283         case LDB_REPLY_REFERRAL:
284                 /* ignore */
285                 talloc_free(ares);
286                 break;
287
288         case LDB_REPLY_DONE:
289                 talloc_free(ares);
290                 ret = ac->step_fn(ac);
291                 if (ret != LDB_SUCCESS) {
292                         return ldb_module_done(ac->req, NULL, NULL, ret);
293                 }
294                 break;
295         }
296
297         return LDB_SUCCESS;
298 }
299 static int descriptor_op_callback(struct ldb_request *req, struct ldb_reply *ares)
300 {
301         struct descriptor_context *ac;
302
303         ac = talloc_get_type(req->context, struct descriptor_context);
304
305         if (!ares) {
306                 return ldb_module_done(ac->req, NULL, NULL,
307                                         LDB_ERR_OPERATIONS_ERROR);
308         }
309         if (ares->error != LDB_SUCCESS) {
310                 return ldb_module_done(ac->req, ares->controls,
311                                         ares->response, ares->error);
312         }
313
314         if (ares->type != LDB_REPLY_DONE) {
315                 talloc_free(ares);
316                 return ldb_module_done(ac->req, NULL, NULL,
317                                         LDB_ERR_OPERATIONS_ERROR);
318         }
319
320         return ldb_module_done(ac->req, ares->controls,
321                                 ares->response, ares->error);
322 }
323
324 static int descriptor_do_add(struct descriptor_context *ac)
325 {
326         struct ldb_context *ldb;
327         const struct dsdb_schema *schema;
328         struct ldb_request *add_req;
329         struct ldb_message_element *objectclass_element, *sd_element = NULL;
330         struct ldb_message *msg;
331         TALLOC_CTX *mem_ctx;
332         int ret;
333         struct ldb_val *sd_val = NULL;
334         const struct ldb_val *parentsd_val = NULL;
335         DATA_BLOB *sd;
336         const struct dsdb_class *objectclass;
337
338         ldb = ldb_module_get_ctx(ac->module);
339         schema = dsdb_get_schema(ldb);
340
341         mem_ctx = talloc_new(ac);
342         if (mem_ctx == NULL) {
343                 return LDB_ERR_OPERATIONS_ERROR;
344         }
345
346         msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
347
348         /* get the security descriptor values*/
349         sd_element = ldb_msg_find_element(msg, "nTSecurityDescriptor");
350         objectclass_element = ldb_msg_find_element(msg, "objectClass");
351         objectclass = get_last_structural_class(schema, objectclass_element);
352
353         if (!objectclass)
354                 return LDB_ERR_OPERATIONS_ERROR;
355
356         if (sd_element)
357                 sd_val = &sd_element->values[0];
358         /* NC's have no parent */
359         if ((ldb_dn_compare(msg->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
360                         (ldb_dn_compare(msg->dn, (ldb_get_config_basedn(ldb))) == 0) ||
361                         (ldb_dn_compare(msg->dn, (ldb_get_root_basedn(ldb))) == 0))
362                 parentsd_val = NULL;
363         else if (ac->search_res != NULL)
364                 parentsd_val = ldb_msg_find_ldb_val(ac->search_res->message, "nTSecurityDescriptor");
365
366
367         /* get the parent descriptor and the one provided. If not provided, get the default.*/
368         /* convert to security descriptor and calculate */
369         sd = get_new_descriptor(ac->module, msg->dn, mem_ctx, objectclass,
370                         parentsd_val, sd_val);
371         if (sd) {
372                 ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
373         }
374
375         talloc_free(mem_ctx);
376         ret = ldb_msg_sanity_check(ldb, msg);
377
378         if (ret != LDB_SUCCESS) {
379                 return ret;
380         }
381
382         ret = ldb_build_add_req(&add_req, ldb, ac,
383                                 msg,
384                                 ac->req->controls,
385                                 ac, descriptor_op_callback,
386                                 ac->req);
387         if (ret != LDB_SUCCESS) {
388                 return ret;
389         }
390
391         /* perform the add */
392         return ldb_next_request(ac->module, add_req);
393 }
394
395 static int descriptor_add(struct ldb_module *module, struct ldb_request *req)
396 {
397         struct ldb_context *ldb;
398         struct ldb_request *search_req;
399         struct descriptor_context *ac;
400         struct ldb_dn *parent_dn;
401         int ret;
402         struct descriptor_data *data;
403         static const char * const descr_attrs[] = { "nTSecurityDescriptor", NULL };
404
405         data = talloc_get_type(ldb_module_get_private(module), struct descriptor_data);
406         ldb = ldb_module_get_ctx(module);
407
408         if (!data->inherit)
409                 return ldb_next_request(module, req);
410
411         ldb_debug(ldb, LDB_DEBUG_TRACE, "descriptor_add\n");
412
413         if (ldb_dn_is_special(req->op.add.message->dn)) {
414                 return ldb_next_request(module, req);
415         }
416
417         ac = descriptor_init_context(module, req);
418         if (ac == NULL) {
419                 return LDB_ERR_OPERATIONS_ERROR;
420         }
421
422         /* If there isn't a parent, just go on to the add processing */
423         if (ldb_dn_get_comp_num(ac->req->op.add.message->dn) == 1) {
424                 return descriptor_do_add(ac);
425         }
426
427         /* get copy of parent DN */
428         parent_dn = ldb_dn_get_parent(ac, ac->req->op.add.message->dn);
429         if (parent_dn == NULL) {
430                 ldb_oom(ldb);
431                 return LDB_ERR_OPERATIONS_ERROR;
432         }
433
434         ret = ldb_build_search_req(&search_req, ldb,
435                                    ac, parent_dn, LDB_SCOPE_BASE,
436                                    "(objectClass=*)", descr_attrs,
437                                    NULL,
438                                    ac, get_search_callback,
439                                    req);
440         if (ret != LDB_SUCCESS) {
441                 return ret;
442         }
443         talloc_steal(search_req, parent_dn);
444
445         ac->step_fn = descriptor_do_add;
446
447         return ldb_next_request(ac->module, search_req);
448 }
449 /* TODO */
450 static int descriptor_modify(struct ldb_module *module, struct ldb_request *req)
451 {
452         struct ldb_context *ldb = ldb_module_get_ctx(module);
453         ldb_debug(ldb, LDB_DEBUG_TRACE, "descriptor_modify\n");
454         return ldb_next_request(module, req);
455 }
456 /* TODO */
457 static int descriptor_rename(struct ldb_module *module, struct ldb_request *req)
458 {
459         struct ldb_context *ldb = ldb_module_get_ctx(module);
460         ldb_debug(ldb, LDB_DEBUG_TRACE, "descriptor_rename\n");
461         return ldb_next_request(module, req);
462 }
463
464 static int descriptor_init(struct ldb_module *module)
465 {
466         struct ldb_context *ldb;
467         struct descriptor_data *data;
468
469         ldb = ldb_module_get_ctx(module);
470         data = talloc(module, struct descriptor_data);
471         if (data == NULL) {
472                 ldb_oom(ldb);
473                 return LDB_ERR_OPERATIONS_ERROR;
474         }
475
476         data->inherit = lp_parm_bool(ldb_get_opaque(ldb, "loadparm"),
477                                   NULL, "acl", "inheritance", false);
478         ldb_module_set_private(module, data);
479         return ldb_next_init(module);
480 }
481
482
483 _PUBLIC_ const struct ldb_module_ops ldb_descriptor_module_ops = {
484         .name              = "descriptor",
485         .add           = descriptor_add,
486         .modify        = descriptor_modify,
487         .rename        = descriptor_rename,
488         .init_context  = descriptor_init
489 };
490
491