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

Annotation of src/usr.bin/doas/doas.c, Revision 1.77

1.77    ! tedu        1: /* $OpenBSD: doas.c,v 1.76 2019/06/12 02:50:29 tedu Exp $ */
1.1       tedu        2: /*
                      3:  * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
1.6       nicm       17:
1.1       tedu       18: #include <sys/types.h>
1.6       nicm       19: #include <sys/stat.h>
1.63      tedu       20: #include <sys/ioctl.h>
1.1       tedu       21:
                     22: #include <limits.h>
                     23: #include <login_cap.h>
                     24: #include <bsd_auth.h>
1.46      tedu       25: #include <readpassphrase.h>
1.1       tedu       26: #include <string.h>
                     27: #include <stdio.h>
                     28: #include <stdlib.h>
                     29: #include <err.h>
                     30: #include <unistd.h>
                     31: #include <pwd.h>
                     32: #include <grp.h>
                     33: #include <syslog.h>
1.10      tedu       34: #include <errno.h>
1.63      tedu       35: #include <fcntl.h>
1.1       tedu       36:
                     37: #include "doas.h"
                     38:
                     39: static void __dead
                     40: usage(void)
                     41: {
1.63      tedu       42:        fprintf(stderr, "usage: doas [-Lns] [-a style] [-C config] [-u user]"
1.47      sthen      43:            " command [args]\n");
1.1       tedu       44:        exit(1);
                     45: }
                     46:
                     47: static int
                     48: parseuid(const char *s, uid_t *uid)
                     49: {
                     50:        struct passwd *pw;
                     51:        const char *errstr;
                     52:
                     53:        if ((pw = getpwnam(s)) != NULL) {
                     54:                *uid = pw->pw_uid;
                     55:                return 0;
                     56:        }
                     57:        *uid = strtonum(s, 0, UID_MAX, &errstr);
                     58:        if (errstr)
                     59:                return -1;
                     60:        return 0;
                     61: }
                     62:
                     63: static int
                     64: uidcheck(const char *s, uid_t desired)
                     65: {
                     66:        uid_t uid;
                     67:
                     68:        if (parseuid(s, &uid) != 0)
                     69:                return -1;
                     70:        if (uid != desired)
                     71:                return -1;
                     72:        return 0;
                     73: }
                     74:
1.33      tedu       75: static int
                     76: parsegid(const char *s, gid_t *gid)
1.1       tedu       77: {
                     78:        struct group *gr;
                     79:        const char *errstr;
                     80:
1.33      tedu       81:        if ((gr = getgrnam(s)) != NULL) {
                     82:                *gid = gr->gr_gid;
                     83:                return 0;
                     84:        }
                     85:        *gid = strtonum(s, 0, GID_MAX, &errstr);
1.1       tedu       86:        if (errstr)
                     87:                return -1;
1.33      tedu       88:        return 0;
1.1       tedu       89: }
                     90:
                     91: static int
                     92: match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
1.15      zhuk       93:     const char **cmdargs, struct rule *r)
1.1       tedu       94: {
                     95:        int i;
                     96:
                     97:        if (r->ident[0] == ':') {
1.33      tedu       98:                gid_t rgid;
                     99:                if (parsegid(r->ident + 1, &rgid) == -1)
1.1       tedu      100:                        return 0;
                    101:                for (i = 0; i < ngroups; i++) {
                    102:                        if (rgid == groups[i])
                    103:                                break;
                    104:                }
                    105:                if (i == ngroups)
                    106:                        return 0;
                    107:        } else {
                    108:                if (uidcheck(r->ident, uid) != 0)
                    109:                        return 0;
                    110:        }
                    111:        if (r->target && uidcheck(r->target, target) != 0)
                    112:                return 0;
1.15      zhuk      113:        if (r->cmd) {
                    114:                if (strcmp(r->cmd, cmd))
                    115:                        return 0;
                    116:                if (r->cmdargs) {
                    117:                        /* if arguments were given, they should match explicitly */
                    118:                        for (i = 0; r->cmdargs[i]; i++) {
                    119:                                if (!cmdargs[i])
                    120:                                        return 0;
                    121:                                if (strcmp(r->cmdargs[i], cmdargs[i]))
                    122:                                        return 0;
                    123:                        }
                    124:                        if (cmdargs[i])
                    125:                                return 0;
                    126:                }
                    127:        }
1.1       tedu      128:        return 1;
                    129: }
                    130:
                    131: static int
