Merge branch 'master' of ssh://git.samba.org/data/git/samba
[samba.git] / source3 / smbd / ipc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Inter-process communication and named pipe handling
4    Copyright (C) Andrew Tridgell 1992-1998
5
6    SMB Version handling
7    Copyright (C) John H Terpstra 1995-1998
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21    */
22 /*
23    This file handles the named pipe and mailslot calls
24    in the SMBtrans protocol
25    */
26
27 #include "includes.h"
28 #include "smbd/globals.h"
29
30 #define NERR_notsupported 50
31
32 static void api_no_reply(connection_struct *conn, struct smb_request *req);
33
34 /*******************************************************************
35  copies parameters and data, as needed, into the smb buffer
36
37  *both* the data and params sections should be aligned.  this
38  is fudged in the rpc pipes by 
39  at present, only the data section is.  this may be a possible
40  cause of some of the ipc problems being experienced.  lkcl26dec97
41
42  ******************************************************************/
43
44 static void copy_trans_params_and_data(char *outbuf, int align,
45                                 char *rparam, int param_offset, int param_len,
46                                 char *rdata, int data_offset, int data_len)
47 {
48         char *copy_into = smb_buf(outbuf);
49
50         if(param_len < 0)
51                 param_len = 0;
52
53         if(data_len < 0)
54                 data_len = 0;
55
56         DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d] (align %d)\n",
57                         param_offset, param_offset + param_len,
58                         data_offset , data_offset  + data_len,
59                         align));
60
61         *copy_into = '\0';
62
63         copy_into += 1;
64
65         if (param_len)
66                 memcpy(copy_into, &rparam[param_offset], param_len);
67
68         copy_into += param_len;
69         if (align) {
70                 memset(copy_into, '\0', align);
71         }
72
73         copy_into += align;
74
75         if (data_len )
76                 memcpy(copy_into, &rdata[data_offset], data_len);
77 }
78
79 /****************************************************************************
80  Send a trans reply.
81  ****************************************************************************/
82
83 void send_trans_reply(connection_struct *conn,
84                       struct smb_request *req,
85                       char *rparam, int rparam_len,
86                       char *rdata, int rdata_len,
87                       bool buffer_too_large)
88 {
89         int this_ldata,this_lparam;
90         int tot_data_sent = 0;
91         int tot_param_sent = 0;
92         int align;
93
94         int ldata  = rdata  ? rdata_len : 0;
95         int lparam = rparam ? rparam_len : 0;
96         struct smbd_server_connection *sconn = smbd_server_conn;
97         int max_send = sconn->smb1.sessions.max_send;
98
99         if (buffer_too_large)
100                 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata ));
101
102         this_lparam = MIN(lparam,max_send - 500); /* hack */
103         this_ldata  = MIN(ldata,max_send - (500+this_lparam));
104
105         align = ((this_lparam)%4);
106
107         reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
108
109         /*
110          * We might have SMBtranss in req which was transferred to the outbuf,
111          * fix that.
112          */
113         SCVAL(req->outbuf, smb_com, SMBtrans);
114
115         copy_trans_params_and_data((char *)req->outbuf, align,
116                                 rparam, tot_param_sent, this_lparam,
117                                 rdata, tot_data_sent, this_ldata);
118
119         SSVAL(req->outbuf,smb_vwv0,lparam);
120         SSVAL(req->outbuf,smb_vwv1,ldata);
121         SSVAL(req->outbuf,smb_vwv3,this_lparam);
122         SSVAL(req->outbuf,smb_vwv4,
123               smb_offset(smb_buf(req->outbuf)+1, req->outbuf));
124         SSVAL(req->outbuf,smb_vwv5,0);
125         SSVAL(req->outbuf,smb_vwv6,this_ldata);
126         SSVAL(req->outbuf,smb_vwv7,
127               smb_offset(smb_buf(req->outbuf)+1+this_lparam+align,
128                          req->outbuf));
129         SSVAL(req->outbuf,smb_vwv8,0);
130         SSVAL(req->outbuf,smb_vwv9,0);
131
132         if (buffer_too_large) {
133                 error_packet_set((char *)req->outbuf, ERRDOS, ERRmoredata,
134                                  STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
135         }
136
137         show_msg((char *)req->outbuf);
138         if (!srv_send_smb(smbd_server_fd(), (char *)req->outbuf,
139                           true, req->seqnum+1,
140                           IS_CONN_ENCRYPTED(conn), &req->pcd)) {
141                 exit_server_cleanly("send_trans_reply: srv_send_smb failed.");
142         }
143
144         TALLOC_FREE(req->outbuf);
145
146         tot_data_sent = this_ldata;
147         tot_param_sent = this_lparam;
148
149         while (tot_data_sent < ldata || tot_param_sent < lparam)
150         {
151                 this_lparam = MIN(lparam-tot_param_sent,
152                                   max_send - 500); /* hack */
153                 this_ldata  = MIN(ldata -tot_data_sent,
154                                   max_send - (500+this_lparam));
155
156                 if(this_lparam < 0)
157                         this_lparam = 0;
158
159                 if(this_ldata < 0)
160                         this_ldata = 0;
161
162                 align = (this_lparam%4);
163
164                 reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
165
166                 /*
167                  * We might have SMBtranss in req which was transferred to the
168                  * outbuf, fix that.
169                  */
170                 SCVAL(req->outbuf, smb_com, SMBtrans);
171
172                 copy_trans_params_and_data((char *)req->outbuf, align,
173                                            rparam, tot_param_sent, this_lparam,
174                                            rdata, tot_data_sent, this_ldata);
175
176                 SSVAL(req->outbuf,smb_vwv3,this_lparam);
177                 SSVAL(req->outbuf,smb_vwv4,
178                       smb_offset(smb_buf(req->outbuf)+1,req->outbuf));
179                 SSVAL(req->outbuf,smb_vwv5,tot_param_sent);
180                 SSVAL(req->outbuf,smb_vwv6,this_ldata);
181                 SSVAL(req->outbuf,smb_vwv7,
182                       smb_offset(smb_buf(req->outbuf)+1+this_lparam+align,
183                                  req->outbuf));
184                 SSVAL(req->outbuf,smb_vwv8,tot_data_sent);
185                 SSVAL(req->outbuf,smb_vwv9,0);
186
187                 if (buffer_too_large) {
188                         error_packet_set((char *)req->outbuf,
189                                          ERRDOS, ERRmoredata,
190                                          STATUS_BUFFER_OVERFLOW,
191                                          __LINE__, __FILE__);
192                 }
193
194                 show_msg((char *)req->outbuf);
195                 if (!srv_send_smb(smbd_server_fd(), (char *)req->outbuf,
196                                   true, req->seqnum+1,
197                                   IS_CONN_ENCRYPTED(conn), &req->pcd))
198                         exit_server_cleanly("send_trans_reply: srv_send_smb "
199                                             "failed.");
200
201                 tot_data_sent  += this_ldata;
202                 tot_param_sent += this_lparam;
203                 TALLOC_FREE(req->outbuf);
204         }
205 }
206
207 /****************************************************************************
208  Start the first part of an RPC reply which began with an SMBtrans request.
209 ****************************************************************************/
210
211 struct dcerpc_cmd_state {
212         struct fake_file_handle *handle;
213         uint8_t *data;
214         size_t num_data;
215         size_t max_read;
216 };
217
218 static void api_dcerpc_cmd_write_done(struct tevent_req *subreq);
219 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq);
220
221 static void api_dcerpc_cmd(connection_struct *conn, struct smb_request *req,
222                            files_struct *fsp, uint8_t *data, size_t length,
223                            size_t max_read)
224 {
225         struct tevent_req *subreq;
226         struct dcerpc_cmd_state *state;
227
228         if (!fsp_is_np(fsp)) {
229                 api_no_reply(conn, req);
230                 return;
231         }
232
233         state = talloc(req, struct dcerpc_cmd_state);
234         if (state == NULL) {
235                 reply_nterror(req, NT_STATUS_NO_MEMORY);
236                 return;
237         }
238         req->async_priv = state;
239
240         state->handle = fsp->fake_file_handle;
241
242         /*
243          * This memdup severely sucks. But doing it properly essentially means
244          * to rewrite lanman.c, something which I don't really want to do now.
245          */
246         state->data = (uint8_t *)talloc_memdup(state, data, length);
247         if (state->data == NULL) {
248                 reply_nterror(req, NT_STATUS_NO_MEMORY);
249                 return;
250         }
251         state->num_data = length;
252         state->max_read = max_read;
253
254         subreq = np_write_send(state, smbd_event_context(), state->handle,
255                                state->data, length);
256         if (subreq == NULL) {
257                 TALLOC_FREE(state);
258                 reply_nterror(req, NT_STATUS_NO_MEMORY);
259                 return;
260         }
261         tevent_req_set_callback(subreq, api_dcerpc_cmd_write_done,
262                                 talloc_move(conn, &req));
263 }
264
265 static void api_dcerpc_cmd_write_done(struct tevent_req *subreq)
266 {
267         struct smb_request *req = tevent_req_callback_data(
268                 subreq, struct smb_request);
269         struct dcerpc_cmd_state *state = talloc_get_type_abort(
270                 req->async_priv, struct dcerpc_cmd_state);
271         NTSTATUS status;
272         ssize_t nwritten = -1;
273
274         status = np_write_recv(subreq, &nwritten);
275         TALLOC_FREE(subreq);
276         if (!NT_STATUS_IS_OK(status) || (nwritten != state->num_data)) {
277                 DEBUG(10, ("Could not write to pipe: %s (%d/%d)\n",
278                            nt_errstr(status), (int)state->num_data,
279                            (int)nwritten));
280                 reply_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
281                 goto send;
282         }
283
284         state->data = TALLOC_REALLOC_ARRAY(state, state->data, uint8_t,
285                                            state->max_read);
286         if (state->data == NULL) {
287                 reply_nterror(req, NT_STATUS_NO_MEMORY);
288                 goto send;
289         }
290
291         subreq = np_read_send(req->conn, smbd_event_context(),
292                               state->handle, state->data, state->max_read);
293         if (subreq == NULL) {
294                 reply_nterror(req, NT_STATUS_NO_MEMORY);
295                 goto send;
296         }
297         tevent_req_set_callback(subreq, api_dcerpc_cmd_read_done, req);
298         return;
299
300  send:
301         if (!srv_send_smb(
302                     smbd_server_fd(), (char *)req->outbuf,
303                     true, req->seqnum+1,
304                     IS_CONN_ENCRYPTED(req->conn) || req->encrypted,
305                     &req->pcd)) {
306                 exit_server_cleanly("construct_reply: srv_send_smb failed.");
307         }
308         TALLOC_FREE(req);
309 }
310
311 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq)
312 {
313         struct smb_request *req = tevent_req_callback_data(
314                 subreq, struct smb_request);
315         struct dcerpc_cmd_state *state = talloc_get_type_abort(
316                 req->async_priv, struct dcerpc_cmd_state);
317         NTSTATUS status;
318         ssize_t nread;
319         bool is_data_outstanding;
320
321         status = np_read_recv(subreq, &nread, &is_data_outstanding);
322         TALLOC_FREE(subreq);
323
324         if (!NT_STATUS_IS_OK(status)) {
325                 DEBUG(10, ("Could not read from to pipe: %s\n",
326                            nt_errstr(status)));
327                 reply_nterror(req, status);
328
329                 if (!srv_send_smb(smbd_server_fd(), (char *)req->outbuf,
330                                   true, req->seqnum+1,
331                                   IS_CONN_ENCRYPTED(req->conn)
332                                   ||req->encrypted, &req->pcd)) {
333                         exit_server_cleanly("construct_reply: srv_send_smb "
334                                             "failed.");
335                 }
336                 TALLOC_FREE(req);
337                 return;
338         }
339
340         send_trans_reply(req->conn, req, NULL, 0, (char *)state->data, nread,
341                          is_data_outstanding);
342         TALLOC_FREE(req);
343 }
344
345 /****************************************************************************
346  WaitNamedPipeHandleState 
347 ****************************************************************************/
348
349 static void api_WNPHS(connection_struct *conn, struct smb_request *req,
350                       struct files_struct *fsp, char *param, int param_len)
351 {
352         if (!param || param_len < 2) {
353                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
354                 return;
355         }
356
357         DEBUG(4,("WaitNamedPipeHandleState priority %x\n",
358                  (int)SVAL(param,0)));
359
360         send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
361 }
362
363
364 /****************************************************************************
365  SetNamedPipeHandleState 
366 ****************************************************************************/
367
368 static void api_SNPHS(connection_struct *conn, struct smb_request *req,
369                       struct files_struct *fsp, char *param, int param_len)
370 {
371         if (!param || param_len < 2) {
372                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
373                 return;
374         }
375
376         DEBUG(4,("SetNamedPipeHandleState to code %x\n", (int)SVAL(param,0)));
377
378         send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
379 }
380
381
382 /****************************************************************************
383  When no reply is generated, indicate unsupported.
384  ****************************************************************************/
385
386 static void api_no_reply(connection_struct *conn, struct smb_request *req)
387 {
388         char rparam[4];
389
390         /* unsupported */
391         SSVAL(rparam,0,NERR_notsupported);
392         SSVAL(rparam,2,0); /* converter word */
393
394         DEBUG(3,("Unsupported API fd command\n"));
395
396         /* now send the reply */
397         send_trans_reply(conn, req, rparam, 4, NULL, 0, False);
398
399         return;
400 }
401
402 /****************************************************************************
403  Handle remote api calls delivered to a named pipe already opened.
404  ****************************************************************************/
405
406 static void api_fd_reply(connection_struct *conn, uint16 vuid,
407                          struct smb_request *req,
408                          uint16 *setup, uint8_t *data, char *params,
409                          int suwcnt, int tdscnt, int tpscnt,
410                          int mdrcnt, int mprcnt)
411 {
412         struct files_struct *fsp;
413         int pnum;
414         int subcommand;
415
416         DEBUG(5,("api_fd_reply\n"));
417
418         /* First find out the name of this file. */
419         if (suwcnt != 2) {
420                 DEBUG(0,("Unexpected named pipe transaction.\n"));
421                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
422                 return;
423         }
424
425         /* Get the file handle and hence the file name. */
426         /* 
427          * NB. The setup array has already been transformed
428          * via SVAL and so is in host byte order.
429          */
430         pnum = ((int)setup[1]) & 0xFFFF;
431         subcommand = ((int)setup[0]) & 0xFFFF;
432
433         fsp = file_fsp(req, pnum);
434
435         if (!fsp_is_np(fsp)) {
436                 if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
437                         /* Win9x does this call with a unicode pipe name, not a pnum. */
438                         /* Just return success for now... */
439                         DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
440                         send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
441                         return;
442                 }
443
444                 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
445                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
446                 return;
447         }
448
449         if (vuid != fsp->vuid) {
450                 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
451                           "expected %d\n", pnum, vuid, fsp->vuid));
452                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
453                 return;
454         }
455
456         DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n",
457                  subcommand, fsp_str_dbg(fsp), pnum));
458
459         DEBUG(10, ("api_fd_reply: p:%p max_trans_reply: %d\n", fsp, mdrcnt));
460
461         switch (subcommand) {
462         case TRANSACT_DCERPCCMD: {
463                 /* dce/rpc command */
464                 api_dcerpc_cmd(conn, req, fsp, (uint8_t *)data, tdscnt,
465                                mdrcnt);
466                 break;
467         }
468         case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
469                 /* Wait Named Pipe Handle state */
470                 api_WNPHS(conn, req, fsp, params, tpscnt);
471                 break;
472         case TRANSACT_SETNAMEDPIPEHANDLESTATE:
473                 /* Set Named Pipe Handle state */
474                 api_SNPHS(conn, req, fsp, params, tpscnt);
475                 break;
476         default:
477                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
478                 return;
479         }
480 }
481
482 /****************************************************************************
483  Handle named pipe commands.
484 ****************************************************************************/
485
486 static void named_pipe(connection_struct *conn, uint16 vuid,
487                        struct smb_request *req,
488                        const char *name, uint16 *setup,
489                        char *data, char *params,
490                        int suwcnt, int tdscnt,int tpscnt,
491                        int msrcnt, int mdrcnt, int mprcnt)
492 {
493         DEBUG(3,("named pipe command on <%s> name\n", name));
494
495         if (strequal(name,"LANMAN")) {
496                 api_reply(conn, vuid, req,
497                           data, params,
498                           tdscnt, tpscnt,
499                           mdrcnt, mprcnt);
500                 return;
501         }
502
503         if (strequal(name,"WKSSVC") ||
504             strequal(name,"SRVSVC") ||
505             strequal(name,"WINREG") ||
506             strequal(name,"SAMR") ||
507             strequal(name,"LSARPC")) {
508
509                 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
510
511                 api_fd_reply(conn, vuid, req,
512                              setup, (uint8_t *)data, params,
513                              suwcnt, tdscnt, tpscnt,
514                              mdrcnt, mprcnt);
515                 return;
516         }
517
518         if (strlen(name) < 1) {
519                 api_fd_reply(conn, vuid, req,
520                              setup, (uint8_t *)data,
521                              params, suwcnt, tdscnt,
522                              tpscnt, mdrcnt, mprcnt);
523                 return;
524         }
525
526         if (setup)
527                 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
528                          (int)setup[0],(int)setup[1]));
529
530         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
531         return;
532 }
533
534 static void handle_trans(connection_struct *conn, struct smb_request *req,
535                          struct trans_state *state)
536 {
537         char *local_machine_name;
538         int name_offset = 0;
539
540         DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
541                  state->name,(unsigned int)state->total_data,(unsigned int)state->total_param,
542                  (unsigned int)state->setup_count));
543
544         /*
545          * WinCE wierdness....
546          */
547
548         local_machine_name = talloc_asprintf(state, "\\%s\\",
549                                              get_local_machine_name());
550
551         if (local_machine_name == NULL) {
552                 reply_nterror(req, NT_STATUS_NO_MEMORY);
553                 return;
554         }
555
556         if (strnequal(state->name, local_machine_name,
557                       strlen(local_machine_name))) {
558                 name_offset = strlen(local_machine_name)-1;
559         }
560
561         if (!strnequal(&state->name[name_offset], "\\PIPE",
562                        strlen("\\PIPE"))) {
563                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
564                 return;
565         }
566
567         name_offset += strlen("\\PIPE");
568
569         /* Win9x weirdness.  When talking to a unicode server Win9x
570            only sends \PIPE instead of \PIPE\ */
571
572         if (state->name[name_offset] == '\\')
573                 name_offset++;
574
575         DEBUG(5,("calling named_pipe\n"));
576         named_pipe(conn, state->vuid, req,
577                    state->name+name_offset,
578                    state->setup,state->data,
579                    state->param,
580                    state->setup_count,state->total_data,
581                    state->total_param,
582                    state->max_setup_return,
583                    state->max_data_return,
584                    state->max_param_return);
585
586         if (state->close_on_completion) {
587                 close_cnum(conn,state->vuid);
588                 req->conn = NULL;
589         }
590
591         return;
592 }
593
594 /****************************************************************************
595  Reply to a SMBtrans.
596  ****************************************************************************/
597
598 void reply_trans(struct smb_request *req)
599 {
600         connection_struct *conn = req->conn;
601         unsigned int dsoff;
602         unsigned int dscnt;
603         unsigned int psoff;
604         unsigned int pscnt;
605         struct trans_state *state;
606         NTSTATUS result;
607
608         START_PROFILE(SMBtrans);
609
610         if (req->wct < 14) {
611                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
612                 END_PROFILE(SMBtrans);
613                 return;
614         }
615
616         dsoff = SVAL(req->vwv+12, 0);
617         dscnt = SVAL(req->vwv+11, 0);
618         psoff = SVAL(req->vwv+10, 0);
619         pscnt = SVAL(req->vwv+9, 0);
620
621         result = allow_new_trans(conn->pending_trans, req->mid);
622         if (!NT_STATUS_IS_OK(result)) {
623                 DEBUG(2, ("Got invalid trans request: %s\n",
624                           nt_errstr(result)));
625                 reply_nterror(req, result);
626                 END_PROFILE(SMBtrans);
627                 return;
628         }
629
630         if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
631                 DEBUG(0, ("talloc failed\n"));
632                 reply_nterror(req, NT_STATUS_NO_MEMORY);
633                 END_PROFILE(SMBtrans);
634                 return;
635         }
636
637         state->cmd = SMBtrans;
638
639         state->mid = req->mid;
640         state->vuid = req->vuid;
641         state->setup_count = CVAL(req->vwv+13, 0);
642         state->setup = NULL;
643         state->total_param = SVAL(req->vwv+0, 0);
644         state->param = NULL;
645         state->total_data = SVAL(req->vwv+1, 0);
646         state->data = NULL;
647         state->max_param_return = SVAL(req->vwv+2, 0);
648         state->max_data_return = SVAL(req->vwv+3, 0);
649         state->max_setup_return = CVAL(req->vwv+4, 0);
650         state->close_on_completion = BITSETW(req->vwv+5, 0);
651         state->one_way = BITSETW(req->vwv+5, 1);
652
653         srvstr_pull_req_talloc(state, req, &state->name, req->buf,
654                                STR_TERMINATE);
655
656         if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
657                         !state->name)
658                 goto bad_param;
659
660         if (state->total_data)  {
661
662                 if (trans_oob(state->total_data, 0, dscnt)
663                     || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
664                         goto bad_param;
665                 }
666
667                 /* Can't use talloc here, the core routines do realloc on the
668                  * params and data. Out of paranoia, 100 bytes too many. */
669                 state->data = (char *)SMB_MALLOC(state->total_data+100);
670                 if (state->data == NULL) {
671                         DEBUG(0,("reply_trans: data malloc fail for %u "
672                                  "bytes !\n", (unsigned int)state->total_data));
673                         TALLOC_FREE(state);
674                         reply_nterror(req, NT_STATUS_NO_MEMORY);
675                         END_PROFILE(SMBtrans);
676                         return;
677                 }
678                 /* null-terminate the slack space */
679                 memset(&state->data[state->total_data], 0, 100);
680
681                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
682         }
683
684         if (state->total_param) {
685
686                 if (trans_oob(state->total_param, 0, pscnt)
687                     || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
688                         goto bad_param;
689                 }
690
691                 /* Can't use talloc here, the core routines do realloc on the
692                  * params and data. Out of paranoia, 100 bytes too many */
693                 state->param = (char *)SMB_MALLOC(state->total_param+100);
694                 if (state->param == NULL) {
695                         DEBUG(0,("reply_trans: param malloc fail for %u "
696                                  "bytes !\n", (unsigned int)state->total_param));
697                         SAFE_FREE(state->data);
698                         TALLOC_FREE(state);
699                         reply_nterror(req, NT_STATUS_NO_MEMORY);
700                         END_PROFILE(SMBtrans);
701                         return;
702                 } 
703                 /* null-terminate the slack space */
704                 memset(&state->param[state->total_param], 0, 100);
705
706                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
707         }
708
709         state->received_data  = dscnt;
710         state->received_param = pscnt;
711
712         if (state->setup_count) {
713                 unsigned int i;
714
715                 /*
716                  * No overflow possible here, state->setup_count is an
717                  * unsigned int, being filled by a single byte from
718                  * CVAL(req->vwv+13, 0) above. The cast in the comparison
719                  * below is not necessary, it's here to clarify things. The
720                  * validity of req->vwv and req->wct has been checked in
721                  * init_smb_request already.
722                  */
723                 if (state->setup_count + 14 > (unsigned int)req->wct) {
724                         goto bad_param;
725                 }
726
727                 if((state->setup = TALLOC_ARRAY(
728                             state, uint16, state->setup_count)) == NULL) {
729                         DEBUG(0,("reply_trans: setup malloc fail for %u "
730                                  "bytes !\n", (unsigned int)
731                                  (state->setup_count * sizeof(uint16))));
732                         SAFE_FREE(state->data);
733                         SAFE_FREE(state->param);
734                         TALLOC_FREE(state);
735                         reply_nterror(req, NT_STATUS_NO_MEMORY);
736                         END_PROFILE(SMBtrans);
737                         return;
738                 } 
739
740                 for (i=0;i<state->setup_count;i++) {
741                         state->setup[i] = SVAL(req->vwv + 14 + i, 0);
742                 }
743         }
744
745         state->received_param = pscnt;
746
747         if ((state->received_param != state->total_param) ||
748             (state->received_data != state->total_data)) {
749                 DLIST_ADD(conn->pending_trans, state);
750
751                 /* We need to send an interim response then receive the rest
752                    of the parameter/data bytes */
753                 reply_outbuf(req, 0, 0);
754                 show_msg((char *)req->outbuf);
755                 END_PROFILE(SMBtrans);
756                 return;
757         }
758
759         talloc_steal(talloc_tos(), state);
760
761         handle_trans(conn, req, state);
762
763         SAFE_FREE(state->data);
764         SAFE_FREE(state->param);
765         TALLOC_FREE(state);
766
767         END_PROFILE(SMBtrans);
768         return;
769
770   bad_param:
771
772         DEBUG(0,("reply_trans: invalid trans parameters\n"));
773         SAFE_FREE(state->data);
774         SAFE_FREE(state->param);
775         TALLOC_FREE(state);
776         END_PROFILE(SMBtrans);
777         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
778         return;
779 }
780
781 /****************************************************************************
782  Reply to a secondary SMBtrans.
783  ****************************************************************************/
784
785 void reply_transs(struct smb_request *req)
786 {
787         connection_struct *conn = req->conn;
788         unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
789         struct trans_state *state;
790
791         START_PROFILE(SMBtranss);
792
793         show_msg((char *)req->inbuf);
794
795         if (req->wct < 8) {
796                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
797                 END_PROFILE(SMBtranss);
798                 return;
799         }
800
801         for (state = conn->pending_trans; state != NULL;
802              state = state->next) {
803                 if (state->mid == req->mid) {
804                         break;
805                 }
806         }
807
808         if ((state == NULL) || (state->cmd != SMBtrans)) {
809                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
810                 END_PROFILE(SMBtranss);
811                 return;
812         }
813
814         /* Revise total_params and total_data in case they have changed
815          * downwards */
816
817         if (SVAL(req->vwv+0, 0) < state->total_param)
818                 state->total_param = SVAL(req->vwv+0, 0);
819         if (SVAL(req->vwv+1, 0) < state->total_data)
820                 state->total_data = SVAL(req->vwv+1, 0);
821
822         pcnt = SVAL(req->vwv+2, 0);
823         poff = SVAL(req->vwv+3, 0);
824         pdisp = SVAL(req->vwv+4, 0);
825
826         dcnt = SVAL(req->vwv+5, 0);
827         doff = SVAL(req->vwv+6, 0);
828         ddisp = SVAL(req->vwv+7, 0);
829
830         state->received_param += pcnt;
831         state->received_data += dcnt;
832
833         if ((state->received_data > state->total_data) ||
834             (state->received_param > state->total_param))
835                 goto bad_param;
836
837         if (pcnt) {
838                 if (trans_oob(state->total_param, pdisp, pcnt)
839                     || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
840                         goto bad_param;
841                 }
842                 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
843         }
844
845         if (dcnt) {
846                 if (trans_oob(state->total_data, ddisp, dcnt)
847                     || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
848                         goto bad_param;
849                 }
850                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
851         }
852
853         if ((state->received_param < state->total_param) ||
854             (state->received_data < state->total_data)) {
855                 END_PROFILE(SMBtranss);
856                 return;
857         }
858
859         talloc_steal(talloc_tos(), state);
860
861         handle_trans(conn, req, state);
862
863         DLIST_REMOVE(conn->pending_trans, state);
864         SAFE_FREE(state->data);
865         SAFE_FREE(state->param);
866         TALLOC_FREE(state);
867
868         END_PROFILE(SMBtranss);
869         return;
870
871   bad_param:
872
873         DEBUG(0,("reply_transs: invalid trans parameters\n"));
874         DLIST_REMOVE(conn->pending_trans, state);
875         SAFE_FREE(state->data);
876         SAFE_FREE(state->param);
877         TALLOC_FREE(state);
878         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
879         END_PROFILE(SMBtranss);
880         return;
881 }