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