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