Merge branch 'master' of ssh://git.samba.org/data/git/samba into wspp-schema
[samba.git] / source3 / smbd / ipc.c
index c4a12ab531b50d50d3a2998ae69b0e9e7885f72b..f20c8512975c573c6987896ce42fb36630aba271 100644 (file)
@@ -5,20 +5,19 @@
 
    SMB Version handling
    Copyright (C) John H Terpstra 1995-1998
-   
+
    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,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    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/>.
    */
 /*
    This file handles the named pipe and mailslot calls
    */
 
 #include "includes.h"
-
-extern int max_send;
-
-extern fstring local_machine;
+#include "smbd/globals.h"
 
 #define NERR_notsupported 50
 
-extern int smb_read_error;
+static void api_no_reply(connection_struct *conn, struct smb_request *req);
 
 /*******************************************************************
  copies parameters and data, as needed, into the smb buffer
@@ -49,7 +45,7 @@ static void copy_trans_params_and_data(char *outbuf, int align,
                                char *rparam, int param_offset, int param_len,
                                char *rdata, int data_offset, int data_len)
 {
-       char *copy_into = smb_buf(outbuf)+1;
+       char *copy_into = smb_buf(outbuf);
 
        if(param_len < 0)
                param_len = 0;
@@ -57,14 +53,24 @@ static void copy_trans_params_and_data(char *outbuf, int align,
        if(data_len < 0)
                data_len = 0;
 
-       DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d]\n",
+       DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d] (align %d)\n",
                        param_offset, param_offset + param_len,
-                       data_offset , data_offset  + data_len));
+                       data_offset , data_offset  + data_len,
+                       align));
+
+       *copy_into = '\0';
+
+       copy_into += 1;
 
        if (param_len)
                memcpy(copy_into, &rparam[param_offset], param_len);
 
-       copy_into += param_len + align;
+       copy_into += param_len;
+       if (align) {
+               memset(copy_into, '\0', align);
+       }
+
+       copy_into += align;
 
        if (data_len )
                memcpy(copy_into, &rdata[data_offset], data_len);
@@ -74,10 +80,11 @@ static void copy_trans_params_and_data(char *outbuf, int align,
  Send a trans reply.
  ****************************************************************************/
 
