2 * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2002 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2010, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/ioctl.h>
44 #include "prototypes.h"
47 #include "exitcodes.h"
54 static char name[BUFSIZ];
55 static char pass[BUFSIZ];
57 static struct passwd pwent;
59 extern char **newenvp;
60 extern size_t newenvc;
62 extern char **environ;
68 /* local function prototypes */
69 static RETSIGTYPE catch_signals (int);
71 static RETSIGTYPE catch_signals (unused int sig)
77 * syslogd is usually not running at the time when sulogin is typically
78 * called, cluttering the screen with unnecessary messages. Suggested by
79 * Ivan Nejgebauer <ian@unsux.ns.ac.yu>. --marekm
83 /*ARGSUSED*/ int main (int argc, char **argv)
86 char **envp = environ;
91 ioctl (0, TCGETA, &termio);
92 termio.c_iflag |= (ICRNL | IXON);
93 termio.c_oflag |= (OPOST | ONLCR);
94 termio.c_cflag |= (CREAD);
95 termio.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK);
96 ioctl (0, TCSETAF, &termio);
99 tcgetattr (0, &termio);
100 termio.c_iflag |= (ICRNL | IXON);
101 termio.c_oflag |= (CREAD);
102 termio.c_lflag |= (ECHO | ECHOE | ECHOK | ICANON | ISIG);
103 tcsetattr (0, TCSANOW, &termio);
106 Prog = Basename (argv[0]);
107 (void) setlocale (LC_ALL, "");
108 (void) bindtextdomain (PACKAGE, LOCALEDIR);
109 (void) textdomain (PACKAGE);
120 if (open (argv[1], O_RDWR) >= 0) {
125 SYSLOG (LOG_WARN, "cannot open %s\n", argv[1]);
131 if (access (PASSWD_FILE, F_OK) == -1) { /* must be a password file! */
132 (void) puts (_("No password file"));
134 SYSLOG (LOG_WARN, "No password file\n");
139 #if !defined(DEBUG) && defined(SULOGIN_ONLY_INIT)
140 if (getppid () != 1) { /* parent must be INIT */
142 SYSLOG (LOG_WARN, "Pid == %d, not 1\n", getppid ());
148 if ((isatty (0) == 0) || (isatty (1) == 0) || (isatty (2) == 0)) {
152 exit (1); /* must be a terminal */
154 /* If we were init, we need to start a new session */
155 if (getppid() == 1) {
157 if (ioctl(0, TIOCSCTTY, 1) != 0) {
158 (void) fputs (_("TIOCSCTTY failed"), stderr);
161 while (NULL != *envp) { /* add inherited environment, */
162 addenv (*envp, NULL); /* some variables change later */
168 env = getdef_str ("ENV_TZ");
170 addenv (('/' == *env) ? tz (env) : env, NULL);
172 env = getdef_str ("ENV_HZ");
174 addenv (env, NULL); /* set the default $HZ, if one */
176 #endif /* !USE_PAM */
178 (void) strcpy (name, "root"); /* KLUDGE!!! */
180 (void) signal (SIGALRM, catch_signals); /* exit if the timer expires */
181 (void) alarm (ALARM); /* only wait so long ... */
183 while (true) { /* repeatedly get login/password pairs */
185 pw_entry (name, &pwent); /* get entry from password file */
186 if (pwent.pw_name == (char *) 0) {
190 (void) puts (_("No password entry for 'root'"));
192 SYSLOG (LOG_WARN, "No password entry for 'root'\n");
199 * Here we prompt for the root password, or if no password
200 * is given we just exit.
203 /* get a password for root */
206 "Type control-d to proceed with normal startup,\n"
207 "(or give root password for system maintenance):"));
209 * XXX - can't enter single user mode if root password is
210 * empty. I think this doesn't happen very often :-). But
211 * it will work with standard getpass() (no NULL on EOF).
214 if ((NULL == cp) || ('\0' == *cp)) {
216 SYSLOG (LOG_INFO, "Normal startup\n");
221 execl (PATH_TELINIT, "telinit", RUNLEVEL, (char *) 0);
228 if (valid (pass, &pwent)) { /* check encrypted passwords ... */
229 break; /* ... encrypted passwords matched */
233 SYSLOG (LOG_WARN, "Incorrect root password\n");
236 (void) puts (_("Login incorrect"));
240 (void) signal (SIGALRM, SIG_DFL);
241 environ = newenvp; /* make new environment active */
243 (void) puts (_("Entering System Maintenance Mode"));
245 SYSLOG (LOG_INFO, "System Maintenance Mode\n");
251 /* exec the shell finally. */
252 err = shell (pwent.pw_shell, (char *) 0, environ);
254 return ((err == ENOENT) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);