libsocket: Add "mem_ctx" to socket_create()
[samba.git] / source4 / libcli / ldap / ldap_client.c
index d7cfd7c7e5c1c91e7d932c007a4ae87d9a42737c..1cbcd0d42d58c937f0ecc5f290d842f79cd97427 100644 (file)
@@ -1,5 +1,5 @@
 /* 
-   Unix SMB/CIFS mplementation.
+   Unix SMB/CIFS implementation.
    LDAP protocol helper functions for SAMBA
    
    Copyright (C) Andrew Tridgell  2004
@@ -9,7 +9,7 @@
     
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
 */
 
 #include "includes.h"
-#include "asn_1.h"
-#include "dlinklist.h"
-#include "lib/events/events.h"
+#include <tevent.h>
 #include "lib/socket/socket.h"
-#include "lib/tls/tls.h"
-#include "libcli/ldap/ldap.h"
+#include "lib/tsocket/tsocket.h"
+#include "libcli/util/tstream.h"
+#include "../lib/util/asn1.h"
+#include "../lib/util/dlinklist.h"
+#include "libcli/ldap/libcli_ldap.h"
+#include "libcli/ldap/ldap_proto.h"
 #include "libcli/ldap/ldap_client.h"
 #include "libcli/composite/composite.h"
+#include "lib/tls/tls.h"
+#include "auth/gensec/gensec.h"
+#include "system/time.h"
+#include "param/param.h"
+#include "libcli/resolve/resolve.h"
 
+static void ldap_connection_dead(struct ldap_connection *conn, NTSTATUS status);
 
-/*
+static int ldap_connection_destructor(struct ldap_connection *conn)
+{
+       /*
+        * NT_STATUS_OK means that callbacks of pending requests are not
+        * triggered
+        */
+       ldap_connection_dead(conn, NT_STATUS_OK);
+       return 0;
+}
+
+/**
   create a new ldap_connection stucture. The event context is optional
 */
