Annotation of src/usr.bin/doas/doas.c, Revision 1.60
1.60 ! zhuk 1: /* $OpenBSD: doas.c,v 1.59 2016/07/12 12:10:42 semarie 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
1.57 martijn 207: main(int argc, char **argv)
1.1 tedu 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.7 doug 215: char cmdline[LINE_MAX];
216: char myname[_PW_NAME_LEN + 1];
1.9 tedu 217: struct passwd *pw;
218: struct rule *rule;
219: uid_t uid;
220: uid_t target = 0;
1.1 tedu 221: gid_t groups[NGROUPS_MAX + 1];
222: int ngroups;
223: int i, ch;
1.8 nicm 224: int sflag = 0;
1.26 espie 225: int nflag = 0;
1.38 doug 226: char cwdpath[PATH_MAX];
227: const char *cwd;
1.47 sthen 228: char *login_style = NULL;
1.57 martijn 229: char **envp;
1.52 tedu 230:
231: setprogname("doas");
1.42 tedu 232:
1.59 semarie 233: if (pledge("stdio rpath getpw tty recvfd 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");
1.60 ! zhuk 284: if (sh == NULL || *sh == '\0') {
! 285: shargv[0] = strdup(pw->pw_shell);
! 286: if (shargv[0] == NULL)
! 287: err(1, NULL);
! 288: } else
1.8 nicm 289: shargv[0] = sh;
290: argv = shargv;
291: argc = 1;
292: }
1.22 zhuk 293:
1.24 tedu 294: if (confpath) {
295: checkconfig(confpath, argc, argv, uid, groups, ngroups,
296: target);
297: exit(1); /* fail safe */
298: }
299:
1.22 zhuk 300: parseconfig("/etc/doas.conf", 1);
1.8 nicm 301:
1.23 zhuk 302: /* cmdline is used only for logging, no need to abort on truncate */
1.58 tedu 303: (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
1.8 nicm 304: for (i = 1; i < argc; i++) {
305: if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
1.23 zhuk 306: break;
1.8 nicm 307: if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
1.23 zhuk 308: break;
1.8 nicm 309: }
1.1 tedu 310:
1.23 zhuk 311: cmd = argv[0];
1.15 zhuk 312: if (!permit(uid, groups, ngroups, &rule, target, cmd,
1.58 tedu 313: (const char **)argv + 1)) {
1.4 deraadt 314: syslog(LOG_AUTHPRIV | LOG_NOTICE,
315: "failed command for %s: %s", myname, cmdline);
1.41 tedu 316: errc(1, EPERM, NULL);
1.1 tedu 317: }
318:
319: if (!(rule->options & NOPASS)) {
1.46 tedu 320: char *challenge = NULL, *response, rbuf[1024], cbuf[128];
321: auth_session_t *as;
322:
1.26 espie 323: if (nflag)
324: errx(1, "Authorization required");
1.46 tedu 325:
1.47 sthen 326: if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
1.46 tedu 327: &challenge)))
1.48 tedu 328: errx(1, "Authorization failed");
1.46 tedu 329: if (!challenge) {
330: char host[HOST_NAME_MAX + 1];
331: if (gethostname(host, sizeof(host)))
332: snprintf(host, sizeof(host), "?");
333: snprintf(cbuf, sizeof(cbuf),
1.51 martijn 334: "\rdoas (%.32s@%.32s) password: ", myname, host);
1.46 tedu 335: challenge = cbuf;
336: }
1.50 tedu 337: response = readpassphrase(challenge, rbuf, sizeof(rbuf),
338: RPP_REQUIRE_TTY);
339: if (response == NULL && errno == ENOTTY) {
340: syslog(LOG_AUTHPRIV | LOG_NOTICE,
341: "tty required for %s", myname);
342: errx(1, "a tty is required");
343: }
1.46 tedu 344: if (!auth_userresponse(as, response, 0)) {
1.4 deraadt 345: syslog(LOG_AUTHPRIV | LOG_NOTICE,
1.46 tedu 346: "failed auth for %s", myname);
1.41 tedu 347: errc(1, EPERM, NULL);
1.1 tedu 348: }
1.49 gsoares 349: explicit_bzero(rbuf, sizeof(rbuf));
1.1 tedu 350: }
1.43 deraadt 351:
352: if (pledge("stdio rpath getpw exec id", NULL) == -1)
353: err(1, "pledge");
354:
1.1 tedu 355: pw = getpwuid(target);
356: if (!pw)
357: errx(1, "no passwd entry for target");
1.43 deraadt 358:
1.1 tedu 359: if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
360: LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
361: LOGIN_SETUSER) != 0)
362: errx(1, "failed to set user context for target");
363:
1.43 deraadt 364: if (pledge("stdio rpath exec", NULL) == -1)
365: err(1, "pledge");
366:
1.38 doug 367: if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
368: cwd = "(failed)";
369: else
370: cwd = cwdpath;
1.43 deraadt 371:
372: if (pledge("stdio exec", NULL) == -1)
373: err(1, "pledge");
1.38 doug 374:
375: syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
376: myname, cmdline, pw->pw_name, cwd);
1.44 tedu 377:
1.57 martijn 378: envp = prepenv(rule);
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: }