r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / librpc / rpc / dcerpc_smb.c
index deef2232bf743cbb22c9710d8af8d33533116338..c0716a1af894755c566e8de27c1d1777a75ad9e0 100644 (file)
 */
 
 #include "includes.h"
+#include "libcli/raw/libcliraw.h"
+#include "libcli/composite/composite.h"
 
 /* transport private information used by SMB pipe transport */
 struct smb_private {
        uint16_t fnum;
        struct smbcli_tree *tree;
+       const char *server_name;
 };
 
 
 /*
   tell the dcerpc layer that the transport is dead
 */
-static void pipe_dead(struct dcerpc_pipe *p, NTSTATUS status)
+static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
 {
-       p->transport.recv_data(p, NULL, status);
+       c->transport.recv_data(c, NULL, status);
 }
 
 
@@ -43,7 +46,7 @@ static void pipe_dead(struct dcerpc_pipe *p, NTSTATUS status)
    this holds the state of an in-flight call
 */
 struct smb_read_state {
-       struct dcerpc_pipe *p;
+       struct dcerpc_connection *c;
        struct smbcli_request *req;
        size_t received;
        DATA_BLOB data;
@@ -61,13 +64,13 @@ static void smb_read_callback(struct smbcli_request *req)
        uint16_t frag_length;
        NTSTATUS status;
 
-       state = req->async.private;
-       smb = state->p->transport.private;
+       state = talloc_get_type(req->async.private, struct smb_read_state);
+       smb = talloc_get_type(state->c->transport.private, struct smb_private);
        io = state->io;
 
        status = smb_raw_read_recv(state->req, io);
        if (NT_STATUS_IS_ERR(status)) {
-               pipe_dead(state->p, status);
+               pipe_dead(state->c, status);
                talloc_free(state);
                return;
        }
@@ -76,8 +79,8 @@ static void smb_read_callback(struct smbcli_request *req)
 
        if (state->received < 16) {
                DEBUG(0,("dcerpc_smb: short packet (length %d) in read callback!\n",
-                        state->received));
-               pipe_dead(state->p, NT_STATUS_INFO_LENGTH_MISMATCH);
+                        (int)state->received));
+               pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
                talloc_free(state);
                return;
        }
@@ -85,23 +88,26 @@ static void smb_read_callback(struct smbcli_request *req)
        frag_length = dcerpc_get_frag_length(&state->data);
 
        if (frag_length <= state->received) {
-               state->data.length = state->received;
-               state->p->transport.recv_data(state->p, &state->data, NT_STATUS_OK);
+               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;
        }
 
        /* initiate another read request, as we only got part of a fragment */
-       state->data.data = talloc_realloc(state->data.data, frag_length);
+       state->data.data = talloc_realloc(state, state->data.data, uint8_t, frag_length);
 
-       io->readx.in.mincnt = MIN(state->p->srv_max_xmit_frag, 
+       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->p, NT_STATUS_NO_MEMORY);
+               pipe_dead(state->c, NT_STATUS_NO_MEMORY);
                talloc_free(state);
                return;
        }
@@ -114,19 +120,19 @@ static void smb_read_callback(struct smbcli_request *req)
   trigger a read request from the server, possibly with some initial
   data in the read buffer
 */
