2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Copyright 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of Julianne F. Haugh nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2007-10-07 17:14:59 +05:30
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
|
2007-10-07 17:14:02 +05:30
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-10-07 17:17:01 +05:30
|
|
|
#ident "$Id: sulogin.c,v 1.23 2005/09/07 15:00:45 kloczek Exp $"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
2007-10-07 17:17:01 +05:30
|
|
|
#include "defines.h"
|
|
|
|
#include "getdef.h"
|
|
|
|
#include "prototypes.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "pwauth.h"
|
2007-10-07 17:17:01 +05:30
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
static char name[BUFSIZ];
|
|
|
|
static char pass[BUFSIZ];
|
|
|
|
|
|
|
|
static struct passwd pwent;
|
2007-10-07 17:14:59 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
extern char **newenvp;
|
|
|
|
extern size_t newenvc;
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
extern char **environ;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifndef ALARM
|
|
|
|
#define ALARM 60
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* local function prototypes */
|
2007-10-07 17:14:59 +05:30
|
|
|
static RETSIGTYPE catch (int);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static RETSIGTYPE catch (int sig)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
/*
|
|
|
|
* syslogd is usually not running at the time when sulogin is typically
|
|
|
|
* called, cluttering the screen with unnecessary messages. Suggested by
|
|
|
|
* Ivan Nejgebauer <ian@unsux.ns.ac.yu>. --marekm
|
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
#undef USE_SYSLOG
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
/*ARGSUSED*/ int main (int argc, char **argv)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
char *cp;
|
|
|
|
char **envp = environ;
|
|
|
|
TERMIO termio;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifdef USE_TERMIO
|
|
|
|
ioctl (0, TCGETA, &termio);
|
2007-10-07 17:14:59 +05:30
|
|
|
termio.c_iflag |= (ICRNL | IXON);
|
|
|
|
termio.c_oflag |= (OPOST | ONLCR);
|
2007-10-07 17:14:02 +05:30
|
|
|
termio.c_cflag |= (CREAD);
|
2007-10-07 17:14:59 +05:30
|
|
|
termio.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK);
|
2007-10-07 17:14:02 +05:30
|
|
|
ioctl (0, TCSETAF, &termio);
|
|
|
|
#endif
|
|
|
|
#ifdef USE_TERMIOS
|
|
|
|
tcgetattr (0, &termio);
|
2007-10-07 17:14:59 +05:30
|
|
|
termio.c_iflag |= (ICRNL | IXON);
|
2007-10-07 17:14:02 +05:30
|
|
|
termio.c_oflag |= (CREAD);
|
2007-10-07 17:14:59 +05:30
|
|
|
termio.c_lflag |= (ECHO | ECHOE | ECHOK | ICANON | ISIG);
|
2007-10-07 17:14:02 +05:30
|
|
|
tcsetattr (0, TCSANOW, &termio);
|
|
|
|
#endif
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifdef USE_SYSLOG
|
2007-10-07 17:14:59 +05:30
|
|
|
OPENLOG ("sulogin");
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
initenv ();
|
2007-10-07 17:14:02 +05:30
|
|
|
if (argc > 1) {
|
|
|
|
close (0);
|
|
|
|
close (1);
|
|
|
|
close (2);
|
|
|
|
|
|
|
|
if (open (argv[1], O_RDWR) >= 0) {
|
|
|
|
dup (0);
|
|
|
|
dup (0);
|
|
|
|
} else {
|
|
|
|
#ifdef USE_SYSLOG
|
2007-10-07 17:16:34 +05:30
|
|
|
SYSLOG (LOG_WARN, "cannot open %s\n", argv[1]);
|
2007-10-07 17:14:02 +05:30
|
|
|
closelog ();
|
|
|
|
#endif
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (access (PASSWD_FILE, F_OK) == -1) { /* must be a password file! */
|
|
|
|
printf (_("No password file\n"));
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef USE_SYSLOG
|
2007-10-07 17:16:34 +05:30
|
|
|
SYSLOG (LOG_WARN, "No password file\n");
|
2007-10-07 17:14:02 +05:30
|
|
|
closelog ();
|
|
|
|
#endif
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
#if !defined(DEBUG) && defined(SULOGIN_ONLY_INIT)
|
2007-10-07 17:14:59 +05:30
|
|
|
if (getppid () != 1) { /* parent must be INIT */
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef USE_SYSLOG
|
2007-10-07 17:16:34 +05:30
|
|
|
SYSLOG (LOG_WARN, "Pid == %d, not 1\n", getppid ());
|
2007-10-07 17:14:02 +05:30
|
|
|
closelog ();
|
|
|
|
#endif
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!isatty (0) || !isatty (1) || !isatty (2)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef USE_SYSLOG
|
|
|
|
closelog ();
|
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (1); /* must be a terminal */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
while (*envp) /* add inherited environment, */
|
|
|
|
addenv (*envp++, NULL); /* some variables change later */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:16:34 +05:30
|
|
|
#ifndef USE_PAM
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if ((cp = getdef_str ("ENV_TZ")))
|
|
|
|
addenv (*cp == '/' ? tz (cp) : cp, NULL);
|
|
|
|
if ((cp = getdef_str ("ENV_HZ")))
|
|
|
|
addenv (cp, NULL); /* set the default $HZ, if one */
|
2007-10-07 17:16:34 +05:30
|
|
|
#endif /* !USE_PAM */
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
(void) strcpy (name, "root"); /* KLUDGE!!! */
|
|
|
|
|
|
|
|
signal (SIGALRM, catch); /* exit if the timer expires */
|
2007-10-07 17:14:59 +05:30
|
|
|
alarm (ALARM); /* only wait so long ... */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
while (1) { /* repeatedly get login/password pairs */
|
2007-10-07 17:14:59 +05:30
|
|
|
pw_entry (name, &pwent); /* get entry from password file */
|
2007-10-07 17:14:02 +05:30
|
|
|
if (pwent.pw_name == (char *) 0) {
|
|
|
|
/*
|
|
|
|
* Fail secure
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
printf (_("No password entry for 'root'\n"));
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef USE_SYSLOG
|
2007-10-07 17:16:34 +05:30
|
|
|
SYSLOG (LOG_WARN, "No password entry for 'root'\n");
|
2007-10-07 17:14:02 +05:30
|
|
|
closelog ();
|
|
|
|
#endif
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
/*
|
|
|
|
* Here we prompt for the root password, or if no password
|
|
|
|
* is given we just exit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* get a password for root */
|
|
|
|
cp = getpass (_
|
|
|
|
("\nType control-d to proceed with normal startup,\n(or give root password for system maintenance):"));
|
|
|
|
/*
|
|
|
|
* XXX - can't enter single user mode if root password is
|
|
|
|
* empty. I think this doesn't happen very often :-). But
|
|
|
|
* it will work with standard getpass() (no NULL on EOF).
|
|
|
|
* --marekm
|
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!cp || !*cp) {
|
|
|
|
#ifdef USE_SYSLOG
|
2007-10-07 17:16:34 +05:30
|
|
|
SYSLOG (LOG_INFO, "Normal startup\n");
|
2007-10-07 17:14:02 +05:30
|
|
|
closelog ();
|
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
puts ("\n");
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef TELINIT
|
2007-10-07 17:16:07 +05:30
|
|
|
execl (PATH_TELINIT, "telinit", RUNLEVEL, (char *) 0);
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
|
|
|
exit (0);
|
|
|
|
} else {
|
2007-10-07 17:14:59 +05:30
|
|
|
STRFCPY (pass, cp);
|
|
|
|
strzero (cp);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (valid (pass, &pwent)) /* check encrypted passwords ... */
|
|
|
|
break; /* ... encrypted passwords matched */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifdef USE_SYSLOG
|
2007-10-07 17:16:34 +05:30
|
|
|
SYSLOG (LOG_WARN, "Incorrect root password\n");
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
sleep (2);
|
2007-10-07 17:14:02 +05:30
|
|
|
puts (_("Login incorrect"));
|
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
strzero (pass);
|
2007-10-07 17:14:02 +05:30
|
|
|
alarm (0);
|
|
|
|
signal (SIGALRM, SIG_DFL);
|
2007-10-07 17:14:59 +05:30
|
|
|
environ = newenvp; /* make new environment active */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
puts (_("Entering System Maintenance Mode\n"));
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef USE_SYSLOG
|
2007-10-07 17:16:34 +05:30
|
|
|
SYSLOG (LOG_INFO, "System Maintenance Mode\n");
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_SYSLOG
|
|
|
|
closelog ();
|
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
shell (pwent.pw_shell, (char *) 0); /* exec the shell finally. */
|
|
|
|
/*NOTREACHED*/ return (0);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|