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

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