xref: /titanic_54/usr/src/cmd/sulogin/sulogin.c (revision 3289e1bdec732dbdf04517907516fd0d00f6a6f8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3289e1bdSnakanon  * Common Development and Distribution License (the "License").
6*3289e1bdSnakanon  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*3289e1bdSnakanon  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
287c478bd9Sstevel@tonic-gate  *	All rights reserved.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  *	Copyright (c) 1987, 1988 Microsoft Corporation.
317c478bd9Sstevel@tonic-gate  *	All rights reserved.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  *	sulogin - special login program exec'd from init to let user
387c478bd9Sstevel@tonic-gate  *	come up single user, or go to default init state straight away.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  *	Explain the scoop to the user, and prompt for root password or
417c478bd9Sstevel@tonic-gate  *	^D. Good root password gets you single user, ^D exits sulogin,
427c478bd9Sstevel@tonic-gate  *	and init will go to default init state.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  *	If /etc/passwd is missing, or there's no entry for root,
457c478bd9Sstevel@tonic-gate  *	go single user, no questions asked.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <sys/stat.h>
507c478bd9Sstevel@tonic-gate #include <sys/param.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysmsg_impl.h>
527c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
537c478bd9Sstevel@tonic-gate #include <sys/resource.h>
547c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
557c478bd9Sstevel@tonic-gate #include <sys/wait.h>
567c478bd9Sstevel@tonic-gate #include <sys/stermio.h>
577c478bd9Sstevel@tonic-gate #include <fcntl.h>
587c478bd9Sstevel@tonic-gate #include <termio.h>
597c478bd9Sstevel@tonic-gate #include <pwd.h>
607c478bd9Sstevel@tonic-gate #include <shadow.h>
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate #include <stdio.h>
637c478bd9Sstevel@tonic-gate #include <signal.h>
647c478bd9Sstevel@tonic-gate #include <siginfo.h>
657c478bd9Sstevel@tonic-gate #include <utmpx.h>
667c478bd9Sstevel@tonic-gate #include <unistd.h>
677c478bd9Sstevel@tonic-gate #include <ucontext.h>
687c478bd9Sstevel@tonic-gate #include <string.h>
697c478bd9Sstevel@tonic-gate #include <strings.h>
707c478bd9Sstevel@tonic-gate #include <deflt.h>
717c478bd9Sstevel@tonic-gate #include <limits.h>
727c478bd9Sstevel@tonic-gate #include <errno.h>
737c478bd9Sstevel@tonic-gate #include <crypt.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Intervals to sleep after failed login
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate #ifndef SLEEPTIME
797c478bd9Sstevel@tonic-gate #define	SLEEPTIME	4	/* sleeptime before login incorrect msg */
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	SLEEPTIME_MAX	5	/* maximum sleeptime */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  *	the name of the file containing the login defaults we deliberately
867c478bd9Sstevel@tonic-gate  *	use the same file as login(1)
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #define	DEFAULT_LOGIN	"/etc/default/login"
907c478bd9Sstevel@tonic-gate #define	DEFAULT_SULOGIN	"/etc/default/sulogin"
917c478bd9Sstevel@tonic-gate #define	DEFAULT_CONSOLE	"/dev/console"
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static char	shell[]	= "/sbin/sh";
947c478bd9Sstevel@tonic-gate static char	su[]	= "/sbin/su.static";
957c478bd9Sstevel@tonic-gate static int	sleeptime	= SLEEPTIME;
967c478bd9Sstevel@tonic-gate static int	nchild = 0;
977c478bd9Sstevel@tonic-gate static pid_t	pidlist[10];
987c478bd9Sstevel@tonic-gate static pid_t	masterpid = 0;
997c478bd9Sstevel@tonic-gate static pid_t	originalpid = 0;
1007c478bd9Sstevel@tonic-gate static struct sigaction	sa;
1017c478bd9Sstevel@tonic-gate static struct termio	ttymodes;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static char	*findttyname(int fd);
1047c478bd9Sstevel@tonic-gate static char	*stripttyname(char *);
1057c478bd9Sstevel@tonic-gate static char	*sulogin_getpass(char *);
1067c478bd9Sstevel@tonic-gate static void	noop(int);
1077c478bd9Sstevel@tonic-gate static void	single(const char *, char *);
1087c478bd9Sstevel@tonic-gate static void	main_loop(char *, struct spwd *, boolean_t);
1097c478bd9Sstevel@tonic-gate static void	parenthandler();
1107c478bd9Sstevel@tonic-gate static void	termhandler(int);
1117c478bd9Sstevel@tonic-gate static void	setupsigs(void);
1127c478bd9Sstevel@tonic-gate static int	pathcmp(char *, char *);
1137c478bd9Sstevel@tonic-gate static void	doit(char *, char *, struct spwd *);
1147c478bd9Sstevel@tonic-gate static void	childcleanup(int);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /* ARGSUSED */
1177c478bd9Sstevel@tonic-gate int
1187c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	struct spwd	*shpw;
1217c478bd9Sstevel@tonic-gate 	int		passreq = B_TRUE;
1227c478bd9Sstevel@tonic-gate 	int		flags;
1237c478bd9Sstevel@tonic-gate 	int		fd;
1247c478bd9Sstevel@tonic-gate 	char		*infop, *ptr, *p;
1257c478bd9Sstevel@tonic-gate 	pid_t		pid;
1267c478bd9Sstevel@tonic-gate 	int		bufsize;
1277c478bd9Sstevel@tonic-gate 	struct stat	st;
1287c478bd9Sstevel@tonic-gate 	char		cttyname[100];
1297c478bd9Sstevel@tonic-gate 	char		namedlist[500];
1307c478bd9Sstevel@tonic-gate 	char		scratchlist[500];
1317c478bd9Sstevel@tonic-gate 	dev_t		cttyd;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
1347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: must be root\n", argv[0]);
1357c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* Do the magic to determine the children */
1397c478bd9Sstevel@tonic-gate 	if ((fd = open(SYSMSG, 0)) < 0)
1407c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/*
1437c478bd9Sstevel@tonic-gate 	 * If the console supports the CIOCTTYCONSOLE ioctl, then fetch
1447c478bd9Sstevel@tonic-gate 	 * its console device list.  If not, then we use the default
1457c478bd9Sstevel@tonic-gate 	 * console name.
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CIOCTTYCONSOLE, &cttyd) == 0) {
1487c478bd9Sstevel@tonic-gate 		if ((bufsize = ioctl(fd, CIOCGETCONSOLE, NULL)) < 0)
1497c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		if (bufsize > 0) {
1527c478bd9Sstevel@tonic-gate 			if ((infop = calloc(bufsize, sizeof (char))) == NULL)
1537c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 			if (ioctl(fd, CIOCGETCONSOLE, infop) < 0)
1567c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 			(void) snprintf(namedlist, sizeof (namedlist), "%s %s",
1597c478bd9Sstevel@tonic-gate 			    DEFAULT_CONSOLE, infop);
1607c478bd9Sstevel@tonic-gate 		} else
1617c478bd9Sstevel@tonic-gate 			(void) snprintf(namedlist, sizeof (namedlist), "%s",
1627c478bd9Sstevel@tonic-gate 			    DEFAULT_CONSOLE);
1637c478bd9Sstevel@tonic-gate 	} else {
1647c478bd9Sstevel@tonic-gate 		(void) snprintf(namedlist, sizeof (namedlist), "%s",
1657c478bd9Sstevel@tonic-gate 		    DEFAULT_CONSOLE);
1667c478bd9Sstevel@tonic-gate 		cttyd = NODEV;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * The attempt to turn the controlling terminals dev_t into a string
1717c478bd9Sstevel@tonic-gate 	 * may not be successful, thus leaving the variable cttyname as a
1727c478bd9Sstevel@tonic-gate 	 * NULL.  This occurs if during boot we find
1737c478bd9Sstevel@tonic-gate 	 * the root partition (or some other partition)
1747c478bd9Sstevel@tonic-gate 	 * requires manual fsck, thus resulting in sulogin
1757c478bd9Sstevel@tonic-gate 	 * getting invoked.  The ioctl for CIOCTTYCONSOLE
1767c478bd9Sstevel@tonic-gate 	 * called above returned NODEV for cttyd
1777c478bd9Sstevel@tonic-gate 	 * in these cases.  NODEV gets returned when the vnode pointer
1787c478bd9Sstevel@tonic-gate 	 * in our session structure is NULL.  In these cases it
1797c478bd9Sstevel@tonic-gate 	 * must be assumed that the default console is used.
1807c478bd9Sstevel@tonic-gate 	 *
1817c478bd9Sstevel@tonic-gate 	 * See uts/common/os/session.c:cttydev().
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 	(void) strcpy(cttyname, DEFAULT_CONSOLE);
1847c478bd9Sstevel@tonic-gate 	(void) strcpy(scratchlist, namedlist);
1857c478bd9Sstevel@tonic-gate 	ptr = scratchlist;
1867c478bd9Sstevel@tonic-gate 	while (ptr != NULL) {
1877c478bd9Sstevel@tonic-gate 		p = strchr(ptr, ' ');
1887c478bd9Sstevel@tonic-gate 		if (p == NULL) {
1897c478bd9Sstevel@tonic-gate 			if (stat(ptr, &st))
1907c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1917c478bd9Sstevel@tonic-gate 			if (st.st_rdev == cttyd)
1927c478bd9Sstevel@tonic-gate 				(void) strcpy(cttyname, ptr);
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 		*p++ = '\0';
1967c478bd9Sstevel@tonic-gate 		if (stat(ptr, &st))
1977c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
1987c478bd9Sstevel@tonic-gate 		if (st.st_rdev == cttyd) {
1997c478bd9Sstevel@tonic-gate 			(void) strcpy(cttyname, ptr);
2007c478bd9Sstevel@tonic-gate 			break;
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 		ptr = p;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * Use the same value of SLEEPTIME that login(1) uses.  This
2077c478bd9Sstevel@tonic-gate 	 * is obtained by reading the file /etc/default/login using
2087c478bd9Sstevel@tonic-gate 	 * the def*() functions.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (defopen(DEFAULT_LOGIN) == 0) {
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		/* ignore case */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		flags = defcntl(DC_GETFLAGS, 0);
2167c478bd9Sstevel@tonic-gate 		TURNOFF(flags, DC_CASE);
2177c478bd9Sstevel@tonic-gate 		(void) defcntl(DC_SETFLAGS, flags);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		if ((ptr = defread("SLEEPTIME=")) != NULL)
2207c478bd9Sstevel@tonic-gate 			sleeptime = atoi(ptr);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		if (sleeptime < 0 || sleeptime > SLEEPTIME_MAX)
2237c478bd9Sstevel@tonic-gate 			sleeptime = SLEEPTIME;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);	/* closes DEFAULT_LOGIN */
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * Use our own value of PASSREQ, separate from the one login(1) uses.
2307c478bd9Sstevel@tonic-gate 	 * This is obtained by reading the file /etc/default/sulogin using
2317c478bd9Sstevel@tonic-gate 	 * the def*() functions.
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (defopen(DEFAULT_SULOGIN) == 0) {
2357c478bd9Sstevel@tonic-gate 		if ((ptr = defread("PASSREQ=")) != NULL)
2367c478bd9Sstevel@tonic-gate 			if (strcmp("NO", ptr) == 0)
2377c478bd9Sstevel@tonic-gate 				passreq = B_FALSE;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);	/* closes DEFAULT_SULOGIN */
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (passreq == B_FALSE)
2437c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * if no 'root' entry in /etc/shadow, give maint. mode single
2477c478bd9Sstevel@tonic-gate 	 * user shell prompt
2487c478bd9Sstevel@tonic-gate 	 */
2497c478bd9Sstevel@tonic-gate 	setspent();
2507c478bd9Sstevel@tonic-gate 	if ((shpw = getspnam("root")) == NULL) {
2517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
2527c478bd9Sstevel@tonic-gate 		    "in shadow password file ***\n\n");
2537c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	endspent();
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * if no 'root' entry in /etc/passwd, give maint. mode single
2587c478bd9Sstevel@tonic-gate 	 * user shell prompt
2597c478bd9Sstevel@tonic-gate 	 */
2607c478bd9Sstevel@tonic-gate 	setpwent();
2617c478bd9Sstevel@tonic-gate 	if (getpwnam("root") == NULL) {
2627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
2637c478bd9Sstevel@tonic-gate 		    "in password file ***\n\n");
2647c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	endpwent();
2677c478bd9Sstevel@tonic-gate 	/* process with controlling tty treated special */
2687c478bd9Sstevel@tonic-gate 	if ((pid = fork()) != (pid_t)0) {
2697c478bd9Sstevel@tonic-gate 		if (pid == -1)
2707c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
2717c478bd9Sstevel@tonic-gate 		else {
2727c478bd9Sstevel@tonic-gate 			setupsigs();
2737c478bd9Sstevel@tonic-gate 			masterpid = pid;
2747c478bd9Sstevel@tonic-gate 			originalpid = getpid();
2757c478bd9Sstevel@tonic-gate 			/*
2767c478bd9Sstevel@tonic-gate 			 * init() was invoked from a console that was not
2777c478bd9Sstevel@tonic-gate 			 * the default console, nor was it an auxiliary.
2787c478bd9Sstevel@tonic-gate 			 */
2797c478bd9Sstevel@tonic-gate 			if (cttyname[0] == NULL)
2807c478bd9Sstevel@tonic-gate 				termhandler(0);
2817c478bd9Sstevel@tonic-gate 				/* Never returns */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 			main_loop(cttyname, shpw, B_TRUE);
2847c478bd9Sstevel@tonic-gate 			/* Never returns */
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 	masterpid = getpid();
2887c478bd9Sstevel@tonic-gate 	originalpid = getppid();
2897c478bd9Sstevel@tonic-gate 	pidlist[nchild++] = originalpid;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	sa.sa_handler = childcleanup;
2927c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
2937c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
2947c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
2957c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sa, NULL);
2967c478bd9Sstevel@tonic-gate 	sa.sa_handler = parenthandler;
2977c478bd9Sstevel@tonic-gate 	sa.sa_flags = SA_SIGINFO;
2987c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
2997c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &sa, NULL);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
3027c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
3037c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3047c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &sa, NULL);
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * If there isn't a password on root, then don't permit
3077c478bd9Sstevel@tonic-gate 	 * the fanout capability of sulogin.
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	if (*shpw->sp_pwdp != '\0') {
3107c478bd9Sstevel@tonic-gate 		ptr = namedlist;
3117c478bd9Sstevel@tonic-gate 		while (ptr != NULL) {
3127c478bd9Sstevel@tonic-gate 			p = strchr(ptr, ' ');
3137c478bd9Sstevel@tonic-gate 			if (p == NULL) {
3147c478bd9Sstevel@tonic-gate 				doit(ptr, cttyname, shpw);
3157c478bd9Sstevel@tonic-gate 				break;
3167c478bd9Sstevel@tonic-gate 			}
3177c478bd9Sstevel@tonic-gate 			*p++ = '\0';
3187c478bd9Sstevel@tonic-gate 			doit(ptr, cttyname, shpw);
3197c478bd9Sstevel@tonic-gate 			ptr = p;
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 	if (pathcmp(cttyname, DEFAULT_CONSOLE) != 0) {
3237c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
3247c478bd9Sstevel@tonic-gate 			setupsigs();
3257c478bd9Sstevel@tonic-gate 			main_loop(DEFAULT_CONSOLE, shpw, B_FALSE);
3267c478bd9Sstevel@tonic-gate 		} else if (pid == -1)
3277c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
3287c478bd9Sstevel@tonic-gate 		pidlist[nchild++] = pid;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * When parent is all done, it pauses until one of its children
3327c478bd9Sstevel@tonic-gate 	 * signals that its time to kill the underpriviledged.
3337c478bd9Sstevel@tonic-gate 	 */
3347c478bd9Sstevel@tonic-gate 	(void) wait(NULL);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	return (0);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * These flags are taken from stty's "sane" table entries in
3417c478bd9Sstevel@tonic-gate  * usr/src/cmd/ttymon/sttytable.c
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate #define	SET_IFLAG (BRKINT|IGNPAR|ISTRIP|ICRNL|IXON|IMAXBEL)
3447c478bd9Sstevel@tonic-gate #define	RESET_IFLAG (IGNBRK|PARMRK|INPCK|INLCR|IGNCR|IUCLC|IXOFF|IXANY)
3457c478bd9Sstevel@tonic-gate #define	SET_OFLAG (OPOST|ONLCR)
3467c478bd9Sstevel@tonic-gate #define	RESET_OFLAG (OLCUC|OCRNL|ONOCR|ONLRET|OFILL|OFDEL| \
3477c478bd9Sstevel@tonic-gate 	NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY)
3487c478bd9Sstevel@tonic-gate #define	SET_LFLAG (ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL)
3497c478bd9Sstevel@tonic-gate #define	RESET_LFLAG (XCASE|ECHONL|NOFLSH|STFLUSH|STWRAP|STAPPL)
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate  * Do the equivalent of 'stty sane' on the terminal since we don't know
3537c478bd9Sstevel@tonic-gate  * what state it was in on startup.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate static void
3567c478bd9Sstevel@tonic-gate sanitize_tty(int fd)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	(void) ioctl(fd, TCGETA, &ttymodes);
3597c478bd9Sstevel@tonic-gate 	ttymodes.c_iflag &= ~RESET_IFLAG;
360*3289e1bdSnakanon 	ttymodes.c_iflag |= SET_IFLAG;
3617c478bd9Sstevel@tonic-gate 	ttymodes.c_oflag &= ~RESET_OFLAG;
362*3289e1bdSnakanon 	ttymodes.c_oflag |= SET_OFLAG;
3637c478bd9Sstevel@tonic-gate 	ttymodes.c_lflag &= ~RESET_LFLAG;
364*3289e1bdSnakanon 	ttymodes.c_lflag |= SET_LFLAG;
3657c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VERASE] = CERASE;
3667c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VKILL] = CKILL;
3677c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VQUIT] = CQUIT;
3687c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VINTR] = CINTR;
3697c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VEOF] = CEOF;
3707c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VEOL] = CNUL;
3717c478bd9Sstevel@tonic-gate 	(void) ioctl(fd, TCSETAF, &ttymodes);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * Fork a child of sulogin for each of the auxiliary consoles.
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate static void
3787c478bd9Sstevel@tonic-gate doit(char *ptr, char *cttyname, struct spwd *shpw)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	pid_t	pid;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (pathcmp(ptr, DEFAULT_CONSOLE) != 0 &&
3837c478bd9Sstevel@tonic-gate 	    pathcmp(ptr, cttyname) != 0) {
3847c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
3857c478bd9Sstevel@tonic-gate 			setupsigs();
3867c478bd9Sstevel@tonic-gate 			main_loop(ptr, shpw, B_FALSE);
3877c478bd9Sstevel@tonic-gate 		} else if (pid == -1)
3887c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
3897c478bd9Sstevel@tonic-gate 		pidlist[nchild++] = pid;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate static int
3947c478bd9Sstevel@tonic-gate pathcmp(char *adev, char *bdev)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	struct stat	st1;
3977c478bd9Sstevel@tonic-gate 	struct stat	st2;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	if (adev == NULL || bdev == NULL)
4007c478bd9Sstevel@tonic-gate 		return (1);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (strcmp(adev, bdev) == 0)
4037c478bd9Sstevel@tonic-gate 		return (0);
4047c478bd9Sstevel@tonic-gate 
4054bc0a2efScasper 	if (stat(adev, &st1) || !S_ISCHR(st1.st_mode))
4067c478bd9Sstevel@tonic-gate 		return (1);
4077c478bd9Sstevel@tonic-gate 
4084bc0a2efScasper 	if (stat(bdev, &st2) || !S_ISCHR(st2.st_mode))
4097c478bd9Sstevel@tonic-gate 		return (1);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (st1.st_rdev == st2.st_rdev)
4127c478bd9Sstevel@tonic-gate 		return (0);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	return (1);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate /* Handlers for the children at initialization */
4187c478bd9Sstevel@tonic-gate static void
4197c478bd9Sstevel@tonic-gate setupsigs()
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	sa.sa_handler = noop;
4227c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
4237c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
4247c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, NULL);
4257c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sa, NULL);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	sa.sa_handler = termhandler;
4287c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
4297c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
4307c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
4317c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGKILL, &sa, NULL);
4327c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sa, NULL);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate static void
4367c478bd9Sstevel@tonic-gate main_loop(char *devname, struct spwd *shpw, boolean_t cttyflag)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	int		fd, i;
4397c478bd9Sstevel@tonic-gate 	char		*pass;			/* password from user */
4407c478bd9Sstevel@tonic-gate 	FILE		*sysmsgfd;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++)
4437c478bd9Sstevel@tonic-gate 		(void) close(i);
4447c478bd9Sstevel@tonic-gate 	if (cttyflag == B_FALSE) {
4457c478bd9Sstevel@tonic-gate 		if (setsid() == -1)
4467c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	if ((fd = open(devname, O_RDWR)) < 0)
4497c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
4507c478bd9Sstevel@tonic-gate 	if (fd != 0)
4517c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDIN_FILENO);
4527c478bd9Sstevel@tonic-gate 	if (fd != 1)
4537c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDOUT_FILENO);
4547c478bd9Sstevel@tonic-gate 	if (fd != 2)
4557c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDERR_FILENO);
4567c478bd9Sstevel@tonic-gate 	if (fd > 2)
4577c478bd9Sstevel@tonic-gate 		(void) close(fd);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	sysmsgfd = fopen("/dev/sysmsg", "w");
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	sanitize_tty(fileno(stdin));
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	for (;;) {
4647c478bd9Sstevel@tonic-gate 		(void) fputs("\nRoot password for system maintenance "
4657c478bd9Sstevel@tonic-gate 		    "(control-d to bypass): ", stdout);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		if ((pass = sulogin_getpass(devname)) == NULL) {
4687c478bd9Sstevel@tonic-gate 			/* signal other children to exit */
4697c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, masterpid, SIGUSR1);
4707c478bd9Sstevel@tonic-gate 			/* ^D, so straight to default init state */
4717c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4727c478bd9Sstevel@tonic-gate 		}
4737c478bd9Sstevel@tonic-gate 		if (*shpw->sp_pwdp == '\0' && *pass == '\0') {
4747c478bd9Sstevel@tonic-gate 			(void) fprintf(sysmsgfd,
4757c478bd9Sstevel@tonic-gate 			    "\nsingle-user privilege assigned to %s.\n",
4767c478bd9Sstevel@tonic-gate 			    devname);
4777c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, masterpid, SIGUSR1);
4787c478bd9Sstevel@tonic-gate 			(void) wait(NULL);
4797c478bd9Sstevel@tonic-gate 			single(su, devname);
4807c478bd9Sstevel@tonic-gate 		} else if (*shpw->sp_pwdp != '\0') {
4817c478bd9Sstevel@tonic-gate 			/*
4827c478bd9Sstevel@tonic-gate 			 * There is a special case error to catch here,
4837c478bd9Sstevel@tonic-gate 			 * because sulogin is statically linked:
4847c478bd9Sstevel@tonic-gate 			 * If the root password is hashed with an algorithm
4857c478bd9Sstevel@tonic-gate 			 * other than the old unix crypt the call to crypt(3c)
4867c478bd9Sstevel@tonic-gate 			 * could fail if /usr is corrupt or not available
4877c478bd9Sstevel@tonic-gate 			 * since by default /etc/security/crypt.conf will
4887c478bd9Sstevel@tonic-gate 			 * have the crypt_ modules located under /usr/lib.
4897c478bd9Sstevel@tonic-gate 			 *
4907c478bd9Sstevel@tonic-gate 			 * If this happens crypt(3c) will return NULL and
4917c478bd9Sstevel@tonic-gate 			 * set errno to ELIBACC, in this case we just give
4927c478bd9Sstevel@tonic-gate 			 * access because this is similar to the case of
4937c478bd9Sstevel@tonic-gate 			 * root not existing in /etc/passwd.
4947c478bd9Sstevel@tonic-gate 			 */
4957c478bd9Sstevel@tonic-gate 			pass = crypt(pass, shpw->sp_pwdp);
4967c478bd9Sstevel@tonic-gate 			if ((strcmp(pass, shpw->sp_pwdp) == 0) ||
4977c478bd9Sstevel@tonic-gate 			    ((pass == NULL) && (errno == ELIBACC) &&
4987c478bd9Sstevel@tonic-gate 			    (shpw->sp_pwdp[0] == '$'))) {
4997c478bd9Sstevel@tonic-gate 				(void) fprintf(sysmsgfd,
5007c478bd9Sstevel@tonic-gate 			    "\nsingle-user privilege assigned to %s.\n",
5017c478bd9Sstevel@tonic-gate 				    devname);
5027c478bd9Sstevel@tonic-gate 				(void) sigsend(P_PID, masterpid, SIGUSR1);
5037c478bd9Sstevel@tonic-gate 				(void) wait(NULL);
5047c478bd9Sstevel@tonic-gate 				single(su, devname);
5057c478bd9Sstevel@tonic-gate 			}
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 		(void) sleep(sleeptime);
5087c478bd9Sstevel@tonic-gate 		(void) printf("Login incorrect\n");
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate  * single() - exec shell for single user mode
5147c478bd9Sstevel@tonic-gate  */
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate static void
5177c478bd9Sstevel@tonic-gate single(const char *cmd, char *ttyn)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	struct utmpx	*u;
5207c478bd9Sstevel@tonic-gate 	char		found = B_FALSE;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (ttyn == NULL)
5237c478bd9Sstevel@tonic-gate 		ttyn = findttyname(STDIN_FILENO);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/*
5267c478bd9Sstevel@tonic-gate 	 * utmpx records on the console device are expected to be "console"
5277c478bd9Sstevel@tonic-gate 	 * by other processes, such as dtlogin.
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate 	ttyn = stripttyname(ttyn);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/* update the utmpx file. */
5327c478bd9Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
5337c478bd9Sstevel@tonic-gate 		if (strcmp(u->ut_line, ttyn) == 0) {
5347c478bd9Sstevel@tonic-gate 			u->ut_tv.tv_sec = time(NULL);
5357c478bd9Sstevel@tonic-gate 			u->ut_type = USER_PROCESS;
5367c478bd9Sstevel@tonic-gate 			u->ut_pid = getpid();
5377c478bd9Sstevel@tonic-gate 			if (strcmp(u->ut_user, "root") != 0)
5387c478bd9Sstevel@tonic-gate 				(void) strcpy(u->ut_user, "root");
5397c478bd9Sstevel@tonic-gate 			(void) pututxline(u);
5407c478bd9Sstevel@tonic-gate 			found = B_TRUE;
5417c478bd9Sstevel@tonic-gate 			break;
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	if (!found) {
5457c478bd9Sstevel@tonic-gate 		struct utmpx entryx;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		entryx.ut_tv.tv_sec = time(NULL);
5487c478bd9Sstevel@tonic-gate 		entryx.ut_type = USER_PROCESS;
5497c478bd9Sstevel@tonic-gate 		entryx.ut_pid = getpid();
5507c478bd9Sstevel@tonic-gate 		(void) strcpy(entryx.ut_user, "root");
5517c478bd9Sstevel@tonic-gate 		(void) strcpy(entryx.ut_line, ttyn);
5527c478bd9Sstevel@tonic-gate 		entryx.ut_tv.tv_usec = 0;
5537c478bd9Sstevel@tonic-gate 		entryx.ut_session = 0;
5547c478bd9Sstevel@tonic-gate 		entryx.ut_id[0] = 'c';
5557c478bd9Sstevel@tonic-gate 		entryx.ut_id[1] = 'o';
5567c478bd9Sstevel@tonic-gate 		entryx.ut_id[2] = 's';
5577c478bd9Sstevel@tonic-gate 		entryx.ut_id[3] = 'u';
5587c478bd9Sstevel@tonic-gate 		entryx.ut_syslen = 1;
5597c478bd9Sstevel@tonic-gate 		entryx.ut_host[0] = '\0';
5607c478bd9Sstevel@tonic-gate 		entryx.ut_exit.e_termination = WTERMSIG(0);
5617c478bd9Sstevel@tonic-gate 		entryx.ut_exit.e_exit = WEXITSTATUS(0);
5627c478bd9Sstevel@tonic-gate 		(void) pututxline(&entryx);
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 	endutxent();
5657c478bd9Sstevel@tonic-gate 	(void) printf("Entering System Maintenance Mode\n\n");
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if (execl(cmd, cmd, "-", (char *)0) < 0)
5687c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate  * sulogin_getpass() - hacked from the stdio library version so we can
5737c478bd9Sstevel@tonic-gate  *		       distinguish newline and EOF.  also don't need this
5747c478bd9Sstevel@tonic-gate  *		       routine to give a prompt.
5757c478bd9Sstevel@tonic-gate  *
5767c478bd9Sstevel@tonic-gate  * returns the password string, or NULL if the used typed EOF.
5777c478bd9Sstevel@tonic-gate  */
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate static char *
5807c478bd9Sstevel@tonic-gate sulogin_getpass(char *devname)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	struct termio	ttyb;
5837c478bd9Sstevel@tonic-gate 	int		c;
5847c478bd9Sstevel@tonic-gate 	FILE		*fi;
5857c478bd9Sstevel@tonic-gate 	static char	pbuf[PASS_MAX + 1];
5867c478bd9Sstevel@tonic-gate 	void		(*saved_handler)();
5877c478bd9Sstevel@tonic-gate 	char		*rval = pbuf;
5887c478bd9Sstevel@tonic-gate 	int		i = 0;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	if ((fi = fopen(devname, "r")) == NULL)
5917c478bd9Sstevel@tonic-gate 		fi = stdin;
5927c478bd9Sstevel@tonic-gate 	else
5937c478bd9Sstevel@tonic-gate 		setbuf(fi, NULL);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	saved_handler = signal(SIGINT, SIG_IGN);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	ttyb = ttymodes;
5987c478bd9Sstevel@tonic-gate 	ttyb.c_lflag &= ~(ECHO | ECHOE | ECHONL);
5997c478bd9Sstevel@tonic-gate 	(void) ioctl(fileno(fi), TCSETAF, &ttyb);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	while ((c = getc(fi)) != '\n') {
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		if (c == EOF && i == 0) { 	/* ^D, No password */
6047c478bd9Sstevel@tonic-gate 			rval = NULL;
6057c478bd9Sstevel@tonic-gate 			break;
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		if (i < PASS_MAX)
6097c478bd9Sstevel@tonic-gate 			pbuf[i++] = c;
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 	pbuf[i] = '\0';
6127c478bd9Sstevel@tonic-gate 	(void) fputc('\n', fi);
6137c478bd9Sstevel@tonic-gate 	(void) ioctl(fileno(fi), TCSETAW, &ttymodes);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (saved_handler != SIG_ERR)
6167c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, saved_handler);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	return (rval);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate static char *
6227c478bd9Sstevel@tonic-gate findttyname(int fd)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	char	*ttyn = ttyname(fd);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if (ttyn == NULL)
6277c478bd9Sstevel@tonic-gate 		ttyn = "/dev/???";
6287c478bd9Sstevel@tonic-gate 	else {
6297c478bd9Sstevel@tonic-gate 		/*
6307c478bd9Sstevel@tonic-gate 		 * /dev/syscon and /dev/systty are usually links to
6317c478bd9Sstevel@tonic-gate 		 * /dev/console.  prefer /dev/console.
6327c478bd9Sstevel@tonic-gate 		 */
6337c478bd9Sstevel@tonic-gate 		if (((strcmp(ttyn, "/dev/syscon") == 0) ||
6347c478bd9Sstevel@tonic-gate 		    (strcmp(ttyn, "/dev/systty") == 0)) &&
6357c478bd9Sstevel@tonic-gate 		    access("/dev/console", F_OK))
6367c478bd9Sstevel@tonic-gate 			ttyn = "/dev/console";
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	return (ttyn);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate static char *
6427c478bd9Sstevel@tonic-gate stripttyname(char *ttyn)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	/* saw off the /dev/ */
6457c478bd9Sstevel@tonic-gate 	if (strncmp(ttyn, "/dev/", sizeof ("/dev/") -1) == 0)
6467c478bd9Sstevel@tonic-gate 		return (ttyn + sizeof ("/dev/") - 1);
6477c478bd9Sstevel@tonic-gate 	else
6487c478bd9Sstevel@tonic-gate 		return (ttyn);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /* ARGSUSED */
6537c478bd9Sstevel@tonic-gate static	void
6547c478bd9Sstevel@tonic-gate noop(int sig)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * This signal handler does nothing except return.  We use it
6587c478bd9Sstevel@tonic-gate 	 * as the signal disposition in this program instead of
6597c478bd9Sstevel@tonic-gate 	 * SIG_IGN so that we do not have to restore the disposition
6607c478bd9Sstevel@tonic-gate 	 * back to SIG_DFL. Instead we allow exec(2) to set the
6617c478bd9Sstevel@tonic-gate 	 * dispostion to SIG_DFL to avoid a race condition.
6627c478bd9Sstevel@tonic-gate 	 */
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /* ARGSUSED */
6667c478bd9Sstevel@tonic-gate static void
6677c478bd9Sstevel@tonic-gate parenthandler(int sig, siginfo_t *si, ucontext_t *uc)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	int i;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	/*
6727c478bd9Sstevel@tonic-gate 	 * We get here if someone has successfully entered a password
6737c478bd9Sstevel@tonic-gate 	 * from the auxiliary console and is getting the single-user shell.
6747c478bd9Sstevel@tonic-gate 	 * When this happens, the parent needs to kill the children
6757c478bd9Sstevel@tonic-gate 	 * that didn't get the shell.
6767c478bd9Sstevel@tonic-gate 	 *
6777c478bd9Sstevel@tonic-gate 	 */
6787c478bd9Sstevel@tonic-gate 	for (i = 0; i < nchild; i++) {
6797c478bd9Sstevel@tonic-gate 		if (pidlist[i] != si->__data.__proc.__pid)
6807c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, pidlist[i], SIGTERM);
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
6837c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
6847c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
6857c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, NULL);
6867c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sa, NULL);
6877c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
6887c478bd9Sstevel@tonic-gate 	(void) wait(NULL);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate /*
6927c478bd9Sstevel@tonic-gate  * The master pid will get SIGTERM or SIGHUP from init, and then
6937c478bd9Sstevel@tonic-gate  * has to make sure the shell isn't still running.
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate /* ARGSUSED */
6977c478bd9Sstevel@tonic-gate static	void
6987c478bd9Sstevel@tonic-gate childcleanup(int sig)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	int i;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	/* Only need to kill the child that became the shell. */
7037c478bd9Sstevel@tonic-gate 	for (i = 0; i < nchild; i++) {
7047c478bd9Sstevel@tonic-gate 		/* Don't kill gramps before his time */
7057c478bd9Sstevel@tonic-gate 		if (pidlist[i] != getppid())
7067c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, pidlist[i], SIGHUP);
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /* ARGSUSED */
7117c478bd9Sstevel@tonic-gate static	void
7127c478bd9Sstevel@tonic-gate termhandler(int sig)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	FILE *fi;
7157c478bd9Sstevel@tonic-gate 	pid_t pid;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	/* Processes come here when they fail to receive the password. */
7187c478bd9Sstevel@tonic-gate 	if ((fi = fopen("/dev/tty", "r+")) == NULL)
7197c478bd9Sstevel@tonic-gate 		fi = stdin;
7207c478bd9Sstevel@tonic-gate 	else
7217c478bd9Sstevel@tonic-gate 		setbuf(fi, NULL);
7227c478bd9Sstevel@tonic-gate 	sanitize_tty(fileno(fi));
7237c478bd9Sstevel@tonic-gate 	/* If you're the controlling tty, then just wait */
7247c478bd9Sstevel@tonic-gate 	pid = getpid();
7257c478bd9Sstevel@tonic-gate 	if (pid == originalpid || pid == masterpid) {
7267c478bd9Sstevel@tonic-gate 		sa.sa_handler = SIG_IGN;
7277c478bd9Sstevel@tonic-gate 		sa.sa_flags = 0;
7287c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&sa.sa_mask);
7297c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGINT, &sa, NULL);
7307c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGQUIT, &sa, NULL);
7317c478bd9Sstevel@tonic-gate 		sa.sa_handler = SIG_DFL;
7327c478bd9Sstevel@tonic-gate 		sa.sa_flags = 0;
7337c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&sa.sa_mask);
7347c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGTERM, &sa, NULL);
7357c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGHUP, &sa, NULL);
7367c478bd9Sstevel@tonic-gate 		(void) wait(NULL);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	exit(0);
7397c478bd9Sstevel@tonic-gate }
740