Merge branch 'master' of ssh://git.samba.org/data/git/samba into regsrv
[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 static void api_no_reply(connection_struct *conn, 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(connection_struct *conn, const uint8_t *inbuf,
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         char *outbuf;
94
95         int ldata  = rdata  ? rdata_len : 0;
96         int lparam = rparam ? rparam_len : 0;
97
98         if (buffer_too_large)
99                 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata ));
100
101         this_lparam = MIN(lparam,max_send - 500); /* hack */
102         this_ldata  = MIN(ldata,max_send - (500+this_lparam));
103
104         align = ((this_lparam)%4);
105
106         if (!create_outbuf(talloc_tos(), (char *)inbuf, &outbuf,
107                            10, 1+align+this_ldata+this_lparam)) {
108                 smb_panic("could not allocate outbuf");
109         }
110
111         copy_trans_params_and_data(outbuf, align,
112                                 rparam, tot_param_sent, this_lparam,
113                                 rdata, tot_data_sent, this_ldata);
114
115         SSVAL(outbuf,smb_vwv0,lparam);
116         SSVAL(outbuf,smb_vwv1,ldata);
117         SSVAL(outbuf,smb_vwv3,this_lparam);
118         SSVAL(outbuf,smb_vwv4,smb_offset(smb_buf(outbuf)+1,outbuf));
119         SSVAL(outbuf,smb_vwv5,0);
120         SSVAL(outbuf,smb_vwv6,this_ldata);
121         SSVAL(outbuf,smb_vwv7,smb_offset(smb_buf(outbuf)+1+this_lparam+align,
122                                          outbuf));
123         SSVAL(outbuf,smb_vwv8,0);
124         SSVAL(outbuf,smb_vwv9,0);
125
126         if (buffer_too_large) {
127                 error_packet_set((char *)outbuf, ERRDOS, ERRmoredata,
128                                  STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
129         }
130
131         show_msg(outbuf);
132         if (!srv_send_smb(smbd_server_fd(), (char *)outbuf,
133                           IS_CONN_ENCRYPTED(conn))) {
134                 exit_server_cleanly("send_trans_reply: srv_send_smb failed.");
135         }
136
137         TALLOC_FREE(outbuf);
138
139         tot_data_sent = this_ldata;
140         tot_param_sent = this_lparam;
141
142         while (tot_data_sent < ldata || tot_param_sent < lparam)
143         {
144                 this_lparam = MIN(lparam-tot_param_sent,
145                                   max_send - 500); /* hack */
146                 this_ldata  = MIN(ldata -tot_data_sent,
147                                   max_send - (500+this_lparam));
148
149                 if(this_lparam < 0)
150                         this_lparam = 0;
151
152                 if(this_ldata < 0)
153                         this_ldata = 0;
154
155                 align = (this_lparam%4);
156
157                 if (!create_outbuf(talloc_tos(), (char *)inbuf, &outbuf,
158                                    10, 1+align+this_ldata+this_lparam)) {
159                         smb_panic("could not allocate outbuf");
160                 }
161
162                 copy_trans_params_and_data(outbuf, align,
163                                            rparam, tot_param_sent, this_lparam,
164                                            rdata, tot_data_sent, this_ldata);
165
166                 SSVAL(outbuf,smb_vwv3,this_lparam);
167                 SSVAL(outbuf,smb_vwv4,smb_offset(smb_buf(outbuf)+1,outbuf));
168                 SSVAL(outbuf,smb_vwv5,tot_param_sent);
169                 SSVAL(outbuf,smb_vwv6,this_ldata);
170                 SSVAL(outbuf,smb_vwv7,
171                       smb_offset(smb_buf(outbuf)+1+this_lparam+align, outbuf));
172                 SSVAL(outbuf,smb_vwv8,tot_data_sent);
173                 SSVAL(outbuf,smb_vwv9,0);
174
175                 if (buffer_too_large) {
176                         error_packet_set(outbuf, ERRDOS, ERRmoredata,
177                                          STATUS_BUFFER_OVERFLOW,
178                                          __LINE__, __FILE__);
179                 }
180
181                 show_msg(outbuf);
182                 if (!srv_send_smb(smbd_server_fd(), outbuf,
183                                   IS_CONN_ENCRYPTED(conn)))
184                         exit_server_cleanly("send_trans_reply: srv_send_smb "
185                                             "failed.");
186
187                 tot_data_sent  += this_ldata;
188                 tot_param_sent += this_lparam;
189                 TALLOC_FREE(outbuf);
190         }
191 }
192
193 /****************************************************************************
194  Start the first part of an RPC reply which began with an SMBtrans request.
195 ****************************************************************************/
196
197 static void api_rpc_trans_reply(connection_struct *conn,
198                                 struct smb_request *req,
199                                 files_struct *fsp,
200                                 int max_trans_reply)
201 {
202         bool is_data_outstanding;
203         uint8_t *rdata = SMB_MALLOC_ARRAY(uint8_t, max_trans_reply);
204         ssize_t data_len;
205         NTSTATUS status;
206
207         if(rdata == NULL) {
208                 DEBUG(0,("api_rpc_trans_reply: malloc fail.\n"));
209                 reply_nterror(req, NT_STATUS_NO_MEMORY);
210                 return;
211         }
212
213         status = np_read(fsp, rdata, max_trans_reply, &data_len,
214                          &is_data_outstanding);
215         if (!NT_STATUS_IS_OK(status)) {
216                 SAFE_FREE(rdata);
217                 api_no_reply(conn,req);
218                 return;
219         }
220
221         send_trans_reply(conn, req->inbuf, NULL, 0, (char *)rdata, data_len,
222                          is_data_outstanding);
223         SAFE_FREE(rdata);
224         return;
225 }
226
227 /****************************************************************************
228  WaitNamedPipeHandleState 
229 ****************************************************************************/
230
231 static void api_WNPHS(connection_struct *conn, struct smb_request *req,
232                       struct files_struct *fsp, char *param, int param_len)
233 {
234         if (!param || param_len < 2) {
235                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
236                 return;
237         }
238
239         DEBUG(4,("WaitNamedPipeHandleState priority %x\n",
240                  (int)SVAL(param,0)));
241
242         send_trans_reply(conn, req->inbuf, NULL, 0, NULL, 0, False);
243 }
244
245
246 /****************************************************************************
247  SetNamedPipeHandleState 
248 ****************************************************************************/
249
250 static void api_SNPHS(connection_struct *conn, struct smb_request *req,
251                       struct files_struct *fsp, char *param, int param_len)
252 {
253         if (!param || param_len < 2) {
254                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
255                 return;
256         }
257
258         DEBUG(4,("SetNamedPipeHandleState to code %x\n", (int)SVAL(param,0)));
259
260         send_trans_reply(conn, req->inbuf, NULL, 0, NULL, 0, False);
261 }
262
263
264 /****************************************************************************
265  When no reply is generated, indicate unsupported.
266  ****************************************************************************/
267
268 static void api_no_reply(connection_struct *conn, struct smb_request *req)
269 {
270         char rparam[4];
271
272         /* unsupported */
273         SSVAL(rparam,0,NERR_notsupported);
274         SSVAL(rparam,2,0); /* converter word */
275
276         DEBUG(3,("Unsupported API fd command\n"));
277
278         /* now send the reply */
279         send_trans_reply(conn, req->inbuf, rparam, 4, NULL, 0, False);
280
281         return;
282 }
283
284 /****************************************************************************
285  Handle remote api calls delivered to a named pipe already opened.
286  ****************************************************************************/
287
288 static void api_fd_reply(connection_struct *conn, uint16 vuid,
289                          struct smb_request *req,
290                          uint16 *setup, uint8_t *data, char *params,
291                          int suwcnt, int tdscnt, int tpscnt,
292                          int mdrcnt, int mprcnt)
293 {
294         struct files_struct *fsp;
295         int pnum;
296         int subcommand;
297         NTSTATUS status;
298
299         DEBUG(5,("api_fd_reply\n"));
300
301         /* First find out the name of this file. */
302         if (suwcnt != 2) {
303                 DEBUG(0,("Unexpected named pipe transaction.\n"));
304                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
305                 return;
306         }
307
308         /* Get the file handle and hence the file name. */
309         /* 
310          * NB. The setup array has already been transformed
311          * via SVAL and so is in host byte order.
312          */
313         pnum = ((int)setup[1]) & 0xFFFF;
314         subcommand = ((int)setup[0]) & 0xFFFF;
315
316         fsp = file_fsp(req, pnum);
317
318         if (!fsp_is_np(fsp)) {
319                 if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
320                         /* Win9x does this call with a unicode pipe name, not a pnum. */
321                         /* Just return success for now... */
322                         DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
323                         send_trans_reply(conn, req->inbuf, NULL, 0, NULL, 0,
324                                          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 != fsp->vuid) {
334                 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
335                           "expected %d\n", pnum, vuid, fsp->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",
341                  subcommand, fsp->fsp_name, pnum));
342
343         DEBUG(10, ("api_fd_reply: p:%p max_trans_reply: %d\n", fsp, mdrcnt));
344
345         switch (subcommand) {
346         case TRANSACT_DCERPCCMD: {
347                 /* dce/rpc command */
348                 ssize_t nwritten;
349                 status = np_write(fsp, data, tdscnt, &nwritten);
350                 if (!NT_STATUS_IS_OK(status)) {
351                         api_no_reply(conn, req);
352                         return;
353                 }
354                 api_rpc_trans_reply(conn, req, fsp, mdrcnt);
355                 break;
356         }
357         case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
358                 /* Wait Named Pipe Handle state */
359                 api_WNPHS(conn, req, fsp, params, tpscnt);
360                 break;
361         case TRANSACT_SETNAMEDPIPEHANDLESTATE:
362                 /* Set Named Pipe Handle state */
363                 api_SNPHS(conn, req, fsp, 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                        const 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, (uint8_t *)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, (uint8_t *)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                 req->conn = NULL;
478         }
479
480         return;
481 }
482
483 /****************************************************************************
484  Reply to a SMBtrans.
485  ****************************************************************************/
486
487 void reply_trans(struct smb_request *req)
488 {
489         connection_struct *conn = req->conn;
490         unsigned int dsoff;
491         unsigned int dscnt;
492         unsigned int psoff;
493         unsigned int pscnt;
494         struct trans_state *state;
495         NTSTATUS result;
496         unsigned int size;
497         unsigned int av_size;
498
499         START_PROFILE(SMBtrans);
500
501         if (req->wct < 14) {
502                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
503                 END_PROFILE(SMBtrans);
504                 return;
505         }
506
507         size = smb_len(req->inbuf) + 4;
508         av_size = smb_len(req->inbuf);
509         dsoff = SVAL(req->inbuf, smb_dsoff);
510         dscnt = SVAL(req->inbuf, smb_dscnt);
511         psoff = SVAL(req->inbuf, smb_psoff);
512         pscnt = SVAL(req->inbuf, smb_pscnt);
513
514         result = allow_new_trans(conn->pending_trans, req->mid);
515         if (!NT_STATUS_IS_OK(result)) {
516                 DEBUG(2, ("Got invalid trans request: %s\n",
517                           nt_errstr(result)));
518                 reply_nterror(req, result);
519                 END_PROFILE(SMBtrans);
520                 return;
521         }
522
523         if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
524                 DEBUG(0, ("talloc failed\n"));
525                 reply_nterror(req, NT_STATUS_NO_MEMORY);
526                 END_PROFILE(SMBtrans);
527                 return;
528         }
529
530         state->cmd = SMBtrans;
531
532         state->mid = req->mid;
533         state->vuid = req->vuid;
534         state->setup_count = CVAL(req->inbuf, smb_suwcnt);
535         state->setup = NULL;
536         state->total_param = SVAL(req->inbuf, smb_tpscnt);
537         state->param = NULL;
538         state->total_data = SVAL(req->inbuf, smb_tdscnt);
539         state->data = NULL;
540         state->max_param_return = SVAL(req->inbuf, smb_mprcnt);
541         state->max_data_return = SVAL(req->inbuf, smb_mdrcnt);
542         state->max_setup_return = CVAL(req->inbuf, smb_msrcnt);
543         state->close_on_completion = BITSETW(req->inbuf+smb_vwv5,0);
544         state->one_way = BITSETW(req->inbuf+smb_vwv5,1);
545
546         srvstr_pull_buf_talloc(state, req->inbuf, req->flags2, &state->name,
547                         smb_buf(req->inbuf), STR_TERMINATE);
548
549         if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
550                         !state->name)
551                 goto bad_param;
552
553         if (state->total_data)  {
554                 /* Can't use talloc here, the core routines do realloc on the
555                  * params and data. Out of paranoia, 100 bytes too many. */
556                 state->data = (char *)SMB_MALLOC(state->total_data+100);
557                 if (state->data == NULL) {
558                         DEBUG(0,("reply_trans: data malloc fail for %u "
559                                  "bytes !\n", (unsigned int)state->total_data));
560                         TALLOC_FREE(state);
561                         reply_nterror(req, NT_STATUS_NO_MEMORY);
562                         END_PROFILE(SMBtrans);
563                         return;
564                 }
565                 /* null-terminate the slack space */
566                 memset(&state->data[state->total_data], 0, 100);
567
568                 if (dscnt > state->total_data ||
569                                 dsoff+dscnt < dsoff) {
570                         goto bad_param;
571                 }
572
573                 if (dsoff > av_size ||
574                                 dscnt > av_size ||
575                                 dsoff+dscnt > av_size) {
576                         goto bad_param;
577                 }
578
579                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
580         }
581
582         if (state->total_param) {
583                 /* Can't use talloc here, the core routines do realloc on the
584                  * params and data. Out of paranoia, 100 bytes too many */
585                 state->param = (char *)SMB_MALLOC(state->total_param+100);
586                 if (state->param == NULL) {
587                         DEBUG(0,("reply_trans: param malloc fail for %u "
588                                  "bytes !\n", (unsigned int)state->total_param));
589                         SAFE_FREE(state->data);
590                         TALLOC_FREE(state);
591                         reply_nterror(req, NT_STATUS_NO_MEMORY);
592                         END_PROFILE(SMBtrans);
593                         return;
594                 } 
595                 /* null-terminate the slack space */
596                 memset(&state->param[state->total_param], 0, 100);
597
598                 if (pscnt > state->total_param ||
599                                 psoff+pscnt < psoff) {
600                         goto bad_param;
601                 }
602
603                 if (psoff > av_size ||
604                                 pscnt > av_size ||
605                                 psoff+pscnt > av_size) {
606                         goto bad_param;
607                 }
608
609                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
610         }
611
612         state->received_data  = dscnt;
613         state->received_param = pscnt;
614
615         if (state->setup_count) {
616                 unsigned int i;
617                 if((state->setup = TALLOC_ARRAY(
618                             state, uint16, state->setup_count)) == NULL) {
619                         DEBUG(0,("reply_trans: setup malloc fail for %u "
620                                  "bytes !\n", (unsigned int)
621                                  (state->setup_count * sizeof(uint16))));
622                         SAFE_FREE(state->data);
623                         SAFE_FREE(state->param);
624                         TALLOC_FREE(state);
625                         reply_nterror(req, NT_STATUS_NO_MEMORY);
626                         END_PROFILE(SMBtrans);
627                         return;
628                 } 
629                 if (req->inbuf+smb_vwv14+(state->setup_count*SIZEOFWORD) >
630                     req->inbuf + size)
631                         goto bad_param;
632                 if ((smb_vwv14+(state->setup_count*SIZEOFWORD) < smb_vwv14) ||
633                     (smb_vwv14+(state->setup_count*SIZEOFWORD) <
634                      (state->setup_count*SIZEOFWORD)))
635                         goto bad_param;
636
637                 for (i=0;i<state->setup_count;i++)
638                         state->setup[i] = SVAL(req->inbuf,
639                                                smb_vwv14+i*SIZEOFWORD);
640         }
641
642         state->received_param = pscnt;
643
644         if ((state->received_param != state->total_param) ||
645             (state->received_data != state->total_data)) {
646                 DLIST_ADD(conn->pending_trans, state);
647
648                 /* We need to send an interim response then receive the rest
649                    of the parameter/data bytes */
650                 reply_outbuf(req, 0, 0);
651                 show_msg((char *)req->outbuf);
652                 END_PROFILE(SMBtrans);
653                 return;
654         }
655
656         handle_trans(conn, req, state);
657
658         SAFE_FREE(state->data);
659         SAFE_FREE(state->param);
660         TALLOC_FREE(state);
661
662         END_PROFILE(SMBtrans);
663         return;
664
665   bad_param:
666
667         DEBUG(0,("reply_trans: invalid trans parameters\n"));
668         SAFE_FREE(state->data);
669         SAFE_FREE(state->param);
670         TALLOC_FREE(state);
671         END_PROFILE(SMBtrans);
672         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
673         return;
674 }
675
676 /****************************************************************************
677  Reply to a secondary SMBtrans.
678  ****************************************************************************/
679
680 void reply_transs(struct smb_request *req)
681 {
682         connection_struct *conn = req->conn;
683         unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
684         struct trans_state *state;
685         unsigned int av_size;
686
687         START_PROFILE(SMBtranss);
688
689         show_msg((char *)req->inbuf);
690
691         if (req->wct < 8) {
692                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
693                 END_PROFILE(SMBtranss);
694                 return;
695         }
696
697         for (state = conn->pending_trans; state != NULL;
698              state = state->next) {
699                 if (state->mid == req->mid) {
700                         break;
701                 }
702         }
703
704         if ((state == NULL) || (state->cmd != SMBtrans)) {
705                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
706                 END_PROFILE(SMBtranss);
707                 return;
708         }
709
710         /* Revise total_params and total_data in case they have changed
711          * downwards */
712
713         if (SVAL(req->inbuf, smb_vwv0) < state->total_param)
714                 state->total_param = SVAL(req->inbuf,smb_vwv0);
715         if (SVAL(req->inbuf, smb_vwv1) < state->total_data)
716                 state->total_data = SVAL(req->inbuf,smb_vwv1);
717
718         av_size = smb_len(req->inbuf);
719
720         pcnt = SVAL(req->inbuf, smb_spscnt);
721         poff = SVAL(req->inbuf, smb_spsoff);
722         pdisp = SVAL(req->inbuf, smb_spsdisp);
723
724         dcnt = SVAL(req->inbuf, smb_sdscnt);
725         doff = SVAL(req->inbuf, smb_sdsoff);
726         ddisp = SVAL(req->inbuf, smb_sdsdisp);
727
728         state->received_param += pcnt;
729         state->received_data += dcnt;
730
731         if ((state->received_data > state->total_data) ||
732             (state->received_param > state->total_param))
733                 goto bad_param;
734
735         if (pcnt) {
736                 if (pdisp > state->total_param ||
737                                 pcnt > state->total_param ||
738                                 pdisp+pcnt > state->total_param ||
739                                 pdisp+pcnt < pdisp) {
740                         goto bad_param;
741                 }
742
743                 if (poff > av_size ||
744                                 pcnt > av_size ||
745                                 poff+pcnt > av_size ||
746                                 poff+pcnt < poff) {
747                         goto bad_param;
748                 }
749
750                 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,
751                        pcnt);
752         }
753
754         if (dcnt) {
755                 if (ddisp > state->total_data ||
756                                 dcnt > state->total_data ||
757                                 ddisp+dcnt > state->total_data ||
758                                 ddisp+dcnt < ddisp) {
759                         goto bad_param;
760                 }
761
762                 if (ddisp > av_size ||
763                                 dcnt > av_size ||
764                                 ddisp+dcnt > av_size ||
765                                 ddisp+dcnt < ddisp) {
766                         goto bad_param;
767                 }
768
769                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
770                        dcnt);
771         }
772
773         if ((state->received_param < state->total_param) ||
774             (state->received_data < state->total_data)) {
775                 END_PROFILE(SMBtranss);
776                 return;
777         }
778
779         /*
780          * construct_reply_common will copy smb_com from inbuf to
781          * outbuf. SMBtranss is wrong here.
782          */
783         SCVAL(req->inbuf,smb_com,SMBtrans);
784
785         handle_trans(conn, req, state);
786
787         DLIST_REMOVE(conn->pending_trans, state);
788         SAFE_FREE(state->data);
789         SAFE_FREE(state->param);
790         TALLOC_FREE(state);
791
792         END_PROFILE(SMBtranss);
793         return;
794
795   bad_param:
796
797         DEBUG(0,("reply_transs: invalid trans parameters\n"));
798         DLIST_REMOVE(conn->pending_trans, state);
799         SAFE_FREE(state->data);
800         SAFE_FREE(state->param);
801         TALLOC_FREE(state);
802         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
803         END_PROFILE(SMBtranss);
804         return;
805 }