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

1.67    ! tedu        1: /* $OpenBSD: doas.c,v 1.66 2016/10/05 17:36:53 tedu Exp $ */
1.1       tedu        2: /*
                      3:  * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
1.6       nicm       17:
1.1       tedu       18: #include <sys/types.h>
1.6       nicm       19: #include <sys/stat.h>
1.63      tedu       20: #include <sys/ioctl.h>
1.1       tedu       21:
                     22: #include <limits.h>
                     23: #include <login_cap.h>
                     24: #include <bsd_auth.h>
1.46      tedu       25: #include <readpassphrase.h>
1.1       tedu       26: #include <string.h>
                     27: #include <stdio.h>
                     28: #include <stdlib.h>
                     29: #include <err.h>
                     30: #include <unistd.h>
                     31: #include <pwd.h>
                     32: #include <grp.h>
                     33: #include <syslog.h>
1.10      tedu       34: #include <errno.h>
1.63      tedu       35: #include <fcntl.h>
1.1       tedu       36:
                     37: #include "doas.h"
                     38:
                     39: static void __dead
                     40: usage(void)
                     41: {
1.63      tedu       42:        fprintf(stderr, "usage: doas [-Lns] [-a style] [-C config] [-u user]"
1.47      sthen      43:            " command [args]\n");
1.1       tedu       44:        exit(1);
                     45: }
                     46:
                     47: static int
                     48: parseuid(const char *s, uid_t *uid)
                     49: {
                     50:        struct passwd *pw;
                     51:        const char *errstr;
                     52:
                     53:        if ((pw = getpwnam(s)) != NULL) {
                     54:                *uid = pw->pw_uid;
                     55:                return 0;
                     56:        }
                     57:        *uid = strtonum(s, 0, UID_MAX, &errstr);
                     58:        if (errstr)
                     59:                return -1;
                     60:        return 0;
                     61: }
                     62:
                     63: static int
                     64: uidcheck(const char *s, uid_t desired)
                     65: {
                     66:        uid_t uid;
                     67:
                     68:        if (parseuid(s, &uid) != 0)
                     69:                return -1;
                     70:        if (uid != desired)
                     71:                return -1;
                     72:        return 0;
                     73: }
                     74:
1.33      tedu       75: static int
                     76: parsegid(const char *s, gid_t *gid)
1.1       tedu       77: {
                     78:        struct group *gr;
                     79:        const char *errstr;
                     80:
1.33      tedu       81:        if ((gr = getgrnam(s)) != NULL) {
                     82:                *gid = gr->gr_gid;
                     83:                return 0;
                     84:        }
                     85:        *gid = strtonum(s, 0, GID_MAX, &errstr);
1.1       tedu       86:        if (errstr)
                     87:                return -1;
1.33      tedu       88:        return 0;
1.1       tedu       89: }
                     90:
                     91: static int
                     92: match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
1.15      zhuk       93:     const char **cmdargs, struct rule *r)
1.1       tedu       94: {
                     95:        int i;
                     96:
                     97:        if (r->ident[0] == ':') {
1.33      tedu       98:                gid_t rgid;
                     99:                if (parsegid(r->ident + 1, &rgid) == -1)
1.1       tedu      100:                        return 0;
                    101:                for (i = 0; i < ngroups; i++) {
                    102:                        if (rgid == groups[i])
                    103:                                break;
                    104:                }
                    105:                if (i == ngroups)
                    106:                        return 0;
                    107:        } else {
                    108:                if (uidcheck(r->ident, uid) != 0)
                    109:                        return 0;
                    110:        }
                    111:        if (r->target && uidcheck(r->target, target) != 0)
                    112:                return 0;
1.15      zhuk      113:        if (r->cmd) {
                    114:                if (strcmp(r->cmd, cmd))
                    115:                        return 0;
                    116:                if (r->cmdargs) {
                    117:                        /* if arguments were given, they should match explicitly */
                    118:                        for (i = 0; r->cmdargs[i]; i++) {
                    119:                                if (!cmdargs[i])
                    120:                                        return 0;
                    121:                                if (strcmp(r->cmdargs[i], cmdargs[i]))
                    122:                                        return 0;
                    123:                        }
                    124:                        if (cmdargs[i])
                    125:                                return 0;
                    126:                }
                    127:        }
