s4/ldap: Fix nested searches SEGFAULT bug
[ira/wip.git] / source4 / lib / ldb / ldb_ildap / ldb_ildap.c
index 440686c65efdbedd665a9e75d45969e0f402688e..6eb2e1719c8b2b50abe52506037b9263e01c1e74 100644 (file)
@@ -1,13 +1,13 @@
-/* 
+/*
    ldb database library - ildap backend
 
    Copyright (C) Andrew Tridgell  2005
-   Copyright (C) Simo Sorce       2006
+   Copyright (C) Simo Sorce       2008
 
      ** NOTE! The following LGPL license applies to the ldb
      ** library. This does NOT imply that all of Samba is released
      ** under the LGPL
-   
+
    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
  *    author: Simo Sorce
  */
 
-
 #include "includes.h"
-#include "ldb_includes.h"
+#include "ldb_module.h"
+#include "dlinklist.h"
 
-#include "lib/events/events.h"
 #include "libcli/ldap/ldap.h"
 #include "libcli/ldap/ldap_client.h"
 #include "auth/auth.h"
 
 struct ildb_private {
        struct ldap_connection *ldap;
-       struct ldb_module *module;
+       struct tevent_context *event_ctx;
 };
 
 struct ildb_context {
+       struct ldb_module *module;
+       struct ldb_request *req;
+
        struct ildb_private *ildb;
-       struct ldb_handle *handle;
-       struct ldap_request *req;
-       void *context;
-       int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
+       struct ldap_request *ireq;
+
+       bool done;
+
+       struct ildb_destructor_ctx *dc;
 };
 
+static void ildb_request_done(struct ildb_context *ctx,
+                             struct ldb_control **ctrls, int error)
+{
+       struct ldb_context *ldb;
+       struct ldb_reply *ares;
+
+       ldb = ldb_module_get_ctx(ctx->module);
+
+       ctx->done = true;
+
+       if (ctx->req == NULL) {
+               /* if the req has been freed already just return */
+               return;
+       }
+
+       ares = talloc_zero(ctx->req, struct ldb_reply);
+       if (!ares) {
+               ldb_oom(ldb);
+               ctx->req->callback(ctx->req, NULL);
+               return;
+       }
+       ares->type = LDB_REPLY_DONE;
+       ares->controls = talloc_steal(ares, ctrls);
+       ares->error = error;
+
+       ctx->req->callback(ctx->req, ares);
+}
+
+static void ildb_auto_done_callback(struct tevent_context *ev,
+                                   struct tevent_timer *te,
+                                   struct timeval t,
+                                   void *private_data)
+{
+       struct ildb_context *ac;
+
+       ac = talloc_get_type(private_data, struct ildb_context);
+       ildb_request_done(ac, NULL, LDB_SUCCESS);
+}
+
 /*
   convert a ldb_message structure to a list of ldap_mod structures
   ready for ildap_add() or ildap_modify()
 */
 static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
