[BACK]Return to su.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / su

Annotation of src/usr.bin/su/su.c, Revision 1.38

1.38    ! millert     1: /*     $OpenBSD: su.c,v 1.37 2001/05/31 17:34: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.
                     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.38    ! millert    44: static char rcsid[] = "$OpenBSD: su.c,v 1.37 2001/05/31 17:34:17 millert 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>
1.33      millert    54: #include <login_cap.h>
1.8       millert    55: #include <paths.h>
                     56: #include <pwd.h>
1.1       deraadt    57: #include <stdio.h>
                     58: #include <stdlib.h>
                     59: #include <string.h>
1.8       millert    60: #include <syslog.h>
1.1       deraadt    61: #include <unistd.h>
1.36      millert    62: #ifdef __STDC__
                     63: #include <stdarg.h>
1.1       deraadt    64: #else
1.36      millert    65: #include <varargs.h>
1.1       deraadt    66: #endif
1.36      millert    67: #include <bsd_auth.h>
                     68:
1.8       millert    69: char   *ontty __P((void));
                     70: int    chshell __P((char *));
1.36      millert    71: void   usage __P((void));
                     72: void   auth_err __P((auth_session_t *, int, const char *, ...));
                     73: void   auth_errx __P((auth_session_t *, int, const char *, ...));
1.1       deraadt    74:
                     75: int
                     76: main(argc, argv)
                     77:        int argc;
                     78:        char **argv;
                     79: {
                     80:        extern char **environ;
1.36      millert    81:        enum { UNSET, YES, NO } iscsh;
                     82:        struct passwd *pwd;
1.1       deraadt    83:        struct group *gr;
1.9       millert    84:        uid_t ruid;
1.33      millert    85:        login_cap_t *lc;
1.36      millert    86:        auth_session_t *as;
                     87:        int asme, asthem, authok, ch, fastlogin, prio;
                     88:        char *class, *style, *p, **g;
                     89:        char *user, *shell, *avshell, *username, **np, *fullname;
1.1       deraadt    90:        char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
                     91:
1.33      millert    92:        iscsh = UNSET;
1.36      millert    93:        class = shell = style = NULL;
1.1       deraadt    94:        asme = asthem = fastlogin = 0;
1.37      millert    95:        while ((ch = getopt(argc, argv, "-a:c:fKlm")) != -1)
1.36      millert    96:                switch(ch) {
                     97:                case 'a':
                     98:                        if (style)
                     99:                                usage();
                    100:                        style = optarg;
1.1       deraadt   101:                        break;
1.33      millert   102:                case 'c':
1.36      millert   103:                        if (class)
                    104:                                usage();
1.33      millert   105:                        class = optarg;
                    106:                        break;
1.1       deraadt   107:                case 'f':
                    108:                        fastlogin = 1;
                    109:                        break;
1.36      millert   110:                case 'K':
                    111:                        if (style)
                    112:                                usage();
                    113:                        style = "passwd";
                    114:                        break;
1.1       deraadt   115:                case '-':
                    116:                case 'l':
                    117:                        asme = 0;
                    118:                        asthem = 1;
                    119:                        break;
                    120:                case 'm':
                    121:                        asme = 1;
                    122:                        asthem = 0;
                    123:                        break;
                    124:                case '?':
                    125:                default:
1.36      millert   126:                        usage();
1.1       deraadt   127:                }
                    128:        argv += optind;
                    129:
                    130:        errno = 0;
                    131:        prio = getpriority(PRIO_PROCESS, 0);
                    132:        if (errno)
                    133:                prio = 0;
                    134:        (void)setpriority(PRIO_PROCESS, 0, -2);
                    135:        openlog("su", LOG_CONS, 0);
                    136:
1.36      millert   137:        if ((as = auth_open()) == NULL) {
                    138:                syslog(LOG_ERR, "auth_open: %m");
                    139:                err(1, "unable to begin authentication");
                    140:        }
                    141:
1.1       deraadt   142:        /* get current login name and shell */
                    143:        ruid = getuid();
                    144:        username = getlogin();
                    145:        if (username == NULL || (pwd = getpwnam(username)) == NULL ||
                    146:            pwd->pw_uid != ruid)
                    147:                pwd = getpwuid(ruid);
