s4:librpc/rpc: split out continue_smb_open()
[samba.git] / source4 / librpc / rpc / dcerpc_connect.c
index 59bcca0046b74493fbe312698ad7963d9ad66a21..f779d1a9d70ce983ff9d7c8cdd4efc86a1ebb131 100644 (file)
 #include "lib/events/events.h"
 #include "libcli/smb2/smb2.h"
 #include "libcli/smb2/smb2_calls.h"
+#include "libcli/smb/smbXcli_base.h"
 #include "librpc/rpc/dcerpc.h"
 #include "librpc/rpc/dcerpc_proto.h"
 #include "auth/credentials/credentials.h"
 #include "param/param.h"
 #include "libcli/resolve/resolve.h"
 
+struct dcerpc_pipe_connect {
+       struct dcecli_connection *conn;
+       struct dcerpc_binding *binding;
+       const struct ndr_interface_table *interface;
+       struct cli_credentials *creds;
+       struct resolve_context *resolve_ctx;
+       struct {
+               const char *dir;
+       } ncalrpc;
+       struct {
+               struct smbXcli_conn *conn;
+               struct smbXcli_session *session;
+               struct smbXcli_tcon *tcon;
+               const char *pipe_name;
+       } smb;
+};
 
 struct pipe_np_smb_state {
        struct smb_composite_connect conn;
-       struct smbcli_tree *tree;
        struct dcerpc_pipe_connect io;
 };
 
@@ -58,28 +74,49 @@ static void continue_pipe_open_smb(struct composite_context *ctx)
        composite_done(c);
 }
 
+static void continue_smb_open(struct composite_context *c);
 
 /*
   Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
 */
 static void continue_smb_connect(struct composite_context *ctx)
 {
-       struct composite_context *open_ctx;
        struct composite_context *c = talloc_get_type(ctx->async.private_data,
                                                      struct composite_context);
        struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
                                                      struct pipe_np_smb_state);
-       
+       struct smbcli_tree *t;
+
        /* receive result of smb connect request */
-       c->status = smb_composite_connect_recv(ctx, c);
+       c->status = smb_composite_connect_recv(ctx, s->io.conn);
        if (!composite_is_ok(c)) return;
 
+       t = s->conn.out.tree;
+
        /* prepare named pipe open parameters */
-       s->tree         = s->conn.out.tree;
-       s->io.pipe_name = s->io.binding->endpoint;
+       s->io.smb.conn = t->session->transport->conn;
+       s->io.smb.session = t->session->smbXcli;
+       s->io.smb.tcon = t->smbXcli;
+       smb1cli_tcon_set_id(s->io.smb.tcon, t->tid);
+       s->io.smb.pipe_name = dcerpc_binding_get_string_option(s->io.binding,
+                                                              "endpoint");
+
+       continue_smb_open(c);
+}
+
+static void continue_smb_open(struct composite_context *c)
+{
+       struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
+                                                     struct pipe_np_smb_state);
+       struct composite_context *open_ctx;
 
        /* send named pipe open request */
-       open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe, s->tree, s->io.pipe_name);
+       open_ctx = dcerpc_pipe_open_smb_send(s->io.conn,
+                                            s->io.smb.conn,
+                                            s->io.smb.session,
+                                            s->io.smb.tcon,
+                                            DCERPC_REQUEST_TIMEOUT * 1000,
+                                            s->io.smb.pipe_name);
        if (composite_nomem(open_ctx, c)) return;
 
        composite_continue(c, open_ctx, continue_pipe_open_smb, c);
@@ -96,9 +133,10 @@ static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CT
        struct pipe_np_smb_state *s;
        struct composite_context *conn_req;
        struct smb_composite_connect *conn;
+       uint32_t flags;
 
        /* composite context allocation and setup */
-       c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
+       c = composite_create(mem_ctx, io->conn->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct pipe_np_smb_state);
@@ -110,39 +148,40 @@ static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CT
 
        /* prepare smb connection parameters: we're connecting to IPC$ share on
           remote rpc server */
