r23792: convert Samba4 to GPLv3
[kai/samba-autobuild/.git] / source4 / librpc / rpc / dcerpc_connect.c
index 6e0fc0c743db5e8de4df34c06fdb493a7eae7d88..443b8b458a4ab857621fa38f8428698f375b68a7 100644 (file)
@@ -10,7 +10,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 "smb.h"
 #include "libcli/composite/composite.h"
 #include "libcli/smb_composite/smb_composite.h"
+#include "lib/events/events.h"
 #include "libcli/smb2/smb2.h"
 #include "libcli/smb2/smb2_calls.h"
+#include "librpc/rpc/dcerpc.h"
 #include "auth/credentials/credentials.h"
 
 
-struct dcerpc_pipe_connect;
-
 struct pipe_np_smb_state {
        struct smb_composite_connect conn;
        struct smbcli_tree *tree;
@@ -57,6 +55,7 @@ static void continue_pipe_open_smb(struct composite_context *ctx)
        composite_done(c);
 }
 
+
 /*
   Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
 */
@@ -77,7 +76,7 @@ static void continue_smb_connect(struct composite_context *ctx)
        s->io.pipe_name = s->io.binding->endpoint;
 
        /* send named pipe open request */
-       open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
+       open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe, s->tree, s->io.pipe_name);
        if (composite_nomem(open_ctx, c)) return;
 
        composite_continue(c, open_ctx, continue_pipe_open_smb, c);
@@ -88,7 +87,7 @@ static void continue_smb_connect(struct composite_context *ctx)
   Initiate async open of a rpc connection to a rpc pipe on SMB using
   the binding structure to determine the endpoint and options
 */
-struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_ctx, 
+static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_ctx, 
                                                                struct dcerpc_pipe_connect *io)
 {
        struct composite_context *c;
@@ -97,15 +96,12 @@ struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_
        struct smb_composite_connect *conn;
 
        /* composite context allocation and setup */
-       c = talloc_zero(mem_ctx, struct composite_context);
+       c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct pipe_np_smb_state);
        if (composite_nomem(s, c)) return c;
-
-       c->state = COMPOSITE_STATE_IN_PROGRESS;
        c->private_data = s;
-       c->event_ctx = io->pipe->conn->event_ctx;
 
        s->io  = *io;
        conn   = &s->conn;
@@ -114,14 +110,11 @@ struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_
           remote rpc server */
        conn->in.dest_host              = s->io.binding->host;
        conn->in.port                   = 0;
-       conn->in.called_name            = strupper_talloc(mem_ctx, s->io.binding->host);
+       conn->in.called_name            = s->io.binding->target_hostname;
        conn->in.service                = "IPC$";
        conn->in.service_type           = NULL;
        conn->in.workgroup              = lp_workgroup();
 
-       /* verify if called_name has been allocated when uppercasing */
-       if (composite_nomem(conn->in.called_name, c)) return c;
-
        /*
         * provide proper credentials - user supplied, but allow a
         * fallback to anonymous if this is an schannel connection
@@ -147,7 +140,7 @@ struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_
 /*
   Receive result of a rpc connection to a rpc pipe on SMB
 */
-NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
+static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
 {
        NTSTATUS status = composite_wait(c);
 
@@ -156,18 +149,6 @@ NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
 }
 
 
