1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* 28*7c478bd9Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T 29*7c478bd9Sstevel@tonic-gate * All rights reserved. 30*7c478bd9Sstevel@tonic-gate * 31*7c478bd9Sstevel@tonic-gate * Copyright (c) 1987, 1988 Microsoft Corporation. 32*7c478bd9Sstevel@tonic-gate * All rights reserved. 33*7c478bd9Sstevel@tonic-gate */ 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate /* 38*7c478bd9Sstevel@tonic-gate * sulogin - special login program exec'd from init to let user 39*7c478bd9Sstevel@tonic-gate * come up single user, or go to default init state straight away. 40*7c478bd9Sstevel@tonic-gate * 41*7c478bd9Sstevel@tonic-gate * Explain the scoop to the user, and prompt for root password or 42*7c478bd9Sstevel@tonic-gate * ^D. Good root password gets you single user, ^D exits sulogin, 43*7c478bd9Sstevel@tonic-gate * and init will go to default init state. 44*7c478bd9Sstevel@tonic-gate * 45*7c478bd9Sstevel@tonic-gate * If /etc/passwd is missing, or there's no entry for root, 46*7c478bd9Sstevel@tonic-gate * go single user, no questions asked. 47*7c478bd9Sstevel@tonic-gate */ 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 50*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 51*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 52*7c478bd9Sstevel@tonic-gate #include <sys/sysmsg_impl.h> 53*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 54*7c478bd9Sstevel@tonic-gate #include <sys/resource.h> 55*7c478bd9Sstevel@tonic-gate #include <sys/uadmin.h> 56*7c478bd9Sstevel@tonic-gate #include <sys/wait.h> 57*7c478bd9Sstevel@tonic-gate #include <sys/stermio.h> 58*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 59*7c478bd9Sstevel@tonic-gate #include <termio.h> 60*7c478bd9Sstevel@tonic-gate #include <pwd.h> 61*7c478bd9Sstevel@tonic-gate #include <shadow.h> 62*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 63*7c478bd9Sstevel@tonic-gate #include <stdio.h> 64*7c478bd9Sstevel@tonic-gate #include <signal.h> 65*7c478bd9Sstevel@tonic-gate #include <siginfo.h> 66*7c478bd9Sstevel@tonic-gate #include <utmpx.h> 67*7c478bd9Sstevel@tonic-gate #include <unistd.h> 68*7c478bd9Sstevel@tonic-gate #include <ucontext.h> 69*7c478bd9Sstevel@tonic-gate #include <string.h> 70*7c478bd9Sstevel@tonic-gate #include <strings.h> 71*7c478bd9Sstevel@tonic-gate #include <deflt.h> 72*7c478bd9Sstevel@tonic-gate #include <limits.h> 73*7c478bd9Sstevel@tonic-gate #include <errno.h> 74*7c478bd9Sstevel@tonic-gate #include <crypt.h> 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate /* 77*7c478bd9Sstevel@tonic-gate * Intervals to sleep after failed login 78*7c478bd9Sstevel@tonic-gate */ 79*7c478bd9Sstevel@tonic-gate #ifndef SLEEPTIME 80*7c478bd9Sstevel@tonic-gate #define SLEEPTIME 4 /* sleeptime before login incorrect msg */ 81*7c478bd9Sstevel@tonic-gate #endif 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate #define SLEEPTIME_MAX 5 /* maximum sleeptime */ 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate /* 86*7c478bd9Sstevel@tonic-gate * the name of the file containing the login defaults we deliberately 87*7c478bd9Sstevel@tonic-gate * use the same file as login(1) 88*7c478bd9Sstevel@tonic-gate */ 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate #define DEFAULT_LOGIN "/etc/default/login" 91*7c478bd9Sstevel@tonic-gate #define DEFAULT_SULOGIN "/etc/default/sulogin" 92*7c478bd9Sstevel@tonic-gate #define DEFAULT_CONSOLE "/dev/console" 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate static char shell[] = "/sbin/sh"; 95*7c478bd9Sstevel@tonic-gate static char su[] = "/sbin/su.static"; 96*7c478bd9Sstevel@tonic-gate static int sleeptime = SLEEPTIME; 97*7c478bd9Sstevel@tonic-gate static int nchild = 0; 98*7c478bd9Sstevel@tonic-gate static pid_t pidlist[10]; 99*7c478bd9Sstevel@tonic-gate static pid_t masterpid = 0; 100*7c478bd9Sstevel@tonic-gate static pid_t originalpid = 0; 101*7c478bd9Sstevel@tonic-gate static struct sigaction sa; 102*7c478bd9Sstevel@tonic-gate static struct termio ttymodes; 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate static char *findttyname(int fd); 105*7c478bd9Sstevel@tonic-gate static char *stripttyname(char *); 106*7c478bd9Sstevel@tonic-gate static char *sulogin_getpass(char *); 107*7c478bd9Sstevel@tonic-gate static void noop(int); 108*7c478bd9Sstevel@tonic-gate static void single(const char *, char *); 109*7c478bd9Sstevel@tonic-gate static void main_loop(char *, struct spwd *, boolean_t); 110*7c478bd9Sstevel@tonic-gate static void parenthandler(); 111*7c478bd9Sstevel@tonic-gate static void termhandler(int); 112*7c478bd9Sstevel@tonic-gate static void setupsigs(void); 113*7c478bd9Sstevel@tonic-gate static int pathcmp(char *, char *); 114*7c478bd9Sstevel@tonic-gate static void doit(char *, char *, struct spwd *); 115*7c478bd9Sstevel@tonic-gate static void childcleanup(int); 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 118*7c478bd9Sstevel@tonic-gate int 119*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 120*7c478bd9Sstevel@tonic-gate { 121*7c478bd9Sstevel@tonic-gate struct spwd *shpw; 122*7c478bd9Sstevel@tonic-gate int passreq = B_TRUE; 123*7c478bd9Sstevel@tonic-gate int flags; 124*7c478bd9Sstevel@tonic-gate int fd; 125*7c478bd9Sstevel@tonic-gate char *infop, *ptr, *p; 126*7c478bd9Sstevel@tonic-gate pid_t pid; 127*7c478bd9Sstevel@tonic-gate int bufsize; 128*7c478bd9Sstevel@tonic-gate struct stat st; 129*7c478bd9Sstevel@tonic-gate char cttyname[100]; 130*7c478bd9Sstevel@tonic-gate char namedlist[500]; 131*7c478bd9Sstevel@tonic-gate char scratchlist[500]; 132*7c478bd9Sstevel@tonic-gate dev_t cttyd; 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate if (geteuid() != 0) { 135*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: must be root\n", argv[0]); 136*7c478bd9Sstevel@tonic-gate return (EXIT_FAILURE); 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate /* Do the magic to determine the children */ 140*7c478bd9Sstevel@tonic-gate if ((fd = open(SYSMSG, 0)) < 0) 141*7c478bd9Sstevel@tonic-gate return (EXIT_FAILURE); 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* 144*7c478bd9Sstevel@tonic-gate * If the console supports the CIOCTTYCONSOLE ioctl, then fetch 145*7c478bd9Sstevel@tonic-gate * its console device list. If not, then we use the default 146*7c478bd9Sstevel@tonic-gate * console name. 147*7c478bd9Sstevel@tonic-gate */ 148*7c478bd9Sstevel@tonic-gate if (ioctl(fd, CIOCTTYCONSOLE, &cttyd) == 0) { 149*7c478bd9Sstevel@tonic-gate if ((bufsize = ioctl(fd, CIOCGETCONSOLE, NULL)) < 0) 150*7c478bd9Sstevel@tonic-gate return (EXIT_FAILURE); 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate if (bufsize > 0) { 153*7c478bd9Sstevel@tonic-gate if ((infop = calloc(bufsize, sizeof (char))) == NULL) 154*7c478bd9Sstevel@tonic-gate return (EXIT_FAILURE); 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate if (ioctl(fd, CIOCGETCONSOLE, infop) < 0) 157*7c478bd9Sstevel@tonic-gate return (EXIT_FAILURE); 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate (void) snprintf(namedlist, sizeof (namedlist), "%s %s", 160*7c478bd9Sstevel@tonic-gate DEFAULT_CONSOLE, infop); 161*7c478bd9Sstevel@tonic-gate } else 162*7c478bd9Sstevel@tonic-gate (void) snprintf(namedlist, sizeof (namedlist), "%s", 163*7c478bd9Sstevel@tonic-gate DEFAULT_CONSOLE); 164*7c478bd9Sstevel@tonic-gate } else { 165*7c478bd9Sstevel@tonic-gate (void) snprintf(namedlist, sizeof (namedlist), "%s", 166*7c478bd9Sstevel@tonic-gate DEFAULT_CONSOLE); 167*7c478bd9Sstevel@tonic-gate cttyd = NODEV; 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate /* 171*7c478bd9Sstevel@tonic-gate * The attempt to turn the controlling terminals dev_t into a string 172*7c478bd9Sstevel@tonic-gate * may not be successful, thus leaving the variable cttyname as a 173*7c478bd9Sstevel@tonic-gate * NULL. This occurs if during boot we find 174*7c478bd9Sstevel@tonic-gate * the root partition (or some other partition) 175*7c478bd9Sstevel@tonic-gate * requires manual fsck, thus resulting in sulogin 176*7c478bd9Sstevel@tonic-gate * getting invoked. The ioctl for CIOCTTYCONSOLE 177*7c478bd9Sstevel@tonic-gate * called above returned NODEV for cttyd 178*7c478bd9Sstevel@tonic-gate * in these cases. NODEV gets returned when the vnode pointer 179*7c478bd9Sstevel@tonic-gate * in our session structure is NULL. In these cases it 180*7c478bd9Sstevel@tonic-gate * must be assumed that the default console is used. 181*7c478bd9Sstevel@tonic-gate * 182*7c478bd9Sstevel@tonic-gate * See uts/common/os/session.c:cttydev(). 183*7c478bd9Sstevel@tonic-gate */ 184*7c478bd9Sstevel@tonic-gate (void) strcpy(cttyname, DEFAULT_CONSOLE); 185*7c478bd9Sstevel@tonic-gate (void) strcpy(scratchlist, namedlist); 186*7c478bd9Sstevel@tonic-gate ptr = scratchlist; 187*7c478bd9Sstevel@tonic-gate while (ptr != NULL) { 188*7c478bd9Sstevel@tonic-gate p = strchr(ptr, ' '); 189*7c478bd9Sstevel@tonic-gate if (p == NULL) { 190*7c478bd9Sstevel@tonic-gate if (stat(ptr, &st)) 191*7c478bd9Sstevel@tonic-gate return (EXIT_FAILURE); 192*7c478bd9Sstevel@tonic-gate if (st.st_rdev == cttyd) 193*7c478bd9Sstevel@tonic-gate (void) strcpy(cttyname, ptr); 194*7c478bd9Sstevel@tonic-gate break; 195*7c478bd9Sstevel@tonic-gate } 196*7c478bd9Sstevel@tonic-gate *p++ = '\0'; 197*7c478bd9Sstevel@tonic-gate if (stat(ptr, &st)) 198*7c478bd9Sstevel@tonic-gate return (EXIT_FAILURE); 199*7c478bd9Sstevel@tonic-gate if (st.st_rdev == cttyd) { 200*7c478bd9Sstevel@tonic-gate (void) strcpy(cttyname, ptr); 201*7c478bd9Sstevel@tonic-gate break; 202*7c478bd9Sstevel@tonic-gate } 203*7c478bd9Sstevel@tonic-gate ptr = p; 204*7c478bd9Sstevel@tonic-gate } 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate /* 207*7c478bd9Sstevel@tonic-gate * Use the same value of SLEEPTIME that login(1) uses. This 208*7c478bd9Sstevel@tonic-gate * is obtained by reading the file /etc/default/login using 209*7c478bd9Sstevel@tonic-gate * the def*() functions. 210*7c478bd9Sstevel@tonic-gate */ 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate if (defopen(DEFAULT_LOGIN) == 0) { 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate /* ignore case */ 215*7c478bd9Sstevel@tonic-gate 216*7c478bd9Sstevel@tonic-gate flags = defcntl(DC_GETFLAGS, 0); 217*7c478bd9Sstevel@tonic-gate TURNOFF(flags, DC_CASE); 218*7c478bd9Sstevel@tonic-gate (void) defcntl(DC_SETFLAGS, flags); 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate if ((ptr = defread("SLEEPTIME=")) != NULL) 221*7c478bd9Sstevel@tonic-gate sleeptime = atoi(ptr); 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate if (sleeptime < 0 || sleeptime > SLEEPTIME_MAX) 224*7c478bd9Sstevel@tonic-gate sleeptime = SLEEPTIME; 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate (void) defopen(NULL); /* closes DEFAULT_LOGIN */ 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate /* 230*7c478bd9Sstevel@tonic-gate * Use our own value of PASSREQ, separate from the one login(1) uses. 231*7c478bd9Sstevel@tonic-gate * This is obtained by reading the file /etc/default/sulogin using 232*7c478bd9Sstevel@tonic-gate * the def*() functions. 233*7c478bd9Sstevel@tonic-gate */ 234*7c478bd9Sstevel@tonic-gate 235*7c478bd9Sstevel@tonic-gate if (defopen(DEFAULT_SULOGIN) == 0) { 236*7c478bd9Sstevel@tonic-gate if ((ptr = defread("PASSREQ=")) != NULL) 237*7c478bd9Sstevel@tonic-gate if (strcmp("NO", ptr) == 0) 238*7c478bd9Sstevel@tonic-gate passreq = B_FALSE; 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate (void) defopen(NULL); /* closes DEFAULT_SULOGIN */ 241*7c478bd9Sstevel@tonic-gate } 242*7c478bd9Sstevel@tonic-gate 243*7c478bd9Sstevel@tonic-gate if (passreq == B_FALSE) 244*7c478bd9Sstevel@tonic-gate single(shell, NULL); 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate /* 247*7c478bd9Sstevel@tonic-gate * if no 'root' entry in /etc/shadow, give maint. mode single 248*7c478bd9Sstevel@tonic-gate * user shell prompt 249*7c478bd9Sstevel@tonic-gate */ 250*7c478bd9Sstevel@tonic-gate setspent(); 251*7c478bd9Sstevel@tonic-gate if ((shpw = getspnam("root")) == NULL) { 252*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n*** Unable to retrieve `root' entry " 253*7c478bd9Sstevel@tonic-gate "in shadow password file ***\n\n"); 254*7c478bd9Sstevel@tonic-gate single(shell, NULL); 255*7c478bd9Sstevel@tonic-gate } 256*7c478bd9Sstevel@tonic-gate endspent(); 257*7c478bd9Sstevel@tonic-gate /* 258*7c478bd9Sstevel@tonic-gate * if no 'root' entry in /etc/passwd, give maint. mode single 259*7c478bd9Sstevel@tonic-gate * user shell prompt 260*7c478bd9Sstevel@tonic-gate */ 261*7c478bd9Sstevel@tonic-gate setpwent(); 262*7c478bd9Sstevel@tonic-gate if (getpwnam("root") == NULL) { 263*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n*** Unable to retrieve `root' entry " 264*7c478bd9Sstevel@tonic-gate "in password file ***\n\n"); 265*7c478bd9Sstevel@tonic-gate single(shell, NULL); 266*7c478bd9Sstevel@tonic-gate } 267*7c478bd9Sstevel@tonic-gate endpwent(); 268*7c478bd9Sstevel@tonic-gate /* process with controlling tty treated special */ 269*7c478bd9Sstevel@tonic-gate if ((pid = fork()) != (pid_t)0) { 270*7c478bd9Sstevel@tonic-gate if (pid == -1) 271*7c478bd9Sstevel@tonic-gate return (EXIT_FAILURE); 272*7c478bd9Sstevel@tonic-gate else { 273*7c478bd9Sstevel@tonic-gate setupsigs(); 274*7c478bd9Sstevel@tonic-gate masterpid = pid; 275*7c478bd9Sstevel@tonic-gate originalpid = getpid(); 276*7c478bd9Sstevel@tonic-gate /* 277*7c478bd9Sstevel@tonic-gate * init() was invoked from a console that was not 278*7c478bd9Sstevel@tonic-gate * the default console, nor was it an auxiliary. 279*7c478bd9Sstevel@tonic-gate */ 280*7c478bd9Sstevel@tonic-gate if (cttyname[0] == NULL) 281*7c478bd9Sstevel@tonic-gate termhandler(0); 282*7c478bd9Sstevel@tonic-gate /* Never returns */ 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate main_loop(cttyname, shpw, B_TRUE); 285*7c478bd9Sstevel@tonic-gate /* Never returns */ 286*7c478bd9Sstevel@tonic-gate } 287*7c478bd9Sstevel@tonic-gate } 288*7c478bd9Sstevel@tonic-gate masterpid = getpid(); 289*7c478bd9Sstevel@tonic-gate originalpid = getppid(); 290*7c478bd9Sstevel@tonic-gate pidlist[nchild++] = originalpid; 291*7c478bd9Sstevel@tonic-gate 292*7c478bd9Sstevel@tonic-gate sa.sa_handler = childcleanup; 293*7c478bd9Sstevel@tonic-gate sa.sa_flags = 0; 294*7c478bd9Sstevel@tonic-gate (void) sigemptyset(&sa.sa_mask); 295*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGTERM, &sa, NULL); 296*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGHUP, &sa, NULL); 297*7c478bd9Sstevel@tonic-gate sa.sa_handler = parenthandler; 298*7c478bd9Sstevel@tonic-gate sa.sa_flags = SA_SIGINFO; 299*7c478bd9Sstevel@tonic-gate (void) sigemptyset(&sa.sa_mask); 300*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGUSR1, &sa, NULL); 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate sa.sa_handler = SIG_IGN; 303*7c478bd9Sstevel@tonic-gate sa.sa_flags = 0; 304*7c478bd9Sstevel@tonic-gate (void) sigemptyset(&sa.sa_mask); 305*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGCHLD, &sa, NULL); 306*7c478bd9Sstevel@tonic-gate /* 307*7c478bd9Sstevel@tonic-gate * If there isn't a password on root, then don't permit 308*7c478bd9Sstevel@tonic-gate * the fanout capability of sulogin. 309*7c478bd9Sstevel@tonic-gate */ 310*7c478bd9Sstevel@tonic-gate if (*shpw->sp_pwdp != '\0') { 311*7c478bd9Sstevel@tonic-gate ptr = namedlist; 312*7c478bd9Sstevel@tonic-gate while (ptr != NULL) { 313*7c478bd9Sstevel@tonic-gate p = strchr(ptr, ' '); 314*7c478bd9Sstevel@tonic-gate if (p == NULL) { 315*7c478bd9Sstevel@tonic-gate doit(ptr, cttyname, shpw); 316*7c478bd9Sstevel@tonic-gate break; 317*7c478bd9Sstevel@tonic-gate } 318*7c478bd9Sstevel@tonic-gate *p++ = '\0'; 319*7c478bd9Sstevel@tonic-gate doit(ptr, cttyname, shpw); 320*7c478bd9Sstevel@tonic-gate ptr = p; 321*7c478bd9Sstevel@tonic-gate } 322*7c478bd9Sstevel@tonic-gate } 323*7c478bd9Sstevel@tonic-gate if (pathcmp(cttyname, DEFAULT_CONSOLE) != 0) { 324*7c478bd9Sstevel@tonic-gate if ((pid = fork()) == (pid_t)0) { 325*7c478bd9Sstevel@tonic-gate setupsigs(); 326*7c478bd9Sstevel@tonic-gate main_loop(DEFAULT_CONSOLE, shpw, B_FALSE); 327*7c478bd9Sstevel@tonic-gate } else if (pid == -1) 328*7c478bd9Sstevel@tonic-gate return (EXIT_FAILURE); 329*7c478bd9Sstevel@tonic-gate pidlist[nchild++] = pid; 330*7c478bd9Sstevel@tonic-gate } 331*7c478bd9Sstevel@tonic-gate /* 332*7c478bd9Sstevel@tonic-gate * When parent is all done, it pauses until one of its children 333*7c478bd9Sstevel@tonic-gate * signals that its time to kill the underpriviledged. 334*7c478bd9Sstevel@tonic-gate */ 335*7c478bd9Sstevel@tonic-gate (void) wait(NULL); 336*7c478bd9Sstevel@tonic-gate 337*7c478bd9Sstevel@tonic-gate return (0); 338*7c478bd9Sstevel@tonic-gate } 339*7c478bd9Sstevel@tonic-gate 340*7c478bd9Sstevel@tonic-gate /* 341*7c478bd9Sstevel@tonic-gate * These flags are taken from stty's "sane" table entries in 342*7c478bd9Sstevel@tonic-gate * usr/src/cmd/ttymon/sttytable.c 343*7c478bd9Sstevel@tonic-gate */ 344*7c478bd9Sstevel@tonic-gate #define SET_IFLAG (BRKINT|IGNPAR|ISTRIP|ICRNL|IXON|IMAXBEL) 345*7c478bd9Sstevel@tonic-gate #define RESET_IFLAG (IGNBRK|PARMRK|INPCK|INLCR|IGNCR|IUCLC|IXOFF|IXANY) 346*7c478bd9Sstevel@tonic-gate #define SET_OFLAG (OPOST|ONLCR) 347*7c478bd9Sstevel@tonic-gate #define RESET_OFLAG (OLCUC|OCRNL|ONOCR|ONLRET|OFILL|OFDEL| \ 348*7c478bd9Sstevel@tonic-gate NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY) 349*7c478bd9Sstevel@tonic-gate #define SET_LFLAG (ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL) 350*7c478bd9Sstevel@tonic-gate #define RESET_LFLAG (XCASE|ECHONL|NOFLSH|STFLUSH|STWRAP|STAPPL) 351*7c478bd9Sstevel@tonic-gate 352*7c478bd9Sstevel@tonic-gate /* 353*7c478bd9Sstevel@tonic-gate * Do the equivalent of 'stty sane' on the terminal since we don't know 354*7c478bd9Sstevel@tonic-gate * what state it was in on startup. 355*7c478bd9Sstevel@tonic-gate */ 356*7c478bd9Sstevel@tonic-gate static void 357*7c478bd9Sstevel@tonic-gate sanitize_tty(int fd) 358*7c478bd9Sstevel@tonic-gate { 359*7c478bd9Sstevel@tonic-gate (void) ioctl(fd, TCGETA, &ttymodes); 360*7c478bd9Sstevel@tonic-gate ttymodes.c_iflag |= SET_IFLAG; 361*7c478bd9Sstevel@tonic-gate ttymodes.c_iflag &= ~RESET_IFLAG; 362*7c478bd9Sstevel@tonic-gate ttymodes.c_oflag |= SET_OFLAG; 363*7c478bd9Sstevel@tonic-gate ttymodes.c_oflag &= ~RESET_OFLAG; 364*7c478bd9Sstevel@tonic-gate ttymodes.c_lflag |= SET_LFLAG; 365*7c478bd9Sstevel@tonic-gate ttymodes.c_lflag &= ~RESET_LFLAG; 366*7c478bd9Sstevel@tonic-gate ttymodes.c_cc[VERASE] = CERASE; 367*7c478bd9Sstevel@tonic-gate ttymodes.c_cc[VKILL] = CKILL; 368*7c478bd9Sstevel@tonic-gate ttymodes.c_cc[VQUIT] = CQUIT; 369*7c478bd9Sstevel@tonic-gate ttymodes.c_cc[VINTR] = CINTR; 370*7c478bd9Sstevel@tonic-gate ttymodes.c_cc[VEOF] = CEOF; 371*7c478bd9Sstevel@tonic-gate ttymodes.c_cc[VEOL] = CNUL; 372*7c478bd9Sstevel@tonic-gate (void) ioctl(fd, TCSETAF, &ttymodes); 373*7c478bd9Sstevel@tonic-gate } 374*7c478bd9Sstevel@tonic-gate 375*7c478bd9Sstevel@tonic-gate /* 376*7c478bd9Sstevel@tonic-gate * Fork a child of sulogin for each of the auxiliary consoles. 377*7c478bd9Sstevel@tonic-gate */ 378*7c478bd9Sstevel@tonic-gate static void 379*7c478bd9Sstevel@tonic-gate doit(char *ptr, char *cttyname, struct spwd *shpw) 380*7c478bd9Sstevel@tonic-gate { 381*7c478bd9Sstevel@tonic-gate pid_t pid; 382*7c478bd9Sstevel@tonic-gate 383*7c478bd9Sstevel@tonic-gate if (pathcmp(ptr, DEFAULT_CONSOLE) != 0 && 384*7c478bd9Sstevel@tonic-gate pathcmp(ptr, cttyname) != 0) { 385*7c478bd9Sstevel@tonic-gate if ((pid = fork()) == (pid_t)0) { 386*7c478bd9Sstevel@tonic-gate setupsigs(); 387*7c478bd9Sstevel@tonic-gate main_loop(ptr, shpw, B_FALSE); 388*7c478bd9Sstevel@tonic-gate } else if (pid == -1) 389*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 390*7c478bd9Sstevel@tonic-gate pidlist[nchild++] = pid; 391*7c478bd9Sstevel@tonic-gate } 392*7c478bd9Sstevel@tonic-gate } 393*7c478bd9Sstevel@tonic-gate 394*7c478bd9Sstevel@tonic-gate static int 395*7c478bd9Sstevel@tonic-gate pathcmp(char *adev, char *bdev) 396*7c478bd9Sstevel@tonic-gate { 397*7c478bd9Sstevel@tonic-gate struct stat st1; 398*7c478bd9Sstevel@tonic-gate struct stat st2; 399*7c478bd9Sstevel@tonic-gate 400*7c478bd9Sstevel@tonic-gate if (adev == NULL || bdev == NULL) 401*7c478bd9Sstevel@tonic-gate return (1); 402*7c478bd9Sstevel@tonic-gate 403*7c478bd9Sstevel@tonic-gate if (strcmp(adev, bdev) == 0) 404*7c478bd9Sstevel@tonic-gate return (0); 405*7c478bd9Sstevel@tonic-gate 406*7c478bd9Sstevel@tonic-gate if (stat(adev, &st1) || (st1.st_mode & S_IFCHR) == 0) 407*7c478bd9Sstevel@tonic-gate return (1); 408*7c478bd9Sstevel@tonic-gate 409*7c478bd9Sstevel@tonic-gate if (stat(bdev, &st2) || (st2.st_mode & S_IFCHR) == 0) 410*7c478bd9Sstevel@tonic-gate return (1); 411*7c478bd9Sstevel@tonic-gate 412*7c478bd9Sstevel@tonic-gate if (st1.st_rdev == st2.st_rdev) 413*7c478bd9Sstevel@tonic-gate return (0); 414*7c478bd9Sstevel@tonic-gate 415*7c478bd9Sstevel@tonic-gate return (1); 416*7c478bd9Sstevel@tonic-gate } 417*7c478bd9Sstevel@tonic-gate 418*7c478bd9Sstevel@tonic-gate /* Handlers for the children at initialization */ 419*7c478bd9Sstevel@tonic-gate static void 420*7c478bd9Sstevel@tonic-gate setupsigs() 421*7c478bd9Sstevel@tonic-gate { 422*7c478bd9Sstevel@tonic-gate sa.sa_handler = noop; 423*7c478bd9Sstevel@tonic-gate sa.sa_flags = 0; 424*7c478bd9Sstevel@tonic-gate (void) sigemptyset(&sa.sa_mask); 425*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &sa, NULL); 426*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGQUIT, &sa, NULL); 427*7c478bd9Sstevel@tonic-gate 428*7c478bd9Sstevel@tonic-gate sa.sa_handler = termhandler; 429*7c478bd9Sstevel@tonic-gate sa.sa_flags = 0; 430*7c478bd9Sstevel@tonic-gate (void) sigemptyset(&sa.sa_mask); 431*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGTERM, &sa, NULL); 432*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGKILL, &sa, NULL); 433*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGHUP, &sa, NULL); 434*7c478bd9Sstevel@tonic-gate } 435*7c478bd9Sstevel@tonic-gate 436*7c478bd9Sstevel@tonic-gate static void 437*7c478bd9Sstevel@tonic-gate main_loop(char *devname, struct spwd *shpw, boolean_t cttyflag) 438*7c478bd9Sstevel@tonic-gate { 439*7c478bd9Sstevel@tonic-gate int fd, i; 440*7c478bd9Sstevel@tonic-gate char *pass; /* password from user */ 441*7c478bd9Sstevel@tonic-gate FILE *sysmsgfd; 442*7c478bd9Sstevel@tonic-gate 443*7c478bd9Sstevel@tonic-gate for (i = 0; i < 3; i++) 444*7c478bd9Sstevel@tonic-gate (void) close(i); 445*7c478bd9Sstevel@tonic-gate if (cttyflag == B_FALSE) { 446*7c478bd9Sstevel@tonic-gate if (setsid() == -1) 447*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 448*7c478bd9Sstevel@tonic-gate } 449*7c478bd9Sstevel@tonic-gate if ((fd = open(devname, O_RDWR)) < 0) 450*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 451*7c478bd9Sstevel@tonic-gate if (fd != 0) 452*7c478bd9Sstevel@tonic-gate (void) dup2(fd, STDIN_FILENO); 453*7c478bd9Sstevel@tonic-gate if (fd != 1) 454*7c478bd9Sstevel@tonic-gate (void) dup2(fd, STDOUT_FILENO); 455*7c478bd9Sstevel@tonic-gate if (fd != 2) 456*7c478bd9Sstevel@tonic-gate (void) dup2(fd, STDERR_FILENO); 457*7c478bd9Sstevel@tonic-gate if (fd > 2) 458*7c478bd9Sstevel@tonic-gate (void) close(fd); 459*7c478bd9Sstevel@tonic-gate 460*7c478bd9Sstevel@tonic-gate sysmsgfd = fopen("/dev/sysmsg", "w"); 461*7c478bd9Sstevel@tonic-gate 462*7c478bd9Sstevel@tonic-gate sanitize_tty(fileno(stdin)); 463*7c478bd9Sstevel@tonic-gate 464*7c478bd9Sstevel@tonic-gate for (;;) { 465*7c478bd9Sstevel@tonic-gate (void) fputs("\nRoot password for system maintenance " 466*7c478bd9Sstevel@tonic-gate "(control-d to bypass): ", stdout); 467*7c478bd9Sstevel@tonic-gate 468*7c478bd9Sstevel@tonic-gate if ((pass = sulogin_getpass(devname)) == NULL) { 469*7c478bd9Sstevel@tonic-gate /* signal other children to exit */ 470*7c478bd9Sstevel@tonic-gate (void) sigsend(P_PID, masterpid, SIGUSR1); 471*7c478bd9Sstevel@tonic-gate /* ^D, so straight to default init state */ 472*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 473*7c478bd9Sstevel@tonic-gate } 474*7c478bd9Sstevel@tonic-gate if (*shpw->sp_pwdp == '\0' && *pass == '\0') { 475*7c478bd9Sstevel@tonic-gate (void) fprintf(sysmsgfd, 476*7c478bd9Sstevel@tonic-gate "\nsingle-user privilege assigned to %s.\n", 477*7c478bd9Sstevel@tonic-gate devname); 478*7c478bd9Sstevel@tonic-gate (void) sigsend(P_PID, masterpid, SIGUSR1); 479*7c478bd9Sstevel@tonic-gate (void) wait(NULL); 480*7c478bd9Sstevel@tonic-gate single(su, devname); 481*7c478bd9Sstevel@tonic-gate } else if (*shpw->sp_pwdp != '\0') { 482*7c478bd9Sstevel@tonic-gate /* 483*7c478bd9Sstevel@tonic-gate * There is a special case error to catch here, 484*7c478bd9Sstevel@tonic-gate * because sulogin is statically linked: 485*7c478bd9Sstevel@tonic-gate * If the root password is hashed with an algorithm 486*7c478bd9Sstevel@tonic-gate * other than the old unix crypt the call to crypt(3c) 487*7c478bd9Sstevel@tonic-gate * could fail if /usr is corrupt or not available 488*7c478bd9Sstevel@tonic-gate * since by default /etc/security/crypt.conf will 489*7c478bd9Sstevel@tonic-gate * have the crypt_ modules located under /usr/lib. 490*7c478bd9Sstevel@tonic-gate * 491*7c478bd9Sstevel@tonic-gate * If this happens crypt(3c) will return NULL and 492*7c478bd9Sstevel@tonic-gate * set errno to ELIBACC, in this case we just give 493*7c478bd9Sstevel@tonic-gate * access because this is similar to the case of 494*7c478bd9Sstevel@tonic-gate * root not existing in /etc/passwd. 495*7c478bd9Sstevel@tonic-gate */ 496*7c478bd9Sstevel@tonic-gate pass = crypt(pass, shpw->sp_pwdp); 497*7c478bd9Sstevel@tonic-gate if ((strcmp(pass, shpw->sp_pwdp) == 0) || 498*7c478bd9Sstevel@tonic-gate ((pass == NULL) && (errno == ELIBACC) && 499*7c478bd9Sstevel@tonic-gate (shpw->sp_pwdp[0] == '$'))) { 500*7c478bd9Sstevel@tonic-gate (void) fprintf(sysmsgfd, 501*7c478bd9Sstevel@tonic-gate "\nsingle-user privilege assigned to %s.\n", 502*7c478bd9Sstevel@tonic-gate devname); 503*7c478bd9Sstevel@tonic-gate (void) sigsend(P_PID, masterpid, SIGUSR1); 504*7c478bd9Sstevel@tonic-gate (void) wait(NULL); 505*7c478bd9Sstevel@tonic-gate single(su, devname); 506*7c478bd9Sstevel@tonic-gate } 507*7c478bd9Sstevel@tonic-gate } 508*7c478bd9Sstevel@tonic-gate (void) sleep(sleeptime); 509*7c478bd9Sstevel@tonic-gate (void) printf("Login incorrect\n"); 510*7c478bd9Sstevel@tonic-gate } 511*7c478bd9Sstevel@tonic-gate } 512*7c478bd9Sstevel@tonic-gate 513*7c478bd9Sstevel@tonic-gate /* 514*7c478bd9Sstevel@tonic-gate * single() - exec shell for single user mode 515*7c478bd9Sstevel@tonic-gate */ 516*7c478bd9Sstevel@tonic-gate 517*7c478bd9Sstevel@tonic-gate static void 518*7c478bd9Sstevel@tonic-gate single(const char *cmd, char *ttyn) 519*7c478bd9Sstevel@tonic-gate { 520*7c478bd9Sstevel@tonic-gate struct utmpx *u; 521*7c478bd9Sstevel@tonic-gate char found = B_FALSE; 522*7c478bd9Sstevel@tonic-gate 523*7c478bd9Sstevel@tonic-gate if (ttyn == NULL) 524*7c478bd9Sstevel@tonic-gate ttyn = findttyname(STDIN_FILENO); 525*7c478bd9Sstevel@tonic-gate 526*7c478bd9Sstevel@tonic-gate /* 527*7c478bd9Sstevel@tonic-gate * utmpx records on the console device are expected to be "console" 528*7c478bd9Sstevel@tonic-gate * by other processes, such as dtlogin. 529*7c478bd9Sstevel@tonic-gate */ 530*7c478bd9Sstevel@tonic-gate ttyn = stripttyname(ttyn); 531*7c478bd9Sstevel@tonic-gate 532*7c478bd9Sstevel@tonic-gate /* update the utmpx file. */ 533*7c478bd9Sstevel@tonic-gate while ((u = getutxent()) != NULL) { 534*7c478bd9Sstevel@tonic-gate if (strcmp(u->ut_line, ttyn) == 0) { 535*7c478bd9Sstevel@tonic-gate u->ut_tv.tv_sec = time(NULL); 536*7c478bd9Sstevel@tonic-gate u->ut_type = USER_PROCESS; 537*7c478bd9Sstevel@tonic-gate u->ut_pid = getpid(); 538*7c478bd9Sstevel@tonic-gate if (strcmp(u->ut_user, "root") != 0) 539*7c478bd9Sstevel@tonic-gate (void) strcpy(u->ut_user, "root"); 540*7c478bd9Sstevel@tonic-gate (void) pututxline(u); 541*7c478bd9Sstevel@tonic-gate found = B_TRUE; 542*7c478bd9Sstevel@tonic-gate break; 543*7c478bd9Sstevel@tonic-gate } 544*7c478bd9Sstevel@tonic-gate } 545*7c478bd9Sstevel@tonic-gate if (!found) { 546*7c478bd9Sstevel@tonic-gate struct utmpx entryx; 547*7c478bd9Sstevel@tonic-gate 548*7c478bd9Sstevel@tonic-gate entryx.ut_tv.tv_sec = time(NULL); 549*7c478bd9Sstevel@tonic-gate entryx.ut_type = USER_PROCESS; 550*7c478bd9Sstevel@tonic-gate entryx.ut_pid = getpid(); 551*7c478bd9Sstevel@tonic-gate (void) strcpy(entryx.ut_user, "root"); 552*7c478bd9Sstevel@tonic-gate (void) strcpy(entryx.ut_line, ttyn); 553*7c478bd9Sstevel@tonic-gate entryx.ut_tv.tv_usec = 0; 554*7c478bd9Sstevel@tonic-gate entryx.ut_session = 0; 555*7c478bd9Sstevel@tonic-gate entryx.ut_id[0] = 'c'; 556*7c478bd9Sstevel@tonic-gate entryx.ut_id[1] = 'o'; 557*7c478bd9Sstevel@tonic-gate entryx.ut_id[2] = 's'; 558*7c478bd9Sstevel@tonic-gate entryx.ut_id[3] = 'u'; 559*7c478bd9Sstevel@tonic-gate entryx.ut_syslen = 1; 560*7c478bd9Sstevel@tonic-gate entryx.ut_host[0] = '\0'; 561*7c478bd9Sstevel@tonic-gate entryx.ut_exit.e_termination = WTERMSIG(0); 562*7c478bd9Sstevel@tonic-gate entryx.ut_exit.e_exit = WEXITSTATUS(0); 563*7c478bd9Sstevel@tonic-gate (void) pututxline(&entryx); 564*7c478bd9Sstevel@tonic-gate } 565*7c478bd9Sstevel@tonic-gate endutxent(); 566*7c478bd9Sstevel@tonic-gate (void) printf("Entering System Maintenance Mode\n\n"); 567*7c478bd9Sstevel@tonic-gate 568*7c478bd9Sstevel@tonic-gate if (execl(cmd, cmd, "-", (char *)0) < 0) 569*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 570*7c478bd9Sstevel@tonic-gate } 571*7c478bd9Sstevel@tonic-gate 572*7c478bd9Sstevel@tonic-gate /* 573*7c478bd9Sstevel@tonic-gate * sulogin_getpass() - hacked from the stdio library version so we can 574*7c478bd9Sstevel@tonic-gate * distinguish newline and EOF. also don't need this 575*7c478bd9Sstevel@tonic-gate * routine to give a prompt. 576*7c478bd9Sstevel@tonic-gate * 577*7c478bd9Sstevel@tonic-gate * returns the password string, or NULL if the used typed EOF. 578*7c478bd9Sstevel@tonic-gate */ 579*7c478bd9Sstevel@tonic-gate 580*7c478bd9Sstevel@tonic-gate static char * 581*7c478bd9Sstevel@tonic-gate sulogin_getpass(char *devname) 582*7c478bd9Sstevel@tonic-gate { 583*7c478bd9Sstevel@tonic-gate struct termio ttyb; 584*7c478bd9Sstevel@tonic-gate int c; 585*7c478bd9Sstevel@tonic-gate FILE *fi; 586*7c478bd9Sstevel@tonic-gate static char pbuf[PASS_MAX + 1]; 587*7c478bd9Sstevel@tonic-gate void (*saved_handler)(); 588*7c478bd9Sstevel@tonic-gate char *rval = pbuf; 589*7c478bd9Sstevel@tonic-gate int i = 0; 590*7c478bd9Sstevel@tonic-gate 591*7c478bd9Sstevel@tonic-gate if ((fi = fopen(devname, "r")) == NULL) 592*7c478bd9Sstevel@tonic-gate fi = stdin; 593*7c478bd9Sstevel@tonic-gate else 594*7c478bd9Sstevel@tonic-gate setbuf(fi, NULL); 595*7c478bd9Sstevel@tonic-gate 596*7c478bd9Sstevel@tonic-gate saved_handler = signal(SIGINT, SIG_IGN); 597*7c478bd9Sstevel@tonic-gate 598*7c478bd9Sstevel@tonic-gate ttyb = ttymodes; 599*7c478bd9Sstevel@tonic-gate ttyb.c_lflag &= ~(ECHO | ECHOE | ECHONL); 600*7c478bd9Sstevel@tonic-gate (void) ioctl(fileno(fi), TCSETAF, &ttyb); 601*7c478bd9Sstevel@tonic-gate 602*7c478bd9Sstevel@tonic-gate while ((c = getc(fi)) != '\n') { 603*7c478bd9Sstevel@tonic-gate 604*7c478bd9Sstevel@tonic-gate if (c == EOF && i == 0) { /* ^D, No password */ 605*7c478bd9Sstevel@tonic-gate rval = NULL; 606*7c478bd9Sstevel@tonic-gate break; 607*7c478bd9Sstevel@tonic-gate } 608*7c478bd9Sstevel@tonic-gate 609*7c478bd9Sstevel@tonic-gate if (i < PASS_MAX) 610*7c478bd9Sstevel@tonic-gate pbuf[i++] = c; 611*7c478bd9Sstevel@tonic-gate } 612*7c478bd9Sstevel@tonic-gate pbuf[i] = '\0'; 613*7c478bd9Sstevel@tonic-gate (void) fputc('\n', fi); 614*7c478bd9Sstevel@tonic-gate (void) ioctl(fileno(fi), TCSETAW, &ttymodes); 615*7c478bd9Sstevel@tonic-gate 616*7c478bd9Sstevel@tonic-gate if (saved_handler != SIG_ERR) 617*7c478bd9Sstevel@tonic-gate (void) signal(SIGINT, saved_handler); 618*7c478bd9Sstevel@tonic-gate 619*7c478bd9Sstevel@tonic-gate return (rval); 620*7c478bd9Sstevel@tonic-gate } 621*7c478bd9Sstevel@tonic-gate 622*7c478bd9Sstevel@tonic-gate static char * 623*7c478bd9Sstevel@tonic-gate findttyname(int fd) 624*7c478bd9Sstevel@tonic-gate { 625*7c478bd9Sstevel@tonic-gate char *ttyn = ttyname(fd); 626*7c478bd9Sstevel@tonic-gate 627*7c478bd9Sstevel@tonic-gate if (ttyn == NULL) 628*7c478bd9Sstevel@tonic-gate ttyn = "/dev/???"; 629*7c478bd9Sstevel@tonic-gate else { 630*7c478bd9Sstevel@tonic-gate /* 631*7c478bd9Sstevel@tonic-gate * /dev/syscon and /dev/systty are usually links to 632*7c478bd9Sstevel@tonic-gate * /dev/console. prefer /dev/console. 633*7c478bd9Sstevel@tonic-gate */ 634*7c478bd9Sstevel@tonic-gate if (((strcmp(ttyn, "/dev/syscon") == 0) || 635*7c478bd9Sstevel@tonic-gate (strcmp(ttyn, "/dev/systty") == 0)) && 636*7c478bd9Sstevel@tonic-gate access("/dev/console", F_OK)) 637*7c478bd9Sstevel@tonic-gate ttyn = "/dev/console"; 638*7c478bd9Sstevel@tonic-gate } 639*7c478bd9Sstevel@tonic-gate return (ttyn); 640*7c478bd9Sstevel@tonic-gate } 641*7c478bd9Sstevel@tonic-gate 642*7c478bd9Sstevel@tonic-gate static char * 643*7c478bd9Sstevel@tonic-gate stripttyname(char *ttyn) 644*7c478bd9Sstevel@tonic-gate { 645*7c478bd9Sstevel@tonic-gate /* saw off the /dev/ */ 646*7c478bd9Sstevel@tonic-gate if (strncmp(ttyn, "/dev/", sizeof ("/dev/") -1) == 0) 647*7c478bd9Sstevel@tonic-gate return (ttyn + sizeof ("/dev/") - 1); 648*7c478bd9Sstevel@tonic-gate else 649*7c478bd9Sstevel@tonic-gate return (ttyn); 650*7c478bd9Sstevel@tonic-gate } 651*7c478bd9Sstevel@tonic-gate 652*7c478bd9Sstevel@tonic-gate 653*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 654*7c478bd9Sstevel@tonic-gate static void 655*7c478bd9Sstevel@tonic-gate noop(int sig) 656*7c478bd9Sstevel@tonic-gate { 657*7c478bd9Sstevel@tonic-gate /* 658*7c478bd9Sstevel@tonic-gate * This signal handler does nothing except return. We use it 659*7c478bd9Sstevel@tonic-gate * as the signal disposition in this program instead of 660*7c478bd9Sstevel@tonic-gate * SIG_IGN so that we do not have to restore the disposition 661*7c478bd9Sstevel@tonic-gate * back to SIG_DFL. Instead we allow exec(2) to set the 662*7c478bd9Sstevel@tonic-gate * dispostion to SIG_DFL to avoid a race condition. 663*7c478bd9Sstevel@tonic-gate */ 664*7c478bd9Sstevel@tonic-gate } 665*7c478bd9Sstevel@tonic-gate 666*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 667*7c478bd9Sstevel@tonic-gate static void 668*7c478bd9Sstevel@tonic-gate parenthandler(int sig, siginfo_t *si, ucontext_t *uc) 669*7c478bd9Sstevel@tonic-gate { 670*7c478bd9Sstevel@tonic-gate int i; 671*7c478bd9Sstevel@tonic-gate 672*7c478bd9Sstevel@tonic-gate /* 673*7c478bd9Sstevel@tonic-gate * We get here if someone has successfully entered a password 674*7c478bd9Sstevel@tonic-gate * from the auxiliary console and is getting the single-user shell. 675*7c478bd9Sstevel@tonic-gate * When this happens, the parent needs to kill the children 676*7c478bd9Sstevel@tonic-gate * that didn't get the shell. 677*7c478bd9Sstevel@tonic-gate * 678*7c478bd9Sstevel@tonic-gate */ 679*7c478bd9Sstevel@tonic-gate for (i = 0; i < nchild; i++) { 680*7c478bd9Sstevel@tonic-gate if (pidlist[i] != si->__data.__proc.__pid) 681*7c478bd9Sstevel@tonic-gate (void) sigsend(P_PID, pidlist[i], SIGTERM); 682*7c478bd9Sstevel@tonic-gate } 683*7c478bd9Sstevel@tonic-gate sa.sa_handler = SIG_IGN; 684*7c478bd9Sstevel@tonic-gate sa.sa_flags = 0; 685*7c478bd9Sstevel@tonic-gate (void) sigemptyset(&sa.sa_mask); 686*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &sa, NULL); 687*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGQUIT, &sa, NULL); 688*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGTERM, &sa, NULL); 689*7c478bd9Sstevel@tonic-gate (void) wait(NULL); 690*7c478bd9Sstevel@tonic-gate } 691*7c478bd9Sstevel@tonic-gate 692*7c478bd9Sstevel@tonic-gate /* 693*7c478bd9Sstevel@tonic-gate * The master pid will get SIGTERM or SIGHUP from init, and then 694*7c478bd9Sstevel@tonic-gate * has to make sure the shell isn't still running. 695*7c478bd9Sstevel@tonic-gate */ 696*7c478bd9Sstevel@tonic-gate 697*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 698*7c478bd9Sstevel@tonic-gate static void 699*7c478bd9Sstevel@tonic-gate childcleanup(int sig) 700*7c478bd9Sstevel@tonic-gate { 701*7c478bd9Sstevel@tonic-gate int i; 702*7c478bd9Sstevel@tonic-gate 703*7c478bd9Sstevel@tonic-gate /* Only need to kill the child that became the shell. */ 704*7c478bd9Sstevel@tonic-gate for (i = 0; i < nchild; i++) { 705*7c478bd9Sstevel@tonic-gate /* Don't kill gramps before his time */ 706*7c478bd9Sstevel@tonic-gate if (pidlist[i] != getppid()) 707*7c478bd9Sstevel@tonic-gate (void) sigsend(P_PID, pidlist[i], SIGHUP); 708*7c478bd9Sstevel@tonic-gate } 709*7c478bd9Sstevel@tonic-gate } 710*7c478bd9Sstevel@tonic-gate 711*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 712*7c478bd9Sstevel@tonic-gate static void 713*7c478bd9Sstevel@tonic-gate termhandler(int sig) 714*7c478bd9Sstevel@tonic-gate { 715*7c478bd9Sstevel@tonic-gate FILE *fi; 716*7c478bd9Sstevel@tonic-gate pid_t pid; 717*7c478bd9Sstevel@tonic-gate 718*7c478bd9Sstevel@tonic-gate /* Processes come here when they fail to receive the password. */ 719*7c478bd9Sstevel@tonic-gate if ((fi = fopen("/dev/tty", "r+")) == NULL) 720*7c478bd9Sstevel@tonic-gate fi = stdin; 721*7c478bd9Sstevel@tonic-gate else 722*7c478bd9Sstevel@tonic-gate setbuf(fi, NULL); 723*7c478bd9Sstevel@tonic-gate sanitize_tty(fileno(fi)); 724*7c478bd9Sstevel@tonic-gate /* If you're the controlling tty, then just wait */ 725*7c478bd9Sstevel@tonic-gate pid = getpid(); 726*7c478bd9Sstevel@tonic-gate if (pid == originalpid || pid == masterpid) { 727*7c478bd9Sstevel@tonic-gate sa.sa_handler = SIG_IGN; 728*7c478bd9Sstevel@tonic-gate sa.sa_flags = 0; 729*7c478bd9Sstevel@tonic-gate (void) sigemptyset(&sa.sa_mask); 730*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &sa, NULL); 731*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGQUIT, &sa, NULL); 732*7c478bd9Sstevel@tonic-gate sa.sa_handler = SIG_DFL; 733*7c478bd9Sstevel@tonic-gate sa.sa_flags = 0; 734*7c478bd9Sstevel@tonic-gate (void) sigemptyset(&sa.sa_mask); 735*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGTERM, &sa, NULL); 736*7c478bd9Sstevel@tonic-gate (void) sigaction(SIGHUP, &sa, NULL); 737*7c478bd9Sstevel@tonic-gate (void) wait(NULL); 738*7c478bd9Sstevel@tonic-gate } 739*7c478bd9Sstevel@tonic-gate exit(0); 740*7c478bd9Sstevel@tonic-gate } 741