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

1.87    ! kn          1: /* $OpenBSD: doas.c,v 1.86 2021/01/16 09:18:41 martijn 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;
1.82      tedu       55:                if (*uid == UID_MAX)
                     56:                        return -1;
1.1       tedu       57:                return 0;
                     58:        }
1.82      tedu       59:        *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1.1       tedu       60:        if (errstr)
                     61:                return -1;
                     62:        return 0;
                     63: }
                     64:
                     65: static int
                     66: uidcheck(const char *s, uid_t desired)
                     67: {
                     68:        uid_t uid;
                     69:
                     70:        if (parseuid(s, &uid) != 0)
                     71:                return -1;
                     72:        if (uid != desired)
                     73:                return -1;
                     74:        return 0;
                     75: }
                     76:
1.33      tedu       77: static int
                     78: parsegid(const char *s, gid_t *gid)
1.1       tedu       79: {
                     80:        struct group *gr;
                     81:        const char *errstr;
                     82:
1.33      tedu       83:        if ((gr = getgrnam(s)) != NULL) {
                     84:                *gid = gr->gr_gid;
1.82      tedu       85:                if (*gid == GID_MAX)
                     86:                        return -1;
1.33      tedu       87:                return 0;
                     88:        }
1.82      tedu       89:        *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
1.1       tedu       90:        if (errstr)
                     91:                return -1;
1.33      tedu       92:        return 0;
1.1       tedu       93: }
                     94:
                     95: static int
                     96: match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
1.15      zhuk       97:     const char **cmdargs, struct rule *r)
1.1       tedu       98: {
                     99:        int i;
                    100:
                    101:        if (r->ident[0] == ':') {
1.33      tedu      102:                gid_t rgid;
                    103:                if (parsegid(r->ident + 1, &rgid) == -1)
1.1       tedu      104:                        return 0;
                    105:                for (i = 0; i < ngroups; i++) {
                    106:                        if (rgid == groups[i])
                    107:                                break;
                    108:                }
                    109:                if (i == ngroups)
                    110:                        return 0;
                    111:        } else {
                    112:                if (uidcheck(r->ident, uid) != 0)
                    113:                        return 0;
                    114:        }
                    115:        if (r->target && uidcheck(r->target, target) != 0)
                    116:                return 0;
1.15      zhuk      117:        if (r->cmd) {
                    118:                if (strcmp(r->cmd, cmd))
                    119:                        return 0;
                    120:                if (r->cmdargs) {
                    121:                        /* if arguments were given, they should match explicitly */
                    122:                        for (i = 0; r->cmdargs[i]; i++) {
                    123:                                if (!cmdargs[i])
                    124:                                        return 0;
                    125:                                if (strcmp(r->cmdargs[i], cmdargs[i]))
                    126:                                        return 0;
                    127:                        }
                    128:                        if (cmdargs[i])
                    129:                                return 0;
                    130:                }
                    131:        }
1.1       tedu      132:        return 1;
                    133: }
                    134:
                    135: static int
1.71      tedu      136: permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
1.15      zhuk      137:     uid_t target, const char *cmd, const char **cmdargs)
1.1       tedu      138: {
                    139:        int i;
                    140:
                    141:        *lastr = NULL;
                    142:        for (i = 0; i < nrules; i++) {
1.31      deraadt   143:                if (match(uid, groups, ngroups, target, cmd,
                    144:                    cmdargs, rules[i]))
1.1       tedu      145:                        *lastr = rules[i];
                    146:        }
                    147:        if (!*lastr)
                    148:                return 0;
                    149:        return (*lastr)->action == PERMIT;
                    150: }
                    151:
                    152: static void
1.22      zhuk      153: parseconfig(const char *filename, int checkperms)
1.1       tedu      154: {
                    155:        extern FILE *yyfp;
1.67      tedu      156:        extern int yyparse(void);
1.6       nicm      157:        struct stat sb;
1.1       tedu      158:
                    159:        yyfp = fopen(filename, "r");
1.36      espie     160:        if (!yyfp)
                    161:                err(1, checkperms ? "doas is not enabled, %s" :
                    162:                    "could not open config file %s", filename);
1.6       nicm      163:
1.22      zhuk      164:        if (checkperms) {
                    165:                if (fstat(fileno(yyfp), &sb) != 0)
                    166:                        err(1, "fstat(\"%s\")", filename);
                    167:                if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
                    168:                        errx(1, "%s is writable by group or other", filename);
                    169:                if (sb.st_uid != 0)
                    170:                        errx(1, "%s is not owned by root", filename);
                    171:        }
1.6       nicm      172:
1.68      tb        173:        yyparse();
1.1       tedu      174:        fclose(yyfp);
1.21      zhuk      175:        if (parse_errors)
                    176:                exit(1);
1.1       tedu      177: }
                    178:
                    179: static void __dead
