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_ldb_val(schema, &objectclass_element->values[i]);
138 if (!current->objectclass) {
139 ldb_asprintf_errstring(ldb, "objectclass %.*s is not a valid objectClass in schema",
140 (int)objectclass_element->values[i].length, (const char *)objectclass_element->values[i].data);
141 return LDB_ERR_OBJECT_CLASS_VIOLATION;
144 /* this is the root of the tree. We will start
145 * looking for subclasses from here */
146 if (ldb_attr_cmp("top", current->objectclass->lDAPDisplayName) == 0) {
147 DLIST_ADD_END(parent_class, current, struct class_list *);
149 DLIST_ADD_END(unsorted, current, struct class_list *);
153 if (parent_class == NULL) {
154 current = talloc(mem_ctx, struct class_list);
155 current->objectclass = dsdb_class_by_lDAPDisplayName(schema, "top");
156 DLIST_ADD_END(parent_class, current, struct class_list *);
159 /* For each object: find parent chain */
160 for (current = unsorted; schema && current; current = current->next) {
161 for (poss_parent = unsorted; poss_parent; poss_parent = poss_parent->next) {
162 if (ldb_attr_cmp(poss_parent->objectclass->lDAPDisplayName, current->objectclass->subClassOf) == 0) {
166 /* If we didn't get to the end of the list, we need to add this parent */
167 if (poss_parent || (ldb_attr_cmp("top", current->objectclass->subClassOf) == 0)) {
171 new_parent = talloc(mem_ctx, struct class_list);
172 new_parent->objectclass = dsdb_class_by_lDAPDisplayName(schema, current->objectclass->subClassOf);
173 DLIST_ADD_END(unsorted, new_parent, struct class_list *);
176 /* DEBUGGING aid: how many layers are we down now? */
180 /* Find all the subclasses of classes in the
181 * parent_classes. Push them onto the subclass list */
183 /* Ensure we don't bother if there are no unsorted entries left */
184 for (current = parent_class; schema && unsorted && current; current = current->next) {
185 /* Walk the list of possible subclasses in unsorted */
186 for (poss_subclass = unsorted; poss_subclass; ) {
187 struct class_list *next;
189 /* Save the next pointer, as the DLIST_ macros will change poss_subclass->next */
190 next = poss_subclass->next;
192 if (ldb_attr_cmp(poss_subclass->objectclass->subClassOf, current->objectclass->lDAPDisplayName) == 0) {
193 DLIST_REMOVE(unsorted, poss_subclass);
194 DLIST_ADD(subclass, poss_subclass);
198 poss_subclass = next;
202 /* Now push the parent_classes as sorted, we are done with
203 these. Add to the END of the list by concatenation */
204 DLIST_CONCATENATE(sorted, parent_class, struct class_list *);
206 /* and now find subclasses of these */
207 parent_class = subclass;
210 /* If we didn't find any subclasses we will fall out
212 } while (parent_class);
215 *sorted_out = sorted;
220 /* If we don't have schema yet, then just merge the lists again */
221 DLIST_CONCATENATE(sorted, unsorted, struct class_list *);
222 *sorted_out = sorted;
226 /* This shouldn't happen, and would break MMC, perhaps there
227 * was no 'top', a conflict in the objectClasses or some other
230 ldb_asprintf_errstring(ldb, "objectclass %s is not a valid objectClass in objectClass chain", unsorted->objectclass->lDAPDisplayName);
231 return LDB_ERR_OBJECT_CLASS_VIOLATION;
234 static DATA_BLOB *get_sd(struct ldb_module *module, TALLOC_CTX *mem_ctx,
235 const struct dsdb_class *objectclass)
237 struct ldb_context *ldb = ldb_module_get_ctx(module);
238 enum ndr_err_code ndr_err;
239 DATA_BLOB *linear_sd;
240 struct auth_session_info *session_info
241 = ldb_get_opaque(ldb, "sessionInfo");
242 struct security_descriptor *sd;
243 const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
245 if (!objectclass->defaultSecurityDescriptor || !domain_sid) {
249 sd = sddl_decode(mem_ctx,
250 objectclass->defaultSecurityDescriptor,
253 if (!sd || !session_info || !session_info->security_token) {
257 sd->owner_sid = session_info->security_token->user_sid;
258 sd->group_sid = session_info->security_token->group_sid;
260 linear_sd = talloc(mem_ctx, DATA_BLOB);
265 ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx,
266 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
268 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
269 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
277 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
279 struct ldb_context *ldb;
280 struct oc_context *ac;
283 ac = talloc_get_type(req->context, struct oc_context);
284 ldb = ldb_module_get_ctx(ac->module);
287 return ldb_module_done(ac->req, NULL, NULL,
288 LDB_ERR_OPERATIONS_ERROR);
290 if (ares->error != LDB_SUCCESS &&
291 ares->error != LDB_ERR_NO_SUCH_OBJECT) {
292 return ldb_module_done(ac->req, ares->controls,
293 ares->response, ares->error);
296 switch (ares->type) {
297 case LDB_REPLY_ENTRY:
298 if (ac->search_res != NULL) {
299 ldb_set_errstring(ldb, "Too many results");
301 return ldb_module_done(ac->req, NULL, NULL,
302 LDB_ERR_OPERATIONS_ERROR);
305 ac->search_res = talloc_steal(ac, ares);
308 case LDB_REPLY_REFERRAL:
315 ret = ac->step_fn(ac);
316 if (ret != LDB_SUCCESS) {
317 return ldb_module_done(ac->req, NULL, NULL, ret);
325 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
327 struct oc_context *ac;
329 ac = talloc_get_type(req->context, struct oc_context);
332 return ldb_module_done(ac->req, NULL, NULL,
333 LDB_ERR_OPERATIONS_ERROR);
335 if (ares->error != LDB_SUCCESS) {
336 return ldb_module_done(ac->req, ares->controls,
337 ares->response, ares->error);
340 if (ares->type != LDB_REPLY_DONE) {
342 return ldb_module_done(ac->req, NULL, NULL,
343 LDB_ERR_OPERATIONS_ERROR);
346 return ldb_module_done(ac->req, ares->controls,
347 ares->response, ares->error);
350 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN
352 This should mean that if the parent is:
353 CN=Users,DC=samba,DC=example,DC=com
354 and a proposed child is
355 cn=Admins ,cn=USERS,dc=Samba,dc=example,dc=COM
357 The resulting DN should be:
359 CN=Admins,CN=Users,DC=samba,DC=example,DC=com
362 static int fix_dn(TALLOC_CTX *mem_ctx,
363 struct ldb_dn *newdn, struct ldb_dn *parent_dn,
364 struct ldb_dn **fixed_dn)
366 char *upper_rdn_attr;
367 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
368 *fixed_dn = ldb_dn_copy(mem_ctx, parent_dn);
370 /* We need the attribute name in upper case */
371 upper_rdn_attr = strupper_talloc(*fixed_dn,
372 ldb_dn_get_rdn_name(newdn));
373 if (!upper_rdn_attr) {
374 return LDB_ERR_OPERATIONS_ERROR;
377 /* Create a new child */
378 if (ldb_dn_add_child_fmt(*fixed_dn, "X=X") == false) {
379 return LDB_ERR_OPERATIONS_ERROR;
382 /* And replace it with CN=foo (we need the attribute in upper case */
383 return ldb_dn_set_component(*fixed_dn, 0, upper_rdn_attr,
384 *ldb_dn_get_rdn_val(newdn));
387 /* Fix all attribute names to be in the correct case, and check they are all valid per the schema */
388 static int fix_attributes(struct ldb_context *ldb, const struct dsdb_schema *schema, struct ldb_message *msg)
391 for (i=0; i < msg->num_elements; i++) {
392 const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema, msg->elements[i].name);
393 /* Add in a very special case for 'clearTextPassword',
394 * which is used for internal processing only, and is
395 * not presented in the schema */
397 if (strcasecmp(msg->elements[i].name, "clearTextPassword") != 0) {
398 ldb_asprintf_errstring(ldb, "attribute %s is not a valid attribute in schema", msg->elements[i].name);
399 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
402 msg->elements[i].name = attribute->lDAPDisplayName;
409 static int objectclass_do_add(struct oc_context *ac);
411 static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
413 struct ldb_context *ldb;
414 struct ldb_request *search_req;
415 struct oc_context *ac;
416 struct ldb_dn *parent_dn;
418 static const char * const parent_attrs[] = { "objectGUID", NULL };
420 ldb = ldb_module_get_ctx(module);
422 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
424 /* do not manipulate our control entries */
425 if (ldb_dn_is_special(req->op.add.message->dn)) {
426 return ldb_next_request(module, req);
429 /* the objectClass must be specified on add */
430 if (ldb_msg_find_element(req->op.add.message,
431 "objectClass") == NULL) {
432 return LDB_ERR_OBJECT_CLASS_VIOLATION;
435 ac = oc_init_context(module, req);
437 return LDB_ERR_OPERATIONS_ERROR;
440 /* If there isn't a parent, just go on to the add processing */
441 if (ldb_dn_get_comp_num(ac->req->op.add.message->dn) == 1) {
442 return objectclass_do_add(ac);
445 /* get copy of parent DN */
446 parent_dn = ldb_dn_get_parent(ac, ac->req->op.add.message->dn);
447 if (parent_dn == NULL) {
449 return LDB_ERR_OPERATIONS_ERROR;
452 ret = ldb_build_search_req(&search_req, ldb,
453 ac, parent_dn, LDB_SCOPE_BASE,
454 "(objectClass=*)", parent_attrs,
456 ac, get_search_callback,
458 if (ret != LDB_SUCCESS) {
461 talloc_steal(search_req, parent_dn);
463 ac->step_fn = objectclass_do_add;
465 return ldb_next_request(ac->module, search_req);
468 static int objectclass_do_add(struct oc_context *ac)
470 struct ldb_context *ldb;
471 const struct dsdb_schema *schema;
472 struct ldb_request *add_req;
474 struct ldb_message_element *objectclass_element;
475 struct ldb_message *msg;
477 struct class_list *sorted, *current;
480 ldb = ldb_module_get_ctx(ac->module);
481 schema = dsdb_get_schema(ldb);
483 mem_ctx = talloc_new(ac);
484 if (mem_ctx == NULL) {
485 return LDB_ERR_OPERATIONS_ERROR;
488 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
490 /* Check we have a valid parent */
491 if (ac->search_res == NULL) {
492 if (ldb_dn_compare(ldb_get_root_basedn(ldb),
494 /* Allow the tree to be started */
496 /* but don't keep any error string, it's meaningless */
497 ldb_set_errstring(ldb, NULL);
499 ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not exist!",
500 ldb_dn_get_linearized(msg->dn));
501 talloc_free(mem_ctx);
502 return LDB_ERR_UNWILLING_TO_PERFORM;
505 const struct ldb_val *parent_guid;
507 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
509 ac->req->op.add.message->dn,
510 ac->search_res->message->dn,
513 if (ret != LDB_SUCCESS) {
514 ldb_asprintf_errstring(ldb, "Could not munge DN %s into normal form",
515 ldb_dn_get_linearized(ac->req->op.add.message->dn));
516 talloc_free(mem_ctx);
520 parent_guid = ldb_msg_find_ldb_val(ac->search_res->message, "objectGUID");
521 if (parent_guid == NULL) {
522 ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not have an objectGUID!",
523 ldb_dn_get_linearized(msg->dn));
524 talloc_free(mem_ctx);
525 return LDB_ERR_UNWILLING_TO_PERFORM;
528 /* TODO: Check this is a valid child to this parent,
529 * by reading the allowedChildClasses and
530 * allowedChildClasssesEffective attributes */
531 ret = ldb_msg_add_steal_value(msg, "parentGUID", discard_const(parent_guid));
532 if (ret != LDB_SUCCESS) {
533 ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, failed to add parentGUID",
534 ldb_dn_get_linearized(msg->dn));
535 talloc_free(mem_ctx);
536 return LDB_ERR_UNWILLING_TO_PERFORM;
541 ret = fix_attributes(ldb, schema, msg);
542 if (ret != LDB_SUCCESS) {
543 talloc_free(mem_ctx);
547 /* This is now the objectClass list from the database */
548 objectclass_element = ldb_msg_find_element(msg, "objectClass");
550 if (!objectclass_element) {
551 /* Where did it go? bail now... */
552 talloc_free(mem_ctx);
553 return LDB_ERR_OPERATIONS_ERROR;
555 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
556 if (ret != LDB_SUCCESS) {
557 talloc_free(mem_ctx);
561 ldb_msg_remove_attr(msg, "objectClass");
562 ret = ldb_msg_add_empty(msg, "objectClass", 0, NULL);
564 if (ret != LDB_SUCCESS) {
565 talloc_free(mem_ctx);
569 /* We must completely replace the existing objectClass entry,
570 * because we need it sorted */
572 /* Move from the linked list back into an ldb msg */
573 for (current = sorted; current; current = current->next) {
574 value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
577 talloc_free(mem_ctx);
578 return LDB_ERR_OPERATIONS_ERROR;
580 ret = ldb_msg_add_string(msg, "objectClass", value);
581 if (ret != LDB_SUCCESS) {
582 ldb_set_errstring(ldb,
583 "objectclass: could not re-add sorted "
584 "objectclass to modify msg");
585 talloc_free(mem_ctx);
588 /* Last one is the critical one */
589 if (!current->next) {
590 struct ldb_message_element *el;
591 int32_t systemFlags = 0;
592 if (!ldb_msg_find_element(msg, "objectCategory")) {
593 value = talloc_strdup(msg, current->objectclass->defaultObjectCategory);
596 talloc_free(mem_ctx);
597 return LDB_ERR_OPERATIONS_ERROR;
599 ldb_msg_add_string(msg, "objectCategory", value);
601 if (!ldb_msg_find_element(msg, "showInAdvancedViewOnly") && (current->objectclass->defaultHidingValue == true)) {
602 ldb_msg_add_string(msg, "showInAdvancedViewOnly",
605 if (!ldb_msg_find_element(msg, "nTSecurityDescriptor")) {
606 DATA_BLOB *sd = get_sd(ac->module, mem_ctx, current->objectclass);
608 ldb_msg_add_steal_value(msg, "nTSecurityDescriptor", sd);
612 /* There are very special rules for systemFlags, see MS-ADTS 3.1.1.5.2.4 */
613 el = ldb_msg_find_element(msg, "systemFlags");
615 systemFlags = ldb_msg_find_attr_as_int(msg, "systemFlags", 0);
618 /* Only these flags may be set by a client, but we can't tell between a client and our provision at this point */
619 /* systemFlags &= ( SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_MOVE | SYSTEM_FLAG_CONFIG_LIMITED_MOVE); */
620 ldb_msg_remove_element(msg, el);
623 /* This flag is only allowed on attributeSchema objects */
624 if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "attributeSchema") == 0) {
625 systemFlags &= ~SYSTEM_FLAG_ATTR_IS_RDN;
628 if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "server") == 0) {
629 systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE | SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE);
630 } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "site") == 0
631 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "serverContainer") == 0
632 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "ntDSDSA") == 0) {
633 systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
635 } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLink") == 0
636 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLinkBridge") == 0
637 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "nTDSConnection") == 0) {
638 systemFlags |= (int32_t)(SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
641 /* TODO: If parent object is site or subnet, also add (SYSTEM_FLAG_CONFIG_ALLOW_RENAME) */
643 if (el || systemFlags != 0) {
644 samdb_msg_add_int(ldb, msg, msg, "systemFlags", systemFlags);
650 talloc_free(mem_ctx);
651 ret = ldb_msg_sanity_check(ldb, msg);
654 if (ret != LDB_SUCCESS) {
658 ret = ldb_build_add_req(&add_req, ldb, ac,
663 if (ret != LDB_SUCCESS) {
667 /* perform the add */
668 return ldb_next_request(ac->module, add_req);
671 static int oc_modify_callback(struct ldb_request *req,
672 struct ldb_reply *ares);
673 static int objectclass_do_mod(struct oc_context *ac);
675 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
677 struct ldb_context *ldb = ldb_module_get_ctx(module);
678 struct ldb_message_element *objectclass_element;
679 struct ldb_message *msg;
680 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
681 struct class_list *sorted, *current;
682 struct ldb_request *down_req;
683 struct oc_context *ac;
688 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
690 /* do not manipulate our control entries */
691 if (ldb_dn_is_special(req->op.mod.message->dn)) {
692 return ldb_next_request(module, req);
695 /* Without schema, there isn't much to do here */
697 return ldb_next_request(module, req);
699 objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
701 ac = oc_init_context(module, req);
703 return LDB_ERR_OPERATIONS_ERROR;
706 /* If no part of this touches the objectClass, then we don't
707 * need to make any changes. */
709 /* If the only operation is the deletion of the objectClass
710 * then go on with just fixing the attribute case */
711 if (!objectclass_element) {
712 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
714 return LDB_ERR_OPERATIONS_ERROR;
717 ret = fix_attributes(ldb, schema, msg);
718 if (ret != LDB_SUCCESS) {
722 ret = ldb_build_mod_req(&down_req, ldb, ac,
727 if (ret != LDB_SUCCESS) {
731 /* go on with the call chain */
732 return ldb_next_request(module, down_req);
735 switch (objectclass_element->flags & LDB_FLAG_MOD_MASK) {
736 case LDB_FLAG_MOD_DELETE:
737 if (objectclass_element->num_values == 0) {
738 return LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED;
742 case LDB_FLAG_MOD_REPLACE:
743 mem_ctx = talloc_new(ac);
744 if (mem_ctx == NULL) {
745 return LDB_ERR_OPERATIONS_ERROR;
748 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
750 talloc_free(mem_ctx);
751 return LDB_ERR_OPERATIONS_ERROR;
754 ret = fix_attributes(ldb, schema, msg);
755 if (ret != LDB_SUCCESS) {
756 talloc_free(mem_ctx);
760 ret = objectclass_sort(module, schema, mem_ctx, objectclass_element, &sorted);
761 if (ret != LDB_SUCCESS) {
762 talloc_free(mem_ctx);
766 /* We must completely replace the existing objectClass entry,
767 * because we need it sorted */
769 ldb_msg_remove_attr(msg, "objectClass");
770 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
772 if (ret != LDB_SUCCESS) {
773 talloc_free(mem_ctx);
777 /* Move from the linked list back into an ldb msg */
778 for (current = sorted; current; current = current->next) {
779 /* copy the value as this string is on the schema
780 * context and we can't rely on it not changing
781 * before the operation is over */
782 value = talloc_strdup(msg,
783 current->objectclass->lDAPDisplayName);
786 talloc_free(mem_ctx);
787 return LDB_ERR_OPERATIONS_ERROR;
789 ret = ldb_msg_add_string(msg, "objectClass", value);
790 if (ret != LDB_SUCCESS) {
791 ldb_set_errstring(ldb,
792 "objectclass: could not re-add sorted "
793 "objectclass to modify msg");
794 talloc_free(mem_ctx);
799 talloc_free(mem_ctx);
801 ret = ldb_msg_sanity_check(ldb, msg);
802 if (ret != LDB_SUCCESS) {
806 ret = ldb_build_mod_req(&down_req, ldb, ac,
811 if (ret != LDB_SUCCESS) {
815 /* go on with the call chain */
816 return ldb_next_request(module, down_req);
819 /* This isn't the default branch of the switch, but a 'in any
820 * other case'. When a delete isn't for all objectClasses for
824 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
827 return LDB_ERR_OPERATIONS_ERROR;
830 ret = fix_attributes(ldb, schema, msg);
831 if (ret != LDB_SUCCESS) {
836 ret = ldb_build_mod_req(&down_req, ldb, ac,
839 ac, oc_modify_callback,
841 if (ret != LDB_SUCCESS) {
845 return ldb_next_request(module, down_req);
848 static int oc_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
850 struct ldb_context *ldb;
851 static const char * const attrs[] = { "objectClass", NULL };
852 struct ldb_request *search_req;
853 struct oc_context *ac;
856 ac = talloc_get_type(req->context, struct oc_context);
857 ldb = ldb_module_get_ctx(ac->module);
860 return ldb_module_done(ac->req, NULL, NULL,
861 LDB_ERR_OPERATIONS_ERROR);
863 if (ares->error != LDB_SUCCESS) {
864 return ldb_module_done(ac->req, ares->controls,
865 ares->response, ares->error);
868 if (ares->type != LDB_REPLY_DONE) {
870 return ldb_module_done(ac->req, NULL, NULL,
871 LDB_ERR_OPERATIONS_ERROR);
874 ret = ldb_build_search_req(&search_req, ldb, ac,
875 ac->req->op.mod.message->dn, LDB_SCOPE_BASE,
878 ac, get_search_callback,
880 if (ret != LDB_SUCCESS) {
881 return ldb_module_done(ac->req, NULL, NULL, ret);
884 ac->step_fn = objectclass_do_mod;
886 ret = ldb_next_request(ac->module, search_req);
887 if (ret != LDB_SUCCESS) {
888 return ldb_module_done(ac->req, NULL, NULL, ret);
893 static int objectclass_do_mod(struct oc_context *ac)
895 struct ldb_context *ldb;
896 const struct dsdb_schema *schema;
897 struct ldb_request *mod_req;
899 struct ldb_message_element *objectclass_element;
900 struct ldb_message *msg;
902 struct class_list *sorted, *current;
905 ldb = ldb_module_get_ctx(ac->module);
907 if (ac->search_res == NULL) {
908 return LDB_ERR_OPERATIONS_ERROR;
910 schema = dsdb_get_schema(ldb);
912 mem_ctx = talloc_new(ac);
913 if (mem_ctx == NULL) {
914 return LDB_ERR_OPERATIONS_ERROR;
917 /* use a new message structure */
918 msg = ldb_msg_new(ac);
920 ldb_set_errstring(ldb,
921 "objectclass: could not create new modify msg");
922 talloc_free(mem_ctx);
923 return LDB_ERR_OPERATIONS_ERROR;
926 /* This is now the objectClass list from the database */
927 objectclass_element = ldb_msg_find_element(ac->search_res->message,
929 if (!objectclass_element) {
930 /* Where did it go? bail now... */
931 talloc_free(mem_ctx);
932 return LDB_ERR_OPERATIONS_ERROR;
936 msg->dn = ac->req->op.mod.message->dn;
938 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
939 if (ret != LDB_SUCCESS) {
943 /* We must completely replace the existing objectClass entry.
944 * We could do a constrained add/del, but we are meant to be
945 * in a transaction... */
947 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
948 if (ret != LDB_SUCCESS) {
949 ldb_set_errstring(ldb, "objectclass: could not clear objectclass in modify msg");
950 talloc_free(mem_ctx);
954 /* Move from the linked list back into an ldb msg */
955 for (current = sorted; current; current = current->next) {
956 value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
959 return LDB_ERR_OPERATIONS_ERROR;
961 ret = ldb_msg_add_string(msg, "objectClass", value);
962 if (ret != LDB_SUCCESS) {
963 ldb_set_errstring(ldb, "objectclass: could not re-add sorted objectclass to modify msg");
964 talloc_free(mem_ctx);
969 ret = ldb_msg_sanity_check(ldb, msg);
970 if (ret != LDB_SUCCESS) {
971 talloc_free(mem_ctx);
975 ret = ldb_build_mod_req(&mod_req, ldb, ac,
980 if (ret != LDB_SUCCESS) {
981 talloc_free(mem_ctx);
985 talloc_free(mem_ctx);
986 /* perform the modify */
987 return ldb_next_request(ac->module, mod_req);
990 static int objectclass_do_rename(struct oc_context *ac);
992 static int objectclass_rename(struct ldb_module *module, struct ldb_request *req)
994 static const char * const attrs[] = { "objectGUID", NULL };
995 struct ldb_context *ldb;
996 struct ldb_request *search_req;
997 struct oc_context *ac;
998 struct ldb_dn *parent_dn;
1000 struct ldb_control **ctrl;
1002 ldb = ldb_module_get_ctx(module);
1004 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_rename\n");
1006 if (ldb_dn_is_special(req->op.rename.newdn)) { /* do not manipulate our control entries */
1007 return ldb_next_request(module, req);
1010 /* Firstly ensure we are not trying to rename it to be a child of itself */
1011 if ((ldb_dn_compare_base(req->op.rename.olddn, req->op.rename.newdn) == 0)
1012 && (ldb_dn_compare(req->op.rename.olddn, req->op.rename.newdn) != 0)) {
1013 ldb_asprintf_errstring(ldb, "Cannot rename %s to be a child of itself",
1014 ldb_dn_get_linearized(req->op.rename.olddn));
1015 return LDB_ERR_UNWILLING_TO_PERFORM;
1018 ac = oc_init_context(module, req);
1020 return LDB_ERR_OPERATIONS_ERROR;
1023 parent_dn = ldb_dn_get_parent(ac, req->op.rename.newdn);
1024 if (parent_dn == NULL) {
1026 return LDB_ERR_OPERATIONS_ERROR;
1029 /* we have to add the show deleted control, as otherwise DRS
1030 deletes will be refused as we will think the target parent
1032 ctrl = talloc_array(req, struct ldb_control, 2);
1035 return LDB_ERR_OPERATIONS_ERROR;
1037 ctrl[0] = talloc(ctrl, struct ldb_control);
1040 return LDB_ERR_OPERATIONS_ERROR;
1042 ctrl[0]->oid = LDB_CONTROL_SHOW_DELETED_OID;
1043 ctrl[0]->critical = 0;
1044 ctrl[0]->data = NULL;
1047 /* note that the results of this search are kept and used to
1048 update the parentGUID in objectclass_rename_callback() */
1049 ret = ldb_build_search_req(&search_req, ldb,
1050 ac, parent_dn, LDB_SCOPE_BASE,
1053 ac, get_search_callback,
1055 if (ret != LDB_SUCCESS) {
1059 ac->step_fn = objectclass_do_rename;
1061 return ldb_next_request(ac->module, search_req);
1065 called after the rename happens.
1066 We now need to fix the parentGUID of the object to be the objectGUID of
1069 static int objectclass_rename_callback(struct ldb_request *req, struct ldb_reply *ares)
1071 struct ldb_context *ldb;
1072 struct oc_context *ac;
1073 const struct ldb_val *parent_guid;
1074 struct ldb_request *mod_req = NULL;
1076 struct ldb_message *msg;
1077 struct ldb_message_element *el = NULL;
1079 ac = talloc_get_type(req->context, struct oc_context);
1080 ldb = ldb_module_get_ctx(ac->module);
1082 /* make sure the rename succeeded */
1084 return ldb_module_done(ac->req, NULL, NULL,
1085 LDB_ERR_OPERATIONS_ERROR);
1087 if (ares->error != LDB_SUCCESS) {
1088 return ldb_module_done(ac->req, ares->controls,
1089 ares->response, ares->error);
1093 /* the ac->search_res should contain the new parents objectGUID */
1094 parent_guid = ldb_msg_find_ldb_val(ac->search_res->message, "objectGUID");
1095 if (parent_guid == NULL) {
1096 ldb_asprintf_errstring(ldb, "objectclass: Cannot rename %s, new parent does not have an objectGUID!",
1097 ldb_dn_get_linearized(ac->req->op.rename.newdn));
1098 return LDB_ERR_UNWILLING_TO_PERFORM;
1102 /* construct the modify message */
1103 msg = ldb_msg_new(ac);
1106 return LDB_ERR_OPERATIONS_ERROR;
1109 msg->dn = ac->req->op.rename.newdn;
1111 ret = ldb_msg_add_value(msg, "parentGUID", parent_guid, &el);
1112 if (ret != LDB_SUCCESS) {
1116 el->flags = LDB_FLAG_MOD_REPLACE;
1118 ret = ldb_build_mod_req(&mod_req, ldb, ac, msg,
1119 NULL, ac, oc_op_callback, req);
1121 return ldb_next_request(ac->module, mod_req);
1124 static int objectclass_do_rename(struct oc_context *ac)
1126 struct ldb_context *ldb;
1127 struct ldb_request *rename_req;
1128 struct ldb_dn *fixed_dn;
1131 ldb = ldb_module_get_ctx(ac->module);
1133 /* Check we have a valid parent */
1134 if (ac->search_res == NULL) {
1135 ldb_asprintf_errstring(ldb, "objectclass: Cannot rename %s, parent does not exist!",
1136 ldb_dn_get_linearized(ac->req->op.rename.newdn));
1137 return LDB_ERR_UNWILLING_TO_PERFORM;
1140 /* Fix up the DN to be in the standard form,
1141 * taking particular care to match the parent DN */
1143 ac->req->op.rename.newdn,
1144 ac->search_res->message->dn,
1146 if (ret != LDB_SUCCESS) {
1150 /* TODO: Check this is a valid child to this parent,
1151 * by reading the allowedChildClasses and
1152 * allowedChildClasssesEffective attributes */
1154 ret = ldb_build_rename_req(&rename_req, ldb, ac,
1155 ac->req->op.rename.olddn, fixed_dn,
1157 ac, objectclass_rename_callback,
1159 if (ret != LDB_SUCCESS) {
1163 /* perform the rename */
1164 return ldb_next_request(ac->module, rename_req);
1167 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_module_ops = {
1168 .name = "objectclass",
1169 .add = objectclass_add,
1170 .modify = objectclass_modify,
1171 .rename = objectclass_rename,