-void send_trans_reply(char *outbuf,
-                               char *rparam, int rparam_len,
-                               char *rdata, int rdata_len,
-                               BOOL buffer_too_large)
+void send_trans_reply(connection_struct *conn,
+                     struct smb_request *req,
+                     char *rparam, int rparam_len,
+                     char *rdata, int rdata_len,
+                     bool buffer_too_large)
 {
        int this_ldata,this_lparam;
        int tot_data_sent = 0;
@@ -95,37 +102,53 @@ void send_trans_reply(char *outbuf,
 
        align = ((this_lparam)%4);
 
-       if (buffer_too_large) {
-               ERROR_NT(STATUS_BUFFER_OVERFLOW);
-       }
+       reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
 
-       set_message(outbuf,10,1+align+this_ldata+this_lparam,True);
+       /*
+        * We might have SMBtranss in req which was transferred to the outbuf,
+        * fix that.
+        */
+       SCVAL(req->outbuf, smb_com, SMBtrans);
+
+       copy_trans_params_and_data((char *)req->outbuf, align,
+                               rparam, tot_param_sent, this_lparam,
+                               rdata, tot_data_sent, this_ldata);
+
+       SSVAL(req->outbuf,smb_vwv0,lparam);
+       SSVAL(req->outbuf,smb_vwv1,ldata);
+       SSVAL(req->outbuf,smb_vwv3,this_lparam);
+       SSVAL(req->outbuf,smb_vwv4,
+             smb_offset(smb_buf(req->outbuf)+1, req->outbuf));
+       SSVAL(req->outbuf,smb_vwv5,0);
+       SSVAL(req->outbuf,smb_vwv6,this_ldata);
+       SSVAL(req->outbuf,smb_vwv7,
+             smb_offset(smb_buf(req->outbuf)+1+this_lparam+align,
+                        req->outbuf));
+       SSVAL(req->outbuf,smb_vwv8,0);
+       SSVAL(req->outbuf,smb_vwv9,0);
 
-       copy_trans_params_and_data(outbuf, align,
-                                                               rparam, tot_param_sent, this_lparam,
-                                                               rdata, tot_data_sent, this_ldata);
+       if (buffer_too_large) {
+               error_packet_set((char *)req->outbuf, ERRDOS, ERRmoredata,
+                                STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
+       }
 
-       SSVAL(outbuf,smb_vwv0,lparam);
-       SSVAL(outbuf,smb_vwv1,ldata);
-       SSVAL(outbuf,smb_vwv3,this_lparam);
-       SSVAL(outbuf,smb_vwv4,smb_offset(smb_buf(outbuf)+1,outbuf));
-       SSVAL(outbuf,smb_vwv5,0);
-       SSVAL(outbuf,smb_vwv6,this_ldata);
-       SSVAL(outbuf,smb_vwv7,smb_offset(smb_buf(outbuf)+1+this_lparam+align,outbuf));
-       SSVAL(outbuf,smb_vwv8,0);
-       SSVAL(outbuf,smb_vwv9,0);
+       show_msg((char *)req->outbuf);
+       if (!srv_send_smb(smbd_server_fd(), (char *)req->outbuf,
+                         IS_CONN_ENCRYPTED(conn), &req->pcd)) {
+               exit_server_cleanly("send_trans_reply: srv_send_smb failed.");
+       }
 
-       show_msg(outbuf);
-       if (!send_smb(smbd_server_fd(),outbuf))
-               exit_server("send_trans_reply: send_smb failed.");
+       TALLOC_FREE(req->outbuf);
 
        tot_data_sent = this_ldata;
        tot_param_sent = this_lparam;
 
        while (tot_data_sent < ldata || tot_param_sent < lparam)
        {
-               this_lparam = MIN(lparam-tot_param_sent, max_send - 500); /* hack */
-               this_ldata  = MIN(ldata -tot_data_sent, max_send - (500+this_lparam));
+               this_lparam = MIN(lparam-tot_param_sent,
+                                 max_send - 500); /* hack */
+               this_ldata  = MIN(ldata -tot_data_sent,
+                                 max_send - (500+this_lparam));
 
                if(this_lparam < 0)
                        this_lparam = 0;
@@ -135,26 +158,45 @@ void send_trans_reply(char *outbuf,
 
                align = (this_lparam%4);
 
-               set_message(outbuf,10,1+this_ldata+this_lparam+align,False);
-
-               copy_trans_params_and_data(outbuf, align,
-                                                                       rparam, tot_param_sent, this_lparam,
-                                                                       rdata, tot_data_sent, this_ldata);
-
-               SSVAL(outbuf,smb_vwv3,this_lparam);
-               SSVAL(outbuf,smb_vwv4,smb_offset(smb_buf(outbuf)+1,outbuf));
-               SSVAL(outbuf,smb_vwv5,tot_param_sent);
-               SSVAL(outbuf,smb_vwv6,this_ldata);
-               SSVAL(outbuf,smb_vwv7,smb_offset(smb_buf(outbuf)+1+this_lparam+align,outbuf));
-               SSVAL(outbuf,smb_vwv8,tot_data_sent);
-               SSVAL(outbuf,smb_vwv9,0);
+               reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
+
+               /*
+                * We might have SMBtranss in req which was transferred to the
+                * outbuf, fix that.
+                */
+               SCVAL(req->outbuf, smb_com, SMBtrans);
+
+               copy_trans_params_and_data((char *)req->outbuf, align,
+                                          rparam, tot_param_sent, this_lparam,
+                                          rdata, tot_data_sent, this_ldata);
+
+               SSVAL(req->outbuf,smb_vwv3,this_lparam);
+               SSVAL(req->outbuf,smb_vwv4,
+                     smb_offset(smb_buf(req->outbuf)+1,req->outbuf));
+               SSVAL(req->outbuf,smb_vwv5,tot_param_sent);
+               SSVAL(req->outbuf,smb_vwv6,this_ldata);
+               SSVAL(req->outbuf,smb_vwv7,
+                     smb_offset(smb_buf(req->outbuf)+1+this_lparam+align,
+                                req->outbuf));
+               SSVAL(req->outbuf,smb_vwv8,tot_data_sent);
+               SSVAL(req->outbuf,smb_vwv9,0);
+
+               if (buffer_too_large) {
+                       error_packet_set((char *)req->outbuf,
+                                        ERRDOS, ERRmoredata,
+                                        STATUS_BUFFER_OVERFLOW,
+                                        __LINE__, __FILE__);
+               }
 
-               show_msg(outbuf);
-               if (!send_smb(smbd_server_fd(),outbuf))
-                       exit_server("send_trans_reply: send_smb failed.");
+               show_msg((char *)req->outbuf);
+               if (!srv_send_smb(smbd_server_fd(), (char *)req->outbuf,
+                                 IS_CONN_ENCRYPTED(conn), &req->pcd))
+                       exit_server_cleanly("send_trans_reply: srv_send_smb "
+                                           "failed.");
 
                tot_data_sent  += this_ldata;
                tot_param_sent += this_lparam;
+               TALLOC_FREE(req->outbuf);
        }
 }
 
@@ -162,49 +204,154 @@ void send_trans_reply(char *outbuf,
  Start the first part of an RPC reply which began with an SMBtrans request.
 ****************************************************************************/
 
-static BOOL api_rpc_trans_reply(char *outbuf, smb_np_struct *p)
+struct dcerpc_cmd_state {
+       struct fake_file_handle *handle;
+       uint8_t *data;
+       size_t num_data;
+       size_t max_read;
+};
+
+static void api_dcerpc_cmd_write_done(struct tevent_req *subreq);
+static void api_dcerpc_cmd_read_done(struct tevent_req *subreq);
+
+static void api_dcerpc_cmd(connection_struct *conn, struct smb_request *req,
+                          files_struct *fsp, uint8_t *data, size_t length,
+                          size_t max_read)
 {
-       BOOL is_data_outstanding;
-       char *rdata = malloc(p->max_trans_reply);
-       int data_len;
+       struct tevent_req *subreq;
+       struct dcerpc_cmd_state *state;
 
-       if(rdata == NULL) {
-               DEBUG(0,("api_rpc_trans_reply: malloc fail.\n"));
-               return False;
+       if (!fsp_is_np(fsp)) {
+               api_no_reply(conn, req);
+               return;
        }
 
-       if((data_len = read_from_pipe( p, rdata, p->max_trans_reply,
-                                       &is_data_outstanding)) < 0) {
-               SAFE_FREE(rdata);
-               return False;
+       state = talloc(req, struct dcerpc_cmd_state);
+       if (state == NULL) {
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
+               return;
        }
+       req->async_priv = state;
 
-       send_trans_reply(outbuf, NULL, 0, rdata, data_len, is_data_outstanding);
+       state->handle = fsp->fake_file_handle;
 
-       SAFE_FREE(rdata);
-       return True;
+       /*
+        * This memdup severely sucks. But doing it properly essentially means
+        * to rewrite lanman.c, something which I don't really want to do now.
+        */
+       state->data = (uint8_t *)talloc_memdup(state, data, length);
+       if (state->data == NULL) {
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
+               return;
+       }
+       state->num_data = length;
+       state->max_read = max_read;
+
+       subreq = np_write_send(state, smbd_event_context(), state->handle,
+                              state->data, length);
+       if (subreq == NULL) {
+               TALLOC_FREE(state);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
+               return;
+       }
+       tevent_req_set_callback(subreq, api_dcerpc_cmd_write_done,
+                               talloc_move(conn, &req));
+}
+
+static void api_dcerpc_cmd_write_done(struct tevent_req *subreq)
+{
+       struct smb_request *req = tevent_req_callback_data(
+               subreq, struct smb_request);
+       struct dcerpc_cmd_state *state = talloc_get_type_abort(
+               req->async_priv, struct dcerpc_cmd_state);
+       NTSTATUS status;
+       ssize_t nwritten = -1;
+
+       status = np_write_recv(subreq, &nwritten);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status) || (nwritten != state->num_data)) {
+               DEBUG(10, ("Could not write to pipe: %s (%d/%d)\n",
+                          nt_errstr(status), (int)state->num_data,
+                          (int)nwritten));
+               reply_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
+               goto send;
+       }
+
+       state->data = TALLOC_REALLOC_ARRAY(state, state->data, uint8_t,
+                                          state->max_read);
+       if (state->data == NULL) {
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
+               goto send;
+       }
+
+       subreq = np_read_send(req->conn, smbd_event_context(),
+                             state->handle, state->data, state->max_read);
+       if (subreq == NULL) {
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
+               goto send;
+       }
+       tevent_req_set_callback(subreq, api_dcerpc_cmd_read_done, req);
+       return;
+
+ send:
+       if (!srv_send_smb(
+                   smbd_server_fd(), (char *)req->outbuf,
+                   IS_CONN_ENCRYPTED(req->conn) || req->encrypted,
+                   &req->pcd)) {
+               exit_server_cleanly("construct_reply: srv_send_smb failed.");
+       }
+       TALLOC_FREE(req);
+}
+
+static void api_dcerpc_cmd_read_done(struct tevent_req *subreq)
+{
+       struct smb_request *req = tevent_req_callback_data(
+               subreq, struct smb_request);
+       struct dcerpc_cmd_state *state = talloc_get_type_abort(
+               req->async_priv, struct dcerpc_cmd_state);
+       NTSTATUS status;
+       ssize_t nread;
+       bool is_data_outstanding;
+
+       status = np_read_recv(subreq, &nread, &is_data_outstanding);
+       TALLOC_FREE(subreq);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(10, ("Could not read from to pipe: %s\n",
+                          nt_errstr(status)));
+               reply_nterror(req, status);
+
+               if (!srv_send_smb(smbd_server_fd(), (char *)req->outbuf,
+                                 IS_CONN_ENCRYPTED(req->conn)
+                                 ||req->encrypted, &req->pcd)) {
+                       exit_server_cleanly("construct_reply: srv_send_smb "
+                                           "failed.");
+               }
+               TALLOC_FREE(req);
+               return;
+       }
+
+       send_trans_reply(req->conn, req, NULL, 0, (char *)state->data, nread,
+                        is_data_outstanding);
+       TALLOC_FREE(req);
 }
 
 /****************************************************************************
  WaitNamedPipeHandleState 
 ****************************************************************************/
 
-static BOOL api_WNPHS(char *outbuf, smb_np_struct *p, char *param, int param_len)
+static void api_WNPHS(connection_struct *conn, struct smb_request *req,
+                     struct files_struct *fsp, char *param, int param_len)
 {
-       uint16 priority;
-
-       if (!param || param_len < 2)
-               return False;
+       if (!param || param_len < 2) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               return;
+       }
 
-       priority = SVAL(param,0);
-       DEBUG(4,("WaitNamedPipeHandleState priority %x\n", priority));
+       DEBUG(4,("WaitNamedPipeHandleState priority %x\n",
+                (int)SVAL(param,0)));
 
-       if (wait_rpc_pipe_hnd_state(p, priority)) {
-               /* now send the reply */
-               send_trans_reply(outbuf, NULL, 0, NULL, 0, False);
-               return True;
-       }
-       return False;
+       send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
 }
 
 
@@ -212,22 +359,17 @@ static BOOL api_WNPHS(char *outbuf, smb_np_struct *p, char *param, int param_len
  SetNamedPipeHandleState 
 ****************************************************************************/
 
-static BOOL api_SNPHS(char *outbuf, smb_np_struct *p, char *param, int param_len)
+static void api_SNPHS(connection_struct *conn, struct smb_request *req,
+                     struct files_struct *fsp, char *param, int param_len)
 {
-       uint16 id;
-
-       if (!param || param_len < 2)
-               return False;
+       if (!param || param_len < 2) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               return;
+       }
 
-       id = SVAL(param,0);
-       DEBUG(4,("SetNamedPipeHandleState to code %x\n", id));
+       DEBUG(4,("SetNamedPipeHandleState to code %x\n", (int)SVAL(param,0)));
 
-       if (set_rpc_pipe_hnd_state(p, id)) {
-               /* now send the reply */
-               send_trans_reply(outbuf, NULL, 0, NULL, 0, False);
-               return True;
-       }
-       return False;
+       send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
 }
 
 
@@ -235,7 +377,7 @@ static BOOL api_SNPHS(char *outbuf, smb_np_struct *p, char *param, int param_len
  When no reply is generated, indicate unsupported.
  ****************************************************************************/
 
-static BOOL api_no_reply(char *outbuf, int max_rdata_len)
+static void api_no_reply(connection_struct *conn, struct smb_request *req)
 {
        char rparam[4];
 
@@ -246,21 +388,22 @@ static BOOL api_no_reply(char *outbuf, int max_rdata_len)
        DEBUG(3,("Unsupported API fd command\n"));
 
        /* now send the reply */
-       send_trans_reply(outbuf, rparam, 4, NULL, 0, False);
+       send_trans_reply(conn, req, rparam, 4, NULL, 0, False);
 
-       return -1;
+       return;
 }
 
 /****************************************************************************
  Handle remote api calls delivered to a named pipe already opened.
  ****************************************************************************/
 
-static int api_fd_reply(connection_struct *conn,uint16 vuid,char *outbuf,
-                       uint16 *setup,char *data,char *params,
-                       int suwcnt,int tdscnt,int tpscnt,int mdrcnt,int mprcnt)
+static void api_fd_reply(connection_struct *conn, uint16 vuid,
+                        struct smb_request *req,
+                        uint16 *setup, uint8_t *data, char *params,
+                        int suwcnt, int tdscnt, int tpscnt,
+                        int mdrcnt, int mprcnt)
 {
-       BOOL reply = False;
-       smb_np_struct *p = NULL;
+       struct files_struct *fsp;
        int pnum;
        int subcommand;
 
@@ -269,302 +412,460 @@ static int api_fd_reply(connection_struct *conn,uint16 vuid,char *outbuf,
        /* First find out the name of this file. */
        if (suwcnt != 2) {
                DEBUG(0,("Unexpected named pipe transaction.\n"));
-               return(-1);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               return;
        }
 
        /* Get the file handle and hence the file name. */
        /* 
         * NB. The setup array has already been transformed
-        * via SVAL and so is in gost byte order.
+        * via SVAL and so is in host byte order.
         */
        pnum = ((int)setup[1]) & 0xFFFF;
        subcommand = ((int)setup[0]) & 0xFFFF;
 
-       if(!(p = get_rpc_pipe(pnum))) {
+       fsp = file_fsp(req, pnum);
+
+       if (!fsp_is_np(fsp)) {
+               if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
+                       /* Win9x does this call with a unicode pipe name, not a pnum. */
+                       /* Just return success for now... */
+                       DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
+                       send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
+                       return;
+               }
+
                DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
-               return api_no_reply(outbuf, mdrcnt);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
+               return;
        }
 
-       DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)", subcommand, p->name, pnum));
+       if (vuid != fsp->vuid) {
+               DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
+                         "expected %d\n", pnum, vuid, fsp->vuid));
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
+               return;
+       }
 
-       /* record maximum data length that can be transmitted in an SMBtrans */
-       p->max_trans_reply = mdrcnt;
+       DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n",
+                subcommand, fsp->fsp_name, pnum));
 
