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