-static NTSTATUS send_read_request_continue(struct dcerpc_pipe *p, DATA_BLOB *blob)
+static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
 {
-       struct smb_private *smb = p->transport.private;
+       struct smb_private *smb = c->transport.private;
        union smb_read *io;
        struct smb_read_state *state;
        struct smbcli_request *req;
 
-       state = talloc_p(smb, struct smb_read_state);
+       state = talloc(smb, struct smb_read_state);
        if (state == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       state->p = p;
+       state->c = c;
        if (blob == NULL) {
                state->received = 0;
                state->data = data_blob_talloc(state, NULL, 0x2000);
@@ -142,7 +148,7 @@ static NTSTATUS send_read_request_continue(struct dcerpc_pipe *p, DATA_BLOB *blo
                memcpy(state->data.data, blob->data, blob->length);
        }
 
-       state->io = talloc_p(state, union smb_read);
+       state->io = talloc(state, union smb_read);
 
        io = state->io;
        io->generic.level = RAW_READ_READX;
@@ -169,16 +175,16 @@ static NTSTATUS send_read_request_continue(struct dcerpc_pipe *p, DATA_BLOB *blo
 /*
   trigger a read request from the server
 */
-static NTSTATUS send_read_request(struct dcerpc_pipe *p)
+static NTSTATUS send_read_request(struct dcerpc_connection *c)
 {
-       return send_read_request_continue(p, NULL);
+       return send_read_request_continue(c, NULL);
 }
 
 /* 
    this holds the state of an in-flight trans call
 */
 struct smb_trans_state {
-       struct dcerpc_pipe *p;
+       struct dcerpc_connection *c;
        struct smbcli_request *req;
        struct smb_trans2 *trans;
 };
@@ -189,44 +195,46 @@ struct smb_trans_state {
 static void smb_trans_callback(struct smbcli_request *req)
 {
        struct smb_trans_state *state = req->async.private;
-       struct dcerpc_pipe *p = state->p;
+       struct dcerpc_connection *c = state->c;
        NTSTATUS status;
 
        status = smb_raw_trans_recv(req, state, state->trans);
 
        if (NT_STATUS_IS_ERR(status)) {
-               pipe_dead(p, status);
+               pipe_dead(c, status);
                return;
        }
 
        if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
-               p->transport.recv_data(p, &state->trans->out.data, NT_STATUS_OK);
+               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;
        }
 
        /* there is more to receive - setup a readx */
-       send_read_request_continue(p, &state->trans->out.data);
+       send_read_request_continue(c, &state->trans->out.data);
        talloc_free(state);
 }
 
 /*
   send a SMBtrans style request
 */
-static NTSTATUS smb_send_trans_request(struct dcerpc_pipe *p, DATA_BLOB *blob)
+static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
 {
-        struct smb_private *smb = p->transport.private;
+        struct smb_private *smb = c->transport.private;
         struct smb_trans2 *trans;
-        uint16 setup[2];
+        uint16_t setup[2];
        struct smb_trans_state *state;
 
-       state = talloc_p(smb, struct smb_trans_state);
+       state = talloc(smb, struct smb_trans_state);
        if (state == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       state->p = p;
-       state->trans = talloc_p(state, struct smb_trans2);
+       state->c = c;
+       state->trans = talloc(state, struct smb_trans2);
        trans = state->trans;
 
         trans->in.data = *blob;
@@ -236,7 +244,7 @@ static NTSTATUS smb_send_trans_request(struct dcerpc_pipe *p, DATA_BLOB *blob)
         setup[1] = smb->fnum;
 
         trans->in.max_param = 0;
-        trans->in.max_data = 0x8000;
+        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;
@@ -253,6 +261,8 @@ static NTSTATUS smb_send_trans_request(struct dcerpc_pipe *p, DATA_BLOB *blob)
        state->req->async.fn = smb_trans_callback;
        state->req->async.private = state;
 
+       talloc_steal(state, state->req);
+
         return NT_STATUS_OK;
 }
 
@@ -261,11 +271,11 @@ static NTSTATUS smb_send_trans_request(struct dcerpc_pipe *p, DATA_BLOB *blob)
 */
 static void smb_write_callback(struct smbcli_request *req)
 {
-       struct dcerpc_pipe *p = req->async.private;
+       struct dcerpc_connection *c = req->async.private;
 
        if (!NT_STATUS_IS_OK(req->status)) {
                DEBUG(0,("dcerpc_smb: write callback error\n"));
-               pipe_dead(p, req->status);
+               pipe_dead(c, req->status);
        }
 
        smbcli_request_destroy(req);
@@ -274,14 +284,14 @@ static void smb_write_callback(struct smbcli_request *req)
 /* 
    send a packet to the server
 */
-static NTSTATUS smb_send_request(struct dcerpc_pipe *p, DATA_BLOB *blob, BOOL trigger_read)
+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;
        union smb_write io;
        struct smbcli_request *req;
 
        if (trigger_read) {
-               return smb_send_trans_request(p, blob);
+               return smb_send_trans_request(c, blob);
        }
 
        io.generic.level = RAW_WRITE_WRITEX;
@@ -292,49 +302,47 @@ static NTSTATUS smb_send_request(struct dcerpc_pipe *p, DATA_BLOB *blob, BOOL tr
        io.writex.in.count = blob->length;
        io.writex.in.data = blob->data;
 
+       /* 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;
        }
 
        req->async.fn = smb_write_callback;
-       req->async.private = p;
+       req->async.private = c;
 
        if (trigger_read) {
-               send_read_request(p);
+               send_read_request(c);
        }
 
        return NT_STATUS_OK;
 }
 
-/* 
-   return the event context for the pipe, so the caller can wait
-   for events asynchronously
-*/
-static struct event_context *smb_event_context(struct dcerpc_pipe *p)
-{
-       struct smb_private *smb = p->transport.private;
-
-       return smb->tree->session->transport->event.ctx;
-}
-
-
 /* 
    shutdown SMB pipe connection
 */
-static NTSTATUS smb_shutdown_pipe(struct dcerpc_pipe *p)
+static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c)
 {
-       struct smb_private *smb = p->transport.private;
-       union smb_close c;
+       struct smb_private *smb = c->transport.private;
+       union smb_close io;
+       struct smbcli_request *req;
 
        /* maybe we're still starting up */
        if (!smb) return NT_STATUS_OK;
 
-       c.close.level = RAW_CLOSE_CLOSE;
-       c.close.in.fnum = smb->fnum;
-       c.close.in.write_time = 0;
-       smb_raw_close(smb->tree, &c);
-       smbcli_tree_close(smb->tree);
+       io.close.level = RAW_CLOSE_CLOSE;
+       io.close.in.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;
+       }
+
+       talloc_free(smb);
 
        return NT_STATUS_OK;
 }
@@ -342,103 +350,181 @@ static NTSTATUS smb_shutdown_pipe(struct dcerpc_pipe *p)
 /*
   return SMB server 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;
+       return smb->server_name;
 }
 
-/* 
-   open a rpc connection to a named pipe 
+/*
+  fetch the user session key 
 */
-NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe **p, 
-                             struct smbcli_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;
 
-       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;
+}
 
-       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 = 
+struct pipe_open_smb_state {
+       union smb_open *open;
+       struct dcerpc_connection *c;
+       struct smbcli_request *req;
+       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_connection *c, 
+                                                   struct smbcli_tree *tree,
+                                                   const char *pipe_name)
+{
+       struct composite_context *ctx;
+       struct pipe_open_smb_state *state;
+
+       ctx = talloc_zero(NULL, struct composite_context);
+       if (ctx == NULL) goto failed;
+       ctx->state = COMPOSITE_STATE_IN_PROGRESS;
+       ctx->event_ctx = talloc_reference(c, c->event_ctx);
+
+       state = talloc(ctx, struct pipe_open_smb_state);
+       if (state == NULL) goto failed;
+
+       state->c = c;
+       state->tree = tree;
+       state->ctx = ctx;
+
+       state->open = talloc(state, union smb_open);
+       if (state->open == NULL) goto failed;
+
+       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_free(mem_ctx);
+       state->open->ntcreatex.in.fname =
+               (pipe_name[0] == '\\') ?
+               talloc_strdup(state->open, pipe_name) :
+               talloc_asprintf(state->open, "\\%s", pipe_name);
+       if (state->open->ntcreatex.in.fname == NULL) goto failed;
 
-       if (!NT_STATUS_IS_OK(status)) {
-                return status;
-        }
+       state->req = smb_raw_open_send(tree, state->open);
+       if (state->req == NULL) goto failed;
+
+       state->req->async.fn = pipe_open_recv;
+       state->req->async.private = state;
+
+       return ctx;
+
+ failed:
+       talloc_free(ctx);
+       return NULL;
+}
+
+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 (!NT_STATUS_IS_OK(ctx->status)) goto done;
 
-        if (!(*p = dcerpc_pipe_init())) {
-                return NT_STATUS_NO_MEMORY;
-       }
        /*
          fill in the transport methods
        */
-       (*p)->transport.transport = NCACN_NP;
-       (*p)->transport.private = NULL;
-       (*p)->transport.shutdown_pipe = smb_shutdown_pipe;
-       (*p)->transport.peer_name = smb_peer_name;
-
-       (*p)->transport.send_request = smb_send_request;
-       (*p)->transport.send_read = send_read_request;
-       (*p)->transport.event_context = smb_event_context;
-       (*p)->transport.recv_data = NULL;
+       c->transport.transport = NCACN_NP;
+       c->transport.private = NULL;
+       c->transport.shutdown_pipe = smb_shutdown_pipe;
+       c->transport.peer_name = smb_peer_name;
+
+       c->transport.send_request = smb_send_request;
+       c->transport.send_read = send_read_request;
+       c->transport.recv_data = NULL;
        
-       smb = talloc((*p), 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 = talloc(c, struct smb_private);
+       if (smb == NULL) {
+               ctx->status = NT_STATUS_NO_MEMORY;
+               goto done;
        }
 
-       smb->fnum = io.ntcreatex.out.fnum;
-       smb->tree = tree;
+       smb->fnum       = state->open->ntcreatex.out.fnum;
+       smb->tree       = talloc_reference(smb, state->tree);
+       smb->server_name= strupper_talloc(
+               smb, state->tree->session->transport->called.name);
+       if (smb->server_name == NULL) {
+               ctx->status = NT_STATUS_NO_MEMORY;
+               goto done;
+       }
+       c->transport.private = smb;
 
-       (*p)->transport.private = smb;
-       tree->reference_count++;
+       ctx->status = NT_STATUS_OK;
+       ctx->state = COMPOSITE_STATE_DONE;
 
-        return NT_STATUS_OK;
+ done:
+       if (!NT_STATUS_IS_OK(ctx->status)) {
+               ctx->state = COMPOSITE_STATE_ERROR;
+       }
+       if ((ctx->state >= COMPOSITE_STATE_DONE) &&
+           (ctx->async.fn != NULL)) {
+               ctx->async.fn(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_connection *c,
+                             struct smbcli_tree *tree,
+                             const char *pipe_name)
+{
+       struct composite_context *ctx = dcerpc_pipe_open_smb_send(c, 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_pipe *p)
+struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
 {
-       struct smb_private *smb = p->transport.private;
+       struct smb_private *smb = c->transport.private;
 
-       if (p->transport.transport != NCACN_NP) {
+       if (c->transport.transport != NCACN_NP) {
                return NULL;
        }