-/*
-  Sync version of a rpc connection to a rpc pipe on SMB
-*/
-NTSTATUS dcerpc_pipe_connect_ncacn_np_smb(TALLOC_CTX *mem_ctx,
-                                         struct dcerpc_pipe_connect *io)
-{
-       struct composite_context *c;
-       c = dcerpc_pipe_connect_ncacn_np_smb_send(mem_ctx, io);
-       return dcerpc_pipe_connect_ncacn_np_smb_recv(c);
-}
-
-
 struct pipe_np_smb2_state {
        struct smb2_tree *tree;
        struct dcerpc_pipe_connect io;
@@ -209,7 +190,7 @@ static void continue_smb2_connect(struct composite_context *ctx)
        s->io.pipe_name = s->io.binding->endpoint;
 
        /* send named pipe open request */
-       open_req = dcerpc_pipe_open_smb2_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
+       open_req = dcerpc_pipe_open_smb2_send(s->io.pipe, s->tree, s->io.pipe_name);
        if (composite_nomem(open_req, c)) return;
 
        composite_continue(c, open_req, continue_pipe_open_smb2, c);
@@ -220,23 +201,20 @@ static void continue_smb2_connect(struct composite_context *ctx)
    Initiate async open of a rpc connection request on SMB2 using
    the binding structure to determine the endpoint and options
 */
-struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_CTX *mem_ctx,
-                                                                struct dcerpc_pipe_connect *io)
+static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_CTX *mem_ctx,
+                                                                       struct dcerpc_pipe_connect *io)
 {
        struct composite_context *c;
        struct pipe_np_smb2_state *s;
        struct composite_context *conn_req;
 
        /* composite context allocation and setup */
-       c = talloc_zero(mem_ctx, struct composite_context);
+       c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct pipe_np_smb2_state);
        if (composite_nomem(s, c)) return c;
-       
-       c->state = COMPOSITE_STATE_IN_PROGRESS;
        c->private_data = s;
-       c->event_ctx = io->pipe->conn->event_ctx;
 
        s->io = *io;
 
@@ -255,10 +233,7 @@ struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_CTX *mem
        /* send smb2 connect request */
        conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, "IPC$", s->io.creds,
                                     c->event_ctx);
-       if (composite_nomem(conn_req, c)) return c;
-
        composite_continue(c, conn_req, continue_smb2_connect, c);
-
        return c;
 }
 
@@ -266,7 +241,7 @@ struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_CTX *mem
 /*
   Receive result of a rpc connection to a rpc pipe on SMB2
 */
-NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
+static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
 {
        NTSTATUS status = composite_wait(c);
        
@@ -275,21 +250,10 @@ NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
 }
 
 
-/*
-  Sync version of a rpc connection to a rpc pipe on SMB2
-*/
-NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2(TALLOC_CTX *mem_ctx,
-                                          struct dcerpc_pipe_connect *io)
-{
-       struct composite_context *c;
-       c = dcerpc_pipe_connect_ncacn_np_smb2_send(mem_ctx, io);
-       return dcerpc_pipe_connect_ncacn_np_smb2_recv(c);
-}
-
-
 struct pipe_ip_tcp_state {
        struct dcerpc_pipe_connect io;
        const char *host;
+       const char *target_hostname;
        uint32_t port;
 };
 
@@ -314,33 +278,31 @@ static void continue_pipe_open_ncacn_ip_tcp(struct composite_context *ctx)
   Initiate async open of a rpc connection to a rpc pipe on TCP/IP using
   the binding structure to determine the endpoint and options
 */
-struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_ctx,
-                                                               struct dcerpc_pipe_connect *io)
+static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_ctx,
+                                                                      struct dcerpc_pipe_connect *io)
 {
        struct composite_context *c;
        struct pipe_ip_tcp_state *s;
        struct composite_context *pipe_req;
 
        /* composite context allocation and setup */
-       c = talloc_zero(mem_ctx, struct composite_context);
+       c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct pipe_ip_tcp_state);
        if (composite_nomem(s, c)) return c;
-       
-       c->state = COMPOSITE_STATE_IN_PROGRESS;
        c->private_data = s;
-       c->event_ctx = io->pipe->conn->event_ctx;
 
        /* store input parameters in state structure */
-       s->io    = *io;
-       s->host  = talloc_strdup(c, io->binding->host);
-       s->port  = atoi(io->binding->endpoint);   /* port number is a binding endpoint here */
+       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);   
 
        /* send pipe open request on tcp/ip */
