xref: /titanic_50/usr/src/cmd/sulogin/sulogin.c (revision aecfc01d1bad84e66649703f7fc2926ef70b34ba)
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  *
3812f130f2Sgww  *	Explain the scoop to the user, prompt for an authorized user
3912f130f2Sgww  *	name or ^D and then prompt for password or ^D.  If the password
4012f130f2Sgww  *	is correct, check if the user is authorized, if so enter
4112f130f2Sgww  *	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>
7312f130f2Sgww #include <auth_attr.h>
7412f130f2Sgww #include <auth_list.h>
7512f130f2Sgww #include <nss_dbdefs.h>
7612f130f2Sgww #include <user_attr.h>
77*aecfc01dSrui zang - Sun Microsystems - Beijing China #include <sys/vt.h>
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * Intervals to sleep after failed login
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate #ifndef SLEEPTIME
837c478bd9Sstevel@tonic-gate #define	SLEEPTIME	4	/* sleeptime before login incorrect msg */
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	SLEEPTIME_MAX	5	/* maximum sleeptime */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  *	the name of the file containing the login defaults we deliberately
907c478bd9Sstevel@tonic-gate  *	use the same file as login(1)
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	DEFAULT_LOGIN	"/etc/default/login"
947c478bd9Sstevel@tonic-gate #define	DEFAULT_SULOGIN	"/etc/default/sulogin"
957c478bd9Sstevel@tonic-gate #define	DEFAULT_CONSOLE	"/dev/console"
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static char	shell[]	= "/sbin/sh";
987c478bd9Sstevel@tonic-gate static char	su[]	= "/sbin/su.static";
997c478bd9Sstevel@tonic-gate static int	sleeptime	= SLEEPTIME;
1007c478bd9Sstevel@tonic-gate static int	nchild = 0;
1017c478bd9Sstevel@tonic-gate static pid_t	pidlist[10];
1027c478bd9Sstevel@tonic-gate static pid_t	masterpid = 0;
1037c478bd9Sstevel@tonic-gate static pid_t	originalpid = 0;
1047c478bd9Sstevel@tonic-gate static struct sigaction	sa;
1057c478bd9Sstevel@tonic-gate static struct termio	ttymodes;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static char	*findttyname(int fd);
1087c478bd9Sstevel@tonic-gate static char	*stripttyname(char *);
10912f130f2Sgww static char	*sulogin_getinput(char *, int);
1107c478bd9Sstevel@tonic-gate static void	noop(int);
1117c478bd9Sstevel@tonic-gate static void	single(const char *, char *);
11212f130f2Sgww static void	main_loop(char *, boolean_t);
1137c478bd9Sstevel@tonic-gate static void	parenthandler();
1147c478bd9Sstevel@tonic-gate static void	termhandler(int);
1157c478bd9Sstevel@tonic-gate static void	setupsigs(void);
1167c478bd9Sstevel@tonic-gate static int	pathcmp(char *, char *);
11712f130f2Sgww static void	doit(char *, char *);
1187c478bd9Sstevel@tonic-gate static void	childcleanup(int);
1197c478bd9Sstevel@tonic-gate 
12012f130f2Sgww #define	ECHOON	0
12112f130f2Sgww #define	ECHOOFF	1
12212f130f2Sgww 
1237c478bd9Sstevel@tonic-gate /* ARGSUSED */
1247c478bd9Sstevel@tonic-gate int
1257c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	struct spwd	*shpw;
1287c478bd9Sstevel@tonic-gate 	int		passreq = B_TRUE;
1297c478bd9Sstevel@tonic-gate 	int		flags;
1307c478bd9Sstevel@tonic-gate 	int		fd;
1317c478bd9Sstevel@tonic-gate 	char		*infop, *ptr, *p;
1327c478bd9Sstevel@tonic-gate 	pid_t		pid;
1337c478bd9Sstevel@tonic-gate 	int		bufsize;
1347c478bd9Sstevel@tonic-gate 	struct stat	st;
1357c478bd9Sstevel@tonic-gate 	char		cttyname[100];
1367c478bd9Sstevel@tonic-gate 	char		namedlist[500];
1377c478bd9Sstevel@tonic-gate 	char		scratchlist[500];
1387c478bd9Sstevel@tonic-gate 	dev_t		cttyd;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
1417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: must be root\n", argv[0]);
1427c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/* Do the magic to determine the children */
1467c478bd9Sstevel@tonic-gate 	if ((fd = open(SYSMSG, 0)) < 0)
1477c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * If the console supports the CIOCTTYCONSOLE ioctl, then fetch
1517c478bd9Sstevel@tonic-gate 	 * its console device list.  If not, then we use the default
1527c478bd9Sstevel@tonic-gate 	 * console name.
1537c478bd9Sstevel@tonic-gate 	 */
1547c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CIOCTTYCONSOLE, &cttyd) == 0) {
1557c478bd9Sstevel@tonic-gate 		if ((bufsize = ioctl(fd, CIOCGETCONSOLE, NULL)) < 0)
1567c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		if (bufsize > 0) {
1597c478bd9Sstevel@tonic-gate 			if ((infop = calloc(bufsize, sizeof (char))) == NULL)
1607c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 			if (ioctl(fd, CIOCGETCONSOLE, infop) < 0)
1637c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 			(void) snprintf(namedlist, sizeof (namedlist), "%s %s",
1667c478bd9Sstevel@tonic-gate 			    DEFAULT_CONSOLE, infop);
1677c478bd9Sstevel@tonic-gate 		} else
1687c478bd9Sstevel@tonic-gate 			(void) snprintf(namedlist, sizeof (namedlist), "%s",
1697c478bd9Sstevel@tonic-gate 			    DEFAULT_CONSOLE);
1707c478bd9Sstevel@tonic-gate 	} else {
1717c478bd9Sstevel@tonic-gate 		(void) snprintf(namedlist, sizeof (namedlist), "%s",
1727c478bd9Sstevel@tonic-gate 		    DEFAULT_CONSOLE);
1737c478bd9Sstevel@tonic-gate 		cttyd = NODEV;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * The attempt to turn the controlling terminals dev_t into a string
1787c478bd9Sstevel@tonic-gate 	 * may not be successful, thus leaving the variable cttyname as a
1797c478bd9Sstevel@tonic-gate 	 * NULL.  This occurs if during boot we find
1807c478bd9Sstevel@tonic-gate 	 * the root partition (or some other partition)
1817c478bd9Sstevel@tonic-gate 	 * requires manual fsck, thus resulting in sulogin
1827c478bd9Sstevel@tonic-gate 	 * getting invoked.  The ioctl for CIOCTTYCONSOLE
1837c478bd9Sstevel@tonic-gate 	 * called above returned NODEV for cttyd
1847c478bd9Sstevel@tonic-gate 	 * in these cases.  NODEV gets returned when the vnode pointer
1857c478bd9Sstevel@tonic-gate 	 * in our session structure is NULL.  In these cases it
1867c478bd9Sstevel@tonic-gate 	 * must be assumed that the default console is used.
1877c478bd9Sstevel@tonic-gate 	 *
1887c478bd9Sstevel@tonic-gate 	 * See uts/common/os/session.c:cttydev().
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 	(void) strcpy(cttyname, DEFAULT_CONSOLE);
1917c478bd9Sstevel@tonic-gate 	(void) strcpy(scratchlist, namedlist);
1927c478bd9Sstevel@tonic-gate 	ptr = scratchlist;
1937c478bd9Sstevel@tonic-gate 	while (ptr != NULL) {
1947c478bd9Sstevel@tonic-gate 		p = strchr(ptr, ' ');
1957c478bd9Sstevel@tonic-gate 		if (p == NULL) {
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 		*p++ = '\0';
2037c478bd9Sstevel@tonic-gate 		if (stat(ptr, &st))
2047c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
2057c478bd9Sstevel@tonic-gate 		if (st.st_rdev == cttyd) {
2067c478bd9Sstevel@tonic-gate 			(void) strcpy(cttyname, ptr);
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 		ptr = p;
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * Use the same value of SLEEPTIME that login(1) uses.  This
2147c478bd9Sstevel@tonic-gate 	 * is obtained by reading the file /etc/default/login using
2157c478bd9Sstevel@tonic-gate 	 * the def*() functions.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (defopen(DEFAULT_LOGIN) == 0) {
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		/* ignore case */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		flags = defcntl(DC_GETFLAGS, 0);
2237c478bd9Sstevel@tonic-gate 		TURNOFF(flags, DC_CASE);
2247c478bd9Sstevel@tonic-gate 		(void) defcntl(DC_SETFLAGS, flags);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		if ((ptr = defread("SLEEPTIME=")) != NULL)
2277c478bd9Sstevel@tonic-gate 			sleeptime = atoi(ptr);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		if (sleeptime < 0 || sleeptime > SLEEPTIME_MAX)
2307c478bd9Sstevel@tonic-gate 			sleeptime = SLEEPTIME;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);	/* closes DEFAULT_LOGIN */
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * Use our own value of PASSREQ, separate from the one login(1) uses.
2377c478bd9Sstevel@tonic-gate 	 * This is obtained by reading the file /etc/default/sulogin using
2387c478bd9Sstevel@tonic-gate 	 * the def*() functions.
2397c478bd9Sstevel@tonic-gate 	 */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (defopen(DEFAULT_SULOGIN) == 0) {
2427c478bd9Sstevel@tonic-gate 		if ((ptr = defread("PASSREQ=")) != NULL)
2437c478bd9Sstevel@tonic-gate 			if (strcmp("NO", ptr) == 0)
2447c478bd9Sstevel@tonic-gate 				passreq = B_FALSE;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);	/* closes DEFAULT_SULOGIN */
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (passreq == B_FALSE)
2507c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * if no 'root' entry in /etc/shadow, give maint. mode single
2547c478bd9Sstevel@tonic-gate 	 * user shell prompt
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	setspent();
2577c478bd9Sstevel@tonic-gate 	if ((shpw = getspnam("root")) == NULL) {
2587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
2597c478bd9Sstevel@tonic-gate 		    "in shadow password file ***\n\n");
2607c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	endspent();
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * if no 'root' entry in /etc/passwd, give maint. mode single
2657c478bd9Sstevel@tonic-gate 	 * user shell prompt
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	setpwent();
2687c478bd9Sstevel@tonic-gate 	if (getpwnam("root") == NULL) {
2697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
2707c478bd9Sstevel@tonic-gate 		    "in password file ***\n\n");
2717c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	endpwent();
2747c478bd9Sstevel@tonic-gate 	/* process with controlling tty treated special */
2757c478bd9Sstevel@tonic-gate 	if ((pid = fork()) != (pid_t)0) {
2767c478bd9Sstevel@tonic-gate 		if (pid == -1)
2777c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
2787c478bd9Sstevel@tonic-gate 		else {
2797c478bd9Sstevel@tonic-gate 			setupsigs();
2807c478bd9Sstevel@tonic-gate 			masterpid = pid;
2817c478bd9Sstevel@tonic-gate 			originalpid = getpid();
2827c478bd9Sstevel@tonic-gate 			/*
2837c478bd9Sstevel@tonic-gate 			 * init() was invoked from a console that was not
2847c478bd9Sstevel@tonic-gate 			 * the default console, nor was it an auxiliary.
2857c478bd9Sstevel@tonic-gate 			 */
2867c478bd9Sstevel@tonic-gate 			if (cttyname[0] == NULL)
2877c478bd9Sstevel@tonic-gate 				termhandler(0);
2887c478bd9Sstevel@tonic-gate 				/* Never returns */
2897c478bd9Sstevel@tonic-gate 
29012f130f2Sgww 			main_loop(cttyname, B_TRUE);
2917c478bd9Sstevel@tonic-gate 			/* Never returns */
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	masterpid = getpid();
2957c478bd9Sstevel@tonic-gate 	originalpid = getppid();
2967c478bd9Sstevel@tonic-gate 	pidlist[nchild++] = originalpid;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	sa.sa_handler = childcleanup;
2997c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
3007c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3017c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
3027c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sa, NULL);
3037c478bd9Sstevel@tonic-gate 	sa.sa_handler = parenthandler;
3047c478bd9Sstevel@tonic-gate 	sa.sa_flags = SA_SIGINFO;
3057c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3067c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &sa, NULL);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
3097c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
3107c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3117c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &sa, NULL);
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * If there isn't a password on root, then don't permit
3147c478bd9Sstevel@tonic-gate 	 * the fanout capability of sulogin.
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 	if (*shpw->sp_pwdp != '\0') {
3177c478bd9Sstevel@tonic-gate 		ptr = namedlist;
3187c478bd9Sstevel@tonic-gate 		while (ptr != NULL) {
3197c478bd9Sstevel@tonic-gate 			p = strchr(ptr, ' ');
3207c478bd9Sstevel@tonic-gate 			if (p == NULL) {
32112f130f2Sgww 				doit(ptr, cttyname);
3227c478bd9Sstevel@tonic-gate 				break;
3237c478bd9Sstevel@tonic-gate 			}
3247c478bd9Sstevel@tonic-gate 			*p++ = '\0';
32512f130f2Sgww 			doit(ptr, cttyname);
3267c478bd9Sstevel@tonic-gate 			ptr = p;
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 	if (pathcmp(cttyname, DEFAULT_CONSOLE) != 0) {
3307c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
3317c478bd9Sstevel@tonic-gate 			setupsigs();
33212f130f2Sgww 			main_loop(DEFAULT_CONSOLE, B_FALSE);
3337c478bd9Sstevel@tonic-gate 		} else if (pid == -1)
3347c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
3357c478bd9Sstevel@tonic-gate 		pidlist[nchild++] = pid;
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * When parent is all done, it pauses until one of its children
3397c478bd9Sstevel@tonic-gate 	 * signals that its time to kill the underpriviledged.
3407c478bd9Sstevel@tonic-gate 	 */
3417c478bd9Sstevel@tonic-gate 	(void) wait(NULL);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	return (0);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * These flags are taken from stty's "sane" table entries in
3487c478bd9Sstevel@tonic-gate  * usr/src/cmd/ttymon/sttytable.c
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate #define	SET_IFLAG (BRKINT|IGNPAR|ISTRIP|ICRNL|IXON|IMAXBEL)
3517c478bd9Sstevel@tonic-gate #define	RESET_IFLAG (IGNBRK|PARMRK|INPCK|INLCR|IGNCR|IUCLC|IXOFF|IXANY)
3527c478bd9Sstevel@tonic-gate #define	SET_OFLAG (OPOST|ONLCR)
3537c478bd9Sstevel@tonic-gate #define	RESET_OFLAG (OLCUC|OCRNL|ONOCR|ONLRET|OFILL|OFDEL| \
3547c478bd9Sstevel@tonic-gate 	NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY)
3557c478bd9Sstevel@tonic-gate #define	SET_LFLAG (ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL)
3567c478bd9Sstevel@tonic-gate #define	RESET_LFLAG (XCASE|ECHONL|NOFLSH|STFLUSH|STWRAP|STAPPL)
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * Do the equivalent of 'stty sane' on the terminal since we don't know
3607c478bd9Sstevel@tonic-gate  * what state it was in on startup.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate static void
3637c478bd9Sstevel@tonic-gate sanitize_tty(int fd)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	(void) ioctl(fd, TCGETA, &ttymodes);
3667c478bd9Sstevel@tonic-gate 	ttymodes.c_iflag &= ~RESET_IFLAG;
3673289e1bdSnakanon 	ttymodes.c_iflag |= SET_IFLAG;
3687c478bd9Sstevel@tonic-gate 	ttymodes.c_oflag &= ~RESET_OFLAG;
3693289e1bdSnakanon 	ttymodes.c_oflag |= SET_OFLAG;
3707c478bd9Sstevel@tonic-gate 	ttymodes.c_lflag &= ~RESET_LFLAG;
3713289e1bdSnakanon 	ttymodes.c_lflag |= SET_LFLAG;
3727c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VERASE] = CERASE;
3737c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VKILL] = CKILL;
3747c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VQUIT] = CQUIT;
3757c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VINTR] = CINTR;
3767c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VEOF] = CEOF;
3777c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VEOL] = CNUL;
3787c478bd9Sstevel@tonic-gate 	(void) ioctl(fd, TCSETAF, &ttymodes);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * Fork a child of sulogin for each of the auxiliary consoles.
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate static void
38512f130f2Sgww doit(char *ptr, char *cttyname)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	pid_t	pid;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (pathcmp(ptr, DEFAULT_CONSOLE) != 0 &&
3907c478bd9Sstevel@tonic-gate 	    pathcmp(ptr, cttyname) != 0) {
3917c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
3927c478bd9Sstevel@tonic-gate 			setupsigs();
39312f130f2Sgww 			main_loop(ptr, B_FALSE);
3947c478bd9Sstevel@tonic-gate 		} else if (pid == -1)
3957c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
3967c478bd9Sstevel@tonic-gate 		pidlist[nchild++] = pid;
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate static int
4017c478bd9Sstevel@tonic-gate pathcmp(char *adev, char *bdev)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	struct stat	st1;
4047c478bd9Sstevel@tonic-gate 	struct stat	st2;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if (adev == NULL || bdev == NULL)
4077c478bd9Sstevel@tonic-gate 		return (1);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if (strcmp(adev, bdev) == 0)
4107c478bd9Sstevel@tonic-gate 		return (0);
4117c478bd9Sstevel@tonic-gate 
4124bc0a2efScasper 	if (stat(adev, &st1) || !S_ISCHR(st1.st_mode))
4137c478bd9Sstevel@tonic-gate 		return (1);
4147c478bd9Sstevel@tonic-gate 
4154bc0a2efScasper 	if (stat(bdev, &st2) || !S_ISCHR(st2.st_mode))
4167c478bd9Sstevel@tonic-gate 		return (1);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (st1.st_rdev == st2.st_rdev)
4197c478bd9Sstevel@tonic-gate 		return (0);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	return (1);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate /* Handlers for the children at initialization */
4257c478bd9Sstevel@tonic-gate static void
4267c478bd9Sstevel@tonic-gate setupsigs()
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	sa.sa_handler = noop;
4297c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
4307c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
4317c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, NULL);
4327c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sa, NULL);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	sa.sa_handler = termhandler;
4357c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
4367c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
4377c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
4387c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGKILL, &sa, NULL);
4397c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sa, NULL);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate static void
44312f130f2Sgww main_loop(char *devname, boolean_t cttyflag)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	int		fd, i;
44612f130f2Sgww 	char		*user = NULL;		/* authorized user */
4477c478bd9Sstevel@tonic-gate 	char		*pass;			/* password from user */
44812f130f2Sgww 	char		*cpass;			/* crypted password */
44912f130f2Sgww 	struct spwd	spwd;
45012f130f2Sgww 	struct spwd	*lshpw;			/* local shadow */
45112f130f2Sgww 	char		shadow[NSS_BUFLEN_SHADOW];
4527c478bd9Sstevel@tonic-gate 	FILE		*sysmsgfd;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++)
4557c478bd9Sstevel@tonic-gate 		(void) close(i);
4567c478bd9Sstevel@tonic-gate 	if (cttyflag == B_FALSE) {
4577c478bd9Sstevel@tonic-gate 		if (setsid() == -1)
4587c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	if ((fd = open(devname, O_RDWR)) < 0)
4617c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
462*aecfc01dSrui zang - Sun Microsystems - Beijing China 
463*aecfc01dSrui zang - Sun Microsystems - Beijing China 	/*
464*aecfc01dSrui zang - Sun Microsystems - Beijing China 	 * In system maintenance mode, all virtual console instances
465*aecfc01dSrui zang - Sun Microsystems - Beijing China 	 * of the svc:/system/console-login service are not available
466*aecfc01dSrui zang - Sun Microsystems - Beijing China 	 * any more, and only the system console is available. So here
467*aecfc01dSrui zang - Sun Microsystems - Beijing China 	 * we always switch to the system console in case at the moment
468*aecfc01dSrui zang - Sun Microsystems - Beijing China 	 * the active console isn't it.
469*aecfc01dSrui zang - Sun Microsystems - Beijing China 	 */
470*aecfc01dSrui zang - Sun Microsystems - Beijing China 	(void) ioctl(fd, VT_ACTIVATE, 1);
471*aecfc01dSrui zang - Sun Microsystems - Beijing China 
4727c478bd9Sstevel@tonic-gate 	if (fd != 0)
4737c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDIN_FILENO);
4747c478bd9Sstevel@tonic-gate 	if (fd != 1)
4757c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDOUT_FILENO);
4767c478bd9Sstevel@tonic-gate 	if (fd != 2)
4777c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDERR_FILENO);
4787c478bd9Sstevel@tonic-gate 	if (fd > 2)
4797c478bd9Sstevel@tonic-gate 		(void) close(fd);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	sysmsgfd = fopen("/dev/sysmsg", "w");
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	sanitize_tty(fileno(stdin));
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	for (;;) {
48612f130f2Sgww 		(void) printf("\nEnter user name for system maintenance "
48712f130f2Sgww 		    "(control-d to bypass): ");
48812f130f2Sgww 		if ((user = sulogin_getinput(devname, ECHOON)) == NULL) {
4897c478bd9Sstevel@tonic-gate 			/* signal other children to exit */
4907c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, masterpid, SIGUSR1);
4917c478bd9Sstevel@tonic-gate 			/* ^D, so straight to default init state */
4927c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4937c478bd9Sstevel@tonic-gate 		}
49412f130f2Sgww 		(void) printf("\nEnter %s password for system maintenance "
49512f130f2Sgww 		    "(control-d to bypass): ", user);
49612f130f2Sgww 
49712f130f2Sgww 		if ((pass = sulogin_getinput(devname, ECHOOFF)) == NULL) {
49812f130f2Sgww 			/* signal other children to exit */
4997c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, masterpid, SIGUSR1);
50012f130f2Sgww 			/* ^D, so straight to default init state */
50112f130f2Sgww 			free(user);
50212f130f2Sgww 			exit(EXIT_FAILURE);
50312f130f2Sgww 		}
50412f130f2Sgww 		lshpw = getspnam_r(user, &spwd, shadow, sizeof (shadow));
50512f130f2Sgww 		if (lshpw == NULL) {
5067c478bd9Sstevel@tonic-gate 			/*
50712f130f2Sgww 			 * the user entered doesn't exist, too bad.
50812f130f2Sgww 			 */
50912f130f2Sgww 			goto sorry;
51012f130f2Sgww 		}
51112f130f2Sgww 
51212f130f2Sgww 		/*
51312f130f2Sgww 		 * There is a special case error to catch here:
51412f130f2Sgww 		 * If the password is hashed with an algorithm
5157c478bd9Sstevel@tonic-gate 		 * other than the old unix crypt the call to crypt(3c)
5167c478bd9Sstevel@tonic-gate 		 * could fail if /usr is corrupt or not available
5177c478bd9Sstevel@tonic-gate 		 * since by default /etc/security/crypt.conf will
5187c478bd9Sstevel@tonic-gate 		 * have the crypt_ modules located under /usr/lib.
51912f130f2Sgww 		 * Or it could happen if /etc/security/crypt.conf
52012f130f2Sgww 		 * is corrupted.
5217c478bd9Sstevel@tonic-gate 		 *
5227c478bd9Sstevel@tonic-gate 		 * If this happens crypt(3c) will return NULL and
52312f130f2Sgww 		 * set errno to ELIBACC for the former condition or
52412f130f2Sgww 		 * EINVAL for the latter, in this case we bypass
52512f130f2Sgww 		 * authentication and just verify that the user is
52612f130f2Sgww 		 * authorized.
5277c478bd9Sstevel@tonic-gate 		 */
52812f130f2Sgww 
52912f130f2Sgww 		errno = 0;
53012f130f2Sgww 		cpass = crypt(pass, lshpw->sp_pwdp);
53112f130f2Sgww 		if (((cpass == NULL) && (lshpw->sp_pwdp[0] == '$')) &&
53212f130f2Sgww 		    ((errno == ELIBACC) || (errno == EINVAL))) {
53312f130f2Sgww 			goto checkauth;
53412f130f2Sgww 		} else if ((cpass == NULL) ||
53512f130f2Sgww 		    (strcmp(cpass, lshpw->sp_pwdp) != 0)) {
53612f130f2Sgww 			goto sorry;
53712f130f2Sgww 		}
53812f130f2Sgww 
53912f130f2Sgww checkauth:
54012f130f2Sgww 		/*
54112f130f2Sgww 		 * There is a special case error here as well.
54212f130f2Sgww 		 * If /etc/user_attr is corrupt, getusernam("root")
54312f130f2Sgww 		 * returns NULL.
54412f130f2Sgww 		 * In this case, we just give access because this is similar
54512f130f2Sgww 		 * to the case of root not existing in /etc/passwd.
54612f130f2Sgww 		 */
54712f130f2Sgww 
54812f130f2Sgww 		if ((getusernam("root") != NULL) &&
54912f130f2Sgww 		    (chkauthattr(MAINTENANCE_AUTH, user) != 1)) {
55012f130f2Sgww 			goto sorry;
55112f130f2Sgww 		}
55212f130f2Sgww 		(void) fprintf(sysmsgfd, "\nsingle-user privilege "
55312f130f2Sgww 		    "assigned to %s on %s.\n", user, devname);
5547c478bd9Sstevel@tonic-gate 		(void) sigsend(P_PID, masterpid, SIGUSR1);
5557c478bd9Sstevel@tonic-gate 		(void) wait(NULL);
55612f130f2Sgww 		free(user);
55712f130f2Sgww 		free(pass);
5587c478bd9Sstevel@tonic-gate 		single(su, devname);
55912f130f2Sgww 		/* single never returns */
56012f130f2Sgww 
56112f130f2Sgww sorry:
56212f130f2Sgww 		(void) printf("\nLogin incorrect or user %s not authorized\n",
56312f130f2Sgww 		    user);
56412f130f2Sgww 		free(user);
56512f130f2Sgww 		free(pass);
5667c478bd9Sstevel@tonic-gate 		(void) sleep(sleeptime);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * single() - exec shell for single user mode
5727c478bd9Sstevel@tonic-gate  */
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate static void
5757c478bd9Sstevel@tonic-gate single(const char *cmd, char *ttyn)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	struct utmpx	*u;
5787c478bd9Sstevel@tonic-gate 	char		found = B_FALSE;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (ttyn == NULL)
5817c478bd9Sstevel@tonic-gate 		ttyn = findttyname(STDIN_FILENO);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/*
5847c478bd9Sstevel@tonic-gate 	 * utmpx records on the console device are expected to be "console"
5857c478bd9Sstevel@tonic-gate 	 * by other processes, such as dtlogin.
5867c478bd9Sstevel@tonic-gate 	 */
5877c478bd9Sstevel@tonic-gate 	ttyn = stripttyname(ttyn);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/* update the utmpx file. */
5907c478bd9Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
5917c478bd9Sstevel@tonic-gate 		if (strcmp(u->ut_line, ttyn) == 0) {
5927c478bd9Sstevel@tonic-gate 			u->ut_tv.tv_sec = time(NULL);
5937c478bd9Sstevel@tonic-gate 			u->ut_type = USER_PROCESS;
5947c478bd9Sstevel@tonic-gate 			u->ut_pid = getpid();
5957c478bd9Sstevel@tonic-gate 			if (strcmp(u->ut_user, "root") != 0)
5967c478bd9Sstevel@tonic-gate 				(void) strcpy(u->ut_user, "root");
5977c478bd9Sstevel@tonic-gate 			(void) pututxline(u);
5987c478bd9Sstevel@tonic-gate 			found = B_TRUE;
5997c478bd9Sstevel@tonic-gate 			break;
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 	if (!found) {
6037c478bd9Sstevel@tonic-gate 		struct utmpx entryx;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		entryx.ut_tv.tv_sec = time(NULL);
6067c478bd9Sstevel@tonic-gate 		entryx.ut_type = USER_PROCESS;
6077c478bd9Sstevel@tonic-gate 		entryx.ut_pid = getpid();
6087c478bd9Sstevel@tonic-gate 		(void) strcpy(entryx.ut_user, "root");
6097c478bd9Sstevel@tonic-gate 		(void) strcpy(entryx.ut_line, ttyn);
6107c478bd9Sstevel@tonic-gate 		entryx.ut_tv.tv_usec = 0;
6117c478bd9Sstevel@tonic-gate 		entryx.ut_session = 0;
6127c478bd9Sstevel@tonic-gate 		entryx.ut_id[0] = 'c';
6137c478bd9Sstevel@tonic-gate 		entryx.ut_id[1] = 'o';
6147c478bd9Sstevel@tonic-gate 		entryx.ut_id[2] = 's';
6157c478bd9Sstevel@tonic-gate 		entryx.ut_id[3] = 'u';
6167c478bd9Sstevel@tonic-gate 		entryx.ut_syslen = 1;
6177c478bd9Sstevel@tonic-gate 		entryx.ut_host[0] = '\0';
6187c478bd9Sstevel@tonic-gate 		entryx.ut_exit.e_termination = WTERMSIG(0);
6197c478bd9Sstevel@tonic-gate 		entryx.ut_exit.e_exit = WEXITSTATUS(0);
6207c478bd9Sstevel@tonic-gate 		(void) pututxline(&entryx);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 	endutxent();
6237c478bd9Sstevel@tonic-gate 	(void) printf("Entering System Maintenance Mode\n\n");
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	if (execl(cmd, cmd, "-", (char *)0) < 0)
6267c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate /*
63012f130f2Sgww  * sulogin_getinput() - hacked from the standard PAM tty conversation
63112f130f2Sgww  *			function getpassphrase() library version
63212f130f2Sgww  *			so we can distinguish newline and EOF.
63312f130f2Sgww  *		        also don't need this routine to give a prompt.
6347c478bd9Sstevel@tonic-gate  *
6357c478bd9Sstevel@tonic-gate  * returns the password string, or NULL if the used typed EOF.
6367c478bd9Sstevel@tonic-gate  */
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate static char *
63912f130f2Sgww sulogin_getinput(char *devname, int echooff)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	struct termio	ttyb;
6427c478bd9Sstevel@tonic-gate 	int		c;
6437c478bd9Sstevel@tonic-gate 	FILE		*fi;
64412f130f2Sgww 	static char	input[PASS_MAX + 1];
6457c478bd9Sstevel@tonic-gate 	void		(*saved_handler)();
64612f130f2Sgww 	char		*rval = input;
6477c478bd9Sstevel@tonic-gate 	int		i = 0;
6487c478bd9Sstevel@tonic-gate 
64912f130f2Sgww 	if ((fi = fopen(devname, "r")) == NULL) {
6507c478bd9Sstevel@tonic-gate 		fi = stdin;
65112f130f2Sgww 	}
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	saved_handler = signal(SIGINT, SIG_IGN);
6547c478bd9Sstevel@tonic-gate 
65512f130f2Sgww 	if (echooff) {
6567c478bd9Sstevel@tonic-gate 		ttyb = ttymodes;
6577c478bd9Sstevel@tonic-gate 		ttyb.c_lflag &= ~(ECHO | ECHOE | ECHONL);
6587c478bd9Sstevel@tonic-gate 		(void) ioctl(fileno(fi), TCSETAF, &ttyb);
65912f130f2Sgww 	}
6607c478bd9Sstevel@tonic-gate 
66112f130f2Sgww 	/* get characters up to PASS_MAX, but don't overflow */
66212f130f2Sgww 	while ((c = getc(fi)) != '\n' && (c != '\r')) {
66312f130f2Sgww 		if (c == EOF && i == 0) {	/* ^D, no input */
6647c478bd9Sstevel@tonic-gate 			rval = NULL;
6657c478bd9Sstevel@tonic-gate 			break;
6667c478bd9Sstevel@tonic-gate 		}
66712f130f2Sgww 		if (i < PASS_MAX) {
66812f130f2Sgww 			input[i++] = (char)c;
6697c478bd9Sstevel@tonic-gate 		}
67012f130f2Sgww 	}
67112f130f2Sgww 	input[i] = '\0';
6727c478bd9Sstevel@tonic-gate 	(void) fputc('\n', fi);
67312f130f2Sgww 	if (echooff) {
6747c478bd9Sstevel@tonic-gate 		(void) ioctl(fileno(fi), TCSETAW, &ttymodes);
67512f130f2Sgww 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (saved_handler != SIG_ERR)
6787c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, saved_handler);
67912f130f2Sgww 	return (rval == NULL ? NULL : strdup(rval));
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate static char *
6837c478bd9Sstevel@tonic-gate findttyname(int fd)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	char	*ttyn = ttyname(fd);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if (ttyn == NULL)
6887c478bd9Sstevel@tonic-gate 		ttyn = "/dev/???";
6897c478bd9Sstevel@tonic-gate 	else {
6907c478bd9Sstevel@tonic-gate 		/*
6917c478bd9Sstevel@tonic-gate 		 * /dev/syscon and /dev/systty are usually links to
6927c478bd9Sstevel@tonic-gate 		 * /dev/console.  prefer /dev/console.
6937c478bd9Sstevel@tonic-gate 		 */
6947c478bd9Sstevel@tonic-gate 		if (((strcmp(ttyn, "/dev/syscon") == 0) ||
6957c478bd9Sstevel@tonic-gate 		    (strcmp(ttyn, "/dev/systty") == 0)) &&
6967c478bd9Sstevel@tonic-gate 		    access("/dev/console", F_OK))
6977c478bd9Sstevel@tonic-gate 			ttyn = "/dev/console";
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 	return (ttyn);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate static char *
7037c478bd9Sstevel@tonic-gate stripttyname(char *ttyn)
7047c478bd9Sstevel@tonic-gate {
7057c478bd9Sstevel@tonic-gate 	/* saw off the /dev/ */
7067c478bd9Sstevel@tonic-gate 	if (strncmp(ttyn, "/dev/", sizeof ("/dev/") -1) == 0)
7077c478bd9Sstevel@tonic-gate 		return (ttyn + sizeof ("/dev/") - 1);
7087c478bd9Sstevel@tonic-gate 	else
7097c478bd9Sstevel@tonic-gate 		return (ttyn);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /* ARGSUSED */
7147c478bd9Sstevel@tonic-gate static	void
7157c478bd9Sstevel@tonic-gate noop(int sig)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate 	/*
7187c478bd9Sstevel@tonic-gate 	 * This signal handler does nothing except return.  We use it
7197c478bd9Sstevel@tonic-gate 	 * as the signal disposition in this program instead of
7207c478bd9Sstevel@tonic-gate 	 * SIG_IGN so that we do not have to restore the disposition
7217c478bd9Sstevel@tonic-gate 	 * back to SIG_DFL. Instead we allow exec(2) to set the
7227c478bd9Sstevel@tonic-gate 	 * dispostion to SIG_DFL to avoid a race condition.
7237c478bd9Sstevel@tonic-gate 	 */
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate /* ARGSUSED */
7277c478bd9Sstevel@tonic-gate static void
7287c478bd9Sstevel@tonic-gate parenthandler(int sig, siginfo_t *si, ucontext_t *uc)
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	int i;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/*
7337c478bd9Sstevel@tonic-gate 	 * We get here if someone has successfully entered a password
7347c478bd9Sstevel@tonic-gate 	 * from the auxiliary console and is getting the single-user shell.
7357c478bd9Sstevel@tonic-gate 	 * When this happens, the parent needs to kill the children
7367c478bd9Sstevel@tonic-gate 	 * that didn't get the shell.
7377c478bd9Sstevel@tonic-gate 	 *
7387c478bd9Sstevel@tonic-gate 	 */
7397c478bd9Sstevel@tonic-gate 	for (i = 0; i < nchild; i++) {
7407c478bd9Sstevel@tonic-gate 		if (pidlist[i] != si->__data.__proc.__pid)
7417c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, pidlist[i], SIGTERM);
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
7447c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
7457c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
7467c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, NULL);
7477c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sa, NULL);
7487c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
7497c478bd9Sstevel@tonic-gate 	(void) wait(NULL);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * The master pid will get SIGTERM or SIGHUP from init, and then
7547c478bd9Sstevel@tonic-gate  * has to make sure the shell isn't still running.
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate /* ARGSUSED */
7587c478bd9Sstevel@tonic-gate static	void
7597c478bd9Sstevel@tonic-gate childcleanup(int sig)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	int i;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/* Only need to kill the child that became the shell. */
7647c478bd9Sstevel@tonic-gate 	for (i = 0; i < nchild; i++) {
7657c478bd9Sstevel@tonic-gate 		/* Don't kill gramps before his time */
7667c478bd9Sstevel@tonic-gate 		if (pidlist[i] != getppid())
7677c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, pidlist[i], SIGHUP);
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate /* ARGSUSED */
7727c478bd9Sstevel@tonic-gate static	void
7737c478bd9Sstevel@tonic-gate termhandler(int sig)
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	FILE *fi;
7767c478bd9Sstevel@tonic-gate 	pid_t pid;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	/* Processes come here when they fail to receive the password. */
7797c478bd9Sstevel@tonic-gate 	if ((fi = fopen("/dev/tty", "r+")) == NULL)
7807c478bd9Sstevel@tonic-gate 		fi = stdin;
7817c478bd9Sstevel@tonic-gate 	else
7827c478bd9Sstevel@tonic-gate 		setbuf(fi, NULL);
7837c478bd9Sstevel@tonic-gate 	sanitize_tty(fileno(fi));
7847c478bd9Sstevel@tonic-gate 	/* If you're the controlling tty, then just wait */
7857c478bd9Sstevel@tonic-gate 	pid = getpid();
7867c478bd9Sstevel@tonic-gate 	if (pid == originalpid || pid == masterpid) {
7877c478bd9Sstevel@tonic-gate 		sa.sa_handler = SIG_IGN;
7887c478bd9Sstevel@tonic-gate 		sa.sa_flags = 0;
7897c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&sa.sa_mask);
7907c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGINT, &sa, NULL);
7917c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGQUIT, &sa, NULL);
7927c478bd9Sstevel@tonic-gate 		sa.sa_handler = SIG_DFL;
7937c478bd9Sstevel@tonic-gate 		sa.sa_flags = 0;
7947c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&sa.sa_mask);
7957c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGTERM, &sa, NULL);
7967c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGHUP, &sa, NULL);
7977c478bd9Sstevel@tonic-gate 		(void) wait(NULL);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 	exit(0);
8007c478bd9Sstevel@tonic-gate }
801