4 Copyright (C) Simo Sorce 2006
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
26 * Description: sort the objectClass attribute into the class hierarchy
28 * Author: Andrew Bartlett
33 #include "ldb/include/ldb.h"
34 #include "ldb/include/ldb_errors.h"
35 #include "ldb/include/ldb_private.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "lib/util/dlinklist.h"
40 enum oc_step {OC_DO_REQ, OC_SEARCH_SELF, OC_DO_MOD} step;
42 struct ldb_module *module;
43 struct ldb_request *orig_req;
45 struct ldb_request *down_req;
47 struct ldb_request *search_req;
48 struct ldb_reply *search_res;
50 struct ldb_request *mod_req;
54 struct class_list *prev, *next;
55 const char *objectclass;
58 static struct ldb_handle *oc_init_handle(struct ldb_request *req, struct ldb_module *module)
60 struct oc_context *ac;
63 h = talloc_zero(req, struct ldb_handle);
65 ldb_set_errstring(module->ldb, "Out of Memory");
71 ac = talloc_zero(h, struct oc_context);
73 ldb_set_errstring(module->ldb, "Out of Memory");
78 h->private_data = (void *)ac;
80 h->state = LDB_ASYNC_INIT;
81 h->status = LDB_SUCCESS;
89 static int objectclass_sort(struct ldb_module *module,
91 struct ldb_message_element *objectclass_element,
92 struct class_list **sorted_out)
96 const struct dsdb_schema *schema = dsdb_get_schema(module->ldb);
97 struct class_list *sorted = NULL, *parent_class = NULL,
98 *subclass = NULL, *unsorted = NULL, *current, *poss_subclass;
101 * We work on 4 different 'bins' (implemented here as linked lists):
103 * * sorted: the eventual list, in the order we wish to push
104 * into the database. This is the only ordered list.
106 * * parent_class: The current parent class 'bin' we are
107 * trying to find subclasses for
109 * * subclass: The subclasses we have found so far
111 * * unsorted: The remaining objectClasses
113 * The process is a matter of filtering objectClasses up from
114 * unsorted into sorted. Order is irrelevent in the later 3 'bins'.
116 * We start with 'top' (found and promoted to parent_class
117 * initially). Then we find (in unsorted) all the direct
118 * subclasses of 'top'. parent_classes is concatenated onto
119 * the end of 'sorted', and subclass becomes the list in
122 * We then repeat, until we find no more subclasses. Any left
123 * over classes are added to the end.
127 /* Firstly, dump all the objectClass elements into the
128 * unsorted bin, except for 'top', which is special */
129 for (i=0; i < objectclass_element->num_values; i++) {
130 current = talloc(mem_ctx, struct class_list);
132 ldb_set_errstring(module->ldb, "objectclass: out of memory allocating objectclass list");
133 talloc_free(mem_ctx);
134 return LDB_ERR_OPERATIONS_ERROR;
136 current->objectclass = (const char *)objectclass_element->values[i].data;
138 /* this is the root of the tree. We will start
139 * looking for subclasses from here */
140 if (ldb_attr_cmp("top", current->objectclass) == 0) {
141 DLIST_ADD_END(parent_class, current, struct class_list *);
143 DLIST_ADD_END(unsorted, current, struct class_list *);
147 /* DEBUGGING aid: how many layers are we down now? */
151 /* Find all the subclasses of classes in the
152 * parent_classes. Push them onto the subclass list */
154 /* Ensure we don't bother if there are no unsorted entries left */
155 for (current = parent_class; schema && unsorted && current; current = current->next) {
156 /* Walk the list of possible subclasses in unsorted */
157 for (poss_subclass = unsorted; poss_subclass; ) {
158 const struct dsdb_class *class = dsdb_class_by_lDAPDisplayName(schema, poss_subclass->objectclass);
159 struct class_list *next;
161 /* Save the next pointer, as the DLIST_ macros will change poss_subclass->next */
162 next = poss_subclass->next;
164 if (ldb_attr_cmp(class->subClassOf, current->objectclass) == 0) {
165 DLIST_REMOVE(unsorted, poss_subclass);
166 DLIST_ADD(subclass, poss_subclass);
170 poss_subclass = next;
174 /* Now push the parent_classes as sorted, we are done with
175 these. Add to the END of the list by concatenation */
176 DLIST_CONCATENATE(sorted, parent_class, struct class_list *);
178 /* and now find subclasses of these */
179 parent_class = subclass;
182 /* If we didn't find any subclasses we will fall out
184 } while (parent_class);
186 /* This shouldn't happen, and would break MMC, but we can't
187 * afford to loose objectClasses. Perhaps there was no 'top',
188 * or some other schema error?
190 * Detecting schema errors is the job of the schema module, so
191 * at this layer we just try not to loose data
193 DLIST_CONCATENATE(sorted, unsorted, struct class_list *);
195 *sorted_out = sorted;
199 static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
201 struct ldb_message_element *objectclass_element;
202 struct class_list *sorted, *current;
203 struct ldb_request *down_req;
204 struct ldb_message *msg;
208 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
210 if (ldb_dn_is_special(req->op.add.message->dn)) { /* do not manipulate our control entries */
211 return ldb_next_request(module, req);
214 objectclass_element = ldb_msg_find_element(req->op.add.message, "objectClass");
216 /* If no part of this add has an objectClass, then we don't
217 * need to make any changes. cn=rootdse doesn't have an objectClass */
218 if (!objectclass_element) {
219 return ldb_next_request(module, req);
222 mem_ctx = talloc_new(req);
223 if (mem_ctx == NULL) {
224 return LDB_ERR_OPERATIONS_ERROR;
227 ret = objectclass_sort(module, mem_ctx, objectclass_element, &sorted);
228 if (ret != LDB_SUCCESS) {
232 /* prepare the first operation */
233 down_req = talloc(req, struct ldb_request);
234 if (down_req == NULL) {
235 ldb_set_errstring(module->ldb, "Out of memory!");
236 talloc_free(mem_ctx);
237 return LDB_ERR_OPERATIONS_ERROR;
240 *down_req = *req; /* copy the request */
242 down_req->op.add.message = msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
244 if (down_req->op.add.message == NULL) {
245 talloc_free(mem_ctx);
246 return LDB_ERR_OPERATIONS_ERROR;
249 ldb_msg_remove_attr(msg, "objectClass");
250 ret = ldb_msg_add_empty(msg, "objectClass", 0, NULL);
252 if (ret != LDB_SUCCESS) {
253 talloc_free(mem_ctx);
257 /* We must completely replace the existing objectClass entry,
258 * because we need it sorted */
260 /* Move from the linked list back into an ldb msg */
261 for (current = sorted; current; current = current->next) {
262 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass);
263 if (ret != LDB_SUCCESS) {
264 ldb_set_errstring(module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
265 talloc_free(mem_ctx);
270 talloc_free(mem_ctx);
271 ret = ldb_msg_sanity_check(module->ldb, msg);
273 if (ret != LDB_SUCCESS) {
277 /* go on with the call chain */
278 ret = ldb_next_request(module, down_req);
280 /* do not free down_req as the call results may be linked to it,
281 * it will be freed when the upper level request get freed */
282 if (ret == LDB_SUCCESS) {
283 req->handle = down_req->handle;
288 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
290 struct ldb_message_element *objectclass_element;
291 struct ldb_message *msg;
292 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
294 if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
295 return ldb_next_request(module, req);
298 objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
300 /* If no part of this touches the objectClass, then we don't
301 * need to make any changes. */
302 /* If the only operation is the deletion of the objectClass then go on */
303 if (!objectclass_element) {
304 return ldb_next_request(module, req);
307 switch (objectclass_element->flags & LDB_FLAG_MOD_MASK) {
308 case LDB_FLAG_MOD_DELETE:
309 /* Delete everything? Probably totally illigal, but hey! */
310 if (objectclass_element->num_values == 0) {
311 return ldb_next_request(module, req);
314 case LDB_FLAG_MOD_REPLACE:
316 struct ldb_request *down_req;
317 struct class_list *sorted, *current;
320 mem_ctx = talloc_new(req);
321 if (mem_ctx == NULL) {
322 return LDB_ERR_OPERATIONS_ERROR;
325 /* prepare the first operation */
326 down_req = talloc(req, struct ldb_request);
327 if (down_req == NULL) {
328 ldb_set_errstring(module->ldb, "Out of memory!");
329 talloc_free(mem_ctx);
330 return LDB_ERR_OPERATIONS_ERROR;
333 *down_req = *req; /* copy the request */
335 down_req->op.mod.message = msg = ldb_msg_copy_shallow(down_req, req->op.mod.message);
337 if (down_req->op.add.message == NULL) {
338 talloc_free(mem_ctx);
339 return LDB_ERR_OPERATIONS_ERROR;
342 ret = objectclass_sort(module, mem_ctx, objectclass_element, &sorted);
343 if (ret != LDB_SUCCESS) {
347 /* We must completely replace the existing objectClass entry,
348 * because we need it sorted */
350 ldb_msg_remove_attr(msg, "objectClass");
351 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
353 if (ret != LDB_SUCCESS) {
354 talloc_free(mem_ctx);
358 /* Move from the linked list back into an ldb msg */
359 for (current = sorted; current; current = current->next) {
360 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass);
361 if (ret != LDB_SUCCESS) {
362 ldb_set_errstring(module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
363 talloc_free(mem_ctx);
368 talloc_free(mem_ctx);
370 ret = ldb_msg_sanity_check(module->ldb, msg);
371 if (ret != LDB_SUCCESS) {
372 talloc_free(mem_ctx);
376 /* go on with the call chain */
377 ret = ldb_next_request(module, down_req);
379 /* do not free down_req as the call results may be linked to it,
380 * it will be freed when the upper level request get freed */
381 if (ret == LDB_SUCCESS) {
382 req->handle = down_req->handle;
389 struct ldb_handle *h;
390 struct oc_context *ac;
392 h = oc_init_handle(req, module);
394 return LDB_ERR_OPERATIONS_ERROR;
396 ac = talloc_get_type(h->private_data, struct oc_context);
398 /* return or own handle to deal with this call */
401 /* prepare the first operation */
402 ac->down_req = talloc(ac, struct ldb_request);
403 if (ac->down_req == NULL) {
404 ldb_set_errstring(module->ldb, "Out of memory!");
405 return LDB_ERR_OPERATIONS_ERROR;
408 *(ac->down_req) = *req; /* copy the request */
410 ac->down_req->context = NULL;
411 ac->down_req->callback = NULL;
412 ldb_set_timeout_from_prev_req(module->ldb, req, ac->down_req);
414 ac->step = OC_DO_REQ;
416 return ldb_next_request(module, ac->down_req);
420 static int get_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
422 struct oc_context *ac;
424 if (!context || !ares) {
425 ldb_set_errstring(ldb, "NULL Context or Result in callback");
426 return LDB_ERR_OPERATIONS_ERROR;
429 ac = talloc_get_type(context, struct oc_context);
431 /* we are interested only in the single reply (base search) we receive here */
432 if (ares->type == LDB_REPLY_ENTRY) {
433 if (ac->search_res != NULL) {
434 ldb_set_errstring(ldb, "Too many results");
436 return LDB_ERR_OPERATIONS_ERROR;
439 ac->search_res = talloc_move(ac, &ares);
447 static int objectclass_search_self(struct ldb_handle *h) {
449 struct oc_context *ac;
450 static const char * const attrs[] = { "objectClass", NULL };
452 ac = talloc_get_type(h->private_data, struct oc_context);
454 /* prepare the search operation */
455 ac->search_req = talloc_zero(ac, struct ldb_request);
456 if (ac->search_req == NULL) {
457 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
458 return LDB_ERR_OPERATIONS_ERROR;
461 ac->search_req->operation = LDB_SEARCH;
462 ac->search_req->op.search.base = ac->orig_req->op.mod.message->dn;
463 ac->search_req->op.search.scope = LDB_SCOPE_BASE;
464 ac->search_req->op.search.tree = ldb_parse_tree(ac->search_req, NULL);
465 if (ac->search_req->op.search.tree == NULL) {
466 ldb_set_errstring(ac->module->ldb, "objectclass: Internal error producing null search");
467 return LDB_ERR_OPERATIONS_ERROR;
469 ac->search_req->op.search.attrs = attrs;
470 ac->search_req->controls = NULL;
471 ac->search_req->context = ac;
472 ac->search_req->callback = get_self_callback;
473 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
475 ac->step = OC_SEARCH_SELF;
477 return ldb_next_request(ac->module, ac->search_req);
480 static int objectclass_do_mod(struct ldb_handle *h) {
482 struct oc_context *ac;
483 struct ldb_message_element *objectclass_element;
484 struct ldb_message *msg;
486 struct class_list *sorted, *current;
489 ac = talloc_get_type(h->private_data, struct oc_context);
491 mem_ctx = talloc_new(ac);
492 if (mem_ctx == NULL) {
493 return LDB_ERR_OPERATIONS_ERROR;
496 ac->mod_req = talloc(ac, struct ldb_request);
497 if (ac->mod_req == NULL) {
498 talloc_free(mem_ctx);
499 return LDB_ERR_OPERATIONS_ERROR;
502 ac->mod_req->operation = LDB_MODIFY;
503 ac->mod_req->controls = NULL;
504 ac->mod_req->context = ac;
505 ac->mod_req->callback = NULL;
506 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->mod_req);
508 /* use a new message structure */
509 ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
511 ldb_set_errstring(ac->module->ldb, "objectclass: could not create new modify msg");
512 talloc_free(mem_ctx);
513 return LDB_ERR_OPERATIONS_ERROR;
516 /* This is now the objectClass list from the database */
517 objectclass_element = ldb_msg_find_element(ac->search_res->message,
519 if (!objectclass_element) {
520 /* Where did it go? Move along now, nothing to see here */
521 talloc_free(mem_ctx);
526 msg->dn = ac->orig_req->op.mod.message->dn;
528 ret = objectclass_sort(ac->module, mem_ctx, objectclass_element, &sorted);
529 if (ret != LDB_SUCCESS) {
533 /* We must completely replace the existing objectClass entry.
534 * We could do a constrained add/del, but we are meant to be
535 * in a transaction... */
537 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
538 if (ret != LDB_SUCCESS) {
539 ldb_set_errstring(ac->module->ldb, "objectclass: could not clear objectclass in modify msg");
540 talloc_free(mem_ctx);
544 /* Move from the linked list back into an ldb msg */
545 for (current = sorted; current; current = current->next) {
546 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass);
547 if (ret != LDB_SUCCESS) {
548 ldb_set_errstring(ac->module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
549 talloc_free(mem_ctx);
554 ret = ldb_msg_sanity_check(ac->module->ldb, msg);
555 if (ret != LDB_SUCCESS) {
556 talloc_free(mem_ctx);
561 h->state = LDB_ASYNC_INIT;
562 h->status = LDB_SUCCESS;
564 ac->step = OC_DO_MOD;
566 talloc_free(mem_ctx);
567 /* perform the search */
568 return ldb_next_request(ac->module, ac->mod_req);
571 static int oc_wait(struct ldb_handle *handle) {
572 struct oc_context *ac;
575 if (!handle || !handle->private_data) {
576 return LDB_ERR_OPERATIONS_ERROR;
579 if (handle->state == LDB_ASYNC_DONE) {
580 return handle->status;
583 handle->state = LDB_ASYNC_PENDING;
584 handle->status = LDB_SUCCESS;
586 ac = talloc_get_type(handle->private_data, struct oc_context);
590 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
592 if (ret != LDB_SUCCESS) {
593 handle->status = ret;
596 if (ac->down_req->handle->status != LDB_SUCCESS) {
597 handle->status = ac->down_req->handle->status;
601 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
605 /* mods done, go on */
606 return objectclass_search_self(handle);
609 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
611 if (ret != LDB_SUCCESS) {
612 handle->status = ret;
615 if (ac->search_req->handle->status != LDB_SUCCESS) {
616 handle->status = ac->search_req->handle->status;
620 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
624 /* self search done, go on */
625 return objectclass_do_mod(handle);
628 ret = ldb_wait(ac->mod_req->handle, LDB_WAIT_NONE);
630 if (ret != LDB_SUCCESS) {
631 handle->status = ret;
634 if (ac->mod_req->handle->status != LDB_SUCCESS) {
635 handle->status = ac->mod_req->handle->status;
639 if (ac->mod_req->handle->state != LDB_ASYNC_DONE) {
646 ret = LDB_ERR_OPERATIONS_ERROR;
653 handle->state = LDB_ASYNC_DONE;
657 static int oc_wait_all(struct ldb_handle *handle) {
661 while (handle->state != LDB_ASYNC_DONE) {
662 ret = oc_wait(handle);
663 if (ret != LDB_SUCCESS) {
668 return handle->status;
671 static int objectclass_wait(struct ldb_handle *handle, enum ldb_wait_type type)
673 if (type == LDB_WAIT_ALL) {
674 return oc_wait_all(handle);
676 return oc_wait(handle);
680 static const struct ldb_module_ops objectclass_ops = {
681 .name = "objectclass",
682 .add = objectclass_add,
683 .modify = objectclass_modify,
684 .wait = objectclass_wait
687 int ldb_objectclass_init(void)
689 return ldb_register_module(&objectclass_ops);