Fixed some major bugs in inheritance and access checks.
[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         int _dummy;
47 };
48
49 struct descriptor_context {
50         struct ldb_module *module;
51         struct ldb_request *req;
52         struct ldb_reply *search_res;
53         struct ldb_reply *search_oc_res;
54         struct ldb_val *parentsd_val;
55         struct ldb_val *sd_val;
56         int (*step_fn)(struct descriptor_context *);
57 };
58
59 static const struct dsdb_class * get_last_structural_class(const struct dsdb_schema *schema, struct ldb_message_element *element)
60 {
61         const struct dsdb_class *last_class = NULL;
62         int i;
63         for (i = 0; i < element->num_values; i++){
64                 if (!last_class) {
65                         last_class = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &element->values[i]);
66                 } else {
67                         const struct dsdb_class *tmp_class = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &element->values[i]);
68                         if (tmp_class->subClass_order > last_class->subClass_order)
69                                 last_class = tmp_class;
70                 }
71         }
72         return last_class;
73 }
74
75 struct dom_sid *get_default_ag(TALLOC_CTX *mem_ctx,
76                                struct ldb_dn *dn,
77                                struct security_token *token,
78                                struct ldb_context *ldb)
79 {
80         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
81         struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
82         struct ldb_dn *schema_base_dn = ldb_get_schema_basedn(ldb);
83         struct ldb_dn *config_base_dn = ldb_get_config_basedn(ldb);
84         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
85         struct dom_sid *da_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_ADMINS);
86         struct dom_sid *ea_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_ENTERPRISE_ADMINS);
87         struct dom_sid *sa_sid = dom_sid_add_rid(tmp_ctx, domain_sid, DOMAIN_RID_SCHEMA_ADMINS);
88         struct dom_sid *dag_sid;
89
90         if (ldb_dn_compare_base(schema_base_dn, dn) == 0){
91                 if (security_token_has_sid(token, sa_sid))
92                         dag_sid = dom_sid_dup(mem_ctx, sa_sid);
93                 else if (security_token_has_sid(token, ea_sid))
94                         dag_sid = dom_sid_dup(mem_ctx, ea_sid);
95                 else if (security_token_has_sid(token, da_sid))
96                         dag_sid = dom_sid_dup(mem_ctx, da_sid);
97                 else
98                         dag_sid = NULL;
99         }
100         else if (ldb_dn_compare_base(config_base_dn, dn) == 0){
101                 if (security_token_has_sid(token, ea_sid))
102                         dag_sid = dom_sid_dup(mem_ctx, ea_sid);
103                 else if (security_token_has_sid(token, da_sid))
104                         dag_sid = dom_sid_dup(mem_ctx, da_sid);
105                 else
106                         dag_sid = NULL;
107         }
108         else if (ldb_dn_compare_base(root_base_dn, dn) == 0){
109                 if (security_token_has_sid(token, da_sid))
110                         dag_sid = dom_sid_dup(mem_ctx, da_sid);
111                 else if (security_token_has_sid(token, ea_sid))
112                                 dag_sid = dom_sid_dup(mem_ctx, ea_sid);
113                 else
114                         dag_sid = NULL;
115         }
116         else
117                 dag_sid = NULL;
118
119         talloc_free(tmp_ctx);
120         return dag_sid;
121 }
122
123 static struct security_descriptor *get_sd_unpacked(struct ldb_module *module, TALLOC_CTX *mem_ctx,
124                                             const struct dsdb_class *objectclass)
125 {
126         struct ldb_context *ldb = ldb_module_get_ctx(module);
127         struct security_descriptor *sd;
128         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
129
130         if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
131                 return NULL;
132         }
133
134         sd = sddl_decode(mem_ctx,
135                          objectclass->defaultSecurityDescriptor,
136                          domain_sid);
137         return sd;
138 }
139
140 static struct dom_sid *get_default_group(TALLOC_CTX *mem_ctx,
141                                          struct ldb_context *ldb,
142                                          struct dom_sid *dag)
143 {
144         int *domainFunctionality;
145
146         domainFunctionality = talloc_get_type(
147                 ldb_get_opaque(ldb, "domainFunctionality"), int);
148
149         if (*domainFunctionality
150                         && (*domainFunctionality >= DS_DOMAIN_FUNCTION_2008)) {
151                 return dag;
152         }
153
154         return NULL;
155 }
156
157 static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
158                                      struct ldb_dn *dn,
159                                      TALLOC_CTX *mem_ctx,
160                                      const struct dsdb_class *objectclass,
161                                      const struct ldb_val *parent,
162                                      struct ldb_val *object)
163 {
164         struct security_descriptor *user_descriptor = NULL, *parent_descriptor = NULL;
165         struct security_descriptor *new_sd;
166         DATA_BLOB *linear_sd;
167         enum ndr_err_code ndr_err;
168         struct ldb_context *ldb = ldb_module_get_ctx(module);
169         struct auth_session_info *session_info
170                 = ldb_get_opaque(ldb, "sessionInfo");
171         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
172         char *sddl_sd;
173         struct dom_sid *default_owner;
174         struct dom_sid *default_group;
175
176         if (object) {
177                 user_descriptor = talloc(mem_ctx, struct security_descriptor);
178                 if(!user_descriptor)
179                         return NULL;
180                 ndr_err = ndr_pull_struct_blob(object, user_descriptor, NULL,
181                                                user_descriptor,
182                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
183
184                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)){
185                         talloc_free(user_descriptor);
186                         return NULL;
187                 }
188         } else {
189                 user_descriptor = get_sd_unpacked(module, mem_ctx, objectclass);
190         }
191
192         if (parent){
193                 parent_descriptor = talloc(mem_ctx, struct security_descriptor);
194                 if(!parent_descriptor)
195                         return NULL;
196                 ndr_err = ndr_pull_struct_blob(parent, parent_descriptor, NULL,
197                                                parent_descriptor,
198                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
199
200                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)){
201                         talloc_free(parent_descriptor);
202                         return NULL;
203                 }
204         }
205         default_owner = get_default_ag(mem_ctx, dn,
206                                        session_info->security_token, ldb);
207         default_group = get_default_group(mem_ctx, ldb, default_owner);
208         new_sd = create_security_descriptor(mem_ctx, parent_descriptor, user_descriptor, true,
209                                             NULL, SEC_DACL_AUTO_INHERIT|SEC_SACL_AUTO_INHERIT,
210                                             session_info->security_token,
211                                             default_owner, default_group,
212                                             map_generic_rights_ds);
213         if (!new_sd)
214                 return NULL;
215
216
217         sddl_sd = sddl_encode(mem_ctx, new_sd, domain_sid);
218         DEBUG(10, ("Object %s created with desriptor %s\n\n", ldb_dn_get_linearized(dn), sddl_sd));
219
220         linear_sd = talloc(mem_ctx, DATA_BLOB);
221         if (!linear_sd) {
222                 return NULL;
223         }
224
225         ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
226                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
227                                        new_sd,
228                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
229         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
230                 return NULL;
231         }
232
233         return linear_sd;
234 }
235
236 static struct descriptor_context *descriptor_init_context(struct ldb_module *module,
237                                                           struct ldb_request *req)
238 {
239         struct ldb_context *ldb;
240         struct descriptor_context *ac;
241
242         ldb = ldb_module_get_ctx(module);
243
244         ac = talloc_zero(req, struct descriptor_context);
245         if (ac == NULL) {
246                 ldb_set_errstring(ldb, "Out of Memory");
247                 return NULL;
248         }
249
250         ac->module = module;
251         ac->req = req;
252         return ac;
253 }
254
255 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
256 {
257         struct ldb_context *ldb;
258         struct descriptor_context *ac;
259         int ret;
260
261         ac = talloc_get_type(req->context, struct descriptor_context);
262         ldb = ldb_module_get_ctx(ac->module);
263
264         if (!ares) {
265                 return ldb_module_done(ac->req, NULL, NULL,
266                                         LDB_ERR_OPERATIONS_ERROR);
267         }
268         if (ares->error != LDB_SUCCESS &&
269             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
270                 return ldb_module_done(ac->req, ares->controls,
271                                         ares->response, ares->error);
272         }
273
274         ldb_reset_err_string(ldb);
275
276         switch (ares->type) {
277         case LDB_REPLY_ENTRY:
278                         if (ac->search_res != NULL) {
279                         ldb_set_errstring(ldb, "Too many results");
280                         talloc_free(ares);
281                         return ldb_module_done(ac->req, NULL, NULL,
282                                                 LDB_ERR_OPERATIONS_ERROR);
283                                                 }
284
285                 ac->search_res = talloc_steal(ac, ares);
286                 break;
287
288         case LDB_REPLY_REFERRAL:
289                 /* ignore */
290                 talloc_free(ares);
291                 break;
292
293         case LDB_REPLY_DONE:
294                 talloc_free(ares);
295                 ret = ac->step_fn(ac);
296                 if (ret != LDB_SUCCESS) {
297                         return ldb_module_done(ac->req, NULL, NULL, ret);
298                 }
299                 break;
300         }
301
302         return LDB_SUCCESS;
303 }
304
305 static int get_search_oc_callback(struct ldb_request *req, struct ldb_reply *ares)
306 {
307         struct ldb_context *ldb;
308         struct descriptor_context *ac;
309         int ret;
310
311         ac = talloc_get_type(req->context, struct descriptor_context);
312         ldb = ldb_module_get_ctx(ac->module);
313
314         if (!ares) {
315                 return ldb_module_done(ac->req, NULL, NULL,
316                                         LDB_ERR_OPERATIONS_ERROR);
317         }
318         if (ares->error != LDB_SUCCESS &&
319             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
320                 return ldb_module_done(ac->req, ares->controls,
321                                         ares->response, ares->error);
322         }
323
324         ldb_reset_err_string(ldb);
325
326         switch (ares->type) {
327         case LDB_REPLY_ENTRY:
328                         if (ac->search_oc_res != NULL) {
329                         ldb_set_errstring(ldb, "Too many results");
330                         talloc_free(ares);
331                         return ldb_module_done(ac->req, NULL, NULL,
332                                                 LDB_ERR_OPERATIONS_ERROR);
333                                                 }
334
335                 ac->search_oc_res = talloc_steal(ac, ares);
336                 break;
337
338         case LDB_REPLY_REFERRAL:
339                 /* ignore */
340                 talloc_free(ares);
341                 break;
342
343         case LDB_REPLY_DONE:
344                 talloc_free(ares);
345                 ret = ac->step_fn(ac);
346                 if (ret != LDB_SUCCESS) {
347                         return ldb_module_done(ac->req, NULL, NULL, ret);
348                 }
349                 break;
350         }
351
352         return LDB_SUCCESS;
353 }
354
355
356 static int descriptor_op_callback(struct ldb_request *req, struct ldb_reply *ares)
357 {
358         struct descriptor_context *ac;
359
360         ac = talloc_get_type(req->context, struct descriptor_context);
361
362         if (!ares) {
363                 return ldb_module_done(ac->req, NULL, NULL,
364                                         LDB_ERR_OPERATIONS_ERROR);
365         }
366         if (ares->error != LDB_SUCCESS) {
367                 return ldb_module_done(ac->req, ares->controls,
368                                         ares->response, ares->error);
369         }
370
371         if (ares->type != LDB_REPLY_DONE) {
372                 talloc_free(ares);
373                 return ldb_module_done(ac->req, NULL, NULL,
374                                         LDB_ERR_OPERATIONS_ERROR);
375         }
376
377         return ldb_module_done(ac->req, ares->controls,
378                                 ares->response, ares->error);
379 }
380
381 static int descriptor_do_mod(struct descriptor_context *ac)
382 {
383         struct ldb_context *ldb;
384         const struct dsdb_schema *schema;
385         struct ldb_request *mod_req;
386         struct ldb_message_element *objectclass_element, *tmp_element;
387         int ret;
388         DATA_BLOB *sd;
389         const struct dsdb_class *objectclass;
390         struct ldb_message *msg;
391         int flags = 0;
392
393         ldb = ldb_module_get_ctx(ac->module);
394         schema = dsdb_get_schema(ldb);
395
396         msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
397         objectclass_element = ldb_msg_find_element(ac->search_oc_res->message, "objectClass");
398         objectclass = get_last_structural_class(schema, objectclass_element);
399
400         if (!objectclass) {
401                 ldb_asprintf_errstring(ldb, "No last structural objectclass found on %s",
402                                        ldb_dn_get_linearized(ac->search_oc_res->message->dn));
403                 return LDB_ERR_OPERATIONS_ERROR;
404         }
405
406         sd = get_new_descriptor(ac->module, msg->dn, ac, objectclass,
407                                 ac->parentsd_val, ac->sd_val);
408         if (ac->sd_val) {
409                 tmp_element = ldb_msg_find_element(msg, "ntSecurityDescriptor");
410                 flags = tmp_element->flags;
411                 ldb_msg_remove_attr(msg, "nTSecurityDescriptor");
412         }
413
414         if (sd) {
415                 ret = ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
416                 if (ret != LDB_SUCCESS) {
417                         return ret;
418                 }
419                 tmp_element = ldb_msg_find_element(msg, "ntSecurityDescriptor");
420                 tmp_element->flags = flags;
421         }
422         ret = ldb_build_mod_req(&mod_req, ldb, ac,
423                                 msg,
424                                 ac->req->controls,
425                                 ac, descriptor_op_callback,
426                                 ac->req);
427         if (ret != LDB_SUCCESS) {
428                 return ret;
429         }
430         return ldb_next_request(ac->module, mod_req);
431 }
432
433 static int descriptor_do_add(struct descriptor_context *ac)
434 {
435         struct ldb_context *ldb;
436         const struct dsdb_schema *schema;
437         struct ldb_request *add_req;
438         struct ldb_message_element *objectclass_element, *sd_element = NULL;
439         struct ldb_message *msg;
440         TALLOC_CTX *mem_ctx;
441         int ret;
442         DATA_BLOB *sd;
443         const struct dsdb_class *objectclass;
444         static const char *const attrs[] = { "objectClass", NULL };
445         struct ldb_request *search_req;
446
447         ldb = ldb_module_get_ctx(ac->module);
448         schema = dsdb_get_schema(ldb);
449         mem_ctx = talloc_new(ac);
450         if (mem_ctx == NULL) {
451                 return LDB_ERR_OPERATIONS_ERROR;
452         }
453
454         switch (ac->req->operation) {
455         case LDB_ADD:
456                 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
457                 objectclass_element = ldb_msg_find_element(msg, "objectClass");
458                 objectclass = get_last_structural_class(schema, objectclass_element);
459
460                 if (!objectclass) {
461                         ldb_asprintf_errstring(ldb, "No last structural objectclass found on %s", ldb_dn_get_linearized(msg->dn));
462                         return LDB_ERR_OPERATIONS_ERROR;
463                 }
464                 break;
465         case LDB_MODIFY:
466                 msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
467                 break;
468         default:
469                 return LDB_ERR_OPERATIONS_ERROR;
470         }
471
472
473         /* get the security descriptor values*/
474         sd_element = ldb_msg_find_element(msg, "nTSecurityDescriptor");
475         if (sd_element) {
476                 ac->sd_val = talloc_memdup(ac, &sd_element->values[0], sizeof(struct ldb_val));
477         }
478         /* NC's have no parent */
479         if ((ldb_dn_compare(msg->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
480             (ldb_dn_compare(msg->dn, (ldb_get_config_basedn(ldb))) == 0) ||
481             (ldb_dn_compare(msg->dn, (ldb_get_root_basedn(ldb))) == 0)) {
482                 ac->parentsd_val = NULL;
483         } else if (ac->search_res != NULL) {
484                 struct ldb_message_element *parent_element = ldb_msg_find_element(ac->search_res->message, "nTSecurityDescriptor");
485                 if (parent_element) {
486                         ac->parentsd_val = talloc_memdup(ac, &parent_element->values[0], sizeof(struct ldb_val));
487                 }
488         }
489
490         if (ac->req->operation == LDB_ADD) {
491         /* get the parent descriptor and the one provided. If not provided, get the default.*/
492         /* convert to security descriptor and calculate */
493                 sd = get_new_descriptor(ac->module, msg->dn, mem_ctx, objectclass,
494                                         ac->parentsd_val, ac->sd_val);
495                 if (ac->sd_val) {
496                         ldb_msg_remove_attr(msg, "nTSecurityDescriptor");
497                 }
498
499                 if (sd) {
500                         ret = ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
501                         if (ret != LDB_SUCCESS) {
502                                 return ret;
503                         }
504                 }
505
506                 talloc_free(mem_ctx);
507                 ret = ldb_msg_sanity_check(ldb, msg);
508
509                 if (ret != LDB_SUCCESS) {
510                         ldb_asprintf_errstring(ldb, "No last structural objectclass found on %s",
511                                                ldb_dn_get_linearized(msg->dn));
512                         return ret;
513                 }
514
515                 ret = ldb_build_add_req(&add_req, ldb, ac,
516                                         msg,
517                                         ac->req->controls,
518                                         ac, descriptor_op_callback,
519                                         ac->req);
520                 if (ret != LDB_SUCCESS) {
521                         return ret;
522                 }
523                 return ldb_next_request(ac->module, add_req);
524         } else {
525                 ret = ldb_build_search_req(&search_req, ldb,
526                                    ac, msg->dn, LDB_SCOPE_BASE,
527                                    "(objectClass=*)", attrs,
528                                    NULL,
529                                    ac, get_search_oc_callback,
530                                    ac->req);
531                 if (ret != LDB_SUCCESS) {
532                         return ret;
533                 }
534                 ac->step_fn = descriptor_do_mod;
535                 return ldb_next_request(ac->module, search_req);
536         }
537 }
538
539 static int descriptor_change(struct ldb_module *module, struct ldb_request *req)
540 {
541         struct ldb_context *ldb;
542         struct ldb_request *search_req;
543         struct descriptor_context *ac;
544         struct ldb_dn *parent_dn, *dn;
545         struct ldb_message_element *sd_element;
546         int ret;
547         static const char * const descr_attrs[] = { "nTSecurityDescriptor", NULL };
548
549         ldb = ldb_module_get_ctx(module);
550
551         switch (req->operation) {
552         case LDB_ADD:
553                 dn = req->op.add.message->dn;
554                 break;
555         case LDB_MODIFY:
556                 dn = req->op.mod.message->dn;
557                 sd_element = ldb_msg_find_element(req->op.mod.message, "nTSecurityDescriptor");
558                 if (!sd_element) {
559                         return ldb_next_request(module, req);
560                 }
561                 break;
562         default:
563                 return LDB_ERR_OPERATIONS_ERROR;
564         }
565         ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_change: %s\n", ldb_dn_get_linearized(dn));
566
567         if (ldb_dn_is_special(dn)) {
568                 return ldb_next_request(module, req);
569         }
570
571         ac = descriptor_init_context(module, req);
572         if (ac == NULL) {
573                 return LDB_ERR_OPERATIONS_ERROR;
574         }
575
576         /* If there isn't a parent, just go on to the add processing */
577         if (ldb_dn_get_comp_num(dn) == 1) {
578                 return descriptor_do_add(ac);
579         }
580
581         /* get copy of parent DN */
582         parent_dn = ldb_dn_get_parent(ac, dn);
583         if (parent_dn == NULL) {
584                 ldb_oom(ldb);
585                 return LDB_ERR_OPERATIONS_ERROR;
586         }
587
588         ret = ldb_build_search_req(&search_req, ldb,
589                                    ac, parent_dn, LDB_SCOPE_BASE,
590                                    "(objectClass=*)", descr_attrs,
591                                    NULL,
592                                    ac, get_search_callback,
593                                    req);
594         if (ret != LDB_SUCCESS) {
595                 return ret;
596         }
597         talloc_steal(search_req, parent_dn);
598
599         ac->step_fn = descriptor_do_add;
600
601         return ldb_next_request(ac->module, search_req);
602 }
603
604 /* TODO */
605 static int descriptor_rename(struct ldb_module *module, struct ldb_request *req)
606 {
607         struct ldb_context *ldb = ldb_module_get_ctx(module);
608         ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn));
609         return ldb_next_request(module, req);
610 }
611
612 static int descriptor_init(struct ldb_module *module)
613 {
614         return ldb_next_init(module);
615 }
616
617
618 _PUBLIC_ const struct ldb_module_ops ldb_descriptor_module_ops = {
619         .name              = "descriptor",
620         .add           = descriptor_change,
621         .modify        = descriptor_change,
622         .rename        = descriptor_rename,
623         .init_context  = descriptor_init
624 };
625
626