-       conn->in.dest_host              = s->io.binding->host;
-       conn->in.dest_ports                  = lp_smb_ports(lp_ctx);
-       if (s->io.binding->target_hostname == NULL)
-               conn->in.called_name = "*SMBSERVER"; /* FIXME: This is invalid */
-       else
-               conn->in.called_name            = s->io.binding->target_hostname;
-       conn->in.socket_options         = lp_socket_options(lp_ctx);
+       conn->in.dest_host = dcerpc_binding_get_string_option(s->io.binding, "host");
+       conn->in.dest_ports = lpcfg_smb_ports(lp_ctx);
+       conn->in.called_name =
+               dcerpc_binding_get_string_option(s->io.binding, "target_hostname");
+       if (conn->in.called_name == NULL) {
+               conn->in.called_name = "*SMBSERVER";
+       }
+       conn->in.socket_options         = lpcfg_socket_options(lp_ctx);
        conn->in.service                = "IPC$";
        conn->in.service_type           = NULL;
-       conn->in.workgroup              = lp_workgroup(lp_ctx);
-
-       conn->in.iconv_convenience = lp_iconv_convenience(lp_ctx);
+       conn->in.workgroup              = lpcfg_workgroup(lp_ctx);
+       conn->in.gensec_settings = lpcfg_gensec_settings(conn, lp_ctx);
 
-       lp_smbcli_options(lp_ctx, &conn->in.options);
-       lp_smbcli_session_options(lp_ctx, &conn->in.session_options);
+       lpcfg_smbcli_options(lp_ctx, &conn->in.options);
+       lpcfg_smbcli_session_options(lp_ctx, &conn->in.session_options);
 
        /*
         * provide proper credentials - user supplied, but allow a
         * fallback to anonymous if this is an schannel connection
         * (might be NT4 not allowing machine logins at session
-        * setup).
+        * setup) or if asked to do so by the caller (perhaps a SAMR password change?)
         */
        s->conn.in.credentials = s->io.creds;
-       if (s->io.binding->flags & DCERPC_SCHANNEL) {
+       flags = dcerpc_binding_get_flags(s->io.binding);
+       if (flags & (DCERPC_SCHANNEL|DCERPC_ANON_FALLBACK)) {
                conn->in.fallback_to_anonymous  = true;
        } else {
                conn->in.fallback_to_anonymous  = false;
        }
 
        /* send smb connect request */
-       conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, 
+       conn_req = smb_composite_connect_send(conn, s->io.conn,
                                              s->io.resolve_ctx,
-                                             s->io.pipe->conn->event_ctx);
+                                             c->event_ctx);
        if (composite_nomem(conn_req, c)) return c;
 
        composite_continue(c, conn_req, continue_smb_connect, c);
@@ -161,52 +200,30 @@ static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *
        return status;
 }
 
-
-struct pipe_np_smb2_state {
-       struct smb2_tree *tree;
-       struct dcerpc_pipe_connect io;
-};
-
-
-/*
-  Stage 3 of ncacn_np_smb: Named pipe opened (or not)
-*/
-static void continue_pipe_open_smb2(struct composite_context *ctx)
-{
-       struct composite_context *c = talloc_get_type(ctx->async.private_data,
-                                                     struct composite_context);
-
-       /* receive result of named pipe open request on smb2 */
-       c->status = dcerpc_pipe_open_smb2_recv(ctx);
-       if (!composite_is_ok(c)) return;
-
-       composite_done(c);
-}
-
-
 /*
   Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
 */
-static void continue_smb2_connect(struct composite_context *ctx)
+static void continue_smb2_connect(struct tevent_req *subreq)
 {
-       struct composite_context *open_req;
-       struct composite_context *c = talloc_get_type(ctx->async.private_data,
-                                                     struct composite_context);
-       struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
-                                                      struct pipe_np_smb2_state);
+       struct composite_context *c =
+               tevent_req_callback_data(subreq,
+               struct composite_context);
+       struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
+                                                     struct pipe_np_smb_state);
+       struct smb2_tree *t;
 
        /* receive result of smb2 connect request */
-       c->status = smb2_connect_recv(ctx, c, &s->tree);
+       c->status = smb2_connect_recv(subreq, s->io.conn, &t);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
-       /* prepare named pipe open parameters */
-       s->io.pipe_name = s->io.binding->endpoint;
-
-       /* send named pipe open request */
-       open_req = dcerpc_pipe_open_smb2_send(s->io.pipe, s->tree, s->io.pipe_name);
-       if (composite_nomem(open_req, c)) return;
+       s->io.smb.conn = t->session->transport->conn;
+       s->io.smb.session = t->session->smbXcli;
+       s->io.smb.tcon = t->smbXcli;
+       s->io.smb.pipe_name = dcerpc_binding_get_string_option(s->io.binding,
+                                                              "endpoint");
 