-       DEBUG(10,("api_fd_reply: p:%p max_trans_reply: %d\n", p, p->max_trans_reply));
+       DEBUG(10, ("api_fd_reply: p:%p max_trans_reply: %d\n", fsp, mdrcnt));
 
        switch (subcommand) {
-       case TRANSACT_DCERPCCMD:
+       case TRANSACT_DCERPCCMD: {
                /* dce/rpc command */
-               reply = write_to_pipe(p, data, tdscnt);
-               if (reply)
-                       reply = api_rpc_trans_reply(outbuf, p);
+               api_dcerpc_cmd(conn, req, fsp, (uint8_t *)data, tdscnt,
+                              mdrcnt);
                break;
+       }
        case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
                /* Wait Named Pipe Handle state */
-               reply = api_WNPHS(outbuf, p, params, tpscnt);
+               api_WNPHS(conn, req, fsp, params, tpscnt);
                break;
        case TRANSACT_SETNAMEDPIPEHANDLESTATE:
                /* Set Named Pipe Handle state */
-               reply = api_SNPHS(outbuf, p, params, tpscnt);
+               api_SNPHS(conn, req, fsp, params, tpscnt);
                break;
+       default:
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               return;
        }
-
-       if (!reply)
-               return api_no_reply(outbuf, mdrcnt);
-
-       return -1;
 }
 
 /****************************************************************************
-  handle named pipe commands
-  ****************************************************************************/
-static int named_pipe(connection_struct *conn,uint16 vuid, char *outbuf,char *name,
-                     uint16 *setup,char *data,char *params,
-                     int suwcnt,int tdscnt,int tpscnt,
-                     int msrcnt,int mdrcnt,int mprcnt)
+ Handle named pipe commands.
+****************************************************************************/
+
+static void named_pipe(connection_struct *conn, uint16 vuid,
+                      struct smb_request *req,
+                      const char *name, uint16 *setup,
+                      char *data, char *params,
+                      int suwcnt, int tdscnt,int tpscnt,
+                      int msrcnt, int mdrcnt, int mprcnt)
 {
        DEBUG(3,("named pipe command on <%s> name\n", name));
 
-       if (strequal(name,"LANMAN"))
-               return api_reply(conn,vuid,outbuf,data,params,tdscnt,tpscnt,mdrcnt,mprcnt);
+       if (strequal(name,"LANMAN")) {
+               api_reply(conn, vuid, req,
+                         data, params,
+                         tdscnt, tpscnt,
+                         mdrcnt, mprcnt);
+               return;
+       }
 
        if (strequal(name,"WKSSVC") ||
            strequal(name,"SRVSVC") ||
            strequal(name,"WINREG") ||
            strequal(name,"SAMR") ||
-           strequal(name,"LSARPC"))
-       {
+           strequal(name,"LSARPC")) {
+
                DEBUG(4,("named pipe command from Win95 (wow!)\n"));
-               return api_fd_reply(conn,vuid,outbuf,setup,data,params,suwcnt,tdscnt,tpscnt,mdrcnt,mprcnt);
+
+               api_fd_reply(conn, vuid, req,
+                            setup, (uint8_t *)data, params,
+                            suwcnt, tdscnt, tpscnt,
+                            mdrcnt, mprcnt);
+               return;
        }
 
-       if (strlen(name) < 1)
-               return api_fd_reply(conn,vuid,outbuf,setup,data,params,suwcnt,tdscnt,tpscnt,mdrcnt,mprcnt);
+       if (strlen(name) < 1) {
+               api_fd_reply(conn, vuid, req,
+                            setup, (uint8_t *)data,
+                            params, suwcnt, tdscnt,
+                            tpscnt, mdrcnt, mprcnt);
+               return;
+       }
 
        if (setup)
-               DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n", (int)setup[0],(int)setup[1]));
+               DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
+                        (int)setup[0],(int)setup[1]));
 
