Fix the offset checks in the trans routines
[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(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 size;
507         unsigned int av_size;
508
509         START_PROFILE(SMBtrans);
510
511         if (req->wct < 14) {
512                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
513                 END_PROFILE(SMBtrans);
514                 return;
515         }
516
517         size = smb_len(req->inbuf) + 4;
518         av_size = smb_len(req->inbuf);
519         dsoff = SVAL(req->vwv+12, 0);
520         dscnt = SVAL(req->vwv+11, 0);
521         psoff = SVAL(req->vwv+10, 0);
522         pscnt = SVAL(req->vwv+9, 0);
523
524         result = allow_new_trans(conn->pending_trans, req->mid);
525         if (!NT_STATUS_IS_OK(result)) {
526                 DEBUG(2, ("Got invalid trans request: %s\n",
527                           nt_errstr(result)));
528                 reply_nterror(req, result);
529                 END_PROFILE(SMBtrans);
530                 return;
531         }
532
533         if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
534                 DEBUG(0, ("talloc failed\n"));
535                 reply_nterror(req, NT_STATUS_NO_MEMORY);
536                 END_PROFILE(SMBtrans);
537                 return;
538         }
539
540         state->cmd = SMBtrans;
541
542         state->mid = req->mid;
543         state->vuid = req->vuid;
544         state->setup_count = CVAL(req->vwv+13, 0);
545         state->setup = NULL;
546         state->total_param = SVAL(req->vwv+0, 0);
547         state->param = NULL;
548         state->total_data = SVAL(req->vwv+1, 0);
549         state->data = NULL;
550         state->max_param_return = SVAL(req->vwv+2, 0);
551         state->max_data_return = SVAL(req->vwv+3, 0);
552         state->max_setup_return = CVAL(req->vwv+4, 0);
553         state->close_on_completion = BITSETW(req->vwv+5, 0);
554         state->one_way = BITSETW(req->vwv+5, 1);
555
556         srvstr_pull_req_talloc(state, req, &state->name, req->buf,
557                                STR_TERMINATE);
558
559         if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
560                         !state->name)
561                 goto bad_param;
562
563         if (state->total_data)  {
564                 /* Can't use talloc here, the core routines do realloc on the
565                  * params and data. Out of paranoia, 100 bytes too many. */
566                 state->data = (char *)SMB_MALLOC(state->total_data+100);
567                 if (state->data == NULL) {
568                         DEBUG(0,("reply_trans: data malloc fail for %u "
569                                  "bytes !\n", (unsigned int)state->total_data));
570                         TALLOC_FREE(state);
571                         reply_nterror(req, NT_STATUS_NO_MEMORY);
572                         END_PROFILE(SMBtrans);
573                         return;
574                 }
575                 /* null-terminate the slack space */
576                 memset(&state->data[state->total_data], 0, 100);
577
578                 if (dscnt > state->total_data ||
579                                 dsoff+dscnt < dsoff) {
580                         goto bad_param;
581                 }
582
583                 if (dsoff > av_size ||
584                                 dscnt > av_size ||
585                                 dsoff+dscnt > av_size) {
586                         goto bad_param;
587                 }
588
589                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
590         }
591
592         if (state->total_param) {
593                 /* Can't use talloc here, the core routines do realloc on the
594                  * params and data. Out of paranoia, 100 bytes too many */
595                 state->param = (char *)SMB_MALLOC(state->total_param+100);
596                 if (state->param == NULL) {
597                         DEBUG(0,("reply_trans: param malloc fail for %u "
598                                  "bytes !\n", (unsigned int)state->total_param));
599                         SAFE_FREE(state->data);
600                         TALLOC_FREE(state);
601                         reply_nterror(req, NT_STATUS_NO_MEMORY);
602                         END_PROFILE(SMBtrans);
603                         return;
604                 } 
605                 /* null-terminate the slack space */
606                 memset(&state->param[state->total_param], 0, 100);
607
608                 if (pscnt > state->total_param ||
609                                 psoff+pscnt < psoff) {
610                         goto bad_param;
611                 }
612
613                 if (psoff > av_size ||
614                                 pscnt > av_size ||
615                                 psoff+pscnt > av_size) {
616                         goto bad_param;
617                 }
618
619                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
620         }
621
622         state->received_data  = dscnt;
623         state->received_param = pscnt;
624
625         if (state->setup_count) {
626                 unsigned int i;
627                 if((state->setup = TALLOC_ARRAY(
628                             state, uint16, state->setup_count)) == NULL) {
629                         DEBUG(0,("reply_trans: setup malloc fail for %u "
630                                  "bytes !\n", (unsigned int)
631                                  (state->setup_count * sizeof(uint16))));
632                         SAFE_FREE(state->data);
633                         SAFE_FREE(state->param);
634                         TALLOC_FREE(state);
635                         reply_nterror(req, NT_STATUS_NO_MEMORY);
636                         END_PROFILE(SMBtrans);
637                         return;
638                 } 
639                 if (req->inbuf+smb_vwv14+(state->setup_count*SIZEOFWORD) >
640                     req->inbuf + size)
641                         goto bad_param;
642                 if ((smb_vwv14+(state->setup_count*SIZEOFWORD) < smb_vwv14) ||
643                     (smb_vwv14+(state->setup_count*SIZEOFWORD) <
644                      (state->setup_count*SIZEOFWORD)))
645                         goto bad_param;
646
647                 for (i=0;i<state->setup_count;i++)
648                         state->setup[i] = SVAL(req->inbuf,
649                                                smb_vwv14+i*SIZEOFWORD);
650         }
651
652         state->received_param = pscnt;
653
654         if ((state->received_param != state->total_param) ||
655             (state->received_data != state->total_data)) {
656                 DLIST_ADD(conn->pending_trans, state);
657
658                 /* We need to send an interim response then receive the rest
659                    of the parameter/data bytes */
660                 reply_outbuf(req, 0, 0);
661                 show_msg((char *)req->outbuf);
662                 END_PROFILE(SMBtrans);
663                 return;
664         }
665
666         handle_trans(conn, req, state);
667
668         SAFE_FREE(state->data);
669         SAFE_FREE(state->param);
670         TALLOC_FREE(state);
671
672         END_PROFILE(SMBtrans);
673         return;
674
675   bad_param:
676
677         DEBUG(0,("reply_trans: invalid trans parameters\n"));
678         SAFE_FREE(state->data);
679         SAFE_FREE(state->param);
680         TALLOC_FREE(state);
681         END_PROFILE(SMBtrans);
682         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
683         return;
684 }
685
686 /****************************************************************************
687  Reply to a secondary SMBtrans.
688  ****************************************************************************/
689
690 void reply_transs(struct smb_request *req)
691 {
692         connection_struct *conn = req->conn;
693         unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
694         struct trans_state *state;
695         unsigned int av_size;
696
697         START_PROFILE(SMBtranss);
698
699         show_msg((char *)req->inbuf);
700
701         if (req->wct < 8) {
702                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
703                 END_PROFILE(SMBtranss);
704                 return;
705         }
706
707         for (state = conn->pending_trans; state != NULL;
708              state = state->next) {
709                 if (state->mid == req->mid) {
710                         break;
711                 }
712         }
713
714         if ((state == NULL) || (state->cmd != SMBtrans)) {
715                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
716                 END_PROFILE(SMBtranss);
717                 return;
718         }
719
720         /* Revise total_params and total_data in case they have changed
721          * downwards */
722
723         if (SVAL(req->vwv+0, 0) < state->total_param)
724                 state->total_param = SVAL(req->vwv+0, 0);
725         if (SVAL(req->vwv+1, 0) < state->total_data)
726                 state->total_data = SVAL(req->vwv+1, 0);
727
728         av_size = smb_len(req->inbuf);
729
730         pcnt = SVAL(req->vwv+2, 0);
731         poff = SVAL(req->vwv+3, 0);
732         pdisp = SVAL(req->vwv+4, 0);
733
734         dcnt = SVAL(req->vwv+5, 0);
735         doff = SVAL(req->vwv+6, 0);
736         ddisp = SVAL(req->vwv+7, 0);
737
738         state->received_param += pcnt;
739         state->received_data += dcnt;
740
741         if ((state->received_data > state->total_data) ||
742             (state->received_param > state->total_param))
743                 goto bad_param;
744
745         if (pcnt) {
746                 if (pdisp > state->total_param ||
747                                 pcnt > state->total_param ||
748                                 pdisp+pcnt > state->total_param ||
749                                 pdisp+pcnt < pdisp) {
750                         goto bad_param;
751                 }
752
753                 if (poff > av_size ||
754                                 pcnt > av_size ||
755                                 poff+pcnt > av_size ||
756                                 poff+pcnt < poff) {
757                         goto bad_param;
758                 }
759
760                 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,
761                        pcnt);
762         }
763
764         if (dcnt) {
765                 if (ddisp > state->total_data ||
766                                 dcnt > state->total_data ||
767                                 ddisp+dcnt > state->total_data ||
768                                 ddisp+dcnt < ddisp) {
769                         goto bad_param;
770                 }
771
772                 if (doff > av_size ||
773                                 dcnt > av_size ||
774                                 doff+dcnt > av_size ||
775                                 doff+dcnt < doff) {
776                         goto bad_param;
777                 }
778
779                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
780                        dcnt);
781         }
782
783         if ((state->received_param < state->total_param) ||
784             (state->received_data < state->total_data)) {
785                 END_PROFILE(SMBtranss);
786                 return;
787         }
788
789         handle_trans(conn, req, state);
790
791         DLIST_REMOVE(conn->pending_trans, state);
792         SAFE_FREE(state->data);
793         SAFE_FREE(state->param);
794         TALLOC_FREE(state);
795
796         END_PROFILE(SMBtranss);
797         return;
798
799   bad_param:
800
801         DEBUG(0,("reply_transs: invalid trans parameters\n"));
802         DLIST_REMOVE(conn->pending_trans, state);
803         SAFE_FREE(state->data);
804         SAFE_FREE(state->param);
805         TALLOC_FREE(state);
806         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
807         END_PROFILE(SMBtranss);
808         return;
809 }