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

1.50    ! tedu        1: /* $OpenBSD: doas.c,v 1.49 2016/01/24 13:19:21 gsoares 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.1       tedu       20:
                     21: #include <limits.h>
                     22: #include <login_cap.h>
                     23: #include <bsd_auth.h>
1.46      tedu       24: #include <readpassphrase.h>
1.1       tedu       25: #include <string.h>
                     26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <err.h>
                     29: #include <unistd.h>
                     30: #include <pwd.h>
                     31: #include <grp.h>
                     32: #include <syslog.h>
1.10      tedu       33: #include <errno.h>
1.1       tedu       34:
                     35: #include "doas.h"
                     36:
                     37: static void __dead
                     38: usage(void)
                     39: {
1.47      sthen      40:        fprintf(stderr, "usage: doas [-ns] [-a style] [-C config] [-u user]"
                     41:            " command [args]\n");
1.1       tedu       42:        exit(1);
                     43: }
                     44:
                     45: size_t
                     46: arraylen(const char **arr)
                     47: {
                     48:        size_t cnt = 0;
1.9       tedu       49:
1.1       tedu       50:        while (*arr) {
                     51:                cnt++;
                     52:                arr++;
                     53:        }
                     54:        return cnt;
                     55: }
                     56:
                     57: static int
                     58: parseuid(const char *s, uid_t *uid)
                     59: {
                     60:        struct passwd *pw;
                     61:        const char *errstr;
                     62:
                     63:        if ((pw = getpwnam(s)) != NULL) {
                     64:                *uid = pw->pw_uid;
                     65:                return 0;
                     66:        }
                     67:        *uid = strtonum(s, 0, UID_MAX, &errstr);
                     68:        if (errstr)
                     69:                return -1;
                     70:        return 0;
                     71: }
                     72:
                     73: static int
                     74: uidcheck(const char *s, uid_t desired)
                     75: {
                     76:        uid_t uid;
                     77:
                     78:        if (parseuid(s, &uid) != 0)
                     79:                return -1;
                     80:        if (uid != desired)
                     81:                return -1;
                     82:        return 0;
                     83: }
                     84:
1.33      tedu       85: static int
                     86: parsegid(const char *s, gid_t *gid)
1.1       tedu       87: {
                     88:        struct group *gr;
                     89:        const char *errstr;
                     90:
1.33      tedu       91:        if ((gr = getgrnam(s)) != NULL) {
                     92:                *gid = gr->gr_gid;
                     93:                return 0;
                     94:        }
                     95:        *gid = strtonum(s, 0, GID_MAX, &errstr);
1.1       tedu       96:        if (errstr)
                     97:                return -1;
1.33      tedu       98:        return 0;
1.1       tedu       99: }
                    100:
                    101: static int
                    102: match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
1.15      zhuk      103:     const char **cmdargs, struct rule *r)
1.1       tedu      104: {
                    105:        int i;
                    106:
                    107:        if (r->ident[0] == ':') {
1.33      tedu      108:                gid_t rgid;
                    109:                if (parsegid(r->ident + 1, &rgid) == -1)
1.1       tedu      110:                        return 0;
                    111:                for (i = 0; i < ngroups; i++) {
                    112:                        if (rgid == groups[i])
                    113:                                break;
                    114:                }
                    115:                if (i == ngroups)
                    116:                        return 0;
                    117:        } else {
                    118:                if (uidcheck(r->ident, uid) != 0)
                    119:                        return 0;
                    120:        }
                    121:        if (r->target && uidcheck(r->target, target) != 0)
                    122:                return 0;
1.15      zhuk      123:        if (r->cmd) {
                    124:                if (strcmp(r->cmd, cmd))
                    125:                        return 0;
                    126:                if (r->cmdargs) {
                    127:                        /* if arguments were given, they should match explicitly */
                    128:                        for (i = 0; r->cmdargs[i]; i++) {
                    129:                                if (!cmdargs[i])
                    130:                                        return 0;
                    131:                                if (strcmp(r->cmdargs[i], cmdargs[i]))
                    132:                                        return 0;
                    133:                        }
                    134:                        if (cmdargs[i])
                    135:                                return 0;
                    136:                }
                    137:        }
1.1       tedu      138:        return 1;
                    139: }
                    140:
                    141: static int
                    142: permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
1.15      zhuk      143:     uid_t target, const char *cmd, const char **cmdargs)
1.1       tedu      144: {
                    145:        int i;
                    146:
                    147:        *lastr = NULL;
                    148:        for (i = 0; i < nrules; i++) {
1.31      deraadt   149:                if (match(uid, groups, ngroups, target, cmd,
                    150:                    cmdargs, rules[i]))
1.1       tedu      151:                        *lastr = rules[i];
                    152:        }
                    153:        if (!*lastr)
                    154:                return 0;
                    155:        return (*lastr)->action == PERMIT;
                    156: }
                    157:
                    158: static void
