[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.42

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