-       composite_continue(c, open_req, continue_pipe_open_smb2, c);
+       continue_smb_open(c);
 }
 
 
@@ -220,45 +237,49 @@ static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(
                                        struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
-       struct pipe_np_smb2_state *s;
-       struct composite_context *conn_req;
+       struct pipe_np_smb_state *s;
+       struct tevent_req *subreq;
        struct smbcli_options options;
+       const char *host;
+       uint32_t flags;
 
        /* composite context allocation and setup */
-       c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
+       c = composite_create(mem_ctx, io->conn->event_ctx);
        if (c == NULL) return NULL;
 
-       s = talloc_zero(c, struct pipe_np_smb2_state);
+       s = talloc_zero(c, struct pipe_np_smb_state);
        if (composite_nomem(s, c)) return c;
        c->private_data = s;
 
        s->io = *io;
 
+       host = dcerpc_binding_get_string_option(s->io.binding, "host");
+       flags = dcerpc_binding_get_flags(s->io.binding);
+
        /*
         * provide proper credentials - user supplied or anonymous in case this is
         * schannel connection
         */
-       if (s->io.binding->flags & DCERPC_SCHANNEL) {
-               s->io.creds = cli_credentials_init(mem_ctx);
+       if (flags & DCERPC_SCHANNEL) {
+               s->io.creds = cli_credentials_init_anon(mem_ctx);
                if (composite_nomem(s->io.creds, c)) return c;
-
-               cli_credentials_guess(s->io.creds, lp_ctx);
        }
 
-       lp_smbcli_options(lp_ctx, &options);
+       lpcfg_smbcli_options(lp_ctx, &options);
 
        /* send smb2 connect request */
-       conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, 
-                       lp_parm_string_list(mem_ctx, lp_ctx, NULL, "smb2", "ports", NULL),
-                                       "IPC$", 
-                                    s->io.resolve_ctx,
-                                    s->io.creds,
-                                    c->event_ctx,
-                                    &options,
-                                        lp_socket_options(lp_ctx),
-                                        lp_gensec_settings(mem_ctx, lp_ctx)
-                                        );
-       composite_continue(c, conn_req, continue_smb2_connect, c);
+       subreq = smb2_connect_send(s, c->event_ctx,
+                       host,
+                       lpcfg_parm_string_list(mem_ctx, lp_ctx, NULL, "smb2", "ports", NULL),
+                       "IPC$",
+                       s->io.resolve_ctx,
+                       s->io.creds,
+                       0, /* previous_session_id */
+                       &options,
+                       lpcfg_socket_options(lp_ctx),
+                       lpcfg_gensec_settings(mem_ctx, lp_ctx));
+       if (composite_nomem(subreq, c)) return c;
+       tevent_req_set_callback(subreq, continue_smb2_connect, c);
        return c;
 }
 
