home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume6 / shadow-2.pt2 / sulogin.c < prev   
Encoding:
C/C++ Source or Header  |  1989-02-03  |  2.5 KB  |  110 lines

  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <pwd.h>
  4. #include <utmp.h>
  5. #include "config.h"
  6. #include "lastlog.h"
  7.  
  8. char    name[BUFSIZ];
  9. char    pass[BUFSIZ];
  10. char    home[BUFSIZ];
  11. char    prog[BUFSIZ];
  12. char    mail[BUFSIZ];
  13.  
  14. struct    passwd    pwent;
  15. struct    utmp    utent;
  16. struct    lastlog    lastlog;
  17.  
  18. #ifndef    MAXENV
  19. #define    MAXENV    64
  20. #endif
  21.  
  22. char    *newenvp[MAXENV];
  23. int    newenvc = 0;
  24. int    maxenv = MAXENV;
  25. extern    char    **environ;
  26.  
  27. #ifndef    ALARM
  28. #define    ALARM    60
  29. #endif
  30.  
  31. #ifndef    RETRIES
  32. #define    RETRIES    3
  33. #endif
  34.  
  35. int    main (argc, argv, envp)
  36. int    argc;
  37. char    **argv;
  38. char    **envp;
  39. {
  40.     char    *getenv ();
  41.     char    *ttyname ();
  42.     char    *cp;
  43.  
  44.     if (access (PWDFILE, 0) == -1) { /* must be a password file! */
  45.         printf ("No password file\n");
  46.         exit (1);
  47.     }
  48. #ifndef    DEBUG
  49.     if (getppid () != 1)        /* parent must be INIT */
  50.         exit (1);
  51. #endif
  52.     if (! isatty (0))        /* must be a terminal */
  53.         exit (1);
  54.  
  55.     while (*envp)            /* add inherited environment, */
  56.         addenv (*envp++);    /* some variables change later */
  57.  
  58. #ifdef    TZ
  59.     addenv (TZ);            /* set the default $TZ, if one */
  60. #endif
  61. #ifdef    HZ
  62.     addenv (HZ);            /* set the default $HZ, if one */
  63. #endif
  64.     (void) strcpy (name, "root");    /* KLUDGE!!! */
  65.  
  66.     while (1) {        /* repeatedly get login/password pairs */
  67.         entry (name, &pwent);    /* get entry from password file */
  68.         if (pwent.pw_name == (char *) 0) {
  69.             printf ("No password entry for 'root'\n");
  70.             exit (1);
  71.         }
  72.  
  73.     /*
  74.      * Here we prompt for the root password, or if no password is
  75.      * given we just exit and let INIT go to runlevel 2.
  76.      */
  77.  
  78.                     /* get a password for root */
  79.         if (! password ("Type control-d for normal startup,\n(or give root password for system maintenance):", pass))
  80.             exit (0);
  81.  
  82.         if (valid (pass, &pwent)) /* check encrypted passwords ... */
  83.             break;        /* ... encrypted passwords matched */
  84.  
  85.         puts ("Login incorrect");
  86.     }
  87.     environ = newenvp;        /* make new environment active */
  88.  
  89.     puts ("Entering System Maintenance Mode");
  90.  
  91.     /*
  92.      * Normally there would be a utmp entry for login to mung on
  93.      * to get the tty name, date, etc. from.  We don't need all that
  94.      * stuff because we won't update the utmp or wtmp files.  BUT!,
  95.      * we do need the tty name so we can set the permissions and
  96.      * ownership.
  97.      */
  98.  
  99.     if (cp = ttyname (0))        /* found entry in /dev/ */
  100.         strcpy (utent.ut_line, cp); /* needed for tty perms (setup) */
  101.         
  102.     if (getenv ("IFS"))        /* don't export user IFS ... */
  103.         addenv ("IFS= \t\n");    /* ... instead, set a safe IFS */
  104.  
  105.     setup (&pwent);            /* set UID, GID, HOME, etc ... */
  106.  
  107.     shell (pwent.pw_shell);        /* exec the shell finally. */
  108.     /*NOTREACHED*/
  109. }
  110.