r24224: Check wct in reply_trans
[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
29 extern int max_send;
30
31 #define NERR_notsupported 50
32
33 extern int smb_read_error;
34
35 /*******************************************************************
36  copies parameters and data, as needed, into the smb buffer
37
38  *both* the data and params sections should be aligned.  this
39  is fudged in the rpc pipes by 
40  at present, only the data section is.  this may be a possible
41  cause of some of the ipc problems being experienced.  lkcl26dec97
42
43  ******************************************************************/
44
45 static void copy_trans_params_and_data(char *outbuf, int align,
46                                 char *rparam, int param_offset, int param_len,
47                                 char *rdata, int data_offset, int data_len)
48 {
49         char *copy_into = smb_buf(outbuf)+1;
50
51         if(param_len < 0)
52                 param_len = 0;
53
54         if(data_len < 0)
55                 data_len = 0;
56
57         DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d]\n",
58                         param_offset, param_offset + param_len,
59                         data_offset , data_offset  + data_len));
60
61         if (param_len)
62                 memcpy(copy_into, &rparam[param_offset], param_len);
63
64         copy_into += param_len + align;
65
66         if (data_len )
67                 memcpy(copy_into, &rdata[data_offset], data_len);
68 }
69
70 /****************************************************************************
71  Send a trans reply.
72  ****************************************************************************/
73
74 void send_trans_reply(const char *inbuf,
75                         char *outbuf,
76                         char *rparam,
77                         int rparam_len,
78                         char *rdata,
79                         int rdata_len,
80                         BOOL buffer_too_large)
81 {
82         int this_ldata,this_lparam;
83         int tot_data_sent = 0;
84         int tot_param_sent = 0;
85         int align;
86
87         int ldata  = rdata  ? rdata_len : 0;
88         int lparam = rparam ? rparam_len : 0;
89
90         if (buffer_too_large)
91                 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata ));
92
93         this_lparam = MIN(lparam,max_send - 500); /* hack */
94         this_ldata  = MIN(ldata,max_send - (500+this_lparam));
95
96         align = ((this_lparam)%4);
97
98         if (buffer_too_large) {
99                 ERROR_BOTH(STATUS_BUFFER_OVERFLOW,ERRDOS,ERRmoredata);
100         }
101
102         set_message(inbuf,outbuf,10,1+align+this_ldata+this_lparam,True);
103
104         copy_trans_params_and_data(outbuf, align,
105                                 rparam, tot_param_sent, this_lparam,
106                                 rdata, tot_data_sent, this_ldata);
107
108         SSVAL(outbuf,smb_vwv0,lparam);
109         SSVAL(outbuf,smb_vwv1,ldata);
110         SSVAL(outbuf,smb_vwv3,this_lparam);
111         SSVAL(outbuf,smb_vwv4,smb_offset(smb_buf(outbuf)+1,outbuf));
112         SSVAL(outbuf,smb_vwv5,0);
113         SSVAL(outbuf,smb_vwv6,this_ldata);
114         SSVAL(outbuf,smb_vwv7,smb_offset(smb_buf(outbuf)+1+this_lparam+align,outbuf));
115         SSVAL(outbuf,smb_vwv8,0);
116         SSVAL(outbuf,smb_vwv9,0);
117
118         show_msg(outbuf);
119         if (!send_smb(smbd_server_fd(),outbuf))
120                 exit_server_cleanly("send_trans_reply: send_smb failed.");
121
122         tot_data_sent = this_ldata;
123         tot_param_sent = this_lparam;
124
125         while (tot_data_sent < ldata || tot_param_sent < lparam)
126         {
127                 this_lparam = MIN(lparam-tot_param_sent, max_send - 500); /* hack */
128                 this_ldata  = MIN(ldata -tot_data_sent, max_send - (500+this_lparam));
129
130                 if(this_lparam < 0)
131                         this_lparam = 0;
132
133                 if(this_ldata < 0)
134                         this_ldata = 0;
135
136                 align = (this_lparam%4);
137
138                 set_message(inbuf,outbuf,10,1+this_ldata+this_lparam+align,False);
139
140                 copy_trans_params_and_data(outbuf, align,
141                                            rparam, tot_param_sent, this_lparam,
142                                            rdata, tot_data_sent, this_ldata);
143                 
144                 SSVAL(outbuf,smb_vwv3,this_lparam);
145                 SSVAL(outbuf,smb_vwv4,smb_offset(smb_buf(outbuf)+1,outbuf));
146                 SSVAL(outbuf,smb_vwv5,tot_param_sent);
147                 SSVAL(outbuf,smb_vwv6,this_ldata);
148                 SSVAL(outbuf,smb_vwv7,smb_offset(smb_buf(outbuf)+1+this_lparam+align,outbuf));
149                 SSVAL(outbuf,smb_vwv8,tot_data_sent);
150                 SSVAL(outbuf,smb_vwv9,0);
151
152                 show_msg(outbuf);
153                 if (!send_smb(smbd_server_fd(),outbuf))
154                         exit_server_cleanly("send_trans_reply: send_smb failed.");
155
156                 tot_data_sent  += this_ldata;
157                 tot_param_sent += this_lparam;
158         }
159 }
160
161 /****************************************************************************
162  Start the first part of an RPC reply which began with an SMBtrans request.
163 ****************************************************************************/
164
165 static BOOL api_rpc_trans_reply(const char *inbuf,
166                                 char *outbuf,
167                                 smb_np_struct *p)
168 {
169         BOOL is_data_outstanding;
170         char *rdata = (char *)SMB_MALLOC(p->max_trans_reply);
171         int data_len;
172
173         if(rdata == NULL) {
174                 DEBUG(0,("api_rpc_trans_reply: malloc fail.\n"));
175                 return False;
176         }
177
178         if((data_len = read_from_pipe( p, rdata, p->max_trans_reply,
179                                         &is_data_outstanding)) < 0) {
180                 SAFE_FREE(rdata);
181                 return False;
182         }
183
184         send_trans_reply(inbuf, outbuf, NULL, 0, rdata, data_len, is_data_outstanding);
185
186         SAFE_FREE(rdata);
187         return True;
188 }
189
190 /****************************************************************************
191  WaitNamedPipeHandleState 
192 ****************************************************************************/
193
194 static BOOL api_WNPHS(const char *inbuf,
195                         char *outbuf,
196                         smb_np_struct *p,
197                         char *param,
198                         int param_len)
199 {
200         uint16 priority;
201
202         if (!param || param_len < 2)
203                 return False;
204
205         priority = SVAL(param,0);
206         DEBUG(4,("WaitNamedPipeHandleState priority %x\n", priority));
207
208         if (wait_rpc_pipe_hnd_state(p, priority)) {
209                 /* now send the reply */
210                 send_trans_reply(inbuf, outbuf, NULL, 0, NULL, 0, False);
211                 return True;
212         }
213         return False;
214 }
215
216
217 /****************************************************************************
218  SetNamedPipeHandleState 
219 ****************************************************************************/
220
221 static BOOL api_SNPHS(const char *inbuf,
222                         char *outbuf,
223                         smb_np_struct *p,
224                         char *param,
225                         int param_len)
226 {
227         uint16 id;
228
229         if (!param || param_len < 2)
230                 return False;
231
232         id = SVAL(param,0);
233         DEBUG(4,("SetNamedPipeHandleState to code %x\n", id));
234
235         if (set_rpc_pipe_hnd_state(p, id)) {
236                 /* now send the reply */
237                 send_trans_reply(inbuf, outbuf, NULL, 0, NULL, 0, False);
238                 return True;
239         }
240         return False;
241 }
242
243
244 /****************************************************************************
245  When no reply is generated, indicate unsupported.
246  ****************************************************************************/
247
248 static BOOL api_no_reply(const char *inbuf, char *outbuf, int max_rdata_len)
249 {
250         char rparam[4];
251
252         /* unsupported */
253         SSVAL(rparam,0,NERR_notsupported);
254         SSVAL(rparam,2,0); /* converter word */
255
256         DEBUG(3,("Unsupported API fd command\n"));
257
258         /* now send the reply */
259         send_trans_reply(inbuf, outbuf, rparam, 4, NULL, 0, False);
260
261         return -1;
262 }
263
264 /****************************************************************************
265  Handle remote api calls delivered to a named pipe already opened.
266  ****************************************************************************/
267
268 static int api_fd_reply(connection_struct *conn,
269                         uint16 vuid,
270                         const char *inbuf,
271                         char *outbuf,
272                         uint16 *setup,
273                         char *data,
274                         char *params,
275                         int suwcnt,
276                         int tdscnt,
277                         int tpscnt,
278                         int mdrcnt,
279                         int mprcnt)
280 {
281         BOOL reply = False;
282         smb_np_struct *p = NULL;
283         int pnum;
284         int subcommand;
285
286         DEBUG(5,("api_fd_reply\n"));
287
288         /* First find out the name of this file. */
289         if (suwcnt != 2) {
290                 DEBUG(0,("Unexpected named pipe transaction.\n"));
291                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
292         }
293
294         /* Get the file handle and hence the file name. */
295         /* 
296          * NB. The setup array has already been transformed
297          * via SVAL and so is in gost byte order.
298          */
299         pnum = ((int)setup[1]) & 0xFFFF;
300         subcommand = ((int)setup[0]) & 0xFFFF;
301
302         if(!(p = get_rpc_pipe(pnum))) {
303                 if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
304                         /* Win9x does this call with a unicode pipe name, not a pnum. */
305                         /* Just return success for now... */
306                         DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
307                         send_trans_reply(inbuf, outbuf, NULL, 0, NULL, 0, False);
308                         return -1;
309                 }
310
311                 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
312                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
313         }
314
315         if (vuid != p->vuid) {
316                 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
317                           "expected %d\n", pnum, vuid, p->vuid));
318                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
319         }
320
321         DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n", subcommand, p->name, pnum));
322
323         /* record maximum data length that can be transmitted in an SMBtrans */
324         p->max_trans_reply = mdrcnt;
325
326         DEBUG(10,("api_fd_reply: p:%p max_trans_reply: %d\n", p, p->max_trans_reply));
327
328         switch (subcommand) {
329         case TRANSACT_DCERPCCMD:
330                 /* dce/rpc command */
331                 reply = write_to_pipe(p, data, tdscnt);
332                 if (reply)
333                         reply = api_rpc_trans_reply(inbuf, outbuf, p);
334                 break;
335         case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
336                 /* Wait Named Pipe Handle state */
337                 reply = api_WNPHS(inbuf, outbuf, p, params, tpscnt);
338                 break;
339         case TRANSACT_SETNAMEDPIPEHANDLESTATE:
340                 /* Set Named Pipe Handle state */
341                 reply = api_SNPHS(inbuf, outbuf, p, params, tpscnt);
342                 break;
343         default:
344                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
345         }
346
347         if (!reply)
348                 return api_no_reply(inbuf, outbuf, mdrcnt);
349
350         return -1;
351 }
352
353 /****************************************************************************
354  Handle named pipe commands.
355 ****************************************************************************/
356
357 static int named_pipe(connection_struct *conn,
358                         uint16 vuid,
359                         const char *inbuf,
360                         char *outbuf,
361                         char *name,
362                         uint16 *setup,
363                         char *data,
364                         char *params,
365                         int suwcnt,
366                         int tdscnt,
367                         int tpscnt,
368                         int msrcnt,
369                         int mdrcnt,
370                         int mprcnt)
371 {
372         DEBUG(3,("named pipe command on <%s> name\n", name));
373
374         if (strequal(name,"LANMAN")) {
375                 return api_reply(conn,
376                                 vuid,
377                                 inbuf,
378                                 outbuf,
379                                 data,
380                                 params,
381                                 tdscnt,
382                                 tpscnt,
383                                 mdrcnt,
384                                 mprcnt);
385         }
386
387         if (strequal(name,"WKSSVC") ||
388             strequal(name,"SRVSVC") ||
389             strequal(name,"WINREG") ||
390             strequal(name,"SAMR") ||
391             strequal(name,"LSARPC")) {
392                 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
393                 return api_fd_reply(conn,
394                                         vuid,
395                                         inbuf,
396                                         outbuf,
397                                         setup,
398                                         data,
399                                         params,
400                                         suwcnt,
401                                         tdscnt,
402                                         tpscnt,
403                                         mdrcnt,
404                                         mprcnt);
405         }
406
407         if (strlen(name) < 1) {
408                 return api_fd_reply(conn,
409                                         vuid,
410                                         inbuf,
411                                         outbuf,
412                                         setup,
413                                         data,
414                                         params,
415                                         suwcnt,
416                                         tdscnt,
417                                         tpscnt,
418                                         mdrcnt,
419                                         mprcnt);
420         }
421
422         if (setup)
423                 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n", (int)setup[0],(int)setup[1]));
424
425         return 0;
426 }
427
428 static NTSTATUS handle_trans(connection_struct *conn,
429                                 struct trans_state *state,
430                                 const char *inbuf,
431                                 char *outbuf,
432                                 int *outsize)
433 {
434         char *local_machine_name;
435         int name_offset = 0;
436
437         DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
438                  state->name,(unsigned int)state->total_data,(unsigned int)state->total_param,
439                  (unsigned int)state->setup_count));
440
441         /*
442          * WinCE wierdness....
443          */
444
445         local_machine_name = talloc_asprintf(state, "\\%s\\",
446                                              get_local_machine_name());
447
448         if (local_machine_name == NULL) {
449                 return NT_STATUS_NO_MEMORY;
450         }
451
452         if (strnequal(state->name, local_machine_name,
453                       strlen(local_machine_name))) {
454                 name_offset = strlen(local_machine_name)-1;
455         }
456
457         if (!strnequal(&state->name[name_offset], "\\PIPE",
458                        strlen("\\PIPE"))) {
459                 return NT_STATUS_NOT_SUPPORTED;
460         }
461         
462         name_offset += strlen("\\PIPE");
463
464         /* Win9x weirdness.  When talking to a unicode server Win9x
465            only sends \PIPE instead of \PIPE\ */
466
467         if (state->name[name_offset] == '\\')
468                 name_offset++;
469
470         DEBUG(5,("calling named_pipe\n"));
471         *outsize = named_pipe(conn,
472                                 state->vuid,
473                                 inbuf,
474                                 outbuf,
475                                 state->name+name_offset,
476                                 state->setup,state->data,
477                                 state->param,
478                                 state->setup_count,state->total_data,
479                                 state->total_param,
480                                 state->max_setup_return,
481                                 state->max_data_return,
482                                 state->max_param_return);
483
484         if (*outsize == 0) {
485                 return NT_STATUS_NOT_SUPPORTED;
486         }
487
488         if (state->close_on_completion)
489                 close_cnum(conn,state->vuid);
490
491         return NT_STATUS_OK;
492 }
493
494 /****************************************************************************
495  Reply to a SMBtrans.
496  ****************************************************************************/
497
498 int reply_trans(connection_struct *conn,
499                 char *inbuf,
500                 char *outbuf,
501                 int size,
502                 int bufsize)
503 {
504         int outsize = 0;
505         unsigned int dsoff;
506         unsigned int dscnt;
507         unsigned int psoff;
508         unsigned int pscnt;
509         struct trans_state *state;
510         NTSTATUS result;
511
512         START_PROFILE(SMBtrans);
513
514         if (SVAL(inbuf, smb_wct) < 10) {
515                 END_PROFILE(SMBtrans);
516                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
517         }
518
519         dsoff = SVAL(inbuf, smb_dsoff);
520         dscnt = SVAL(inbuf, smb_dscnt);
521         psoff = SVAL(inbuf, smb_psoff);
522         pscnt = SVAL(inbuf, smb_pscnt);
523
524         result = allow_new_trans(conn->pending_trans, SVAL(inbuf, smb_mid));
525         if (!NT_STATUS_IS_OK(result)) {
526                 DEBUG(2, ("Got invalid trans request: %s\n",
527                           nt_errstr(result)));
528                 END_PROFILE(SMBtrans);
529                 return ERROR_NT(result);
530         }
531
532         if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
533                 DEBUG(0, ("talloc failed\n"));
534                 END_PROFILE(SMBtrans);
535                 return ERROR_NT(NT_STATUS_NO_MEMORY);
536         }
537
538         state->cmd = SMBtrans;
539
540         state->mid = SVAL(inbuf, smb_mid);
541         state->vuid = SVAL(inbuf, smb_uid);
542         state->setup_count = CVAL(inbuf, smb_suwcnt);
543         state->setup = NULL;
544         state->total_param = SVAL(inbuf, smb_tpscnt);
545         state->param = NULL;
546         state->total_data = SVAL(inbuf, smb_tdscnt);
547         state->data = NULL;
548         state->max_param_return = SVAL(inbuf, smb_mprcnt);
549         state->max_data_return = SVAL(inbuf, smb_mdrcnt);
550         state->max_setup_return = CVAL(inbuf, smb_msrcnt);
551         state->close_on_completion = BITSETW(inbuf+smb_vwv5,0);
552         state->one_way = BITSETW(inbuf+smb_vwv5,1);
553
554         memset(state->name, '\0',sizeof(state->name));
555         srvstr_pull_buf(inbuf, SVAL(inbuf, smb_flg2), state->name,
556                         smb_buf(inbuf), sizeof(state->name), STR_TERMINATE);
557         
558         if ((dscnt > state->total_data) || (pscnt > state->total_param))
559                 goto bad_param;
560
561         if (state->total_data)  {
562                 /* Can't use talloc here, the core routines do realloc on the
563                  * params and data. Out of paranoia, 100 bytes too many. */
564                 state->data = (char *)SMB_MALLOC(state->total_data+100);
565                 if (state->data == NULL) {
566                         DEBUG(0,("reply_trans: data malloc fail for %u "
567                                  "bytes !\n", (unsigned int)state->total_data));
568                         TALLOC_FREE(state);
569                         END_PROFILE(SMBtrans);
570                         return(ERROR_DOS(ERRDOS,ERRnomem));
571                 } 
572                 /* null-terminate the slack space */
573                 memset(&state->data[state->total_data], 0, 100);
574                 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
575                         goto bad_param;
576                 if ((smb_base(inbuf)+dsoff+dscnt > inbuf + size) ||
577                     (smb_base(inbuf)+dsoff+dscnt < smb_base(inbuf)))
578                         goto bad_param;
579
580                 memcpy(state->data,smb_base(inbuf)+dsoff,dscnt);
581         }
582
583         if (state->total_param) {
584                 /* Can't use talloc here, the core routines do realloc on the
585                  * params and data. Out of paranoia, 100 bytes too many */
586                 state->param = (char *)SMB_MALLOC(state->total_param+100);
587                 if (state->param == NULL) {
588                         DEBUG(0,("reply_trans: param malloc fail for %u "
589                                  "bytes !\n", (unsigned int)state->total_param));
590                         SAFE_FREE(state->data);
591                         TALLOC_FREE(state);
592                         END_PROFILE(SMBtrans);
593                         return(ERROR_DOS(ERRDOS,ERRnomem));
594                 } 
595                 /* null-terminate the slack space */
596                 memset(&state->param[state->total_param], 0, 100);
597                 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
598                         goto bad_param;
599                 if ((smb_base(inbuf)+psoff+pscnt > inbuf + size) ||
600                     (smb_base(inbuf)+psoff+pscnt < smb_base(inbuf)))
601                         goto bad_param;
602
603                 memcpy(state->param,smb_base(inbuf)+psoff,pscnt);
604         }
605
606         state->received_data  = dscnt;
607         state->received_param = pscnt;
608
609         if (state->setup_count) {
610                 unsigned int i;
611                 if((state->setup = TALLOC_ARRAY(
612                             state, uint16, state->setup_count)) == NULL) {
613                         DEBUG(0,("reply_trans: setup malloc fail for %u "
614                                  "bytes !\n", (unsigned int)
615                                  (state->setup_count * sizeof(uint16))));
616                         SAFE_FREE(state->data);
617                         SAFE_FREE(state->param);
618                         TALLOC_FREE(state);
619                         END_PROFILE(SMBtrans);
620                         return(ERROR_DOS(ERRDOS,ERRnomem));
621                 } 
622                 if (inbuf+smb_vwv14+(state->setup_count*SIZEOFWORD) >
623                     inbuf + size)
624                         goto bad_param;
625                 if ((smb_vwv14+(state->setup_count*SIZEOFWORD) < smb_vwv14) ||
626                     (smb_vwv14+(state->setup_count*SIZEOFWORD) <
627                      (state->setup_count*SIZEOFWORD)))
628                         goto bad_param;
629
630                 for (i=0;i<state->setup_count;i++)
631                         state->setup[i] = SVAL(inbuf,smb_vwv14+i*SIZEOFWORD);
632         }
633
634         state->received_param = pscnt;
635
636         if ((state->received_param == state->total_param) &&
637             (state->received_data == state->total_data)) {
638
639                 result = handle_trans(conn, state, inbuf, outbuf, &outsize);
640
641                 SAFE_FREE(state->data);
642                 SAFE_FREE(state->param);
643                 TALLOC_FREE(state);
644
645                 if (!NT_STATUS_IS_OK(result)) {
646                         END_PROFILE(SMBtrans);
647                         return ERROR_NT(result);
648                 }
649
650                 if (outsize == 0) {
651                         END_PROFILE(SMBtrans);
652                         return ERROR_NT(NT_STATUS_INTERNAL_ERROR);
653                 }
654
655                 END_PROFILE(SMBtrans);
656                 return outsize;
657         }
658
659         DLIST_ADD(conn->pending_trans, state);
660
661         /* We need to send an interim response then receive the rest
662            of the parameter/data bytes */
663         outsize = set_message(inbuf,outbuf,0,0,True);
664         show_msg(outbuf);
665         END_PROFILE(SMBtrans);
666         return outsize;
667
668   bad_param:
669
670         DEBUG(0,("reply_trans: invalid trans parameters\n"));
671         SAFE_FREE(state->data);
672         SAFE_FREE(state->param);
673         TALLOC_FREE(state);
674         END_PROFILE(SMBtrans);
675         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
676 }
677
678 /****************************************************************************
679  Reply to a secondary SMBtrans.
680  ****************************************************************************/
681
682 int reply_transs(connection_struct *conn, char *inbuf,char *outbuf,
683                  int size, int bufsize)
684 {
685         int outsize = 0;
686         unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
687         struct trans_state *state;
688         NTSTATUS result;
689
690         START_PROFILE(SMBtranss);
691
692         show_msg(inbuf);
693
694         if (SVAL(inbuf, smb_wct) < 10) {
695                 END_PROFILE(SMBtranss);
696                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
697         }
698
699         for (state = conn->pending_trans; state != NULL;
700              state = state->next) {
701                 if (state->mid == SVAL(inbuf,smb_mid)) {
702                         break;
703                 }
704         }
705
706         if ((state == NULL) || (state->cmd != SMBtrans)) {
707                 END_PROFILE(SMBtranss);
708                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
709         }
710
711         /* Revise total_params and total_data in case they have changed
712          * downwards */
713
714         if (SVAL(inbuf, smb_vwv0) < state->total_param)
715                 state->total_param = SVAL(inbuf,smb_vwv0);
716         if (SVAL(inbuf, smb_vwv1) < state->total_data)
717                 state->total_data = SVAL(inbuf,smb_vwv1);
718
719         pcnt = SVAL(inbuf, smb_spscnt);
720         poff = SVAL(inbuf, smb_spsoff);
721         pdisp = SVAL(inbuf, smb_spsdisp);
722
723         dcnt = SVAL(inbuf, smb_sdscnt);
724         doff = SVAL(inbuf, smb_sdsoff);
725         ddisp = SVAL(inbuf, smb_sdsdisp);
726
727         state->received_param += pcnt;
728         state->received_data += dcnt;
729                 
730         if ((state->received_data > state->total_data) ||
731             (state->received_param > state->total_param))
732                 goto bad_param;
733                 
734         if (pcnt) {
735                 if (pdisp+pcnt > state->total_param)
736                         goto bad_param;
737                 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
738                         goto bad_param;
739                 if (pdisp > state->total_param)
740                         goto bad_param;
741                 if ((smb_base(inbuf) + poff + pcnt > inbuf + size) ||
742                     (smb_base(inbuf) + poff + pcnt < smb_base(inbuf)))
743                         goto bad_param;
744                 if (state->param + pdisp < state->param)
745                         goto bad_param;
746
747                 memcpy(state->param+pdisp,smb_base(inbuf)+poff,
748                        pcnt);
749         }
750
751         if (dcnt) {
752                 if (ddisp+dcnt > state->total_data)
753                         goto bad_param;
754                 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
755                         goto bad_param;
756                 if (ddisp > state->total_data)
757                         goto bad_param;
758                 if ((smb_base(inbuf) + doff + dcnt > inbuf + size) ||
759                     (smb_base(inbuf) + doff + dcnt < smb_base(inbuf)))
760                         goto bad_param;
761                 if (state->data + ddisp < state->data)
762                         goto bad_param;
763
764                 memcpy(state->data+ddisp, smb_base(inbuf)+doff,
765                        dcnt);      
766         }
767
768         if ((state->received_param < state->total_param) ||
769             (state->received_data < state->total_data)) {
770                 END_PROFILE(SMBtranss);
771                 return -1;
772         }
773
774         /* construct_reply_common has done us the favor to pre-fill the
775          * command field with SMBtranss which is wrong :-)
776          */
777         SCVAL(outbuf,smb_com,SMBtrans);
778
779         result = handle_trans(conn, state, inbuf, outbuf, &outsize);
780
781         DLIST_REMOVE(conn->pending_trans, state);
782         SAFE_FREE(state->data);
783         SAFE_FREE(state->param);
784         TALLOC_FREE(state);
785
786         if ((outsize == 0) || !NT_STATUS_IS_OK(result)) {
787                 END_PROFILE(SMBtranss);
788                 return(ERROR_DOS(ERRSRV,ERRnosupport));
789         }
790         
791         END_PROFILE(SMBtranss);
792         return(outsize);
793
794   bad_param:
795
796         DEBUG(0,("reply_transs: invalid trans parameters\n"));
797         DLIST_REMOVE(conn->pending_trans, state);
798         SAFE_FREE(state->data);
799         SAFE_FREE(state->param);
800         TALLOC_FREE(state);
801         END_PROFILE(SMBtranss);
802         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
803 }