r23792: convert Samba4 to GPLv3
[sfrench/samba-autobuild/.git] / source4 / librpc / rpc / dcerpc_smb2.c
index d733ab47138e8b57705df69becbcae25831f9532..53f731f8b363913fec0035b9bff51c3762ae351b 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,
    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 "librpc/gen_ndr/ndr_security.h"
 #include "libcli/composite/composite.h"
 #include "libcli/smb2/smb2.h"
 #include "libcli/smb2/smb2_calls.h"
+#include "libcli/raw/ioctl.h"
+#include "librpc/rpc/dcerpc.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;
 };
 
 
@@ -40,7 +41,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 = 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,7 +83,7 @@ static void smb2_read_callback(struct smb2_request *req)
        NTSTATUS status;
 
        state = talloc_get_type(req->async.private, struct smb2_read_state);
-       smb = talloc_get_type(state->c->transport.private, struct smb2_private);
+       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)) {
@@ -102,12 +121,12 @@ static void smb2_read_callback(struct smb2_request *req)
 
        /* initiate another read request, as we only got part of a fragment */
        ZERO_STRUCT(io);
+       io.in.file.handle = smb->handle;
        io.in.length = MIN(state->c->srv_max_xmit_frag, 
                           frag_length - state->data.length);
        if (io.in.length < 16) {
                io.in.length = 16;
        }
-       io.in.handle = smb->handle;
        
        req = smb2_read_send(smb->tree, &io);
        if (req == NULL) {
@@ -127,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 = c->transport.private_data;
        struct smb2_read io;
        struct smb2_read_state *state;
        struct smb2_request *req;
@@ -146,7 +165,7 @@ static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLO
        }
 
        ZERO_STRUCT(io);
-       io.in.handle = smb->handle;
+       io.in.file.handle = smb->handle;
 
        if (state->data.length >= 16) {
                uint16_t frag_length = dcerpc_get_frag_length(&state->data);
@@ -172,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 = c->transport.private_data;
+
+       if (smb->dead) {
+               return NT_STATUS_CONNECTION_DISCONNECTED;
+       }
+
        return send_read_request_continue(c, NULL);
 }
 
@@ -191,9 +216,9 @@ static void smb2_trans_callback(struct smb2_request *req)
                                                        struct smb2_trans_state);
        struct dcerpc_connection *c = state->c;
        NTSTATUS status;
-       struct smb2_trans io;
+       struct smb2_ioctl io;
 
-       status = smb2_trans_recv(req, state, &io);
+       status = smb2_ioctl_recv(req, state, &io);
        if (NT_STATUS_IS_ERR(status)) {
                pipe_dead(c, status);
                return;
@@ -213,13 +238,13 @@ static void smb2_trans_callback(struct smb2_request *req)
 }
 
 /*
-  send a SMBtrans style request
+  send a SMBtrans style request, using a named pipe read_write fsctl
 */
 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_trans io;
+        struct smb2_ioctl io;
        struct smb2_trans_state *state;
        struct smb2_request *req;
 
@@ -231,13 +256,13 @@ static NTSTATUS smb2_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *
        state->c = c;
        
        ZERO_STRUCT(io);
-       io.in.pipe_flags = SMB2_TRANS_PIPE_FLAGS;
-       io.in.handle = smb->handle;
-       io.in.max_response_size = 0x1000;
-       io.in.flags = 1;
-       io.in.out = *blob;
+       io.in.file.handle       = smb->handle;
+       io.in.function          = FSCTL_NAMED_PIPE_READ_WRITE;
+       io.in.max_response_size = 0x1000;
+       io.in.flags             = 1;
+       io.in.out               = *blob;
 
-        req = smb2_trans_send(smb->tree, &io);
+        req = smb2_ioctl_send(smb->tree, &io);
        if (req == NULL) {
                talloc_free(state);
                return NT_STATUS_NO_MEMORY;
@@ -272,17 +297,21 @@ static void smb2_write_callback(struct smb2_request *req)
 static NTSTATUS smb2_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, 
                                  BOOL trigger_read)
 {
-       struct smb2_private *smb = c->transport.private;
+       struct smb2_private *smb = 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);
        }
 
        ZERO_STRUCT(io);
-       io.in.handle = smb->handle;
-       io.in.data = *blob;
+       io.in.file.handle       = smb->handle;
+       io.in.data              = *blob;
 
        req = smb2_write_send(smb->tree, &io);
        if (req == NULL) {
@@ -298,17 +327,17 @@ 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 = 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.handle = smb->handle;
+       io.in.file.handle = smb->handle;
        req = smb2_close_send(smb->tree, &io);
        if (req != NULL) {
                /* we don't care if this fails, so just free it if it succeeds */
@@ -317,7 +346,7 @@ static NTSTATUS smb2_shutdown_pipe(struct dcerpc_connection *c)
 
        talloc_free(smb);
 
-       return NT_STATUS_OK;
+       return status;
 }
 
 /*
@@ -325,17 +354,27 @@ 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;
 }
 
+/*
+  return remote name we make the actual connection (good for kerberos) 
+*/
+static const char *smb2_target_hostname(struct dcerpc_connection *c)
+{
+       struct smb2_private *smb = talloc_get_type(c->transport.private_data, 
+                                                  struct smb2_private);
+       return smb->tree->session->transport->socket->hostname;
+}
+
 /*
   fetch the user session key 
 */
 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) {
@@ -351,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)
 {
@@ -359,14 +398,14 @@ 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 = 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);
+       ctx = composite_create(c, c->event_ctx);
+       if (ctx == NULL) return NULL;
 
        state = talloc(ctx, struct pipe_open_smb2_state);
-       if (state == NULL) goto failed;
+       if (composite_nomem(state, ctx)) return ctx;
+       ctx->private_data = state;
 
        state->c = c;
        state->ctx = ctx;
@@ -398,16 +437,8 @@ struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_connection *c
        io.in.fname = pipe_name;
 
        req = smb2_create_send(tree, &io);
-       if (req == NULL) goto failed;
-
-       req->async.fn = pipe_open_recv;
-       req->async.private = state;
-
+       composite_continue_smb2(ctx, req, pipe_open_recv, state);
        return ctx;
-
- failed:
-       talloc_free(ctx);
-       return NULL;
 }
 
 static void pipe_open_recv(struct smb2_request *req)
@@ -428,9 +459,10 @@ 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;
 
        c->transport.send_request = smb2_send_request;
        c->transport.send_read = send_read_request;
@@ -442,13 +474,14 @@ static void pipe_open_recv(struct smb2_request *req)
        smb = talloc(c, struct smb2_private);
        if (composite_nomem(smb, ctx)) return;
 
-       smb->handle     = io.out.handle;
+       smb->handle     = io.out.file.handle;
        smb->tree       = talloc_reference(smb, tree);
        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);
 }
@@ -460,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);
 }
 
@@ -473,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;
 }