1.22      zhuk      159: parseconfig(const char *filename, int checkperms)
1.1       tedu      160: {
                    161:        extern FILE *yyfp;
                    162:        extern int yyparse(void);
1.6       nicm      163:        struct stat sb;
1.1       tedu      164:
                    165:        yyfp = fopen(filename, "r");
1.36      espie     166:        if (!yyfp)
                    167:                err(1, checkperms ? "doas is not enabled, %s" :
                    168:                    "could not open config file %s", filename);
1.6       nicm      169:
1.22      zhuk      170:        if (checkperms) {
                    171:                if (fstat(fileno(yyfp), &sb) != 0)
                    172:                        err(1, "fstat(\"%s\")", filename);
                    173:                if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
                    174:                        errx(1, "%s is writable by group or other", filename);
                    175:                if (sb.st_uid != 0)
                    176:                        errx(1, "%s is not owned by root", filename);
                    177:        }
1.6       nicm      178:
1.1       tedu      179:        yyparse();
                    180:        fclose(yyfp);
1.21      zhuk      181:        if (parse_errors)
                    182:                exit(1);
1.1       tedu      183: }
                    184:
1.30      zhuk      185: /*
1.32      tedu      186:  * Copy the environment variables in safeset from oldenvp to envp.
1.30      zhuk      187:  */
1.1       tedu      188: static int
1.4       deraadt   189: copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
                    190:     char **envp, int ei)
1.1       tedu      191: {
                    192:        int i;
1.9       tedu      193:
1.1       tedu      194:        for (i = 0; i < nsafe; i++) {
                    195:                const char **oe = oldenvp;
                    196:                while (*oe) {
                    197:                        size_t len = strlen(safeset[i]);
                    198:                        if (strncmp(*oe, safeset[i], len) == 0 &&
                    199:                            (*oe)[len] == '=') {
                    200:                                if (!(envp[ei++] = strdup(*oe)))
                    201:                                        err(1, "strdup");
                    202:                                break;
                    203:                        }
                    204:                        oe++;
                    205:                }
                    206:        }
                    207:        return ei;
                    208: }
                    209:
                    210: static char **
                    211: copyenv(const char **oldenvp, struct rule *rule)
                    212: {
                    213:        const char *safeset[] = {
1.14      tedu      214:                "DISPLAY", "HOME", "LOGNAME", "MAIL",
1.1       tedu      215:                "PATH", "TERM", "USER", "USERNAME",
1.11      tedu      216:                NULL
                    217:        };
                    218:        const char *badset[] = {
                    219:                "ENV",
                    220:                NULL
1.1       tedu      221:        };
                    222:        char **envp;
                    223:        const char **extra;
                    224:        int ei;
1.11      tedu      225:        int nsafe, nbad;
1.9       tedu      226:        int nextras = 0;
1.20      zhuk      227:
1.30      zhuk      228:        /* if there was no envvar whitelist, pass all except badset ones */
1.11      tedu      229:        nbad = arraylen(badset);
1.1       tedu      230:        if ((rule->options & KEEPENV) && !rule->envlist) {
1.30      zhuk      231:                size_t iold, inew;
1.12      tedu      232:                size_t oldlen = arraylen(oldenvp);
                    233:                envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
1.5       nicm      234:                if (!envp)
                    235:                        err(1, "reallocarray");
1.30      zhuk      236:                for (inew = iold = 0; iold < oldlen; iold++) {
                    237:                        size_t ibad;
                    238:                        for (ibad = 0; ibad < nbad; ibad++) {
                    239:                                size_t len = strlen(badset[ibad]);
                    240:                                if (strncmp(oldenvp[iold], badset[ibad], len) == 0 &&
                    241:                                    oldenvp[iold][len] == '=') {
1.11      tedu      242:                                        break;
                    243:                                }
                    244:                        }
1.30      zhuk      245:                        if (ibad == nbad) {
                    246:                                if (!(envp[inew] = strdup(oldenvp[iold])))
1.11      tedu      247:                                        err(1, "strdup");
1.30      zhuk      248:                                inew++;
1.11      tedu      249:                        }
1.1       tedu      250:                }
1.30      zhuk      251:                envp[inew] = NULL;
1.1       tedu      252:                return envp;
                    253:        }
                    254:
                    255:        nsafe = arraylen(safeset);
                    256:        if ((extra = rule->envlist)) {
1.30      zhuk      257:                size_t isafe;
1.1       tedu      258:                nextras = arraylen(extra);
1.30      zhuk      259:                for (isafe = 0; isafe < nsafe; isafe++) {
                    260:                        size_t iextras;
                    261:                        for (iextras = 0; iextras < nextras; iextras++) {
                    262:                                if (strcmp(extra[iextras], safeset[isafe]) == 0) {
1.29      zhuk      263:                                        nextras--;
1.30      zhuk      264:                                        extra[iextras] = extra[nextras];
1.1       tedu      265:                                        extra[nextras] = NULL;
1.30      zhuk      266:                                        iextras--;
1.1       tedu      267:                                }
                    268:                        }
                    269:                }
                    270:        }
                    271:
                    272:        envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
                    273:        if (!envp)
                    274:                err(1, "can't allocate new environment");
                    275:
                    276:        ei = 0;
                    277:        ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
                    278:        ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
                    279:        envp[ei] = NULL;
                    280:
                    281:        return envp;
                    282: }
                    283:
                    284: static void __dead