-       return 0;
+       reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
+       return;
 }
 
+static void handle_trans(connection_struct *conn, struct smb_request *req,
+                        struct trans_state *state)
+{
+       char *local_machine_name;
+       int name_offset = 0;
+
+       DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
+                state->name,(unsigned int)state->total_data,(unsigned int)state->total_param,
+                (unsigned int)state->setup_count));
+
+       /*
+        * WinCE wierdness....
+        */
+
+       local_machine_name = talloc_asprintf(state, "\\%s\\",
+                                            get_local_machine_name());
+
+       if (local_machine_name == NULL) {
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
+               return;
+       }
+
+       if (strnequal(state->name, local_machine_name,
+                     strlen(local_machine_name))) {
+               name_offset = strlen(local_machine_name)-1;
+       }
+
+       if (!strnequal(&state->name[name_offset], "\\PIPE",
+                      strlen("\\PIPE"))) {
+               reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
+               return;
+       }
+
+       name_offset += strlen("\\PIPE");
+
+       /* Win9x weirdness.  When talking to a unicode server Win9x
+          only sends \PIPE instead of \PIPE\ */
+
+       if (state->name[name_offset] == '\\')
+               name_offset++;
+
+       DEBUG(5,("calling named_pipe\n"));
+       named_pipe(conn, state->vuid, req,
+                  state->name+name_offset,
+                  state->setup,state->data,
+                  state->param,
+                  state->setup_count,state->total_data,
+                  state->total_param,
+                  state->max_setup_return,
+                  state->max_data_return,
+                  state->max_param_return);
+
+       if (state->close_on_completion) {
+               close_cnum(conn,state->vuid);
+               req->conn = NULL;
+       }
+
+       return;
+}
 
 /****************************************************************************
  Reply to a SMBtrans.
  ****************************************************************************/
 
