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

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