4 Copyright (C) Simo Sorce 2006
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2006
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 * Component: objectClass sorting module
30 * Description: sort the objectClass attribute into the class hierarchy
32 * Author: Andrew Bartlett
35 #include "ldb_includes.h"
39 enum oc_step {OC_DO_REQ, OC_SEARCH_SELF, OC_DO_MOD} step;
41 struct ldb_module *module;
42 struct ldb_request *orig_req;
44 struct ldb_request *down_req;
46 struct ldb_request *search_req;
47 struct ldb_reply *search_res;
49 struct ldb_request *mod_req;
53 struct class_list *prev, *next;
54 const char *objectclass;
57 static struct ldb_handle *oc_init_handle(struct ldb_request *req, struct ldb_module *module)
59 struct oc_context *ac;
62 h = talloc_zero(req, struct ldb_handle);
64 ldb_set_errstring(module->ldb, "Out of Memory");
70 ac = talloc_zero(h, struct oc_context);
72 ldb_set_errstring(module->ldb, "Out of Memory");
77 h->private_data = (void *)ac;
79 h->state = LDB_ASYNC_INIT;
80 h->status = LDB_SUCCESS;
88 static int objectclass_sort(struct ldb_module *module,
90 struct ldb_message_element *objectclass_element,
91 struct class_list **sorted_out)
95 struct class_list *sorted = NULL, *parent_class = NULL,
96 *subclass = NULL, *unsorted = NULL, *current, *poss_subclass;
99 * We work on 4 different 'bins' (implemented here as linked lists):
101 * * sorted: the eventual list, in the order we wish to push
102 * into the database. This is the only ordered list.
104 * * parent_class: The current parent class 'bin' we are
105 * trying to find subclasses for
107 * * subclass: The subclasses we have found so far
109 * * unsorted: The remaining objectClasses
111 * The process is a matter of filtering objectClasses up from
112 * unsorted into sorted. Order is irrelevent in the later 3 'bins'.
114 * We start with 'top' (found and promoted to parent_class
115 * initially). Then we find (in unsorted) all the direct
116 * subclasses of 'top'. parent_classes is concatenated onto
117 * the end of 'sorted', and subclass becomes the list in
120 * We then repeat, until we find no more subclasses. Any left
121 * over classes are added to the end.
125 /* Firstly, dump all the objectClass elements into the
126 * unsorted bin, except for 'top', which is special */
127 for (i=0; i < objectclass_element->num_values; i++) {
128 current = talloc(mem_ctx, struct class_list);
130 ldb_set_errstring(module->ldb, "objectclass: out of memory allocating objectclass list");
131 talloc_free(mem_ctx);
132 return LDB_ERR_OPERATIONS_ERROR;
134 current->objectclass = (const char *)objectclass_element->values[i].data;
136 /* this is the root of the tree. We will start
137 * looking for subclasses from here */
138 if (ldb_attr_cmp("top", current->objectclass) == 0) {
139 DLIST_ADD_END(parent_class, current, struct class_list *);
141 DLIST_ADD_END(unsorted, current, struct class_list *);
145 /* DEBUGGING aid: how many layers are we down now? */
149 /* Find all the subclasses of classes in the
150 * parent_classes. Push them onto the subclass list */
152 /* Ensure we don't bother if there are no unsorted entries left */
153 for (current = parent_class; unsorted && current; current = current->next) {
154 const char **subclasses = ldb_subclass_list(module->ldb, current->objectclass);
156 /* Walk the list of possible subclasses in unsorted */
157 for (poss_subclass = unsorted; poss_subclass; ) {
158 struct class_list *next;
160 /* Save the next pointer, as the DLIST_ macros will change poss_subclass->next */
161 next = poss_subclass->next;
163 for (i = 0; subclasses && subclasses[i]; i++) {
164 if (ldb_attr_cmp(poss_subclass->objectclass, subclasses[i]) == 0) {
165 DLIST_REMOVE(unsorted, poss_subclass);
166 DLIST_ADD(subclass, poss_subclass);
171 poss_subclass = next;
175 /* Now push the parent_classes as sorted, we are done with
176 these. Add to the END of the list by concatenation */
177 DLIST_CONCATENATE(sorted, parent_class, struct class_list *);
179 /* and now find subclasses of these */
180 parent_class = subclass;
183 /* If we didn't find any subclasses we will fall out
185 } while (parent_class);
187 /* This shouldn't happen, and would break MMC, but we can't
188 * afford to loose objectClasses. Perhaps there was no 'top',
189 * or some other schema error?
191 * Detecting schema errors is the job of the schema module, so
192 * at this layer we just try not to loose data
194 DLIST_CONCATENATE(sorted, unsorted, struct class_list *);
196 *sorted_out = sorted;
200 static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
202 struct ldb_message_element *objectclass_element;
203 struct class_list *sorted, *current;
204 struct ldb_request *down_req;
205 struct ldb_message *msg;
209 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
211 if (ldb_dn_is_special(req->op.add.message->dn)) { /* do not manipulate our control entries */
212 return ldb_next_request(module, req);
215 objectclass_element = ldb_msg_find_element(req->op.add.message, "objectClass");
217 /* If no part of this add has an objectClass, then we don't
218 * need to make any changes. cn=rootdse doesn't have an objectClass */
219 if (!objectclass_element) {
220 return ldb_next_request(module, req);
223 mem_ctx = talloc_new(req);
224 if (mem_ctx == NULL) {
225 return LDB_ERR_OPERATIONS_ERROR;
228 ret = objectclass_sort(module, mem_ctx, objectclass_element, &sorted);
229 if (ret != LDB_SUCCESS) {
233 /* prepare the first operation */
234 down_req = talloc(req, struct ldb_request);
235 if (down_req == NULL) {
236 ldb_set_errstring(module->ldb, "Out of memory!");
237 talloc_free(mem_ctx);
238 return LDB_ERR_OPERATIONS_ERROR;
241 *down_req = *req; /* copy the request */
243 down_req->op.add.message = msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
245 if (down_req->op.add.message == NULL) {
246 talloc_free(mem_ctx);
247 return LDB_ERR_OPERATIONS_ERROR;
250 ldb_msg_remove_attr(msg, "objectClass");
251 ret = ldb_msg_add_empty(msg, "objectClass", 0, NULL);
253 if (ret != LDB_SUCCESS) {
254 talloc_free(mem_ctx);
258 /* We must completely replace the existing objectClass entry,
259 * because we need it sorted */
261 /* Move from the linked list back into an ldb msg */
262 for (current = sorted; current; current = current->next) {
263 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass);
264 if (ret != LDB_SUCCESS) {
265 ldb_set_errstring(module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
266 talloc_free(mem_ctx);
271 talloc_free(mem_ctx);
272 ret = ldb_msg_sanity_check(module->ldb, msg);
274 if (ret != LDB_SUCCESS) {
278 /* go on with the call chain */
279 ret = ldb_next_request(module, down_req);
281 /* do not free down_req as the call results may be linked to it,
282 * it will be freed when the upper level request get freed */
283 if (ret == LDB_SUCCESS) {
284 req->handle = down_req->handle;
289 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
291 struct ldb_message_element *objectclass_element;
292 struct ldb_message *msg;
293 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
295 if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
296 return ldb_next_request(module, req);
299 objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
301 /* If no part of this touches the objectClass, then we don't
302 * need to make any changes. */
303 /* If the only operation is the deletion of the objectClass then go on */
304 if (!objectclass_element) {
305 return ldb_next_request(module, req);
308 switch (objectclass_element->flags & LDB_FLAG_MOD_MASK) {
309 case LDB_FLAG_MOD_DELETE:
310 /* Delete everything? Probably totally illigal, but hey! */
311 if (objectclass_element->num_values == 0) {
312 return ldb_next_request(module, req);
315 case LDB_FLAG_MOD_REPLACE:
317 struct ldb_request *down_req;
318 struct class_list *sorted, *current;
321 mem_ctx = talloc_new(req);
322 if (mem_ctx == NULL) {
323 return LDB_ERR_OPERATIONS_ERROR;
326 /* prepare the first operation */
327 down_req = talloc(req, struct ldb_request);
328 if (down_req == NULL) {
329 ldb_set_errstring(module->ldb, "Out of memory!");
330 talloc_free(mem_ctx);
331 return LDB_ERR_OPERATIONS_ERROR;
334 *down_req = *req; /* copy the request */
336 down_req->op.mod.message = msg = ldb_msg_copy_shallow(down_req, req->op.mod.message);
338 if (down_req->op.add.message == NULL) {
339 talloc_free(mem_ctx);
340 return LDB_ERR_OPERATIONS_ERROR;
343 ret = objectclass_sort(module, mem_ctx, objectclass_element, &sorted);
344 if (ret != LDB_SUCCESS) {
348 /* We must completely replace the existing objectClass entry,
349 * because we need it sorted */
351 ldb_msg_remove_attr(msg, "objectClass");
352 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
354 if (ret != LDB_SUCCESS) {
355 talloc_free(mem_ctx);
359 /* Move from the linked list back into an ldb msg */
360 for (current = sorted; current; current = current->next) {
361 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass);
362 if (ret != LDB_SUCCESS) {
363 ldb_set_errstring(module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
364 talloc_free(mem_ctx);
369 talloc_free(mem_ctx);
371 ret = ldb_msg_sanity_check(module->ldb, msg);
372 if (ret != LDB_SUCCESS) {
373 talloc_free(mem_ctx);
377 /* go on with the call chain */
378 ret = ldb_next_request(module, down_req);
380 /* do not free down_req as the call results may be linked to it,
381 * it will be freed when the upper level request get freed */
382 if (ret == LDB_SUCCESS) {
383 req->handle = down_req->handle;
390 struct ldb_handle *h;
391 struct oc_context *ac;
393 h = oc_init_handle(req, module);
395 return LDB_ERR_OPERATIONS_ERROR;
397 ac = talloc_get_type(h->private_data, struct oc_context);
399 /* return or own handle to deal with this call */
402 /* prepare the first operation */
403 ac->down_req = talloc(ac, struct ldb_request);
404 if (ac->down_req == NULL) {
405 ldb_set_errstring(module->ldb, "Out of memory!");
406 return LDB_ERR_OPERATIONS_ERROR;
409 *(ac->down_req) = *req; /* copy the request */
411 ac->down_req->context = NULL;
412 ac->down_req->callback = NULL;
413 ldb_set_timeout_from_prev_req(module->ldb, req, ac->down_req);
415 ac->step = OC_DO_REQ;
417 return ldb_next_request(module, ac->down_req);
421 static int get_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
423 struct oc_context *ac;
425 if (!context || !ares) {
426 ldb_set_errstring(ldb, "NULL Context or Result in callback");
427 return LDB_ERR_OPERATIONS_ERROR;
430 ac = talloc_get_type(context, struct oc_context);
432 /* we are interested only in the single reply (base search) we receive here */
433 if (ares->type == LDB_REPLY_ENTRY) {
434 if (ac->search_res != NULL) {
435 ldb_set_errstring(ldb, "Too many results");
437 return LDB_ERR_OPERATIONS_ERROR;
440 ac->search_res = talloc_move(ac, &ares);
448 static int objectclass_search_self(struct ldb_handle *h) {
450 struct oc_context *ac;
451 static const char * const attrs[] = { "objectClass", NULL };
453 ac = talloc_get_type(h->private_data, struct oc_context);
455 /* prepare the search operation */
456 ac->search_req = talloc_zero(ac, struct ldb_request);
457 if (ac->search_req == NULL) {
458 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
459 return LDB_ERR_OPERATIONS_ERROR;
462 ac->search_req->operation = LDB_SEARCH;
463 ac->search_req->op.search.base = ac->orig_req->op.mod.message->dn;
464 ac->search_req->op.search.scope = LDB_SCOPE_BASE;
465 ac->search_req->op.search.tree = ldb_parse_tree(ac->search_req, NULL);
466 if (ac->search_req->op.search.tree == NULL) {
467 ldb_set_errstring(ac->module->ldb, "objectclass: Internal error producing null search");
468 return LDB_ERR_OPERATIONS_ERROR;
470 ac->search_req->op.search.attrs = attrs;
471 ac->search_req->controls = NULL;
472 ac->search_req->context = ac;
473 ac->search_req->callback = get_self_callback;
474 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
476 ac->step = OC_SEARCH_SELF;
478 return ldb_next_request(ac->module, ac->search_req);
481 static int objectclass_do_mod(struct ldb_handle *h) {
483 struct oc_context *ac;
484 struct ldb_message_element *objectclass_element;
485 struct ldb_message *msg;
487 struct class_list *sorted, *current;
490 ac = talloc_get_type(h->private_data, struct oc_context);
492 mem_ctx = talloc_new(ac);
493 if (mem_ctx == NULL) {
494 return LDB_ERR_OPERATIONS_ERROR;
497 ac->mod_req = talloc(ac, struct ldb_request);
498 if (ac->mod_req == NULL) {
499 talloc_free(mem_ctx);
500 return LDB_ERR_OPERATIONS_ERROR;
503 ac->mod_req->operation = LDB_MODIFY;
504 ac->mod_req->controls = NULL;
505 ac->mod_req->context = ac;
506 ac->mod_req->callback = NULL;
507 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->mod_req);
509 /* use a new message structure */
510 ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
512 ldb_set_errstring(ac->module->ldb, "objectclass: could not create new modify msg");
513 talloc_free(mem_ctx);
514 return LDB_ERR_OPERATIONS_ERROR;
517 /* This is now the objectClass list from the database */
518 objectclass_element = ldb_msg_find_element(ac->search_res->message,
520 if (!objectclass_element) {
521 /* Where did it go? Move along now, nothing to see here */
522 talloc_free(mem_ctx);
527 msg->dn = ac->orig_req->op.mod.message->dn;
529 ret = objectclass_sort(ac->module, mem_ctx, objectclass_element, &sorted);
530 if (ret != LDB_SUCCESS) {
534 /* We must completely replace the existing objectClass entry.
535 * We could do a constrained add/del, but we are meant to be
536 * in a transaction... */
538 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
539 if (ret != LDB_SUCCESS) {
540 ldb_set_errstring(ac->module->ldb, "objectclass: could not clear objectclass in modify msg");
541 talloc_free(mem_ctx);
545 /* Move from the linked list back into an ldb msg */
546 for (current = sorted; current; current = current->next) {
547 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass);
548 if (ret != LDB_SUCCESS) {
549 ldb_set_errstring(ac->module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
550 talloc_free(mem_ctx);
555 ret = ldb_msg_sanity_check(ac->module->ldb, msg);
556 if (ret != LDB_SUCCESS) {
557 talloc_free(mem_ctx);
562 h->state = LDB_ASYNC_INIT;
563 h->status = LDB_SUCCESS;
565 ac->step = OC_DO_MOD;
567 talloc_free(mem_ctx);
568 /* perform the search */
569 return ldb_next_request(ac->module, ac->mod_req);
572 static int oc_wait(struct ldb_handle *handle) {
573 struct oc_context *ac;
576 if (!handle || !handle->private_data) {
577 return LDB_ERR_OPERATIONS_ERROR;
580 if (handle->state == LDB_ASYNC_DONE) {
581 return handle->status;
584 handle->state = LDB_ASYNC_PENDING;
585 handle->status = LDB_SUCCESS;
587 ac = talloc_get_type(handle->private_data, struct oc_context);
591 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
593 if (ret != LDB_SUCCESS) {
594 handle->status = ret;
597 if (ac->down_req->handle->status != LDB_SUCCESS) {
598 handle->status = ac->down_req->handle->status;
602 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
606 /* mods done, go on */
607 return objectclass_search_self(handle);
610 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
612 if (ret != LDB_SUCCESS) {
613 handle->status = ret;
616 if (ac->search_req->handle->status != LDB_SUCCESS) {
617 handle->status = ac->search_req->handle->status;
621 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
625 /* self search done, go on */
626 return objectclass_do_mod(handle);
629 ret = ldb_wait(ac->mod_req->handle, LDB_WAIT_NONE);
631 if (ret != LDB_SUCCESS) {
632 handle->status = ret;
635 if (ac->mod_req->handle->status != LDB_SUCCESS) {
636 handle->status = ac->mod_req->handle->status;
640 if (ac->mod_req->handle->state != LDB_ASYNC_DONE) {
647 ret = LDB_ERR_OPERATIONS_ERROR;
654 handle->state = LDB_ASYNC_DONE;
658 static int oc_wait_all(struct ldb_handle *handle) {
662 while (handle->state != LDB_ASYNC_DONE) {
663 ret = oc_wait(handle);
664 if (ret != LDB_SUCCESS) {
669 return handle->status;
672 static int objectclass_wait(struct ldb_handle *handle, enum ldb_wait_type type)
674 if (type == LDB_WAIT_ALL) {
675 return oc_wait_all(handle);
677 return oc_wait(handle);
681 static const struct ldb_module_ops objectclass_ops = {
682 .name = "objectclass",
683 .add = objectclass_add,
684 .modify = objectclass_modify,
685 .wait = objectclass_wait
688 int ldb_objectclass_init(void)
690 return ldb_register_module(&objectclass_ops);