s4-descriptor: Fixed some compiler warnings.
[obnox/samba/samba-obnox.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 "dsdb/samdb/ldb_modules/schema.h"
43 #include "auth/auth.h"
44 #include "param/param.h"
45 #include "util.h"
46
47 struct descriptor_data {
48         int _dummy;
49 };
50
51 struct descriptor_context {
52         struct ldb_module *module;
53         struct ldb_request *req;
54         struct ldb_message *msg;
55         struct ldb_reply *search_res;
56         struct ldb_reply *search_oc_res;
57         struct ldb_val *parentsd_val;
58         struct ldb_message_element *sd_element;
59         struct ldb_val *sd_val;
60         int (*step_fn)(struct descriptor_context *);
61 };
62
63 struct dom_sid *get_default_ag(TALLOC_CTX *mem_ctx,
64                                struct ldb_dn *dn,
65                                struct security_token *token,
66                                struct ldb_context *ldb)
67 {
68         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
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         struct ldb_dn *nc_root;
75         int ret;
76
77         ret = dsdb_find_nc_root(ldb, tmp_ctx, dn, &nc_root);
78         if (ret != LDB_SUCCESS) {
79                 talloc_free(tmp_ctx);
80                 return NULL;
81         }
82
83         if (ldb_dn_compare(nc_root, ldb_get_schema_basedn(ldb)) == 0) {
84                 if (security_token_has_sid(token, sa_sid))
85                         dag_sid = dom_sid_dup(mem_ctx, sa_sid);
86                 else if (security_token_has_sid(token, ea_sid))
87                         dag_sid = dom_sid_dup(mem_ctx, ea_sid);
88                 else if (security_token_has_sid(token, da_sid))
89                         dag_sid = dom_sid_dup(mem_ctx, da_sid);
90                 else
91                         dag_sid = NULL;
92         } else if (ldb_dn_compare(nc_root, ldb_get_config_basedn(ldb)) == 0) {
93                 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         } else if (ldb_dn_compare(nc_root, ldb_get_default_basedn(ldb)) == 0) {
100                 if (security_token_has_sid(token, da_sid))
101                         dag_sid = dom_sid_dup(mem_ctx, da_sid);
102                 else if (security_token_has_sid(token, ea_sid))
103                                 dag_sid = dom_sid_dup(mem_ctx, ea_sid);
104                 else
105                         dag_sid = NULL;
106         } else {
107                 dag_sid = NULL;
108         }
109
110         talloc_free(tmp_ctx);
111         return dag_sid;
112 }
113
114 static struct security_descriptor *get_sd_unpacked(struct ldb_module *module, TALLOC_CTX *mem_ctx,
115                                             const struct dsdb_class *objectclass)
116 {
117         struct ldb_context *ldb = ldb_module_get_ctx(module);
118         struct security_descriptor *sd;
119         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
120
121         if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
122                 return NULL;
123         }
124
125         sd = sddl_decode(mem_ctx,
126                          objectclass->defaultSecurityDescriptor,
127                          domain_sid);
128         return sd;
129 }
130
131 static struct dom_sid *get_default_group(TALLOC_CTX *mem_ctx,
132                                          struct ldb_context *ldb,
133                                          struct dom_sid *dag)
134 {
135         if (dsdb_functional_level(ldb) >= DS_DOMAIN_FUNCTION_2008) {
136                 return dag;
137         }
138
139         return NULL;
140 }
141
142 static struct security_descriptor *descr_handle_sd_flags(TALLOC_CTX *mem_ctx,
143                                                          struct security_descriptor *new_sd,
144                                                          struct security_descriptor *old_sd,
145                                                          uint32_t sd_flags)
146 {
147         struct security_descriptor *final_sd; 
148         /* if there is no control or control == 0 modify everything */
149         if (!sd_flags) {
150                 return new_sd;
151         }
152
153         final_sd = talloc_zero(mem_ctx, struct security_descriptor);
154         final_sd->revision = SECURITY_DESCRIPTOR_REVISION_1;
155         final_sd->type = SEC_DESC_SELF_RELATIVE;
156
157         if (sd_flags & (SECINFO_OWNER)) {
158                 final_sd->owner_sid = talloc_memdup(mem_ctx, new_sd->owner_sid, sizeof(struct dom_sid));
159                 final_sd->type |= new_sd->type & SEC_DESC_OWNER_DEFAULTED;
160         }
161         else if (old_sd) {
162                 final_sd->owner_sid = talloc_memdup(mem_ctx, old_sd->owner_sid, sizeof(struct dom_sid));
163                 final_sd->type |= old_sd->type & SEC_DESC_OWNER_DEFAULTED;
164         }
165
166         if (sd_flags & (SECINFO_GROUP)) {
167                 final_sd->group_sid = talloc_memdup(mem_ctx, new_sd->group_sid, sizeof(struct dom_sid));
168                 final_sd->type |= new_sd->type & SEC_DESC_GROUP_DEFAULTED;
169         } 
170         else if (old_sd) {
171                 final_sd->group_sid = talloc_memdup(mem_ctx, old_sd->group_sid, sizeof(struct dom_sid));
172                 final_sd->type |= old_sd->type & SEC_DESC_GROUP_DEFAULTED;
173         }
174
175         if (sd_flags & (SECINFO_SACL)) {
176                 final_sd->sacl = security_acl_dup(mem_ctx,new_sd->sacl);
177                 final_sd->type |= new_sd->type & (SEC_DESC_SACL_PRESENT |
178                         SEC_DESC_SACL_DEFAULTED|SEC_DESC_SACL_AUTO_INHERIT_REQ |
179                         SEC_DESC_SACL_AUTO_INHERITED|SEC_DESC_SACL_PROTECTED |
180                         SEC_DESC_SERVER_SECURITY);
181         } 
182         else if (old_sd && old_sd->sacl) {
183                 final_sd->sacl = security_acl_dup(mem_ctx,old_sd->sacl);
184                 final_sd->type |= old_sd->type & (SEC_DESC_SACL_PRESENT |
185                         SEC_DESC_SACL_DEFAULTED|SEC_DESC_SACL_AUTO_INHERIT_REQ |
186                         SEC_DESC_SACL_AUTO_INHERITED|SEC_DESC_SACL_PROTECTED |
187                         SEC_DESC_SERVER_SECURITY);
188         }
189
190         if (sd_flags & (SECINFO_DACL)) {
191                 final_sd->dacl = security_acl_dup(mem_ctx,new_sd->dacl);
192                 final_sd->type |= new_sd->type & (SEC_DESC_DACL_PRESENT |
193                         SEC_DESC_DACL_DEFAULTED|SEC_DESC_DACL_AUTO_INHERIT_REQ |
194                         SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_PROTECTED |
195                         SEC_DESC_DACL_TRUSTED);
196         } 
197         else if (old_sd && old_sd->dacl) {
198                 final_sd->dacl = security_acl_dup(mem_ctx,old_sd->dacl);
199                 final_sd->type |= old_sd->type & (SEC_DESC_DACL_PRESENT |
200                         SEC_DESC_DACL_DEFAULTED|SEC_DESC_DACL_AUTO_INHERIT_REQ |
201                         SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_PROTECTED |
202                         SEC_DESC_DACL_TRUSTED);
203         }
204         /* not so sure about this */
205         final_sd->type |= new_sd->type & SEC_DESC_RM_CONTROL_VALID;
206         return final_sd;
207 }
208
209 static DATA_BLOB *get_new_descriptor(struct ldb_module *module,
210                                      struct ldb_dn *dn,
211                                      TALLOC_CTX *mem_ctx,
212                                      const struct dsdb_class *objectclass,
213                                      const struct ldb_val *parent,
214                                      const struct ldb_val *object,
215                                      const struct ldb_val *old_sd,
216                                      uint32_t sd_flags)
217 {
218         struct security_descriptor *user_descriptor = NULL, *parent_descriptor = NULL;
219         struct security_descriptor *old_descriptor = NULL;
220         struct security_descriptor *new_sd, *final_sd;
221         DATA_BLOB *linear_sd;
222         enum ndr_err_code ndr_err;
223         struct ldb_context *ldb = ldb_module_get_ctx(module);
224         struct auth_session_info *session_info
225                 = ldb_get_opaque(ldb, "sessionInfo");
226         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
227         char *sddl_sd;
228         struct dom_sid *default_owner;
229         struct dom_sid *default_group;
230
231         if (object) {
232                 user_descriptor = talloc(mem_ctx, struct security_descriptor);
233                 if (!user_descriptor) {
234                         return NULL;
235                 }
236                 ndr_err = ndr_pull_struct_blob(object, user_descriptor, 
237                                                user_descriptor,
238                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
239
240                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
241                         talloc_free(user_descriptor);
242                         return NULL;
243                 }
244         } else {
245                 user_descriptor = get_sd_unpacked(module, mem_ctx, objectclass);
246         }
247
248         if (old_sd) {
249                 old_descriptor = talloc(mem_ctx, struct security_descriptor);
250                 if (!old_descriptor) {
251                         return NULL;
252                 }
253                 ndr_err = ndr_pull_struct_blob(old_sd, old_descriptor, 
254                                                old_descriptor,
255                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
256
257                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
258                         talloc_free(old_descriptor);
259                         return NULL;
260                 }
261         }
262
263         if (parent) {
264                 parent_descriptor = talloc(mem_ctx, struct security_descriptor);
265                 if (!parent_descriptor) {
266                         return NULL;
267                 }
268                 ndr_err = ndr_pull_struct_blob(parent, parent_descriptor, 
269                                                parent_descriptor,
270                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
271
272                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
273                         talloc_free(parent_descriptor);
274                         return NULL;
275                 }
276         }
277
278         default_owner = get_default_ag(mem_ctx, dn,
279                                        session_info->security_token, ldb);
280         default_group = get_default_group(mem_ctx, ldb, default_owner);
281         new_sd = create_security_descriptor(mem_ctx, parent_descriptor, user_descriptor, true,
282                                             NULL, SEC_DACL_AUTO_INHERIT|SEC_SACL_AUTO_INHERIT,
283                                             session_info->security_token,
284                                             default_owner, default_group,
285                                             map_generic_rights_ds);
286         if (!new_sd) {
287                 return NULL;
288         }
289         final_sd = descr_handle_sd_flags(mem_ctx, new_sd, old_descriptor, sd_flags);
290
291         if (!final_sd) {
292                 return NULL;
293         }
294
295         if (final_sd->dacl) {
296                 final_sd->dacl->revision = SECURITY_ACL_REVISION_ADS;
297         }
298         if (final_sd->sacl) {
299                 final_sd->sacl->revision = SECURITY_ACL_REVISION_ADS;
300         }
301
302         sddl_sd = sddl_encode(mem_ctx, final_sd, domain_sid);
303         DEBUG(10, ("Object %s created with desriptor %s\n\n", ldb_dn_get_linearized(dn), sddl_sd));
304
305         linear_sd = talloc(mem_ctx, DATA_BLOB);
306         if (!linear_sd) {
307                 return NULL;
308         }
309
310         ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
311                                        final_sd,
312                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
313         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
314                 return NULL;
315         }
316
317         return linear_sd;
318 }
319
320 static DATA_BLOB *descr_get_descriptor_to_show(struct ldb_module *module,
321                                                TALLOC_CTX *mem_ctx,
322                                                struct ldb_val *sd,
323                                                uint32_t sd_flags)
324 {
325         struct security_descriptor *old_sd, *final_sd;
326         DATA_BLOB *linear_sd;
327         enum ndr_err_code ndr_err;
328
329         old_sd = talloc(mem_ctx, struct security_descriptor);
330         if (!old_sd) {
331                 return NULL;
332         }
333         ndr_err = ndr_pull_struct_blob(sd, old_sd, 
334                                        old_sd,
335                                        (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
336
337         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
338                 talloc_free(old_sd);
339                 return NULL;
340         }
341
342         final_sd = descr_handle_sd_flags(mem_ctx, old_sd, NULL, sd_flags);
343
344         if (!final_sd) {
345                 return NULL;
346         }
347
348         linear_sd = talloc(mem_ctx, DATA_BLOB);
349         if (!linear_sd) {
350                 return NULL;
351         }
352
353         ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
354                                        final_sd,
355                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
356         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
357                 return NULL;
358         }
359
360         return linear_sd;
361 }
362
363 static struct descriptor_context *descriptor_init_context(struct ldb_module *module,
364                                                           struct ldb_request *req)
365 {
366         struct ldb_context *ldb;
367         struct descriptor_context *ac;
368
369         ldb = ldb_module_get_ctx(module);
370
371         ac = talloc_zero(req, struct descriptor_context);
372         if (ac == NULL) {
373                 ldb_set_errstring(ldb, "Out of Memory");
374                 return NULL;
375         }
376
377         ac->module = module;
378         ac->req = req;
379         return ac;
380 }
381
382 static int descriptor_search_callback(struct ldb_request *req, struct ldb_reply *ares)
383 {
384         struct descriptor_context *ac;
385         struct ldb_control *sd_control;
386         struct ldb_val *sd_val = NULL;
387         struct ldb_message_element *sd_el;
388         DATA_BLOB *show_sd;
389         int ret;
390         uint32_t sd_flags = 0;
391
392         ac = talloc_get_type(req->context, struct descriptor_context);
393
394         if (!ares) {
395                 ret = LDB_ERR_OPERATIONS_ERROR;
396                 goto fail;
397         }
398         if (ares->error != LDB_SUCCESS) {
399                 return ldb_module_done(ac->req, ares->controls,
400                                         ares->response, ares->error);
401         }
402
403         sd_control = ldb_request_get_control(ac->req, LDB_CONTROL_SD_FLAGS_OID);
404         if (sd_control) {
405                 struct ldb_sd_flags_control *sdctr = (struct ldb_sd_flags_control *)sd_control->data;
406                 sd_flags = sdctr->secinfo_flags;
407                 /* we only care for the last 4 bits */
408                 sd_flags = sd_flags & 0x0000000F;
409                 if (sd_flags == 0) {
410                         /* MS-ADTS 3.1.1.3.4.1.11 says that no bits
411                            equals all 4 bits */
412                         sd_flags = 0xF;
413                 }
414         }
415
416         switch (ares->type) {
417         case LDB_REPLY_ENTRY:
418                 if (sd_flags != 0) {
419                         sd_el = ldb_msg_find_element(ares->message, "nTSecurityDescriptor");
420                         if (sd_el) {
421                                 sd_val = sd_el->values;
422                         }
423                 }
424                 if (sd_val) {
425                         show_sd = descr_get_descriptor_to_show(ac->module, ac->req,
426                                                                sd_val, sd_flags);
427                         if (!show_sd) {
428                                 ret = LDB_ERR_OPERATIONS_ERROR;
429                                 goto fail;
430                         }
431                         ldb_msg_remove_attr(ares->message, "nTSecurityDescriptor");
432                         ret = ldb_msg_add_steal_value(ares->message, "nTSecurityDescriptor", show_sd);
433                         if (ret != LDB_SUCCESS) {
434                                 goto fail;
435                         }
436                 }
437                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
438
439         case LDB_REPLY_REFERRAL:
440                 return ldb_module_send_referral(ac->req, ares->referral);
441
442         case LDB_REPLY_DONE:
443                 return ldb_module_done(ac->req, ares->controls,
444                                         ares->response, ares->error);
445         }
446
447 fail:
448         talloc_free(ares);
449         return ldb_module_done(ac->req, NULL, NULL, ret);
450 }
451
452 static int descriptor_add(struct ldb_module *module, struct ldb_request *req)
453 {
454         struct ldb_context *ldb;
455         struct ldb_request *add_req;
456         struct ldb_message *msg;
457         struct ldb_result *parent_res;
458         const struct ldb_val *parent_sd = NULL;
459         const struct ldb_val *user_sd;
460         struct ldb_dn *parent_dn, *dn, *nc_root;
461         struct ldb_message_element *objectclass_element, *sd_element;
462         int ret;
463         const struct dsdb_schema *schema;
464         DATA_BLOB *sd;
465         const struct dsdb_class *objectclass;
466         static const char * const parent_attrs[] = { "nTSecurityDescriptor", NULL };
467
468         ldb = ldb_module_get_ctx(module);
469         dn = req->op.add.message->dn;
470         user_sd = ldb_msg_find_ldb_val(req->op.add.message, "nTSecurityDescriptor");
471         sd_element = ldb_msg_find_element(req->op.add.message, "nTSecurityDescriptor");
472         /* nTSecurityDescriptor without a value is an error, letting through so it is handled */
473         if (user_sd == NULL && sd_element) {
474                 return ldb_next_request(module, req);
475         }
476
477         ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_add: %s\n", ldb_dn_get_linearized(dn));
478
479         /* do not manipulate our control entries */
480         if (ldb_dn_is_special(dn)) {
481                 return ldb_next_request(module, req);
482         }
483
484         /* if the object has a parent, retrieve its SD to
485          * use for calculation. unfortunately we do not yet have
486          * instanceType*/
487         parent_dn = ldb_dn_get_parent(req, dn);
488         if (parent_dn == NULL) {
489                 return ldb_oom(ldb);
490         }
491
492         ret = dsdb_find_nc_root(ldb, req, dn, &nc_root);
493         if (ret != LDB_SUCCESS) {
494                 ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_add: Could not find NC root for %s\n",
495                           ldb_dn_get_linearized(dn));
496                 return ret;
497         }
498
499         if (ldb_dn_compare(dn, nc_root) != 0) {
500                 /* we aren't any NC */
501                 ret = dsdb_module_search_dn(module, req, &parent_res, parent_dn,
502                                             parent_attrs,
503                                             DSDB_FLAG_NEXT_MODULE,
504                                             req);
505                 if (ret != LDB_SUCCESS) {
506                         ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_add: Could not find SD for %s\n",
507                                   ldb_dn_get_linearized(parent_dn));
508                         return ret;
509                 }
510                 if (parent_res->count != 1) {
511                         return ldb_operr(ldb);
512                 }
513                 parent_sd = ldb_msg_find_ldb_val(parent_res->msgs[0], "nTSecurityDescriptor");
514         }
515
516         schema = dsdb_get_schema(ldb, req);
517
518         objectclass_element = ldb_msg_find_element(req->op.add.message, "objectClass");
519         if (objectclass_element == NULL) {
520                 return ldb_operr(ldb);
521         }
522
523         objectclass = get_last_structural_class(schema, objectclass_element, req);
524         if (objectclass == NULL) {
525                 return ldb_operr(ldb);
526         }
527
528         sd = get_new_descriptor(module, dn, req,
529                                 objectclass, parent_sd,
530                                 user_sd, NULL, 0);
531         msg = ldb_msg_copy_shallow(req, req->op.add.message);
532         if (sd != NULL) {
533                 if (sd_element != NULL) {
534                         sd_element->values[0] = *sd;
535                 } else {
536                         ret = ldb_msg_add_steal_value(msg,
537                                                       "nTSecurityDescriptor",
538                                                       sd);
539                         if (ret != LDB_SUCCESS) {
540                                 return ret;
541                         }
542                 }
543         }
544
545         ret = ldb_build_add_req(&add_req, ldb, req,
546                                 msg,
547                                 req->controls,
548                                 req, dsdb_next_callback,
549                                 req);
550         LDB_REQ_SET_LOCATION(add_req);
551         if (ret != LDB_SUCCESS) {
552                 return ldb_error(ldb, ret,
553                                  "descriptor_add: Error creating new add request.");
554         }
555
556         return ldb_next_request(module, add_req);
557 }
558
559 static int descriptor_modify(struct ldb_module *module, struct ldb_request *req)
560 {
561         struct ldb_context *ldb;
562         struct ldb_control *sd_recalculate_control, *sd_flags_control;
563         struct ldb_request *mod_req;
564         struct ldb_message *msg;
565         struct ldb_result *current_res, *parent_res;
566         const struct ldb_val *old_sd = NULL;
567         const struct ldb_val *parent_sd = NULL;
568         const struct ldb_val *user_sd;
569         struct ldb_dn *parent_dn, *dn;
570         struct ldb_message_element *objectclass_element;
571         int ret;
572         uint32_t instanceType, sd_flags = 0;
573         const struct dsdb_schema *schema;
574         DATA_BLOB *sd;
575         const struct dsdb_class *objectclass;
576         static const char * const parent_attrs[] = { "nTSecurityDescriptor", NULL };
577         static const char * const current_attrs[] = { "nTSecurityDescriptor",
578                                                       "instanceType",
579                                                       "objectClass", NULL };
580         ldb = ldb_module_get_ctx(module);
581         dn = req->op.mod.message->dn;
582         user_sd = ldb_msg_find_ldb_val(req->op.mod.message, "nTSecurityDescriptor");
583         /* This control forces the recalculation of the SD also when
584          * no modification is performed. */
585         sd_recalculate_control = ldb_request_get_control(req,
586                                              LDB_CONTROL_RECALCULATE_SD_OID);
587         if (!user_sd && !sd_recalculate_control) {
588                 return ldb_next_request(module, req);
589         }
590
591         ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_modify: %s\n", ldb_dn_get_linearized(dn));
592
593         /* do not manipulate our control entries */
594         if (ldb_dn_is_special(dn)) {
595                 return ldb_next_request(module, req);
596         }
597
598         ret = dsdb_module_search_dn(module, req, &current_res, dn,
599                                     current_attrs,
600                                     DSDB_FLAG_NEXT_MODULE,
601                                     req);
602         if (ret != LDB_SUCCESS) {
603                 ldb_debug(ldb, LDB_DEBUG_ERROR,"descriptor_modify: Could not find %s\n",
604                           ldb_dn_get_linearized(dn));
605                 return ret;
606         }
607
608         instanceType = ldb_msg_find_attr_as_uint(current_res->msgs[0],
609                                                  "instanceType", 0);
610         /* if the object has a parent, retrieve its SD to
611          * use for calculation */
612         if (!ldb_dn_is_null(current_res->msgs[0]->dn) &&
613             !(instanceType & INSTANCE_TYPE_IS_NC_HEAD)) {
614                 parent_dn = ldb_dn_get_parent(req, dn);
615                 if (parent_dn == NULL) {
616                         return ldb_oom(ldb);
617                 }
618                 ret = dsdb_module_search_dn(module, req, &parent_res, parent_dn,
619                                             parent_attrs,
620                                             DSDB_FLAG_NEXT_MODULE,
621                                             req);
622                 if (ret != LDB_SUCCESS) {
623                         ldb_debug(ldb, LDB_DEBUG_ERROR, "descriptor_modify: Could not find SD for %s\n",
624                                   ldb_dn_get_linearized(parent_dn));
625                         return ret;
626                 }
627                 if (parent_res->count != 1) {
628                         return ldb_operr(ldb);
629                 }
630                 parent_sd = ldb_msg_find_ldb_val(parent_res->msgs[0], "nTSecurityDescriptor");
631         }
632         sd_flags_control = ldb_request_get_control(req, LDB_CONTROL_SD_FLAGS_OID);
633
634         schema = dsdb_get_schema(ldb, req);
635
636         objectclass_element = ldb_msg_find_element(current_res->msgs[0], "objectClass");
637         if (objectclass_element == NULL) {
638                 return ldb_operr(ldb);
639         }
640
641         objectclass = get_last_structural_class(schema, objectclass_element, req);
642         if (objectclass == NULL) {
643                 return ldb_operr(ldb);
644         }
645
646         if (sd_flags_control) {
647                 struct ldb_sd_flags_control *sdctr = (struct ldb_sd_flags_control *)sd_flags_control->data;
648                 sd_flags = sdctr->secinfo_flags;
649                 /* we only care for the last 4 bits */
650                 sd_flags = sd_flags & 0x0000000F;
651         }
652         if (sd_flags != 0) {
653                 old_sd = ldb_msg_find_ldb_val(current_res->msgs[0], "nTSecurityDescriptor");
654         }
655
656         sd = get_new_descriptor(module, dn, req,
657                                 objectclass, parent_sd,
658                                 user_sd, old_sd, sd_flags);
659         msg = ldb_msg_copy_shallow(req, req->op.mod.message);
660         if (sd != NULL) {
661                 struct ldb_message_element *sd_element;
662                 if (user_sd != NULL) {
663                         sd_element = ldb_msg_find_element(msg,
664                                                           "nTSecurityDescriptor");
665                         sd_element->values[0] = *sd;
666                 } else if (sd_recalculate_control != NULL) {
667                         /* In this branch we really do force the recalculation
668                          * of the SD */
669                         ldb_msg_remove_attr(msg, "nTSecurityDescriptor");
670
671                         ret = ldb_msg_add_steal_value(msg,
672                                                       "nTSecurityDescriptor",
673                                                       sd);
674                         if (ret != LDB_SUCCESS) {
675                                 return ldb_error(ldb, ret,
676                                          "descriptor_modify: Could not replace SD value in message.");
677                         }
678                         sd_element = ldb_msg_find_element(msg,
679                                                           "nTSecurityDescriptor");
680                         sd_element->flags = LDB_FLAG_MOD_REPLACE;
681                 }
682         }
683
684         /* mark the controls as non-critical since we've handled them */
685         if (sd_flags_control != NULL) {
686                 sd_flags_control->critical = 0;
687         }
688         if (sd_recalculate_control != NULL) {
689                 sd_recalculate_control->critical = 0;
690         }
691
692         ret = ldb_build_mod_req(&mod_req, ldb, req,
693                                 msg,
694                                 req->controls,
695                                 req,
696                                 dsdb_next_callback,
697                                 req);
698         LDB_REQ_SET_LOCATION(mod_req);
699         if (ret != LDB_SUCCESS) {
700                 return ret;
701         }
702
703         return ldb_next_request(module, mod_req);
704 }
705
706 static int descriptor_search(struct ldb_module *module, struct ldb_request *req)
707 {
708         int ret;
709         struct ldb_context *ldb;
710         struct ldb_control *sd_control;
711         struct ldb_request *down_req;
712         struct descriptor_context *ac;
713
714         sd_control = ldb_request_get_control(req, LDB_CONTROL_SD_FLAGS_OID);
715         if (!sd_control) {
716                 return ldb_next_request(module, req);
717         }
718
719         ldb = ldb_module_get_ctx(module);
720         ac = descriptor_init_context(module, req);
721         if (ac == NULL) {
722                 return ldb_operr(ldb);
723         }
724
725         ret = ldb_build_search_req_ex(&down_req, ldb, ac,
726                                       req->op.search.base,
727                                       req->op.search.scope,
728                                       req->op.search.tree,
729                                       req->op.search.attrs,
730                                       req->controls,
731                                       ac, descriptor_search_callback,
732                                       ac->req);
733         LDB_REQ_SET_LOCATION(down_req);
734         if (ret != LDB_SUCCESS) {
735                 return ret;
736         }
737         /* mark it as handled */
738         if (sd_control) {
739                 sd_control->critical = 0;
740         }
741
742         return ldb_next_request(ac->module, down_req);
743 }
744 /* TODO */
745 static int descriptor_rename(struct ldb_module *module, struct ldb_request *req)
746 {
747         struct ldb_context *ldb = ldb_module_get_ctx(module);
748         ldb_debug(ldb, LDB_DEBUG_TRACE,"descriptor_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn));
749
750         /* do not manipulate our control entries */
751         if (ldb_dn_is_special(req->op.rename.olddn)) {
752                 return ldb_next_request(module, req);
753         }
754
755         return ldb_next_request(module, req);
756 }
757
758 static int descriptor_init(struct ldb_module *module)
759 {
760         int ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
761         struct ldb_context *ldb = ldb_module_get_ctx(module);
762         if (ret != LDB_SUCCESS) {
763                 ldb_debug(ldb, LDB_DEBUG_ERROR,
764                         "descriptor: Unable to register control with rootdse!\n");
765                 return ldb_operr(ldb);
766         }
767         return ldb_next_init(module);
768 }
769
770
771 static const struct ldb_module_ops ldb_descriptor_module_ops = {
772         .name          = "descriptor",
773         .search        = descriptor_search,
774         .add           = descriptor_add,
775         .modify        = descriptor_modify,
776         .rename        = descriptor_rename,
777         .init_context  = descriptor_init
778 };
779
780 int ldb_descriptor_module_init(const char *version)
781 {
782         LDB_MODULE_CHECK_VERSION(version);
783         return ldb_register_module(&ldb_descriptor_module_ops);
784 }