-int reply_trans(connection_struct *conn, char *inbuf,char *outbuf, int size, int bufsize)
+void reply_trans(struct smb_request *req)
 {
-       fstring name;
-       int name_offset = 0;
-       char *data=NULL,*params=NULL;
-       uint16 *setup=NULL;
-       int outsize = 0;
-       uint16 vuid = SVAL(inbuf,smb_uid);
-       unsigned int tpscnt = SVAL(inbuf,smb_vwv0);
-       unsigned int tdscnt = SVAL(inbuf,smb_vwv1);
-       unsigned int mprcnt = SVAL(inbuf,smb_vwv2);
-       unsigned int mdrcnt = SVAL(inbuf,smb_vwv3);
-       unsigned int msrcnt = CVAL(inbuf,smb_vwv4);
-       BOOL close_on_completion = BITSETW(inbuf+smb_vwv5,0);
-       BOOL one_way = BITSETW(inbuf+smb_vwv5,1);
-       unsigned int pscnt = SVAL(inbuf,smb_vwv9);
-       unsigned int psoff = SVAL(inbuf,smb_vwv10);
-       unsigned int dscnt = SVAL(inbuf,smb_vwv11);
-       unsigned int dsoff = SVAL(inbuf,smb_vwv12);
-       unsigned int suwcnt = CVAL(inbuf,smb_vwv13);
+       connection_struct *conn = req->conn;
+       unsigned int dsoff;
+       unsigned int dscnt;
+       unsigned int psoff;
+       unsigned int pscnt;
+       struct trans_state *state;
+       NTSTATUS result;
+
        START_PROFILE(SMBtrans);
 
-       memset(name, '\0',sizeof(name));
-       srvstr_pull_buf(inbuf, name, smb_buf(inbuf), sizeof(name), STR_TERMINATE);
+       if (req->wct < 14) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               END_PROFILE(SMBtrans);
+               return;
+       }
+
+       dsoff = SVAL(req->vwv+12, 0);
+       dscnt = SVAL(req->vwv+11, 0);
+       psoff = SVAL(req->vwv+10, 0);
+       pscnt = SVAL(req->vwv+9, 0);
 
