home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1989, 1990, John F. Haugh II
- * All rights reserved.
- *
- * Use, duplication, and disclosure prohibited without
- * the express written permission of the author.
- */
-
- #include <sys/types.h>
- #include <stdio.h>
- #include <pwd.h>
- #include <utmp.h>
- #ifndef BSD
- #include <string.h>
- #include <memory.h>
- #else
- #include <strings.h>
- #define strchr index
- #define strrchr rindex
- #endif
- #include "config.h"
-
- #ifndef lint
- static char _sccsid[] = "@(#)sulogin.c 2.3.1.1 08:28:36 12/5/90";
- #endif
-
- char name[BUFSIZ];
- char pass[BUFSIZ];
- char home[BUFSIZ];
- char prog[BUFSIZ];
- char mail[BUFSIZ];
-
- struct passwd pwent;
- struct utmp utent;
-
- #ifdef TZ
- FILE *tzfile;
- char tzbuf[16] = TZ;
- #endif
-
- #ifndef MAXENV
- #define MAXENV 64
- #endif
-
- char *newenvp[MAXENV];
- int newenvc = 0;
- int maxenv = MAXENV;
- extern char **environ;
-
- #ifndef ALARM
- #define ALARM 60
- #endif
-
- #ifndef RETRIES
- #define RETRIES 3
- #endif
-
- int main (argc, argv, envp)
- int argc;
- char **argv;
- char **envp;
- {
- char *getenv ();
- char *ttyname ();
- char *cp;
-
- if (access (PWDFILE, 0) == -1) { /* must be a password file! */
- printf ("No password file\n");
- exit (1);
- }
- #ifdef NDEBUG
- if (getppid () != 1) /* parent must be INIT */
- exit (1);
- #endif
- if (! isatty (0)) /* must be a terminal */
- exit (1);
-
- while (*envp) /* add inherited environment, */
- addenv (*envp++); /* some variables change later */
-
- #ifdef TZ
- if (tzbuf[0] == '/') {
- if ((tzfile = fopen (tzbuf, "r")) != (FILE *) 0) {
- if (fgets (tzbuf, sizeof tzbuf, tzfile)) {
- tzbuf[strlen (tzbuf) - 1] = '\0';
- addenv (tzbuf);
- }
- fclose (tzfile);
- }
- } else {
- addenv (tzbuf);
- }
- #endif
- #ifdef HZ
- addenv (HZ); /* set the default $HZ, if one */
- #endif
- (void) strcpy (name, "root"); /* KLUDGE!!! */
-
- alarm (ALARM);
- while (1) { /* repeatedly get login/password pairs */
- entry (name, &pwent); /* get entry from password file */
- if (pwent.pw_name == (char *) 0) {
- printf ("No password entry for 'root'\n");
- exit (1);
- }
-
- /*
- * Here we prompt for the root password, or if no password is
- * given we just exit.
- */
-
- /* get a password for root */
- if (! password ("Type control-d for normal startup,\n\
- (or give root password for system maintenance):", pass))
- exit (0);
-
- if (valid (pass, &pwent)) /* check encrypted passwords ... */
- break; /* ... encrypted passwords matched */
-
- puts ("Login incorrect");
- }
- alarm (0);
- environ = newenvp; /* make new environment active */
-
- puts ("Entering System Maintenance Mode");
-
- /*
- * Normally there would be a utmp entry for login to mung on
- * to get the tty name, date, etc. from. We don't need all that
- * stuff because we won't update the utmp or wtmp files. BUT!,
- * we do need the tty name so we can set the permissions and
- * ownership.
- */
-
- if (cp = ttyname (0)) { /* found entry in /dev/ */
- if (strrchr (cp, '/') != (char *) 0)
- strcpy (utent.ut_line, strrchr (cp, '/') + 1);
- else
- strcpy (utent.ut_line, cp);
- }
- if (getenv ("IFS")) /* don't export user IFS ... */
- addenv ("IFS= \t\n"); /* ... instead, set a safe IFS */
-
- setup (&pwent); /* set UID, GID, HOME, etc ... */
-
- shell (pwent.pw_shell); /* exec the shell finally. */
- /*NOTREACHED*/
- }
-