| 1 | /* |
| 2 | * Copyright (c) 1988 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms are permitted |
| 6 | * provided that the above copyright notice and this paragraph are |
| 7 | * duplicated in all such forms and that any documentation, |
| 8 | * advertising materials, and other materials related to such |
| 9 | * distribution and use acknowledge that the software was developed |
| 10 | * by the University of California, Berkeley. The name of the |
| 11 | * University may not be used to endorse or promote products derived |
| 12 | * from this software without specific prior written permission. |
| 13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 16 | */ |
| 17 | |
| 18 | #ifndef lint |
| 19 | char copyright[] = |
| 20 | "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ |
| 21 | All rights reserved.\n"; |
| 22 | #endif /* not lint */ |
| 23 | |
| 24 | #ifndef lint |
| 25 | static char sccsid[] = "@(#)su.c 5.7 (Berkeley) %G%"; |
| 26 | #endif /* not lint */ |
| 27 | |
| 28 | #include <sys/param.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <sys/resource.h> |
| 31 | #include <syslog.h> |
| 32 | #include <stdio.h> |
| 33 | #include <pwd.h> |
| 34 | #include <grp.h> |
| 35 | |
| 36 | main(argc, argv) |
| 37 | int argc; |
| 38 | char **argv; |
| 39 | { |
| 40 | extern char **environ; |
| 41 | extern int errno, optind; |
| 42 | register struct passwd *pwd; |
| 43 | register char *p, **g; |
| 44 | struct group *gr; |
| 45 | uid_t ruid, getuid(); |
| 46 | int ch, fulllogin, fastlogin, prio; |
| 47 | enum { UNSET, YES, NO } iscsh = UNSET; |
| 48 | char *user, *shell, *username, *cleanenv[2]; |
| 49 | char namebuf[50], shellbuf[MAXPATHLEN]; |
| 50 | char *crypt(), *getpass(), *getenv(), *getlogin(), *rindex(), *strcpy(); |
| 51 | |
| 52 | fulllogin = fastlogin = 0; |
| 53 | while ((ch = getopt(argc, argv, "-fl")) != EOF) |
| 54 | switch((char)ch) { |
| 55 | case 'f': |
| 56 | fastlogin = 1; |
| 57 | break; |
| 58 | case '-': |
| 59 | case 'l': |
| 60 | fulllogin = 1; |
| 61 | break; |
| 62 | case '?': |
| 63 | default: |
| 64 | fprintf(stderr, "usage: su [-fl] [login]\n"); |
| 65 | exit(1); |
| 66 | } |
| 67 | argv += optind; |
| 68 | |
| 69 | if ((pwd = getpwuid(ruid = getuid())) == NULL) { |
| 70 | fprintf(stderr, "su: who are you?\n"); |
| 71 | exit(1); |
| 72 | } |
| 73 | username = strcpy(namebuf, pwd->pw_name); |
| 74 | if (!fulllogin) |
| 75 | if (pwd->pw_shell && *pwd->pw_shell) |
| 76 | shell = strcpy(shellbuf, pwd->pw_shell); |
| 77 | else { |
| 78 | shell = "/bin/sh"; |
| 79 | iscsh = NO; |
| 80 | } |
| 81 | |
| 82 | user = *argv ? *argv++ : "root"; |
| 83 | if ((pwd = getpwnam(user)) == NULL) { |
| 84 | fprintf(stderr, "su: unknown login %s\n", user); |
| 85 | exit(1); |
| 86 | } |
| 87 | |
| 88 | /* only allow those in group zero to su to root. */ |
| 89 | if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0))) |
| 90 | for (g = gr->gr_mem;; ++g) { |
| 91 | if (!*g) { |
| 92 | fprintf(stderr, "su: you are not in the correct group to su %s.\n", user); |
| 93 | exit(1); |
| 94 | } |
| 95 | if (!strcmp(username, *g)) |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | errno = 0; |
| 100 | prio = getpriority(PRIO_PROCESS, 0); |
| 101 | if (errno) |
| 102 | prio = 0; |
| 103 | (void)setpriority(PRIO_PROCESS, 0, -2); |
| 104 | |
| 105 | if ((p = getlogin()) && *p) |
| 106 | username = p; |
| 107 | |
| 108 | if (*pwd->pw_passwd && ruid) { |
| 109 | p = getpass("Password:"); |
| 110 | if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) { |
| 111 | fprintf(stderr, "Sorry\n"); |
| 112 | if (pwd->pw_uid == 0) |
| 113 | syslog(LOG_CRIT|LOG_AUTH, "su: BAD SU %s on %s", |
| 114 | username, ttyname(2)); |
| 115 | exit(1); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /* if target user has no password, or fulllogin, use their shell */ |
| 120 | if (!*pwd->pw_passwd || fulllogin) |
| 121 | if (pwd->pw_shell && *pwd->pw_shell) { |
| 122 | shell = pwd->pw_shell; |
| 123 | iscsh = UNSET; |
| 124 | } else { |
| 125 | shell = "/bin/sh"; |
| 126 | iscsh = NO; |
| 127 | } |
| 128 | |
| 129 | if (iscsh == UNSET) { |
| 130 | if (p = rindex(shell, '/')) |
| 131 | ++p; |
| 132 | else |
| 133 | p = shell; |
| 134 | iscsh = strcmp(p, "csh") ? NO : YES; |
| 135 | } |
| 136 | |
| 137 | if (setgid(pwd->pw_gid) < 0) { |
| 138 | perror("su: setgid"); |
| 139 | exit(1); |
| 140 | } |
| 141 | if (initgroups(user, pwd->pw_gid)) { |
| 142 | fprintf(stderr, "su: initgroups failed\n"); |
| 143 | exit(1); |
| 144 | } |
| 145 | if (setuid(pwd->pw_uid) < 0) { |
| 146 | perror("su: setuid"); |
| 147 | exit(1); |
| 148 | } |
| 149 | if (fulllogin) { |
| 150 | p = getenv("TERM"); |
| 151 | cleanenv[0] = "PATH=:/usr/ucb:/bin:/usr/bin"; |
| 152 | cleanenv[1] = NULL; |
| 153 | environ = cleanenv; |
| 154 | (void)setenv("HOME", pwd->pw_dir, 1); |
| 155 | (void)setenv("SHELL", shell, 1); |
| 156 | (void)setenv("TERM", p, 1); |
| 157 | (void)setenv("USER", pwd->pw_name, 1); |
| 158 | if (chdir(pwd->pw_dir) < 0) { |
| 159 | fprintf(stderr, "su: no directory\n"); |
| 160 | exit(1); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if (fastlogin && iscsh == YES) |
| 165 | *--argv = "-f"; |
| 166 | |
| 167 | /* csh strips the first character... */ |
| 168 | *--argv = fulllogin ? "-su" : iscsh == YES ? "_su" : "su"; |
| 169 | |
| 170 | if (pwd->pw_uid == 0) |
| 171 | syslog(LOG_NOTICE|LOG_AUTH, "su: %s on %s", |
| 172 | username, ttyname(2)); |
| 173 | |
| 174 | (void)setpriority(PRIO_PROCESS, 0, prio); |
| 175 | |
| 176 | execv(shell, argv); |
| 177 | fprintf(stderr, "su: no shell.\n"); |
| 178 | exit(1); |
| 179 | } |