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