-       pipe_req = dcerpc_pipe_open_tcp_send(s->io.pipe->conn, s->host, s->port);
-       if (composite_nomem(pipe_req, c)) return c;
-
+       pipe_req = dcerpc_pipe_open_tcp_send(s->io.pipe->conn, s->host, s->target_hostname, 
+                                            s->port);
        composite_continue(c, pipe_req, continue_pipe_open_ncacn_ip_tcp, c);
        return c;
 }
@@ -349,7 +311,7 @@ struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_
 /*
   Receive result of a rpc connection to a rpc pipe on TCP/IP
 */
-NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
+static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
 {
        NTSTATUS status = composite_wait(c);
        
@@ -358,18 +320,6 @@ NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
 }
 
 
-/*
-  Sync version of rpc connection to a rpc pipe on TCP/IP
-*/
-NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(TALLOC_CTX *mem_ctx,
-                                         struct dcerpc_pipe_connect *io)
-{
-       struct composite_context *c;
-       c = dcerpc_pipe_connect_ncacn_ip_tcp_send(mem_ctx, io);
-       return dcerpc_pipe_connect_ncacn_ip_tcp_recv(c);
-}
-
-
 struct pipe_unix_state {
        struct dcerpc_pipe_connect io;
        const char *path;
@@ -396,23 +346,20 @@ static void continue_pipe_open_ncacn_unix_stream(struct composite_context *ctx)
   Initiate async open of a rpc connection to a rpc pipe on unix socket using
   the binding structure to determine the endpoint and options
 */
-struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX *mem_ctx,
-                                                                    struct dcerpc_pipe_connect *io)
+static struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX *mem_ctx,
+                                                                           struct dcerpc_pipe_connect *io)
 {
        struct composite_context *c;
        struct pipe_unix_state *s;
        struct composite_context *pipe_req;
 
        /* composite context allocation and setup */
-       c = talloc_zero(mem_ctx, struct composite_context);
+       c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct pipe_unix_state);
        if (composite_nomem(s, c)) return c;
-       
-       c->state = COMPOSITE_STATE_IN_PROGRESS;
        c->private_data = s;
-       c->event_ctx = io->pipe->conn->event_ctx;
 
        /* prepare pipe open parameters and store them in state structure
           also, verify whether biding endpoint is not null */
@@ -425,11 +372,10 @@ struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX
        }
 
        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);
-       if (composite_nomem(pipe_req, c)) return c;
-
        composite_continue(c, pipe_req, continue_pipe_open_ncacn_unix_stream, c);
        return c;
 }
@@ -438,7 +384,7 @@ struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX
 /*
   Receive result of a rpc connection to a pipe on unix socket
 */
-NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
+static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
 {
        NTSTATUS status = composite_wait(c);
 
@@ -447,22 +393,11 @@ NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
 }
 
 
-/*
-  Sync version of a rpc connection to a rpc pipe on unix socket
-*/
-NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(TALLOC_CTX *mem_ctx,
-                                              struct dcerpc_pipe_connect *io)
-{
-       struct composite_context *c;
-       c = dcerpc_pipe_connect_ncacn_unix_stream_send(mem_ctx, io);
-       return dcerpc_pipe_connect_ncacn_unix_stream_recv(c);   
-}
-
-
 struct pipe_ncalrpc_state {
        struct dcerpc_pipe_connect io;
 };
 
+static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c);
 
 /*
   Stage 2 of ncalrpc: rpc pipe opened (or not)
@@ -484,31 +419,26 @@ static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
    Initiate async open of a rpc connection request on NCALRPC using
    the binding structure to determine the endpoint and options
 */
-struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
-                                                          struct dcerpc_pipe_connect *io)
+static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
+                                                                 struct dcerpc_pipe_connect *io)
 {
        struct composite_context *c;
        struct pipe_ncalrpc_state *s;
        struct composite_context *pipe_req;
 
        /* composite context allocation and setup */
-       c = talloc_zero(mem_ctx, struct composite_context);
+       c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct pipe_ncalrpc_state);
        if (composite_nomem(s, c)) return c;
