Annotation of src/usr.bin/doas/doas.c, Revision 1.14
1.14 ! tedu 1: /* $OpenBSD: doas.c,v 1.13 2015/07/20 01:00:48 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[] = {
1.14 ! tedu 191: "DISPLAY", "HOME", "LOGNAME", "MAIL",
1.1 tedu 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]);
1.13 tedu 216: if (strncmp(oldenvp[i], badset[j], len) == 0 &&
217: oldenvp[i][len] == '=') {
1.11 tedu 218: break;
219: }
220: }
1.12 tedu 221: if (j == nbad) {
1.11 tedu 222: if (!(envp[ii] = strdup(oldenvp[i])))
223: err(1, "strdup");
224: ii++;
225: }
1.1 tedu 226: }
1.11 tedu 227: envp[ii] = NULL;
1.1 tedu 228: return envp;
229: }
230:
231: nsafe = arraylen(safeset);
232: if ((extra = rule->envlist)) {
1.12 tedu 233: size_t i;
1.1 tedu 234: nextras = arraylen(extra);
235: for (i = 0; i < nsafe; i++) {
1.12 tedu 236: size_t j;
1.1 tedu 237: for (j = 0; j < nextras; j++) {
238: if (strcmp(extra[j], safeset[i]) == 0) {
239: extra[j--] = extra[nextras--];
240: extra[nextras] = NULL;
241: }
242: }
243: }
244: }
245:
246: envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
247: if (!envp)
248: err(1, "can't allocate new environment");
249:
250: ei = 0;
251: ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
252: ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
253: envp[ei] = NULL;
254:
255: return envp;
256: }
257:
258: static void __dead
259: fail(void)
260: {
1.3 tedu 261: fprintf(stderr, "Permission denied\n");
1.1 tedu 262: exit(1);
263: }
264:
265: int
266: main(int argc, char **argv, char **envp)
267: {
1.9 tedu 268: const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
269: "/usr/local/bin:/usr/local/sbin";
270: char *shargv[] = { NULL, NULL };
271: char *sh;
272: const char *cmd;
1.7 doug 273: char cmdline[LINE_MAX];
274: char myname[_PW_NAME_LEN + 1];
1.9 tedu 275: struct passwd *pw;
276: struct rule *rule;
277: uid_t uid;
278: uid_t target = 0;
1.1 tedu 279: gid_t groups[NGROUPS_MAX + 1];
280: int ngroups;
281: int i, ch;
1.8 nicm 282: int sflag = 0;
1.1 tedu 283:
284: parseconfig("/etc/doas.conf");
285:
1.8 nicm 286: while ((ch = getopt(argc, argv, "su:")) != -1) {
1.1 tedu 287: switch (ch) {
288: case 'u':
289: if (parseuid(optarg, &target) != 0)
290: errx(1, "unknown user");
291: break;
1.8 nicm 292: case 's':
293: sflag = 1;
294: break;
1.1 tedu 295: default:
296: usage();
297: break;
298: }
299: }
300: argv += optind;
301: argc -= optind;
302:
1.8 nicm 303: if ((!sflag && !argc) || (sflag && argc))
1.1 tedu 304: usage();
305:
306: uid = getuid();
307: pw = getpwuid(uid);
308: if (!pw)
309: err(1, "getpwuid failed");
1.7 doug 310: if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
311: errx(1, "pw_name too long");
1.1 tedu 312: ngroups = getgroups(NGROUPS_MAX, groups);
313: if (ngroups == -1)
314: err(1, "can't get groups");
315: groups[ngroups++] = getgid();
1.8 nicm 316:
317: if (sflag) {
318: sh = getenv("SHELL");
319: if (sh == NULL || *sh == '\0')
320: shargv[0] = pw->pw_shell;
321: else
322: shargv[0] = sh;
323: argv = shargv;
324: argc = 1;
325: }
326:
327: cmd = argv[0];
328: if (strlcpy(cmdline, argv[0], sizeof(cmdline)) >= sizeof(cmdline))
329: errx(1, "command line too long");
330: for (i = 1; i < argc; i++) {
331: if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
332: errx(1, "command line too long");
333: if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
334: errx(1, "command line too long");
335: }
1.1 tedu 336:
337: if (!permit(uid, groups, ngroups, &rule, target, cmd)) {
1.4 deraadt 338: syslog(LOG_AUTHPRIV | LOG_NOTICE,
339: "failed command for %s: %s", myname, cmdline);
1.1 tedu 340: fail();
341: }
342:
343: if (!(rule->options & NOPASS)) {
344: if (!auth_userokay(myname, NULL, NULL, NULL)) {
1.4 deraadt 345: syslog(LOG_AUTHPRIV | LOG_NOTICE,
346: "failed password for %s", myname);
1.1 tedu 347: fail();
348: }
349: }
350: envp = copyenv((const char **)envp, rule);
351:
352: pw = getpwuid(target);
353: if (!pw)
354: errx(1, "no passwd entry for target");
355: if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
356: LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
357: LOGIN_SETUSER) != 0)
358: errx(1, "failed to set user context for target");
359:
1.4 deraadt 360: syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
361: myname, pw->pw_name, cmdline);
1.7 doug 362: if (setenv("PATH", safepath, 1) == -1)
363: err(1, "failed to set PATH '%s'", safepath);
1.1 tedu 364: execvpe(cmd, argv, envp);
1.10 tedu 365: if (errno == ENOENT)
366: errx(1, "%s: command not found", cmd);
1.1 tedu 367: err(1, "%s", cmd);
368: }