1.22      zhuk      180: checkconfig(const char *confpath, int argc, char **argv,
1.24      tedu      181:     uid_t uid, gid_t *groups, int ngroups, uid_t target)
                    182: {
1.71      tedu      183:        const struct rule *rule;
1.22      zhuk      184:
                    185:        setresuid(uid, uid, uid);
1.85      kn        186:        if (pledge("stdio rpath getpw", NULL) == -1)
                    187:                err(1, "pledge");
1.22      zhuk      188:        parseconfig(confpath, 0);
                    189:        if (!argc)
                    190:                exit(0);
                    191:
                    192:        if (permit(uid, groups, ngroups, &rule, target, argv[0],
                    193:            (const char **)argv + 1)) {
                    194:                printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
1.24      tedu      195:                exit(0);
1.22      zhuk      196:        } else {
                    197:                printf("deny\n");
1.24      tedu      198:                exit(1);
1.22      zhuk      199:        }
                    200: }
                    201:
1.61      tedu      202: static void
1.63      tedu      203: authuser(char *myname, char *login_style, int persist)
1.61      tedu      204: {
                    205:        char *challenge = NULL, *response, rbuf[1024], cbuf[128];
                    206:        auth_session_t *as;
1.63      tedu      207:        int fd = -1;
                    208:
1.87    ! kn        209:        if (persist) {
1.63      tedu      210:                fd = open("/dev/tty", O_RDWR);
1.87    ! kn        211:                if (fd != -1) {
        !           212:                        if (ioctl(fd, TIOCCHKVERAUTH) == 0)
        !           213:                                goto good;
        !           214:                }
        !           215:        } else {
        !           216:                if (pledge("stdio rpath getpw exec id unveil", NULL) == -1)
        !           217:                        err(1, "pledge");
1.63      tedu      218:        }
1.61      tedu      219:
                    220:        if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
                    221:            &challenge)))
1.86      martijn   222:                errx(1, "Authentication failed");
1.61      tedu      223:        if (!challenge) {
                    224:                char host[HOST_NAME_MAX + 1];
                    225:                if (gethostname(host, sizeof(host)))
                    226:                        snprintf(host, sizeof(host), "?");
                    227:                snprintf(cbuf, sizeof(cbuf),
                    228:                    "\rdoas (%.32s@%.32s) password: ", myname, host);
                    229:                challenge = cbuf;
                    230:        }
                    231:        response = readpassphrase(challenge, rbuf, sizeof(rbuf),
                    232:            RPP_REQUIRE_TTY);
                    233:        if (response == NULL && errno == ENOTTY) {
                    234:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
                    235:                    "tty required for %s", myname);
                    236:                errx(1, "a tty is required");
                    237:        }
                    238:        if (!auth_userresponse(as, response, 0)) {
1.74      tedu      239:                explicit_bzero(rbuf, sizeof(rbuf));
1.61      tedu      240:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
                    241:                    "failed auth for %s", myname);
1.86      martijn   242:                errx(1, "Authentication failed");
1.61      tedu      243:        }
                    244:        explicit_bzero(rbuf, sizeof(rbuf));
1.63      tedu      245: good:
                    246:        if (fd != -1) {
1.64      tedu      247:                int secs = 5 * 60;
1.63      tedu      248:                ioctl(fd, TIOCSETVERAUTH, &secs);
                    249:                close(fd);
                    250:        }
1.61      tedu      251: }
                    252:
1.1       tedu      253: int
1.73      deraadt   254: unveilcommands(const char *ipath, const char *cmd)
                    255: {
                    256:        char *path = NULL, *p;
                    257:        int unveils = 0;
                    258:
                    259:        if (strchr(cmd, '/') != NULL) {
                    260:                if (unveil(cmd, "x") != -1)
                    261:                        unveils++;
                    262:                goto done;
                    263:        }
                    264:
                    265:        if (!ipath) {
                    266:                errno = ENOENT;
                    267:                goto done;
                    268:        }
                    269:        path = strdup(ipath);
                    270:        if (!path) {
                    271:                errno = ENOENT;
                    272:                goto done;
                    273:        }
                    274:        for (p = path; p && *p; ) {
                    275:                char buf[PATH_MAX];
                    276:                char *cp = strsep(&p, ":");
                    277:
                    278:                if (cp) {
                    279:                        int r = snprintf(buf, sizeof buf, "%s/%s", cp, cmd);
1.80      deraadt   280:                        if (r >= 0 && r < sizeof buf) {
1.73      deraadt   281:                                if (unveil(buf, "x") != -1)
                    282:                                        unveils++;
                    283:                        }
                    284:                }
                    285:        }
                    286: done:
                    287:        free(path);
                    288:        return (unveils);
                    289: }
                    290:
                    291: int
