xref: /titanic_50/usr/src/cmd/sulogin/sulogin.c (revision 12f130f292b17d30e8cf927d99f846261c92105c)
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
53289e1bdSnakanon  * Common Development and Distribution License (the "License").
63289e1bdSnakanon  * 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 /*
223289e1bdSnakanon  * 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 /*
357c478bd9Sstevel@tonic-gate  *	sulogin - special login program exec'd from init to let user
367c478bd9Sstevel@tonic-gate  *	come up single user, or go to default init state straight away.
377c478bd9Sstevel@tonic-gate  *
38*12f130f2Sgww  *	Explain the scoop to the user, prompt for an authorized user
39*12f130f2Sgww  *	name or ^D and then prompt for password or ^D.  If the password
40*12f130f2Sgww  *	is correct, check if the user is authorized, if so enter
41*12f130f2Sgww  *	single user. ^D exits sulogin, and init will go to default init state.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  *	If /etc/passwd is missing, or there's no entry for root,
447c478bd9Sstevel@tonic-gate  *	go single user, no questions asked.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <sys/param.h>
507c478bd9Sstevel@tonic-gate #include <sys/sysmsg_impl.h>
517c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
527c478bd9Sstevel@tonic-gate #include <sys/resource.h>
537c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
547c478bd9Sstevel@tonic-gate #include <sys/wait.h>
557c478bd9Sstevel@tonic-gate #include <sys/stermio.h>
567c478bd9Sstevel@tonic-gate #include <fcntl.h>
577c478bd9Sstevel@tonic-gate #include <termio.h>
587c478bd9Sstevel@tonic-gate #include <pwd.h>
597c478bd9Sstevel@tonic-gate #include <shadow.h>
607c478bd9Sstevel@tonic-gate #include <stdlib.h>
617c478bd9Sstevel@tonic-gate #include <stdio.h>
627c478bd9Sstevel@tonic-gate #include <signal.h>
637c478bd9Sstevel@tonic-gate #include <siginfo.h>
647c478bd9Sstevel@tonic-gate #include <utmpx.h>
657c478bd9Sstevel@tonic-gate #include <unistd.h>
667c478bd9Sstevel@tonic-gate #include <ucontext.h>
677c478bd9Sstevel@tonic-gate #include <string.h>
687c478bd9Sstevel@tonic-gate #include <strings.h>
697c478bd9Sstevel@tonic-gate #include <deflt.h>
707c478bd9Sstevel@tonic-gate #include <limits.h>
717c478bd9Sstevel@tonic-gate #include <errno.h>
727c478bd9Sstevel@tonic-gate #include <crypt.h>
73*12f130f2Sgww #include <auth_attr.h>
74*12f130f2Sgww #include <auth_list.h>
75*12f130f2Sgww #include <nss_dbdefs.h>
76*12f130f2Sgww #include <user_attr.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Intervals to sleep after failed login
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate #ifndef SLEEPTIME
827c478bd9Sstevel@tonic-gate #define	SLEEPTIME	4	/* sleeptime before login incorrect msg */
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	SLEEPTIME_MAX	5	/* maximum sleeptime */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  *	the name of the file containing the login defaults we deliberately
897c478bd9Sstevel@tonic-gate  *	use the same file as login(1)
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #define	DEFAULT_LOGIN	"/etc/default/login"
937c478bd9Sstevel@tonic-gate #define	DEFAULT_SULOGIN	"/etc/default/sulogin"
947c478bd9Sstevel@tonic-gate #define	DEFAULT_CONSOLE	"/dev/console"
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static char	shell[]	= "/sbin/sh";
977c478bd9Sstevel@tonic-gate static char	su[]	= "/sbin/su.static";
987c478bd9Sstevel@tonic-gate static int	sleeptime	= SLEEPTIME;
997c478bd9Sstevel@tonic-gate static int	nchild = 0;
1007c478bd9Sstevel@tonic-gate static pid_t	pidlist[10];
1017c478bd9Sstevel@tonic-gate static pid_t	masterpid = 0;
1027c478bd9Sstevel@tonic-gate static pid_t	originalpid = 0;
1037c478bd9Sstevel@tonic-gate static struct sigaction	sa;
1047c478bd9Sstevel@tonic-gate static struct termio	ttymodes;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static char	*findttyname(int fd);
1077c478bd9Sstevel@tonic-gate static char	*stripttyname(char *);
108*12f130f2Sgww static char	*sulogin_getinput(char *, int);
1097c478bd9Sstevel@tonic-gate static void	noop(int);
1107c478bd9Sstevel@tonic-gate static void	single(const char *, char *);
111*12f130f2Sgww static void	main_loop(char *, boolean_t);
1127c478bd9Sstevel@tonic-gate static void	parenthandler();
1137c478bd9Sstevel@tonic-gate static void	termhandler(int);
1147c478bd9Sstevel@tonic-gate static void	setupsigs(void);
1157c478bd9Sstevel@tonic-gate static int	pathcmp(char *, char *);
116*12f130f2Sgww static void	doit(char *, char *);
1177c478bd9Sstevel@tonic-gate static void	childcleanup(int);
1187c478bd9Sstevel@tonic-gate 
119*12f130f2Sgww #define	ECHOON	0
120*12f130f2Sgww #define	ECHOOFF	1
121*12f130f2Sgww 
1227c478bd9Sstevel@tonic-gate /* ARGSUSED */
1237c478bd9Sstevel@tonic-gate int
1247c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	struct spwd	*shpw;
1277c478bd9Sstevel@tonic-gate 	int		passreq = B_TRUE;
1287c478bd9Sstevel@tonic-gate 	int		flags;
1297c478bd9Sstevel@tonic-gate 	int		fd;
1307c478bd9Sstevel@tonic-gate 	char		*infop, *ptr, *p;
1317c478bd9Sstevel@tonic-gate 	pid_t		pid;
1327c478bd9Sstevel@tonic-gate 	int		bufsize;
1337c478bd9Sstevel@tonic-gate 	struct stat	st;
1347c478bd9Sstevel@tonic-gate 	char		cttyname[100];
1357c478bd9Sstevel@tonic-gate 	char		namedlist[500];
1367c478bd9Sstevel@tonic-gate 	char		scratchlist[500];
1377c478bd9Sstevel@tonic-gate 	dev_t		cttyd;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
1407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: must be root\n", argv[0]);
1417c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/* Do the magic to determine the children */
1457c478bd9Sstevel@tonic-gate 	if ((fd = open(SYSMSG, 0)) < 0)
1467c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * If the console supports the CIOCTTYCONSOLE ioctl, then fetch
1507c478bd9Sstevel@tonic-gate 	 * its console device list.  If not, then we use the default
1517c478bd9Sstevel@tonic-gate 	 * console name.
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CIOCTTYCONSOLE, &cttyd) == 0) {
1547c478bd9Sstevel@tonic-gate 		if ((bufsize = ioctl(fd, CIOCGETCONSOLE, NULL)) < 0)
1557c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		if (bufsize > 0) {
1587c478bd9Sstevel@tonic-gate 			if ((infop = calloc(bufsize, sizeof (char))) == NULL)
1597c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 			if (ioctl(fd, CIOCGETCONSOLE, infop) < 0)
1627c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 			(void) snprintf(namedlist, sizeof (namedlist), "%s %s",
1657c478bd9Sstevel@tonic-gate 			    DEFAULT_CONSOLE, infop);
1667c478bd9Sstevel@tonic-gate 		} else
1677c478bd9Sstevel@tonic-gate 			(void) snprintf(namedlist, sizeof (namedlist), "%s",
1687c478bd9Sstevel@tonic-gate 			    DEFAULT_CONSOLE);
1697c478bd9Sstevel@tonic-gate 	} else {
1707c478bd9Sstevel@tonic-gate 		(void) snprintf(namedlist, sizeof (namedlist), "%s",
1717c478bd9Sstevel@tonic-gate 		    DEFAULT_CONSOLE);
1727c478bd9Sstevel@tonic-gate 		cttyd = NODEV;
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * The attempt to turn the controlling terminals dev_t into a string
1777c478bd9Sstevel@tonic-gate 	 * may not be successful, thus leaving the variable cttyname as a
1787c478bd9Sstevel@tonic-gate 	 * NULL.  This occurs if during boot we find
1797c478bd9Sstevel@tonic-gate 	 * the root partition (or some other partition)
1807c478bd9Sstevel@tonic-gate 	 * requires manual fsck, thus resulting in sulogin
1817c478bd9Sstevel@tonic-gate 	 * getting invoked.  The ioctl for CIOCTTYCONSOLE
1827c478bd9Sstevel@tonic-gate 	 * called above returned NODEV for cttyd
1837c478bd9Sstevel@tonic-gate 	 * in these cases.  NODEV gets returned when the vnode pointer
1847c478bd9Sstevel@tonic-gate 	 * in our session structure is NULL.  In these cases it
1857c478bd9Sstevel@tonic-gate 	 * must be assumed that the default console is used.
1867c478bd9Sstevel@tonic-gate 	 *
1877c478bd9Sstevel@tonic-gate 	 * See uts/common/os/session.c:cttydev().
1887c478bd9Sstevel@tonic-gate 	 */
1897c478bd9Sstevel@tonic-gate 	(void) strcpy(cttyname, DEFAULT_CONSOLE);
1907c478bd9Sstevel@tonic-gate 	(void) strcpy(scratchlist, namedlist);
1917c478bd9Sstevel@tonic-gate 	ptr = scratchlist;
1927c478bd9Sstevel@tonic-gate 	while (ptr != NULL) {
1937c478bd9Sstevel@tonic-gate 		p = strchr(ptr, ' ');
1947c478bd9Sstevel@tonic-gate 		if (p == NULL) {
1957c478bd9Sstevel@tonic-gate 			if (stat(ptr, &st))
1967c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1977c478bd9Sstevel@tonic-gate 			if (st.st_rdev == cttyd)
1987c478bd9Sstevel@tonic-gate 				(void) strcpy(cttyname, ptr);
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 		*p++ = '\0';
2027c478bd9Sstevel@tonic-gate 		if (stat(ptr, &st))
2037c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
2047c478bd9Sstevel@tonic-gate 		if (st.st_rdev == cttyd) {
2057c478bd9Sstevel@tonic-gate 			(void) strcpy(cttyname, ptr);
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 		ptr = p;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/*
2127c478bd9Sstevel@tonic-gate 	 * Use the same value of SLEEPTIME that login(1) uses.  This
2137c478bd9Sstevel@tonic-gate 	 * is obtained by reading the file /etc/default/login using
2147c478bd9Sstevel@tonic-gate 	 * the def*() functions.
2157c478bd9Sstevel@tonic-gate 	 */
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (defopen(DEFAULT_LOGIN) == 0) {
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		/* ignore case */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		flags = defcntl(DC_GETFLAGS, 0);
2227c478bd9Sstevel@tonic-gate 		TURNOFF(flags, DC_CASE);
2237c478bd9Sstevel@tonic-gate 		(void) defcntl(DC_SETFLAGS, flags);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		if ((ptr = defread("SLEEPTIME=")) != NULL)
2267c478bd9Sstevel@tonic-gate 			sleeptime = atoi(ptr);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		if (sleeptime < 0 || sleeptime > SLEEPTIME_MAX)
2297c478bd9Sstevel@tonic-gate 			sleeptime = SLEEPTIME;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);	/* closes DEFAULT_LOGIN */
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * Use our own value of PASSREQ, separate from the one login(1) uses.
2367c478bd9Sstevel@tonic-gate 	 * This is obtained by reading the file /etc/default/sulogin using
2377c478bd9Sstevel@tonic-gate 	 * the def*() functions.
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (defopen(DEFAULT_SULOGIN) == 0) {
2417c478bd9Sstevel@tonic-gate 		if ((ptr = defread("PASSREQ=")) != NULL)
2427c478bd9Sstevel@tonic-gate 			if (strcmp("NO", ptr) == 0)
2437c478bd9Sstevel@tonic-gate 				passreq = B_FALSE;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);	/* closes DEFAULT_SULOGIN */
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (passreq == B_FALSE)
2497c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * if no 'root' entry in /etc/shadow, give maint. mode single
2537c478bd9Sstevel@tonic-gate 	 * user shell prompt
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 	setspent();
2567c478bd9Sstevel@tonic-gate 	if ((shpw = getspnam("root")) == NULL) {
2577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
2587c478bd9Sstevel@tonic-gate 		    "in shadow password file ***\n\n");
2597c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	endspent();
2627c478bd9Sstevel@tonic-gate 	/*
2637c478bd9Sstevel@tonic-gate 	 * if no 'root' entry in /etc/passwd, give maint. mode single
2647c478bd9Sstevel@tonic-gate 	 * user shell prompt
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	setpwent();
2677c478bd9Sstevel@tonic-gate 	if (getpwnam("root") == NULL) {
2687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
2697c478bd9Sstevel@tonic-gate 		    "in password file ***\n\n");
2707c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	endpwent();
2737c478bd9Sstevel@tonic-gate 	/* process with controlling tty treated special */
2747c478bd9Sstevel@tonic-gate 	if ((pid = fork()) != (pid_t)0) {
2757c478bd9Sstevel@tonic-gate 		if (pid == -1)
2767c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
2777c478bd9Sstevel@tonic-gate 		else {
2787c478bd9Sstevel@tonic-gate 			setupsigs();
2797c478bd9Sstevel@tonic-gate 			masterpid = pid;
2807c478bd9Sstevel@tonic-gate 			originalpid = getpid();
2817c478bd9Sstevel@tonic-gate 			/*
2827c478bd9Sstevel@tonic-gate 			 * init() was invoked from a console that was not
2837c478bd9Sstevel@tonic-gate 			 * the default console, nor was it an auxiliary.
2847c478bd9Sstevel@tonic-gate 			 */
2857c478bd9Sstevel@tonic-gate 			if (cttyname[0] == NULL)
2867c478bd9Sstevel@tonic-gate 				termhandler(0);
2877c478bd9Sstevel@tonic-gate 				/* Never returns */
2887c478bd9Sstevel@tonic-gate 
289*12f130f2Sgww 			main_loop(cttyname, B_TRUE);
2907c478bd9Sstevel@tonic-gate 			/* Never returns */
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	masterpid = getpid();
2947c478bd9Sstevel@tonic-gate 	originalpid = getppid();
2957c478bd9Sstevel@tonic-gate 	pidlist[nchild++] = originalpid;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	sa.sa_handler = childcleanup;
2987c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
2997c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3007c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
3017c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sa, NULL);
3027c478bd9Sstevel@tonic-gate 	sa.sa_handler = parenthandler;
3037c478bd9Sstevel@tonic-gate 	sa.sa_flags = SA_SIGINFO;
3047c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3057c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &sa, NULL);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
3087c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
3097c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3107c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &sa, NULL);
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * If there isn't a password on root, then don't permit
3137c478bd9Sstevel@tonic-gate 	 * the fanout capability of sulogin.
3147c478bd9Sstevel@tonic-gate 	 */
3157c478bd9Sstevel@tonic-gate 	if (*shpw->sp_pwdp != '\0') {
3167c478bd9Sstevel@tonic-gate 		ptr = namedlist;
3177c478bd9Sstevel@tonic-gate 		while (ptr != NULL) {
3187c478bd9Sstevel@tonic-gate 			p = strchr(ptr, ' ');
3197c478bd9Sstevel@tonic-gate 			if (p == NULL) {
320*12f130f2Sgww 				doit(ptr, cttyname);
3217c478bd9Sstevel@tonic-gate 				break;
3227c478bd9Sstevel@tonic-gate 			}
3237c478bd9Sstevel@tonic-gate 			*p++ = '\0';
324*12f130f2Sgww 			doit(ptr, cttyname);
3257c478bd9Sstevel@tonic-gate 			ptr = p;
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 	if (pathcmp(cttyname, DEFAULT_CONSOLE) != 0) {
3297c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
3307c478bd9Sstevel@tonic-gate 			setupsigs();
331*12f130f2Sgww 			main_loop(DEFAULT_CONSOLE, B_FALSE);
3327c478bd9Sstevel@tonic-gate 		} else if (pid == -1)
3337c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
3347c478bd9Sstevel@tonic-gate 		pidlist[nchild++] = pid;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 * When parent is all done, it pauses until one of its children
3387c478bd9Sstevel@tonic-gate 	 * signals that its time to kill the underpriviledged.
3397c478bd9Sstevel@tonic-gate 	 */
3407c478bd9Sstevel@tonic-gate 	(void) wait(NULL);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	return (0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * These flags are taken from stty's "sane" table entries in
3477c478bd9Sstevel@tonic-gate  * usr/src/cmd/ttymon/sttytable.c
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate #define	SET_IFLAG (BRKINT|IGNPAR|ISTRIP|ICRNL|IXON|IMAXBEL)
3507c478bd9Sstevel@tonic-gate #define	RESET_IFLAG (IGNBRK|PARMRK|INPCK|INLCR|IGNCR|IUCLC|IXOFF|IXANY)
3517c478bd9Sstevel@tonic-gate #define	SET_OFLAG (OPOST|ONLCR)
3527c478bd9Sstevel@tonic-gate #define	RESET_OFLAG (OLCUC|OCRNL|ONOCR|ONLRET|OFILL|OFDEL| \
3537c478bd9Sstevel@tonic-gate 	NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY)
3547c478bd9Sstevel@tonic-gate #define	SET_LFLAG (ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL)
3557c478bd9Sstevel@tonic-gate #define	RESET_LFLAG (XCASE|ECHONL|NOFLSH|STFLUSH|STWRAP|STAPPL)
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * Do the equivalent of 'stty sane' on the terminal since we don't know
3597c478bd9Sstevel@tonic-gate  * what state it was in on startup.
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate static void
3627c478bd9Sstevel@tonic-gate sanitize_tty(int fd)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	(void) ioctl(fd, TCGETA, &ttymodes);
3657c478bd9Sstevel@tonic-gate 	ttymodes.c_iflag &= ~RESET_IFLAG;
3663289e1bdSnakanon 	ttymodes.c_iflag |= SET_IFLAG;
3677c478bd9Sstevel@tonic-gate 	ttymodes.c_oflag &= ~RESET_OFLAG;
3683289e1bdSnakanon 	ttymodes.c_oflag |= SET_OFLAG;
3697c478bd9Sstevel@tonic-gate 	ttymodes.c_lflag &= ~RESET_LFLAG;
3703289e1bdSnakanon 	ttymodes.c_lflag |= SET_LFLAG;
3717c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VERASE] = CERASE;
3727c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VKILL] = CKILL;
3737c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VQUIT] = CQUIT;
3747c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VINTR] = CINTR;
3757c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VEOF] = CEOF;
3767c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VEOL] = CNUL;
3777c478bd9Sstevel@tonic-gate 	(void) ioctl(fd, TCSETAF, &ttymodes);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * Fork a child of sulogin for each of the auxiliary consoles.
3827c478bd9Sstevel@tonic-gate  */
3837c478bd9Sstevel@tonic-gate static void
384*12f130f2Sgww doit(char *ptr, char *cttyname)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	pid_t	pid;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (pathcmp(ptr, DEFAULT_CONSOLE) != 0 &&
3897c478bd9Sstevel@tonic-gate 	    pathcmp(ptr, cttyname) != 0) {
3907c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
3917c478bd9Sstevel@tonic-gate 			setupsigs();
392*12f130f2Sgww 			main_loop(ptr, B_FALSE);
3937c478bd9Sstevel@tonic-gate 		} else if (pid == -1)
3947c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
3957c478bd9Sstevel@tonic-gate 		pidlist[nchild++] = pid;
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static int
4007c478bd9Sstevel@tonic-gate pathcmp(char *adev, char *bdev)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	struct stat	st1;
4037c478bd9Sstevel@tonic-gate 	struct stat	st2;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (adev == NULL || bdev == NULL)
4067c478bd9Sstevel@tonic-gate 		return (1);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if (strcmp(adev, bdev) == 0)
4097c478bd9Sstevel@tonic-gate 		return (0);
4107c478bd9Sstevel@tonic-gate 
4114bc0a2efScasper 	if (stat(adev, &st1) || !S_ISCHR(st1.st_mode))
4127c478bd9Sstevel@tonic-gate 		return (1);
4137c478bd9Sstevel@tonic-gate 
4144bc0a2efScasper 	if (stat(bdev, &st2) || !S_ISCHR(st2.st_mode))
4157c478bd9Sstevel@tonic-gate 		return (1);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (st1.st_rdev == st2.st_rdev)
4187c478bd9Sstevel@tonic-gate 		return (0);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	return (1);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /* Handlers for the children at initialization */
4247c478bd9Sstevel@tonic-gate static void
4257c478bd9Sstevel@tonic-gate setupsigs()
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	sa.sa_handler = noop;
4287c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
4297c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
4307c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, NULL);
4317c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sa, NULL);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	sa.sa_handler = termhandler;
4347c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
4357c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
4367c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
4377c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGKILL, &sa, NULL);
4387c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sa, NULL);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate static void
442*12f130f2Sgww main_loop(char *devname, boolean_t cttyflag)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	int		fd, i;
445*12f130f2Sgww 	char		*user = NULL;		/* authorized user */
4467c478bd9Sstevel@tonic-gate 	char		*pass;			/* password from user */
447*12f130f2Sgww 	char		*cpass;			/* crypted password */
448*12f130f2Sgww 	struct spwd	spwd;
449*12f130f2Sgww 	struct spwd	*lshpw;			/* local shadow */
450*12f130f2Sgww 	char		shadow[NSS_BUFLEN_SHADOW];
4517c478bd9Sstevel@tonic-gate 	FILE		*sysmsgfd;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++)
4547c478bd9Sstevel@tonic-gate 		(void) close(i);
4557c478bd9Sstevel@tonic-gate 	if (cttyflag == B_FALSE) {
4567c478bd9Sstevel@tonic-gate 		if (setsid() == -1)
4577c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	if ((fd = open(devname, O_RDWR)) < 0)
4607c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
4617c478bd9Sstevel@tonic-gate 	if (fd != 0)
4627c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDIN_FILENO);
4637c478bd9Sstevel@tonic-gate 	if (fd != 1)
4647c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDOUT_FILENO);
4657c478bd9Sstevel@tonic-gate 	if (fd != 2)
4667c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDERR_FILENO);
4677c478bd9Sstevel@tonic-gate 	if (fd > 2)
4687c478bd9Sstevel@tonic-gate 		(void) close(fd);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	sysmsgfd = fopen("/dev/sysmsg", "w");
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	sanitize_tty(fileno(stdin));
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	for (;;) {
475*12f130f2Sgww 		(void) printf("\nEnter user name for system maintenance "
476*12f130f2Sgww 		    "(control-d to bypass): ");
477*12f130f2Sgww 		if ((user = sulogin_getinput(devname, ECHOON)) == NULL) {
4787c478bd9Sstevel@tonic-gate 			/* signal other children to exit */
4797c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, masterpid, SIGUSR1);
4807c478bd9Sstevel@tonic-gate 			/* ^D, so straight to default init state */
4817c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4827c478bd9Sstevel@tonic-gate 		}
483*12f130f2Sgww 		(void) printf("\nEnter %s password for system maintenance "
484*12f130f2Sgww 		    "(control-d to bypass): ", user);
485*12f130f2Sgww 
486*12f130f2Sgww 		if ((pass = sulogin_getinput(devname, ECHOOFF)) == NULL) {
487*12f130f2Sgww 			/* signal other children to exit */
4887c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, masterpid, SIGUSR1);
489*12f130f2Sgww 			/* ^D, so straight to default init state */
490*12f130f2Sgww 			free(user);
491*12f130f2Sgww 			exit(EXIT_FAILURE);
492*12f130f2Sgww 		}
493*12f130f2Sgww 		lshpw = getspnam_r(user, &spwd, shadow, sizeof (shadow));
494*12f130f2Sgww 		if (lshpw == NULL) {
4957c478bd9Sstevel@tonic-gate 			/*
496*12f130f2Sgww 			 * the user entered doesn't exist, too bad.
497*12f130f2Sgww 			 */
498*12f130f2Sgww 			goto sorry;
499*12f130f2Sgww 		}
500*12f130f2Sgww 
501*12f130f2Sgww 		/*
502*12f130f2Sgww 		 * There is a special case error to catch here:
503*12f130f2Sgww 		 * If the password is hashed with an algorithm
5047c478bd9Sstevel@tonic-gate 		 * other than the old unix crypt the call to crypt(3c)
5057c478bd9Sstevel@tonic-gate 		 * could fail if /usr is corrupt or not available
5067c478bd9Sstevel@tonic-gate 		 * since by default /etc/security/crypt.conf will
5077c478bd9Sstevel@tonic-gate 		 * have the crypt_ modules located under /usr/lib.
508*12f130f2Sgww 		 * Or it could happen if /etc/security/crypt.conf
509*12f130f2Sgww 		 * is corrupted.
5107c478bd9Sstevel@tonic-gate 		 *
5117c478bd9Sstevel@tonic-gate 		 * If this happens crypt(3c) will return NULL and
512*12f130f2Sgww 		 * set errno to ELIBACC for the former condition or
513*12f130f2Sgww 		 * EINVAL for the latter, in this case we bypass
514*12f130f2Sgww 		 * authentication and just verify that the user is
515*12f130f2Sgww 		 * authorized.
5167c478bd9Sstevel@tonic-gate 		 */
517*12f130f2Sgww 
518*12f130f2Sgww 		errno = 0;
519*12f130f2Sgww 		cpass = crypt(pass, lshpw->sp_pwdp);
520*12f130f2Sgww 		if (((cpass == NULL) && (lshpw->sp_pwdp[0] == '$')) &&
521*12f130f2Sgww 		    ((errno == ELIBACC) || (errno == EINVAL))) {
522*12f130f2Sgww 			goto checkauth;
523*12f130f2Sgww 		} else if ((cpass == NULL) ||
524*12f130f2Sgww 		    (strcmp(cpass, lshpw->sp_pwdp) != 0)) {
525*12f130f2Sgww 			goto sorry;
526*12f130f2Sgww 		}
527*12f130f2Sgww 
528*12f130f2Sgww checkauth:
529*12f130f2Sgww 		/*
530*12f130f2Sgww 		 * There is a special case error here as well.
531*12f130f2Sgww 		 * If /etc/user_attr is corrupt, getusernam("root")
532*12f130f2Sgww 		 * returns NULL.
533*12f130f2Sgww 		 * In this case, we just give access because this is similar
534*12f130f2Sgww 		 * to the case of root not existing in /etc/passwd.
535*12f130f2Sgww 		 */
536*12f130f2Sgww 
537*12f130f2Sgww 		if ((getusernam("root") != NULL) &&
538*12f130f2Sgww 		    (chkauthattr(MAINTENANCE_AUTH, user) != 1)) {
539*12f130f2Sgww 			goto sorry;
540*12f130f2Sgww 		}
541*12f130f2Sgww 		(void) fprintf(sysmsgfd, "\nsingle-user privilege "
542*12f130f2Sgww 		    "assigned to %s on %s.\n", user, devname);
5437c478bd9Sstevel@tonic-gate 		(void) sigsend(P_PID, masterpid, SIGUSR1);
5447c478bd9Sstevel@tonic-gate 		(void) wait(NULL);
545*12f130f2Sgww 		free(user);
546*12f130f2Sgww 		free(pass);
5477c478bd9Sstevel@tonic-gate 		single(su, devname);
548*12f130f2Sgww 		/* single never returns */
549*12f130f2Sgww 
550*12f130f2Sgww sorry:
551*12f130f2Sgww 		(void) printf("\nLogin incorrect or user %s not authorized\n",
552*12f130f2Sgww 		    user);
553*12f130f2Sgww 		free(user);
554*12f130f2Sgww 		free(pass);
5557c478bd9Sstevel@tonic-gate 		(void) sleep(sleeptime);
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /*
5607c478bd9Sstevel@tonic-gate  * single() - exec shell for single user mode
5617c478bd9Sstevel@tonic-gate  */
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate static void
5647c478bd9Sstevel@tonic-gate single(const char *cmd, char *ttyn)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	struct utmpx	*u;
5677c478bd9Sstevel@tonic-gate 	char		found = B_FALSE;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	if (ttyn == NULL)
5707c478bd9Sstevel@tonic-gate 		ttyn = findttyname(STDIN_FILENO);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * utmpx records on the console device are expected to be "console"
5747c478bd9Sstevel@tonic-gate 	 * by other processes, such as dtlogin.
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 	ttyn = stripttyname(ttyn);
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/* update the utmpx file. */
5797c478bd9Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
5807c478bd9Sstevel@tonic-gate 		if (strcmp(u->ut_line, ttyn) == 0) {
5817c478bd9Sstevel@tonic-gate 			u->ut_tv.tv_sec = time(NULL);
5827c478bd9Sstevel@tonic-gate 			u->ut_type = USER_PROCESS;
5837c478bd9Sstevel@tonic-gate 			u->ut_pid = getpid();
5847c478bd9Sstevel@tonic-gate 			if (strcmp(u->ut_user, "root") != 0)
5857c478bd9Sstevel@tonic-gate 				(void) strcpy(u->ut_user, "root");
5867c478bd9Sstevel@tonic-gate 			(void) pututxline(u);
5877c478bd9Sstevel@tonic-gate 			found = B_TRUE;
5887c478bd9Sstevel@tonic-gate 			break;
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 	if (!found) {
5927c478bd9Sstevel@tonic-gate 		struct utmpx entryx;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		entryx.ut_tv.tv_sec = time(NULL);
5957c478bd9Sstevel@tonic-gate 		entryx.ut_type = USER_PROCESS;
5967c478bd9Sstevel@tonic-gate 		entryx.ut_pid = getpid();
5977c478bd9Sstevel@tonic-gate 		(void) strcpy(entryx.ut_user, "root");
5987c478bd9Sstevel@tonic-gate 		(void) strcpy(entryx.ut_line, ttyn);
5997c478bd9Sstevel@tonic-gate 		entryx.ut_tv.tv_usec = 0;
6007c478bd9Sstevel@tonic-gate 		entryx.ut_session = 0;
6017c478bd9Sstevel@tonic-gate 		entryx.ut_id[0] = 'c';
6027c478bd9Sstevel@tonic-gate 		entryx.ut_id[1] = 'o';
6037c478bd9Sstevel@tonic-gate 		entryx.ut_id[2] = 's';
6047c478bd9Sstevel@tonic-gate 		entryx.ut_id[3] = 'u';
6057c478bd9Sstevel@tonic-gate 		entryx.ut_syslen = 1;
6067c478bd9Sstevel@tonic-gate 		entryx.ut_host[0] = '\0';
6077c478bd9Sstevel@tonic-gate 		entryx.ut_exit.e_termination = WTERMSIG(0);
6087c478bd9Sstevel@tonic-gate 		entryx.ut_exit.e_exit = WEXITSTATUS(0);
6097c478bd9Sstevel@tonic-gate 		(void) pututxline(&entryx);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 	endutxent();
6127c478bd9Sstevel@tonic-gate 	(void) printf("Entering System Maintenance Mode\n\n");
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (execl(cmd, cmd, "-", (char *)0) < 0)
6157c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
619*12f130f2Sgww  * sulogin_getinput() - hacked from the standard PAM tty conversation
620*12f130f2Sgww  *			function getpassphrase() library version
621*12f130f2Sgww  *			so we can distinguish newline and EOF.
622*12f130f2Sgww  *		        also don't need this routine to give a prompt.
6237c478bd9Sstevel@tonic-gate  *
6247c478bd9Sstevel@tonic-gate  * returns the password string, or NULL if the used typed EOF.
6257c478bd9Sstevel@tonic-gate  */
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate static char *
628*12f130f2Sgww sulogin_getinput(char *devname, int echooff)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 	struct termio	ttyb;
6317c478bd9Sstevel@tonic-gate 	int		c;
6327c478bd9Sstevel@tonic-gate 	FILE		*fi;
633*12f130f2Sgww 	static char	input[PASS_MAX + 1];
6347c478bd9Sstevel@tonic-gate 	void		(*saved_handler)();
635*12f130f2Sgww 	char		*rval = input;
6367c478bd9Sstevel@tonic-gate 	int		i = 0;
6377c478bd9Sstevel@tonic-gate 
638*12f130f2Sgww 	if ((fi = fopen(devname, "r")) == NULL) {
6397c478bd9Sstevel@tonic-gate 		fi = stdin;
640*12f130f2Sgww 	}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	saved_handler = signal(SIGINT, SIG_IGN);
6437c478bd9Sstevel@tonic-gate 
644*12f130f2Sgww 	if (echooff) {
6457c478bd9Sstevel@tonic-gate 		ttyb = ttymodes;
6467c478bd9Sstevel@tonic-gate 		ttyb.c_lflag &= ~(ECHO | ECHOE | ECHONL);
6477c478bd9Sstevel@tonic-gate 		(void) ioctl(fileno(fi), TCSETAF, &ttyb);
648*12f130f2Sgww 	}
6497c478bd9Sstevel@tonic-gate 
650*12f130f2Sgww 	/* get characters up to PASS_MAX, but don't overflow */
651*12f130f2Sgww 	while ((c = getc(fi)) != '\n' && (c != '\r')) {
652*12f130f2Sgww 		if (c == EOF && i == 0) {	/* ^D, no input */
6537c478bd9Sstevel@tonic-gate 			rval = NULL;
6547c478bd9Sstevel@tonic-gate 			break;
6557c478bd9Sstevel@tonic-gate 		}
656*12f130f2Sgww 		if (i < PASS_MAX) {
657*12f130f2Sgww 			input[i++] = (char)c;
6587c478bd9Sstevel@tonic-gate 		}
659*12f130f2Sgww 	}
660*12f130f2Sgww 	input[i] = '\0';
6617c478bd9Sstevel@tonic-gate 	(void) fputc('\n', fi);
662*12f130f2Sgww 	if (echooff) {
6637c478bd9Sstevel@tonic-gate 		(void) ioctl(fileno(fi), TCSETAW, &ttymodes);
664*12f130f2Sgww 	}
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (saved_handler != SIG_ERR)
6677c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, saved_handler);
668*12f130f2Sgww 	return (rval == NULL ? NULL : strdup(rval));
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate static char *
6727c478bd9Sstevel@tonic-gate findttyname(int fd)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	char	*ttyn = ttyname(fd);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	if (ttyn == NULL)
6777c478bd9Sstevel@tonic-gate 		ttyn = "/dev/???";
6787c478bd9Sstevel@tonic-gate 	else {
6797c478bd9Sstevel@tonic-gate 		/*
6807c478bd9Sstevel@tonic-gate 		 * /dev/syscon and /dev/systty are usually links to
6817c478bd9Sstevel@tonic-gate 		 * /dev/console.  prefer /dev/console.
6827c478bd9Sstevel@tonic-gate 		 */
6837c478bd9Sstevel@tonic-gate 		if (((strcmp(ttyn, "/dev/syscon") == 0) ||
6847c478bd9Sstevel@tonic-gate 		    (strcmp(ttyn, "/dev/systty") == 0)) &&
6857c478bd9Sstevel@tonic-gate 		    access("/dev/console", F_OK))
6867c478bd9Sstevel@tonic-gate 			ttyn = "/dev/console";
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	return (ttyn);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate static char *
6927c478bd9Sstevel@tonic-gate stripttyname(char *ttyn)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	/* saw off the /dev/ */
6957c478bd9Sstevel@tonic-gate 	if (strncmp(ttyn, "/dev/", sizeof ("/dev/") -1) == 0)
6967c478bd9Sstevel@tonic-gate 		return (ttyn + sizeof ("/dev/") - 1);
6977c478bd9Sstevel@tonic-gate 	else
6987c478bd9Sstevel@tonic-gate 		return (ttyn);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate /* ARGSUSED */
7037c478bd9Sstevel@tonic-gate static	void
7047c478bd9Sstevel@tonic-gate noop(int sig)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	/*
7077c478bd9Sstevel@tonic-gate 	 * This signal handler does nothing except return.  We use it
7087c478bd9Sstevel@tonic-gate 	 * as the signal disposition in this program instead of
7097c478bd9Sstevel@tonic-gate 	 * SIG_IGN so that we do not have to restore the disposition
7107c478bd9Sstevel@tonic-gate 	 * back to SIG_DFL. Instead we allow exec(2) to set the
7117c478bd9Sstevel@tonic-gate 	 * dispostion to SIG_DFL to avoid a race condition.
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate /* ARGSUSED */
7167c478bd9Sstevel@tonic-gate static void
7177c478bd9Sstevel@tonic-gate parenthandler(int sig, siginfo_t *si, ucontext_t *uc)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	int i;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	/*
7227c478bd9Sstevel@tonic-gate 	 * We get here if someone has successfully entered a password
7237c478bd9Sstevel@tonic-gate 	 * from the auxiliary console and is getting the single-user shell.
7247c478bd9Sstevel@tonic-gate 	 * When this happens, the parent needs to kill the children
7257c478bd9Sstevel@tonic-gate 	 * that didn't get the shell.
7267c478bd9Sstevel@tonic-gate 	 *
7277c478bd9Sstevel@tonic-gate 	 */
7287c478bd9Sstevel@tonic-gate 	for (i = 0; i < nchild; i++) {
7297c478bd9Sstevel@tonic-gate 		if (pidlist[i] != si->__data.__proc.__pid)
7307c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, pidlist[i], SIGTERM);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
7337c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
7347c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
7357c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, NULL);
7367c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sa, NULL);
7377c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
7387c478bd9Sstevel@tonic-gate 	(void) wait(NULL);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate  * The master pid will get SIGTERM or SIGHUP from init, and then
7437c478bd9Sstevel@tonic-gate  * has to make sure the shell isn't still running.
7447c478bd9Sstevel@tonic-gate  */
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate /* ARGSUSED */
7477c478bd9Sstevel@tonic-gate static	void
7487c478bd9Sstevel@tonic-gate childcleanup(int sig)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	int i;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	/* Only need to kill the child that became the shell. */
7537c478bd9Sstevel@tonic-gate 	for (i = 0; i < nchild; i++) {
7547c478bd9Sstevel@tonic-gate 		/* Don't kill gramps before his time */
7557c478bd9Sstevel@tonic-gate 		if (pidlist[i] != getppid())
7567c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, pidlist[i], SIGHUP);
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate /* ARGSUSED */
7617c478bd9Sstevel@tonic-gate static	void
7627c478bd9Sstevel@tonic-gate termhandler(int sig)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate 	FILE *fi;
7657c478bd9Sstevel@tonic-gate 	pid_t pid;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	/* Processes come here when they fail to receive the password. */
7687c478bd9Sstevel@tonic-gate 	if ((fi = fopen("/dev/tty", "r+")) == NULL)
7697c478bd9Sstevel@tonic-gate 		fi = stdin;
7707c478bd9Sstevel@tonic-gate 	else
7717c478bd9Sstevel@tonic-gate 		setbuf(fi, NULL);
7727c478bd9Sstevel@tonic-gate 	sanitize_tty(fileno(fi));
7737c478bd9Sstevel@tonic-gate 	/* If you're the controlling tty, then just wait */
7747c478bd9Sstevel@tonic-gate 	pid = getpid();
7757c478bd9Sstevel@tonic-gate 	if (pid == originalpid || pid == masterpid) {
7767c478bd9Sstevel@tonic-gate 		sa.sa_handler = SIG_IGN;
7777c478bd9Sstevel@tonic-gate 		sa.sa_flags = 0;
7787c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&sa.sa_mask);
7797c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGINT, &sa, NULL);
7807c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGQUIT, &sa, NULL);
7817c478bd9Sstevel@tonic-gate 		sa.sa_handler = SIG_DFL;
7827c478bd9Sstevel@tonic-gate 		sa.sa_flags = 0;
7837c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&sa.sa_mask);
7847c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGTERM, &sa, NULL);
7857c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGHUP, &sa, NULL);
7867c478bd9Sstevel@tonic-gate 		(void) wait(NULL);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 	exit(0);
7897c478bd9Sstevel@tonic-gate }
790