@@ -277,6 +298,7 @@ static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context
 
 struct pipe_ip_tcp_state {
        struct dcerpc_pipe_connect io;
+       const char *localaddr;
        const char *host;
        const char *target_hostname;
        uint32_t port;
@@ -290,9 +312,23 @@ static void continue_pipe_open_ncacn_ip_tcp(struct composite_context *ctx)
 {
        struct composite_context *c = talloc_get_type(ctx->async.private_data,
                                                      struct composite_context);
+       struct pipe_ip_tcp_state *s = talloc_get_type(c->private_data,
+                                                     struct pipe_ip_tcp_state);
+       char *localaddr = NULL;
+       char *remoteaddr = NULL;
 
        /* receive result of named pipe open request on tcp/ip */
-       c->status = dcerpc_pipe_open_tcp_recv(ctx);
+       c->status = dcerpc_pipe_open_tcp_recv(ctx, s, &localaddr, &remoteaddr);
+       if (!composite_is_ok(c)) return;
+
+       c->status = dcerpc_binding_set_string_option(s->io.binding,
+                                                    "localaddress",
+                                                    localaddr);
+       if (!composite_is_ok(c)) return;
+
+       c->status = dcerpc_binding_set_string_option(s->io.binding,
+                                                    "host",
+                                                    remoteaddr);
        if (!composite_is_ok(c)) return;
 
        composite_done(c);
@@ -309,9 +345,10 @@ static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CT
        struct composite_context *c;
        struct pipe_ip_tcp_state *s;
        struct composite_context *pipe_req;
+       const char *endpoint;
 
        /* composite context allocation and setup */
-       c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
+       c = composite_create(mem_ctx, io->conn->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct pipe_ip_tcp_state);
@@ -319,14 +356,25 @@ static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CT
        c->private_data = s;
 
        /* store input parameters in state structure */
-       s->io               = *io;
-       s->host             = talloc_reference(c, io->binding->host);
-       s->target_hostname  = talloc_reference(c, io->binding->target_hostname);
-                             /* port number is a binding endpoint here */
-       s->port             = atoi(io->binding->endpoint);   
+       s->io = *io;
+       s->localaddr = dcerpc_binding_get_string_option(io->binding,
+                                                       "localaddress");
+       s->host = dcerpc_binding_get_string_option(io->binding, "host");
+       s->target_hostname = dcerpc_binding_get_string_option(io->binding,
+                                                             "target_hostname");
+       endpoint = dcerpc_binding_get_string_option(io->binding, "endpoint");
+       /* port number is a binding endpoint here */
+       if (endpoint != NULL) {
+               s->port = atoi(endpoint);
+       }
+
+       if (s->port == 0) {
+               composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
+               return c;
+       }
 
        /* send pipe open request on tcp/ip */
-       pipe_req = dcerpc_pipe_open_tcp_send(s->io.pipe->conn, s->host, s->target_hostname, 
+       pipe_req = dcerpc_pipe_open_tcp_send(s->io.conn, s->localaddr, s->host, s->target_hostname,
                                             s->port, io->resolve_ctx);
        composite_continue(c, pipe_req, continue_pipe_open_ncacn_ip_tcp, c);
        return c;
@@ -379,7 +427,7 @@ static struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALL
        struct composite_context *pipe_req;
 
        /* composite context allocation and setup */
-       c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
+       c = composite_create(mem_ctx, io->conn->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct pipe_unix_state);
@@ -389,18 +437,15 @@ static struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALL
        /* prepare pipe open parameters and store them in state structure
           also, verify whether biding endpoint is not null */
        s->io = *io;
-       
-       if (!io->binding->endpoint) {
-               DEBUG(0, ("Path to unix socket not specified\n"));
-               composite_error(c, NT_STATUS_INVALID_PARAMETER);
+
+       s->path = dcerpc_binding_get_string_option(io->binding, "endpoint");
+       if (s->path == NULL) {
+               composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
                return c;
        }
 
-       s->path  = talloc_strdup(c, io->binding->endpoint);  /* path is a binding endpoint here */
-       if (composite_nomem(s->path, c)) return c;
-
        /* send pipe open request on unix socket */
-       pipe_req = dcerpc_pipe_open_unix_stream_send(s->io.pipe->conn, s->path);
+       pipe_req = dcerpc_pipe_open_unix_stream_send(s->io.conn, s->path);
        composite_continue(c, pipe_req, continue_pipe_open_ncacn_unix_stream, c);
        return c;
 }
@@ -445,14 +490,15 @@ static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
    the binding structure to determine the endpoint and options
 */
 static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
-                                                                 struct dcerpc_pipe_connect *io, struct loadparm_context *lp_ctx)
+                                                                 struct dcerpc_pipe_connect *io)
 {
        struct composite_context *c;
        struct pipe_ncalrpc_state *s;
        struct composite_context *pipe_req;
+       const char *endpoint;
 
        /* composite context allocation and setup */
-       c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
+       c = composite_create(mem_ctx, io->conn->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct pipe_ncalrpc_state);
@@ -462,9 +508,16 @@ static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *me
        /* store input parameters in state structure */
        s->io  = *io;
 
+       endpoint = dcerpc_binding_get_string_option(io->binding, "endpoint");
+       if (endpoint == NULL) {
+               composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
+               return c;
+       }
+
        /* send pipe open request */
-       pipe_req = dcerpc_pipe_open_pipe_send(s->io.pipe->conn, lp_ncalrpc_dir(lp_ctx), 
-                                             s->io.binding->endpoint);
+       pipe_req = dcerpc_pipe_open_pipe_send(s->io.conn,
+                                             s->io.ncalrpc.dir,
+                                             endpoint);
        composite_continue(c, pipe_req, continue_pipe_open_ncalrpc, c);
        return c;
 }
@@ -511,12 +564,14 @@ static void continue_map_binding(struct composite_context *ctx)
                                                      struct composite_context);
        struct pipe_connect_state *s = talloc_get_type(c->private_data,
                                                       struct pipe_connect_state);
