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

1.97    ! deraadt     1: /* $OpenBSD: doas.c,v 1.96 2022/03/04 05:37:21 tb 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: {
1.89      millert   139:        size_t i;
1.1       tedu      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.93      tobias    175:        if (parse_error)
1.21      zhuk      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.91      jcs       202: static int
1.92      millert   203: authuser_checkpass(char *myname, char *login_style)
1.61      tedu      204: {
                    205:        char *challenge = NULL, *response, rbuf[1024], cbuf[128];
                    206:        auth_session_t *as;
                    207:
                    208:        if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
1.91      jcs       209:            &challenge))) {
                    210:                warnx("Authentication failed");
                    211:                return AUTH_FAILED;
                    212:        }
1.61      tedu      213:        if (!challenge) {
                    214:                char host[HOST_NAME_MAX + 1];
1.97    ! deraadt   215:
1.61      tedu      216:                if (gethostname(host, sizeof(host)))
                    217:                        snprintf(host, sizeof(host), "?");
                    218:                snprintf(cbuf, sizeof(cbuf),
                    219:                    "\rdoas (%.32s@%.32s) password: ", myname, host);
                    220:                challenge = cbuf;
                    221:        }
                    222:        response = readpassphrase(challenge, rbuf, sizeof(rbuf),
                    223:            RPP_REQUIRE_TTY);
                    224:        if (response == NULL && errno == ENOTTY) {
                    225:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
                    226:                    "tty required for %s", myname);
                    227:                errx(1, "a tty is required");
                    228:        }
                    229:        if (!auth_userresponse(as, response, 0)) {
1.74      tedu      230:                explicit_bzero(rbuf, sizeof(rbuf));
1.61      tedu      231:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
                    232:                    "failed auth for %s", myname);
1.91      jcs       233:                warnx("Authentication failed");
                    234:                return AUTH_FAILED;
1.61      tedu      235:        }
                    236:        explicit_bzero(rbuf, sizeof(rbuf));
1.92      millert   237:        return AUTH_OK;
                    238: }
                    239:
                    240: static void
                    241: authuser(char *myname, char *login_style, int persist)
                    242: {
                    243:        int i, fd = -1;
                    244:
                    245:        if (persist)
                    246:                fd = open("/dev/tty", O_RDWR);
                    247:        if (fd != -1) {
                    248:                if (ioctl(fd, TIOCCHKVERAUTH) == 0)
                    249:                        goto good;
                    250:        }
                    251:        for (i = 0; i < AUTH_RETRIES; i++) {
                    252:                if (authuser_checkpass(myname, login_style) == AUTH_OK)
                    253:                        goto good;
                    254:        }
                    255:        exit(1);
1.63      tedu      256: good:
                    257:        if (fd != -1) {
1.64      tedu      258:                int secs = 5 * 60;
1.63      tedu      259:                ioctl(fd, TIOCSETVERAUTH, &secs);
                    260:                close(fd);
                    261:        }
1.61      tedu      262: }
                    263:
1.1       tedu      264: int
1.73      deraadt   265: unveilcommands(const char *ipath, const char *cmd)
                    266: {
                    267:        char *path = NULL, *p;
                    268:        int unveils = 0;
                    269:
                    270:        if (strchr(cmd, '/') != NULL) {
                    271:                if (unveil(cmd, "x") != -1)
                    272:                        unveils++;
                    273:                goto done;
                    274:        }
                    275:
                    276:        if (!ipath) {
                    277:                errno = ENOENT;
                    278:                goto done;
                    279:        }
                    280:        path = strdup(ipath);
                    281:        if (!path) {
                    282:                errno = ENOENT;
                    283:                goto done;
                    284:        }
                    285:        for (p = path; p && *p; ) {
                    286:                char buf[PATH_MAX];
                    287:                char *cp = strsep(&p, ":");
                    288:
                    289:                if (cp) {
                    290:                        int r = snprintf(buf, sizeof buf, "%s/%s", cp, cmd);
1.80      deraadt   291:                        if (r >= 0 && r < sizeof buf) {
1.73      deraadt   292:                                if (unveil(buf, "x") != -1)
                    293:                                        unveils++;
                    294:                        }
                    295:                }
                    296:        }
                    297: done:
                    298:        free(path);
                    299:        return (unveils);
                    300: }
                    301:
                    302: int
1.57      martijn   303: main(int argc, char **argv)
1.1       tedu      304: {
1.9       tedu      305:        const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
                    306:            "/usr/local/bin:/usr/local/sbin";
1.22      zhuk      307:        const char *confpath = NULL;
1.9       tedu      308:        char *shargv[] = { NULL, NULL };
                    309:        char *sh;
1.78      tedu      310:        const char *p;
1.9       tedu      311:        const char *cmd;
1.7       doug      312:        char cmdline[LINE_MAX];
1.75      tedu      313:        char mypwbuf[_PW_BUF_LEN], targpwbuf[_PW_BUF_LEN];
                    314:        struct passwd mypwstore, targpwstore;
                    315:        struct passwd *mypw, *targpw;
1.71      tedu      316:        const struct rule *rule;
1.9       tedu      317:        uid_t uid;
                    318:        uid_t target = 0;
1.1       tedu      319:        gid_t groups[NGROUPS_MAX + 1];
                    320:        int ngroups;
1.75      tedu      321:        int i, ch, rv;
1.8       nicm      322:        int sflag = 0;
1.26      espie     323:        int nflag = 0;
1.38      doug      324:        char cwdpath[PATH_MAX];
                    325:        const char *cwd;
1.47      sthen     326:        char *login_style = NULL;
1.57      martijn   327:        char **envp;
1.52      tedu      328:
                    329:        setprogname("doas");
1.42      tedu      330:
                    331:        closefrom(STDERR_FILENO + 1);
1.1       tedu      332:
1.26      espie     333:        uid = getuid();
1.34      tedu      334:
1.63      tedu      335:        while ((ch = getopt(argc, argv, "a:C:Lnsu:")) != -1) {
1.1       tedu      336:                switch (ch) {
1.47      sthen     337:                case 'a':
                    338:                        login_style = optarg;
                    339:                        break;
1.16      tedu      340:                case 'C':
1.22      zhuk      341:                        confpath = optarg;
                    342:                        break;
1.63      tedu      343:                case 'L':
                    344:                        i = open("/dev/tty", O_RDWR);
                    345:                        if (i != -1)
                    346:                                ioctl(i, TIOCCLRVERAUTH);
1.70      tedu      347:                        exit(i == -1);
1.1       tedu      348:                case 'u':
                    349:                        if (parseuid(optarg, &target) != 0)
                    350:                                errx(1, "unknown user");
                    351:                        break;
1.26      espie     352:                case 'n':
                    353:                        nflag = 1;
                    354:                        break;
1.8       nicm      355:                case 's':
                    356:                        sflag = 1;
                    357:                        break;
1.1       tedu      358:                default:
                    359:                        usage();
                    360:                        break;
                    361:                }
                    362:        }
                    363:        argv += optind;
                    364:        argc -= optind;
                    365:
1.22      zhuk      366:        if (confpath) {
                    367:                if (sflag)
                    368:                        usage();
                    369:        } else if ((!sflag && !argc) || (sflag && argc))
1.1       tedu      370:                usage();
1.16      tedu      371:
1.75      tedu      372:        rv = getpwuid_r(uid, &mypwstore, mypwbuf, sizeof(mypwbuf), &mypw);
1.76      tedu      373:        if (rv != 0)
1.75      tedu      374:                err(1, "getpwuid_r failed");
1.76      tedu      375:        if (mypw == NULL)
                    376:                errx(1, "no passwd entry for self");
1.1       tedu      377:        ngroups = getgroups(NGROUPS_MAX, groups);
                    378:        if (ngroups == -1)
                    379:                err(1, "can't get groups");
                    380:        groups[ngroups++] = getgid();
1.8       nicm      381:
                    382:        if (sflag) {
                    383:                sh = getenv("SHELL");
1.60      zhuk      384:                if (sh == NULL || *sh == '\0') {
1.76      tedu      385:                        shargv[0] = mypw->pw_shell;
1.60      zhuk      386:                } else
1.8       nicm      387:                        shargv[0] = sh;
                    388:                argv = shargv;
                    389:                argc = 1;
                    390:        }
1.22      zhuk      391:
1.24      tedu      392:        if (confpath) {
1.85      kn        393:                if (pledge("stdio rpath getpw id", NULL) == -1)
                    394:                        err(1, "pledge");
1.24      tedu      395:                checkconfig(confpath, argc, argv, uid, groups, ngroups,
                    396:                    target);
                    397:                exit(1);        /* fail safe */
                    398:        }
