2 Unix SMB/CIFS implementation.
3 Inter-process communication and named pipe handling
4 Copyright (C) Andrew Tridgell 1992-1998
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
31 #define NERR_notsupported 50
33 static void api_no_reply(struct smb_request *req);
35 /*******************************************************************
36 copies parameters and data, as needed, into the smb buffer
38 *both* the data and params sections should be aligned. this
39 is fudged in the rpc pipes by
40 at present, only the data section is. this may be a possible
41 cause of some of the ipc problems being experienced. lkcl26dec97
43 ******************************************************************/
45 static void copy_trans_params_and_data(char *outbuf, int align,
46 char *rparam, int param_offset, int param_len,
47 char *rdata, int data_offset, int data_len)
49 char *copy_into = smb_buf(outbuf);
57 DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d] (align %d)\n",
58 param_offset, param_offset + param_len,
59 data_offset , data_offset + data_len,
67 memcpy(copy_into, &rparam[param_offset], param_len);
69 copy_into += param_len;
71 memset(copy_into, '\0', align);
77 memcpy(copy_into, &rdata[data_offset], data_len);
80 /****************************************************************************
82 ****************************************************************************/
84 void send_trans_reply(struct smb_request *req,
85 char *rparam, int rparam_len,
86 char *rdata, int rdata_len,
87 bool buffer_too_large)
89 int this_ldata,this_lparam;
90 int tot_data_sent = 0;
91 int tot_param_sent = 0;
94 int ldata = rdata ? rdata_len : 0;
95 int lparam = rparam ? rparam_len : 0;
98 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata ));
100 this_lparam = MIN(lparam,max_send - 500); /* hack */
101 this_ldata = MIN(ldata,max_send - (500+this_lparam));
103 align = ((this_lparam)%4);
105 reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
107 copy_trans_params_and_data((char *)req->outbuf, align,
108 rparam, tot_param_sent, this_lparam,
109 rdata, tot_data_sent, this_ldata);
111 SSVAL(req->outbuf,smb_vwv0,lparam);
112 SSVAL(req->outbuf,smb_vwv1,ldata);
113 SSVAL(req->outbuf,smb_vwv3,this_lparam);
114 SSVAL(req->outbuf,smb_vwv4,smb_offset(smb_buf(req->outbuf)+1,
116 SSVAL(req->outbuf,smb_vwv5,0);
117 SSVAL(req->outbuf,smb_vwv6,this_ldata);
118 SSVAL(req->outbuf,smb_vwv7,smb_offset(smb_buf(req->outbuf)+1+
121 SSVAL(req->outbuf,smb_vwv8,0);
122 SSVAL(req->outbuf,smb_vwv9,0);
124 if (buffer_too_large) {
125 error_packet_set((char *)req->outbuf,
127 STATUS_BUFFER_OVERFLOW,
131 show_msg((char *)req->outbuf);
132 if (!send_smb(smbd_server_fd(),(char *)req->outbuf))
133 exit_server_cleanly("send_trans_reply: send_smb failed.");
135 TALLOC_FREE(req->outbuf);
137 tot_data_sent = this_ldata;
138 tot_param_sent = this_lparam;
140 while (tot_data_sent < ldata || tot_param_sent < lparam)
142 this_lparam = MIN(lparam-tot_param_sent, max_send - 500); /* hack */
143 this_ldata = MIN(ldata -tot_data_sent, max_send - (500+this_lparam));
151 align = (this_lparam%4);
153 reply_outbuf(req, 10, 1+this_ldata+this_lparam+align);
155 copy_trans_params_and_data((char *)req->outbuf, align,
156 rparam, tot_param_sent, this_lparam,
157 rdata, tot_data_sent, this_ldata);
159 SSVAL(req->outbuf,smb_vwv3,this_lparam);
160 SSVAL(req->outbuf,smb_vwv4,smb_offset(smb_buf(req->outbuf)+1,
162 SSVAL(req->outbuf,smb_vwv5,tot_param_sent);
163 SSVAL(req->outbuf,smb_vwv6,this_ldata);
164 SSVAL(req->outbuf,smb_vwv7,smb_offset(smb_buf(req->outbuf)+1+
167 SSVAL(req->outbuf,smb_vwv8,tot_data_sent);
168 SSVAL(req->outbuf,smb_vwv9,0);
170 if (buffer_too_large) {
171 error_packet_set((char *)req->outbuf,
173 STATUS_BUFFER_OVERFLOW,
177 show_msg((char *)req->outbuf);
178 if (!send_smb(smbd_server_fd(), (char *)req->outbuf))
179 exit_server_cleanly("send_trans_reply: send_smb failed.");
181 tot_data_sent += this_ldata;
182 tot_param_sent += this_lparam;
183 TALLOC_FREE(req->outbuf);
187 /****************************************************************************
188 Start the first part of an RPC reply which began with an SMBtrans request.
189 ****************************************************************************/
191 static void api_rpc_trans_reply(struct smb_request *req, smb_np_struct *p)
193 bool is_data_outstanding;
194 char *rdata = (char *)SMB_MALLOC(p->max_trans_reply);
198 DEBUG(0,("api_rpc_trans_reply: malloc fail.\n"));
199 reply_nterror(req, NT_STATUS_NO_MEMORY);
203 if((data_len = read_from_pipe( p, rdata, p->max_trans_reply,
204 &is_data_outstanding)) < 0) {
210 send_trans_reply(req, NULL, 0, rdata, data_len, is_data_outstanding);
215 /****************************************************************************
216 WaitNamedPipeHandleState
217 ****************************************************************************/
219 static void api_WNPHS(struct smb_request *req, smb_np_struct *p,
220 char *param, int param_len)
224 if (!param || param_len < 2) {
225 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
229 priority = SVAL(param,0);
230 DEBUG(4,("WaitNamedPipeHandleState priority %x\n", priority));
232 if (wait_rpc_pipe_hnd_state(p, priority)) {
233 /* now send the reply */
234 send_trans_reply(req, NULL, 0, NULL, 0, False);
241 /****************************************************************************
242 SetNamedPipeHandleState
243 ****************************************************************************/
245 static void api_SNPHS(struct smb_request *req, smb_np_struct *p,
246 char *param, int param_len)
250 if (!param || param_len < 2) {
251 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
256 DEBUG(4,("SetNamedPipeHandleState to code %x\n", id));
258 if (set_rpc_pipe_hnd_state(p, id)) {
259 /* now send the reply */
260 send_trans_reply(req, NULL, 0, NULL, 0, False);
267 /****************************************************************************
268 When no reply is generated, indicate unsupported.
269 ****************************************************************************/
271 static void api_no_reply(struct smb_request *req)
276 SSVAL(rparam,0,NERR_notsupported);
277 SSVAL(rparam,2,0); /* converter word */
279 DEBUG(3,("Unsupported API fd command\n"));
281 /* now send the reply */
282 send_trans_reply(req, rparam, 4, NULL, 0, False);
287 /****************************************************************************
288 Handle remote api calls delivered to a named pipe already opened.
289 ****************************************************************************/
291 static void api_fd_reply(connection_struct *conn, uint16 vuid,
292 struct smb_request *req,
293 uint16 *setup, char *data, char *params,
294 int suwcnt, int tdscnt, int tpscnt,
295 int mdrcnt, int mprcnt)
298 smb_np_struct *p = NULL;
302 DEBUG(5,("api_fd_reply\n"));
304 /* First find out the name of this file. */
306 DEBUG(0,("Unexpected named pipe transaction.\n"));
307 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
311 /* Get the file handle and hence the file name. */
313 * NB. The setup array has already been transformed
314 * via SVAL and so is in gost byte order.
316 pnum = ((int)setup[1]) & 0xFFFF;
317 subcommand = ((int)setup[0]) & 0xFFFF;
319 if(!(p = get_rpc_pipe(pnum))) {
320 if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
321 /* Win9x does this call with a unicode pipe name, not a pnum. */
322 /* Just return success for now... */
323 DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
324 send_trans_reply(req, NULL, 0, NULL, 0, False);
328 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
329 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
333 if (vuid != p->vuid) {
334 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
335 "expected %d\n", pnum, vuid, p->vuid));
336 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
340 DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n", subcommand, p->name, pnum));
342 /* record maximum data length that can be transmitted in an SMBtrans */
343 p->max_trans_reply = mdrcnt;
345 DEBUG(10,("api_fd_reply: p:%p max_trans_reply: %d\n", p, p->max_trans_reply));
347 switch (subcommand) {
348 case TRANSACT_DCERPCCMD:
349 /* dce/rpc command */
350 reply = write_to_pipe(p, data, tdscnt);
355 api_rpc_trans_reply(req, p);
357 case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
358 /* Wait Named Pipe Handle state */
359 api_WNPHS(req, p, params, tpscnt);
361 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
362 /* Set Named Pipe Handle state */
363 api_SNPHS(req, p, params, tpscnt);
366 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
371 /****************************************************************************
372 Handle named pipe commands.
373 ****************************************************************************/
375 static void named_pipe(connection_struct *conn, uint16 vuid,
376 struct smb_request *req,
377 const char *name, uint16 *setup,
378 char *data, char *params,
379 int suwcnt, int tdscnt,int tpscnt,
380 int msrcnt, int mdrcnt, int mprcnt)
382 DEBUG(3,("named pipe command on <%s> name\n", name));
384 if (strequal(name,"LANMAN")) {
385 api_reply(conn, vuid, req,
392 if (strequal(name,"WKSSVC") ||
393 strequal(name,"SRVSVC") ||
394 strequal(name,"WINREG") ||
395 strequal(name,"SAMR") ||
396 strequal(name,"LSARPC")) {
398 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
400 api_fd_reply(conn, vuid, req,
402 suwcnt, tdscnt, tpscnt,
407 if (strlen(name) < 1) {
408 api_fd_reply(conn, vuid, req,
410 params, suwcnt, tdscnt,
411 tpscnt, mdrcnt, mprcnt);
416 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
417 (int)setup[0],(int)setup[1]));
419 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
423 static void handle_trans(connection_struct *conn, struct smb_request *req,
424 struct trans_state *state)
426 char *local_machine_name;
429 DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
430 state->name,(unsigned int)state->total_data,(unsigned int)state->total_param,
431 (unsigned int)state->setup_count));
434 * WinCE wierdness....
437 local_machine_name = talloc_asprintf(state, "\\%s\\",
438 get_local_machine_name());
440 if (local_machine_name == NULL) {
441 reply_nterror(req, NT_STATUS_NO_MEMORY);
445 if (strnequal(state->name, local_machine_name,
446 strlen(local_machine_name))) {
447 name_offset = strlen(local_machine_name)-1;
450 if (!strnequal(&state->name[name_offset], "\\PIPE",
452 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
456 name_offset += strlen("\\PIPE");
458 /* Win9x weirdness. When talking to a unicode server Win9x
459 only sends \PIPE instead of \PIPE\ */
461 if (state->name[name_offset] == '\\')
464 DEBUG(5,("calling named_pipe\n"));
465 named_pipe(conn, state->vuid, req,
466 state->name+name_offset,
467 state->setup,state->data,
469 state->setup_count,state->total_data,
471 state->max_setup_return,
472 state->max_data_return,
473 state->max_param_return);
475 if (state->close_on_completion)
476 close_cnum(conn,state->vuid);
481 /****************************************************************************
483 ****************************************************************************/
485 void reply_trans(connection_struct *conn, struct smb_request *req)
491 struct trans_state *state;
495 START_PROFILE(SMBtrans);
498 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
499 END_PROFILE(SMBtrans);
503 size = smb_len(req->inbuf) + 4;
504 dsoff = SVAL(req->inbuf, smb_dsoff);
505 dscnt = SVAL(req->inbuf, smb_dscnt);
506 psoff = SVAL(req->inbuf, smb_psoff);
507 pscnt = SVAL(req->inbuf, smb_pscnt);
509 result = allow_new_trans(conn->pending_trans, req->mid);
510 if (!NT_STATUS_IS_OK(result)) {
511 DEBUG(2, ("Got invalid trans request: %s\n",
513 reply_nterror(req, result);
514 END_PROFILE(SMBtrans);
518 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
519 DEBUG(0, ("talloc failed\n"));
520 reply_nterror(req, NT_STATUS_NO_MEMORY);
521 END_PROFILE(SMBtrans);
525 state->cmd = SMBtrans;
527 state->mid = req->mid;
528 state->vuid = req->vuid;
529 state->setup_count = CVAL(req->inbuf, smb_suwcnt);
531 state->total_param = SVAL(req->inbuf, smb_tpscnt);
533 state->total_data = SVAL(req->inbuf, smb_tdscnt);
535 state->max_param_return = SVAL(req->inbuf, smb_mprcnt);
536 state->max_data_return = SVAL(req->inbuf, smb_mdrcnt);
537 state->max_setup_return = CVAL(req->inbuf, smb_msrcnt);
538 state->close_on_completion = BITSETW(req->inbuf+smb_vwv5,0);
539 state->one_way = BITSETW(req->inbuf+smb_vwv5,1);
541 srvstr_pull_buf_talloc(state, req->inbuf, req->flags2, &state->name,
542 smb_buf(req->inbuf), STR_TERMINATE);
544 if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
548 if (state->total_data) {
549 /* Can't use talloc here, the core routines do realloc on the
550 * params and data. Out of paranoia, 100 bytes too many. */
551 state->data = (char *)SMB_MALLOC(state->total_data+100);
552 if (state->data == NULL) {
553 DEBUG(0,("reply_trans: data malloc fail for %u "
554 "bytes !\n", (unsigned int)state->total_data));
556 reply_nterror(req, NT_STATUS_NO_MEMORY);
557 END_PROFILE(SMBtrans);
560 /* null-terminate the slack space */
561 memset(&state->data[state->total_data], 0, 100);
562 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
564 if ((smb_base(req->inbuf)+dsoff+dscnt
565 > (char *)req->inbuf + size) ||
566 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
569 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
572 if (state->total_param) {
573 /* Can't use talloc here, the core routines do realloc on the
574 * params and data. Out of paranoia, 100 bytes too many */
575 state->param = (char *)SMB_MALLOC(state->total_param+100);
576 if (state->param == NULL) {
577 DEBUG(0,("reply_trans: param malloc fail for %u "
578 "bytes !\n", (unsigned int)state->total_param));
579 SAFE_FREE(state->data);
581 reply_nterror(req, NT_STATUS_NO_MEMORY);
582 END_PROFILE(SMBtrans);
585 /* null-terminate the slack space */
586 memset(&state->param[state->total_param], 0, 100);
587 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
589 if ((smb_base(req->inbuf)+psoff+pscnt
590 > (char *)req->inbuf + size) ||
591 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
594 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
597 state->received_data = dscnt;
598 state->received_param = pscnt;
600 if (state->setup_count) {
602 if((state->setup = TALLOC_ARRAY(
603 state, uint16, state->setup_count)) == NULL) {
604 DEBUG(0,("reply_trans: setup malloc fail for %u "
605 "bytes !\n", (unsigned int)
606 (state->setup_count * sizeof(uint16))));
607 SAFE_FREE(state->data);
608 SAFE_FREE(state->param);
610 reply_nterror(req, NT_STATUS_NO_MEMORY);
611 END_PROFILE(SMBtrans);
614 if (req->inbuf+smb_vwv14+(state->setup_count*SIZEOFWORD) >
617 if ((smb_vwv14+(state->setup_count*SIZEOFWORD) < smb_vwv14) ||
618 (smb_vwv14+(state->setup_count*SIZEOFWORD) <
619 (state->setup_count*SIZEOFWORD)))
622 for (i=0;i<state->setup_count;i++)
623 state->setup[i] = SVAL(req->inbuf,
624 smb_vwv14+i*SIZEOFWORD);
627 state->received_param = pscnt;
629 if ((state->received_param != state->total_param) ||
630 (state->received_data != state->total_data)) {
631 DLIST_ADD(conn->pending_trans, state);
633 /* We need to send an interim response then receive the rest
634 of the parameter/data bytes */
635 reply_outbuf(req, 0, 0);
636 show_msg((char *)req->outbuf);
637 END_PROFILE(SMBtrans);
641 handle_trans(conn, req, state);
643 SAFE_FREE(state->data);
644 SAFE_FREE(state->param);
647 END_PROFILE(SMBtrans);
652 DEBUG(0,("reply_trans: invalid trans parameters\n"));
653 SAFE_FREE(state->data);
654 SAFE_FREE(state->param);
656 END_PROFILE(SMBtrans);
657 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
661 /****************************************************************************
662 Reply to a secondary SMBtrans.
663 ****************************************************************************/
665 void reply_transs(connection_struct *conn, struct smb_request *req)
667 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
668 struct trans_state *state;
671 START_PROFILE(SMBtranss);
673 show_msg((char *)req->inbuf);
676 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
677 END_PROFILE(SMBtranss);
681 for (state = conn->pending_trans; state != NULL;
682 state = state->next) {
683 if (state->mid == req->mid) {
688 if ((state == NULL) || (state->cmd != SMBtrans)) {
689 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
690 END_PROFILE(SMBtranss);
694 /* Revise total_params and total_data in case they have changed
697 if (SVAL(req->inbuf, smb_vwv0) < state->total_param)
698 state->total_param = SVAL(req->inbuf,smb_vwv0);
699 if (SVAL(req->inbuf, smb_vwv1) < state->total_data)
700 state->total_data = SVAL(req->inbuf,smb_vwv1);
702 size = smb_len(req->inbuf) + 4;
704 pcnt = SVAL(req->inbuf, smb_spscnt);
705 poff = SVAL(req->inbuf, smb_spsoff);
706 pdisp = SVAL(req->inbuf, smb_spsdisp);
708 dcnt = SVAL(req->inbuf, smb_sdscnt);
709 doff = SVAL(req->inbuf, smb_sdsoff);
710 ddisp = SVAL(req->inbuf, smb_sdsdisp);
712 state->received_param += pcnt;
713 state->received_data += dcnt;
715 if ((state->received_data > state->total_data) ||
716 (state->received_param > state->total_param))
720 if (pdisp+pcnt > state->total_param)
722 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
724 if (pdisp > state->total_param)
726 if ((smb_base(req->inbuf) + poff + pcnt
727 > (char *)req->inbuf + size) ||
728 (smb_base(req->inbuf) + poff + pcnt
729 < smb_base(req->inbuf)))
731 if (state->param + pdisp < state->param)
734 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,
739 if (ddisp+dcnt > state->total_data)
741 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
743 if (ddisp > state->total_data)
745 if ((smb_base(req->inbuf) + doff + dcnt
746 > (char *)req->inbuf + size) ||
747 (smb_base(req->inbuf) + doff + dcnt
748 < smb_base(req->inbuf)))
750 if (state->data + ddisp < state->data)
753 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
757 if ((state->received_param < state->total_param) ||
758 (state->received_data < state->total_data)) {
759 END_PROFILE(SMBtranss);
764 * construct_reply_common will copy smb_com from inbuf to
765 * outbuf. SMBtranss is wrong here.
767 SCVAL(req->inbuf,smb_com,SMBtrans);
769 handle_trans(conn, req, state);
771 DLIST_REMOVE(conn->pending_trans, state);
772 SAFE_FREE(state->data);
773 SAFE_FREE(state->param);
776 END_PROFILE(SMBtranss);
781 DEBUG(0,("reply_transs: invalid trans parameters\n"));
782 DLIST_REMOVE(conn->pending_trans, state);
783 SAFE_FREE(state->data);
784 SAFE_FREE(state->param);
786 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
787 END_PROFILE(SMBtranss);