| 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, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #ifndef lint |
| 35 | char copyright[] = |
| 36 | "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ |
| 37 | All rights reserved.\n"; |
| 38 | #endif /* not lint */ |
| 39 | |
| 40 | #ifndef lint |
| 41 | static char sccsid[] = "@(#)su.c 5.26 (Berkeley) 7/6/91"; |
| 42 | #endif /* not lint */ |
| 43 | |
| 44 | #include <sys/param.h> |
| 45 | #include <sys/time.h> |
| 46 | #include <sys/resource.h> |
| 47 | #include <syslog.h> |
| 48 | #include <stdio.h> |
| 49 | #include <pwd.h> |
| 50 | #include <grp.h> |
| 51 | #include <string.h> |
| 52 | #include <unistd.h> |
| 53 | #include <paths.h> |
| 54 | |
| 55 | #ifdef KERBEROS |
| 56 | #include <kerberosIV/des.h> |
| 57 | #include <kerberosIV/krb.h> |
| 58 | #include <netdb.h> |
| 59 | |
| 60 | #define ARGSTR "-Kflm" |
| 61 | |
| 62 | int use_kerberos = 1; |
| 63 | #else |
| 64 | #define ARGSTR "-flm" |
| 65 | #endif |
| 66 | |
| 67 | main(argc, argv) |
| 68 | int argc; |
| 69 | char **argv; |
| 70 | { |
| 71 | extern char **environ; |
| 72 | extern int errno, optind; |
| 73 | register struct passwd *pwd; |
| 74 | register char *p, **g; |
| 75 | struct group *gr; |
| 76 | uid_t ruid, getuid(); |
| 77 | int asme, ch, asthem, fastlogin, prio; |
| 78 | enum { UNSET, YES, NO } iscsh = UNSET; |
| 79 | char *user, *shell, *username, *cleanenv[2], *nargv[4], **np; |
| 80 | char shellbuf[MAXPATHLEN]; |
| 81 | char *crypt(), *getpass(), *getenv(), *getlogin(), *ontty(); |
| 82 | |
| 83 | np = &nargv[3]; |
| 84 | *np-- = NULL; |
| 85 | asme = asthem = fastlogin = 0; |
| 86 | while ((ch = getopt(argc, argv, ARGSTR)) != EOF) |
| 87 | switch((char)ch) { |
| 88 | #ifdef KERBEROS |
| 89 | case 'K': |
| 90 | use_kerberos = 0; |
| 91 | break; |
| 92 | #endif |
| 93 | case 'f': |
| 94 | fastlogin = 1; |
| 95 | break; |
| 96 | case '-': |
| 97 | case 'l': |
| 98 | asme = 0; |
| 99 | asthem = 1; |
| 100 | break; |
| 101 | case 'm': |
| 102 | asme = 1; |
| 103 | asthem = 0; |
| 104 | break; |
| 105 | case '?': |
| 106 | default: |
| 107 | (void)fprintf(stderr, "usage: su [%s] [login]\n", |
| 108 | ARGSTR); |
| 109 | exit(1); |
| 110 | } |
| 111 | argv += optind; |
| 112 | |
| 113 | errno = 0; |
| 114 | prio = getpriority(PRIO_PROCESS, 0); |
| 115 | if (errno) |
| 116 | prio = 0; |
| 117 | (void)setpriority(PRIO_PROCESS, 0, -2); |
| 118 | openlog("su", LOG_CONS, 0); |
| 119 | |
| 120 | /* get current login name and shell */ |
| 121 | ruid = getuid(); |
| 122 | username = getlogin(); |
| 123 | if (username == NULL || (pwd = getpwnam(username)) == NULL || |
| 124 | pwd->pw_uid != ruid) |
| 125 | pwd = getpwuid(ruid); |
| 126 | if (pwd == NULL) { |
| 127 | fprintf(stderr, "su: who are you?\n"); |
| 128 | exit(1); |
| 129 | } |
| 130 | username = strdup(pwd->pw_name); |
| 131 | if (asme) |
| 132 | if (pwd->pw_shell && *pwd->pw_shell) |
| 133 | shell = strcpy(shellbuf, pwd->pw_shell); |
| 134 | else { |
| 135 | shell = _PATH_BSHELL; |
| 136 | iscsh = NO; |
| 137 | } |
| 138 | |
| 139 | /* get target login information, default to root */ |
| 140 | user = *argv ? *argv : "root"; |
| 141 | if ((pwd = getpwnam(user)) == NULL) { |
| 142 | fprintf(stderr, "su: unknown login %s\n", user); |
| 143 | exit(1); |
| 144 | } |
| 145 | |
| 146 | if (ruid) { |
| 147 | #ifdef KERBEROS |
| 148 | if (!use_kerberos || kerberos(username, user, pwd->pw_uid)) |
| 149 | #endif |
| 150 | { |
| 151 | /* only allow those in group zero to su to root. */ |
| 152 | if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0))) |
| 153 | for (g = gr->gr_mem;; ++g) { |
| 154 | if (!*g) { |
| 155 | (void)fprintf(stderr, |
| 156 | "su: you are not in the correct group to su %s.\n", |
| 157 | user); |
| 158 | exit(1); |
| 159 | } |
| 160 | if (!strcmp(username, *g)) |
| 161 | break; |
| 162 | } |
| 163 | /* if target requires a password, verify it */ |
| 164 | if (*pwd->pw_passwd) { |
| 165 | p = getpass("Password:"); |
| 166 | if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) { |
| 167 | fprintf(stderr, "Sorry\n"); |
| 168 | syslog(LOG_AUTH|LOG_WARNING, |
| 169 | "BAD SU %s to %s%s", username, |
| 170 | user, ontty()); |
| 171 | exit(1); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (asme) { |
| 178 | /* if asme and non-standard target shell, must be root */ |
| 179 | if (!chshell(pwd->pw_shell) && ruid) { |
| 180 | (void)fprintf(stderr, |
| 181 | "su: permission denied (shell).\n"); |
| 182 | exit(1); |
| 183 | } |
| 184 | } else if (pwd->pw_shell && *pwd->pw_shell) { |
| 185 | shell = pwd->pw_shell; |
| 186 | iscsh = UNSET; |
| 187 | } else { |
| 188 | shell = _PATH_BSHELL; |
| 189 | iscsh = NO; |
| 190 | } |
| 191 | |
| 192 | /* if we're forking a csh, we want to slightly muck the args */ |
| 193 | if (iscsh == UNSET) { |
| 194 | if (p = rindex(shell, '/')) |
| 195 | ++p; |
| 196 | else |
| 197 | p = shell; |
| 198 | iscsh = strcmp(p, "csh") ? NO : YES; |
| 199 | } |
| 200 | |
| 201 | /* set permissions */ |
| 202 | if (setgid(pwd->pw_gid) < 0) { |
| 203 | perror("su: setgid"); |
| 204 | exit(1); |
| 205 | } |
| 206 | if (initgroups(user, pwd->pw_gid)) { |
| 207 | (void)fprintf(stderr, "su: initgroups failed.\n"); |
| 208 | exit(1); |
| 209 | } |
| 210 | if (setuid(pwd->pw_uid) < 0) { |
| 211 | perror("su: setuid"); |
| 212 | exit(1); |
| 213 | } |
| 214 | |
| 215 | if (!asme) { |
| 216 | if (asthem) { |
| 217 | p = getenv("TERM"); |
| 218 | cleanenv[0] = _PATH_DEFPATH; |
| 219 | cleanenv[1] = NULL; |
| 220 | environ = cleanenv; |
| 221 | (void)setenv("TERM", p, 1); |
| 222 | if (chdir(pwd->pw_dir) < 0) { |
| 223 | fprintf(stderr, "su: no directory\n"); |
| 224 | exit(1); |
| 225 | } |
| 226 | } |
| 227 | if (asthem || pwd->pw_uid) |
| 228 | (void)setenv("USER", pwd->pw_name, 1); |
| 229 | (void)setenv("HOME", pwd->pw_dir, 1); |
| 230 | (void)setenv("SHELL", shell, 1); |
| 231 | } |
| 232 | |
| 233 | if (iscsh == YES) { |
| 234 | if (fastlogin) |
| 235 | *np-- = "-f"; |
| 236 | if (asme) |
| 237 | *np-- = "-m"; |
| 238 | } |
| 239 | |
| 240 | /* csh strips the first character... */ |
| 241 | *np = asthem ? "-su" : iscsh == YES ? "_su" : "su"; |
| 242 | |
| 243 | if (ruid != 0) |
| 244 | syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s", |
| 245 | username, user, ontty()); |
| 246 | |
| 247 | (void)setpriority(PRIO_PROCESS, 0, prio); |
| 248 | |
| 249 | execv(shell, np); |
| 250 | (void)fprintf(stderr, "su: %s not found.\n", shell); |
| 251 | exit(1); |
| 252 | } |
| 253 | |
| 254 | chshell(sh) |
| 255 | char *sh; |
| 256 | { |
| 257 | register char *cp; |
| 258 | char *getusershell(); |
| 259 | |
| 260 | while ((cp = getusershell()) != NULL) |
| 261 | if (!strcmp(cp, sh)) |
| 262 | return (1); |
| 263 | return (0); |
| 264 | } |
| 265 | |
| 266 | char * |
| 267 | ontty() |
| 268 | { |
| 269 | char *p, *ttyname(); |
| 270 | static char buf[MAXPATHLEN + 4]; |
| 271 | |
| 272 | buf[0] = 0; |
| 273 | if (p = ttyname(STDERR_FILENO)) |
| 274 | sprintf(buf, " on %s", p); |
| 275 | return (buf); |
| 276 | } |
| 277 | |
| 278 | #ifdef KERBEROS |
| 279 | kerberos(username, user, uid) |
| 280 | char *username, *user; |
| 281 | int uid; |
| 282 | { |
| 283 | extern char *krb_err_txt[]; |
| 284 | KTEXT_ST ticket; |
| 285 | AUTH_DAT authdata; |
| 286 | struct hostent *hp; |
| 287 | register char *p; |
| 288 | int kerno; |
| 289 | u_long faddr; |
| 290 | char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN]; |
| 291 | char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN]; |
| 292 | char *ontty(), *krb_get_phost(); |
| 293 | |
| 294 | if (krb_get_lrealm(lrealm, 1) != KSUCCESS) |
| 295 | return (1); |
| 296 | if (koktologin(username, lrealm, user) && !uid) { |
| 297 | (void)fprintf(stderr, "kerberos su: not in %s's ACL.\n", user); |
| 298 | return (1); |
| 299 | } |
| 300 | (void)sprintf(krbtkfile, "%s_%s_%d", TKT_ROOT, user, getuid()); |
| 301 | |
| 302 | (void)setenv("KRBTKFILE", krbtkfile, 1); |
| 303 | (void)krb_set_tkt_string(krbtkfile); |
| 304 | /* |
| 305 | * Set real as well as effective ID to 0 for the moment, |
| 306 | * to make the kerberos library do the right thing. |
| 307 | */ |
| 308 | if (setuid(0) < 0) { |
| 309 | perror("su: setuid"); |
| 310 | return (1); |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Little trick here -- if we are su'ing to root, |
| 315 | * we need to get a ticket for "xxx.root", where xxx represents |
| 316 | * the name of the person su'ing. Otherwise (non-root case), |
| 317 | * we need to get a ticket for "yyy.", where yyy represents |
| 318 | * the name of the person being su'd to, and the instance is null |
| 319 | * |
| 320 | * We should have a way to set the ticket lifetime, |
| 321 | * with a system default for root. |
| 322 | */ |
| 323 | kerno = krb_get_pw_in_tkt((uid == 0 ? username : user), |
| 324 | (uid == 0 ? "root" : ""), lrealm, |
| 325 | "krbtgt", lrealm, DEFAULT_TKT_LIFE, 0); |
| 326 | |
| 327 | if (kerno != KSUCCESS) { |
| 328 | if (kerno == KDC_PR_UNKNOWN) { |
| 329 | fprintf(stderr, "principal unknown: %s.%s@%s\n", |
| 330 | (uid == 0 ? username : user), |
| 331 | (uid == 0 ? "root" : ""), lrealm); |
| 332 | return (1); |
| 333 | } |
| 334 | (void)fprintf(stderr, "su: unable to su: %s\n", |
| 335 | krb_err_txt[kerno]); |
| 336 | syslog(LOG_NOTICE|LOG_AUTH, |
| 337 | "BAD Kerberos SU: %s to %s%s: %s", |
| 338 | username, user, ontty(), krb_err_txt[kerno]); |
| 339 | return (1); |
| 340 | } |
| 341 | |
| 342 | if (chown(krbtkfile, uid, -1) < 0) { |
| 343 | perror("su: chown:"); |
| 344 | (void)unlink(krbtkfile); |
| 345 | return (1); |
| 346 | } |
| 347 | |
| 348 | (void)setpriority(PRIO_PROCESS, 0, -2); |
| 349 | |
| 350 | if (gethostname(hostname, sizeof(hostname)) == -1) { |
| 351 | perror("su: gethostname"); |
| 352 | dest_tkt(); |
| 353 | return (1); |
| 354 | } |
| 355 | |
| 356 | (void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost)); |
| 357 | savehost[sizeof(savehost) - 1] = '\0'; |
| 358 | |
| 359 | kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33); |
| 360 | |
| 361 | if (kerno == KDC_PR_UNKNOWN) { |
| 362 | (void)fprintf(stderr, "Warning: TGT not verified.\n"); |
| 363 | syslog(LOG_NOTICE|LOG_AUTH, |
| 364 | "%s to %s%s, TGT not verified (%s); %s.%s not registered?", |
| 365 | username, user, ontty(), krb_err_txt[kerno], |
| 366 | "rcmd", savehost); |
| 367 | } else if (kerno != KSUCCESS) { |
| 368 | (void)fprintf(stderr, "Unable to use TGT: %s\n", |
| 369 | krb_err_txt[kerno]); |
| 370 | syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s", |
| 371 | username, user, ontty(), krb_err_txt[kerno]); |
| 372 | dest_tkt(); |
| 373 | return (1); |
| 374 | } else { |
| 375 | if (!(hp = gethostbyname(hostname))) { |
| 376 | (void)fprintf(stderr, "su: can't get addr of %s\n", |
| 377 | hostname); |
| 378 | dest_tkt(); |
| 379 | return (1); |
| 380 | } |
| 381 | (void)bcopy((char *)hp->h_addr, (char *)&faddr, sizeof(faddr)); |
| 382 | |
| 383 | if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr, |
| 384 | &authdata, "")) != KSUCCESS) { |
| 385 | (void)fprintf(stderr, |
| 386 | "su: unable to verify rcmd ticket: %s\n", |
| 387 | krb_err_txt[kerno]); |
| 388 | syslog(LOG_NOTICE|LOG_AUTH, |
| 389 | "failed su: %s to %s%s: %s", username, |
| 390 | user, ontty(), krb_err_txt[kerno]); |
| 391 | dest_tkt(); |
| 392 | return (1); |
| 393 | } |
| 394 | } |
| 395 | return (0); |
| 396 | } |
| 397 | |
| 398 | koktologin(name, realm, toname) |
| 399 | char *name, *realm, *toname; |
| 400 | { |
| 401 | register AUTH_DAT *kdata; |
| 402 | AUTH_DAT kdata_st; |
| 403 | |
| 404 | kdata = &kdata_st; |
| 405 | bzero((caddr_t) kdata, sizeof(*kdata)); |
| 406 | (void)strcpy(kdata->pname, name); |
| 407 | (void)strcpy(kdata->pinst, |
| 408 | ((strcmp(toname, "root") == 0) ? "root" : "")); |
| 409 | (void)strcpy(kdata->prealm, realm); |
| 410 | return (kuserok(kdata, toname)); |
| 411 | } |
| 412 | #endif |