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