-                                         const struct ldb_message *msg, int use_flags)
+                                         const struct ldb_message *msg,
+                                         int use_flags)
 {
        struct ldap_mod **mods;
        unsigned int i;
@@ -120,17 +163,26 @@ failed:
 /*
   map an ildap NTSTATUS to a ldb error code
 */
-static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
+static int ildb_map_error(struct ldb_module *module, NTSTATUS status)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(ildb);
+       struct ildb_private *ildb;
+       struct ldb_context *ldb;
+       TALLOC_CTX *mem_ctx;
+
+       ildb = talloc_get_type(ldb_module_get_private(module), struct ildb_private);
+       ldb = ldb_module_get_ctx(module);
+
        if (NT_STATUS_IS_OK(status)) {
                return LDB_SUCCESS;
        }
+
+       mem_ctx = talloc_new(ildb);
        if (!mem_ctx) {
-               ldb_oom(ildb->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
-       ldb_set_errstring(ildb->module->ldb, ldap_errstr(ildb->ldap, mem_ctx, status));
+       ldb_set_errstring(ldb,
+                         ldap_errstr(ildb->ldap, mem_ctx, status));
        talloc_free(mem_ctx);
        if (NT_STATUS_IS_LDAP(status)) {
                return NT_STATUS_LDAP_CODE(status);
@@ -138,304 +190,265 @@ static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
        return LDB_ERR_OPERATIONS_ERROR;
 }
 
-static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
+static void ildb_request_timeout(struct tevent_context *ev, struct tevent_timer *te,
                                 struct timeval t, void *private_data)
 {
        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);
+       if (ac->ireq->state == LDAP_REQUEST_PENDING) {
+               DLIST_REMOVE(ac->ireq->conn->pending, ac->ireq);
        }
 
-       handle->status = LDB_ERR_TIME_LIMIT_EXCEEDED;
-
-       return;
+       ildb_request_done(ac, NULL, LDB_ERR_TIME_LIMIT_EXCEEDED);
 }
 
 static void ildb_callback(struct ldap_request *req)
 {
-       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;
+       struct ldb_context *ldb;
+       struct ildb_context *ac;
        NTSTATUS status;
+       struct ldap_SearchResEntry *search;
+       struct ldap_message *msg;
+       struct ldb_control **controls;
+       struct ldb_message *ldbmsg;
+       char *referral;
+       bool callback_failed;
+       bool request_done;
+       int ret;
        int i;
 
-       handle->status = LDB_SUCCESS;
+       ac = talloc_get_type(req->async.private_data, struct ildb_context);
+       ldb = ldb_module_get_ctx(ac->module);
+       callback_failed = false;
+       request_done = false;
+       controls = NULL;
 
        if (!NT_STATUS_IS_OK(req->status)) {
-               handle->status = ildb_map_error(ildb, req->status);
+               ret = ildb_map_error(ac->module, req->status);
+               ildb_request_done(ac, NULL, ret);
                return;
        }
 
        if (req->num_replies < 1) {
-               handle->status = LDB_ERR_OPERATIONS_ERROR;
+               ret = LDB_ERR_OPERATIONS_ERROR;
+               ildb_request_done(ac, NULL, ret);
                return;
-       } 
-               
+       }
+
        switch (req->type) {
 
        case LDAP_TAG_ModifyRequest:
                if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
-                       handle->status = LDB_ERR_PROTOCOL_ERROR;
-                       return;
-               }
-               status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
-               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->ildb->module->ldb, ac->context, NULL);
+                       ret = LDB_ERR_PROTOCOL_ERROR;
+                       break;
                }
-               handle->state = LDB_ASYNC_DONE;
+               status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
+               ret = ildb_map_error(ac->module, status);
+               request_done = true;
                break;
 
        case LDAP_TAG_AddRequest:
                if (req->replies[0]->type != LDAP_TAG_AddResponse) {
-                       handle->status = LDB_ERR_PROTOCOL_ERROR;
+                       ret = LDB_ERR_PROTOCOL_ERROR;
                        return;
                }
-               status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
-               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->ildb->module->ldb, ac->context, NULL);
-               }
-               handle->state = LDB_ASYNC_DONE;
+               status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
+               ret = ildb_map_error(ac->module, status);
+               request_done = true;
                break;
 
        case LDAP_TAG_DelRequest:
                if (req->replies[0]->type != LDAP_TAG_DelResponse) {
-                       handle->status = LDB_ERR_PROTOCOL_ERROR;
+                       ret = LDB_ERR_PROTOCOL_ERROR;
                        return;
                }
-               status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
-               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->ildb->module->ldb, ac->context, NULL);
-               }
-               handle->state = LDB_ASYNC_DONE;
+               status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
+               ret = ildb_map_error(ac->module, status);
+               request_done = true;
                break;
 
        case LDAP_TAG_ModifyDNRequest:
                if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
-                       handle->status = LDB_ERR_PROTOCOL_ERROR;
+                       ret = LDB_ERR_PROTOCOL_ERROR;
                        return;
                }
-               status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
-               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->ildb->module->ldb, ac->context, NULL);
-               }
-               handle->state = LDB_ASYNC_DONE;
+               status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
+               ret = ildb_map_error(ac->module, status);
+               request_done = true;
                break;
 
        case LDAP_TAG_SearchRequest:
