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