xref: /illumos-gate/usr/src/cmd/vntsd/cmd.c (revision f8871fbc)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo /*
22a93081c1SChris Gerhard  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
231ae08745Sheppo  * Use is subject to license terms.
241ae08745Sheppo  */
251ae08745Sheppo 
261ae08745Sheppo /*
271ae08745Sheppo  * Vntsd handles two types of special commands, one is telnet
281ae08745Sheppo  * commands and another is vntsd special commands.
291ae08745Sheppo  * telnet commands supported are:
301ae08745Sheppo  * WILL
311ae08745Sheppo  * WONT
321ae08745Sheppo  * DO
331ae08745Sheppo  * DONT
341ae08745Sheppo  *  TEL_ECHO
351ae08745Sheppo  *  SUPRESS
361ae08745Sheppo  *  LINEMODE
371ae08745Sheppo  * BRK
381ae08745Sheppo  * AYT
391ae08745Sheppo  * HT
40a93081c1SChris Gerhard  * NOP
411ae08745Sheppo  *
421ae08745Sheppo  * Vntsd special commands are:
4341c9e69bSZach Kissel  *  Send break			(~#)
4441c9e69bSZach Kissel  *  Send alternate break	(~^B)
4541c9e69bSZach Kissel  *  Exit			(~.)
4641c9e69bSZach Kissel  *  Force write access		(~w)
4741c9e69bSZach Kissel  *  Console next		(~n)
4841c9e69bSZach Kissel  *  Console previous		(~p)
4941c9e69bSZach Kissel  *  Help			(~?)
501ae08745Sheppo  */
511ae08745Sheppo 
521ae08745Sheppo #include <stdio.h>
531ae08745Sheppo #include <stdlib.h>
541ae08745Sheppo #include <string.h>
551ae08745Sheppo #include <unistd.h>
561ae08745Sheppo #include <sys/types.h>
571ae08745Sheppo #include <sys/socket.h>
581ae08745Sheppo #include <netinet/in.h>
591ae08745Sheppo #include <thread.h>
601ae08745Sheppo #include <ctype.h>
611ae08745Sheppo #include <sys/termio.h>
621ae08745Sheppo #include <libintl.h>
631ae08745Sheppo #include <syslog.h>
641ae08745Sheppo #include "vntsd.h"
651ae08745Sheppo #include "chars.h"
661ae08745Sheppo 
671ae08745Sheppo char vntsd_eol[] = { CR, LF, 0};
681ae08745Sheppo 
691ae08745Sheppo typedef	int	    (*e_func_t)(vntsd_client_t *clientp);
701ae08745Sheppo /* structure for daemon special cmd */
711ae08745Sheppo typedef struct {
721ae08745Sheppo 	char e_char;				/* char to match on */
731ae08745Sheppo 	char *e_help;				/* help string */
741ae08745Sheppo 	e_func_t e_func;			/* command */
751ae08745Sheppo } esctable_t;
761ae08745Sheppo 
771ae08745Sheppo /* genbrk() -  send a break to vcc driver */
781ae08745Sheppo static int
791ae08745Sheppo genbrk(vntsd_client_t *clientp)
801ae08745Sheppo {
811ae08745Sheppo 
821ae08745Sheppo 	vntsd_cons_t *consp;
831ae08745Sheppo 
841ae08745Sheppo 	assert(clientp);
851ae08745Sheppo 	assert(clientp->cons);
861ae08745Sheppo 
871ae08745Sheppo 	consp = clientp->cons;
881ae08745Sheppo 	D1(stderr, "t@%d genbrk fd=%d sockfd %d\n", thr_self(),
891ae08745Sheppo 	    consp->vcc_fd, clientp->sockfd);
901ae08745Sheppo 
911ae08745Sheppo 	assert(consp->clientpq != NULL);
921ae08745Sheppo 	if (consp->clientpq->handle != clientp) {
931ae08745Sheppo 		/* reader */
941ae08745Sheppo 		return (vntsd_write_line(clientp,
95a93081c1SChris Gerhard 		    gettext(VNTSD_NO_WRITE_ACCESS_MSG)));
961ae08745Sheppo 	}
971ae08745Sheppo 
981ae08745Sheppo 	/* writer */
991ae08745Sheppo 	if (ioctl(consp->vcc_fd, TCSBRK, NULL)) {
1001ae08745Sheppo 		return (VNTSD_ERR_VCC_IOCTL);
1011ae08745Sheppo 	}
1021ae08745Sheppo 
1031ae08745Sheppo 	return (VNTSD_STATUS_CONTINUE);
1041ae08745Sheppo }
1051ae08745Sheppo 
10641c9e69bSZach Kissel 
10741c9e69bSZach Kissel /* genaltbrk() - handle the alternate break sequence */
10841c9e69bSZach Kissel static int
10941c9e69bSZach Kissel genaltbrk(vntsd_client_t *clientp)
11041c9e69bSZach Kissel {
11141c9e69bSZach Kissel 	vntsd_cons_t *consp;
11241c9e69bSZach Kissel 	char brkseq[2] = { '~', CNTRL('B')};
11341c9e69bSZach Kissel 
11441c9e69bSZach Kissel 	assert(clientp);
11541c9e69bSZach Kissel 	assert(clientp->cons);
11641c9e69bSZach Kissel 
11741c9e69bSZach Kissel 	consp = clientp->cons;
11841c9e69bSZach Kissel 	D1(stderr, "t@%d genaltbrk fd=%d sockfd %d\n", thr_self(),
11941c9e69bSZach Kissel 	    consp->vcc_fd, clientp->sockfd);
12041c9e69bSZach Kissel 
12141c9e69bSZach Kissel 	assert(consp->clientpq != NULL);
12241c9e69bSZach Kissel 	if (consp->clientpq->handle != clientp) {
12341c9e69bSZach Kissel 		/* reader */
12441c9e69bSZach Kissel 		return (vntsd_write_line(clientp,
12541c9e69bSZach Kissel 		    gettext(VNTSD_NO_WRITE_ACCESS_MSG)));
12641c9e69bSZach Kissel 	}
12741c9e69bSZach Kissel 
12841c9e69bSZach Kissel 	/*
12941c9e69bSZach Kissel 	 * Unlike the genbrk() function we will just forward the break sequence
13041c9e69bSZach Kissel 	 * on to vcc and subsequently the underlying console driver. This will
13141c9e69bSZach Kissel 	 * involve sending the characters '~' and CNTRL('B').
13241c9e69bSZach Kissel 	 */
13341c9e69bSZach Kissel 	if ((vntsd_write_fd(clientp->cons->vcc_fd, brkseq, sizeof (brkseq))) ==
13441c9e69bSZach Kissel 	    VNTSD_SUCCESS)
13541c9e69bSZach Kissel 		return (VNTSD_STATUS_CONTINUE);
13641c9e69bSZach Kissel 	else
13741c9e69bSZach Kissel 		return (VNTSD_STATUS_VCC_IO_ERR);
13841c9e69bSZach Kissel }
13941c9e69bSZach Kissel 
1401ae08745Sheppo /*
1411ae08745Sheppo  * console_forward()  - cycle client to the next console
1421ae08745Sheppo  * in the group queue.
1431ae08745Sheppo  */
1441ae08745Sheppo static int
1451e8f6090Sdtse console_forward(vntsd_client_t *clientp)
1461ae08745Sheppo {
1471e8f6090Sdtse 	/* forward when there are mutiple consoles in the group */
1481e8f6090Sdtse 	if (clientp->cons->group->num_cons > 1)
1491e8f6090Sdtse 		return (VNTSD_STATUS_MOV_CONS_FORWARD);
1501e8f6090Sdtse 
1511e8f6090Sdtse 	return (VNTSD_STATUS_CONTINUE);
1521e8f6090Sdtse 
1531ae08745Sheppo }
1541ae08745Sheppo 
1551ae08745Sheppo /*
1561ae08745Sheppo  * console_backward()  - cycle client to the previous
1571ae08745Sheppo  * console in the group queue.
1581ae08745Sheppo  */
1591ae08745Sheppo static int
1601e8f6090Sdtse console_backward(vntsd_client_t *clientp)
1611ae08745Sheppo {
1621e8f6090Sdtse 	/* backward when there are mutiple consoles in the group */
1631e8f6090Sdtse 	if (clientp->cons->group->num_cons > 1)
1641e8f6090Sdtse 		return (VNTSD_STATUS_MOV_CONS_BACKWARD);
1651e8f6090Sdtse 
1661e8f6090Sdtse 	return (VNTSD_STATUS_CONTINUE);
1671e8f6090Sdtse 
1681ae08745Sheppo }
1691ae08745Sheppo 
1701ae08745Sheppo /* acquire_write() - acquire write access to a console. */
1711ae08745Sheppo static int
1721ae08745Sheppo acquire_write(vntsd_client_t *clientp)
1731ae08745Sheppo {
1741ae08745Sheppo 	int	rv;
1751ae08745Sheppo 	int	yes_no = 1;
1761ae08745Sheppo 	vntsd_cons_t *consp;
1771ae08745Sheppo 
1781ae08745Sheppo 	assert(clientp);
1791ae08745Sheppo 	consp = clientp->cons;
1801ae08745Sheppo 	assert(consp);
1811ae08745Sheppo 
1821ae08745Sheppo 	if (consp->clientpq->handle == clientp) {
1831ae08745Sheppo 		/* client is a  writer */
1841ae08745Sheppo 		if ((rv = vntsd_write_line(clientp,
185a93081c1SChris Gerhard 		    gettext("You have write permission"))) !=
1861ae08745Sheppo 		    VNTSD_SUCCESS) {
1871ae08745Sheppo 			return (rv);
1881ae08745Sheppo 
1891ae08745Sheppo 		}
1901ae08745Sheppo 		return (VNTSD_STATUS_CONTINUE);
1911ae08745Sheppo 	}
1921ae08745Sheppo 
1931ae08745Sheppo 	/* message to client */
1941ae08745Sheppo 	if ((rv = vntsd_write_client(clientp, vntsd_eol, VNTSD_EOL_LEN))
1951ae08745Sheppo 	    != VNTSD_SUCCESS) {
1961ae08745Sheppo 		return (rv);
1971ae08745Sheppo 	}
1981ae08745Sheppo 
1991ae08745Sheppo 	/*
2001ae08745Sheppo 	 * TRANSLATION_NOTE
2011ae08745Sheppo 	 * The following string should be formatted to fit on multiple lines
2021ae08745Sheppo 	 * assuming a line width of at most 78 characters. There must be no
2031ae08745Sheppo 	 * trailing newline.
2041ae08745Sheppo 	 */
2051ae08745Sheppo 	if ((rv = vntsd_write_lines(clientp,
206a93081c1SChris Gerhard 	    gettext("Warning: another user currently "
2071ae08745Sheppo 	    "has write permission\nto this console and forcibly removing "
2081ae08745Sheppo 	    "him/her will terminate\nany current write action and all work "
2091ae08745Sheppo 	    "will be lost."))) != VNTSD_SUCCESS) {
2101ae08745Sheppo 		return (rv);
2111ae08745Sheppo 	}
2121ae08745Sheppo 
2131ae08745Sheppo 	/* get client yes no */
2141ae08745Sheppo 	if ((rv = vntsd_write_client(clientp, vntsd_eol,
215a93081c1SChris Gerhard 	    VNTSD_EOL_LEN)) != VNTSD_SUCCESS) {
2161ae08745Sheppo 		return (rv);
2171ae08745Sheppo 	}
2181ae08745Sheppo 
2191ae08745Sheppo 	if ((rv = vntsd_get_yes_no(clientp,
220a93081c1SChris Gerhard 	    gettext("Would you like to continue?"),
221a93081c1SChris Gerhard 	    &yes_no)) != VNTSD_SUCCESS) {
2221ae08745Sheppo 		return (rv);
2231ae08745Sheppo 	}
2241ae08745Sheppo 
2251ae08745Sheppo 	if (yes_no == B_FALSE) {
2261ae08745Sheppo 		/* client change mind no need to acquire  write access */
2271ae08745Sheppo 		return (VNTSD_STATUS_CONTINUE);
2281ae08745Sheppo 	}
2291ae08745Sheppo 
2301ae08745Sheppo 	return (VNTSD_STATUS_ACQUIRE_WRITER);
2311ae08745Sheppo }
2321ae08745Sheppo 
2331ae08745Sheppo /* client_exit()  - disconnect client from the console. */
2341ae08745Sheppo static int
235*f8871fbcSToomas Soome client_exit(vntsd_client_t *arg __unused)
2361ae08745Sheppo {
2371ae08745Sheppo 	return (VNTSD_STATUS_RESELECT_CONS);
2381ae08745Sheppo }
2391ae08745Sheppo 
2401ae08745Sheppo static int daemon_cmd_help(vntsd_client_t *clientp);
2411ae08745Sheppo 
2421ae08745Sheppo /* table for daemon commands */
2431ae08745Sheppo 
2441ae08745Sheppo static esctable_t  etable[] = {
2451ae08745Sheppo 
2461ae08745Sheppo 	/* send a break to vcc */
2478e6a2a04Slm 	{'#', "Send break",  genbrk},
2481ae08745Sheppo 
24941c9e69bSZach Kissel 	/* alternate break sequence */
25041c9e69bSZach Kissel 	{CNTRL('B'), "Send alternate break", genaltbrk},
25141c9e69bSZach Kissel 
2521ae08745Sheppo 	/* exit */
253*f8871fbcSToomas Soome 	{'.', "Exit from this console",  client_exit},
2541ae08745Sheppo 
2551ae08745Sheppo 	/* acquire write access */
2568e6a2a04Slm 	{'w', "Force write access", acquire_write},
2571ae08745Sheppo 
2581ae08745Sheppo 	/* connect to next console in queue */
2598e6a2a04Slm 	{'n', "Console next", (e_func_t)console_forward},
2601ae08745Sheppo 
2611ae08745Sheppo 	/* connect to previous console in queue */
2628e6a2a04Slm 	{'p', "Console previous", (e_func_t)console_backward},
2631ae08745Sheppo 
2641ae08745Sheppo 	/* help must be next to last */
2658e6a2a04Slm 	{'?', "Help", daemon_cmd_help},
2661ae08745Sheppo 
2671ae08745Sheppo 	/* table terminator */
2681ae08745Sheppo 	{0, 0, 0}
2691ae08745Sheppo };
2701ae08745Sheppo 
2711ae08745Sheppo void
2721ae08745Sheppo vntsd_init_esctable_msgs(void)
2731ae08745Sheppo {
2741ae08745Sheppo 	esctable_t  *p;
2751ae08745Sheppo 
2761ae08745Sheppo 	for (p = etable; p->e_char != '\0'; p++) {
2771ae08745Sheppo 		p->e_help = gettext(p->e_help);
2781ae08745Sheppo 	}
2791ae08745Sheppo }
2801ae08745Sheppo 
2811ae08745Sheppo /* daemon_cmd_help() - print help. */
2821ae08745Sheppo static int
2831ae08745Sheppo daemon_cmd_help(vntsd_client_t *clientp)
2841ae08745Sheppo {
2851ae08745Sheppo 	esctable_t  *p;
2861ae08745Sheppo 	int	    rv;
2871ae08745Sheppo 	char	    buf[VNTSD_LINE_LEN];
2881ae08745Sheppo 
2891ae08745Sheppo 	if ((rv = vntsd_write_client(clientp, vntsd_eol,
290a93081c1SChris Gerhard 	    VNTSD_EOL_LEN)) != VNTSD_SUCCESS) {
291a93081c1SChris Gerhard 		return (rv);
2921ae08745Sheppo 	}
2931ae08745Sheppo 
2941ae08745Sheppo 	/*
2951ae08745Sheppo 	 * TRANSLATION_NOTE
2961ae08745Sheppo 	 * VNTSD is the name of the VNTS daemon and should not be translated.
2971ae08745Sheppo 	 */
2981ae08745Sheppo 	if ((rv = vntsd_write_line(clientp, gettext("VNTSD commands"))) !=
2991ae08745Sheppo 	    VNTSD_SUCCESS) {
3001ae08745Sheppo 		return (rv);
3011ae08745Sheppo 	}
3021ae08745Sheppo 
3031ae08745Sheppo 	for (p = etable; p->e_char; p++) {
30441c9e69bSZach Kissel 
30541c9e69bSZach Kissel 		if (p->e_char == CNTRL('B')) {
30641c9e69bSZach Kissel 			(void) snprintf(buf, sizeof (buf), "~^B --%s",
30741c9e69bSZach Kissel 			    p->e_help);
30841c9e69bSZach Kissel 		} else {
30941c9e69bSZach Kissel 			(void) snprintf(buf, sizeof (buf),
31041c9e69bSZach Kissel 			    "~%c --%s", p->e_char, p->e_help);
31141c9e69bSZach Kissel 		}
3121ae08745Sheppo 
3131ae08745Sheppo 		if ((rv = vntsd_write_line(clientp, buf)) != VNTSD_SUCCESS) {
3141ae08745Sheppo 			return (rv);
3151ae08745Sheppo 		}
3161ae08745Sheppo 	}
3171ae08745Sheppo 
3181ae08745Sheppo 	return (VNTSD_STATUS_CONTINUE);
3191ae08745Sheppo }
3201ae08745Sheppo 
3211ae08745Sheppo /* exit from daemon command */
3221ae08745Sheppo static int
3231ae08745Sheppo exit_daemon_cmd(vntsd_client_t *clientp, int rv)
3241ae08745Sheppo {
3251ae08745Sheppo 	(void) mutex_lock(&clientp->lock);
3261ae08745Sheppo 	clientp->status &= ~VNTSD_CLIENT_DISABLE_DAEMON_CMD;
3271ae08745Sheppo 	(void) mutex_unlock(&clientp->lock);
3281ae08745Sheppo 	return (rv);
3291ae08745Sheppo }
3301ae08745Sheppo 
331d10e4ef2Snarayan /*
332d10e4ef2Snarayan  * vntsd_process_daemon_cmd() - special commands
333d10e4ef2Snarayan  * "<RET>~"  vntsd daemon commands
334d10e4ef2Snarayan  * "<RET>~~" enter '~' character
335d10e4ef2Snarayan  */
3361ae08745Sheppo int
3371ae08745Sheppo vntsd_process_daemon_cmd(vntsd_client_t *clientp, char c)
3381ae08745Sheppo {
3391ae08745Sheppo 	esctable_t *p;
3401ae08745Sheppo 	int	    rv;
341d10e4ef2Snarayan 	char	    prev_char;
342d10e4ef2Snarayan 
343d10e4ef2Snarayan 	prev_char = clientp->prev_char;
3441ae08745Sheppo 
345d10e4ef2Snarayan 	if (c != VNTSD_DAEMON_CMD || (prev_char != 0 && prev_char != CR)) {
3461ae08745Sheppo 		/* not a daemon command */
3471ae08745Sheppo 		return (VNTSD_SUCCESS);
3481ae08745Sheppo 	}
3491ae08745Sheppo 
3501ae08745Sheppo 	if (clientp->status & VNTSD_CLIENT_DISABLE_DAEMON_CMD) {
3511ae08745Sheppo 		return (VNTSD_STATUS_CONTINUE);
3521ae08745Sheppo 	}
3531ae08745Sheppo 
3541ae08745Sheppo 	/* no reentry to process_daemon_cmd */
3551ae08745Sheppo 	(void) mutex_lock(&clientp->lock);
3561ae08745Sheppo 	clientp->status |= VNTSD_CLIENT_DISABLE_DAEMON_CMD;
3571ae08745Sheppo 	(void) mutex_unlock(&clientp->lock);
3581ae08745Sheppo 
3591ae08745Sheppo 	D3(stderr, "t@%d process_daemon_cmd %d %d \n", thr_self(),
3601ae08745Sheppo 	    clientp->cons->vcc_fd, clientp->sockfd);
3611ae08745Sheppo 
3621ae08745Sheppo 	/* read in command */
3631ae08745Sheppo 	if ((rv = vntsd_read_char(clientp, &c)) != VNTSD_SUCCESS) {
3641ae08745Sheppo 		return (exit_daemon_cmd(clientp, rv));
3651ae08745Sheppo 	}
3661ae08745Sheppo 
367d10e4ef2Snarayan 	if (c == VNTSD_DAEMON_CMD) {
368d10e4ef2Snarayan 		/*
369d10e4ef2Snarayan 		 * received another '~'
370d10e4ef2Snarayan 		 * a user types '~~' to get '~'
371d10e4ef2Snarayan 		 */
372d10e4ef2Snarayan 		(void) mutex_lock(&clientp->lock);
373d10e4ef2Snarayan 		clientp->status &= ~VNTSD_CLIENT_DISABLE_DAEMON_CMD;
374d10e4ef2Snarayan 		(void) mutex_unlock(&clientp->lock);
375d10e4ef2Snarayan 		return (VNTSD_SUCCESS);
376d10e4ef2Snarayan 	}
377d10e4ef2Snarayan 
3781ae08745Sheppo 	for (p = etable; p->e_char; p++) {
3791ae08745Sheppo 		if (p->e_char == c) {
3801ae08745Sheppo 			/* found match */
3811ae08745Sheppo 			assert(p->e_func);
3821ae08745Sheppo 			rv = (*p->e_func)(clientp);
3831ae08745Sheppo 			return (exit_daemon_cmd(clientp, rv));
3841ae08745Sheppo 		}
3851ae08745Sheppo 	}
3861ae08745Sheppo 
387