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