s3: Add a 10-second timeout for the 445 or netbios connection to a DC
[Samba.git] / source3 / smbd / ipc.c
blob4f2fea52658947c4a1637a2d8bc998afac02a7a3
1 /*
2 Unix SMB/CIFS implementation.
3 Inter-process communication and named pipe handling
4 Copyright (C) Andrew Tridgell 1992-1998
6 SMB Version handling
7 Copyright (C) John H Terpstra 1995-1998
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.
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.
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/>.
23 This file handles the named pipe and mailslot calls
24 in the SMBtrans protocol
27 #include "includes.h"
28 #include "smbd/smbd.h"
29 #include "smbd/globals.h"
30 #include "smbprofile.h"
32 #define NERR_notsupported 50
34 static void api_no_reply(connection_struct *conn, struct smb_request *req);
36 /*******************************************************************
37 copies parameters and data, as needed, into the smb buffer
39 *both* the data and params sections should be aligned. this
40 is fudged in the rpc pipes by
41 at present, only the data section is. this may be a possible
42 cause of some of the ipc problems being experienced. lkcl26dec97
44 ******************************************************************/
46 static void copy_trans_params_and_data(char *outbuf, int align,
47 char *rparam, int param_offset, int param_len,
48 char *rdata, int data_offset, int data_len)
50 char *copy_into = smb_buf(outbuf);
52 if(param_len < 0)
53 param_len = 0;
55 if(data_len < 0)
56 data_len = 0;
58 DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d] (align %d)\n",
59 param_offset, param_offset + param_len,
60 data_offset , data_offset + data_len,
61 align));
63 *copy_into = '\0';
65 copy_into += 1;
67 if (param_len)
68 memcpy(copy_into, &rparam[param_offset], param_len);
70 copy_into += param_len;
71 if (align) {
72 memset(copy_into, '\0', align);
75 copy_into += align;
77 if (data_len )
78 memcpy(copy_into, &rdata[data_offset], data_len);
81 /****************************************************************************
82 Send a trans reply.
83 ****************************************************************************/
85 void send_trans_reply(connection_struct *conn,
86 struct smb_request *req,
87 char *rparam, int rparam_len,
88 char *rdata, int rdata_len,
89 bool buffer_too_large)
91 int this_ldata,this_lparam;
92 int tot_data_sent = 0;
93 int tot_param_sent = 0;
94 int align;
96 int ldata = rdata ? rdata_len : 0;
97 int lparam = rparam ? rparam_len : 0;
98 struct smbd_server_connection *sconn = req->sconn;
99 int max_send = sconn->smb1.sessions.max_send;
101 if (buffer_too_large)
102 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata ));
104 this_lparam = MIN(lparam,max_send - 500); /* hack */
105 this_ldata = MIN(ldata,max_send - (500+this_lparam));
107 align = ((this_lparam)%4);
109 reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
112 * We might have SMBtranss in req which was transferred to the outbuf,
113 * fix that.
115 SCVAL(req->outbuf, smb_com, SMBtrans);
117 copy_trans_params_and_data((char *)req->outbuf, align,
118 rparam, tot_param_sent, this_lparam,
119 rdata, tot_data_sent, this_ldata);
121 SSVAL(req->outbuf,smb_vwv0,lparam);
122 SSVAL(req->outbuf,smb_vwv1,ldata);
123 SSVAL(req->outbuf,smb_vwv3,this_lparam);
124 SSVAL(req->outbuf,smb_vwv4,
125 smb_offset(smb_buf(req->outbuf)+1, req->outbuf));
126 SSVAL(req->outbuf,smb_vwv5,0);
127 SSVAL(req->outbuf,smb_vwv6,this_ldata);
128 SSVAL(req->outbuf,smb_vwv7,
129 smb_offset(smb_buf(req->outbuf)+1+this_lparam+align,
130 req->outbuf));
131 SSVAL(req->outbuf,smb_vwv8,0);
132 SSVAL(req->outbuf,smb_vwv9,0);
134 if (buffer_too_large) {
135 error_packet_set((char *)req->outbuf, ERRDOS, ERRmoredata,
136 STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
139 show_msg((char *)req->outbuf);
140 if (!srv_send_smb(sconn, (char *)req->outbuf,
141 true, req->seqnum+1,
142 IS_CONN_ENCRYPTED(conn), &req->pcd)) {
143 exit_server_cleanly("send_trans_reply: srv_send_smb failed.");
146 TALLOC_FREE(req->outbuf);
148 tot_data_sent = this_ldata;
149 tot_param_sent = this_lparam;
151 while (tot_data_sent < ldata || tot_param_sent < lparam)
153 this_lparam = MIN(lparam-tot_param_sent,
154 max_send - 500); /* hack */
155 this_ldata = MIN(ldata -tot_data_sent,
156 max_send - (500+this_lparam));
158 if(this_lparam < 0)
159 this_lparam = 0;
161 if(this_ldata < 0)
162 this_ldata = 0;
164 align = (this_lparam%4);
166 reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
169 * We might have SMBtranss in req which was transferred to the
170 * outbuf, fix that.
172 SCVAL(req->outbuf, smb_com, SMBtrans);
174 copy_trans_params_and_data((char *)req->outbuf, align,
175 rparam, tot_param_sent, this_lparam,
176 rdata, tot_data_sent, this_ldata);
178 SSVAL(req->outbuf,smb_vwv0,lparam);
179 SSVAL(req->outbuf,smb_vwv1,ldata);
181 SSVAL(req->outbuf,smb_vwv3,this_lparam);
182 SSVAL(req->outbuf,smb_vwv4,
183 smb_offset(smb_buf(req->outbuf)+1,req->outbuf));
184 SSVAL(req->outbuf,smb_vwv5,tot_param_sent);
185 SSVAL(req->outbuf,smb_vwv6,this_ldata);
186 SSVAL(req->outbuf,smb_vwv7,
187 smb_offset(smb_buf(req->outbuf)+1+this_lparam+align,
188 req->outbuf));
189 SSVAL(req->outbuf,smb_vwv8,tot_data_sent);
190 SSVAL(req->outbuf,smb_vwv9,0);
192 if (buffer_too_large) {
193 error_packet_set((char *)req->outbuf,
194 ERRDOS, ERRmoredata,
195 STATUS_BUFFER_OVERFLOW,
196 __LINE__, __FILE__);
199 show_msg((char *)req->outbuf);
200 if (!srv_send_smb(sconn, (char *)req->outbuf,
201 true, req->seqnum+1,
202 IS_CONN_ENCRYPTED(conn), &req->pcd))
203 exit_server_cleanly("send_trans_reply: srv_send_smb "
204 "failed.");
206 tot_data_sent += this_ldata;
207 tot_param_sent += this_lparam;
208 TALLOC_FREE(req->outbuf);
212 /****************************************************************************
213 Start the first part of an RPC reply which began with an SMBtrans request.
214 ****************************************************************************/
216 struct dcerpc_cmd_state {
217 struct fake_file_handle *handle;
218 uint8_t *data;
219 size_t num_data;
220 size_t max_read;
223 static void api_dcerpc_cmd_write_done(struct tevent_req *subreq);
224 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq);
226 static void api_dcerpc_cmd(connection_struct *conn, struct smb_request *req,
227 files_struct *fsp, uint8_t *data, size_t length,
228 size_t max_read)
230 struct tevent_req *subreq;
231 struct dcerpc_cmd_state *state;
232 bool busy;
234 if (!fsp_is_np(fsp)) {
235 api_no_reply(conn, req);
236 return;
240 * Trans requests are only allowed
241 * if no other Trans or Read is active
243 busy = np_read_in_progress(fsp->fake_file_handle);
244 if (busy) {
245 reply_nterror(req, NT_STATUS_PIPE_BUSY);
246 return;
249 state = talloc(req, struct dcerpc_cmd_state);
250 if (state == NULL) {
251 reply_nterror(req, NT_STATUS_NO_MEMORY);
252 return;
254 req->async_priv = state;
256 state->handle = fsp->fake_file_handle;
259 * This memdup severely sucks. But doing it properly essentially means
260 * to rewrite lanman.c, something which I don't really want to do now.
262 state->data = (uint8_t *)talloc_memdup(state, data, length);
263 if (state->data == NULL) {
264 reply_nterror(req, NT_STATUS_NO_MEMORY);
265 return;
267 state->num_data = length;
268 state->max_read = max_read;
270 subreq = np_write_send(state, smbd_event_context(), state->handle,
271 state->data, length);
272 if (subreq == NULL) {
273 TALLOC_FREE(state);
274 reply_nterror(req, NT_STATUS_NO_MEMORY);
275 return;
277 tevent_req_set_callback(subreq, api_dcerpc_cmd_write_done,
278 talloc_move(conn, &req));
281 static void api_dcerpc_cmd_write_done(struct tevent_req *subreq)
283 struct smb_request *req = tevent_req_callback_data(
284 subreq, struct smb_request);
285 struct dcerpc_cmd_state *state = talloc_get_type_abort(
286 req->async_priv, struct dcerpc_cmd_state);
287 NTSTATUS status;
288 ssize_t nwritten = -1;
290 status = np_write_recv(subreq, &nwritten);
291 TALLOC_FREE(subreq);
292 if (!NT_STATUS_IS_OK(status) || (nwritten != state->num_data)) {
293 DEBUG(10, ("Could not write to pipe: %s (%d/%d)\n",
294 nt_errstr(status), (int)state->num_data,
295 (int)nwritten));
296 reply_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
297 goto send;
300 state->data = TALLOC_REALLOC_ARRAY(state, state->data, uint8_t,
301 state->max_read);
302 if (state->data == NULL) {
303 reply_nterror(req, NT_STATUS_NO_MEMORY);
304 goto send;
307 subreq = np_read_send(req->conn, smbd_event_context(),
308 state->handle, state->data, state->max_read);
309 if (subreq == NULL) {
310 reply_nterror(req, NT_STATUS_NO_MEMORY);
311 goto send;
313 tevent_req_set_callback(subreq, api_dcerpc_cmd_read_done, req);
314 return;
316 send:
317 if (!srv_send_smb(
318 req->sconn, (char *)req->outbuf,
319 true, req->seqnum+1,
320 IS_CONN_ENCRYPTED(req->conn) || req->encrypted,
321 &req->pcd)) {
322 exit_server_cleanly("api_dcerpc_cmd_write_done: "
323 "srv_send_smb failed.");
325 TALLOC_FREE(req);
328 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq)
330 struct smb_request *req = tevent_req_callback_data(
331 subreq, struct smb_request);
332 struct dcerpc_cmd_state *state = talloc_get_type_abort(
333 req->async_priv, struct dcerpc_cmd_state);
334 NTSTATUS status;
335 ssize_t nread;
336 bool is_data_outstanding;
338 status = np_read_recv(subreq, &nread, &is_data_outstanding);
339 TALLOC_FREE(subreq);
341 if (!NT_STATUS_IS_OK(status)) {
342 DEBUG(10, ("Could not read from to pipe: %s\n",
343 nt_errstr(status)));
344 reply_nterror(req, status);
346 if (!srv_send_smb(req->sconn, (char *)req->outbuf,
347 true, req->seqnum+1,
348 IS_CONN_ENCRYPTED(req->conn)
349 ||req->encrypted, &req->pcd)) {
350 exit_server_cleanly("api_dcerpc_cmd_read_done: "
351 "srv_send_smb failed.");
353 TALLOC_FREE(req);
354 return;
357 send_trans_reply(req->conn, req, NULL, 0, (char *)state->data, nread,
358 is_data_outstanding);
359 TALLOC_FREE(req);
362 /****************************************************************************
363 WaitNamedPipeHandleState
364 ****************************************************************************/
366 static void api_WNPHS(connection_struct *conn, struct smb_request *req,
367 struct files_struct *fsp, char *param, int param_len)
369 if (!param || param_len < 2) {
370 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
371 return;
374 DEBUG(4,("WaitNamedPipeHandleState priority %x\n",
375 (int)SVAL(param,0)));
377 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
381 /****************************************************************************
382 SetNamedPipeHandleState
383 ****************************************************************************/
385 static void api_SNPHS(connection_struct *conn, struct smb_request *req,
386 struct files_struct *fsp, char *param, int param_len)
388 if (!param || param_len < 2) {
389 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
390 return;
393 DEBUG(4,("SetNamedPipeHandleState to code %x\n", (int)SVAL(param,0)));
395 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
399 /****************************************************************************
400 When no reply is generated, indicate unsupported.
401 ****************************************************************************/
403 static void api_no_reply(connection_struct *conn, struct smb_request *req)
405 char rparam[4];
407 /* unsupported */
408 SSVAL(rparam,0,NERR_notsupported);
409 SSVAL(rparam,2,0); /* converter word */
411 DEBUG(3,("Unsupported API fd command\n"));
413 /* now send the reply */
414 send_trans_reply(conn, req, rparam, 4, NULL, 0, False);
416 return;
419 /****************************************************************************
420 Handle remote api calls delivered to a named pipe already opened.
421 ****************************************************************************/
423 static void api_fd_reply(connection_struct *conn, uint16 vuid,
424 struct smb_request *req,
425 uint16 *setup, uint8_t *data, char *params,
426 int suwcnt, int tdscnt, int tpscnt,
427 int mdrcnt, int mprcnt)
429 struct files_struct *fsp;
430 int pnum;
431 int subcommand;
433 DEBUG(5,("api_fd_reply\n"));
435 /* First find out the name of this file. */
436 if (suwcnt != 2) {
437 DEBUG(0,("Unexpected named pipe transaction.\n"));
438 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
439 return;
442 /* Get the file handle and hence the file name. */
444 * NB. The setup array has already been transformed
445 * via SVAL and so is in host byte order.
447 pnum = ((int)setup[1]) & 0xFFFF;
448 subcommand = ((int)setup[0]) & 0xFFFF;
450 fsp = file_fsp(req, pnum);
452 if (!fsp_is_np(fsp)) {
453 if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
454 /* Win9x does this call with a unicode pipe name, not a pnum. */
455 /* Just return success for now... */
456 DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
457 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
458 return;
461 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
462 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
463 return;
466 if (vuid != fsp->vuid) {
467 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
468 "expected %d\n", pnum, vuid, fsp->vuid));
469 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
470 return;
473 DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n",
474 subcommand, fsp_str_dbg(fsp), pnum));
476 DEBUG(10, ("api_fd_reply: p:%p max_trans_reply: %d\n", fsp, mdrcnt));
478 switch (subcommand) {
479 case TRANSACT_DCERPCCMD: {
480 /* dce/rpc command */
481 api_dcerpc_cmd(conn, req, fsp, (uint8_t *)data, tdscnt,
482 mdrcnt);
483 break;
485 case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
486 /* Wait Named Pipe Handle state */
487 api_WNPHS(conn, req, fsp, params, tpscnt);
488 break;
489 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
490 /* Set Named Pipe Handle state */
491 api_SNPHS(conn, req, fsp, params, tpscnt);
492 break;
493 default:
494 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
495 return;
499 /****************************************************************************
500 Handle named pipe commands.
501 ****************************************************************************/
503 static void named_pipe(connection_struct *conn, uint16 vuid,
504 struct smb_request *req,
505 const char *name, uint16 *setup,
506 char *data, char *params,
507 int suwcnt, int tdscnt,int tpscnt,
508 int msrcnt, int mdrcnt, int mprcnt)
510 DEBUG(3,("named pipe command on <%s> name\n", name));
512 if (strequal(name,"LANMAN")) {
513 api_reply(conn, vuid, req,
514 data, params,
515 tdscnt, tpscnt,
516 mdrcnt, mprcnt);
517 return;
520 if (strequal(name,"WKSSVC") ||
521 strequal(name,"SRVSVC") ||
522 strequal(name,"WINREG") ||
523 strequal(name,"SAMR") ||
524 strequal(name,"LSARPC")) {
526 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
528 api_fd_reply(conn, vuid, req,
529 setup, (uint8_t *)data, params,
530 suwcnt, tdscnt, tpscnt,
531 mdrcnt, mprcnt);
532 return;
535 if (strlen(name) < 1) {
536 api_fd_reply(conn, vuid, req,
537 setup, (uint8_t *)data,
538 params, suwcnt, tdscnt,
539 tpscnt, mdrcnt, mprcnt);
540 return;
543 if (setup)
544 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
545 (int)setup[0],(int)setup[1]));
547 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
548 return;
551 static void handle_trans(connection_struct *conn, struct smb_request *req,
552 struct trans_state *state)
554 char *local_machine_name;
555 int name_offset = 0;
557 DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
558 state->name,(unsigned int)state->total_data,(unsigned int)state->total_param,
559 (unsigned int)state->setup_count));
562 * WinCE wierdness....
565 local_machine_name = talloc_asprintf(state, "\\%s\\",
566 get_local_machine_name());
568 if (local_machine_name == NULL) {
569 reply_nterror(req, NT_STATUS_NO_MEMORY);
570 return;
573 if (strnequal(state->name, local_machine_name,
574 strlen(local_machine_name))) {
575 name_offset = strlen(local_machine_name)-1;
578 if (!strnequal(&state->name[name_offset], "\\PIPE",
579 strlen("\\PIPE"))) {
580 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
581 return;
584 name_offset += strlen("\\PIPE");
586 /* Win9x weirdness. When talking to a unicode server Win9x
587 only sends \PIPE instead of \PIPE\ */
589 if (state->name[name_offset] == '\\')
590 name_offset++;
592 DEBUG(5,("calling named_pipe\n"));
593 named_pipe(conn, state->vuid, req,
594 state->name+name_offset,
595 state->setup,state->data,
596 state->param,
597 state->setup_count,state->total_data,
598 state->total_param,
599 state->max_setup_return,
600 state->max_data_return,
601 state->max_param_return);
603 if (state->close_on_completion) {
604 close_cnum(conn,state->vuid);
605 req->conn = NULL;
608 return;
611 /****************************************************************************
612 Reply to a SMBtrans.
613 ****************************************************************************/
615 void reply_trans(struct smb_request *req)
617 connection_struct *conn = req->conn;
618 unsigned int dsoff;
619 unsigned int dscnt;
620 unsigned int psoff;
621 unsigned int pscnt;
622 struct trans_state *state;
623 NTSTATUS result;
625 START_PROFILE(SMBtrans);
627 if (req->wct < 14) {
628 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
629 END_PROFILE(SMBtrans);
630 return;
633 dsoff = SVAL(req->vwv+12, 0);
634 dscnt = SVAL(req->vwv+11, 0);
635 psoff = SVAL(req->vwv+10, 0);
636 pscnt = SVAL(req->vwv+9, 0);
638 result = allow_new_trans(conn->pending_trans, req->mid);
639 if (!NT_STATUS_IS_OK(result)) {
640 DEBUG(2, ("Got invalid trans request: %s\n",
641 nt_errstr(result)));
642 reply_nterror(req, result);
643 END_PROFILE(SMBtrans);
644 return;
647 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
648 DEBUG(0, ("talloc failed\n"));
649 reply_nterror(req, NT_STATUS_NO_MEMORY);
650 END_PROFILE(SMBtrans);
651 return;
654 state->cmd = SMBtrans;
656 state->mid = req->mid;
657 state->vuid = req->vuid;
658 state->setup_count = CVAL(req->vwv+13, 0);
659 state->setup = NULL;
660 state->total_param = SVAL(req->vwv+0, 0);
661 state->param = NULL;
662 state->total_data = SVAL(req->vwv+1, 0);
663 state->data = NULL;
664 state->max_param_return = SVAL(req->vwv+2, 0);
665 state->max_data_return = SVAL(req->vwv+3, 0);
666 state->max_setup_return = CVAL(req->vwv+4, 0);
667 state->close_on_completion = BITSETW(req->vwv+5, 0);
668 state->one_way = BITSETW(req->vwv+5, 1);
670 srvstr_pull_req_talloc(state, req, &state->name, req->buf,
671 STR_TERMINATE);
673 if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
674 !state->name)
675 goto bad_param;
677 if (state->total_data) {
679 if (trans_oob(state->total_data, 0, dscnt)
680 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
681 goto bad_param;
684 /* Can't use talloc here, the core routines do realloc on the
685 * params and data. Out of paranoia, 100 bytes too many. */
686 state->data = (char *)SMB_MALLOC(state->total_data+100);
687 if (state->data == NULL) {
688 DEBUG(0,("reply_trans: data malloc fail for %u "
689 "bytes !\n", (unsigned int)state->total_data));
690 TALLOC_FREE(state);
691 reply_nterror(req, NT_STATUS_NO_MEMORY);
692 END_PROFILE(SMBtrans);
693 return;
695 /* null-terminate the slack space */
696 memset(&state->data[state->total_data], 0, 100);
698 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
701 if (state->total_param) {
703 if (trans_oob(state->total_param, 0, pscnt)
704 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
705 goto bad_param;
708 /* Can't use talloc here, the core routines do realloc on the
709 * params and data. Out of paranoia, 100 bytes too many */
710 state->param = (char *)SMB_MALLOC(state->total_param+100);
711 if (state->param == NULL) {
712 DEBUG(0,("reply_trans: param malloc fail for %u "
713 "bytes !\n", (unsigned int)state->total_param));
714 SAFE_FREE(state->data);
715 TALLOC_FREE(state);
716 reply_nterror(req, NT_STATUS_NO_MEMORY);
717 END_PROFILE(SMBtrans);
718 return;
720 /* null-terminate the slack space */
721 memset(&state->param[state->total_param], 0, 100);
723 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
726 state->received_data = dscnt;
727 state->received_param = pscnt;
729 if (state->setup_count) {
730 unsigned int i;
733 * No overflow possible here, state->setup_count is an
734 * unsigned int, being filled by a single byte from
735 * CVAL(req->vwv+13, 0) above. The cast in the comparison
736 * below is not necessary, it's here to clarify things. The
737 * validity of req->vwv and req->wct has been checked in
738 * init_smb_request already.
740 if (state->setup_count + 14 > (unsigned int)req->wct) {
741 goto bad_param;
744 if((state->setup = TALLOC_ARRAY(
745 state, uint16, state->setup_count)) == NULL) {
746 DEBUG(0,("reply_trans: setup malloc fail for %u "
747 "bytes !\n", (unsigned int)
748 (state->setup_count * sizeof(uint16))));
749 SAFE_FREE(state->data);
750 SAFE_FREE(state->param);
751 TALLOC_FREE(state);
752 reply_nterror(req, NT_STATUS_NO_MEMORY);
753 END_PROFILE(SMBtrans);
754 return;
757 for (i=0;i<state->setup_count;i++) {
758 state->setup[i] = SVAL(req->vwv + 14 + i, 0);
762 state->received_param = pscnt;
764 if ((state->received_param != state->total_param) ||
765 (state->received_data != state->total_data)) {
766 DLIST_ADD(conn->pending_trans, state);
768 /* We need to send an interim response then receive the rest
769 of the parameter/data bytes */
770 reply_outbuf(req, 0, 0);
771 show_msg((char *)req->outbuf);
772 END_PROFILE(SMBtrans);
773 return;
776 talloc_steal(talloc_tos(), state);
778 handle_trans(conn, req, state);
780 SAFE_FREE(state->data);
781 SAFE_FREE(state->param);
782 TALLOC_FREE(state);
784 END_PROFILE(SMBtrans);
785 return;
787 bad_param:
789 DEBUG(0,("reply_trans: invalid trans parameters\n"));
790 SAFE_FREE(state->data);
791 SAFE_FREE(state->param);
792 TALLOC_FREE(state);
793 END_PROFILE(SMBtrans);
794 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
795 return;
798 /****************************************************************************
799 Reply to a secondary SMBtrans.
800 ****************************************************************************/
802 void reply_transs(struct smb_request *req)
804 connection_struct *conn = req->conn;
805 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
806 struct trans_state *state;
808 START_PROFILE(SMBtranss);
810 show_msg((char *)req->inbuf);
812 if (req->wct < 8) {
813 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
814 END_PROFILE(SMBtranss);
815 return;
818 for (state = conn->pending_trans; state != NULL;
819 state = state->next) {
820 if (state->mid == req->mid) {
821 break;
825 if ((state == NULL) || (state->cmd != SMBtrans)) {
826 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
827 END_PROFILE(SMBtranss);
828 return;
831 /* Revise total_params and total_data in case they have changed
832 * downwards */
834 if (SVAL(req->vwv+0, 0) < state->total_param)
835 state->total_param = SVAL(req->vwv+0, 0);
836 if (SVAL(req->vwv+1, 0) < state->total_data)
837 state->total_data = SVAL(req->vwv+1, 0);
839 pcnt = SVAL(req->vwv+2, 0);
840 poff = SVAL(req->vwv+3, 0);
841 pdisp = SVAL(req->vwv+4, 0);
843 dcnt = SVAL(req->vwv+5, 0);
844 doff = SVAL(req->vwv+6, 0);
845 ddisp = SVAL(req->vwv+7, 0);
847 state->received_param += pcnt;
848 state->received_data += dcnt;
850 if ((state->received_data > state->total_data) ||
851 (state->received_param > state->total_param))
852 goto bad_param;
854 if (pcnt) {
855 if (trans_oob(state->total_param, pdisp, pcnt)
856 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
857 goto bad_param;
859 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
862 if (dcnt) {
863 if (trans_oob(state->total_data, ddisp, dcnt)
864 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
865 goto bad_param;
867 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
870 if ((state->received_param < state->total_param) ||
871 (state->received_data < state->total_data)) {
872 END_PROFILE(SMBtranss);
873 return;
876 talloc_steal(talloc_tos(), state);
878 handle_trans(conn, req, state);
880 DLIST_REMOVE(conn->pending_trans, state);
881 SAFE_FREE(state->data);
882 SAFE_FREE(state->param);
883 TALLOC_FREE(state);
885 END_PROFILE(SMBtranss);
886 return;
888 bad_param:
890 DEBUG(0,("reply_transs: invalid trans parameters\n"));
891 DLIST_REMOVE(conn->pending_trans, state);
892 SAFE_FREE(state->data);
893 SAFE_FREE(state->param);
894 TALLOC_FREE(state);
895 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
896 END_PROFILE(SMBtranss);
897 return;