Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-local
[kai/samba.git] / source4 / librpc / rpc / dcerpc_smb2.c
index 845884115cd2049858b0162e7f31949a1677d15b..4767165fba8dd62596f9d3689fa763776ec62a48 100644 (file)
@@ -7,7 +7,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,
@@ -16,8 +16,7 @@
    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/smb2/smb2_calls.h"
 #include "libcli/raw/ioctl.h"
 #include "librpc/rpc/dcerpc.h"
+#include "librpc/rpc/dcerpc_proto.h"
 
 /* transport private information used by SMB2 pipe transport */
 struct smb2_private {
        struct smb2_handle handle;
        struct smb2_tree *tree;
        const char *server_name;
+       bool dead;
 };
 
 
@@ -41,7 +42,25 @@ struct smb2_private {
 */
 static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
 {
-       c->transport.recv_data(c, NULL, status);
+       struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
+
+       if (smb->dead) {
+               return;
+       }
+
+       smb->dead = true;
+
+       if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
+               status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
+       }
+
+       if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
+               status = NT_STATUS_END_OF_FILE;
+       }
+
+       if (c->transport.recv_data) {
+               c->transport.recv_data(c, NULL, status);
+       }
 }
 
 
@@ -64,8 +83,8 @@ static void smb2_read_callback(struct smb2_request *req)
        uint16_t frag_length;
        NTSTATUS status;
 
-       state = talloc_get_type(req->async.private, struct smb2_read_state);
-       smb = talloc_get_type(state->c->transport.private, struct smb2_private);
+       state = talloc_get_type(req->async.private_data, struct smb2_read_state);
+       smb = talloc_get_type(state->c->transport.private_data, struct smb2_private);
 
        status = smb2_read_recv(req, state, &io);
        if (NT_STATUS_IS_ERR(status)) {
@@ -74,10 +93,9 @@ static void smb2_read_callback(struct smb2_request *req)
                return;
        }
 
-       status = data_blob_append(state, &state->data, 
-                                 io.out.data.data, io.out.data.length);
-       if (NT_STATUS_IS_ERR(status)) {
-               pipe_dead(state->c, status);
+       if (!data_blob_append(state, &state->data, 
+                                 io.out.data.data, io.out.data.length)) {
+               pipe_dead(state->c, NT_STATUS_NO_MEMORY);
                talloc_free(state);
                return;
        }
@@ -118,7 +136,7 @@ static void smb2_read_callback(struct smb2_request *req)
        }
 
        req->async.fn = smb2_read_callback;
-       req->async.private = state;
+       req->async.private_data = state;
 }
 
 
@@ -128,7 +146,7 @@ static void smb2_read_callback(struct smb2_request *req)
 */
 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
 {
-       struct smb2_private *smb = c->transport.private;
+       struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
        struct smb2_read io;
        struct smb2_read_state *state;
        struct smb2_request *req;
@@ -162,7 +180,7 @@ static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLO
        }
 
        req->async.fn = smb2_read_callback;
-       req->async.private = state;
+       req->async.private_data = state;
 
        return NT_STATUS_OK;
 }
@@ -173,6 +191,12 @@ static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLO
 */
 static NTSTATUS send_read_request(struct dcerpc_connection *c)
 {
+       struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
+
+       if (smb->dead) {
+               return NT_STATUS_CONNECTION_DISCONNECTED;
+       }
+
        return send_read_request_continue(c, NULL);
 }
 
