home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / su.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-05-05  |  883 b   |  51 lines

  1. #include <stdio.h>
  2. #include <pwd.h>
  3.  
  4. struct    passwd *pwd,*getpwnam();
  5. char    *crypt();
  6. char    *getpass();
  7. char    **environ;
  8.  
  9. main(argc,argv)
  10. int    argc;
  11. char    **argv;
  12. {
  13.     register char **p;
  14.     char *nptr;
  15.     char *password;
  16.     int badsw = 0;
  17.     char *shell = "/bin/sh";
  18.  
  19.     if(argc > 1)
  20.         nptr = argv[1];
  21.     else
  22.         nptr = "root";
  23.     if((pwd=getpwnam(nptr)) == NULL) {
  24.         printf("Unknown id: %s\n",nptr);
  25.         exit(1);
  26.     }
  27.     if(pwd->pw_passwd[0] == '\0' || getuid() == 0)
  28.         goto ok;
  29.     password = getpass("Password:");
  30.     if(badsw || (strcmp(pwd->pw_passwd, crypt(password, pwd->pw_passwd)) != 0)) {
  31.         printf("Sorry\n");
  32.         exit(2);
  33.     }
  34.  
  35. ok:
  36.     endpwent();
  37.     setgid(pwd->pw_gid);
  38.     setuid(pwd->pw_uid);
  39.     if (pwd->pw_shell && *pwd->pw_shell)
  40.         shell = pwd->pw_shell;
  41.     for (p=environ; *p; p++) {
  42.         if (strncmp("PS1=", *p, 4) == 0) {
  43.             *p = "PS1=# ";
  44.             break;
  45.         }
  46.     }
  47.     execl(shell, "su", 0);
  48.     printf("No shell\n");
  49.     exit(3);
  50. }
  51.