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