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