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