s3:include: change files_struct->vuid to uint64_t
[Samba/gebeck_regimport.git] / source3 / smbd / ipc.c
blob083c6882a70fee9383f5e9ee42105c1ca4b96f62
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 %llu\n", pnum, vuid,
498 (unsigned long long)fsp->vuid));
499 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
500 return;
503 DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n",
504 subcommand, fsp_str_dbg(fsp), pnum));
506 DEBUG(10, ("api_fd_reply: p:%p max_trans_reply: %d\n", fsp, mdrcnt));
508 switch (subcommand) {
509 case TRANSACT_DCERPCCMD: {
510 /* dce/rpc command */
511 api_dcerpc_cmd(conn, req, fsp, (uint8_t *)data, tdscnt,
512 mdrcnt);
513 break;
515 case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
516 /* Wait Named Pipe Handle state */
517 api_WNPHS(conn, req, fsp, params, tpscnt);
518 break;
519 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
520 /* Set Named Pipe Handle state */
521 api_SNPHS(conn, req, fsp, params, tpscnt);
522 break;
523 default:
524 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
525 return;
529 /****************************************************************************
530 Handle named pipe commands.
531 ****************************************************************************/
533 static void named_pipe(connection_struct *conn, uint16 vuid,
534 struct smb_request *req,
535 const char *name, uint16 *setup,
536 char *data, char *params,
537 int suwcnt, int tdscnt,int tpscnt,
538 int msrcnt, int mdrcnt, int mprcnt)
540 DEBUG(3,("named pipe command on <%s> name\n", name));
542 if (strequal(name,"LANMAN")) {
543 api_reply(conn, vuid, req,
544 data, params,
545 tdscnt, tpscnt,
546 mdrcnt, mprcnt);
547 return;
550 if (strequal(name,"WKSSVC") ||
551 strequal(name,"SRVSVC") ||
552 strequal(name,"WINREG") ||
553 strequal(name,"SAMR") ||
554 strequal(name,"LSARPC")) {
556 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
558 api_fd_reply(conn, vuid, req,
559 setup, (uint8_t *)data, params,
560 suwcnt, tdscnt, tpscnt,
561 mdrcnt, mprcnt);
562 return;
565 if (strlen(name) < 1) {
566 api_fd_reply(conn, vuid, req,
567 setup, (uint8_t *)data,
568 params, suwcnt, tdscnt,
569 tpscnt, mdrcnt, mprcnt);
570 return;
573 if (setup)
574 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
575 (int)setup[0],(int)setup[1]));
577 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
578 return;
581 static void handle_trans(connection_struct *conn, struct smb_request *req,
582 struct trans_state *state)
584 char *local_machine_name;
585 int name_offset = 0;
587 DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
588 state->name,(unsigned int)state->total_data,(unsigned int)state->total_param,
589 (unsigned int)state->setup_count));
592 * WinCE wierdness....
595 local_machine_name = talloc_asprintf(state, "\\%s\\",
596 get_local_machine_name());
598 if (local_machine_name == NULL) {
599 reply_nterror(req, NT_STATUS_NO_MEMORY);
600 return;
603 if (strnequal(state->name, local_machine_name,
604 strlen(local_machine_name))) {
605 name_offset = strlen(local_machine_name)-1;
608 if (!strnequal(&state->name[name_offset], "\\PIPE",
609 strlen("\\PIPE"))) {
610 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
611 return;
614 name_offset += strlen("\\PIPE");
616 /* Win9x weirdness. When talking to a unicode server Win9x
617 only sends \PIPE instead of \PIPE\ */
619 if (state->name[name_offset] == '\\')
620 name_offset++;
622 DEBUG(5,("calling named_pipe\n"));
623 named_pipe(conn, state->vuid, req,
624 state->name+name_offset,
625 state->setup,state->data,
626 state->param,
627 state->setup_count,state->total_data,
628 state->total_param,
629 state->max_setup_return,
630 state->max_data_return,
631 state->max_param_return);
633 if (state->close_on_completion) {
634 close_cnum(conn,state->vuid);
635 req->conn = NULL;
638 return;
641 /****************************************************************************
642 Reply to a SMBtrans.
643 ****************************************************************************/
645 void reply_trans(struct smb_request *req)
647 connection_struct *conn = req->conn;
648 unsigned int dsoff;
649 unsigned int dscnt;
650 unsigned int psoff;
651 unsigned int pscnt;
652 struct trans_state *state;
653 NTSTATUS result;
655 START_PROFILE(SMBtrans);
657 if (req->wct < 14) {
658 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
659 END_PROFILE(SMBtrans);
660 return;
663 dsoff = SVAL(req->vwv+12, 0);
664 dscnt = SVAL(req->vwv+11, 0);
665 psoff = SVAL(req->vwv+10, 0);
666 pscnt = SVAL(req->vwv+9, 0);
668 result = allow_new_trans(conn->pending_trans, req->mid);
669 if (!NT_STATUS_IS_OK(result)) {
670 DEBUG(2, ("Got invalid trans request: %s\n",
671 nt_errstr(result)));
672 reply_nterror(req, result);
673 END_PROFILE(SMBtrans);
674 return;
677 if ((state = talloc(conn, struct trans_state)) == NULL) {
678 DEBUG(0, ("talloc failed\n"));
679 reply_nterror(req, NT_STATUS_NO_MEMORY);
680 END_PROFILE(SMBtrans);
681 return;
684 state->cmd = SMBtrans;
686 state->mid = req->mid;
687 state->vuid = req->vuid;
688 state->setup_count = CVAL(req->vwv+13, 0);
689 state->setup = NULL;
690 state->total_param = SVAL(req->vwv+0, 0);
691 state->param = NULL;
692 state->total_data = SVAL(req->vwv+1, 0);
693 state->data = NULL;
694 state->max_param_return = SVAL(req->vwv+2, 0);
695 state->max_data_return = SVAL(req->vwv+3, 0);
696 state->max_setup_return = CVAL(req->vwv+4, 0);
697 state->close_on_completion = BITSETW(req->vwv+5, 0);
698 state->one_way = BITSETW(req->vwv+5, 1);
700 srvstr_pull_req_talloc(state, req, &state->name, req->buf,
701 STR_TERMINATE);
703 if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
704 !state->name)
705 goto bad_param;
707 if (state->total_data) {
709 if (trans_oob(state->total_data, 0, dscnt)
710 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
711 goto bad_param;
714 /* Can't use talloc here, the core routines do realloc on the
715 * params and data. Out of paranoia, 100 bytes too many. */
716 state->data = (char *)SMB_MALLOC(state->total_data+100);
717 if (state->data == NULL) {
718 DEBUG(0,("reply_trans: data malloc fail for %u "
719 "bytes !\n", (unsigned int)state->total_data));
720 TALLOC_FREE(state);
721 reply_nterror(req, NT_STATUS_NO_MEMORY);
722 END_PROFILE(SMBtrans);
723 return;
725 /* null-terminate the slack space */
726 memset(&state->data[state->total_data], 0, 100);
728 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
731 if (state->total_param) {
733 if (trans_oob(state->total_param, 0, pscnt)
734 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
735 goto bad_param;
738 /* Can't use talloc here, the core routines do realloc on the
739 * params and data. Out of paranoia, 100 bytes too many */
740 state->param = (char *)SMB_MALLOC(state->total_param+100);
741 if (state->param == NULL) {
742 DEBUG(0,("reply_trans: param malloc fail for %u "
743 "bytes !\n", (unsigned int)state->total_param));
744 SAFE_FREE(state->data);
745 TALLOC_FREE(state);
746 reply_nterror(req, NT_STATUS_NO_MEMORY);
747 END_PROFILE(SMBtrans);
748 return;
750 /* null-terminate the slack space */
751 memset(&state->param[state->total_param], 0, 100);
753 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
756 state->received_data = dscnt;
757 state->received_param = pscnt;
759 if (state->setup_count) {
760 unsigned int i;
763 * No overflow possible here, state->setup_count is an
764 * unsigned int, being filled by a single byte from
765 * CVAL(req->vwv+13, 0) above. The cast in the comparison
766 * below is not necessary, it's here to clarify things. The
767 * validity of req->vwv and req->wct has been checked in
768 * init_smb_request already.
770 if (state->setup_count + 14 > (unsigned int)req->wct) {
771 goto bad_param;
774 if((state->setup = talloc_array(
775 state, uint16, state->setup_count)) == NULL) {
776 DEBUG(0,("reply_trans: setup malloc fail for %u "
777 "bytes !\n", (unsigned int)
778 (state->setup_count * sizeof(uint16))));
779 SAFE_FREE(state->data);
780 SAFE_FREE(state->param);
781 TALLOC_FREE(state);
782 reply_nterror(req, NT_STATUS_NO_MEMORY);
783 END_PROFILE(SMBtrans);
784 return;
787 for (i=0;i<state->setup_count;i++) {
788 state->setup[i] = SVAL(req->vwv + 14 + i, 0);
792 state->received_param = pscnt;
794 if ((state->received_param != state->total_param) ||
795 (state->received_data != state->total_data)) {
796 DLIST_ADD(conn->pending_trans, state);
798 /* We need to send an interim response then receive the rest
799 of the parameter/data bytes */
800 reply_outbuf(req, 0, 0);
801 show_msg((char *)req->outbuf);
802 END_PROFILE(SMBtrans);
803 return;
806 talloc_steal(talloc_tos(), state);
808 handle_trans(conn, req, state);
810 SAFE_FREE(state->data);
811 SAFE_FREE(state->param);
812 TALLOC_FREE(state);
814 END_PROFILE(SMBtrans);
815 return;
817 bad_param:
819 DEBUG(0,("reply_trans: invalid trans parameters\n"));
820 SAFE_FREE(state->data);
821 SAFE_FREE(state->param);
822 TALLOC_FREE(state);
823 END_PROFILE(SMBtrans);
824 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
825 return;
828 /****************************************************************************
829 Reply to a secondary SMBtrans.
830 ****************************************************************************/
832 void reply_transs(struct smb_request *req)
834 connection_struct *conn = req->conn;
835 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
836 struct trans_state *state;
838 START_PROFILE(SMBtranss);
840 show_msg((const char *)req->inbuf);
842 if (req->wct < 8) {
843 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
844 END_PROFILE(SMBtranss);
845 return;
848 for (state = conn->pending_trans; state != NULL;
849 state = state->next) {
850 if (state->mid == req->mid) {
851 break;
855 if ((state == NULL) || (state->cmd != SMBtrans)) {
856 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
857 END_PROFILE(SMBtranss);
858 return;
861 /* Revise total_params and total_data in case they have changed
862 * downwards */
864 if (SVAL(req->vwv+0, 0) < state->total_param)
865 state->total_param = SVAL(req->vwv+0, 0);
866 if (SVAL(req->vwv+1, 0) < state->total_data)
867 state->total_data = SVAL(req->vwv+1, 0);
869 pcnt = SVAL(req->vwv+2, 0);
870 poff = SVAL(req->vwv+3, 0);
871 pdisp = SVAL(req->vwv+4, 0);
873 dcnt = SVAL(req->vwv+5, 0);
874 doff = SVAL(req->vwv+6, 0);
875 ddisp = SVAL(req->vwv+7, 0);
877 state->received_param += pcnt;
878 state->received_data += dcnt;
880 if ((state->received_data > state->total_data) ||
881 (state->received_param > state->total_param))
882 goto bad_param;
884 if (pcnt) {
885 if (trans_oob(state->total_param, pdisp, pcnt)
886 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
887 goto bad_param;
889 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
892 if (dcnt) {
893 if (trans_oob(state->total_data, ddisp, dcnt)
894 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
895 goto bad_param;
897 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
900 if ((state->received_param < state->total_param) ||
901 (state->received_data < state->total_data)) {
902 END_PROFILE(SMBtranss);
903 return;
906 talloc_steal(talloc_tos(), state);
908 handle_trans(conn, req, state);
910 DLIST_REMOVE(conn->pending_trans, state);
911 SAFE_FREE(state->data);
912 SAFE_FREE(state->param);
913 TALLOC_FREE(state);
915 END_PROFILE(SMBtranss);
916 return;
918 bad_param:
920 DEBUG(0,("reply_transs: invalid trans parameters\n"));
921 DLIST_REMOVE(conn->pending_trans, state);
922 SAFE_FREE(state->data);
923 SAFE_FREE(state->param);
924 TALLOC_FREE(state);
925 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
926 END_PROFILE(SMBtranss);
927 return;