1: #include <whoami.h> 2: #include <stdio.h> 3: #include <pwd.h> 4: #include <sys/types.h> 5: #include <sys/stat.h> 6: 7: #define DFLT_SHELL "/bin/sh" 8: #define PATH "PATH=:/bin:/usr/ucb:/usr/bin" /* default path */ 9: #define SULOGFILE "/usr/adm/sulog" 10: 11: struct passwd *pwd,*getpwnam(); 12: char *crypt(); 13: char *getpass(); 14: char *getenv(); 15: char **environ; 16: int ruid, rgid; 17: char homedir[64] = "HOME="; 18: char term[64] = "TERM=unknown"; 19: char shell[64] = "SHELL="; 20: char user[64] = "USER="; 21: 22: char *envinit[8] = {homedir, PATH, term, shell, user, 0, 0}; 23: #define E_TERMCAP 5 /* where to put termcap in envinit */ 24: 25: main(argc,argv) 26: int argc; 27: char **argv; 28: { 29: register char **p; 30: char *nptr; 31: char *password; 32: register char *cp; 33: register char *cp2; 34: char hometmp[50]; 35: char *sh = DFLT_SHELL; 36: int minusflag = 0; 37: int new_uid, new_gid; 38: 39: if (argc > 1 && argv[1][0]=='-') { 40: minusflag++; 41: argc--; 42: argv++; 43: } 44: if(argc > 1) 45: nptr = argv[1]; 46: else 47: nptr = "root"; 48: if((pwd=getpwnam(nptr)) == NULL) { 49: printf("Unknown id: %s\n",nptr); 50: exit(1); 51: } 52: ruid = getuid(); 53: rgid = getgid(); 54: /* 55: * If the new user has no password, force the use 56: * of his shell. 57: */ 58: if (pwd->pw_passwd[0] == '\0') 59: minusflag++; 60: if(pwd->pw_passwd[0] == '\0' || ruid == 0) 61: goto ok; 62: #ifdef UCB_GRPMAST 63: if ((ruid == rgid) && (rgid == pwd->pw_gid)) 64: goto ok; 65: #endif 66: password = getpass("Password: "); 67: if(strcmp(pwd->pw_passwd, crypt(password, pwd->pw_passwd)) != 0) { 68: if (pwd->pw_uid == 0) 69: sulog(ruid, nptr, password); 70: printf("Sorry\n"); 71: exit(2); 72: } 73: 74: ok: 75: endpwent(); 76: 77: /* 78: * ``All information is contained in a static area so it must be 79: * copied if it is to be saved.'' - getpwent(3) 80: */ 81: new_uid = pwd->pw_uid; 82: new_gid = pwd->pw_gid; 83: 84: if (pwd->pw_uid == 0) 85: sulog(ruid, nptr, (char *) NULL); 86: setgid(new_gid); 87: setuid(new_uid); 88: if (minusflag) { 89: if (pwd->pw_shell && *pwd->pw_shell) 90: sh = pwd->pw_shell; 91: strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 92: strncat(shell, sh, sizeof(shell)-7); 93: strncat(user, pwd->pw_name, sizeof(user)-5); 94: if ((cp=getenv("TERM")) != NULL) 95: strncpy(term+5, cp, sizeof(term)-5); 96: for (p=environ; *p; p++) 97: if (strncmp(*p, "TERMCAP=", 8) == 0) { 98: envinit[E_TERMCAP] = *p; 99: break; 100: } 101: if (chdir(pwd->pw_dir) < 0) 102: perror(pwd->pw_dir); 103: environ = envinit; 104: } else { 105: if ((cp=getenv("SHELL")) != NULL) 106: sh = cp; 107: } 108: if(strcmp(sh,"/bin/csh") != 0) 109: execl(sh, "su", 0); 110: else 111: execl(sh, "_su", 0); 112: printf("No shell\n"); 113: exit(3); 114: } 115: 116: sulog(whoid, whoto, password) 117: register char *whoto, *password; 118: { 119: register FILE *logf; 120: int i; 121: long now; 122: char *ttyn, *ttyname(); 123: struct stat statb; 124: struct passwd *getpwuid(); 125: 126: if (stat(SULOGFILE, &statb) < 0) 127: return; 128: if ((logf = fopen (SULOGFILE, "a")) == NULL) 129: return; 130: 131: for (i = 0; i < 3; i++) 132: if ((ttyn = ttyname(i)) != NULL) 133: break; 134: time (&now); 135: fprintf (logf, "%24.24s %-8.8s %-8.8s-> %-8.8s ", 136: ctime(&now), ttyn+5, getpwuid(whoid)->pw_name, whoto); 137: if (password == (char *) 0) 138: fprintf(logf, "OK\n"); 139: else 140: fprintf(logf, "BAD: PW = '%s'\n", password); 141: fclose (logf); 142: }