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

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