4 Copyright (C) Simo Sorce 2006
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2007
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.
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.
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/>.
24 * Component: objectClass sorting module
27 * - sort the objectClass attribute into the class
29 * - fix DNs and attributes into 'standard' case
30 * - Add objectCategory and ntSecurityDescriptor defaults
32 * Author: Andrew Bartlett
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"
50 enum oc_step {OC_DO_REQ, OC_SEARCH_SELF, OC_DO_MOD,
51 OC_SEARCH_ADD_PARENT, OC_DO_ADD,
52 OC_SEARCH_RENAME_PARENT, OC_DO_RENAME} step;
54 struct ldb_module *module;
55 struct ldb_request *orig_req;
57 struct ldb_request *down_req;
59 struct ldb_request *search_req;
60 struct ldb_reply *search_res;
62 struct ldb_request *add_req;
63 struct ldb_request *mod_req;
64 struct ldb_request *rename_req;
68 struct class_list *prev, *next;
69 const struct dsdb_class *objectclass;
72 static int objectclass_do_add(struct ldb_handle *h);
74 static struct ldb_handle *oc_init_handle(struct ldb_request *req, struct ldb_module *module)
76 struct oc_context *ac;
79 h = talloc_zero(req, struct ldb_handle);
81 ldb_set_errstring(module->ldb, "Out of Memory");
87 ac = talloc_zero(h, struct oc_context);
89 ldb_set_errstring(module->ldb, "Out of Memory");
94 h->private_data = (void *)ac;
96 h->state = LDB_ASYNC_INIT;
97 h->status = LDB_SUCCESS;
105 /* Sort objectClasses into correct order, and validate that all
106 * objectClasses specified actually exist in the schema
109 static int objectclass_sort(struct ldb_module *module,
110 const struct dsdb_schema *schema,
111 struct ldb_message *msg, /* so that when we create new elements, we put it on the right parent */
113 struct ldb_message_element *objectclass_element,
114 struct class_list **sorted_out)
118 struct class_list *sorted = NULL, *parent_class = NULL,
119 *subclass = NULL, *unsorted = NULL, *current, *poss_subclass, *poss_parent, *new_parent;
122 * We work on 4 different 'bins' (implemented here as linked lists):
124 * * sorted: the eventual list, in the order we wish to push
125 * into the database. This is the only ordered list.
127 * * parent_class: The current parent class 'bin' we are
128 * trying to find subclasses for
130 * * subclass: The subclasses we have found so far
132 * * unsorted: The remaining objectClasses
134 * The process is a matter of filtering objectClasses up from
135 * unsorted into sorted. Order is irrelevent in the later 3 'bins'.
137 * We start with 'top' (found and promoted to parent_class
138 * initially). Then we find (in unsorted) all the direct
139 * subclasses of 'top'. parent_classes is concatenated onto
140 * the end of 'sorted', and subclass becomes the list in
143 * We then repeat, until we find no more subclasses. Any left
144 * over classes are added to the end.
148 /* Firstly, dump all the objectClass elements into the
149 * unsorted bin, except for 'top', which is special */
150 for (i=0; i < objectclass_element->num_values; i++) {
151 current = talloc(mem_ctx, struct class_list);
153 ldb_set_errstring(module->ldb, "objectclass: out of memory allocating objectclass list");
154 talloc_free(mem_ctx);
155 return LDB_ERR_OPERATIONS_ERROR;
157 current->objectclass = dsdb_class_by_lDAPDisplayName(schema, (const char *)objectclass_element->values[i].data);
158 if (!current->objectclass) {
159 ldb_asprintf_errstring(module->ldb, "objectclass %s is not a valid objectClass in schema", (const char *)objectclass_element->values[i].data);
160 return LDB_ERR_OBJECT_CLASS_VIOLATION;
163 /* this is the root of the tree. We will start
164 * looking for subclasses from here */
165 if (ldb_attr_cmp("top", current->objectclass->lDAPDisplayName) == 0) {
166 DLIST_ADD_END(parent_class, current, struct class_list *);
168 DLIST_ADD_END(unsorted, current, struct class_list *);
172 if (parent_class == NULL) {
173 current = talloc(mem_ctx, struct class_list);
174 current->objectclass = dsdb_class_by_lDAPDisplayName(schema, "top");
175 DLIST_ADD_END(parent_class, current, struct class_list *);
178 /* For each object: find parent chain */
179 for (current = unsorted; schema && current; current = current->next) {
180 for (poss_parent = unsorted; poss_parent; poss_parent = poss_parent->next) {
181 if (ldb_attr_cmp(poss_parent->objectclass->lDAPDisplayName, current->objectclass->subClassOf) == 0) {
185 /* If we didn't get to the end of the list, we need to add this parent */
186 if (poss_parent || (ldb_attr_cmp("top", current->objectclass->subClassOf) == 0)) {
190 new_parent = talloc(mem_ctx, struct class_list);
191 new_parent->objectclass = dsdb_class_by_lDAPDisplayName(schema, current->objectclass->subClassOf);
192 DLIST_ADD_END(unsorted, new_parent, struct class_list *);
195 /* DEBUGGING aid: how many layers are we down now? */
199 /* Find all the subclasses of classes in the
200 * parent_classes. Push them onto the subclass list */
202 /* Ensure we don't bother if there are no unsorted entries left */
203 for (current = parent_class; schema && unsorted && current; current = current->next) {
204 /* Walk the list of possible subclasses in unsorted */
205 for (poss_subclass = unsorted; poss_subclass; ) {
206 struct class_list *next;
208 /* Save the next pointer, as the DLIST_ macros will change poss_subclass->next */
209 next = poss_subclass->next;
211 if (ldb_attr_cmp(poss_subclass->objectclass->subClassOf, current->objectclass->lDAPDisplayName) == 0) {
212 DLIST_REMOVE(unsorted, poss_subclass);
213 DLIST_ADD(subclass, poss_subclass);
217 poss_subclass = next;
221 /* Now push the parent_classes as sorted, we are done with
222 these. Add to the END of the list by concatenation */
223 DLIST_CONCATENATE(sorted, parent_class, struct class_list *);
225 /* and now find subclasses of these */
226 parent_class = subclass;
229 /* If we didn't find any subclasses we will fall out
231 } while (parent_class);
234 *sorted_out = sorted;
239 /* If we don't have schema yet, then just merge the lists again */
240 DLIST_CONCATENATE(sorted, unsorted, struct class_list *);
241 *sorted_out = sorted;
245 /* This shouldn't happen, and would break MMC, perhaps there
246 * was no 'top', a conflict in the objectClasses or some other
249 ldb_asprintf_errstring(module->ldb, "objectclass %s is not a valid objectClass in objectClass chain", unsorted->objectclass->lDAPDisplayName);
250 return LDB_ERR_OBJECT_CLASS_VIOLATION;
253 static DATA_BLOB *get_sd(struct ldb_module *module, TALLOC_CTX *mem_ctx,
254 const struct dsdb_class *objectclass)
256 enum ndr_err_code ndr_err;
257 DATA_BLOB *linear_sd;
258 struct auth_session_info *session_info
259 = ldb_get_opaque(module->ldb, "sessionInfo");
260 struct security_descriptor *sd;
261 struct dom_sid *domain_sid = samdb_domain_sid(module->ldb);
263 if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
267 sd = sddl_decode(mem_ctx,
268 objectclass->defaultSecurityDescriptor,
271 if (!sd || !session_info || !session_info->security_token) {
275 sd->owner_sid = session_info->security_token->user_sid;
276 sd->group_sid = session_info->security_token->group_sid;
278 linear_sd = talloc(mem_ctx, DATA_BLOB);
283 ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
284 lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
286 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
287 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
295 static int get_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
297 struct oc_context *ac;
299 ac = talloc_get_type(context, struct oc_context);
301 /* we are interested only in the single reply (base search) we receive here */
302 if (ares->type == LDB_REPLY_ENTRY) {
303 if (ac->search_res != NULL) {
304 ldb_set_errstring(ldb, "Too many results");
306 return LDB_ERR_OPERATIONS_ERROR;
309 ac->search_res = talloc_move(ac, &ares);
317 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN
319 This should mean that if the parent is:
320 CN=Users,DC=samba,DC=example,DC=com
321 and a proposed child is
322 cn=Admins ,cn=USERS,dc=Samba,dc=example,dc=COM
324 The resulting DN should be:
326 CN=Admins,CN=Users,DC=samba,DC=example,DC=com
329 static int fix_dn(TALLOC_CTX *mem_ctx,
330 struct ldb_dn *newdn, struct ldb_dn *parent_dn,
331 struct ldb_dn **fixed_dn)
333 char *upper_rdn_attr;
334 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
335 *fixed_dn = ldb_dn_copy(mem_ctx, parent_dn);
337 /* We need the attribute name in upper case */
338 upper_rdn_attr = strupper_talloc(*fixed_dn,
339 ldb_dn_get_rdn_name(newdn));
340 if (!upper_rdn_attr) {
341 return LDB_ERR_OPERATIONS_ERROR;
344 /* Create a new child */
345 if (ldb_dn_add_child_fmt(*fixed_dn, "X=X") == false) {
346 return LDB_ERR_OPERATIONS_ERROR;
349 /* And replace it with CN=foo (we need the attribute in upper case */
350 return ldb_dn_set_component(*fixed_dn, 0, upper_rdn_attr,
351 *ldb_dn_get_rdn_val(newdn));
354 /* Fix all attribute names to be in the correct case, and check they are all valid per the schema */
355 static int fix_attributes(struct ldb_context *ldb, const struct dsdb_schema *schema, struct ldb_message *msg)
358 for (i=0; i < msg->num_elements; i++) {
359 const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema, msg->elements[i].name);
361 ldb_asprintf_errstring(ldb, "attribute %s is not a valid attribute in schema", msg->elements[i].name);
362 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
364 msg->elements[i].name = attribute->lDAPDisplayName;
370 static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
373 static const char * const attrs[] = { NULL };
375 struct ldb_handle *h;
376 struct oc_context *ac;
377 struct ldb_dn *parent_dn;
380 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
382 /* do not manipulate our control entries */
383 if (ldb_dn_is_special(req->op.add.message->dn)) {
384 return ldb_next_request(module, req);
387 /* Need to object to this, but cn=rootdse doesn't hae an objectClass... */
388 if (ldb_msg_find_element(req->op.add.message,
389 "objectClass") == NULL) {
390 return ldb_next_request(module, req);
393 h = oc_init_handle(req, module);
395 return LDB_ERR_OPERATIONS_ERROR;
397 ac = talloc_get_type(h->private_data, struct oc_context);
399 /* return or own handle to deal with this call */
402 /* If there isn't a parent, just go on to the add processing */
403 if (ldb_dn_get_comp_num(ac->orig_req->op.add.message->dn) == 1) {
404 return objectclass_do_add(h);
407 parent_dn = ldb_dn_get_parent(ac, ac->orig_req->op.add.message->dn);
408 if (parent_dn == NULL) {
409 ldb_oom(module->ldb);
410 return LDB_ERR_OPERATIONS_ERROR;
413 ret = ldb_build_search_req(&ac->search_req, module->ldb,
414 ac, parent_dn, LDB_SCOPE_BASE,
417 ac, get_search_callback);
418 if (ret != LDB_SUCCESS) {
422 talloc_steal(ac->search_req, parent_dn);
424 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
426 ac->step = OC_SEARCH_ADD_PARENT;
428 return ldb_next_request(ac->module, ac->search_req);
431 static int objectclass_do_add(struct ldb_handle *h)
433 const struct dsdb_schema *schema;
434 struct oc_context *ac;
435 struct ldb_message_element *objectclass_element;
436 struct ldb_message *msg;
438 struct class_list *sorted, *current;
441 ac = talloc_get_type(h->private_data, struct oc_context);
442 schema = dsdb_get_schema(ac->module->ldb);
444 mem_ctx = talloc_new(ac);
445 if (mem_ctx == NULL) {
446 return LDB_ERR_OPERATIONS_ERROR;
449 ac->add_req = talloc(ac, struct ldb_request);
450 if (ac->add_req == NULL) {
451 talloc_free(mem_ctx);
452 return LDB_ERR_OPERATIONS_ERROR;
455 *ac->add_req = *ac->orig_req;
457 ac->add_req->op.add.message = msg = ldb_msg_copy_shallow(ac->add_req, ac->orig_req->op.add.message);
459 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->add_req);
461 /* Check we have a valid parent */
462 if (ac->search_res == NULL) {
463 if (ldb_dn_compare(ldb_get_root_basedn(ac->module->ldb), ac->orig_req->op.add.message->dn) == 0) {
464 /* Allow the tree to be started */
466 /* but don't keep any error string, it's meaningless */
467 ldb_set_errstring(ac->module->ldb, NULL);
469 ldb_asprintf_errstring(ac->module->ldb, "objectclass: Cannot add %s, parent does not exist!",
470 ldb_dn_get_linearized(ac->orig_req->op.add.message->dn));
471 return LDB_ERR_UNWILLING_TO_PERFORM;
475 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
477 ac->orig_req->op.add.message->dn,
478 ac->search_res->message->dn,
481 if (ret != LDB_SUCCESS) {
482 ldb_asprintf_errstring(ac->module->ldb, "Could not munge DN %s into normal form",
483 ldb_dn_get_linearized(ac->orig_req->op.add.message->dn));
487 /* TODO: Check this is a valid child to this parent,
488 * by reading the allowedChildClasses and
489 * allowedChildClasssesEffective attributes */
494 ret = fix_attributes(ac->module->ldb, schema, msg);
495 if (ret != LDB_SUCCESS) {
496 talloc_free(mem_ctx);
500 /* This is now the objectClass list from the database */
501 objectclass_element = ldb_msg_find_element(msg, "objectClass");
503 if (!objectclass_element) {
504 /* Where did it go? bail now... */
505 talloc_free(mem_ctx);
506 return LDB_ERR_OPERATIONS_ERROR;
508 ret = objectclass_sort(ac->module, schema, msg, mem_ctx, objectclass_element, &sorted);
509 if (ret != LDB_SUCCESS) {
510 talloc_free(mem_ctx);
514 ldb_msg_remove_attr(msg, "objectClass");
515 ret = ldb_msg_add_empty(msg, "objectClass", 0, NULL);
517 if (ret != LDB_SUCCESS) {
518 talloc_free(mem_ctx);
522 /* We must completely replace the existing objectClass entry,
523 * because we need it sorted */
525 /* Move from the linked list back into an ldb msg */
526 for (current = sorted; current; current = current->next) {
527 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass->lDAPDisplayName);
528 if (ret != LDB_SUCCESS) {
529 ldb_set_errstring(ac->module->ldb,
530 "objectclass: could not re-add sorted "
531 "objectclass to modify msg");
532 talloc_free(mem_ctx);
535 /* Last one is the critical one */
536 if (!current->next) {
537 if (!ldb_msg_find_element(msg, "objectCategory")) {
538 ldb_msg_add_string(msg, "objectCategory",
539 current->objectclass->defaultObjectCategory);
541 if (!ldb_msg_find_element(msg, "showInAdvancedViewOnly") && (current->objectclass->defaultHidingValue == true)) {
542 ldb_msg_add_string(msg, "showInAdvancedViewOnly",
545 if (!ldb_msg_find_element(msg, "nTSecurityDescriptor")) {
546 DATA_BLOB *sd = get_sd(ac->module, mem_ctx, current->objectclass);
548 ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
555 talloc_free(mem_ctx);
556 ret = ldb_msg_sanity_check(ac->module->ldb, msg);
559 if (ret != LDB_SUCCESS) {
563 h->state = LDB_ASYNC_INIT;
564 h->status = LDB_SUCCESS;
566 ac->step = OC_DO_ADD;
568 /* perform the add */
569 return ldb_next_request(ac->module, ac->add_req);
572 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
574 struct ldb_message_element *objectclass_element;
575 struct ldb_message *msg;
576 const struct dsdb_schema *schema = dsdb_get_schema(module->ldb);
579 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
581 /* do not manipulate our control entries */
582 if (ldb_dn_is_special(req->op.mod.message->dn)) {
583 return ldb_next_request(module, req);
586 /* Without schema, there isn't much to do here */
588 return ldb_next_request(module, req);
590 objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
592 /* If no part of this touches the objectClass, then we don't
593 * need to make any changes. */
595 /* If the only operation is the deletion of the objectClass
596 * then go on with just fixing the attribute case */
597 if (!objectclass_element) {
598 struct ldb_request *down_req = talloc(req, struct ldb_request);
599 if (down_req == NULL) {
600 ldb_set_errstring(module->ldb, "Out of memory!");
601 return LDB_ERR_OPERATIONS_ERROR;
604 *down_req = *req; /* copy the request */
606 down_req->op.mod.message = msg = ldb_msg_copy_shallow(down_req, req->op.mod.message);
608 if (down_req->op.mod.message == NULL) {
609 return LDB_ERR_OPERATIONS_ERROR;
612 ret = fix_attributes(module->ldb, schema, msg);
613 if (ret != LDB_SUCCESS) {
617 /* go on with the call chain */
618 ret = ldb_next_request(module, down_req);
620 /* do not free down_req as the call results may be linked to it,
621 * it will be freed when the upper level request get freed */
622 if (ret == LDB_SUCCESS) {
623 req->handle = down_req->handle;
628 switch (objectclass_element->flags & LDB_FLAG_MOD_MASK) {
629 case LDB_FLAG_MOD_DELETE:
630 if (objectclass_element->num_values == 0) {
631 return LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED;
634 case LDB_FLAG_MOD_REPLACE:
636 struct ldb_request *down_req;
637 struct class_list *sorted, *current;
639 mem_ctx = talloc_new(req);
640 if (mem_ctx == NULL) {
641 return LDB_ERR_OPERATIONS_ERROR;
644 /* prepare the first operation */
645 down_req = talloc(req, struct ldb_request);
646 if (down_req == NULL) {
647 ldb_set_errstring(module->ldb, "Out of memory!");
648 talloc_free(mem_ctx);
649 return LDB_ERR_OPERATIONS_ERROR;
652 *down_req = *req; /* copy the request */
654 down_req->op.mod.message = msg = ldb_msg_copy_shallow(down_req, req->op.mod.message);
656 if (down_req->op.mod.message == NULL) {
657 talloc_free(mem_ctx);
658 return LDB_ERR_OPERATIONS_ERROR;
661 ret = fix_attributes(module->ldb, schema, msg);
662 if (ret != LDB_SUCCESS) {
663 talloc_free(mem_ctx);
667 ret = objectclass_sort(module, schema, msg, mem_ctx, objectclass_element, &sorted);
668 if (ret != LDB_SUCCESS) {
672 /* We must completely replace the existing objectClass entry,
673 * because we need it sorted */
675 ldb_msg_remove_attr(msg, "objectClass");
676 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
678 if (ret != LDB_SUCCESS) {
679 talloc_free(mem_ctx);
683 /* Move from the linked list back into an ldb msg */
684 for (current = sorted; current; current = current->next) {
685 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass->lDAPDisplayName);
686 if (ret != LDB_SUCCESS) {
687 ldb_set_errstring(module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
688 talloc_free(mem_ctx);
693 talloc_free(mem_ctx);
695 ret = ldb_msg_sanity_check(module->ldb, msg);
696 if (ret != LDB_SUCCESS) {
697 talloc_free(mem_ctx);
701 /* go on with the call chain */
702 ret = ldb_next_request(module, down_req);
704 /* do not free down_req as the call results may be linked to it,
705 * it will be freed when the upper level request get freed */
706 if (ret == LDB_SUCCESS) {
707 req->handle = down_req->handle;
713 /* This isn't the default branch of the switch, but a 'in any
714 * other case'. When a delete isn't for all objectClasses for
718 struct ldb_handle *h;
719 struct oc_context *ac;
721 h = oc_init_handle(req, module);
723 return LDB_ERR_OPERATIONS_ERROR;
725 ac = talloc_get_type(h->private_data, struct oc_context);
727 /* return or own handle to deal with this call */
730 /* prepare the first operation */
731 ac->down_req = talloc(ac, struct ldb_request);
732 if (ac->down_req == NULL) {
733 ldb_oom(ac->module->ldb);
734 return LDB_ERR_OPERATIONS_ERROR;
737 *(ac->down_req) = *req; /* copy the request */
739 ac->down_req->op.mod.message = msg = ldb_msg_copy_shallow(ac->down_req, req->op.mod.message);
741 if (ac->down_req->op.mod.message == NULL) {
742 ldb_oom(ac->module->ldb);
743 return LDB_ERR_OPERATIONS_ERROR;
746 ret = fix_attributes(ac->module->ldb, schema, msg);
747 if (ret != LDB_SUCCESS) {
748 ldb_oom(ac->module->ldb);
752 ac->down_req->context = NULL;
753 ac->down_req->callback = NULL;
754 ldb_set_timeout_from_prev_req(module->ldb, req, ac->down_req);
756 ac->step = OC_DO_REQ;
758 return ldb_next_request(module, ac->down_req);
762 static int objectclass_search_self(struct ldb_handle *h)
765 struct oc_context *ac;
766 static const char * const attrs[] = { "objectClass", NULL };
768 ac = talloc_get_type(h->private_data, struct oc_context);
770 ret = ldb_build_search_req(&ac->search_req, ac->module->ldb,
771 ac, ac->orig_req->op.mod.message->dn, LDB_SCOPE_BASE,
774 ac, get_search_callback);
776 if (ret != LDB_SUCCESS) {
780 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
782 ac->step = OC_SEARCH_SELF;
784 return ldb_next_request(ac->module, ac->search_req);
787 static int objectclass_do_mod(struct ldb_handle *h) {
789 const struct dsdb_schema *schema;
790 struct oc_context *ac;
791 struct ldb_message_element *objectclass_element;
792 struct ldb_message *msg;
794 struct class_list *sorted, *current;
797 ac = talloc_get_type(h->private_data, struct oc_context);
798 schema = dsdb_get_schema(ac->module->ldb);
800 mem_ctx = talloc_new(ac);
801 if (mem_ctx == NULL) {
802 return LDB_ERR_OPERATIONS_ERROR;
805 ac->mod_req = talloc(ac, struct ldb_request);
806 if (ac->mod_req == NULL) {
807 talloc_free(mem_ctx);
808 return LDB_ERR_OPERATIONS_ERROR;
811 ac->mod_req->operation = LDB_MODIFY;
812 ac->mod_req->controls = NULL;
813 ac->mod_req->context = ac;
814 ac->mod_req->callback = NULL;
815 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->mod_req);
817 /* use a new message structure */
818 ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
820 ldb_set_errstring(ac->module->ldb, "objectclass: could not create new modify msg");
821 talloc_free(mem_ctx);
822 return LDB_ERR_OPERATIONS_ERROR;
825 /* This is now the objectClass list from the database */
826 objectclass_element = ldb_msg_find_element(ac->search_res->message,
828 if (!objectclass_element) {
829 /* Where did it go? bail now... */
830 talloc_free(mem_ctx);
831 return LDB_ERR_OPERATIONS_ERROR;
835 msg->dn = ac->orig_req->op.mod.message->dn;
837 ret = objectclass_sort(ac->module, schema, msg, mem_ctx, objectclass_element, &sorted);
838 if (ret != LDB_SUCCESS) {
842 /* We must completely replace the existing objectClass entry.
843 * We could do a constrained add/del, but we are meant to be
844 * in a transaction... */
846 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
847 if (ret != LDB_SUCCESS) {
848 ldb_set_errstring(ac->module->ldb, "objectclass: could not clear objectclass in modify msg");
849 talloc_free(mem_ctx);
853 /* Move from the linked list back into an ldb msg */
854 for (current = sorted; current; current = current->next) {
855 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass->lDAPDisplayName);
856 if (ret != LDB_SUCCESS) {
857 ldb_set_errstring(ac->module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
858 talloc_free(mem_ctx);
863 ret = ldb_msg_sanity_check(ac->module->ldb, msg);
864 if (ret != LDB_SUCCESS) {
865 talloc_free(mem_ctx);
870 h->state = LDB_ASYNC_INIT;
871 h->status = LDB_SUCCESS;
873 ac->step = OC_DO_MOD;
875 talloc_free(mem_ctx);
876 /* perform the search */
877 return ldb_next_request(ac->module, ac->mod_req);
880 static int objectclass_rename(struct ldb_module *module, struct ldb_request *req)
883 static const char * const attrs[] = { NULL };
885 struct ldb_handle *h;
886 struct oc_context *ac;
887 struct ldb_dn *parent_dn;
890 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_rename\n");
892 if (ldb_dn_is_special(req->op.rename.newdn)) { /* do not manipulate our control entries */
893 return ldb_next_request(module, req);
896 /* Firstly ensure we are not trying to rename it to be a child of itself */
897 if ((ldb_dn_compare_base(req->op.rename.olddn, req->op.rename.newdn) == 0)
898 && (ldb_dn_compare(req->op.rename.olddn, req->op.rename.newdn) != 0)) {
899 ldb_asprintf_errstring(module->ldb, "Cannot rename %s to be a child of itself",
900 ldb_dn_get_linearized(req->op.rename.olddn));
901 return LDB_ERR_UNWILLING_TO_PERFORM;
904 h = oc_init_handle(req, module);
906 return LDB_ERR_OPERATIONS_ERROR;
908 ac = talloc_get_type(h->private_data, struct oc_context);
910 /* return or own handle to deal with this call */
913 parent_dn = ldb_dn_get_parent(ac, ac->orig_req->op.rename.newdn);
914 if (parent_dn == NULL) {
915 ldb_oom(module->ldb);
916 return LDB_ERR_OPERATIONS_ERROR;
918 ret = ldb_build_search_req(&ac->search_req, module->ldb,
919 ac, parent_dn, LDB_SCOPE_BASE,
922 ac, get_search_callback);
923 if (ret != LDB_SUCCESS) {
926 talloc_steal(ac->search_req, parent_dn);
927 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
929 ac->step = OC_SEARCH_RENAME_PARENT;
931 return ldb_next_request(ac->module, ac->search_req);
934 static int objectclass_do_rename(struct ldb_handle *h)
936 struct oc_context *ac;
939 ac = talloc_get_type(h->private_data, struct oc_context);
941 ac->rename_req = talloc(ac, struct ldb_request);
942 if (ac->rename_req == NULL) {
943 return LDB_ERR_OPERATIONS_ERROR;
946 *ac->rename_req = *ac->orig_req;
948 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->rename_req);
950 /* Check we have a valid parent */
951 if (ac->search_res == NULL) {
952 ldb_asprintf_errstring(ac->module->ldb, "objectclass: Cannot rename %s, parent does not exist!",
953 ldb_dn_get_linearized(ac->orig_req->op.rename.newdn));
954 return LDB_ERR_UNWILLING_TO_PERFORM;
957 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
958 ret = fix_dn(ac->rename_req,
959 ac->orig_req->op.rename.newdn,
960 ac->search_res->message->dn,
961 &ac->rename_req->op.rename.newdn);
963 if (ret != LDB_SUCCESS) {
967 /* TODO: Check this is a valid child to this parent,
968 * by reading the allowedChildClasses and
969 * allowedChildClasssesEffective attributes */
971 h->state = LDB_ASYNC_INIT;
972 h->status = LDB_SUCCESS;
974 ac->step = OC_DO_RENAME;
976 /* perform the rename */
977 return ldb_next_request(ac->module, ac->rename_req);
980 static int oc_wait(struct ldb_handle *handle) {
981 struct oc_context *ac;
984 if (!handle || !handle->private_data) {
985 return LDB_ERR_OPERATIONS_ERROR;
988 if (handle->state == LDB_ASYNC_DONE) {
989 return handle->status;
992 handle->state = LDB_ASYNC_PENDING;
993 handle->status = LDB_SUCCESS;
995 ac = talloc_get_type(handle->private_data, struct oc_context);
999 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
1001 if (ret != LDB_SUCCESS) {
1002 handle->status = ret;
1005 if (ac->down_req->handle->status != LDB_SUCCESS) {
1006 handle->status = ac->down_req->handle->status;
1010 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
1014 /* mods done, go on */
1015 return objectclass_search_self(handle);
1017 case OC_SEARCH_SELF:
1018 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
1020 if (ret != LDB_SUCCESS) {
1021 handle->status = ret;
1024 if (ac->search_req->handle->status != LDB_SUCCESS) {
1025 handle->status = ac->search_req->handle->status;
1029 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
1033 /* self search done, go on */
1034 return objectclass_do_mod(handle);
1037 ret = ldb_wait(ac->mod_req->handle, LDB_WAIT_NONE);
1039 if (ret != LDB_SUCCESS) {
1040 handle->status = ret;
1043 if (ac->mod_req->handle->status != LDB_SUCCESS) {
1044 handle->status = ac->mod_req->handle->status;
1048 if (ac->mod_req->handle->state != LDB_ASYNC_DONE) {
1054 case OC_SEARCH_ADD_PARENT:
1055 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
1057 if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
1058 handle->status = ret;
1061 if (ac->search_req->handle->status != LDB_SUCCESS
1062 && ac->search_req->handle->status != LDB_ERR_NO_SUCH_OBJECT) {
1063 handle->status = ac->search_req->handle->status;
1067 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
1071 /* parent search done, go on */
1072 return objectclass_do_add(handle);
1075 ret = ldb_wait(ac->add_req->handle, LDB_WAIT_NONE);
1077 if (ret != LDB_SUCCESS) {
1078 handle->status = ret;
1081 if (ac->add_req->handle->status != LDB_SUCCESS) {
1082 handle->status = ac->add_req->handle->status;
1086 if (ac->add_req->handle->state != LDB_ASYNC_DONE) {
1092 case OC_SEARCH_RENAME_PARENT:
1093 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
1095 if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
1096 handle->status = ret;
1099 if (ac->search_req->handle->status != LDB_SUCCESS && ac->search_req->handle->status != LDB_ERR_NO_SUCH_OBJECT) {
1100 handle->status = ac->search_req->handle->status;
1104 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
1108 /* parent search done, go on */
1109 return objectclass_do_rename(handle);
1112 ret = ldb_wait(ac->rename_req->handle, LDB_WAIT_NONE);
1114 if (ret != LDB_SUCCESS) {
1115 handle->status = ret;
1118 if (ac->rename_req->handle->status != LDB_SUCCESS) {
1119 handle->status = ac->rename_req->handle->status;
1123 if (ac->rename_req->handle->state != LDB_ASYNC_DONE) {
1130 ret = LDB_ERR_OPERATIONS_ERROR;
1137 handle->state = LDB_ASYNC_DONE;
1141 static int oc_wait_all(struct ldb_handle *handle) {
1145 while (handle->state != LDB_ASYNC_DONE) {
1146 ret = oc_wait(handle);
1147 if (ret != LDB_SUCCESS) {
1152 return handle->status;
1155 static int objectclass_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1157 if (type == LDB_WAIT_ALL) {
1158 return oc_wait_all(handle);
1160 return oc_wait(handle);
1164 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_module_ops = {
1165 .name = "objectclass",
1166 .add = objectclass_add,
1167 .modify = objectclass_modify,
1168 .rename = objectclass_rename,
1169 .wait = objectclass_wait