Annotation of src/usr.bin/doas/doas.c, Revision 1.54
1.54 ! tedu 1: /* $OpenBSD: doas.c,v 1.53 2016/06/05 00:46:34 djm 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;
1.54 ! tedu 196: if (strchr(safeset[i], '='))
! 197: continue;
1.1 tedu 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:
1.53 djm 286: /* find index of 'name' in environment envp */
287: static int
288: findenv(const char **envp, const char *name, size_t namelen)
289: {
290: int i;
291:
292: for (i = 0 ; envp[i] != NULL; i++) {
293: if (strlen(envp[i]) < namelen + 1)
294: continue;
295: if (strncmp(envp[i], name, namelen) == 0 &&
296: envp[i][namelen] == '=')
297: return i;
298: }
299: return -1;
300: }
301:
1.54 ! tedu 302: /* merge rule->envlist into environment list; frees oldenvp */
1.53 djm 303: static char **
304: dosetenv(char **oldenvp, struct rule *rule)
305: {
306: size_t n, i, nset, nold;
307: char **envp, *cp, *cp2;
308: int found;
309:
310: if (!(rule->options & SETENV))
311: return oldenvp;
312:
1.54 ! tedu 313: nset = arraylen(rule->envlist);
1.53 djm 314: nold = arraylen((const char**)oldenvp);
315:
316: /* insert new variables */
317: n = 0;
318: envp = NULL;
319: for (i = 0; i < nset; i++) {
1.54 ! tedu 320: if ((cp = strchr(rule->envlist[i], '=')) == NULL)
! 321: continue;
! 322: if (cp[1] == '\0' || cp - rule->envlist[i] > INT_MAX)
1.53 djm 323: continue; /* skip variables with empty values */
324: if ((envp = reallocarray(envp, n + 2, sizeof(*envp))) == NULL)
325: errx(1, "reallocarray failed");
326: if (cp[1] == '$') {
327: /* FOO=$BAR: lookup and copy */
328: if ((cp2 = getenv(cp + 2)) == NULL)
329: continue; /* not found; skip */
330: if (asprintf(&(envp[n++]), "%.*s=%s",
1.54 ! tedu 331: (int)(cp - rule->envlist[i]),
! 332: rule->envlist[i], cp2) == -1)
1.53 djm 333: errx(1, "asprintf failed");
334: continue;
335: } else {
336: /* plain setenv */
1.54 ! tedu 337: if ((envp[n++] = strdup(rule->envlist[i])) == NULL)
1.53 djm 338: errx(1, "strdup failed");
339: }
340: }
341: /* move old variables, dropping ones already set */
342: for (i = 0; i < nold; i++) {
343: if ((cp = strchr(oldenvp[i], '=')) == NULL)
344: errx(1, "invalid env"); /* shouldn't happen */
1.54 ! tedu 345: found = findenv(rule->envlist, oldenvp[i], cp - oldenvp[i]);
1.53 djm 346: if (found != -1)
347: free(oldenvp[i]); /* discard */
348: else {
349: if ((envp = reallocarray(envp, n + 2,
350: sizeof(*envp))) == NULL)
351: errx(1, "reallocarray failed");
352: envp[n++] = oldenvp[i]; /* move */
353: }
354: }
355: free(oldenvp);
356: if (n > 0)
357: envp[n] = NULL;
358: return envp;
359: }
360:
1.1 tedu 361: static void __dead
1.22 zhuk 362: checkconfig(const char *confpath, int argc, char **argv,
1.24 tedu 363: uid_t uid, gid_t *groups, int ngroups, uid_t target)
364: {
1.22 zhuk 365: struct rule *rule;
366:
367: setresuid(uid, uid, uid);
368: parseconfig(confpath, 0);
369: if (!argc)
370: exit(0);
371:
372: if (permit(uid, groups, ngroups, &rule, target, argv[0],
373: (const char **)argv + 1)) {
374: printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
1.24 tedu 375: exit(0);
1.22 zhuk 376: } else {
377: printf("deny\n");
1.24 tedu 378: exit(1);
1.22 zhuk 379: }
380: }
381:
1.1 tedu 382: int
383: main(int argc, char **argv, char **envp)
384: {
1.9 tedu 385: const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
386: "/usr/local/bin:/usr/local/sbin";
1.22 zhuk 387: const char *confpath = NULL;
1.9 tedu 388: char *shargv[] = { NULL, NULL };
389: char *sh;
390: const char *cmd;
1.7 doug 391: char cmdline[LINE_MAX];
392: char myname[_PW_NAME_LEN + 1];
1.9 tedu 393: struct passwd *pw;
394: struct rule *rule;
395: uid_t uid;
396: uid_t target = 0;
1.1 tedu 397: gid_t groups[NGROUPS_MAX + 1];
398: int ngroups;
399: int i, ch;
1.8 nicm 400: int sflag = 0;
1.26 espie 401: int nflag = 0;
1.38 doug 402: char cwdpath[PATH_MAX];
403: const char *cwd;
1.47 sthen 404: char *login_style = NULL;
1.52 tedu 405:
406: setprogname("doas");
1.42 tedu 407:
1.46 tedu 408: if (pledge("stdio rpath getpw tty proc exec id", NULL) == -1)
1.43 deraadt 409: err(1, "pledge");
410:
1.42 tedu 411: closefrom(STDERR_FILENO + 1);
1.1 tedu 412:
1.26 espie 413: uid = getuid();
1.34 tedu 414:
1.47 sthen 415: while ((ch = getopt(argc, argv, "a:C:nsu:")) != -1) {
1.1 tedu 416: switch (ch) {
1.47 sthen 417: case 'a':
418: login_style = optarg;
419: break;
1.16 tedu 420: case 'C':
1.22 zhuk 421: confpath = optarg;
422: break;
1.1 tedu 423: case 'u':
424: if (parseuid(optarg, &target) != 0)
425: errx(1, "unknown user");
426: break;
1.26 espie 427: case 'n':
428: nflag = 1;
429: break;
1.8 nicm 430: case 's':
431: sflag = 1;
432: break;
1.1 tedu 433: default:
434: usage();
435: break;
436: }
437: }
438: argv += optind;
439: argc -= optind;
440:
1.22 zhuk 441: if (confpath) {
442: if (sflag)
443: usage();
444: } else if ((!sflag && !argc) || (sflag && argc))
1.1 tedu 445: usage();
1.16 tedu 446:
1.1 tedu 447: pw = getpwuid(uid);
448: if (!pw)
449: err(1, "getpwuid failed");
1.7 doug 450: if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
451: errx(1, "pw_name too long");
1.1 tedu 452: ngroups = getgroups(NGROUPS_MAX, groups);
453: if (ngroups == -1)
454: err(1, "can't get groups");
455: groups[ngroups++] = getgid();
1.8 nicm 456:
457: if (sflag) {
458: sh = getenv("SHELL");
459: if (sh == NULL || *sh == '\0')
460: shargv[0] = pw->pw_shell;
461: else
462: shargv[0] = sh;
463: argv = shargv;
464: argc = 1;
465: }
1.22 zhuk 466:
1.24 tedu 467: if (confpath) {
468: checkconfig(confpath, argc, argv, uid, groups, ngroups,
469: target);
470: exit(1); /* fail safe */
471: }
472:
1.22 zhuk 473: parseconfig("/etc/doas.conf", 1);
1.8 nicm 474:
1.23 zhuk 475: /* cmdline is used only for logging, no need to abort on truncate */
1.25 zhuk 476: (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
1.8 nicm 477: for (i = 1; i < argc; i++) {
478: if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
1.23 zhuk 479: break;
1.8 nicm 480: if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
1.23 zhuk 481: break;
1.8 nicm 482: }
1.1 tedu 483:
1.23 zhuk 484: cmd = argv[0];
1.15 zhuk 485: if (!permit(uid, groups, ngroups, &rule, target, cmd,
486: (const char**)argv + 1)) {
1.4 deraadt 487: syslog(LOG_AUTHPRIV | LOG_NOTICE,
488: "failed command for %s: %s", myname, cmdline);
1.41 tedu 489: errc(1, EPERM, NULL);
1.1 tedu 490: }
491:
492: if (!(rule->options & NOPASS)) {
1.46 tedu 493: char *challenge = NULL, *response, rbuf[1024], cbuf[128];
494: auth_session_t *as;
495:
1.26 espie 496: if (nflag)
497: errx(1, "Authorization required");
1.46 tedu 498:
1.47 sthen 499: if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
1.46 tedu 500: &challenge)))
1.48 tedu 501: errx(1, "Authorization failed");
1.46 tedu 502: if (!challenge) {
503: char host[HOST_NAME_MAX + 1];
504: if (gethostname(host, sizeof(host)))
505: snprintf(host, sizeof(host), "?");
506: snprintf(cbuf, sizeof(cbuf),
1.51 martijn 507: "\rdoas (%.32s@%.32s) password: ", myname, host);
1.46 tedu 508: challenge = cbuf;
509: }
1.50 tedu 510: response = readpassphrase(challenge, rbuf, sizeof(rbuf),
511: RPP_REQUIRE_TTY);
512: if (response == NULL && errno == ENOTTY) {
513: syslog(LOG_AUTHPRIV | LOG_NOTICE,
514: "tty required for %s", myname);
515: errx(1, "a tty is required");
516: }
1.46 tedu 517: if (!auth_userresponse(as, response, 0)) {
1.4 deraadt 518: syslog(LOG_AUTHPRIV | LOG_NOTICE,
1.46 tedu 519: "failed auth for %s", myname);
1.41 tedu 520: errc(1, EPERM, NULL);
1.1 tedu 521: }
1.49 gsoares 522: explicit_bzero(rbuf, sizeof(rbuf));
1.1 tedu 523: }
1.43 deraadt 524:
525: if (pledge("stdio rpath getpw exec id", NULL) == -1)
526: err(1, "pledge");
527:
1.1 tedu 528: pw = getpwuid(target);
529: if (!pw)
530: errx(1, "no passwd entry for target");
1.43 deraadt 531:
1.1 tedu 532: if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
533: LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
534: LOGIN_SETUSER) != 0)
535: errx(1, "failed to set user context for target");
536:
1.43 deraadt 537: if (pledge("stdio rpath exec", NULL) == -1)
538: err(1, "pledge");
539:
1.38 doug 540: if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
541: cwd = "(failed)";
542: else
543: cwd = cwdpath;
1.43 deraadt 544:
545: if (pledge("stdio exec", NULL) == -1)
546: err(1, "pledge");
1.38 doug 547:
548: syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
549: myname, cmdline, pw->pw_name, cwd);
1.44 tedu 550:
551: envp = copyenv((const char **)envp, rule);
1.53 djm 552:
553: envp = dosetenv(envp, rule);
1.38 doug 554:
1.40 tedu 555: if (rule->cmd) {
556: if (setenv("PATH", safepath, 1) == -1)
557: err(1, "failed to set PATH '%s'", safepath);
558: }
1.1 tedu 559: execvpe(cmd, argv, envp);
1.10 tedu 560: if (errno == ENOENT)
561: errx(1, "%s: command not found", cmd);
1.1 tedu 562: err(1, "%s", cmd);
563: }