r19831: Big ldb_dn optimization and interfaces enhancement patch
[ira/wip.git] / source4 / lib / ldb / ldb_ldap / ldb_ldap.c
index c27ded7767625d43b9f433599dd83a09c4bd4ba4..ee2d818935d3477838549afc46b2d68cc0772ad4 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
 */
 
 /*
- *  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/includes.h"
 
-#include "ldb/ldb_ldap/ldb_ldap.h"
+#define LDAP_DEPRECATED 1
+#include <ldap.h>
 
-/*
-  rename a record
-*/
-static int lldb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
-{
-       TALLOC_CTX *local_ctx;
-       struct lldb_private *lldb = module->private_data;
-       int ret = 0;
-       char *old_dn;
-       char *newrdn;
-       const char *parentdn = "";
+struct lldb_private {
+       LDAP *ldap;
+};
 
-       /* ignore ltdb specials */
-       if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
-               return 0;
-       }
+struct lldb_context {
+       struct ldb_module *module;
+       int msgid;
+       int timeout;
+       time_t starttime;
+       void *context;
+       int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
+};
 
-       local_ctx = talloc_named(lldb, 0, "lldb_rename local context");
-       if (local_ctx == NULL) {
-               return -1;
-       }
+static int lldb_ldap_to_ldb(int err) {
+       /* Ldap errors and ldb errors are defined to the same values */
+       return err;
+}
 
-       old_dn = ldb_dn_linearize(local_ctx, olddn);
-       if (old_dn == NULL) {
-               goto failed;
-       }
+static struct ldb_handle *init_handle(struct lldb_private *lldb, struct ldb_module *module,
+                                           void *context,
+                                           int (*callback)(struct ldb_context *, void *, struct ldb_reply *),
+                                           int timeout, time_t starttime)
+{
+       struct lldb_context *ac;
+       struct ldb_handle *h;
 
-       newrdn = talloc_asprintf(lldb, "%s=%s",
-                                     newdn->components[0].name,
-                                     ldb_dn_escape_value(lldb, newdn->components[0].value));
-       if (!newrdn) {
-               goto failed;
+       h = talloc_zero(lldb, struct ldb_handle);
+       if (h == NULL) {
+               ldb_set_errstring(module->ldb, "Out of Memory");
+               return NULL;
        }
 
-       parentdn = ldb_dn_linearize(lldb, ldb_dn_get_parent(lldb, newdn));
-       if (!parentdn) {
-               goto failed;
-       }
+       h->module = module;
 
-       lldb->last_rc = ldap_rename_s(lldb->ldap, old_dn, newrdn, parentdn, 1, NULL, NULL);
-       if (lldb->last_rc != LDAP_SUCCESS) {
-               ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(lldb->last_rc)));
-               ret = -1;
+       ac = talloc(h, struct lldb_context);
+       if (ac == NULL) {
+               ldb_set_errstring(module->ldb, "Out of Memory");
+               talloc_free(h);
+               return NULL;
        }
 
-       talloc_free(local_ctx);
-       return ret;
+       h->private_data = (void *)ac;
 
-failed:
-       talloc_free(local_ctx);
-       return -1;
-}
+       h->state = LDB_ASYNC_INIT;
+       h->status = LDB_SUCCESS;
 
+       ac->module = module;
+       ac->context = context;
+       ac->callback = callback;
+       ac->timeout = timeout;
+       ac->starttime = starttime;
+       ac->msgid = 0;
+
+       return h;
+}
 /*
-  delete a record
+  convert a ldb_message structure to a list of LDAPMod structures
+  ready for ldap_add() or ldap_modify()
 */