+               /* check if we are already processing this request */
+               if (req->in_dispatch_replies) {
+                       return;
+               }
+
+               req->in_dispatch_replies = true;
+
                /* loop over all messages */
                for (i = 0; i < req->num_replies; i++) {
-                       struct ldap_SearchResEntry *search;
-                       struct ldb_reply *ares = NULL;
-                       struct ldap_message *msg;
-                       int ret;
-
-                       ares = talloc_zero(ac, struct ldb_reply);
-                       if (!ares) {
-                               handle->status = LDB_ERR_OPERATIONS_ERROR;
-                               return;
-                       }
 
                        msg = req->replies[i];
                        switch (msg->type) {
 
                        case LDAP_TAG_SearchResultDone:
 
-                               status = ldap_check_response(req->conn, &msg->r.GeneralResult);
+                               status = ldap_check_response(ac->ireq->conn, &msg->r.GeneralResult);
                                if (!NT_STATUS_IS_OK(status)) {
-                                       handle->status = ildb_map_error(ildb, status);
-                                       return;
+                                       ret = ildb_map_error(ac->module, status);
+                                       break;
                                }
-                               
-                               ares->controls = talloc_move(ares, &msg->controls);
+
+                               controls = talloc_steal(ac, msg->controls);
                                if (msg->r.SearchResultDone.resultcode) {
                                        if (msg->r.SearchResultDone.errormessage) {
-                                               ldb_set_errstring(ac->ildb->module->ldb, msg->r.SearchResultDone.errormessage);
+                                               ldb_set_errstring(ldb, msg->r.SearchResultDone.errormessage);
                                        }
                                }
 
-                               handle->status = msg->r.SearchResultDone.resultcode;
-                               handle->state = LDB_ASYNC_DONE;
-                               ares->type = LDB_REPLY_DONE;
+                               ret = msg->r.SearchResultDone.resultcode;
+                               request_done = true;
                                break;
 
                        case LDAP_TAG_SearchResultEntry:
 
-
-                               ares->message = ldb_msg_new(ares);
-                               if (!ares->message) {
-                                       handle->status = LDB_ERR_OPERATIONS_ERROR;
-                                       return;
+                               ldbmsg = ldb_msg_new(ac);
+                               if (!ldbmsg) {
+                                       ret = LDB_ERR_OPERATIONS_ERROR;
+                                       break;
                                }
 
                                search = &(msg->r.SearchResultEntry);
-               
-                               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;
+
+                               ldbmsg->dn = ldb_dn_new(ldbmsg, ldb, search->dn);
+                               if ( ! ldb_dn_validate(ldbmsg->dn)) {
+                                       ret = LDB_ERR_OPERATIONS_ERROR;
+                                       break;
+                               }
+                               ldbmsg->num_elements = search->num_attributes;
+                               ldbmsg->elements = talloc_move(ldbmsg, &search->attributes);
+
+                               controls = talloc_steal(ac, msg->controls);
+                               
+                               ret = ldb_module_send_entry(ac->req, ldbmsg, controls);
+                               if (ret != LDB_SUCCESS) {
+                                       callback_failed = true;
                                }
-                               ares->message->num_elements = search->num_attributes;
-                               ares->message->elements = talloc_move(ares->message,
-                                                                     &search->attributes);
 
-                               handle->status = LDB_SUCCESS;
-                               handle->state = LDB_ASYNC_PENDING;
-                               ares->type = LDB_REPLY_ENTRY;
                                break;
 
                        case LDAP_TAG_SearchResultReference:
 
-                               ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral);
-                               
-                               handle->status = LDB_SUCCESS;
-                               handle->state = LDB_ASYNC_PENDING;
-                               ares->type = LDB_REPLY_REFERRAL;
+                               referral = talloc_strdup(ac, msg->r.SearchResultReference.referral);
+
+                               ret = ldb_module_send_referral(ac->req, referral);
+                               if (ret != LDB_SUCCESS) {
+                                       callback_failed = true;
+                               }
+
                                break;
 
                        default:
                                /* TAG not handled, fail ! */
-                               handle->status = LDB_ERR_PROTOCOL_ERROR;
-                               return;
+                               ret = LDB_ERR_PROTOCOL_ERROR;
+                               break;
                        }
 
-                       ret = ac->callback(ac->ildb->module->ldb, ac->context, ares);
-                       if (ret) {
-                               handle->status = ret;
+                       if (ret != LDB_SUCCESS) {
+                               break;
                        }
                }
 
+               req->in_dispatch_replies = false;
+
                talloc_free(req->replies);
                req->replies = NULL;
                req->num_replies = 0;
 
                break;
-               
+
        default:
-               handle->status = LDB_ERR_PROTOCOL_ERROR;
-               return;
+               ret = LDB_ERR_PROTOCOL_ERROR;
+               break;
        }
