xref: /onnv-gate/usr/src/cmd/sulogin/sulogin.c (revision 10020:ff5f2b3729b6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56672Snakanon  * Common Development and Distribution License (the "License").
66672Snakanon  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*10020SJoep.Vesseur@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  *	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
280Sstevel@tonic-gate  *	All rights reserved.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  *	Copyright (c) 1987, 1988 Microsoft Corporation.
310Sstevel@tonic-gate  *	All rights reserved.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  *	sulogin - special login program exec'd from init to let user
360Sstevel@tonic-gate  *	come up single user, or go to default init state straight away.
370Sstevel@tonic-gate  *
387487Sgww@eng.sun.com  *	Explain the scoop to the user, prompt for an authorized user
397487Sgww@eng.sun.com  *	name or ^D and then prompt for password or ^D.  If the password
407487Sgww@eng.sun.com  *	is correct, check if the user is authorized, if so enter
417487Sgww@eng.sun.com  *	single user. ^D exits sulogin, and init will go to default init state.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  *	If /etc/passwd is missing, or there's no entry for root,
440Sstevel@tonic-gate  *	go single user, no questions asked.
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <sys/stat.h>
490Sstevel@tonic-gate #include <sys/param.h>
500Sstevel@tonic-gate #include <sys/sysmsg_impl.h>
510Sstevel@tonic-gate #include <sys/mkdev.h>
520Sstevel@tonic-gate #include <sys/resource.h>
530Sstevel@tonic-gate #include <sys/uadmin.h>
540Sstevel@tonic-gate #include <sys/wait.h>
550Sstevel@tonic-gate #include <sys/stermio.h>
560Sstevel@tonic-gate #include <fcntl.h>
570Sstevel@tonic-gate #include <termio.h>
580Sstevel@tonic-gate #include <pwd.h>
590Sstevel@tonic-gate #include <shadow.h>
600Sstevel@tonic-gate #include <stdlib.h>
610Sstevel@tonic-gate #include <stdio.h>
620Sstevel@tonic-gate #include <signal.h>
630Sstevel@tonic-gate #include <siginfo.h>
640Sstevel@tonic-gate #include <utmpx.h>
650Sstevel@tonic-gate #include <unistd.h>
660Sstevel@tonic-gate #include <ucontext.h>
670Sstevel@tonic-gate #include <string.h>
680Sstevel@tonic-gate #include <strings.h>
690Sstevel@tonic-gate #include <deflt.h>
700Sstevel@tonic-gate #include <limits.h>
710Sstevel@tonic-gate #include <errno.h>
720Sstevel@tonic-gate #include <crypt.h>
737487Sgww@eng.sun.com #include <auth_attr.h>
747487Sgww@eng.sun.com #include <auth_list.h>
757487Sgww@eng.sun.com #include <nss_dbdefs.h>
767487Sgww@eng.sun.com #include <user_attr.h>
777688SAaron.Zang@Sun.COM #include <sys/vt.h>
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Intervals to sleep after failed login
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate #ifndef SLEEPTIME
830Sstevel@tonic-gate #define	SLEEPTIME	4	/* sleeptime before login incorrect msg */
840Sstevel@tonic-gate #endif
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #define	SLEEPTIME_MAX	5	/* maximum sleeptime */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  *	the name of the file containing the login defaults we deliberately
900Sstevel@tonic-gate  *	use the same file as login(1)
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #define	DEFAULT_LOGIN	"/etc/default/login"
940Sstevel@tonic-gate #define	DEFAULT_SULOGIN	"/etc/default/sulogin"
950Sstevel@tonic-gate #define	DEFAULT_CONSOLE	"/dev/console"
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static char	shell[]	= "/sbin/sh";
980Sstevel@tonic-gate static char	su[]	= "/sbin/su.static";
990Sstevel@tonic-gate static int	sleeptime	= SLEEPTIME;
1000Sstevel@tonic-gate static int	nchild = 0;
1010Sstevel@tonic-gate static pid_t	pidlist[10];
1020Sstevel@tonic-gate static pid_t	masterpid = 0;
1030Sstevel@tonic-gate static pid_t	originalpid = 0;
1040Sstevel@tonic-gate static struct sigaction	sa;
1050Sstevel@tonic-gate static struct termio	ttymodes;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static char	*findttyname(int fd);
1080Sstevel@tonic-gate static char	*stripttyname(char *);
1097487Sgww@eng.sun.com static char	*sulogin_getinput(char *, int);
1100Sstevel@tonic-gate static void	noop(int);
1110Sstevel@tonic-gate static void	single(const char *, char *);
1127487Sgww@eng.sun.com static void	main_loop(char *, boolean_t);
1130Sstevel@tonic-gate static void	parenthandler();
1140Sstevel@tonic-gate static void	termhandler(int);
1150Sstevel@tonic-gate static void	setupsigs(void);
1160Sstevel@tonic-gate static int	pathcmp(char *, char *);
1177487Sgww@eng.sun.com static void	doit(char *, char *);
1180Sstevel@tonic-gate static void	childcleanup(int);
1190Sstevel@tonic-gate 
1207487Sgww@eng.sun.com #define	ECHOON	0
1217487Sgww@eng.sun.com #define	ECHOOFF	1
1227487Sgww@eng.sun.com 
1230Sstevel@tonic-gate /* ARGSUSED */
1240Sstevel@tonic-gate int
main(int argc,char ** argv)1250Sstevel@tonic-gate main(int argc, char **argv)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	struct spwd	*shpw;
1280Sstevel@tonic-gate 	int		passreq = B_TRUE;
1290Sstevel@tonic-gate 	int		flags;
1300Sstevel@tonic-gate 	int		fd;
1310Sstevel@tonic-gate 	char		*infop, *ptr, *p;
1320Sstevel@tonic-gate 	pid_t		pid;
1330Sstevel@tonic-gate 	int		bufsize;
1340Sstevel@tonic-gate 	struct stat	st;
1350Sstevel@tonic-gate 	char		cttyname[100];
1360Sstevel@tonic-gate 	char		namedlist[500];
1370Sstevel@tonic-gate 	char		scratchlist[500];
1380Sstevel@tonic-gate 	dev_t		cttyd;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (geteuid() != 0) {
1410Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: must be root\n", argv[0]);
1420Sstevel@tonic-gate 		return (EXIT_FAILURE);
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/* Do the magic to determine the children */
1460Sstevel@tonic-gate 	if ((fd = open(SYSMSG, 0)) < 0)
1470Sstevel@tonic-gate 		return (EXIT_FAILURE);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/*
1500Sstevel@tonic-gate 	 * If the console supports the CIOCTTYCONSOLE ioctl, then fetch
1510Sstevel@tonic-gate 	 * its console device list.  If not, then we use the default
1520Sstevel@tonic-gate 	 * console name.
1530Sstevel@tonic-gate 	 */
1540Sstevel@tonic-gate 	if (ioctl(fd, CIOCTTYCONSOLE, &cttyd) == 0) {
1550Sstevel@tonic-gate 		if ((bufsize = ioctl(fd, CIOCGETCONSOLE, NULL)) < 0)
1560Sstevel@tonic-gate 			return (EXIT_FAILURE);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		if (bufsize > 0) {
1590Sstevel@tonic-gate 			if ((infop = calloc(bufsize, sizeof (char))) == NULL)
1600Sstevel@tonic-gate 				return (EXIT_FAILURE);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 			if (ioctl(fd, CIOCGETCONSOLE, infop) < 0)
1630Sstevel@tonic-gate 				return (EXIT_FAILURE);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 			(void) snprintf(namedlist, sizeof (namedlist), "%s %s",
1660Sstevel@tonic-gate 			    DEFAULT_CONSOLE, infop);
1670Sstevel@tonic-gate 		} else
1680Sstevel@tonic-gate 			(void) snprintf(namedlist, sizeof (namedlist), "%s",
1690Sstevel@tonic-gate 			    DEFAULT_CONSOLE);
1700Sstevel@tonic-gate 	} else {
1710Sstevel@tonic-gate 		(void) snprintf(namedlist, sizeof (namedlist), "%s",
1720Sstevel@tonic-gate 		    DEFAULT_CONSOLE);
1730Sstevel@tonic-gate 		cttyd = NODEV;
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/*
1770Sstevel@tonic-gate 	 * The attempt to turn the controlling terminals dev_t into a string
1780Sstevel@tonic-gate 	 * may not be successful, thus leaving the variable cttyname as a
1790Sstevel@tonic-gate 	 * NULL.  This occurs if during boot we find
1800Sstevel@tonic-gate 	 * the root partition (or some other partition)
1810Sstevel@tonic-gate 	 * requires manual fsck, thus resulting in sulogin
1820Sstevel@tonic-gate 	 * getting invoked.  The ioctl for CIOCTTYCONSOLE
1830Sstevel@tonic-gate 	 * called above returned NODEV for cttyd
1840Sstevel@tonic-gate 	 * in these cases.  NODEV gets returned when the vnode pointer
1850Sstevel@tonic-gate 	 * in our session structure is NULL.  In these cases it
1860Sstevel@tonic-gate 	 * must be assumed that the default console is used.
1870Sstevel@tonic-gate 	 *
1880Sstevel@tonic-gate 	 * See uts/common/os/session.c:cttydev().
1890Sstevel@tonic-gate 	 */
1900Sstevel@tonic-gate 	(void) strcpy(cttyname, DEFAULT_CONSOLE);
1910Sstevel@tonic-gate 	(void) strcpy(scratchlist, namedlist);
1920Sstevel@tonic-gate 	ptr = scratchlist;
1930Sstevel@tonic-gate 	while (ptr != NULL) {
1940Sstevel@tonic-gate 		p = strchr(ptr, ' ');
1950Sstevel@tonic-gate 		if (p == NULL) {
1960Sstevel@tonic-gate 			if (stat(ptr, &st))
1970Sstevel@tonic-gate 				return (EXIT_FAILURE);
1980Sstevel@tonic-gate 			if (st.st_rdev == cttyd)
1990Sstevel@tonic-gate 				(void) strcpy(cttyname, ptr);
2000Sstevel@tonic-gate 			break;
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 		*p++ = '\0';
2030Sstevel@tonic-gate 		if (stat(ptr, &st))
2040Sstevel@tonic-gate 			return (EXIT_FAILURE);
2050Sstevel@tonic-gate 		if (st.st_rdev == cttyd) {
2060Sstevel@tonic-gate 			(void) strcpy(cttyname, ptr);
2070Sstevel@tonic-gate 			break;
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 		ptr = p;
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * Use the same value of SLEEPTIME that login(1) uses.  This
2140Sstevel@tonic-gate 	 * is obtained by reading the file /etc/default/login using
2150Sstevel@tonic-gate 	 * the def*() functions.
2160Sstevel@tonic-gate 	 */
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (defopen(DEFAULT_LOGIN) == 0) {
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 		/* ignore case */
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		flags = defcntl(DC_GETFLAGS, 0);
2230Sstevel@tonic-gate 		TURNOFF(flags, DC_CASE);
2240Sstevel@tonic-gate 		(void) defcntl(DC_SETFLAGS, flags);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		if ((ptr = defread("SLEEPTIME=")) != NULL)
2270Sstevel@tonic-gate 			sleeptime = atoi(ptr);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 		if (sleeptime < 0 || sleeptime > SLEEPTIME_MAX)
2300Sstevel@tonic-gate 			sleeptime = SLEEPTIME;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		(void) defopen(NULL);	/* closes DEFAULT_LOGIN */
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/*
2360Sstevel@tonic-gate 	 * Use our own value of PASSREQ, separate from the one login(1) uses.
2370Sstevel@tonic-gate 	 * This is obtained by reading the file /etc/default/sulogin using
2380Sstevel@tonic-gate 	 * the def*() functions.
2390Sstevel@tonic-gate 	 */
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (defopen(DEFAULT_SULOGIN) == 0) {
2420Sstevel@tonic-gate 		if ((ptr = defread("PASSREQ=")) != NULL)
2430Sstevel@tonic-gate 			if (strcmp("NO", ptr) == 0)
2440Sstevel@tonic-gate 				passreq = B_FALSE;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 		(void) defopen(NULL);	/* closes DEFAULT_SULOGIN */
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	if (passreq == B_FALSE)
2500Sstevel@tonic-gate 		single(shell, NULL);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * if no 'root' entry in /etc/shadow, give maint. mode single
2540Sstevel@tonic-gate 	 * user shell prompt
2550Sstevel@tonic-gate 	 */
2560Sstevel@tonic-gate 	setspent();
2570Sstevel@tonic-gate 	if ((shpw = getspnam("root")) == NULL) {
2580Sstevel@tonic-gate 		(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
2590Sstevel@tonic-gate 		    "in shadow password file ***\n\n");
2600Sstevel@tonic-gate 		single(shell, NULL);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	endspent();
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * if no 'root' entry in /etc/passwd, give maint. mode single
2650Sstevel@tonic-gate 	 * user shell prompt
2660Sstevel@tonic-gate 	 */
2670Sstevel@tonic-gate 	setpwent();
2680Sstevel@tonic-gate 	if (getpwnam("root") == NULL) {
2690Sstevel@tonic-gate 		(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
2700Sstevel@tonic-gate 		    "in password file ***\n\n");
2710Sstevel@tonic-gate 		single(shell, NULL);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 	endpwent();
2740Sstevel@tonic-gate 	/* process with controlling tty treated special */
2750Sstevel@tonic-gate 	if ((pid = fork()) != (pid_t)0) {
2760Sstevel@tonic-gate 		if (pid == -1)
2770Sstevel@tonic-gate 			return (EXIT_FAILURE);
2780Sstevel@tonic-gate 		else {
2790Sstevel@tonic-gate 			setupsigs();
2800Sstevel@tonic-gate 			masterpid = pid;
2810Sstevel@tonic-gate 			originalpid = getpid();
2820Sstevel@tonic-gate 			/*
2830Sstevel@tonic-gate 			 * init() was invoked from a console that was not
2840Sstevel@tonic-gate 			 * the default console, nor was it an auxiliary.
2850Sstevel@tonic-gate 			 */
2860Sstevel@tonic-gate 			if (cttyname[0] == NULL)
2870Sstevel@tonic-gate 				termhandler(0);
2880Sstevel@tonic-gate 				/* Never returns */
2890Sstevel@tonic-gate 
2907487Sgww@eng.sun.com 			main_loop(cttyname, B_TRUE);
2910Sstevel@tonic-gate 			/* Never returns */
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 	masterpid = getpid();
2950Sstevel@tonic-gate 	originalpid = getppid();
2960Sstevel@tonic-gate 	pidlist[nchild++] = originalpid;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	sa.sa_handler = childcleanup;
2990Sstevel@tonic-gate 	sa.sa_flags = 0;
3000Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3010Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
3020Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sa, NULL);
3030Sstevel@tonic-gate 	sa.sa_handler = parenthandler;
3040Sstevel@tonic-gate 	sa.sa_flags = SA_SIGINFO;
3050Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3060Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &sa, NULL);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
3090Sstevel@tonic-gate 	sa.sa_flags = 0;
3100Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3110Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &sa, NULL);
3120Sstevel@tonic-gate 	/*
3130Sstevel@tonic-gate 	 * If there isn't a password on root, then don't permit
3140Sstevel@tonic-gate 	 * the fanout capability of sulogin.
3150Sstevel@tonic-gate 	 */
3160Sstevel@tonic-gate 	if (*shpw->sp_pwdp != '\0') {
3170Sstevel@tonic-gate 		ptr = namedlist;
3180Sstevel@tonic-gate 		while (ptr != NULL) {
3190Sstevel@tonic-gate 			p = strchr(ptr, ' ');
3200Sstevel@tonic-gate 			if (p == NULL) {
3217487Sgww@eng.sun.com 				doit(ptr, cttyname);
3220Sstevel@tonic-gate 				break;
3230Sstevel@tonic-gate 			}
3240Sstevel@tonic-gate 			*p++ = '\0';
3257487Sgww@eng.sun.com 			doit(ptr, cttyname);
3260Sstevel@tonic-gate 			ptr = p;
3270Sstevel@tonic-gate 		}
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 	if (pathcmp(cttyname, DEFAULT_CONSOLE) != 0) {
3300Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
3310Sstevel@tonic-gate 			setupsigs();
3327487Sgww@eng.sun.com 			main_loop(DEFAULT_CONSOLE, B_FALSE);
3330Sstevel@tonic-gate 		} else if (pid == -1)
3340Sstevel@tonic-gate 			return (EXIT_FAILURE);
3350Sstevel@tonic-gate 		pidlist[nchild++] = pid;
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 	/*
3380Sstevel@tonic-gate 	 * When parent is all done, it pauses until one of its children
3390Sstevel@tonic-gate 	 * signals that its time to kill the underpriviledged.
3400Sstevel@tonic-gate 	 */
3410Sstevel@tonic-gate 	(void) wait(NULL);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	return (0);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate  * These flags are taken from stty's "sane" table entries in
3480Sstevel@tonic-gate  * usr/src/cmd/ttymon/sttytable.c
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate #define	SET_IFLAG (BRKINT|IGNPAR|ISTRIP|ICRNL|IXON|IMAXBEL)
3510Sstevel@tonic-gate #define	RESET_IFLAG (IGNBRK|PARMRK|INPCK|INLCR|IGNCR|IUCLC|IXOFF|IXANY)
3520Sstevel@tonic-gate #define	SET_OFLAG (OPOST|ONLCR)
3530Sstevel@tonic-gate #define	RESET_OFLAG (OLCUC|OCRNL|ONOCR|ONLRET|OFILL|OFDEL| \
3540Sstevel@tonic-gate 	NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY)
3550Sstevel@tonic-gate #define	SET_LFLAG (ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL)
3560Sstevel@tonic-gate #define	RESET_LFLAG (XCASE|ECHONL|NOFLSH|STFLUSH|STWRAP|STAPPL)
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * Do the equivalent of 'stty sane' on the terminal since we don't know
3600Sstevel@tonic-gate  * what state it was in on startup.
3610Sstevel@tonic-gate  */
3620Sstevel@tonic-gate static void
sanitize_tty(int fd)3630Sstevel@tonic-gate sanitize_tty(int fd)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate 	(void) ioctl(fd, TCGETA, &ttymodes);
3666672Snakanon 	ttymodes.c_iflag &= ~RESET_IFLAG;
3670Sstevel@tonic-gate 	ttymodes.c_iflag |= SET_IFLAG;
3686672Snakanon 	ttymodes.c_oflag &= ~RESET_OFLAG;
3690Sstevel@tonic-gate 	ttymodes.c_oflag |= SET_OFLAG;
3706672Snakanon 	ttymodes.c_lflag &= ~RESET_LFLAG;
3710Sstevel@tonic-gate 	ttymodes.c_lflag |= SET_LFLAG;
3720Sstevel@tonic-gate 	ttymodes.c_cc[VERASE] = CERASE;
3730Sstevel@tonic-gate 	ttymodes.c_cc[VKILL] = CKILL;
3740Sstevel@tonic-gate 	ttymodes.c_cc[VQUIT] = CQUIT;
3750Sstevel@tonic-gate 	ttymodes.c_cc[VINTR] = CINTR;
3760Sstevel@tonic-gate 	ttymodes.c_cc[VEOF] = CEOF;
3770Sstevel@tonic-gate 	ttymodes.c_cc[VEOL] = CNUL;
3780Sstevel@tonic-gate 	(void) ioctl(fd, TCSETAF, &ttymodes);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate /*
3820Sstevel@tonic-gate  * Fork a child of sulogin for each of the auxiliary consoles.
3830Sstevel@tonic-gate  */
3840Sstevel@tonic-gate static void
doit(char * ptr,char * cttyname)3857487Sgww@eng.sun.com doit(char *ptr, char *cttyname)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	pid_t	pid;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (pathcmp(ptr, DEFAULT_CONSOLE) != 0 &&
3900Sstevel@tonic-gate 	    pathcmp(ptr, cttyname) != 0) {
3910Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
3920Sstevel@tonic-gate 			setupsigs();
3937487Sgww@eng.sun.com 			main_loop(ptr, B_FALSE);
3940Sstevel@tonic-gate 		} else if (pid == -1)
3950Sstevel@tonic-gate 			exit(EXIT_FAILURE);
3960Sstevel@tonic-gate 		pidlist[nchild++] = pid;
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate static int
pathcmp(char * adev,char * bdev)4010Sstevel@tonic-gate pathcmp(char *adev, char *bdev)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	struct stat	st1;
4040Sstevel@tonic-gate 	struct stat	st2;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if (adev == NULL || bdev == NULL)
4070Sstevel@tonic-gate 		return (1);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	if (strcmp(adev, bdev) == 0)
4100Sstevel@tonic-gate 		return (0);
4110Sstevel@tonic-gate 
412871Scasper 	if (stat(adev, &st1) || !S_ISCHR(st1.st_mode))
4130Sstevel@tonic-gate 		return (1);
4140Sstevel@tonic-gate 
415871Scasper 	if (stat(bdev, &st2) || !S_ISCHR(st2.st_mode))
4160Sstevel@tonic-gate 		return (1);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if (st1.st_rdev == st2.st_rdev)
4190Sstevel@tonic-gate 		return (0);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	return (1);
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate /* Handlers for the children at initialization */
4250Sstevel@tonic-gate static void
setupsigs()4260Sstevel@tonic-gate setupsigs()
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	sa.sa_handler = noop;
4290Sstevel@tonic-gate 	sa.sa_flags = 0;
4300Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
4310Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, NULL);
4320Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sa, NULL);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	sa.sa_handler = termhandler;
4350Sstevel@tonic-gate 	sa.sa_flags = 0;
4360Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
4370Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
4380Sstevel@tonic-gate 	(void) sigaction(SIGKILL, &sa, NULL);
4390Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sa, NULL);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate static void
main_loop(char * devname,boolean_t cttyflag)4437487Sgww@eng.sun.com main_loop(char *devname, boolean_t cttyflag)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	int		fd, i;
4467487Sgww@eng.sun.com 	char		*user = NULL;		/* authorized user */
4470Sstevel@tonic-gate 	char		*pass;			/* password from user */
4487487Sgww@eng.sun.com 	char		*cpass;			/* crypted password */
4497487Sgww@eng.sun.com 	struct spwd	spwd;
4507487Sgww@eng.sun.com 	struct spwd	*lshpw;			/* local shadow */
4517487Sgww@eng.sun.com 	char		shadow[NSS_BUFLEN_SHADOW];
4520Sstevel@tonic-gate 	FILE		*sysmsgfd;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	for (i = 0; i < 3; i++)
4550Sstevel@tonic-gate 		(void) close(i);
4560Sstevel@tonic-gate 	if (cttyflag == B_FALSE) {
4570Sstevel@tonic-gate 		if (setsid() == -1)
4580Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 	if ((fd = open(devname, O_RDWR)) < 0)
4610Sstevel@tonic-gate 		exit(EXIT_FAILURE);
4627688SAaron.Zang@Sun.COM 
4637688SAaron.Zang@Sun.COM 	/*
4647688SAaron.Zang@Sun.COM 	 * In system maintenance mode, all virtual console instances
4657688SAaron.Zang@Sun.COM 	 * of the svc:/system/console-login service are not available
4667688SAaron.Zang@Sun.COM 	 * any more, and only the system console is available. So here
4677688SAaron.Zang@Sun.COM 	 * we always switch to the system console in case at the moment
4687688SAaron.Zang@Sun.COM 	 * the active console isn't it.
4697688SAaron.Zang@Sun.COM 	 */
4707688SAaron.Zang@Sun.COM 	(void) ioctl(fd, VT_ACTIVATE, 1);
4717688SAaron.Zang@Sun.COM 
4720Sstevel@tonic-gate 	if (fd != 0)
4730Sstevel@tonic-gate 		(void) dup2(fd, STDIN_FILENO);
4740Sstevel@tonic-gate 	if (fd != 1)
4750Sstevel@tonic-gate 		(void) dup2(fd, STDOUT_FILENO);
4760Sstevel@tonic-gate 	if (fd != 2)
4770Sstevel@tonic-gate 		(void) dup2(fd, STDERR_FILENO);
4780Sstevel@tonic-gate 	if (fd > 2)
4790Sstevel@tonic-gate 		(void) close(fd);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	sysmsgfd = fopen("/dev/sysmsg", "w");
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	sanitize_tty(fileno(stdin));
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	for (;;) {
486*10020SJoep.Vesseur@Sun.COM 		do {
487*10020SJoep.Vesseur@Sun.COM 			(void) printf("\nEnter user name for system "
488*10020SJoep.Vesseur@Sun.COM 			    "maintenance (control-d to bypass): ");
489*10020SJoep.Vesseur@Sun.COM 			user = sulogin_getinput(devname, ECHOON);
490*10020SJoep.Vesseur@Sun.COM 			if (user == NULL) {
491*10020SJoep.Vesseur@Sun.COM 				/* signal other children to exit */
492*10020SJoep.Vesseur@Sun.COM 				(void) sigsend(P_PID, masterpid, SIGUSR1);
493*10020SJoep.Vesseur@Sun.COM 				/* ^D, so straight to default init state */
494*10020SJoep.Vesseur@Sun.COM 				exit(EXIT_FAILURE);
495*10020SJoep.Vesseur@Sun.COM 			}
496*10020SJoep.Vesseur@Sun.COM 		} while (user[0] == '\0');
497*10020SJoep.Vesseur@Sun.COM 		(void) printf("Enter %s password (control-d to bypass): ",
498*10020SJoep.Vesseur@Sun.COM 		    user);
4997487Sgww@eng.sun.com 
5007487Sgww@eng.sun.com 		if ((pass = sulogin_getinput(devname, ECHOOFF)) == NULL) {
5017487Sgww@eng.sun.com 			/* signal other children to exit */
5020Sstevel@tonic-gate 			(void) sigsend(P_PID, masterpid, SIGUSR1);
5037487Sgww@eng.sun.com 			/* ^D, so straight to default init state */
5047487Sgww@eng.sun.com 			free(user);
5057487Sgww@eng.sun.com 			exit(EXIT_FAILURE);
5067487Sgww@eng.sun.com 		}
5077487Sgww@eng.sun.com 		lshpw = getspnam_r(user, &spwd, shadow, sizeof (shadow));
5087487Sgww@eng.sun.com 		if (lshpw == NULL) {
5090Sstevel@tonic-gate 			/*
5107487Sgww@eng.sun.com 			 * the user entered doesn't exist, too bad.
5110Sstevel@tonic-gate 			 */
5127487Sgww@eng.sun.com 			goto sorry;
5137487Sgww@eng.sun.com 		}
5147487Sgww@eng.sun.com 
5157487Sgww@eng.sun.com 		/*
5167487Sgww@eng.sun.com 		 * There is a special case error to catch here:
5177487Sgww@eng.sun.com 		 * If the password is hashed with an algorithm
5187487Sgww@eng.sun.com 		 * other than the old unix crypt the call to crypt(3c)
5197487Sgww@eng.sun.com 		 * could fail if /usr is corrupt or not available
5207487Sgww@eng.sun.com 		 * since by default /etc/security/crypt.conf will
5217487Sgww@eng.sun.com 		 * have the crypt_ modules located under /usr/lib.
5227487Sgww@eng.sun.com 		 * Or it could happen if /etc/security/crypt.conf
5237487Sgww@eng.sun.com 		 * is corrupted.
5247487Sgww@eng.sun.com 		 *
5257487Sgww@eng.sun.com 		 * If this happens crypt(3c) will return NULL and
5267487Sgww@eng.sun.com 		 * set errno to ELIBACC for the former condition or
5277487Sgww@eng.sun.com 		 * EINVAL for the latter, in this case we bypass
5287487Sgww@eng.sun.com 		 * authentication and just verify that the user is
5297487Sgww@eng.sun.com 		 * authorized.
5307487Sgww@eng.sun.com 		 */
5317487Sgww@eng.sun.com 
5327487Sgww@eng.sun.com 		errno = 0;
5337487Sgww@eng.sun.com 		cpass = crypt(pass, lshpw->sp_pwdp);
5347487Sgww@eng.sun.com 		if (((cpass == NULL) && (lshpw->sp_pwdp[0] == '$')) &&
5357487Sgww@eng.sun.com 		    ((errno == ELIBACC) || (errno == EINVAL))) {
5367487Sgww@eng.sun.com 			goto checkauth;
5377487Sgww@eng.sun.com 		} else if ((cpass == NULL) ||
5387487Sgww@eng.sun.com 		    (strcmp(cpass, lshpw->sp_pwdp) != 0)) {
5397487Sgww@eng.sun.com 			goto sorry;
5400Sstevel@tonic-gate 		}
5417487Sgww@eng.sun.com 
5427487Sgww@eng.sun.com checkauth:
5437487Sgww@eng.sun.com 		/*
5447487Sgww@eng.sun.com 		 * There is a special case error here as well.
5457487Sgww@eng.sun.com 		 * If /etc/user_attr is corrupt, getusernam("root")
5467487Sgww@eng.sun.com 		 * returns NULL.
5477487Sgww@eng.sun.com 		 * In this case, we just give access because this is similar
5487487Sgww@eng.sun.com 		 * to the case of root not existing in /etc/passwd.
5497487Sgww@eng.sun.com 		 */
5507487Sgww@eng.sun.com 
5517487Sgww@eng.sun.com 		if ((getusernam("root") != NULL) &&
5527487Sgww@eng.sun.com 		    (chkauthattr(MAINTENANCE_AUTH, user) != 1)) {
5537487Sgww@eng.sun.com 			goto sorry;
5547487Sgww@eng.sun.com 		}
5557487Sgww@eng.sun.com 		(void) fprintf(sysmsgfd, "\nsingle-user privilege "
5567487Sgww@eng.sun.com 		    "assigned to %s on %s.\n", user, devname);
5577487Sgww@eng.sun.com 		(void) sigsend(P_PID, masterpid, SIGUSR1);
5587487Sgww@eng.sun.com 		(void) wait(NULL);
5597487Sgww@eng.sun.com 		free(user);
5607487Sgww@eng.sun.com 		free(pass);
5617487Sgww@eng.sun.com 		single(su, devname);
5627487Sgww@eng.sun.com 		/* single never returns */
5637487Sgww@eng.sun.com 
5647487Sgww@eng.sun.com sorry:
5657487Sgww@eng.sun.com 		(void) printf("\nLogin incorrect or user %s not authorized\n",
5667487Sgww@eng.sun.com 		    user);
5677487Sgww@eng.sun.com 		free(user);
5687487Sgww@eng.sun.com 		free(pass);
5690Sstevel@tonic-gate 		(void) sleep(sleeptime);
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate  * single() - exec shell for single user mode
5750Sstevel@tonic-gate  */
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate static void
single(const char * cmd,char * ttyn)5780Sstevel@tonic-gate single(const char *cmd, char *ttyn)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	struct utmpx	*u;
5810Sstevel@tonic-gate 	char		found = B_FALSE;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if (ttyn == NULL)
5840Sstevel@tonic-gate 		ttyn = findttyname(STDIN_FILENO);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	/*
5870Sstevel@tonic-gate 	 * utmpx records on the console device are expected to be "console"
5880Sstevel@tonic-gate 	 * by other processes, such as dtlogin.
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	ttyn = stripttyname(ttyn);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	/* update the utmpx file. */
5930Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
5940Sstevel@tonic-gate 		if (strcmp(u->ut_line, ttyn) == 0) {
5950Sstevel@tonic-gate 			u->ut_tv.tv_sec = time(NULL);
5960Sstevel@tonic-gate 			u->ut_type = USER_PROCESS;
5970Sstevel@tonic-gate 			u->ut_pid = getpid();
5980Sstevel@tonic-gate 			if (strcmp(u->ut_user, "root") != 0)
5990Sstevel@tonic-gate 				(void) strcpy(u->ut_user, "root");
6000Sstevel@tonic-gate 			(void) pututxline(u);
6010Sstevel@tonic-gate 			found = B_TRUE;
6020Sstevel@tonic-gate 			break;
6030Sstevel@tonic-gate 		}
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 	if (!found) {
6060Sstevel@tonic-gate 		struct utmpx entryx;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		entryx.ut_tv.tv_sec = time(NULL);
6090Sstevel@tonic-gate 		entryx.ut_type = USER_PROCESS;
6100Sstevel@tonic-gate 		entryx.ut_pid = getpid();
6110Sstevel@tonic-gate 		(void) strcpy(entryx.ut_user, "root");
6120Sstevel@tonic-gate 		(void) strcpy(entryx.ut_line, ttyn);
6130Sstevel@tonic-gate 		entryx.ut_tv.tv_usec = 0;
6140Sstevel@tonic-gate 		entryx.ut_session = 0;
6150Sstevel@tonic-gate 		entryx.ut_id[0] = 'c';
6160Sstevel@tonic-gate 		entryx.ut_id[1] = 'o';
6170Sstevel@tonic-gate 		entryx.ut_id[2] = 's';
6180Sstevel@tonic-gate 		entryx.ut_id[3] = 'u';
6190Sstevel@tonic-gate 		entryx.ut_syslen = 1;
6200Sstevel@tonic-gate 		entryx.ut_host[0] = '\0';
6210Sstevel@tonic-gate 		entryx.ut_exit.e_termination = WTERMSIG(0);
6220Sstevel@tonic-gate 		entryx.ut_exit.e_exit = WEXITSTATUS(0);
6230Sstevel@tonic-gate 		(void) pututxline(&entryx);
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 	endutxent();
6260Sstevel@tonic-gate 	(void) printf("Entering System Maintenance Mode\n\n");
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (execl(cmd, cmd, "-", (char *)0) < 0)
6290Sstevel@tonic-gate 		exit(EXIT_FAILURE);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate /*
6337487Sgww@eng.sun.com  * sulogin_getinput() - hacked from the standard PAM tty conversation
6347487Sgww@eng.sun.com  *			function getpassphrase() library version
6357487Sgww@eng.sun.com  *			so we can distinguish newline and EOF.
6367487Sgww@eng.sun.com  *		        also don't need this routine to give a prompt.
6370Sstevel@tonic-gate  *
6380Sstevel@tonic-gate  * returns the password string, or NULL if the used typed EOF.
6390Sstevel@tonic-gate  */
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate static char *
sulogin_getinput(char * devname,int echooff)6427487Sgww@eng.sun.com sulogin_getinput(char *devname, int echooff)
6430Sstevel@tonic-gate {
6440Sstevel@tonic-gate 	struct termio	ttyb;
6450Sstevel@tonic-gate 	int		c;
6460Sstevel@tonic-gate 	FILE		*fi;
6477487Sgww@eng.sun.com 	static char	input[PASS_MAX + 1];
6480Sstevel@tonic-gate 	void		(*saved_handler)();
6497487Sgww@eng.sun.com 	char		*rval = input;
6500Sstevel@tonic-gate 	int		i = 0;
6510Sstevel@tonic-gate 
6527487Sgww@eng.sun.com 	if ((fi = fopen(devname, "r")) == NULL) {
6530Sstevel@tonic-gate 		fi = stdin;
6547487Sgww@eng.sun.com 	}
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	saved_handler = signal(SIGINT, SIG_IGN);
6570Sstevel@tonic-gate 
6587487Sgww@eng.sun.com 	if (echooff) {
6597487Sgww@eng.sun.com 		ttyb = ttymodes;
6607487Sgww@eng.sun.com 		ttyb.c_lflag &= ~(ECHO | ECHOE | ECHONL);
6617487Sgww@eng.sun.com 		(void) ioctl(fileno(fi), TCSETAF, &ttyb);
6627487Sgww@eng.sun.com 	}
6630Sstevel@tonic-gate 
6647487Sgww@eng.sun.com 	/* get characters up to PASS_MAX, but don't overflow */
6657487Sgww@eng.sun.com 	while ((c = getc(fi)) != '\n' && (c != '\r')) {
6667487Sgww@eng.sun.com 		if (c == EOF && i == 0) {	/* ^D, no input */
6670Sstevel@tonic-gate 			rval = NULL;
6680Sstevel@tonic-gate 			break;
6690Sstevel@tonic-gate 		}
6707487Sgww@eng.sun.com 		if (i < PASS_MAX) {
6717487Sgww@eng.sun.com 			input[i++] = (char)c;
6727487Sgww@eng.sun.com 		}
6730Sstevel@tonic-gate 	}
6747487Sgww@eng.sun.com 	input[i] = '\0';
6750Sstevel@tonic-gate 	(void) fputc('\n', fi);
6767487Sgww@eng.sun.com 	if (echooff) {
6777487Sgww@eng.sun.com 		(void) ioctl(fileno(fi), TCSETAW, &ttymodes);
6787487Sgww@eng.sun.com 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	if (saved_handler != SIG_ERR)
6810Sstevel@tonic-gate 		(void) signal(SIGINT, saved_handler);
6827487Sgww@eng.sun.com 	return (rval == NULL ? NULL : strdup(rval));
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate static char *
findttyname(int fd)6860Sstevel@tonic-gate findttyname(int fd)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate 	char	*ttyn = ttyname(fd);
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	if (ttyn == NULL)
6910Sstevel@tonic-gate 		ttyn = "/dev/???";
6920Sstevel@tonic-gate 	else {
6930Sstevel@tonic-gate 		/*
6940Sstevel@tonic-gate 		 * /dev/syscon and /dev/systty are usually links to
6950Sstevel@tonic-gate 		 * /dev/console.  prefer /dev/console.
6960Sstevel@tonic-gate 		 */
6970Sstevel@tonic-gate 		if (((strcmp(ttyn, "/dev/syscon") == 0) ||
6980Sstevel@tonic-gate 		    (strcmp(ttyn, "/dev/systty") == 0)) &&
6990Sstevel@tonic-gate 		    access("/dev/console", F_OK))
7000Sstevel@tonic-gate 			ttyn = "/dev/console";
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 	return (ttyn);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate static char *
stripttyname(char * ttyn)7060Sstevel@tonic-gate stripttyname(char *ttyn)
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	/* saw off the /dev/ */
7090Sstevel@tonic-gate 	if (strncmp(ttyn, "/dev/", sizeof ("/dev/") -1) == 0)
7100Sstevel@tonic-gate 		return (ttyn + sizeof ("/dev/") - 1);
7110Sstevel@tonic-gate 	else
7120Sstevel@tonic-gate 		return (ttyn);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate /* ARGSUSED */
7170Sstevel@tonic-gate static	void
noop(int sig)7180Sstevel@tonic-gate noop(int sig)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate 	/*
7210Sstevel@tonic-gate 	 * This signal handler does nothing except return.  We use it
7220Sstevel@tonic-gate 	 * as the signal disposition in this program instead of
7230Sstevel@tonic-gate 	 * SIG_IGN so that we do not have to restore the disposition
7240Sstevel@tonic-gate 	 * back to SIG_DFL. Instead we allow exec(2) to set the
7250Sstevel@tonic-gate 	 * dispostion to SIG_DFL to avoid a race condition.
7260Sstevel@tonic-gate 	 */
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /* ARGSUSED */
7300Sstevel@tonic-gate static void
parenthandler(int sig,siginfo_t * si,ucontext_t * uc)7310Sstevel@tonic-gate parenthandler(int sig, siginfo_t *si, ucontext_t *uc)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	int i;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	/*
7360Sstevel@tonic-gate 	 * We get here if someone has successfully entered a password
7370Sstevel@tonic-gate 	 * from the auxiliary console and is getting the single-user shell.
7380Sstevel@tonic-gate 	 * When this happens, the parent needs to kill the children
7390Sstevel@tonic-gate 	 * that didn't get the shell.
7400Sstevel@tonic-gate 	 *
7410Sstevel@tonic-gate 	 */
7420Sstevel@tonic-gate 	for (i = 0; i < nchild; i++) {
7430Sstevel@tonic-gate 		if (pidlist[i] != si->__data.__proc.__pid)
7440Sstevel@tonic-gate 			(void) sigsend(P_PID, pidlist[i], SIGTERM);
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
7470Sstevel@tonic-gate 	sa.sa_flags = 0;
7480Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
7490Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, NULL);
7500Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sa, NULL);
7510Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
7520Sstevel@tonic-gate 	(void) wait(NULL);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate  * The master pid will get SIGTERM or SIGHUP from init, and then
7570Sstevel@tonic-gate  * has to make sure the shell isn't still running.
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate /* ARGSUSED */
7610Sstevel@tonic-gate static	void
childcleanup(int sig)7620Sstevel@tonic-gate childcleanup(int sig)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate 	int i;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	/* Only need to kill the child that became the shell. */
7670Sstevel@tonic-gate 	for (i = 0; i < nchild; i++) {
7680Sstevel@tonic-gate 		/* Don't kill gramps before his time */
7690Sstevel@tonic-gate 		if (pidlist[i] != getppid())
7700Sstevel@tonic-gate 			(void) sigsend(P_PID, pidlist[i], SIGHUP);
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate /* ARGSUSED */
7750Sstevel@tonic-gate static	void
termhandler(int sig)7760Sstevel@tonic-gate termhandler(int sig)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	FILE *fi;
7790Sstevel@tonic-gate 	pid_t pid;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	/* Processes come here when they fail to receive the password. */
7820Sstevel@tonic-gate 	if ((fi = fopen("/dev/tty", "r+")) == NULL)
7830Sstevel@tonic-gate 		fi = stdin;
7840Sstevel@tonic-gate 	else
7850Sstevel@tonic-gate 		setbuf(fi, NULL);
7860Sstevel@tonic-gate 	sanitize_tty(fileno(fi));
7870Sstevel@tonic-gate 	/* If you're the controlling tty, then just wait */
7880Sstevel@tonic-gate 	pid = getpid();
7890Sstevel@tonic-gate 	if (pid == originalpid || pid == masterpid) {
7900Sstevel@tonic-gate 		sa.sa_handler = SIG_IGN;
7910Sstevel@tonic-gate 		sa.sa_flags = 0;
7920Sstevel@tonic-gate 		(void) sigemptyset(&sa.sa_mask);
7930Sstevel@tonic-gate 		(void) sigaction(SIGINT, &sa, NULL);
7940Sstevel@tonic-gate 		(void) sigaction(SIGQUIT, &sa, NULL);
7950Sstevel@tonic-gate 		sa.sa_handler = SIG_DFL;
7960Sstevel@tonic-gate 		sa.sa_flags = 0;
7970Sstevel@tonic-gate 		(void) sigemptyset(&sa.sa_mask);
7980Sstevel@tonic-gate 		(void) sigaction(SIGTERM, &sa, NULL);
7990Sstevel@tonic-gate 		(void) sigaction(SIGHUP, &sa, NULL);
8000Sstevel@tonic-gate 		(void) wait(NULL);
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 	exit(0);
8030Sstevel@tonic-gate }
804