1.1       tedu      128:        return 1;
                    129: }
                    130:
                    131: static int
                    132: permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
1.15      zhuk      133:     uid_t target, const char *cmd, const char **cmdargs)
1.1       tedu      134: {
                    135:        int i;
                    136:
                    137:        *lastr = NULL;
                    138:        for (i = 0; i < nrules; i++) {
1.31      deraadt   139:                if (match(uid, groups, ngroups, target, cmd,
                    140:                    cmdargs, rules[i]))
1.1       tedu      141:                        *lastr = rules[i];
                    142:        }
                    143:        if (!*lastr)
                    144:                return 0;
                    145:        return (*lastr)->action == PERMIT;
                    146: }
                    147:
                    148: static void
1.22      zhuk      149: parseconfig(const char *filename, int checkperms)
1.1       tedu      150: {
                    151:        extern FILE *yyfp;
1.67    ! tedu      152:        extern int yyparse(void);
1.6       nicm      153:        struct stat sb;
1.1       tedu      154:
                    155:        yyfp = fopen(filename, "r");
1.36      espie     156:        if (!yyfp)
                    157:                err(1, checkperms ? "doas is not enabled, %s" :
                    158:                    "could not open config file %s", filename);
1.6       nicm      159:
1.22      zhuk      160:        if (checkperms) {
                    161:                if (fstat(fileno(yyfp), &sb) != 0)
                    162:                        err(1, "fstat(\"%s\")", filename);
                    163:                if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
                    164:                        errx(1, "%s is writable by group or other", filename);
                    165:                if (sb.st_uid != 0)
                    166:                        errx(1, "%s is not owned by root", filename);
                    167:        }
1.6       nicm      168:
1.1       tedu      169:        fclose(yyfp);
1.21      zhuk      170:        if (parse_errors)
                    171:                exit(1);
1.1       tedu      172: }
                    173:
                    174: static void __dead
1.22      zhuk      175: checkconfig(const char *confpath, int argc, char **argv,
1.24      tedu      176:     uid_t uid, gid_t *groups, int ngroups, uid_t target)
                    177: {
1.22      zhuk      178:        struct rule *rule;
                    179:
                    180:        setresuid(uid, uid, uid);
                    181:        parseconfig(confpath, 0);
                    182:        if (!argc)
                    183:                exit(0);
                    184:
                    185:        if (permit(uid, groups, ngroups, &rule, target, argv[0],
                    186:            (const char **)argv + 1)) {
                    187:                printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
1.24      tedu      188:                exit(0);
1.22      zhuk      189:        } else {
                    190:                printf("deny\n");
1.24      tedu      191:                exit(1);
1.22      zhuk      192:        }
                    193: }
                    194:
1.61      tedu      195: static void
1.63      tedu      196: authuser(char *myname, char *login_style, int persist)
1.61      tedu      197: {
                    198:        char *challenge = NULL, *response, rbuf[1024], cbuf[128];
                    199:        auth_session_t *as;
1.63      tedu      200:        int fd = -1;
                    201:
                    202:        if (persist)
                    203:                fd = open("/dev/tty", O_RDWR);
                    204:        if (fd != -1) {
                    205:                if (ioctl(fd, TIOCCHKVERAUTH) == 0)
                    206:                        goto good;
                    207:        }
1.61      tedu      208:
                    209:        if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
                    210:            &challenge)))
                    211:                errx(1, "Authorization failed");
                    212:        if (!challenge) {
                    213:                char host[HOST_NAME_MAX + 1];
                    214:                if (gethostname(host, sizeof(host)))
                    215:                        snprintf(host, sizeof(host), "?");
                    216:                snprintf(cbuf, sizeof(cbuf),
                    217:                    "\rdoas (%.32s@%.32s) password: ", myname, host);
                    218:                challenge = cbuf;
                    219:        }
                    220:        response = readpassphrase(challenge, rbuf, sizeof(rbuf),
                    221:            RPP_REQUIRE_TTY);
                    222:        if (response == NULL && errno == ENOTTY) {
                    223:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
                    224:                    "tty required for %s", myname);
                    225:                errx(1, "a tty is required");
                    226:        }
                    227:        if (!auth_userresponse(as, response, 0)) {
                    228:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
                    229:                    "failed auth for %s", myname);
                    230:                errc(1, EPERM, NULL);
                    231:        }
                    232:        explicit_bzero(rbuf, sizeof(rbuf));
