s4:dsdb Use helper function to add 'show deleted' control
[kai/samba-autobuild/.git] / source4 / dsdb / samdb / ldb_modules / objectclass.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
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: objectClass sorting module
25  *
26  *  Description: 
27  *  - sort the objectClass attribute into the class
28  *    hierarchy, 
29  *  - fix DNs and attributes into 'standard' case
30  *  - Add objectCategory and ntSecurityDescriptor defaults
31  *
32  *  Author: Andrew Bartlett
33  */
34
35
36 #include "includes.h"
37 #include "ldb_module.h"
38 #include "dlinklist.h"
39 #include "dsdb/samdb/samdb.h"
40 #include "librpc/ndr/libndr.h"
41 #include "librpc/gen_ndr/ndr_security.h"
42 #include "libcli/security/security.h"
43 #include "auth/auth.h"
44 #include "param/param.h"
45
46 struct oc_context {
47
48         struct ldb_module *module;
49         struct ldb_request *req;
50
51         struct ldb_reply *search_res;
52
53         int (*step_fn)(struct oc_context *);
54 };
55
56 struct class_list {
57         struct class_list *prev, *next;
58         const struct dsdb_class *objectclass;
59 };
60
61 static struct oc_context *oc_init_context(struct ldb_module *module,
62                                           struct ldb_request *req)
63 {
64         struct ldb_context *ldb;
65         struct oc_context *ac;
66
67         ldb = ldb_module_get_ctx(module);
68
69         ac = talloc_zero(req, struct oc_context);
70         if (ac == NULL) {
71                 ldb_set_errstring(ldb, "Out of Memory");
72                 return NULL;
73         }
74
75         ac->module = module;
76         ac->req = req;
77
78         return ac;
79 }
80
81 static int objectclass_do_add(struct oc_context *ac);
82
83 /* Sort objectClasses into correct order, and validate that all
84  * objectClasses specified actually exist in the schema
85  */
86
87 static int objectclass_sort(struct ldb_module *module,
88                             const struct dsdb_schema *schema,
89                             TALLOC_CTX *mem_ctx,
90                             struct ldb_message_element *objectclass_element,
91                             struct class_list **sorted_out) 
92 {
93         struct ldb_context *ldb;
94         int i;
95         int layer;
96         struct class_list *sorted = NULL, *parent_class = NULL,
97                 *subclass = NULL, *unsorted = NULL, *current, *poss_subclass, *poss_parent, *new_parent;
98
99         ldb = ldb_module_get_ctx(module);
100
101         /* DESIGN:
102          *
103          * We work on 4 different 'bins' (implemented here as linked lists):
104          *
105          * * sorted:       the eventual list, in the order we wish to push
106          *                 into the database.  This is the only ordered list.
107          *
108          * * parent_class: The current parent class 'bin' we are
109          *                 trying to find subclasses for
110          *
111          * * subclass:     The subclasses we have found so far
112          *
113          * * unsorted:     The remaining objectClasses
114          *
115          * The process is a matter of filtering objectClasses up from
116          * unsorted into sorted.  Order is irrelevent in the later 3 'bins'.
117          * 
118          * We start with 'top' (found and promoted to parent_class
119          * initially).  Then we find (in unsorted) all the direct
120          * subclasses of 'top'.  parent_classes is concatenated onto
121          * the end of 'sorted', and subclass becomes the list in
122          * parent_class.
123          *
124          * We then repeat, until we find no more subclasses.  Any left
125          * over classes are added to the end.
126          *
127          */
128
129         /* Firstly, dump all the objectClass elements into the
130          * unsorted bin, except for 'top', which is special */
131         for (i=0; i < objectclass_element->num_values; i++) {
132                 current = talloc(mem_ctx, struct class_list);
133                 if (!current) {
134                         ldb_oom(ldb);
135                         return LDB_ERR_OPERATIONS_ERROR;
136                 }
137                 current->objectclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &objectclass_element->values[i]);
138                 if (!current->objectclass) {
139                         ldb_asprintf_errstring(ldb, "objectclass %.*s is not a valid objectClass in schema", 
140                                                (int)objectclass_element->values[i].length, (const char *)objectclass_element->values[i].data);
141                         return LDB_ERR_OBJECT_CLASS_VIOLATION;
142                 }
143
144                 /* this is the root of the tree.  We will start
145                  * looking for subclasses from here */
146                 if (ldb_attr_cmp("top", current->objectclass->lDAPDisplayName) == 0) {
147                         DLIST_ADD_END(parent_class, current, struct class_list *);
148                 } else {
149                         DLIST_ADD_END(unsorted, current, struct class_list *);
150                 }
151         }
152
153         if (parent_class == NULL) {
154                 current = talloc(mem_ctx, struct class_list);
155                 current->objectclass = dsdb_class_by_lDAPDisplayName(schema, "top");
156                 DLIST_ADD_END(parent_class, current, struct class_list *);
157         }
158
159         /* For each object:  find parent chain */
160         for (current = unsorted; schema && current; current = current->next) {
161                 for (poss_parent = unsorted; poss_parent; poss_parent = poss_parent->next) {
162                         if (ldb_attr_cmp(poss_parent->objectclass->lDAPDisplayName, current->objectclass->subClassOf) == 0) {
163                                 break;
164                         }
165                 }
166                 /* If we didn't get to the end of the list, we need to add this parent */
167                 if (poss_parent || (ldb_attr_cmp("top", current->objectclass->subClassOf) == 0)) {
168                         continue;
169                 }
170
171                 new_parent = talloc(mem_ctx, struct class_list);
172                 new_parent->objectclass = dsdb_class_by_lDAPDisplayName(schema, current->objectclass->subClassOf);
173                 DLIST_ADD_END(unsorted, new_parent, struct class_list *);
174         }
175
176         /* DEBUGGING aid:  how many layers are we down now? */
177         layer = 0;
178         do {
179                 layer++;
180                 /* Find all the subclasses of classes in the
181                  * parent_classes.  Push them onto the subclass list */
182
183                 /* Ensure we don't bother if there are no unsorted entries left */
184                 for (current = parent_class; schema && unsorted && current; current = current->next) {
185                         /* Walk the list of possible subclasses in unsorted */
186                         for (poss_subclass = unsorted; poss_subclass; ) {
187                                 struct class_list *next;
188                                 
189                                 /* Save the next pointer, as the DLIST_ macros will change poss_subclass->next */
190                                 next = poss_subclass->next;
191
192                                 if (ldb_attr_cmp(poss_subclass->objectclass->subClassOf, current->objectclass->lDAPDisplayName) == 0) {
193                                         DLIST_REMOVE(unsorted, poss_subclass);
194                                         DLIST_ADD(subclass, poss_subclass);
195                                         
196                                         break;
197                                 }
198                                 poss_subclass = next;
199                         }
200                 }
201
202                 /* Now push the parent_classes as sorted, we are done with
203                 these.  Add to the END of the list by concatenation */
204                 DLIST_CONCATENATE(sorted, parent_class, struct class_list *);
205
206                 /* and now find subclasses of these */
207                 parent_class = subclass;
208                 subclass = NULL;
209
210                 /* If we didn't find any subclasses we will fall out
211                  * the bottom here */
212         } while (parent_class);
213
214         if (!unsorted) {
215                 *sorted_out = sorted;
216                 return LDB_SUCCESS;
217         }
218
219         if (!schema) {
220                 /* If we don't have schema yet, then just merge the lists again */
221                 DLIST_CONCATENATE(sorted, unsorted, struct class_list *);
222                 *sorted_out = sorted;
223                 return LDB_SUCCESS;
224         }
225
226         /* This shouldn't happen, and would break MMC, perhaps there
227          * was no 'top', a conflict in the objectClasses or some other
228          * schema error?
229          */
230         ldb_asprintf_errstring(ldb, "objectclass %s is not a valid objectClass in objectClass chain", unsorted->objectclass->lDAPDisplayName);
231         return LDB_ERR_OBJECT_CLASS_VIOLATION;
232 }
233
234 static DATA_BLOB *get_sd(struct ldb_module *module, TALLOC_CTX *mem_ctx, 
235                          const struct dsdb_class *objectclass) 
236 {
237         struct ldb_context *ldb = ldb_module_get_ctx(module);
238         enum ndr_err_code ndr_err;
239         DATA_BLOB *linear_sd;
240         struct auth_session_info *session_info
241                 = ldb_get_opaque(ldb, "sessionInfo");
242         struct security_descriptor *sd;
243         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
244
245         if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
246                 return NULL;
247         }
248         
249         sd = sddl_decode(mem_ctx, 
250                          objectclass->defaultSecurityDescriptor,
251                          domain_sid);
252
253         if (!sd || !session_info || !session_info->security_token) {
254                 return NULL;
255         }
256         
257         sd->owner_sid = session_info->security_token->user_sid;
258         sd->group_sid = session_info->security_token->group_sid;
259         
260         linear_sd = talloc(mem_ctx, DATA_BLOB);
261         if (!linear_sd) {
262                 return NULL;
263         }
264
265         ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx, 
266                                         lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
267                                        sd,
268                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
269         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
270                 return NULL;
271         }
272         
273         return linear_sd;
274
275 }
276
277 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
278 {
279         struct ldb_context *ldb;
280         struct oc_context *ac;
281         int ret;
282
283         ac = talloc_get_type(req->context, struct oc_context);
284         ldb = ldb_module_get_ctx(ac->module);
285
286         if (!ares) {
287                 return ldb_module_done(ac->req, NULL, NULL,
288                                         LDB_ERR_OPERATIONS_ERROR);
289         }
290         if (ares->error != LDB_SUCCESS &&
291             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
292                 return ldb_module_done(ac->req, ares->controls,
293                                         ares->response, ares->error);
294         }
295
296         switch (ares->type) {
297         case LDB_REPLY_ENTRY:
298                 if (ac->search_res != NULL) {
299                         ldb_set_errstring(ldb, "Too many results");
300                         talloc_free(ares);
301                         return ldb_module_done(ac->req, NULL, NULL,
302                                                 LDB_ERR_OPERATIONS_ERROR);
303                 }
304
305                 ac->search_res = talloc_steal(ac, ares);
306                 break;
307
308         case LDB_REPLY_REFERRAL:
309                 /* ignore */
310                 talloc_free(ares);
311                 break;
312
313         case LDB_REPLY_DONE:
314                 talloc_free(ares);
315                 ret = ac->step_fn(ac);
316                 if (ret != LDB_SUCCESS) {
317                         return ldb_module_done(ac->req, NULL, NULL, ret);
318                 }
319                 break;
320         }
321
322         return LDB_SUCCESS;
323 }
324
325 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
326 {
327         struct oc_context *ac;
328
329         ac = talloc_get_type(req->context, struct oc_context);
330
331         if (!ares) {
332                 return ldb_module_done(ac->req, NULL, NULL,
333                                         LDB_ERR_OPERATIONS_ERROR);
334         }
335         if (ares->error != LDB_SUCCESS) {
336                 return ldb_module_done(ac->req, ares->controls,
337                                         ares->response, ares->error);
338         }
339
340         if (ares->type != LDB_REPLY_DONE) {
341                 talloc_free(ares);
342                 return ldb_module_done(ac->req, NULL, NULL,
343                                         LDB_ERR_OPERATIONS_ERROR);
344         }
345
346         return ldb_module_done(ac->req, ares->controls,
347                                 ares->response, ares->error);
348 }
349
350 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN
351
352    This should mean that if the parent is:
353     CN=Users,DC=samba,DC=example,DC=com
354    and a proposed child is
355     cn=Admins ,cn=USERS,dc=Samba,dc=example,dc=COM
356
357    The resulting DN should be:
358
359     CN=Admins,CN=Users,DC=samba,DC=example,DC=com
360    
361  */
362 static int fix_dn(TALLOC_CTX *mem_ctx, 
363                   struct ldb_dn *newdn, struct ldb_dn *parent_dn, 
364                   struct ldb_dn **fixed_dn) 
365 {
366         char *upper_rdn_attr;
367         /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
368         *fixed_dn = ldb_dn_copy(mem_ctx, parent_dn);
369
370         /* We need the attribute name in upper case */
371         upper_rdn_attr = strupper_talloc(*fixed_dn, 
372                                          ldb_dn_get_rdn_name(newdn));
373         if (!upper_rdn_attr) {
374                 return LDB_ERR_OPERATIONS_ERROR;
375         }
376                                                
377         /* Create a new child */
378         if (ldb_dn_add_child_fmt(*fixed_dn, "X=X") == false) {
379                 return LDB_ERR_OPERATIONS_ERROR;
380         }
381
382         /* And replace it with CN=foo (we need the attribute in upper case */
383         return ldb_dn_set_component(*fixed_dn, 0, upper_rdn_attr,
384                                     *ldb_dn_get_rdn_val(newdn));
385 }
386
387 /* Fix all attribute names to be in the correct case, and check they are all valid per the schema */
388 static int fix_attributes(struct ldb_context *ldb, const struct dsdb_schema *schema, struct ldb_message *msg) 
389 {
390         int i;
391         for (i=0; i < msg->num_elements; i++) {
392                 const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema, msg->elements[i].name);
393                 /* Add in a very special case for 'clearTextPassword',
394                  * which is used for internal processing only, and is
395                  * not presented in the schema */
396                 if (!attribute) {
397                         if (strcasecmp(msg->elements[i].name, "clearTextPassword") != 0) {
398                                 ldb_asprintf_errstring(ldb, "attribute %s is not a valid attribute in schema", msg->elements[i].name);
399                                 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
400                         }
401                 } else {
402                         msg->elements[i].name = attribute->lDAPDisplayName;
403                 }
404         }
405
406         return LDB_SUCCESS;
407 }
408
409 static int objectclass_do_add(struct oc_context *ac);
410
411 static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
412 {
413         struct ldb_context *ldb;
414         struct ldb_request *search_req;
415         struct oc_context *ac;
416         struct ldb_dn *parent_dn;
417         int ret;
418         static const char * const parent_attrs[] = { "objectGUID", NULL };
419
420         ldb = ldb_module_get_ctx(module);
421
422         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
423
424         /* do not manipulate our control entries */
425         if (ldb_dn_is_special(req->op.add.message->dn)) {
426                 return ldb_next_request(module, req);
427         }
428
429         /* the objectClass must be specified on add */
430         if (ldb_msg_find_element(req->op.add.message, 
431                                  "objectClass") == NULL) {
432                 return LDB_ERR_OBJECT_CLASS_VIOLATION;
433         }
434
435         ac = oc_init_context(module, req);
436         if (ac == NULL) {
437                 return LDB_ERR_OPERATIONS_ERROR;
438         }
439
440         /* If there isn't a parent, just go on to the add processing */
441         if (ldb_dn_get_comp_num(ac->req->op.add.message->dn) == 1) {
442                 return objectclass_do_add(ac);
443         }
444
445         /* get copy of parent DN */
446         parent_dn = ldb_dn_get_parent(ac, ac->req->op.add.message->dn);
447         if (parent_dn == NULL) {
448                 ldb_oom(ldb);
449                 return LDB_ERR_OPERATIONS_ERROR;
450         }
451
452         ret = ldb_build_search_req(&search_req, ldb,
453                                    ac, parent_dn, LDB_SCOPE_BASE,
454                                    "(objectClass=*)", parent_attrs,
455                                    NULL,
456                                    ac, get_search_callback,
457                                    req);
458         if (ret != LDB_SUCCESS) {
459                 return ret;
460         }
461         talloc_steal(search_req, parent_dn);
462
463         ac->step_fn = objectclass_do_add;
464
465         return ldb_next_request(ac->module, search_req);
466 }
467
468 static int objectclass_do_add(struct oc_context *ac)
469 {
470         struct ldb_context *ldb;
471         const struct dsdb_schema *schema;
472         struct ldb_request *add_req;
473         char *value;
474         struct ldb_message_element *objectclass_element;
475         struct ldb_message *msg;
476         TALLOC_CTX *mem_ctx;
477         struct class_list *sorted, *current;
478         int ret;
479
480         ldb = ldb_module_get_ctx(ac->module);
481         schema = dsdb_get_schema(ldb);
482
483         mem_ctx = talloc_new(ac);
484         if (mem_ctx == NULL) {
485                 return LDB_ERR_OPERATIONS_ERROR;
486         }
487
488         msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
489
490         /* Check we have a valid parent */
491         if (ac->search_res == NULL) {
492                 if (ldb_dn_compare(ldb_get_root_basedn(ldb),
493                                                                 msg->dn) == 0) {
494                         /* Allow the tree to be started */
495                         
496                         /* but don't keep any error string, it's meaningless */
497                         ldb_set_errstring(ldb, NULL);
498                 } else {
499                         ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not exist!", 
500                                                ldb_dn_get_linearized(msg->dn));
501                         talloc_free(mem_ctx);
502                         return LDB_ERR_UNWILLING_TO_PERFORM;
503                 }
504         } else {
505                 const struct ldb_val *parent_guid;
506
507                 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
508                 ret = fix_dn(msg, 
509                              ac->req->op.add.message->dn,
510                              ac->search_res->message->dn,
511                              &msg->dn);
512
513                 if (ret != LDB_SUCCESS) {
514                         ldb_asprintf_errstring(ldb, "Could not munge DN %s into normal form", 
515                                                ldb_dn_get_linearized(ac->req->op.add.message->dn));
516                         talloc_free(mem_ctx);
517                         return ret;
518                 }
519
520                 parent_guid = ldb_msg_find_ldb_val(ac->search_res->message, "objectGUID");
521                 if (parent_guid == NULL) {
522                         ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not have an objectGUID!", 
523                                                ldb_dn_get_linearized(msg->dn));
524                         talloc_free(mem_ctx);
525                         return LDB_ERR_UNWILLING_TO_PERFORM;                    
526                 }
527
528                 /* TODO: Check this is a valid child to this parent,
529                  * by reading the allowedChildClasses and
530                  * allowedChildClasssesEffective attributes */
531                 ret = ldb_msg_add_steal_value(msg, "parentGUID", discard_const(parent_guid));
532                 if (ret != LDB_SUCCESS) {
533                         ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, failed to add parentGUID", 
534                                                ldb_dn_get_linearized(msg->dn));
535                         talloc_free(mem_ctx);
536                         return LDB_ERR_UNWILLING_TO_PERFORM;                                            
537                 }
538         }
539
540         if (schema) {
541                 ret = fix_attributes(ldb, schema, msg);
542                 if (ret != LDB_SUCCESS) {
543                         talloc_free(mem_ctx);
544                         return ret;
545                 }
546
547                 /* This is now the objectClass list from the database */
548                 objectclass_element = ldb_msg_find_element(msg, "objectClass");
549                 
550                 if (!objectclass_element) {
551                         /* Where did it go?  bail now... */
552                         talloc_free(mem_ctx);
553                         return LDB_ERR_OPERATIONS_ERROR;
554                 }
555                 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
556                 if (ret != LDB_SUCCESS) {
557                         talloc_free(mem_ctx);
558                         return ret;
559                 }
560                 
561                 ldb_msg_remove_attr(msg, "objectClass");
562                 ret = ldb_msg_add_empty(msg, "objectClass", 0, NULL);
563                 
564                 if (ret != LDB_SUCCESS) {
565                         talloc_free(mem_ctx);
566                         return ret;
567                 }
568                 
569                 /* We must completely replace the existing objectClass entry,
570                  * because we need it sorted */
571                 
572                 /* Move from the linked list back into an ldb msg */
573                 for (current = sorted; current; current = current->next) {
574                         value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
575                         if (value == NULL) {
576                                 ldb_oom(ldb);
577                                 talloc_free(mem_ctx);
578                                 return LDB_ERR_OPERATIONS_ERROR;
579                         }
580                         ret = ldb_msg_add_string(msg, "objectClass", value);
581                         if (ret != LDB_SUCCESS) {
582                                 ldb_set_errstring(ldb, 
583                                                   "objectclass: could not re-add sorted "
584                                                   "objectclass to modify msg");
585                                 talloc_free(mem_ctx);
586                                 return ret;
587                         }
588                         /* Last one is the critical one */
589                         if (!current->next) {
590                                 struct ldb_message_element *el;
591                                 int32_t systemFlags = 0;
592                                 if (!ldb_msg_find_element(msg, "objectCategory")) {
593                                         value = talloc_strdup(msg, current->objectclass->defaultObjectCategory);
594                                         if (value == NULL) {
595                                                 ldb_oom(ldb);
596                                                 talloc_free(mem_ctx);
597                                                 return LDB_ERR_OPERATIONS_ERROR;
598                                         }
599                                         ldb_msg_add_string(msg, "objectCategory", value);
600                                 }
601                                 if (!ldb_msg_find_element(msg, "showInAdvancedViewOnly") && (current->objectclass->defaultHidingValue == true)) {
602                                         ldb_msg_add_string(msg, "showInAdvancedViewOnly", 
603                                                            "TRUE");
604                                 }
605                                 if (!ldb_msg_find_element(msg, "nTSecurityDescriptor")) {
606                                         DATA_BLOB *sd = get_sd(ac->module, mem_ctx, current->objectclass);
607                                         if (sd) {
608                                                 ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
609                                         }
610                                 }
611
612                                 /* There are very special rules for systemFlags, see MS-ADTS 3.1.1.5.2.4 */
613                                 el = ldb_msg_find_element(msg, "systemFlags");
614
615                                 systemFlags = ldb_msg_find_attr_as_int(msg, "systemFlags", 0);
616
617                                 if (el) {
618                                         /* Only these flags may be set by a client, but we can't tell between a client and our provision at this point */
619                                         /* systemFlags &= ( SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_MOVE | SYSTEM_FLAG_CONFIG_LIMITED_MOVE); */
620                                         ldb_msg_remove_element(msg, el);
621                                 }
622                                 
623                                 /* This flag is only allowed on attributeSchema objects */
624                                 if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "attributeSchema") == 0) {
625                                         systemFlags &= ~SYSTEM_FLAG_ATTR_IS_RDN;
626                                 }
627
628                                 if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "server") == 0) {
629                                         systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE | SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE);
630                                 } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "site") == 0
631                                            || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "serverContainer") == 0
632                                            || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "ntDSDSA") == 0) {
633                                         systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
634
635                                 } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLink") == 0 
636                                            || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLinkBridge") == 0
637                                            || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "nTDSConnection") == 0) {
638                                         systemFlags |= (int32_t)(SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
639                                 }
640
641                                 /* TODO: If parent object is site or subnet, also add (SYSTEM_FLAG_CONFIG_ALLOW_RENAME) */
642
643                                 if (el || systemFlags != 0) {
644                                         samdb_msg_add_int(ldb, msg, msg, "systemFlags", systemFlags);
645                                 }
646                         }
647                 }
648         }
649
650         talloc_free(mem_ctx);
651         ret = ldb_msg_sanity_check(ldb, msg);
652
653
654         if (ret != LDB_SUCCESS) {
655                 return ret;
656         }
657
658         ret = ldb_build_add_req(&add_req, ldb, ac,
659                                 msg,
660                                 ac->req->controls,
661                                 ac, oc_op_callback,
662                                 ac->req);
663         if (ret != LDB_SUCCESS) {
664                 return ret;
665         }
666
667         /* perform the add */
668         return ldb_next_request(ac->module, add_req);
669 }
670
671 static int oc_modify_callback(struct ldb_request *req,
672                                 struct ldb_reply *ares);
673 static int objectclass_do_mod(struct oc_context *ac);
674
675 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
676 {
677         struct ldb_context *ldb = ldb_module_get_ctx(module);
678         struct ldb_message_element *objectclass_element;
679         struct ldb_message *msg;
680         const struct dsdb_schema *schema = dsdb_get_schema(ldb);
681         struct class_list *sorted, *current;
682         struct ldb_request *down_req;
683         struct oc_context *ac;
684         TALLOC_CTX *mem_ctx;
685         char *value;
686         int ret;
687
688         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
689
690         /* do not manipulate our control entries */
691         if (ldb_dn_is_special(req->op.mod.message->dn)) {
692                 return ldb_next_request(module, req);
693         }
694         
695         /* Without schema, there isn't much to do here */
696         if (!schema) {
697                 return ldb_next_request(module, req);
698         }
699         objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
700
701         ac = oc_init_context(module, req);
702         if (ac == NULL) {
703                 return LDB_ERR_OPERATIONS_ERROR;
704         }
705
706         /* If no part of this touches the objectClass, then we don't
707          * need to make any changes.  */
708
709         /* If the only operation is the deletion of the objectClass
710          * then go on with just fixing the attribute case */
711         if (!objectclass_element) {
712                 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
713                 if (msg == NULL) {
714                         return LDB_ERR_OPERATIONS_ERROR;
715                 }
716                 
717                 ret = fix_attributes(ldb, schema, msg);
718                 if (ret != LDB_SUCCESS) {
719                         return ret;
720                 }
721
722                 ret = ldb_build_mod_req(&down_req, ldb, ac,
723                                         msg,
724                                         req->controls,
725                                         ac, oc_op_callback,
726                                         req);
727                 if (ret != LDB_SUCCESS) {
728                         return ret;
729                 }
730
731                 /* go on with the call chain */
732                 return ldb_next_request(module, down_req);
733         }
734
735         switch (objectclass_element->flags & LDB_FLAG_MOD_MASK) {
736         case LDB_FLAG_MOD_DELETE:
737                 if (objectclass_element->num_values == 0) {
738                         return LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED;
739                 }
740                 break;
741
742         case LDB_FLAG_MOD_REPLACE:
743                 mem_ctx = talloc_new(ac);
744                 if (mem_ctx == NULL) {
745                         return LDB_ERR_OPERATIONS_ERROR;
746                 }
747
748                 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
749                 if (msg == NULL) {
750                         talloc_free(mem_ctx);
751                         return LDB_ERR_OPERATIONS_ERROR;
752                 }
753
754                 ret = fix_attributes(ldb, schema, msg);
755                 if (ret != LDB_SUCCESS) {
756                         talloc_free(mem_ctx);
757                         return ret;
758                 }
759
760                 ret = objectclass_sort(module, schema, mem_ctx, objectclass_element, &sorted);
761                 if (ret != LDB_SUCCESS) {
762                         talloc_free(mem_ctx);
763                         return ret;
764                 }
765
766                 /* We must completely replace the existing objectClass entry,
767                  * because we need it sorted */
768                 
769                 ldb_msg_remove_attr(msg, "objectClass");
770                 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
771                 
772                 if (ret != LDB_SUCCESS) {
773                         talloc_free(mem_ctx);
774                         return ret;
775                 }
776
777                 /* Move from the linked list back into an ldb msg */
778                 for (current = sorted; current; current = current->next) {
779                         /* copy the value as this string is on the schema
780                          * context and we can't rely on it not changing
781                          * before the operation is over */
782                         value = talloc_strdup(msg,
783                                         current->objectclass->lDAPDisplayName);
784                         if (value == NULL) {
785                                 ldb_oom(ldb);
786                                 talloc_free(mem_ctx);
787                                 return LDB_ERR_OPERATIONS_ERROR;
788                         }
789                         ret = ldb_msg_add_string(msg, "objectClass", value);
790                         if (ret != LDB_SUCCESS) {
791                                 ldb_set_errstring(ldb,
792                                         "objectclass: could not re-add sorted "
793                                         "objectclass to modify msg");
794                                 talloc_free(mem_ctx);
795                                 return ret;
796                         }
797                 }
798                 
799                 talloc_free(mem_ctx);
800
801                 ret = ldb_msg_sanity_check(ldb, msg);
802                 if (ret != LDB_SUCCESS) {
803                         return ret;
804                 }
805
806                 ret = ldb_build_mod_req(&down_req, ldb, ac,
807                                         msg,
808                                         req->controls,
809                                         ac, oc_op_callback,
810                                         req);
811                 if (ret != LDB_SUCCESS) {
812                         return ret;
813                 }
814
815                 /* go on with the call chain */
816                 return ldb_next_request(module, down_req);
817         }
818
819         /* This isn't the default branch of the switch, but a 'in any
820          * other case'.  When a delete isn't for all objectClasses for
821          * example
822          */
823
824         msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
825         if (msg == NULL) {
826                 ldb_oom(ldb);
827                 return LDB_ERR_OPERATIONS_ERROR;
828         }
829
830         ret = fix_attributes(ldb, schema, msg);
831         if (ret != LDB_SUCCESS) {
832                 ldb_oom(ldb);
833                 return ret;
834         }
835
836         ret = ldb_build_mod_req(&down_req, ldb, ac,
837                                 msg,
838                                 req->controls,
839                                 ac, oc_modify_callback,
840                                 req);
841         if (ret != LDB_SUCCESS) {
842                 return ret;
843         }
844
845         return ldb_next_request(module, down_req);
846 }
847
848 static int oc_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
849 {
850         struct ldb_context *ldb;
851         static const char * const attrs[] = { "objectClass", NULL };
852         struct ldb_request *search_req;
853         struct oc_context *ac;
854         int ret;
855
856         ac = talloc_get_type(req->context, struct oc_context);
857         ldb = ldb_module_get_ctx(ac->module);
858
859         if (!ares) {
860                 return ldb_module_done(ac->req, NULL, NULL,
861                                         LDB_ERR_OPERATIONS_ERROR);
862         }
863         if (ares->error != LDB_SUCCESS) {
864                 return ldb_module_done(ac->req, ares->controls,
865                                         ares->response, ares->error);
866         }
867
868         if (ares->type != LDB_REPLY_DONE) {
869                 talloc_free(ares);
870                 return ldb_module_done(ac->req, NULL, NULL,
871                                         LDB_ERR_OPERATIONS_ERROR);
872         }
873
874         ret = ldb_build_search_req(&search_req, ldb, ac,
875                                    ac->req->op.mod.message->dn, LDB_SCOPE_BASE,
876                                    "(objectClass=*)",
877                                    attrs, NULL, 
878                                    ac, get_search_callback,
879                                    ac->req);
880         if (ret != LDB_SUCCESS) {
881                 return ldb_module_done(ac->req, NULL, NULL, ret);
882         }
883
884         ac->step_fn = objectclass_do_mod;
885
886         ret = ldb_next_request(ac->module, search_req);
887         if (ret != LDB_SUCCESS) {
888                 return ldb_module_done(ac->req, NULL, NULL, ret);
889         }
890         return LDB_SUCCESS;
891 }
892
893 static int objectclass_do_mod(struct oc_context *ac)
894 {
895         struct ldb_context *ldb;
896         const struct dsdb_schema *schema;
897         struct ldb_request *mod_req;
898         char *value;
899         struct ldb_message_element *objectclass_element;
900         struct ldb_message *msg;
901         TALLOC_CTX *mem_ctx;
902         struct class_list *sorted, *current;
903         int ret;
904
905         ldb = ldb_module_get_ctx(ac->module);
906
907         if (ac->search_res == NULL) {
908                 return LDB_ERR_OPERATIONS_ERROR;
909         }
910         schema = dsdb_get_schema(ldb);
911
912         mem_ctx = talloc_new(ac);
913         if (mem_ctx == NULL) {
914                 return LDB_ERR_OPERATIONS_ERROR;
915         }
916
917         /* use a new message structure */
918         msg = ldb_msg_new(ac);
919         if (msg == NULL) {
920                 ldb_set_errstring(ldb,
921                         "objectclass: could not create new modify msg");
922                 talloc_free(mem_ctx);
923                 return LDB_ERR_OPERATIONS_ERROR;
924         }
925
926         /* This is now the objectClass list from the database */
927         objectclass_element = ldb_msg_find_element(ac->search_res->message, 
928                                                    "objectClass");
929         if (!objectclass_element) {
930                 /* Where did it go?  bail now... */
931                 talloc_free(mem_ctx);
932                 return LDB_ERR_OPERATIONS_ERROR;
933         }
934         
935         /* modify dn */
936         msg->dn = ac->req->op.mod.message->dn;
937
938         ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
939         if (ret != LDB_SUCCESS) {
940                 return ret;
941         }
942
943         /* We must completely replace the existing objectClass entry.
944          * We could do a constrained add/del, but we are meant to be
945          * in a transaction... */
946
947         ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
948         if (ret != LDB_SUCCESS) {
949                 ldb_set_errstring(ldb, "objectclass: could not clear objectclass in modify msg");
950                 talloc_free(mem_ctx);
951                 return ret;
952         }
953         
954         /* Move from the linked list back into an ldb msg */
955         for (current = sorted; current; current = current->next) {
956                 value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
957                 if (value == NULL) {
958                         ldb_oom(ldb);
959                         return LDB_ERR_OPERATIONS_ERROR;
960                 }
961                 ret = ldb_msg_add_string(msg, "objectClass", value);
962                 if (ret != LDB_SUCCESS) {
963                         ldb_set_errstring(ldb, "objectclass: could not re-add sorted objectclass to modify msg");
964                         talloc_free(mem_ctx);
965                         return ret;
966                 }
967         }
968
969         ret = ldb_msg_sanity_check(ldb, msg);
970         if (ret != LDB_SUCCESS) {
971                 talloc_free(mem_ctx);
972                 return ret;
973         }
974
975         ret = ldb_build_mod_req(&mod_req, ldb, ac,
976                                 msg,
977                                 ac->req->controls,
978                                 ac, oc_op_callback,
979                                 ac->req);
980         if (ret != LDB_SUCCESS) {
981                 talloc_free(mem_ctx);
982                 return ret;
983         }
984
985         talloc_free(mem_ctx);
986         /* perform the modify */
987         return ldb_next_request(ac->module, mod_req);
988 }
989
990 static int objectclass_do_rename(struct oc_context *ac);
991
992 static int objectclass_rename(struct ldb_module *module, struct ldb_request *req)
993 {
994         static const char * const attrs[] = { "objectGUID", NULL };
995         struct ldb_context *ldb;
996         struct ldb_request *search_req;
997         struct oc_context *ac;
998         struct ldb_dn *parent_dn;
999         int ret;
1000
1001         ldb = ldb_module_get_ctx(module);
1002
1003         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_rename\n");
1004
1005         if (ldb_dn_is_special(req->op.rename.newdn)) { /* do not manipulate our control entries */
1006                 return ldb_next_request(module, req);
1007         }
1008
1009         /* Firstly ensure we are not trying to rename it to be a child of itself */
1010         if ((ldb_dn_compare_base(req->op.rename.olddn, req->op.rename.newdn) == 0) 
1011             && (ldb_dn_compare(req->op.rename.olddn, req->op.rename.newdn) != 0)) {
1012                 ldb_asprintf_errstring(ldb, "Cannot rename %s to be a child of itself",
1013                                        ldb_dn_get_linearized(req->op.rename.olddn));
1014                 return LDB_ERR_UNWILLING_TO_PERFORM;
1015         }
1016
1017         ac = oc_init_context(module, req);
1018         if (ac == NULL) {
1019                 return LDB_ERR_OPERATIONS_ERROR;
1020         }
1021
1022         parent_dn = ldb_dn_get_parent(ac, req->op.rename.newdn);
1023         if (parent_dn == NULL) {
1024                 ldb_oom(ldb);
1025                 return LDB_ERR_OPERATIONS_ERROR;
1026         }
1027
1028         /* note that the results of this search are kept and used to
1029            update the parentGUID in objectclass_rename_callback() */
1030         ret = ldb_build_search_req(&search_req, ldb,
1031                                    ac, parent_dn, LDB_SCOPE_BASE,
1032                                    "(objectClass=*)",
1033                                    attrs, NULL, 
1034                                    ac, get_search_callback,
1035                                    req);
1036         if (ret != LDB_SUCCESS) {
1037                 return ret;
1038         }
1039
1040         /* we have to add the show deleted control, as otherwise DRS
1041            deletes will be refused as we will think the target parent
1042            does not exist */
1043         ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID, false, NULL);
1044
1045         if (ret != LDB_SUCCESS) {
1046                 return ret;
1047         }
1048
1049         ac->step_fn = objectclass_do_rename;
1050
1051         return ldb_next_request(ac->module, search_req);
1052 }
1053
1054 /* 
1055    called after the rename happens. 
1056    We now need to fix the parentGUID of the object to be the objectGUID of
1057    the new parent 
1058 */
1059 static int objectclass_rename_callback(struct ldb_request *req, struct ldb_reply *ares)
1060 {
1061         struct ldb_context *ldb;
1062         struct oc_context *ac;
1063         const struct ldb_val *parent_guid;
1064         struct ldb_request *mod_req = NULL;
1065         int ret;
1066         struct ldb_message *msg;
1067         struct ldb_message_element *el = NULL;
1068
1069         ac = talloc_get_type(req->context, struct oc_context);
1070         ldb = ldb_module_get_ctx(ac->module);
1071
1072         /* make sure the rename succeeded */
1073         if (!ares) {
1074                 return ldb_module_done(ac->req, NULL, NULL,
1075                                         LDB_ERR_OPERATIONS_ERROR);
1076         }
1077         if (ares->error != LDB_SUCCESS) {
1078                 return ldb_module_done(ac->req, ares->controls,
1079                                         ares->response, ares->error);
1080         }
1081
1082
1083         /* the ac->search_res should contain the new parents objectGUID */
1084         parent_guid = ldb_msg_find_ldb_val(ac->search_res->message, "objectGUID");
1085         if (parent_guid == NULL) {
1086                 ldb_asprintf_errstring(ldb, "objectclass: Cannot rename %s, new parent does not have an objectGUID!", 
1087                                        ldb_dn_get_linearized(ac->req->op.rename.newdn));
1088                 return LDB_ERR_UNWILLING_TO_PERFORM;
1089
1090         }
1091
1092         /* construct the modify message */
1093         msg = ldb_msg_new(ac);
1094         if (msg == NULL) {
1095                 ldb_oom(ldb);
1096                 return LDB_ERR_OPERATIONS_ERROR;
1097         }
1098
1099         msg->dn = ac->req->op.rename.newdn;
1100
1101         ret = ldb_msg_add_value(msg, "parentGUID", parent_guid, &el);
1102         if (ret != LDB_SUCCESS) {
1103                 return ret;
1104         }
1105
1106         el->flags = LDB_FLAG_MOD_REPLACE;
1107
1108         ret = ldb_build_mod_req(&mod_req, ldb, ac, msg,
1109                                 NULL, ac, oc_op_callback, req);
1110
1111         return ldb_next_request(ac->module, mod_req);
1112 }
1113
1114 static int objectclass_do_rename(struct oc_context *ac)
1115 {
1116         struct ldb_context *ldb;
1117         struct ldb_request *rename_req;
1118         struct ldb_dn *fixed_dn;
1119         int ret;
1120
1121         ldb = ldb_module_get_ctx(ac->module);
1122
1123         /* Check we have a valid parent */
1124         if (ac->search_res == NULL) {
1125                 ldb_asprintf_errstring(ldb, "objectclass: Cannot rename %s, parent does not exist!", 
1126                                        ldb_dn_get_linearized(ac->req->op.rename.newdn));
1127                 return LDB_ERR_UNWILLING_TO_PERFORM;
1128         }
1129         
1130         /* Fix up the DN to be in the standard form,
1131          * taking particular care to match the parent DN */
1132         ret = fix_dn(ac,
1133                      ac->req->op.rename.newdn,
1134                      ac->search_res->message->dn,
1135                      &fixed_dn);
1136         if (ret != LDB_SUCCESS) {
1137                 return ret;
1138         }
1139
1140         /* TODO: Check this is a valid child to this parent,
1141          * by reading the allowedChildClasses and
1142          * allowedChildClasssesEffective attributes */
1143
1144         ret = ldb_build_rename_req(&rename_req, ldb, ac,
1145                                    ac->req->op.rename.olddn, fixed_dn,
1146                                    ac->req->controls,
1147                                    ac, objectclass_rename_callback,
1148                                    ac->req);
1149         if (ret != LDB_SUCCESS) {
1150                 return ret;
1151         }
1152
1153         /* perform the rename */
1154         return ldb_next_request(ac->module, rename_req);
1155 }
1156
1157 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_module_ops = {
1158         .name              = "objectclass",
1159         .add           = objectclass_add,
1160         .modify        = objectclass_modify,
1161         .rename        = objectclass_rename,
1162 };