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