1.22      zhuk      285: checkconfig(const char *confpath, int argc, char **argv,
1.24      tedu      286:     uid_t uid, gid_t *groups, int ngroups, uid_t target)
                    287: {
1.22      zhuk      288:        struct rule *rule;
                    289:
                    290:        setresuid(uid, uid, uid);
                    291:        parseconfig(confpath, 0);
                    292:        if (!argc)
                    293:                exit(0);
                    294:
                    295:        if (permit(uid, groups, ngroups, &rule, target, argv[0],
                    296:            (const char **)argv + 1)) {
                    297:                printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
1.24      tedu      298:                exit(0);
1.22      zhuk      299:        } else {
                    300:                printf("deny\n");
1.24      tedu      301:                exit(1);
1.22      zhuk      302:        }
                    303: }
                    304:
1.1       tedu      305: int
                    306: main(int argc, char **argv, char **envp)
                    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;
                    313:        const char *cmd;
1.7       doug      314:        char cmdline[LINE_MAX];
                    315:        char myname[_PW_NAME_LEN + 1];
1.9       tedu      316:        struct passwd *pw;
                    317:        struct rule *rule;
                    318:        uid_t uid;
                    319:        uid_t target = 0;
1.1       tedu      320:        gid_t groups[NGROUPS_MAX + 1];
                    321:        int ngroups;
                    322:        int i, ch;
1.8       nicm      323:        int sflag = 0;
1.26      espie     324:        int nflag = 0;
1.38      doug      325:        char cwdpath[PATH_MAX];
                    326:        const char *cwd;
1.47      sthen     327:        char *login_style = NULL;
1.42      tedu      328:
1.46      tedu      329:        if (pledge("stdio rpath getpw tty proc exec id", NULL) == -1)
1.43      deraadt   330:                err(1, "pledge");
                    331:
1.42      tedu      332:        closefrom(STDERR_FILENO + 1);
1.1       tedu      333:
1.26      espie     334:        uid = getuid();
1.34      tedu      335:
1.47      sthen     336:        while ((ch = getopt(argc, argv, "a:C:nsu:")) != -1) {
1.1       tedu      337:                switch (ch) {
1.47      sthen     338:                case 'a':
                    339:                        login_style = optarg;
                    340:                        break;
1.16      tedu      341:                case 'C':
1.22      zhuk      342:                        confpath = optarg;
                    343:                        break;
1.1       tedu      344:                case 'u':
                    345:                        if (parseuid(optarg, &target) != 0)
                    346:                                errx(1, "unknown user");
                    347:                        break;
1.26      espie     348:                case 'n':
                    349:                        nflag = 1;
                    350:                        break;
1.8       nicm      351:                case 's':
                    352:                        sflag = 1;
                    353:                        break;
1.1       tedu      354:                default:
                    355:                        usage();
                    356:                        break;
                    357:                }
                    358:        }
                    359:        argv += optind;
                    360:        argc -= optind;
                    361:
1.22      zhuk      362:        if (confpath) {
                    363:                if (sflag)
                    364:                        usage();
                    365:        } else if ((!sflag && !argc) || (sflag && argc))
1.1       tedu      366:                usage();
1.16      tedu      367:
1.1       tedu      368:        pw = getpwuid(uid);
                    369:        if (!pw)
                    370:                err(1, "getpwuid failed");
1.7       doug      371:        if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
                    372:                errx(1, "pw_name too long");
1.1       tedu      373:        ngroups = getgroups(NGROUPS_MAX, groups);
                    374:        if (ngroups == -1)
                    375:                err(1, "can't get groups");
                    376:        groups[ngroups++] = getgid();
1.8       nicm      377:
                    378:        if (sflag) {
                    379:                sh = getenv("SHELL");
                    380:                if (sh == NULL || *sh == '\0')
                    381:                        shargv[0] = pw->pw_shell;
                    382:                else
                    383:                        shargv[0] = sh;
                    384:                argv = shargv;
                    385:                argc = 1;
                    386:        }
