Annotation of src/usr.bin/doas/doas.c, Revision 1.56
1.56 ! tedu 1: /* $OpenBSD: doas.c,v 1.55 2016/06/07 16:49:23 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>
1.46 tedu 24: #include <readpassphrase.h>
1.1 tedu 25: #include <string.h>
26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <err.h>
29: #include <unistd.h>
30: #include <pwd.h>
31: #include <grp.h>
32: #include <syslog.h>
1.10 tedu 33: #include <errno.h>
1.1 tedu 34:
35: #include "doas.h"
36:
37: static void __dead
38: usage(void)
39: {
1.47 sthen 40: fprintf(stderr, "usage: doas [-ns] [-a style] [-C config] [-u user]"
41: " command [args]\n");
1.1 tedu 42: exit(1);
43: }
44:
45: size_t
46: arraylen(const char **arr)
47: {
48: size_t cnt = 0;
1.9 tedu 49:
1.1 tedu 50: while (*arr) {
51: cnt++;
52: arr++;
53: }
54: return cnt;
55: }
56:
57: static int
58: parseuid(const char *s, uid_t *uid)
59: {
60: struct passwd *pw;
61: const char *errstr;
62:
63: if ((pw = getpwnam(s)) != NULL) {
64: *uid = pw->pw_uid;
65: return 0;
66: }
67: *uid = strtonum(s, 0, UID_MAX, &errstr);
68: if (errstr)
69: return -1;
70: return 0;
71: }
72:
73: static int
74: uidcheck(const char *s, uid_t desired)
75: {
76: uid_t uid;
77:
78: if (parseuid(s, &uid) != 0)
79: return -1;
80: if (uid != desired)
81: return -1;
82: return 0;
83: }
84:
1.33 tedu 85: static int
86: parsegid(const char *s, gid_t *gid)
1.1 tedu 87: {
88: struct group *gr;
89: const char *errstr;
90:
1.33 tedu 91: if ((gr = getgrnam(s)) != NULL) {
92: *gid = gr->gr_gid;
93: return 0;
94: }
95: *gid = strtonum(s, 0, GID_MAX, &errstr);
1.1 tedu 96: if (errstr)
97: return -1;
1.33 tedu 98: return 0;
1.1 tedu 99: }
100:
101: static int
102: match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
1.15 zhuk 103: const char **cmdargs, struct rule *r)
1.1 tedu 104: {
105: int i;
106:
107: if (r->ident[0] == ':') {
1.33 tedu 108: gid_t rgid;
109: if (parsegid(r->ident + 1, &rgid) == -1)
1.1 tedu 110: return 0;
111: for (i = 0; i < ngroups; i++) {
112: if (rgid == groups[i])
113: break;
114: }
115: if (i == ngroups)
116: return 0;
117: } else {
118: if (uidcheck(r->ident, uid) != 0)
119: return 0;
120: }
121: if (r->target && uidcheck(r->target, target) != 0)
122: return 0;
1.15 zhuk 123: if (r->cmd) {
124: if (strcmp(r->cmd, cmd))
125: return 0;
126: if (r->cmdargs) {
127: /* if arguments were given, they should match explicitly */
128: for (i = 0; r->cmdargs[i]; i++) {
129: if (!cmdargs[i])
130: return 0;
131: if (strcmp(r->cmdargs[i], cmdargs[i]))
132: return 0;
133: }
134: if (cmdargs[i])
135: return 0;
136: }
137: }
1.1 tedu 138: return 1;
139: }
140:
141: static int
142: permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
1.15 zhuk 143: uid_t target, const char *cmd, const char **cmdargs)
1.1 tedu 144: {
145: int i;
146:
147: *lastr = NULL;
148: for (i = 0; i < nrules; i++) {
1.31 deraadt 149: if (match(uid, groups, ngroups, target, cmd,
150: cmdargs, rules[i]))
1.1 tedu 151: *lastr = rules[i];
152: }
153: if (!*lastr)
154: return 0;
155: return (*lastr)->action == PERMIT;
156: }
157:
158: static void
1.22 zhuk 159: parseconfig(const char *filename, int checkperms)
1.1 tedu 160: {
161: extern FILE *yyfp;
162: extern int yyparse(void);
1.6 nicm 163: struct stat sb;
1.1 tedu 164:
165: yyfp = fopen(filename, "r");
1.36 espie 166: if (!yyfp)
167: err(1, checkperms ? "doas is not enabled, %s" :
168: "could not open config file %s", filename);
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 void __dead
1.22 zhuk 186: checkconfig(const char *confpath, int argc, char **argv,
1.24 tedu 187: uid_t uid, gid_t *groups, int ngroups, uid_t target)
188: {
1.22 zhuk 189: struct rule *rule;
190:
191: setresuid(uid, uid, uid);
192: parseconfig(confpath, 0);
193: if (!argc)
194: exit(0);
195:
196: if (permit(uid, groups, ngroups, &rule, target, argv[0],
197: (const char **)argv + 1)) {
198: printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
1.24 tedu 199: exit(0);
1.22 zhuk 200: } else {
201: printf("deny\n");
1.24 tedu 202: exit(1);
1.22 zhuk 203: }
204: }
205:
1.1 tedu 206: int
207: main(int argc, char **argv, char **envp)
208: {
1.9 tedu 209: const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
210: "/usr/local/bin:/usr/local/sbin";
1.22 zhuk 211: const char *confpath = NULL;
1.9 tedu 212: char *shargv[] = { NULL, NULL };
213: char *sh;
214: const char *cmd;
1.56 ! tedu 215: struct env *env;
1.7 doug 216: char cmdline[LINE_MAX];
217: char myname[_PW_NAME_LEN + 1];
1.9 tedu 218: struct passwd *pw;
219: struct rule *rule;
220: uid_t uid;
221: uid_t target = 0;
1.1 tedu 222: gid_t groups[NGROUPS_MAX + 1];
223: int ngroups;
224: int i, ch;
1.8 nicm 225: int sflag = 0;
1.26 espie 226: int nflag = 0;
1.38 doug 227: char cwdpath[PATH_MAX];
228: const char *cwd;
1.47 sthen 229: char *login_style = NULL;
1.52 tedu 230:
231: setprogname("doas");
1.42 tedu 232:
1.46 tedu 233: if (pledge("stdio rpath getpw tty proc exec id", NULL) == -1)
1.43 deraadt 234: err(1, "pledge");
235:
1.42 tedu 236: closefrom(STDERR_FILENO + 1);
1.1 tedu 237:
1.26 espie 238: uid = getuid();
1.34 tedu 239:
1.47 sthen 240: while ((ch = getopt(argc, argv, "a:C:nsu:")) != -1) {
1.1 tedu 241: switch (ch) {
1.47 sthen 242: case 'a':
243: login_style = optarg;
244: break;
1.16 tedu 245: case 'C':
1.22 zhuk 246: confpath = optarg;
247: break;
1.1 tedu 248: case 'u':
249: if (parseuid(optarg, &target) != 0)
250: errx(1, "unknown user");
251: break;
1.26 espie 252: case 'n':
253: nflag = 1;
254: break;
1.8 nicm 255: case 's':
256: sflag = 1;
257: break;
1.1 tedu 258: default:
259: usage();
260: break;
261: }
262: }
263: argv += optind;
264: argc -= optind;
265:
1.22 zhuk 266: if (confpath) {
267: if (sflag)
268: usage();
269: } else if ((!sflag && !argc) || (sflag && argc))
1.1 tedu 270: usage();
1.16 tedu 271:
1.1 tedu 272: pw = getpwuid(uid);
273: if (!pw)
274: err(1, "getpwuid failed");
1.7 doug 275: if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
276: errx(1, "pw_name too long");
1.1 tedu 277: ngroups = getgroups(NGROUPS_MAX, groups);
278: if (ngroups == -1)
279: err(1, "can't get groups");
280: groups[ngroups++] = getgid();
1.8 nicm 281:
282: if (sflag) {
283: sh = getenv("SHELL");
284: if (sh == NULL || *sh == '\0')
285: shargv[0] = pw->pw_shell;
286: else
287: shargv[0] = sh;
288: argv = shargv;
289: argc = 1;
290: }
1.22 zhuk 291:
1.24 tedu 292: if (confpath) {
293: checkconfig(confpath, argc, argv, uid, groups, ngroups,
294: target);
295: exit(1); /* fail safe */
296: }
297:
1.22 zhuk 298: parseconfig("/etc/doas.conf", 1);
1.8 nicm 299:
1.23 zhuk 300: /* cmdline is used only for logging, no need to abort on truncate */
1.25 zhuk 301: (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
1.8 nicm 302: for (i = 1; i < argc; i++) {
303: if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
1.23 zhuk 304: break;
1.8 nicm 305: if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
1.23 zhuk 306: break;
1.8 nicm 307: }
1.1 tedu 308:
1.23 zhuk 309: cmd = argv[0];
1.15 zhuk 310: if (!permit(uid, groups, ngroups, &rule, target, cmd,
311: (const char**)argv + 1)) {
1.4 deraadt 312: syslog(LOG_AUTHPRIV | LOG_NOTICE,
313: "failed command for %s: %s", myname, cmdline);
1.41 tedu 314: errc(1, EPERM, NULL);
1.1 tedu 315: }
316:
317: if (!(rule->options & NOPASS)) {
1.46 tedu 318: char *challenge = NULL, *response, rbuf[1024], cbuf[128];
319: auth_session_t *as;
320:
1.26 espie 321: if (nflag)
322: errx(1, "Authorization required");
1.46 tedu 323:
1.47 sthen 324: if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
1.46 tedu 325: &challenge)))
1.48 tedu 326: errx(1, "Authorization failed");
1.46 tedu 327: if (!challenge) {
328: char host[HOST_NAME_MAX + 1];
329: if (gethostname(host, sizeof(host)))
330: snprintf(host, sizeof(host), "?");
331: snprintf(cbuf, sizeof(cbuf),
1.51 martijn 332: "\rdoas (%.32s@%.32s) password: ", myname, host);
1.46 tedu 333: challenge = cbuf;
334: }
1.50 tedu 335: response = readpassphrase(challenge, rbuf, sizeof(rbuf),
336: RPP_REQUIRE_TTY);
337: if (response == NULL && errno == ENOTTY) {
338: syslog(LOG_AUTHPRIV | LOG_NOTICE,
339: "tty required for %s", myname);
340: errx(1, "a tty is required");
341: }
1.46 tedu 342: if (!auth_userresponse(as, response, 0)) {
1.4 deraadt 343: syslog(LOG_AUTHPRIV | LOG_NOTICE,
1.46 tedu 344: "failed auth for %s", myname);
1.41 tedu 345: errc(1, EPERM, NULL);
1.1 tedu 346: }
1.49 gsoares 347: explicit_bzero(rbuf, sizeof(rbuf));
1.1 tedu 348: }
1.43 deraadt 349:
350: if (pledge("stdio rpath getpw exec id", NULL) == -1)
351: err(1, "pledge");
352:
1.1 tedu 353: pw = getpwuid(target);
354: if (!pw)
355: errx(1, "no passwd entry for target");
1.43 deraadt 356:
1.1 tedu 357: if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
358: LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
359: LOGIN_SETUSER) != 0)
360: errx(1, "failed to set user context for target");
361:
1.43 deraadt 362: if (pledge("stdio rpath exec", NULL) == -1)
363: err(1, "pledge");
364:
1.38 doug 365: if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
366: cwd = "(failed)";
367: else
368: cwd = cwdpath;
1.43 deraadt 369:
370: if (pledge("stdio exec", NULL) == -1)
371: err(1, "pledge");
1.38 doug 372:
373: syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
374: myname, cmdline, pw->pw_name, cwd);
1.44 tedu 375:
1.56 ! tedu 376: env = createenv(envp);
! 377: env = filterenv(env, rule);
! 378: envp = flattenenv(env);
1.38 doug 379:
1.40 tedu 380: if (rule->cmd) {
381: if (setenv("PATH", safepath, 1) == -1)
382: err(1, "failed to set PATH '%s'", safepath);
383: }
1.1 tedu 384: execvpe(cmd, argv, envp);
1.10 tedu 385: if (errno == ENOENT)
386: errx(1, "%s: command not found", cmd);
1.1 tedu 387: err(1, "%s", cmd);
388: }