1.69      tedu      399:
                    400:        if (geteuid())
                    401:                errx(1, "not installed setuid");
1.24      tedu      402:
1.22      zhuk      403:        parseconfig("/etc/doas.conf", 1);
1.8       nicm      404:
1.23      zhuk      405:        /* cmdline is used only for logging, no need to abort on truncate */
1.58      tedu      406:        (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
1.8       nicm      407:        for (i = 1; i < argc; i++) {
                    408:                if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
1.23      zhuk      409:                        break;
1.8       nicm      410:                if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
1.23      zhuk      411:                        break;
1.8       nicm      412:        }
1.1       tedu      413:
1.23      zhuk      414:        cmd = argv[0];
1.15      zhuk      415:        if (!permit(uid, groups, ngroups, &rule, target, cmd,
1.58      tedu      416:            (const char **)argv + 1)) {
1.4       deraadt   417:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
1.83      kn        418:                    "command not permitted for %s: %s", mypw->pw_name, cmdline);
1.41      tedu      419:                errc(1, EPERM, NULL);
1.1       tedu      420:        }
                    421:
                    422:        if (!(rule->options & NOPASS)) {
1.26      espie     423:                if (nflag)
1.86      martijn   424:                        errx(1, "Authentication required");
1.46      tedu      425:
1.92      millert   426:                authuser(mypw->pw_name, login_style, rule->options & PERSIST);
1.1       tedu      427:        }
1.78      tedu      428:
                    429:        if ((p = getenv("PATH")) != NULL)
                    430:                formerpath = strdup(p);
                    431:        if (formerpath == NULL)
                    432:                formerpath = "";
