Annotation of src/usr.bin/doas/doas.c, Revision 1.19
1.19 ! deraadt 1: /* $OpenBSD: doas.c,v 1.18 2015/07/21 17:49:33 jmc 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
155: parseconfig(const char *filename)
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) {
163: fprintf(stderr, "doas is not enabled.\n");
164: exit(1);
165: }
1.6 nicm 166:
167: if (fstat(fileno(yyfp), &sb) != 0)
168: err(1, "fstat(\"%s\")", filename);
169: if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
170: errx(1, "%s is writable by group or other", filename);
171: if (sb.st_uid != 0)
172: errx(1, "%s is not owned by root", filename);
173:
1.1 tedu 174: yyparse();
175: fclose(yyfp);
176: }
177:
178: static int
1.4 deraadt 179: copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
180: char **envp, int ei)
1.1 tedu 181: {
182: int i;
1.9 tedu 183:
1.1 tedu 184: for (i = 0; i < nsafe; i++) {
185: const char **oe = oldenvp;
186: while (*oe) {
187: size_t len = strlen(safeset[i]);
188: if (strncmp(*oe, safeset[i], len) == 0 &&
189: (*oe)[len] == '=') {
190: if (!(envp[ei++] = strdup(*oe)))
191: err(1, "strdup");
192: break;
193: }
194: oe++;
195: }
196: }
197: return ei;
198: }
199:
200: static char **
201: copyenv(const char **oldenvp, struct rule *rule)
202: {
203: const char *safeset[] = {
1.14 tedu 204: "DISPLAY", "HOME", "LOGNAME", "MAIL",
1.1 tedu 205: "PATH", "TERM", "USER", "USERNAME",
1.11 tedu 206: NULL
207: };
208: const char *badset[] = {
209: "ENV",
210: NULL
1.1 tedu 211: };
212: char **envp;
213: const char **extra;
214: int ei;
1.11 tedu 215: int nsafe, nbad;
1.9 tedu 216: int nextras = 0;
1.1 tedu 217:
1.11 tedu 218: nbad = arraylen(badset);
1.1 tedu 219: if ((rule->options & KEEPENV) && !rule->envlist) {
1.12 tedu 220: size_t i, ii;
221: size_t oldlen = arraylen(oldenvp);
222: envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
1.5 nicm 223: if (!envp)
224: err(1, "reallocarray");
1.12 tedu 225: for (ii = i = 0; i < oldlen; i++) {
226: size_t j;
227: for (j = 0; j < nbad; j++) {
228: size_t len = strlen(badset[j]);
1.13 tedu 229: if (strncmp(oldenvp[i], badset[j], len) == 0 &&
1.19 ! deraadt 230: oldenvp[i][len] == '=') {
1.11 tedu 231: break;
232: }
233: }
1.12 tedu 234: if (j == nbad) {
1.11 tedu 235: if (!(envp[ii] = strdup(oldenvp[i])))
236: err(1, "strdup");
237: ii++;
238: }
1.1 tedu 239: }
1.11 tedu 240: envp[ii] = NULL;
1.1 tedu 241: return envp;
242: }
243:
244: nsafe = arraylen(safeset);
245: if ((extra = rule->envlist)) {
1.12 tedu 246: size_t i;
1.1 tedu 247: nextras = arraylen(extra);
248: for (i = 0; i < nsafe; i++) {
1.12 tedu 249: size_t j;
1.1 tedu 250: for (j = 0; j < nextras; j++) {
251: if (strcmp(extra[j], safeset[i]) == 0) {
252: extra[j--] = extra[nextras--];
253: extra[nextras] = NULL;
254: }
255: }
256: }
257: }
258:
259: envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
260: if (!envp)
261: err(1, "can't allocate new environment");
262:
263: ei = 0;
264: ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
265: ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
266: envp[ei] = NULL;
267:
268: return envp;
269: }
270:
271: static void __dead
272: fail(void)
273: {
1.3 tedu 274: fprintf(stderr, "Permission denied\n");
1.1 tedu 275: exit(1);
276: }
277:
278: int
279: main(int argc, char **argv, char **envp)
280: {
1.9 tedu 281: const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
282: "/usr/local/bin:/usr/local/sbin";
283: char *shargv[] = { NULL, NULL };
284: char *sh;
285: const char *cmd;
1.7 doug 286: char cmdline[LINE_MAX];
287: char myname[_PW_NAME_LEN + 1];
1.9 tedu 288: struct passwd *pw;
289: struct rule *rule;
290: uid_t uid;
291: uid_t target = 0;
1.1 tedu 292: gid_t groups[NGROUPS_MAX + 1];
293: int ngroups;
294: int i, ch;
1.8 nicm 295: int sflag = 0;
1.1 tedu 296:
1.16 tedu 297: while ((ch = getopt(argc, argv, "C:su:")) != -1) {
1.1 tedu 298: switch (ch) {
1.16 tedu 299: case 'C':
1.17 tedu 300: target = getuid();
301: setresuid(target, target, target);
1.16 tedu 302: parseconfig(optarg);
303: exit(0);
1.1 tedu 304: case 'u':
305: if (parseuid(optarg, &target) != 0)
306: errx(1, "unknown user");
307: break;
1.8 nicm 308: case 's':
309: sflag = 1;
310: break;
1.1 tedu 311: default:
312: usage();
313: break;
314: }
315: }
316: argv += optind;
317: argc -= optind;
318:
1.8 nicm 319: if ((!sflag && !argc) || (sflag && argc))
1.1 tedu 320: usage();
1.16 tedu 321:
322: parseconfig("/etc/doas.conf");
1.1 tedu 323:
324: uid = getuid();
325: pw = getpwuid(uid);
326: if (!pw)
327: err(1, "getpwuid failed");
1.7 doug 328: if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
329: errx(1, "pw_name too long");
1.1 tedu 330: ngroups = getgroups(NGROUPS_MAX, groups);
331: if (ngroups == -1)
332: err(1, "can't get groups");
333: groups[ngroups++] = getgid();
1.8 nicm 334:
335: if (sflag) {
336: sh = getenv("SHELL");
337: if (sh == NULL || *sh == '\0')
338: shargv[0] = pw->pw_shell;
339: else
340: shargv[0] = sh;
341: argv = shargv;
342: argc = 1;
343: }
344:
345: cmd = argv[0];
346: if (strlcpy(cmdline, argv[0], sizeof(cmdline)) >= sizeof(cmdline))
347: errx(1, "command line too long");
348: for (i = 1; i < argc; i++) {
349: if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
350: errx(1, "command line too long");
351: if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
352: errx(1, "command line too long");
353: }
1.1 tedu 354:
1.15 zhuk 355: if (!permit(uid, groups, ngroups, &rule, target, cmd,
356: (const char**)argv + 1)) {
1.4 deraadt 357: syslog(LOG_AUTHPRIV | LOG_NOTICE,
358: "failed command for %s: %s", myname, cmdline);
1.1 tedu 359: fail();
360: }
361:
362: if (!(rule->options & NOPASS)) {
363: if (!auth_userokay(myname, NULL, NULL, NULL)) {
1.4 deraadt 364: syslog(LOG_AUTHPRIV | LOG_NOTICE,
365: "failed password for %s", myname);
1.1 tedu 366: fail();
367: }
368: }
369: envp = copyenv((const char **)envp, rule);
370:
371: pw = getpwuid(target);
372: if (!pw)
373: errx(1, "no passwd entry for target");
374: if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
375: LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
376: LOGIN_SETUSER) != 0)
377: errx(1, "failed to set user context for target");
378:
1.4 deraadt 379: syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
380: myname, pw->pw_name, cmdline);
1.7 doug 381: if (setenv("PATH", safepath, 1) == -1)
382: err(1, "failed to set PATH '%s'", safepath);
1.1 tedu 383: execvpe(cmd, argv, envp);
1.10 tedu 384: if (errno == ENOENT)
385: errx(1, "%s: command not found", cmd);
1.1 tedu 386: err(1, "%s", cmd);
387: }