1.22      zhuk      387:
1.24      tedu      388:        if (confpath) {
                    389:                checkconfig(confpath, argc, argv, uid, groups, ngroups,
                    390:                    target);
                    391:                exit(1);        /* fail safe */
                    392:        }
                    393:
1.22      zhuk      394:        parseconfig("/etc/doas.conf", 1);
1.8       nicm      395:
1.23      zhuk      396:        /* cmdline is used only for logging, no need to abort on truncate */
1.25      zhuk      397:        (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
1.8       nicm      398:        for (i = 1; i < argc; i++) {
                    399:                if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
1.23      zhuk      400:                        break;
1.8       nicm      401:                if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
1.23      zhuk      402:                        break;
1.8       nicm      403:        }
1.1       tedu      404:
1.23      zhuk      405:        cmd = argv[0];
1.15      zhuk      406:        if (!permit(uid, groups, ngroups, &rule, target, cmd,
                    407:            (const char**)argv + 1)) {
1.4       deraadt   408:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
                    409:                    "failed command for %s: %s", myname, cmdline);
1.41      tedu      410:                errc(1, EPERM, NULL);
1.1       tedu      411:        }
                    412:
                    413:        if (!(rule->options & NOPASS)) {
1.46      tedu      414:                char *challenge = NULL, *response, rbuf[1024], cbuf[128];
                    415:                auth_session_t *as;
                    416:
1.26      espie     417:                if (nflag)
                    418:                        errx(1, "Authorization required");
1.46      tedu      419:
1.47      sthen     420:                if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
1.46      tedu      421:                    &challenge)))
1.48      tedu      422:                        errx(1, "Authorization failed");
1.46      tedu      423:                if (!challenge) {
                    424:                        char host[HOST_NAME_MAX + 1];
                    425:                        if (gethostname(host, sizeof(host)))
                    426:                                snprintf(host, sizeof(host), "?");
                    427:                        snprintf(cbuf, sizeof(cbuf),
                    428:                            "doas (%.32s@%.32s) password: ", myname, host);
                    429:                        challenge = cbuf;
                    430:                }
1.50    ! tedu      431:                response = readpassphrase(challenge, rbuf, sizeof(rbuf),
        !           432:                    RPP_REQUIRE_TTY);
        !           433:                if (response == NULL && errno == ENOTTY) {
        !           434:                        syslog(LOG_AUTHPRIV | LOG_NOTICE,
        !           435:                            "tty required for %s", myname);
        !           436:                        errx(1, "a tty is required");
        !           437:                }
1.46      tedu      438:                if (!auth_userresponse(as, response, 0)) {
1.4       deraadt   439:                        syslog(LOG_AUTHPRIV | LOG_NOTICE,
1.46      tedu      440:                            "failed auth for %s", myname);
1.41      tedu      441:                        errc(1, EPERM, NULL);
1.1       tedu      442:                }
1.49      gsoares   443:                explicit_bzero(rbuf, sizeof(rbuf));
1.1       tedu      444:        }
1.43      deraadt   445:
                    446:        if (pledge("stdio rpath getpw exec id", NULL) == -1)
                    447:                err(1, "pledge");
                    448:
1.1       tedu      449:        pw = getpwuid(target);
                    450:        if (!pw)
                    451:                errx(1, "no passwd entry for target");
1.43      deraadt   452:
1.1       tedu      453:        if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
                    454:            LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
                    455:            LOGIN_SETUSER) != 0)
                    456:                errx(1, "failed to set user context for target");
                    457:
1.43      deraadt   458:        if (pledge("stdio rpath exec", NULL) == -1)
                    459:                err(1, "pledge");
                    460:
1.38      doug      461:        if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
                    462:                cwd = "(failed)";
                    463:        else
                    464:                cwd = cwdpath;
1.43      deraadt   465:
                    466:        if (pledge("stdio exec", NULL) == -1)
                    467:                err(1, "pledge");
1.38      doug      468:
                    469:        syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
                    470:            myname, cmdline, pw->pw_name, cwd);
1.44      tedu      471:
                    472:        envp = copyenv((const char **)envp, rule);
1.38      doug      473:
1.40      tedu      474:        if (rule->cmd) {
                    475:                if (setenv("PATH", safepath, 1) == -1)
                    476:                        err(1, "failed to set PATH '%s'", safepath);
                    477:        }
1.1       tedu      478:        execvpe(cmd, argv, envp);
1.10      tedu      479:        if (errno == ENOENT)
                    480:                errx(1, "%s: command not found", cmd);
1.1       tedu      481:        err(1, "%s", cmd);
                    482: }