r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / librpc / rpc / dcerpc_smb.c
index 1a5a31c330fb69f1a6aab02a55d3b553579f6638..c0716a1af894755c566e8de27c1d1777a75ad9e0 100644 (file)
 
 #include "includes.h"
 #include "libcli/raw/libcliraw.h"
-#include "librpc/gen_ndr/ndr_security.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;
 };
 
 
@@ -63,8 +64,8 @@ static void smb_read_callback(struct smbcli_request *req)
        uint16_t frag_length;
        NTSTATUS status;
 
-       state = req->async.private;
-       smb = state->c->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);
@@ -78,7 +79,7 @@ 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));
+                        (int)state->received));
                pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
                talloc_free(state);
                return;
@@ -87,9 +88,12 @@ 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->c->transport.recv_data(state->c, &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;
        }
 
@@ -202,8 +206,10 @@ static void smb_trans_callback(struct smbcli_request *req)
        }
 
        if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
-               c->transport.recv_data(c, &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;
        }
 
@@ -255,6 +261,8 @@ static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *b
        state->req->async.fn = smb_trans_callback;
        state->req->async.private = state;
 
+       talloc_steal(state, state->req);
+
         return NT_STATUS_OK;
 }
 
@@ -294,6 +302,10 @@ static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, B
        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;
@@ -309,18 +321,6 @@ static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, B
        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_connection *c)
-{
-       struct smb_private *smb = c->transport.private;
-
-       return smb->tree->session->transport->socket->event.ctx;
-}
-
-
 /* 
    shutdown SMB pipe connection
 */
@@ -328,6 +328,7 @@ static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *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;
@@ -335,7 +336,11 @@ static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c)
        io.close.level = RAW_CLOSE_CLOSE;
        io.close.in.fnum = smb->fnum;
        io.close.in.write_time = 0;
-       smb_raw_close(smb->tree, &io);
+       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);
 
@@ -348,7 +353,7 @@ static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c)
 static const char *smb_peer_name(struct dcerpc_connection *c)
 {
        struct smb_private *smb = c->transport.private;
-       return smb->tree->session->transport->called.name;
+       return smb->server_name;
 }
 
 /*
@@ -365,56 +370,92 @@ static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_
        return NT_STATUS_NO_USER_SESSION_KEY;
 }
 
-/* 
-   open a rpc connection to a named pipe 
-*/
-NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_connection *c, 
-                             struct smbcli_tree *tree,
-                             const char *pipe_name)
+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 smb_private *smb;
-        NTSTATUS status;
-       union smb_open io;
-       char *pipe_name_talloc;
+       struct composite_context *ctx;
+       struct pipe_open_smb_state *state;
 
-       if (!strncasecmp(pipe_name, "/pipe/", 6) || 
-           !strncasecmp(pipe_name, "\\pipe\\", 6)) {
-               pipe_name += 6;
-       }
+       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);
 
-       if (pipe_name[0] != '\\') {
-               pipe_name_talloc = talloc_asprintf(NULL, "\\%s", pipe_name);
-       } else {
-               pipe_name_talloc = talloc_strdup(NULL, pipe_name);
-       }
-       
-       io.ntcreatex.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.flags = 0;
-       io.ntcreatex.in.root_fid = 0;
-       io.ntcreatex.in.access_mask = 
+       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;
-       io.ntcreatex.in.file_attr = 0;
-       io.ntcreatex.in.alloc_size = 0;
-       io.ntcreatex.in.share_access = 
+       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 = pipe_name_talloc;
+       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;
+       }
+       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;
+
+       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;
 
-       status = smb_raw_open(tree, tree, &io);
+       return ctx;
 
-       talloc_free(pipe_name_talloc);
+ failed:
+       talloc_free(ctx);
+       return NULL;
+}
 
-       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 (!NT_STATUS_IS_OK(ctx->status)) goto done;
 
        /*
          fill in the transport methods
@@ -426,7 +467,6 @@ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_connection *c,
 
        c->transport.send_request = smb_send_request;
        c->transport.send_read = send_read_request;
-       c->transport.event_context = smb_event_context;
        c->transport.recv_data = NULL;
        
        /* Over-ride the default session key with the SMB session key */
@@ -434,15 +474,47 @@ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_connection *c,
 
        smb = talloc(c, struct smb_private);
        if (smb == NULL) {
-               return NT_STATUS_NO_MEMORY;
+               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;
 
-        return NT_STATUS_OK;
+       ctx->status = NT_STATUS_OK;
+       ctx->state = COMPOSITE_STATE_DONE;
+
+ 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);
 }
 
 /*