1.8       millert   148:        if (pwd == NULL)
1.36      millert   149:                auth_errx(as, 1, "who are you?");
1.9       millert   150:        if ((username = strdup(pwd->pw_name)) == NULL)
1.36      millert   151:                auth_err(as, 1, "can't allocate memory");
1.31      art       152:        if (asme) {
1.10      millert   153:                if (pwd->pw_shell && *pwd->pw_shell) {
1.36      millert   154:                        strlcpy(shellbuf, pwd->pw_shell, sizeof(shellbuf));
                    155:                        shell = shellbuf;
1.10      millert   156:                } else {
1.1       deraadt   157:                        shell = _PATH_BSHELL;
                    158:                        iscsh = NO;
                    159:                }
1.31      art       160:        }
1.1       deraadt   161:
                    162:        /* get target login information, default to root */
                    163:        user = *argv ? *argv : "root";
1.36      millert   164:        np = *argv ? argv : argv - 1;
1.1       deraadt   165:
1.8       millert   166:        if ((pwd = getpwnam(user)) == NULL)
1.36      millert   167:                auth_errx(as, 1, "unknown login %s", user);
1.16      millert   168:        if ((user = strdup(pwd->pw_name)) == NULL)
1.36      millert   169:                auth_err(as, 1, "can't allocate memory");
1.1       deraadt   170:
1.33      millert   171:        /* If the user specified a login class and we are root, use it */
                    172:        if (ruid && class)
1.36      millert   173:                auth_errx(as, 1, "only the superuser may specify a login class");
1.33      millert   174:        if (class)
                    175:                pwd->pw_class = class;
                    176:        if ((lc = login_getclass(pwd->pw_class)) == NULL)
1.36      millert   177:                auth_errx(as, 1, "no such login class: %s",
1.33      millert   178:                    class ? class : LOGIN_DEFCLASS);
                    179:
1.1       deraadt   180:        if (ruid) {
1.36      millert   181:                /*
                    182:                 * If we are trying to become root and the default style
                    183:                 * is being used, don't bother to look it up (we might be
                    184:                 * be su'ing up to fix /etc/login.conf)
                    185:                 */
                    186:                if ((pwd->pw_uid || !style || strcmp(style, LOGIN_DEFSTYLE)) &&
                    187:                    (style = login_getstyle(lc, style, "auth-su")) == NULL)
                    188:                        auth_errx(as, 1, "invalid authentication type");
                    189:                if (pwd->pw_uid || strcmp(user, "root") != 0)
                    190:                        fullname = user;
                    191:                else {
                    192:                        if ((fullname =
                    193:                            malloc(strlen(username) + 6)) == NULL)
                    194:                                auth_err(as, 1, NULL);
                    195:                        (void)sprintf(fullname, "%s.root", username);
                    196:                }
                    197:                /*
                    198:                 * Let the authentication program know whether they are
                    199:                 * in group wheel or not (if trying to become super user)
                    200:                 */
1.3       deraadt   201:                if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0))
1.36      millert   202:                    && gr->gr_mem && *(gr->gr_mem)) {
                    203:                        for (g = gr->gr_mem; *g; ++g) {
                    204:                                if (strcmp(username, *g) == 0) {
                    205:                                        auth_setoption(as, "wheel", "yes");
1.1       deraadt   206:                                        break;
1.36      millert   207:                                }
                    208:                        }
                    209:                        if (!*g)
                    210:                                auth_setoption(as, "wheel", "no");
1.1       deraadt   211:                }
1.36      millert   212:
                    213:                auth_verify(as, style, fullname, lc->lc_class, NULL);
                    214:                authok = auth_getstate(as);
                    215:                if ((authok & AUTH_ALLOW) == 0) {
                    216:                        if ((p = auth_getvalue(as, "errormsg")) != NULL)
                    217:                                fprintf(stderr, "%s\n", p);
                    218:                        fprintf(stderr, "Sorry\n");
                    219:                        syslog(LOG_AUTH|LOG_WARNING,
                    220:                                "BAD SU %s to %s%s", username, user, ontty());
                    221:                        auth_close(as);
                    222:                        exit(1);
1.1       deraadt   223:                }
                    224:        }
                    225:
                    226:        if (asme) {
                    227:                /* if asme and non-standard target shell, must be root */
1.8       millert   228:                if (!chshell(pwd->pw_shell) && ruid)
1.36      millert   229:                        auth_errx(as, 1, "permission denied (shell).");
1.1       deraadt   230:        } else if (pwd->pw_shell && *pwd->pw_shell) {
                    231:                shell = pwd->pw_shell;
                    232:                iscsh = UNSET;
                    233:        } else {
                    234:                shell = _PATH_BSHELL;
                    235:                iscsh = NO;
                    236:        }
                    237:
