s4-dsdb: use ldb_operr() in the dsdb code
[samba.git] / source4 / dsdb / samdb / ldb_modules / linked_attributes.c
index dd199c0137522f803efcc728d2c5fca50321c610..67ae236fb190cf1074c5182f540200ad005e5e81 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
    ldb database library
 
    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
@@ -8,12 +8,12 @@
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
  */
 
 #include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_errors.h"
-#include "ldb/include/ldb_private.h"
+#include "ldb_module.h"
+#include "util/dlinklist.h"
 #include "dsdb/samdb/samdb.h"
+#include "librpc/gen_ndr/ndr_misc.h"
+#include "dsdb/samdb/ldb_modules/util.h"
+
+struct la_private {
+       struct la_context *la_list;
+};
 
 struct la_op_store {
        struct la_op_store *next;
+       struct la_op_store *prev;
        enum la_op {LA_OP_ADD, LA_OP_DEL} op;
-       struct ldb_dn *dn;
+       struct GUID guid;
        char *name;
        char *value;
 };
@@ -49,144 +55,159 @@ struct replace_context {
 };
 
 struct la_context {
+       struct la_context *next, *prev;
        const struct dsdb_schema *schema;
        struct ldb_module *module;
        struct ldb_request *req;
-
+       struct ldb_dn *add_dn;
+       struct ldb_dn *del_dn;
        struct replace_context *rc;
        struct la_op_store *ops;
-       struct la_op_store *cur;
+       struct ldb_extended *op_response;
+       struct ldb_control **op_controls;
 };
 
 static struct la_context *linked_attributes_init(struct ldb_module *module,
                                                 struct ldb_request *req)
 {
+       struct ldb_context *ldb;
        struct la_context *ac;
 
+       ldb = ldb_module_get_ctx(module);
+
        ac = talloc_zero(req, struct la_context);
        if (ac == NULL) {
-               ldb_set_errstring(module->ldb, "Out of Memory");
+               ldb_oom(ldb);
                return NULL;
        }
 
-       ac->schema = dsdb_get_schema(module->ldb);
+       ac->schema = dsdb_get_schema(ldb, ac);
        ac->module = module;
        ac->req = req;
 
        return ac;
 }
 
