s4:librpc/rpc: remove unused dcerpc_secondary_smb_send/recv()
[metze/samba/wip.git] / source4 / librpc / rpc / dcerpc_smb.c
index 6835213cf4968bc36cabedddfd536171b7f0b60a..1487bde75a5890f213cc3da8f9970bc7092770f1 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 "system/filesys.h"
+#include <tevent.h>
+#include "lib/tsocket/tsocket.h"
+#include "libcli/smb/smb_constants.h"
+#include "libcli/smb/smbXcli_base.h"
+#include "libcli/smb/tstream_smbXcli_np.h"
 #include "libcli/raw/libcliraw.h"
-#include "librpc/gen_ndr/ndr_security.h"
+#include "libcli/smb2/smb2.h"
+#include "librpc/rpc/dcerpc.h"
+#include "librpc/rpc/dcerpc_proto.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_connection *c, NTSTATUS status)
-{
-       c->transport.recv_data(c, NULL, status);
-}
-
-
-/* 
-   this holds the state of an in-flight call
-*/
-struct smb_read_state {
-       struct dcerpc_connection *c;
-       struct smbcli_request *req;
-       size_t received;
-       DATA_BLOB data;
-       union smb_read *io;
+       DATA_BLOB session_key;
 };
 
 /*
-  called when a read request has completed
+  fetch the user session key 
 */
-static void smb_read_callback(struct smbcli_request *req)
+static NTSTATUS smb_session_key(struct dcecli_connection *c, DATA_BLOB *session_key)
 {
-       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;
-       io = state->io;
-
-       status = smb_raw_read_recv(state->req, io);
-       if (NT_STATUS_IS_ERR(status)) {
-               pipe_dead(state->c, status);
-               talloc_free(state);
-               return;
-       }
+       struct smb_private *smb = talloc_get_type_abort(
+               c->transport.private_data, struct smb_private);
 
-       state->received += io->readx.out.nread;
+       if (smb == NULL) return NT_STATUS_CONNECTION_DISCONNECTED;
 
-       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);
-               return;
+       if (smb->session_key.length == 0) {
+               return NT_STATUS_NO_USER_SESSION_KEY;
        }
 