-       
+       const char *endpoint;
+
        c->status = dcerpc_epm_map_binding_recv(ctx);
        if (!composite_is_ok(c)) return;
 
-       DEBUG(2,("Mapped to DCERPC endpoint %s\n", s->binding->endpoint));
-       
+       endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
+       DEBUG(4,("Mapped to DCERPC endpoint %s\n", endpoint));
+
        continue_connect(c, s);
 }
 
@@ -534,18 +589,24 @@ static void continue_connect(struct composite_context *c, struct pipe_connect_st
        struct composite_context *ncacn_ip_tcp_req;
        struct composite_context *ncacn_unix_req;
        struct composite_context *ncalrpc_req;
+       enum dcerpc_transport_t transport;
+       uint32_t flags;
 
        /* dcerpc pipe connect input parameters */
-       pc.pipe         = s->pipe;
+       ZERO_STRUCT(pc);
+       pc.conn         = s->pipe->conn;
        pc.binding      = s->binding;
        pc.interface    = s->table;
        pc.creds        = s->credentials;
-       pc.resolve_ctx  = lp_resolve_context(s->lp_ctx);
+       pc.resolve_ctx  = lpcfg_resolve_context(s->lp_ctx);
+
+       transport = dcerpc_binding_get_transport(s->binding);
+       flags = dcerpc_binding_get_flags(s->binding);
 
        /* connect dcerpc pipe depending on required transport */
-       switch (s->binding->transport) {
+       switch (transport) {
        case NCACN_NP:
-               if (pc.binding->flags & DCERPC_SMB2) {
+               if (flags & DCERPC_SMB2) {
                        /* new varient of SMB a.k.a. SMB2 */
                        ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc, s->lp_ctx);
                        composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
@@ -570,7 +631,11 @@ static void continue_connect(struct composite_context *c, struct pipe_connect_st
                return;
 
        case NCALRPC:
-               ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc, s->lp_ctx);
+               pc.ncalrpc.dir = lpcfg_ncalrpc_dir(s->lp_ctx);
+               c->status = dcerpc_binding_set_string_option(s->binding, "ncalrpc_dir",
+                                                            pc.ncalrpc.dir);
+               if (!composite_is_ok(c)) return;
+               ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc);
                composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
                return;
 
@@ -676,9 +741,8 @@ static void continue_pipe_connect(struct composite_context *c, struct pipe_conne
 {
        struct composite_context *auth_bind_req;
 
-       s->pipe->binding = s->binding;
-       if (!talloc_reference(s->pipe, s->binding)) {
-               composite_error(c, NT_STATUS_NO_MEMORY);
+       s->pipe->binding = dcerpc_binding_dup(s->pipe, s->binding);
+       if (composite_nomem(s->pipe->binding, c)) {
                return;
        }
 
@@ -708,11 +772,17 @@ static void continue_pipe_auth(struct composite_context *ctx)
 /*
   handle timeouts of a dcerpc connect
 */
-static void dcerpc_connect_timeout_handler(struct event_context *ev, struct timed_event *te, 
-                                          struct timeval t, void *private)
+static void dcerpc_connect_timeout_handler(struct tevent_context *ev, struct tevent_timer *te, 
+                                          struct timeval t, void *private_data)
 {
-       struct composite_context *c = talloc_get_type(private, struct composite_context);
-       composite_error(c, NT_STATUS_IO_TIMEOUT);
+       struct composite_context *c = talloc_get_type_abort(private_data,
+                                                     struct composite_context);
+       struct pipe_connect_state *s = talloc_get_type_abort(c->private_data, struct pipe_connect_state);
+       if (!s->pipe->inhibit_timeout_processing) {
+               composite_error(c, NT_STATUS_IO_TIMEOUT);
+       } else {
+               s->pipe->timed_out = true;
+       }
 }
 
 /*
@@ -720,71 +790,70 @@ static void dcerpc_connect_timeout_handler(struct event_context *ev, struct time
   specified binding structure to determine the endpoint and options
 */
 _PUBLIC_ struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
-                                                    struct dcerpc_binding *binding,
+                                                    const struct dcerpc_binding *binding,
                                                     const struct ndr_interface_table *table,
                                                     struct cli_credentials *credentials,
-                                                    struct event_context *ev,
+                                                    struct tevent_context *ev,
                                                     struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
        struct pipe_connect_state *s;
-       struct event_context *new_ev = NULL;
+       enum dcerpc_transport_t transport;
+       const char *endpoint = NULL;
 
        /* composite context allocation and setup */
        c = composite_create(parent_ctx, ev);
        if (c == NULL) {
-               talloc_free(new_ev);
                return NULL;
        }
-       talloc_steal(c, new_ev);
 
        s = talloc_zero(c, struct pipe_connect_state);
        if (composite_nomem(s, c)) return c;
        c->private_data = s;
 
        /* initialise dcerpc pipe structure */
-       s->pipe = dcerpc_pipe_init(c, ev, lp_iconv_convenience(lp_ctx));
+       s->pipe = dcerpc_pipe_init(c, ev);
        if (composite_nomem(s->pipe, c)) return c;
 
        if (DEBUGLEVEL >= 10)
-               s->pipe->conn->packet_log_dir = lp_lockdir(lp_ctx);
+               s->pipe->conn->packet_log_dir = lpcfg_lock_directory(lp_ctx);
 
        /* store parameters in state structure */
-       s->binding      = binding;
+       s->binding      = dcerpc_binding_dup(s, binding);
+       if (composite_nomem(s->binding, c)) return c;
        s->table        = table;
        s->credentials  = credentials;
        s->lp_ctx       = lp_ctx;
 
-       event_add_timed(c->event_ctx, c,
-                       timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
-                       dcerpc_connect_timeout_handler, c);
-       
-       switch (s->binding->transport) {
-       case NCA_UNKNOWN: {
-               struct composite_context *binding_req;
-               binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
-                                                         s->pipe->conn->event_ctx,
-                                                         s->lp_ctx);
-               composite_continue(c, binding_req, continue_map_binding, c);
-               return c;
-               }
+       s->pipe->timed_out = false;
+       s->pipe->inhibit_timeout_processing = false;
+
+       tevent_add_timer(c->event_ctx, c,
+                        timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
+                        dcerpc_connect_timeout_handler, c);
 
+       transport = dcerpc_binding_get_transport(s->binding);
+
+       switch (transport) {
        case NCACN_NP:
        case NCACN_IP_TCP:
        case NCALRPC:
-               if (!s->binding->endpoint) {
-                       struct composite_context *binding_req;
-                       binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
-                                                                 s->pipe->conn->event_ctx,
-                                                                 s->lp_ctx);
-                       composite_continue(c, binding_req, continue_map_binding, c);
-                       return c;
-               }
-
+               endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
+               break;
        default:
                break;
        }
 
+       if (endpoint == NULL) {
+               struct composite_context *binding_req;
+
+               binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
+                                                         s->pipe->conn->event_ctx,
+                                                         s->lp_ctx);
+               composite_continue(c, binding_req, continue_map_binding, c);
+               return c;
+       }
+
        continue_connect(c, s);
        return c;
 }
@@ -817,10 +886,10 @@ _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC
 */
 _PUBLIC_ NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
                               struct dcerpc_pipe **pp,
-                              struct dcerpc_binding *binding,
+                              const struct dcerpc_binding *binding,
                               const struct ndr_interface_table *table,
                               struct cli_credentials *credentials,
-                              struct event_context *ev,
+                              struct tevent_context *ev,
                               struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
@@ -848,7 +917,7 @@ _PUBLIC_ struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_c
                                                   const char *binding,
                                                   const struct ndr_interface_table *table,
                                                   struct cli_credentials *credentials,
-                                                  struct event_context *ev, struct loadparm_context *lp_ctx)
+                                                  struct tevent_context *ev, struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
        struct pipe_conn_state *s;
@@ -935,7 +1004,7 @@ _PUBLIC_ NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx,
                             const char *binding,
                             const struct ndr_interface_table *table,
                             struct cli_credentials *credentials,
-                            struct event_context *ev,
+                            struct tevent_context *ev,
                             struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;