@@ -188,7 +212,7 @@ struct smb2_trans_state {
 */
 static void smb2_trans_callback(struct smb2_request *req)
 {
-       struct smb2_trans_state *state = talloc_get_type(req->async.private,
+       struct smb2_trans_state *state = talloc_get_type(req->async.private_data,
                                                        struct smb2_trans_state);
        struct dcerpc_connection *c = state->c;
        NTSTATUS status;
@@ -218,7 +242,7 @@ static void smb2_trans_callback(struct smb2_request *req)
 */
 static NTSTATUS smb2_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
 {
-        struct smb2_private *smb = talloc_get_type(c->transport.private,
+        struct smb2_private *smb = talloc_get_type(c->transport.private_data,
                                                   struct smb2_private);
         struct smb2_ioctl io;
        struct smb2_trans_state *state;
@@ -245,7 +269,7 @@ static NTSTATUS smb2_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *
        }
 
        req->async.fn = smb2_trans_callback;
-       req->async.private = state;
+       req->async.private_data = state;
 
        talloc_steal(state, req);
 
@@ -257,7 +281,7 @@ static NTSTATUS smb2_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *
 */
 static void smb2_write_callback(struct smb2_request *req)
 {
-       struct dcerpc_connection *c = req->async.private;
+       struct dcerpc_connection *c = (struct dcerpc_connection *)req->async.private_data;
 
        if (!NT_STATUS_IS_OK(req->status)) {
                DEBUG(0,("dcerpc_smb2: write callback error\n"));
@@ -271,12 +295,16 @@ static void smb2_write_callback(struct smb2_request *req)
    send a packet to the server
 */
 static NTSTATUS smb2_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, 
-                                 BOOL trigger_read)
+                                 bool trigger_read)
 {
-       struct smb2_private *smb = c->transport.private;
+       struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
        struct smb2_write io;
        struct smb2_request *req;
 
+       if (smb->dead) {
+               return NT_STATUS_CONNECTION_DISCONNECTED;
+       }
+
        if (trigger_read) {
                return smb2_send_trans_request(c, blob);
        }
@@ -291,7 +319,7 @@ static NTSTATUS smb2_send_request(struct dcerpc_connection *c, DATA_BLOB *blob,
        }
 
        req->async.fn = smb2_write_callback;
-       req->async.private = c;
+       req->async.private_data = c;
 
        return NT_STATUS_OK;
 }
@@ -299,14 +327,14 @@ static NTSTATUS smb2_send_request(struct dcerpc_connection *c, DATA_BLOB *blob,
 /* 
    shutdown SMB pipe connection
 */
-static NTSTATUS smb2_shutdown_pipe(struct dcerpc_connection *c)
+static NTSTATUS smb2_shutdown_pipe(struct dcerpc_connection *c, NTSTATUS status)
 {
-       struct smb2_private *smb = c->transport.private;
+       struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
        struct smb2_close io;
        struct smb2_request *req;
 
        /* maybe we're still starting up */
-       if (!smb) return NT_STATUS_OK;
+       if (!smb) return status;
 
        ZERO_STRUCT(io);
        io.in.file.handle = smb->handle;
@@ -318,7 +346,7 @@ static NTSTATUS smb2_shutdown_pipe(struct dcerpc_connection *c)
 
        talloc_free(smb);
 
-       return NT_STATUS_OK;
+       return status;
 }
 
 /*
@@ -326,7 +354,7 @@ static NTSTATUS smb2_shutdown_pipe(struct dcerpc_connection *c)
 */
 static const char *smb2_peer_name(struct dcerpc_connection *c)
 {
-       struct smb2_private *smb = talloc_get_type(c->transport.private,
+       struct smb2_private *smb = talloc_get_type(c->transport.private_data,
                                                   struct smb2_private);
        return smb->server_name;
 }
@@ -336,7 +364,7 @@ static const char *smb2_peer_name(struct dcerpc_connection *c)
 */
 static const char *smb2_target_hostname(struct dcerpc_connection *c)
 {
-       struct smb2_private *smb = talloc_get_type(c->transport.private, 
+       struct smb2_private *smb = talloc_get_type(c->transport.private_data
                                                   struct smb2_private);
        return smb->tree->session->transport->socket->hostname;
 }
@@ -346,7 +374,7 @@ static const char *smb2_target_hostname(struct dcerpc_connection *c)
 */
 static NTSTATUS smb2_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
 {
-       struct smb2_private *smb = talloc_get_type(c->transport.private,
+       struct smb2_private *smb = talloc_get_type(c->transport.private_data,
                                                   struct smb2_private);
        *session_key = smb->tree->session->session_key;
        if (session_key->data == NULL) {
@@ -362,7 +390,7 @@ struct pipe_open_smb2_state {
 
 static void pipe_open_recv(struct smb2_request *req);
 
-struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_connection *c
+struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_pipe *p
                                                     struct smb2_tree *tree,
                                                     const char *pipe_name)
 {
@@ -370,6 +398,7 @@ struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_connection *c
        struct pipe_open_smb2_state *state;
        struct smb2_create io;
        struct smb2_request *req;
+       struct dcerpc_connection *c = p->conn;
 
        ctx = composite_create(c, c->event_ctx);
        if (ctx == NULL) return NULL;
@@ -382,7 +411,7 @@ struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_connection *c
        state->ctx = ctx;
 
        ZERO_STRUCT(io);
-       io.in.access_mask = 
+       io.in.desired_access = 
                SEC_STD_READ_CONTROL |
                SEC_FILE_READ_ATTRIBUTE |
                SEC_FILE_WRITE_ATTRIBUTE |
@@ -395,11 +424,11 @@ struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_connection *c
        io.in.share_access = 
                NTCREATEX_SHARE_ACCESS_READ |
                NTCREATEX_SHARE_ACCESS_WRITE;
-       io.in.open_disposition = NTCREATEX_DISP_OPEN;
+       io.in.create_disposition = NTCREATEX_DISP_OPEN;
        io.in.create_options   = 
                NTCREATEX_OPTIONS_NON_DIRECTORY_FILE | 
                NTCREATEX_OPTIONS_UNKNOWN_400000;
-       io.in.impersonation    = NTCREATEX_IMPERSONATION_IMPERSONATION;
+       io.in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
 
        if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) || 
            (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
@@ -415,7 +444,7 @@ struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_connection *c
 static void pipe_open_recv(struct smb2_request *req)
 {
        struct pipe_open_smb2_state *state =
-               talloc_get_type(req->async.private,
+               talloc_get_type(req->async.private_data,
                                struct pipe_open_smb2_state);
        struct composite_context *ctx = state->ctx;
        struct dcerpc_connection *c = state->c;
@@ -430,7 +459,7 @@ static void pipe_open_recv(struct smb2_request *req)
          fill in the transport methods
        */
        c->transport.transport = NCACN_NP;
-       c->transport.private = NULL;
+       c->transport.private_data = NULL;
        c->transport.shutdown_pipe = smb2_shutdown_pipe;
        c->transport.peer_name = smb2_peer_name;
        c->transport.target_hostname = smb2_target_hostname;
@@ -450,8 +479,9 @@ static void pipe_open_recv(struct smb2_request *req)
        smb->server_name= strupper_talloc(smb, 
                                          tree->session->transport->socket->hostname);
        if (composite_nomem(smb->server_name, ctx)) return;
+       smb->dead       = false;
 
-       c->transport.private = smb;
+       c->transport.private_data = smb;
 
        composite_done(ctx);
 }
@@ -463,11 +493,11 @@ NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
        return status;
 }
 
-NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_connection *c,
+NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_pipe *p,
                               struct smb2_tree *tree,
                               const char *pipe_name)
 {
-       struct composite_context *ctx = dcerpc_pipe_open_smb2_send(c, tree, pipe_name);
+       struct composite_context *ctx = dcerpc_pipe_open_smb2_send(p, tree, pipe_name);
        return dcerpc_pipe_open_smb2_recv(ctx);
 }
 
@@ -476,7 +506,7 @@ NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_connection *c,
 */
 struct smb2_tree *dcerpc_smb2_tree(struct dcerpc_connection *c)
 {
-       struct smb2_private *smb = talloc_get_type(c->transport.private,
+       struct smb2_private *smb = talloc_get_type(c->transport.private_data,
                                                   struct smb2_private);
        return smb->tree;
 }