4 Copyright (C) Simo Sorce 2006-2008
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 struct ldb_module *module;
51 struct ldb_request *req;
53 struct ldb_reply *search_res;
55 int (*step_fn)(struct oc_context *);
59 struct class_list *prev, *next;
60 const struct dsdb_class *objectclass;
63 static struct oc_context *oc_init_context(struct ldb_module *module,
64 struct ldb_request *req)
66 struct oc_context *ac;
68 ac = talloc_zero(req, struct oc_context);
70 ldb_set_errstring(module->ldb, "Out of Memory");
80 static int objectclass_do_add(struct oc_context *ac);
82 /* Sort objectClasses into correct order, and validate that all
83 * objectClasses specified actually exist in the schema
86 static int objectclass_sort(struct ldb_module *module,
87 const struct dsdb_schema *schema,
89 struct ldb_message_element *objectclass_element,
90 struct class_list **sorted_out)
94 struct class_list *sorted = NULL, *parent_class = NULL,
95 *subclass = NULL, *unsorted = NULL, *current, *poss_subclass, *poss_parent, *new_parent;
99 * We work on 4 different 'bins' (implemented here as linked lists):
101 * * sorted: the eventual list, in the order we wish to push
102 * into the database. This is the only ordered list.
104 * * parent_class: The current parent class 'bin' we are
105 * trying to find subclasses for
107 * * subclass: The subclasses we have found so far
109 * * unsorted: The remaining objectClasses
111 * The process is a matter of filtering objectClasses up from
112 * unsorted into sorted. Order is irrelevent in the later 3 'bins'.
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
120 * We then repeat, until we find no more subclasses. Any left
121 * over classes are added to the end.
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);
130 ldb_oom(module->ldb);
131 return LDB_ERR_OPERATIONS_ERROR;
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;
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 *);
144 DLIST_ADD_END(unsorted, current, struct class_list *);
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 *);
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) {
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)) {
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 *);
171 /* DEBUGGING aid: how many layers are we down now? */
175 /* Find all the subclasses of classes in the
176 * parent_classes. Push them onto the subclass list */
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;
184 /* Save the next pointer, as the DLIST_ macros will change poss_subclass->next */
185 next = poss_subclass->next;
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);
193 poss_subclass = next;
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 *);
201 /* and now find subclasses of these */
202 parent_class = subclass;
205 /* If we didn't find any subclasses we will fall out
207 } while (parent_class);
210 *sorted_out = sorted;
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;
221 /* This shouldn't happen, and would break MMC, perhaps there
222 * was no 'top', a conflict in the objectClasses or some other
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;
229 static DATA_BLOB *get_sd(struct ldb_module *module, TALLOC_CTX *mem_ctx,
230 const struct dsdb_class *objectclass)
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);
239 if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
243 sd = sddl_decode(mem_ctx,
244 objectclass->defaultSecurityDescriptor,
247 if (!sd || !session_info || !session_info->security_token) {
251 sd->owner_sid = session_info->security_token->user_sid;
252 sd->group_sid = session_info->security_token->group_sid;
254 linear_sd = talloc(mem_ctx, DATA_BLOB);
259 ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
260 lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
262 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
263 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
271 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
273 struct oc_context *ac;
276 ac = talloc_get_type(req->context, struct oc_context);
279 return ldb_module_done(ac->req, NULL, NULL,
280 LDB_ERR_OPERATIONS_ERROR);
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);
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");
293 return ldb_module_done(ac->req, NULL, NULL,
294 LDB_ERR_OPERATIONS_ERROR);
297 ac->search_res = talloc_steal(ac, ares);
300 case LDB_REPLY_REFERRAL:
307 ret = ac->step_fn(ac);
308 if (ret != LDB_SUCCESS) {
309 return ldb_module_done(ac->req, NULL, NULL, ret);
317 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
319 struct oc_context *ac;
321 ac = talloc_get_type(req->context, struct oc_context);
324 return ldb_module_done(ac->req, NULL, NULL,
325 LDB_ERR_OPERATIONS_ERROR);
327 if (ares->error != LDB_SUCCESS) {
328 return ldb_module_done(ac->req, ares->controls,
329 ares->response, ares->error);
332 if (ares->type != LDB_REPLY_DONE) {
334 return ldb_module_done(ac->req, NULL, NULL,
335 LDB_ERR_OPERATIONS_ERROR);
338 return ldb_module_done(ac->req, ares->controls,
339 ares->response, ares->error);
342 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN
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
349 The resulting DN should be:
351 CN=Admins,CN=Users,DC=samba,DC=example,DC=com
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)
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);
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;
369 /* Create a new child */
370 if (ldb_dn_add_child_fmt(*fixed_dn, "X=X") == false) {
371 return LDB_ERR_OPERATIONS_ERROR;
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));
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)
383 for (i=0; i < msg->num_elements; i++) {
384 const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema, msg->elements[i].name);
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;
389 msg->elements[i].name = attribute->lDAPDisplayName;
395 static int objectclass_do_add(struct oc_context *ac);
397 static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
399 struct ldb_request *search_req;
400 struct oc_context *ac;
401 struct ldb_dn *parent_dn;
404 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
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);
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);
417 ac = oc_init_context(module, req);
419 return LDB_ERR_OPERATIONS_ERROR;
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);
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;
434 ret = ldb_build_search_req(&search_req, module->ldb,
435 ac, parent_dn, LDB_SCOPE_BASE,
436 "(objectClass=*)", NULL,
438 ac, get_search_callback,
440 if (ret != LDB_SUCCESS) {
443 talloc_steal(search_req, parent_dn);
445 ac->step_fn = objectclass_do_add;
447 return ldb_next_request(ac->module, search_req);
450 static int objectclass_do_add(struct oc_context *ac)
452 const struct dsdb_schema *schema;
453 struct ldb_request *add_req;
455 struct ldb_message_element *objectclass_element;
456 struct ldb_message *msg;
458 struct class_list *sorted, *current;
461 schema = dsdb_get_schema(ac->module->ldb);
463 mem_ctx = talloc_new(ac);
464 if (mem_ctx == NULL) {
465 return LDB_ERR_OPERATIONS_ERROR;
468 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
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),
474 /* Allow the tree to be started */
476 /* but don't keep any error string, it's meaningless */
477 ldb_set_errstring(ac->module->ldb, NULL);
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;
486 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
488 ac->req->op.add.message->dn,
489 ac->search_res->message->dn,
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);
499 /* TODO: Check this is a valid child to this parent,
500 * by reading the allowedChildClasses and
501 * allowedChildClasssesEffective attributes */
506 ret = fix_attributes(ac->module->ldb, schema, msg);
507 if (ret != LDB_SUCCESS) {
508 talloc_free(mem_ctx);
512 /* This is now the objectClass list from the database */
513 objectclass_element = ldb_msg_find_element(msg, "objectClass");
515 if (!objectclass_element) {
516 /* Where did it go? bail now... */
517 talloc_free(mem_ctx);
518 return LDB_ERR_OPERATIONS_ERROR;
520 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
521 if (ret != LDB_SUCCESS) {
522 talloc_free(mem_ctx);
526 ldb_msg_remove_attr(msg, "objectClass");
527 ret = ldb_msg_add_empty(msg, "objectClass", 0, NULL);
529 if (ret != LDB_SUCCESS) {
530 talloc_free(mem_ctx);
534 /* We must completely replace the existing objectClass entry,
535 * because we need it sorted */
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);
541 ldb_oom(ac->module->ldb);
542 talloc_free(mem_ctx);
543 return LDB_ERR_OPERATIONS_ERROR;
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);
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);
560 ldb_oom(ac->module->ldb);
561 talloc_free(mem_ctx);
562 return LDB_ERR_OPERATIONS_ERROR;
564 ldb_msg_add_string(msg, "objectCategory", value);
566 if (!ldb_msg_find_element(msg, "showInAdvancedViewOnly") && (current->objectclass->defaultHidingValue == true)) {
567 ldb_msg_add_string(msg, "showInAdvancedViewOnly",
570 if (!ldb_msg_find_element(msg, "nTSecurityDescriptor")) {
571 DATA_BLOB *sd = get_sd(ac->module, mem_ctx, current->objectclass);
573 ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
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");
580 systemFlags = ldb_msg_find_attr_as_int(msg, "systemFlags", 0);
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);
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;
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);
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);
606 /* TODO: If parent object is site or subnet, also add (SYSTEM_FLAG_CONFIG_ALLOW_RENAME) */
608 if (el || systemFlags != 0) {
609 samdb_msg_add_int(ac->module->ldb, msg, msg, "systemFlags", systemFlags);
615 talloc_free(mem_ctx);
616 ret = ldb_msg_sanity_check(ac->module->ldb, msg);
619 if (ret != LDB_SUCCESS) {
623 ret = ldb_build_add_req(&add_req, ac->module->ldb, ac,
628 if (ret != LDB_SUCCESS) {
632 /* perform the add */
633 return ldb_next_request(ac->module, add_req);
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);
640 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
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;
652 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
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);
659 /* Without schema, there isn't much to do here */
661 return ldb_next_request(module, req);
663 objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
665 ac = oc_init_context(module, req);
667 return LDB_ERR_OPERATIONS_ERROR;
670 /* If no part of this touches the objectClass, then we don't
671 * need to make any changes. */
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);
678 return LDB_ERR_OPERATIONS_ERROR;
681 ret = fix_attributes(module->ldb, schema, msg);
682 if (ret != LDB_SUCCESS) {
686 ret = ldb_build_mod_req(&down_req, module->ldb, ac,
691 if (ret != LDB_SUCCESS) {
695 /* go on with the call chain */
696 return ldb_next_request(module, down_req);
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;
706 case LDB_FLAG_MOD_REPLACE:
707 mem_ctx = talloc_new(ac);
708 if (mem_ctx == NULL) {
709 return LDB_ERR_OPERATIONS_ERROR;
712 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
714 talloc_free(mem_ctx);
715 return LDB_ERR_OPERATIONS_ERROR;
718 ret = fix_attributes(module->ldb, schema, msg);
719 if (ret != LDB_SUCCESS) {
720 talloc_free(mem_ctx);
724 ret = objectclass_sort(module, schema, mem_ctx, objectclass_element, &sorted);
725 if (ret != LDB_SUCCESS) {
726 talloc_free(mem_ctx);
730 /* We must completely replace the existing objectClass entry,
731 * because we need it sorted */
733 ldb_msg_remove_attr(msg, "objectClass");
734 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
736 if (ret != LDB_SUCCESS) {
737 talloc_free(mem_ctx);
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);
749 ldb_oom(module->ldb);
750 talloc_free(mem_ctx);
751 return LDB_ERR_OPERATIONS_ERROR;
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);
763 talloc_free(mem_ctx);
765 ret = ldb_msg_sanity_check(module->ldb, msg);
766 if (ret != LDB_SUCCESS) {
770 ret = ldb_build_mod_req(&down_req, module->ldb, ac,
775 if (ret != LDB_SUCCESS) {
779 /* go on with the call chain */
780 return ldb_next_request(module, down_req);
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
788 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
790 ldb_oom(module->ldb);
791 return LDB_ERR_OPERATIONS_ERROR;
794 ret = fix_attributes(module->ldb, schema, msg);
795 if (ret != LDB_SUCCESS) {
796 ldb_oom(ac->module->ldb);
800 ret = ldb_build_mod_req(&down_req, module->ldb, ac,
803 ac, oc_modify_callback,
805 if (ret != LDB_SUCCESS) {
809 return ldb_next_request(module, down_req);
812 static int oc_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
814 static const char * const attrs[] = { "objectClass", NULL };
815 struct ldb_request *search_req;
816 struct oc_context *ac;
819 ac = talloc_get_type(req->context, struct oc_context);
822 return ldb_module_done(ac->req, NULL, NULL,
823 LDB_ERR_OPERATIONS_ERROR);
825 if (ares->error != LDB_SUCCESS) {
826 return ldb_module_done(ac->req, ares->controls,
827 ares->response, ares->error);
830 if (ares->type != LDB_REPLY_DONE) {
832 return ldb_module_done(ac->req, NULL, NULL,
833 LDB_ERR_OPERATIONS_ERROR);
836 ret = ldb_build_search_req(&search_req, ac->module->ldb, ac,
837 ac->req->op.mod.message->dn, LDB_SCOPE_BASE,
840 ac, get_search_callback,
842 if (ret != LDB_SUCCESS) {
843 return ldb_module_done(ac->req, NULL, NULL, ret);
846 ac->step_fn = objectclass_do_mod;
848 ret = ldb_next_request(ac->module, search_req);
849 if (ret != LDB_SUCCESS) {
850 return ldb_module_done(ac->req, NULL, NULL, ret);
855 static int objectclass_do_mod(struct oc_context *ac)
858 const struct dsdb_schema *schema;
859 struct ldb_request *mod_req;
861 struct ldb_message_element *objectclass_element;
862 struct ldb_message *msg;
864 struct class_list *sorted, *current;
867 if (ac->search_res == NULL) {
868 return LDB_ERR_OPERATIONS_ERROR;
870 schema = dsdb_get_schema(ac->module->ldb);
872 mem_ctx = talloc_new(ac);
873 if (mem_ctx == NULL) {
874 return LDB_ERR_OPERATIONS_ERROR;
877 /* use a new message structure */
878 msg = ldb_msg_new(ac);
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;
886 /* This is now the objectClass list from the database */
887 objectclass_element = ldb_msg_find_element(ac->search_res->message,
889 if (!objectclass_element) {
890 /* Where did it go? bail now... */
891 talloc_free(mem_ctx);
892 return LDB_ERR_OPERATIONS_ERROR;
896 msg->dn = ac->req->op.mod.message->dn;
898 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
899 if (ret != LDB_SUCCESS) {
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... */
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);
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);
918 ldb_oom(ac->module->ldb);
919 return LDB_ERR_OPERATIONS_ERROR;
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);
929 ret = ldb_msg_sanity_check(ac->module->ldb, msg);
930 if (ret != LDB_SUCCESS) {
931 talloc_free(mem_ctx);
935 ret = ldb_build_mod_req(&mod_req, ac->module->ldb, ac,
940 if (ret != LDB_SUCCESS) {
941 talloc_free(mem_ctx);
945 talloc_free(mem_ctx);
946 /* perform the modify */
947 return ldb_next_request(ac->module, mod_req);
950 static int objectclass_do_rename(struct oc_context *ac);
952 static int objectclass_rename(struct ldb_module *module, struct ldb_request *req)
954 static const char * const attrs[] = { NULL };
956 struct ldb_request *search_req;
957 struct oc_context *ac;
958 struct ldb_dn *parent_dn;
961 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_rename\n");
963 if (ldb_dn_is_special(req->op.rename.newdn)) { /* do not manipulate our control entries */
964 return ldb_next_request(module, req);
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;
975 ac = oc_init_context(module, req);
977 return LDB_ERR_OPERATIONS_ERROR;
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;
985 ret = ldb_build_search_req(&search_req, module->ldb,
986 ac, parent_dn, LDB_SCOPE_BASE,
989 ac, get_search_callback,
991 if (ret != LDB_SUCCESS) {
995 ac->step_fn = objectclass_do_rename;
997 return ldb_next_request(ac->module, search_req);
1000 static int objectclass_do_rename(struct oc_context *ac)
1002 struct ldb_request *rename_req;
1003 struct ldb_dn *fixed_dn;
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;
1013 /* Fix up the DN to be in the standard form,
1014 * taking particular care to match the parent DN */
1016 ac->req->op.rename.newdn,
1017 ac->search_res->message->dn,
1019 if (ret != LDB_SUCCESS) {
1023 /* TODO: Check this is a valid child to this parent,
1024 * by reading the allowedChildClasses and
1025 * allowedChildClasssesEffective attributes */
1027 ret = ldb_build_rename_req(&rename_req, ac->module->ldb, ac,
1028 ac->req->op.rename.olddn, fixed_dn,
1032 if (ret != LDB_SUCCESS) {
1036 /* perform the rename */
1037 return ldb_next_request(ac->module, rename_req);
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,