1.30      deraadt   238:        if ((p = strrchr(shell, '/')))
1.1       deraadt   239:                avshell = p+1;
                    240:        else
                    241:                avshell = shell;
                    242:
                    243:        /* if we're forking a csh, we want to slightly muck the args */
                    244:        if (iscsh == UNSET)
                    245:                iscsh = strcmp(avshell, "csh") ? NO : YES;
                    246:
                    247:        if (!asme) {
                    248:                if (asthem) {
                    249:                        p = getenv("TERM");
1.23      deraadt   250:                        if ((environ = calloc(1, sizeof (char *))) == NULL)
1.36      millert   251:                                auth_errx(as, 1, "calloc");
1.33      millert   252:                        if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH))
1.36      millert   253:                                auth_err(as, 1, "unable to set user context");
                    254:                        if (p && setenv("TERM", p, 1) == -1)
                    255:                                auth_err(as, 1, "unable to set environment");
1.5       deraadt   256:
                    257:                        seteuid(pwd->pw_uid);
                    258:                        setegid(pwd->pw_gid);
1.8       millert   259:                        if (chdir(pwd->pw_dir) < 0)
1.36      millert   260:                                auth_err(as, 1, "%s", pwd->pw_dir);
1.5       deraadt   261:                        seteuid(0);
                    262:                        setegid(0);     /* XXX use a saved gid instead? */
1.33      millert   263:                } else if (pwd->pw_uid == 0) {
                    264:                        if (setusercontext(lc,
                    265:                            pwd, pwd->pw_uid, LOGIN_SETPATH|LOGIN_SETUMASK))
1.36      millert   266:                                auth_err(as, 1, "unable to set user context");
1.1       deraadt   267:                }
1.15      millert   268:                if (asthem || pwd->pw_uid) {
1.34      deraadt   269:                        if (setenv("LOGNAME", pwd->pw_name, 1) == -1 ||
                    270:                            setenv("USER", pwd->pw_name, 1) == -1)
1.36      millert   271:                                auth_err(as, 1, "unable to set environment");
1.15      millert   272:                }
1.34      deraadt   273:                if (setenv("HOME", pwd->pw_dir, 1) == -1 ||
                    274:                    setenv("SHELL", shell, 1) == -1)
1.36      millert   275:                        auth_err(as, 1, "unable to set environment");
1.34      deraadt   276:        }
1.28      deraadt   277:
1.1       deraadt   278:        if (iscsh == YES) {
                    279:                if (fastlogin)
                    280:                        *np-- = "-f";
                    281:                if (asme)
                    282:                        *np-- = "-m";
                    283:        }
                    284:
                    285:        if (asthem) {
                    286:                avshellbuf[0] = '-';
1.36      millert   287:                strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
1.1       deraadt   288:                avshell = avshellbuf;
                    289:        } else if (iscsh == YES) {
                    290:                /* csh strips the first character... */
                    291:                avshellbuf[0] = '_';
1.36      millert   292:                strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
1.1       deraadt   293:                avshell = avshellbuf;
                    294:        }
                    295:
                    296:        *np = avshell;
                    297:
                    298:        if (ruid != 0)
                    299:                syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
                    300:                    username, user, ontty());
                    301:
                    302:        (void)setpriority(PRIO_PROCESS, 0, prio);