-}
 
-static struct ildb_context *init_ildb_handle(struct ildb_private *ildb,
-                                            struct ldb_request *req)
-{
-       struct ildb_context *ildb_ac;
-       struct ldb_handle *h;
+       if (ret != LDB_SUCCESS) {
 
-       h = talloc_zero(req, struct ldb_handle);
-       if (h == NULL) {
-               ldb_set_errstring(ildb->module->ldb, "Out of Memory");
-               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) {
+                       request_done = true;
+               }
        }
 
-       h->module = ildb->module;
-
-       ildb_ac = talloc(h, struct ildb_context);
-       if (ildb_ac == NULL) {
-               ldb_set_errstring(ildb->module->ldb, "Out of Memory");
-               talloc_free(h);
-               return NULL;
+       if (request_done) {
+               ildb_request_done(ac, controls, ret);
        }
-
-       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;
+       return;
 }
 
-static int ildb_request_send(struct ildb_private *ildb, struct ldap_message *msg, struct ldb_request *r)
+static int ildb_request_send(struct ildb_context *ac, struct ldap_message *msg)
 {
-       struct ildb_context *ildb_ac = init_ildb_handle(ildb, r);
+       struct ldb_context *ldb;
        struct ldap_request *req;
 
-       if (!ildb_ac) {
-               return LDB_ERR_OPERATIONS_ERROR;                
+       if (!ac) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       req = ldap_request_send(ildb->ldap, msg);
+       ldb = ldb_module_get_ctx(ac->module);
+
+       req = ldap_request_send(ac->ildb->ldap, msg);
        if (req == NULL) {
-               ldb_set_errstring(ildb->module->ldb, "async send request failed");
+               ldb_set_errstring(ldb, "async send request failed");
                return LDB_ERR_OPERATIONS_ERROR;
        }
-       ildb_ac->req = talloc_steal(ildb_ac, req);
+       ac->ireq = talloc_steal(ac, req);
 
-       if (!req->conn) {
-               ldb_set_errstring(ildb->module->ldb, "connection to remote LDAP server dropped?");
+       if (!ac->ireq->conn) {
+               ldb_set_errstring(ldb, "connection to remote LDAP server dropped?");
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        talloc_free(req->time_event);
        req->time_event = NULL;
-       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);
+       if (ac->req->timeout) {
+               req->time_event = tevent_add_timer(ac->ildb->event_ctx, ac,
+                                                  timeval_current_ofs(ac->req->timeout, 0),
+                                                  ildb_request_timeout, ac);
        }
 
        req->async.fn = ildb_callback;
-       req->async.private_data = ildb_ac;
+       req->async.private_data = 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;
-}
-
 /*
   search for matching records using an asynchronous function
  */
-static int ildb_search(struct ldb_module *module, struct ldb_request *req)
+static int ildb_search(struct ildb_context *ac)
 {
-       struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
+       struct ldb_context *ldb;
+       struct ldb_request *req = ac->req;
        struct ldap_message *msg;
        int n;
 
-       req->handle = NULL;
+       ldb = ldb_module_get_ctx(ac->module);
 
        if (!req->callback || !req->context) {
-               ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
+               ldb_set_errstring(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");
+               ldb_set_errstring(ldb, "Invalid expression parse tree");
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        msg = new_ldap_message(req);
        if (msg == NULL) {
-               ldb_set_errstring(module->ldb, "Out of Memory");
+               ldb_set_errstring(ldb, "Out of Memory");
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -444,10 +457,10 @@ static int ildb_search(struct ldb_module *module, struct ldb_request *req)
        if (req->op.search.base == NULL) {
                msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
        } else {
-               msg->r.SearchRequest.basedn  = ldb_dn_alloc_linearized(msg, req->op.search.base);
+               msg->r.SearchRequest.basedn  = ldb_dn_get_extended_linearized(msg, req->op.search.base, 0);
        }
        if (msg->r.SearchRequest.basedn == NULL) {
-               ldb_set_errstring(module->ldb, "Unable to determine baseDN");
+               ldb_set_errstring(ldb, "Unable to determine baseDN");
                talloc_free(msg);
                return LDB_ERR_OPERATIONS_ERROR;
        }
@@ -457,38 +470,31 @@ static int ildb_search(struct ldb_module *module, struct ldb_request *req)
        } else {
                msg->r.SearchRequest.scope = req->op.search.scope;
        }
-       
+
        msg->r.SearchRequest.deref  = LDAP_DEREFERENCE_NEVER;
        msg->r.SearchRequest.timelimit = 0;
        msg->r.SearchRequest.sizelimit = 0;
        msg->r.SearchRequest.attributesonly = 0;
        msg->r.SearchRequest.tree = discard_const(req->op.search.tree);
-       
+
        for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
        msg->r.SearchRequest.num_attributes = n;
-       msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs);
+       msg->r.SearchRequest.attributes = req->op.search.attrs;
        msg->controls = req->controls;
 
-       return ildb_request_send(ildb, msg, req);
+       return ildb_request_send(ac, msg);
 }
 
 /*
   add a record
 */
-static int ildb_add(struct ldb_module *module, struct ldb_request *req)
+static int ildb_add(struct ildb_context *ac)
 {
-       struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
+       struct ldb_request *req = ac->req;
        struct ldap_message *msg;
        struct ldap_mod **mods;
        int i,n;
 
-       req->handle = NULL;
-
-       /* ignore ltdb specials */
-       if (ldb_dn_is_special(req->op.add.message->dn)) {
-               return ildb_request_noop(ildb, req);
-       }
-
        msg = new_ldap_message(req);
        if (msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
@@ -496,7 +502,7 @@ static int ildb_add(struct ldb_module *module, struct ldb_request *req)
 
        msg->type = LDAP_TAG_AddRequest;
 
-       msg->r.AddRequest.dn = ldb_dn_alloc_linearized(msg, req->op.add.message->dn);
+       msg->r.AddRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.add.message->dn, 0);
        if (msg->r.AddRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
@@ -518,27 +524,21 @@ static int ildb_add(struct ldb_module *module, struct ldb_request *req)
        for (i = 0; i < n; i++) {
                msg->r.AddRequest.attributes[i] = mods[i]->attrib;
        }
+       msg->controls = req->controls;
 
-       return ildb_request_send(ildb, msg, req);
+       return ildb_request_send(ac, msg);
 }
 
 /*
   modify a record
 */
-static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
+static int ildb_modify(struct ildb_context *ac)
 {
-       struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
+       struct ldb_request *req = ac->req;
        struct ldap_message *msg;
        struct ldap_mod **mods;
        int i,n;
 
-       req->handle = NULL;
-
-       /* ignore ltdb specials */
-       if (ldb_dn_is_special(req->op.mod.message->dn)) {
-               return ildb_request_noop(ildb, req);
-       }
-
        msg = new_ldap_message(req);
        if (msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
@@ -546,7 +546,7 @@ static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
 
        msg->type = LDAP_TAG_ModifyRequest;
 
-       msg->r.ModifyRequest.dn = ldb_dn_alloc_linearized(msg, req->op.mod.message->dn);
+       msg->r.ModifyRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.mod.message->dn, 0);
        if (msg->r.ModifyRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
@@ -568,55 +568,44 @@ static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
        for (i = 0; i < n; i++) {
                msg->r.ModifyRequest.mods[i] = *mods[i];
        }
-
-       return ildb_request_send(ildb, msg, req);
+       msg->controls = req->controls;
+       return ildb_request_send(ac, msg);
 }
 
 /*
   delete a record
 */
-static int ildb_delete(struct ldb_module *module, struct ldb_request *req)
+static int ildb_delete(struct ildb_context *ac)
 {
-       struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
+       struct ldb_request *req = ac->req;
        struct ldap_message *msg;
 
-       req->handle = NULL;
-
-       /* ignore ltdb specials */
-       if (ldb_dn_is_special(req->op.del.dn)) {
-               return ildb_request_noop(ildb, req);
-       }
-
        msg = new_ldap_message(req);
        if (msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        msg->type = LDAP_TAG_DelRequest;
-       
-       msg->r.DelRequest.dn = ldb_dn_alloc_linearized(msg, req->op.del.dn);
+
+       msg->r.DelRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.del.dn, 0);
        if (msg->r.DelRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
        }
+       msg->controls = req->controls;
 
-       return ildb_request_send(ildb, msg, req);
+       return ildb_request_send(ac, msg);
 }
 
 /*
   rename a record
 */
-static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
+static int ildb_rename(struct ildb_context *ac)
 {
-       struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
+       struct ldb_request *req = ac->req;
        struct ldap_message *msg;
-
-       req->handle = NULL;
-
-       /* ignore ltdb specials */
-       if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
-               return ildb_request_noop(ildb, req);
-       }
+       const char *rdn_name;
+       const struct ldb_val *rdn_val;
 
        msg = new_ldap_message(req);
        if (msg == NULL) {
@@ -624,16 +613,22 @@ static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
        }
 
        msg->type = LDAP_TAG_ModifyDNRequest;
-       msg->r.ModifyDNRequest.dn = ldb_dn_alloc_linearized(msg, req->op.rename.olddn);
+       msg->r.ModifyDNRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.rename.olddn, 0);
        if (msg->r.ModifyDNRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
        }
 
-       msg->r.ModifyDNRequest.newrdn = 
-               talloc_asprintf(msg, "%s=%s",
-                               ldb_dn_get_rdn_name(req->op.rename.newdn),
-                               ldb_dn_escape_value(msg, *ldb_dn_get_rdn_val(req->op.rename.newdn)));
+       rdn_name = ldb_dn_get_rdn_name(req->op.rename.newdn);
+       rdn_val = ldb_dn_get_rdn_val(req->op.rename.newdn);
+
+       if ((rdn_name != NULL) && (rdn_val != NULL)) {
+               msg->r.ModifyDNRequest.newrdn =
+                       talloc_asprintf(msg, "%s=%s", rdn_name,
+                               ldb_dn_escape_value(msg, *rdn_val));
+       } else {
+               msg->r.ModifyDNRequest.newrdn = talloc_strdup(msg, "");
+       }
        if (msg->r.ModifyDNRequest.newrdn == NULL) {
                talloc_free(msg);
                return LDB_ERR_OPERATIONS_ERROR;
@@ -646,9 +641,10 @@ static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
                return LDB_ERR_INVALID_DN_SYNTAX;
        }
 
-       msg->r.ModifyDNRequest.deleteolddn = True;
+       msg->r.ModifyDNRequest.deleteolddn = true;
+       msg->controls = req->controls;
 
-       return ildb_request_send(ildb, msg, req);
+       return ildb_request_send(ac, msg);
 }
 
 static int ildb_start_trans(struct ldb_module *module)
@@ -672,63 +668,113 @@ static int ildb_del_trans(struct ldb_module *module)
        return LDB_SUCCESS;
 }
 
-static int ildb_request(struct ldb_module *module, struct ldb_request *req)
+static bool ildb_dn_is_special(struct ldb_request *req)
 {
-       return LDB_ERR_OPERATIONS_ERROR;
+       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 (dn && ldb_dn_is_special(dn)) {
+               return true;
+       }
+       return false;
 }
 
-static int ildb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
+static int ildb_handle_request(struct ldb_module *module, struct ldb_request *req)
 {
-       struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
+       struct ldb_context *ldb;
+       struct ildb_private *ildb;
+       struct ildb_context *ac;
+       struct tevent_timer *te;
+       int ret;
+
+       ildb = talloc_get_type(ldb_module_get_private(module), struct ildb_private);
+       ldb = ldb_module_get_ctx(module);
 
-       if (handle->state == LDB_ASYNC_DONE) {
-               return handle->status;
+       if (req->starttime == 0 || req->timeout == 0) {
+               ldb_set_errstring(ldb, "Invalid timeout settings");
+               return LDB_ERR_TIME_LIMIT_EXCEEDED;
        }
 
-       if (!ac) {
+       ac = talloc_zero(req, struct ildb_context);
+       if (ac == NULL) {
+               ldb_set_errstring(ldb, "Out of Memory");
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       handle->state = LDB_ASYNC_INIT;
+       ac->module = module;
+       ac->req = req;
+       ac->ildb = ildb;
+
+       if (ildb_dn_is_special(req)) {
 
-       switch(type) {
-       case LDB_WAIT_NONE:
-               if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
-                       return LDB_ERR_OTHER;
+               te = tevent_add_timer(ac->ildb->event_ctx,
+                                     ac, timeval_zero(),
+                                     ildb_auto_done_callback, ac);
+               if (NULL == te) {
+                       return LDB_ERR_OPERATIONS_ERROR;
                }
+
+               return LDB_SUCCESS;
+       }
+
+       switch (ac->req->operation) {
+       case LDB_SEARCH:
+               ret = ildb_search(ac);
                break;
-       case LDB_WAIT_ALL:
-               while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
-                       if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
-                               return LDB_ERR_OTHER;
-                       }
-               }
+       case LDB_ADD:
+               ret = ildb_add(ac);
+               break;
+       case LDB_MODIFY:
+               ret = ildb_modify(ac);
+               break;
+       case LDB_DELETE:
+               ret = ildb_delete(ac);
+               break;
+       case LDB_RENAME:
+               ret = ildb_rename(ac);
                break;
        default:
-               return LDB_ERR_OPERATIONS_ERROR;
+               /* no other op supported */
+               ret = LDB_ERR_OPERATIONS_ERROR;
+               break;
        }
-       
-       return handle->status;
+
+       return ret;
 }
 
 static const struct ldb_module_ops ildb_ops = {
        .name              = "ldap",
-       .search            = ildb_search,
-       .add               = ildb_add,
-       .modify            = ildb_modify,
-       .del               = ildb_delete,
-       .rename            = ildb_rename,
-       .request           = ildb_request,
+       .search            = ildb_handle_request,
+       .add               = ildb_handle_request,
+       .modify            = ildb_handle_request,
+       .del               = ildb_handle_request,
+       .rename            = ildb_handle_request,
+/*     .request           = ildb_handle_request, */
        .start_transaction = ildb_start_trans,
        .end_transaction   = ildb_end_trans,
        .del_transaction   = ildb_del_trans,
-       .wait              = ildb_wait
 };
 
 /*
   connect to the database
 */
-static int ildb_connect(struct ldb_context *ldb, const char *url, 
+static int ildb_connect(struct ldb_context *ldb, const char *url,
                        unsigned int flags, const char *options[],
                        struct ldb_module **_module)
 {
@@ -736,26 +782,25 @@ static int ildb_connect(struct ldb_context *ldb, const char *url,
        struct ildb_private *ildb;
        NTSTATUS status;
        struct cli_credentials *creds;
+       struct loadparm_context *lp_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;
+       module = ldb_module_new(ldb, ldb, "ldb_ildap backend", &ildb_ops);
+       if (!module) return LDB_ERR_OPERATIONS_ERROR;
 
        ildb = talloc(module, struct ildb_private);
        if (!ildb) {
                ldb_oom(ldb);
                goto failed;
        }
-       module->private_data    = ildb;
-       ildb->module            = module;
-       ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
+       ldb_module_set_private(module, ildb);
+
+       ildb->event_ctx = ldb_get_event_context(ldb);
+
+       lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
+                                struct loadparm_context);
+
+       ildb->ldap = ldap4_new_connection(ildb, lp_ctx,
+                                         ildb->event_ctx);
        if (!ildb->ldap) {
                ldb_oom(ldb);
                goto failed;
@@ -767,7 +812,7 @@ static int ildb_connect(struct ldb_context *ldb, const char *url,
 
        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",
+               ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s",
                          url, ldap_errstr(ildb->ldap, module, status));
                goto failed;
        }
@@ -787,14 +832,14 @@ static int ildb_connect(struct ldb_context *ldb, const char *url,
                        const char *password = cli_credentials_get_password(creds);
                        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",
+                               ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s",
                                          ldap_errstr(ildb->ldap, module, status));
                                goto failed;
                        }
                } else {
-                       status = ldap_bind_sasl(ildb->ldap, creds);
+                       status = ldap_bind_sasl(ildb->ldap, creds, lp_ctx);
                        if (!NT_STATUS_IS_OK(status)) {
-                               ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
+                               ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s",
                                          ldap_errstr(ildb->ldap, module, status));
                                goto failed;
                        }
@@ -802,16 +847,25 @@ static int ildb_connect(struct ldb_context *ldb, const char *url,
        }
 
        *_module = module;
-       return 0;
+       return LDB_SUCCESS;
 
 failed:
        talloc_free(module);
-       return -1;
+       return LDB_ERR_OPERATIONS_ERROR;
 }
 
-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
+};
+