Fix use of wrong union arm in linked_attributes module
[Samba/eduardoll.git] / source4 / dsdb / samdb / ldb_modules / linked_attributes.c
blobbafd7e5ecf72e06dc9c8e56370e42537ad660480
1 /*
2 ldb database library
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
5 Copyright (C) Simo Sorce <idra@samba.org> 2008
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: ldb linked_attributes module
26 * Description: Module to ensure linked attribute pairs remain in sync
28 * Author: Andrew Bartlett
31 #include "includes.h"
32 #include "ldb/include/ldb.h"
33 #include "ldb/include/ldb_errors.h"
34 #include "ldb/include/ldb_private.h"
35 #include "dsdb/samdb/samdb.h"
37 struct la_op_store {
38 struct la_op_store *next;
39 enum la_op {LA_OP_ADD, LA_OP_DEL} op;
40 struct ldb_dn *dn;
41 char *name;
42 char *value;
45 struct replace_context {
46 struct la_context *ac;
47 unsigned int num_elements;
48 struct ldb_message_element *el;
51 struct la_context {
52 const struct dsdb_schema *schema;
53 struct ldb_module *module;
54 struct ldb_request *req;
56 struct replace_context *rc;
57 struct la_op_store *ops;
58 struct la_op_store *cur;
61 static struct la_context *linked_attributes_init(struct ldb_module *module,
62 struct ldb_request *req)
64 struct la_context *ac;
66 ac = talloc_zero(req, struct la_context);
67 if (ac == NULL) {
68 ldb_set_errstring(module->ldb, "Out of Memory");
69 return NULL;
72 ac->schema = dsdb_get_schema(module->ldb);
73 ac->module = module;
74 ac->req = req;
76 return ac;
79 /* Common routine to handle reading the attributes and creating a
80 * series of modify requests */
81 static int la_store_op(struct la_context *ac,
82 enum la_op op, char *dn,
83 const char *name, const char *value)
85 struct la_op_store *os, *tmp;
86 struct ldb_dn *op_dn;
88 op_dn = ldb_dn_new(ac, ac->module->ldb, dn);
89 if (!op_dn) {
90 return LDB_ERR_OPERATIONS_ERROR;
93 /* optimize out del - add operations that would end up
94 * with no changes */
95 if (ac->ops && op == LA_OP_DEL) {
96 /* do a linear search to find out if there is
97 * an equivalent add */
98 os = ac->ops;
99 while (os->next) {
101 tmp = os->next;
102 if (tmp->op == LA_OP_ADD) {
104 if ((strcmp(name, tmp->name) == 0) &&
105 (strcmp(value, tmp->value) == 0) &&
106 (ldb_dn_compare(op_dn, tmp->dn) == 0)) {
108 break;
111 os = os->next;
113 if (os->next) {
114 /* pair found, remove it and return */
115 os->next = tmp->next;
116 talloc_free(tmp);
117 talloc_free(op_dn);
118 return LDB_SUCCESS;
122 os = talloc_zero(ac, struct la_op_store);
123 if (!os) {
124 return LDB_ERR_OPERATIONS_ERROR;
127 os->op = op;
129 os->dn = talloc_steal(os, op_dn);
130 if (!os->dn) {
131 return LDB_ERR_OPERATIONS_ERROR;
134 os->name = talloc_strdup(os, name);
135 if (!os->name) {
136 return LDB_ERR_OPERATIONS_ERROR;
139 if ((op != LA_OP_DEL) && (value == NULL)) {
140 return LDB_ERR_OPERATIONS_ERROR;
142 if (value) {
143 os->value = talloc_strdup(os, value);
144 if (!os->value) {
145 return LDB_ERR_OPERATIONS_ERROR;
149 if (ac->ops) {
150 ac->cur->next = os;
151 } else {
152 ac->ops = os;
154 ac->cur = os;
156 return LDB_SUCCESS;
159 static int la_op_search_callback(struct ldb_request *req,
160 struct ldb_reply *ares);
161 static int la_do_mod_request(struct la_context *ac);
162 static int la_mod_callback(struct ldb_request *req,
163 struct ldb_reply *ares);
164 static int la_down_req(struct la_context *ac);
165 static int la_down_callback(struct ldb_request *req,
166 struct ldb_reply *ares);
170 /* add */
171 static int linked_attributes_add(struct ldb_module *module, struct ldb_request *req)
173 const struct dsdb_attribute *target_attr;
174 struct la_context *ac;
175 const char *attr_name;
176 const char *attr_val;
177 int ret;
178 int i, j;
180 if (ldb_dn_is_special(req->op.add.message->dn)) {
181 /* do not manipulate our control entries */
182 return ldb_next_request(module, req);
185 ac = linked_attributes_init(module, req);
186 if (!ac) {
187 return LDB_ERR_OPERATIONS_ERROR;
190 if (!ac->schema) {
191 /* without schema, this doesn't make any sense */
192 talloc_free(ac);
193 return ldb_next_request(module, req);
196 /* Need to ensure we only have forward links being specified */
197 for (i=0; i < req->op.add.message->num_elements; i++) {
198 const struct ldb_message_element *el = &req->op.add.message->elements[i];
199 const struct dsdb_attribute *schema_attr
200 = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
201 if (!schema_attr) {
202 ldb_asprintf_errstring(module->ldb,
203 "attribute %s is not a valid attribute in schema", el->name);
204 return LDB_ERR_OBJECT_CLASS_VIOLATION;
206 /* We have a valid attribute, now find out if it is linked */
207 if (schema_attr->linkID == 0) {
208 continue;
211 if ((schema_attr->linkID & 1) == 1) {
212 /* Odd is for the target. Illigal to modify */
213 ldb_asprintf_errstring(module->ldb,
214 "attribute %s must not be modified directly, it is a linked attribute", el->name);
215 return LDB_ERR_UNWILLING_TO_PERFORM;
218 /* Even link IDs are for the originating attribute */
219 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
220 if (!target_attr) {
222 * windows 2003 has a broken schema where
223 * the definition of msDS-IsDomainFor
224 * is missing (which is supposed to be
225 * the backlink of the msDS-HasDomainNCs
226 * attribute
228 continue;
231 attr_name = target_attr->lDAPDisplayName;
232 attr_val = ldb_dn_get_linearized(ac->req->op.add.message->dn);
234 for (j = 0; j < el->num_values; j++) {
235 ret = la_store_op(ac, LA_OP_ADD,
236 (char *)el->values[j].data,
237 attr_name, attr_val);
238 if (ret != LDB_SUCCESS) {
239 return ret;
244 /* if no linked attributes are present continue */
245 if (ac->ops == NULL) {
246 talloc_free(ac);
247 return ldb_next_request(module, req);
250 /* start with the first one */
251 return la_do_mod_request(ac);
254 static int la_mod_search_callback(struct ldb_request *req, struct ldb_reply *ares)
256 const struct dsdb_attribute *schema_attr;
257 const struct dsdb_attribute *target_attr;
258 struct ldb_message_element *search_el;
259 struct replace_context *rc;
260 struct la_context *ac;
261 const char *attr_name;
262 const char *dn;
263 int i, j;
264 int ret = LDB_SUCCESS;
266 ac = talloc_get_type(req->context, struct la_context);
267 rc = ac->rc;
269 if (!ares) {
270 return ldb_module_done(ac->req, NULL, NULL,
271 LDB_ERR_OPERATIONS_ERROR);
273 if (ares->error != LDB_SUCCESS) {
274 return ldb_module_done(ac->req, ares->controls,
275 ares->response, ares->error);
278 /* Only entries are interesting, and we only want the olddn */
279 switch (ares->type) {
280 case LDB_REPLY_ENTRY:
282 if (ldb_dn_compare(ares->message->dn, ac->req->op.mod.message->dn) != 0) {
283 /* Guh? We only asked for this DN */
284 ldb_oom(ac->module->ldb);
285 talloc_free(ares);
286 return ldb_module_done(ac->req, NULL, NULL,
287 LDB_ERR_OPERATIONS_ERROR);
290 dn = ldb_dn_get_linearized(ac->req->op.add.message->dn);
292 for (i = 0; i < rc->num_elements; i++) {
294 schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, rc->el[i].name);
295 if (!schema_attr) {
296 ldb_asprintf_errstring(ac->module->ldb,
297 "attribute %s is not a valid attribute in schema",
298 rc->el[i].name);
299 talloc_free(ares);
300 return ldb_module_done(ac->req, NULL, NULL,
301 LDB_ERR_OBJECT_CLASS_VIOLATION);
304 search_el = ldb_msg_find_element(ares->message,
305 rc->el[i].name);
307 /* See if this element already exists */
308 /* otherwise just ignore as
309 * the add has already been scheduled */
310 if ( ! search_el) {
311 continue;
314 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
315 if (!target_attr) {
317 * windows 2003 has a broken schema where
318 * the definition of msDS-IsDomainFor
319 * is missing (which is supposed to be
320 * the backlink of the msDS-HasDomainNCs
321 * attribute
323 continue;
325 attr_name = target_attr->lDAPDisplayName;
327 /* make sure we manage each value */
328 for (j = 0; j < search_el->num_values; j++) {
329 ret = la_store_op(ac, LA_OP_DEL,
330 (char *)search_el->values[j].data,
331 attr_name, dn);
332 if (ret != LDB_SUCCESS) {
333 talloc_free(ares);
334 return ldb_module_done(ac->req,
335 NULL, NULL, ret);
340 break;
342 case LDB_REPLY_REFERRAL:
343 /* ignore */
344 break;
346 case LDB_REPLY_DONE:
348 talloc_free(ares);
350 /* All mods set up, start with the first one */
351 ret = la_do_mod_request(ac);
352 if (ret != LDB_SUCCESS) {
353 return ldb_module_done(ac->req, NULL, NULL, ret);
355 return LDB_SUCCESS;
358 talloc_free(ares);
359 return ret;
363 /* modify */
364 static int linked_attributes_modify(struct ldb_module *module, struct ldb_request *req)
366 /* Look over list of modifications */
367 /* Find if any are for linked attributes */
368 /* Determine the effect of the modification */
369 /* Apply the modify to the linked entry */
371 int i, j;
372 struct la_context *ac;
373 struct ldb_request *search_req;
374 int ret;
376 if (ldb_dn_is_special(req->op.mod.message->dn)) {
377 /* do not manipulate our control entries */
378 return ldb_next_request(module, req);
381 ac = linked_attributes_init(module, req);
382 if (!ac) {
383 return LDB_ERR_OPERATIONS_ERROR;
386 if (!ac->schema) {
387 /* without schema, this doesn't make any sense */
388 return ldb_next_request(module, req);
391 ac->rc = NULL;
393 for (i=0; i < req->op.mod.message->num_elements; i++) {
394 bool store_el = false;
395 const char *attr_name;
396 const char *attr_val;
397 const struct dsdb_attribute *target_attr;
398 const struct ldb_message_element *el = &req->op.mod.message->elements[i];
399 const struct dsdb_attribute *schema_attr
400 = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
401 if (!schema_attr) {
402 ldb_asprintf_errstring(module->ldb,
403 "attribute %s is not a valid attribute in schema", el->name);
404 return LDB_ERR_OBJECT_CLASS_VIOLATION;
406 /* We have a valid attribute, now find out if it is linked */
407 if (schema_attr->linkID == 0) {
408 continue;
411 if ((schema_attr->linkID & 1) == 1) {
412 /* Odd is for the target. Illegal to modify */
413 ldb_asprintf_errstring(module->ldb,
414 "attribute %s must not be modified directly, it is a linked attribute", el->name);
415 return LDB_ERR_UNWILLING_TO_PERFORM;
418 /* Even link IDs are for the originating attribute */
420 /* Now find the target attribute */
421 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
422 if (!target_attr) {
424 * windows 2003 has a broken schema where
425 * the definition of msDS-IsDomainFor
426 * is missing (which is supposed to be
427 * the backlink of the msDS-HasDomainNCs
428 * attribute
430 continue;
433 attr_name = target_attr->lDAPDisplayName;
434 attr_val = ldb_dn_get_linearized(ac->req->op.mod.message->dn);
436 switch (el->flags & LDB_FLAG_MOD_MASK) {
437 case LDB_FLAG_MOD_REPLACE:
438 /* treat as just a normal add the delete part is handled by the callback */
439 store_el = true;
441 /* break intentionally missing */
443 case LDB_FLAG_MOD_ADD:
445 /* For each value being added, we need to setup the adds */
446 for (j = 0; j < el->num_values; j++) {
447 ret = la_store_op(ac, LA_OP_ADD,
448 (char *)el->values[j].data,
449 attr_name, attr_val);
450 if (ret != LDB_SUCCESS) {
451 return ret;
454 break;
456 case LDB_FLAG_MOD_DELETE:
458 if (el->num_values) {
459 /* For each value being deleted, we need to setup the delete */
460 for (j = 0; j < el->num_values; j++) {
461 ret = la_store_op(ac, LA_OP_DEL,
462 (char *)el->values[j].data,
463 attr_name, attr_val);
464 if (ret != LDB_SUCCESS) {
465 return ret;
468 } else {
469 /* Flag that there was a DELETE
470 * without a value specified, so we
471 * need to look for the old value */
472 store_el = true;
475 break;
478 if (store_el) {
479 struct ldb_message_element *search_el;
481 /* Fill out ac->rc only if we have to find the old values */
482 if (!ac->rc) {
483 ac->rc = talloc_zero(ac, struct replace_context);
484 if (!ac->rc) {
485 ldb_oom(module->ldb);
486 return LDB_ERR_OPERATIONS_ERROR;
490 search_el = talloc_realloc(ac->rc, ac->rc->el,
491 struct ldb_message_element,
492 ac->rc->num_elements +1);
493 if (!search_el) {
494 ldb_oom(module->ldb);
495 return LDB_ERR_OPERATIONS_ERROR;
497 ac->rc->el = search_el;
499 ac->rc->el[ac->rc->num_elements] = *el;
500 ac->rc->num_elements++;
504 /* both replace and delete without values are handled in the callback
505 * after the search on the entry to be modified is performed */
507 /* Only bother doing a search of this entry (to find old
508 * values) if replace or delete operations are attempted */
509 if (ac->rc) {
510 const char **attrs;
512 attrs = talloc_array(ac->rc, const char *, ac->rc->num_elements +1);
513 if (!attrs) {
514 ldb_oom(module->ldb);
515 return LDB_ERR_OPERATIONS_ERROR;
517 for (i = 0; i < ac->rc->num_elements; i++) {
518 attrs[i] = ac->rc->el[i].name;
520 attrs[i] = NULL;
522 /* The callback does all the hard work here */
523 ret = ldb_build_search_req(&search_req, module->ldb, ac,
524 req->op.mod.message->dn,
525 LDB_SCOPE_BASE,
526 "(objectClass=*)", attrs,
527 NULL,
528 ac, la_mod_search_callback,
529 req);
531 if (ret == LDB_SUCCESS) {
532 talloc_steal(search_req, attrs);
534 ret = ldb_next_request(module, search_req);
538 } else {
539 if (ac->ops) {
540 /* Jump directly to handling the modifies */
541 ret = la_do_mod_request(ac);
542 } else {
543 /* nothing to do for this module, proceed */
544 talloc_free(ac);
545 ret = ldb_next_request(module, req);
549 return ret;
552 /* delete, rename */
553 static int linked_attributes_op(struct ldb_module *module, struct ldb_request *req)
555 struct ldb_request *search_req;
556 struct ldb_dn *base_dn;
557 struct la_context *ac;
558 const char **attrs;
559 WERROR werr;
560 int ret;
562 /* This gets complex: We need to:
563 - Do a search for the entry
564 - Wait for these result to appear
565 - In the callback for the result, issue a modify
566 request based on the linked attributes found
567 - Wait for each modify result
568 - Regain our sainity
571 switch (req->operation) {
572 case LDB_RENAME:
573 base_dn = req->op.rename.olddn;
574 break;
575 case LDB_DELETE:
576 base_dn = req->op.del.dn;
577 break;
578 default:
579 return LDB_ERR_OPERATIONS_ERROR;
582 ac = linked_attributes_init(module, req);
583 if (!ac) {
584 return LDB_ERR_OPERATIONS_ERROR;
587 if (!ac->schema) {
588 /* without schema, this doesn't make any sense */
589 return ldb_next_request(module, req);
592 werr = dsdb_linked_attribute_lDAPDisplayName_list(ac->schema, ac, &attrs);
593 if (!W_ERROR_IS_OK(werr)) {
594 return LDB_ERR_OPERATIONS_ERROR;
597 ret = ldb_build_search_req(&search_req, module->ldb, req,
598 base_dn, LDB_SCOPE_BASE,
599 "(objectClass=*)", attrs,
600 NULL,
601 ac, la_op_search_callback,
602 req);
604 if (ret != LDB_SUCCESS) {
605 return ret;
608 talloc_steal(search_req, attrs);
610 return ldb_next_request(module, search_req);
613 static int la_op_search_callback(struct ldb_request *req,
614 struct ldb_reply *ares)
616 struct la_context *ac;
617 const struct dsdb_attribute *schema_attr;
618 const struct dsdb_attribute *target_attr;
619 const struct ldb_message_element *el;
620 const char *attr_name;
621 const char *deldn;
622 const char *adddn;
623 int i, j;
624 int ret;
626 ac = talloc_get_type(req->context, struct la_context);
628 if (!ares) {
629 return ldb_module_done(ac->req, NULL, NULL,
630 LDB_ERR_OPERATIONS_ERROR);
632 if (ares->error != LDB_SUCCESS) {
633 return ldb_module_done(ac->req, ares->controls,
634 ares->response, ares->error);
637 /* Only entries are interesting, and we only want the olddn */
638 switch (ares->type) {
639 case LDB_REPLY_ENTRY:
640 ret = ldb_dn_compare(ares->message->dn, req->op.search.base);
641 if (ret != 0) {
642 /* Guh? We only asked for this DN */
643 talloc_free(ares);
644 return ldb_module_done(ac->req, NULL, NULL,
645 LDB_ERR_OPERATIONS_ERROR);
647 if (ares->message->num_elements == 0) {
648 /* only bother at all if there were some
649 * linked attributes found */
650 talloc_free(ares);
651 return LDB_SUCCESS;
654 switch (ac->req->operation) {
655 case LDB_DELETE:
656 deldn = ldb_dn_get_linearized(ac->req->op.del.dn);
657 adddn = NULL;
658 break;
659 case LDB_RENAME:
660 deldn = ldb_dn_get_linearized(ac->req->op.rename.olddn);
661 adddn = ldb_dn_get_linearized(ac->req->op.rename.newdn);
662 break;
663 default:
664 talloc_free(ares);
665 return ldb_module_done(ac->req, NULL, NULL,
666 LDB_ERR_OPERATIONS_ERROR);
669 for (i = 0; i < ares->message->num_elements; i++) {
670 el = &ares->message->elements[i];
672 schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
673 if (!schema_attr) {
674 ldb_asprintf_errstring(ac->module->ldb,
675 "attribute %s is not a valid attribute"
676 " in schema", el->name);
677 talloc_free(ares);
678 return ldb_module_done(ac->req, NULL, NULL,
679 LDB_ERR_OBJECT_CLASS_VIOLATION);
682 /* Valid attribute, now find out if it is linked */
683 if (schema_attr->linkID == 0) {
684 /* Not a linked attribute, skip */
685 continue;
688 if ((schema_attr->linkID & 1) == 0) {
689 /* Odd is for the target. */
690 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
691 if (!target_attr) {
692 continue;
694 attr_name = target_attr->lDAPDisplayName;
695 } else {
696 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID - 1);
697 if (!target_attr) {
698 continue;
700 attr_name = target_attr->lDAPDisplayName;
702 for (j = 0; j < el->num_values; j++) {
703 ret = la_store_op(ac, LA_OP_DEL,
704 (char *)el->values[j].data,
705 attr_name, deldn);
706 if (ret != LDB_SUCCESS) {
707 talloc_free(ares);
708 return ldb_module_done(ac->req,
709 NULL, NULL, ret);
711 if (!adddn) continue;
712 ret = la_store_op(ac, LA_OP_ADD,
713 (char *)el->values[j].data,
714 attr_name, adddn);
715 if (ret != LDB_SUCCESS) {
716 talloc_free(ares);
717 return ldb_module_done(ac->req,
718 NULL, NULL, ret);
723 break;
725 case LDB_REPLY_REFERRAL:
726 /* ignore */
727 break;
729 case LDB_REPLY_DONE:
731 talloc_free(ares);
733 if (ac->ops) {
734 /* start the mod requests chain */
735 ret = la_do_mod_request(ac);
736 } else {
737 ret = la_down_req(ac);
739 if (ret != LDB_SUCCESS) {
740 return ldb_module_done(ac->req, NULL, NULL, ret);
742 return LDB_SUCCESS;
745 talloc_free(ares);
746 return LDB_SUCCESS;
749 /* do a linked attributes modify request */
750 static int la_do_mod_request(struct la_context *ac)
752 struct ldb_message_element *ret_el;
753 struct ldb_request *mod_req;
754 struct ldb_message *new_msg;
755 struct ldb_context *ldb;
756 int ret;
758 ldb = ac->module->ldb;
760 /* Create the modify request */
761 new_msg = ldb_msg_new(ac);
762 if (!new_msg) {
763 ldb_oom(ldb);
764 return LDB_ERR_OPERATIONS_ERROR;
766 new_msg->dn = ldb_dn_copy(new_msg, ac->ops->dn);
767 if (!new_msg->dn) {
768 return LDB_ERR_OPERATIONS_ERROR;
771 if (ac->ops->op == LA_OP_ADD) {
772 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
773 LDB_FLAG_MOD_ADD, &ret_el);
774 } else {
775 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
776 LDB_FLAG_MOD_DELETE, &ret_el);
778 if (ret != LDB_SUCCESS) {
779 return ret;
781 ret_el->values = talloc_array(new_msg, struct ldb_val, 1);
782 if (!ret_el->values) {
783 ldb_oom(ldb);
784 return LDB_ERR_OPERATIONS_ERROR;
786 ret_el->values[0] = data_blob_string_const(ac->ops->value);
787 ret_el->num_values = 1;
789 /* use ac->ops as the mem_ctx so that the request will be freed
790 * in the callback as soon as completed */
791 ret = ldb_build_mod_req(&mod_req, ldb, ac->ops,
792 new_msg,
793 NULL,
794 ac, la_mod_callback,
795 ac->req);
796 if (ret != LDB_SUCCESS) {
797 return ret;
799 talloc_steal(mod_req, new_msg);
801 /* Run the new request */
802 return ldb_next_request(ac->module, mod_req);
805 static int la_mod_callback(struct ldb_request *req, struct ldb_reply *ares)
807 struct la_context *ac;
808 struct la_op_store *os;
809 int ret;
811 ac = talloc_get_type(req->context, struct la_context);
813 if (!ares) {
814 return ldb_module_done(ac->req, NULL, NULL,
815 LDB_ERR_OPERATIONS_ERROR);
817 if (ares->error != LDB_SUCCESS) {
818 return ldb_module_done(ac->req, ares->controls,
819 ares->response, ares->error);
822 if (ares->type != LDB_REPLY_DONE) {
823 ldb_set_errstring(ac->module->ldb,
824 "invalid ldb_reply_type in callback");
825 talloc_free(ares);
826 return ldb_module_done(ac->req, NULL, NULL,
827 LDB_ERR_OPERATIONS_ERROR);
830 talloc_free(ares);
832 if (ac->ops) {
833 os = ac->ops;
834 ac->ops = os->next;
836 /* this frees the request too
837 * DO NOT access 'req' after this point */
838 talloc_free(os);
841 /* as last op run the original request */
842 if (ac->ops) {
843 ret = la_do_mod_request(ac);
844 } else {
845 ret = la_down_req(ac);
848 if (ret != LDB_SUCCESS) {
849 return ldb_module_done(ac->req, NULL, NULL, ret);
851 return LDB_SUCCESS;
854 static int la_down_req(struct la_context *ac)
856 struct ldb_request *down_req;
857 int ret;
859 switch (ac->req->operation) {
860 case LDB_ADD:
861 ret = ldb_build_add_req(&down_req, ac->module->ldb, ac,
862 ac->req->op.add.message,
863 ac->req->controls,
864 ac, la_down_callback,
865 ac->req);
866 break;
867 case LDB_MODIFY:
868 ret = ldb_build_mod_req(&down_req, ac->module->ldb, ac,
869 ac->req->op.mod.message,
870 ac->req->controls,
871 ac, la_down_callback,
872 ac->req);
873 break;
874 case LDB_DELETE:
875 ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
876 ac->req->op.del.dn,
877 ac->req->controls,
878 ac, la_down_callback,
879 ac->req);
880 break;
881 case LDB_RENAME:
882 ret = ldb_build_rename_req(&down_req, ac->module->ldb, ac,
883 ac->req->op.rename.olddn,
884 ac->req->op.rename.newdn,
885 ac->req->controls,
886 ac, la_down_callback,
887 ac->req);
888 break;
889 default:
890 ret = LDB_ERR_OPERATIONS_ERROR;
892 if (ret != LDB_SUCCESS) {
893 return ret;
896 return ldb_next_request(ac->module, down_req);
899 static int la_down_callback(struct ldb_request *req, struct ldb_reply *ares)
901 struct la_context *ac;
903 ac = talloc_get_type(req->context, struct la_context);
905 if (!ares) {
906 return ldb_module_done(ac->req, NULL, NULL,
907 LDB_ERR_OPERATIONS_ERROR);
909 if (ares->error != LDB_SUCCESS) {
910 return ldb_module_done(ac->req, ares->controls,
911 ares->response, ares->error);
914 if (ares->type != LDB_REPLY_DONE) {
915 ldb_set_errstring(ac->module->ldb,
916 "invalid ldb_reply_type in callback");
917 talloc_free(ares);
918 return ldb_module_done(ac->req, NULL, NULL,
919 LDB_ERR_OPERATIONS_ERROR);
922 return ldb_module_done(ac->req, ares->controls,
923 ares->response, ares->error);
926 _PUBLIC_ const struct ldb_module_ops ldb_linked_attributes_module_ops = {
927 .name = "linked_attributes",
928 .add = linked_attributes_add,
929 .modify = linked_attributes_modify,
930 .del = linked_attributes_op,
931 .rename = linked_attributes_op,