-       
-       c->state = COMPOSITE_STATE_IN_PROGRESS;
        c->private_data = s;
-       c->event_ctx = io->pipe->conn->event_ctx;
        
        /* store input parameters in state structure */
        s->io  = *io;
 
        /* send pipe open request */
        pipe_req = dcerpc_pipe_open_pipe_send(s->io.pipe->conn, s->io.binding->endpoint);
-       if (composite_nomem(pipe_req, c)) return c;
-       
        composite_continue(c, pipe_req, continue_pipe_open_ncalrpc, c);
        return c;
 }
@@ -517,7 +447,7 @@ struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
 /*
   Receive result of a rpc connection to a rpc pipe on NCALRPC
 */
-NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
+static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
 {
        NTSTATUS status = composite_wait(c);
        
@@ -526,17 +456,6 @@ NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
 }
 
 
-/*
-  Sync version of a rpc connection to a rpc pipe on NCALRPC
-*/
-NTSTATUS dcerpc_pipe_connect_ncalrpc(TALLOC_CTX *mem_ctx,
-                                    struct dcerpc_pipe_connect *io)
-{
-       struct composite_context *c = dcerpc_pipe_connect_ncalrpc_send(mem_ctx, io);
-       return dcerpc_pipe_connect_ncalrpc_recv(c);
-}
-
-
 struct pipe_connect_state {
        struct dcerpc_pipe *pipe;
        struct dcerpc_binding *binding;
@@ -594,23 +513,19 @@ static void continue_connect(struct composite_context *c, struct pipe_connect_st
        pc.binding      = s->binding;
        pc.interface    = s->table;
        pc.creds        = s->credentials;
-       
+
        /* connect dcerpc pipe depending on required transport */
        switch (s->binding->transport) {
        case NCACN_NP:
                if (pc.binding->flags & DCERPC_SMB2) {
                        /* new varient of SMB a.k.a. SMB2 */
                        ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc);
-                       if (composite_nomem(ncacn_np_smb2_req, c)) return;
-
                        composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
                        return;
 
                } else {
                        /* good old ordinary SMB */
                        ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc);
-                       if (composite_nomem(ncacn_np_smb_req, c)) return;
-                       
                        composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
                        return;
                }
@@ -618,22 +533,16 @@ static void continue_connect(struct composite_context *c, struct pipe_connect_st
 
        case NCACN_IP_TCP:
                ncacn_ip_tcp_req = dcerpc_pipe_connect_ncacn_ip_tcp_send(c, &pc);
-               if (composite_nomem(ncacn_ip_tcp_req, c)) return;
-               
                composite_continue(c, ncacn_ip_tcp_req, continue_pipe_connect_ncacn_ip_tcp, c);
                return;
 
        case NCACN_UNIX_STREAM:
                ncacn_unix_req = dcerpc_pipe_connect_ncacn_unix_stream_send(c, &pc);
-               if (composite_nomem(ncacn_unix_req, c)) return;
-               
                composite_continue(c, ncacn_unix_req, continue_pipe_connect_ncacn_unix, c);
                return;
 
        case NCALRPC:
                ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc);
-               if (composite_nomem(ncalrpc_req, c)) return;
-               
                composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
                return;
 
@@ -747,8 +656,6 @@ static void continue_pipe_connect(struct composite_context *c, struct pipe_conne
 
        auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
                                              s->credentials);
-       if (composite_nomem(auth_bind_req, c)) return;
-       
        composite_continue(c, auth_bind_req, continue_pipe_auth, c);
 }
 
@@ -761,20 +668,30 @@ static void continue_pipe_auth(struct composite_context *ctx)
 {
        struct composite_context *c = talloc_get_type(ctx->async.private_data,
                                                      struct composite_context);
+       struct pipe_connect_state *s = talloc_get_type(c->private_data, struct pipe_connect_state);
 
-       c->status = dcerpc_pipe_auth_recv(ctx);
+       c->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe);
        if (!composite_is_ok(c)) return;
 
        composite_done(c);
 }
 
 
