s4:librpc/rpc: remove unused dcerpc_smb_fnum()
[mat/samba.git] / source4 / librpc / rpc / dcerpc_smb.c
index b63a3080f5aaddd00282bea1b3998b471b6ba474..71faeb4b3feed3a3cf33167fa506390fe657fc19 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 "librpc/gen_ndr/ndr_security.h"
 #include "libcli/composite/composite.h"
+#include "librpc/rpc/dcerpc.h"
+#include "librpc/rpc/dcerpc_proto.h"
+#include "librpc/rpc/rpc_common.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 /* transport private information used by SMB pipe transport */
 struct smb_private {
        uint16_t fnum;
        struct smbcli_tree *tree;
+       DATA_BLOB session_key;
        const char *server_name;
+       bool dead;
 };
 
 
 /*
   tell the dcerpc layer that the transport is dead
 */
-static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
+static void pipe_dead(struct dcecli_connection *c, NTSTATUS status)
 {
-       c->transport.recv_data(c, NULL, status);
+       struct smb_private *smb = (struct smb_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);
+       }
 }
 
 
@@ -47,7 +69,7 @@ static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
    this holds the state of an in-flight call
 */
 struct smb_read_state {
-       struct dcerpc_connection *c;
+       struct dcecli_connection *c;
        struct smbcli_request *req;
        size_t received;
        DATA_BLOB data;
@@ -59,20 +81,22 @@ struct smb_read_state {
 */
 static void smb_read_callback(struct smbcli_request *req)
 {
+       struct dcecli_connection *c;
        struct smb_private *smb;
        struct smb_read_state *state;
        union smb_read *io;
        uint16_t frag_length;
        NTSTATUS status;
 
-       state = req->async.private;
-       smb = state->c->transport.private;
+       state = talloc_get_type(req->async.private_data, struct smb_read_state);
+       smb = talloc_get_type(state->c->transport.private_data, struct smb_private);
        io = state->io;
+       c = state->c;
 
        status = smb_raw_read_recv(state->req, io);
        if (NT_STATUS_IS_ERR(status)) {
-               pipe_dead(state->c, status);
                talloc_free(state);
+               pipe_dead(c, status);
                return;
        }
 
@@ -81,17 +105,19 @@ 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",
                         (int)state->received));
-               pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
                talloc_free(state);
+               pipe_dead(c, NT_STATUS_INFO_LENGTH_MISMATCH);
                return;
        }
 
        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;
+               data.length = state->received;
+               talloc_steal(state->c, data.data);
                talloc_free(state);
+               c->transport.recv_data(c, &data, NT_STATUS_OK);
                return;
        }
 
@@ -105,22 +131,22 @@ static void smb_read_callback(struct smbcli_request *req)
 
        state->req = smb_raw_read_send(smb->tree, io);
        if (state->req == NULL) {
-               pipe_dead(state->c, NT_STATUS_NO_MEMORY);
                talloc_free(state);
+               pipe_dead(c, NT_STATUS_NO_MEMORY);
                return;
        }
 
        state->req->async.fn = smb_read_callback;
-       state->req->async.private = state;
+       state->req->async.private_data = state;
 }
 
 /*
   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)
+static NTSTATUS send_read_request_continue(struct dcecli_connection *c, DATA_BLOB *blob)
 {
-       struct smb_private *smb = c->transport.private;
+       struct smb_private *smb = (struct smb_private *)c->transport.private_data;
        union smb_read *io;
        struct smb_read_state *state;
        struct smbcli_request *req;
@@ -150,11 +176,12 @@ static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLO
 
        io = state->io;
        io->generic.level = RAW_READ_READX;
-       io->readx.in.fnum = smb->fnum;
+       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) {
@@ -162,7 +189,7 @@ static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLO
        }
 
        req->async.fn = smb_read_callback;
-       req->async.private = state;
+       req->async.private_data = state;
 
        state->req = req;
 
@@ -173,8 +200,14 @@ static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLO
 /*
   trigger a read request from the server
 */
-static NTSTATUS send_read_request(struct dcerpc_connection *c)
+static NTSTATUS send_read_request(struct dcecli_connection *c)
 {
+       struct smb_private *smb = (struct smb_private *)c->transport.private_data;
+
+       if (smb->dead) {
+               return NT_STATUS_CONNECTION_DISCONNECTED;
+       }
+
        return send_read_request_continue(c, NULL);
 }
 
@@ -182,7 +215,7 @@ static NTSTATUS send_read_request(struct dcerpc_connection *c)
    this holds the state of an in-flight trans call
 */
 struct smb_trans_state {
-       struct dcerpc_connection *c;
+       struct dcecli_connection *c;
        struct smbcli_request *req;
        struct smb_trans2 *trans;
 };