-static int lldb_delete(struct ldb_module *module, const struct ldb_dn *edn)
+static LDAPMod **lldb_msg_to_mods(void *mem_ctx, const struct ldb_message *msg, int use_flags)
 {
-       struct lldb_private *lldb = module->private_data;
-       char *dn;
-       int ret = 0;
+       LDAPMod **mods;
+       unsigned int i, j;
+       int num_mods = 0;
 
-       /* ignore ltdb specials */
-       if (ldb_dn_is_special(edn)) {
-               return 0;
+       /* 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;
+
+       for (i=0;i<msg->num_elements;i++) {
+               const struct ldb_message_element *el = &msg->elements[i];
 
-       dn = ldb_dn_linearize(lldb, edn);
+               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;
+               }
 
-       lldb->last_rc = ldap_delete_s(lldb->ldap, dn);
-       if (lldb->last_rc != LDAP_SUCCESS) {
-               ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(lldb->last_rc)));
-               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++;
        }
 
-       talloc_free(dn);
-       return ret;
+       return mods;
+
+failed:
+       talloc_free(mods);
+       return NULL;
 }
 
 /*
@@ -158,10 +210,15 @@ static int lldb_add_msg_attr(struct ldb_context *ldb,
        }
 
        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++;
        }
@@ -174,32 +231,51 @@ static int lldb_add_msg_attr(struct ldb_context *ldb,
 /*
   search for matching records
 */
-static int lldb_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
-                             enum ldb_scope scope, struct ldb_parse_tree *tree,
-                             const char * const *attrs, struct ldb_result **res)
+static int lldb_search(struct ldb_module *module, struct ldb_request *req)
 {
-       struct ldb_context *ldb = module->ldb;
-       struct lldb_private *lldb = module->private_data;
-       int count, msg_count, ldap_scope;
+       struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
+       struct lldb_context *lldb_ac;
+       struct timeval tv;
+       int ldap_scope;
        char *search_base;
-       LDAPMessage *ldapres, *msg;
        char *expression;
+       int ret;
 
-       search_base = ldb_dn_linearize(ldb, base);
-       if (base == NULL) {
-               search_base = talloc_strdup(ldb, "");
+       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 (req->op.search.tree == NULL) {
+               ldb_set_errstring(module->ldb, "Invalid expression parse tree");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       if (req->controls != NULL) {
+               ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls are not yet supported by ldb_ldap backend!\n");
+       }
+
+       req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
+       if (req->handle == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
+
+       search_base = ldb_dn_linearize(lldb_ac, req->op.search.base);
+       if (req->op.search.base == NULL) {
+               search_base = talloc_strdup(lldb_ac, "");
        }
        if (search_base == NULL) {
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       expression = ldb_filter_from_tree(search_base, tree);
+       expression = ldb_filter_from_tree(lldb_ac, req->op.search.tree);
        if (expression == NULL) {
-               talloc_free(search_base);
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       switch (scope) {
+       switch (req->op.search.scope) {
        case LDB_SCOPE_BASE:
                ldap_scope = LDAP_SCOPE_BASE;
                break;
@@ -211,252 +287,448 @@ static int lldb_search_bytree(struct ldb_module *module, const struct ldb_dn *ba
                break;
        }
 
-       (*res) = talloc(lldb, struct ldb_result);
-       if (! *res) {
-               errno = ENOMEM;
-               return LDB_ERR_OTHER;
-       }
-       (*res)->count = 0;
-       (*res)->msgs = NULL;
-       (*res)->controls = NULL;
+       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);
 
-       lldb->last_rc = ldap_search_s(lldb->ldap, search_base, ldap_scope, 
-                                     expression, 
-                                     discard_const_p(char *, attrs), 
-                                     0, &ldapres);
-       talloc_free(search_base);
-       if (lldb->last_rc != LDAP_SUCCESS) {
-               ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(lldb->last_rc)));
-               return lldb->last_rc;
+       if (ret != LDAP_SUCCESS) {
+               ldb_set_errstring(module->ldb, ldap_err2string(ret));
        }
 
-       count = ldap_count_entries(lldb->ldap, ldapres);
-       if (count == -1 || count == 0) {
-               ldap_msgfree(ldapres);
-               return LDB_SUCCESS;
+       return lldb_ldap_to_ldb(ret);
+}
+
+/*
+  add a record
+*/
+static int lldb_add(struct ldb_module *module, struct ldb_request *req)
+{
+       struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
+       struct lldb_context *lldb_ac;
+       LDAPMod **mods;
+       char *dn;
+       int ret;
+
+       /* ltdb specials should not reach this point */
+       if (ldb_dn_is_special(req->op.add.message->dn)) {
+               return LDB_ERR_INVALID_DN_SYNTAX;
        }
 
-       (*res)->msgs = talloc_array(*res, struct ldb_message *, count+1);
-       if (! (*res)->msgs) {
-               ldap_msgfree(ldapres);
-               talloc_free(*res);
-               errno = ENOMEM;
-               return LDB_ERR_OTHER;
+       req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
+       if (req->handle == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       (*res)->msgs[0] = NULL;
+       lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
 
-       msg_count = 0;
+       mods = lldb_msg_to_mods(lldb_ac, req->op.add.message, 0);
+       if (mods == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-       /* 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;
+       dn = ldb_dn_linearize(lldb_ac, req->op.add.message->dn);
+       if (dn == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-               if (msg_count == count) {
-                       /* hmm, got too many? */
-                       ldb_debug(ldb, LDB_DEBUG_FATAL, "Fatal: ldap message count inconsistent\n");
-                       break;
-               }
+       ret = ldap_add_ext(lldb->ldap, dn, mods,
+                          NULL,
+                          NULL,
+                          &lldb_ac->msgid);
 
-               (*res)->msgs[msg_count] = talloc((*res)->msgs, struct ldb_message);
-               if (!(*res)->msgs[msg_count]) {
-                       goto failed;
-               }
-               (*res)->msgs[msg_count+1] = NULL;
+       if (ret != LDAP_SUCCESS) {
+               ldb_set_errstring(module->ldb, ldap_err2string(ret));
+       }
 
-               dn = ldap_get_dn(lldb->ldap, msg);
-               if (!dn) {
-                       goto failed;
-               }
+       return lldb_ldap_to_ldb(ret);
+}
 
-               (*res)->msgs[msg_count]->dn = ldb_dn_explode_or_special((*res)->msgs[msg_count], dn);
-               ldap_memfree(dn);
-               if (!(*res)->msgs[msg_count]->dn) {
-                       goto failed;
-               }
+/*
+  modify a record
+*/
+static int lldb_modify(struct ldb_module *module, struct ldb_request *req)
+{
+       struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
+       struct lldb_context *lldb_ac;
+       LDAPMod **mods;
+       char *dn;
+       int ret;
 
+       /* ltdb specials should not reach this point */
+       if (ldb_dn_is_special(req->op.mod.message->dn)) {
+               return LDB_ERR_INVALID_DN_SYNTAX;
+       }
 
-               (*res)->msgs[msg_count]->num_elements = 0;
-               (*res)->msgs[msg_count]->elements = NULL;
-               (*res)->msgs[msg_count]->private_data = NULL;
+       req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
+       if (req->handle == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-               /* 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);
+       lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
 
-                       if (bval) {
-                               lldb_add_msg_attr(ldb, (*res)->msgs[msg_count], attr, bval);
-                               ldap_value_free_len(bval);
-                       }                                         
-                       
-                       ldap_memfree(attr);
-               }
-               if (berptr) ber_free(berptr, 0);
+       mods = lldb_msg_to_mods(lldb_ac, req->op.mod.message, 1);
+       if (mods == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-               msg_count++;
+       dn = ldb_dn_linearize(lldb_ac, req->op.mod.message->dn);
+       if (dn == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       ldap_msgfree(ldapres);
+       ret = ldap_modify_ext(lldb->ldap, dn, mods,
+                             NULL,
+                             NULL,
+                             &lldb_ac->msgid);
 
-       (*res)->count = msg_count;
-       return LDB_SUCCESS;
+       if (ret != LDAP_SUCCESS) {
+               ldb_set_errstring(module->ldb, ldap_err2string(ret));
+       }
 
-failed:
-       if (*res) talloc_free(*res);
-       return LDB_ERR_OTHER;
+       return lldb_ldap_to_ldb(ret);
 }
 
-
 /*
-  convert a ldb_message structure to a list of LDAPMod structures
-  ready for ldap_add() or ldap_modify()
+  delete a record
 */
-static LDAPMod **lldb_msg_to_mods(struct ldb_context *ldb,
-                                 const struct ldb_message *msg, int use_flags)
+static int lldb_delete(struct ldb_module *module, struct ldb_request *req)
 {
-       LDAPMod **mods;
-       unsigned int i, j;
-       int num_mods = 0;
+       struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
+       struct lldb_context *lldb_ac;
+       char *dnstr;
+       int ret;
+       
+       /* ltdb specials should not reach this point */
+       if (ldb_dn_is_special(req->op.del.dn)) {
+               return LDB_ERR_INVALID_DN_SYNTAX;
+       }
 
-       /* allocate maximum number of elements needed */
-       mods = talloc_array(ldb, LDAPMod *, msg->num_elements+1);
-       if (!mods) {
-               errno = ENOMEM;
-               return NULL;
+       req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
+       if (req->handle == NULL) {
+               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];
+       lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
 
-               mods[num_mods] = talloc(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;
-                               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;
-               }
+       dnstr = ldb_dn_linearize(lldb_ac, req->op.del.dn);
 
-               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++;
-       }
+       ret = ldap_delete_ext(lldb->ldap, dnstr,
+                             NULL,
+                             NULL,
+                             &lldb_ac->msgid);
 
-       return mods;
+       if (ret != LDAP_SUCCESS) {
+               ldb_set_errstring(module->ldb, ldap_err2string(ret));
+       }
 
-failed:
-       talloc_free(mods);
-       return NULL;
+       return lldb_ldap_to_ldb(ret);
 }
 
-
 /*
-  add a record
+  rename a record
 */
-static int lldb_add(struct ldb_module *module, const struct ldb_message *msg)
+static int lldb_rename(struct ldb_module *module, struct ldb_request *req)
 {
-       struct ldb_context *ldb = module->ldb;
-       struct lldb_private *lldb = module->private_data;
-       LDAPMod **mods;
-       char *dn;
-       int ret = 0;
+       struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
+       struct lldb_context *lldb_ac;
+       char *old_dn;
+               char *newrdn;
+       char *parentdn;
+       int ret;
+       
+       /* ltdb specials should not reach this point */
+       if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
+               return LDB_ERR_INVALID_DN_SYNTAX;
+       }
 
-       /* ignore ltdb specials */
-       if (ldb_dn_is_special(msg->dn)) {
-               return 0;
+       req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
+       if (req->handle == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       mods = lldb_msg_to_mods(ldb, msg, 0);
-       if (mods == NULL) {
-               return -1;
+       lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
+
+       old_dn = ldb_dn_linearize(lldb_ac, req->op.rename.olddn);
+       if (old_dn == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       dn = ldb_dn_linearize(mods, msg->dn);
-       if (dn == NULL) {
-               talloc_free(mods);
-               return -1;
+       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;
        }
 
-       lldb->last_rc = ldap_add_s(lldb->ldap, dn, mods);
-       if (lldb->last_rc != LDAP_SUCCESS) {
-               ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(lldb->last_rc)));
-               ret = -1;
+       parentdn = ldb_dn_linearize(lldb_ac, ldb_dn_get_parent(lldb_ac, req->op.rename.newdn));
+       if (!parentdn) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       talloc_free(mods);
+       ret = ldap_rename(lldb->ldap, old_dn, newrdn, parentdn,
+                         1, NULL, NULL,
+                         &lldb_ac->msgid);
 
-       return ret;
-}
+       if (ret != LDAP_SUCCESS) {
+               ldb_set_errstring(module->ldb, ldap_err2string(ret));
+       }
 
+       return lldb_ldap_to_ldb(ret);
+}
 
-/*
-  modify a record
-*/
-static int lldb_modify(struct ldb_module *module, const struct ldb_message *msg)
+static int lldb_parse_result(struct ldb_handle *handle, LDAPMessage *result)
 {
-       struct ldb_context *ldb = module->ldb;
-       struct lldb_private *lldb = module->private_data;
-       LDAPMod **mods;
-       char *dn;
-       int ret = 0;
+       struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
+       struct lldb_private *lldb = talloc_get_type(ac->module->private_data, struct lldb_private);
+       struct ldb_reply *ares = NULL;
+       LDAPMessage *msg;
+       int type;
+       char *matcheddnp = NULL;
+       char *errmsgp = NULL;
+       char **referralsp = NULL;
+       LDAPControl **serverctrlsp = NULL;
+       int ret = LDB_SUCCESS;
+       
+       type = ldap_msgtype(result);
+
+       handle->status = 0;
+
+       switch (type) {
+
+       case LDAP_RES_SEARCH_ENTRY:
+               msg = ldap_first_entry(lldb->ldap, result);
+               if (msg != NULL) {
+                       BerElement *berptr = NULL;
+                       char *attr, *dn;
+
+                       ares = talloc_zero(ac, struct ldb_reply);
+                       if (!ares) {
+                               ret = LDB_ERR_OPERATIONS_ERROR;
+                               goto error;
+                       }
+
+                       ares->message = ldb_msg_new(ares);
+                       if (!ares->message) {
+                               ret = LDB_ERR_OPERATIONS_ERROR;
+                               goto error;
+                       }
+
+                       dn = ldap_get_dn(lldb->ldap, msg);
+                       if (!dn) {
+                               ret = LDB_ERR_OPERATIONS_ERROR;
+                               goto error;
+                       }
+                       ares->message->dn = ldb_dn_new(ares->message, ac->module->ldb, dn);
+                       if ( ! ldb_dn_validate(ares->message->dn)) {
+                               ret = LDB_ERR_OPERATIONS_ERROR;
+                               goto error;
+                       }
+                       ldap_memfree(dn);
+
+                       ares->message->num_elements = 0;
+                       ares->message->elements = NULL;
+                       ares->message->private_data = 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, ares->message, attr, bval);
+                                       ldap_value_free_len(bval);
+                               }                                         
+                       }
+                       if (berptr) ber_free(berptr, 0);
+
+
+                       ares->type = LDB_REPLY_ENTRY;
+                       ret = ac->callback(ac->module->ldb, ac->context, ares);
+               } else {
+                       handle->status = LDB_ERR_PROTOCOL_ERROR;
+                       handle->state = LDB_ASYNC_DONE;
+               }
+               break;
+
+       case LDAP_RES_SEARCH_REFERENCE:
+               if (ldap_parse_result(lldb->ldap, result, &handle->status,
+                                       &matcheddnp, &errmsgp,
+                                       &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
+                       ret = LDB_ERR_OPERATIONS_ERROR;
+                       goto error;
+               }
+               if (referralsp == NULL) {
+                       handle->status = LDB_ERR_PROTOCOL_ERROR;
+                       goto error;
+               }
 
-       /* ignore ltdb specials */
-       if (ldb_dn_is_special(msg->dn)) {
-               return 0;
+               ares = talloc_zero(ac, struct ldb_reply);
+               if (!ares) {
+                       ret = LDB_ERR_OPERATIONS_ERROR;
+                       goto error;
+               }
+
+               ares->referral = talloc_strdup(ares, *referralsp);
+               ares->type = LDB_REPLY_REFERRAL;
+               ret = ac->callback(ac->module->ldb, ac->context, ares);
+
+               break;
+
+       case LDAP_RES_SEARCH_RESULT:
+               if (ldap_parse_result(lldb->ldap, result, &handle->status,
+                                       &matcheddnp, &errmsgp,
+                                       &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
+                       handle->status = LDB_ERR_OPERATIONS_ERROR;
+                       goto error;
+               }
+
+               ares = talloc_zero(ac, struct ldb_reply);
+               if (!ares) {
+                       ret = LDB_ERR_OPERATIONS_ERROR;
+                       goto error;
+               }
+
+               if (serverctrlsp != NULL) {
+                       /* FIXME: transform the LDAPControl list into an ldb_control one */
+                       ares->controls = NULL;
+               }
+               
+               ares->type = LDB_REPLY_DONE;
+               handle->state = LDB_ASYNC_DONE;
+               ret = ac->callback(ac->module->ldb, ac->context, ares);
+
+               break;
+
+       case LDAP_RES_MODIFY:
+       case LDAP_RES_ADD:
+       case LDAP_RES_DELETE:
+       case LDAP_RES_MODDN:
+               if (ldap_parse_result(lldb->ldap, result, &handle->status,
+                                       &matcheddnp, &errmsgp,
+                                       &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
+                       handle->status = LDB_ERR_OPERATIONS_ERROR;
+                       goto error;
+               }
+               if (ac->callback && handle->status == LDB_SUCCESS) {
+                       ares = NULL; /* FIXME: build a corresponding ares to pass on */
+                       ret = ac->callback(ac->module->ldb, ac->context, ares);
+               }
+               handle->state = LDB_ASYNC_DONE;
+               break;
+
+       default:
+               ret = LDB_ERR_PROTOCOL_ERROR;
+               goto error;
        }
 
-       mods = lldb_msg_to_mods(ldb, msg, 1);
-       if (mods == NULL) {
-               return -1;
+       if (matcheddnp) ldap_memfree(matcheddnp);
+       if (errmsgp && *errmsgp) {
+               ldb_set_errstring(ac->module->ldb, errmsgp);
+       } else if (handle->status) {
+               ldb_set_errstring(ac->module->ldb, ldap_err2string(handle->status));
+       }
+       if (errmsgp) {
+               ldap_memfree(errmsgp);
        }
+       if (referralsp) ldap_value_free(referralsp);
+       if (serverctrlsp) ldap_controls_free(serverctrlsp);
 
-       dn = ldb_dn_linearize(mods, msg->dn);
-       if (dn == NULL) {
-               talloc_free(mods);
-               return -1;
+       ldap_msgfree(result);
+       return lldb_ldap_to_ldb(handle->status);
+
+error:
+       handle->state = LDB_ASYNC_DONE;
+       ldap_msgfree(result);
+       return ret;
+}
+
+static int lldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
+{
+       struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
+       struct lldb_private *lldb = talloc_get_type(handle->module->private_data, struct lldb_private);
+       struct timeval timeout;
+       LDAPMessage *result;
+       int ret, lret;
+
+       if (handle->state == LDB_ASYNC_DONE) {
+               return handle->status;
        }
 
-       lldb->last_rc = ldap_modify_s(lldb->ldap, dn, mods);
-       if (lldb->last_rc != LDAP_SUCCESS) {
-               ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(lldb->last_rc)));
-               ret = -1;
+       if (!ac || !ac->msgid) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       talloc_free(mods);
+       handle->state = LDB_ASYNC_PENDING;
+       handle->status = LDB_SUCCESS;
+
+       switch(type) {
+       case LDB_WAIT_NONE:
+
+               if ((ac->timeout != -1) &&
+                   ((ac->starttime + ac->timeout) > time(NULL))) {
+                       return LDB_ERR_TIME_LIMIT_EXCEEDED;
+               }
+
+               timeout.tv_sec = 0;
+               timeout.tv_usec = 0;
+
+               lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
+               if (lret == -1) {
+                       return LDB_ERR_OPERATIONS_ERROR;
+               }
+               if (lret == 0) {
+                       ret = LDB_SUCCESS;
+                       goto done;
+               }
+
+               return lldb_parse_result(handle, result);
 
+       case LDB_WAIT_ALL:
+               timeout.tv_usec = 0;
+               ret = LDB_ERR_OPERATIONS_ERROR;
+
+               while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
+
+                       if (ac->timeout == -1) {
+                               lret = ldap_result(lldb->ldap, ac->msgid, 0, NULL, &result);
+                       } else {
+                               timeout.tv_sec = ac->timeout - (time(NULL) - ac->starttime);
+                               if (timeout.tv_sec <= 0)
+                                       return LDB_ERR_TIME_LIMIT_EXCEEDED;
+                               lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
+                       }
+                       if (lret == -1) {
+                               return LDB_ERR_OPERATIONS_ERROR;
+                       }
+                       if (lret == 0) {
+                               return LDB_ERR_TIME_LIMIT_EXCEEDED;
+                       }
+
+                       ret = lldb_parse_result(handle, result);
+                       if (ret != LDB_SUCCESS) {
+                               return ret;
+                       }
+               }
+
+               break;
+               
+       default:
+               handle->state = LDB_ASYNC_DONE;
+               ret = LDB_ERR_OPERATIONS_ERROR;
+       }
+
+done:
        return ret;
 }
 
@@ -464,73 +736,45 @@ static int lldb_start_trans(struct ldb_module *module)
 {
        /* TODO implement a local transaction mechanism here */
 
-       return 0;
+       return LDB_SUCCESS;
 }
 
 static int lldb_end_trans(struct ldb_module *module)
 {
        /* TODO implement a local transaction mechanism here */
 
-       return 0;
+       return LDB_SUCCESS;
 }
 
 static int lldb_del_trans(struct ldb_module *module)
 {
        /* TODO implement a local transaction mechanism here */
 
-       return 0;
+       return LDB_SUCCESS;
 }
 
 static int lldb_request(struct ldb_module *module, struct ldb_request *req)
 {
-       switch (req->operation) {
-
-       case LDB_REQ_SEARCH:
-               return lldb_search_bytree(module,
-                                         req->op.search.base,
-                                         req->op.search.scope, 
-                                         req->op.search.tree, 
-                                         req->op.search.attrs, 
-                                         &req->op.search.res);
-
-       case LDB_REQ_ADD:
-               return lldb_add(module, req->op.add.message);
-
-       case LDB_REQ_MODIFY:
-               return lldb_modify(module, req->op.mod.message);
-
-       case LDB_REQ_DELETE:
-               return lldb_delete(module, req->op.del.dn);
-
-       case LDB_REQ_RENAME:
-               return lldb_rename(module,
-                                       req->op.rename.olddn,
-                                       req->op.rename.newdn);
-
-       default:
-               return -1;
-
-       }
-}
-
-static int lldb_init_2(struct ldb_module *module)
-{
-       return LDB_SUCCESS;
+       return LDB_ERR_OPERATIONS_ERROR;
 }
 
 static const struct ldb_module_ops lldb_ops = {
        .name              = "ldap",
+       .search            = lldb_search,
+       .add               = lldb_add,
+       .modify            = lldb_modify,
+       .del               = lldb_delete,
+       .rename            = lldb_rename,
        .request           = lldb_request,
        .start_transaction = lldb_start_trans,
        .end_transaction   = lldb_end_trans,
        .del_transaction   = lldb_del_trans,
-       .second_stage_init = lldb_init_2
+       .wait              = lldb_wait
 };
 
 
-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;
 }
@@ -538,13 +782,15 @@ static int lldb_destructor(void *p)
 /*
   connect to the database
 */
-int lldb_connect(struct ldb_context *ldb,
-                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 lldb_private *lldb = NULL;
        int version = 3;
+       int ret;
 
        lldb = talloc(ldb, struct lldb_private);
        if (!lldb) {
@@ -553,32 +799,34 @@ int lldb_connect(struct ldb_context *ldb,
        }
 
        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(lldb->last_rc));
+                         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(lldb->last_rc));
+                         ldap_err2string(ret));
+               goto failed;
        }
 
-       ldb->modules = talloc(ldb, struct ldb_module);
-       if (!ldb->modules) {
+       *module = talloc(ldb, struct ldb_module);
+       if (*module == NULL) {
                ldb_oom(ldb);
-               goto failed;
+               talloc_free(lldb);
+               return -1;
        }
-       ldb->modules->ldb = ldb;
-       ldb->modules->prev = ldb->modules->next = NULL;
-       ldb->modules->private_data = lldb;
-       ldb->modules->ops = &lldb_ops;
+       talloc_set_name_const(*module, "ldb_ldap backend");
+       (*module)->ldb = ldb;
+       (*module)->prev = (*module)->next = NULL;
+       (*module)->private_data = lldb;
+       (*module)->ops = &lldb_ops;
 
        return 0;
 
@@ -587,3 +835,9 @@ failed:
        return -1;
 }
 
+int ldb_ldap_init(void)
+{
+       return ldb_register_backend("ldap", lldb_connect) +
+                  ldb_register_backend("ldapi", lldb_connect) + 
+                  ldb_register_backend("ldaps", lldb_connect);
+}