-       if (dscnt > tdscnt || pscnt > tpscnt)
+       result = allow_new_trans(conn->pending_trans, req->mid);
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(2, ("Got invalid trans request: %s\n",
+                         nt_errstr(result)));
+               reply_nterror(req, result);
+               END_PROFILE(SMBtrans);
+               return;
+       }
+
+       if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
+               DEBUG(0, ("talloc failed\n"));
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
+               END_PROFILE(SMBtrans);
+               return;
+       }
+
+       state->cmd = SMBtrans;
+
+       state->mid = req->mid;
+       state->vuid = req->vuid;
+       state->setup_count = CVAL(req->vwv+13, 0);
+       state->setup = NULL;
+       state->total_param = SVAL(req->vwv+0, 0);
+       state->param = NULL;
+       state->total_data = SVAL(req->vwv+1, 0);
+       state->data = NULL;
+       state->max_param_return = SVAL(req->vwv+2, 0);
+       state->max_data_return = SVAL(req->vwv+3, 0);
+       state->max_setup_return = CVAL(req->vwv+4, 0);
+       state->close_on_completion = BITSETW(req->vwv+5, 0);
+       state->one_way = BITSETW(req->vwv+5, 1);
+
+       srvstr_pull_req_talloc(state, req, &state->name, req->buf,
+                              STR_TERMINATE);
+
+       if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
+                       !state->name)
                goto bad_param;
