home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / shadow-2.pt3 / lmain.c < prev    next >
C/C++ Source or Header  |  1989-02-03  |  4KB  |  165 lines

  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <pwd.h>
  4. #include <utmp.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include "config.h"
  9. #include "lastlog.h"
  10.  
  11. char    name[BUFSIZ];
  12. char    pass[BUFSIZ];
  13. char    home[BUFSIZ];
  14. char    prog[BUFSIZ];
  15. char    mail[BUFSIZ];
  16.  
  17. struct    passwd    pwent;
  18. struct    utmp    utent;
  19. struct    lastlog    lastlog;
  20.  
  21. #ifndef    MAXENV
  22. #define    MAXENV    64
  23. #endif
  24.  
  25. char    *newenvp[MAXENV];
  26. int    newenvc = 0;
  27. int    maxenv = MAXENV;
  28. extern    char    **environ;
  29.  
  30. char    *getenv ();
  31. char    *memset ();
  32. void    checkutmp ();
  33. void    addenv ();
  34. void    setenv ();
  35. unsigned alarm ();
  36. void    login ();
  37. void    entry ();
  38. void    setutmp ();
  39. void    subsystem ();
  40. void    log ();
  41. void    setup ();
  42. void    expire ();
  43. void    motd ();
  44. void    mailcheck ();
  45. void    shell ();
  46.  
  47. #ifndef    ALARM
  48. #define    ALARM    60
  49. #endif
  50.  
  51. #ifndef    RETRIES
  52. #define    RETRIES    3
  53. #endif
  54.  
  55. int    main (argc, argv, envp)
  56. int    argc;
  57. char    **argv;
  58. char    **envp;
  59. {
  60.     int    retries = RETRIES;
  61.  
  62.     checkutmp ();            /* must be lowest level shell */
  63.  
  64.     if (! isatty (0))        /* must be a terminal */
  65.         exit (1);
  66.  
  67.     while (*envp)            /* add inherited environment, */
  68.         addenv (*envp++);    /* some variables change later */
  69.  
  70. #ifdef    TZ
  71.     addenv (TZ);            /* set the default $TZ, if one */
  72. #endif
  73. #ifdef    HZ
  74.     addenv (HZ);            /* set the default $HZ, if one */
  75. #endif
  76.     if (argc >= 2) {        /* now set command line variables */
  77.         setenv (argc - 2, &argv[2]);
  78.         (void) strncpy (name, argv[1], sizeof name);
  79.     }
  80.     (void) alarm (ALARM);        /* only allow ALARM sec. for login */
  81.  
  82.     while (1) {        /* repeatedly get login/password pairs */
  83.         if (! name[0]) {    /* need to get a login id */
  84.             login (name);
  85.             continue;
  86.         }
  87.         entry (name, &pwent);    /* get entry from password file */
  88.  
  89.     /*
  90.      * Here we have a sticky situation.  Some accounts may have no
  91.      * password entry in the password file.  So, we don't ask for a
  92.      * password.  Others, have a blank password entered - you be the
  93.      * judge.  The conditional compilation NOBLANK requires even
  94.      * blank passwords to be prompted for.  This may well break
  95.      * quite a few systems.  Use with discretion.
  96.      */
  97.  
  98. #ifdef    NOBLANK
  99.                     /* get a password from user */
  100.         if (! password ("Password:", pass))
  101.             continue;
  102. #else
  103.         if ((! pwent.pw_name || pwent.pw_passwd)
  104.                     && ! password ("Password:", pass))
  105.             continue;
  106. #endif
  107.         if (valid (pass, &pwent)) /* check encrypted passwords ... */
  108.             break;        /* ... encrypted passwords matched */
  109.  
  110.         if (--retries <= 0)    /* only allow so many failures */
  111.             exit (1);
  112.  
  113.         puts ("Login incorrect");
  114.         (void) memset (name, '\0', sizeof name);
  115.     }
  116. #ifdef    DIALUP
  117.     if (! dialcheck (utent.ut_line,
  118.             pwent.pw_shell ? pwent.pw_shell:"/bin/sh")) {
  119.         puts ("Dialup password incorrect");
  120.         exit (1);
  121.     }
  122. #endif
  123.     (void) alarm (0);        /* turn off alarm clock */
  124.     environ = newenvp;        /* make new environment active */
  125.  
  126.     if (getenv ("IFS"))        /* don't export user IFS ... */
  127.         addenv ("IFS= \t\n");    /* ... instead, set a safe IFS */
  128.  
  129.     setutmp ();            /* make entry in utmp & wtmp files */
  130. #ifdef    CONSOLE
  131.     if (pwent.pw_uid == 0 &&    /* root no logging in on console ? */
  132.             strncmp (CONSOLE, utent.ut_line, sizeof utent.ut_line))
  133.         exit (1);        /* then exit! */
  134. #endif
  135.     if (pwent.pw_shell[0] == '*')    /* subsystem root required */
  136.         subsystem ();        /* figure out what to execute */
  137.  
  138. #ifdef    LASTLOG
  139.     log ();                /* give last login and log this one */
  140. #endif
  141.     setup (&pwent);            /* set UID, GID, HOME, etc ... */
  142. #ifdef    AGING
  143.     if (pwent.pw_age)        /* check for age of password ... */
  144.         expire (pwent.pw_age);    /* ... ask for new one if expired */
  145. #endif
  146. #ifdef    MOTD
  147.     motd ();            /* print the message of the day */
  148. #endif
  149. #ifdef    LASTLOG
  150.     if (lastlog.ll_time != 0)
  151.         printf ("Last login: %.19s on %s\n",
  152.             ctime (&lastlog.ll_time), lastlog.ll_line);
  153. #endif
  154. #ifdef    MAILCHECK
  155.     mailcheck ();            /* report on the status of mail */
  156. #endif
  157.     signal (SIGINT, SIG_DFL);    /* default interrupt signal */
  158.     signal (SIGQUIT, SIG_DFL);    /* default quit signal */
  159.     signal (SIGTERM, SIG_DFL);    /* default terminate signal */
  160.     signal (SIGALRM, SIG_DFL);    /* default alarm signal */
  161.  
  162.     shell (pwent.pw_shell);        /* exec the shell finally. */
  163.     /*NOTREACHED*/
  164. }
  165.