Annotation of src/usr.bin/doas/doas.c, Revision 1.32
1.32 ! tedu 1: /* $OpenBSD: doas.c,v 1.31 2015/07/28 21:36:03 deraadt 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: /*
1.32 ! tedu 187: * Copy the environment variables in safeset from oldenvp to envp.
1.30 zhuk 188: */
1.1 tedu 189: static int
1.4 deraadt 190: copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
191: char **envp, int ei)
1.1 tedu 192: {
193: int i;
1.9 tedu 194:
1.1 tedu 195: for (i = 0; i < nsafe; i++) {
196: const char **oe = oldenvp;
197: while (*oe) {
198: size_t len = strlen(safeset[i]);
199: if (strncmp(*oe, safeset[i], len) == 0 &&
200: (*oe)[len] == '=') {
201: if (!(envp[ei++] = strdup(*oe)))
202: err(1, "strdup");
203: break;
204: }
205: oe++;
206: }
207: }
208: return ei;
209: }
210:
211: static char **
212: copyenv(const char **oldenvp, struct rule *rule)
213: {
214: const char *safeset[] = {
1.14 tedu 215: "DISPLAY", "HOME", "LOGNAME", "MAIL",
1.1 tedu 216: "PATH", "TERM", "USER", "USERNAME",
1.11 tedu 217: NULL
218: };
219: const char *badset[] = {
220: "ENV",
221: NULL
1.1 tedu 222: };
223: char **envp;
224: const char **extra;
225: int ei;
1.11 tedu 226: int nsafe, nbad;
1.9 tedu 227: int nextras = 0;
1.20 zhuk 228:
1.30 zhuk 229: /* if there was no envvar whitelist, pass all except badset ones */
1.11 tedu 230: nbad = arraylen(badset);
1.1 tedu 231: if ((rule->options & KEEPENV) && !rule->envlist) {
1.30 zhuk 232: size_t iold, inew;
1.12 tedu 233: size_t oldlen = arraylen(oldenvp);
234: envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
1.5 nicm 235: if (!envp)
236: err(1, "reallocarray");
1.30 zhuk 237: for (inew = iold = 0; iold < oldlen; iold++) {
238: size_t ibad;
239: for (ibad = 0; ibad < nbad; ibad++) {
240: size_t len = strlen(badset[ibad]);
241: if (strncmp(oldenvp[iold], badset[ibad], len) == 0 &&
242: oldenvp[iold][len] == '=') {
1.11 tedu 243: break;
244: }
245: }
1.30 zhuk 246: if (ibad == nbad) {
247: if (!(envp[inew] = strdup(oldenvp[iold])))
1.11 tedu 248: err(1, "strdup");
1.30 zhuk 249: inew++;
1.11 tedu 250: }
1.1 tedu 251: }
1.30 zhuk 252: envp[inew] = NULL;
1.1 tedu 253: return envp;
254: }
255:
256: nsafe = arraylen(safeset);
257: if ((extra = rule->envlist)) {
1.30 zhuk 258: size_t isafe;
1.1 tedu 259: nextras = arraylen(extra);
1.30 zhuk 260: for (isafe = 0; isafe < nsafe; isafe++) {
261: size_t iextras;
262: for (iextras = 0; iextras < nextras; iextras++) {
263: if (strcmp(extra[iextras], safeset[isafe]) == 0) {
1.29 zhuk 264: nextras--;
1.30 zhuk 265: extra[iextras] = extra[nextras];
1.1 tedu 266: extra[nextras] = NULL;
1.30 zhuk 267: iextras--;
1.1 tedu 268: }
269: }
270: }
271: }
272:
273: envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
274: if (!envp)
275: err(1, "can't allocate new environment");
276:
277: ei = 0;
278: ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
279: ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
280: envp[ei] = NULL;
281:
282: return envp;
283: }
284:
285: static void __dead
286: fail(void)
287: {
1.3 tedu 288: fprintf(stderr, "Permission denied\n");
1.1 tedu 289: exit(1);
290: }
291:
1.27 tedu 292: static void __dead
1.22 zhuk 293: checkconfig(const char *confpath, int argc, char **argv,
1.24 tedu 294: uid_t uid, gid_t *groups, int ngroups, uid_t target)
295: {
1.22 zhuk 296: struct rule *rule;
297:
298: setresuid(uid, uid, uid);
299: parseconfig(confpath, 0);
300: if (!argc)
301: exit(0);
302:
303: if (permit(uid, groups, ngroups, &rule, target, argv[0],
304: (const char **)argv + 1)) {
305: printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
1.24 tedu 306: exit(0);
1.22 zhuk 307: } else {
308: printf("deny\n");
1.24 tedu 309: exit(1);
1.22 zhuk 310: }
311: }
312:
1.1 tedu 313: int
314: main(int argc, char **argv, char **envp)
315: {
1.9 tedu 316: const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
317: "/usr/local/bin:/usr/local/sbin";
1.22 zhuk 318: const char *confpath = NULL;
1.9 tedu 319: char *shargv[] = { NULL, NULL };
320: char *sh;
321: const char *cmd;
1.7 doug 322: char cmdline[LINE_MAX];
323: char myname[_PW_NAME_LEN + 1];
1.9 tedu 324: struct passwd *pw;
325: struct rule *rule;
326: uid_t uid;
327: uid_t target = 0;
1.1 tedu 328: gid_t groups[NGROUPS_MAX + 1];
329: int ngroups;
330: int i, ch;
1.8 nicm 331: int sflag = 0;
1.26 espie 332: int nflag = 0;
1.1 tedu 333:
1.26 espie 334: uid = getuid();
335: while ((ch = getopt(argc, argv, "C:nsu:")) != -1) {
1.1 tedu 336: switch (ch) {
1.16 tedu 337: case 'C':
1.22 zhuk 338: confpath = optarg;
339: break;
1.1 tedu 340: case 'u':
341: if (parseuid(optarg, &target) != 0)
342: errx(1, "unknown user");
343: break;
1.26 espie 344: case 'n':
345: nflag = 1;
346: break;
1.8 nicm 347: case 's':
348: sflag = 1;
349: break;
1.1 tedu 350: default:
351: usage();
352: break;
353: }
354: }
355: argv += optind;
356: argc -= optind;
357:
1.22 zhuk 358: if (confpath) {
359: if (sflag)
360: usage();
361: } else if ((!sflag && !argc) || (sflag && argc))
1.1 tedu 362: usage();
1.16 tedu 363:
1.22 zhuk 364: uid = getuid();
1.1 tedu 365: pw = getpwuid(uid);
366: if (!pw)
367: err(1, "getpwuid failed");
1.7 doug 368: if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
369: errx(1, "pw_name too long");
1.1 tedu 370: ngroups = getgroups(NGROUPS_MAX, groups);
371: if (ngroups == -1)
372: err(1, "can't get groups");
373: groups[ngroups++] = getgid();
1.8 nicm 374:
375: if (sflag) {
376: sh = getenv("SHELL");
377: if (sh == NULL || *sh == '\0')
378: shargv[0] = pw->pw_shell;
379: else
380: shargv[0] = sh;
381: argv = shargv;
382: argc = 1;
383: }
1.22 zhuk 384:
1.24 tedu 385: if (confpath) {
386: checkconfig(confpath, argc, argv, uid, groups, ngroups,
387: target);
388: exit(1); /* fail safe */
389: }
390:
1.22 zhuk 391: parseconfig("/etc/doas.conf", 1);
1.8 nicm 392:
1.23 zhuk 393: /* cmdline is used only for logging, no need to abort on truncate */
1.25 zhuk 394: (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
1.8 nicm 395: for (i = 1; i < argc; i++) {
396: if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
1.23 zhuk 397: break;
1.8 nicm 398: if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
1.23 zhuk 399: break;
1.8 nicm 400: }
1.1 tedu 401:
1.23 zhuk 402: cmd = argv[0];
1.15 zhuk 403: if (!permit(uid, groups, ngroups, &rule, target, cmd,
404: (const char**)argv + 1)) {
1.4 deraadt 405: syslog(LOG_AUTHPRIV | LOG_NOTICE,
406: "failed command for %s: %s", myname, cmdline);
1.1 tedu 407: fail();
408: }
409:
410: if (!(rule->options & NOPASS)) {
1.26 espie 411: if (nflag)
412: errx(1, "Authorization required");
1.1 tedu 413: if (!auth_userokay(myname, NULL, NULL, NULL)) {
1.4 deraadt 414: syslog(LOG_AUTHPRIV | LOG_NOTICE,
415: "failed password for %s", myname);
1.1 tedu 416: fail();
417: }
418: }
419: envp = copyenv((const char **)envp, rule);
420:
421: pw = getpwuid(target);
422: if (!pw)
423: errx(1, "no passwd entry for target");
424: if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
425: LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
426: LOGIN_SETUSER) != 0)
427: errx(1, "failed to set user context for target");
428:
1.4 deraadt 429: syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
430: myname, pw->pw_name, cmdline);
1.7 doug 431: if (setenv("PATH", safepath, 1) == -1)
432: err(1, "failed to set PATH '%s'", safepath);
1.1 tedu 433: execvpe(cmd, argv, envp);
1.10 tedu 434: if (errno == ENOENT)
435: errx(1, "%s: command not found", cmd);
1.1 tedu 436: err(1, "%s", cmd);
437: }