r23792: convert Samba4 to GPLv3
[kai/samba-autobuild/.git] / source4 / librpc / rpc / dcerpc_smb.c
index fc71a47cf4012758fb0aa6cabafc75add3c7610d..6b43de3358ace0020b7c7bfd0ceec5398bcaa338 100644 (file)
@@ -8,7 +8,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 "libcli/raw/libcliraw.h"
+#include "libcli/composite/composite.h"
+#include "librpc/rpc/dcerpc.h"
 
 /* transport private information used by SMB pipe transport */
 struct smb_private {
-       uint16 fnum;
-       struct cli_tree *tree;
+       uint16_t fnum;
+       struct smbcli_tree *tree;
+       const char *server_name;
+       bool dead;
 };
 
-static struct cli_request *dcerpc_raw_send(struct dcerpc_pipe *p, DATA_BLOB *blob)
-{
-       struct smb_private *smb = p->transport.private;
-       struct smb_trans2 trans;
-       uint16 setup[2];
-       struct cli_request *req;
-       TALLOC_CTX *mem_ctx;
 
-       mem_ctx = talloc_init("dcerpc_raw_send");
-       if (!mem_ctx) return NULL;
+/*
+  tell the dcerpc layer that the transport is dead
+*/
+static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
+{
+       struct smb_private *smb = c->transport.private_data;
 
-       trans.in.data = *blob;
-       trans.in.params = data_blob(NULL, 0);
-       
-       setup[0] = TRANSACT_DCERPCCMD;
-       setup[1] = smb->fnum;
+       if (smb->dead) {
+               return;
+       }
 
-       trans.in.max_param = 0;
-       trans.in.max_data = 0x8000;
-       trans.in.max_setup = 0;
-       trans.in.setup_count = 2;
-       trans.in.flags = 0;
-       trans.in.timeout = 0;
-       trans.in.setup = setup;
-       trans.in.trans_name = "\\PIPE\\";
+       smb->dead = true;
 
-       req = smb_raw_trans_send(smb->tree, &trans);
+       if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
+               status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
+       }
 
-       talloc_destroy(mem_ctx);
+       if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
+               status = NT_STATUS_END_OF_FILE;
+       }
 
-       return req;
+       if (c->transport.recv_data) {
+               c->transport.recv_data(c, NULL, status);
+       }
 }
 
 
