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;
417 static const char * const parent_attrs[] = { "objectGUID", NULL };
419 ldb = ldb_module_get_ctx(module);
421 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
423 /* do not manipulate our control entries */
424 if (ldb_dn_is_special(req->op.add.message->dn)) {
425 return ldb_next_request(module, req);
428 /* the objectClass must be specified on add */
429 if (ldb_msg_find_element(req->op.add.message,
430 "objectClass") == NULL) {
431 return LDB_ERR_OBJECT_CLASS_VIOLATION;
434 ac = oc_init_context(module, req);
436 return LDB_ERR_OPERATIONS_ERROR;
439 /* If there isn't a parent, just go on to the add processing */
440 if (ldb_dn_get_comp_num(ac->req->op.add.message->dn) == 1) {
441 return objectclass_do_add(ac);
444 /* get copy of parent DN */
445 parent_dn = ldb_dn_get_parent(ac, ac->req->op.add.message->dn);
446 if (parent_dn == NULL) {
448 return LDB_ERR_OPERATIONS_ERROR;
451 ret = ldb_build_search_req(&search_req, ldb,
452 ac, parent_dn, LDB_SCOPE_BASE,
453 "(objectClass=*)", parent_attrs,
455 ac, get_search_callback,
457 if (ret != LDB_SUCCESS) {
460 talloc_steal(search_req, parent_dn);
462 ac->step_fn = objectclass_do_add;
464 return ldb_next_request(ac->module, search_req);
467 static int objectclass_do_add(struct oc_context *ac)
469 struct ldb_context *ldb;
470 const struct dsdb_schema *schema;
471 struct ldb_request *add_req;
473 struct ldb_message_element *objectclass_element;
474 struct ldb_message *msg;
476 struct class_list *sorted, *current;
479 ldb = ldb_module_get_ctx(ac->module);
480 schema = dsdb_get_schema(ldb);
482 mem_ctx = talloc_new(ac);
483 if (mem_ctx == NULL) {
484 return LDB_ERR_OPERATIONS_ERROR;
487 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
489 /* Check we have a valid parent */
490 if (ac->search_res == NULL) {
491 if (ldb_dn_compare(ldb_get_root_basedn(ldb),
493 /* Allow the tree to be started */
495 /* but don't keep any error string, it's meaningless */
496 ldb_set_errstring(ldb, NULL);
498 ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not exist!",
499 ldb_dn_get_linearized(msg->dn));
500 talloc_free(mem_ctx);
501 return LDB_ERR_UNWILLING_TO_PERFORM;
504 const struct ldb_val *parent_guid;
506 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
508 ac->req->op.add.message->dn,
509 ac->search_res->message->dn,
512 if (ret != LDB_SUCCESS) {
513 ldb_asprintf_errstring(ldb, "Could not munge DN %s into normal form",
514 ldb_dn_get_linearized(ac->req->op.add.message->dn));
515 talloc_free(mem_ctx);
519 parent_guid = ldb_msg_find_ldb_val(ac->search_res->message, "objectGUID");
520 if (parent_guid == NULL) {
521 ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not have an objectGUID!",
522 ldb_dn_get_linearized(msg->dn));
523 talloc_free(mem_ctx);
524 return LDB_ERR_UNWILLING_TO_PERFORM;
527 /* TODO: Check this is a valid child to this parent,
528 * by reading the allowedChildClasses and
529 * allowedChildClasssesEffective attributes */
530 ret = ldb_msg_add_steal_value(msg, "parentGUID", discard_const(parent_guid));
531 if (ret != LDB_SUCCESS) {
532 ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, failed to add parentGUID",
533 ldb_dn_get_linearized(msg->dn));
534 talloc_free(mem_ctx);
535 return LDB_ERR_UNWILLING_TO_PERFORM;
540 ret = fix_attributes(ldb, schema, msg);
541 if (ret != LDB_SUCCESS) {
542 talloc_free(mem_ctx);
546 /* This is now the objectClass list from the database */
547 objectclass_element = ldb_msg_find_element(msg, "objectClass");
549 if (!objectclass_element) {
550 /* Where did it go? bail now... */
551 talloc_free(mem_ctx);
552 return LDB_ERR_OPERATIONS_ERROR;
554 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
555 if (ret != LDB_SUCCESS) {
556 talloc_free(mem_ctx);
560 ldb_msg_remove_attr(msg, "objectClass");
561 ret = ldb_msg_add_empty(msg, "objectClass", 0, NULL);
563 if (ret != LDB_SUCCESS) {
564 talloc_free(mem_ctx);
568 /* We must completely replace the existing objectClass entry,
569 * because we need it sorted */
571 /* Move from the linked list back into an ldb msg */
572 for (current = sorted; current; current = current->next) {
573 value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
576 talloc_free(mem_ctx);
577 return LDB_ERR_OPERATIONS_ERROR;
579 ret = ldb_msg_add_string(msg, "objectClass", value);
580 if (ret != LDB_SUCCESS) {
581 ldb_set_errstring(ldb,
582 "objectclass: could not re-add sorted "
583 "objectclass to modify msg");
584 talloc_free(mem_ctx);
587 /* Last one is the critical one */
588 if (!current->next) {
589 struct ldb_message_element *el;
590 int32_t systemFlags = 0;
591 if (!ldb_msg_find_element(msg, "objectCategory")) {
592 value = talloc_strdup(msg, current->objectclass->defaultObjectCategory);
595 talloc_free(mem_ctx);
596 return LDB_ERR_OPERATIONS_ERROR;
598 ldb_msg_add_string(msg, "objectCategory", value);
600 if (!ldb_msg_find_element(msg, "showInAdvancedViewOnly") && (current->objectclass->defaultHidingValue == true)) {
601 ldb_msg_add_string(msg, "showInAdvancedViewOnly",
604 if (!ldb_msg_find_element(msg, "nTSecurityDescriptor")) {
605 DATA_BLOB *sd = get_sd(ac->module, mem_ctx, current->objectclass);
607 ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
611 /* There are very special rules for systemFlags, see MS-ADTS 3.1.1.5.2.4 */
612 el = ldb_msg_find_element(msg, "systemFlags");
614 systemFlags = ldb_msg_find_attr_as_int(msg, "systemFlags", 0);
617 /* Only these flags may be set by a client, but we can't tell between a client and our provision at this point */
618 /* systemFlags &= ( SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_MOVE | SYSTEM_FLAG_CONFIG_LIMITED_MOVE); */
619 ldb_msg_remove_element(msg, el);
622 /* This flag is only allowed on attributeSchema objects */
623 if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "attributeSchema") == 0) {
624 systemFlags &= ~SYSTEM_FLAG_ATTR_IS_RDN;
627 if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "server") == 0) {
628 systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE | SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE);
629 } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "site") == 0
630 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "serverContainer") == 0
631 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "ntDSDSA") == 0) {
632 systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
634 } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLink") == 0
635 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLinkBridge") == 0
636 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "nTDSConnection") == 0) {
637 systemFlags |= (int32_t)(SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
640 /* TODO: If parent object is site or subnet, also add (SYSTEM_FLAG_CONFIG_ALLOW_RENAME) */
642 if (el || systemFlags != 0) {
643 samdb_msg_add_int(ldb, msg, msg, "systemFlags", systemFlags);
649 talloc_free(mem_ctx);
650 ret = ldb_msg_sanity_check(ldb, msg);
653 if (ret != LDB_SUCCESS) {
657 ret = ldb_build_add_req(&add_req, ldb, ac,
662 if (ret != LDB_SUCCESS) {
666 /* perform the add */
667 return ldb_next_request(ac->module, add_req);
670 static int oc_modify_callback(struct ldb_request *req,
671 struct ldb_reply *ares);
672 static int objectclass_do_mod(struct oc_context *ac);
674 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
676 struct ldb_context *ldb = ldb_module_get_ctx(module);
677 struct ldb_message_element *objectclass_element;
678 struct ldb_message *msg;
679 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
680 struct class_list *sorted, *current;
681 struct ldb_request *down_req;
682 struct oc_context *ac;
687 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
689 /* do not manipulate our control entries */
690 if (ldb_dn_is_special(req->op.mod.message->dn)) {
691 return ldb_next_request(module, req);
694 /* Without schema, there isn't much to do here */
696 return ldb_next_request(module, req);
698 objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
700 ac = oc_init_context(module, req);
702 return LDB_ERR_OPERATIONS_ERROR;
705 /* If no part of this touches the objectClass, then we don't
706 * need to make any changes. */
708 /* If the only operation is the deletion of the objectClass
709 * then go on with just fixing the attribute case */
710 if (!objectclass_element) {
711 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
713 return LDB_ERR_OPERATIONS_ERROR;
716 ret = fix_attributes(ldb, schema, msg);
717 if (ret != LDB_SUCCESS) {
721 ret = ldb_build_mod_req(&down_req, ldb, ac,
726 if (ret != LDB_SUCCESS) {
730 /* go on with the call chain */
731 return ldb_next_request(module, down_req);
734 switch (objectclass_element->flags & LDB_FLAG_MOD_MASK) {
735 case LDB_FLAG_MOD_DELETE:
736 if (objectclass_element->num_values == 0) {
737 return LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED;
741 case LDB_FLAG_MOD_REPLACE:
742 mem_ctx = talloc_new(ac);
743 if (mem_ctx == NULL) {
744 return LDB_ERR_OPERATIONS_ERROR;
747 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
749 talloc_free(mem_ctx);
750 return LDB_ERR_OPERATIONS_ERROR;
753 ret = fix_attributes(ldb, schema, msg);
754 if (ret != LDB_SUCCESS) {
755 talloc_free(mem_ctx);
759 ret = objectclass_sort(module, schema, mem_ctx, objectclass_element, &sorted);
760 if (ret != LDB_SUCCESS) {
761 talloc_free(mem_ctx);
765 /* We must completely replace the existing objectClass entry,
766 * because we need it sorted */
768 ldb_msg_remove_attr(msg, "objectClass");
769 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
771 if (ret != LDB_SUCCESS) {
772 talloc_free(mem_ctx);
776 /* Move from the linked list back into an ldb msg */
777 for (current = sorted; current; current = current->next) {
778 /* copy the value as this string is on the schema
779 * context and we can't rely on it not changing
780 * before the operation is over */
781 value = talloc_strdup(msg,
782 current->objectclass->lDAPDisplayName);
785 talloc_free(mem_ctx);
786 return LDB_ERR_OPERATIONS_ERROR;
788 ret = ldb_msg_add_string(msg, "objectClass", value);
789 if (ret != LDB_SUCCESS) {
790 ldb_set_errstring(ldb,
791 "objectclass: could not re-add sorted "
792 "objectclass to modify msg");
793 talloc_free(mem_ctx);
798 talloc_free(mem_ctx);
800 ret = ldb_msg_sanity_check(ldb, msg);
801 if (ret != LDB_SUCCESS) {
805 ret = ldb_build_mod_req(&down_req, ldb, ac,
810 if (ret != LDB_SUCCESS) {
814 /* go on with the call chain */
815 return ldb_next_request(module, down_req);
818 /* This isn't the default branch of the switch, but a 'in any
819 * other case'. When a delete isn't for all objectClasses for
823 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
826 return LDB_ERR_OPERATIONS_ERROR;
829 ret = fix_attributes(ldb, schema, msg);
830 if (ret != LDB_SUCCESS) {
835 ret = ldb_build_mod_req(&down_req, ldb, ac,
838 ac, oc_modify_callback,
840 if (ret != LDB_SUCCESS) {
844 return ldb_next_request(module, down_req);
847 static int oc_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
849 struct ldb_context *ldb;
850 static const char * const attrs[] = { "objectClass", NULL };
851 struct ldb_request *search_req;
852 struct oc_context *ac;
855 ac = talloc_get_type(req->context, struct oc_context);
856 ldb = ldb_module_get_ctx(ac->module);
859 return ldb_module_done(ac->req, NULL, NULL,
860 LDB_ERR_OPERATIONS_ERROR);
862 if (ares->error != LDB_SUCCESS) {
863 return ldb_module_done(ac->req, ares->controls,
864 ares->response, ares->error);
867 if (ares->type != LDB_REPLY_DONE) {
869 return ldb_module_done(ac->req, NULL, NULL,
870 LDB_ERR_OPERATIONS_ERROR);
873 ret = ldb_build_search_req(&search_req, ldb, ac,
874 ac->req->op.mod.message->dn, LDB_SCOPE_BASE,
877 ac, get_search_callback,
879 if (ret != LDB_SUCCESS) {
880 return ldb_module_done(ac->req, NULL, NULL, ret);
883 ac->step_fn = objectclass_do_mod;
885 ret = ldb_next_request(ac->module, search_req);
886 if (ret != LDB_SUCCESS) {
887 return ldb_module_done(ac->req, NULL, NULL, ret);
892 static int objectclass_do_mod(struct oc_context *ac)
894 struct ldb_context *ldb;
895 const struct dsdb_schema *schema;
896 struct ldb_request *mod_req;
898 struct ldb_message_element *objectclass_element;
899 struct ldb_message *msg;
901 struct class_list *sorted, *current;
904 ldb = ldb_module_get_ctx(ac->module);
906 if (ac->search_res == NULL) {
907 return LDB_ERR_OPERATIONS_ERROR;
909 schema = dsdb_get_schema(ldb);
911 mem_ctx = talloc_new(ac);
912 if (mem_ctx == NULL) {
913 return LDB_ERR_OPERATIONS_ERROR;
916 /* use a new message structure */
917 msg = ldb_msg_new(ac);
919 ldb_set_errstring(ldb,
920 "objectclass: could not create new modify msg");
921 talloc_free(mem_ctx);
922 return LDB_ERR_OPERATIONS_ERROR;
925 /* This is now the objectClass list from the database */
926 objectclass_element = ldb_msg_find_element(ac->search_res->message,
928 if (!objectclass_element) {
929 /* Where did it go? bail now... */
930 talloc_free(mem_ctx);
931 return LDB_ERR_OPERATIONS_ERROR;
935 msg->dn = ac->req->op.mod.message->dn;
937 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
938 if (ret != LDB_SUCCESS) {
942 /* We must completely replace the existing objectClass entry.
943 * We could do a constrained add/del, but we are meant to be
944 * in a transaction... */
946 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
947 if (ret != LDB_SUCCESS) {
948 ldb_set_errstring(ldb, "objectclass: could not clear objectclass in modify msg");
949 talloc_free(mem_ctx);
953 /* Move from the linked list back into an ldb msg */
954 for (current = sorted; current; current = current->next) {
955 value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
958 return LDB_ERR_OPERATIONS_ERROR;
960 ret = ldb_msg_add_string(msg, "objectClass", value);
961 if (ret != LDB_SUCCESS) {
962 ldb_set_errstring(ldb, "objectclass: could not re-add sorted objectclass to modify msg");
963 talloc_free(mem_ctx);
968 ret = ldb_msg_sanity_check(ldb, msg);
969 if (ret != LDB_SUCCESS) {
970 talloc_free(mem_ctx);
974 ret = ldb_build_mod_req(&mod_req, ldb, ac,
979 if (ret != LDB_SUCCESS) {
980 talloc_free(mem_ctx);
984 talloc_free(mem_ctx);
985 /* perform the modify */
986 return ldb_next_request(ac->module, mod_req);
989 static int objectclass_do_rename(struct oc_context *ac);
991 static int objectclass_rename(struct ldb_module *module, struct ldb_request *req)
993 static const char * const attrs[] = { "objectGUID", NULL };
994 struct ldb_context *ldb;
995 struct ldb_request *search_req;
996 struct oc_context *ac;
997 struct ldb_dn *parent_dn;
1000 ldb = ldb_module_get_ctx(module);
1002 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_rename\n");
1004 if (ldb_dn_is_special(req->op.rename.newdn)) { /* do not manipulate our control entries */
1005 return ldb_next_request(module, req);
1008 /* Firstly ensure we are not trying to rename it to be a child of itself */
1009 if ((ldb_dn_compare_base(req->op.rename.olddn, req->op.rename.newdn) == 0)
1010 && (ldb_dn_compare(req->op.rename.olddn, req->op.rename.newdn) != 0)) {
1011 ldb_asprintf_errstring(ldb, "Cannot rename %s to be a child of itself",
1012 ldb_dn_get_linearized(req->op.rename.olddn));
1013 return LDB_ERR_UNWILLING_TO_PERFORM;
1016 ac = oc_init_context(module, req);
1018 return LDB_ERR_OPERATIONS_ERROR;
1021 parent_dn = ldb_dn_get_parent(ac, req->op.rename.newdn);
1022 if (parent_dn == NULL) {
1024 return LDB_ERR_OPERATIONS_ERROR;
1027 /* note that the results of this search are kept and used to
1028 update the parentGUID in objectclass_rename_callback() */
1029 ret = ldb_build_search_req(&search_req, ldb,
1030 ac, parent_dn, LDB_SCOPE_BASE,
1033 ac, get_search_callback,
1035 if (ret != LDB_SUCCESS) {
1039 ac->step_fn = objectclass_do_rename;
1041 return ldb_next_request(ac->module, search_req);
1045 called after the rename happens.
1046 We now need to fix the parentGUID of the object to be the objectGUID of
1049 static int objectclass_rename_callback(struct ldb_request *req, struct ldb_reply *ares)
1051 struct ldb_context *ldb;
1052 struct oc_context *ac;
1053 const struct ldb_val *parent_guid;
1054 struct ldb_request *mod_req = NULL;
1056 struct ldb_message *msg;
1057 struct ldb_message_element *el = NULL;
1059 ac = talloc_get_type(req->context, struct oc_context);
1060 ldb = ldb_module_get_ctx(ac->module);
1062 /* make sure the rename succeeded */
1064 return ldb_module_done(ac->req, NULL, NULL,
1065 LDB_ERR_OPERATIONS_ERROR);
1067 if (ares->error != LDB_SUCCESS) {
1068 return ldb_module_done(ac->req, ares->controls,
1069 ares->response, ares->error);
1073 /* the ac->search_res should contain the new parents objectGUID */
1074 parent_guid = ldb_msg_find_ldb_val(ac->search_res->message, "objectGUID");
1075 if (parent_guid == NULL) {
1076 ldb_asprintf_errstring(ldb, "objectclass: Cannot rename %s, new parent does not have an objectGUID!",
1077 ldb_dn_get_linearized(ac->req->op.rename.newdn));
1078 return LDB_ERR_UNWILLING_TO_PERFORM;
1082 /* construct the modify message */
1083 msg = ldb_msg_new(ac);
1086 return LDB_ERR_OPERATIONS_ERROR;
1089 msg->dn = ac->req->op.rename.newdn;
1091 ret = ldb_msg_add_value(msg, "parentGUID", parent_guid, &el);
1092 if (ret != LDB_SUCCESS) {
1096 el->flags = LDB_FLAG_MOD_REPLACE;
1098 ret = ldb_build_mod_req(&mod_req, ldb, ac, msg,
1099 NULL, ac, oc_op_callback, req);
1101 return ldb_next_request(ac->module, mod_req);
1104 static int objectclass_do_rename(struct oc_context *ac)
1106 struct ldb_context *ldb;
1107 struct ldb_request *rename_req;
1108 struct ldb_dn *fixed_dn;
1111 ldb = ldb_module_get_ctx(ac->module);
1113 /* Check we have a valid parent */
1114 if (ac->search_res == NULL) {
1115 ldb_asprintf_errstring(ldb, "objectclass: Cannot rename %s, parent does not exist!",
1116 ldb_dn_get_linearized(ac->req->op.rename.newdn));
1117 return LDB_ERR_UNWILLING_TO_PERFORM;
1120 /* Fix up the DN to be in the standard form,
1121 * taking particular care to match the parent DN */
1123 ac->req->op.rename.newdn,
1124 ac->search_res->message->dn,
1126 if (ret != LDB_SUCCESS) {
1130 /* TODO: Check this is a valid child to this parent,
1131 * by reading the allowedChildClasses and
1132 * allowedChildClasssesEffective attributes */
1134 ret = ldb_build_rename_req(&rename_req, ldb, ac,
1135 ac->req->op.rename.olddn, fixed_dn,
1137 ac, objectclass_rename_callback,
1139 if (ret != LDB_SUCCESS) {
1143 /* perform the rename */
1144 return ldb_next_request(ac->module, rename_req);
1147 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_module_ops = {
1148 .name = "objectclass",
1149 .add = objectclass_add,
1150 .modify = objectclass_modify,
1151 .rename = objectclass_rename,