-struct ldap_connection *ldap_new_connection(TALLOC_CTX *mem_ctx, 
-                                           struct event_context *ev)
+
+_PUBLIC_ struct ldap_connection *ldap4_new_connection(TALLOC_CTX *mem_ctx, 
+                                            struct loadparm_context *lp_ctx,
+                                            struct tevent_context *ev)
 {
        struct ldap_connection *conn;
 
-       conn = talloc_zero(mem_ctx, struct ldap_connection);
-       if (conn == NULL) {
+       if (ev == NULL) {
                return NULL;
        }
 
-       if (ev == NULL) {
-               ev = event_context_init(conn);
-               if (ev == NULL) {
-                       talloc_free(conn);
-                       return NULL;
-               }
+       conn = talloc_zero(mem_ctx, struct ldap_connection);
+       if (conn == NULL) {
+               return NULL;
        }
 
        conn->next_messageid  = 1;
        conn->event.event_ctx = ev;
 
+       conn->sockets.send_queue = tevent_queue_create(conn,
+                                       "ldap_connection send_queue");
+       if (conn->sockets.send_queue == NULL) {
+               TALLOC_FREE(conn);
+               return NULL;
+       }
+
+       conn->lp_ctx = lp_ctx;
+
        /* set a reasonable request timeout */
        conn->timeout = 60;
 
+       /* explicitly avoid reconnections by default */
+       conn->reconnect.max_retries = 0;
+
+       talloc_set_destructor(conn, ldap_connection_destructor);
        return conn;
 }
 
-
 /*
   the connection is dead
 */
-static void ldap_connection_dead(struct ldap_connection *conn)
+static void ldap_connection_dead(struct ldap_connection *conn, NTSTATUS status)
 {
        struct ldap_request *req;
 
+       tevent_queue_stop(conn->sockets.send_queue);
+       TALLOC_FREE(conn->sockets.recv_subreq);
+       conn->sockets.active = NULL;
+       TALLOC_FREE(conn->sockets.sasl);
+       TALLOC_FREE(conn->sockets.tls);
+       TALLOC_FREE(conn->sockets.raw);
+
+       /* return an error for any pending request ... */
        while (conn->pending) {
                req = conn->pending;
                DLIST_REMOVE(req->conn->pending, req);
+               req->conn = NULL;
                req->state = LDAP_REQUEST_DONE;
-               req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
-               if (req->async.fn) {
-                       req->async.fn(req);
+               if (NT_STATUS_IS_OK(status)) {
+                       continue;
                }
-       }       
-
-       while (conn->send_queue) {
-               req = conn->send_queue;
-               DLIST_REMOVE(req->conn->send_queue, req);
-               req->state = LDAP_REQUEST_DONE;
-               req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
+               req->status = status;
                if (req->async.fn) {
                        req->async.fn(req);
                }
        }
+}
+
+static void ldap_reconnect(struct ldap_connection *conn);
+
+/*
+  handle packet errors
+*/
+static void ldap_error_handler(struct ldap_connection *conn, NTSTATUS status)
+{
+       ldap_connection_dead(conn, status);
 
-       talloc_free(conn->tls);
-       conn->tls = NULL;
+       /* but try to reconnect so that the ldb client can go on */
+       ldap_reconnect(conn);
 }
 
 
@@ -103,6 +142,7 @@ static void ldap_connection_dead(struct ldap_connection *conn)
 static void ldap_match_message(struct ldap_connection *conn, struct ldap_message *msg)
 {
        struct ldap_request *req;
+       int i;
 
        for (req=conn->pending; req; req=req->next) {
                if (req->messageid == msg->messageid) break;
@@ -115,15 +155,30 @@ static void ldap_match_message(struct ldap_connection *conn, struct ldap_message
        if (req == NULL) {
                DEBUG(0,("ldap: no matching message id for %u\n",
                         msg->messageid));
-               talloc_free(msg);
+               TALLOC_FREE(msg);
                return;
        }
 
+       /* Check for undecoded critical extensions */
+       for (i=0; msg->controls && msg->controls[i]; i++) {
+               if (!msg->controls_decoded[i] && 
+                   msg->controls[i]->critical) {
+                       TALLOC_FREE(msg);
+                       req->status = NT_STATUS_LDAP(LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
+                       req->state = LDAP_REQUEST_DONE;
+                       DLIST_REMOVE(conn->pending, req);
+                       if (req->async.fn) {
+                               req->async.fn(req);
+                       }
+                       return;
+               }
+       }
+
        /* add to the list of replies received */
-       talloc_steal(req, msg);
        req->replies = talloc_realloc(req, req->replies, 
                                      struct ldap_message *, req->num_replies+1);
        if (req->replies == NULL) {
+               TALLOC_FREE(msg);
                req->status = NT_STATUS_NO_MEMORY;
                req->state = LDAP_REQUEST_DONE;
                DLIST_REMOVE(conn->pending, req);
@@ -149,234 +204,147 @@ static void ldap_match_message(struct ldap_connection *conn, struct ldap_message
        }
 }
 
-/*
-  try and decode/process plain data
-*/
-static void ldap_try_decode_plain(struct ldap_connection *conn)
+static void ldap_connection_recv_done(struct tevent_req *subreq);
+
+static void ldap_connection_recv_next(struct ldap_connection *conn)
 {
-       struct asn1_data asn1;
+       struct tevent_req *subreq = NULL;
 
-       if (!asn1_load(&asn1, conn->partial)) {
-               ldap_connection_dead(conn);
+       if (conn->sockets.recv_subreq != NULL) {
                return;
        }
 
-       /* try and decode - this will fail if we don't have a full packet yet */
-       while (asn1.ofs < asn1.length) {
-               struct ldap_message *msg = talloc(conn, struct ldap_message);
-               off_t saved_ofs = asn1.ofs;
-                       
-               if (msg == NULL) {
-                       ldap_connection_dead(conn);
-                       return;
-               }
-
-               if (ldap_decode(&asn1, msg)) {
-                       ldap_match_message(conn, msg);
-               } else {
-                       asn1.ofs = saved_ofs;
-                       talloc_free(msg);
-                       break;
-               }
+       if (conn->sockets.active == NULL) {
+               return;
        }
 
-       /* keep any remaining data in conn->partial */
-       data_blob_free(&conn->partial);
-       if (asn1.ofs != asn1.length) {
-               conn->partial = data_blob_talloc(conn, 
-                                                asn1.data + asn1.ofs, 
-                                                asn1.length - asn1.ofs);
+       if (conn->pending == NULL) {
+               return;
        }
-       asn1_free(&asn1);
-}
-
-/*
-  try and decode/process wrapped data
-*/
-static void ldap_try_decode_wrapped(struct ldap_connection *conn)
-{
-       uint32_t len;
-
-       /* keep decoding while we have a full wrapped packet */
-       while (conn->partial.length >= 4 &&
-              (len=RIVAL(conn->partial.data, 0)) <= conn->partial.length-4) {
-               DATA_BLOB wrapped, unwrapped;
-               struct asn1_data asn1;
-               struct ldap_message *msg = talloc(conn, struct ldap_message);
-               NTSTATUS status;
-
-               if (msg == NULL) {
-                       ldap_connection_dead(conn);
-                       return;
-               }
-
-               wrapped.data   = conn->partial.data+4;
-               wrapped.length = len;
-
-               status = gensec_unwrap(conn->gensec, msg, &wrapped, &unwrapped);
-               if (!NT_STATUS_IS_OK(status)) {
-                       ldap_connection_dead(conn);
-                       return;
-               }
-
-               if (!asn1_load(&asn1, unwrapped)) {
-                       ldap_connection_dead(conn);
-                       return;
-               }
 
-               while (ldap_decode(&asn1, msg)) {
-                       ldap_match_message(conn, msg);
-                       msg = talloc(conn, struct ldap_message);
-               }
-               
-               talloc_free(msg);
-               asn1_free(&asn1);
-
-               if (conn->partial.length == len + 4) {
-                       data_blob_free(&conn->partial);
-               } else {
-                       memmove(conn->partial.data, conn->partial.data+len+4,
-                               conn->partial.length - (len+4));
-                       conn->partial.length -= len + 4;
-               }
+       /*
+        * The minimum size of a LDAP pdu is 7 bytes
+        *
+        * dumpasn1 -hh ldap-unbind-min.dat
+        *
+        *     <30 05 02 01 09 42 00>
+        *    0    5: SEQUENCE {
+        *     <02 01 09>
+        *    2    1:   INTEGER 9
+        *     <42 00>
+        *    5    0:   [APPLICATION 2]
+        *          :     Error: Object has zero length.
+        *          :   }
+        *
+        * dumpasn1 -hh ldap-unbind-windows.dat
+        *
+        *     <30 84 00 00 00 05 02 01 09 42 00>
+        *    0    5: SEQUENCE {
+        *     <02 01 09>
+        *    6    1:   INTEGER 9
+        *     <42 00>
+        *    9    0:   [APPLICATION 2]
+        *          :     Error: Object has zero length.
+        *          :   }
+        *
+        * This means using an initial read size
+        * of 7 is ok.
+        */
+       subreq = tstream_read_pdu_blob_send(conn,
+                                           conn->event.event_ctx,
+                                           conn->sockets.active,
+                                           7, /* initial_read_size */
+                                           ldap_full_packet,
+                                           conn);
+       if (subreq == NULL) {
+               ldap_error_handler(conn, NT_STATUS_NO_MEMORY);
+               return;
        }
+       tevent_req_set_callback(subreq, ldap_connection_recv_done, conn);
+       conn->sockets.recv_subreq = subreq;
+       return;
 }
 
-
 /*
-  handle ldap recv events
+  decode/process LDAP data
 */
-static void ldap_recv_handler(struct ldap_connection *conn)
+static void ldap_connection_recv_done(struct tevent_req *subreq)
 {
        NTSTATUS status;
-       size_t npending=0, nread;
-
-       /* work out how much data is pending */
-       status = tls_socket_pending(conn->tls, &npending);
-       if (!NT_STATUS_IS_OK(status) || npending == 0) {
-               ldap_connection_dead(conn);
+       struct ldap_connection *conn =
+               tevent_req_callback_data(subreq,
+               struct ldap_connection);
+       struct ldap_message *msg;
+       struct asn1_data *asn1;
+       DATA_BLOB blob;
+
+       msg = talloc_zero(conn, struct ldap_message);
+       if (msg == NULL) {
+               ldap_error_handler(conn, NT_STATUS_NO_MEMORY);
                return;
        }
 
-       conn->partial.data = talloc_realloc_size(conn, conn->partial.data, 
-                                                conn->partial.length + npending);
-       if (conn->partial.data == NULL) {
-               ldap_connection_dead(conn);
+       asn1 = asn1_init(conn);
+       if (asn1 == NULL) {
+               TALLOC_FREE(msg);
+               ldap_error_handler(conn, NT_STATUS_NO_MEMORY);
                return;
        }
 
-       /* receive the pending data */
-       status = tls_socket_recv(conn->tls, conn->partial.data + conn->partial.length,
-                                npending, &nread);
-       if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
-               return;
-       }
+       conn->sockets.recv_subreq = NULL;
+
+       status = tstream_read_pdu_blob_recv(subreq,
+                                           asn1,
+                                           &blob);
+       TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               ldap_connection_dead(conn);
+               TALLOC_FREE(msg);
+               asn1_free(asn1);
+               ldap_error_handler(conn, status);
                return;
        }
-       conn->partial.length += nread;
-
-       /* see if we can decode what we have */
-       if (conn->enable_wrap) {
-               ldap_try_decode_wrapped(conn);
-       } else {
-               ldap_try_decode_plain(conn);
-       }
-}
-
 
-/*
-  handle ldap send events
-*/
-static void ldap_send_handler(struct ldap_connection *conn)
-{
-       while (conn->send_queue) {
-               struct ldap_request *req = conn->send_queue;
-               size_t nsent;
-               NTSTATUS status;
-
-               status = tls_socket_send(conn->tls, &req->data, &nsent);
-               if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
-                       break;
-               }
-               if (!NT_STATUS_IS_OK(status)) {
-                       ldap_connection_dead(conn);
-                       return;
-               }
+       asn1_load_nocopy(asn1, blob.data, blob.length);
 
-               req->data.data += nsent;
-               req->data.length -= nsent;
-               if (req->data.length == 0) {
-                       req->state = LDAP_REQUEST_PENDING;
-                       DLIST_REMOVE(conn->send_queue, req);
-
-                       /* some types of requests don't expect a reply */
-                       if (req->type == LDAP_TAG_AbandonRequest ||
-                           req->type == LDAP_TAG_UnbindRequest) {
-                               req->status = NT_STATUS_OK;
-                               req->state = LDAP_REQUEST_DONE;
-                               if (req->async.fn) {
-                                       req->async.fn(req);
-                               }
-                       } else {
-                               DLIST_ADD(conn->pending, req);
-                       }
-               }
-       }
-       if (conn->send_queue == NULL) {
-               EVENT_FD_NOT_WRITEABLE(conn->event.fde);
+       status = ldap_decode(asn1, samba_ldap_control_handlers(), msg);
+       asn1_free(asn1);
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(msg);
+               ldap_error_handler(conn, status);
+               return;
        }
-}
 
+       ldap_match_message(conn, msg);
+       ldap_connection_recv_next(conn);
 
-/*
-  handle ldap socket events
-*/
-static void ldap_io_handler(struct event_context *ev, struct fd_event *fde, 
-                           uint16_t flags, void *private)
-{
-       struct ldap_connection *conn = talloc_get_type(private, struct ldap_connection);
-       if (flags & EVENT_FD_WRITE) {
-               ldap_send_handler(conn);
-               if (conn->tls == NULL) return;
-       }
-       if (flags & EVENT_FD_READ) {
-               ldap_recv_handler(conn);
-       }
+       return;
 }
 
 /*
   parse a ldap URL
 */
 static NTSTATUS ldap_parse_basic_url(TALLOC_CTX *mem_ctx, const char *url,
-                                    char **host, uint16_t *port, BOOL *ldaps)
+                                    char **host, uint16_t *port, bool *ldaps)
 {
        int tmp_port = 0;
        char protocol[11];
-       char tmp_host[255];
-       const char *p = url;
+       char tmp_host[1025];
        int ret;
 
-       /* skip leading "URL:" (if any) */
-       if (strncasecmp(p, "URL:", 4) == 0) {
-               p += 4;
-       }
-
        /* Paranoia check */
        SMB_ASSERT(sizeof(protocol)>10 && sizeof(tmp_host)>254);
                
-       ret = sscanf(p, "%10[^:]://%254[^:/]:%d", protocol, tmp_host, &tmp_port);
+       ret = sscanf(url, "%10[^:]://%254[^:/]:%d", protocol, tmp_host, &tmp_port);
        if (ret < 2) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
        if (strequal(protocol, "ldap")) {
                *port = 389;
-               *ldaps = False;
+               *ldaps = false;
        } else if (strequal(protocol, "ldaps")) {
                *port = 636;
-               *ldaps = True;
+               *ldaps = true;
        } else {
                DEBUG(0, ("unrecognised ldap protocol (%s)!\n", protocol));
                return NT_STATUS_PROTOCOL_UNREACHABLE;
@@ -398,17 +366,24 @@ static NTSTATUS ldap_parse_basic_url(TALLOC_CTX *mem_ctx, const char *url,
 struct ldap_connect_state {
        struct composite_context *ctx;
        struct ldap_connection *conn;
+       struct socket_context *sock;
+       struct tstream_context *raw;
+       struct tstream_tls_params *tls_params;
+       struct tstream_context *tls;
 };
 
-static void ldap_connect_recv_conn(struct composite_context *ctx);
+static void ldap_connect_recv_unix_conn(struct composite_context *ctx);
+static void ldap_connect_recv_tcp_conn(struct composite_context *ctx);
 
-struct composite_context *ldap_connect_send(struct ldap_connection *conn,
+_PUBLIC_ struct composite_context *ldap_connect_send(struct ldap_connection *conn,
                                            const char *url)
 {
        struct composite_context *result, *ctx;
        struct ldap_connect_state *state;
+       char protocol[11];
+       int ret;
 
-       result = talloc_zero(NULL, struct composite_context);
+       result = talloc_zero(conn, struct composite_context);
        if (result == NULL) goto failed;
        result->state = COMPOSITE_STATE_IN_PROGRESS;
        result->async.fn = NULL;
@@ -421,106 +396,347 @@ struct composite_context *ldap_connect_send(struct ldap_connection *conn,
 
        state->conn = conn;
 
-       state->ctx->status = ldap_parse_basic_url(conn, url, &conn->host,
-                                                 &conn->port, &conn->ldaps);
-       if (!NT_STATUS_IS_OK(state->ctx->status)) {
-               composite_trigger_error(state->ctx);
-               return result;
+       if (conn->reconnect.url == NULL) {
+               conn->reconnect.url = talloc_strdup(conn, url);
+               if (conn->reconnect.url == NULL) goto failed;
        }
 
-       state->ctx->status = socket_create("ipv4", SOCKET_TYPE_STREAM,
-                                          &conn->sock, 0);
-       if (!NT_STATUS_IS_OK(state->ctx->status)) {
-               composite_trigger_error(state->ctx);
-               return result;
+       /* Paranoia check */
+       SMB_ASSERT(sizeof(protocol)>10);
+
+       ret = sscanf(url, "%10[^:]://", protocol);
+       if (ret < 1) {
+               return NULL;
        }
 
-       talloc_steal(conn, conn->sock);
+       if (strequal(protocol, "ldapi")) {
+               struct socket_address *unix_addr;
+               char path[1025];
+               char *end = NULL;
+               NTSTATUS status = socket_create(state, "unix",
+                                               SOCKET_TYPE_STREAM,
+                                               &state->sock, 0);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return NULL;
+               }
+               SMB_ASSERT(sizeof(protocol)>10);
+               SMB_ASSERT(sizeof(path)>1024);
+       
+               /* LDAPI connections are to localhost, so give the
+                * local host name as the target for gensec's
+                * DIGEST-MD5 mechanism */
+               conn->host = talloc_asprintf(conn, "%s.%s",
+                                            lpcfg_netbios_name(conn->lp_ctx),
+                                            lpcfg_dnsdomain(conn->lp_ctx));
+               if (composite_nomem(conn->host, state->ctx)) {
+                       return result;
+               }
+
+               /* The %c specifier doesn't null terminate :-( */
+               ZERO_STRUCT(path);
+               ret = sscanf(url, "%10[^:]://%1025c", protocol, path);
+               if (ret < 2) {
+                       composite_error(state->ctx, NT_STATUS_INVALID_PARAMETER);
+                       return result;
+               }
+
+               end = rfc1738_unescape(path);
+               if (end == NULL) {
+                       composite_error(state->ctx,
+                                       NT_STATUS_INVALID_PARAMETER);
+                       return result;
+               }       
+               unix_addr = socket_address_from_strings(state, state->sock->backend_name,
+                                                       path, 0);
+               if (composite_nomem(unix_addr, result)) {
+                       return result;
+               }
+
+               ctx = socket_connect_send(state->sock, NULL, unix_addr,
+                                         0, result->event_ctx);
+               ctx->async.fn = ldap_connect_recv_unix_conn;
+               ctx->async.private_data = state;
+               return result;
+       } else {
+               NTSTATUS status = ldap_parse_basic_url(conn, url, &conn->host,
+                                                         &conn->port, &conn->ldaps);
+               if (!NT_STATUS_IS_OK(status)) {
+                       composite_error(result, status);
+                       return result;
+               }
 
-       ctx = socket_connect_send(conn->sock, NULL, 0, conn->host,
-                                 conn->port, 0, conn->event.event_ctx);
-       if (ctx == NULL) goto failed;
+               if (conn->ldaps) {
+                       char *ca_file = lpcfg_tls_cafile(state, conn->lp_ctx);
+                       char *crl_file = lpcfg_tls_crlfile(state, conn->lp_ctx);
+                       const char *tls_priority = lpcfg_tls_priority(conn->lp_ctx);
+                       enum tls_verify_peer_state verify_peer =
+                               lpcfg_tls_verify_peer(conn->lp_ctx);
+
+                       status = tstream_tls_params_client(state,
+                                                          ca_file,
+                                                          crl_file,
+                                                          tls_priority,
+                                                          verify_peer,
+                                                          conn->host,
+                                                          &state->tls_params);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               composite_error(result, status);
+                               return result;
+                       }
+               }
 
-       ctx->async.fn = ldap_connect_recv_conn;
-       ctx->async.private_data = state;
-       return result;
+               ctx = socket_connect_multi_send(state, conn->host, 1, &conn->port,
+                                               lpcfg_resolve_context(conn->lp_ctx),
+                                               result->event_ctx);
+               if (composite_nomem(ctx, result)) {
+                       return result;
+               }
 
+               ctx->async.fn = ldap_connect_recv_tcp_conn;
+               ctx->async.private_data = state;
+               return result;
+       }
  failed:
        talloc_free(result);
        return NULL;
 }
 
-static void ldap_connect_recv_conn(struct composite_context *ctx)
+static void ldap_connect_got_tls(struct tevent_req *subreq);
+
+static void ldap_connect_got_sock(struct composite_context *ctx, 
+                                 struct ldap_connection *conn)
 {
        struct ldap_connect_state *state =
-               talloc_get_type(ctx->async.private_data,
-                               struct ldap_connect_state);
-       struct ldap_connection *conn = state->conn;
+               talloc_get_type_abort(ctx->private_data,
+               struct ldap_connect_state);
+       struct tevent_req *subreq = NULL;
+       int fd;
+       int ret;
+
+       socket_set_flags(state->sock, SOCKET_FLAG_NOCLOSE);
+       fd = socket_get_fd(state->sock);
+       TALLOC_FREE(state->sock);
+
+       smb_set_close_on_exec(fd);
 
-       state->ctx->status = socket_connect_recv(ctx);
-       if (!composite_is_ok(state->ctx)) return;
+       ret = set_blocking(fd, false);
+       if (ret == -1) {
+               NTSTATUS status = map_nt_error_from_unix_common(errno);
+               composite_error(state->ctx, status);
+               return;
+       }
+
+       ret = tstream_bsd_existing_socket(state, fd, &state->raw);
+       if (ret == -1) {
+               NTSTATUS status = map_nt_error_from_unix_common(errno);
+               composite_error(state->ctx, status);
+               return;
+       }
+
+       if (!conn->ldaps) {
+               conn->sockets.raw = talloc_move(conn, &state->raw);
+               conn->sockets.active = conn->sockets.raw;
+               composite_done(state->ctx);
+               return;
+       }
 
-       /* setup a handler for events on this socket */
-       conn->event.fde = event_add_fd(conn->event.event_ctx, conn->sock, 
-                                      socket_get_fd(conn->sock), 
-                                      EVENT_FD_READ, ldap_io_handler, conn);
-       if (conn->event.fde == NULL) {
-               composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
+       subreq = tstream_tls_connect_send(state, state->ctx->event_ctx,
+                                         state->raw, state->tls_params);
+       if (composite_nomem(subreq, state->ctx)) {
                return;
        }
+       tevent_req_set_callback(subreq, ldap_connect_got_tls, state);
+}
+
+static void ldap_connect_got_tls(struct tevent_req *subreq)
+{
+       struct ldap_connect_state *state =
+               tevent_req_callback_data(subreq,
+               struct ldap_connect_state);
+       int err;
+       int ret;
 
-       conn->tls = tls_init_client(conn->sock, conn->event.fde, conn->ldaps);
-       if (conn->tls == NULL) {
-               talloc_free(conn->sock);
+       ret = tstream_tls_connect_recv(subreq, &err, state, &state->tls);
+       TALLOC_FREE(subreq);
+       if (ret == -1) {
+               NTSTATUS status = map_nt_error_from_unix_common(err);
+               composite_error(state->ctx, status);
                return;
        }
-       talloc_steal(conn, conn->tls);
-       talloc_steal(conn->tls, conn->sock);
 
+       talloc_steal(state->tls, state->tls_params);
+
+       state->conn->sockets.raw = talloc_move(state->conn, &state->raw);
+       state->conn->sockets.tls = talloc_move(state->conn->sockets.raw,
+                                              &state->tls);
+       state->conn->sockets.active = state->conn->sockets.tls;
        composite_done(state->ctx);
+}
 
-       return;
+static void ldap_connect_recv_tcp_conn(struct composite_context *ctx)
+{
+       struct ldap_connect_state *state =
+               talloc_get_type_abort(ctx->async.private_data,
+               struct ldap_connect_state);
+       struct ldap_connection *conn = state->conn;
+       uint16_t port;
+       NTSTATUS status = socket_connect_multi_recv(ctx, state, &state->sock,
+                                                      &port);
+       if (!NT_STATUS_IS_OK(status)) {
+               composite_error(state->ctx, status);
+               return;
+       }
+
+       ldap_connect_got_sock(state->ctx, conn);
+}
+
+static void ldap_connect_recv_unix_conn(struct composite_context *ctx)
+{
+       struct ldap_connect_state *state =
+               talloc_get_type_abort(ctx->async.private_data,
+               struct ldap_connect_state);
+       struct ldap_connection *conn = state->conn;
+
+       NTSTATUS status = socket_connect_recv(ctx);
+
+       if (!NT_STATUS_IS_OK(state->ctx->status)) {
+               composite_error(state->ctx, status);
+               return;
+       }
+
+       ldap_connect_got_sock(state->ctx, conn);
 }
 
-NTSTATUS ldap_connect_recv(struct composite_context *ctx)
+_PUBLIC_ NTSTATUS ldap_connect_recv(struct composite_context *ctx)
 {
        NTSTATUS status = composite_wait(ctx);
        talloc_free(ctx);
        return status;
 }
 
-NTSTATUS ldap_connect(struct ldap_connection *conn, const char *url)
+_PUBLIC_ NTSTATUS ldap_connect(struct ldap_connection *conn, const char *url)
 {
        struct composite_context *ctx = ldap_connect_send(conn, url);
        return ldap_connect_recv(ctx);
 }
 
-/* destroy an open ldap request */
-static int ldap_request_destructor(void *ptr)
+/* set reconnect parameters */
+
+_PUBLIC_ void ldap_set_reconn_params(struct ldap_connection *conn, int max_retries)
+{
+       if (conn) {
+               conn->reconnect.max_retries = max_retries;
+               conn->reconnect.retries = 0;
+               conn->reconnect.previous = time_mono(NULL);
+       }
+}
+
+/* Actually this function is NOT ASYNC safe, FIXME? */
+static void ldap_reconnect(struct ldap_connection *conn)
 {
-       struct ldap_request *req = talloc_get_type(ptr, struct ldap_request);
-       if (req->state == LDAP_REQUEST_SEND) {
-               DLIST_REMOVE(req->conn->send_queue, req);
+       NTSTATUS status;
+       time_t now = time_mono(NULL);
+
+       /* do we have set up reconnect ? */
+       if (conn->reconnect.max_retries == 0) return;
+
+       /* is the retry time expired ? */
+       if (now > conn->reconnect.previous + 30) {
+               conn->reconnect.retries = 0;
+               conn->reconnect.previous = now;
+       }
+
+       /* are we reconnectind too often and too fast? */
+       if (conn->reconnect.retries > conn->reconnect.max_retries) return;
+
+       /* keep track of the number of reconnections */
+       conn->reconnect.retries++;
+
+       /* reconnect */
+       status = ldap_connect(conn, conn->reconnect.url);
+       if ( ! NT_STATUS_IS_OK(status)) {
+               return;
        }
+
+       /* rebind */
+       status = ldap_rebind(conn);
+       if ( ! NT_STATUS_IS_OK(status)) {
+               ldap_connection_dead(conn, status);
+       }
+}
+
+static void ldap_request_destructor_abandon(struct ldap_request *abandon)
+{
+       TALLOC_FREE(abandon);
+}
+
+/* destroy an open ldap request */
+static int ldap_request_destructor(struct ldap_request *req)
+{
        if (req->state == LDAP_REQUEST_PENDING) {
+               struct ldap_message msg = {
+                       .type = LDAP_TAG_AbandonRequest,
+                       .r.AbandonRequest.messageid = req->messageid,
+               };
+               struct ldap_request *abandon = NULL;
+
                DLIST_REMOVE(req->conn->pending, req);
+
+               abandon = ldap_request_send(req->conn, &msg);
+               if (abandon == NULL) {
+                       ldap_error_handler(req->conn, NT_STATUS_NO_MEMORY);
+                       return 0;
+               }
+               abandon->async.fn = ldap_request_destructor_abandon;
+               abandon->async.private_data = NULL;
        }
+
        return 0;
 }
 
+static void ldap_request_timeout_abandon(struct ldap_request *abandon)
+{
+       struct ldap_request *req =
+               talloc_get_type_abort(abandon->async.private_data,
+               struct ldap_request);
+
+       if (req->state == LDAP_REQUEST_PENDING) {
+               DLIST_REMOVE(req->conn->pending, req);
+       }
+       req->state = LDAP_REQUEST_DONE;
+       if (req->async.fn) {
+               req->async.fn(req);
+       }
+}
+
 /*
   called on timeout of a ldap request
 */
-static void ldap_request_timeout(struct event_context *ev, struct timed_event *te, 
-                                     struct timeval t, void *private)
+static void ldap_request_timeout(struct tevent_context *ev, struct tevent_timer *te, 
+                                     struct timeval t, void *private_data)
 {
-       struct ldap_request *req = talloc_get_type(private, struct ldap_request);
+       struct ldap_request *req =
+               talloc_get_type_abort(private_data,
+               struct ldap_request);
+
        req->status = NT_STATUS_IO_TIMEOUT;
-       if (req->state == LDAP_REQUEST_SEND) {
-               DLIST_REMOVE(req->conn->send_queue, req);
-       }
        if (req->state == LDAP_REQUEST_PENDING) {
+               struct ldap_message msg = {
+                       .type = LDAP_TAG_AbandonRequest,
+                       .r.AbandonRequest.messageid = req->messageid,
+               };
+               struct ldap_request *abandon = NULL;
+
+               abandon = ldap_request_send(req->conn, &msg);
+               if (abandon == NULL) {
+                       ldap_error_handler(req->conn, NT_STATUS_NO_MEMORY);
+                       return;
+               }
+               talloc_reparent(req->conn, req, abandon);
+               abandon->async.fn = ldap_request_timeout_abandon;
+               abandon->async.private_data = req;
                DLIST_REMOVE(req->conn->pending, req);
+               return;
        }
        req->state = LDAP_REQUEST_DONE;
        if (req->async.fn) {
@@ -528,20 +744,41 @@ static void ldap_request_timeout(struct event_context *ev, struct timed_event *t
        }
 }
 
+
+/*
+  called on completion of a failed ldap request
+*/
+static void ldap_request_failed_complete(struct tevent_context *ev, struct tevent_timer *te,
+                                     struct timeval t, void *private_data)
+{
+       struct ldap_request *req =
+               talloc_get_type_abort(private_data,
+               struct ldap_request);
+
+       if (req->async.fn) {
+               req->async.fn(req);
+       }
+}
+
+static void ldap_request_written(struct tevent_req *subreq);
+
 /*
   send a ldap message - async interface
 */
-struct ldap_request *ldap_request_send(struct ldap_connection *conn,
+_PUBLIC_ struct ldap_request *ldap_request_send(struct ldap_connection *conn,
                                       struct ldap_message *msg)
 {
        struct ldap_request *req;
-
-       if (conn->tls == NULL) {
-               return NULL;
-       }
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       struct tevent_req *subreq = NULL;
 
        req = talloc_zero(conn, struct ldap_request);
-       if (req == NULL) goto failed;
+       if (req == NULL) return NULL;
+
+       if (conn->sockets.active == NULL) {
+               status = NT_STATUS_INVALID_CONNECTION;
+               goto failed;
+       }
 
        req->state       = LDAP_REQUEST_SEND;
        req->conn        = conn;
@@ -558,45 +795,77 @@ struct ldap_request *ldap_request_send(struct ldap_connection *conn,
 
        msg->messageid = req->messageid;
 
-       if (!ldap_encode(msg, &req->data, req)) {
+       if (!ldap_encode(msg, samba_ldap_control_handlers(), &req->data, req)) {
+               status = NT_STATUS_INTERNAL_ERROR;
                goto failed;            
        }
 
-       /* possibly encrypt/sign the request */
-       if (conn->enable_wrap) {
-               DATA_BLOB wrapped;
-               NTSTATUS status;
-
-               status = gensec_wrap(conn->gensec, req, &req->data, &wrapped);
-               if (!NT_STATUS_IS_OK(status)) {
-                       goto failed;
-               }
-               data_blob_free(&req->data);
-               req->data = data_blob_talloc(req, NULL, wrapped.length + 4);
-               if (req->data.data == NULL) {
-                       goto failed;
-               }
-               RSIVAL(req->data.data, 0, wrapped.length);
-               memcpy(req->data.data+4, wrapped.data, wrapped.length);
-               data_blob_free(&wrapped);
+       /* put a timeout on the request */
+       req->time_event = tevent_add_timer(conn->event.event_ctx, req,
+                                          timeval_current_ofs(conn->timeout, 0),
+                                          ldap_request_timeout, req);
+       if (req->time_event == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto failed;
        }
 
+       req->write_iov.iov_base = req->data.data;
+       req->write_iov.iov_len = req->data.length;
 
-       if (conn->send_queue == NULL) {
-               EVENT_FD_WRITEABLE(conn->event.fde);
+       subreq = tstream_writev_queue_send(req, conn->event.event_ctx,
+                                          conn->sockets.active,
+                                          conn->sockets.send_queue,
+                                          &req->write_iov, 1);
+       if (subreq == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto failed;
        }
-       DLIST_ADD_END(conn->send_queue, req, struct ldap_request *);
+       tevent_req_set_callback(subreq, ldap_request_written, req);
 
-       /* put a timeout on the request */
-       event_add_timed(conn->event.event_ctx, req, 
-                       timeval_current_ofs(conn->timeout, 0),
-                       ldap_request_timeout, req);
+       req->state = LDAP_REQUEST_PENDING;
+       DLIST_ADD(conn->pending, req);
 
        return req;
 
 failed:
-       talloc_free(req);
-       return NULL;
+       req->status = status;
+       req->state = LDAP_REQUEST_ERROR;
+       tevent_add_timer(conn->event.event_ctx, req, timeval_zero(),
+                        ldap_request_failed_complete, req);
+
+       return req;
+}
+
+static void ldap_request_written(struct tevent_req *subreq)
+{
+       struct ldap_request *req =
+               tevent_req_callback_data(subreq,
+               struct ldap_request);
+       int err;
+       ssize_t ret;
+
+       ret = tstream_writev_queue_recv(subreq, &err);
+       TALLOC_FREE(subreq);
+       if (ret == -1) {
+               NTSTATUS error = map_nt_error_from_unix_common(err);
+               ldap_error_handler(req->conn, error);
+               return;
+       }
+
+       if (req->type == LDAP_TAG_AbandonRequest ||
+           req->type == LDAP_TAG_UnbindRequest)
+       {
+               if (req->state == LDAP_REQUEST_PENDING) {
+                       DLIST_REMOVE(req->conn->pending, req);
+               }
+               req->state = LDAP_REQUEST_DONE;
+               if (req->async.fn) {
+                       req->async.fn(req);
+               }
+               return;
+       }
+
+       ldap_connection_recv_next(req->conn);
 }
 
 
@@ -604,10 +873,11 @@ failed:
   wait for a request to complete
   note that this does not destroy the request
 */
-NTSTATUS ldap_request_wait(struct ldap_request *req)
+_PUBLIC_ NTSTATUS ldap_request_wait(struct ldap_request *req)
 {
-       while (req->state != LDAP_REQUEST_DONE) {
-               if (event_loop_once(req->conn->event.event_ctx) != 0) {
+       while (req->state < LDAP_REQUEST_DONE) {
+               if (tevent_loop_once(req->conn->event.event_ctx) != 0) {
+                       req->state = LDAP_REQUEST_ERROR;
                        req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
                        break;
                }
@@ -650,7 +920,7 @@ static const struct {
        _LDAP_MAP_CODE(LDAP_ALIAS_DEREFERENCING_PROBLEM),
        _LDAP_MAP_CODE(LDAP_INAPPROPRIATE_AUTHENTICATION),
        _LDAP_MAP_CODE(LDAP_INVALID_CREDENTIALS),
-       _LDAP_MAP_CODE(LDAP_INSUFFICIENT_ACCESS_RIGHTs),
+       _LDAP_MAP_CODE(LDAP_INSUFFICIENT_ACCESS_RIGHTS),
        _LDAP_MAP_CODE(LDAP_BUSY),
        _LDAP_MAP_CODE(LDAP_UNAVAILABLE),
        _LDAP_MAP_CODE(LDAP_UNWILLING_TO_PERFORM),
@@ -668,7 +938,7 @@ static const struct {
 /*
   used to setup the status code from a ldap response
 */
-NTSTATUS ldap_check_response(struct ldap_connection *conn, struct ldap_Result *r)
+_PUBLIC_ NTSTATUS ldap_check_response(struct ldap_connection *conn, struct ldap_Result *r)
 {
        int i;
        const char *codename = "unknown";
@@ -701,26 +971,28 @@ NTSTATUS ldap_check_response(struct ldap_connection *conn, struct ldap_Result *r
 /*
   return error string representing the last error
 */
-const char *ldap_errstr(struct ldap_connection *conn, NTSTATUS status)
+_PUBLIC_ const char *ldap_errstr(struct ldap_connection *conn, 
+                       TALLOC_CTX *mem_ctx, 
+                       NTSTATUS status)
 {
        if (NT_STATUS_IS_LDAP(status) && conn->last_error != NULL) {
-               return conn->last_error;
+               return talloc_strdup(mem_ctx, conn->last_error);
        }
-       return nt_errstr(status);
+       return talloc_asprintf(mem_ctx, "LDAP client internal error: %s", nt_errstr(status));
 }
 
 
 /*
   return the Nth result message, waiting if necessary
 */
-NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **msg)
+_PUBLIC_ NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **msg)
 {
        *msg = NULL;
 
        NT_STATUS_HAVE_NO_MEMORY(req);
 
-       while (req->state != LDAP_REQUEST_DONE && n >= req->num_replies) {
-               if (event_loop_once(req->conn->event.event_ctx) != 0) {
+       while (req->state < LDAP_REQUEST_DONE && n >= req->num_replies) {
+               if (tevent_loop_once(req->conn->event.event_ctx) != 0) {
                        return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
                }
        }
@@ -741,7 +1013,7 @@ NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **ms
 /*
   return a single result message, checking if it is of the expected LDAP type
 */
-NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, int type)
+_PUBLIC_ NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, int type)
 {
        NTSTATUS status;
        status = ldap_result_n(req, 0, msg);
@@ -759,7 +1031,7 @@ NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, in
   a simple ldap transaction, for single result requests that only need a status code
   this relies on single valued requests having the response type == request type + 1
 */
-NTSTATUS ldap_transaction(struct ldap_connection *conn, struct ldap_message *msg)
+_PUBLIC_ NTSTATUS ldap_transaction(struct ldap_connection *conn, struct ldap_message *msg)
 {
        struct ldap_request *req = ldap_request_send(conn, msg);
        struct ldap_message *res;