LDB ASYNC: other backends
[kai/samba.git] / source4 / lib / ldb / ldb_ldap / ldb_ldap.c
index bab3c86e16ad343fb2400dbfc9a0c94e4559dcb3..7caee10b47f3ed431849dd2e893edb964ff3c03c 100644 (file)
@@ -2,6 +2,7 @@
    ldb database library
 
    Copyright (C) Andrew Tridgell  2004
+   Copyright (C) Simo Sorce       2006
 
      ** NOTE! The following LGPL license applies to the ldb
      ** library. This does NOT imply that all of Samba is released
@@ -10,7 +11,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
- *  Name: ldb
+ *  Name: ldb_ldap
  *
  *  Component: ldb ldap backend
  *
  *  Description: core files for LDAP backend
  *
  *  Author: Andrew Tridgell
+ *
+ *  Modifications:
+ *
+ *  - description: make the module use asyncronous calls
+ *    date: Feb 2006
+ *    author: Simo Sorce
  */
 
-#include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_private.h"
-#include "ldb/ldb_ldap/ldb_ldap.h"
+#include "ldb_includes.h"
 
-#if 0
-/*
-  we don't need this right now, but will once we add some backend 
-  options
-*/
+#define LDAP_DEPRECATED 1
+#include <ldap.h>
 
-/*
-  find an option in an option list (a null terminated list of strings)
+struct lldb_private {
+       LDAP *ldap;
+};
 
-  this assumes the list is short. If it ever gets long then we really
-  should do this in some smarter way
- */
-static const char *lldb_option_find(const struct lldb_private *lldb, const char *name)
-{
-       int i;
-       size_t len = strlen(name);
+struct lldb_context {
+       struct ldb_module *module;
+       struct ldb_request *req;
 
-       if (!lldb->options) return NULL;
+       struct lldb_private *lldb;
 
-       for (i=0;lldb->options[i];i++) {                
-               if (strncmp(lldb->options[i], name, len) == 0 &&
-                   lldb->options[i][len] == '=') {
-                       return &lldb->options[i][len+1];
-               }
-       }
-
-       return NULL;
-}
-#endif
+       struct ldb_control **controls;
+       int msgid;
+};
 
