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

  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <pwd.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include "config.h"
  7. #include "lastlog.h"
  8.  
  9. #ifndef    MAXENV
  10. #define    MAXENV    64
  11. #endif
  12.  
  13. char    name[BUFSIZ];
  14. char    pass[BUFSIZ];
  15. char    home[BUFSIZ];
  16. char    prog[BUFSIZ];
  17. char    mail[BUFSIZ];
  18. char    oldname[BUFSIZ];
  19. char    *newenvp[MAXENV];
  20. int    newenvc = 0;
  21. int    maxenv = MAXENV;
  22. struct    passwd    pwent;
  23.  
  24. void    addenv ();
  25. void    entry ();
  26. void    sulog ();
  27. void    subsystem ();
  28. void    setup ();
  29. void    motd ();
  30. void    mailcheck ();
  31. void    shell ();
  32.  
  33. extern    char    **environ;
  34.  
  35. int    main (argc, argv, envp)
  36. int    argc;
  37. char    **argv;
  38. char    **envp;
  39. {
  40.     char    *getenv ();
  41.     int    doshell;
  42.     int    fakelogin = 0;
  43.     int    amroot;
  44.     struct    passwd    *pw;
  45.     struct    passwd    *getpwuid ();
  46.  
  47.     while (*envp)            /* add inherited environment, */
  48.         addenv (*envp++);    /* some variables change later */
  49.  
  50. #ifdef    TZ
  51.     addenv (TZ);            /* set the default $TZ, if one */
  52. #endif
  53. #ifdef    HZ
  54.     addenv (HZ);            /* set the default $HZ, if one */
  55. #endif
  56.     argc--; argv++;            /* shift out command name */
  57.  
  58.     if (argc > 0 && argv[0][0] == '-' && argv[0][1] == '\0') {
  59.         fakelogin = 1;
  60.         argc--; argv++;        /* shift ... */
  61.     }
  62.     if (argc > 0 && argv[0][0] != '-') {
  63.         (void) strcpy (name, argv[0]);    /* use this login id */
  64.         argc--; argv++;        /* shift ... */
  65.     }
  66.     doshell = argc == 0;        /* any arguments remaining? */
  67.  
  68.     if (pw = getpwuid (getuid ()))    /* need old user name */
  69.         (void) strcpy (oldname, pw->pw_name);
  70.     else                /* user ID MUST exist */
  71.         goto failure;
  72.  
  73.     amroot = getuid () == 0;    /* currently am super user */
  74.  
  75.     if (! name[0])             /* use default user ID */
  76.         (void) strcpy (name, "root");
  77.  
  78.     entry (name, &pwent);        /* get password file entry */
  79.  
  80.     if (pwent.pw_name == (char *) 0) { /* unknown user */
  81.         (void) fprintf (stderr, "Unknown id: %s\n", pwent.pw_name);
  82.         exit (1);
  83.     }
  84.  
  85.     /*
  86.      * Here we have a sticky situation.  Some accounts may have no
  87.      * password entry in the password file.  So, we don't ask for a
  88.      * password.  Others, have a blank password entered - you be the
  89.      * judge.  The conditional compilation NOBLANK requires even
  90.      * blank passwords to be prompted for.  This may well break
  91.      * quite a few systems.  Use with discretion.
  92.      */
  93.  
  94. #ifdef    NOBLANK
  95.     if (! amroot && ! password ("Password:", pass))
  96.         goto failure;
  97. #else
  98.     if (! amroot && (pwent.pw_name == (char *) 0 || pwent.pw_passwd)
  99.             && ! password ("Password:", pass))
  100.         goto failure;
  101. #endif
  102.                     /* check encrypted passwords ... */
  103.     if (! amroot && ! valid (pass, &pwent)) {
  104. failure:    sulog (0);        /* log failed attempt */
  105.         puts ("Sorry.");
  106.         exit (1);
  107.     }
  108. #ifdef    SULOG
  109.     sulog (1);            /* save SU information */
  110. #endif
  111.     if (pwent.pw_uid == 0)
  112.         addenv (SUPATH);
  113.     else
  114.         addenv (PATH);
  115.  
  116.     environ = newenvp;        /* make new environment active */
  117.  
  118.     if (getenv ("IFS"))        /* don't export user IFS ... */
  119.         addenv ("IFS= \t\n");    /* ... instead, set a safe IFS */
  120.  
  121.     if (doshell && pwent.pw_shell[0] == '*') /* subsystem root required */
  122.         subsystem ();        /* figure out what to execute */
  123.  
  124.     if (fakelogin)
  125.         setup (&pwent);        /* set UID, GID, HOME, etc ... */
  126.     else {
  127.         if (setgid (pwent.pw_gid) || setuid (pwent.pw_uid))  {
  128.             perror ("Can't set ID");
  129.             exit (1);
  130.         }
  131.     }
  132.     if (! doshell) {        /* execute arguments as command */
  133.         if (! pwent.pw_shell)
  134.             pwent.pw_shell = "/bin/sh";
  135.  
  136.         argv[-1] = pwent.pw_shell;
  137.         (void) execv (pwent.pw_shell, &argv[-1]);
  138.         (void) fprintf (stderr, "No shell\n");
  139.         exit (1);
  140.     }
  141.     if (fakelogin) {
  142. #ifdef    MOTD
  143.         motd ();        /* print the message of the day */
  144. #endif
  145. #ifdef    MAILCHECK
  146.         mailcheck ();        /* report on the status of mail */
  147. #endif
  148.         shell (pwent.pw_shell);    /* exec the shell finally. */
  149.     } else {
  150.         if (pwent.pw_shell == (char *) 0)
  151.             pwent.pw_shell = "/bin/sh";
  152.  
  153.         execl (pwent.pw_shell, "su", (char *) 0);
  154.         perror (pwent.pw_shell);
  155.         exit (1);
  156.     }
  157.     /*NOTREACHED*/
  158. }
  159.