@@ -192,8 +225,8 @@ struct smb_trans_state {
 */
 static void smb_trans_callback(struct smbcli_request *req)
 {
-       struct smb_trans_state *state = req->async.private;
-       struct dcerpc_connection *c = state->c;
+       struct smb_trans_state *state = (struct smb_trans_state *)req->async.private_data;
+       struct dcecli_connection *c = state->c;
        NTSTATUS status;
 
        status = smb_raw_trans_recv(req, state, state->trans);
@@ -204,8 +237,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;
        }
 
@@ -217,14 +252,15 @@ static void smb_trans_callback(struct smbcli_request *req)
 /*
   send a SMBtrans style request
 */
-static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
+static NTSTATUS smb_send_trans_request(struct dcecli_connection *c, DATA_BLOB *blob)
 {
-        struct smb_private *smb = c->transport.private;
+        struct smb_private *smb = (struct smb_private *)c->transport.private_data;
         struct smb_trans2 *trans;
         uint16_t setup[2];
        struct smb_trans_state *state;
+       uint16_t max_data;
 
-       state = talloc(smb, struct smb_trans_state);
+       state = talloc(c, struct smb_trans_state);
        if (state == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -239,8 +275,14 @@ static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *b
         setup[0] = TRANSACT_DCERPCCMD;
         setup[1] = smb->fnum;
 
+       if (c->srv_max_xmit_frag > 0) {
+               max_data = MIN(UINT16_MAX, c->srv_max_xmit_frag);
+       } else {
+               max_data = UINT16_MAX;
+       }
+
         trans->in.max_param = 0;
-        trans->in.max_data = smb_raw_max_trans_data(smb->tree, 0);
+        trans->in.max_data = max_data;
         trans->in.max_setup = 0;
         trans->in.setup_count = 2;
         trans->in.flags = 0;
@@ -255,7 +297,9 @@ 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;
+       state->req->async.private_data = state;
+
+       talloc_steal(state, state->req);
 
         return NT_STATUS_OK;
 }
@@ -265,31 +309,41 @@ static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *b
 */
 static void smb_write_callback(struct smbcli_request *req)
 {
-       struct dcerpc_connection *c = req->async.private;
+       struct dcecli_connection *c = (struct dcecli_connection *)req->async.private_data;
+       union smb_write io;
+       NTSTATUS status;
 
-       if (!NT_STATUS_IS_OK(req->status)) {
-               DEBUG(0,("dcerpc_smb: write callback error\n"));
-               pipe_dead(c, req->status);
-       }
+       ZERO_STRUCT(io);
+       io.generic.level = RAW_WRITE_WRITEX;
 
-       smbcli_request_destroy(req);
+       status = smb_raw_write_recv(req, &io);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("dcerpc_smb: write callback error: %s\n",
+                       nt_errstr(status)));
+               pipe_dead(c, status);
+       }
 }
 
 /* 
    send a packet to the server
 */
-static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, BOOL trigger_read)
+static NTSTATUS smb_send_request(struct dcecli_connection *c, DATA_BLOB *blob, 
+                                bool trigger_read)
 {
-       struct smb_private *smb = c->transport.private;
+       struct smb_private *smb = (struct smb_private *)c->transport.private_data;
        union smb_write io;
        struct smbcli_request *req;
 
+       if (!smb || 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;
@@ -306,94 +360,131 @@ static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, B
        }
 
        req->async.fn = smb_write_callback;
-       req->async.private = c;
-
-       if (trigger_read) {
-               send_read_request(c);
-       }
+       req->async.private_data = c;
 
        return NT_STATUS_OK;
 }
 
+
+static void free_request(struct smbcli_request *req)
+{
+       talloc_free(req);
+}
+
 /* 
    shutdown SMB pipe connection
 */
-static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c)
+static NTSTATUS smb_shutdown_pipe(struct dcecli_connection *c, NTSTATUS status)
 {
-       struct smb_private *smb = c->transport.private;
+       struct smb_private *smb = (struct smb_private *)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.fnum = smb->fnum;
+       io.close.in.file.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 = free_request;
+       }
 
        talloc_free(smb);
+       c->transport.private_data = NULL;
 
-       return NT_STATUS_OK;
+       return status;
 }
 
 /*
-  return SMB server name
+  return SMB server name (called name)
 */
-static const char *smb_peer_name(struct dcerpc_connection *c)
+static const char *smb_peer_name(struct dcecli_connection *c)
 {
-       struct smb_private *smb = c->transport.private;
+       struct smb_private *smb = (struct smb_private *)c->transport.private_data;
+       if (smb == NULL) return "";
        return smb->server_name;
 }
 
