s3:smbd/files: remove unused VALID_FNUM()
[Samba/gebeck_regimport.git] / source3 / smbd / ipc.c
blob3d9b174cf98968f0245a809e3175c62cfaea2c8e
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"
31 #include "rpc_server/srv_pipe_hnd.h"
33 #define NERR_notsupported 50
35 static void api_no_reply(connection_struct *conn, struct smb_request *req);
37 /*******************************************************************
38 copies parameters and data, as needed, into the smb buffer
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
45 ******************************************************************/
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)
51 char *copy_into = smb_buf(outbuf);
53 if(param_len < 0)
54 param_len = 0;
56 if(data_len < 0)
57 data_len = 0;
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));
64 *copy_into = '\0';
66 copy_into += 1;
68 if (param_len)
69 memcpy(copy_into, &rparam[param_offset], param_len);
71 copy_into += param_len;
72 if (align) {
73 memset(copy_into, '\0', align);
76 copy_into += align;
78 if (data_len )
79 memcpy(copy_into, &rdata[data_offset], data_len);
82 NTSTATUS nt_status_np_pipe(NTSTATUS status)
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;
90 return status;
93 /****************************************************************************
94 Send a trans reply.
95 ****************************************************************************/
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)
103 int this_ldata,this_lparam;
104 int tot_data_sent = 0;
105 int tot_param_sent = 0;
106 int align;
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;
113 if (buffer_too_large)
114 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata ));
116 this_lparam = MIN(lparam,max_send - 500); /* hack */
117 this_ldata = MIN(ldata,max_send - (500+this_lparam));
119 align = ((this_lparam)%4);
121 reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
124 * We might have SMBtranss in req which was transferred to the outbuf,
125 * fix that.
127 SCVAL(req->outbuf, smb_com, SMBtrans);
129 copy_trans_params_and_data((char *)req->outbuf, align,
130 rparam, tot_param_sent, this_lparam,
131 rdata, tot_data_sent, this_ldata);
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);
146 if (buffer_too_large) {
147 error_packet_set((char *)req->outbuf, ERRDOS, ERRmoredata,
148 STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
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.");
158 TALLOC_FREE(req->outbuf);
160 tot_data_sent = this_ldata;
161 tot_param_sent = this_lparam;
163 while (tot_data_sent < ldata || tot_param_sent < lparam)
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));
170 if(this_lparam < 0)
171 this_lparam = 0;
173 if(this_ldata < 0)
174 this_ldata = 0;
176 align = (this_lparam%4);
178 reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
181 * We might have SMBtranss in req which was transferred to the
182 * outbuf, fix that.
184 SCVAL(req->outbuf, smb_com, SMBtrans);
186 copy_trans_params_and_data((char *)req->outbuf, align,
187 rparam, tot_param_sent, this_lparam,
188 rdata, tot_data_sent, this_ldata);
190 SSVAL(req->outbuf,smb_vwv0,lparam);
191 SSVAL(req->outbuf,smb_vwv1,ldata);
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);
204 if (buffer_too_large) {
205 error_packet_set((char *)req->outbuf,
206 ERRDOS, ERRmoredata,
207 STATUS_BUFFER_OVERFLOW,
208 __LINE__, __FILE__);
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.");
218 tot_data_sent += this_ldata;
219 tot_param_sent += this_lparam;
220 TALLOC_FREE(req->outbuf);
224 /****************************************************************************
225 Start the first part of an RPC reply which began with an SMBtrans request.
226 ****************************************************************************/
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;
235 static void api_dcerpc_cmd_write_done(struct tevent_req *subreq);
236 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq);
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)
242 struct tevent_req *subreq;
243 struct dcerpc_cmd_state *state;
244 bool busy;
246 if (!fsp_is_np(fsp)) {
247 api_no_reply(conn, req);
248 return;
252 * Trans requests are only allowed
253 * if no other Trans or Read is active
255 busy = np_read_in_progress(fsp->fake_file_handle);
256 if (busy) {
257 reply_nterror(req, NT_STATUS_PIPE_BUSY);
258 return;
261 state = talloc(req, struct dcerpc_cmd_state);
262 if (state == NULL) {
263 reply_nterror(req, NT_STATUS_NO_MEMORY);
264 return;
266 req->async_priv = state;
268 state->handle = fsp->fake_file_handle;
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.
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;
279 state->num_data = length;
280 state->max_read = max_read;
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;
289 tevent_req_set_callback(subreq, api_dcerpc_cmd_write_done,
290 talloc_move(conn, &req));
293 static void api_dcerpc_cmd_write_done(struct tevent_req *subreq)
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;
302 status = np_write_recv(subreq, &nwritten);
303 TALLOC_FREE(subreq);
304 if (!NT_STATUS_IS_OK(status)) {
305 NTSTATUS old = status;
306 status = nt_status_np_pipe(old);
308 DEBUG(10, ("Could not write to pipe: %s%s%s\n",
309 nt_errstr(old),
310 NT_STATUS_EQUAL(old, status)?"":" => ",
311 NT_STATUS_EQUAL(old, status)?"":nt_errstr(status)));
312 reply_nterror(req, status);
313 goto send;
315 if (nwritten != state->num_data) {
316 status = NT_STATUS_PIPE_NOT_AVAILABLE;
317 DEBUG(10, ("Could not write to pipe: (%d/%d) => %s\n",
318 (int)state->num_data,
319 (int)nwritten, nt_errstr(status)));
320 reply_nterror(req, status);
321 goto send;
324 state->data = talloc_realloc(state, state->data, uint8_t,
325 state->max_read);
326 if (state->data == NULL) {
327 reply_nterror(req, NT_STATUS_NO_MEMORY);
328 goto send;
331 subreq = np_read_send(state, req->sconn->ev_ctx,
332 state->handle, state->data, state->max_read);
333 if (subreq == NULL) {
334 reply_nterror(req, NT_STATUS_NO_MEMORY);
335 goto send;
337 tevent_req_set_callback(subreq, api_dcerpc_cmd_read_done, req);
338 return;
340 send:
341 if (!srv_send_smb(
342 req->sconn, (char *)req->outbuf,
343 true, req->seqnum+1,
344 IS_CONN_ENCRYPTED(req->conn) || req->encrypted,
345 &req->pcd)) {
346 exit_server_cleanly("api_dcerpc_cmd_write_done: "
347 "srv_send_smb failed.");
349 TALLOC_FREE(req);
352 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq)
354 struct smb_request *req = tevent_req_callback_data(
355 subreq, struct smb_request);
356 struct dcerpc_cmd_state *state = talloc_get_type_abort(
357 req->async_priv, struct dcerpc_cmd_state);
358 NTSTATUS status;
359 ssize_t nread;
360 bool is_data_outstanding;
362 status = np_read_recv(subreq, &nread, &is_data_outstanding);
363 TALLOC_FREE(subreq);
365 if (!NT_STATUS_IS_OK(status)) {
366 NTSTATUS old = status;
367 status = nt_status_np_pipe(old);
369 DEBUG(10, ("Could not read from to pipe: %s%s%s\n",
370 nt_errstr(old),
371 NT_STATUS_EQUAL(old, status)?"":" => ",
372 NT_STATUS_EQUAL(old, status)?"":nt_errstr(status)));
373 reply_nterror(req, status);
375 if (!srv_send_smb(req->sconn, (char *)req->outbuf,
376 true, req->seqnum+1,
377 IS_CONN_ENCRYPTED(req->conn)
378 ||req->encrypted, &req->pcd)) {
379 exit_server_cleanly("api_dcerpc_cmd_read_done: "
380 "srv_send_smb failed.");
382 TALLOC_FREE(req);
383 return;
386 send_trans_reply(req->conn, req, NULL, 0, (char *)state->data, nread,
387 is_data_outstanding);
388 TALLOC_FREE(req);
391 /****************************************************************************
392 WaitNamedPipeHandleState
393 ****************************************************************************/
395 static void api_WNPHS(connection_struct *conn, struct smb_request *req,
396 struct files_struct *fsp, char *param, int param_len)
398 if (!param || param_len < 2) {
399 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
400 return;
403 DEBUG(4,("WaitNamedPipeHandleState priority %x\n",
404 (int)SVAL(param,0)));
406 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
410 /****************************************************************************
411 SetNamedPipeHandleState
412 ****************************************************************************/
414 static void api_SNPHS(connection_struct *conn, struct smb_request *req,
415 struct files_struct *fsp, char *param, int param_len)
417 if (!param || param_len < 2) {
418 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
419 return;
422 DEBUG(4,("SetNamedPipeHandleState to code %x\n", (int)SVAL(param,0)));
424 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
428 /****************************************************************************
429 When no reply is generated, indicate unsupported.
430 ****************************************************************************/
432 static void api_no_reply(connection_struct *conn, struct smb_request *req)
434 char rparam[4];
436 /* unsupported */
437 SSVAL(rparam,0,NERR_notsupported);
438 SSVAL(rparam,2,0); /* converter word */
440 DEBUG(3,("Unsupported API fd command\n"));
442 /* now send the reply */
443 send_trans_reply(conn, req, rparam, 4, NULL, 0, False);
445 return;
448 /****************************************************************************
449 Handle remote api calls delivered to a named pipe already opened.
450 ****************************************************************************/
452 static void api_fd_reply(connection_struct *conn, uint16 vuid,
453 struct smb_request *req,
454 uint16 *setup, uint8_t *data, char *params,
455 int suwcnt, int tdscnt, int tpscnt,
456 int mdrcnt, int mprcnt)
458 struct files_struct *fsp;
459 int pnum;
460 int subcommand;
462 DEBUG(5,("api_fd_reply\n"));
464 /* First find out the name of this file. */
465 if (suwcnt != 2) {
466 DEBUG(0,("Unexpected named pipe transaction.\n"));
467 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
468 return;
471 /* Get the file handle and hence the file name. */
473 * NB. The setup array has already been transformed
474 * via SVAL and so is in host byte order.
476 pnum = ((int)setup[1]) & 0xFFFF;
477 subcommand = ((int)setup[0]) & 0xFFFF;
479 fsp = file_fsp(req, pnum);
481 if (!fsp_is_np(fsp)) {
482 if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
483 /* Win9x does this call with a unicode pipe name, not a pnum. */
484 /* Just return success for now... */
485 DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
486 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
487 return;
490 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
491 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
492 return;
495 if (vuid != fsp->vuid) {
496 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
497 "expected %d\n", pnum, vuid, fsp->vuid));
498 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
499 return;
502 DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n",
503 subcommand, fsp_str_dbg(fsp), pnum));
505 DEBUG(10, ("api_fd_reply: p:%p max_trans_reply: %d\n", fsp, mdrcnt));
507 switch (subcommand) {
508 case TRANSACT_DCERPCCMD: {
509 /* dce/rpc command */
510 api_dcerpc_cmd(conn, req, fsp, (uint8_t *)data, tdscnt,
511 mdrcnt);
512 break;
514 case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
515 /* Wait Named Pipe Handle state */
516 api_WNPHS(conn, req, fsp, params, tpscnt);
517 break;
518 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
519 /* Set Named Pipe Handle state */
520 api_SNPHS(conn, req, fsp, params, tpscnt);
521 break;
522 default:
523 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
524 return;
528 /****************************************************************************
529 Handle named pipe commands.
530 ****************************************************************************/
532 static void named_pipe(connection_struct *conn, uint16 vuid,
533 struct smb_request *req,
534 const char *name, uint16 *setup,
535 char *data, char *params,
536 int suwcnt, int tdscnt,int tpscnt,
537 int msrcnt, int mdrcnt, int mprcnt)
539 DEBUG(3,("named pipe command on <%s> name\n", name));
541 if (strequal(name,"LANMAN")) {
542 api_reply(conn, vuid, req,
543 data, params,
544 tdscnt, tpscnt,
545 mdrcnt, mprcnt);
546 return;
549 if (strequal(name,"WKSSVC") ||
550 strequal(name,"SRVSVC") ||
551 strequal(name,"WINREG") ||
552 strequal(name,"SAMR") ||
553 strequal(name,"LSARPC")) {
555 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
557 api_fd_reply(conn, vuid, req,
558 setup, (uint8_t *)data, params,
559 suwcnt, tdscnt, tpscnt,
560 mdrcnt, mprcnt);
561 return;
564 if (strlen(name) < 1) {
565 api_fd_reply(conn, vuid, req,
566 setup, (uint8_t *)data,
567 params, suwcnt, tdscnt,
568 tpscnt, mdrcnt, mprcnt);
569 return;
572 if (setup)
573 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
574 (int)setup[0],(int)setup[1]));
576 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
577 return;
580 static void handle_trans(connection_struct *conn, struct smb_request *req,
581 struct trans_state *state)
583 char *local_machine_name;
584 int name_offset = 0;
586 DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
587 state->name,(unsigned int)state->total_data,(unsigned int)state->total_param,
588 (unsigned int)state->setup_count));
591 * WinCE wierdness....
594 local_machine_name = talloc_asprintf(state, "\\%s\\",
595 get_local_machine_name());
597 if (local_machine_name == NULL) {
598 reply_nterror(req, NT_STATUS_NO_MEMORY);
599 return;
602 if (strnequal(state->name, local_machine_name,
603 strlen(local_machine_name))) {
604 name_offset = strlen(local_machine_name)-1;
607 if (!strnequal(&state->name[name_offset], "\\PIPE",
608 strlen("\\PIPE"))) {
609 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
610 return;
613 name_offset += strlen("\\PIPE");
615 /* Win9x weirdness. When talking to a unicode server Win9x
616 only sends \PIPE instead of \PIPE\ */
618 if (state->name[name_offset] == '\\')
619 name_offset++;
621 DEBUG(5,("calling named_pipe\n"));
622 named_pipe(conn, state->vuid, req,
623 state->name+name_offset,
624 state->setup,state->data,
625 state->param,
626 state->setup_count,state->total_data,
627 state->total_param,
628 state->max_setup_return,
629 state->max_data_return,
630 state->max_param_return);
632 if (state->close_on_completion) {
633 close_cnum(conn,state->vuid);
634 req->conn = NULL;
637 return;
640 /****************************************************************************
641 Reply to a SMBtrans.
642 ****************************************************************************/
644 void reply_trans(struct smb_request *req)
646 connection_struct *conn = req->conn;
647 unsigned int dsoff;
648 unsigned int dscnt;
649 unsigned int psoff;
650 unsigned int pscnt;
651 struct trans_state *state;
652 NTSTATUS result;
654 START_PROFILE(SMBtrans);
656 if (req->wct < 14) {
657 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
658 END_PROFILE(SMBtrans);
659 return;
662 dsoff = SVAL(req->vwv+12, 0);
663 dscnt = SVAL(req->vwv+11, 0);
664 psoff = SVAL(req->vwv+10, 0);
665 pscnt = SVAL(req->vwv+9, 0);
667 result = allow_new_trans(conn->pending_trans, req->mid);
668 if (!NT_STATUS_IS_OK(result)) {
669 DEBUG(2, ("Got invalid trans request: %s\n",
670 nt_errstr(result)));
671 reply_nterror(req, result);
672 END_PROFILE(SMBtrans);
673 return;
676 if ((state = talloc(conn, struct trans_state)) == NULL) {
677 DEBUG(0, ("talloc failed\n"));
678 reply_nterror(req, NT_STATUS_NO_MEMORY);
679 END_PROFILE(SMBtrans);
680 return;
683 state->cmd = SMBtrans;
685 state->mid = req->mid;
686 state->vuid = req->vuid;
687 state->setup_count = CVAL(req->vwv+13, 0);
688 state->setup = NULL;
689 state->total_param = SVAL(req->vwv+0, 0);
690 state->param = NULL;
691 state->total_data = SVAL(req->vwv+1, 0);
692 state->data = NULL;
693 state->max_param_return = SVAL(req->vwv+2, 0);
694 state->max_data_return = SVAL(req->vwv+3, 0);
695 state->max_setup_return = CVAL(req->vwv+4, 0);
696 state->close_on_completion = BITSETW(req->vwv+5, 0);
697 state->one_way = BITSETW(req->vwv+5, 1);
699 srvstr_pull_req_talloc(state, req, &state->name, req->buf,
700 STR_TERMINATE);
702 if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
703 !state->name)
704 goto bad_param;
706 if (state->total_data) {
708 if (trans_oob(state->total_data, 0, dscnt)
709 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
710 goto bad_param;
713 /* Can't use talloc here, the core routines do realloc on the
714 * params and data. Out of paranoia, 100 bytes too many. */
715 state->data = (char *)SMB_MALLOC(state->total_data+100);
716 if (state->data == NULL) {
717 DEBUG(0,("reply_trans: data malloc fail for %u "
718 "bytes !\n", (unsigned int)state->total_data));
719 TALLOC_FREE(state);
720 reply_nterror(req, NT_STATUS_NO_MEMORY);
721 END_PROFILE(SMBtrans);
722 return;
724 /* null-terminate the slack space */
725 memset(&state->data[state->total_data], 0, 100);
727 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
730 if (state->total_param) {
732 if (trans_oob(state->total_param, 0, pscnt)
733 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
734 goto bad_param;
737 /* Can't use talloc here, the core routines do realloc on the
738 * params and data. Out of paranoia, 100 bytes too many */
739 state->param = (char *)SMB_MALLOC(state->total_param+100);
740 if (state->param == NULL) {
741 DEBUG(0,("reply_trans: param malloc fail for %u "
742 "bytes !\n", (unsigned int)state->total_param));
743 SAFE_FREE(state->data);
744 TALLOC_FREE(state);
745 reply_nterror(req, NT_STATUS_NO_MEMORY);
746 END_PROFILE(SMBtrans);
747 return;
749 /* null-terminate the slack space */
750 memset(&state->param[state->total_param], 0, 100);
752 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
755 state->received_data = dscnt;
756 state->received_param = pscnt;
758 if (state->setup_count) {
759 unsigned int i;
762 * No overflow possible here, state->setup_count is an
763 * unsigned int, being filled by a single byte from
764 * CVAL(req->vwv+13, 0) above. The cast in the comparison
765 * below is not necessary, it's here to clarify things. The
766 * validity of req->vwv and req->wct has been checked in
767 * init_smb_request already.
769 if (state->setup_count + 14 > (unsigned int)req->wct) {
770 goto bad_param;
773 if((state->setup = talloc_array(
774 state, uint16, state->setup_count)) == NULL) {
775 DEBUG(0,("reply_trans: setup malloc fail for %u "
776 "bytes !\n", (unsigned int)
777 (state->setup_count * sizeof(uint16))));
778 SAFE_FREE(state->data);
779 SAFE_FREE(state->param);
780 TALLOC_FREE(state);
781 reply_nterror(req, NT_STATUS_NO_MEMORY);
782 END_PROFILE(SMBtrans);
783 return;
786 for (i=0;i<state->setup_count;i++) {
787 state->setup[i] = SVAL(req->vwv + 14 + i, 0);
791 state->received_param = pscnt;
793 if ((state->received_param != state->total_param) ||
794 (state->received_data != state->total_data)) {
795 DLIST_ADD(conn->pending_trans, state);
797 /* We need to send an interim response then receive the rest
798 of the parameter/data bytes */
799 reply_outbuf(req, 0, 0);
800 show_msg((char *)req->outbuf);
801 END_PROFILE(SMBtrans);
802 return;
805 talloc_steal(talloc_tos(), state);
807 handle_trans(conn, req, state);
809 SAFE_FREE(state->data);
810 SAFE_FREE(state->param);
811 TALLOC_FREE(state);
813 END_PROFILE(SMBtrans);
814 return;
816 bad_param:
818 DEBUG(0,("reply_trans: invalid trans parameters\n"));
819 SAFE_FREE(state->data);
820 SAFE_FREE(state->param);
821 TALLOC_FREE(state);
822 END_PROFILE(SMBtrans);
823 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
824 return;
827 /****************************************************************************
828 Reply to a secondary SMBtrans.
829 ****************************************************************************/
831 void reply_transs(struct smb_request *req)
833 connection_struct *conn = req->conn;
834 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
835 struct trans_state *state;
837 START_PROFILE(SMBtranss);
839 show_msg((const char *)req->inbuf);
841 if (req->wct < 8) {
842 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
843 END_PROFILE(SMBtranss);
844 return;
847 for (state = conn->pending_trans; state != NULL;
848 state = state->next) {
849 if (state->mid == req->mid) {
850 break;
854 if ((state == NULL) || (state->cmd != SMBtrans)) {
855 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
856 END_PROFILE(SMBtranss);
857 return;
860 /* Revise total_params and total_data in case they have changed
861 * downwards */
863 if (SVAL(req->vwv+0, 0) < state->total_param)
864 state->total_param = SVAL(req->vwv+0, 0);
865 if (SVAL(req->vwv+1, 0) < state->total_data)
866 state->total_data = SVAL(req->vwv+1, 0);
868 pcnt = SVAL(req->vwv+2, 0);
869 poff = SVAL(req->vwv+3, 0);
870 pdisp = SVAL(req->vwv+4, 0);
872 dcnt = SVAL(req->vwv+5, 0);
873 doff = SVAL(req->vwv+6, 0);
874 ddisp = SVAL(req->vwv+7, 0);
876 state->received_param += pcnt;
877 state->received_data += dcnt;
879 if ((state->received_data > state->total_data) ||
880 (state->received_param > state->total_param))
881 goto bad_param;
883 if (pcnt) {
884 if (trans_oob(state->total_param, pdisp, pcnt)
885 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
886 goto bad_param;
888 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
891 if (dcnt) {
892 if (trans_oob(state->total_data, ddisp, dcnt)
893 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
894 goto bad_param;
896 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
899 if ((state->received_param < state->total_param) ||
900 (state->received_data < state->total_data)) {
901 END_PROFILE(SMBtranss);
902 return;
905 talloc_steal(talloc_tos(), state);
907 handle_trans(conn, req, state);
909 DLIST_REMOVE(conn->pending_trans, state);
910 SAFE_FREE(state->data);
911 SAFE_FREE(state->param);
912 TALLOC_FREE(state);
914 END_PROFILE(SMBtranss);
915 return;
917 bad_param:
919 DEBUG(0,("reply_transs: invalid trans parameters\n"));
920 DLIST_REMOVE(conn->pending_trans, state);
921 SAFE_FREE(state->data);
922 SAFE_FREE(state->param);
923 TALLOC_FREE(state);
924 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
925 END_PROFILE(SMBtranss);
926 return;