Annotation of src/usr.bin/doas/doas.c, Revision 1.33
1.33 ! tedu 1: /* $OpenBSD: doas.c,v 1.32 2015/07/29 00:00:31 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.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:
1.33 ! tedu 83: static int
! 84: parsegid(const char *s, gid_t *gid)
1.1 tedu 85: {
86: struct group *gr;
87: const char *errstr;
88:
1.33 ! tedu 89: if ((gr = getgrnam(s)) != NULL) {
! 90: *gid = gr->gr_gid;
! 91: return 0;
! 92: }
! 93: *gid = strtonum(s, 0, GID_MAX, &errstr);
1.1 tedu 94: if (errstr)
95: return -1;
1.33 ! tedu 96: return 0;
1.1 tedu 97: }
98:
99: static int
100: match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
1.15 zhuk 101: const char **cmdargs, struct rule *r)
1.1 tedu 102: {
103: int i;
104:
105: if (r->ident[0] == ':') {
1.33 ! tedu 106: gid_t rgid;
! 107: if (parsegid(r->ident + 1, &rgid) == -1)
1.1 tedu 108: return 0;
109: for (i = 0; i < ngroups; i++) {
110: if (rgid == groups[i])
111: break;
112: }
113: if (i == ngroups)
114: return 0;
115: } else {
116: if (uidcheck(r->ident, uid) != 0)
117: return 0;
118: }
119: if (r->target && uidcheck(r->target, target) != 0)
120: return 0;
1.15 zhuk 121: if (r->cmd) {
122: if (strcmp(r->cmd, cmd))
123: return 0;
124: if (r->cmdargs) {
125: /* if arguments were given, they should match explicitly */
126: for (i = 0; r->cmdargs[i]; i++) {
127: if (!cmdargs[i])
128: return 0;
129: if (strcmp(r->cmdargs[i], cmdargs[i]))
130: return 0;
131: }
132: if (cmdargs[i])
133: return 0;
134: }
135: }
1.1 tedu 136: return 1;
137: }
138:
139: static int
140: permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
1.15 zhuk 141: uid_t target, const char *cmd, const char **cmdargs)
1.1 tedu 142: {
143: int i;
144:
145: *lastr = NULL;
146: for (i = 0; i < nrules; i++) {
1.31 deraadt 147: if (match(uid, groups, ngroups, target, cmd,
148: cmdargs, rules[i]))
1.1 tedu 149: *lastr = rules[i];
150: }
151: if (!*lastr)
152: return 0;
153: return (*lastr)->action == PERMIT;
154: }
155:
156: static void
1.22 zhuk 157: parseconfig(const char *filename, int checkperms)
1.1 tedu 158: {
159: extern FILE *yyfp;
160: extern int yyparse(void);
1.6 nicm 161: struct stat sb;
1.1 tedu 162:
163: yyfp = fopen(filename, "r");
164: if (!yyfp) {
1.22 zhuk 165: if (checkperms)
166: fprintf(stderr, "doas is not enabled.\n");
167: else
168: warn("could not open config file");
1.1 tedu 169: exit(1);
170: }
1.6 nicm 171:
1.22 zhuk 172: if (checkperms) {
173: if (fstat(fileno(yyfp), &sb) != 0)
174: err(1, "fstat(\"%s\")", filename);
175: if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
176: errx(1, "%s is writable by group or other", filename);
177: if (sb.st_uid != 0)
178: errx(1, "%s is not owned by root", filename);
179: }
1.6 nicm 180:
1.1 tedu 181: yyparse();
182: fclose(yyfp);
1.21 zhuk 183: if (parse_errors)
184: exit(1);
1.1 tedu 185: }
186:
1.30 zhuk 187: /*
1.32 tedu 188: * Copy the environment variables in safeset from oldenvp to envp.
1.30 zhuk 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: }