home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2286 / sulogin.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  3.1 KB  |  149 lines

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