1.33      millert   303:        if (setusercontext(lc, pwd, pwd->pw_uid,
                    304:            (asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
                    305:            LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETUSER))
1.36      millert   306:                auth_err(as, 1, "unable to set user context");
                    307:        if (pwd->pw_uid && auth_approval(as, lc, pwd->pw_name, "su") <= 0)
                    308:                auth_err(as, 1, "approval failure");
                    309:        auth_close(as);
1.1       deraadt   310:
                    311:        execv(shell, np);
1.32      millert   312:        err(1, "%s", shell);
1.1       deraadt   313: }
                    314:
                    315: int
                    316: chshell(sh)
                    317:        char *sh;
                    318: {
1.36      millert   319:        char *cp;
1.1       deraadt   320:
                    321:        while ((cp = getusershell()) != NULL)
1.8       millert   322:                if (strcmp(cp, sh) == 0)
1.1       deraadt   323:                        return (1);
                    324:        return (0);
                    325: }
                    326:
                    327: char *
                    328: ontty()
                    329: {
                    330:        static char buf[MAXPATHLEN + 4];
1.36      millert   331:        char *p;
1.1       deraadt   332:
                    333:        buf[0] = 0;
1.30      deraadt   334:        if ((p = ttyname(STDERR_FILENO)))
1.8       millert   335:                snprintf(buf, sizeof(buf), " on %s", p);
1.1       deraadt   336:        return (buf);
                    337: }
                    338:
1.36      millert   339: void
                    340: usage()
1.1       deraadt   341: {
1.36      millert   342:        extern char *__progname;
1.1       deraadt   343:
1.37      millert   344:        fprintf(stderr, "usage: %s [-fKlm] [-a auth-type] %s ", __progname,
1.38    ! millert   345:            "[-c login-class] [login [shell arguments]]\n");
1.36      millert   346:        exit(1);
1.1       deraadt   347: }
                    348:
1.36      millert   349: void
                    350: #ifdef __STDC__
                    351: auth_err(auth_session_t *as, int eval, const char *fmt, ...)
                    352: #else
                    353: auth_err(va_alist)
                    354:        va_dcl
                    355: #endif
1.1       deraadt   356: {
1.36      millert   357:        va_list ap;
                    358: #ifdef __STDC__
                    359:        va_start(ap, fmt);
                    360: #else
                    361:        auth_session_t *as;
                    362:        int eval;
                    363:        const char *fmt;
                    364:
                    365:        va_start(ap);
                    366:        as = va_arg(ap, auth_session_t *);
                    367:        eval = va_arg(ap, int);
                    368:        fmt = va_arg(ap, const char *);
                    369: #endif
                    370:        verr(eval, fmt, ap);
                    371:        auth_close(as);
                    372:        va_end(ap);
1.28      deraadt   373: }
                    374:
1.36      millert   375: void
                    376: #ifdef __STDC__
                    377: auth_errx(auth_session_t *as, int eval, const char *fmt, ...)
                    378: #else
                    379: auth_errx(va_alist)
                    380:        va_dcl
                    381: #endif
1.28      deraadt   382: {
1.36      millert   383:        va_list ap;
                    384: #ifdef __STDC__
                    385:        va_start(ap, fmt);
                    386: #else
                    387:        auth_session_t *as;
                    388:        int eval;
                    389:        const char *fmt;
                    390:
                    391:        va_start(ap);
                    392:        as = va_arg(ap, auth_session_t *);
                    393:        eval = va_arg(ap, int);
                    394:        fmt = va_arg(ap, const char *);
                    395: #endif
                    396:        verrx(eval, fmt, ap);
                    397:        auth_close(as);
                    398:        va_end(ap);
1.1       deraadt   399: }