Let send_trans_reply work on only the inbuf
[samba.git] / source3 / smbd / ipc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Inter-process communication and named pipe handling
4    Copyright (C) Andrew Tridgell 1992-1998
5
6    SMB Version handling
7    Copyright (C) John H Terpstra 1995-1998
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21    */
22 /*
23    This file handles the named pipe and mailslot calls
24    in the SMBtrans protocol
25    */
26
27 #include "includes.h"
28
29 extern int max_send;
30
31 #define NERR_notsupported 50
32
33 static void api_no_reply(connection_struct *conn, struct smb_request *req);
34
35 /*******************************************************************
36  copies parameters and data, as needed, into the smb buffer
37
38  *both* the data and params sections should be aligned.  this
39  is fudged in the rpc pipes by 
40  at present, only the data section is.  this may be a possible
41  cause of some of the ipc problems being experienced.  lkcl26dec97
42
43  ******************************************************************/
44
45 static void copy_trans_params_and_data(char *outbuf, int align,
46                                 char *rparam, int param_offset, int param_len,
47                                 char *rdata, int data_offset, int data_len)
48 {
49         char *copy_into = smb_buf(outbuf);
50
51         if(param_len < 0)
52                 param_len = 0;
53
54         if(data_len < 0)
55                 data_len = 0;
56
57         DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d] (align %d)\n",
58                         param_offset, param_offset + param_len,
59                         data_offset , data_offset  + data_len,
60                         align));
61
62         *copy_into = '\0';
63
64         copy_into += 1;
65
66         if (param_len)
67                 memcpy(copy_into, &rparam[param_offset], param_len);
68
69         copy_into += param_len;
70         if (align) {
71                 memset(copy_into, '\0', align);
72         }
73
74         copy_into += align;
75
76         if (data_len )
77                 memcpy(copy_into, &rdata[data_offset], data_len);
78 }
79
80 /****************************************************************************
81  Send a trans reply.
82  ****************************************************************************/
83
84 void send_trans_reply(connection_struct *conn, const uint8_t *inbuf,
85                       char *rparam, int rparam_len,
86                       char *rdata, int rdata_len,
87                       bool buffer_too_large)
88 {
89         int this_ldata,this_lparam;
90         int tot_data_sent = 0;
91         int tot_param_sent = 0;
92         int align;
93         char *outbuf;
94
95         int ldata  = rdata  ? rdata_len : 0;
96         int lparam = rparam ? rparam_len : 0;
97
98         if (buffer_too_large)
99                 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata ));
100
101         this_lparam = MIN(lparam,max_send - 500); /* hack */
102         this_ldata  = MIN(ldata,max_send - (500+this_lparam));
103
104         align = ((this_lparam)%4);
105
106         if (!create_outbuf(talloc_tos(), (char *)inbuf, &outbuf,
107                            10, 1+align+this_ldata+this_lparam)) {
108                 smb_panic("could not allocate outbuf");
109         }
110
111         copy_trans_params_and_data(outbuf, align,
112                                 rparam, tot_param_sent, this_lparam,
113                                 rdata, tot_data_sent, this_ldata);
114
115         SSVAL(outbuf,smb_vwv0,lparam);
116         SSVAL(outbuf,smb_vwv1,ldata);
117         SSVAL(outbuf,smb_vwv3,this_lparam);
118         SSVAL(outbuf,smb_vwv4,smb_offset(smb_buf(outbuf)+1,outbuf));
119         SSVAL(outbuf,smb_vwv5,0);
120         SSVAL(outbuf,smb_vwv6,this_ldata);
121         SSVAL(outbuf,smb_vwv7,smb_offset(smb_buf(outbuf)+1+this_lparam+align,
122                                          outbuf));
123         SSVAL(outbuf,smb_vwv8,0);
124         SSVAL(outbuf,smb_vwv9,0);
125
126         if (buffer_too_large) {
127                 error_packet_set((char *)outbuf, ERRDOS, ERRmoredata,
128                                  STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
129         }
130
131         show_msg(outbuf);
132         if (!srv_send_smb(smbd_server_fd(), (char *)outbuf,
133                           IS_CONN_ENCRYPTED(conn))) {
134                 exit_server_cleanly("send_trans_reply: srv_send_smb failed.");
135         }
136
137         TALLOC_FREE(outbuf);
138
139         tot_data_sent = this_ldata;
140         tot_param_sent = this_lparam;
141
142         while (tot_data_sent < ldata || tot_param_sent < lparam)
143         {
144                 this_lparam = MIN(lparam-tot_param_sent,
145                                   max_send - 500); /* hack */
146                 this_ldata  = MIN(ldata -tot_data_sent,
147                                   max_send - (500+this_lparam));
148
149                 if(this_lparam < 0)
150                         this_lparam = 0;
151
152                 if(this_ldata < 0)
153                         this_ldata = 0;
154
155                 align = (this_lparam%4);
156
157                 if (!create_outbuf(talloc_tos(), (char *)inbuf, &outbuf,
158                                    10, 1+align+this_ldata+this_lparam)) {
159                         smb_panic("could not allocate outbuf");
160                 }
161
162                 copy_trans_params_and_data(outbuf, align,
163                                            rparam, tot_param_sent, this_lparam,
164                                            rdata, tot_data_sent, this_ldata);
165                 
166                 SSVAL(outbuf,smb_vwv3,this_lparam);
167                 SSVAL(outbuf,smb_vwv4,smb_offset(smb_buf(outbuf)+1,outbuf));
168                 SSVAL(outbuf,smb_vwv5,tot_param_sent);
169                 SSVAL(outbuf,smb_vwv6,this_ldata);
170                 SSVAL(outbuf,smb_vwv7,
171                       smb_offset(smb_buf(outbuf)+1+this_lparam+align, outbuf));
172                 SSVAL(outbuf,smb_vwv8,tot_data_sent);
173                 SSVAL(outbuf,smb_vwv9,0);
174
175                 if (buffer_too_large) {
176                         error_packet_set(outbuf, ERRDOS, ERRmoredata,
177                                          STATUS_BUFFER_OVERFLOW,
178                                          __LINE__, __FILE__);
179                 }
180
181                 show_msg(outbuf);
182                 if (!srv_send_smb(smbd_server_fd(), outbuf,
183                                   IS_CONN_ENCRYPTED(conn)))
184                         exit_server_cleanly("send_trans_reply: srv_send_smb "
185                                             "failed.");
186
187                 tot_data_sent  += this_ldata;
188                 tot_param_sent += this_lparam;
189                 TALLOC_FREE(outbuf);
190         }
191 }
192
193 /****************************************************************************
194  Start the first part of an RPC reply which began with an SMBtrans request.
195 ****************************************************************************/
196
197 static void api_rpc_trans_reply(connection_struct *conn, struct smb_request *req, smb_np_struct *p)
198 {
199         bool is_data_outstanding;
200         char *rdata = (char *)SMB_MALLOC(p->max_trans_reply);
201         int data_len;
202
203         if(rdata == NULL) {
204                 DEBUG(0,("api_rpc_trans_reply: malloc fail.\n"));
205                 reply_nterror(req, NT_STATUS_NO_MEMORY);
206                 return;
207         }
208
209         if((data_len = read_from_pipe( p, rdata, p->max_trans_reply,
210                                         &is_data_outstanding)) < 0) {
211                 SAFE_FREE(rdata);
212                 api_no_reply(conn,req);
213                 return;
214         }
215
216         send_trans_reply(conn, req->inbuf, NULL, 0, rdata, data_len,
217                          is_data_outstanding);
218         SAFE_FREE(rdata);
219         return;
220 }
221
222 /****************************************************************************
223  WaitNamedPipeHandleState 
224 ****************************************************************************/
225
226 static void api_WNPHS(connection_struct *conn, struct smb_request *req, smb_np_struct *p,
227                       char *param, int param_len)
228 {
229         uint16 priority;
230
231         if (!param || param_len < 2) {
232                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
233                 return;
234         }
235
236         priority = SVAL(param,0);
237         DEBUG(4,("WaitNamedPipeHandleState priority %x\n", priority));
238
239         if (wait_rpc_pipe_hnd_state(p, priority)) {
240                 /* now send the reply */
241                 send_trans_reply(conn, req->inbuf, NULL, 0, NULL, 0, False);
242                 return;
243         }
244         api_no_reply(conn,req);
245 }
246
247
248 /****************************************************************************
249  SetNamedPipeHandleState 
250 ****************************************************************************/
251
252 static void api_SNPHS(connection_struct *conn, struct smb_request *req, smb_np_struct *p,
253                       char *param, int param_len)
254 {
255         uint16 id;
256
257         if (!param || param_len < 2) {
258                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
259                 return;
260         }
261
262         id = SVAL(param,0);
263         DEBUG(4,("SetNamedPipeHandleState to code %x\n", id));
264
265         if (set_rpc_pipe_hnd_state(p, id)) {
266                 /* now send the reply */
267                 send_trans_reply(conn, req->inbuf, NULL, 0, NULL, 0, False);
268                 return;
269         }
270         api_no_reply(conn,req);
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->inbuf, 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, char *data, char *params,
301                          int suwcnt, int tdscnt, int tpscnt,
302                          int mdrcnt, int mprcnt)
303 {
304         bool reply = False;
305         smb_np_struct *p = NULL;
306         int pnum;
307         int subcommand;
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 gost byte order.
322          */
323         pnum = ((int)setup[1]) & 0xFFFF;
324         subcommand = ((int)setup[0]) & 0xFFFF;
325
326         if(!(p = get_rpc_pipe(pnum))) {
327                 if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
328                         /* Win9x does this call with a unicode pipe name, not a pnum. */
329                         /* Just return success for now... */
330                         DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
331                         send_trans_reply(conn, req->inbuf, NULL, 0, NULL, 0,
332                                          False);
333                         return;
334                 }
335
336                 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
337                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
338                 return;
339         }
340
341         if (vuid != p->vuid) {
342                 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
343                           "expected %d\n", pnum, vuid, p->vuid));
344                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
345                 return;
346         }
347
348         DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n", subcommand, p->name, pnum));
349
350         /* record maximum data length that can be transmitted in an SMBtrans */
351         p->max_trans_reply = mdrcnt;
352
353         DEBUG(10,("api_fd_reply: p:%p max_trans_reply: %d\n", p, p->max_trans_reply));
354
355         switch (subcommand) {
356         case TRANSACT_DCERPCCMD:
357                 /* dce/rpc command */
358                 reply = write_to_pipe(p, data, tdscnt);
359                 if (!reply) {
360                         api_no_reply(conn, req);
361                         return;
362                 }
363                 api_rpc_trans_reply(conn, req, p);
364                 break;
365         case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
366                 /* Wait Named Pipe Handle state */
367                 api_WNPHS(conn, req, p, params, tpscnt);
368                 break;
369         case TRANSACT_SETNAMEDPIPEHANDLESTATE:
370                 /* Set Named Pipe Handle state */
371                 api_SNPHS(conn, req, p, params, tpscnt);
372                 break;
373         default:
374                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
375                 return;
376         }
377 }
378
379 /****************************************************************************
380  Handle named pipe commands.
381 ****************************************************************************/
382
383 static void named_pipe(connection_struct *conn, uint16 vuid,
384                        struct smb_request *req,
385                        const char *name, uint16 *setup,
386                        char *data, char *params,
387                        int suwcnt, int tdscnt,int tpscnt,
388                        int msrcnt, int mdrcnt, int mprcnt)
389 {
390         DEBUG(3,("named pipe command on <%s> name\n", name));
391
392         if (strequal(name,"LANMAN")) {
393                 api_reply(conn, vuid, req,
394                           data, params,
395                           tdscnt, tpscnt,
396                           mdrcnt, mprcnt);
397                 return;
398         }
399
400         if (strequal(name,"WKSSVC") ||
401             strequal(name,"SRVSVC") ||
402             strequal(name,"WINREG") ||
403             strequal(name,"SAMR") ||
404             strequal(name,"LSARPC")) {
405
406                 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
407
408                 api_fd_reply(conn, vuid, req,
409                              setup, data, params,
410                              suwcnt, tdscnt, tpscnt,
411                              mdrcnt, mprcnt);
412                 return;
413         }
414
415         if (strlen(name) < 1) {
416                 api_fd_reply(conn, vuid, req,
417                              setup, data,
418                              params, suwcnt, tdscnt,
419                              tpscnt, mdrcnt, mprcnt);
420                 return;
421         }
422
423         if (setup)
424                 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
425                          (int)setup[0],(int)setup[1]));
426
427         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
428         return;
429 }
430
431 static void handle_trans(connection_struct *conn, struct smb_request *req,
432                          struct trans_state *state)
433 {
434         char *local_machine_name;
435         int name_offset = 0;
436
437         DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
438                  state->name,(unsigned int)state->total_data,(unsigned int)state->total_param,
439                  (unsigned int)state->setup_count));
440
441         /*
442          * WinCE wierdness....
443          */
444
445         local_machine_name = talloc_asprintf(state, "\\%s\\",
446                                              get_local_machine_name());
447
448         if (local_machine_name == NULL) {
449                 reply_nterror(req, NT_STATUS_NO_MEMORY);
450                 return;
451         }
452
453         if (strnequal(state->name, local_machine_name,
454                       strlen(local_machine_name))) {
455                 name_offset = strlen(local_machine_name)-1;
456         }
457
458         if (!strnequal(&state->name[name_offset], "\\PIPE",
459                        strlen("\\PIPE"))) {
460                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
461                 return;
462         }
463
464         name_offset += strlen("\\PIPE");
465
466         /* Win9x weirdness.  When talking to a unicode server Win9x
467            only sends \PIPE instead of \PIPE\ */
468
469         if (state->name[name_offset] == '\\')
470                 name_offset++;
471
472         DEBUG(5,("calling named_pipe\n"));
473         named_pipe(conn, state->vuid, req,
474                    state->name+name_offset,
475                    state->setup,state->data,
476                    state->param,
477                    state->setup_count,state->total_data,
478                    state->total_param,
479                    state->max_setup_return,
480                    state->max_data_return,
481                    state->max_param_return);
482
483         if (state->close_on_completion) {
484                 close_cnum(conn,state->vuid);
485                 req->conn = NULL;
486         }
487
488         return;
489 }
490
491 /****************************************************************************
492  Reply to a SMBtrans.
493  ****************************************************************************/
494
495 void reply_trans(struct smb_request *req)
496 {
497         connection_struct *conn = req->conn;
498         unsigned int dsoff;
499         unsigned int dscnt;
500         unsigned int psoff;
501         unsigned int pscnt;
502         struct trans_state *state;
503         NTSTATUS result;
504         unsigned int size;
505         unsigned int av_size;
506
507         START_PROFILE(SMBtrans);
508
509         if (req->wct < 14) {
510                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
511                 END_PROFILE(SMBtrans);
512                 return;
513         }
514
515         size = smb_len(req->inbuf) + 4;
516         av_size = smb_len(req->inbuf);
517         dsoff = SVAL(req->inbuf, smb_dsoff);
518         dscnt = SVAL(req->inbuf, smb_dscnt);
519         psoff = SVAL(req->inbuf, smb_psoff);
520         pscnt = SVAL(req->inbuf, smb_pscnt);
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->inbuf, smb_suwcnt);
543         state->setup = NULL;
544         state->total_param = SVAL(req->inbuf, smb_tpscnt);
545         state->param = NULL;
546         state->total_data = SVAL(req->inbuf, smb_tdscnt);
547         state->data = NULL;
548         state->max_param_return = SVAL(req->inbuf, smb_mprcnt);
549         state->max_data_return = SVAL(req->inbuf, smb_mdrcnt);
550         state->max_setup_return = CVAL(req->inbuf, smb_msrcnt);
551         state->close_on_completion = BITSETW(req->inbuf+smb_vwv5,0);
552         state->one_way = BITSETW(req->inbuf+smb_vwv5,1);
553
554         srvstr_pull_buf_talloc(state, req->inbuf, req->flags2, &state->name,
555                         smb_buf(req->inbuf), 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                 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                 if (req->inbuf+smb_vwv14+(state->setup_count*SIZEOFWORD) >
638                     req->inbuf + size)
639                         goto bad_param;
640                 if ((smb_vwv14+(state->setup_count*SIZEOFWORD) < smb_vwv14) ||
641                     (smb_vwv14+(state->setup_count*SIZEOFWORD) <
642                      (state->setup_count*SIZEOFWORD)))
643                         goto bad_param;
644
645                 for (i=0;i<state->setup_count;i++)
646                         state->setup[i] = SVAL(req->inbuf,
647                                                smb_vwv14+i*SIZEOFWORD);
648         }
649
650         state->received_param = pscnt;
651
652         if ((state->received_param != state->total_param) ||
653             (state->received_data != state->total_data)) {
654                 DLIST_ADD(conn->pending_trans, state);
655
656                 /* We need to send an interim response then receive the rest
657                    of the parameter/data bytes */
658                 reply_outbuf(req, 0, 0);
659                 show_msg((char *)req->outbuf);
660                 END_PROFILE(SMBtrans);
661                 return;
662         }
663
664         handle_trans(conn, req, state);
665
666         SAFE_FREE(state->data);
667         SAFE_FREE(state->param);
668         TALLOC_FREE(state);
669
670         END_PROFILE(SMBtrans);
671         return;
672
673   bad_param:
674
675         DEBUG(0,("reply_trans: invalid trans parameters\n"));
676         SAFE_FREE(state->data);
677         SAFE_FREE(state->param);
678         TALLOC_FREE(state);
679         END_PROFILE(SMBtrans);
680         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
681         return;
682 }
683
684 /****************************************************************************
685  Reply to a secondary SMBtrans.
686  ****************************************************************************/
687
688 void reply_transs(struct smb_request *req)
689 {
690         connection_struct *conn = req->conn;
691         unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
692         struct trans_state *state;
693         unsigned int av_size;
694
695         START_PROFILE(SMBtranss);
696
697         show_msg((char *)req->inbuf);
698
699         if (req->wct < 8) {
700                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
701                 END_PROFILE(SMBtranss);
702                 return;
703         }
704
705         for (state = conn->pending_trans; state != NULL;
706              state = state->next) {
707                 if (state->mid == req->mid) {
708                         break;
709                 }
710         }
711
712         if ((state == NULL) || (state->cmd != SMBtrans)) {
713                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
714                 END_PROFILE(SMBtranss);
715                 return;
716         }
717
718         /* Revise total_params and total_data in case they have changed
719          * downwards */
720
721         if (SVAL(req->inbuf, smb_vwv0) < state->total_param)
722                 state->total_param = SVAL(req->inbuf,smb_vwv0);
723         if (SVAL(req->inbuf, smb_vwv1) < state->total_data)
724                 state->total_data = SVAL(req->inbuf,smb_vwv1);
725
726         av_size = smb_len(req->inbuf);
727
728         pcnt = SVAL(req->inbuf, smb_spscnt);
729         poff = SVAL(req->inbuf, smb_spsoff);
730         pdisp = SVAL(req->inbuf, smb_spsdisp);
731
732         dcnt = SVAL(req->inbuf, smb_sdscnt);
733         doff = SVAL(req->inbuf, smb_sdsoff);
734         ddisp = SVAL(req->inbuf, smb_sdsdisp);
735
736         state->received_param += pcnt;
737         state->received_data += dcnt;
738                 
739         if ((state->received_data > state->total_data) ||
740             (state->received_param > state->total_param))
741                 goto bad_param;
742                 
743         if (pcnt) {
744                 if (pdisp > state->total_param ||
745                                 pcnt > state->total_param ||
746                                 pdisp+pcnt > state->total_param ||
747                                 pdisp+pcnt < pdisp) {
748                         goto bad_param;
749                 }
750
751                 if (poff > av_size ||
752                                 pcnt > av_size ||
753                                 poff+pcnt > av_size ||
754                                 poff+pcnt < poff) {
755                         goto bad_param;
756                 }
757
758                 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,
759                        pcnt);
760         }
761
762         if (dcnt) {
763                 if (ddisp > state->total_data ||
764                                 dcnt > state->total_data ||
765                                 ddisp+dcnt > state->total_data ||
766                                 ddisp+dcnt < ddisp) {
767                         goto bad_param;
768                 }
769
770                 if (ddisp > av_size ||
771                                 dcnt > av_size ||
772                                 ddisp+dcnt > av_size ||
773                                 ddisp+dcnt < ddisp) {
774                         goto bad_param;
775                 }
776
777                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
778                        dcnt);
779         }
780
781         if ((state->received_param < state->total_param) ||
782             (state->received_data < state->total_data)) {
783                 END_PROFILE(SMBtranss);
784                 return;
785         }
786
787         /*
788          * construct_reply_common will copy smb_com from inbuf to
789          * outbuf. SMBtranss is wrong here.
790          */
791         SCVAL(req->inbuf,smb_com,SMBtrans);
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 }