-  
-       if (tdscnt)  {
-               if((data = (char *)malloc(tdscnt)) == NULL) {
-                       DEBUG(0,("reply_trans: data malloc fail for %u bytes !\n", tdscnt));
-                       END_PROFILE(SMBtrans);
-                       return(ERROR_DOS(ERRDOS,ERRnomem));
-               } 
-               if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
-                       goto bad_param;
-               if (smb_base(inbuf)+dsoff+dscnt > inbuf + size)
+
+       if (state->total_data)  {
+
+               if (trans_oob(state->total_data, 0, dscnt)
+                   || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
                        goto bad_param;
+               }
+
+               /* Can't use talloc here, the core routines do realloc on the
+                * params and data. Out of paranoia, 100 bytes too many. */
+               state->data = (char *)SMB_MALLOC(state->total_data+100);
+               if (state->data == NULL) {
+                       DEBUG(0,("reply_trans: data malloc fail for %u "
+                                "bytes !\n", (unsigned int)state->total_data));
+                       TALLOC_FREE(state);
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       END_PROFILE(SMBtrans);
+                       return;
+               }
+               /* null-terminate the slack space */
+               memset(&state->data[state->total_data], 0, 100);
 
-               memcpy(data,smb_base(inbuf)+dsoff,dscnt);
+               memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
        }
 
-       if (tpscnt) {
-               if((params = (char *)malloc(tpscnt)) == NULL) {
-                       DEBUG(0,("reply_trans: param malloc fail for %u bytes !\n", tpscnt));
-                       SAFE_FREE(data);
+       if (state->total_param) {
+
+               if (trans_oob(state->total_param, 0, pscnt)
+                   || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
+                       goto bad_param;
+               }
+
+               /* Can't use talloc here, the core routines do realloc on the
+                * params and data. Out of paranoia, 100 bytes too many */
+               state->param = (char *)SMB_MALLOC(state->total_param+100);
+               if (state->param == NULL) {
+                       DEBUG(0,("reply_trans: param malloc fail for %u "
+                                "bytes !\n", (unsigned int)state->total_param));
+                       SAFE_FREE(state->data);
+                       TALLOC_FREE(state);
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
                        END_PROFILE(SMBtrans);
-                       return(ERROR_DOS(ERRDOS,ERRnomem));
+                       return;
                } 
-               if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
-                        goto bad_param;
-               if (smb_base(inbuf)+psoff+pscnt > inbuf + size)
-                       goto bad_param;
+               /* null-terminate the slack space */
+               memset(&state->param[state->total_param], 0, 100);
 
-               memcpy(params,smb_base(inbuf)+psoff,pscnt);
+               memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
        }
 
-       if (suwcnt) {
+       state->received_data  = dscnt;
+       state->received_param = pscnt;
+
+       if (state->setup_count) {
                unsigned int i;
-               if((setup = (uint16 *)malloc(suwcnt*sizeof(uint16))) == NULL) {
-                       DEBUG(0,("reply_trans: setup malloc fail for %u bytes !\n", (unsigned int)(suwcnt * sizeof(uint16))));
-                       SAFE_FREE(data);
-                       SAFE_FREE(params);
+
+               /*
+                * No overflow possible here, state->setup_count is an
+                * unsigned int, being filled by a single byte from
+                * CVAL(req->vwv+13, 0) above. The cast in the comparison
+                * below is not necessary, it's here to clarify things. The
+                * validity of req->vwv and req->wct has been checked in
+                * init_smb_request already.
+                */
+               if (state->setup_count + 14 > (unsigned int)req->wct) {
+                       goto bad_param;
+               }
+
+               if((state->setup = TALLOC_ARRAY(
+                           state, uint16, state->setup_count)) == NULL) {
+                       DEBUG(0,("reply_trans: setup malloc fail for %u "
+                                "bytes !\n", (unsigned int)
+                                (state->setup_count * sizeof(uint16))));
+                       SAFE_FREE(state->data);
+                       SAFE_FREE(state->param);
+                       TALLOC_FREE(state);
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
                        END_PROFILE(SMBtrans);
-                       return(ERROR_DOS(ERRDOS,ERRnomem));
+                       return;
                } 
-               if (inbuf+smb_vwv14+(suwcnt*SIZEOFWORD) > inbuf + size)
-                       goto bad_param;
-               if ((smb_vwv14+(suwcnt*SIZEOFWORD) < smb_vwv14) || (smb_vwv14+(suwcnt*SIZEOFWORD) < (suwcnt*SIZEOFWORD)))
-                       goto bad_param;
 
-               for (i=0;i<suwcnt;i++)
-                       setup[i] = SVAL(inbuf,smb_vwv14+i*SIZEOFWORD);
+               for (i=0;i<state->setup_count;i++) {
+                       state->setup[i] = SVAL(req->vwv + 14 + i, 0);
+               }
        }
 
+       state->received_param = pscnt;
+
+       if ((state->received_param != state->total_param) ||
+           (state->received_data != state->total_data)) {
+               DLIST_ADD(conn->pending_trans, state);
 
-       if (pscnt < tpscnt || dscnt < tdscnt) {
                /* We need to send an interim response then receive the rest
                   of the parameter/data bytes */
-               outsize = set_message(outbuf,0,0,True);
-               show_msg(outbuf);
-               if (!send_smb(smbd_server_fd(),outbuf))
-                       exit_server("reply_trans: send_smb failed.");
-       }
-
-       /* receive the rest of the trans packet */
-       while (pscnt < tpscnt || dscnt < tdscnt) {
-               BOOL ret;
-               unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
-      
-               ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
-
-               if ((ret && (CVAL(inbuf, smb_com) != SMBtranss)) || !ret) {
-                       if(ret) {
-                               DEBUG(0,("reply_trans: Invalid secondary trans packet\n"));
-                       } else {
-                               DEBUG(0,("reply_trans: %s in getting secondary trans response.\n",
-                                        (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
-                       }
-                       SAFE_FREE(params);
-                       SAFE_FREE(data);
-                       SAFE_FREE(setup);
-                       END_PROFILE(SMBtrans);
-                       return(ERROR_DOS(ERRSRV,ERRerror));
-               }
+               reply_outbuf(req, 0, 0);
+               show_msg((char *)req->outbuf);
+               END_PROFILE(SMBtrans);
+               return;
+       }
 
-               show_msg(inbuf);
-      
-               /* Revise total_params and total_data in case they have changed downwards */
-               if (SVAL(inbuf,smb_vwv0) < tpscnt)
-                       tpscnt = SVAL(inbuf,smb_vwv0);
-               if (SVAL(inbuf,smb_vwv1) < tdscnt)
-                       tdscnt = SVAL(inbuf,smb_vwv1);
-
-               pcnt = SVAL(inbuf,smb_vwv2);
-               poff = SVAL(inbuf,smb_vwv3);
-               pdisp = SVAL(inbuf,smb_vwv4);
-               
-               dcnt = SVAL(inbuf,smb_vwv5);
-               doff = SVAL(inbuf,smb_vwv6);
-               ddisp = SVAL(inbuf,smb_vwv7);
-               
-               pscnt += pcnt;
-               dscnt += dcnt;
-               
-               if (dscnt > tdscnt || pscnt > tpscnt)
-                       goto bad_param;
-               
-               if (pcnt) {
-                       if (pdisp+pcnt >= tpscnt)
-                               goto bad_param;
-                       if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
-                                goto bad_param;
-                       if (smb_base(inbuf) + poff + pcnt >= inbuf + bufsize)
-                               goto bad_param;
-                       if (params + pdisp < params)
-                               goto bad_param;
-
-                       memcpy(params+pdisp,smb_base(inbuf)+poff,pcnt);
-               }
+       handle_trans(conn, req, state);
 
-               if (dcnt) {
-                       if (ddisp+dcnt >= tdscnt)
-                               goto bad_param;
-                       if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
-                               goto bad_param;
-                       if (smb_base(inbuf) + doff + dcnt >= inbuf + bufsize)
-                               goto bad_param;
-                       if (data + ddisp < data)
-                               goto bad_param;
-
-                       memcpy(data+ddisp,smb_base(inbuf)+doff,dcnt);      
+       SAFE_FREE(state->data);
+       SAFE_FREE(state->param);
+       TALLOC_FREE(state);
+
+       END_PROFILE(SMBtrans);
+       return;
+
+  bad_param:
+
+       DEBUG(0,("reply_trans: invalid trans parameters\n"));
+       SAFE_FREE(state->data);
+       SAFE_FREE(state->param);
+       TALLOC_FREE(state);
+       END_PROFILE(SMBtrans);
+       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+       return;
+}
+
+/****************************************************************************
+ Reply to a secondary SMBtrans.
+ ****************************************************************************/
+
+void reply_transs(struct smb_request *req)
+{
+       connection_struct *conn = req->conn;
+       unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
+       struct trans_state *state;
+
+       START_PROFILE(SMBtranss);
+
+       show_msg((char *)req->inbuf);
+
+       if (req->wct < 8) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               END_PROFILE(SMBtranss);
+               return;
+       }
+
+       for (state = conn->pending_trans; state != NULL;
+            state = state->next) {
+               if (state->mid == req->mid) {
+                       break;
                }
        }
-       
-       
-       DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
-                name,tdscnt,tpscnt,suwcnt));
-       
-       /*
-        * WinCE wierdness....
-        */
 
-       if (name[0] == '\\' && (StrnCaseCmp(&name[1],local_machine, strlen(local_machine)) == 0) &&
-                       (name[strlen(local_machine)+1] == '\\'))
-               name_offset = strlen(local_machine)+1;
+       if ((state == NULL) || (state->cmd != SMBtrans)) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               END_PROFILE(SMBtranss);
+               return;
+       }
 
-       if (strnequal(&name[name_offset], "\\PIPE", strlen("\\PIPE"))) {
-               name_offset += strlen("\\PIPE");
+       /* Revise total_params and total_data in case they have changed
+        * downwards */
 
-               /* Win9x weirdness.  When talking to a unicode server Win9x
-                  only sends \PIPE instead of \PIPE\ */
+       if (SVAL(req->vwv+0, 0) < state->total_param)
+               state->total_param = SVAL(req->vwv+0, 0);
+       if (SVAL(req->vwv+1, 0) < state->total_data)
+               state->total_data = SVAL(req->vwv+1, 0);
 
-               if (name[name_offset] == '\\')
-                       name_offset++;
+       pcnt = SVAL(req->vwv+2, 0);
+       poff = SVAL(req->vwv+3, 0);
+       pdisp = SVAL(req->vwv+4, 0);
 
-               DEBUG(5,("calling named_pipe\n"));
-               outsize = named_pipe(conn,vuid,outbuf,
-                                    name+name_offset,setup,data,params,
-                                    suwcnt,tdscnt,tpscnt,msrcnt,mdrcnt,mprcnt);
-       } else {
-               DEBUG(3,("invalid pipe name\n"));
-               outsize = 0;
-       }
+       dcnt = SVAL(req->vwv+5, 0);
+       doff = SVAL(req->vwv+6, 0);
+       ddisp = SVAL(req->vwv+7, 0);
 
-       
-       SAFE_FREE(data);
-       SAFE_FREE(params);
-       SAFE_FREE(setup);
-       
-       if (close_on_completion)
-               close_cnum(conn,vuid);
+       state->received_param += pcnt;
+       state->received_data += dcnt;
 
-       if (one_way) {
-               END_PROFILE(SMBtrans);
-               return(-1);
+       if ((state->received_data > state->total_data) ||
+           (state->received_param > state->total_param))
+               goto bad_param;
+
+       if (pcnt) {
+               if (trans_oob(state->total_param, pdisp, pcnt)
+                   || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
+                       goto bad_param;
+               }
+               memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
        }
-       
-       if (outsize == 0) {
-               END_PROFILE(SMBtrans);
-               return(ERROR_DOS(ERRSRV,ERRnosupport));
+
+       if (dcnt) {
+               if (trans_oob(state->total_data, ddisp, dcnt)
+                   || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
+                       goto bad_param;
+               }
+               memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
        }
-       
-       END_PROFILE(SMBtrans);
-       return(outsize);
 
+       if ((state->received_param < state->total_param) ||
+           (state->received_data < state->total_data)) {
+               END_PROFILE(SMBtranss);
+               return;
+       }
+
+       handle_trans(conn, req, state);
+
+       DLIST_REMOVE(conn->pending_trans, state);
+       SAFE_FREE(state->data);
+       SAFE_FREE(state->param);
+       TALLOC_FREE(state);
+
+       END_PROFILE(SMBtranss);
+       return;
 
   bad_param:
 
-       DEBUG(0,("reply_trans: invalid trans parameters\n"));
-       SAFE_FREE(data);
-       SAFE_FREE(params);
-       SAFE_FREE(setup);
-       END_PROFILE(SMBtrans);
-       return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
+       DEBUG(0,("reply_transs: invalid trans parameters\n"));
+       DLIST_REMOVE(conn->pending_trans, state);
+       SAFE_FREE(state->data);
+       SAFE_FREE(state->param);
+       TALLOC_FREE(state);
+       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+       END_PROFILE(SMBtranss);
+       return;
 }