1.71      tedu      132: permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
1.15      zhuk      133:     uid_t target, const char *cmd, const char **cmdargs)
1.1       tedu      134: {
                    135:        int i;
                    136:
                    137:        *lastr = NULL;
                    138:        for (i = 0; i < nrules; i++) {
1.31      deraadt   139:                if (match(uid, groups, ngroups, target, cmd,
                    140:                    cmdargs, rules[i]))
1.1       tedu      141:                        *lastr = rules[i];
                    142:        }
                    143:        if (!*lastr)
                    144:                return 0;
                    145:        return (*lastr)->action == PERMIT;
                    146: }
                    147:
                    148: static void
1.22      zhuk      149: parseconfig(const char *filename, int checkperms)
1.1       tedu      150: {
                    151:        extern FILE *yyfp;
1.67      tedu      152:        extern int yyparse(void);
1.6       nicm      153:        struct stat sb;
1.1       tedu      154:
                    155:        yyfp = fopen(filename, "r");
1.36      espie     156:        if (!yyfp)
                    157:                err(1, checkperms ? "doas is not enabled, %s" :
                    158:                    "could not open config file %s", filename);
1.6       nicm      159:
1.22      zhuk      160:        if (checkperms) {
                    161:                if (fstat(fileno(yyfp), &sb) != 0)
                    162:                        err(1, "fstat(\"%s\")", filename);
                    163:                if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
                    164:                        errx(1, "%s is writable by group or other", filename);
                    165:                if (sb.st_uid != 0)
                    166:                        errx(1, "%s is not owned by root", filename);
                    167:        }
1.6       nicm      168:
1.68      tb        169:        yyparse();
1.1       tedu      170:        fclose(yyfp);
1.21      zhuk      171:        if (parse_errors)
                    172:                exit(1);
1.1       tedu      173: }
                    174:
                    175: static void __dead
1.22      zhuk      176: checkconfig(const char *confpath, int argc, char **argv,
1.24      tedu      177:     uid_t uid, gid_t *groups, int ngroups, uid_t target)
                    178: {
1.71      tedu      179:        const struct rule *rule;
1.22      zhuk      180:
                    181:        setresuid(uid, uid, uid);
                    182:        parseconfig(confpath, 0);
                    183:        if (!argc)
                    184:                exit(0);
                    185:
                    186:        if (permit(uid, groups, ngroups, &rule, target, argv[0],
                    187:            (const char **)argv + 1)) {
                    188:                printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
1.24      tedu      189:                exit(0);
1.22      zhuk      190:        } else {
                    191:                printf("deny\n");
1.24      tedu      192:                exit(1);
1.22      zhuk      193:        }
                    194: }
                    195:
1.61      tedu      196: static void
1.63      tedu      197: authuser(char *myname, char *login_style, int persist)
1.61      tedu      198: {
                    199:        char *challenge = NULL, *response, rbuf[1024], cbuf[128];
                    200:        auth_session_t *as;
1.63      tedu      201:        int fd = -1;
                    202:
                    203:        if (persist)
                    204:                fd = open("/dev/tty", O_RDWR);
                    205:        if (fd != -1) {
                    206:                if (ioctl(fd, TIOCCHKVERAUTH) == 0)
                    207:                        goto good;
                    208:        }
1.61      tedu      209:
                    210:        if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
                    211:            &challenge)))
                    212:                errx(1, "Authorization failed");
                    213:        if (!challenge) {
                    214:                char host[HOST_NAME_MAX + 1];
                    215:                if (gethostname(host, sizeof(host)))
                    216:                        snprintf(host, sizeof(host), "?");
                    217:                snprintf(cbuf, sizeof(cbuf),
                    218:                    "\rdoas (%.32s@%.32s) password: ", myname, host);
                    219:                challenge = cbuf;
                    220:        }
                    221:        response = readpassphrase(challenge, rbuf, sizeof(rbuf),
                    222:            RPP_REQUIRE_TTY);
                    223:        if (response == NULL && errno == ENOTTY) {
                    224:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
                    225:                    "tty required for %s", myname);
                    226:                errx(1, "a tty is required");
                    227:        }
                    228:        if (!auth_userresponse(as, response, 0)) {
1.74      tedu      229:                explicit_bzero(rbuf, sizeof(rbuf));
1.61      tedu      230:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
                    231:                    "failed auth for %s", myname);
1.72      tedu      232:                errx(1, "Authorization failed");
1.61      tedu      233:        }
                    234:        explicit_bzero(rbuf, sizeof(rbuf));
1.63      tedu      235: good:
                    236:        if (fd != -1) {
1.64      tedu      237:                int secs = 5 * 60;
1.63      tedu      238:                ioctl(fd, TIOCSETVERAUTH, &secs);
                    239:                close(fd);
                    240:        }
1.61      tedu      241: }
                    242:
1.1       tedu      243: int
1.73      deraadt   244: unveilcommands(const char *ipath, const char *cmd)
                    245: {
                    246:        char *path = NULL, *p;
                    247:        int unveils = 0;
                    248:
                    249:        if (strchr(cmd, '/') != NULL) {
                    250:                if (unveil(cmd, "x") != -1)
                    251:                        unveils++;
                    252:                goto done;
                    253:        }
                    254:
                    255:        if (!ipath) {
                    256:                errno = ENOENT;
                    257:                goto done;
                    258:        }
                    259:        path = strdup(ipath);
                    260:        if (!path) {
                    261:                errno = ENOENT;
                    262:                goto done;
                    263:        }
                    264:        for (p = path; p && *p; ) {
                    265:                char buf[PATH_MAX];
                    266:                char *cp = strsep(&p, ":");
                    267:
                    268:                if (cp) {
                    269:                        int r = snprintf(buf, sizeof buf, "%s/%s", cp, cmd);
                    270:                        if (r != -1 && r < sizeof buf) {
                    271:                                if (unveil(buf, "x") != -1)
                    272:                                        unveils++;
                    273:                        }
                    274:                }
                    275:        }
                    276: done:
                    277:        free(path);
                    278:        return (unveils);
                    279: }
                    280:
                    281: int