+/*
+  return remote name we make the actual connection (good for kerberos) 
+*/
+static const char *smb_target_hostname(struct dcecli_connection *c)
+{
+       struct smb_private *smb = talloc_get_type(c->transport.private_data, struct smb_private);
+       if (smb == NULL) return "";
+       return smbXcli_conn_remote_name(smb->tree->session->transport->conn);
+}
+
 /*
   fetch the user session key 
 */
-static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
+static NTSTATUS smb_session_key(struct dcecli_connection *c, DATA_BLOB *session_key)
 {
-       struct smb_private *smb = c->transport.private;
+       struct smb_private *smb = (struct smb_private *)c->transport.private_data;
+
+       if (smb == NULL) return NT_STATUS_CONNECTION_DISCONNECTED;
 
-       if (smb->tree->session->user_session_key.data) {
-               *session_key = smb->tree->session->user_session_key;
-               return NT_STATUS_OK;
+       if (smb->session_key.length == 0) {
+               return NT_STATUS_NO_USER_SESSION_KEY;
        }
-       return NT_STATUS_NO_USER_SESSION_KEY;
+
+       *session_key = smb->session_key;
+       return NT_STATUS_OK;
 }
 
 struct pipe_open_smb_state {
        union smb_open *open;
-       struct dcerpc_connection *c;
-       struct smbcli_request *req;
+       struct dcecli_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_connection *c
+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 dcecli_connection *c = p->conn;
+
+       /* if we don't have a binding on this pipe yet, then create one */
+       if (p->binding == NULL) {
+               NTSTATUS status;
+               const char *r = smbXcli_conn_remote_name(tree->session->transport->conn);
+               char *s;
+               SMB_ASSERT(r != NULL);
+               s = talloc_asprintf(p, "ncacn_np:%s", r);
+               if (s == NULL) return NULL;
+               status = dcerpc_parse_binding(p, s, &p->binding);
+               talloc_free(s);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return NULL;
+               }
+       }
 
-       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_smb_state);
-       if (state == NULL) goto failed;
+       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 (state->open == NULL) goto failed;
+       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.root_fid.fnum = 0;
        state->open->ntcreatex.in.access_mask = 
                SEC_STD_READ_CONTROL |
                SEC_FILE_WRITE_ATTRIBUTE |
@@ -419,75 +510,61 @@ struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_connection *c,
                (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;
+       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;
-
- 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 pipe_open_smb_state *state = talloc_get_type(req->async.private_data,
+                                           struct pipe_open_smb_state);
        struct composite_context *ctx = state->ctx;
-       struct dcerpc_connection *c = state->c;
+       struct dcecli_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 (!composite_is_ok(ctx)) return;
 
        /*
          fill in the transport methods
        */
-       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;
+       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;
        
        /* 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;
-       }
+       if (composite_nomem(smb, ctx)) return;
 
-       smb->fnum       = state->open->ntcreatex.out.fnum;
+       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 (smb->server_name == NULL) {
-               ctx->status = NT_STATUS_NO_MEMORY;
-               goto done;
+       smb->server_name= strupper_talloc(smb,
+               smbXcli_conn_remote_name(state->tree->session->transport->conn));
+       if (composite_nomem(smb->server_name, ctx)) return;
+       smb->dead       = false;
+
+       ctx->status = smbXcli_session_application_key(state->tree->session->smbXcli,
+                                                     smb, &smb->session_key);
+       if (NT_STATUS_EQUAL(ctx->status, NT_STATUS_NO_USER_SESSION_KEY)) {
+               smb->session_key = data_blob_null;
+               ctx->status = NT_STATUS_OK;
        }
-       c->transport.private = smb;
+       if (!composite_is_ok(ctx)) return;
 
-       ctx->status = NT_STATUS_OK;
-       ctx->state = COMPOSITE_STATE_DONE;
+       c->transport.private_data = smb;
 
- 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);
-       }
+       composite_done(ctx);
 }
 
 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
@@ -497,11 +574,11 @@ NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
        return status;
 }
 
-NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_connection *c,
+_PUBLIC_ 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(c, tree,
+       struct composite_context *ctx = dcerpc_pipe_open_smb_send(p, tree,
                                                                  pipe_name);
        return dcerpc_pipe_open_smb_recv(ctx);
 }
@@ -509,13 +586,14 @@ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_connection *c,
 /*
   return the SMB tree used for a dcerpc over SMB pipe
 */
-struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
+_PUBLIC_ struct smbcli_tree *dcerpc_smb_tree(struct dcecli_connection *c)
 {
-       struct smb_private *smb = c->transport.private;
+       struct smb_private *smb;
 
-       if (c->transport.transport != NCACN_NP) {
-               return NULL;
-       }
+       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;
 }