1.43      deraadt   433:
1.90      beck      434:        if (unveil(_PATH_LOGIN_CONF, "r") == -1)
                    435:                err(1, "unveil %s", _PATH_LOGIN_CONF);
                    436:        if (unveil(_PATH_LOGIN_CONF ".db", "r") == -1)
                    437:                err(1, "unveil %s.db", _PATH_LOGIN_CONF);
1.94      robert    438:        if (unveil(_PATH_LOGIN_CONF_D, "r") == -1)
                    439:                err(1, "unveil %s", _PATH_LOGIN_CONF_D);
1.73      deraadt   440:        if (rule->cmd) {
                    441:                if (setenv("PATH", safepath, 1) == -1)
                    442:                        err(1, "failed to set PATH '%s'", safepath);
                    443:        }
                    444:        if (unveilcommands(getenv("PATH"), cmd) == 0)
                    445:                goto fail;
                    446:
1.43      deraadt   447:        if (pledge("stdio rpath getpw exec id", NULL) == -1)
                    448:                err(1, "pledge");
                    449:
1.75      tedu      450:        rv = getpwuid_r(target, &targpwstore, targpwbuf, sizeof(targpwbuf), &targpw);
1.76      tedu      451:        if (rv != 0)
                    452:                err(1, "getpwuid_r failed");
                    453:        if (targpw == NULL)
1.1       tedu      454:                errx(1, "no passwd entry for target");
1.43      deraadt   455:
1.96      tb        456:        if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
                    457:            LOGIN_SETPATH |
                    458:            LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
                    459:            LOGIN_SETUSER | LOGIN_SETENV | LOGIN_SETRTABLE) != 0)
1.1       tedu      460:                errx(1, "failed to set user context for target");
                    461:
1.43      deraadt   462:        if (pledge("stdio rpath exec", NULL) == -1)
                    463:                err(1, "pledge");
                    464:
1.38      doug      465:        if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
                    466:                cwd = "(failed)";
                    467:        else
                    468:                cwd = cwdpath;
1.43      deraadt   469:
                    470:        if (pledge("stdio exec", NULL) == -1)
                    471:                err(1, "pledge");
1.38      doug      472:
1.84      kn        473:        if (!(rule->options & NOLOG)) {
                    474:                syslog(LOG_AUTHPRIV | LOG_INFO,
                    475:                    "%s ran command %s as %s from %s",
                    476:                    mypw->pw_name, cmdline, targpw->pw_name, cwd);
                    477:        }
1.44      tedu      478:
1.77      tedu      479:        envp = prepenv(rule, mypw, targpw);
1.38      doug      480:
1.79      tedu      481:        /* setusercontext set path for the next process, so reset it for us */
1.77      tedu      482:        if (rule->cmd) {
                    483:                if (setenv("PATH", safepath, 1) == -1)
                    484:                        err(1, "failed to set PATH '%s'", safepath);
1.79      tedu      485:        } else {
                    486:                if (setenv("PATH", formerpath, 1) == -1)
                    487:                        err(1, "failed to set PATH '%s'", formerpath);
1.77      tedu      488:        }
1.1       tedu      489:        execvpe(cmd, argv, envp);
1.73      deraadt   490: fail:
1.10      tedu      491:        if (errno == ENOENT)
                    492:                errx(1, "%s: command not found", cmd);
1.1       tedu      493:        err(1, "%s", cmd);
                    494: }