home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3339 / sulogin.c < prev   
Encoding:
C/C++ Source or Header  |  1991-05-17  |  3.4 KB  |  154 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include <sys/types.h>
  13. #include <stdio.h>
  14. #include "pwd.h"
  15. #include <utmp.h>
  16. #ifdef    BSD
  17. #include <strings.h>
  18. #define    strchr    index
  19. #define    strrchr    rindex
  20. #else
  21. #include <string.h>
  22. #include <memory.h>
  23. #endif
  24. #include "config.h"
  25.  
  26. #ifndef    lint
  27. static    char    sccsid[] = "@(#)sulogin.c    3.3    12:31:35    12/12/90";
  28. #endif
  29.  
  30. char    name[BUFSIZ];
  31. char    pass[BUFSIZ];
  32. char    home[BUFSIZ];
  33. char    prog[BUFSIZ];
  34. char    mail[BUFSIZ];
  35.  
  36. struct    passwd    pwent;
  37. struct    utmp    utent;
  38.  
  39. #ifdef    TZ
  40. FILE    *tzfile;
  41. char    tzbuf[BUFSIZ] = TZ;
  42. #endif
  43.  
  44. #ifndef    MAXENV
  45. #define    MAXENV    64
  46. #endif
  47.  
  48. char    *newenvp[MAXENV];
  49. int    newenvc = 0;
  50. int    maxenv = MAXENV;
  51. extern    char    **environ;
  52.  
  53. #ifndef    ALARM
  54. #define    ALARM    60
  55. #endif
  56.  
  57. #ifndef    RETRIES
  58. #define    RETRIES    3
  59. #endif
  60.  
  61. int    main (argc, argv, envp)
  62. int    argc;
  63. char    **argv;
  64. char    **envp;
  65. {
  66.     char    *getenv ();
  67.     char    *ttyname ();
  68.     char    *cp;
  69.  
  70.     if (access (PWDFILE, 0) == -1) { /* must be a password file! */
  71.         printf ("No password file\n");
  72.         exit (1);
  73.     }
  74. #ifdef    NDEBUG
  75.     if (getppid () != 1)        /* parent must be INIT */
  76.         exit (1);
  77. #endif
  78.     if (! isatty (0) || ! isatty (1) || ! isatty (2))
  79.         exit (1);        /* must be a terminal */
  80.  
  81.     while (*envp)            /* add inherited environment, */
  82.         addenv (*envp++);    /* some variables change later */
  83.  
  84. #ifdef    TZ
  85.     if (tzbuf[0] == '/') {
  86.         if ((tzfile = fopen (tzbuf, "r")) != (FILE *) 0) {
  87.             if (fgets (tzbuf, sizeof tzbuf, tzfile)) {
  88.                 tzbuf[strlen (tzbuf) - 1] = '\0';
  89.                 addenv (tzbuf);
  90.             }
  91.             fclose (tzfile);
  92.         }
  93.     } else {
  94.         addenv (tzbuf);
  95.     }
  96. #endif    /* TZ */
  97. #ifdef    HZ
  98.     addenv (HZ);            /* set the default $HZ, if one */
  99. #endif    /* HZ */
  100.     (void) strcpy (name, "root");    /* KLUDGE!!! */
  101.  
  102.     alarm (ALARM);        /* only wait so long ... */
  103.     while (1) {        /* repeatedly get login/password pairs */
  104.         entry (name, &pwent);    /* get entry from password file */
  105.         if (pwent.pw_name == (char *) 0) {
  106.             printf ("No password entry for 'root'\n");
  107.             exit (1);
  108.         }
  109.  
  110.     /*
  111.      * Here we prompt for the root password, or if no password is
  112.      * given we just exit.
  113.      */
  114.  
  115.                     /* get a password for root */
  116.         if (! (cp = getpass ("Type control-d for normal startup,\n\
  117. (or give root password for system maintenance):")))
  118.             exit (0);
  119.         else
  120.             strcpy (pass, cp);
  121.  
  122.         if (valid (pass, &pwent)) /* check encrypted passwords ... */
  123.             break;        /* ... encrypted passwords matched */
  124.  
  125.         puts ("Login incorrect");
  126.     }
  127.     alarm (0);
  128.     environ = newenvp;        /* make new environment active */
  129.  
  130.     puts ("Entering System Maintenance Mode");
  131.  
  132.     /*
  133.      * Normally there would be a utmp entry for login to mung on
  134.      * to get the tty name, date, etc. from.  We don't need all that
  135.      * stuff because we won't update the utmp or wtmp files.  BUT!,
  136.      * we do need the tty name so we can set the permissions and
  137.      * ownership.
  138.      */
  139.  
  140.     if (cp = ttyname (0)) {        /* found entry in /dev/ */
  141.         if (strrchr (cp, '/') != (char *) 0)
  142.             strcpy (utent.ut_line, strrchr (cp, '/') + 1);
  143.         else
  144.             strcpy (utent.ut_line, cp);
  145.     }
  146.     if (getenv ("IFS"))        /* don't export user IFS ... */
  147.         addenv ("IFS= \t\n");    /* ... instead, set a safe IFS */
  148.  
  149.     setup (&pwent);            /* set UID, GID, HOME, etc ... */
  150.  
  151.     shell (pwent.pw_shell, (char *) 0); /* exec the shell finally. */
  152.     /*NOTREACHED*/
  153. }
  154.