959a7d8cd119fe13bb38a0d9ea3f5c33e160eb94
[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 "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 "util.h"
45
46 struct descriptor_data {
47         int _dummy;
48 };
49
50 struct descriptor_context {
51         struct ldb_module *module;
52         struct ldb_request *req;
53         struct ldb_reply *search_res;
54         struct ldb_reply *search_oc_res;
55         struct ldb_val *parentsd_val;
56         struct ldb_val *sd_val;
57         int (*step_fn)(struct descriptor_context *);
58 };
59
60 struct dom_sid *get_default_ag(TALLOC_CTX *mem_ctx,
61                                struct ldb_dn *dn,
62                                struct security_token *token,
63                                struct ldb_context *ldb)
64 {
65         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
66         struct ldb_dn *default_base_dn = ldb_get_default_basedn(ldb);
67         struct ldb_dn *schema_base_dn = ldb_get_schema_basedn(ldb);
68         struct ldb_dn *config_base_dn = ldb_get_config_basedn(ldb);
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
75         /* FIXME: this has to be fixed regarding the forest DN (root DN) and
76          * the domain DN (default DN) - they aren't always the same. */
77
78         if (ldb_dn_compare_base(schema_base_dn, dn) == 0){
79                 if (security_token_has_sid(token, sa_sid))
80                         dag_sid = dom_sid_dup(mem_ctx, sa_sid);
81                 else if (security_token_has_sid(token, ea_sid))
82                         dag_sid = dom_sid_dup(mem_ctx, ea_sid);
83                 else if (security_token_has_sid(token, da_sid))
84                         dag_sid = dom_sid_dup(mem_ctx, da_sid);
85                 else
86                         dag_sid = NULL;
87         }
88         else if (ldb_dn_compare_base(config_base_dn, dn) == 0){
89                 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(default_base_dn, dn) == 0){
97                 if (security_token_has_sid(token, da_sid))
98                         dag_sid = dom_sid_dup(mem_ctx, da_sid);
99                 else if (security_token_has_sid(token, ea_sid))
100                                 dag_sid = dom_sid_dup(mem_ctx, ea_sid);
101                 else
102                         dag_sid = NULL;
103         }
104         else
105                 dag_sid = NULL;
106
107         talloc_free(tmp_ctx);
108         return dag_sid;
109 }
110
111 static struct security_descriptor *get_sd_unpacked(struct ldb_module *module, TALLOC_CTX *mem_ctx,
112                                             const struct dsdb_class *objectclass)
113 {
114         struct ldb_context *ldb = ldb_module_get_ctx(module);
115         struct security_descriptor *sd;
116         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
117
118         if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
119                 return NULL;
120         }
121
122         sd = sddl_decode(mem_ctx,
123                          objectclass->defaultSecurityDescriptor,
124                          domain_sid);
125         return sd;
126 }
127
128 static struct dom_sid *get_default_group(TALLOC_CTX *mem_ctx,
129                                          struct ldb_context *ldb,
130                                          struct dom_sid *dag)
131 {
132         if (dsdb_functional_level(ldb) >= DS_DOMAIN_FUNCTION_2008) {
133                 return dag;
134         }
135
136         return NULL;
137 }
138
139 static struct security_descriptor *descr_handle_sd_flags(TALLOC_CTX *mem_ctx,
140                                                          struct security_descriptor *new_sd,
141                                                          struct security_descriptor *old_sd,
142                                                          uint32_t sd_flags)
143 {
144         struct security_descriptor *final_sd; 
145         /* if there is no control or control == 0 modify everything */
146         if (!sd_flags) {
147                 return new_sd;
148         }
149
150         final_sd = talloc_zero(mem_ctx, struct security_descriptor);
151         final_sd->revision = SECURITY_DESCRIPTOR_REVISION_1;
152         final_sd->type = SEC_DESC_SELF_RELATIVE;
153
154         if (sd_flags & (SECINFO_OWNER)) {
155                 final_sd->owner_sid = talloc_memdup(mem_ctx, new_sd->owner_sid, sizeof(struct dom_sid));
156                 final_sd->type |= new_sd->type & SEC_DESC_OWNER_DEFAULTED;
157         }
158         else if (old_sd) {
159                 final_sd->owner_sid = talloc_memdup(mem_ctx, old_sd->owner_sid, sizeof(struct dom_sid));
160                 final_sd->type |= old_sd->type & SEC_DESC_OWNER_DEFAULTED;
161         }
162
163         if (sd_flags & (SECINFO_GROUP)) {
164                 final_sd->group_sid = talloc_memdup(mem_ctx, new_sd->group_sid, sizeof(struct dom_sid));
165                 final_sd->type |= new_sd->type & SEC_DESC_GROUP_DEFAULTED;
166         } 
167         else if (old_sd) {
168                 final_sd->group_sid = talloc_memdup(mem_ctx, old_sd->group_sid, sizeof(struct dom_sid));
169                 final_sd->type |= old_sd->type & SEC_DESC_GROUP_DEFAULTED;
170         }
171
172         if (sd_flags & (SECINFO_SACL)) {
173                 final_sd->sacl = security_acl_dup(mem_ctx,new_sd->sacl);
174                 final_sd->type |= new_sd->type & (SEC_DESC_SACL_PRESENT |
175                         SEC_DESC_SACL_DEFAULTED|SEC_DESC_SACL_AUTO_INHERIT_REQ |
176                         SEC_DESC_SACL_AUTO_INHERITED|SEC_DESC_SACL_PROTECTED |
177                         SEC_DESC_SERVER_SECURITY);
178         } 
179         else if (old_sd && old_sd->sacl) {
180                 final_sd->sacl = security_acl_dup(mem_ctx,old_sd->sacl);
181                 final_sd->type |= old_sd->type & (SEC_DESC_SACL_PRESENT |
182                         SEC_DESC_SACL_DEFAULTED|SEC_DESC_SACL_AUTO_INHERIT_REQ |
183                         SEC_DESC_SACL_AUTO_INHERITED|SEC_DESC_SACL_PROTECTED |
184                         SEC_DESC_SERVER_SECURITY);
185         }
186
187         if (sd_flags & (SECINFO_DACL)) {
188                 final_sd->dacl = security_acl_dup(mem_ctx,new_sd->dacl);
189                 final_sd->type |= new_sd->type & (SEC_DESC_DACL_PRESENT |
190                         SEC_DESC_DACL_DEFAULTED|SEC_DESC_DACL_AUTO_INHERIT_REQ |
191                         SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_PROTECTED |
192                         SEC_DESC_DACL_TRUSTED);
193         } 
194         else if (old_sd && old_sd->dacl) {
195                 final_sd->dacl = security_acl_dup(mem_ctx,old_sd->dacl);
196                 final_sd->type |= old_sd->type & (SEC_DESC_DACL_PRESENT |
197                         SEC_DESC_DACL_DEFAULTED|SEC_DESC_DACL_AUTO_INHERIT_REQ |
198                         SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_PROTECTED |
199                         SEC_DESC_DACL_TRUSTED);
200         }
201         /* not so sure about this */
202         final_sd->type |= new_sd->type & SEC_DESC_RM_CONTROL_VALID;
203         return final_sd;
204 }
205
206 static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
207                                      struct ldb_dn *dn,
208                                      TALLOC_CTX *mem_ctx,
209                                      const struct dsdb_class *objectclass,
210                                      const struct ldb_val *parent,
211                                      struct ldb_val *object,
212                                      struct ldb_val *old_sd,
213                                      uint32_t sd_flags)
214 {
215         struct security_descriptor *user_descriptor = NULL, *parent_descriptor = NULL;
216         struct security_descriptor *old_descriptor = NULL;
217         struct security_descriptor *new_sd, *final_sd;
218         DATA_BLOB *linear_sd;
219         enum ndr_err_code ndr_err;
220         struct ldb_context *ldb = ldb_module_get_ctx(module);
221         struct auth_session_info *session_info
222                 = ldb_get_opaque(ldb, "sessionInfo");
223         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
224         char *sddl_sd;
225         struct dom_sid *default_owner;
226         struct dom_sid *default_group;
227
228         if (object) {
229                 user_descriptor = talloc(mem_ctx, struct security_descriptor);
230                 if (!user_descriptor) {
231                         return NULL;
232                 }
233                 ndr_err = ndr_pull_struct_blob(object, user_descriptor, 
234                                                user_descriptor,
235                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
236
237                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
238                         talloc_free(user_descriptor);
239                         return NULL;
240                 }
241         } else {
242                 user_descriptor = get_sd_unpacked(module, mem_ctx, objectclass);
243         }
244
245         if (old_sd) {
246                 old_descriptor = talloc(mem_ctx, struct security_descriptor);
247                 if (!old_descriptor) {
248                         return NULL;
249                 }
250                 ndr_err = ndr_pull_struct_blob(old_sd, old_descriptor, 
251                                                old_descriptor,
252                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
253
254                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
255                         talloc_free(old_descriptor);
256                         return NULL;
257                 }
258         }
259
260         if (parent) {
261                 parent_descriptor = talloc(mem_ctx, struct security_descriptor);
262                 if (!parent_descriptor) {
263                         return NULL;
264                 }
265                 ndr_err = ndr_pull_struct_blob(parent, parent_descriptor, 
266                                                parent_descriptor,
267                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
268
269                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
270                         talloc_free(parent_descriptor);
271                         return NULL;
272                 }
273         }
274
275         default_owner = get_default_ag(mem_ctx, dn,
276                                        session_info->security_token, ldb);
277         default_group = get_default_group(mem_ctx, ldb, default_owner);
278         new_sd = create_security_descriptor(mem_ctx, parent_descriptor, user_descriptor, true,
279                                             NULL, SEC_DACL_AUTO_INHERIT|SEC_SACL_AUTO_INHERIT,
280                                             session_info->security_token,
281                                             default_owner, default_group,
282                                             map_generic_rights_ds);
283         if (!new_sd) {
284                 return NULL;
285         }
286         final_sd = descr_handle_sd_flags(mem_ctx, new_sd, old_descriptor, sd_flags);
287
288         if (!final_sd) {
289                 return NULL;
290         }
291
292         if (final_sd->dacl) {
293                 final_sd->dacl->revision = SECURITY_ACL_REVISION_ADS;
294         }
295         if (final_sd->sacl) {
296                 final_sd->sacl->revision = SECURITY_ACL_REVISION_ADS;
297         }
298
299         sddl_sd = sddl_encode(mem_ctx, final_sd, domain_sid);
300         DEBUG(10, ("Object %s created with desriptor %s\n\n", ldb_dn_get_linearized(dn), sddl_sd));
301
302         linear_sd = talloc(mem_ctx, DATA_BLOB);
303         if (!linear_sd) {
304                 return NULL;
305         }
306
307         ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
308                                        final_sd,
309                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
310         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
311                 return NULL;
312         }
313
314         return linear_sd;
315 }
316
317 static DATA_BLOB *descr_get_descriptor_to_show(struct ldb_module *module,
318                                                TALLOC_CTX *mem_ctx,
319                                                struct ldb_val *sd,
320                                                uint32_t sd_flags)
321 {
322         struct security_descriptor *old_sd, *final_sd;
323         DATA_BLOB *linear_sd;
324         enum ndr_err_code ndr_err;
325
326         old_sd = talloc(mem_ctx, struct security_descriptor);
327         if (!old_sd) {
328                 return NULL;
329         }
330         ndr_err = ndr_pull_struct_blob(sd, old_sd, 
331                                        old_sd,
332                                        (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
333
334         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
335                 talloc_free(old_sd);
336                 return NULL;
337         }
338
339         final_sd = descr_handle_sd_flags(mem_ctx, old_sd, NULL, sd_flags);
340
341         if (!final_sd) {
342                 return NULL;
343         }
344
345         linear_sd = talloc(mem_ctx, DATA_BLOB);
346         if (!linear_sd) {
347                 return NULL;
348         }
349
350         ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
351                                        final_sd,
352                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
353         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
354                 return NULL;
355         }
356
357         return linear_sd;
358 }
359
360 static struct descriptor_context *descriptor_init_context(struct ldb_module *module,
361                                                           struct ldb_request *req)
362 {
363         struct ldb_context *ldb;
364         struct descriptor_context *ac;
365
366         ldb = ldb_module_get_ctx(module);
367
368         ac = talloc_zero(req, struct descriptor_context);
369         if (ac == NULL) {
370                 ldb_set_errstring(ldb, "Out of Memory");
371                 return NULL;
372         }
373
374         ac->module = module;
375         ac->req = req;
376         return ac;
377 }
378
379 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
380 {
381         struct ldb_context *ldb;
382         struct descriptor_context *ac;
383         int ret;
384
385         ac = talloc_get_type(req->context, struct descriptor_context);
386         ldb = ldb_module_get_ctx(ac->module);
387
388         if (!ares) {
389                 return ldb_module_done(ac->req, NULL, NULL,
390                                         LDB_ERR_OPERATIONS_ERROR);
391         }
392         if (ares->error != LDB_SUCCESS &&
393             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
394                 return ldb_module_done(ac->req, ares->controls,
395                                         ares->response, ares->error);
396         }
397
398         ldb_reset_err_string(ldb);
399
400         switch (ares->type) {
401         case LDB_REPLY_ENTRY:
402                 if (ac->search_res != NULL) {
403                         ldb_set_errstring(ldb, "Too many results");
404                         talloc_free(ares);
405                         return ldb_module_done(ac->req, NULL, NULL,
406                                                 LDB_ERR_OPERATIONS_ERROR);
407                 }
408
409                 ac->search_res = talloc_steal(ac, ares);
410                 break;
411
412         case LDB_REPLY_REFERRAL:
413                 /* ignore */
414                 talloc_free(ares);
415                 break;
416
417         case LDB_REPLY_DONE:
418                 talloc_free(ares);
419                 ret = ac->step_fn(ac);
420                 if (ret != LDB_SUCCESS) {
421                         return ldb_module_done(ac->req, NULL, NULL, ret);
422                 }
423                 break;
424         }
425
426         return LDB_SUCCESS;
427 }
428
429 static int get_search_oc_callback(struct ldb_request *req, struct ldb_reply *ares)
430 {
431         struct ldb_context *ldb;
432         struct descriptor_context *ac;
433         int ret;
434
435         ac = talloc_get_type(req->context, struct descriptor_context);
436         ldb = ldb_module_get_ctx(ac->module);
437
438         if (!ares) {
439                 return ldb_module_done(ac->req, NULL, NULL,
440                                         LDB_ERR_OPERATIONS_ERROR);
441         }
442         if (ares->error != LDB_SUCCESS &&
443             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
444                 return ldb_module_done(ac->req, ares->controls,
445                                         ares->response, ares->error);
446         }
447
448         ldb_reset_err_string(ldb);
449
450         switch (ares->type) {
451         case LDB_REPLY_ENTRY:
452                 if (ac->search_oc_res != NULL) {
453                         ldb_set_errstring(ldb, "Too many results");
454                         talloc_free(ares);
455                         return ldb_module_done(ac->req, NULL, NULL,
456                                                 LDB_ERR_OPERATIONS_ERROR);
457                 }
458
459                 ac->search_oc_res = talloc_steal(ac, ares);
460                 break;
461
462         case LDB_REPLY_REFERRAL:
463                 /* ignore */
464                 talloc_free(ares);
465                 break;
466
467         case LDB_REPLY_DONE:
468                 talloc_free(ares);
469                 ret = ac->step_fn(ac);
470                 if (ret != LDB_SUCCESS) {
471                         return ldb_module_done(ac->req, NULL, NULL, ret);
472                 }
473                 break;
474         }
475
476         return LDB_SUCCESS;
477 }
478
479
480 static int descriptor_op_callback(struct ldb_request *req, struct ldb_reply *ares)
481 {
482         struct descriptor_context *ac;
483
484         ac = talloc_get_type(req->context, struct descriptor_context);
485
486         if (!ares) {
487                 return ldb_module_done(ac->req, NULL, NULL,
488                                         LDB_ERR_OPERATIONS_ERROR);
489         }
490
491         if (ares->type == LDB_REPLY_REFERRAL) {
492                 return ldb_module_send_referral(ac->req, ares->referral);
493         }
494
495         if (ares->error != LDB_SUCCESS) {
496                 return ldb_module_done(ac->req, ares->controls,
497                                         ares->response, ares->error);
498         }
499
500         if (ares->type != LDB_REPLY_DONE) {
501                 talloc_free(ares);
502                 return ldb_module_done(ac->req, NULL, NULL,
503                                         LDB_ERR_OPERATIONS_ERROR);
504         }
505
506         return ldb_module_done(ac->req, ares->controls,
507                                 ares->response, ares->error);
508 }
509
510 static int descriptor_search_callback(struct ldb_request *req, struct ldb_reply *ares)
511 {
512         struct descriptor_context *ac;
513         struct ldb_control *sd_control;
514         struct ldb_val *sd_val = NULL;
515         struct ldb_message_element *sd_el;
516         DATA_BLOB *show_sd;
517         int ret;
518         uint32_t sd_flags = 0;
519
520         ac = talloc_get_type(req->context, struct descriptor_context);
521
522         if (!ares) {
523                 ret = LDB_ERR_OPERATIONS_ERROR;
524                 goto fail;
525         }
526         if (ares->error != LDB_SUCCESS) {
527                 return ldb_module_done(ac->req, ares->controls,
528                                         ares->response, ares->error);
529         }
530
531         sd_control = ldb_request_get_control(ac->req, LDB_CONTROL_SD_FLAGS_OID);
532         if (sd_control) {
533                 struct ldb_sd_flags_control *sdctr = (struct ldb_sd_flags_control *)sd_control->data;
534                 sd_flags = sdctr->secinfo_flags;
535                 /* we only care for the last 4 bits */
536                 sd_flags = sd_flags & 0x0000000F;
537                 if (sd_flags == 0) {
538                         /* MS-ADTS 3.1.1.3.4.1.11 says that no bits
539                            equals all 4 bits */
540                         sd_flags = 0xF;
541                 }
542         }
543
544         switch (ares->type) {
545         case LDB_REPLY_ENTRY:
546                 if (sd_flags != 0) {
547                         sd_el = ldb_msg_find_element(ares->message, "nTSecurityDescriptor");
548                         if (sd_el) {
549                                 sd_val = sd_el->values;
550                         }
551                 }
552                 if (sd_val) {
553                         show_sd = descr_get_descriptor_to_show(ac->module, ac->req,
554                                                                sd_val, sd_flags);
555                         if (!show_sd) {
556                                 ret = LDB_ERR_OPERATIONS_ERROR;
557                                 goto fail;
558                         }
559                         ldb_msg_remove_attr(ares->message, "nTSecurityDescriptor");
560                         ret = ldb_msg_add_steal_value(ares->message, "nTSecurityDescriptor", show_sd);
561                         if (ret != LDB_SUCCESS) {
562                                 goto fail;
563                         }
564                 }
565                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
566
567         case LDB_REPLY_REFERRAL:
568                 /* ignore referrals */
569                 break;
570
571         case LDB_REPLY_DONE:
572                 return ldb_module_done(ac->req, ares->controls,
573                                         ares->response, ares->error);
574         }
575
576         talloc_free(ares);
577         return LDB_SUCCESS;
578 fail:
579         return ldb_module_done(ac->req, NULL, NULL, ret);
580 }
581
582 static int descriptor_do_mod(struct descriptor_context *ac)
583 {
584         struct ldb_context *ldb;
585         const struct dsdb_schema *schema;
586         struct ldb_request *mod_req;
587         struct ldb_message_element *objectclass_element, *tmp_element, *oldsd_el;
588         struct ldb_val *oldsd_val = NULL;
589         int ret;
590         DATA_BLOB *sd;
591         const struct dsdb_class *objectclass;
592         struct ldb_message *msg;
593         struct ldb_control *sd_control;
594         struct ldb_control *sd_control2;
595         int flags = 0;
596         uint32_t sd_flags = 0;
597
598         ldb = ldb_module_get_ctx(ac->module);
599         schema = dsdb_get_schema(ldb, ac);
600         msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
601         objectclass_element = ldb_msg_find_element(ac->search_oc_res->message, "objectClass");
602         objectclass = get_last_structural_class(schema, objectclass_element);
603
604         if (!objectclass) {
605                 ldb_asprintf_errstring(ldb, "No last structural objectclass found on %s",
606                                        ldb_dn_get_linearized(ac->search_oc_res->message->dn));
607                 return LDB_ERR_OPERATIONS_ERROR;
608         }
609         sd_control = ldb_request_get_control(ac->req, LDB_CONTROL_SD_FLAGS_OID);
610         sd_control2 = ldb_request_get_control(ac->req, LDB_CONTROL_RECALCULATE_SD_OID);
611         if (sd_control) {
612                 struct ldb_sd_flags_control *sdctr = (struct ldb_sd_flags_control *)sd_control->data;
613                 sd_flags = sdctr->secinfo_flags;
614                 /* we only care for the last 4 bits */
615                 sd_flags = sd_flags & 0x0000000F;
616         }
617         if (sd_flags != 0) {
618                 oldsd_el = ldb_msg_find_element(ac->search_oc_res->message, "nTSecurityDescriptor");
619                 if (oldsd_el) {
620                         oldsd_val = oldsd_el->values;
621                 }
622         }
623         sd = get_new_descriptor(ac->module, msg->dn, ac, objectclass,
624                                 ac->parentsd_val, ac->sd_val, oldsd_val, sd_flags);
625         if (ac->sd_val) {
626                 tmp_element = ldb_msg_find_element(msg, "ntSecurityDescriptor");
627                 flags = tmp_element->flags;
628                 ldb_msg_remove_attr(msg, "nTSecurityDescriptor");
629         }
630
631         if (sd) {
632                 ret = ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
633                 if (ret != LDB_SUCCESS) {
634                         return ret;
635                 }
636                 tmp_element = ldb_msg_find_element(msg, "ntSecurityDescriptor");
637                 if (sd_control2) {
638                         tmp_element->flags = LDB_FLAG_MOD_REPLACE;
639                 } else {
640                         tmp_element->flags = flags;
641                 }
642         }
643         ret = ldb_build_mod_req(&mod_req, ldb, ac,
644                                 msg,
645                                 ac->req->controls,
646                                 ac, descriptor_op_callback,
647                                 ac->req);
648         LDB_REQ_SET_LOCATION(mod_req);
649         if (ret != LDB_SUCCESS) {
650                 return ret;
651         }
652         /* mark it non-critical, so we don't get an error from the
653            backend, but mark that we've handled it */
654         if (sd_control) {
655                 sd_control->critical = 0;
656         }
657
658         return ldb_next_request(ac->module, mod_req);
659 }
660
661 static int descriptor_do_add(struct descriptor_context *ac)
662 {
663         struct ldb_context *ldb;
664         const struct dsdb_schema *schema;
665         struct ldb_request *add_req;
666         struct ldb_message_element *objectclass_element, *sd_element = NULL;
667         struct ldb_message *msg;
668         TALLOC_CTX *mem_ctx;
669         int ret;
670         DATA_BLOB *sd;
671         const struct dsdb_class *objectclass;
672         static const char *const attrs[] = { "objectClass", "nTSecurityDescriptor", NULL };
673         struct ldb_request *search_req;
674
675         ldb = ldb_module_get_ctx(ac->module);
676         schema = dsdb_get_schema(ldb, ac);
677         mem_ctx = talloc_new(ac);
678         if (mem_ctx == NULL) {
679                 return ldb_oom(ldb);
680         }
681         switch (ac->req->operation) {
682         case LDB_ADD:
683                 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
684                 objectclass_element = ldb_msg_find_element(msg, "objectClass");
685                 objectclass = get_last_structural_class(schema, objectclass_element);
686
687                 if (!objectclass) {
688                         ldb_asprintf_errstring(ldb, "No last structural objectclass found on %s", ldb_dn_get_linearized(msg->dn));
689                         return LDB_ERR_OPERATIONS_ERROR;
690                 }
691                 break;
692         case LDB_MODIFY:
693                 msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
694                 break;
695         default:
696                 return ldb_operr(ldb);
697         }
698
699
700         /* get the security descriptor values*/
701         sd_element = ldb_msg_find_element(msg, "nTSecurityDescriptor");
702         if (sd_element) {
703                 ac->sd_val = talloc_memdup(ac, &sd_element->values[0], sizeof(struct ldb_val));
704         }
705         /* NC's have no parent */
706         /* FIXME: this has to be made dynamic at some point */
707         if ((ldb_dn_compare(msg->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
708             (ldb_dn_compare(msg->dn, (ldb_get_config_basedn(ldb))) == 0) ||
709             (ldb_dn_compare(msg->dn, (ldb_get_default_basedn(ldb))) == 0)) {
710                 ac->parentsd_val = NULL;
711         } else if (ac->search_res != NULL) {
712                 struct ldb_message_element *parent_element = ldb_msg_find_element(ac->search_res->message, "nTSecurityDescriptor");
713                 if (parent_element) {
714                         ac->parentsd_val = talloc_memdup(ac, &parent_element->values[0], sizeof(struct ldb_val));
715                 }
716         }
717
718         if (ac->req->operation == LDB_ADD) {
719         /* get the parent descriptor and the one provided. If not provided, get the default.*/
720         /* convert to security descriptor and calculate */
721                 sd = get_new_descriptor(ac->module, msg->dn, mem_ctx, objectclass,
722                                         ac->parentsd_val, ac->sd_val, NULL, 0);
723                 if (ac->sd_val) {
724                         ldb_msg_remove_attr(msg, "nTSecurityDescriptor");
725                 }
726
727                 if (sd) {
728                         ret = ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
729                         if (ret != LDB_SUCCESS) {
730                                 return ret;
731                         }
732                 }
733
734                 talloc_free(mem_ctx);
735                 ret = ldb_msg_sanity_check(ldb, msg);
736
737                 if (ret != LDB_SUCCESS) {
738                         ldb_asprintf_errstring(ldb, "No last structural objectclass found on %s",
739                                                ldb_dn_get_linearized(msg->dn));
740                         return ret;
741                 }
742
743                 ret = ldb_build_add_req(&add_req, ldb, ac,
744                                         msg,
745                                         ac->req->controls,
746                                         ac, descriptor_op_callback,
747                                         ac->req);
748                 LDB_REQ_SET_LOCATION(add_req);
749                 if (ret != LDB_SUCCESS) {
750                         return ret;
751                 }
752                 return ldb_next_request(ac->module, add_req);
753         } else {
754                 ret = ldb_build_search_req(&search_req, ldb,
755                                    ac, msg->dn, LDB_SCOPE_BASE,
756                                    "(objectClass=*)", attrs,
757                                    NULL,
758                                    ac, get_search_oc_callback,
759                                    ac->req);
760                 LDB_REQ_SET_LOCATION(search_req);
761                 if (ret != LDB_SUCCESS) {
762                         return ret;
763                 }
764                 ac->step_fn = descriptor_do_mod;
765                 return ldb_next_request(ac->module, search_req);
766         }
767 }
768
769 static int descriptor_change(struct ldb_module *module, struct ldb_request *req)
770 {
771         struct ldb_context *ldb;
772         struct ldb_control *sd_control;
773         struct ldb_request *search_req;
774         struct descriptor_context *ac;
775         struct ldb_dn *parent_dn, *dn;
776         struct ldb_message_element *sd_element;
777         int ret;
778         static const char * const descr_attrs[] = { "nTSecurityDescriptor", NULL };
779
780         ldb = ldb_module_get_ctx(module);
781
782         switch (req->operation) {
783         case LDB_ADD:
784                 dn = req->op.add.message->dn;
785                 break;
786         case LDB_MODIFY:
787                 dn = req->op.mod.message->dn;
788                 sd_element = ldb_msg_find_element(req->op.mod.message, "nTSecurityDescriptor");
789                 /* This control allow forcing the recalculation of the SD */
790                 sd_control = ldb_request_get_control(req, LDB_CONTROL_RECALCULATE_SD_OID);
791                 if (!sd_element && !sd_control) {
792                         return ldb_next_request(module, req);
793                 }
794                 break;
795         default:
796                 return ldb_operr(ldb);
797         }
798         ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_change: %s\n", ldb_dn_get_linearized(dn));
799
800         if (ldb_dn_is_special(dn)) {
801                 return ldb_next_request(module, req);
802         }
803
804         ac = descriptor_init_context(module, req);
805         if (ac == NULL) {
806                 return ldb_operr(ldb);
807         }
808
809         /* If there isn't a parent, just go on to the add processing */
810         if (ldb_dn_get_comp_num(dn) == 1) {
811                 return descriptor_do_add(ac);
812         }
813
814         /* get copy of parent DN */
815         parent_dn = ldb_dn_get_parent(ac, dn);
816         if (parent_dn == NULL) {
817                 return ldb_oom(ldb);
818         }
819
820         ret = ldb_build_search_req(&search_req, ldb,
821                                    ac, parent_dn, LDB_SCOPE_BASE,
822                                    "(objectClass=*)", descr_attrs,
823                                    NULL,
824                                    ac, get_search_callback,
825                                    req);
826         LDB_REQ_SET_LOCATION(search_req);
827         if (ret != LDB_SUCCESS) {
828                 return ret;
829         }
830         talloc_steal(search_req, parent_dn);
831
832         ac->step_fn = descriptor_do_add;
833
834         return ldb_next_request(ac->module, search_req);
835 }
836
837 static int descriptor_search(struct ldb_module *module, struct ldb_request *req)
838 {
839         int ret;
840         struct ldb_context *ldb;
841         struct ldb_control *sd_control;
842         struct ldb_request *down_req;
843         struct descriptor_context *ac;
844
845         sd_control = ldb_request_get_control(req, LDB_CONTROL_SD_FLAGS_OID);
846         if (!sd_control) {
847                 return ldb_next_request(module, req);
848         }
849
850         ldb = ldb_module_get_ctx(module);
851         ac = descriptor_init_context(module, req);
852         if (ac == NULL) {
853                 return ldb_operr(ldb);
854         }
855
856         ret = ldb_build_search_req_ex(&down_req, ldb, ac,
857                                       req->op.search.base,
858                                       req->op.search.scope,
859                                       req->op.search.tree,
860                                       req->op.search.attrs,
861                                       req->controls,
862                                       ac, descriptor_search_callback,
863                                       ac->req);
864         LDB_REQ_SET_LOCATION(down_req);
865         if (ret != LDB_SUCCESS) {
866                 return ret;
867         }
868         /* mark it as handled */
869         if (sd_control) {
870                 sd_control->critical = 0;
871         }
872
873         return ldb_next_request(ac->module, down_req);
874 }
875 /* TODO */
876 static int descriptor_rename(struct ldb_module *module, struct ldb_request *req)
877 {
878         struct ldb_context *ldb = ldb_module_get_ctx(module);
879         ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn));
880         return ldb_next_request(module, req);
881 }
882
883 static int descriptor_init(struct ldb_module *module)
884 {
885         int ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
886         struct ldb_context *ldb = ldb_module_get_ctx(module);
887         if (ret != LDB_SUCCESS) {
888                 ldb_debug(ldb, LDB_DEBUG_ERROR,
889                         "descriptor: Unable to register control with rootdse!\n");
890                 return ldb_operr(ldb);
891         }
892         return ldb_next_init(module);
893 }
894
895
896 _PUBLIC_ const struct ldb_module_ops ldb_descriptor_module_ops = {
897         .name          = "descriptor",
898         .search        = descriptor_search,
899         .add           = descriptor_change,
900         .modify        = descriptor_change,
901         .rename        = descriptor_rename,
902         .init_context  = descriptor_init
903 };