xref: /illumos-gate/usr/src/cmd/sulogin/sulogin.c (revision f1ecae5b)
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  */
21be468fc6SAlexander Eremin 
22be468fc6SAlexander Eremin /*
23be468fc6SAlexander Eremin  * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
2448bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
25be468fc6SAlexander Eremin  */
26be468fc6SAlexander Eremin 
277c478bd9Sstevel@tonic-gate /*
2801ef659dSJoep Vesseur  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
297c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
347c478bd9Sstevel@tonic-gate  *	All rights reserved.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  *	Copyright (c) 1987, 1988 Microsoft Corporation.
377c478bd9Sstevel@tonic-gate  *	All rights reserved.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  *	sulogin - special login program exec'd from init to let user
427c478bd9Sstevel@tonic-gate  *	come up single user, or go to default init state straight away.
437c478bd9Sstevel@tonic-gate  *
4412f130f2Sgww  *	Explain the scoop to the user, prompt for an authorized user
4512f130f2Sgww  *	name or ^D and then prompt for password or ^D.  If the password
4612f130f2Sgww  *	is correct, check if the user is authorized, if so enter
4712f130f2Sgww  *	single user. ^D exits sulogin, and init will go to default init state.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  *	If /etc/passwd is missing, or there's no entry for root,
507c478bd9Sstevel@tonic-gate  *	go single user, no questions asked.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <sys/types.h>
547c478bd9Sstevel@tonic-gate #include <sys/stat.h>
557c478bd9Sstevel@tonic-gate #include <sys/param.h>
567c478bd9Sstevel@tonic-gate #include <sys/sysmsg_impl.h>
577c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
587c478bd9Sstevel@tonic-gate #include <sys/resource.h>
597c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
607c478bd9Sstevel@tonic-gate #include <sys/wait.h>
617c478bd9Sstevel@tonic-gate #include <sys/stermio.h>
627c478bd9Sstevel@tonic-gate #include <fcntl.h>
637c478bd9Sstevel@tonic-gate #include <termio.h>
647c478bd9Sstevel@tonic-gate #include <pwd.h>
657c478bd9Sstevel@tonic-gate #include <shadow.h>
667c478bd9Sstevel@tonic-gate #include <stdlib.h>
677c478bd9Sstevel@tonic-gate #include <stdio.h>
687c478bd9Sstevel@tonic-gate #include <signal.h>
697c478bd9Sstevel@tonic-gate #include <siginfo.h>
707c478bd9Sstevel@tonic-gate #include <utmpx.h>
717c478bd9Sstevel@tonic-gate #include <unistd.h>
727c478bd9Sstevel@tonic-gate #include <ucontext.h>
737c478bd9Sstevel@tonic-gate #include <string.h>
747c478bd9Sstevel@tonic-gate #include <strings.h>
757c478bd9Sstevel@tonic-gate #include <deflt.h>
767c478bd9Sstevel@tonic-gate #include <limits.h>
777c478bd9Sstevel@tonic-gate #include <errno.h>
787c478bd9Sstevel@tonic-gate #include <crypt.h>
7912f130f2Sgww #include <auth_attr.h>
8012f130f2Sgww #include <auth_list.h>
8112f130f2Sgww #include <nss_dbdefs.h>
8212f130f2Sgww #include <user_attr.h>
83aecfc01dSrui zang - Sun Microsystems - Beijing China #include <sys/vt.h>
84be468fc6SAlexander Eremin #include <sys/kd.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Intervals to sleep after failed login
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate #ifndef SLEEPTIME
907c478bd9Sstevel@tonic-gate #define	SLEEPTIME	4	/* sleeptime before login incorrect msg */
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	SLEEPTIME_MAX	5	/* maximum sleeptime */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  *	the name of the file containing the login defaults we deliberately
977c478bd9Sstevel@tonic-gate  *	use the same file as login(1)
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #define	DEFAULT_LOGIN	"/etc/default/login"
1017c478bd9Sstevel@tonic-gate #define	DEFAULT_SULOGIN	"/etc/default/sulogin"
1027c478bd9Sstevel@tonic-gate #define	DEFAULT_CONSOLE	"/dev/console"
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static char	shell[]	= "/sbin/sh";
1057c478bd9Sstevel@tonic-gate static char	su[]	= "/sbin/su.static";
1067c478bd9Sstevel@tonic-gate static int	sleeptime	= SLEEPTIME;
1077c478bd9Sstevel@tonic-gate static int	nchild = 0;
1087c478bd9Sstevel@tonic-gate static pid_t	pidlist[10];
1097c478bd9Sstevel@tonic-gate static pid_t	masterpid = 0;
1107c478bd9Sstevel@tonic-gate static pid_t	originalpid = 0;
1117c478bd9Sstevel@tonic-gate static struct sigaction	sa;
1127c478bd9Sstevel@tonic-gate static struct termio	ttymodes;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static char	*findttyname(int fd);
1157c478bd9Sstevel@tonic-gate static char	*stripttyname(char *);
11612f130f2Sgww static char	*sulogin_getinput(char *, int);
1177c478bd9Sstevel@tonic-gate static void	noop(int);
1187c478bd9Sstevel@tonic-gate static void	single(const char *, char *);
11912f130f2Sgww static void	main_loop(char *, boolean_t);
1207c478bd9Sstevel@tonic-gate static void	parenthandler();
1217c478bd9Sstevel@tonic-gate static void	termhandler(int);
1227c478bd9Sstevel@tonic-gate static void	setupsigs(void);
1237c478bd9Sstevel@tonic-gate static int	pathcmp(char *, char *);
12412f130f2Sgww static void	doit(char *, char *);
1257c478bd9Sstevel@tonic-gate static void	childcleanup(int);
1267c478bd9Sstevel@tonic-gate 
12712f130f2Sgww #define	ECHOON	0
12812f130f2Sgww #define	ECHOOFF	1
12912f130f2Sgww 
1307c478bd9Sstevel@tonic-gate /* ARGSUSED */
1317c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1327c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	struct spwd	*shpw;
1357c478bd9Sstevel@tonic-gate 	int		passreq = B_TRUE;
1367c478bd9Sstevel@tonic-gate 	int		flags;
1377c478bd9Sstevel@tonic-gate 	int		fd;
1387c478bd9Sstevel@tonic-gate 	char		*infop, *ptr, *p;
1397c478bd9Sstevel@tonic-gate 	pid_t		pid;
1407c478bd9Sstevel@tonic-gate 	int		bufsize;
1417c478bd9Sstevel@tonic-gate 	struct stat	st;
1427c478bd9Sstevel@tonic-gate 	char		cttyname[100];
1437c478bd9Sstevel@tonic-gate 	char		namedlist[500];
1447c478bd9Sstevel@tonic-gate 	char		scratchlist[500];
1457c478bd9Sstevel@tonic-gate 	dev_t		cttyd;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
1487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: must be root\n", argv[0]);
1497c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/* Do the magic to determine the children */
1537c478bd9Sstevel@tonic-gate 	if ((fd = open(SYSMSG, 0)) < 0)
1547c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * If the console supports the CIOCTTYCONSOLE ioctl, then fetch
1587c478bd9Sstevel@tonic-gate 	 * its console device list.  If not, then we use the default
1597c478bd9Sstevel@tonic-gate 	 * console name.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CIOCTTYCONSOLE, &cttyd) == 0) {
1627c478bd9Sstevel@tonic-gate 		if ((bufsize = ioctl(fd, CIOCGETCONSOLE, NULL)) < 0)
1637c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		if (bufsize > 0) {
1667c478bd9Sstevel@tonic-gate 			if ((infop = calloc(bufsize, sizeof (char))) == NULL)
1677c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 			if (ioctl(fd, CIOCGETCONSOLE, infop) < 0)
1707c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 			(void) snprintf(namedlist, sizeof (namedlist), "%s %s",
1737c478bd9Sstevel@tonic-gate 			    DEFAULT_CONSOLE, infop);
1747c478bd9Sstevel@tonic-gate 		} else
1757c478bd9Sstevel@tonic-gate 			(void) snprintf(namedlist, sizeof (namedlist), "%s",
1767c478bd9Sstevel@tonic-gate 			    DEFAULT_CONSOLE);
1777c478bd9Sstevel@tonic-gate 	} else {
1787c478bd9Sstevel@tonic-gate 		(void) snprintf(namedlist, sizeof (namedlist), "%s",
1797c478bd9Sstevel@tonic-gate 		    DEFAULT_CONSOLE);
1807c478bd9Sstevel@tonic-gate 		cttyd = NODEV;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * The attempt to turn the controlling terminals dev_t into a string
1857c478bd9Sstevel@tonic-gate 	 * may not be successful, thus leaving the variable cttyname as a
1867c478bd9Sstevel@tonic-gate 	 * NULL.  This occurs if during boot we find
1877c478bd9Sstevel@tonic-gate 	 * the root partition (or some other partition)
1887c478bd9Sstevel@tonic-gate 	 * requires manual fsck, thus resulting in sulogin
1897c478bd9Sstevel@tonic-gate 	 * getting invoked.  The ioctl for CIOCTTYCONSOLE
1907c478bd9Sstevel@tonic-gate 	 * called above returned NODEV for cttyd
1917c478bd9Sstevel@tonic-gate 	 * in these cases.  NODEV gets returned when the vnode pointer
1927c478bd9Sstevel@tonic-gate 	 * in our session structure is NULL.  In these cases it
1937c478bd9Sstevel@tonic-gate 	 * must be assumed that the default console is used.
1947c478bd9Sstevel@tonic-gate 	 *
1957c478bd9Sstevel@tonic-gate 	 * See uts/common/os/session.c:cttydev().
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	(void) strcpy(cttyname, DEFAULT_CONSOLE);
1987c478bd9Sstevel@tonic-gate 	(void) strcpy(scratchlist, namedlist);
1997c478bd9Sstevel@tonic-gate 	ptr = scratchlist;
2007c478bd9Sstevel@tonic-gate 	while (ptr != NULL) {
2017c478bd9Sstevel@tonic-gate 		p = strchr(ptr, ' ');
2027c478bd9Sstevel@tonic-gate 		if (p == NULL) {
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 		*p++ = '\0';
2107c478bd9Sstevel@tonic-gate 		if (stat(ptr, &st))
2117c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
2127c478bd9Sstevel@tonic-gate 		if (st.st_rdev == cttyd) {
2137c478bd9Sstevel@tonic-gate 			(void) strcpy(cttyname, ptr);
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 		ptr = p;
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * Use the same value of SLEEPTIME that login(1) uses.  This
2217c478bd9Sstevel@tonic-gate 	 * is obtained by reading the file /etc/default/login using
2227c478bd9Sstevel@tonic-gate 	 * the def*() functions.
2237c478bd9Sstevel@tonic-gate 	 */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (defopen(DEFAULT_LOGIN) == 0) {
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		/* ignore case */
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		flags = defcntl(DC_GETFLAGS, 0);
2307c478bd9Sstevel@tonic-gate 		TURNOFF(flags, DC_CASE);
2317c478bd9Sstevel@tonic-gate 		(void) defcntl(DC_SETFLAGS, flags);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		if ((ptr = defread("SLEEPTIME=")) != NULL)
2347c478bd9Sstevel@tonic-gate 			sleeptime = atoi(ptr);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		if (sleeptime < 0 || sleeptime > SLEEPTIME_MAX)
2377c478bd9Sstevel@tonic-gate 			sleeptime = SLEEPTIME;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);	/* closes DEFAULT_LOGIN */
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * Use our own value of PASSREQ, separate from the one login(1) uses.
2447c478bd9Sstevel@tonic-gate 	 * This is obtained by reading the file /etc/default/sulogin using
2457c478bd9Sstevel@tonic-gate 	 * the def*() functions.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (defopen(DEFAULT_SULOGIN) == 0) {
2497c478bd9Sstevel@tonic-gate 		if ((ptr = defread("PASSREQ=")) != NULL)
2507c478bd9Sstevel@tonic-gate 			if (strcmp("NO", ptr) == 0)
2517c478bd9Sstevel@tonic-gate 				passreq = B_FALSE;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);	/* closes DEFAULT_SULOGIN */
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (passreq == B_FALSE)
2577c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/*
2607c478bd9Sstevel@tonic-gate 	 * if no 'root' entry in /etc/shadow, give maint. mode single
2617c478bd9Sstevel@tonic-gate 	 * user shell prompt
2627c478bd9Sstevel@tonic-gate 	 */
2637c478bd9Sstevel@tonic-gate 	setspent();
2647c478bd9Sstevel@tonic-gate 	if ((shpw = getspnam("root")) == NULL) {
2657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
2667c478bd9Sstevel@tonic-gate 		    "in shadow password file ***\n\n");
2677c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	endspent();
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * if no 'root' entry in /etc/passwd, give maint. mode single
2727c478bd9Sstevel@tonic-gate 	 * user shell prompt
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	setpwent();
2757c478bd9Sstevel@tonic-gate 	if (getpwnam("root") == NULL) {
2767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
2777c478bd9Sstevel@tonic-gate 		    "in password file ***\n\n");
2787c478bd9Sstevel@tonic-gate 		single(shell, NULL);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	endpwent();
2817c478bd9Sstevel@tonic-gate 	/* process with controlling tty treated special */
2827c478bd9Sstevel@tonic-gate 	if ((pid = fork()) != (pid_t)0) {
2837c478bd9Sstevel@tonic-gate 		if (pid == -1)
2847c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
2857c478bd9Sstevel@tonic-gate 		else {
2867c478bd9Sstevel@tonic-gate 			setupsigs();
2877c478bd9Sstevel@tonic-gate 			masterpid = pid;
2887c478bd9Sstevel@tonic-gate 			originalpid = getpid();
2897c478bd9Sstevel@tonic-gate 			/*
2907c478bd9Sstevel@tonic-gate 			 * init() was invoked from a console that was not
2917c478bd9Sstevel@tonic-gate 			 * the default console, nor was it an auxiliary.
2927c478bd9Sstevel@tonic-gate 			 */
293*f1ecae5bSToomas Soome 			if (cttyname[0] == '\0')
2947c478bd9Sstevel@tonic-gate 				termhandler(0);
2957c478bd9Sstevel@tonic-gate 				/* Never returns */
2967c478bd9Sstevel@tonic-gate 
29712f130f2Sgww 			main_loop(cttyname, B_TRUE);
2987c478bd9Sstevel@tonic-gate 			/* Never returns */
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 	masterpid = getpid();
3027c478bd9Sstevel@tonic-gate 	originalpid = getppid();
3037c478bd9Sstevel@tonic-gate 	pidlist[nchild++] = originalpid;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	sa.sa_handler = childcleanup;
3067c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
3077c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3087c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
3097c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sa, NULL);
3107c478bd9Sstevel@tonic-gate 	sa.sa_handler = parenthandler;
3117c478bd9Sstevel@tonic-gate 	sa.sa_flags = SA_SIGINFO;
3127c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3137c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &sa, NULL);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
3167c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
3177c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
3187c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &sa, NULL);
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * If there isn't a password on root, then don't permit
3217c478bd9Sstevel@tonic-gate 	 * the fanout capability of sulogin.
3227c478bd9Sstevel@tonic-gate 	 */
3237c478bd9Sstevel@tonic-gate 	if (*shpw->sp_pwdp != '\0') {
3247c478bd9Sstevel@tonic-gate 		ptr = namedlist;
3257c478bd9Sstevel@tonic-gate 		while (ptr != NULL) {
3267c478bd9Sstevel@tonic-gate 			p = strchr(ptr, ' ');
3277c478bd9Sstevel@tonic-gate 			if (p == NULL) {
32812f130f2Sgww 				doit(ptr, cttyname);
3297c478bd9Sstevel@tonic-gate 				break;
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate 			*p++ = '\0';
33212f130f2Sgww 			doit(ptr, cttyname);
3337c478bd9Sstevel@tonic-gate 			ptr = p;
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	if (pathcmp(cttyname, DEFAULT_CONSOLE) != 0) {
3377c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
3387c478bd9Sstevel@tonic-gate 			setupsigs();
33912f130f2Sgww 			main_loop(DEFAULT_CONSOLE, B_FALSE);
3407c478bd9Sstevel@tonic-gate 		} else if (pid == -1)
3417c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
3427c478bd9Sstevel@tonic-gate 		pidlist[nchild++] = pid;
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * When parent is all done, it pauses until one of its children
3467c478bd9Sstevel@tonic-gate 	 * signals that its time to kill the underpriviledged.
3477c478bd9Sstevel@tonic-gate 	 */
3487c478bd9Sstevel@tonic-gate 	(void) wait(NULL);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	return (0);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * These flags are taken from stty's "sane" table entries in
3557c478bd9Sstevel@tonic-gate  * usr/src/cmd/ttymon/sttytable.c
3567c478bd9Sstevel@tonic-gate  */
3577c478bd9Sstevel@tonic-gate #define	SET_IFLAG (BRKINT|IGNPAR|ISTRIP|ICRNL|IXON|IMAXBEL)
3587c478bd9Sstevel@tonic-gate #define	RESET_IFLAG (IGNBRK|PARMRK|INPCK|INLCR|IGNCR|IUCLC|IXOFF|IXANY)
3597c478bd9Sstevel@tonic-gate #define	SET_OFLAG (OPOST|ONLCR)
3607c478bd9Sstevel@tonic-gate #define	RESET_OFLAG (OLCUC|OCRNL|ONOCR|ONLRET|OFILL|OFDEL| \
3617c478bd9Sstevel@tonic-gate 	NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY)
3627c478bd9Sstevel@tonic-gate #define	SET_LFLAG (ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL)
3637c478bd9Sstevel@tonic-gate #define	RESET_LFLAG (XCASE|ECHONL|NOFLSH|STFLUSH|STWRAP|STAPPL)
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * Do the equivalent of 'stty sane' on the terminal since we don't know
3677c478bd9Sstevel@tonic-gate  * what state it was in on startup.
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate static void
sanitize_tty(int fd)3707c478bd9Sstevel@tonic-gate sanitize_tty(int fd)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	(void) ioctl(fd, TCGETA, &ttymodes);
3737c478bd9Sstevel@tonic-gate 	ttymodes.c_iflag &= ~RESET_IFLAG;
3743289e1bdSnakanon 	ttymodes.c_iflag |= SET_IFLAG;
3757c478bd9Sstevel@tonic-gate 	ttymodes.c_oflag &= ~RESET_OFLAG;
3763289e1bdSnakanon 	ttymodes.c_oflag |= SET_OFLAG;
3777c478bd9Sstevel@tonic-gate 	ttymodes.c_lflag &= ~RESET_LFLAG;
3783289e1bdSnakanon 	ttymodes.c_lflag |= SET_LFLAG;
3797c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VERASE] = CERASE;
3807c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VKILL] = CKILL;
3817c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VQUIT] = CQUIT;
3827c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VINTR] = CINTR;
3837c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VEOF] = CEOF;
3847c478bd9Sstevel@tonic-gate 	ttymodes.c_cc[VEOL] = CNUL;
3857c478bd9Sstevel@tonic-gate 	(void) ioctl(fd, TCSETAF, &ttymodes);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * Fork a child of sulogin for each of the auxiliary consoles.
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate static void
doit(char * ptr,char * cttyname)39212f130f2Sgww doit(char *ptr, char *cttyname)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	pid_t	pid;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (pathcmp(ptr, DEFAULT_CONSOLE) != 0 &&
3977c478bd9Sstevel@tonic-gate 	    pathcmp(ptr, cttyname) != 0) {
3987c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
3997c478bd9Sstevel@tonic-gate 			setupsigs();
40012f130f2Sgww 			main_loop(ptr, B_FALSE);
4017c478bd9Sstevel@tonic-gate 		} else if (pid == -1)
4027c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4037c478bd9Sstevel@tonic-gate 		pidlist[nchild++] = pid;
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate static int
pathcmp(char * adev,char * bdev)4087c478bd9Sstevel@tonic-gate pathcmp(char *adev, char *bdev)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	struct stat	st1;
4117c478bd9Sstevel@tonic-gate 	struct stat	st2;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	if (adev == NULL || bdev == NULL)
4147c478bd9Sstevel@tonic-gate 		return (1);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (strcmp(adev, bdev) == 0)
4177c478bd9Sstevel@tonic-gate 		return (0);
4187c478bd9Sstevel@tonic-gate 
4194bc0a2efScasper 	if (stat(adev, &st1) || !S_ISCHR(st1.st_mode))
4207c478bd9Sstevel@tonic-gate 		return (1);
4217c478bd9Sstevel@tonic-gate 
4224bc0a2efScasper 	if (stat(bdev, &st2) || !S_ISCHR(st2.st_mode))
4237c478bd9Sstevel@tonic-gate 		return (1);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (st1.st_rdev == st2.st_rdev)
4267c478bd9Sstevel@tonic-gate 		return (0);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	return (1);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /* Handlers for the children at initialization */
4327c478bd9Sstevel@tonic-gate static void
setupsigs()4337c478bd9Sstevel@tonic-gate setupsigs()
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate 	sa.sa_handler = noop;
4367c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
4377c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
4387c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, NULL);
4397c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sa, NULL);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	sa.sa_handler = termhandler;
4427c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
4437c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
4447c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
4457c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGKILL, &sa, NULL);
4467c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sa, NULL);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate static void
main_loop(char * devname,boolean_t cttyflag)45012f130f2Sgww main_loop(char *devname, boolean_t cttyflag)
4517c478bd9Sstevel@tonic-gate {
452be468fc6SAlexander Eremin 	int		fd, fb, i;
45312f130f2Sgww 	char		*user = NULL;		/* authorized user */
4547c478bd9Sstevel@tonic-gate 	char		*pass;			/* password from user */
45512f130f2Sgww 	char		*cpass;			/* crypted password */
45612f130f2Sgww 	struct spwd	spwd;
45712f130f2Sgww 	struct spwd	*lshpw;			/* local shadow */
45812f130f2Sgww 	char		shadow[NSS_BUFLEN_SHADOW];
4597c478bd9Sstevel@tonic-gate 	FILE		*sysmsgfd;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++)
4627c478bd9Sstevel@tonic-gate 		(void) close(i);
4637c478bd9Sstevel@tonic-gate 	if (cttyflag == B_FALSE) {
4647c478bd9Sstevel@tonic-gate 		if (setsid() == -1)
4657c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 	if ((fd = open(devname, O_RDWR)) < 0)
4687c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
469aecfc01dSrui zang - Sun Microsystems - Beijing China 
470aecfc01dSrui zang - Sun Microsystems - Beijing China 	/*
471aecfc01dSrui zang - Sun Microsystems - Beijing China 	 * In system maintenance mode, all virtual console instances
472aecfc01dSrui zang - Sun Microsystems - Beijing China 	 * of the svc:/system/console-login service are not available
473aecfc01dSrui zang - Sun Microsystems - Beijing China 	 * any more, and only the system console is available. So here
474aecfc01dSrui zang - Sun Microsystems - Beijing China 	 * we always switch to the system console in case at the moment
475aecfc01dSrui zang - Sun Microsystems - Beijing China 	 * the active console isn't it.
476aecfc01dSrui zang - Sun Microsystems - Beijing China 	 */
477aecfc01dSrui zang - Sun Microsystems - Beijing China 	(void) ioctl(fd, VT_ACTIVATE, 1);
478aecfc01dSrui zang - Sun Microsystems - Beijing China 
4797c478bd9Sstevel@tonic-gate 	if (fd != 0)
4807c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDIN_FILENO);
4817c478bd9Sstevel@tonic-gate 	if (fd != 1)
4827c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDOUT_FILENO);
4837c478bd9Sstevel@tonic-gate 	if (fd != 2)
4847c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDERR_FILENO);
4857c478bd9Sstevel@tonic-gate 	if (fd > 2)
4867c478bd9Sstevel@tonic-gate 		(void) close(fd);
4877c478bd9Sstevel@tonic-gate 
488be468fc6SAlexander Eremin 	/* Stop progress bar and reset console mode to text */
489be468fc6SAlexander Eremin 	if ((fb = open("/dev/fb", O_RDONLY)) >= 0) {
490be468fc6SAlexander Eremin 		(void) ioctl(fb, KDSETMODE, KD_RESETTEXT);
491be468fc6SAlexander Eremin 		(void) close(fb);
492be468fc6SAlexander Eremin 	}
493be468fc6SAlexander Eremin 
4947c478bd9Sstevel@tonic-gate 	sysmsgfd = fopen("/dev/sysmsg", "w");
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	sanitize_tty(fileno(stdin));
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	for (;;) {
49901ef659dSJoep Vesseur 		do {
50001ef659dSJoep Vesseur 			(void) printf("\nEnter user name for system "
50101ef659dSJoep Vesseur 			    "maintenance (control-d to bypass): ");
50201ef659dSJoep Vesseur 			user = sulogin_getinput(devname, ECHOON);
50301ef659dSJoep Vesseur 			if (user == NULL) {
50401ef659dSJoep Vesseur 				/* signal other children to exit */
50501ef659dSJoep Vesseur 				(void) sigsend(P_PID, masterpid, SIGUSR1);
50601ef659dSJoep Vesseur 				/* ^D, so straight to default init state */
50701ef659dSJoep Vesseur 				exit(EXIT_FAILURE);
50801ef659dSJoep Vesseur 			}
50901ef659dSJoep Vesseur 		} while (user[0] == '\0');
51001ef659dSJoep Vesseur 		(void) printf("Enter %s password (control-d to bypass): ",
51101ef659dSJoep Vesseur 		    user);
51212f130f2Sgww 
51312f130f2Sgww 		if ((pass = sulogin_getinput(devname, ECHOOFF)) == NULL) {
51412f130f2Sgww 			/* signal other children to exit */
5157c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, masterpid, SIGUSR1);
51612f130f2Sgww 			/* ^D, so straight to default init state */
51712f130f2Sgww 			free(user);
51812f130f2Sgww 			exit(EXIT_FAILURE);
51912f130f2Sgww 		}
52012f130f2Sgww 		lshpw = getspnam_r(user, &spwd, shadow, sizeof (shadow));
52112f130f2Sgww 		if (lshpw == NULL) {
5227c478bd9Sstevel@tonic-gate 			/*
52312f130f2Sgww 			 * the user entered doesn't exist, too bad.
5247c478bd9Sstevel@tonic-gate 			 */
52512f130f2Sgww 			goto sorry;
52612f130f2Sgww 		}
52712f130f2Sgww 
52812f130f2Sgww 		/*
52912f130f2Sgww 		 * There is a special case error to catch here:
53012f130f2Sgww 		 * If the password is hashed with an algorithm
53112f130f2Sgww 		 * other than the old unix crypt the call to crypt(3c)
53212f130f2Sgww 		 * could fail if /usr is corrupt or not available
53312f130f2Sgww 		 * since by default /etc/security/crypt.conf will
53412f130f2Sgww 		 * have the crypt_ modules located under /usr/lib.
53512f130f2Sgww 		 * Or it could happen if /etc/security/crypt.conf
53612f130f2Sgww 		 * is corrupted.
53712f130f2Sgww 		 *
53812f130f2Sgww 		 * If this happens crypt(3c) will return NULL and
53912f130f2Sgww 		 * set errno to ELIBACC for the former condition or
54012f130f2Sgww 		 * EINVAL for the latter, in this case we bypass
54112f130f2Sgww 		 * authentication and just verify that the user is
54212f130f2Sgww 		 * authorized.
54312f130f2Sgww 		 */
54412f130f2Sgww 
54512f130f2Sgww 		errno = 0;
54612f130f2Sgww 		cpass = crypt(pass, lshpw->sp_pwdp);
54712f130f2Sgww 		if (((cpass == NULL) && (lshpw->sp_pwdp[0] == '$')) &&
54812f130f2Sgww 		    ((errno == ELIBACC) || (errno == EINVAL))) {
54912f130f2Sgww 			goto checkauth;
55012f130f2Sgww 		} else if ((cpass == NULL) ||
55112f130f2Sgww 		    (strcmp(cpass, lshpw->sp_pwdp) != 0)) {
55212f130f2Sgww 			goto sorry;
5537c478bd9Sstevel@tonic-gate 		}
55412f130f2Sgww 
55512f130f2Sgww checkauth:
55612f130f2Sgww 		/*
55712f130f2Sgww 		 * There is a special case error here as well.
55812f130f2Sgww 		 * If /etc/user_attr is corrupt, getusernam("root")
55912f130f2Sgww 		 * returns NULL.
56012f130f2Sgww 		 * In this case, we just give access because this is similar
56112f130f2Sgww 		 * to the case of root not existing in /etc/passwd.
56212f130f2Sgww 		 */
56312f130f2Sgww 
56412f130f2Sgww 		if ((getusernam("root") != NULL) &&
56512f130f2Sgww 		    (chkauthattr(MAINTENANCE_AUTH, user) != 1)) {
56612f130f2Sgww 			goto sorry;
56712f130f2Sgww 		}
56812f130f2Sgww 		(void) fprintf(sysmsgfd, "\nsingle-user privilege "
56912f130f2Sgww 		    "assigned to %s on %s.\n", user, devname);
57012f130f2Sgww 		(void) sigsend(P_PID, masterpid, SIGUSR1);
57112f130f2Sgww 		(void) wait(NULL);
57212f130f2Sgww 		free(user);
57312f130f2Sgww 		free(pass);
57412f130f2Sgww 		single(su, devname);
57512f130f2Sgww 		/* single never returns */
57612f130f2Sgww 
57712f130f2Sgww sorry:
57812f130f2Sgww 		(void) printf("\nLogin incorrect or user %s not authorized\n",
57912f130f2Sgww 		    user);
58012f130f2Sgww 		free(user);
58112f130f2Sgww 		free(pass);
5827c478bd9Sstevel@tonic-gate 		(void) sleep(sleeptime);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate /*
5877c478bd9Sstevel@tonic-gate  * single() - exec shell for single user mode
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate static void
single(const char * cmd,char * ttyn)5917c478bd9Sstevel@tonic-gate single(const char *cmd, char *ttyn)
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	struct utmpx	*u;
5947c478bd9Sstevel@tonic-gate 	char		found = B_FALSE;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (ttyn == NULL)
5977c478bd9Sstevel@tonic-gate 		ttyn = findttyname(STDIN_FILENO);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	/*
6007c478bd9Sstevel@tonic-gate 	 * utmpx records on the console device are expected to be "console"
6017c478bd9Sstevel@tonic-gate 	 * by other processes, such as dtlogin.
6027c478bd9Sstevel@tonic-gate 	 */
6037c478bd9Sstevel@tonic-gate 	ttyn = stripttyname(ttyn);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/* update the utmpx file. */
6067c478bd9Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
6077c478bd9Sstevel@tonic-gate 		if (strcmp(u->ut_line, ttyn) == 0) {
6087c478bd9Sstevel@tonic-gate 			u->ut_tv.tv_sec = time(NULL);
6097c478bd9Sstevel@tonic-gate 			u->ut_type = USER_PROCESS;
6107c478bd9Sstevel@tonic-gate 			u->ut_pid = getpid();
6117c478bd9Sstevel@tonic-gate 			if (strcmp(u->ut_user, "root") != 0)
6127c478bd9Sstevel@tonic-gate 				(void) strcpy(u->ut_user, "root");
6137c478bd9Sstevel@tonic-gate 			(void) pututxline(u);
6147c478bd9Sstevel@tonic-gate 			found = B_TRUE;
6157c478bd9Sstevel@tonic-gate 			break;
6167c478bd9Sstevel@tonic-gate 		}
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 	if (!found) {
6197c478bd9Sstevel@tonic-gate 		struct utmpx entryx;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		entryx.ut_tv.tv_sec = time(NULL);
6227c478bd9Sstevel@tonic-gate 		entryx.ut_type = USER_PROCESS;
6237c478bd9Sstevel@tonic-gate 		entryx.ut_pid = getpid();
6247c478bd9Sstevel@tonic-gate 		(void) strcpy(entryx.ut_user, "root");
6257c478bd9Sstevel@tonic-gate 		(void) strcpy(entryx.ut_line, ttyn);
6267c478bd9Sstevel@tonic-gate 		entryx.ut_tv.tv_usec = 0;
6277c478bd9Sstevel@tonic-gate 		entryx.ut_session = 0;
6287c478bd9Sstevel@tonic-gate 		entryx.ut_id[0] = 'c';
6297c478bd9Sstevel@tonic-gate 		entryx.ut_id[1] = 'o';
6307c478bd9Sstevel@tonic-gate 		entryx.ut_id[2] = 's';
6317c478bd9Sstevel@tonic-gate 		entryx.ut_id[3] = 'u';
6327c478bd9Sstevel@tonic-gate 		entryx.ut_syslen = 1;
6337c478bd9Sstevel@tonic-gate 		entryx.ut_host[0] = '\0';
6347c478bd9Sstevel@tonic-gate 		entryx.ut_exit.e_termination = WTERMSIG(0);
6357c478bd9Sstevel@tonic-gate 		entryx.ut_exit.e_exit = WEXITSTATUS(0);
6367c478bd9Sstevel@tonic-gate 		(void) pututxline(&entryx);
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	endutxent();
6397c478bd9Sstevel@tonic-gate 	(void) printf("Entering System Maintenance Mode\n\n");
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if (execl(cmd, cmd, "-", (char *)0) < 0)
6427c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate /*
64612f130f2Sgww  * sulogin_getinput() - hacked from the standard PAM tty conversation
64712f130f2Sgww  *			function getpassphrase() library version
64812f130f2Sgww  *			so we can distinguish newline and EOF.
64912f130f2Sgww  *		        also don't need this routine to give a prompt.
6507c478bd9Sstevel@tonic-gate  *
6517c478bd9Sstevel@tonic-gate  * returns the password string, or NULL if the used typed EOF.
6527c478bd9Sstevel@tonic-gate  */
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate static char *
sulogin_getinput(char * devname,int echooff)65512f130f2Sgww sulogin_getinput(char *devname, int echooff)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	struct termio	ttyb;
6587c478bd9Sstevel@tonic-gate 	int		c;
6597c478bd9Sstevel@tonic-gate 	FILE		*fi;
66012f130f2Sgww 	static char	input[PASS_MAX + 1];
6617c478bd9Sstevel@tonic-gate 	void		(*saved_handler)();
66212f130f2Sgww 	char		*rval = input;
6637c478bd9Sstevel@tonic-gate 	int		i = 0;
6647c478bd9Sstevel@tonic-gate 
66512f130f2Sgww 	if ((fi = fopen(devname, "r")) == NULL) {
6667c478bd9Sstevel@tonic-gate 		fi = stdin;
66712f130f2Sgww 	}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	saved_handler = signal(SIGINT, SIG_IGN);
6707c478bd9Sstevel@tonic-gate 
67112f130f2Sgww 	if (echooff) {
67212f130f2Sgww 		ttyb = ttymodes;
67312f130f2Sgww 		ttyb.c_lflag &= ~(ECHO | ECHOE | ECHONL);
67412f130f2Sgww 		(void) ioctl(fileno(fi), TCSETAF, &ttyb);
67512f130f2Sgww 	}
6767c478bd9Sstevel@tonic-gate 
67712f130f2Sgww 	/* get characters up to PASS_MAX, but don't overflow */
67812f130f2Sgww 	while ((c = getc(fi)) != '\n' && (c != '\r')) {
67912f130f2Sgww 		if (c == EOF && i == 0) {	/* ^D, no input */
6807c478bd9Sstevel@tonic-gate 			rval = NULL;
6817c478bd9Sstevel@tonic-gate 			break;
6827c478bd9Sstevel@tonic-gate 		}
68312f130f2Sgww 		if (i < PASS_MAX) {
68412f130f2Sgww 			input[i++] = (char)c;
68512f130f2Sgww 		}
6867c478bd9Sstevel@tonic-gate 	}
68712f130f2Sgww 	input[i] = '\0';
6887c478bd9Sstevel@tonic-gate 	(void) fputc('\n', fi);
68912f130f2Sgww 	if (echooff) {
69012f130f2Sgww 		(void) ioctl(fileno(fi), TCSETAW, &ttymodes);
69112f130f2Sgww 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	if (saved_handler != SIG_ERR)
6947c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, saved_handler);
69512f130f2Sgww 	return (rval == NULL ? NULL : strdup(rval));
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate static char *
findttyname(int fd)6997c478bd9Sstevel@tonic-gate findttyname(int fd)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	char	*ttyn = ttyname(fd);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	if (ttyn == NULL)
7047c478bd9Sstevel@tonic-gate 		ttyn = "/dev/???";
7057c478bd9Sstevel@tonic-gate 	else {
7067c478bd9Sstevel@tonic-gate 		/*
7077c478bd9Sstevel@tonic-gate 		 * /dev/syscon and /dev/systty are usually links to
7087c478bd9Sstevel@tonic-gate 		 * /dev/console.  prefer /dev/console.
7097c478bd9Sstevel@tonic-gate 		 */
7107c478bd9Sstevel@tonic-gate 		if (((strcmp(ttyn, "/dev/syscon") == 0) ||
7117c478bd9Sstevel@tonic-gate 		    (strcmp(ttyn, "/dev/systty") == 0)) &&
7127c478bd9Sstevel@tonic-gate 		    access("/dev/console", F_OK))
7137c478bd9Sstevel@tonic-gate 			ttyn = "/dev/console";
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 	return (ttyn);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate static char *
stripttyname(char * ttyn)7197c478bd9Sstevel@tonic-gate stripttyname(char *ttyn)
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate 	/* saw off the /dev/ */
7227c478bd9Sstevel@tonic-gate 	if (strncmp(ttyn, "/dev/", sizeof ("/dev/") -1) == 0)
7237c478bd9Sstevel@tonic-gate 		return (ttyn + sizeof ("/dev/") - 1);
7247c478bd9Sstevel@tonic-gate 	else
7257c478bd9Sstevel@tonic-gate 		return (ttyn);
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate /* ARGSUSED */
7307c478bd9Sstevel@tonic-gate static	void
noop(int sig)7317c478bd9Sstevel@tonic-gate noop(int sig)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 	/*
7347c478bd9Sstevel@tonic-gate 	 * This signal handler does nothing except return.  We use it
7357c478bd9Sstevel@tonic-gate 	 * as the signal disposition in this program instead of
7367c478bd9Sstevel@tonic-gate 	 * SIG_IGN so that we do not have to restore the disposition
7377c478bd9Sstevel@tonic-gate 	 * back to SIG_DFL. Instead we allow exec(2) to set the
7387c478bd9Sstevel@tonic-gate 	 * dispostion to SIG_DFL to avoid a race condition.
7397c478bd9Sstevel@tonic-gate 	 */
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate /* ARGSUSED */
7437c478bd9Sstevel@tonic-gate static void
parenthandler(int sig,siginfo_t * si,ucontext_t * uc)7447c478bd9Sstevel@tonic-gate parenthandler(int sig, siginfo_t *si, ucontext_t *uc)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	int i;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * We get here if someone has successfully entered a password
7507c478bd9Sstevel@tonic-gate 	 * from the auxiliary console and is getting the single-user shell.
7517c478bd9Sstevel@tonic-gate 	 * When this happens, the parent needs to kill the children
7527c478bd9Sstevel@tonic-gate 	 * that didn't get the shell.
7537c478bd9Sstevel@tonic-gate 	 *
7547c478bd9Sstevel@tonic-gate 	 */
7557c478bd9Sstevel@tonic-gate 	for (i = 0; i < nchild; i++) {
7567c478bd9Sstevel@tonic-gate 		if (pidlist[i] != si->__data.__proc.__pid)
7577c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, pidlist[i], SIGTERM);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
7607c478bd9Sstevel@tonic-gate 	sa.sa_flags = 0;
7617c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
7627c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, NULL);
7637c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sa, NULL);
7647c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, NULL);
7657c478bd9Sstevel@tonic-gate 	(void) wait(NULL);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate /*
7697c478bd9Sstevel@tonic-gate  * The master pid will get SIGTERM or SIGHUP from init, and then
7707c478bd9Sstevel@tonic-gate  * has to make sure the shell isn't still running.
7717c478bd9Sstevel@tonic-gate  */
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate /* ARGSUSED */
7747c478bd9Sstevel@tonic-gate static	void
childcleanup(int sig)7757c478bd9Sstevel@tonic-gate childcleanup(int sig)
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 	int i;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	/* Only need to kill the child that became the shell. */
7807c478bd9Sstevel@tonic-gate 	for (i = 0; i < nchild; i++) {
78148bbca81SDaniel Hoffman 		/* Don't kill grandparent before it's necessary */
7827c478bd9Sstevel@tonic-gate 		if (pidlist[i] != getppid())
7837c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PID, pidlist[i], SIGHUP);
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /* ARGSUSED */
7887c478bd9Sstevel@tonic-gate static	void
termhandler(int sig)7897c478bd9Sstevel@tonic-gate termhandler(int sig)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	FILE *fi;
7927c478bd9Sstevel@tonic-gate 	pid_t pid;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	/* Processes come here when they fail to receive the password. */
7957c478bd9Sstevel@tonic-gate 	if ((fi = fopen("/dev/tty", "r+")) == NULL)
7967c478bd9Sstevel@tonic-gate 		fi = stdin;
7977c478bd9Sstevel@tonic-gate 	else
7987c478bd9Sstevel@tonic-gate 		setbuf(fi, NULL);
7997c478bd9Sstevel@tonic-gate 	sanitize_tty(fileno(fi));
8007c478bd9Sstevel@tonic-gate 	/* If you're the controlling tty, then just wait */
8017c478bd9Sstevel@tonic-gate 	pid = getpid();
8027c478bd9Sstevel@tonic-gate 	if (pid == originalpid || pid == masterpid) {
8037c478bd9Sstevel@tonic-gate 		sa.sa_handler = SIG_IGN;
8047c478bd9Sstevel@tonic-gate 		sa.sa_flags = 0;
8057c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&sa.sa_mask);
8067c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGINT, &sa, NULL);
8077c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGQUIT, &sa, NULL);
8087c478bd9Sstevel@tonic-gate 		sa.sa_handler = SIG_DFL;
8097c478bd9Sstevel@tonic-gate 		sa.sa_flags = 0;
8107c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&sa.sa_mask);
8117c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGTERM, &sa, NULL);
8127c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGHUP, &sa, NULL);
8137c478bd9Sstevel@tonic-gate 		(void) wait(NULL);
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 	exit(0);
8167c478bd9Sstevel@tonic-gate }
817