-       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);
-               talloc_free(state);
-               return;
-       }
-
-       /* initiate another read request, as we only got part of a fragment */
-       state->data.data = talloc_realloc(state, state->data.data, uint8_t, frag_length);
-
-       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->c, NT_STATUS_NO_MEMORY);
-               talloc_free(state);
-               return;
-       }
-
-       state->req->async.fn = smb_read_callback;
-       state->req->async.private = 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)
-{
-       struct smb_private *smb = c->transport.private;
-       union smb_read *io;
-       struct smb_read_state *state;
-       struct smbcli_request *req;
-
-       state = talloc(smb, struct smb_read_state);
-       if (state == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       state->c = c;
-       if (blob == NULL) {
-               state->received = 0;
-               state->data = data_blob_talloc(state, NULL, 0x2000);
-       } else {
-               uint32_t frag_length = blob->length>=16?
-                       dcerpc_get_frag_length(blob):0x2000;
-               state->received = blob->length;
-               state->data = data_blob_talloc(state, NULL, frag_length);
-               if (!state->data.data) {
-                       talloc_free(state);
-                       return NT_STATUS_NO_MEMORY;
-               }
-               memcpy(state->data.data, blob->data, blob->length);
-       }
-
-       state->io = talloc(state, union smb_read);
-
-       io = state->io;
-       io->generic.level = RAW_READ_READX;
-       io->readx.in.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.out.data = state->data.data + state->received;
-       req = smb_raw_read_send(smb->tree, io);
-       if (req == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       req->async.fn = smb_read_callback;
-       req->async.private = state;
-
-       state->req = req;
-
+       *session_key = smb->session_key;
        return NT_STATUS_OK;
 }
 
+struct dcerpc_pipe_open_smb_state {
+       struct dcecli_connection *c;
+       struct composite_context *ctx;
 
-/*
-  trigger a read request from the server
-*/
-static NTSTATUS send_read_request(struct dcerpc_connection *c)
-{
-       return send_read_request_continue(c, NULL);
-}
+       const char *fname;
 
-/* 
-   this holds the state of an in-flight trans call
-*/
-struct smb_trans_state {
-       struct dcerpc_connection *c;
-       struct smbcli_request *req;
-       struct smb_trans2 *trans;
+       struct smb_private *smb;
 };
 
-/*
-  called when a trans request has completed
-*/
-static void smb_trans_callback(struct smbcli_request *req)
-{
-       struct smb_trans_state *state = req->async.private;
-       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(c, status);
-               return;
-       }
-
-       if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
-               c->transport.recv_data(c, &state->trans->out.data, NT_STATUS_OK);
-               talloc_free(state);
-               return;
-       }
-
-       /* there is more to receive - setup a readx */
-       send_read_request_continue(c, &state->trans->out.data);
-       talloc_free(state);
-}
+static void dcerpc_pipe_open_smb_done(struct tevent_req *subreq);
 
-/*
-  send a SMBtrans style request
-*/
-static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
+struct composite_context *dcerpc_pipe_open_smb_send(struct dcecli_connection *c,
+                                               struct smbXcli_conn *conn,
+                                               struct smbXcli_session *session,
+                                               struct smbXcli_tcon *tcon,
+                                               uint32_t timeout_msec,
+                                               const char *pipe_name)
 {
-        struct smb_private *smb = c->transport.private;
-        struct smb_trans2 *trans;
-        uint16_t setup[2];
-       struct smb_trans_state *state;
+       struct composite_context *ctx;
+       struct dcerpc_pipe_open_smb_state *state;
+       uint16_t pid = 0;
+       struct tevent_req *subreq;
 
-       state = talloc(smb, struct smb_trans_state);
-       if (state == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
+       ctx = composite_create(c, c->event_ctx);
+       if (ctx == NULL) return NULL;
 
-       state->c = c;
-       state->trans = talloc(state, struct smb_trans2);
-       trans = state->trans;
+       state = talloc(ctx, struct dcerpc_pipe_open_smb_state);
+       if (composite_nomem(state, ctx)) return ctx;
+       ctx->private_data = state;
 
-        trans->in.data = *blob;
-        trans->in.params = data_blob(NULL, 0);
-        
-        setup[0] = TRANSACT_DCERPCCMD;
-        setup[1] = smb->fnum;
-
-        trans->in.max_param = 0;
-        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;
-        trans->in.timeout = 0;
-        trans->in.setup = setup;
-        trans->in.trans_name = "\\PIPE\\";
-
-        state->req = smb_raw_trans_send(smb->tree, trans);
-       if (state->req == NULL) {
-               talloc_free(state);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       state->req->async.fn = smb_trans_callback;
-       state->req->async.private = state;
-
-        return NT_STATUS_OK;
-}
-
-/*
-  called when a write request has completed
-*/
-static void smb_write_callback(struct smbcli_request *req)
-{
-       struct dcerpc_connection *c = req->async.private;
+       state->c = c;
+       state->ctx = ctx;
 
-       if (!NT_STATUS_IS_OK(req->status)) {
-               DEBUG(0,("dcerpc_smb: write callback error\n"));
-               pipe_dead(c, req->status);
+       if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) || 
+           (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
+               pipe_name += 6;
        }
-
-       smbcli_request_destroy(req);
-}
-
-/* 
-   send a packet to the server
-*/
-static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, BOOL trigger_read)
-{
-       struct smb_private *smb = c->transport.private;
-       union smb_write io;
-       struct smbcli_request *req;
-
-       if (trigger_read) {
-               return smb_send_trans_request(c, blob);
+       if ((strncasecmp(pipe_name, "/", 1) == 0) ||
+           (strncasecmp(pipe_name, "\\", 1) == 0)) {
+               pipe_name += 1;
        }
+       state->fname = talloc_strdup(state, pipe_name);
+       if (composite_nomem(state->fname, ctx)) return ctx;
 
-       io.generic.level = RAW_WRITE_WRITEX;
-       io.writex.in.fnum = smb->fnum;
-       io.writex.in.offset = 0;
-       io.writex.in.wmode = PIPE_START_MESSAGE;
-       io.writex.in.remaining = blob->length;
-       io.writex.in.count = blob->length;
-       io.writex.in.data = blob->data;
+       state->smb = talloc_zero(state, struct smb_private);
+       if (composite_nomem(state->smb, ctx)) return ctx;
 
-       /* 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;
+       state->c->server_name = strupper_talloc(state->c,
+               smbXcli_conn_remote_name(conn));
+       if (composite_nomem(state->c->server_name, ctx)) return ctx;
 
-       req = smb_raw_write_send(smb->tree, &io);
-       if (req == NULL) {
-               return NT_STATUS_NO_MEMORY;
+       ctx->status = smbXcli_session_application_key(session,
+                                                     state->smb,
+                                                     &state->smb->session_key);
+       if (NT_STATUS_EQUAL(ctx->status, NT_STATUS_NO_USER_SESSION_KEY)) {
+               state->smb->session_key = data_blob_null;
+               ctx->status = NT_STATUS_OK;
        }
+       if (!composite_is_ok(ctx)) return ctx;
 
-       req->async.fn = smb_write_callback;
-       req->async.private = c;
-
-       if (trigger_read) {
-               send_read_request(c);
-       }
-
-       return NT_STATUS_OK;
-}
-
-/* 
-   shutdown SMB pipe connection
-*/
-static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c)
-{
-       struct smb_private *smb = c->transport.private;
-       union smb_close io;
-
-       /* maybe we're still starting up */
-       if (!smb) return NT_STATUS_OK;
-
-       io.close.level = RAW_CLOSE_CLOSE;
-       io.close.in.fnum = smb->fnum;
-       io.close.in.write_time = 0;
-       smb_raw_close(smb->tree, &io);
-
-       talloc_free(smb);
-
-       return NT_STATUS_OK;
-}
-
-/*
-  return SMB server name
-*/
-static const char *smb_peer_name(struct dcerpc_connection *c)
-{
-       struct smb_private *smb = c->transport.private;
-       return smb->server_name;
-}
-
-/*
-  fetch the user session key 
-*/
-static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
-{
-       struct smb_private *smb = c->transport.private;
+       subreq = tstream_smbXcli_np_open_send(state, c->event_ctx,
+                                             conn, session, tcon, pid,
+                                             timeout_msec, state->fname);
+       if (composite_nomem(subreq, ctx)) return ctx;
+       tevent_req_set_callback(subreq, dcerpc_pipe_open_smb_done, state);
 
-       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;
+       return ctx;
 }
 
-/* 
-   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)
+static void dcerpc_pipe_open_smb_done(struct tevent_req *subreq)
 {
-       struct smb_private *smb;
-        NTSTATUS status;
-       union smb_open io;
-       char *pipe_name_talloc;
-
-       if (!strncasecmp(pipe_name, "/pipe/", 6) || 
-           !strncasecmp(pipe_name, "\\pipe\\", 6)) {
-               pipe_name += 6;
-       }
-
-       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 = 
-               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 = 
-               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;
-
-       status = smb_raw_open(tree, tree, &io);
-
-       talloc_free(pipe_name_talloc);
-
-       NT_STATUS_NOT_OK_RETURN(status);
+       struct dcerpc_pipe_open_smb_state *state =
+               tevent_req_callback_data(subreq,
+               struct dcerpc_pipe_open_smb_state);
+       struct composite_context *ctx = state->ctx;
+       struct dcecli_connection *c = state->c;
+
+       ctx->status = tstream_smbXcli_np_open_recv(subreq,
+                                                  state->smb,
+                                                  &state->c->transport.stream);
+       TALLOC_FREE(subreq);
+       if (!composite_is_ok(ctx)) return;
+
+       state->c->transport.write_queue =
+               tevent_queue_create(state->c, "dcerpc_smb write queue");
+       if (composite_nomem(state->c->transport.write_queue, 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.transport       = NCACN_NP;
+       c->transport.private_data    = NULL;
+
+       /*
+        * Windows uses 4280 for ncacn_np,
+        * so we also use it, this is what our
+        * tstream_smbXcli_np code relies on.
+        */
+       c->srv_max_xmit_frag = 4280;
+       c->srv_max_recv_frag = 4280;
 
-       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);
-       NT_STATUS_HAVE_NO_MEMORY(smb);
+       c->transport.private_data = talloc_move(c, &state->smb);
 
-       smb->fnum       = io.ntcreatex.out.fnum;
-       smb->tree       = tree;
-       smb->server_name= strupper_talloc(smb, tree->session->transport->socket->hostname);
-       NT_STATUS_HAVE_NO_MEMORY(smb->server_name);
-
-       c->transport.private = smb;
-
-        return NT_STATUS_OK;
+       composite_done(ctx);
 }
 
-/*
-  return the SMB tree used for a dcerpc over SMB pipe
-*/
-struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
+NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
 {
-       struct smb_private *smb = c->transport.private;
-
-       if (c->transport.transport != NCACN_NP) {
-               return NULL;
-       }
-
-       return smb->tree;
+       NTSTATUS status = composite_wait(c);
+       talloc_free(c);
+       return status;
 }