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