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
36 #include "ldb/include/includes.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 struct class_list *sorted = NULL, *parent_class = NULL,
97 *subclass = NULL, *unsorted = NULL, *current, *poss_subclass;
100 * We work on 4 different 'bins' (implemented here as linked lists):
102 * * sorted: the eventual list, in the order we wish to push
103 * into the database. This is the only ordered list.
105 * * parent_class: The current parent class 'bin' we are
106 * trying to find subclasses for
108 * * subclass: The subclasses we have found so far
110 * * unsorted: The remaining objectClasses
112 * The process is a matter of filtering objectClasses up from
113 * unsorted into sorted. Order is irrelevent in the later 3 'bins'.
115 * We start with 'top' (found and promoted to parent_class
116 * initially). Then we find (in unsorted) all the direct
117 * subclasses of 'top'. parent_classes is concatenated onto
118 * the end of 'sorted', and subclass becomes the list in
121 * We then repeat, until we find no more subclasses. Any left
122 * over classes are added to the end.
126 /* Firstly, dump all the objectClass elements into the
127 * unsorted bin, except for 'top', which is special */
128 for (i=0; i < objectclass_element->num_values; i++) {
129 current = talloc(mem_ctx, struct class_list);
131 ldb_set_errstring(module->ldb, "objectclass: out of memory allocating objectclass list");
132 talloc_free(mem_ctx);
133 return LDB_ERR_OPERATIONS_ERROR;
135 current->objectclass = (const char *)objectclass_element->values[i].data;
137 /* this is the root of the tree. We will start
138 * looking for subclasses from here */
139 if (ldb_attr_cmp("top", current->objectclass) == 0) {
140 DLIST_ADD(parent_class, current);
142 DLIST_ADD(unsorted, current);
146 /* DEBUGGING aid: how many layers are we down now? */
150 /* Find all the subclasses of classes in the
151 * parent_classes. Push them onto the subclass list */
153 /* Ensure we don't bother if there are no unsorted entries left */
154 for (current = parent_class; unsorted && current; current = current->next) {
155 const char **subclasses = ldb_subclass_list(module->ldb, current->objectclass);
157 /* Walk the list of possible subclasses in unsorted */
158 for (poss_subclass = unsorted; poss_subclass; ) {
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 for (i = 0; subclasses && subclasses[i]; i++) {
165 if (ldb_attr_cmp(poss_subclass->objectclass, subclasses[i]) == 0) {
166 DLIST_REMOVE(unsorted, poss_subclass);
167 DLIST_ADD(subclass, poss_subclass);
172 poss_subclass = next;
176 /* Now push the parent_classes as sorted, we are done with
177 these. Add to the END of the list by concatenation */
178 DLIST_CONCATENATE(sorted, parent_class, struct class_list *);
180 /* and now find subclasses of these */
181 parent_class = subclass;
184 /* If we didn't find any subclasses we will fall out
186 } while (parent_class);
188 /* This shouldn't happen, and would break MMC, but we can't
189 * afford to loose objectClasses. Perhaps there was no 'top',
190 * or some other schema error?
192 * Detecting schema errors is the job of the schema module, so
193 * at this layer we just try not to loose data
195 DLIST_CONCATENATE(sorted, unsorted, struct class_list *);
197 *sorted_out = sorted;
201 static int objectclass_add(struct ldb_module *module, struct ldb_request *req)
203 struct ldb_message_element *objectclass_element;
204 struct class_list *sorted, *current;
205 struct ldb_request *down_req;
206 struct ldb_message *msg;
210 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_add\n");
212 if (ldb_dn_is_special(req->op.add.message->dn)) { /* do not manipulate our control entries */
213 return ldb_next_request(module, req);
216 objectclass_element = ldb_msg_find_element(req->op.add.message, "objectClass");
218 /* If no part of this add has an objectClass, then we don't
219 * need to make any changes. cn=rootdse doesn't have an objectClass */
220 if (!objectclass_element) {
221 return ldb_next_request(module, req);
224 mem_ctx = talloc_new(req);
225 if (mem_ctx == NULL) {
226 return LDB_ERR_OPERATIONS_ERROR;
229 ret = objectclass_sort(module, mem_ctx, objectclass_element, &sorted);
230 if (ret != LDB_SUCCESS) {
234 /* prepare the first operation */
235 down_req = talloc(req, struct ldb_request);
236 if (down_req == NULL) {
237 ldb_set_errstring(module->ldb, "Out of memory!");
238 talloc_free(mem_ctx);
239 return LDB_ERR_OPERATIONS_ERROR;
242 *down_req = *req; /* copy the request */
244 down_req->op.add.message = msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
246 if (down_req->op.add.message == NULL) {
247 talloc_free(mem_ctx);
248 return LDB_ERR_OPERATIONS_ERROR;
251 ldb_msg_remove_attr(msg, "objectClass");
252 ret = ldb_msg_add_empty(msg, "objectClass", 0, NULL);
254 if (ret != LDB_SUCCESS) {
255 talloc_free(mem_ctx);
259 /* We must completely replace the existing objectClass entry,
260 * because we need it sorted */
262 /* Move from the linked list back into an ldb msg */
263 for (current = sorted; current; current = current->next) {
264 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass);
265 if (ret != LDB_SUCCESS) {
266 ldb_set_errstring(module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
267 talloc_free(mem_ctx);
272 talloc_free(mem_ctx);
273 ret = ldb_msg_sanity_check(module->ldb, msg);
275 if (ret != LDB_SUCCESS) {
279 /* go on with the call chain */
280 ret = ldb_next_request(module, down_req);
282 /* do not free down_req as the call results may be linked to it,
283 * it will be freed when the upper level request get freed */
284 if (ret == LDB_SUCCESS) {
285 req->handle = down_req->handle;
290 static int objectclass_modify(struct ldb_module *module, struct ldb_request *req)
292 struct ldb_message_element *objectclass_element;
293 struct ldb_message *msg;
294 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_modify\n");
296 if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
297 return ldb_next_request(module, req);
300 objectclass_element = ldb_msg_find_element(req->op.mod.message, "objectClass");
302 /* If no part of this touches the objectClass, then we don't
303 * need to make any changes. */
304 /* If the only operation is the deletion of the objectClass then go on */
305 if (!objectclass_element) {
306 return ldb_next_request(module, req);
309 switch (objectclass_element->flags & LDB_FLAG_MOD_MASK) {
310 case LDB_FLAG_MOD_DELETE:
311 /* Delete everything? Probably totally illigal, but hey! */
312 if (objectclass_element->num_values == 0) {
313 return ldb_next_request(module, req);
316 case LDB_FLAG_MOD_REPLACE:
318 struct ldb_request *down_req;
319 struct class_list *sorted, *current;
322 mem_ctx = talloc_new(req);
323 if (mem_ctx == NULL) {
324 return LDB_ERR_OPERATIONS_ERROR;
327 /* prepare the first operation */
328 down_req = talloc(req, struct ldb_request);
329 if (down_req == NULL) {
330 ldb_set_errstring(module->ldb, "Out of memory!");
331 talloc_free(mem_ctx);
332 return LDB_ERR_OPERATIONS_ERROR;
335 *down_req = *req; /* copy the request */
337 down_req->op.mod.message = msg = ldb_msg_copy_shallow(down_req, req->op.mod.message);
339 if (down_req->op.add.message == NULL) {
340 talloc_free(mem_ctx);
341 return LDB_ERR_OPERATIONS_ERROR;
344 ret = objectclass_sort(module, mem_ctx, objectclass_element, &sorted);
345 if (ret != LDB_SUCCESS) {
349 /* We must completely replace the existing objectClass entry,
350 * because we need it sorted */
352 ldb_msg_remove_attr(msg, "objectClass");
353 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
355 if (ret != LDB_SUCCESS) {
356 talloc_free(mem_ctx);
360 /* Move from the linked list back into an ldb msg */
361 for (current = sorted; current; current = current->next) {
362 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass);
363 if (ret != LDB_SUCCESS) {
364 ldb_set_errstring(module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
365 talloc_free(mem_ctx);
370 talloc_free(mem_ctx);
372 ret = ldb_msg_sanity_check(module->ldb, msg);
373 if (ret != LDB_SUCCESS) {
374 talloc_free(mem_ctx);
378 /* go on with the call chain */
379 ret = ldb_next_request(module, down_req);
381 /* do not free down_req as the call results may be linked to it,
382 * it will be freed when the upper level request get freed */
383 if (ret == LDB_SUCCESS) {
384 req->handle = down_req->handle;
391 struct ldb_handle *h;
392 struct oc_context *ac;
394 h = oc_init_handle(req, module);
396 return LDB_ERR_OPERATIONS_ERROR;
398 ac = talloc_get_type(h->private_data, struct oc_context);
400 /* return or own handle to deal with this call */
403 /* prepare the first operation */
404 ac->down_req = talloc(ac, struct ldb_request);
405 if (ac->down_req == NULL) {
406 ldb_set_errstring(module->ldb, "Out of memory!");
407 return LDB_ERR_OPERATIONS_ERROR;
410 *(ac->down_req) = *req; /* copy the request */
412 ac->down_req->context = NULL;
413 ac->down_req->callback = NULL;
414 ldb_set_timeout_from_prev_req(module->ldb, req, ac->down_req);
416 ac->step = OC_DO_REQ;
418 return ldb_next_request(module, ac->down_req);
422 static int get_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
424 struct oc_context *ac;
426 if (!context || !ares) {
427 ldb_set_errstring(ldb, "NULL Context or Result in callback");
428 return LDB_ERR_OPERATIONS_ERROR;
431 ac = talloc_get_type(context, struct oc_context);
433 /* we are interested only in the single reply (base search) we receive here */
434 if (ares->type == LDB_REPLY_ENTRY) {
435 if (ac->search_res != NULL) {
436 ldb_set_errstring(ldb, "Too many results");
438 return LDB_ERR_OPERATIONS_ERROR;
441 ac->search_res = talloc_move(ac, &ares);
449 static int objectclass_search_self(struct ldb_handle *h) {
451 struct oc_context *ac;
452 static const char * const attrs[] = { "objectClass", NULL };
454 ac = talloc_get_type(h->private_data, struct oc_context);
456 /* prepare the search operation */
457 ac->search_req = talloc_zero(ac, struct ldb_request);
458 if (ac->search_req == NULL) {
459 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
460 return LDB_ERR_OPERATIONS_ERROR;
463 ac->search_req->operation = LDB_SEARCH;
464 ac->search_req->op.search.base = ac->orig_req->op.mod.message->dn;
465 ac->search_req->op.search.scope = LDB_SCOPE_BASE;
466 ac->search_req->op.search.tree = ldb_parse_tree(ac->search_req, NULL);
467 if (ac->search_req->op.search.tree == NULL) {
468 ldb_set_errstring(ac->module->ldb, "objectclass: Internal error producing null search");
469 return LDB_ERR_OPERATIONS_ERROR;
471 ac->search_req->op.search.attrs = attrs;
472 ac->search_req->controls = NULL;
473 ac->search_req->context = ac;
474 ac->search_req->callback = get_self_callback;
475 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
477 ac->step = OC_SEARCH_SELF;
479 return ldb_next_request(ac->module, ac->search_req);
482 static int objectclass_do_mod(struct ldb_handle *h) {
484 struct oc_context *ac;
485 struct ldb_message_element *objectclass_element;
486 struct ldb_message *msg;
488 struct class_list *sorted, *current;
491 ac = talloc_get_type(h->private_data, struct oc_context);
493 mem_ctx = talloc_new(ac);
494 if (mem_ctx == NULL) {
495 return LDB_ERR_OPERATIONS_ERROR;
498 ac->mod_req = talloc(ac, struct ldb_request);
499 if (ac->mod_req == NULL) {
500 talloc_free(mem_ctx);
501 return LDB_ERR_OPERATIONS_ERROR;
504 ac->mod_req->operation = LDB_MODIFY;
505 ac->mod_req->controls = NULL;
506 ac->mod_req->context = ac;
507 ac->mod_req->callback = NULL;
508 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->mod_req);
510 /* use a new message structure */
511 ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
513 ldb_set_errstring(ac->module->ldb, "objectclass: could not create new modify msg");
514 talloc_free(mem_ctx);
515 return LDB_ERR_OPERATIONS_ERROR;
518 /* This is now the objectClass list from the database */
519 objectclass_element = ldb_msg_find_element(ac->search_res->message,
521 if (!objectclass_element) {
522 /* Where did it go? Move along now, nothing to see here */
523 talloc_free(mem_ctx);
528 msg->dn = ac->orig_req->op.mod.message->dn;
530 ret = objectclass_sort(ac->module, mem_ctx, objectclass_element, &sorted);
531 if (ret != LDB_SUCCESS) {
535 /* We must completely replace the existing objectClass entry.
536 * We could do a constrained add/del, but we are meant to be
537 * in a transaction... */
539 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
540 if (ret != LDB_SUCCESS) {
541 ldb_set_errstring(ac->module->ldb, "objectclass: could not clear objectclass in modify msg");
542 talloc_free(mem_ctx);
546 /* Move from the linked list back into an ldb msg */
547 for (current = sorted; current; current = current->next) {
548 ret = ldb_msg_add_string(msg, "objectClass", current->objectclass);
549 if (ret != LDB_SUCCESS) {
550 ldb_set_errstring(ac->module->ldb, "objectclass: could not re-add sorted objectclass to modify msg");
551 talloc_free(mem_ctx);
556 ret = ldb_msg_sanity_check(ac->module->ldb, msg);
557 if (ret != LDB_SUCCESS) {
558 talloc_free(mem_ctx);
563 h->state = LDB_ASYNC_INIT;
564 h->status = LDB_SUCCESS;
566 ac->step = OC_DO_MOD;
568 talloc_free(mem_ctx);
569 /* perform the search */
570 return ldb_next_request(ac->module, ac->mod_req);
573 static int oc_wait(struct ldb_handle *handle) {
574 struct oc_context *ac;
577 if (!handle || !handle->private_data) {
578 return LDB_ERR_OPERATIONS_ERROR;
581 if (handle->state == LDB_ASYNC_DONE) {
582 return handle->status;
585 handle->state = LDB_ASYNC_PENDING;
586 handle->status = LDB_SUCCESS;
588 ac = talloc_get_type(handle->private_data, struct oc_context);
592 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
594 if (ret != LDB_SUCCESS) {
595 handle->status = ret;
598 if (ac->down_req->handle->status != LDB_SUCCESS) {
599 handle->status = ac->down_req->handle->status;
603 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
607 /* mods done, go on */
608 return objectclass_search_self(handle);
611 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
613 if (ret != LDB_SUCCESS) {
614 handle->status = ret;
617 if (ac->search_req->handle->status != LDB_SUCCESS) {
618 handle->status = ac->search_req->handle->status;
622 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
626 /* self search done, go on */
627 return objectclass_do_mod(handle);
630 ret = ldb_wait(ac->mod_req->handle, LDB_WAIT_NONE);
632 if (ret != LDB_SUCCESS) {
633 handle->status = ret;
636 if (ac->mod_req->handle->status != LDB_SUCCESS) {
637 handle->status = ac->mod_req->handle->status;
641 if (ac->mod_req->handle->state != LDB_ASYNC_DONE) {
648 ret = LDB_ERR_OPERATIONS_ERROR;
655 handle->state = LDB_ASYNC_DONE;
659 static int oc_wait_all(struct ldb_handle *handle) {
663 while (handle->state != LDB_ASYNC_DONE) {
664 ret = oc_wait(handle);
665 if (ret != LDB_SUCCESS) {
670 return handle->status;
673 static int objectclass_wait(struct ldb_handle *handle, enum ldb_wait_type type)
675 if (type == LDB_WAIT_ALL) {
676 return oc_wait_all(handle);
678 return oc_wait(handle);
682 static const struct ldb_module_ops objectclass_ops = {
683 .name = "objectclass",
684 .add = objectclass_add,
685 .modify = objectclass_modify,
686 .wait = objectclass_wait
689 int ldb_objectclass_init(void)
691 return ldb_register_module(&objectclass_ops);