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