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