Merge branch 'master' of git://git.samba.org/samba
[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 };
47
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 *);
53 };
54
55 static const struct dsdb_class * get_last_structural_class(const struct dsdb_schema *schema, struct ldb_message_element *element)
56 {
57         const struct dsdb_class *last_class = NULL;
58         int i;
59         for (i = 0; i < element->num_values; i++){
60                 if (!last_class)
61                         last_class = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &element->values[i]);
62                 else {
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;
66                 }
67         }
68         return last_class;
69 }
70
71 struct dom_sid *get_default_ag(TALLOC_CTX *mem_ctx,
72                                struct ldb_dn *dn,
73                                struct security_token *token,
74                                struct ldb_context *ldb)
75 {
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;
85
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);
93                 else
94                         dag_sid = NULL;
95         }
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);
101                 else
102                         dag_sid = NULL;
103         }
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);
109                 else
110                         dag_sid = NULL;
111         }
112         else
113                 dag_sid = NULL;
114
115         talloc_free(tmp_ctx);
116         return dag_sid;
117 }
118
119 static struct security_descriptor *get_sd_unpacked(struct ldb_module *module, TALLOC_CTX *mem_ctx,
120                                             const struct dsdb_class *objectclass)
121 {
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);
125
126         if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
127                 return NULL;
128         }
129
130         sd = sddl_decode(mem_ctx,
131                          objectclass->defaultSecurityDescriptor,
132                          domain_sid);
133         return sd;
134 }
135
136 static struct dom_sid *get_default_group(TALLOC_CTX *mem_ctx,
137                                          struct ldb_context *ldb,
138                                          struct dom_sid *dag)
139 {
140         int *domainFunctionality;
141
142         domainFunctionality = talloc_get_type(
143                 ldb_get_opaque(ldb, "domainFunctionality"), int);
144
145         if (*domainFunctionality
146                         && (*domainFunctionality >= DS_DOMAIN_FUNCTION_2008)) {
147                 return dag;
148         }
149
150         return NULL;
151 }
152
153 static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
154                                      struct ldb_dn *dn,
155                                      TALLOC_CTX *mem_ctx,
156                                      const struct dsdb_class *objectclass,
157                                      const struct ldb_val *parent,
158                                      struct ldb_val *object)
159 {
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);
168         char *sddl_sd;
169         struct dom_sid *default_owner;
170         struct dom_sid *default_group;
171
172         if (object){
173                 user_descriptor = talloc(mem_ctx, struct security_descriptor);
174                 if(!user_descriptor)
175                         return NULL;
176                 ndr_err = ndr_pull_struct_blob(object, user_descriptor, NULL,
177                                                user_descriptor,
178                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
179
180                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)){
181                         talloc_free(user_descriptor);
182                         return NULL;
183                 }
184         }
185         else
186                 user_descriptor = get_sd_unpacked(module, mem_ctx, objectclass);
187
188         if (parent){
189                 parent_descriptor = talloc(mem_ctx, struct security_descriptor);
190                 if(!parent_descriptor)
191                         return NULL;
192                 ndr_err = ndr_pull_struct_blob(parent, parent_descriptor, NULL,
193                                                parent_descriptor,
194                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
195
196                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)){
197                         talloc_free(parent_descriptor);
198                         return NULL;
199                 }
200         }
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);
209         if (!new_sd)
210                 return NULL;
211
212
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));
215
216         linear_sd = talloc(mem_ctx, DATA_BLOB);
217         if (!linear_sd) {
218                 return NULL;
219         }
220
221         ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
222                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
223                                        new_sd,
224                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
225         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
226                 return NULL;
227         }
228
229         return linear_sd;
230 }
231
232 static struct descriptor_context *descriptor_init_context(struct ldb_module *module,
233                                                           struct ldb_request *req)
234 {
235         struct ldb_context *ldb;
236         struct descriptor_context *ac;
237
238         ldb = ldb_module_get_ctx(module);
239
240         ac = talloc_zero(req, struct descriptor_context);
241         if (ac == NULL) {
242                 ldb_set_errstring(ldb, "Out of Memory");
243                 return NULL;
244         }
245
246         ac->module = module;
247         ac->req = req;
248         return ac;
249 }
250
251 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
252 {
253         struct ldb_context *ldb;
254         struct descriptor_context *ac;
255         int ret;
256
257         ac = talloc_get_type(req->context, struct descriptor_context);
258         ldb = ldb_module_get_ctx(ac->module);
259
260         if (!ares) {
261                 return ldb_module_done(ac->req, NULL, NULL,
262                                         LDB_ERR_OPERATIONS_ERROR);
263         }
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);
268         }
269
270         switch (ares->type) {
271         case LDB_REPLY_ENTRY:
272                 if (ac->search_res != NULL) {
273                         ldb_set_errstring(ldb, "Too many results");
274                         talloc_free(ares);
275                         return ldb_module_done(ac->req, NULL, NULL,
276                                                 LDB_ERR_OPERATIONS_ERROR);
277                 }
278
279                 ac->search_res = talloc_steal(ac, ares);
280                 break;
281
282         case LDB_REPLY_REFERRAL:
283                 /* ignore */
284                 talloc_free(ares);
285                 break;
286
287         case LDB_REPLY_DONE:
288                 talloc_free(ares);
289                 ret = ac->step_fn(ac);
290                 if (ret != LDB_SUCCESS) {
291                         return ldb_module_done(ac->req, NULL, NULL, ret);
292                 }
293                 break;
294         }
295
296         return LDB_SUCCESS;
297 }
298 static int descriptor_op_callback(struct ldb_request *req, struct ldb_reply *ares)
299 {
300         struct descriptor_context *ac;
301
302         ac = talloc_get_type(req->context, struct descriptor_context);
303
304         if (!ares) {
305                 return ldb_module_done(ac->req, NULL, NULL,
306                                         LDB_ERR_OPERATIONS_ERROR);
307         }
308         if (ares->error != LDB_SUCCESS) {
309                 return ldb_module_done(ac->req, ares->controls,
310                                         ares->response, ares->error);
311         }
312
313         if (ares->type != LDB_REPLY_DONE) {
314                 talloc_free(ares);
315                 return ldb_module_done(ac->req, NULL, NULL,
316                                         LDB_ERR_OPERATIONS_ERROR);
317         }
318
319         return ldb_module_done(ac->req, ares->controls,
320                                 ares->response, ares->error);
321 }
322
323 static int descriptor_do_add(struct descriptor_context *ac)
324 {
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;
330         TALLOC_CTX *mem_ctx;
331         int ret;
332         struct ldb_val *sd_val = NULL;
333         const struct ldb_val *parentsd_val = NULL;
334         DATA_BLOB *sd;
335         const struct dsdb_class *objectclass;
336
337         ldb = ldb_module_get_ctx(ac->module);
338         schema = dsdb_get_schema(ldb);
339
340         mem_ctx = talloc_new(ac);
341         if (mem_ctx == NULL) {
342                 return LDB_ERR_OPERATIONS_ERROR;
343         }
344
345         msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
346
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);
351
352         if (!objectclass)
353                 return LDB_ERR_OPERATIONS_ERROR;
354
355         if (sd_element)
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))
361                 parentsd_val = NULL;
362         else if (ac->search_res != NULL)
363                 parentsd_val = ldb_msg_find_ldb_val(ac->search_res->message, "nTSecurityDescriptor");
364
365
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);
370         if (sd) {
371                 ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
372         }
373
374         talloc_free(mem_ctx);
375         ret = ldb_msg_sanity_check(ldb, msg);
376
377         if (ret != LDB_SUCCESS) {
378                 return ret;
379         }
380
381         ret = ldb_build_add_req(&add_req, ldb, ac,
382                                 msg,
383                                 ac->req->controls,
384                                 ac, descriptor_op_callback,
385                                 ac->req);
386         if (ret != LDB_SUCCESS) {
387                 return ret;
388         }
389
390         /* perform the add */
391         return ldb_next_request(ac->module, add_req);
392 }
393
394 static int descriptor_add(struct ldb_module *module, struct ldb_request *req)
395 {
396         struct ldb_context *ldb;
397         struct ldb_request *search_req;
398         struct descriptor_context *ac;
399         struct ldb_dn *parent_dn;
400         int ret;
401         struct descriptor_data *data;
402         static const char * const descr_attrs[] = { "nTSecurityDescriptor", NULL };
403
404         data = talloc_get_type(ldb_module_get_private(module), struct descriptor_data);
405         ldb = ldb_module_get_ctx(module);
406
407         ldb_debug(ldb, LDB_DEBUG_TRACE, "descriptor_add\n");
408
409         if (ldb_dn_is_special(req->op.add.message->dn)) {
410                 return ldb_next_request(module, req);
411         }
412
413         ac = descriptor_init_context(module, req);
414         if (ac == NULL) {
415                 return LDB_ERR_OPERATIONS_ERROR;
416         }
417
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);
421         }
422
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) {
426                 ldb_oom(ldb);
427                 return LDB_ERR_OPERATIONS_ERROR;
428         }
429
430         ret = ldb_build_search_req(&search_req, ldb,
431                                    ac, parent_dn, LDB_SCOPE_BASE,
432                                    "(objectClass=*)", descr_attrs,
433                                    NULL,
434                                    ac, get_search_callback,
435                                    req);
436         if (ret != LDB_SUCCESS) {
437                 return ret;
438         }
439         talloc_steal(search_req, parent_dn);
440
441         ac->step_fn = descriptor_do_add;
442
443         return ldb_next_request(ac->module, search_req);
444 }
445 /* TODO */
446 static int descriptor_modify(struct ldb_module *module, struct ldb_request *req)
447 {
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);
451 }
452 /* TODO */
453 static int descriptor_rename(struct ldb_module *module, struct ldb_request *req)
454 {
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);
458 }
459
460 static int descriptor_init(struct ldb_module *module)
461 {
462         struct ldb_context *ldb;
463         struct descriptor_data *data;
464
465         ldb = ldb_module_get_ctx(module);
466         data = talloc(module, struct descriptor_data);
467         if (data == NULL) {
468                 ldb_oom(ldb);
469                 return LDB_ERR_OPERATIONS_ERROR;
470         }
471
472         ldb_module_set_private(module, data);
473         return ldb_next_init(module);
474 }
475
476
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
483 };
484
485