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