+/*
+  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)
+{
+       struct composite_context *c = talloc_get_type(private, struct composite_context);
+       composite_error(c, NT_STATUS_IO_TIMEOUT);
+}
+
 /*
   start a request to open a rpc connection to a rpc pipe, using
   specified binding structure to determine the endpoint and options
 */
 struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
-                                                    struct dcerpc_pipe **pp,
                                                     struct dcerpc_binding *binding,
                                                     const struct dcerpc_interface_table *table,
                                                     struct cli_credentials *credentials,
@@ -782,39 +699,45 @@ struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
 {
        struct composite_context *c;
        struct pipe_connect_state *s;
+       struct event_context *new_ev = NULL;
 
-       struct composite_context *binding_req;
+       if (ev == NULL) {
+               new_ev = event_context_init(parent_ctx);
+               if (new_ev == NULL) return NULL;
+               ev = new_ev;
+       }
 
        /* composite context allocation and setup */
-       c = talloc_zero(parent_ctx, struct composite_context);
-       if (c == NULL) return NULL;
+       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->state = COMPOSITE_STATE_IN_PROGRESS;
        c->private_data = s;
 
-       (*pp) = NULL;
-
        /* initialise dcerpc pipe structure */
        s->pipe = dcerpc_pipe_init(c, ev);
        if (composite_nomem(s->pipe, c)) return c;
 
-       /* get event context from initialised dcerpc pipe */
-       c->event_ctx = s->pipe->conn->event_ctx;
-       (*pp) = s->pipe;
-
        /* store parameters in state structure */
        s->binding      = binding;
        s->table        = table;
        s->credentials  = credentials;
+
+       event_add_timed(c->event_ctx, c,
+                       timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
+                       dcerpc_connect_timeout_handler, c);
        
        switch (s->binding->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);
                        composite_continue(c, binding_req, continue_map_binding, c);
@@ -841,12 +764,11 @@ NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem
        
        status = composite_wait(c);
        
-       if (NT_STATUS_IS_OK(status) && p) {
+       if (NT_STATUS_IS_OK(status)) {
                s = talloc_get_type(c->private_data, struct pipe_connect_state);
                talloc_steal(mem_ctx, s->pipe);
                *p = s->pipe;
        }
-       
        talloc_free(c);
        return status;
 }
@@ -865,7 +787,7 @@ NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
 {
        struct composite_context *c;
        
-       c = dcerpc_pipe_connect_b_send(parent_ctx, pp, binding, table,
+       c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
                                       credentials, ev);
        return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
 }
@@ -885,33 +807,40 @@ static void continue_pipe_connect_b(struct composite_context *ctx);
   The string is to be parsed to a binding structure first.
 */
 struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
-                                                  struct dcerpc_pipe **pp,
                                                   const char *binding,
                                                   const struct dcerpc_interface_table *table,
                                                   struct cli_credentials *credentials,
                                                   struct event_context *ev)
 {
-       NTSTATUS status;
        struct composite_context *c;
        struct pipe_conn_state *s;
        struct dcerpc_binding *b;
        struct composite_context *pipe_conn_req;
+       struct event_context *new_ev = NULL;
+
+       if (ev == NULL) {
+               new_ev = event_context_init(parent_ctx);
+               if (new_ev == NULL) return NULL;
+               ev = new_ev;
+       }
 
        /* composite context allocation and setup */
-       c = talloc_zero(parent_ctx, struct composite_context);
-       if (c == NULL) return NULL;
+       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_conn_state);
        if (composite_nomem(s, c)) return c;
-
-       c->state = COMPOSITE_STATE_IN_PROGRESS;
        c->private_data = s;
 
        /* parse binding string to the structure */
