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

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