Now that we pass down the event context, start removing calls to
[metze/samba/wip.git] / source / lib / ldb / ldb_ildap / ldb_ildap.c
index 92514515491f6cae6beda333e35b752e21725985..d1677032f0b98bb75e7a9d5e36850b8c4a4d5473 100644 (file)
@@ -11,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
@@ -19,8 +19,7 @@
    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/>.
 */
 
 /*
 
 
 #include "includes.h"
-#include "ldb/include/includes.h"
+#include "ldb_includes.h"
 
 #include "lib/events/events.h"
 #include "libcli/ldap/ldap.h"
 #include "libcli/ldap/ldap_client.h"
 #include "auth/auth.h"
+#include "auth/credentials/credentials.h"
+#include "param/param.h"
 
 struct ildb_private {
        struct ldap_connection *ldap;
-       struct ldb_message *rootDSE;
-       struct ldb_context *ldb;
+       struct ldb_module *module;
 };
 
-struct ildb_async_context {
-       struct ldb_module *module;
+struct ildb_context {
+       struct ildb_private *ildb;
+       struct ldb_handle *handle;
        struct ldap_request *req;
        void *context;
-       int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
+       int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
 };
 
 /*
@@ -122,11 +123,16 @@ failed:
 */
 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
 {
+       TALLOC_CTX *mem_ctx = talloc_new(ildb);
        if (NT_STATUS_IS_OK(status)) {
                return LDB_SUCCESS;
        }
-       talloc_free(ildb->ldb->err_string);
-       ildb->ldb->err_string = talloc_strdup(ildb, ldap_errstr(ildb->ldap, status));
+       if (!mem_ctx) {
+               ldb_oom(ildb->module->ldb);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       ldb_set_errstring(ildb->module->ldb, ldap_errstr(ildb->ldap, mem_ctx, status));
+       talloc_free(mem_ctx);
        if (NT_STATUS_IS_LDAP(status)) {
                return NT_STATUS_LDAP_CODE(status);
        }
@@ -136,8 +142,8 @@ static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
 static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
                                 struct timeval t, void *private_data)
 {
-       struct ldb_async_handle *handle = talloc_get_type(private_data, struct ldb_async_handle);
-       struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
+       struct ildb_context *ac = talloc_get_type(private_data, struct ildb_context);
+       struct ldb_handle *handle = ac->handle;
 
        if (ac->req->state == LDAP_REQUEST_PENDING) {
                DLIST_REMOVE(ac->req->conn->pending, ac->req);
@@ -150,9 +156,9 @@ static void ildb_request_timeout(struct event_context *ev, struct timed_event *t
 
 static void ildb_callback(struct ldap_request *req)
 {
-       struct ldb_async_handle *handle = talloc_get_type(req->async.private_data, struct ldb_async_handle);
-       struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
-       struct ildb_private *ildb = talloc_get_type(ac->module->private_data, struct ildb_private);
+       struct ildb_context *ac = talloc_get_type(req->async.private_data, struct ildb_context);
+       struct ldb_handle *handle = ac->handle;
+       struct ildb_private *ildb = ac->ildb;
        NTSTATUS status;
        int i;
 
@@ -179,7 +185,7 @@ static void ildb_callback(struct ldap_request *req)
                handle->status = ildb_map_error(ildb, status);
                if (ac->callback && handle->status == LDB_SUCCESS) {
                        /* FIXME: build a corresponding ares to pass on */
-                       handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
+                       handle->status = ac->callback(ac->ildb->module->ldb, ac->context, NULL);
                }
                handle->state = LDB_ASYNC_DONE;
                break;
@@ -193,7 +199,7 @@ static void ildb_callback(struct ldap_request *req)
                handle->status = ildb_map_error(ildb, status);
                if (ac->callback && handle->status == LDB_SUCCESS) {
                        /* FIXME: build a corresponding ares to pass on */
-                       handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
+                       handle->status = ac->callback(ac->ildb->module->ldb, ac->context, NULL);
                }
                handle->state = LDB_ASYNC_DONE;
                break;
@@ -207,7 +213,7 @@ static void ildb_callback(struct ldap_request *req)
                handle->status = ildb_map_error(ildb, status);
                if (ac->callback && handle->status == LDB_SUCCESS) {
                        /* FIXME: build a corresponding ares to pass on */
-                       handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
+                       handle->status = ac->callback(ac->ildb->module->ldb, ac->context, NULL);
                }
                handle->state = LDB_ASYNC_DONE;
                break;
@@ -221,7 +227,7 @@ static void ildb_callback(struct ldap_request *req)
                handle->status = ildb_map_error(ildb, status);
                if (ac->callback && handle->status == LDB_SUCCESS) {
                        /* FIXME: build a corresponding ares to pass on */
-                       handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
+                       handle->status = ac->callback(ac->ildb->module->ldb, ac->context, NULL);
                }
                handle->state = LDB_ASYNC_DONE;
                break;
@@ -230,11 +236,11 @@ static void ildb_callback(struct ldap_request *req)
                /* loop over all messages */
                for (i = 0; i < req->num_replies; i++) {
                        struct ldap_SearchResEntry *search;
-                       struct ldb_async_result *ares = NULL;
+                       struct ldb_reply *ares = NULL;
                        struct ldap_message *msg;
                        int ret;
 
-                       ares = talloc_zero(ac, struct ldb_async_result);
+                       ares = talloc_zero(ac, struct ldb_reply);
                        if (!ares) {
                                handle->status = LDB_ERR_OPERATIONS_ERROR;
                                return;
@@ -247,17 +253,14 @@ static void ildb_callback(struct ldap_request *req)
 
                                status = ldap_check_response(req->conn, &msg->r.GeneralResult);
                                if (!NT_STATUS_IS_OK(status)) {
-                                       ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Error: %s\n" ,ldap_errstr(req->conn, status));
                                        handle->status = ildb_map_error(ildb, status);
                                        return;
                                }
                                
-                               if (msg->controls) {
-                                       ares->controls = talloc_steal(ares, msg->controls);
-                               }
+                               ares->controls = talloc_move(ares, &msg->controls);
                                if (msg->r.SearchResultDone.resultcode) {
                                        if (msg->r.SearchResultDone.errormessage) {
-                                               ldb_set_errstring(ac->module->ldb, talloc_strdup(ac->module, msg->r.SearchResultDone.errormessage));
+                                               ldb_set_errstring(ac->ildb->module->ldb, msg->r.SearchResultDone.errormessage);
                                        }
                                }
 
@@ -277,13 +280,14 @@ static void ildb_callback(struct ldap_request *req)
 
                                search = &(msg->r.SearchResultEntry);
                
-                               ares->message->dn = ldb_dn_explode_or_special(ares->message, search->dn);
-                               if (ares->message->dn == NULL) {
+                               ares->message->dn = ldb_dn_new(ares->message, ac->ildb->module->ldb, search->dn);
+                               if ( ! ldb_dn_validate(ares->message->dn)) {
                                        handle->status = LDB_ERR_OPERATIONS_ERROR;
                                        return;
                                }
                                ares->message->num_elements = search->num_attributes;
-                               ares->message->elements = talloc_steal(ares->message, search->attributes);
+                               ares->message->elements = talloc_move(ares->message,
+                                                                     &search->attributes);
 
                                handle->status = LDB_SUCCESS;
                                handle->state = LDB_ASYNC_PENDING;
@@ -305,7 +309,7 @@ static void ildb_callback(struct ldap_request *req)
                                return;
                        }
 
-                       ret = ac->callback(ac->module->ldb, ac->context, ares);
+                       ret = ac->callback(ac->ildb->module->ldb, ac->context, ares);
                        if (ret) {
                                handle->status = ret;
                        }
@@ -323,63 +327,90 @@ static void ildb_callback(struct ldap_request *req)
        }
 }
 
-static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg,
-                            void *context,
-                            int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
-                            int timeout,
-                            struct ldb_async_handle **handle)
+static struct ildb_context *init_ildb_handle(struct ildb_private *ildb,
+                                            struct ldb_request *req)
 {
-       struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
-       struct ildb_async_context *ildb_ac;
-       struct ldb_async_handle *h;
-       struct ldap_request *req;
+       struct ildb_context *ildb_ac;
+       struct ldb_handle *h;
 
-       h = talloc_zero(ildb->ldap, struct ldb_async_handle);
+       h = talloc_zero(req, struct ldb_handle);
        if (h == NULL) {
-               ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
-               return LDB_ERR_OPERATIONS_ERROR;
+               ldb_set_errstring(ildb->module->ldb, "Out of Memory");
+               return NULL;
        }
 
-       h->module = module;
+       h->module = ildb->module;
 
-       ildb_ac = talloc(h, struct ildb_async_context);
+       ildb_ac = talloc(h, struct ildb_context);
        if (ildb_ac == NULL) {
-               ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
+               ldb_set_errstring(ildb->module->ldb, "Out of Memory");
                talloc_free(h);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return NULL;
        }
 
-       h->private_data = (void *)ildb_ac;
+       h->private_data = ildb_ac;
 
        h->state = LDB_ASYNC_INIT;
        h->status = LDB_SUCCESS;
 
+       ildb_ac->ildb = ildb;
+       ildb_ac->handle = h;
+       ildb_ac->context = req->context;
+       ildb_ac->callback = req->callback;
+
+       req->handle = h;
+       return ildb_ac;
+}
+
+static int ildb_request_send(struct ildb_private *ildb, struct ldap_message *msg, struct ldb_request *r)
+{
+       struct ildb_context *ildb_ac = init_ildb_handle(ildb, r);
+       struct ldap_request *req;
+
+       if (!ildb_ac) {
+               return LDB_ERR_OPERATIONS_ERROR;                
+       }
+
        req = ldap_request_send(ildb->ldap, msg);
        if (req == NULL) {
-               ldb_set_errstring(module->ldb, talloc_asprintf(module, "async send request failed"));
+               ldb_set_errstring(ildb->module->ldb, "async send request failed");
                return LDB_ERR_OPERATIONS_ERROR;
        }
-
        ildb_ac->req = talloc_steal(ildb_ac, req);
-       ildb_ac->module = module;
-       ildb_ac->context = context;
-       ildb_ac->callback = callback;
 
-       req->async.fn = ildb_callback;
-       req->async.private_data = (void *)h;
+       if (!req->conn) {
+               ldb_set_errstring(ildb->module->ldb, "connection to remote LDAP server dropped?");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
        talloc_free(req->time_event);
        req->time_event = NULL;
-       if (timeout) {
-               req->time_event = event_add_timed(req->conn->event.event_ctx, h
-                                                 timeval_current_ofs(timeout, 0),
-                                                 ildb_request_timeout, h);
+       if (r->timeout) {
+               req->time_event = event_add_timed(req->conn->event.event_ctx, ildb_ac
+                                                 timeval_current_ofs(r->timeout, 0),
+                                                 ildb_request_timeout, ildb_ac);
        }
 
-       *handle = h;
+       req->async.fn = ildb_callback;
+       req->async.private_data = ildb_ac;
 
        return LDB_SUCCESS;
+}
+
+static int ildb_request_noop(struct ildb_private *ildb, struct ldb_request *req) 
+{
+       struct ildb_context *ildb_ac = init_ildb_handle(ildb, req);
+       int ret = LDB_SUCCESS;
+
+       if (!ildb_ac) {
+               return LDB_ERR_OPERATIONS_ERROR;                
+       }
 
+       if (ildb_ac->callback) {
+               ret = ildb_ac->callback(ildb->module->ldb, ildb_ac->context, NULL);
+       }
+       ildb_ac->handle->state = LDB_ASYNC_DONE;
+       return ret;
 }
 
 /*
@@ -391,38 +422,33 @@ static int ildb_search(struct ldb_module *module, struct ldb_request *req)
        struct ldap_message *msg;
        int n;
 
-       req->async.handle = NULL;
+       req->handle = NULL;
 
-       if (!req->async.callback || !req->async.context) {
-               ldb_set_errstring(module->ldb, talloc_asprintf(module, "Async interface called with NULL callback function or NULL context"));
+       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, talloc_asprintf(module, "Invalid expression parse tree"));
+               ldb_set_errstring(module->ldb, "Invalid expression parse tree");
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       msg = new_ldap_message(ildb);
+       msg = new_ldap_message(req);
        if (msg == NULL) {
-               ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
+               ldb_set_errstring(module->ldb, "Out of Memory");
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        msg->type = LDAP_TAG_SearchRequest;
 
        if (req->op.search.base == NULL) {
-               if (ildb->rootDSE != NULL) {
-                       msg->r.SearchRequest.basedn =
-                               talloc_strdup(msg, ldb_msg_find_string(ildb->rootDSE, "defaultNamingContext", ""));
-               } else {
-                       msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
-               }
+               msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
        } else {
-               msg->r.SearchRequest.basedn  = ldb_dn_linearize(msg, req->op.search.base);
+               msg->r.SearchRequest.basedn  = ldb_dn_alloc_linearized(msg, req->op.search.base);
        }
        if (msg->r.SearchRequest.basedn == NULL) {
-               ldb_set_errstring(module->ldb, talloc_asprintf(module, "Unable to determine baseDN"));
+               ldb_set_errstring(module->ldb, "Unable to determine baseDN");
                talloc_free(msg);
                return LDB_ERR_OPERATIONS_ERROR;
        }
@@ -444,7 +470,7 @@ static int ildb_search(struct ldb_module *module, struct ldb_request *req)
        msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs);
        msg->controls = req->controls;
 
-       return ildb_request_send(module, msg, req->async.context, req->async.callback, req->async.timeout, &(req->async.handle));
+       return ildb_request_send(ildb, msg, req);
 }
 
 /*
@@ -457,21 +483,21 @@ static int ildb_add(struct ldb_module *module, struct ldb_request *req)
        struct ldap_mod **mods;
        int i,n;
 
-       req->async.handle = NULL;
+       req->handle = NULL;
 
        /* ignore ltdb specials */
        if (ldb_dn_is_special(req->op.add.message->dn)) {
-               return LDB_SUCCESS;
+               return ildb_request_noop(ildb, req);
        }
 
-       msg = new_ldap_message(ildb->ldap);
+       msg = new_ldap_message(req);
        if (msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        msg->type = LDAP_TAG_AddRequest;
 
-       msg->r.AddRequest.dn = ldb_dn_linearize(msg, req->op.add.message->dn);
+       msg->r.AddRequest.dn = ldb_dn_alloc_linearized(msg, req->op.add.message->dn);
        if (msg->r.AddRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
@@ -494,7 +520,7 @@ static int ildb_add(struct ldb_module *module, struct ldb_request *req)
                msg->r.AddRequest.attributes[i] = mods[i]->attrib;
        }
 
-       return ildb_request_send(module, msg, req->async.context, req->async.callback, req->async.timeout, &(req->async.handle));
+       return ildb_request_send(ildb, msg, req);
 }
 
 /*
@@ -507,21 +533,21 @@ static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
        struct ldap_mod **mods;
        int i,n;
 
-       req->async.handle = NULL;
+       req->handle = NULL;
 
        /* ignore ltdb specials */
        if (ldb_dn_is_special(req->op.mod.message->dn)) {
-               return LDB_SUCCESS;
+               return ildb_request_noop(ildb, req);
        }
 
-       msg = new_ldap_message(ildb->ldap);
+       msg = new_ldap_message(req);
        if (msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        msg->type = LDAP_TAG_ModifyRequest;
 
-       msg->r.ModifyRequest.dn = ldb_dn_linearize(msg, req->op.mod.message->dn);
+       msg->r.ModifyRequest.dn = ldb_dn_alloc_linearized(msg, req->op.mod.message->dn);
        if (msg->r.ModifyRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
@@ -544,11 +570,7 @@ static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
                msg->r.ModifyRequest.mods[i] = *mods[i];
        }
 
-       return ildb_request_send(module, msg,
-                               req->async.context,
-                               req->async.callback,
-                               req->async.timeout,
-                               &(req->async.handle));
+       return ildb_request_send(ildb, msg, req);
 }
 
 /*
@@ -559,31 +581,27 @@ static int ildb_delete(struct ldb_module *module, struct ldb_request *req)
        struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
        struct ldap_message *msg;
 
-       req->async.handle = NULL;
+       req->handle = NULL;
 
        /* ignore ltdb specials */
        if (ldb_dn_is_special(req->op.del.dn)) {
-               return LDB_SUCCESS;
+               return ildb_request_noop(ildb, req);
        }
 
-       msg = new_ldap_message(ildb->ldap);
+       msg = new_ldap_message(req);
        if (msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        msg->type = LDAP_TAG_DelRequest;
        
-       msg->r.DelRequest.dn = ldb_dn_linearize(msg, req->op.del.dn);
+       msg->r.DelRequest.dn = ldb_dn_alloc_linearized(msg, req->op.del.dn);
        if (msg->r.DelRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
        }
 
-       return ildb_request_send(module, msg,
-                               req->async.context,
-                               req->async.callback,
-                               req->async.timeout,
-                               &(req->async.handle));
+       return ildb_request_send(ildb, msg, req);
 }
 
 /*
@@ -594,20 +612,20 @@ static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
        struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
        struct ldap_message *msg;
 
-       req->async.handle = NULL;
+       req->handle = NULL;
 
        /* ignore ltdb specials */
        if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
-               return LDB_SUCCESS;
+               return ildb_request_noop(ildb, req);
        }
 
-       msg = new_ldap_message(ildb->ldap);
+       msg = new_ldap_message(req);
        if (msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        msg->type = LDAP_TAG_ModifyDNRequest;
-       msg->r.ModifyDNRequest.dn = ldb_dn_linearize(msg, req->op.rename.olddn);
+       msg->r.ModifyDNRequest.dn = ldb_dn_alloc_linearized(msg, req->op.rename.olddn);
        if (msg->r.ModifyDNRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
@@ -615,28 +633,23 @@ static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
 
        msg->r.ModifyDNRequest.newrdn = 
                talloc_asprintf(msg, "%s=%s",
-                               req->op.rename.newdn->components[0].name,
-                               ldb_dn_escape_value(msg, req->op.rename.newdn->components[0].value));
+                               ldb_dn_get_rdn_name(req->op.rename.newdn),
+                               ldb_dn_escape_value(msg, *ldb_dn_get_rdn_val(req->op.rename.newdn)));
        if (msg->r.ModifyDNRequest.newrdn == NULL) {
                talloc_free(msg);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        msg->r.ModifyDNRequest.newsuperior =
-               ldb_dn_linearize(msg,
-                                ldb_dn_get_parent(msg, req->op.rename.newdn));
+               ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn));
        if (msg->r.ModifyDNRequest.newsuperior == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
        }
 
-       msg->r.ModifyDNRequest.deleteolddn = True;
+       msg->r.ModifyDNRequest.deleteolddn = true;
 
-       return ildb_request_send(module, msg,
-                               req->async.context,
-                               req->async.callback,
-                               req->async.timeout,
-                               &(req->async.handle));
+       return ildb_request_send(ildb, msg, req);
 }
 
 static int ildb_start_trans(struct ldb_module *module)
@@ -665,9 +678,9 @@ static int ildb_request(struct ldb_module *module, struct ldb_request *req)
        return LDB_ERR_OPERATIONS_ERROR;
 }
 
-static int ildb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
+static int ildb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
 {
-       struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
+       struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
 
        if (handle->state == LDB_ASYNC_DONE) {
                return handle->status;
@@ -699,80 +712,6 @@ static int ildb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_
        return handle->status;
 }
 
-static int ildb_rootdse_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
-{
-       struct ildb_private *ildb;
-
-       if (!context || !ares) {
-               ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
-               goto error;
-       }
-
-       ildb = talloc_get_type(context, struct ildb_private);
-
-       /* we are interested only in the single reply (rootdse) we receive here */
-       switch (ares->type) {
-       case LDB_REPLY_ENTRY:
-               if (ildb->rootDSE != NULL) {
-                       /* what ? more than one rootdse entry ?! */
-                       goto error;
-               }
-               ildb->rootDSE = talloc_steal(ildb, ares->message);
-               break;
-
-       case LDB_REPLY_REFERRAL:
-               goto error;
-
-       case LDB_REPLY_DONE:
-               break;
-       }
-       
-       talloc_free(ares);
-       return LDB_SUCCESS;
-
-error:
-       talloc_free(ares);
-       return LDB_ERR_OPERATIONS_ERROR;
-}
-
-/*
-  fetch the rootDSE for later use
-*/
-static int ildb_init(struct ldb_module *module)
-{
-       struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
-       struct ldb_request *req;
-       int ret;
-
-       req = talloc(ildb, struct ldb_request);
-       if (req == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       ildb->rootDSE = NULL;
-
-       req->operation = LDB_SEARCH;
-       req->op.search.base = ldb_dn_new(req);
-       req->op.search.scope = LDB_SCOPE_BASE;
-       req->op.search.tree = ldb_parse_tree(req, "dn=dc=rootDSE");
-       req->op.search.attrs = NULL;
-       req->controls = NULL;
-       req->async.context = ildb;
-       req->async.callback = ildb_rootdse_callback;
-       ldb_set_timeout(module->ldb, req, 60);
-
-       ret = ildb_search(module, req);
-       if (ret != LDB_SUCCESS) {
-               talloc_free(req);
-               return ret;
-       }
-
-       ret = ildb_async_wait(req->async.handle, LDB_WAIT_ALL);
-       
-       talloc_free(req);
-       return ret;
-}
-
 static const struct ldb_module_ops ildb_ops = {
        .name              = "ldap",
        .search            = ildb_search,
@@ -784,8 +723,7 @@ static const struct ldb_module_ops ildb_ops = {
        .start_transaction = ildb_start_trans,
        .end_transaction   = ildb_end_trans,
        .del_transaction   = ildb_del_trans,
-       .async_wait        = ildb_async_wait,
-       .init_context      = ildb_init
+       .wait              = ildb_wait
 };
 
 /*
@@ -793,50 +731,53 @@ static const struct ldb_module_ops ildb_ops = {
 */
 static int ildb_connect(struct ldb_context *ldb, const char *url, 
                        unsigned int flags, const char *options[],
-                       struct ldb_module **module)
+                       struct ldb_module **_module)
 {
-       struct ildb_private *ildb = NULL;
+       struct ldb_module *module;
+       struct ildb_private *ildb;
        NTSTATUS status;
        struct cli_credentials *creds;
+       struct event_context *event_ctx;
+
+       module = talloc(ldb, struct ldb_module);
+       if (!module) {
+               ldb_oom(ldb);
+               return -1;
+       }
+       talloc_set_name_const(module, "ldb_ildap backend");
+       module->ldb             = ldb;
+       module->prev            = module->next = NULL;
+       module->private_data    = NULL;
+       module->ops             = &ildb_ops;
 
-       ildb = talloc(ldb, struct ildb_private);
+       ildb = talloc(module, struct ildb_private);
        if (!ildb) {
                ldb_oom(ldb);
                goto failed;
        }
+       module->private_data    = ildb;
+       ildb->module            = module;
 
-       ildb->rootDSE = NULL;
-       ildb->ldb     = ldb;
+       event_ctx = ldb_get_event_context(ldb);
 
-       ildb->ldap = ldap_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
+       ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "loadparm"),
+                                         event_ctx);
        if (!ildb->ldap) {
                ldb_oom(ldb);
                goto failed;
        }
 
-       if (flags == LDB_FLG_RECONNECT) {
+       if (flags & LDB_FLG_RECONNECT) {
                ldap_set_reconn_params(ildb->ldap, 10);
        }
 
        status = ldap_connect(ildb->ldap, url);
        if (!NT_STATUS_IS_OK(status)) {
                ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
-                         url, ldap_errstr(ildb->ldap, status));
+                         url, ldap_errstr(ildb->ldap, module, status));
                goto failed;
        }
 
-
-       *module = talloc(ldb, struct ldb_module);
-       if (!module) {
-               ldb_oom(ldb);
-               talloc_free(ildb);
-               return -1;
-       }
-       (*module)->ldb = ldb;
-       (*module)->prev = (*module)->next = NULL;
-       (*module)->private_data = ildb;
-       (*module)->ops = &ildb_ops;
-
        /* caller can optionally setup credentials using the opaque token 'credentials' */
        creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
        if (creds == NULL) {
@@ -853,29 +794,39 @@ static int ildb_connect(struct ldb_context *ldb, const char *url,
                        status = ldap_bind_simple(ildb->ldap, bind_dn, password);
                        if (!NT_STATUS_IS_OK(status)) {
                                ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
-                                         ldap_errstr(ildb->ldap, status));
+                                         ldap_errstr(ildb->ldap, module, status));
                                goto failed;
                        }
                } else {
-                       status = ldap_bind_sasl(ildb->ldap, creds);
+                       status = ldap_bind_sasl(ildb->ldap, creds, ldb_get_opaque(ldb, "loadparm"));
                        if (!NT_STATUS_IS_OK(status)) {
                                ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
-                                         ldap_errstr(ildb->ldap, status));
+                                         ldap_errstr(ildb->ldap, module, status));
                                goto failed;
                        }
                }
        }
 
+       *_module = module;
        return 0;
 
 failed:
-       talloc_free(ildb);
+       talloc_free(module);
        return -1;
 }
 
-int ldb_ildap_init(void)
-{
-       return ldb_register_backend("ldap", ildb_connect) + 
-                  ldb_register_backend("ldapi", ildb_connect) + 
-                  ldb_register_backend("ldaps", ildb_connect);
-}
+_PUBLIC_ const struct ldb_backend_ops ldb_ldap_backend_ops = {
+       .name = "ldap",
+       .connect_fn = ildb_connect
+};
+
+_PUBLIC_ const struct ldb_backend_ops ldb_ldapi_backend_ops = {
+       .name = "ldapi",
+       .connect_fn = ildb_connect
+};
+
+_PUBLIC_ const struct ldb_backend_ops ldb_ldaps_backend_ops = {
+       .name = "ldaps",
+       .connect_fn = ildb_connect
+};
+