-       status = dcerpc_parse_binding(c, binding, &b);
-       if (!NT_STATUS_IS_OK(status)) {
+       c->status = dcerpc_parse_binding(c, binding, &b);
+       if (!NT_STATUS_IS_OK(c->status)) {
                DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
-               composite_error(c, status);
+               composite_error(c, c->status);
                return c;
        }
 
@@ -921,14 +850,8 @@ struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
           start connecting to a rpc pipe after binding structure
           is established
         */
-       pipe_conn_req = dcerpc_pipe_connect_b_send(c, &s->pipe, b, table,
+       pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
                                                   credentials, ev);
-
-       /* event context for created dcerpc_pipe would be useful... */
-       c->event_ctx = s->pipe->conn->event_ctx;
-
-       if (composite_nomem(pipe_conn_req, c)) return c;
-
        composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
        return c;
 }
@@ -946,6 +869,7 @@ static void continue_pipe_connect_b(struct composite_context *ctx)
                                                    struct pipe_conn_state);
 
        c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
+       talloc_steal(s, s->pipe);
        if (!composite_is_ok(c)) return;
 
        composite_done(c);
@@ -964,11 +888,10 @@ NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
        struct pipe_conn_state *s;
 
        status = composite_wait(c);
-       if (NT_STATUS_IS_OK(status) && pp) {
+       if (NT_STATUS_IS_OK(status)) {
                s = talloc_get_type(c->private_data, struct pipe_conn_state);
                *pp = talloc_steal(mem_ctx, s->pipe);
        }
-
        talloc_free(c);
        return status;
 }
@@ -986,7 +909,8 @@ NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx,
                             struct event_context *ev)
 {
        struct composite_context *c;
-       c = dcerpc_pipe_connect_send(parent_ctx, pp, binding, table,
+       c = dcerpc_pipe_connect_send(parent_ctx, binding, 
+                                    table,
                                     credentials, ev);
        return dcerpc_pipe_connect_recv(c, parent_ctx, pp);
 }
@@ -1020,18 +944,11 @@ struct composite_context* dcerpc_secondary_connection_send(struct dcerpc_pipe *p
        struct composite_context *pipe_ncalrpc_req;
        
        /* composite context allocation and setup */
-       c = talloc_zero(p, struct composite_context);
+       c = composite_create(p, p->conn->event_ctx);
        if (c == NULL) return NULL;
 
-       c->event_ctx = p->conn->event_ctx;
-
        s = talloc_zero(c, struct sec_conn_state);
-       if (s == NULL) {
-               composite_error(c, NT_STATUS_NO_MEMORY);
-               return c;
-       }
-
-       c->state = COMPOSITE_STATE_IN_PROGRESS;
+       if (composite_nomem(s, c)) return c;
        c->private_data = s;
 
        s->pipe     = p;
@@ -1051,27 +968,22 @@ struct composite_context* dcerpc_secondary_connection_send(struct dcerpc_pipe *p
                        return c;
                }
 
-               pipe_smb_req = dcerpc_pipe_open_smb_send(s->pipe2->conn, s->tree,
+               pipe_smb_req = dcerpc_pipe_open_smb_send(s->pipe2, s->tree,
                                                         s->binding->endpoint);
-               if (composite_nomem(pipe_smb_req, c)) return c;
-
                composite_continue(c, pipe_smb_req, continue_open_smb, c);
                return c;
 
        case NCACN_IP_TCP:
                pipe_tcp_req = dcerpc_pipe_open_tcp_send(s->pipe2->conn,
                                                         s->binding->host,
+                                                        s->binding->target_hostname,
                                                         atoi(s->binding->endpoint));
-               if (composite_nomem(pipe_tcp_req, c)) return c;
-
                composite_continue(c, pipe_tcp_req, continue_open_tcp, c);
                return c;
 
        case NCALRPC:
                pipe_ncalrpc_req = dcerpc_pipe_open_pipe_send(s->pipe2->conn,
                                                              s->binding->endpoint);
-               if (composite_nomem(pipe_ncalrpc_req, c)) return c;
-
                composite_continue(c, pipe_ncalrpc_req, continue_open_pipe, c);
                return c;