s3:smbd: add nt_status_np_pipe()
[amitay/samba.git] / source3 / smbd / ipc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Inter-process communication and named pipe handling
4    Copyright (C) Andrew Tridgell 1992-1998
5
6    SMB Version handling
7    Copyright (C) John H Terpstra 1995-1998
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21    */
22 /*
23    This file handles the named pipe and mailslot calls
24    in the SMBtrans protocol
25    */
26
27 #include "includes.h"
28 #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) || (nwritten != state->num_data)) {
305                 DEBUG(10, ("Could not write to pipe: %s (%d/%d)\n",
306                            nt_errstr(status), (int)state->num_data,
307                            (int)nwritten));
308                 reply_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
309                 goto send;
310         }
311
312         state->data = talloc_realloc(state, state->data, uint8_t,
313                                            state->max_read);
314         if (state->data == NULL) {
315                 reply_nterror(req, NT_STATUS_NO_MEMORY);
316                 goto send;
317         }
318
319         subreq = np_read_send(state, req->sconn->ev_ctx,
320                               state->handle, state->data, state->max_read);
321         if (subreq == NULL) {
322                 reply_nterror(req, NT_STATUS_NO_MEMORY);
323                 goto send;
324         }
325         tevent_req_set_callback(subreq, api_dcerpc_cmd_read_done, req);
326         return;
327
328  send:
329         if (!srv_send_smb(
330                     req->sconn, (char *)req->outbuf,
331                     true, req->seqnum+1,
332                     IS_CONN_ENCRYPTED(req->conn) || req->encrypted,
333                     &req->pcd)) {
334                 exit_server_cleanly("api_dcerpc_cmd_write_done: "
335                                     "srv_send_smb failed.");
336         }
337         TALLOC_FREE(req);
338 }
339
340 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq)
341 {
342         struct smb_request *req = tevent_req_callback_data(
343                 subreq, struct smb_request);
344         struct dcerpc_cmd_state *state = talloc_get_type_abort(
345                 req->async_priv, struct dcerpc_cmd_state);
346         NTSTATUS status;
347         ssize_t nread;
348         bool is_data_outstanding;
349
350         status = np_read_recv(subreq, &nread, &is_data_outstanding);
351         TALLOC_FREE(subreq);
352
353         if (!NT_STATUS_IS_OK(status)) {
354                 DEBUG(10, ("Could not read from to pipe: %s\n",
355                            nt_errstr(status)));
356                 reply_nterror(req, status);
357
358                 if (!srv_send_smb(req->sconn, (char *)req->outbuf,
359                                   true, req->seqnum+1,
360                                   IS_CONN_ENCRYPTED(req->conn)
361                                   ||req->encrypted, &req->pcd)) {
362                         exit_server_cleanly("api_dcerpc_cmd_read_done: "
363                                             "srv_send_smb failed.");
364                 }
365                 TALLOC_FREE(req);
366                 return;
367         }
368
369         send_trans_reply(req->conn, req, NULL, 0, (char *)state->data, nread,
370                          is_data_outstanding);
371         TALLOC_FREE(req);
372 }
373
374 /****************************************************************************
375  WaitNamedPipeHandleState 
376 ****************************************************************************/
377
378 static void api_WNPHS(connection_struct *conn, struct smb_request *req,
379                       struct files_struct *fsp, char *param, int param_len)
380 {
381         if (!param || param_len < 2) {
382                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
383                 return;
384         }
385
386         DEBUG(4,("WaitNamedPipeHandleState priority %x\n",
387                  (int)SVAL(param,0)));
388
389         send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
390 }
391
392
393 /****************************************************************************
394  SetNamedPipeHandleState 
395 ****************************************************************************/
396
397 static void api_SNPHS(connection_struct *conn, struct smb_request *req,
398                       struct files_struct *fsp, char *param, int param_len)
399 {
400         if (!param || param_len < 2) {
401                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
402                 return;
403         }
404
405         DEBUG(4,("SetNamedPipeHandleState to code %x\n", (int)SVAL(param,0)));
406
407         send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
408 }
409
410
411 /****************************************************************************
412  When no reply is generated, indicate unsupported.
413  ****************************************************************************/
414
415 static void api_no_reply(connection_struct *conn, struct smb_request *req)
416 {
417         char rparam[4];
418
419         /* unsupported */
420         SSVAL(rparam,0,NERR_notsupported);
421         SSVAL(rparam,2,0); /* converter word */
422
423         DEBUG(3,("Unsupported API fd command\n"));
424
425         /* now send the reply */
426         send_trans_reply(conn, req, rparam, 4, NULL, 0, False);
427
428         return;
429 }
430
431 /****************************************************************************
432  Handle remote api calls delivered to a named pipe already opened.
433  ****************************************************************************/
434
435 static void api_fd_reply(connection_struct *conn, uint16 vuid,
436                          struct smb_request *req,
437                          uint16 *setup, uint8_t *data, char *params,
438                          int suwcnt, int tdscnt, int tpscnt,
439                          int mdrcnt, int mprcnt)
440 {
441         struct files_struct *fsp;
442         int pnum;
443         int subcommand;
444
445         DEBUG(5,("api_fd_reply\n"));
446
447         /* First find out the name of this file. */
448         if (suwcnt != 2) {
449                 DEBUG(0,("Unexpected named pipe transaction.\n"));
450                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
451                 return;
452         }
453
454         /* Get the file handle and hence the file name. */
455         /* 
456          * NB. The setup array has already been transformed
457          * via SVAL and so is in host byte order.
458          */
459         pnum = ((int)setup[1]) & 0xFFFF;
460         subcommand = ((int)setup[0]) & 0xFFFF;
461
462         fsp = file_fsp(req, pnum);
463
464         if (!fsp_is_np(fsp)) {
465                 if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
466                         /* Win9x does this call with a unicode pipe name, not a pnum. */
467                         /* Just return success for now... */
468                         DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
469                         send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
470                         return;
471                 }
472
473                 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
474                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
475                 return;
476         }
477
478         if (vuid != fsp->vuid) {
479                 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
480                           "expected %d\n", pnum, vuid, fsp->vuid));
481                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
482                 return;
483         }
484
485         DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n",
486                  subcommand, fsp_str_dbg(fsp), pnum));
487
488         DEBUG(10, ("api_fd_reply: p:%p max_trans_reply: %d\n", fsp, mdrcnt));
489
490         switch (subcommand) {
491         case TRANSACT_DCERPCCMD: {
492                 /* dce/rpc command */
493                 api_dcerpc_cmd(conn, req, fsp, (uint8_t *)data, tdscnt,
494                                mdrcnt);
495                 break;
496         }
497         case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
498                 /* Wait Named Pipe Handle state */
499                 api_WNPHS(conn, req, fsp, params, tpscnt);
500                 break;
501         case TRANSACT_SETNAMEDPIPEHANDLESTATE:
502                 /* Set Named Pipe Handle state */
503                 api_SNPHS(conn, req, fsp, params, tpscnt);
504                 break;
505         default:
506                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
507                 return;
508         }
509 }
510
511 /****************************************************************************
512  Handle named pipe commands.
513 ****************************************************************************/
514
515 static void named_pipe(connection_struct *conn, uint16 vuid,
516                        struct smb_request *req,
517                        const char *name, uint16 *setup,
518                        char *data, char *params,
519                        int suwcnt, int tdscnt,int tpscnt,
520                        int msrcnt, int mdrcnt, int mprcnt)
521 {
522         DEBUG(3,("named pipe command on <%s> name\n", name));
523
524         if (strequal(name,"LANMAN")) {
525                 api_reply(conn, vuid, req,
526                           data, params,
527                           tdscnt, tpscnt,
528                           mdrcnt, mprcnt);
529                 return;
530         }
531
532         if (strequal(name,"WKSSVC") ||
533             strequal(name,"SRVSVC") ||
534             strequal(name,"WINREG") ||
535             strequal(name,"SAMR") ||
536             strequal(name,"LSARPC")) {
537
538                 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
539
540                 api_fd_reply(conn, vuid, req,
541                              setup, (uint8_t *)data, params,
542                              suwcnt, tdscnt, tpscnt,
543                              mdrcnt, mprcnt);
544                 return;
545         }
546
547         if (strlen(name) < 1) {
548                 api_fd_reply(conn, vuid, req,
549                              setup, (uint8_t *)data,
550                              params, suwcnt, tdscnt,
551                              tpscnt, mdrcnt, mprcnt);
552                 return;
553         }
554
555         if (setup)
556                 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
557                          (int)setup[0],(int)setup[1]));
558
559         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
560         return;
561 }
562
563 static void handle_trans(connection_struct *conn, struct smb_request *req,
564                          struct trans_state *state)
565 {
566         char *local_machine_name;
567         int name_offset = 0;
568
569         DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
570                  state->name,(unsigned int)state->total_data,(unsigned int)state->total_param,
571                  (unsigned int)state->setup_count));
572
573         /*
574          * WinCE wierdness....
575          */
576
577         local_machine_name = talloc_asprintf(state, "\\%s\\",
578                                              get_local_machine_name());
579
580         if (local_machine_name == NULL) {
581                 reply_nterror(req, NT_STATUS_NO_MEMORY);
582                 return;
583         }
584
585         if (strnequal(state->name, local_machine_name,
586                       strlen(local_machine_name))) {
587                 name_offset = strlen(local_machine_name)-1;
588         }
589
590         if (!strnequal(&state->name[name_offset], "\\PIPE",
591                        strlen("\\PIPE"))) {
592                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
593                 return;
594         }
595
596         name_offset += strlen("\\PIPE");
597
598         /* Win9x weirdness.  When talking to a unicode server Win9x
599            only sends \PIPE instead of \PIPE\ */
600
601         if (state->name[name_offset] == '\\')
602                 name_offset++;
603
604         DEBUG(5,("calling named_pipe\n"));
605         named_pipe(conn, state->vuid, req,
606                    state->name+name_offset,
607                    state->setup,state->data,
608                    state->param,
609                    state->setup_count,state->total_data,
610                    state->total_param,
611                    state->max_setup_return,
612                    state->max_data_return,
613                    state->max_param_return);
614
615         if (state->close_on_completion) {
616                 close_cnum(conn,state->vuid);
617                 req->conn = NULL;
618         }
619
620         return;
621 }
622
623 /****************************************************************************
624  Reply to a SMBtrans.
625  ****************************************************************************/
626
627 void reply_trans(struct smb_request *req)
628 {
629         connection_struct *conn = req->conn;
630         unsigned int dsoff;
631         unsigned int dscnt;
632         unsigned int psoff;
633         unsigned int pscnt;
634         struct trans_state *state;
635         NTSTATUS result;
636
637         START_PROFILE(SMBtrans);
638
639         if (req->wct < 14) {
640                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
641                 END_PROFILE(SMBtrans);
642                 return;
643         }
644
645         dsoff = SVAL(req->vwv+12, 0);
646         dscnt = SVAL(req->vwv+11, 0);
647         psoff = SVAL(req->vwv+10, 0);
648         pscnt = SVAL(req->vwv+9, 0);
649
650         result = allow_new_trans(conn->pending_trans, req->mid);
651         if (!NT_STATUS_IS_OK(result)) {
652                 DEBUG(2, ("Got invalid trans request: %s\n",
653                           nt_errstr(result)));
654                 reply_nterror(req, result);
655                 END_PROFILE(SMBtrans);
656                 return;
657         }
658
659         if ((state = talloc(conn, struct trans_state)) == NULL) {
660                 DEBUG(0, ("talloc failed\n"));
661                 reply_nterror(req, NT_STATUS_NO_MEMORY);
662                 END_PROFILE(SMBtrans);
663                 return;
664         }
665
666         state->cmd = SMBtrans;
667
668         state->mid = req->mid;
669         state->vuid = req->vuid;
670         state->setup_count = CVAL(req->vwv+13, 0);
671         state->setup = NULL;
672         state->total_param = SVAL(req->vwv+0, 0);
673         state->param = NULL;
674         state->total_data = SVAL(req->vwv+1, 0);
675         state->data = NULL;
676         state->max_param_return = SVAL(req->vwv+2, 0);
677         state->max_data_return = SVAL(req->vwv+3, 0);
678         state->max_setup_return = CVAL(req->vwv+4, 0);
679         state->close_on_completion = BITSETW(req->vwv+5, 0);
680         state->one_way = BITSETW(req->vwv+5, 1);
681
682         srvstr_pull_req_talloc(state, req, &state->name, req->buf,
683                                STR_TERMINATE);
684
685         if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
686                         !state->name)
687                 goto bad_param;
688
689         if (state->total_data)  {
690
691                 if (trans_oob(state->total_data, 0, dscnt)
692                     || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
693                         goto bad_param;
694                 }
695
696                 /* Can't use talloc here, the core routines do realloc on the
697                  * params and data. Out of paranoia, 100 bytes too many. */
698                 state->data = (char *)SMB_MALLOC(state->total_data+100);
699                 if (state->data == NULL) {
700                         DEBUG(0,("reply_trans: data malloc fail for %u "
701                                  "bytes !\n", (unsigned int)state->total_data));
702                         TALLOC_FREE(state);
703                         reply_nterror(req, NT_STATUS_NO_MEMORY);
704                         END_PROFILE(SMBtrans);
705                         return;
706                 }
707                 /* null-terminate the slack space */
708                 memset(&state->data[state->total_data], 0, 100);
709
710                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
711         }
712
713         if (state->total_param) {
714
715                 if (trans_oob(state->total_param, 0, pscnt)
716                     || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
717                         goto bad_param;
718                 }
719
720                 /* Can't use talloc here, the core routines do realloc on the
721                  * params and data. Out of paranoia, 100 bytes too many */
722                 state->param = (char *)SMB_MALLOC(state->total_param+100);
723                 if (state->param == NULL) {
724                         DEBUG(0,("reply_trans: param malloc fail for %u "
725                                  "bytes !\n", (unsigned int)state->total_param));
726                         SAFE_FREE(state->data);
727                         TALLOC_FREE(state);
728                         reply_nterror(req, NT_STATUS_NO_MEMORY);
729                         END_PROFILE(SMBtrans);
730                         return;
731                 } 
732                 /* null-terminate the slack space */
733                 memset(&state->param[state->total_param], 0, 100);
734
735                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
736         }
737
738         state->received_data  = dscnt;
739         state->received_param = pscnt;
740
741         if (state->setup_count) {
742                 unsigned int i;
743
744                 /*
745                  * No overflow possible here, state->setup_count is an
746                  * unsigned int, being filled by a single byte from
747                  * CVAL(req->vwv+13, 0) above. The cast in the comparison
748                  * below is not necessary, it's here to clarify things. The
749                  * validity of req->vwv and req->wct has been checked in
750                  * init_smb_request already.
751                  */
752                 if (state->setup_count + 14 > (unsigned int)req->wct) {
753                         goto bad_param;
754                 }
755
756                 if((state->setup = talloc_array(
757                             state, uint16, state->setup_count)) == NULL) {
758                         DEBUG(0,("reply_trans: setup malloc fail for %u "
759                                  "bytes !\n", (unsigned int)
760                                  (state->setup_count * sizeof(uint16))));
761                         SAFE_FREE(state->data);
762                         SAFE_FREE(state->param);
763                         TALLOC_FREE(state);
764                         reply_nterror(req, NT_STATUS_NO_MEMORY);
765                         END_PROFILE(SMBtrans);
766                         return;
767                 } 
768
769                 for (i=0;i<state->setup_count;i++) {
770                         state->setup[i] = SVAL(req->vwv + 14 + i, 0);
771                 }
772         }
773
774         state->received_param = pscnt;
775
776         if ((state->received_param != state->total_param) ||
777             (state->received_data != state->total_data)) {
778                 DLIST_ADD(conn->pending_trans, state);
779
780                 /* We need to send an interim response then receive the rest
781                    of the parameter/data bytes */
782                 reply_outbuf(req, 0, 0);
783                 show_msg((char *)req->outbuf);
784                 END_PROFILE(SMBtrans);
785                 return;
786         }
787
788         talloc_steal(talloc_tos(), state);
789
790         handle_trans(conn, req, state);
791
792         SAFE_FREE(state->data);
793         SAFE_FREE(state->param);
794         TALLOC_FREE(state);
795
796         END_PROFILE(SMBtrans);
797         return;
798
799   bad_param:
800
801         DEBUG(0,("reply_trans: invalid trans parameters\n"));
802         SAFE_FREE(state->data);
803         SAFE_FREE(state->param);
804         TALLOC_FREE(state);
805         END_PROFILE(SMBtrans);
806         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
807         return;
808 }
809
810 /****************************************************************************
811  Reply to a secondary SMBtrans.
812  ****************************************************************************/
813
814 void reply_transs(struct smb_request *req)
815 {
816         connection_struct *conn = req->conn;
817         unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
818         struct trans_state *state;
819
820         START_PROFILE(SMBtranss);
821
822         show_msg((const char *)req->inbuf);
823
824         if (req->wct < 8) {
825                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
826                 END_PROFILE(SMBtranss);
827                 return;
828         }
829
830         for (state = conn->pending_trans; state != NULL;
831              state = state->next) {
832                 if (state->mid == req->mid) {
833                         break;
834                 }
835         }
836
837         if ((state == NULL) || (state->cmd != SMBtrans)) {
838                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
839                 END_PROFILE(SMBtranss);
840                 return;
841         }
842
843         /* Revise total_params and total_data in case they have changed
844          * downwards */
845
846         if (SVAL(req->vwv+0, 0) < state->total_param)
847                 state->total_param = SVAL(req->vwv+0, 0);
848         if (SVAL(req->vwv+1, 0) < state->total_data)
849                 state->total_data = SVAL(req->vwv+1, 0);
850
851         pcnt = SVAL(req->vwv+2, 0);
852         poff = SVAL(req->vwv+3, 0);
853         pdisp = SVAL(req->vwv+4, 0);
854
855         dcnt = SVAL(req->vwv+5, 0);
856         doff = SVAL(req->vwv+6, 0);
857         ddisp = SVAL(req->vwv+7, 0);
858
859         state->received_param += pcnt;
860         state->received_data += dcnt;
861
862         if ((state->received_data > state->total_data) ||
863             (state->received_param > state->total_param))
864                 goto bad_param;
865
866         if (pcnt) {
867                 if (trans_oob(state->total_param, pdisp, pcnt)
868                     || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
869                         goto bad_param;
870                 }
871                 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
872         }
873
874         if (dcnt) {
875                 if (trans_oob(state->total_data, ddisp, dcnt)
876                     || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
877                         goto bad_param;
878                 }
879                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
880         }
881
882         if ((state->received_param < state->total_param) ||
883             (state->received_data < state->total_data)) {
884                 END_PROFILE(SMBtranss);
885                 return;
886         }
887
888         talloc_steal(talloc_tos(), state);
889
890         handle_trans(conn, req, state);
891
892         DLIST_REMOVE(conn->pending_trans, state);
893         SAFE_FREE(state->data);
894         SAFE_FREE(state->param);
895         TALLOC_FREE(state);
896
897         END_PROFILE(SMBtranss);
898         return;
899
900   bad_param:
901
902         DEBUG(0,("reply_transs: invalid trans parameters\n"));
903         DLIST_REMOVE(conn->pending_trans, state);
904         SAFE_FREE(state->data);
905         SAFE_FREE(state->param);
906         TALLOC_FREE(state);
907         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
908         END_PROFILE(SMBtranss);
909         return;
910 }