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