-/*
-  close/free the connection
-*/
-static int lldb_close(struct ldb_module *module)
-{
-       struct ldb_context *ldb = module->ldb;
-       talloc_free(ldb);
-       return 0;
+static int lldb_ldap_to_ldb(int err) {
+       /* Ldap errors and ldb errors are defined to the same values */
+       return err;
 }
 
 /*
-  rename a record
+  convert a ldb_message structure to a list of LDAPMod structures
+  ready for ldap_add() or ldap_modify()
 */
-static int lldb_rename(struct ldb_module *module, const char *olddn, const char *newdn)
+static LDAPMod **lldb_msg_to_mods(void *mem_ctx, const struct ldb_message *msg, int use_flags)
 {
-       struct lldb_private *lldb = module->private_data;
-       int ret = 0;
-       char *newrdn, *p;
-       const char *parentdn = "";
-       TALLOC_CTX *mem_ctx = talloc_new(lldb);
-
-       /* ignore ltdb specials */
-       if (olddn[0] == '@' ||newdn[0] == '@') {
-               return 0;
-       }
-
-       newrdn = talloc_strdup(mem_ctx, newdn);
-       if (!newrdn) {
-               return -1;
-       }
-
-       p = strchr(newrdn, ',');
-       if (p) {
-               *p++ = '\0';
-               parentdn = p;
-       }
+       LDAPMod **mods;
+       unsigned int i, j;
+       int num_mods = 0;
 
-       lldb->last_rc = ldap_rename_s(lldb->ldap, olddn, newrdn, parentdn, 1, NULL, NULL);
-       if (lldb->last_rc != LDAP_SUCCESS) {
-               ret = -1;
+       /* allocate maximum number of elements needed */
+       mods = talloc_array(mem_ctx, LDAPMod *, msg->num_elements+1);
+       if (!mods) {
+               errno = ENOMEM;
+               return NULL;
        }
+       mods[0] = NULL;
 
-       talloc_free(mem_ctx);
-
-       return ret;
-}
+       for (i=0;i<msg->num_elements;i++) {
+               const struct ldb_message_element *el = &msg->elements[i];
 
-/*
-  delete a record
-*/
-static int lldb_delete(struct ldb_module *module, const char *dn)
-{
-       struct lldb_private *lldb = module->private_data;
-       int ret = 0;
+               mods[num_mods] = talloc(mods, LDAPMod);
+               if (!mods[num_mods]) {
+                       goto failed;
+               }
+               mods[num_mods+1] = NULL;
+               mods[num_mods]->mod_op = LDAP_MOD_BVALUES;
+               if (use_flags) {
+                       switch (el->flags & LDB_FLAG_MOD_MASK) {
+                       case LDB_FLAG_MOD_ADD:
+                               mods[num_mods]->mod_op |= LDAP_MOD_ADD;
+                               break;
+                       case LDB_FLAG_MOD_DELETE:
+                               mods[num_mods]->mod_op |= LDAP_MOD_DELETE;
+                               break;
+                       case LDB_FLAG_MOD_REPLACE:
+                               mods[num_mods]->mod_op |= LDAP_MOD_REPLACE;
+                               break;
+                       }
+               }
+               mods[num_mods]->mod_type = discard_const_p(char, el->name);
+               mods[num_mods]->mod_vals.modv_bvals = talloc_array(mods[num_mods], 
+                                                                  struct berval *,
+                                                                  1+el->num_values);
+               if (!mods[num_mods]->mod_vals.modv_bvals) {
+                       goto failed;
+               }
 
-       /* ignore ltdb specials */
-       if (dn[0] == '@') {
-               return 0;
-       }
-       
-       lldb->last_rc = ldap_delete_s(lldb->ldap, dn);
-       if (lldb->last_rc != LDAP_SUCCESS) {
-               ret = -1;
+               for (j=0;j<el->num_values;j++) {
+                       mods[num_mods]->mod_vals.modv_bvals[j] = talloc(mods[num_mods]->mod_vals.modv_bvals,
+                                                                       struct berval);
+                       if (!mods[num_mods]->mod_vals.modv_bvals[j]) {
+                               goto failed;
+                       }
+                       mods[num_mods]->mod_vals.modv_bvals[j]->bv_val = el->values[j].data;
+                       mods[num_mods]->mod_vals.modv_bvals[j]->bv_len = el->values[j].length;
+               }
+               mods[num_mods]->mod_vals.modv_bvals[j] = NULL;
+               num_mods++;
        }
 
-       return ret;
-}
+       return mods;
 
-/*
-  free a search result
-*/
-static int lldb_search_free(struct ldb_module *module, struct ldb_message **res)
-{
-       talloc_free(res);
-       return 0;
+failed:
+       talloc_free(mods);
+       return NULL;
 }
 
-
 /*
   add a single set of ldap message values to a ldb_message
 */
@@ -161,7 +146,7 @@ static int lldb_add_msg_attr(struct ldb_context *ldb,
                return -1;
        }
 
-       el = talloc_realloc_p(msg, msg->elements, struct ldb_message_element, 
+       el = talloc_realloc(msg, msg->elements, struct ldb_message_element, 
                              msg->num_elements + 1);
        if (!el) {
                errno = ENOMEM;
@@ -180,17 +165,22 @@ static int lldb_add_msg_attr(struct ldb_context *ldb,
        el->flags = 0;
 
        el->num_values = 0;
-       el->values = talloc_array_p(msg->elements, struct ldb_val, count);
+       el->values = talloc_array(msg->elements, struct ldb_val, count);
        if (!el->values) {
                errno = ENOMEM;
                return -1;
        }
 
        for (i=0;i<count;i++) {
-               el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
+               /* we have to ensure this is null terminated so that
+                  ldb_msg_find_attr_as_string() can work */
+               el->values[i].data = talloc_size(el->values, bval[i]->bv_len+1);
                if (!el->values[i].data) {
+                       errno = ENOMEM;
                        return -1;
                }
+               memcpy(el->values[i].data, bval[i]->bv_val, bval[i]->bv_len);
+               el->values[i].data[bval[i]->bv_len] = 0;
                el->values[i].length = bval[i]->bv_len;
                el->num_values++;
        }
@@ -203,287 +193,630 @@ static int lldb_add_msg_attr(struct ldb_context *ldb,
 /*
   search for matching records
 */
-static int lldb_search(struct ldb_module *module, const char *base,
-                      enum ldb_scope scope, const char *expression,
-                      const char * const *attrs, struct ldb_message ***res)
+static int lldb_search(struct lldb_context *lldb_ac)
 {
-       struct ldb_context *ldb = module->ldb;
-       struct lldb_private *lldb = module->private_data;
-       int count, msg_count;
-       LDAPMessage *ldapres, *msg;
+       struct lldb_private *lldb = lldb_ac->lldb;
+       struct ldb_module *module = lldb_ac->module;
+       struct ldb_request *req = lldb_ac->req;
+       struct timeval tv;
+       int ldap_scope;
+       char *search_base;
+       char *expression;
+       int ret;
+
+       if (!req->callback || !req->context) {
+               ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-       if (base == NULL) {
-               base = "";
+       if (req->op.search.tree == NULL) {
+               ldb_set_errstring(module->ldb, "Invalid expression parse tree");
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       lldb->last_rc = ldap_search_s(lldb->ldap, base, (int)scope, 
-                                     expression, 
-                                     discard_const_p(char *, attrs), 
-                                     0, &ldapres);
-       if (lldb->last_rc != LDAP_SUCCESS) {
-               return -1;
+       if (req->controls != NULL) {
+               ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls are not yet supported by ldb_ldap backend!\n");
        }
 
-       count = ldap_count_entries(lldb->ldap, ldapres);
-       if (count == -1 || count == 0) {
-               ldap_msgfree(ldapres);
-               return count;
+       req->handle->state = LDB_ASYNC_PENDING;
+
+       search_base = ldb_dn_alloc_linearized(lldb_ac, req->op.search.base);
+       if (req->op.search.base == NULL) {
+               search_base = talloc_strdup(lldb_ac, "");
+       }
+       if (search_base == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       (*res) = talloc_array_p(lldb, struct ldb_message *, count+1);
-       if (! *res) {
-               ldap_msgfree(ldapres);
-               errno = ENOMEM;
-               return -1;
+       expression = ldb_filter_from_tree(lldb_ac, req->op.search.tree);
+       if (expression == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       (*res)[0] = NULL;
+       switch (req->op.search.scope) {
+       case LDB_SCOPE_BASE:
+               ldap_scope = LDAP_SCOPE_BASE;
+               break;
+       case LDB_SCOPE_ONELEVEL:
+               ldap_scope = LDAP_SCOPE_ONELEVEL;
+               break;
+       default:
+               ldap_scope = LDAP_SCOPE_SUBTREE;
+               break;
+       }
 
-       msg_count = 0;
+       tv.tv_sec = req->timeout;
+       tv.tv_usec = 0;
+
+       ret = ldap_search_ext(lldb->ldap, search_base, ldap_scope, 
+                           expression, 
+                           discard_const_p(char *, req->op.search.attrs), 
+                           0,
+                           NULL,
+                           NULL,
+                           &tv,
+                           LDAP_NO_LIMIT,
+                           &lldb_ac->msgid);
+
+       if (ret != LDAP_SUCCESS) {
+               ldb_set_errstring(module->ldb, ldap_err2string(ret));
+       }
 
-       /* loop over all messages */
-       for (msg=ldap_first_entry(lldb->ldap, ldapres); 
-            msg; 
-            msg=ldap_next_entry(lldb->ldap, msg)) {
-               BerElement *berptr = NULL;
-               char *attr, *dn;
+       return lldb_ldap_to_ldb(ret);
+}
 
-               if (msg_count == count) {
-                       /* hmm, got too many? */
-                       ldb_debug(ldb, LDB_DEBUG_FATAL, "Fatal: ldap message count inconsistent\n");
-                       break;
-               }
+/*
+  add a record
+*/
+static int lldb_add(struct lldb_context *lldb_ac)
+{
+       struct lldb_private *lldb = lldb_ac->lldb;
+       struct ldb_module *module = lldb_ac->module;
+       struct ldb_request *req = lldb_ac->req;
+       LDAPMod **mods;
+       char *dn;
+       int ret;
 
-               (*res)[msg_count] = talloc_p(*res, struct ldb_message);
-               if (!(*res)[msg_count]) {
-                       goto failed;
-               }
-               (*res)[msg_count+1] = NULL;
+       req->handle->state = LDB_ASYNC_PENDING;
 
-               dn = ldap_get_dn(lldb->ldap, msg);
-               if (!dn) {
-                       goto failed;
-               }
+       mods = lldb_msg_to_mods(lldb_ac, req->op.add.message, 0);
+       if (mods == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-               (*res)[msg_count]->dn = talloc_strdup((*res)[msg_count], dn);
-               ldap_memfree(dn);
-               if (!(*res)[msg_count]->dn) {
-                       goto failed;
-               }
+       dn = ldb_dn_alloc_linearized(lldb_ac, req->op.add.message->dn);
+       if (dn == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
+       ret = ldap_add_ext(lldb->ldap, dn, mods,
+                          NULL,
+                          NULL,
+                          &lldb_ac->msgid);
 
-               (*res)[msg_count]->num_elements = 0;
-               (*res)[msg_count]->elements = NULL;
-               (*res)[msg_count]->private_data = NULL;
+       if (ret != LDAP_SUCCESS) {
+               ldb_set_errstring(module->ldb, ldap_err2string(ret));
+       }
 
-               /* loop over all attributes */
-               for (attr=ldap_first_attribute(lldb->ldap, msg, &berptr);
-                    attr;
-                    attr=ldap_next_attribute(lldb->ldap, msg, berptr)) {
-                       struct berval **bval;
-                       bval = ldap_get_values_len(lldb->ldap, msg, attr);
+       return lldb_ldap_to_ldb(ret);
+}
 
-                       if (bval) {
-                               lldb_add_msg_attr(ldb, (*res)[msg_count], attr, bval);
-                               ldap_value_free_len(bval);
-                       }                                         
-                       
-                       ldap_memfree(attr);
-               }
-               if (berptr) ber_free(berptr, 0);
+/*
+  modify a record
+*/
+static int lldb_modify(struct lldb_context *lldb_ac)
+{
+       struct lldb_private *lldb = lldb_ac->lldb;
+       struct ldb_module *module = lldb_ac->module;
+       struct ldb_request *req = lldb_ac->req;
+       LDAPMod **mods;
+       char *dn;
+       int ret;
 
-               msg_count++;
+       req->handle->state = LDB_ASYNC_PENDING;
+
+       mods = lldb_msg_to_mods(lldb_ac, req->op.mod.message, 1);
+       if (mods == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       ldap_msgfree(ldapres);
+       dn = ldb_dn_alloc_linearized(lldb_ac, req->op.mod.message->dn);
+       if (dn == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-       return msg_count;
+       ret = ldap_modify_ext(lldb->ldap, dn, mods,
+                             NULL,
+                             NULL,
+                             &lldb_ac->msgid);
 
-failed:
-       if (*res) lldb_search_free(module, *res);
-       return -1;
+       if (ret != LDAP_SUCCESS) {
+               ldb_set_errstring(module->ldb, ldap_err2string(ret));
+       }
+
+       return lldb_ldap_to_ldb(ret);
 }
 
+/*
+  delete a record
+*/
+static int lldb_delete(struct lldb_context *lldb_ac)
+{
+       struct lldb_private *lldb = lldb_ac->lldb;
+       struct ldb_module *module = lldb_ac->module;
+       struct ldb_request *req = lldb_ac->req;
+       char *dnstr;
+       int ret;
+
+       req->handle->state = LDB_ASYNC_PENDING;
+
+       dnstr = ldb_dn_alloc_linearized(lldb_ac, req->op.del.dn);
+
+       ret = ldap_delete_ext(lldb->ldap, dnstr,
+                             NULL,
+                             NULL,
+                             &lldb_ac->msgid);
+
+       if (ret != LDAP_SUCCESS) {
+               ldb_set_errstring(module->ldb, ldap_err2string(ret));
+       }
+
+       return lldb_ldap_to_ldb(ret);
+}
 
 /*
-  convert a ldb_message structure to a list of LDAPMod structures
-  ready for ldap_add() or ldap_modify()
+  rename a record
 */
-static LDAPMod **lldb_msg_to_mods(struct ldb_context *ldb,
-                                 const struct ldb_message *msg, int use_flags)
+static int lldb_rename(struct lldb_context *lldb_ac)
 {
-       LDAPMod **mods;
-       unsigned int i, j;
-       int num_mods = 0;
+       struct lldb_private *lldb = lldb_ac->lldb;
+       struct ldb_module *module = lldb_ac->module;
+       struct ldb_request *req = lldb_ac->req;
+       char *old_dn;
+               char *newrdn;
+       char *parentdn;
+       int ret;
+
+       req->handle->state = LDB_ASYNC_PENDING;
+
+       old_dn = ldb_dn_alloc_linearized(lldb_ac, req->op.rename.olddn);
+       if (old_dn == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-       /* allocate maximum number of elements needed */
-       mods = talloc_array_p(ldb, LDAPMod *, msg->num_elements+1);
-       if (!mods) {
-               errno = ENOMEM;
-               return NULL;
+       newrdn = talloc_asprintf(lldb_ac, "%s=%s",
+                                ldb_dn_get_rdn_name(req->op.rename.newdn),
+                                ldb_dn_escape_value(lldb, *(ldb_dn_get_rdn_val(req->op.rename.newdn))));
+       if (!newrdn) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
-       mods[0] = NULL;
 
-       for (i=0;i<msg->num_elements;i++) {
-               const struct ldb_message_element *el = &msg->elements[i];
+       parentdn = ldb_dn_alloc_linearized(lldb_ac, ldb_dn_get_parent(lldb_ac, req->op.rename.newdn));
+       if (!parentdn) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-               mods[num_mods] = talloc_p(ldb, LDAPMod);
-               if (!mods[num_mods]) {
-                       goto failed;
-               }
-               mods[num_mods+1] = NULL;
-               mods[num_mods]->mod_op = LDAP_MOD_BVALUES;
-               if (use_flags) {
-                       switch (el->flags & LDB_FLAG_MOD_MASK) {
-                       case LDB_FLAG_MOD_ADD:
-                               mods[num_mods]->mod_op |= LDAP_MOD_ADD;
+       ret = ldap_rename(lldb->ldap, old_dn, newrdn, parentdn,
+                         1, NULL, NULL,
+                         &lldb_ac->msgid);
+
+       if (ret != LDAP_SUCCESS) {
+               ldb_set_errstring(module->ldb, ldap_err2string(ret));
+       }
+
+       return lldb_ldap_to_ldb(ret);
+}
+
+static int lldb_start_trans(struct ldb_module *module)
+{
+       /* TODO implement a local transaction mechanism here */
+
+       return LDB_SUCCESS;
+}
+
+static int lldb_end_trans(struct ldb_module *module)
+{
+       /* TODO implement a local transaction mechanism here */
+
+       return LDB_SUCCESS;
+}
+
+static int lldb_del_trans(struct ldb_module *module)
+{
+       /* TODO implement a local transaction mechanism here */
+
+       return LDB_SUCCESS;
+}
+
+void lldb_request_done(struct ldb_request *req,
+                       struct ldb_control **ctrls, int error)
+{
+       struct ldb_reply *ares;
+
+       ares = talloc_zero(req, struct ldb_reply);
+       if (!ares) {
+               ldb_oom(req->handle->ldb);
+               req->callback(req, NULL);
+               return;
+       }
+       ares->type = LDB_REPLY_DONE;
+       ares->controls = talloc_steal(ares, ctrls);
+       ares->error = error;
+
+       req->callback(req, ares);
+}
+
+/* return false if the request is still in progress
+ * return true if the request is completed
+ */
+static bool lldb_parse_result(struct lldb_context *ac, LDAPMessage *result)
+{
+       struct lldb_private *lldb = ac->lldb;
+       LDAPControl **serverctrlsp = NULL;
+       char **referralsp = NULL;
+       char *matcheddnp = NULL;
+       char *errmsgp = NULL;
+       LDAPMessage *msg;
+       int type;
+       struct ldb_message *ldbmsg;
+       char *referral;
+       bool callback_failed;
+       bool request_done;
+       bool lret;
+       int ret;
+       int i;
+
+       type = ldap_msgtype(result);
+       callback_failed = false;
+       request_done = false;
+
+       switch (type) {
+       case LDAP_RES_SEARCH_ENTRY:
+
+               msg = ldap_first_entry(lldb->ldap, result);
+               if (msg != NULL) {
+                       BerElement *berptr = NULL;
+                       char *attr, *dn;
+
+                       ldbmsg = ldb_msg_new(ac);
+                       if (!ldbmsg) {
+                               ret = LDB_ERR_OPERATIONS_ERROR;
                                break;
-                       case LDB_FLAG_MOD_DELETE:
-                               mods[num_mods]->mod_op |= LDAP_MOD_DELETE;
+                       }
+
+                       dn = ldap_get_dn(lldb->ldap, msg);
+                       if (!dn) {
+                               talloc_free(ldbmsg);
+                               ret = LDB_ERR_OPERATIONS_ERROR;
                                break;
-                       case LDB_FLAG_MOD_REPLACE:
-                               mods[num_mods]->mod_op |= LDAP_MOD_REPLACE;
+                       }
+                       ldbmsg->dn = ldb_dn_new(ldbmsg, ac->module->ldb, dn);
+                       if ( ! ldb_dn_validate(ldbmsg->dn)) {
+                               talloc_free(ldbmsg);
+                               ret = LDB_ERR_OPERATIONS_ERROR;
                                break;
                        }
+                       ldap_memfree(dn);
+
+                       ldbmsg->num_elements = 0;
+                       ldbmsg->elements = NULL;
+
+                       /* loop over all attributes */
+                       for (attr=ldap_first_attribute(lldb->ldap, msg, &berptr);
+                            attr;
+                            attr=ldap_next_attribute(lldb->ldap, msg, berptr)) {
+                               struct berval **bval;
+                               bval = ldap_get_values_len(lldb->ldap, msg, attr);
+
+                               if (bval) {
+                                       lldb_add_msg_attr(ac->module->ldb, ldbmsg, attr, bval);
+                                       ldap_value_free_len(bval);
+                               }
+                       }
+                       if (berptr) ber_free(berptr, 0);
+
+                       ret = ldb_module_send_entry(ac->req, ldbmsg);
+                       if (ret != LDB_SUCCESS) {
+
+                               callback_failed = true;
+                       }
+               } else {
+                       ret = LDB_ERR_OPERATIONS_ERROR;
                }
-               mods[num_mods]->mod_type = el->name;
-               mods[num_mods]->mod_vals.modv_bvals = talloc_array_p(mods[num_mods], 
-                                                                    struct berval *,
-                                                                    1+el->num_values);
-               if (!mods[num_mods]->mod_vals.modv_bvals) {
-                       goto failed;
+               break;
+
+       case LDAP_RES_SEARCH_REFERENCE:
+
+               if (ldap_parse_result(lldb->ldap, result, &ret,
+                                       &matcheddnp, &errmsgp,
+                                       &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
+                       ret = LDB_ERR_OPERATIONS_ERROR;
+               }
+               if (ret != LDB_SUCCESS) {
+                       break;
+               }
+               if (referralsp == NULL) {
+                       ret = LDB_ERR_PROTOCOL_ERROR;
+                       break;
                }
 
-               for (j=0;j<el->num_values;j++) {
-                       mods[num_mods]->mod_vals.modv_bvals[j] = talloc_p(mods[num_mods]->mod_vals.modv_bvals,
-                                                                         struct berval);
-                       if (!mods[num_mods]->mod_vals.modv_bvals[j]) {
-                               goto failed;
+               for (i = 0; referralsp[i]; i++) {
+                       referral = talloc_strdup(ac, referralsp[i]);
+
+                       ret = ldb_module_send_referral(ac->req, referral);
+                       if (ret != LDB_SUCCESS) {
+                               callback_failed = true;
+                               break;
                        }
-                       mods[num_mods]->mod_vals.modv_bvals[j]->bv_val = el->values[j].data;
-                       mods[num_mods]->mod_vals.modv_bvals[j]->bv_len = el->values[j].length;
                }
-               mods[num_mods]->mod_vals.modv_bvals[j] = NULL;
-               num_mods++;
+               break;
+
+       case LDAP_RES_SEARCH_RESULT:
+       case LDAP_RES_MODIFY:
+       case LDAP_RES_ADD:
+       case LDAP_RES_DELETE:
+       case LDAP_RES_MODDN:
+
+               if (ldap_parse_result(lldb->ldap, result, &ret,
+                                       &matcheddnp, &errmsgp,
+                                       &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
+                       ret = LDB_ERR_OPERATIONS_ERROR;
+               }
+               if (ret != LDB_SUCCESS) {
+                       break;
+               }
+
+               if (serverctrlsp != NULL) {
+                       /* FIXME: transform the LDAPControl list into an ldb_control one */
+                       ac->controls = NULL;
+               }
+
+               request_done = true;
+               break;
+
+       default:
+               ret = LDB_ERR_PROTOCOL_ERROR;
+               break;
        }
 
-       return mods;
+       if (ret != LDB_SUCCESS) {
 
-failed:
-       talloc_free(mods);
-       return NULL;
-}
+               /* if the callback failed the caller will have freed the
+                * request. Just return and don't try to use it */
+               if (callback_failed) {
 
+                       /* tell lldb_wait to remove the request from the
+                        *  queue */
+                       lret = true;
+                       goto free_and_return;
+               }
 
-/*
-  add a record
-*/
-static int lldb_add(struct ldb_module *module, const struct ldb_message *msg)
-{
-       struct ldb_context *ldb = module->ldb;
-       struct lldb_private *lldb = module->private_data;
-       LDAPMod **mods;
-       int ret = 0;
+               request_done = true;
+       }
 
-       /* ignore ltdb specials */
-       if (msg->dn[0] == '@') {
-               return 0;
+       if (request_done) {
+               lldb_request_done(ac->req, ac->controls, ret);
+               lret = true;
+               goto free_and_return;
        }
 
-       mods = lldb_msg_to_mods(ldb, msg, 0);
+       lret = false;
 
-       lldb->last_rc = ldap_add_s(lldb->ldap, msg->dn, mods);
-       if (lldb->last_rc != LDAP_SUCCESS) {
-               ret = -1;
+free_and_return:
+
+       if (matcheddnp) ldap_memfree(matcheddnp);
+       if (errmsgp && *errmsgp) {
+               ldb_set_errstring(ac->module->ldb, errmsgp);
+       }
+       if (errmsgp) {
+               ldap_memfree(errmsgp);
        }
+       if (referralsp) ldap_value_free(referralsp);
+       if (serverctrlsp) ldap_controls_free(serverctrlsp);
 
-       talloc_free(mods);
+       ldap_msgfree(result);
 
-       return ret;
+       return lret;
 }
 
+static void lldb_timeout(struct event_context *ev,
+                        struct timed_event *te,
+                        struct timeval t,
+                        void *private_data)
+{
+       struct lldb_context *ac;
+       ac = talloc_get_type(private_data, struct lldb_context);
 
-/*
-  modify a record
-*/
-static int lldb_modify(struct ldb_module *module, const struct ldb_message *msg)
+       lldb_request_done(ac->req, NULL, LDB_ERR_TIME_LIMIT_EXCEEDED);
+}
+
+static void lldb_callback(struct event_context *ev,
+                         struct timed_event *te,
+                         struct timeval t,
+                         void *private_data)
 {
-       struct ldb_context *ldb = module->ldb;
-       struct lldb_private *lldb = module->private_data;
-       LDAPMod **mods;
-       int ret = 0;
+       struct lldb_context *ac;
+       struct timed_event *lte;
+       struct timeval tv;
+       LDAPMessage *result;
+       int lret;
+
+       ac = talloc_get_type(private_data, struct lldb_context);
 
-       /* ignore ltdb specials */
-       if (msg->dn[0] == '@') {
-               return 0;
+       if (!ac->msgid) {
+               lldb_request_done(ac->req, NULL, LDB_ERR_OPERATIONS_ERROR);
+               return;
        }
 
-       mods = lldb_msg_to_mods(ldb, msg, 1);
+       tv.tv_sec = 0;
+       tv.tv_usec = 0;
+       lret = ldap_result(ac->lldb->ldap, ac->msgid, 0, &tv, &result);
+       if (lret == 0) {
+               goto respin;
+       }
+       if (lret == -1) {
+               lldb_request_done(ac->req, NULL, LDB_ERR_OPERATIONS_ERROR);
+               return;
+       }
 
-       lldb->last_rc = ldap_modify_s(lldb->ldap, msg->dn, mods);
-       if (lldb->last_rc != LDAP_SUCCESS) {
-               ret = -1;
+       if ( ! lldb_parse_result(ac, result)) {
+               goto respin;
        }
 
-       talloc_free(mods);
+       return;
 
-       return ret;
+respin:
+       tv.tv_sec = 0;
+       tv.tv_usec = 100;
+       lte = event_add_timed(ev, ac, tv, lldb_callback, ac);
+       if (NULL == lte) {
+               lldb_request_done(ac->req, NULL, LDB_ERR_OPERATIONS_ERROR);
+       }
 }
 
-static int lldb_lock(struct ldb_module *module, const char *lockname)
+static bool lldb_dn_is_special(struct ldb_request *req)
 {
-       int ret = 0;
+       struct ldb_dn *dn = NULL;
+
+       switch (req->operation) {
+       case LDB_ADD:
+               dn = req->op.add.message->dn;
+               break;
+       case LDB_MODIFY:
+               dn = req->op.mod.message->dn;
+               break;
+       case LDB_DELETE:
+               dn = req->op.del.dn;
+               break;
+       case LDB_RENAME:
+               dn = req->op.rename.olddn;
+               break;
+       default:
+               break;
+       }
 
-       if (lockname == NULL) {
-               return -1;
+       if (dn && ldb_dn_is_special(dn)) {
+               return true;
        }
+       return false;
+}
 
-       /* TODO implement a local locking mechanism here */
+static void lldb_auto_done_callback(struct event_context *ev,
+                                   struct timed_event *te,
+                                   struct timeval t,
+                                   void *private_data)
+{
+       struct lldb_context *ac;
 
-       return ret;
+       ac = talloc_get_type(private_data, struct lldb_context);
+       lldb_request_done(ac->req, NULL, LDB_SUCCESS);
 }
 
-static int lldb_unlock(struct ldb_module *module, const char *lockname)
+static int lldb_handle_request(struct ldb_module *module, struct ldb_request *req)
 {
-       int ret = 0;
+       struct lldb_private *lldb;
+       struct lldb_context *ac;
+       struct event_context *ev;
+       struct timed_event *te;
+       struct timeval tv;
+       int ret;
+
+       lldb = talloc_get_type(module->private_data, struct lldb_private);
+
+       if (req->starttime == 0 || req->timeout == 0) {
+               ldb_set_errstring(module->ldb, "Invalid timeout settings");
+               return LDB_ERR_TIME_LIMIT_EXCEEDED;
+       }
 
-       if (lockname == NULL) {
-               return -1;
+       ev = ldb_get_event_context(module->ldb);
+       if (NULL == ev) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       /* TODO implement a local unlocking mechanism here */
+       ac = talloc_zero(module->ldb, struct lldb_context);
+       if (ac == NULL) {
+               ldb_set_errstring(module->ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-       return ret;
-}
+       ac->module = module;
+       ac->req = req;
+       ac->lldb = lldb;
+       ac->msgid = 0;
+
+       if (lldb_dn_is_special(req)) {
+               tv.tv_sec = 0;
+               tv.tv_usec = 0;
+               te = event_add_timed(ev, ac, tv,
+                                    lldb_auto_done_callback, ac);
+               if (NULL == te) {
+                       return LDB_ERR_OPERATIONS_ERROR;
+               }
 
-/*
-  return extended error information
-*/
-static const char *lldb_errstring(struct ldb_module *module)
-{
-       struct lldb_private *lldb = module->private_data;
-       return ldap_err2string(lldb->last_rc);
-}
+               return LDB_SUCCESS;
+       }
+
+       switch (ac->req->operation) {
+       case LDB_SEARCH:
+               ret = lldb_search(ac);
+               break;
+       case LDB_ADD:
+               ret = lldb_add(ac);
+               break;
+       case LDB_MODIFY:
+               ret = lldb_modify(ac);
+               break;
+       case LDB_DELETE:
+               ret = lldb_delete(ac);
+               break;
+       case LDB_RENAME:
+               ret = lldb_rename(ac);
+               break;
+       default:
+               /* no other op supported */
+               ret = LDB_ERR_OPERATIONS_ERROR;
+               break;
+       }
+
+       if (ret != LDB_SUCCESS) {
+               lldb_request_done(req, NULL, ret);
+               return ret;
+       }
+
+       tv.tv_sec = 0;
+       tv.tv_usec = 0;
+       te = event_add_timed(ev, ac, tv, lldb_callback, ac);
+       if (NULL == te) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
 
+       tv.tv_sec = req->starttime + req->timeout;
+       tv.tv_usec = 0;
+       te = event_add_timed(ev, ac, tv, lldb_timeout, ac);
+       if (NULL == te) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       return LDB_SUCCESS;
+}
+
 static const struct ldb_module_ops lldb_ops = {
-       "ldap",
-       lldb_close, 
-       lldb_search,
-       lldb_search_free,
-       lldb_add,
-       lldb_modify,
-       lldb_delete,
-       lldb_rename,
-       lldb_lock,
-       lldb_unlock,
-       lldb_errstring
+       .name              = "ldap",
+       .search            = lldb_handle_request,
+       .add               = lldb_handle_request,
+       .modify            = lldb_handle_request,
+       .del               = lldb_handle_request,
+       .rename            = lldb_handle_request,
+       .request           = lldb_handle_request,
+       .start_transaction = lldb_start_trans,
+       .end_transaction   = lldb_end_trans,
+       .del_transaction   = lldb_del_trans,
 };
 
 
-static int lldb_destructor(void *p)
+static int lldb_destructor(struct lldb_private *lldb)
 {
-       struct lldb_private *lldb = p;
        ldap_unbind(lldb->ldap);
        return 0;
 }
@@ -491,74 +824,71 @@ static int lldb_destructor(void *p)
 /*
   connect to the database
 */
-struct ldb_context *lldb_connect(const char *url, 
-                                unsigned int flags, 
-                                const char *options[])
+static int lldb_connect(struct ldb_context *ldb,
+                       const char *url,
+                       unsigned int flags,
+                       const char *options[],
+                       struct ldb_module **_module)
 {
-       struct ldb_context *ldb = NULL;
-       struct lldb_private *lldb = NULL;
-       int i, version = 3;
-
-       ldb = talloc_p(NULL, struct ldb_context);
-       if (!ldb) {
-               errno = ENOMEM;
-               goto failed;
+       struct ldb_module *module;
+       struct lldb_private *lldb;
+       int version = 3;
+       int ret;
+
+       module = talloc(ldb, struct ldb_module);
+       if (module == NULL) {
+               ldb_oom(ldb);
+               talloc_free(lldb);
+               return -1;
        }
+       talloc_set_name_const(module, "ldb_ldap backend");
+       module->ldb             = ldb;
+       module->prev            = module->next = NULL;
+       module->ops             = &lldb_ops;
 
-       lldb = talloc_p(ldb, struct lldb_private);
+       lldb = talloc(module, struct lldb_private);
        if (!lldb) {
-               errno = ENOMEM;
+               ldb_oom(ldb);
                goto failed;
        }
+       module->private_data    = lldb;
+       lldb->ldap              = NULL;
 
-       lldb->ldap = NULL;
-       lldb->options = NULL;
-
-       lldb->last_rc = ldap_initialize(&lldb->ldap, url);
-       if (lldb->last_rc != LDAP_SUCCESS) {
+       ret = ldap_initialize(&lldb->ldap, url);
+       if (ret != LDAP_SUCCESS) {
+               ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_initialize failed for URL '%s' - %s\n",
+                         url, ldap_err2string(ret));
                goto failed;
        }
 
        talloc_set_destructor(lldb, lldb_destructor);
 
-       lldb->last_rc = ldap_set_option(lldb->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
-       if (lldb->last_rc != LDAP_SUCCESS) {
+       ret = ldap_set_option(lldb->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
+       if (ret != LDAP_SUCCESS) {
+               ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_set_option failed - %s\n",
+                         ldap_err2string(ret));
                goto failed;
        }
 
-       ldb->modules = talloc_p(ldb, struct ldb_module);
-       if (!ldb->modules) {
-               errno = ENOMEM;
-               goto failed;
-       }
-       ldb->modules->ldb = ldb;
-       ldb->modules->prev = ldb->modules->next = NULL;
-       ldb->modules->private_data = lldb;
-       ldb->modules->ops = &lldb_ops;
-
-       if (options) {
-               /* take a copy of the options array, so we don't have to rely
-                  on the caller keeping it around (it might be dynamic) */
-               for (i=0;options[i];i++) ;
-
-               lldb->options = talloc_array_p(lldb, char *, i+1);
-               if (!lldb->options) {
-                       goto failed;
-               }
-               
-               for (i=0;options[i];i++) {
-                       lldb->options[i+1] = NULL;
-                       lldb->options[i] = talloc_strdup(lldb->options, options[i]);
-                       if (!lldb->options[i]) {
-                               goto failed;
-                       }
-               }
-       }
-
-       return ldb;
+       *_module = module;
+       return 0;
 
 failed:
-       talloc_free(ldb);
-       return NULL;
+       talloc_free(module);
+       return -1;
 }
 
+const struct ldb_backend_ops ldb_ldap_backend_ops = {
+       .name = "ldap",
+       .connect_fn = lldb_connect
+};
+
+const struct ldb_backend_ops ldb_ldapi_backend_ops = {
+       .name = "ldapi",
+       .connect_fn = lldb_connect
+};
+
+const struct ldb_backend_ops ldb_ldaps_backend_ops = {
+       .name = "ldaps",
+       .connect_fn = lldb_connect
+};