-static NTSTATUS dcerpc_raw_recv(struct dcerpc_pipe *p, 
-                               struct cli_request *req,
-                               TALLOC_CTX *mem_ctx,
-                               DATA_BLOB *blob)
+/* 
+   this holds the state of an in-flight call
+*/
+struct smb_read_state {
+       struct dcerpc_connection *c;
+       struct smbcli_request *req;
+       size_t received;
+       DATA_BLOB data;
+       union smb_read *io;
+};
+
+/*
+  called when a read request has completed
+*/
+static void smb_read_callback(struct smbcli_request *req)
 {
-       struct smb_private *smb = p->transport.private;
-       struct smb_trans2 trans;
+       struct smb_private *smb;
+       struct smb_read_state *state;
+       union smb_read *io;
+       uint16_t frag_length;
        NTSTATUS status;
-       uint16 frag_length;
-       DATA_BLOB payload;
-
-       status = smb_raw_trans_recv(req, mem_ctx, &trans);
-       /* STATUS_BUFFER_OVERFLOW means that there is more data
-          available via SMBreadX */
-       if (!NT_STATUS_IS_OK(status) && 
-           !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
-               return status;
+
+       state = talloc_get_type(req->async.private, struct smb_read_state);
+       smb = talloc_get_type(state->c->transport.private_data, struct smb_private);
+       io = state->io;
+
+       status = smb_raw_read_recv(state->req, io);
+       if (NT_STATUS_IS_ERR(status)) {
+               pipe_dead(state->c, status);
+               talloc_free(state);
+               return;
        }
 
-       payload = trans.out.data;
+       state->received += io->readx.out.nread;
 
-       if (trans.out.data.length < 16 || 
-           !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
-               goto done;
+       if (state->received < 16) {
+               DEBUG(0,("dcerpc_smb: short packet (length %d) in read callback!\n",
+                        (int)state->received));
+               pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
+               talloc_free(state);
+               return;
        }
 
-       /* we might have recieved a partial fragment, in which case we
-          need to pull the rest of it */
-       frag_length = dcerpc_get_frag_length(&payload);
-       if (frag_length <= payload.length) {
-               goto done;
+       frag_length = dcerpc_get_frag_length(&state->data);
+
+       if (frag_length <= state->received) {
+               DATA_BLOB data = state->data;
+               struct dcerpc_connection *c = state->c;
+               data.length = state->received;
+               talloc_steal(state->c, data.data);
+               talloc_free(state);
+               c->transport.recv_data(c, &data, NT_STATUS_OK);
+               return;
        }
 
-       /* make sure the payload can hold the whole fragment */
-       payload.data = talloc_realloc(mem_ctx, payload.data, frag_length);
-       if (!payload.data) {
-               return NT_STATUS_NO_MEMORY;
+       /* initiate another read request, as we only got part of a fragment */
+       state->data.data = talloc_realloc(state, state->data.data, uint8_t, frag_length);
+
+       io->readx.in.mincnt = MIN(state->c->srv_max_xmit_frag, 
+                                 frag_length - state->received);
+       io->readx.in.maxcnt = io->readx.in.mincnt;
+       io->readx.out.data = state->data.data + state->received;
+
+       state->req = smb_raw_read_send(smb->tree, io);
+       if (state->req == NULL) {
+               pipe_dead(state->c, NT_STATUS_NO_MEMORY);
+               talloc_free(state);
+               return;
        }
 
-       /* the rest of the data is available via SMBreadX */
-       while (frag_length > payload.length) {
-               uint32 n;
-               union smb_read io;
+       state->req->async.fn = smb_read_callback;
+       state->req->async.private = state;
+}
 
-               n = frag_length - payload.length;
-               if (n > 0xFF00) {
-                       n = 0xFF00;
-               }
+/*
+  trigger a read request from the server, possibly with some initial
+  data in the read buffer
+*/
+static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
+{
+       struct smb_private *smb = c->transport.private_data;
+       union smb_read *io;
+       struct smb_read_state *state;
+       struct smbcli_request *req;
 
-               io.generic.level = RAW_READ_READX;
-               io.readx.in.fnum = smb->fnum;
-               io.readx.in.mincnt = n;
-               io.readx.in.maxcnt = n;
-               io.readx.in.offset = 0;
-               io.readx.in.remaining = 0;
-               io.readx.out.data = payload.data + payload.length;
-               status = smb_raw_read(smb->tree, &io);
-               if (!NT_STATUS_IS_OK(status) &&
-                   !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
-                       break;
-               }
-               
-               n = io.readx.out.nread;
-               if (n == 0) {
-                       status = NT_STATUS_UNSUCCESSFUL;
-                       break;
-               }
-               
-               payload.length += n;
+       state = talloc(smb, struct smb_read_state);
+       if (state == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
-               /* if the SMBreadX returns NT_STATUS_OK then there
-                  isn't any more data to be read */
-               if (NT_STATUS_IS_OK(status)) {
-                       break;
+       state->c = c;
+       if (blob == NULL) {
+               state->received = 0;
+               state->data = data_blob_talloc(state, NULL, 0x2000);
+       } else {
+               uint32_t frag_length = blob->length>=16?
+                       dcerpc_get_frag_length(blob):0x2000;
+               state->received = blob->length;
+               state->data = data_blob_talloc(state, NULL, frag_length);
+               if (!state->data.data) {
+                       talloc_free(state);
+                       return NT_STATUS_NO_MEMORY;
                }
+               memcpy(state->data.data, blob->data, blob->length);
        }
 
-done:
-       if (blob) {
-               *blob = payload;
+       state->io = talloc(state, union smb_read);
+
+       io = state->io;
+       io->generic.level = RAW_READ_READX;
+       io->readx.in.file.fnum = smb->fnum;
+       io->readx.in.mincnt = state->data.length - state->received;
+       io->readx.in.maxcnt = io->readx.in.mincnt;
+       io->readx.in.offset = 0;
+       io->readx.in.remaining = 0;
+       io->readx.in.read_for_execute = False;
+       io->readx.out.data = state->data.data + state->received;
+       req = smb_raw_read_send(smb->tree, io);
+       if (req == NULL) {
+               return NT_STATUS_NO_MEMORY;
        }
 
-       return status;
+       req->async.fn = smb_read_callback;
+       req->async.private = state;
+
+       state->req = req;
+
+       return NT_STATUS_OK;
 }
 
-static NTSTATUS smb_full_request(struct dcerpc_pipe *p, 
-                                TALLOC_CTX *mem_ctx,
-                                DATA_BLOB *request_blob,
-                                DATA_BLOB *reply_blob)
+
+/*
+  trigger a read request from the server
+*/
+static NTSTATUS send_read_request(struct dcerpc_connection *c)
 {
-       struct cli_request *req;
-       req = dcerpc_raw_send(p, request_blob);
-       return dcerpc_raw_recv(p, req, mem_ctx, reply_blob);
+       struct smb_private *smb = c->transport.private_data;
+
+       if (smb->dead) {
+               return NT_STATUS_CONNECTION_DISCONNECTED;
+       }
+
+       return send_read_request_continue(c, NULL);
 }
-             
 
 /* 
-   retrieve a secondary pdu from a pipe 
+   this holds the state of an in-flight trans call
 */
-NTSTATUS smb_secondary_request(struct dcerpc_pipe *p, 
-                              TALLOC_CTX *mem_ctx,
-                              DATA_BLOB *blob)
+struct smb_trans_state {
+       struct dcerpc_connection *c;
+       struct smbcli_request *req;
+       struct smb_trans2 *trans;
+};
+
+/*
+  called when a trans request has completed
+*/
+static void smb_trans_callback(struct smbcli_request *req)
 {
-       struct smb_private *smb = p->transport.private;
-       union smb_read io;
-       uint32 n = 0x2000;
-       uint32 frag_length;
+       struct smb_trans_state *state = req->async.private;
+       struct dcerpc_connection *c = state->c;
        NTSTATUS status;
 
-       *blob = data_blob_talloc(mem_ctx, NULL, n);
-       if (!blob->data) {
-               return NT_STATUS_NO_MEMORY;
+       status = smb_raw_trans_recv(req, state, state->trans);
+
+       if (NT_STATUS_IS_ERR(status)) {
+               pipe_dead(c, status);
+               return;
        }
 
-       io.generic.level = RAW_READ_READX;
-       io.readx.in.fnum = smb->fnum;
-       io.readx.in.mincnt = n;
-       io.readx.in.maxcnt = n;
-       io.readx.in.offset = 0;
-       io.readx.in.remaining = 0;
-       io.readx.out.data = blob->data;
-
-       status = smb_raw_read(smb->tree, &io);
-       if (!NT_STATUS_IS_OK(status) &&
-           !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
-               return status;
+       if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
+               DATA_BLOB data = state->trans->out.data;
+               talloc_steal(c, data.data);
+               talloc_free(state);
+               c->transport.recv_data(c, &data, NT_STATUS_OK);
+               return;
        }
 
-       blob->length = io.readx.out.nread;
+       /* there is more to receive - setup a readx */
+       send_read_request_continue(c, &state->trans->out.data);
+       talloc_free(state);
+}
 
-       if (blob->length < 16) {
-               return status;
-       }
+/*
+  send a SMBtrans style request
+*/
+static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
+{
+        struct smb_private *smb = c->transport.private_data;
+        struct smb_trans2 *trans;
+        uint16_t setup[2];
+       struct smb_trans_state *state;
 
-       frag_length = dcerpc_get_frag_length(blob);
-       if (frag_length <= blob->length) {
-               return status;
+       state = talloc(smb, struct smb_trans_state);
+       if (state == NULL) {
+               return NT_STATUS_NO_MEMORY;
        }
 
-       blob->data = talloc_realloc(mem_ctx, blob->data, frag_length);
-       if (!blob->data) {
+       state->c = c;
+       state->trans = talloc(state, struct smb_trans2);
+       trans = state->trans;
+
+        trans->in.data = *blob;
+        trans->in.params = data_blob(NULL, 0);
+        
+        setup[0] = TRANSACT_DCERPCCMD;
+        setup[1] = smb->fnum;
+
+        trans->in.max_param = 0;
+        trans->in.max_data = smb_raw_max_trans_data(smb->tree, 0);
+        trans->in.max_setup = 0;
+        trans->in.setup_count = 2;
+        trans->in.flags = 0;
+        trans->in.timeout = 0;
+        trans->in.setup = setup;
+        trans->in.trans_name = "\\PIPE\\";
+
+        state->req = smb_raw_trans_send(smb->tree, trans);
+       if (state->req == NULL) {
+               talloc_free(state);
                return NT_STATUS_NO_MEMORY;
        }
 
-       while (frag_length > blob->length &&
-              NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
+       state->req->async.fn = smb_trans_callback;
+       state->req->async.private = state;
 
-               n = frag_length - blob->length;
-               if (n > 0xFF00) {
-                       n = 0xFF00;
-               }
+       talloc_steal(state, state->req);
 
-               io.readx.in.mincnt = n;
-               io.readx.in.maxcnt = n;
-               io.readx.out.data = blob->data + blob->length;
-               status = smb_raw_read(smb->tree, &io);
+        return NT_STATUS_OK;
+}
 
-               if (!NT_STATUS_IS_OK(status) &&
-                   !NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
-                       return status;
-               }
-               
-               n = io.readx.out.nread;
-               blob->length += n;
+/*
+  called when a write request has completed
+*/
+static void smb_write_callback(struct smbcli_request *req)
+{
+       struct dcerpc_connection *c = req->async.private;
+
+       if (!NT_STATUS_IS_OK(req->status)) {
+               DEBUG(0,("dcerpc_smb: write callback error\n"));
+               pipe_dead(c, req->status);
        }
 
-       return status;
+       smbcli_request_destroy(req);
 }
 
-
 /* 
-   send an initial pdu in a multi-pdu sequence
+   send a packet to the server
 */
-static NTSTATUS smb_initial_request(struct dcerpc_pipe *p, 
-                                   TALLOC_CTX *mem_ctx,
-                                   DATA_BLOB *blob)
+static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, BOOL trigger_read)
 {
-       struct smb_private *smb = p->transport.private;
+       struct smb_private *smb = c->transport.private_data;
        union smb_write io;
-       NTSTATUS status;
+       struct smbcli_request *req;
+
+       if (smb->dead) {
+               return NT_STATUS_CONNECTION_DISCONNECTED;
+       }
+
+       if (trigger_read) {
+               return smb_send_trans_request(c, blob);
+       }
 
        io.generic.level = RAW_WRITE_WRITEX;
-       io.writex.in.fnum = smb->fnum;
+       io.writex.in.file.fnum = smb->fnum;
        io.writex.in.offset = 0;
        io.writex.in.wmode = PIPE_START_MESSAGE;
        io.writex.in.remaining = blob->length;
        io.writex.in.count = blob->length;
        io.writex.in.data = blob->data;
 
-       status = smb_raw_write(smb->tree, &io);
-       if (NT_STATUS_IS_OK(status)) {
-               return status;
+       /* we must not timeout at the smb level for rpc requests, as otherwise
+          signing/sealing can be messed up */
+       smb->tree->session->transport->options.request_timeout = 0;
+
+       req = smb_raw_write_send(smb->tree, &io);
+       if (req == NULL) {
+               return NT_STATUS_NO_MEMORY;
        }
 
-       /* make sure it accepted it all */
-       if (io.writex.out.nwritten != blob->length) {
-               return NT_STATUS_UNSUCCESSFUL;
+       req->async.fn = smb_write_callback;
+       req->async.private = c;
+
+       if (trigger_read) {
+               send_read_request(c);
        }
 
-       return status;
+       return NT_STATUS_OK;
 }
 
-
 /* 
    shutdown SMB pipe connection
 */
-static NTSTATUS smb_shutdown_pipe(struct dcerpc_pipe *p)
+static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c, NTSTATUS status)
 {
-       struct smb_private *smb = p->transport.private;
-       union smb_close c;
+       struct smb_private *smb = c->transport.private_data;
+       union smb_close io;
+       struct smbcli_request *req;
 
        /* maybe we're still starting up */
-       if (!smb) return NT_STATUS_OK;
+       if (!smb) return status;
+
+       io.close.level = RAW_CLOSE_CLOSE;
+       io.close.in.file.fnum = smb->fnum;
+       io.close.in.write_time = 0;
+       req = smb_raw_close_send(smb->tree, &io);
+       if (req != NULL) {
+               /* we don't care if this fails, so just free it if it succeeds */
+               req->async.fn = (void (*)(struct smbcli_request *))talloc_free;
+       }
 
-       c.close.level = RAW_CLOSE_CLOSE;
-       c.close.in.fnum = smb->fnum;
-       c.close.in.write_time = 0;
-       smb_raw_close(smb->tree, &c);
-       cli_tree_close(smb->tree);
+       talloc_free(smb);
 
-       return NT_STATUS_OK;
+       return status;
 }
 
 /*
-  return SMB server name
+  return SMB server name (called name)
 */
-static const char *smb_peer_name(struct dcerpc_pipe *p)
+static const char *smb_peer_name(struct dcerpc_connection *c)
 {
-       struct smb_private *smb = p->transport.private;
-       return smb->tree->session->transport->called.name;
+       struct smb_private *smb = c->transport.private_data;
+       return smb->server_name;
 }
 
+/*
+  return remote name we make the actual connection (good for kerberos) 
+*/
+static const char *smb_target_hostname(struct dcerpc_connection *c)
+{
+       struct smb_private *smb = talloc_get_type(c->transport.private_data, struct smb_private);
+       return smb->tree->session->transport->socket->hostname;
+}
 
-/* 
-   open a rpc connection to a named pipe 
+/*
+  fetch the user session key 
 */
-NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe **p, 
-                             struct cli_tree *tree,
-                             const char *pipe_name)
+static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
 {
-       struct smb_private *smb;
-        NTSTATUS status;
-       char *name = NULL;
-       union smb_open io;
-       TALLOC_CTX *mem_ctx;
+       struct smb_private *smb = c->transport.private_data;
 
-       asprintf(&name, "\\%s", pipe_name);
-       if (!name) {
-               return NT_STATUS_NO_MEMORY;
+       if (smb->tree->session->user_session_key.data) {
+               *session_key = smb->tree->session->user_session_key;
+               return NT_STATUS_OK;
+       }
+       return NT_STATUS_NO_USER_SESSION_KEY;
+}
+
+struct pipe_open_smb_state {
+       union smb_open *open;
+       struct dcerpc_connection *c;
+       struct smbcli_tree *tree;
+       struct composite_context *ctx;
+};
+
+static void pipe_open_recv(struct smbcli_request *req);
+
+struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_pipe *p, 
+                                                   struct smbcli_tree *tree,
+                                                   const char *pipe_name)
+{
+       struct composite_context *ctx;
+       struct pipe_open_smb_state *state;
+       struct smbcli_request *req;
+       struct dcerpc_connection *c = p->conn;
+
+       /* if we don't have a binding on this pipe yet, then create one */
+       if (p->binding == NULL) {
+               NTSTATUS status;
+               char *s = talloc_asprintf(p, "ncacn_np:%s", tree->session->transport->socket->hostname);
+               if (s == NULL) return NULL;
+               status = dcerpc_parse_binding(p, s, &p->binding);
+               talloc_free(s);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return NULL;
+               }
        }
 
-       io.ntcreatex.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.flags = 0;
-       io.ntcreatex.in.root_fid = 0;
-       io.ntcreatex.in.access_mask = 
-               STD_RIGHT_READ_CONTROL_ACCESS | 
-               SA_RIGHT_FILE_WRITE_ATTRIBUTES | 
-               SA_RIGHT_FILE_WRITE_EA | 
-               GENERIC_RIGHTS_FILE_READ |
-               GENERIC_RIGHTS_FILE_WRITE;
-       io.ntcreatex.in.file_attr = 0;
-       io.ntcreatex.in.alloc_size = 0;
-       io.ntcreatex.in.share_access = 
+       ctx = composite_create(c, c->event_ctx);
+       if (ctx == NULL) return NULL;
+
+       state = talloc(ctx, struct pipe_open_smb_state);
+       if (composite_nomem(state, ctx)) return ctx;
+       ctx->private_data = state;
+
+       state->c = c;
+       state->tree = tree;
+       state->ctx = ctx;
+
+       state->open = talloc(state, union smb_open);
+       if (composite_nomem(state->open, ctx)) return ctx;
+
+       state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
+       state->open->ntcreatex.in.flags = 0;
+       state->open->ntcreatex.in.root_fid = 0;
+       state->open->ntcreatex.in.access_mask = 
+               SEC_STD_READ_CONTROL |
+               SEC_FILE_WRITE_ATTRIBUTE |
+               SEC_FILE_WRITE_EA |
+               SEC_FILE_READ_DATA |
+               SEC_FILE_WRITE_DATA;
+       state->open->ntcreatex.in.file_attr = 0;
+       state->open->ntcreatex.in.alloc_size = 0;
+       state->open->ntcreatex.in.share_access = 
                NTCREATEX_SHARE_ACCESS_READ |
                NTCREATEX_SHARE_ACCESS_WRITE;
-       io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
-       io.ntcreatex.in.create_options = 0;
-       io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
-       io.ntcreatex.in.security_flags = 0;
-       io.ntcreatex.in.fname = name;
-
-       mem_ctx = talloc_init("torture_rpc_connection");
-       if (!mem_ctx) {
-               free(name);
-               return NT_STATUS_NO_MEMORY;
+       state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
+       state->open->ntcreatex.in.create_options = 0;
+       state->open->ntcreatex.in.impersonation =
+               NTCREATEX_IMPERSONATION_IMPERSONATION;
+       state->open->ntcreatex.in.security_flags = 0;
+
+       if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) || 
+           (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
+               pipe_name += 6;
        }
-       status = smb_raw_open(tree, mem_ctx, &io);
-       free(name);
-       talloc_destroy(mem_ctx);
+       state->open->ntcreatex.in.fname =
+               (pipe_name[0] == '\\') ?
+               talloc_strdup(state->open, pipe_name) :
+               talloc_asprintf(state->open, "\\%s", pipe_name);
+       if (composite_nomem(state->open->ntcreatex.in.fname, ctx)) return ctx;
+
+       req = smb_raw_open_send(tree, state->open);
+       composite_continue_smb(ctx, req, pipe_open_recv, state);
+       return ctx;
+}
 
-       if (!NT_STATUS_IS_OK(status)) {
-                return status;
-        }
+static void pipe_open_recv(struct smbcli_request *req)
+{
+       struct pipe_open_smb_state *state = talloc_get_type(req->async.private,
+                                           struct pipe_open_smb_state);
+       struct composite_context *ctx = state->ctx;
+       struct dcerpc_connection *c = state->c;
+       struct smb_private *smb;
+       
+       ctx->status = smb_raw_open_recv(req, state, state->open);
+       if (!composite_is_ok(ctx)) return;
 
-        if (!(*p = dcerpc_pipe_init())) {
-                return NT_STATUS_NO_MEMORY;
-       }
        /*
          fill in the transport methods
        */
-       (*p)->transport.private = NULL;
-       (*p)->transport.full_request = smb_full_request;
-       (*p)->transport.secondary_request = smb_secondary_request;
-       (*p)->transport.initial_request = smb_initial_request;
-       (*p)->transport.shutdown_pipe = smb_shutdown_pipe;
-       (*p)->transport.peer_name = smb_peer_name;
+       c->transport.transport       = NCACN_NP;
+       c->transport.private_data    = NULL;
+       c->transport.shutdown_pipe   = smb_shutdown_pipe;
+       c->transport.peer_name       = smb_peer_name;
+       c->transport.target_hostname = smb_target_hostname;
+
+       c->transport.send_request    = smb_send_request;
+       c->transport.send_read       = send_read_request;
+       c->transport.recv_data       = NULL;
        
-       smb = talloc((*p)->mem_ctx, sizeof(*smb));
-       if (!smb) {
-               dcerpc_pipe_close(*p);
-               return NT_STATUS_NO_MEMORY;
-       }
+       /* Over-ride the default session key with the SMB session key */
+       c->security_state.session_key = smb_session_key;
 
-       smb->fnum = io.ntcreatex.out.fnum;
-       smb->tree = tree;
+       smb = talloc(c, struct smb_private);
+       if (composite_nomem(smb, ctx)) return;
 
-       (*p)->transport.private = smb;
-       tree->reference_count++;
+       smb->fnum       = state->open->ntcreatex.out.file.fnum;
+       smb->tree       = talloc_reference(smb, state->tree);
+       smb->server_name= strupper_talloc(smb,
+                         state->tree->session->transport->called.name);
+       if (composite_nomem(smb->server_name, ctx)) return;
+       smb->dead       = false;
 
-        return NT_STATUS_OK;
+       c->transport.private_data = smb;
+
+       composite_done(ctx);
+}
+
+NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
+{
+       NTSTATUS status = composite_wait(c);
+       talloc_free(c);
+       return status;
+}
+
+NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe *p,
+                             struct smbcli_tree *tree,
+                             const char *pipe_name)
+{
+       struct composite_context *ctx = dcerpc_pipe_open_smb_send(p, tree,
+                                                                 pipe_name);
+       return dcerpc_pipe_open_smb_recv(ctx);
+}
+
+/*
+  return the SMB tree used for a dcerpc over SMB pipe
+*/
+struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
+{
+       struct smb_private *smb;
+
+       if (c->transport.transport != NCACN_NP) return NULL;
+
+       smb = talloc_get_type(c->transport.private_data, struct smb_private);
+       if (!smb) return NULL;
+
+       return smb->tree;
+}
+
+/*
+  return the SMB fnum used for a dcerpc over SMB pipe (hack for torture operations)
+*/
+uint16_t dcerpc_smb_fnum(struct dcerpc_connection *c)
+{
+       struct smb_private *smb;
+
+       if (c->transport.transport != NCACN_NP) return 0;
+
+       smb = talloc_get_type(c->transport.private_data, struct smb_private);
+       if (!smb) return 0;
+
+       return smb->fnum;
 }