Annotation of src/usr.bin/su/su.c, Revision 1.53
1.53 ! millert 1: /* $OpenBSD: su.c,v 1.52 2003/06/03 02:56:17 millert 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.
1.52 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
31:
32: #ifndef lint
1.41 millert 33: static const char copyright[] =
1.1 deraadt 34: "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
35: All rights reserved.\n";
36: #endif /* not lint */
37:
38: #ifndef lint
1.41 millert 39: #if 0
40: static const char sccsid[] = "from: @(#)su.c 5.26 (Berkeley) 7/6/91";
41: #else
1.53 ! millert 42: static const char rcsid[] = "$OpenBSD: su.c,v 1.52 2003/06/03 02:56:17 millert Exp $";
1.41 millert 43: #endif
1.1 deraadt 44: #endif /* not lint */
45:
46: #include <sys/param.h>
47: #include <sys/time.h>
48: #include <sys/resource.h>
1.8 millert 49:
50: #include <err.h>
51: #include <errno.h>
52: #include <grp.h>
1.33 millert 53: #include <login_cap.h>
1.8 millert 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.47 millert 61: #include <utmp.h>
1.36 millert 62: #include <stdarg.h>
63: #include <bsd_auth.h>
64:
1.47 millert 65: char *getloginname(void);
1.43 millert 66: char *ontty(void);
1.53 ! millert 67: int chshell(const char *);
1.47 millert 68: int verify_user(char *, struct passwd *, char *, login_cap_t *,
69: auth_session_t *);
1.43 millert 70: void usage(void);
71: void auth_err(auth_session_t *, int, const char *, ...);
72: void auth_errx(auth_session_t *, int, const char *, ...);
1.1 deraadt 73:
74: int
1.47 millert 75: main(int argc, char **argv)
1.1 deraadt 76: {
1.48 millert 77: int asme = 0, asthem = 0, ch, fastlogin = 0, emlogin = 0, flags, prio;
1.45 deraadt 78: char *user, *shell = NULL, *avshell, *username, **np;
1.47 millert 79: char *class = NULL, *style = NULL, *p;
1.45 deraadt 80: enum { UNSET, YES, NO } iscsh = UNSET;
81: char avshellbuf[MAXPATHLEN];
1.1 deraadt 82: extern char **environ;
1.45 deraadt 83: auth_session_t *as;
1.36 millert 84: struct passwd *pwd;
1.45 deraadt 85: login_cap_t *lc;
1.9 millert 86: uid_t ruid;
1.1 deraadt 87:
1.50 millert 88: while ((ch = getopt(argc, argv, "a:c:fKLlm-")) != -1)
1.45 deraadt 89: switch (ch) {
1.36 millert 90: case 'a':
91: if (style)
92: usage();
93: style = optarg;
1.1 deraadt 94: break;
1.33 millert 95: case 'c':
1.36 millert 96: if (class)
97: usage();
1.33 millert 98: class = optarg;
99: break;
1.1 deraadt 100: case 'f':
101: fastlogin = 1;
102: break;
1.36 millert 103: case 'K':
104: if (style)
105: usage();
106: style = "passwd";
107: break;
1.47 millert 108: case 'L':
109: emlogin = 1;
110: break;
111: case 'l':
1.1 deraadt 112: case '-':
113: asme = 0;
114: asthem = 1;
115: break;
116: case 'm':
117: asme = 1;
118: asthem = 0;
119: break;
120: default:
1.36 millert 121: usage();
1.1 deraadt 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:
1.36 millert 132: if ((as = auth_open()) == NULL) {
133: syslog(LOG_ERR, "auth_open: %m");
1.39 millert 134: err(1, "unable to initialize BSD authentication");
1.36 millert 135: }
1.39 millert 136: auth_setoption(as, "login", "yes");
1.36 millert 137:
1.1 deraadt 138: /* get current login name and shell */
139: ruid = getuid();
140: username = getlogin();
1.40 hin 141:
1.47 millert 142: if (ruid && class)
143: auth_errx(as, 1, "only the superuser may specify a login class");
144:
1.41 millert 145: if (username != NULL)
1.40 hin 146: auth_setoption(as, "invokinguser", username);
147:
1.1 deraadt 148: if (username == NULL || (pwd = getpwnam(username)) == NULL ||
149: pwd->pw_uid != ruid)
150: pwd = getpwuid(ruid);
1.8 millert 151: if (pwd == NULL)
1.36 millert 152: auth_errx(as, 1, "who are you?");
1.9 millert 153: if ((username = strdup(pwd->pw_name)) == NULL)
1.41 millert 154: auth_errx(as, 1, "can't allocate memory");
1.31 art 155: if (asme) {
1.10 millert 156: if (pwd->pw_shell && *pwd->pw_shell) {
1.42 millert 157: if ((shell = strdup(pwd->pw_shell)) == NULL)
158: auth_errx(as, 1, "can't allocate memory");
1.10 millert 159: } else {
1.1 deraadt 160: shell = _PATH_BSHELL;
161: iscsh = NO;
162: }
1.31 art 163: }
1.1 deraadt 164:
1.47 millert 165: for (;;) {
166: /* get target user, default to root unless in -L mode */
167: if (*argv) {
168: user = *argv;
169: } else if (emlogin) {
170: if ((user = getloginname()) == NULL) {
171: auth_close(as);
172: exit(1);
173: }
174: } else {
175: user = "root";
176: }
177: /* style may be specified as part of the username */
178: if ((p = strchr(user, ':')) != NULL) {
179: *p++ = '\0';
1.49 millert 180: style = p; /* XXX overrides -a flag */
181: }
1.47 millert 182:
1.36 millert 183: /*
1.47 millert 184: * Clean and setup our current authentication session.
185: * Note that options *are* not cleared.
1.36 millert 186: */
1.47 millert 187: auth_clean(as);
188: if (auth_setitem(as, AUTHV_INTERACTIVE, "True") != 0 ||
189: auth_setitem(as, AUTHV_NAME, user) != 0)
190: auth_errx(as, 1, "can't allocate memory");
191: if ((user = auth_getitem(as, AUTHV_NAME)) == NULL)
192: auth_errx(as, 1, "internal error");
193: if (auth_setpwd(as, NULL) || (pwd = auth_getpwd(as)) == NULL) {
194: if (emlogin)
195: pwd = NULL;
196: else
197: auth_errx(as, 1, "unknown login %s", user);
1.1 deraadt 198: }
1.36 millert 199:
1.47 millert 200: /* If the user specified a login class, use it */
201: if (!class && pwd && pwd->pw_class && pwd->pw_class[0] != '\0')
202: class = pwd->pw_class;
203: if ((lc = login_getclass(class)) == NULL)
204: auth_errx(as, 1, "no such login class: %s",
205: class ? class : LOGIN_DEFCLASS);
206:
207: if ((ruid == 0 && !emlogin) ||
208: verify_user(username, pwd, style, lc, as) == 0)
209: break;
210: syslog(LOG_AUTH|LOG_WARNING, "BAD SU %s to %s%s",
211: username, user, ontty());
212: if (!emlogin) {
1.36 millert 213: fprintf(stderr, "Sorry\n");
214: auth_close(as);
215: exit(1);
1.1 deraadt 216: }
1.47 millert 217: fprintf(stderr, "Login incorrect\n");
1.1 deraadt 218: }
219:
220: if (asme) {
221: /* if asme and non-standard target shell, must be root */
1.8 millert 222: if (!chshell(pwd->pw_shell) && ruid)
1.36 millert 223: auth_errx(as, 1, "permission denied (shell).");
1.1 deraadt 224: } else if (pwd->pw_shell && *pwd->pw_shell) {
1.47 millert 225: if ((shell = strdup(pwd->pw_shell)) == NULL)
226: auth_errx(as, 1, "can't allocate memory");
1.1 deraadt 227: iscsh = UNSET;
228: } else {
229: shell = _PATH_BSHELL;
230: iscsh = NO;
231: }
232:
1.30 deraadt 233: if ((p = strrchr(shell, '/')))
1.1 deraadt 234: avshell = p+1;
235: else
236: avshell = shell;
237:
238: /* if we're forking a csh, we want to slightly muck the args */
239: if (iscsh == UNSET)
240: iscsh = strcmp(avshell, "csh") ? NO : YES;
241:
242: if (!asme) {
243: if (asthem) {
244: p = getenv("TERM");
1.23 deraadt 245: if ((environ = calloc(1, sizeof (char *))) == NULL)
1.36 millert 246: auth_errx(as, 1, "calloc");
1.33 millert 247: if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH))
1.36 millert 248: auth_err(as, 1, "unable to set user context");
249: if (p && setenv("TERM", p, 1) == -1)
250: auth_err(as, 1, "unable to set environment");
1.5 deraadt 251:
252: seteuid(pwd->pw_uid);
253: setegid(pwd->pw_gid);
1.8 millert 254: if (chdir(pwd->pw_dir) < 0)
1.36 millert 255: auth_err(as, 1, "%s", pwd->pw_dir);
1.5 deraadt 256: seteuid(0);
257: setegid(0); /* XXX use a saved gid instead? */
1.33 millert 258: } else if (pwd->pw_uid == 0) {
259: if (setusercontext(lc,
260: pwd, pwd->pw_uid, LOGIN_SETPATH|LOGIN_SETUMASK))
1.36 millert 261: auth_err(as, 1, "unable to set user context");
1.1 deraadt 262: }
1.15 millert 263: if (asthem || pwd->pw_uid) {
1.34 deraadt 264: if (setenv("LOGNAME", pwd->pw_name, 1) == -1 ||
265: setenv("USER", pwd->pw_name, 1) == -1)
1.36 millert 266: auth_err(as, 1, "unable to set environment");
1.15 millert 267: }
1.34 deraadt 268: if (setenv("HOME", pwd->pw_dir, 1) == -1 ||
269: setenv("SHELL", shell, 1) == -1)
1.36 millert 270: auth_err(as, 1, "unable to set environment");
1.34 deraadt 271: }
1.28 deraadt 272:
1.47 millert 273: np = *argv ? argv : argv - 1;
1.1 deraadt 274: if (iscsh == YES) {
275: if (fastlogin)
276: *np-- = "-f";
277: if (asme)
278: *np-- = "-m";
279: }
280:
281: if (asthem) {
282: avshellbuf[0] = '-';
1.36 millert 283: strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
1.1 deraadt 284: avshell = avshellbuf;
285: } else if (iscsh == YES) {
286: /* csh strips the first character... */
287: avshellbuf[0] = '_';
1.36 millert 288: strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
1.1 deraadt 289: avshell = avshellbuf;
290: }
1.45 deraadt 291:
1.1 deraadt 292: *np = avshell;
293:
294: if (ruid != 0)
295: syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
296: username, user, ontty());
297:
298: (void)setpriority(PRIO_PROCESS, 0, prio);
1.51 millert 299: if (emlogin) {
1.48 millert 300: flags = LOGIN_SETALL & ~LOGIN_SETPATH;
1.51 millert 301: /*
302: * Only call setlogin() if this proccess is a session leader.
303: * In practice, this means the login name will be set only if
304: * we are exec'd by a shell. This is important because
305: * otherwise the parent shell's login name would change too.
306: */
307: if (getsid(0) != getpid())
308: flags &= ~LOGIN_SETLOGIN;
309: } else
1.48 millert 310: flags = (asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
311: LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETUSER;
312: if (setusercontext(lc, pwd, pwd->pw_uid, flags) != 0)
1.36 millert 313: auth_err(as, 1, "unable to set user context");
314: if (pwd->pw_uid && auth_approval(as, lc, pwd->pw_name, "su") <= 0)
315: auth_err(as, 1, "approval failure");
316: auth_close(as);
1.1 deraadt 317:
318: execv(shell, np);
1.32 millert 319: err(1, "%s", shell);
1.1 deraadt 320: }
321:
322: int
1.47 millert 323: verify_user(char *from, struct passwd *pwd, char *style,
324: login_cap_t *lc, auth_session_t *as)
325: {
326: struct group *gr;
327: char **g, *cp;
328: int authok;
329:
330: /*
331: * If we are trying to become root and the default style
332: * is being used, don't bother to look it up (we might be
333: * be su'ing up to fix /etc/login.conf)
334: */
335: if ((pwd == NULL || pwd->pw_uid != 0 || style == NULL ||
336: strcmp(style, LOGIN_DEFSTYLE) != 0) &&
337: (style = login_getstyle(lc, style, "auth-su")) == NULL)
338: auth_errx(as, 1, "invalid authentication type");
339:
340: /*
341: * Let the authentication program know whether they are
342: * in group wheel or not (if trying to become super user)
343: */
344: if (pwd != NULL && pwd->pw_uid == 0 && (gr = getgrgid(0)) != NULL &&
345: gr->gr_mem != NULL && *(gr->gr_mem) != NULL) {
346: for (g = gr->gr_mem; *g; ++g) {
347: if (strcmp(from, *g) == 0) {
348: auth_setoption(as, "wheel", "yes");
349: break;
350: }
351: }
352: if (!*g)
353: auth_setoption(as, "wheel", "no");
354: }
355:
356: auth_verify(as, style, NULL, lc->lc_class, (char *)NULL);
357: authok = auth_getstate(as);
358: if ((authok & AUTH_ALLOW) == 0) {
359: if ((cp = auth_getvalue(as, "errormsg")) != NULL)
360: fprintf(stderr, "%s\n", cp);
361: return(1);
362: }
363: return(0);
364: }
365:
366: int
1.53 ! millert 367: chshell(const char *sh)
1.1 deraadt 368: {
1.36 millert 369: char *cp;
1.53 ! millert 370: int found = 0;
1.1 deraadt 371:
1.53 ! millert 372: setusershell();
! 373: while ((cp = getusershell()) != NULL) {
! 374: if (strcmp(cp, sh) == 0) {
! 375: found = 1;
! 376: break;
! 377: }
! 378: }
! 379: endusershell();
! 380: return (found);
1.1 deraadt 381: }
382:
383: char *
1.47 millert 384: ontty(void)
1.1 deraadt 385: {
386: static char buf[MAXPATHLEN + 4];
1.36 millert 387: char *p;
1.1 deraadt 388:
389: buf[0] = 0;
1.30 deraadt 390: if ((p = ttyname(STDERR_FILENO)))
1.8 millert 391: snprintf(buf, sizeof(buf), " on %s", p);
1.1 deraadt 392: return (buf);
393: }
394:
1.47 millert 395: /*
396: * Allow for a '.' and 16 characters for any instance as well as
397: * space for a ':' and 16 charcters defining the authentication type.
398: */
399: #define NBUFSIZ (UT_NAMESIZE + 1 + 16 + 1 + 16)
400:
401: char *
402: getloginname(void)
403: {
404: static char nbuf[NBUFSIZ], *p;
405: int ch;
406:
407: for (;;) {
408: (void)printf("login: ");
409: for (p = nbuf; (ch = getchar()) != '\n'; ) {
410: if (ch == EOF)
411: return (NULL);
412: if (p < nbuf + (NBUFSIZ - 1))
413: *p++ = ch;
414: }
415: if (p > nbuf) {
416: if (nbuf[0] == '-') {
417: (void)fprintf(stderr,
418: "login names may not start with '-'.\n");
419: } else {
420: *p = '\0';
421: break;
422: }
423: }
424: }
425: return (nbuf);
426: }
427:
1.36 millert 428: void
1.47 millert 429: usage(void)
1.1 deraadt 430: {
1.36 millert 431: extern char *__progname;
1.1 deraadt 432:
1.47 millert 433: fprintf(stderr, "usage: %s [-fKLlm] [-a auth-type] [-c login-class] "
434: "[login [shell arguments]]\n", __progname);
1.36 millert 435: exit(1);
1.1 deraadt 436: }
437:
1.36 millert 438: void
439: auth_err(auth_session_t *as, int eval, const char *fmt, ...)
1.1 deraadt 440: {
1.36 millert 441: va_list ap;
1.46 millert 442:
1.36 millert 443: va_start(ap, fmt);
1.46 millert 444: vwarn(fmt, ap);
445: va_end(ap);
1.36 millert 446: auth_close(as);
1.46 millert 447: exit(eval);
1.28 deraadt 448: }
449:
1.36 millert 450: void
451: auth_errx(auth_session_t *as, int eval, const char *fmt, ...)
1.28 deraadt 452: {
1.36 millert 453: va_list ap;
1.46 millert 454:
1.36 millert 455: va_start(ap, fmt);
1.46 millert 456: vwarnx(fmt, ap);
457: va_end(ap);
1.36 millert 458: auth_close(as);
1.46 millert 459: exit(eval);
1.1 deraadt 460: }