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_module.h"
38 #include "dlinklist.h"
39 #include "dsdb/samdb/samdb.h"
40 #include "librpc/ndr/libndr.h"
41 #include "librpc/gen_ndr/ndr_security.h"
42 #include "libcli/security/security.h"
43 #include "auth/auth.h"
44 #include "param/param.h"
48 struct ldb_module *module;
49 struct ldb_request *req;
51 struct ldb_reply *search_res;
53 int (*step_fn)(struct oc_context *);
57 struct class_list *prev, *next;
58 const struct dsdb_class *objectclass;
61 static struct oc_context *oc_init_context(struct ldb_module *module,
62 struct ldb_request *req)
64 struct ldb_context *ldb;
65 struct oc_context *ac;
67 ldb = ldb_module_get_ctx(module);
69 ac = talloc_zero(req, struct oc_context);
71 ldb_set_errstring(ldb, "Out of Memory");
81 static int objectclass_do_add(struct oc_context *ac);
83 /* Sort objectClasses into correct order, and validate that all
84 * objectClasses specified actually exist in the schema
87 static int objectclass_sort(struct ldb_module *module,
88 const struct dsdb_schema *schema,
90 struct ldb_message_element *objectclass_element,
91 struct class_list **sorted_out)
93 struct ldb_context *ldb;
96 struct class_list *sorted = NULL, *parent_class = NULL,
97 *subclass = NULL, *unsorted = NULL, *current, *poss_subclass, *poss_parent, *new_parent;
99 ldb = ldb_module_get_ctx(module);
103 * We work on 4 different 'bins' (implemented here as linked lists):
105 * * sorted: the eventual list, in the order we wish to push
106 * into the database. This is the only ordered list.
108 * * parent_class: The current parent class 'bin' we are
109 * trying to find subclasses for
111 * * subclass: The subclasses we have found so far
113 * * unsorted: The remaining objectClasses
115 * The process is a matter of filtering objectClasses up from
116 * unsorted into sorted. Order is irrelevent in the later 3 'bins'.
118 * We start with 'top' (found and promoted to parent_class
119 * initially). Then we find (in unsorted) all the direct
120 * subclasses of 'top'. parent_classes is concatenated onto
121 * the end of 'sorted', and subclass becomes the list in
124 * We then repeat, until we find no more subclasses. Any left
125 * over classes are added to the end.
129 /* Firstly, dump all the objectClass elements into the
130 * unsorted bin, except for 'top', which is special */
131 for (i=0; i < objectclass_element->num_values; i++) {
132 current = talloc(mem_ctx, struct class_list);
135 return LDB_ERR_OPERATIONS_ERROR;
137 current->objectclass = dsdb_class_by_lDAPDisplayName(schema, (const char *)objectclass_element->values[i].data);
138 if (!current->objectclass) {
139 ldb_asprintf_errstring(ldb, "objectclass %s is not a valid objectClass in schema", (const char *)objectclass_element->values[i].data);
140 return LDB_ERR_OBJECT_CLASS_VIOLATION;
143 /* this is the root of the tree. We will start
144 * looking for subclasses from here */
145 if (ldb_attr_cmp("top", current->objectclass->lDAPDisplayName) == 0) {
146 DLIST_ADD_END(parent_class, current, struct class_list *);
148 DLIST_ADD_END(unsorted, current, struct class_list *);
152 if (parent_class == NULL) {
153 current = talloc(mem_ctx, struct class_list);
154 current->objectclass = dsdb_class_by_lDAPDisplayName(schema, "top");
155 DLIST_ADD_END(parent_class, current, struct class_list *);
158 /* For each object: find parent chain */
159 for (current = unsorted; schema && current; current = current->next) {
160 for (poss_parent = unsorted; poss_parent; poss_parent = poss_parent->next) {
161 if (ldb_attr_cmp(poss_parent->objectclass->lDAPDisplayName, current->objectclass->subClassOf) == 0) {
165 /* If we didn't get to the end of the list, we need to add this parent */
166 if (poss_parent || (ldb_attr_cmp("top", current->objectclass->subClassOf) == 0)) {
170 new_parent = talloc(mem_ctx, struct class_list);
171 new_parent->objectclass = dsdb_class_by_lDAPDisplayName(schema, current->objectclass->subClassOf);
172 DLIST_ADD_END(unsorted, new_parent, struct class_list *);
175 /* DEBUGGING aid: how many layers are we down now? */
179 /* Find all the subclasses of classes in the
180 * parent_classes. Push them onto the subclass list */
182 /* Ensure we don't bother if there are no unsorted entries left */
183 for (current = parent_class; schema && unsorted && current; current = current->next) {
184 /* Walk the list of possible subclasses in unsorted */
185 for (poss_subclass = unsorted; poss_subclass; ) {
186 struct class_list *next;
188 /* Save the next pointer, as the DLIST_ macros will change poss_subclass->next */
189 next = poss_subclass->next;
191 if (ldb_attr_cmp(poss_subclass->objectclass->subClassOf, current->objectclass->lDAPDisplayName) == 0) {
192 DLIST_REMOVE(unsorted, poss_subclass);
193 DLIST_ADD(subclass, poss_subclass);
197 poss_subclass = next;
201 /* Now push the parent_classes as sorted, we are done with
202 these. Add to the END of the list by concatenation */
203 DLIST_CONCATENATE(sorted, parent_class, struct class_list *);
205 /* and now find subclasses of these */
206 parent_class = subclass;
209 /* If we didn't find any subclasses we will fall out
211 } while (parent_class);
214 *sorted_out = sorted;
219 /* If we don't have schema yet, then just merge the lists again */
220 DLIST_CONCATENATE(sorted, unsorted, struct class_list *);
221 *sorted_out = sorted;
225 /* This shouldn't happen, and would break MMC, perhaps there
226 * was no 'top', a conflict in the objectClasses or some other
229 ldb_asprintf_errstring(ldb, "objectclass %s is not a valid objectClass in objectClass chain", unsorted->objectclass->lDAPDisplayName);
230 return LDB_ERR_OBJECT_CLASS_VIOLATION;
233 static DATA_BLOB *get_sd(struct ldb_module *module, TALLOC_CTX *mem_ctx,
234 const struct dsdb_class *objectclass)
236 struct ldb_context *ldb = ldb_module_get_ctx(module);
237 enum ndr_err_code ndr_err;
238 DATA_BLOB *linear_sd;
239 struct auth_session_info *session_info
240 = ldb_get_opaque(ldb, "sessionInfo");
241 struct security_descriptor *sd;
242 const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
244 if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
248 sd = sddl_decode(mem_ctx,
249 objectclass->defaultSecurityDescriptor,
252 if (!sd || !session_info || !session_info->security_token) {
256 sd->owner_sid = session_info->security_token->user_sid;
257 sd->group_sid = session_info->security_token->group_sid;
259 linear_sd = talloc(mem_ctx, DATA_BLOB);
264 ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
265 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
267 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
268 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
276 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
278 struct ldb_context *ldb;
279 struct oc_context *ac;
282 ac = talloc_get_type(req->context, struct oc_context);
283 ldb = ldb_module_get_ctx(ac->module);
286 return ldb_module_done(ac->req, NULL, NULL,
287 LDB_ERR_OPERATIONS_ERROR);
289 if (ares->error != LDB_SUCCESS &&
290 ares->error != LDB_ERR_NO_SUCH_OBJECT) {
291 return ldb_module_done(ac->req, ares->controls,
292 ares->response, ares->error);
295 switch (ares->type) {
296 case LDB_REPLY_ENTRY:
297 if (ac->search_res != NULL) {
298 ldb_set_errstring(ldb, "Too many results");
300 return ldb_module_done(ac->req, NULL, NULL,
301 LDB_ERR_OPERATIONS_ERROR);
304 ac->search_res = talloc_steal(ac, ares);
307 case LDB_REPLY_REFERRAL:
314 ret = ac->step_fn(ac);
315 if (ret != LDB_SUCCESS) {
316 return ldb_module_done(ac->req, NULL, NULL, ret);
324 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
326 struct oc_context *ac;
328 ac = talloc_get_type(req->context, struct oc_context);
331 return ldb_module_done(ac->req, NULL, NULL,
332 LDB_ERR_OPERATIONS_ERROR);
334 if (ares->error != LDB_SUCCESS) {
335 return ldb_module_done(ac->req, ares->controls,
336 ares->response, ares->error);
339 if (ares->type != LDB_REPLY_DONE) {
341 return ldb_module_done(ac->req, NULL, NULL,
342 LDB_ERR_OPERATIONS_ERROR);
345 return ldb_module_done(ac->req, ares->controls,
346 ares->response, ares->error);
349 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN
351 This should mean that if the parent is:
352 CN=Users,DC=samba,DC=example,DC=com
353 and a proposed child is
354 cn=Admins ,cn=USERS,dc=Samba,dc=example,dc=COM
356 The resulting DN should be:
358 CN=Admins,CN=Users,DC=samba,DC=example,DC=com
361 static int fix_dn(TALLOC_CTX *mem_ctx,
362 struct ldb_dn *newdn, struct ldb_dn *parent_dn,
363 struct ldb_dn **fixed_dn)
365 char *upper_rdn_attr;
366 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
367 *fixed_dn = ldb_dn_copy(mem_ctx, parent_dn);
369 /* We need the attribute name in upper case */
370 upper_rdn_attr = strupper_talloc(*fixed_dn,
371 ldb_dn_get_rdn_name(newdn));
372 if (!upper_rdn_attr) {
373 return LDB_ERR_OPERATIONS_ERROR;
376 /* Create a new child */
377 if (ldb_dn_add_child_fmt(*fixed_dn, "X=X") == false) {
378 return LDB_ERR_OPERATIONS_ERROR;
381 /* And replace it with CN=foo (we need the attribute in upper case */
382 return ldb_dn_set_component(*fixed_dn, 0, upper_rdn_attr,
383 *ldb_dn_get_rdn_val(newdn));
386 /* Fix all attribute names to be in the correct case, and check they are all valid per the schema */
387 static int fix_attributes(struct ldb_context *ldb, const struct dsdb_schema *schema, struct ldb_message *msg)
390 for (i=0; i < msg->num_elements; i++) {
391 const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema, msg->elements[i].name);
392 /* Add in a very special case for 'clearTextPassword',
393 * which is used for internal processing only, and is
394 * not presented in the schema */
396 if (strcasecmp(msg->elements[i].name, "clearTextPassword") != 0) {
397 ldb_asprintf_errstring(ldb, "attribute %s is not a valid attribute in schema", msg->elements[i].name);
398 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
401 msg->elements[i].name = attribute->lDAPDisplayName;
408 static int objectclass_do_add(struct oc_context *ac);
410 static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
412 struct ldb_context *ldb;
413 struct ldb_request *search_req;
414 struct oc_context *ac;
415 struct ldb_dn *parent_dn;
418 ldb = ldb_module_get_ctx(module);
420 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
422 /* do not manipulate our control entries */
423 if (ldb_dn_is_special(req->op.add.message->dn)) {
424 return ldb_next_request(module, req);
427 /* the objectClass must be specified on add */
428 if (ldb_msg_find_element(req->op.add.message,
429 "objectClass") == NULL) {
430 return LDB_ERR_OBJECT_CLASS_VIOLATION;
433 ac = oc_init_context(module, req);
435 return LDB_ERR_OPERATIONS_ERROR;
438 /* If there isn't a parent, just go on to the add processing */
439 if (ldb_dn_get_comp_num(ac->req->op.add.message->dn) == 1) {
440 return objectclass_do_add(ac);
443 /* get copy of parent DN */
444 parent_dn = ldb_dn_get_parent(ac, ac->req->op.add.message->dn);
445 if (parent_dn == NULL) {
447 return LDB_ERR_OPERATIONS_ERROR;
450 ret = ldb_build_search_req(&search_req, ldb,
451 ac, parent_dn, LDB_SCOPE_BASE,
452 "(objectClass=*)", NULL,
454 ac, get_search_callback,
456 if (ret != LDB_SUCCESS) {
459 talloc_steal(search_req, parent_dn);
461 ac->step_fn = objectclass_do_add;
463 return ldb_next_request(ac->module, search_req);
466 static int objectclass_do_add(struct oc_context *ac)
468 struct ldb_context *ldb;
469 const struct dsdb_schema *schema;
470 struct ldb_request *add_req;
472 struct ldb_message_element *objectclass_element;
473 struct ldb_message *msg;
475 struct class_list *sorted, *current;
478 ldb = ldb_module_get_ctx(ac->module);
479 schema = dsdb_get_schema(ldb);
481 mem_ctx = talloc_new(ac);
482 if (mem_ctx == NULL) {
483 return LDB_ERR_OPERATIONS_ERROR;
486 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
488 /* Check we have a valid parent */
489 if (ac->search_res == NULL) {
490 if (ldb_dn_compare(ldb_get_root_basedn(ldb),
492 /* Allow the tree to be started */
494 /* but don't keep any error string, it's meaningless */
495 ldb_set_errstring(ldb, NULL);
497 ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not exist!",
498 ldb_dn_get_linearized(msg->dn));
499 talloc_free(mem_ctx);
500 return LDB_ERR_UNWILLING_TO_PERFORM;
504 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
506 ac->req->op.add.message->dn,
507 ac->search_res->message->dn,
510 if (ret != LDB_SUCCESS) {
511 ldb_asprintf_errstring(ldb, "Could not munge DN %s into normal form",
512 ldb_dn_get_linearized(ac->req->op.add.message->dn));
513 talloc_free(mem_ctx);
517 /* TODO: Check this is a valid child to this parent,
518 * by reading the allowedChildClasses and
519 * allowedChildClasssesEffective attributes */
524 ret = fix_attributes(ldb, schema, msg);
525 if (ret != LDB_SUCCESS) {
526 talloc_free(mem_ctx);
530 /* This is now the objectClass list from the database */
531 objectclass_element = ldb_msg_find_element(msg, "objectClass");
533 if (!objectclass_element) {
534 /* Where did it go? bail now... */
535 talloc_free(mem_ctx);
536 return LDB_ERR_OPERATIONS_ERROR;
538 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
539 if (ret != LDB_SUCCESS) {
540 talloc_free(mem_ctx);
544 ldb_msg_remove_attr(msg, "objectClass");
545 ret = ldb_msg_add_empty(msg, "objectClass", 0, NULL);
547 if (ret != LDB_SUCCESS) {
548 talloc_free(mem_ctx);
552 /* We must completely replace the existing objectClass entry,
553 * because we need it sorted */
555 /* Move from the linked list back into an ldb msg */
556 for (current = sorted; current; current = current->next) {
557 value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
560 talloc_free(mem_ctx);
561 return LDB_ERR_OPERATIONS_ERROR;
563 ret = ldb_msg_add_string(msg, "objectClass", value);
564 if (ret != LDB_SUCCESS) {
565 ldb_set_errstring(ldb,
566 "objectclass: could not re-add sorted "
567 "objectclass to modify msg");
568 talloc_free(mem_ctx);
571 /* Last one is the critical one */
572 if (!current->next) {
573 struct ldb_message_element *el;
574 int32_t systemFlags = 0;
575 if (!ldb_msg_find_element(msg, "objectCategory")) {
576 value = talloc_strdup(msg, current->objectclass->defaultObjectCategory);
579 talloc_free(mem_ctx);
580 return LDB_ERR_OPERATIONS_ERROR;
582 ldb_msg_add_string(msg, "objectCategory", value);
584 if (!ldb_msg_find_element(msg, "showInAdvancedViewOnly") && (current->objectclass->defaultHidingValue == true)) {
585 ldb_msg_add_string(msg, "showInAdvancedViewOnly",
588 if (!ldb_msg_find_element(msg, "nTSecurityDescriptor")) {
589 DATA_BLOB *sd = get_sd(ac->module, mem_ctx, current->objectclass);
591 ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
595 /* There are very special rules for systemFlags, see MS-ADTS 3.1.1.5.2.4 */
596 el = ldb_msg_find_element(msg, "systemFlags");
598 systemFlags = ldb_msg_find_attr_as_int(msg, "systemFlags", 0);
601 /* Only these flags may be set by a client, but we can't tell between a client and our provision at this point */
602 /* systemFlags &= ( SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_MOVE | SYSTEM_FLAG_CONFIG_LIMITED_MOVE); */
603 ldb_msg_remove_element(msg, el);
606 /* This flag is only allowed on attributeSchema objects */
607 if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "attributeSchema") == 0) {
608 systemFlags &= ~SYSTEM_FLAG_ATTR_IS_RDN;
611 if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "server") == 0) {
612 systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE | SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE);
613 } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "site") == 0
614 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "serverContainer") == 0
615 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "ntDSDSA") == 0) {
616 systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
618 } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLink") == 0
619 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLinkBridge") == 0
620 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "nTDSConnection") == 0) {
621 systemFlags |= (int32_t)(SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
624 /* TODO: If parent object is site or subnet, also add (SYSTEM_FLAG_CONFIG_ALLOW_RENAME) */
626 if (el || systemFlags != 0) {
627 samdb_msg_add_int(ldb, msg, msg, "systemFlags", systemFlags);
633 talloc_free(mem_ctx);
634 ret = ldb_msg_sanity_check(ldb, msg);
637 if (ret != LDB_SUCCESS) {
641 ret = ldb_build_add_req(&add_req, ldb, ac,
646 if (ret != LDB_SUCCESS) {
650 /* perform the add */
651 return ldb_next_request(ac->module, add_req);
654 static int oc_modify_callback(struct ldb_request *req,
655 struct ldb_reply *ares);
656 static int objectclass_do_mod(struct oc_context *ac);
658 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
660 struct ldb_context *ldb = ldb_module_get_ctx(module);
661 struct ldb_message_element *objectclass_element;
662 struct ldb_message *msg;
663 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
664 struct class_list *sorted, *current;
665 struct ldb_request *down_req;
666 struct oc_context *ac;
671 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
673 /* do not manipulate our control entries */
674 if (ldb_dn_is_special(req->op.mod.message->dn)) {
675 return ldb_next_request(module, req);
678 /* Without schema, there isn't much to do here */
680 return ldb_next_request(module, req);
682 objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
684 ac = oc_init_context(module, req);
686 return LDB_ERR_OPERATIONS_ERROR;
689 /* If no part of this touches the objectClass, then we don't
690 * need to make any changes. */
692 /* If the only operation is the deletion of the objectClass
693 * then go on with just fixing the attribute case */
694 if (!objectclass_element) {
695 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
697 return LDB_ERR_OPERATIONS_ERROR;
700 ret = fix_attributes(ldb, schema, msg);
701 if (ret != LDB_SUCCESS) {
705 ret = ldb_build_mod_req(&down_req, ldb, ac,
710 if (ret != LDB_SUCCESS) {
714 /* go on with the call chain */
715 return ldb_next_request(module, down_req);
718 switch (objectclass_element->flags & LDB_FLAG_MOD_MASK) {
719 case LDB_FLAG_MOD_DELETE:
720 if (objectclass_element->num_values == 0) {
721 return LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED;
725 case LDB_FLAG_MOD_REPLACE:
726 mem_ctx = talloc_new(ac);
727 if (mem_ctx == NULL) {
728 return LDB_ERR_OPERATIONS_ERROR;
731 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
733 talloc_free(mem_ctx);
734 return LDB_ERR_OPERATIONS_ERROR;
737 ret = fix_attributes(ldb, schema, msg);
738 if (ret != LDB_SUCCESS) {
739 talloc_free(mem_ctx);
743 ret = objectclass_sort(module, schema, mem_ctx, objectclass_element, &sorted);
744 if (ret != LDB_SUCCESS) {
745 talloc_free(mem_ctx);
749 /* We must completely replace the existing objectClass entry,
750 * because we need it sorted */
752 ldb_msg_remove_attr(msg, "objectClass");
753 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
755 if (ret != LDB_SUCCESS) {
756 talloc_free(mem_ctx);
760 /* Move from the linked list back into an ldb msg */
761 for (current = sorted; current; current = current->next) {
762 /* copy the value as this string is on the schema
763 * context and we can't rely on it not changing
764 * before the operation is over */
765 value = talloc_strdup(msg,
766 current->objectclass->lDAPDisplayName);
769 talloc_free(mem_ctx);
770 return LDB_ERR_OPERATIONS_ERROR;
772 ret = ldb_msg_add_string(msg, "objectClass", value);
773 if (ret != LDB_SUCCESS) {
774 ldb_set_errstring(ldb,
775 "objectclass: could not re-add sorted "
776 "objectclass to modify msg");
777 talloc_free(mem_ctx);
782 talloc_free(mem_ctx);
784 ret = ldb_msg_sanity_check(ldb, msg);
785 if (ret != LDB_SUCCESS) {
789 ret = ldb_build_mod_req(&down_req, ldb, ac,
794 if (ret != LDB_SUCCESS) {
798 /* go on with the call chain */
799 return ldb_next_request(module, down_req);
802 /* This isn't the default branch of the switch, but a 'in any
803 * other case'. When a delete isn't for all objectClasses for
807 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
810 return LDB_ERR_OPERATIONS_ERROR;
813 ret = fix_attributes(ldb, schema, msg);
814 if (ret != LDB_SUCCESS) {
819 ret = ldb_build_mod_req(&down_req, ldb, ac,
822 ac, oc_modify_callback,
824 if (ret != LDB_SUCCESS) {
828 return ldb_next_request(module, down_req);
831 static int oc_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
833 struct ldb_context *ldb;
834 static const char * const attrs[] = { "objectClass", NULL };
835 struct ldb_request *search_req;
836 struct oc_context *ac;
839 ac = talloc_get_type(req->context, struct oc_context);
840 ldb = ldb_module_get_ctx(ac->module);
843 return ldb_module_done(ac->req, NULL, NULL,
844 LDB_ERR_OPERATIONS_ERROR);
846 if (ares->error != LDB_SUCCESS) {
847 return ldb_module_done(ac->req, ares->controls,
848 ares->response, ares->error);
851 if (ares->type != LDB_REPLY_DONE) {
853 return ldb_module_done(ac->req, NULL, NULL,
854 LDB_ERR_OPERATIONS_ERROR);
857 ret = ldb_build_search_req(&search_req, ldb, ac,
858 ac->req->op.mod.message->dn, LDB_SCOPE_BASE,
861 ac, get_search_callback,
863 if (ret != LDB_SUCCESS) {
864 return ldb_module_done(ac->req, NULL, NULL, ret);
867 ac->step_fn = objectclass_do_mod;
869 ret = ldb_next_request(ac->module, search_req);
870 if (ret != LDB_SUCCESS) {
871 return ldb_module_done(ac->req, NULL, NULL, ret);
876 static int objectclass_do_mod(struct oc_context *ac)
878 struct ldb_context *ldb;
879 const struct dsdb_schema *schema;
880 struct ldb_request *mod_req;
882 struct ldb_message_element *objectclass_element;
883 struct ldb_message *msg;
885 struct class_list *sorted, *current;
888 ldb = ldb_module_get_ctx(ac->module);
890 if (ac->search_res == NULL) {
891 return LDB_ERR_OPERATIONS_ERROR;
893 schema = dsdb_get_schema(ldb);
895 mem_ctx = talloc_new(ac);
896 if (mem_ctx == NULL) {
897 return LDB_ERR_OPERATIONS_ERROR;
900 /* use a new message structure */
901 msg = ldb_msg_new(ac);
903 ldb_set_errstring(ldb,
904 "objectclass: could not create new modify msg");
905 talloc_free(mem_ctx);
906 return LDB_ERR_OPERATIONS_ERROR;
909 /* This is now the objectClass list from the database */
910 objectclass_element = ldb_msg_find_element(ac->search_res->message,
912 if (!objectclass_element) {
913 /* Where did it go? bail now... */
914 talloc_free(mem_ctx);
915 return LDB_ERR_OPERATIONS_ERROR;
919 msg->dn = ac->req->op.mod.message->dn;
921 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
922 if (ret != LDB_SUCCESS) {
926 /* We must completely replace the existing objectClass entry.
927 * We could do a constrained add/del, but we are meant to be
928 * in a transaction... */
930 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
931 if (ret != LDB_SUCCESS) {
932 ldb_set_errstring(ldb, "objectclass: could not clear objectclass in modify msg");
933 talloc_free(mem_ctx);
937 /* Move from the linked list back into an ldb msg */
938 for (current = sorted; current; current = current->next) {
939 value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
942 return LDB_ERR_OPERATIONS_ERROR;
944 ret = ldb_msg_add_string(msg, "objectClass", value);
945 if (ret != LDB_SUCCESS) {
946 ldb_set_errstring(ldb, "objectclass: could not re-add sorted objectclass to modify msg");
947 talloc_free(mem_ctx);
952 ret = ldb_msg_sanity_check(ldb, msg);
953 if (ret != LDB_SUCCESS) {
954 talloc_free(mem_ctx);
958 ret = ldb_build_mod_req(&mod_req, ldb, ac,
963 if (ret != LDB_SUCCESS) {
964 talloc_free(mem_ctx);
968 talloc_free(mem_ctx);
969 /* perform the modify */
970 return ldb_next_request(ac->module, mod_req);
973 static int objectclass_do_rename(struct oc_context *ac);
975 static int objectclass_rename(struct ldb_module *module, struct ldb_request *req)
977 static const char * const attrs[] = { NULL };
978 struct ldb_context *ldb;
979 struct ldb_request *search_req;
980 struct oc_context *ac;
981 struct ldb_dn *parent_dn;
984 ldb = ldb_module_get_ctx(module);
986 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_rename\n");
988 if (ldb_dn_is_special(req->op.rename.newdn)) { /* do not manipulate our control entries */
989 return ldb_next_request(module, req);
992 /* Firstly ensure we are not trying to rename it to be a child of itself */
993 if ((ldb_dn_compare_base(req->op.rename.olddn, req->op.rename.newdn) == 0)
994 && (ldb_dn_compare(req->op.rename.olddn, req->op.rename.newdn) != 0)) {
995 ldb_asprintf_errstring(ldb, "Cannot rename %s to be a child of itself",
996 ldb_dn_get_linearized(req->op.rename.olddn));
997 return LDB_ERR_UNWILLING_TO_PERFORM;
1000 ac = oc_init_context(module, req);
1002 return LDB_ERR_OPERATIONS_ERROR;
1005 parent_dn = ldb_dn_get_parent(ac, req->op.rename.newdn);
1006 if (parent_dn == NULL) {
1008 return LDB_ERR_OPERATIONS_ERROR;
1010 ret = ldb_build_search_req(&search_req, ldb,
1011 ac, parent_dn, LDB_SCOPE_BASE,
1014 ac, get_search_callback,
1016 if (ret != LDB_SUCCESS) {
1020 ac->step_fn = objectclass_do_rename;
1022 return ldb_next_request(ac->module, search_req);
1025 static int objectclass_do_rename(struct oc_context *ac)
1027 struct ldb_context *ldb;
1028 struct ldb_request *rename_req;
1029 struct ldb_dn *fixed_dn;
1032 ldb = ldb_module_get_ctx(ac->module);
1034 /* Check we have a valid parent */
1035 if (ac->search_res == NULL) {
1036 ldb_asprintf_errstring(ldb, "objectclass: Cannot rename %s, parent does not exist!",
1037 ldb_dn_get_linearized(ac->req->op.rename.newdn));
1038 return LDB_ERR_UNWILLING_TO_PERFORM;
1041 /* Fix up the DN to be in the standard form,
1042 * taking particular care to match the parent DN */
1044 ac->req->op.rename.newdn,
1045 ac->search_res->message->dn,
1047 if (ret != LDB_SUCCESS) {
1051 /* TODO: Check this is a valid child to this parent,
1052 * by reading the allowedChildClasses and
1053 * allowedChildClasssesEffective attributes */
1055 ret = ldb_build_rename_req(&rename_req, ldb, ac,
1056 ac->req->op.rename.olddn, fixed_dn,
1060 if (ret != LDB_SUCCESS) {
1064 /* perform the rename */
1065 return ldb_next_request(ac->module, rename_req);
1068 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_module_ops = {
1069 .name = "objectclass",
1070 .add = objectclass_add,
1071 .modify = objectclass_modify,
1072 .rename = objectclass_rename,