s4:auth: Set ‘authoritative’ even if there is an error
[Samba.git] / source3 / smbd / smb1_ipc.c
blobec25a540a7013b8d04f595640def918c9f9a86bc
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"
32 #include "source3/lib/substitute.h"
34 #define NERR_notsupported 50
36 static void api_no_reply(connection_struct *conn, struct smb_request *req);
38 /*******************************************************************
39 copies parameters and data, as needed, into the smb buffer
41 *both* the data and params sections should be aligned. this
42 is fudged in the rpc pipes by
43 at present, only the data section is. this may be a possible
44 cause of some of the ipc problems being experienced. lkcl26dec97
46 ******************************************************************/
48 static void copy_trans_params_and_data(char *outbuf, int align,
49 char *rparam, int param_offset, int param_len,
50 char *rdata, int data_offset, int data_len)
52 char *copy_into = smb_buf(outbuf);
54 if(param_len < 0)
55 param_len = 0;
57 if(data_len < 0)
58 data_len = 0;
60 DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d] (align %d)\n",
61 param_offset, param_offset + param_len,
62 data_offset , data_offset + data_len,
63 align));
65 *copy_into = '\0';
67 copy_into += 1;
69 if (param_len)
70 memcpy(copy_into, &rparam[param_offset], param_len);
72 copy_into += param_len;
73 if (align) {
74 memset(copy_into, '\0', align);
77 copy_into += align;
79 if (data_len )
80 memcpy(copy_into, &rdata[data_offset], data_len);
83 /****************************************************************************
84 Send a trans reply.
85 ****************************************************************************/
87 void send_trans_reply(connection_struct *conn,
88 struct smb_request *req,
89 char *rparam, int rparam_len,
90 char *rdata, int rdata_len,
91 bool buffer_too_large)
93 int this_ldata,this_lparam;
94 int tot_data_sent = 0;
95 int tot_param_sent = 0;
96 int align;
98 int ldata = rdata ? rdata_len : 0;
99 int lparam = rparam ? rparam_len : 0;
100 struct smbXsrv_connection *xconn = req->xconn;
101 int max_send = xconn->smb1.sessions.max_send;
102 /* HACK: make sure we send at least 128 byte in one go */
103 int hdr_overhead = SMB_BUFFER_SIZE_MIN - 128;
105 if (buffer_too_large)
106 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata ));
108 this_lparam = MIN(lparam,max_send - hdr_overhead);
109 this_ldata = MIN(ldata,max_send - (hdr_overhead+this_lparam));
111 align = ((this_lparam)%4);
113 reply_smb1_outbuf(req, 10, 1+align+this_ldata+this_lparam);
116 * We might have SMBtranss in req which was transferred to the outbuf,
117 * fix that.
119 SCVAL(req->outbuf, smb_com, SMBtrans);
121 copy_trans_params_and_data((char *)req->outbuf, align,
122 rparam, tot_param_sent, this_lparam,
123 rdata, tot_data_sent, this_ldata);
125 SSVAL(req->outbuf,smb_vwv0,lparam);
126 SSVAL(req->outbuf,smb_vwv1,ldata);
127 SSVAL(req->outbuf,smb_vwv3,this_lparam);
128 SSVAL(req->outbuf,smb_vwv4,
129 smb_offset(smb_buf(req->outbuf)+1, req->outbuf));
130 SSVAL(req->outbuf,smb_vwv5,0);
131 SSVAL(req->outbuf,smb_vwv6,this_ldata);
132 SSVAL(req->outbuf,smb_vwv7,
133 smb_offset(smb_buf(req->outbuf)+1+this_lparam+align,
134 req->outbuf));
135 SSVAL(req->outbuf,smb_vwv8,0);
136 SSVAL(req->outbuf,smb_vwv9,0);
138 if (buffer_too_large) {
139 error_packet_set((char *)req->outbuf, ERRDOS, ERRmoredata,
140 STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
143 show_msg((char *)req->outbuf);
144 if (!smb1_srv_send(xconn,
145 (char *)req->outbuf,
146 true,
147 req->seqnum + 1,
148 IS_CONN_ENCRYPTED(conn))) {
149 exit_server_cleanly("send_trans_reply: smb1_srv_send failed.");
152 TALLOC_FREE(req->outbuf);
154 tot_data_sent = this_ldata;
155 tot_param_sent = this_lparam;
157 while (tot_data_sent < ldata || tot_param_sent < lparam)
159 this_lparam = MIN(lparam-tot_param_sent,
160 max_send - hdr_overhead);
161 this_ldata = MIN(ldata -tot_data_sent,
162 max_send - (hdr_overhead+this_lparam));
164 if(this_lparam < 0)
165 this_lparam = 0;
167 if(this_ldata < 0)
168 this_ldata = 0;
170 align = (this_lparam%4);
172 reply_smb1_outbuf(req, 10, 1+align+this_ldata+this_lparam);
175 * We might have SMBtranss in req which was transferred to the
176 * outbuf, fix that.
178 SCVAL(req->outbuf, smb_com, SMBtrans);
180 copy_trans_params_and_data((char *)req->outbuf, align,
181 rparam, tot_param_sent, this_lparam,
182 rdata, tot_data_sent, this_ldata);
184 SSVAL(req->outbuf,smb_vwv0,lparam);
185 SSVAL(req->outbuf,smb_vwv1,ldata);
187 SSVAL(req->outbuf,smb_vwv3,this_lparam);
188 SSVAL(req->outbuf,smb_vwv4,
189 smb_offset(smb_buf(req->outbuf)+1,req->outbuf));
190 SSVAL(req->outbuf,smb_vwv5,tot_param_sent);
191 SSVAL(req->outbuf,smb_vwv6,this_ldata);
192 SSVAL(req->outbuf,smb_vwv7,
193 smb_offset(smb_buf(req->outbuf)+1+this_lparam+align,
194 req->outbuf));
195 SSVAL(req->outbuf,smb_vwv8,tot_data_sent);
196 SSVAL(req->outbuf,smb_vwv9,0);
198 if (buffer_too_large) {
199 error_packet_set((char *)req->outbuf,
200 ERRDOS, ERRmoredata,
201 STATUS_BUFFER_OVERFLOW,
202 __LINE__, __FILE__);
205 show_msg((char *)req->outbuf);
206 if (!smb1_srv_send(xconn,
207 (char *)req->outbuf,
208 true,
209 req->seqnum + 1,
210 IS_CONN_ENCRYPTED(conn))) {
211 exit_server_cleanly("send_trans_reply: smb1_srv_send "
212 "failed.");
215 tot_data_sent += this_ldata;
216 tot_param_sent += this_lparam;
217 TALLOC_FREE(req->outbuf);
221 /****************************************************************************
222 Start the first part of an RPC reply which began with an SMBtrans request.
223 ****************************************************************************/
225 struct dcerpc_cmd_state {
226 struct fake_file_handle *handle;
227 uint8_t *data;
228 size_t num_data;
229 size_t max_read;
232 static void api_dcerpc_cmd_write_done(struct tevent_req *subreq);
233 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq);
235 static void api_dcerpc_cmd(connection_struct *conn, struct smb_request *req,
236 files_struct *fsp, uint8_t *data, size_t length,
237 size_t max_read)
239 struct tevent_req *subreq;
240 struct dcerpc_cmd_state *state;
241 bool busy;
243 if (!fsp_is_np(fsp)) {
244 api_no_reply(conn, req);
245 return;
249 * Trans requests are only allowed
250 * if no other Trans or Read is active
252 busy = np_read_in_progress(fsp->fake_file_handle);
253 if (busy) {
254 reply_nterror(req, NT_STATUS_PIPE_BUSY);
255 return;
258 state = talloc(req, struct dcerpc_cmd_state);
259 if (state == NULL) {
260 reply_nterror(req, NT_STATUS_NO_MEMORY);
261 return;
263 req->async_priv = state;
265 state->handle = fsp->fake_file_handle;
268 * This memdup severely sucks. But doing it properly essentially means
269 * to rewrite lanman.c, something which I don't really want to do now.
271 state->data = (uint8_t *)talloc_memdup(state, data, length);
272 if (state->data == NULL) {
273 reply_nterror(req, NT_STATUS_NO_MEMORY);
274 return;
276 state->num_data = length;
277 state->max_read = max_read;
279 subreq = np_write_send(state, req->sconn->ev_ctx, state->handle,
280 state->data, length);
281 if (subreq == NULL) {
282 TALLOC_FREE(state);
283 reply_nterror(req, NT_STATUS_NO_MEMORY);
284 return;
286 tevent_req_set_callback(subreq, api_dcerpc_cmd_write_done,
287 talloc_move(conn, &req));
290 static void api_dcerpc_cmd_write_done(struct tevent_req *subreq)
292 struct smb_request *req = tevent_req_callback_data(
293 subreq, struct smb_request);
294 struct dcerpc_cmd_state *state = talloc_get_type_abort(
295 req->async_priv, struct dcerpc_cmd_state);
296 NTSTATUS status;
297 ssize_t nwritten = -1;
299 status = np_write_recv(subreq, &nwritten);
300 TALLOC_FREE(subreq);
301 if (!NT_STATUS_IS_OK(status)) {
302 NTSTATUS old = status;
303 status = nt_status_np_pipe(old);
305 DEBUG(10, ("Could not write to pipe: %s%s%s\n",
306 nt_errstr(old),
307 NT_STATUS_EQUAL(old, status)?"":" => ",
308 NT_STATUS_EQUAL(old, status)?"":nt_errstr(status)));
309 reply_nterror(req, status);
310 goto send;
312 if (nwritten != state->num_data) {
313 status = NT_STATUS_PIPE_NOT_AVAILABLE;
314 DEBUG(10, ("Could not write to pipe: (%d/%d) => %s\n",
315 (int)state->num_data,
316 (int)nwritten, nt_errstr(status)));
317 reply_nterror(req, status);
318 goto send;
321 state->data = talloc_realloc(state, state->data, uint8_t,
322 state->max_read);
323 if (state->data == NULL) {
324 reply_nterror(req, NT_STATUS_NO_MEMORY);
325 goto send;
328 subreq = np_read_send(state, req->sconn->ev_ctx,
329 state->handle, state->data, state->max_read);
330 if (subreq == NULL) {
331 reply_nterror(req, NT_STATUS_NO_MEMORY);
332 goto send;
334 tevent_req_set_callback(subreq, api_dcerpc_cmd_read_done, req);
335 return;
337 send:
338 if (!smb1_srv_send(req->xconn,
339 (char *)req->outbuf,
340 true,
341 req->seqnum + 1,
342 IS_CONN_ENCRYPTED(req->conn) || req->encrypted)) {
343 exit_server_cleanly("api_dcerpc_cmd_write_done: "
344 "smb1_srv_send failed.");
346 TALLOC_FREE(req);
349 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq)
351 struct smb_request *req = tevent_req_callback_data(
352 subreq, struct smb_request);
353 struct dcerpc_cmd_state *state = talloc_get_type_abort(
354 req->async_priv, struct dcerpc_cmd_state);
355 NTSTATUS status;
356 ssize_t nread;
357 bool is_data_outstanding;
359 status = np_read_recv(subreq, &nread, &is_data_outstanding);
360 TALLOC_FREE(subreq);
362 if (!NT_STATUS_IS_OK(status)) {
363 NTSTATUS old = status;
364 status = nt_status_np_pipe(old);
366 DEBUG(10, ("Could not read from to pipe: %s%s%s\n",
367 nt_errstr(old),
368 NT_STATUS_EQUAL(old, status)?"":" => ",
369 NT_STATUS_EQUAL(old, status)?"":nt_errstr(status)));
370 reply_nterror(req, status);
372 if (!smb1_srv_send(req->xconn,
373 (char *)req->outbuf,
374 true,
375 req->seqnum + 1,
376 IS_CONN_ENCRYPTED(req->conn) ||
377 req->encrypted)) {
378 exit_server_cleanly("api_dcerpc_cmd_read_done: "
379 "smb1_srv_send failed.");
381 TALLOC_FREE(req);
382 return;
385 send_trans_reply(req->conn, req, NULL, 0, (char *)state->data, nread,
386 is_data_outstanding);
387 TALLOC_FREE(req);
390 /****************************************************************************
391 WaitNamedPipeHandleState
392 ****************************************************************************/
394 static void api_WNPHS(connection_struct *conn, struct smb_request *req,
395 struct files_struct *fsp, char *param, int param_len)
397 if (!param || param_len < 2) {
398 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
399 return;
402 DEBUG(4,("WaitNamedPipeHandleState priority %x\n",
403 (int)SVAL(param,0)));
405 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
409 /****************************************************************************
410 SetNamedPipeHandleState
411 ****************************************************************************/
413 static void api_SNPHS(connection_struct *conn, struct smb_request *req,
414 struct files_struct *fsp, char *param, int param_len)
416 if (!param || param_len < 2) {
417 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
418 return;
421 DEBUG(4,("SetNamedPipeHandleState to code %x\n", (int)SVAL(param,0)));
423 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
427 /****************************************************************************
428 When no reply is generated, indicate unsupported.
429 ****************************************************************************/
431 static void api_no_reply(connection_struct *conn, struct smb_request *req)
433 char rparam[4];
435 /* unsupported */
436 SSVAL(rparam,0,NERR_notsupported);
437 SSVAL(rparam,2,0); /* converter word */
439 DEBUG(3,("Unsupported API fd command\n"));
441 /* now send the reply */
442 send_trans_reply(conn, req, rparam, 4, NULL, 0, False);
444 return;
447 /****************************************************************************
448 Handle remote api calls delivered to a named pipe already opened.
449 ****************************************************************************/
451 static void api_fd_reply(connection_struct *conn, uint64_t vuid,
452 struct smb_request *req,
453 uint16_t *setup, uint8_t *data, char *params,
454 int suwcnt, int tdscnt, int tpscnt,
455 int mdrcnt, int mprcnt)
457 struct files_struct *fsp;
458 int pnum;
459 int subcommand;
461 DEBUG(5,("api_fd_reply\n"));
463 /* First find out the name of this file. */
464 if (suwcnt != 2) {
465 DEBUG(0,("Unexpected named pipe transaction.\n"));
466 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
467 return;
470 /* Get the file handle and hence the file name. */
472 * NB. The setup array has already been transformed
473 * via SVAL and so is in host byte order.
475 pnum = ((int)setup[1]) & 0xFFFF;
476 subcommand = ((int)setup[0]) & 0xFFFF;
478 fsp = file_fsp(req, pnum);
480 if (!fsp_is_np(fsp)) {
481 if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
482 /* Win9x does this call with a unicode pipe name, not a pnum. */
483 /* Just return success for now... */
484 DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
485 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
486 return;
489 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
490 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
491 return;
494 if (vuid != fsp->vuid) {
495 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %llu, "
496 "expected %llu\n", pnum, (unsigned long long)vuid,
497 (unsigned long long)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, uint64_t vuid,
533 struct smb_request *req,
534 const char *name, uint16_t *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 struct smbXsrv_tcon *tcon;
634 NTSTATUS status;
636 tcon = conn->tcon;
637 req->conn = NULL;
638 conn = NULL;
641 * TODO: cancel all outstanding requests on the tcon
643 status = smbXsrv_tcon_disconnect(tcon, state->vuid);
644 if (!NT_STATUS_IS_OK(status)) {
645 DEBUG(0, ("handle_trans: "
646 "smbXsrv_tcon_disconnect() failed: %s\n",
647 nt_errstr(status)));
649 * If we hit this case, there is something completely
650 * wrong, so we better disconnect the transport connection.
652 exit_server(__location__ ": smbXsrv_tcon_disconnect failed");
653 return;
656 TALLOC_FREE(tcon);
659 return;
662 /****************************************************************************
663 Reply to a SMBtrans.
664 ****************************************************************************/
666 void reply_trans(struct smb_request *req)
668 connection_struct *conn = req->conn;
669 unsigned int dsoff;
670 unsigned int dscnt;
671 unsigned int psoff;
672 unsigned int pscnt;
673 struct trans_state *state;
674 NTSTATUS result;
676 START_PROFILE(SMBtrans);
678 if (req->wct < 14) {
679 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
680 END_PROFILE(SMBtrans);
681 return;
684 dsoff = SVAL(req->vwv+12, 0);
685 dscnt = SVAL(req->vwv+11, 0);
686 psoff = SVAL(req->vwv+10, 0);
687 pscnt = SVAL(req->vwv+9, 0);
689 result = allow_new_trans(conn->pending_trans, req->mid);
690 if (!NT_STATUS_IS_OK(result)) {
691 DEBUG(2, ("Got invalid trans request: %s\n",
692 nt_errstr(result)));
693 reply_nterror(req, result);
694 END_PROFILE(SMBtrans);
695 return;
698 if ((state = talloc(conn, struct trans_state)) == NULL) {
699 DEBUG(0, ("talloc failed\n"));
700 reply_nterror(req, NT_STATUS_NO_MEMORY);
701 END_PROFILE(SMBtrans);
702 return;
705 state->cmd = SMBtrans;
707 state->mid = req->mid;
708 state->vuid = req->vuid;
709 state->setup_count = CVAL(req->vwv+13, 0);
710 state->setup = NULL;
711 state->total_param = SVAL(req->vwv+0, 0);
712 state->param = NULL;
713 state->total_data = SVAL(req->vwv+1, 0);
714 state->data = NULL;
715 state->max_param_return = SVAL(req->vwv+2, 0);
716 state->max_data_return = SVAL(req->vwv+3, 0);
717 state->max_setup_return = CVAL(req->vwv+4, 0);
718 state->close_on_completion = BITSETW(req->vwv+5, 0);
719 state->one_way = BITSETW(req->vwv+5, 1);
721 srvstr_pull_req_talloc(state, req, &state->name, req->buf,
722 STR_TERMINATE);
724 if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
725 !state->name)
726 goto bad_param;
728 if (state->total_data) {
730 if (smb_buffer_oob(state->total_data, 0, dscnt)
731 || smb_buffer_oob(smb_len(req->inbuf), dsoff, dscnt)) {
732 goto bad_param;
735 /* Can't use talloc here, the core routines do realloc on the
736 * params and data. Out of paranoia, 100 bytes too many. */
737 state->data = (char *)SMB_MALLOC(state->total_data+100);
738 if (state->data == NULL) {
739 DEBUG(0,("reply_trans: data malloc fail for %u "
740 "bytes !\n", (unsigned int)state->total_data));
741 TALLOC_FREE(state);
742 reply_nterror(req, NT_STATUS_NO_MEMORY);
743 END_PROFILE(SMBtrans);
744 return;
746 /* null-terminate the slack space */
747 memset(&state->data[state->total_data], 0, 100);
749 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
752 if (state->total_param) {
754 if (smb_buffer_oob(state->total_param, 0, pscnt)
755 || smb_buffer_oob(smb_len(req->inbuf), psoff, pscnt)) {
756 goto bad_param;
759 /* Can't use talloc here, the core routines do realloc on the
760 * params and data. Out of paranoia, 100 bytes too many */
761 state->param = (char *)SMB_MALLOC(state->total_param+100);
762 if (state->param == NULL) {
763 DEBUG(0,("reply_trans: param malloc fail for %u "
764 "bytes !\n", (unsigned int)state->total_param));
765 SAFE_FREE(state->data);
766 TALLOC_FREE(state);
767 reply_nterror(req, NT_STATUS_NO_MEMORY);
768 END_PROFILE(SMBtrans);
769 return;
771 /* null-terminate the slack space */
772 memset(&state->param[state->total_param], 0, 100);
774 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
777 state->received_data = dscnt;
778 state->received_param = pscnt;
780 if (state->setup_count) {
781 unsigned int i;
784 * No overflow possible here, state->setup_count is an
785 * unsigned int, being filled by a single byte from
786 * CVAL(req->vwv+13, 0) above. The cast in the comparison
787 * below is not necessary, it's here to clarify things. The
788 * validity of req->vwv and req->wct has been checked in
789 * init_smb1_request already.
791 if (state->setup_count + 14 > (unsigned int)req->wct) {
792 goto bad_param;
795 if((state->setup = talloc_array(
796 state, uint16_t, state->setup_count)) == NULL) {
797 DEBUG(0,("reply_trans: setup malloc fail for %u "
798 "bytes !\n", (unsigned int)
799 (state->setup_count * sizeof(uint16_t))));
800 SAFE_FREE(state->data);
801 SAFE_FREE(state->param);
802 TALLOC_FREE(state);
803 reply_nterror(req, NT_STATUS_NO_MEMORY);
804 END_PROFILE(SMBtrans);
805 return;
808 for (i=0;i<state->setup_count;i++) {
809 state->setup[i] = SVAL(req->vwv + 14 + i, 0);
813 state->received_param = pscnt;
815 if ((state->received_param != state->total_param) ||
816 (state->received_data != state->total_data)) {
817 DLIST_ADD(conn->pending_trans, state);
819 /* We need to send an interim response then receive the rest
820 of the parameter/data bytes */
821 reply_smb1_outbuf(req, 0, 0);
822 show_msg((char *)req->outbuf);
823 END_PROFILE(SMBtrans);
824 return;
827 talloc_steal(talloc_tos(), state);
829 handle_trans(conn, req, state);
831 SAFE_FREE(state->data);
832 SAFE_FREE(state->param);
833 TALLOC_FREE(state);
835 END_PROFILE(SMBtrans);
836 return;
838 bad_param:
840 DEBUG(0,("reply_trans: invalid trans parameters\n"));
841 SAFE_FREE(state->data);
842 SAFE_FREE(state->param);
843 TALLOC_FREE(state);
844 END_PROFILE(SMBtrans);
845 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
846 return;
849 /****************************************************************************
850 Reply to a secondary SMBtrans.
851 ****************************************************************************/
853 void reply_transs(struct smb_request *req)
855 connection_struct *conn = req->conn;
856 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
857 struct trans_state *state;
859 START_PROFILE(SMBtranss);
861 show_msg((const char *)req->inbuf);
863 if (req->wct < 8) {
864 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
865 END_PROFILE(SMBtranss);
866 return;
869 for (state = conn->pending_trans; state != NULL;
870 state = state->next) {
871 if (state->mid == req->mid) {
872 break;
876 if ((state == NULL) || (state->cmd != SMBtrans)) {
877 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
878 END_PROFILE(SMBtranss);
879 return;
882 /* Revise total_params and total_data in case they have changed
883 * downwards */
885 if (SVAL(req->vwv+0, 0) < state->total_param)
886 state->total_param = SVAL(req->vwv+0, 0);
887 if (SVAL(req->vwv+1, 0) < state->total_data)
888 state->total_data = SVAL(req->vwv+1, 0);
890 pcnt = SVAL(req->vwv+2, 0);
891 poff = SVAL(req->vwv+3, 0);
892 pdisp = SVAL(req->vwv+4, 0);
894 dcnt = SVAL(req->vwv+5, 0);
895 doff = SVAL(req->vwv+6, 0);
896 ddisp = SVAL(req->vwv+7, 0);
898 state->received_param += pcnt;
899 state->received_data += dcnt;
901 if ((state->received_data > state->total_data) ||
902 (state->received_param > state->total_param))
903 goto bad_param;
905 if (pcnt) {
906 if (smb_buffer_oob(state->total_param, pdisp, pcnt)
907 || smb_buffer_oob(smb_len(req->inbuf), poff, pcnt)) {
908 goto bad_param;
910 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
913 if (dcnt) {
914 if (smb_buffer_oob(state->total_data, ddisp, dcnt)
915 || smb_buffer_oob(smb_len(req->inbuf), doff, dcnt)) {
916 goto bad_param;
918 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
921 if ((state->received_param < state->total_param) ||
922 (state->received_data < state->total_data)) {
923 END_PROFILE(SMBtranss);
924 return;
927 talloc_steal(talloc_tos(), state);
929 handle_trans(conn, req, state);
931 DLIST_REMOVE(conn->pending_trans, state);
932 SAFE_FREE(state->data);
933 SAFE_FREE(state->param);
934 TALLOC_FREE(state);
936 END_PROFILE(SMBtranss);
937 return;
939 bad_param:
941 DEBUG(0,("reply_transs: invalid trans parameters\n"));
942 DLIST_REMOVE(conn->pending_trans, state);
943 SAFE_FREE(state->data);
944 SAFE_FREE(state->param);
945 TALLOC_FREE(state);
946 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
947 END_PROFILE(SMBtranss);
948 return;