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