1.57      martijn   282: main(int argc, char **argv)
1.1       tedu      283: {
1.9       tedu      284:        const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
                    285:            "/usr/local/bin:/usr/local/sbin";
1.22      zhuk      286:        const char *confpath = NULL;
1.9       tedu      287:        char *shargv[] = { NULL, NULL };
                    288:        char *sh;
                    289:        const char *cmd;
1.7       doug      290:        char cmdline[LINE_MAX];
1.75      tedu      291:        char mypwbuf[_PW_BUF_LEN], targpwbuf[_PW_BUF_LEN];
                    292:        struct passwd mypwstore, targpwstore;
                    293:        struct passwd *mypw, *targpw;
1.71      tedu      294:        const struct rule *rule;
1.9       tedu      295:        uid_t uid;
                    296:        uid_t target = 0;
1.1       tedu      297:        gid_t groups[NGROUPS_MAX + 1];
                    298:        int ngroups;
1.75      tedu      299:        int i, ch, rv;
1.8       nicm      300:        int sflag = 0;
1.26      espie     301:        int nflag = 0;
1.38      doug      302:        char cwdpath[PATH_MAX];
                    303:        const char *cwd;
1.47      sthen     304:        char *login_style = NULL;
1.57      martijn   305:        char **envp;
1.52      tedu      306:
                    307:        setprogname("doas");
1.42      tedu      308:
                    309:        closefrom(STDERR_FILENO + 1);
1.1       tedu      310:
1.26      espie     311:        uid = getuid();
1.34      tedu      312:
1.63      tedu      313:        while ((ch = getopt(argc, argv, "a:C:Lnsu:")) != -1) {
1.1       tedu      314:                switch (ch) {
1.47      sthen     315:                case 'a':
                    316:                        login_style = optarg;
                    317:                        break;
1.16      tedu      318:                case 'C':
1.22      zhuk      319:                        confpath = optarg;
                    320:                        break;
1.63      tedu      321:                case 'L':
                    322:                        i = open("/dev/tty", O_RDWR);
                    323:                        if (i != -1)
                    324:                                ioctl(i, TIOCCLRVERAUTH);
1.70      tedu      325:                        exit(i == -1);
1.1       tedu      326:                case 'u':
                    327:                        if (parseuid(optarg, &target) != 0)
                    328:                                errx(1, "unknown user");
                    329:                        break;
1.26      espie     330:                case 'n':
                    331:                        nflag = 1;
                    332:                        break;
1.8       nicm      333:                case 's':
                    334:                        sflag = 1;
                    335:                        break;
1.1       tedu      336:                default:
                    337:                        usage();
                    338:                        break;
                    339:                }
                    340:        }
                    341:        argv += optind;
                    342:        argc -= optind;
                    343:
1.22      zhuk      344:        if (confpath) {
                    345:                if (sflag)
                    346:                        usage();
                    347:        } else if ((!sflag && !argc) || (sflag && argc))
1.1       tedu      348:                usage();
1.16      tedu      349:
1.75      tedu      350:        rv = getpwuid_r(uid, &mypwstore, mypwbuf, sizeof(mypwbuf), &mypw);
1.76      tedu      351:        if (rv != 0)
1.75      tedu      352:                err(1, "getpwuid_r failed");
1.76      tedu      353:        if (mypw == NULL)
                    354:                errx(1, "no passwd entry for self");
1.1       tedu      355:        ngroups = getgroups(NGROUPS_MAX, groups);
                    356:        if (ngroups == -1)
                    357:                err(1, "can't get groups");
                    358:        groups[ngroups++] = getgid();
1.8       nicm      359:
                    360:        if (sflag) {
                    361:                sh = getenv("SHELL");
1.60      zhuk      362:                if (sh == NULL || *sh == '\0') {
1.76      tedu      363:                        shargv[0] = mypw->pw_shell;
1.60      zhuk      364:                } else
1.8       nicm      365:                        shargv[0] = sh;
                    366:                argv = shargv;
                    367:                argc = 1;
                    368:        }
1.22      zhuk      369:
1.24      tedu      370:        if (confpath) {
                    371:                checkconfig(confpath, argc, argv, uid, groups, ngroups,
                    372:                    target);
                    373:                exit(1);        /* fail safe */
                    374:        }
1.69      tedu      375:
                    376:        if (geteuid())
                    377:                errx(1, "not installed setuid");
1.24      tedu      378:
1.22      zhuk      379:        parseconfig("/etc/doas.conf", 1);
1.8       nicm      380:
1.23      zhuk      381:        /* cmdline is used only for logging, no need to abort on truncate */
1.58      tedu      382:        (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
1.8       nicm      383:        for (i = 1; i < argc; i++) {
                    384:                if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
1.23      zhuk      385:                        break;
1.8       nicm      386:                if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
1.23      zhuk      387:                        break;
1.8       nicm      388:        }
1.1       tedu      389:
1.23      zhuk      390:        cmd = argv[0];
1.15      zhuk      391:        if (!permit(uid, groups, ngroups, &rule, target, cmd,
1.58      tedu      392:            (const char **)argv + 1)) {
1.4       deraadt   393:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
1.76      tedu      394:                    "failed command for %s: %s", mypw->pw_name, cmdline);
1.41      tedu      395:                errc(1, EPERM, NULL);
1.1       tedu      396:        }
                    397:
                    398:        if (!(rule->options & NOPASS)) {
1.26      espie     399:                if (nflag)
                    400:                        errx(1, "Authorization required");
1.46      tedu      401:
1.76      tedu      402:                authuser(mypw->pw_name, login_style, rule->options & PERSIST);
1.1       tedu      403:        }
1.43      deraadt   404:
1.73      deraadt   405:        if (unveil(_PATH_LOGIN_CONF, "r") == -1)
                    406:                err(1, "unveil");
                    407:        if (rule->cmd) {
                    408:                if (setenv("PATH", safepath, 1) == -1)
                    409:                        err(1, "failed to set PATH '%s'", safepath);
                    410:        }
                    411:        if (unveilcommands(getenv("PATH"), cmd) == 0)
                    412:                goto fail;
                    413:
1.43      deraadt   414:        if (pledge("stdio rpath getpw exec id", NULL) == -1)
                    415:                err(1, "pledge");
                    416:
1.75      tedu      417:        rv = getpwuid_r(target, &targpwstore, targpwbuf, sizeof(targpwbuf), &targpw);
1.76      tedu      418:        if (rv != 0)
                    419:                err(1, "getpwuid_r failed");
                    420:        if (targpw == NULL)
1.1       tedu      421:                errx(1, "no passwd entry for target");
1.43      deraadt   422:
1.75      tedu      423:        if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
1.77    ! tedu      424:            LOGIN_SETPATH |
1.1       tedu      425:            LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
                    426:            LOGIN_SETUSER) != 0)
                    427:                errx(1, "failed to set user context for target");
                    428:
1.43      deraadt   429:        if (pledge("stdio rpath exec", NULL) == -1)
                    430:                err(1, "pledge");
                    431:
1.38      doug      432:        if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
                    433:                cwd = "(failed)";
                    434:        else
                    435:                cwd = cwdpath;
1.43      deraadt   436:
                    437:        if (pledge("stdio exec", NULL) == -1)
                    438:                err(1, "pledge");
1.38      doug      439:
                    440:        syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
1.76      tedu      441:            mypw->pw_name, cmdline, targpw->pw_name, cwd);
1.44      tedu      442:
1.77    ! tedu      443:        envp = prepenv(rule, mypw, targpw);
1.38      doug      444:
1.77    ! tedu      445:        if (rule->cmd) {
        !           446:                /* do this again after setusercontext reset it */
        !           447:                if (setenv("PATH", safepath, 1) == -1)
        !           448:                        err(1, "failed to set PATH '%s'", safepath);
        !           449:        }
1.1       tedu      450:        execvpe(cmd, argv, envp);
1.73      deraadt   451: fail:
1.10      tedu      452:        if (errno == ENOENT)
                    453:                errx(1, "%s: command not found", cmd);
1.1       tedu      454:        err(1, "%s", cmd);
                    455: }