1.63      tedu      233: good:
                    234:        if (fd != -1) {
1.64      tedu      235:                int secs = 5 * 60;
1.63      tedu      236:                ioctl(fd, TIOCSETVERAUTH, &secs);
                    237:                close(fd);
                    238:        }
1.61      tedu      239: }
                    240:
1.1       tedu      241: int
1.57      martijn   242: main(int argc, char **argv)
1.1       tedu      243: {
1.9       tedu      244:        const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
                    245:            "/usr/local/bin:/usr/local/sbin";
1.22      zhuk      246:        const char *confpath = NULL;
1.9       tedu      247:        char *shargv[] = { NULL, NULL };
                    248:        char *sh;
                    249:        const char *cmd;
1.7       doug      250:        char cmdline[LINE_MAX];
                    251:        char myname[_PW_NAME_LEN + 1];
1.9       tedu      252:        struct passwd *pw;
                    253:        struct rule *rule;
                    254:        uid_t uid;
                    255:        uid_t target = 0;
1.1       tedu      256:        gid_t groups[NGROUPS_MAX + 1];
                    257:        int ngroups;
                    258:        int i, ch;
1.8       nicm      259:        int sflag = 0;
1.26      espie     260:        int nflag = 0;
1.38      doug      261:        char cwdpath[PATH_MAX];
                    262:        const char *cwd;
1.47      sthen     263:        char *login_style = NULL;
1.57      martijn   264:        char **envp;
1.52      tedu      265:
                    266:        setprogname("doas");
1.42      tedu      267:
                    268:        closefrom(STDERR_FILENO + 1);
1.1       tedu      269:
1.26      espie     270:        uid = getuid();
1.34      tedu      271:
1.63      tedu      272:        while ((ch = getopt(argc, argv, "a:C:Lnsu:")) != -1) {
1.1       tedu      273:                switch (ch) {
1.47      sthen     274:                case 'a':
                    275:                        login_style = optarg;
                    276:                        break;
1.16      tedu      277:                case 'C':
1.22      zhuk      278:                        confpath = optarg;
                    279:                        break;
1.63      tedu      280:                case 'L':
                    281:                        i = open("/dev/tty", O_RDWR);
                    282:                        if (i != -1)
                    283:                                ioctl(i, TIOCCLRVERAUTH);
                    284:                        exit(i != -1);
1.1       tedu      285:                case 'u':
                    286:                        if (parseuid(optarg, &target) != 0)
                    287:                                errx(1, "unknown user");
                    288:                        break;
1.26      espie     289:                case 'n':
                    290:                        nflag = 1;
                    291:                        break;
1.8       nicm      292:                case 's':
                    293:                        sflag = 1;
                    294:                        break;
1.1       tedu      295:                default:
                    296:                        usage();
                    297:                        break;
                    298:                }
                    299:        }
                    300:        argv += optind;
                    301:        argc -= optind;
                    302:
1.22      zhuk      303:        if (confpath) {
                    304:                if (sflag)
                    305:                        usage();
                    306:        } else if ((!sflag && !argc) || (sflag && argc))
1.1       tedu      307:                usage();
1.16      tedu      308:
1.1       tedu      309:        pw = getpwuid(uid);
                    310:        if (!pw)
                    311:                err(1, "getpwuid failed");
1.7       doug      312:        if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
                    313:                errx(1, "pw_name too long");
1.1       tedu      314:        ngroups = getgroups(NGROUPS_MAX, groups);
                    315:        if (ngroups == -1)
                    316:                err(1, "can't get groups");
                    317:        groups[ngroups++] = getgid();
1.8       nicm      318:
                    319:        if (sflag) {
                    320:                sh = getenv("SHELL");
1.60      zhuk      321:                if (sh == NULL || *sh == '\0') {
                    322:                        shargv[0] = strdup(pw->pw_shell);
                    323:                        if (shargv[0] == NULL)
                    324:                                err(1, NULL);
                    325:                } else
1.8       nicm      326:                        shargv[0] = sh;
                    327:                argv = shargv;
                    328:                argc = 1;
                    329:        }
1.22      zhuk      330:
1.24      tedu      331:        if (confpath) {
                    332:                checkconfig(confpath, argc, argv, uid, groups, ngroups,
                    333:                    target);
                    334:                exit(1);        /* fail safe */
                    335:        }
                    336:
1.22      zhuk      337:        parseconfig("/etc/doas.conf", 1);
1.8       nicm      338:
1.23      zhuk      339:        /* cmdline is used only for logging, no need to abort on truncate */
1.58      tedu      340:        (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
1.8       nicm      341:        for (i = 1; i < argc; i++) {
                    342:                if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
1.23      zhuk      343:                        break;
1.8       nicm      344:                if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
1.23      zhuk      345:                        break;
1.8       nicm      346:        }
1.1       tedu      347:
1.23      zhuk      348:        cmd = argv[0];
1.15      zhuk      349:        if (!permit(uid, groups, ngroups, &rule, target, cmd,
1.58      tedu      350:            (const char **)argv + 1)) {
1.4       deraadt   351:                syslog(LOG_AUTHPRIV | LOG_NOTICE,
                    352:                    "failed command for %s: %s", myname, cmdline);
1.41      tedu      353:                errc(1, EPERM, NULL);
1.1       tedu      354:        }
                    355:
                    356:        if (!(rule->options & NOPASS)) {
1.26      espie     357:                if (nflag)
                    358:                        errx(1, "Authorization required");
1.46      tedu      359:
1.63      tedu      360:                authuser(myname, login_style, rule->options & PERSIST);
1.1       tedu      361:        }
1.43      deraadt   362:
                    363:        if (pledge("stdio rpath getpw exec id", NULL) == -1)
                    364:                err(1, "pledge");
                    365:
1.1       tedu      366:        pw = getpwuid(target);
                    367:        if (!pw)
                    368:                errx(1, "no passwd entry for target");
1.43      deraadt   369:
1.1       tedu      370:        if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
                    371:            LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
                    372:            LOGIN_SETUSER) != 0)
                    373:                errx(1, "failed to set user context for target");
                    374:
1.43      deraadt   375:        if (pledge("stdio rpath exec", NULL) == -1)
                    376:                err(1, "pledge");
                    377:
1.38      doug      378:        if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
                    379:                cwd = "(failed)";
                    380:        else
                    381:                cwd = cwdpath;
1.43      deraadt   382:
                    383:        if (pledge("stdio exec", NULL) == -1)
                    384:                err(1, "pledge");
1.38      doug      385:
                    386:        syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
                    387:            myname, cmdline, pw->pw_name, cwd);
1.44      tedu      388:
1.57      martijn   389:        envp = prepenv(rule);
1.38      doug      390:
1.40      tedu      391:        if (rule->cmd) {
                    392:                if (setenv("PATH", safepath, 1) == -1)
                    393:                        err(1, "failed to set PATH '%s'", safepath);
                    394:        }
1.1       tedu      395:        execvpe(cmd, argv, envp);
1.10      tedu      396:        if (errno == ENOENT)
                    397:                errx(1, "%s: command not found", cmd);
1.1       tedu      398:        err(1, "%s", cmd);
                    399: }