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