Annotation of src/usr.bin/su/su.c, Revision 1.22
1.22 ! deraadt 1: /* $OpenBSD: su.c,v 1.21 1997/02/18 20:07:56 tholo 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.22 ! deraadt 44: static char rcsid[] = "$OpenBSD: su.c,v 1.21 1997/02/18 20:07:56 tholo 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
67: #include <kerberosIV/des.h>
68: #include <kerberosIV/krb.h>
69: #include <netdb.h>
70:
71: #define ARGSTR "-Kflm"
72:
73: int use_kerberos = 1;
74: #else
75: #define ARGSTR "-flm"
76: #endif
77:
1.8 millert 78: char *ontty __P((void));
79: int chshell __P((char *));
1.1 deraadt 80:
81: int
82: main(argc, argv)
83: int argc;
84: char **argv;
85: {
86: extern char **environ;
87: register struct passwd *pwd;
88: register char *p, **g;
89: struct group *gr;
1.9 millert 90: uid_t ruid;
1.1 deraadt 91: int asme, ch, asthem, fastlogin, prio;
92: enum { UNSET, YES, NO } iscsh = UNSET;
1.22 ! deraadt 93: char *user, *shell, *avshell, *username, **cleanenv, **np;
1.1 deraadt 94: char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
95:
96: asme = asthem = fastlogin = 0;
1.18 millert 97: while ((ch = getopt(argc, argv, ARGSTR)) != -1)
1.1 deraadt 98: switch((char)ch) {
99: #ifdef KERBEROS
100: case 'K':
101: use_kerberos = 0;
102: break;
103: #endif
104: case 'f':
105: fastlogin = 1;
106: break;
107: case '-':
108: case 'l':
109: asme = 0;
110: asthem = 1;
111: break;
112: case 'm':
113: asme = 1;
114: asthem = 0;
115: break;
116: case '?':
117: default:
1.7 millert 118: (void)fprintf(stderr,
119: "usage: su [%s] [login [shell arguments]]\n",
1.1 deraadt 120: ARGSTR);
121: exit(1);
122: }
123: argv += optind;
124:
125: errno = 0;
126: prio = getpriority(PRIO_PROCESS, 0);
127: if (errno)
128: prio = 0;
129: (void)setpriority(PRIO_PROCESS, 0, -2);
130: openlog("su", LOG_CONS, 0);
131:
132: /* get current login name and shell */
133: ruid = getuid();
134: username = getlogin();
135: if (username == NULL || (pwd = getpwnam(username)) == NULL ||
136: pwd->pw_uid != ruid)
137: pwd = getpwuid(ruid);
1.8 millert 138: if (pwd == NULL)
139: errx(1, "who are you?");
1.9 millert 140: if ((username = strdup(pwd->pw_name)) == NULL)
141: err(1, "can't allocate memory");
1.1 deraadt 142: if (asme)
1.10 millert 143: if (pwd->pw_shell && *pwd->pw_shell) {
1.11 millert 144: shell = strncpy(shellbuf, pwd->pw_shell, sizeof(shellbuf) - 1);
1.10 millert 145: shellbuf[sizeof(shellbuf) - 1] = '\0';
146: } else {
1.1 deraadt 147: shell = _PATH_BSHELL;
148: iscsh = NO;
149: }
150:
151: /* get target login information, default to root */
152: user = *argv ? *argv : "root";
153: np = *argv ? argv : argv-1;
154:
1.8 millert 155: if ((pwd = getpwnam(user)) == NULL)
156: errx(1, "unknown login %s", user);
1.16 millert 157: if ((user = strdup(pwd->pw_name)) == NULL)
158: err(1, "can't allocate memory");
1.1 deraadt 159:
160: if (ruid) {
161: #ifdef KERBEROS
1.21 tholo 162: if (!use_kerberos || kerberos(username, user, pwd->pw_uid))
1.1 deraadt 163: #endif
164: {
165: /* only allow those in group zero to su to root. */
1.3 deraadt 166: if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0))
167: && gr->gr_mem && *(gr->gr_mem))
1.1 deraadt 168: for (g = gr->gr_mem;; ++g) {
1.8 millert 169: if (!*g)
170: errx(1, "you are not in the correct group to su %s.", user);
171: if (strcmp(username, *g) == 0)
1.1 deraadt 172: break;
173: }
174: /* if target requires a password, verify it */
175: if (*pwd->pw_passwd) {
176: p = getpass("Password:");
177: #ifdef SKEY
178: if (strcasecmp(p, "s/key") == 0) {
1.14 millert 179: if (skey_authenticate(user))
1.8 millert 180: goto badlogin;
1.1 deraadt 181: } else
182: #endif
183: if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) {
184: badlogin:
185: fprintf(stderr, "Sorry\n");
186: syslog(LOG_AUTH|LOG_WARNING,
187: "BAD SU %s to %s%s", username,
188: user, ontty());
189: exit(1);
190: }
191: }
192: }
1.8 millert 193: if (pwd->pw_expire && time(NULL) >= pwd->pw_expire) {
194: fprintf(stderr, "Sorry - account expired\n");
195: syslog(LOG_AUTH|LOG_WARNING, "BAD SU %s to %s%s", username,
196: user, ontty());
197: exit(1);
198: }
1.1 deraadt 199: }
200:
201: if (asme) {
202: /* if asme and non-standard target shell, must be root */
1.8 millert 203: if (!chshell(pwd->pw_shell) && ruid)
204: errx(1, "permission denied (shell).");
1.1 deraadt 205: } else if (pwd->pw_shell && *pwd->pw_shell) {
206: shell = pwd->pw_shell;
207: iscsh = UNSET;
208: } else {
209: shell = _PATH_BSHELL;
210: iscsh = NO;
211: }
212:
1.8 millert 213: if (p = strrchr(shell, '/'))
1.1 deraadt 214: avshell = p+1;
215: else
216: avshell = shell;
217:
218: /* if we're forking a csh, we want to slightly muck the args */
219: if (iscsh == UNSET)
220: iscsh = strcmp(avshell, "csh") ? NO : YES;
221:
222: /* set permissions */
1.17 tholo 223: if (setegid(pwd->pw_gid) < 0)
224: err(1, "setegid");
1.8 millert 225: if (setgid(pwd->pw_gid) < 0)
226: err(1, "setgid");
227: if (initgroups(user, pwd->pw_gid))
228: err(1, "initgroups failed");
1.17 tholo 229: if (seteuid(pwd->pw_uid) < 0)
230: err(1, "seteuid");
1.8 millert 231: if (setuid(pwd->pw_uid) < 0)
232: err(1, "setuid");
1.1 deraadt 233:
234: if (!asme) {
235: if (asthem) {
236: p = getenv("TERM");
1.22 ! deraadt 237: if ((cleanenv = calloc(1, sizeof (char *))) == NULL)
! 238: errx(1, "calloc");
1.1 deraadt 239: environ = cleanenv;
240: (void)setenv("PATH", _PATH_DEFPATH, 1);
1.6 deraadt 241: if (p)
242: (void)setenv("TERM", p, 1);
1.5 deraadt 243:
244: seteuid(pwd->pw_uid);
245: setegid(pwd->pw_gid);
1.8 millert 246: if (chdir(pwd->pw_dir) < 0)
247: errx(1, "no directory");
1.5 deraadt 248: seteuid(0);
249: setegid(0); /* XXX use a saved gid instead? */
1.1 deraadt 250: }
1.15 millert 251: if (asthem || pwd->pw_uid) {
252: (void)setenv("LOGNAME", pwd->pw_name, 1);
1.1 deraadt 253: (void)setenv("USER", pwd->pw_name, 1);
1.15 millert 254: }
1.1 deraadt 255: (void)setenv("HOME", pwd->pw_dir, 1);
256: (void)setenv("SHELL", shell, 1);
257: }
258:
259: if (iscsh == YES) {
260: if (fastlogin)
261: *np-- = "-f";
262: if (asme)
263: *np-- = "-m";
264: }
265:
266: if (asthem) {
267: avshellbuf[0] = '-';
1.11 millert 268: strncpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 2);
1.10 millert 269: avshellbuf[sizeof(avshellbuf) - 1] = '\0';
1.1 deraadt 270: avshell = avshellbuf;
271: } else if (iscsh == YES) {
272: /* csh strips the first character... */
273: avshellbuf[0] = '_';
1.11 millert 274: strncpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 2);
1.10 millert 275: avshellbuf[sizeof(avshellbuf) - 1] = '\0';
1.1 deraadt 276: avshell = avshellbuf;
277: }
278:
279: *np = avshell;
280:
281: if (ruid != 0)
282: syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
283: username, user, ontty());
284:
285: (void)setpriority(PRIO_PROCESS, 0, prio);
286:
287: execv(shell, np);
1.9 millert 288: err(1, shell);
1.1 deraadt 289: }
290:
291: int
292: chshell(sh)
293: char *sh;
294: {
295: register char *cp;
296:
297: while ((cp = getusershell()) != NULL)
1.8 millert 298: if (strcmp(cp, sh) == 0)
1.1 deraadt 299: return (1);
300: return (0);
301: }
302:
303: char *
304: ontty()
305: {
306: char *p, *ttyname();
307: static char buf[MAXPATHLEN + 4];
308:
309: buf[0] = 0;
310: if (p = ttyname(STDERR_FILENO))
1.8 millert 311: snprintf(buf, sizeof(buf), " on %s", p);
1.1 deraadt 312: return (buf);
313: }
314:
315: #ifdef KERBEROS
316: kerberos(username, user, uid)
317: char *username, *user;
318: int uid;
319: {
320: KTEXT_ST ticket;
321: AUTH_DAT authdata;
322: struct hostent *hp;
323: register char *p;
324: int kerno;
325: u_long faddr;
1.21 tholo 326: char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
1.1 deraadt 327: char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN];
328: char *ontty(), *krb_get_phost();
329:
330: if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
331: return (1);
332: if (koktologin(username, lrealm, user) && !uid) {
333: (void)fprintf(stderr, "kerberos su: not in %s's ACL.\n", user);
334: return (1);
335: }
1.8 millert 336: (void)snprintf(krbtkfile, sizeof(krbtkfile), "%s_%s_%d", TKT_ROOT,
337: user, getuid());
1.1 deraadt 338:
339: (void)setenv("KRBTKFILE", krbtkfile, 1);
340: (void)krb_set_tkt_string(krbtkfile);
341: /*
342: * Set real as well as effective ID to 0 for the moment,
343: * to make the kerberos library do the right thing.
344: */
345: if (setuid(0) < 0) {
1.8 millert 346: warn("setuid");
1.1 deraadt 347: return (1);
348: }
349:
350: /*
351: * Little trick here -- if we are su'ing to root,
352: * we need to get a ticket for "xxx.root", where xxx represents
353: * the name of the person su'ing. Otherwise (non-root case),
354: * we need to get a ticket for "yyy.", where yyy represents
355: * the name of the person being su'd to, and the instance is null
356: *
357: * We should have a way to set the ticket lifetime,
358: * with a system default for root.
359: */
360: kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
361: (uid == 0 ? "root" : ""), lrealm,
362: "krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
363:
364: if (kerno != KSUCCESS) {
365: if (kerno == KDC_PR_UNKNOWN) {
1.8 millert 366: warnx("kerberos principal unknown: %s.%s@%s",
1.1 deraadt 367: (uid == 0 ? username : user),
368: (uid == 0 ? "root" : ""), lrealm);
369: return (1);
370: }
1.8 millert 371: warnx("unable to su: %s", krb_err_txt[kerno]);
1.1 deraadt 372: syslog(LOG_NOTICE|LOG_AUTH,
373: "BAD Kerberos SU: %s to %s%s: %s",
374: username, user, ontty(), krb_err_txt[kerno]);
375: return (1);
376: }
377:
378: if (chown(krbtkfile, uid, -1) < 0) {
1.8 millert 379: warn("chown");
1.1 deraadt 380: (void)unlink(krbtkfile);
381: return (1);
382: }
383:
384: (void)setpriority(PRIO_PROCESS, 0, -2);
385:
386: if (gethostname(hostname, sizeof(hostname)) == -1) {
1.8 millert 387: warn("gethostname");
1.1 deraadt 388: dest_tkt();
389: return (1);
390: }
391:
1.11 millert 392: (void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost) - 1);
1.1 deraadt 393: savehost[sizeof(savehost) - 1] = '\0';
394:
395: kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
396:
397: if (kerno == KDC_PR_UNKNOWN) {
1.8 millert 398: warnx("Warning: TGT not verified.");
1.1 deraadt 399: syslog(LOG_NOTICE|LOG_AUTH,
400: "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
401: username, user, ontty(), krb_err_txt[kerno],
402: "rcmd", savehost);
403: } else if (kerno != KSUCCESS) {
1.8 millert 404: warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
1.1 deraadt 405: syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
406: username, user, ontty(), krb_err_txt[kerno]);
407: dest_tkt();
408: return (1);
409: } else {
410: if (!(hp = gethostbyname(hostname))) {
1.8 millert 411: warnx("can't get addr of %s", hostname);
1.1 deraadt 412: dest_tkt();
413: return (1);
414: }
1.10 millert 415: (void)memcpy((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
1.1 deraadt 416:
417: if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
418: &authdata, "")) != KSUCCESS) {
1.8 millert 419: warnx("unable to verify rcmd ticket: %s",
420: krb_err_txt[kerno]);
1.1 deraadt 421: syslog(LOG_NOTICE|LOG_AUTH,
422: "failed su: %s to %s%s: %s", username,
423: user, ontty(), krb_err_txt[kerno]);
424: dest_tkt();
425: return (1);
426: }
427: }
428: return (0);
429: }
430:
431: koktologin(name, realm, toname)
432: char *name, *realm, *toname;
433: {
434: register AUTH_DAT *kdata;
435: AUTH_DAT kdata_st;
436:
1.13 deraadt 437: memset((void *)&kdata_st, 0, sizeof(kdata_st));
1.1 deraadt 438: kdata = &kdata_st;
1.10 millert 439:
1.11 millert 440: (void)strncpy(kdata->pname, name, sizeof(kdata->pname) - 1);
1.10 millert 441: kdata->pname[sizeof(kdata->pname) - 1] = '\0';
442:
443: (void)strncpy(kdata->pinst,
1.11 millert 444: ((strcmp(toname, "root") == 0) ? "root" : ""), sizeof(kdata->pinst) - 1);
1.13 deraadt 445: kdata->pinst[sizeof(kdata->pinst) -1] = '\0';
1.10 millert 446:
1.11 millert 447: (void)strncpy(kdata->prealm, realm, sizeof(kdata->prealm) - 1);
1.10 millert 448: kdata->prealm[sizeof(kdata->prealm) -1] = '\0';
449:
1.1 deraadt 450: return (kuserok(kdata, toname));
451: }
452: #endif