+/*
+  turn a DN into a GUID
+ */
+static int la_guid_from_dn(struct la_context *ac, struct ldb_dn *dn, struct GUID *guid)
+{
+       NTSTATUS status;
+       int ret;
+
+       status = dsdb_get_extended_dn_guid(dn, guid, "GUID");
+       if (NT_STATUS_IS_OK(status)) {
+               return LDB_SUCCESS;
+       }
+       if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
+               DEBUG(4,(__location__ ": Unable to parse GUID for dn %s\n",
+                        ldb_dn_get_linearized(dn)));
+               return ldb_operr(ldb_module_get_ctx(ac->module));
+       }
+
+       ret = dsdb_find_guid_by_dn(ldb_module_get_ctx(ac->module), dn, guid);
+       if (ret != LDB_SUCCESS) {
+               DEBUG(4,(__location__ ": Failed to find GUID for dn %s\n",
+                        ldb_dn_get_linearized(dn)));
+               return ret;
+       }
+       return LDB_SUCCESS;
+}
+
+
 /* Common routine to handle reading the attributes and creating a
  * series of modify requests */
 static int la_store_op(struct la_context *ac,
                       enum la_op op, struct ldb_val *dn,
-                       const char *name, const char *value)
+                      const char *name)
 {
-       struct la_op_store *os, *tmp;
+       struct ldb_context *ldb;
+       struct la_op_store *os;
        struct ldb_dn *op_dn;
+       int ret;
+
+       ldb = ldb_module_get_ctx(ac->module);
 
-       op_dn = ldb_dn_from_ldb_val(ac, ac->module->ldb, dn);
+       op_dn = ldb_dn_from_ldb_val(ac, ldb, dn);
        if (!op_dn) {
-               ldb_asprintf_errstring(ac->module->ldb, 
+               ldb_asprintf_errstring(ldb,
                                       "could not parse attribute as a DN");
                return LDB_ERR_INVALID_DN_SYNTAX;
        }
 
-       /* optimize out del - add operations that would end up
-        * with no changes */
-       if (ac->ops && op == LA_OP_DEL) {
-               /* do a linear search to find out if there is
-                * an equivalent add */
-               os = ac->ops;
-               while (os->next) {
-
-                       tmp = os->next;
-                       if (tmp->op == LA_OP_ADD) {
-
-                               if ((strcmp(name, tmp->name) == 0) &&
-                                   (strcmp(value, tmp->value) == 0) &&
-                                   (ldb_dn_compare(op_dn, tmp->dn) == 0)) {
-
-                                       break;
-                               }
-                       }
-                       os = os->next;
-               }
-               if (os->next) {
-                       /* pair found, remove it and return */
-                       os->next = tmp->next;
-                       talloc_free(tmp);
-                       talloc_free(op_dn);
-                       return LDB_SUCCESS;
-               }
-       }
-
        os = talloc_zero(ac, struct la_op_store);
        if (!os) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        os->op = op;
 
-       os->dn = talloc_steal(os, op_dn);
-       if (!os->dn) {
-               return LDB_ERR_OPERATIONS_ERROR;
+       ret = la_guid_from_dn(ac, op_dn, &os->guid);
+       talloc_free(op_dn);
+       if (ret == LDB_ERR_NO_SUCH_OBJECT && ac->req->operation == LDB_DELETE) {
+               /* we are deleting an object, and we've found it has a
+                * forward link to a target that no longer
+                * exists. This is not an error in the delete, and we
+                * should just not do the deferred delete of the
+                * target attribute
+                */
+               talloc_free(os);
+               return LDB_SUCCESS;
+       }
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
 
        os->name = talloc_strdup(os, name);
        if (!os->name) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
-       if ((op != LA_OP_DEL) && (value == NULL)) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       if (value) {
-               os->value = talloc_strdup(os, value);
-               if (!os->value) {
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
-       }
-
-       if (ac->ops) {
-               ac->cur->next = os;
+       /* Do deletes before adds */
+       if (op == LA_OP_ADD) {
+               DLIST_ADD_END(ac->ops, os, struct la_op_store *);
        } else {
-               ac->ops = os;
+               /* By adding to the head of the list, we do deletes before
+                * adds when processing a replace */
+               DLIST_ADD(ac->ops, os);
        }
-       ac->cur = os;
 
        return LDB_SUCCESS;
 }
 
-static int la_op_search_callback(struct ldb_request *req,
-                                struct ldb_reply *ares);
-static int la_do_mod_request(struct la_context *ac);
-static int la_mod_callback(struct ldb_request *req,
-                          struct ldb_reply *ares);
+static int la_queue_mod_request(struct la_context *ac);
 static int la_down_req(struct la_context *ac);
-static int la_down_callback(struct ldb_request *req,
-                           struct ldb_reply *ares);
 
 
 
 /* add */
 static int linked_attributes_add(struct ldb_module *module, struct ldb_request *req)
 {
+       struct ldb_context *ldb;
        const struct dsdb_attribute *target_attr;
        struct la_context *ac;
        const char *attr_name;
-       const char *attr_val;
+       struct ldb_control *ctrl;
+       unsigned int i, j;
        int ret;
-       int i, j;
+
+       ldb = ldb_module_get_ctx(module);
 
        if (ldb_dn_is_special(req->op.add.message->dn)) {
                /* do not manipulate our control entries */
                return ldb_next_request(module, req);
        }
 
+       if (!(ctrl = ldb_request_get_control(req, DSDB_CONTROL_APPLY_LINKS))) {
+               /* don't do anything special for linked attributes, repl_meta_data has done it */
+               return ldb_next_request(module, req);
+       }
+       ctrl->critical = false;
+
        ac = linked_attributes_init(module, req);
        if (!ac) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        if (!ac->schema) {
@@ -201,22 +222,22 @@ static int linked_attributes_add(struct ldb_module *module, struct ldb_request *
                const struct dsdb_attribute *schema_attr
                        = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
                if (!schema_attr) {
-                       ldb_asprintf_errstring(module->ldb, 
+                       ldb_asprintf_errstring(ldb,
                                               "attribute %s is not a valid attribute in schema", el->name);
-                       return LDB_ERR_OBJECT_CLASS_VIOLATION;                  
+                       return LDB_ERR_OBJECT_CLASS_VIOLATION;
                }
-               /* We have a valid attribute, now find out if it is linked */
-               if (schema_attr->linkID == 0) {
+               /* We have a valid attribute, now find out if it is a forward link */
+               if ((schema_attr->linkID == 0)) {
                        continue;
                }
-               
+
                if ((schema_attr->linkID & 1) == 1) {
-                       /* Odd is for the target.  Illigal to modify */
-                       ldb_asprintf_errstring(module->ldb, 
-                                              "attribute %s must not be modified directly, it is a linked attribute", el->name);
-                       return LDB_ERR_UNWILLING_TO_PERFORM;
+                       unsigned int functional_level;
+
+                       functional_level = dsdb_functional_level(ldb);
+                       SMB_ASSERT(functional_level > DS_DOMAIN_FUNCTION_2000);
                }
-               
+
                /* Even link IDs are for the originating attribute */
                target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
                if (!target_attr) {
@@ -231,12 +252,11 @@ static int linked_attributes_add(struct ldb_module *module, struct ldb_request *
                }
 
                attr_name = target_attr->lDAPDisplayName;
-               attr_val = ldb_dn_get_linearized(ac->req->op.add.message->dn);
 
                for (j = 0; j < el->num_values; j++) {
                        ret = la_store_op(ac, LA_OP_ADD,
                                          &el->values[j],
-                                         attr_name, attr_val);
+                                         attr_name);
                        if (ret != LDB_SUCCESS) {
                                return ret;
                        }
@@ -245,27 +265,33 @@ static int linked_attributes_add(struct ldb_module *module, struct ldb_request *
 
        /* if no linked attributes are present continue */
        if (ac->ops == NULL) {
+               /* nothing to do for this module, proceed */
                talloc_free(ac);
                return ldb_next_request(module, req);
        }
 
-       /* start with the first one */
-       return la_do_mod_request(ac);
+       /* start with the original request */
+       return la_down_req(ac);
 }
 
+/* For a delete or rename, we need to find out what linked attributes
+ * are currently on this DN, and then deal with them.  This is the
+ * callback to the base search */
+
 static int la_mod_search_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
+       struct ldb_context *ldb;
        const struct dsdb_attribute *schema_attr;
        const struct dsdb_attribute *target_attr;
        struct ldb_message_element *search_el;
        struct replace_context *rc;
        struct la_context *ac;
        const char *attr_name;
-       const char *dn;
-       int i, j;
+       unsigned int i, j;
        int ret = LDB_SUCCESS;
 
        ac = talloc_get_type(req->context, struct la_context);
+       ldb = ldb_module_get_ctx(ac->module);
        rc = ac->rc;
 
        if (!ares) {
@@ -282,20 +308,23 @@ static int la_mod_search_callback(struct ldb_request *req, struct ldb_reply *are
        case LDB_REPLY_ENTRY:
 
                if (ldb_dn_compare(ares->message->dn, ac->req->op.mod.message->dn) != 0) {
+                       ldb_asprintf_errstring(ldb,
+                                              "linked_attributes: %s is not the DN we were looking for",
+                                              ldb_dn_get_linearized(ares->message->dn));
                        /* Guh?  We only asked for this DN */
-                       ldb_oom(ac->module->ldb);
                        talloc_free(ares);
                        return ldb_module_done(ac->req, NULL, NULL,
                                                LDB_ERR_OPERATIONS_ERROR);
                }
 
-               dn = ldb_dn_get_linearized(ac->req->op.add.message->dn);
+               ac->add_dn = ac->del_dn = talloc_steal(ac, ares->message->dn);
 
-               for (i = 0; i < rc->num_elements; i++) {
+               /* We don't populate 'rc' for ADD - it can't be deleting elements anyway */
+               for (i = 0; rc && i < rc->num_elements; i++) {
 
                        schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, rc->el[i].name);
                        if (!schema_attr) {
-                               ldb_asprintf_errstring(ac->module->ldb,
+                               ldb_asprintf_errstring(ldb,
                                        "attribute %s is not a valid attribute in schema",
                                        rc->el[i].name);
                                talloc_free(ares);
@@ -326,11 +355,11 @@ static int la_mod_search_callback(struct ldb_request *req, struct ldb_reply *are
                        }
                        attr_name = target_attr->lDAPDisplayName;
 
-                       /* make sure we manage each value */
+                       /* Now we know what was there, we can remove it for the re-add */
                        for (j = 0; j < search_el->num_values; j++) {
                                ret = la_store_op(ac, LA_OP_DEL,
                                                  &search_el->values[j],
-                                                 attr_name, dn);
+                                                 attr_name);
                                if (ret != LDB_SUCCESS) {
                                        talloc_free(ares);
                                        return ldb_module_done(ac->req,
@@ -349,10 +378,20 @@ static int la_mod_search_callback(struct ldb_request *req, struct ldb_reply *are
 
                talloc_free(ares);
 
-               /* All mods set up, start with the first one */
-               ret = la_do_mod_request(ac);
-               if (ret != LDB_SUCCESS) {
-                       return ldb_module_done(ac->req, NULL, NULL, ret);
+               if (ac->req->operation == LDB_ADD) {
+                       /* Start the modifies to the backlinks */
+                       ret = la_queue_mod_request(ac);
+
+                       if (ret != LDB_SUCCESS) {
+                               return ldb_module_done(ac->req, NULL, NULL,
+                                                      ret);
+                       }
+               } else {
+                       /* Start with the original request */
+                       ret = la_down_req(ac);
+                       if (ret != LDB_SUCCESS) {
+                               return ldb_module_done(ac->req, NULL, NULL, ret);
+                       }
                }
                return LDB_SUCCESS;
        }
@@ -370,19 +409,30 @@ static int linked_attributes_modify(struct ldb_module *module, struct ldb_reques
        /* Determine the effect of the modification */
        /* Apply the modify to the linked entry */
 
-       int i, j;
+       struct ldb_context *ldb;
+       unsigned int i, j;
        struct la_context *ac;
        struct ldb_request *search_req;
+       const char **attrs;
+       struct ldb_control *ctrl;
        int ret;
 
+       ldb = ldb_module_get_ctx(module);
+
        if (ldb_dn_is_special(req->op.mod.message->dn)) {
                /* do not manipulate our control entries */
                return ldb_next_request(module, req);
        }
 
+       if (!(ctrl = ldb_request_get_control(req, DSDB_CONTROL_APPLY_LINKS))) {
+               /* don't do anything special for linked attributes, repl_meta_data has done it */
+               return ldb_next_request(module, req);
+       }
+       ctrl->critical = false;
+
        ac = linked_attributes_init(module, req);
        if (!ac) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb);
        }
 
        if (!ac->schema) {
@@ -390,35 +440,35 @@ static int linked_attributes_modify(struct ldb_module *module, struct ldb_reques
                return ldb_next_request(module, req);
        }
 
-       ac->rc = NULL;
+       ac->rc = talloc_zero(ac, struct replace_context);
+       if (!ac->rc) {
+               return ldb_oom(ldb);
+       }
 
        for (i=0; i < req->op.mod.message->num_elements; i++) {
                bool store_el = false;
                const char *attr_name;
-               const char *attr_val;
                const struct dsdb_attribute *target_attr;
                const struct ldb_message_element *el = &req->op.mod.message->elements[i];
                const struct dsdb_attribute *schema_attr
                        = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
                if (!schema_attr) {
-                       ldb_asprintf_errstring(module->ldb, 
+                       ldb_asprintf_errstring(ldb,
                                               "attribute %s is not a valid attribute in schema", el->name);
-                       return LDB_ERR_OBJECT_CLASS_VIOLATION;                  
+                       return LDB_ERR_OBJECT_CLASS_VIOLATION;
                }
-               /* We have a valid attribute, now find out if it is linked */
+               /* We have a valid attribute, now find out if it is a forward link
+                  (Even link IDs are for the originating attribute) */
                if (schema_attr->linkID == 0) {
                        continue;
                }
-               
+
                if ((schema_attr->linkID & 1) == 1) {
-                       /* Odd is for the target.  Illegal to modify */
-                       ldb_asprintf_errstring(module->ldb, 
-                                              "attribute %s must not be modified directly, it is a linked attribute", el->name);
-                       return LDB_ERR_UNWILLING_TO_PERFORM;
+                       unsigned int functional_level;
+
+                       functional_level = dsdb_functional_level(ldb);
+                       SMB_ASSERT(functional_level > DS_DOMAIN_FUNCTION_2000);
                }
-               
-               /* Even link IDs are for the originating attribute */
-               
                /* Now find the target attribute */
                target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
                if (!target_attr) {
@@ -433,7 +483,6 @@ static int linked_attributes_modify(struct ldb_module *module, struct ldb_reques
                }
 
                attr_name = target_attr->lDAPDisplayName;
-               attr_val = ldb_dn_get_linearized(ac->req->op.mod.message->dn);
 
                switch (el->flags & LDB_FLAG_MOD_MASK) {
                case LDB_FLAG_MOD_REPLACE:
@@ -448,7 +497,7 @@ static int linked_attributes_modify(struct ldb_module *module, struct ldb_reques
                        for (j = 0; j < el->num_values; j++) {
                                ret = la_store_op(ac, LA_OP_ADD,
                                                  &el->values[j],
-                                                 attr_name, attr_val);
+                                                 attr_name);
                                if (ret != LDB_SUCCESS) {
                                        return ret;
                                }
@@ -462,7 +511,7 @@ static int linked_attributes_modify(struct ldb_module *module, struct ldb_reques
                                for (j = 0; j < el->num_values; j++) {
                                        ret = la_store_op(ac, LA_OP_DEL,
                                                          &el->values[j],
-                                                         attr_name, attr_val);
+                                                         attr_name);
                                        if (ret != LDB_SUCCESS) {
                                                return ret;
                                        }
@@ -480,21 +529,11 @@ static int linked_attributes_modify(struct ldb_module *module, struct ldb_reques
                if (store_el) {
                        struct ldb_message_element *search_el;
 
-                       /* Fill out ac->rc only if we have to find the old values */
-                       if (!ac->rc) {
-                               ac->rc = talloc_zero(ac, struct replace_context);
-                               if (!ac->rc) {
-                                       ldb_oom(module->ldb);
-                                       return LDB_ERR_OPERATIONS_ERROR;
-                               }
-                       }
-
                        search_el = talloc_realloc(ac->rc, ac->rc->el,
                                                   struct ldb_message_element,
                                                   ac->rc->num_elements +1);
                        if (!search_el) {
-                               ldb_oom(module->ldb);
-                               return LDB_ERR_OPERATIONS_ERROR;
+                               return ldb_oom(ldb);
                        }
                        ac->rc->el = search_el;
 
@@ -503,26 +542,21 @@ static int linked_attributes_modify(struct ldb_module *module, struct ldb_reques
                }
        }
 
-       /* both replace and delete without values are handled in the callback
-        * after the search on the entry to be modified is performed */
+       if (ac->ops || ac->rc->el) {
+               /* both replace and delete without values are handled in the callback
+                * after the search on the entry to be modified is performed */
 
-       /* Only bother doing a search of this entry (to find old
-        * values) if replace or delete operations are attempted */
-       if (ac->rc) {
-               const char **attrs;
-
-               attrs = talloc_array(ac->rc, const char *, ac->rc->num_elements +1);
+               attrs = talloc_array(ac->rc, const char *, ac->rc->num_elements + 1);
                if (!attrs) {
-                       ldb_oom(module->ldb);
-                       return LDB_ERR_OPERATIONS_ERROR;
+                       return ldb_oom(ldb);
                }
-               for (i = 0; i < ac->rc->num_elements; i++) {
+               for (i = 0; ac->rc && i < ac->rc->num_elements; i++) {
                        attrs[i] = ac->rc->el[i].name;
                }
                attrs[i] = NULL;
 
                /* The callback does all the hard work here */
-               ret = ldb_build_search_req(&search_req, module->ldb, ac,
+               ret = ldb_build_search_req(&search_req, ldb, ac,
                                           req->op.mod.message->dn,
                                           LDB_SCOPE_BASE,
                                           "(objectClass=*)", attrs,
@@ -530,287 +564,261 @@ static int linked_attributes_modify(struct ldb_module *module, struct ldb_reques
                                           ac, la_mod_search_callback,
                                           req);
 
+               /* We need to figure out our own extended DN, to fill in as the backlink target */
+               if (ret == LDB_SUCCESS) {
+                       ret = ldb_request_add_control(search_req,
+                                                     LDB_CONTROL_EXTENDED_DN_OID,
+                                                     false, NULL);
+               }
                if (ret == LDB_SUCCESS) {
                        talloc_steal(search_req, attrs);
 
                        ret = ldb_next_request(module, search_req);
                }
 
-               
        } else {
-               if (ac->ops) {
-                       /* Jump directly to handling the modifies */
-                       ret = la_do_mod_request(ac);
-               } else {
-                       /* nothing to do for this module, proceed */
-                       talloc_free(ac);
-                       ret = ldb_next_request(module, req);
-               }
+               /* nothing to do for this module, proceed */
+               talloc_free(ac);
+               ret = ldb_next_request(module, req);
        }
 
        return ret;
 }
 
-/* delete, rename */
-static int linked_attributes_op(struct ldb_module *module, struct ldb_request *req)
+static int linked_attributes_fix_links(struct ldb_module *module,
+                                      struct ldb_dn *old_dn, struct ldb_dn *new_dn,
+                                      struct ldb_message_element *el, struct dsdb_schema *schema,
+                                      const struct dsdb_attribute *schema_attr)
 {
-       struct ldb_request *search_req;
-       struct ldb_dn *base_dn;
-       struct la_context *ac;
-       const char **attrs;
-       WERROR werr;
+       unsigned int i, j;
+       TALLOC_CTX *tmp_ctx = talloc_new(module);
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       const struct dsdb_attribute *target;
+       const char *attrs[2];
        int ret;
 
-       /* This gets complex:  We need to:
-          - Do a search for the entry
-          - Wait for these result to appear
-          - In the callback for the result, issue a modify
-               request based on the linked attributes found
-          - Wait for each modify result
-          - Regain our sainity
-       */
-
-       switch (req->operation) {
-       case LDB_RENAME:
-               base_dn = req->op.rename.olddn;
-               break;
-       case LDB_DELETE:
-               base_dn = req->op.del.dn;
-               break;
-       default:
-               return LDB_ERR_OPERATIONS_ERROR;
+       target = dsdb_attribute_by_linkID(schema, schema_attr->linkID ^ 1);
+       if (target == NULL) {
+               /* there is no counterpart link to change */
+               return LDB_SUCCESS;
        }
 
-       ac = linked_attributes_init(module, req);
-       if (!ac) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
+       attrs[0] = target->lDAPDisplayName;
+       attrs[1] = NULL;
 
-       if (!ac->schema) {
-               /* without schema, this doesn't make any sense */
-               return ldb_next_request(module, req);
-       }
+       for (i=0; i<el->num_values; i++) {
+               struct dsdb_dn *dsdb_dn;
+               struct ldb_result *res;
+               struct ldb_message *msg;
+               struct ldb_message_element *el2;
 
-       werr = dsdb_linked_attribute_lDAPDisplayName_list(ac->schema, ac, &attrs);
-       if (!W_ERROR_IS_OK(werr)) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
+               dsdb_dn = dsdb_dn_parse(tmp_ctx, ldb, &el->values[i], schema_attr->syntax->ldap_oid);
+               if (dsdb_dn == NULL) {
+                       talloc_free(tmp_ctx);
+                       return LDB_ERR_INVALID_DN_SYNTAX;
+               }
 
-       ret = ldb_build_search_req(&search_req, module->ldb, req,
-                                  base_dn, LDB_SCOPE_BASE,
-                                  "(objectClass=*)", attrs,
-                                  NULL,
-                                  ac, la_op_search_callback,
-                                  req);
+               ret = dsdb_module_search_dn(module, tmp_ctx, &res, dsdb_dn->dn,
+                                           attrs,
+                                           DSDB_SEARCH_SHOW_DELETED |
+                                           DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT |
+                                           DSDB_SEARCH_REVEAL_INTERNALS);
+               if (ret != LDB_SUCCESS) {
+                       ldb_asprintf_errstring(ldb, "Linked attribute %s->%s between %s and %s - remote not found - %s",
+                                              el->name, target->lDAPDisplayName,
+                                              ldb_dn_get_linearized(old_dn),
+                                              ldb_dn_get_linearized(dsdb_dn->dn),
+                                              ldb_errstring(ldb));
+                       talloc_free(tmp_ctx);
+                       return ret;
+               }
+               msg = res->msgs[0];
 
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
+               if (msg->num_elements == 0) {
+                       /* Forward link without backlink remaining - nothing to do here */
+                       continue;
+               } else if (msg->num_elements != 1) {
+                       ldb_asprintf_errstring(ldb, "Bad msg elements - got %u elements, expected one element to be returned in linked_attributes_fix_links for %s",
+                                              msg->num_elements, ldb_dn_get_linearized(msg->dn));
+                       talloc_free(tmp_ctx);
+                       return LDB_ERR_OPERATIONS_ERROR;
+               }
+               if (ldb_attr_cmp(msg->elements[0].name, target->lDAPDisplayName) != 0) {
+                       ldb_asprintf_errstring(ldb, "Bad returned attribute in linked_attributes_fix_links: got %s, expected %s for %s", msg->elements[0].name, target->lDAPDisplayName, ldb_dn_get_linearized(msg->dn));
+                       talloc_free(tmp_ctx);
+                       return LDB_ERR_OPERATIONS_ERROR;
+               }
+               el2 = &msg->elements[0];
 
-       talloc_steal(search_req, attrs);
+               el2->flags = LDB_FLAG_MOD_REPLACE;
 
-       return ldb_next_request(module, search_req);
-}
+               /* find our DN in the values */
+               for (j=0; j<el2->num_values; j++) {
+                       struct dsdb_dn *dsdb_dn2;
+                       dsdb_dn2 = dsdb_dn_parse(msg, ldb, &el2->values[j], target->syntax->ldap_oid);
+                       if (dsdb_dn2 == NULL) {
+                               talloc_free(tmp_ctx);
+                               return LDB_ERR_INVALID_DN_SYNTAX;
+                       }
+                       if (ldb_dn_compare(old_dn, dsdb_dn2->dn) != 0) {
+                               continue;
+                       }
+                       ret = ldb_dn_update_components(dsdb_dn2->dn, new_dn);
+                       if (ret != LDB_SUCCESS) {
+                               talloc_free(tmp_ctx);
+                               return ret;
+                       }
 
-static int la_op_search_callback(struct ldb_request *req,
-                                struct ldb_reply *ares)
-{
-       struct la_context *ac;
-       const struct dsdb_attribute *schema_attr;
-       const struct dsdb_attribute *target_attr;
-       const struct ldb_message_element *el;
-       const char *attr_name;
-       const char *deldn;
-       const char *adddn;
-       int i, j;
-       int ret;
+                       el2->values[j] = data_blob_string_const(
+                               dsdb_dn_get_extended_linearized(el2->values, dsdb_dn2, 1));
+               }
 
-       ac = talloc_get_type(req->context, struct la_context);
+               ret = dsdb_check_single_valued_link(target, el2);
+               if (ret != LDB_SUCCESS) {
+                       talloc_free(tmp_ctx);
+                       return ret;
+               }
 
-       if (!ares) {
-               return ldb_module_done(ac->req, NULL, NULL,
-                                       LDB_ERR_OPERATIONS_ERROR);
-       }
-       if (ares->error != LDB_SUCCESS) {
-               return ldb_module_done(ac->req, ares->controls,
-                                       ares->response, ares->error);
+               ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE | DSDB_MODIFY_RELAX);
+               if (ret != LDB_SUCCESS) {
+                       ldb_asprintf_errstring(ldb, "Linked attribute %s->%s between %s and %s - update failed - %s",
+                                              el->name, target->lDAPDisplayName,
+                                              ldb_dn_get_linearized(old_dn),
+                                              ldb_dn_get_linearized(dsdb_dn->dn),
+                                              ldb_errstring(ldb));
+                       talloc_free(tmp_ctx);
+                       return ret;
+               }
        }
 
-       /* Only entries are interesting, and we only want the olddn */
-       switch (ares->type) {
-       case LDB_REPLY_ENTRY:
-               ret = ldb_dn_compare(ares->message->dn, req->op.search.base);
-               if (ret != 0) {
-                       /* Guh?  We only asked for this DN */
-                       talloc_free(ares);
-                       return ldb_module_done(ac->req, NULL, NULL,
-                                               LDB_ERR_OPERATIONS_ERROR);
-               }
-               if (ares->message->num_elements == 0) {
-                       /* only bother at all if there were some
-                        * linked attributes found */
-                       talloc_free(ares);
-                       return LDB_SUCCESS;
-               }
+       talloc_free(tmp_ctx);
+       return LDB_SUCCESS;
+}
 
-               switch (ac->req->operation) {
-               case LDB_DELETE:
-                       deldn = ldb_dn_get_linearized(ac->req->op.del.dn);
-                       adddn = NULL;
-                       break;
-               case LDB_RENAME:
-                       deldn = ldb_dn_get_linearized(ac->req->op.rename.olddn);
-                       adddn = ldb_dn_get_linearized(ac->req->op.rename.newdn);
-                       break;
-               default:
-                       talloc_free(ares);
-                       return ldb_module_done(ac->req, NULL, NULL,
-                                               LDB_ERR_OPERATIONS_ERROR);
-               }
 
-               for (i = 0; i < ares->message->num_elements; i++) {
-                       el = &ares->message->elements[i];
+/* rename */
+static int linked_attributes_rename(struct ldb_module *module, struct ldb_request *req)
+{
+       struct ldb_result *res;
+       struct ldb_message *msg;
+       unsigned int i;
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct dsdb_schema *schema;
+       int ret;
+       /*
+          - load the current msg
+          - find any linked attributes
+          - if its a link then find the target object
+          - modify the target linked attributes with the new DN
+       */
+       ret = dsdb_module_search_dn(module, req, &res, req->op.rename.olddn,
+                                   NULL, DSDB_SEARCH_SHOW_DELETED);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
 
-                       schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
-                       if (!schema_attr) {
-                               ldb_asprintf_errstring(ac->module->ldb,
-                                       "attribute %s is not a valid attribute"
-                                       " in schema", el->name);
-                               talloc_free(ares);
-                               return ldb_module_done(ac->req, NULL, NULL,
-                                               LDB_ERR_OBJECT_CLASS_VIOLATION);
-                       }
+       schema = dsdb_get_schema(ldb, res);
+       if (!schema) {
+               return ldb_oom(ldb);
+       }
 
-                       /* Valid attribute, now find out if it is linked */
-                       if (schema_attr->linkID == 0) {
-                               /* Not a linked attribute, skip */
-                               continue;
-                       }
+       msg = res->msgs[0];
 
-                       if ((schema_attr->linkID & 1) == 0) {
-                               /* Odd is for the target. */
-                               target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
-                               if (!target_attr) {
-                                       continue;
-                               }
-                               attr_name = target_attr->lDAPDisplayName;
-                       } else {
-                               target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID - 1);
-                               if (!target_attr) {
-                                       continue;
-                               }
-                               attr_name = target_attr->lDAPDisplayName;
-                       }
-                       for (j = 0; j < el->num_values; j++) {
-                               ret = la_store_op(ac, LA_OP_DEL,
-                                                 &el->values[j],
-                                                 attr_name, deldn);
-                               if (ret != LDB_SUCCESS) {
-                                       talloc_free(ares);
-                                       return ldb_module_done(ac->req,
-                                                              NULL, NULL, ret);
-                               }
-                               if (!adddn) continue;
-                               ret = la_store_op(ac, LA_OP_ADD,
-                                                 &el->values[j],
-                                                 attr_name, adddn);
-                               if (ret != LDB_SUCCESS) {
-                                       talloc_free(ares);
-                                       return ldb_module_done(ac->req,
-                                                              NULL, NULL, ret);
-                               }
-                       }
+       for (i=0; i<msg->num_elements; i++) {
+               struct ldb_message_element *el = &msg->elements[i];
+               const struct dsdb_attribute *schema_attr
+                       = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
+               if (!schema_attr || schema_attr->linkID == 0) {
+                       continue;
+               }
+               ret = linked_attributes_fix_links(module, msg->dn, req->op.rename.newdn, el,
+                                                 schema, schema_attr);
+               if (ret != LDB_SUCCESS) {
+                       talloc_free(res);
+                       return ret;
                }
+       }
 
-               break;
+       talloc_free(res);
 
-       case LDB_REPLY_REFERRAL:
-               /* ignore */
-               break;
+       return ldb_next_request(module, req);
+}
 
-       case LDB_REPLY_DONE:
 
-               talloc_free(ares);
+/* queue a linked attributes modify request in the la_private
+   structure */
+static int la_queue_mod_request(struct la_context *ac)
+{
+       struct la_private *la_private =
+               talloc_get_type(ldb_module_get_private(ac->module), struct la_private);
 
-               if (ac->ops) {
-                       /* start the mod requests chain */
-                       ret = la_do_mod_request(ac);
-               } else {
-                       ret = la_down_req(ac);
-               }
-               if (ret != LDB_SUCCESS) {
-                       return ldb_module_done(ac->req, NULL, NULL, ret);
-               }
-               return LDB_SUCCESS;
+       if (la_private == NULL) {
+               ldb_debug(ldb_module_get_ctx(ac->module), LDB_DEBUG_ERROR, __location__ ": No la_private transaction setup\n");
+               return ldb_operr(ldb_module_get_ctx(ac->module));
        }
 
-       talloc_free(ares);
-       return LDB_SUCCESS;
+       talloc_steal(la_private, ac);
+       DLIST_ADD(la_private->la_list, ac);
+
+       return ldb_module_done(ac->req, ac->op_controls,
+                              ac->op_response, LDB_SUCCESS);
 }
 
-/* do a linked attributes modify request */
-static int la_do_mod_request(struct la_context *ac)
+/* Having done the original operation, then try to fix up all the linked attributes for modify and delete */
+static int la_mod_del_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
-       struct ldb_message_element *ret_el;
-       struct ldb_request *mod_req;
-       struct ldb_message *new_msg;
+       struct la_context *ac;
        struct ldb_context *ldb;
        int ret;
 
-       ldb = ac->module->ldb;
+       ac = talloc_get_type(req->context, struct la_context);
+       ldb = ldb_module_get_ctx(ac->module);
 
-       /* Create the modify request */
-       new_msg = ldb_msg_new(ac);
-       if (!new_msg) {
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+       if (!ares) {
+               return ldb_module_done(ac->req, NULL, NULL,
+                                       LDB_ERR_OPERATIONS_ERROR);
        }
-       new_msg->dn = ldb_dn_copy(new_msg, ac->ops->dn);
-       if (!new_msg->dn) {
-               return LDB_ERR_OPERATIONS_ERROR;
+       if (ares->error != LDB_SUCCESS) {
+               return ldb_module_done(ac->req, ares->controls,
+                                       ares->response, ares->error);
        }
 
-       if (ac->ops->op == LA_OP_ADD) {
-               ret = ldb_msg_add_empty(new_msg, ac->ops->name,
-                                       LDB_FLAG_MOD_ADD, &ret_el);
-       } else {
-               ret = ldb_msg_add_empty(new_msg, ac->ops->name,
-                                       LDB_FLAG_MOD_DELETE, &ret_el);
-       }
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
-       ret_el->values = talloc_array(new_msg, struct ldb_val, 1);
-       if (!ret_el->values) {
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+       if (ares->type != LDB_REPLY_DONE) {
+               ldb_set_errstring(ldb,
+                                 "invalid ldb_reply_type in callback");
+               talloc_free(ares);
+               return ldb_module_done(ac->req, NULL, NULL,
+                                       LDB_ERR_OPERATIONS_ERROR);
        }
-       ret_el->values[0] = data_blob_string_const(ac->ops->value);
-       ret_el->num_values = 1;
 
-       /* use ac->ops as the mem_ctx so that the request will be freed
-        * in the callback as soon as completed */
-       ret = ldb_build_mod_req(&mod_req, ldb, ac->ops,
-                               new_msg,
-                               NULL,
-                               ac, la_mod_callback,
-                               ac->req);
+       ac->op_controls = talloc_steal(ac, ares->controls);
+       ac->op_response = talloc_steal(ac, ares->response);
+
+       /* If we have modfies to make, this is the time to do them for modify and delete */
+       ret = la_queue_mod_request(ac);
+
        if (ret != LDB_SUCCESS) {
-               return ret;
+               return ldb_module_done(ac->req, NULL, NULL, ret);
        }
-       talloc_steal(mod_req, new_msg);
+       talloc_free(ares);
+
+       /* la_queue_mod_request has already sent the callbacks */
+       return LDB_SUCCESS;
 
-       /* Run the new request */
-       return ldb_next_request(ac->module, mod_req);
 }
 
-static int la_mod_callback(struct ldb_request *req, struct ldb_reply *ares)
+/* Having done the original add, then try to fix up all the linked attributes
+
+  This is done after the add so the links can get the extended DNs correctly.
+ */
+static int la_add_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
        struct la_context *ac;
-       struct la_op_store *os;
+       struct ldb_context *ldb;
        int ret;
 
        ac = talloc_get_type(req->context, struct la_context);
+       ldb = ldb_module_get_ctx(ac->module);
 
        if (!ares) {
                return ldb_module_done(ac->req, NULL, NULL,
@@ -822,72 +830,72 @@ static int la_mod_callback(struct ldb_request *req, struct ldb_reply *ares)
        }
 
        if (ares->type != LDB_REPLY_DONE) {
-               ldb_set_errstring(ac->module->ldb,
+               ldb_set_errstring(ldb,
                                  "invalid ldb_reply_type in callback");
                talloc_free(ares);
                return ldb_module_done(ac->req, NULL, NULL,
                                        LDB_ERR_OPERATIONS_ERROR);
        }
 
-       talloc_free(ares);
-
        if (ac->ops) {
-               os = ac->ops;
-               ac->ops = os->next;
+               struct ldb_request *search_req;
+               static const char *attrs[] = { NULL };
 
-               /* this frees the request too
-                * DO NOT access 'req' after this point */
-               talloc_free(os);
-       }
+               /* The callback does all the hard work here - we need
+                * the objectGUID and SID of the added record */
+               ret = ldb_build_search_req(&search_req, ldb, ac,
+                                          ac->req->op.add.message->dn,
+                                          LDB_SCOPE_BASE,
+                                          "(objectClass=*)", attrs,
+                                          NULL,
+                                          ac, la_mod_search_callback,
+                                          ac->req);
 
-       /* as last op run the original request */
-       if (ac->ops) {
-               ret = la_do_mod_request(ac);
-       } else {
-               ret = la_down_req(ac);
-       }
+               if (ret == LDB_SUCCESS) {
+                       ret = ldb_request_add_control(search_req,
+                                                     LDB_CONTROL_EXTENDED_DN_OID,
+                                                     false, NULL);
+               }
+               if (ret != LDB_SUCCESS) {
+                       return ldb_module_done(ac->req, NULL, NULL,
+                                              ret);
+               }
 
-       if (ret != LDB_SUCCESS) {
-               return ldb_module_done(ac->req, NULL, NULL, ret);
+               ac->op_controls = talloc_steal(ac, ares->controls);
+               ac->op_response = talloc_steal(ac, ares->response);
+
+               return ldb_next_request(ac->module, search_req);
+
+       } else {
+               return ldb_module_done(ac->req, ares->controls,
+                                      ares->response, ares->error);
        }
-       return LDB_SUCCESS;
 }
 
+/* Reconstruct the original request, but pointing at our local callback to finish things off */
 static int la_down_req(struct la_context *ac)
 {
        struct ldb_request *down_req;
+       struct ldb_context *ldb;
        int ret;
 
+       ldb = ldb_module_get_ctx(ac->module);
+
        switch (ac->req->operation) {
        case LDB_ADD:
-               ret = ldb_build_add_req(&down_req, ac->module->ldb, ac,
+               ret = ldb_build_add_req(&down_req, ldb, ac,
                                        ac->req->op.add.message,
                                        ac->req->controls,
-                                       ac, la_down_callback,
+                                       ac, la_add_callback,
                                        ac->req);
                break;
        case LDB_MODIFY:
-               ret = ldb_build_mod_req(&down_req, ac->module->ldb, ac,
+               ret = ldb_build_mod_req(&down_req, ldb, ac,
                                        ac->req->op.mod.message,
                                        ac->req->controls,
-                                       ac, la_down_callback,
-                                       ac->req);
-               break;
-       case LDB_DELETE:
-               ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
-                                       ac->req->op.del.dn,
-                                       ac->req->controls,
-                                       ac, la_down_callback,
+                                       ac, la_mod_del_callback,
                                        ac->req);
                break;
-       case LDB_RENAME:
-               ret = ldb_build_rename_req(&down_req, ac->module->ldb, ac,
-                                          ac->req->op.rename.olddn,
-                                          ac->req->op.rename.newdn,
-                                          ac->req->controls,
-                                          ac, la_down_callback,
-                                          ac->req);
-               break;
        default:
                ret = LDB_ERR_OPERATIONS_ERROR;
        }
@@ -898,37 +906,177 @@ static int la_down_req(struct la_context *ac)
        return ldb_next_request(ac->module, down_req);
 }
 
-static int la_down_callback(struct ldb_request *req, struct ldb_reply *ares)
+/*
+  use the GUID part of an extended DN to find the target DN, in case
+  it has moved
+ */
+static int la_find_dn_target(struct ldb_module *module, struct la_context *ac,
+                            struct GUID *guid, struct ldb_dn **dn)
 {
-       struct la_context *ac;
+       return dsdb_find_dn_by_guid(ldb_module_get_ctx(ac->module), ac, guid, dn);
+}
 
-       ac = talloc_get_type(req->context, struct la_context);
+/* apply one la_context op change */
+static int la_do_op_request(struct ldb_module *module, struct la_context *ac, struct la_op_store *op)
+{
+       struct ldb_message_element *ret_el;
+       struct ldb_message *new_msg;
+       struct ldb_context *ldb;
+       int ret;
 
-       if (!ares) {
-               return ldb_module_done(ac->req, NULL, NULL,
-                                       LDB_ERR_OPERATIONS_ERROR);
+       ldb = ldb_module_get_ctx(ac->module);
+
+       /* Create the modify request */
+       new_msg = ldb_msg_new(ac);
+       if (!new_msg) {
+               return ldb_oom(ldb);
        }
-       if (ares->error != LDB_SUCCESS) {
-               return ldb_module_done(ac->req, ares->controls,
-                                       ares->response, ares->error);
+
+       ret = la_find_dn_target(module, ac, &op->guid, &new_msg->dn);
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
 
-       if (ares->type != LDB_REPLY_DONE) {
-               ldb_set_errstring(ac->module->ldb,
-                                 "invalid ldb_reply_type in callback");
-               talloc_free(ares);
-               return ldb_module_done(ac->req, NULL, NULL,
-                                       LDB_ERR_OPERATIONS_ERROR);
+       if (op->op == LA_OP_ADD) {
+               ret = ldb_msg_add_empty(new_msg, op->name,
+                                       LDB_FLAG_MOD_ADD, &ret_el);
+       } else {
+               ret = ldb_msg_add_empty(new_msg, op->name,
+                                       LDB_FLAG_MOD_DELETE, &ret_el);
+       }
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       ret_el->values = talloc_array(new_msg, struct ldb_val, 1);
+       if (!ret_el->values) {
+               return ldb_oom(ldb);
+       }
+       ret_el->num_values = 1;
+       if (op->op == LA_OP_ADD) {
+               ret_el->values[0] = data_blob_string_const(ldb_dn_get_extended_linearized(new_msg, ac->add_dn, 1));
+       } else {
+               ret_el->values[0] = data_blob_string_const(ldb_dn_get_extended_linearized(new_msg, ac->del_dn, 1));
+       }
+
+#if 0
+       ldb_debug(ldb, LDB_DEBUG_WARNING,
+                 "link on %s %s: %s %s\n",
+                 ldb_dn_get_linearized(new_msg->dn), ret_el->name,
+                 ret_el->values[0].data, ac->ops->op == LA_OP_ADD ? "added" : "deleted");
+#endif
+
+       if (DEBUGLVL(4)) {
+               DEBUG(4,("Applying linked attribute change:\n%s\n",
+                        ldb_ldif_message_string(ldb, op, LDB_CHANGETYPE_MODIFY, new_msg)));
+       }
+
+       ret = dsdb_module_modify(module, new_msg, DSDB_FLAG_NEXT_MODULE);
+       if (ret != LDB_SUCCESS) {
+               ldb_debug(ldb, LDB_DEBUG_WARNING, "Failed to apply linked attribute change '%s'\n%s\n",
+                         ldb_errstring(ldb),
+                         ldb_ldif_message_string(ldb, op, LDB_CHANGETYPE_MODIFY, new_msg));
+       }
+
+       return ret;
+}
+
+/* apply one set of la_context changes */
+static int la_do_mod_request(struct ldb_module *module, struct la_context *ac)
+{
+       struct la_op_store *op;
+
+       for (op = ac->ops; op; op=op->next) {
+               int ret = la_do_op_request(module, ac, op);
+               if (ret != LDB_SUCCESS) {
+                       if (ret != LDB_ERR_NO_SUCH_OBJECT) {
+                               return ret;
+                       }
+               }
+       }
+
+       return LDB_SUCCESS;
+}
+
+
+/*
+  we hook into the transaction operations to allow us to
+  perform the linked attribute updates at the end of the whole
+  transaction. This allows a forward linked attribute to be created
+  before the target is created, as long as the target is created
+  in the same transaction
+ */
+static int linked_attributes_start_transaction(struct ldb_module *module)
+{
+       /* create our private structure for this transaction */
+       struct la_private *la_private = talloc_get_type(ldb_module_get_private(module),
+                                                       struct la_private);
+       talloc_free(la_private);
+       la_private = talloc(module, struct la_private);
+       if (la_private == NULL) {
+               return ldb_oom(ldb_module_get_ctx(module));
        }
+       la_private->la_list = NULL;
+       ldb_module_set_private(module, la_private);
+       return ldb_next_start_trans(module);
+}
 
-       return ldb_module_done(ac->req, ares->controls,
-                               ares->response, ares->error);
+/*
+  on prepare commit we loop over our queued la_context structures
+  and apply each of them
+ */
+static int linked_attributes_prepare_commit(struct ldb_module *module)
+{
+       struct la_private *la_private =
+               talloc_get_type(ldb_module_get_private(module), struct la_private);
+       struct la_context *ac;
+
+       if (!la_private) {
+               /* prepare commit without begin_transaction - let someone else return the error, just don't segfault */
+               return ldb_next_prepare_commit(module);
+       }
+       /* walk the list backwards, to do the first entry first, as we
+        * added the entries with DLIST_ADD() which puts them at the
+        * start of the list */
+
+       /* Start at the end of the list - so we can start
+        * there, but ensure we don't create a loop by NULLing
+        * it out in the first element */
+       ac = DLIST_TAIL(la_private->la_list);
+
+       for (; ac; ac=DLIST_PREV(ac)) {
+               int ret;
+               ac->req = NULL;
+               ret = la_do_mod_request(module, ac);
+               if (ret != LDB_SUCCESS) {
+                       DEBUG(0,(__location__ ": Failed mod request ret=%d\n", ret));
+                       talloc_free(la_private);
+                       ldb_module_set_private(module, NULL);
+                       return ret;
+               }
+       }
+
+       talloc_free(la_private);
+       ldb_module_set_private(module, NULL);
+
+       return ldb_next_prepare_commit(module);
 }
 
+static int linked_attributes_del_transaction(struct ldb_module *module)
+{
+       struct la_private *la_private =
+               talloc_get_type(ldb_module_get_private(module), struct la_private);
+       talloc_free(la_private);
+       ldb_module_set_private(module, NULL);
+       return ldb_next_del_trans(module);
+}
+
+
 _PUBLIC_ const struct ldb_module_ops ldb_linked_attributes_module_ops = {
        .name              = "linked_attributes",
        .add               = linked_attributes_add,
        .modify            = linked_attributes_modify,
-       .del               = linked_attributes_op,
-       .rename            = linked_attributes_op,
+       .rename            = linked_attributes_rename,
+       .start_transaction = linked_attributes_start_transaction,
+       .prepare_commit    = linked_attributes_prepare_commit,
+       .del_transaction   = linked_attributes_del_transaction,
 };