Owner and group defaulting.
[Samba/ekacnet.git] / source4 / dsdb / samdb / ldb_modules / objectclass.c
blob0246de130c8dae87b7729186de7e53baa3560bb9
1 /*
2 ldb database library
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/>.
22 * Name: ldb
24 * Component: objectClass sorting module
26 * Description:
27 * - sort the objectClass attribute into the class
28 * hierarchy,
29 * - fix DNs and attributes into 'standard' case
30 * - Add objectCategory and ntSecurityDescriptor defaults
32 * Author: Andrew Bartlett
36 #include "includes.h"
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"
46 struct oc_context {
48 struct ldb_module *module;
49 struct ldb_request *req;
51 struct ldb_reply *search_res;
53 int (*step_fn)(struct oc_context *);
56 struct class_list {
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);
70 if (ac == NULL) {
71 ldb_set_errstring(ldb, "Out of Memory");
72 return NULL;
75 ac->module = module;
76 ac->req = req;
78 return ac;
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,
89 TALLOC_CTX *mem_ctx,
90 struct ldb_message_element *objectclass_element,
91 struct class_list **sorted_out)
93 struct ldb_context *ldb;
94 int i;
95 int layer;
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);
101 /* DESIGN:
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
122 * parent_class.
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);
133 if (!current) {
134 ldb_oom(ldb);
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 *);
148 } else {
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) {
163 break;
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)) {
168 continue;
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? */
177 layer = 0;
178 do {
179 layer++;
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);
196 break;
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;
208 subclass = NULL;
210 /* If we didn't find any subclasses we will fall out
211 * the bottom here */
212 } while (parent_class);
214 if (!unsorted) {
215 *sorted_out = sorted;
216 return LDB_SUCCESS;
219 if (!schema) {
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;
223 return LDB_SUCCESS;
226 /* This shouldn't happen, and would break MMC, perhaps there
227 * was no 'top', a conflict in the objectClasses or some other
228 * schema error?
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 int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
236 struct ldb_context *ldb;
237 struct oc_context *ac;
238 int ret;
240 ac = talloc_get_type(req->context, struct oc_context);
241 ldb = ldb_module_get_ctx(ac->module);
243 if (!ares) {
244 return ldb_module_done(ac->req, NULL, NULL,
245 LDB_ERR_OPERATIONS_ERROR);
247 if (ares->error != LDB_SUCCESS &&
248 ares->error != LDB_ERR_NO_SUCH_OBJECT) {
249 return ldb_module_done(ac->req, ares->controls,
250 ares->response, ares->error);
253 switch (ares->type) {
254 case LDB_REPLY_ENTRY:
255 if (ac->search_res != NULL) {
256 ldb_set_errstring(ldb, "Too many results");
257 talloc_free(ares);
258 return ldb_module_done(ac->req, NULL, NULL,
259 LDB_ERR_OPERATIONS_ERROR);
262 ac->search_res = talloc_steal(ac, ares);
263 break;
265 case LDB_REPLY_REFERRAL:
266 /* ignore */
267 talloc_free(ares);
268 break;
270 case LDB_REPLY_DONE:
271 talloc_free(ares);
272 ret = ac->step_fn(ac);
273 if (ret != LDB_SUCCESS) {
274 return ldb_module_done(ac->req, NULL, NULL, ret);
276 break;
279 return LDB_SUCCESS;
282 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
284 struct oc_context *ac;
286 ac = talloc_get_type(req->context, struct oc_context);
288 if (!ares) {
289 return ldb_module_done(ac->req, NULL, NULL,
290 LDB_ERR_OPERATIONS_ERROR);
292 if (ares->error != LDB_SUCCESS) {
293 return ldb_module_done(ac->req, ares->controls,
294 ares->response, ares->error);
297 if (ares->type != LDB_REPLY_DONE) {
298 talloc_free(ares);
299 return ldb_module_done(ac->req, NULL, NULL,
300 LDB_ERR_OPERATIONS_ERROR);
303 return ldb_module_done(ac->req, ares->controls,
304 ares->response, ares->error);
307 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN
309 This should mean that if the parent is:
310 CN=Users,DC=samba,DC=example,DC=com
311 and a proposed child is
312 cn=Admins ,cn=USERS,dc=Samba,dc=example,dc=COM
314 The resulting DN should be:
316 CN=Admins,CN=Users,DC=samba,DC=example,DC=com
319 static int fix_dn(TALLOC_CTX *mem_ctx,
320 struct ldb_dn *newdn, struct ldb_dn *parent_dn,
321 struct ldb_dn **fixed_dn)
323 char *upper_rdn_attr;
324 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
325 *fixed_dn = ldb_dn_copy(mem_ctx, parent_dn);
327 /* We need the attribute name in upper case */
328 upper_rdn_attr = strupper_talloc(*fixed_dn,
329 ldb_dn_get_rdn_name(newdn));
330 if (!upper_rdn_attr) {
331 return LDB_ERR_OPERATIONS_ERROR;
334 /* Create a new child */
335 if (ldb_dn_add_child_fmt(*fixed_dn, "X=X") == false) {
336 return LDB_ERR_OPERATIONS_ERROR;
339 /* And replace it with CN=foo (we need the attribute in upper case */
340 return ldb_dn_set_component(*fixed_dn, 0, upper_rdn_attr,
341 *ldb_dn_get_rdn_val(newdn));
344 /* Fix all attribute names to be in the correct case, and check they are all valid per the schema */
345 static int fix_attributes(struct ldb_context *ldb, const struct dsdb_schema *schema, struct ldb_message *msg)
347 int i;
348 for (i=0; i < msg->num_elements; i++) {
349 const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema, msg->elements[i].name);
350 /* Add in a very special case for 'clearTextPassword',
351 * which is used for internal processing only, and is
352 * not presented in the schema */
353 if (!attribute) {
354 if (strcasecmp(msg->elements[i].name, "clearTextPassword") != 0) {
355 ldb_asprintf_errstring(ldb, "attribute %s is not a valid attribute in schema", msg->elements[i].name);
356 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
358 } else {
359 msg->elements[i].name = attribute->lDAPDisplayName;
363 return LDB_SUCCESS;
366 static int objectclass_do_add(struct oc_context *ac);
368 static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
370 struct ldb_context *ldb;
371 struct ldb_request *search_req;
372 struct oc_context *ac;
373 struct ldb_dn *parent_dn;
374 int ret;
375 static const char * const parent_attrs[] = { "objectGUID", NULL };
377 ldb = ldb_module_get_ctx(module);
379 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
381 /* do not manipulate our control entries */
382 if (ldb_dn_is_special(req->op.add.message->dn)) {
383 return ldb_next_request(module, req);
386 /* the objectClass must be specified on add */
387 if (ldb_msg_find_element(req->op.add.message,
388 "objectClass") == NULL) {
389 return LDB_ERR_OBJECT_CLASS_VIOLATION;
392 ac = oc_init_context(module, req);
393 if (ac == NULL) {
394 return LDB_ERR_OPERATIONS_ERROR;
397 /* If there isn't a parent, just go on to the add processing */
398 if (ldb_dn_get_comp_num(ac->req->op.add.message->dn) == 1) {
399 return objectclass_do_add(ac);
402 /* get copy of parent DN */
403 parent_dn = ldb_dn_get_parent(ac, ac->req->op.add.message->dn);
404 if (parent_dn == NULL) {
405 ldb_oom(ldb);
406 return LDB_ERR_OPERATIONS_ERROR;
409 ret = ldb_build_search_req(&search_req, ldb,
410 ac, parent_dn, LDB_SCOPE_BASE,
411 "(objectClass=*)", parent_attrs,
412 NULL,
413 ac, get_search_callback,
414 req);
415 if (ret != LDB_SUCCESS) {
416 return ret;
418 talloc_steal(search_req, parent_dn);
420 ac->step_fn = objectclass_do_add;
422 return ldb_next_request(ac->module, search_req);
425 static int objectclass_do_add(struct oc_context *ac)
427 struct ldb_context *ldb;
428 const struct dsdb_schema *schema;
429 struct ldb_request *add_req;
430 char *value;
431 struct ldb_message_element *objectclass_element;
432 struct ldb_message *msg;
433 TALLOC_CTX *mem_ctx;
434 struct class_list *sorted, *current;
435 int ret;
437 ldb = ldb_module_get_ctx(ac->module);
438 schema = dsdb_get_schema(ldb);
440 mem_ctx = talloc_new(ac);
441 if (mem_ctx == NULL) {
442 return LDB_ERR_OPERATIONS_ERROR;
445 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
447 /* Check we have a valid parent */
448 if (ac->search_res == NULL) {
449 if (ldb_dn_compare(ldb_get_root_basedn(ldb),
450 msg->dn) == 0) {
451 /* Allow the tree to be started */
453 /* but don't keep any error string, it's meaningless */
454 ldb_set_errstring(ldb, NULL);
455 } else {
456 ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not exist!",
457 ldb_dn_get_linearized(msg->dn));
458 talloc_free(mem_ctx);
459 return LDB_ERR_UNWILLING_TO_PERFORM;
461 } else {
462 const struct ldb_val *parent_guid;
464 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN */
465 ret = fix_dn(msg,
466 ac->req->op.add.message->dn,
467 ac->search_res->message->dn,
468 &msg->dn);
470 if (ret != LDB_SUCCESS) {
471 ldb_asprintf_errstring(ldb, "Could not munge DN %s into normal form",
472 ldb_dn_get_linearized(ac->req->op.add.message->dn));
473 talloc_free(mem_ctx);
474 return ret;
477 parent_guid = ldb_msg_find_ldb_val(ac->search_res->message, "objectGUID");
478 if (parent_guid == NULL) {
479 ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, parent does not have an objectGUID!",
480 ldb_dn_get_linearized(msg->dn));
481 talloc_free(mem_ctx);
482 return LDB_ERR_UNWILLING_TO_PERFORM;
485 /* TODO: Check this is a valid child to this parent,
486 * by reading the allowedChildClasses and
487 * allowedChildClasssesEffective attributes */
488 ret = ldb_msg_add_steal_value(msg, "parentGUID", discard_const(parent_guid));
489 if (ret != LDB_SUCCESS) {
490 ldb_asprintf_errstring(ldb, "objectclass: Cannot add %s, failed to add parentGUID",
491 ldb_dn_get_linearized(msg->dn));
492 talloc_free(mem_ctx);
493 return LDB_ERR_UNWILLING_TO_PERFORM;
496 if (schema) {
497 ret = fix_attributes(ldb, schema, msg);
498 if (ret != LDB_SUCCESS) {
499 talloc_free(mem_ctx);
500 return ret;
503 /* This is now the objectClass list from the database */
504 objectclass_element = ldb_msg_find_element(msg, "objectClass");
506 if (!objectclass_element) {
507 /* Where did it go? bail now... */
508 talloc_free(mem_ctx);
509 return LDB_ERR_OPERATIONS_ERROR;
511 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
512 if (ret != LDB_SUCCESS) {
513 talloc_free(mem_ctx);
514 return ret;
517 ldb_msg_remove_attr(msg, "objectClass");
518 ret = ldb_msg_add_empty(msg, "objectClass", 0, NULL);
520 if (ret != LDB_SUCCESS) {
521 talloc_free(mem_ctx);
522 return ret;
525 /* We must completely replace the existing objectClass entry,
526 * because we need it sorted */
528 /* Move from the linked list back into an ldb msg */
529 for (current = sorted; current; current = current->next) {
530 value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
531 if (value == NULL) {
532 ldb_oom(ldb);
533 talloc_free(mem_ctx);
534 return LDB_ERR_OPERATIONS_ERROR;
536 ret = ldb_msg_add_string(msg, "objectClass", value);
537 if (ret != LDB_SUCCESS) {
538 ldb_set_errstring(ldb,
539 "objectclass: could not re-add sorted "
540 "objectclass to modify msg");
541 talloc_free(mem_ctx);
542 return ret;
544 /* Last one is the critical one */
545 if (!current->next) {
546 struct ldb_message_element *el;
547 int32_t systemFlags = 0;
548 DATA_BLOB *sd;
549 if (!ldb_msg_find_element(msg, "objectCategory")) {
550 value = talloc_strdup(msg, current->objectclass->defaultObjectCategory);
551 if (value == NULL) {
552 ldb_oom(ldb);
553 talloc_free(mem_ctx);
554 return LDB_ERR_OPERATIONS_ERROR;
556 ldb_msg_add_string(msg, "objectCategory", value);
558 if (!ldb_msg_find_element(msg, "showInAdvancedViewOnly") && (current->objectclass->defaultHidingValue == true)) {
559 ldb_msg_add_string(msg, "showInAdvancedViewOnly",
560 "TRUE");
563 /* There are very special rules for systemFlags, see MS-ADTS 3.1.1.5.2.4 */
564 el = ldb_msg_find_element(msg, "systemFlags");
566 systemFlags = ldb_msg_find_attr_as_int(msg, "systemFlags", 0);
568 if (el) {
569 /* Only these flags may be set by a client, but we can't tell between a client and our provision at this point */
570 /* systemFlags &= ( SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_MOVE | SYSTEM_FLAG_CONFIG_LIMITED_MOVE); */
571 ldb_msg_remove_element(msg, el);
574 /* This flag is only allowed on attributeSchema objects */
575 if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "attributeSchema") == 0) {
576 systemFlags &= ~SYSTEM_FLAG_ATTR_IS_RDN;
579 if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "server") == 0) {
580 systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE | SYSTEM_FLAG_CONFIG_ALLOW_RENAME | SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE);
581 } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "site") == 0
582 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "serverContainer") == 0
583 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "ntDSDSA") == 0) {
584 systemFlags |= (int32_t)(SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE);
586 } else if (ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLink") == 0
587 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "siteLinkBridge") == 0
588 || ldb_attr_cmp(current->objectclass->lDAPDisplayName, "nTDSConnection") == 0) {
589 systemFlags |= (int32_t)(SYSTEM_FLAG_CONFIG_ALLOW_RENAME);
592 /* TODO: If parent object is site or subnet, also add (SYSTEM_FLAG_CONFIG_ALLOW_RENAME) */
594 if (el || systemFlags != 0) {
595 samdb_msg_add_int(ldb, msg, msg, "systemFlags", systemFlags);
601 talloc_free(mem_ctx);
602 ret = ldb_msg_sanity_check(ldb, msg);
605 if (ret != LDB_SUCCESS) {
606 return ret;
609 ret = ldb_build_add_req(&add_req, ldb, ac,
610 msg,
611 ac->req->controls,
612 ac, oc_op_callback,
613 ac->req);
614 if (ret != LDB_SUCCESS) {
615 return ret;
618 /* perform the add */
619 return ldb_next_request(ac->module, add_req);
622 static int oc_modify_callback(struct ldb_request *req,
623 struct ldb_reply *ares);
624 static int objectclass_do_mod(struct oc_context *ac);
626 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
628 struct ldb_context *ldb = ldb_module_get_ctx(module);
629 struct ldb_message_element *objectclass_element;
630 struct ldb_message *msg;
631 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
632 struct class_list *sorted, *current;
633 struct ldb_request *down_req;
634 struct oc_context *ac;
635 TALLOC_CTX *mem_ctx;
636 char *value;
637 int ret;
639 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
641 /* do not manipulate our control entries */
642 if (ldb_dn_is_special(req->op.mod.message->dn)) {
643 return ldb_next_request(module, req);
646 /* Without schema, there isn't much to do here */
647 if (!schema) {
648 return ldb_next_request(module, req);
650 objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
652 ac = oc_init_context(module, req);
653 if (ac == NULL) {
654 return LDB_ERR_OPERATIONS_ERROR;
657 /* If no part of this touches the objectClass, then we don't
658 * need to make any changes. */
660 /* If the only operation is the deletion of the objectClass
661 * then go on with just fixing the attribute case */
662 if (!objectclass_element) {
663 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
664 if (msg == NULL) {
665 return LDB_ERR_OPERATIONS_ERROR;
668 ret = fix_attributes(ldb, schema, msg);
669 if (ret != LDB_SUCCESS) {
670 return ret;
673 ret = ldb_build_mod_req(&down_req, ldb, ac,
674 msg,
675 req->controls,
676 ac, oc_op_callback,
677 req);
678 if (ret != LDB_SUCCESS) {
679 return ret;
682 /* go on with the call chain */
683 return ldb_next_request(module, down_req);
686 switch (objectclass_element->flags & LDB_FLAG_MOD_MASK) {
687 case LDB_FLAG_MOD_DELETE:
688 if (objectclass_element->num_values == 0) {
689 return LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED;
691 break;
693 case LDB_FLAG_MOD_REPLACE:
694 mem_ctx = talloc_new(ac);
695 if (mem_ctx == NULL) {
696 return LDB_ERR_OPERATIONS_ERROR;
699 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
700 if (msg == NULL) {
701 talloc_free(mem_ctx);
702 return LDB_ERR_OPERATIONS_ERROR;
705 ret = fix_attributes(ldb, schema, msg);
706 if (ret != LDB_SUCCESS) {
707 talloc_free(mem_ctx);
708 return ret;
711 ret = objectclass_sort(module, schema, mem_ctx, objectclass_element, &sorted);
712 if (ret != LDB_SUCCESS) {
713 talloc_free(mem_ctx);
714 return ret;
717 /* We must completely replace the existing objectClass entry,
718 * because we need it sorted */
720 ldb_msg_remove_attr(msg, "objectClass");
721 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
723 if (ret != LDB_SUCCESS) {
724 talloc_free(mem_ctx);
725 return ret;
728 /* Move from the linked list back into an ldb msg */
729 for (current = sorted; current; current = current->next) {
730 /* copy the value as this string is on the schema
731 * context and we can't rely on it not changing
732 * before the operation is over */
733 value = talloc_strdup(msg,
734 current->objectclass->lDAPDisplayName);
735 if (value == NULL) {
736 ldb_oom(ldb);
737 talloc_free(mem_ctx);
738 return LDB_ERR_OPERATIONS_ERROR;
740 ret = ldb_msg_add_string(msg, "objectClass", value);
741 if (ret != LDB_SUCCESS) {
742 ldb_set_errstring(ldb,
743 "objectclass: could not re-add sorted "
744 "objectclass to modify msg");
745 talloc_free(mem_ctx);
746 return ret;
750 talloc_free(mem_ctx);
752 ret = ldb_msg_sanity_check(ldb, msg);
753 if (ret != LDB_SUCCESS) {
754 return ret;
757 ret = ldb_build_mod_req(&down_req, ldb, ac,
758 msg,
759 req->controls,
760 ac, oc_op_callback,
761 req);
762 if (ret != LDB_SUCCESS) {
763 return ret;
766 /* go on with the call chain */
767 return ldb_next_request(module, down_req);
770 /* This isn't the default branch of the switch, but a 'in any
771 * other case'. When a delete isn't for all objectClasses for
772 * example
775 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
776 if (msg == NULL) {
777 ldb_oom(ldb);
778 return LDB_ERR_OPERATIONS_ERROR;
781 ret = fix_attributes(ldb, schema, msg);
782 if (ret != LDB_SUCCESS) {
783 ldb_oom(ldb);
784 return ret;
787 ret = ldb_build_mod_req(&down_req, ldb, ac,
788 msg,
789 req->controls,
790 ac, oc_modify_callback,
791 req);
792 if (ret != LDB_SUCCESS) {
793 return ret;
796 return ldb_next_request(module, down_req);
799 static int oc_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
801 struct ldb_context *ldb;
802 static const char * const attrs[] = { "objectClass", NULL };
803 struct ldb_request *search_req;
804 struct oc_context *ac;
805 int ret;
807 ac = talloc_get_type(req->context, struct oc_context);
808 ldb = ldb_module_get_ctx(ac->module);
810 if (!ares) {
811 return ldb_module_done(ac->req, NULL, NULL,
812 LDB_ERR_OPERATIONS_ERROR);
814 if (ares->error != LDB_SUCCESS) {
815 return ldb_module_done(ac->req, ares->controls,
816 ares->response, ares->error);
819 if (ares->type != LDB_REPLY_DONE) {
820 talloc_free(ares);
821 return ldb_module_done(ac->req, NULL, NULL,
822 LDB_ERR_OPERATIONS_ERROR);
825 ret = ldb_build_search_req(&search_req, ldb, ac,
826 ac->req->op.mod.message->dn, LDB_SCOPE_BASE,
827 "(objectClass=*)",
828 attrs, NULL,
829 ac, get_search_callback,
830 ac->req);
831 if (ret != LDB_SUCCESS) {
832 return ldb_module_done(ac->req, NULL, NULL, ret);
835 ac->step_fn = objectclass_do_mod;
837 ret = ldb_next_request(ac->module, search_req);
838 if (ret != LDB_SUCCESS) {
839 return ldb_module_done(ac->req, NULL, NULL, ret);
841 return LDB_SUCCESS;
844 static int objectclass_do_mod(struct oc_context *ac)
846 struct ldb_context *ldb;
847 const struct dsdb_schema *schema;
848 struct ldb_request *mod_req;
849 char *value;
850 struct ldb_message_element *objectclass_element;
851 struct ldb_message *msg;
852 TALLOC_CTX *mem_ctx;
853 struct class_list *sorted, *current;
854 int ret;
856 ldb = ldb_module_get_ctx(ac->module);
858 if (ac->search_res == NULL) {
859 return LDB_ERR_OPERATIONS_ERROR;
861 schema = dsdb_get_schema(ldb);
863 mem_ctx = talloc_new(ac);
864 if (mem_ctx == NULL) {
865 return LDB_ERR_OPERATIONS_ERROR;
868 /* use a new message structure */
869 msg = ldb_msg_new(ac);
870 if (msg == NULL) {
871 ldb_set_errstring(ldb,
872 "objectclass: could not create new modify msg");
873 talloc_free(mem_ctx);
874 return LDB_ERR_OPERATIONS_ERROR;
877 /* This is now the objectClass list from the database */
878 objectclass_element = ldb_msg_find_element(ac->search_res->message,
879 "objectClass");
880 if (!objectclass_element) {
881 /* Where did it go? bail now... */
882 talloc_free(mem_ctx);
883 return LDB_ERR_OPERATIONS_ERROR;
886 /* modify dn */
887 msg->dn = ac->req->op.mod.message->dn;
889 ret = objectclass_sort(ac->module, schema, mem_ctx, objectclass_element, &sorted);
890 if (ret != LDB_SUCCESS) {
891 return ret;
894 /* We must completely replace the existing objectClass entry.
895 * We could do a constrained add/del, but we are meant to be
896 * in a transaction... */
898 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
899 if (ret != LDB_SUCCESS) {
900 ldb_set_errstring(ldb, "objectclass: could not clear objectclass in modify msg");
901 talloc_free(mem_ctx);
902 return ret;
905 /* Move from the linked list back into an ldb msg */
906 for (current = sorted; current; current = current->next) {
907 value = talloc_strdup(msg, current->objectclass->lDAPDisplayName);
908 if (value == NULL) {
909 ldb_oom(ldb);
910 return LDB_ERR_OPERATIONS_ERROR;
912 ret = ldb_msg_add_string(msg, "objectClass", value);
913 if (ret != LDB_SUCCESS) {
914 ldb_set_errstring(ldb, "objectclass: could not re-add sorted objectclass to modify msg");
915 talloc_free(mem_ctx);
916 return ret;
920 ret = ldb_msg_sanity_check(ldb, msg);
921 if (ret != LDB_SUCCESS) {
922 talloc_free(mem_ctx);
923 return ret;
926 ret = ldb_build_mod_req(&mod_req, ldb, ac,
927 msg,
928 ac->req->controls,
929 ac, oc_op_callback,
930 ac->req);
931 if (ret != LDB_SUCCESS) {
932 talloc_free(mem_ctx);
933 return ret;
936 talloc_free(mem_ctx);
937 /* perform the modify */
938 return ldb_next_request(ac->module, mod_req);
941 static int objectclass_do_rename(struct oc_context *ac);
943 static int objectclass_rename(struct ldb_module *module, struct ldb_request *req)
945 static const char * const attrs[] = { "objectGUID", NULL };
946 struct ldb_context *ldb;
947 struct ldb_request *search_req;
948 struct oc_context *ac;
949 struct ldb_dn *parent_dn;
950 int ret;
952 ldb = ldb_module_get_ctx(module);
954 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_rename\n");
956 if (ldb_dn_is_special(req->op.rename.newdn)) { /* do not manipulate our control entries */
957 return ldb_next_request(module, req);
960 /* Firstly ensure we are not trying to rename it to be a child of itself */
961 if ((ldb_dn_compare_base(req->op.rename.olddn, req->op.rename.newdn) == 0)
962 && (ldb_dn_compare(req->op.rename.olddn, req->op.rename.newdn) != 0)) {
963 ldb_asprintf_errstring(ldb, "Cannot rename %s to be a child of itself",
964 ldb_dn_get_linearized(req->op.rename.olddn));
965 return LDB_ERR_UNWILLING_TO_PERFORM;
968 ac = oc_init_context(module, req);
969 if (ac == NULL) {
970 return LDB_ERR_OPERATIONS_ERROR;
973 parent_dn = ldb_dn_get_parent(ac, req->op.rename.newdn);
974 if (parent_dn == NULL) {
975 ldb_oom(ldb);
976 return LDB_ERR_OPERATIONS_ERROR;
979 /* note that the results of this search are kept and used to
980 update the parentGUID in objectclass_rename_callback() */
981 ret = ldb_build_search_req(&search_req, ldb,
982 ac, parent_dn, LDB_SCOPE_BASE,
983 "(objectClass=*)",
984 attrs, NULL,
985 ac, get_search_callback,
986 req);
987 if (ret != LDB_SUCCESS) {
988 return ret;
991 /* we have to add the show deleted control, as otherwise DRS
992 deletes will be refused as we will think the target parent
993 does not exist */
994 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID, false, NULL);
996 if (ret != LDB_SUCCESS) {
997 return ret;
1000 ac->step_fn = objectclass_do_rename;
1002 return ldb_next_request(ac->module, search_req);
1006 called after the rename happens.
1007 We now need to fix the parentGUID of the object to be the objectGUID of
1008 the new parent
1010 static int objectclass_rename_callback(struct ldb_request *req, struct ldb_reply *ares)
1012 struct ldb_context *ldb;
1013 struct oc_context *ac;
1014 const struct ldb_val *parent_guid;
1015 struct ldb_request *mod_req = NULL;
1016 int ret;
1017 struct ldb_message *msg;
1018 struct ldb_message_element *el = NULL;
1020 ac = talloc_get_type(req->context, struct oc_context);
1021 ldb = ldb_module_get_ctx(ac->module);
1023 /* make sure the rename succeeded */
1024 if (!ares) {
1025 return ldb_module_done(ac->req, NULL, NULL,
1026 LDB_ERR_OPERATIONS_ERROR);
1028 if (ares->error != LDB_SUCCESS) {
1029 return ldb_module_done(ac->req, ares->controls,
1030 ares->response, ares->error);
1034 /* the ac->search_res should contain the new parents objectGUID */
1035 parent_guid = ldb_msg_find_ldb_val(ac->search_res->message, "objectGUID");
1036 if (parent_guid == NULL) {
1037 ldb_asprintf_errstring(ldb, "objectclass: Cannot rename %s, new parent does not have an objectGUID!",
1038 ldb_dn_get_linearized(ac->req->op.rename.newdn));
1039 return LDB_ERR_UNWILLING_TO_PERFORM;
1043 /* construct the modify message */
1044 msg = ldb_msg_new(ac);
1045 if (msg == NULL) {
1046 ldb_oom(ldb);
1047 return LDB_ERR_OPERATIONS_ERROR;
1050 msg->dn = ac->req->op.rename.newdn;
1052 ret = ldb_msg_add_value(msg, "parentGUID", parent_guid, &el);
1053 if (ret != LDB_SUCCESS) {
1054 return ret;
1057 el->flags = LDB_FLAG_MOD_REPLACE;
1059 ret = ldb_build_mod_req(&mod_req, ldb, ac, msg,
1060 NULL, ac, oc_op_callback, req);
1062 return ldb_next_request(ac->module, mod_req);
1065 static int objectclass_do_rename(struct oc_context *ac)
1067 struct ldb_context *ldb;
1068 struct ldb_request *rename_req;
1069 struct ldb_dn *fixed_dn;
1070 int ret;
1072 ldb = ldb_module_get_ctx(ac->module);
1074 /* Check we have a valid parent */
1075 if (ac->search_res == NULL) {
1076 ldb_asprintf_errstring(ldb, "objectclass: Cannot rename %s, parent does not exist!",
1077 ldb_dn_get_linearized(ac->req->op.rename.newdn));
1078 return LDB_ERR_UNWILLING_TO_PERFORM;
1081 /* Fix up the DN to be in the standard form,
1082 * taking particular care to match the parent DN */
1083 ret = fix_dn(ac,
1084 ac->req->op.rename.newdn,
1085 ac->search_res->message->dn,
1086 &fixed_dn);
1087 if (ret != LDB_SUCCESS) {
1088 return ret;
1091 /* TODO: Check this is a valid child to this parent,
1092 * by reading the allowedChildClasses and
1093 * allowedChildClasssesEffective attributes */
1095 ret = ldb_build_rename_req(&rename_req, ldb, ac,
1096 ac->req->op.rename.olddn, fixed_dn,
1097 ac->req->controls,
1098 ac, objectclass_rename_callback,
1099 ac->req);
1100 if (ret != LDB_SUCCESS) {
1101 return ret;
1104 /* perform the rename */
1105 return ldb_next_request(ac->module, rename_req);
1108 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_module_ops = {
1109 .name = "objectclass",
1110 .add = objectclass_add,
1111 .modify = objectclass_modify,
1112 .rename = objectclass_rename,