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 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 memset(state->name, '\0',sizeof(state->name));
542 srvstr_pull_buf(req->inbuf, req->flags2, state->name,
543 smb_buf(req->inbuf), sizeof(state->name),
546 if ((dscnt > state->total_data) || (pscnt > state->total_param))
549 if (state->total_data) {
550 /* Can't use talloc here, the core routines do realloc on the
551 * params and data. Out of paranoia, 100 bytes too many. */
552 state->data = (char *)SMB_MALLOC(state->total_data+100);
553 if (state->data == NULL) {
554 DEBUG(0,("reply_trans: data malloc fail for %u "
555 "bytes !\n", (unsigned int)state->total_data));
557 reply_nterror(req, NT_STATUS_NO_MEMORY);
558 END_PROFILE(SMBtrans);
561 /* null-terminate the slack space */
562 memset(&state->data[state->total_data], 0, 100);
563 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
565 if ((smb_base(req->inbuf)+dsoff+dscnt
566 > (char *)req->inbuf + size) ||
567 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
570 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
573 if (state->total_param) {
574 /* Can't use talloc here, the core routines do realloc on the
575 * params and data. Out of paranoia, 100 bytes too many */
576 state->param = (char *)SMB_MALLOC(state->total_param+100);
577 if (state->param == NULL) {
578 DEBUG(0,("reply_trans: param malloc fail for %u "
579 "bytes !\n", (unsigned int)state->total_param));
580 SAFE_FREE(state->data);
582 reply_nterror(req, NT_STATUS_NO_MEMORY);
583 END_PROFILE(SMBtrans);
586 /* null-terminate the slack space */
587 memset(&state->param[state->total_param], 0, 100);
588 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
590 if ((smb_base(req->inbuf)+psoff+pscnt
591 > (char *)req->inbuf + size) ||
592 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
595 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
598 state->received_data = dscnt;
599 state->received_param = pscnt;
601 if (state->setup_count) {
603 if((state->setup = TALLOC_ARRAY(
604 state, uint16, state->setup_count)) == NULL) {
605 DEBUG(0,("reply_trans: setup malloc fail for %u "
606 "bytes !\n", (unsigned int)
607 (state->setup_count * sizeof(uint16))));
608 SAFE_FREE(state->data);
609 SAFE_FREE(state->param);
611 reply_nterror(req, NT_STATUS_NO_MEMORY);
612 END_PROFILE(SMBtrans);
615 if (req->inbuf+smb_vwv14+(state->setup_count*SIZEOFWORD) >
618 if ((smb_vwv14+(state->setup_count*SIZEOFWORD) < smb_vwv14) ||
619 (smb_vwv14+(state->setup_count*SIZEOFWORD) <
620 (state->setup_count*SIZEOFWORD)))
623 for (i=0;i<state->setup_count;i++)
624 state->setup[i] = SVAL(req->inbuf,
625 smb_vwv14+i*SIZEOFWORD);
628 state->received_param = pscnt;
630 if ((state->received_param != state->total_param) ||
631 (state->received_data != state->total_data)) {
632 DLIST_ADD(conn->pending_trans, state);
634 /* We need to send an interim response then receive the rest
635 of the parameter/data bytes */
636 reply_outbuf(req, 0, 0);
637 show_msg((char *)req->outbuf);
638 END_PROFILE(SMBtrans);
642 handle_trans(conn, req, state);
644 SAFE_FREE(state->data);
645 SAFE_FREE(state->param);
648 END_PROFILE(SMBtrans);
653 DEBUG(0,("reply_trans: invalid trans parameters\n"));
654 SAFE_FREE(state->data);
655 SAFE_FREE(state->param);
657 END_PROFILE(SMBtrans);
658 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
662 /****************************************************************************
663 Reply to a secondary SMBtrans.
664 ****************************************************************************/
666 void reply_transs(connection_struct *conn, struct smb_request *req)
668 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
669 struct trans_state *state;
672 START_PROFILE(SMBtranss);
674 show_msg((char *)req->inbuf);
677 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
678 END_PROFILE(SMBtranss);
682 for (state = conn->pending_trans; state != NULL;
683 state = state->next) {
684 if (state->mid == req->mid) {
689 if ((state == NULL) || (state->cmd != SMBtrans)) {
690 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
691 END_PROFILE(SMBtranss);
695 /* Revise total_params and total_data in case they have changed
698 if (SVAL(req->inbuf, smb_vwv0) < state->total_param)
699 state->total_param = SVAL(req->inbuf,smb_vwv0);
700 if (SVAL(req->inbuf, smb_vwv1) < state->total_data)
701 state->total_data = SVAL(req->inbuf,smb_vwv1);
703 size = smb_len(req->inbuf) + 4;
705 pcnt = SVAL(req->inbuf, smb_spscnt);
706 poff = SVAL(req->inbuf, smb_spsoff);
707 pdisp = SVAL(req->inbuf, smb_spsdisp);
709 dcnt = SVAL(req->inbuf, smb_sdscnt);
710 doff = SVAL(req->inbuf, smb_sdsoff);
711 ddisp = SVAL(req->inbuf, smb_sdsdisp);
713 state->received_param += pcnt;
714 state->received_data += dcnt;
716 if ((state->received_data > state->total_data) ||
717 (state->received_param > state->total_param))
721 if (pdisp+pcnt > state->total_param)
723 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
725 if (pdisp > state->total_param)
727 if ((smb_base(req->inbuf) + poff + pcnt
728 > (char *)req->inbuf + size) ||
729 (smb_base(req->inbuf) + poff + pcnt
730 < smb_base(req->inbuf)))
732 if (state->param + pdisp < state->param)
735 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,
740 if (ddisp+dcnt > state->total_data)
742 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
744 if (ddisp > state->total_data)
746 if ((smb_base(req->inbuf) + doff + dcnt
747 > (char *)req->inbuf + size) ||
748 (smb_base(req->inbuf) + doff + dcnt
749 < smb_base(req->inbuf)))
751 if (state->data + ddisp < state->data)
754 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
758 if ((state->received_param < state->total_param) ||
759 (state->received_data < state->total_data)) {
760 END_PROFILE(SMBtranss);
765 * construct_reply_common will copy smb_com from inbuf to
766 * outbuf. SMBtranss is wrong here.
768 SCVAL(req->inbuf,smb_com,SMBtrans);
770 handle_trans(conn, req, state);
772 DLIST_REMOVE(conn->pending_trans, state);
773 SAFE_FREE(state->data);
774 SAFE_FREE(state->param);
777 END_PROFILE(SMBtranss);
782 DEBUG(0,("reply_transs: invalid trans parameters\n"));
783 DLIST_REMOVE(conn->pending_trans, state);
784 SAFE_FREE(state->data);
785 SAFE_FREE(state->param);
787 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
788 END_PROFILE(SMBtranss);