1.57      martijn   292: main(int argc, char **argv)
1.1       tedu      293: {
1.9       tedu      294:        const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
                    295:            "/usr/local/bin:/usr/local/sbin";
1.22      zhuk      296:        const char *confpath = NULL;
1.9       tedu      297:        char *shargv[] = { NULL, NULL };
                    298:        char *sh;
1.78      tedu      299:        const char *p;
1.9       tedu      300:        const char *cmd;
1.7       doug      301:        char cmdline[LINE_MAX];
1.75      tedu      302:        char mypwbuf[_PW_BUF_LEN], targpwbuf[_PW_BUF_LEN];
                    303:        struct passwd mypwstore, targpwstore;
                    304:        struct passwd *mypw, *targpw;
1.71      tedu      305:        const struct rule *rule;
1.9       tedu      306:        uid_t uid;
                    307:        uid_t target = 0;
1.1       tedu      308:        gid_t groups[NGROUPS_MAX + 1];
                    309:        int ngroups;
1.75      tedu      310:        int i, ch, rv;
1.8       nicm      311:        int sflag = 0;
1.26      espie     312:        int nflag = 0;
1.38      doug      313:        char cwdpath[PATH_MAX];
                    314:        const char *cwd;
1.47      sthen     315:        char *login_style = NULL;
1.57      martijn   316:        char **envp;
1.52      tedu      317:
                    318:        setprogname("doas");
1.42      tedu      319:
                    320:        closefrom(STDERR_FILENO + 1);
1.1       tedu      321:
1.26      espie     322:        uid = getuid();
1.34      tedu      323:
1.63      tedu      324:        while ((ch = getopt(argc, argv, "a:C:Lnsu:")) != -1) {
1.1       tedu      325:                switch (ch) {
1.47      sthen     326:                case 'a':
                    327:                        login_style = optarg;
                    328:                        break;
1.16      tedu      329:                case 'C':
1.22      zhuk      330:                        confpath = optarg;
                    331:                        break;
1.63      tedu      332:                case 'L':
                    333:                        i = open("/dev/tty", O_RDWR);
                    334:                        if (i != -1)
                    335:                                ioctl(i, TIOCCLRVERAUTH);
1.70      tedu      336:                        exit(i == -1);
1.1       tedu      337:                case 'u':
                    338:                        if (parseuid(optarg, &target) != 0)
                    339:                                errx(1, "unknown user");
                    340:                        break;
1.26      espie     341:                case 'n':
                    342:                        nflag = 1;
                    343:                        break;
1.8       nicm      344:                case 's':
                    345:                        sflag = 1;
                    346:                        break;
1.1       tedu      347:                default:
                    348:                        usage();
                    349:                        break;
                    350:                }
                    351:        }
                    352:        argv += optind;
                    353:        argc -= optind;
                    354:
1.22      zhuk      355:        if (confpath) {
                    356:                if (sflag)
                    357:                        usage();
                    358:        } else if ((!sflag && !argc) || (sflag && argc))
1.1       tedu      359:                usage();
1.16      tedu      360:
1.75      tedu      361:        rv = getpwuid_r(uid, &mypwstore, mypwbuf, sizeof(mypwbuf), &mypw);
1.76      tedu      362:        if (rv != 0)
1.75      tedu      363:                err(1, "getpwuid_r failed");
1.76      tedu      364:        if (mypw == NULL)
                    365:                errx(1, "no passwd entry for self");
1.1       tedu      366:        ngroups = getgroups(NGROUPS_MAX, groups);
                    367:        if (ngroups == -1)
                    368:                err(1, "can't get groups");
                    369:        groups[ngroups++] = getgid();
1.8       nicm      370:
                    371:        if (sflag) {
                    372:                sh = getenv("SHELL");
1.60      zhuk      373:                if (sh == NULL || *sh == '\0') {
1.76      tedu      374:                        shargv[0] = mypw->pw_shell;
1.60      zhuk      375:                } else
1.8       nicm      376:                        shargv[0] = sh;
                    377:                argv = shargv;
                    378:                argc = 1;
                    379:        }
1.22      zhuk      380:
1.24      tedu      381:        if (confpath) {
1.85      kn        382:                if (pledge("stdio rpath getpw id", NULL) == -1)
                    383:                        err(1, "pledge");
1.24      tedu      384:                checkconfig(confpath, argc, argv, uid, groups, ngroups,
                    385:                    target);
                    386:                exit(1);        /* fail safe */
                    387:        }
1.69      tedu      388:
                    389:        if (geteuid())
                    390:                errx(1, "not installed setuid");
1.24      tedu      391:
1.22      zhuk      392:        parseconfig("/etc/doas.conf", 1);
1.8       nicm      393:
1.23      zhuk      394:        /* cmdline is used only for logging, no need to abort on truncate */
1.58      tedu      395:        (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
1.8       nicm      396:        for (i = 1; i < argc; i++) {
                    397:                if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
1.23      zhuk      398:                        break;
1.8       nicm      399:                if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
1.23      zhuk      400:                        break;
1.8       nicm      401:        }
1.1       tedu      402:
1.23      zhuk      403:        cmd = argv[0];
1.15      zhuk      404:        if (!permit(uid, groups, ngroups, &rule, target, cmd,
1.58      tedu      405:            (const char **)argv + 1)) {
1.4       deraadt   406:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
1.83      kn        407:                    "command not permitted for %s: %s", mypw->pw_name, cmdline);
1.41      tedu      408:                errc(1, EPERM, NULL);
1.1       tedu      409:        }
                    410:
                    411:        if (!(rule->options & NOPASS)) {
1.26      espie     412:                if (nflag)
1.86      martijn   413:                        errx(1, "Authentication required");
1.46      tedu      414:
1.76      tedu      415:                authuser(mypw->pw_name, login_style, rule->options & PERSIST);
1.1       tedu      416:        }
1.78      tedu      417:
                    418:        if ((p = getenv("PATH")) != NULL)
                    419:                formerpath = strdup(p);
                    420:        if (formerpath == NULL)
                    421:                formerpath = "";
1.43      deraadt   422:
1.81      semarie   423:        if (unveil(_PATH_LOGIN_CONF, "r") == -1 ||
                    424:            unveil(_PATH_LOGIN_CONF ".db", "r") == -1)
1.73      deraadt   425:                err(1, "unveil");
                    426:        if (rule->cmd) {
                    427:                if (setenv("PATH", safepath, 1) == -1)
                    428:                        err(1, "failed to set PATH '%s'", safepath);
                    429:        }
                    430:        if (unveilcommands(getenv("PATH"), cmd) == 0)
                    431:                goto fail;
                    432:
1.43      deraadt   433:        if (pledge("stdio rpath getpw exec id", NULL) == -1)
                    434:                err(1, "pledge");
                    435:
1.75      tedu      436:        rv = getpwuid_r(target, &targpwstore, targpwbuf, sizeof(targpwbuf), &targpw);
1.76      tedu      437:        if (rv != 0)
                    438:                err(1, "getpwuid_r failed");
                    439:        if (targpw == NULL)
1.1       tedu      440:                errx(1, "no passwd entry for target");
1.43      deraadt   441:
1.75      tedu      442:        if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
1.77      tedu      443:            LOGIN_SETPATH |
1.1       tedu      444:            LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
                    445:            LOGIN_SETUSER) != 0)
                    446:                errx(1, "failed to set user context for target");
                    447:
1.43      deraadt   448:        if (pledge("stdio rpath exec", NULL) == -1)
                    449:                err(1, "pledge");
                    450:
1.38      doug      451:        if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
                    452:                cwd = "(failed)";
                    453:        else
                    454:                cwd = cwdpath;
1.43      deraadt   455:
                    456:        if (pledge("stdio exec", NULL) == -1)
                    457:                err(1, "pledge");
1.38      doug      458:
1.84      kn        459:        if (!(rule->options & NOLOG)) {
                    460:                syslog(LOG_AUTHPRIV | LOG_INFO,
                    461:                    "%s ran command %s as %s from %s",
                    462:                    mypw->pw_name, cmdline, targpw->pw_name, cwd);
                    463:        }
1.44      tedu      464:
1.77      tedu      465:        envp = prepenv(rule, mypw, targpw);
1.38      doug      466:
1.79      tedu      467:        /* setusercontext set path for the next process, so reset it for us */
1.77      tedu      468:        if (rule->cmd) {
                    469:                if (setenv("PATH", safepath, 1) == -1)
                    470:                        err(1, "failed to set PATH '%s'", safepath);
1.79      tedu      471:        } else {
                    472:                if (setenv("PATH", formerpath, 1) == -1)
                    473:                        err(1, "failed to set PATH '%s'", formerpath);
1.77      tedu      474:        }
1.1       tedu      475:        execvpe(cmd, argv, envp);
1.73      deraadt   476: fail:
1.10      tedu      477:        if (errno == ENOENT)
                    478:                errx(1, "%s: command not found", cmd);
1.1       tedu      479:        err(1, "%s", cmd);
                    480: }