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