Annotation of src/usr.bin/doas/doas.c, Revision 1.61
1.61 ! tedu 1: /* $OpenBSD: doas.c,v 1.60 2016/07/18 16:46:30 zhuk 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.61 ! tedu 206: static void
! 207: authuser(const char *myname, const char *login_style)
! 208: {
! 209: char *challenge = NULL, *response, rbuf[1024], cbuf[128];
! 210: auth_session_t *as;
! 211:
! 212: if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
! 213: &challenge)))
! 214: errx(1, "Authorization failed");
! 215: if (!challenge) {
! 216: char host[HOST_NAME_MAX + 1];
! 217: if (gethostname(host, sizeof(host)))
! 218: snprintf(host, sizeof(host), "?");
! 219: snprintf(cbuf, sizeof(cbuf),
! 220: "\rdoas (%.32s@%.32s) password: ", myname, host);
! 221: challenge = cbuf;
! 222: }
! 223: response = readpassphrase(challenge, rbuf, sizeof(rbuf),
! 224: RPP_REQUIRE_TTY);
! 225: if (response == NULL && errno == ENOTTY) {
! 226: syslog(LOG_AUTHPRIV | LOG_NOTICE,
! 227: "tty required for %s", myname);
! 228: errx(1, "a tty is required");
! 229: }
! 230: if (!auth_userresponse(as, response, 0)) {
! 231: syslog(LOG_AUTHPRIV | LOG_NOTICE,
! 232: "failed auth for %s", myname);
! 233: errc(1, EPERM, NULL);
! 234: }
! 235: explicit_bzero(rbuf, sizeof(rbuf));
! 236: }
! 237:
1.1 tedu 238: int
1.57 martijn 239: main(int argc, char **argv)
1.1 tedu 240: {
1.9 tedu 241: const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
242: "/usr/local/bin:/usr/local/sbin";
1.22 zhuk 243: const char *confpath = NULL;
1.9 tedu 244: char *shargv[] = { NULL, NULL };
245: char *sh;
246: const char *cmd;
1.7 doug 247: char cmdline[LINE_MAX];
248: char myname[_PW_NAME_LEN + 1];
1.9 tedu 249: struct passwd *pw;
250: struct rule *rule;
251: uid_t uid;
252: uid_t target = 0;
1.1 tedu 253: gid_t groups[NGROUPS_MAX + 1];
254: int ngroups;
255: int i, ch;
1.8 nicm 256: int sflag = 0;
1.26 espie 257: int nflag = 0;
1.38 doug 258: char cwdpath[PATH_MAX];
259: const char *cwd;
1.47 sthen 260: char *login_style = NULL;
1.57 martijn 261: char **envp;
1.52 tedu 262:
263: setprogname("doas");
1.42 tedu 264:
1.59 semarie 265: if (pledge("stdio rpath getpw tty recvfd proc exec id", NULL) == -1)
1.43 deraadt 266: err(1, "pledge");
267:
1.42 tedu 268: closefrom(STDERR_FILENO + 1);
1.1 tedu 269:
1.26 espie 270: uid = getuid();
1.34 tedu 271:
1.47 sthen 272: while ((ch = getopt(argc, argv, "a:C:nsu:")) != -1) {
1.1 tedu 273: switch (ch) {
1.47 sthen 274: case 'a':
275: login_style = optarg;
276: break;
1.16 tedu 277: case 'C':
1.22 zhuk 278: confpath = optarg;
279: break;
1.1 tedu 280: case 'u':
281: if (parseuid(optarg, &target) != 0)
282: errx(1, "unknown user");
283: break;
1.26 espie 284: case 'n':
285: nflag = 1;
286: break;
1.8 nicm 287: case 's':
288: sflag = 1;
289: break;
1.1 tedu 290: default:
291: usage();
292: break;
293: }
294: }
295: argv += optind;
296: argc -= optind;
297:
1.22 zhuk 298: if (confpath) {
299: if (sflag)
300: usage();
301: } else if ((!sflag && !argc) || (sflag && argc))
1.1 tedu 302: usage();
1.16 tedu 303:
1.1 tedu 304: pw = getpwuid(uid);
305: if (!pw)
306: err(1, "getpwuid failed");
1.7 doug 307: if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
308: errx(1, "pw_name too long");
1.1 tedu 309: ngroups = getgroups(NGROUPS_MAX, groups);
310: if (ngroups == -1)
311: err(1, "can't get groups");
312: groups[ngroups++] = getgid();
1.8 nicm 313:
314: if (sflag) {
315: sh = getenv("SHELL");
1.60 zhuk 316: if (sh == NULL || *sh == '\0') {
317: shargv[0] = strdup(pw->pw_shell);
318: if (shargv[0] == NULL)
319: err(1, NULL);
320: } else
1.8 nicm 321: shargv[0] = sh;
322: argv = shargv;
323: argc = 1;
324: }
1.22 zhuk 325:
1.24 tedu 326: if (confpath) {
327: checkconfig(confpath, argc, argv, uid, groups, ngroups,
328: target);
329: exit(1); /* fail safe */
330: }
331:
1.22 zhuk 332: parseconfig("/etc/doas.conf", 1);
1.8 nicm 333:
1.23 zhuk 334: /* cmdline is used only for logging, no need to abort on truncate */
1.58 tedu 335: (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
1.8 nicm 336: for (i = 1; i < argc; i++) {
337: if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
1.23 zhuk 338: break;
1.8 nicm 339: if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
1.23 zhuk 340: break;
1.8 nicm 341: }
1.1 tedu 342:
1.23 zhuk 343: cmd = argv[0];
1.15 zhuk 344: if (!permit(uid, groups, ngroups, &rule, target, cmd,
1.58 tedu 345: (const char **)argv + 1)) {
1.4 deraadt 346: syslog(LOG_AUTHPRIV | LOG_NOTICE,
347: "failed command for %s: %s", myname, cmdline);
1.41 tedu 348: errc(1, EPERM, NULL);
1.1 tedu 349: }
350:
351: if (!(rule->options & NOPASS)) {
1.26 espie 352: if (nflag)
353: errx(1, "Authorization required");
1.46 tedu 354:
1.61 ! tedu 355: authuser(myname, login_style);
1.1 tedu 356: }
1.43 deraadt 357:
358: if (pledge("stdio rpath getpw exec id", NULL) == -1)
359: err(1, "pledge");
360:
1.1 tedu 361: pw = getpwuid(target);
362: if (!pw)
363: errx(1, "no passwd entry for target");
1.43 deraadt 364:
1.1 tedu 365: if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
366: LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
367: LOGIN_SETUSER) != 0)
368: errx(1, "failed to set user context for target");
369:
1.43 deraadt 370: if (pledge("stdio rpath exec", NULL) == -1)
371: err(1, "pledge");
372:
1.38 doug 373: if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
374: cwd = "(failed)";
375: else
376: cwd = cwdpath;
1.43 deraadt 377:
378: if (pledge("stdio exec", NULL) == -1)
379: err(1, "pledge");
1.38 doug 380:
381: syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
382: myname, cmdline, pw->pw_name, cwd);
1.44 tedu 383:
1.57 martijn 384: envp = prepenv(rule);
1.38 doug 385:
1.40 tedu 386: if (rule->cmd) {
387: if (setenv("PATH", safepath, 1) == -1)
388: err(1, "failed to set PATH '%s'", safepath);
389: }
1.1 tedu 390: execvpe(cmd, argv, envp);
1.10 tedu 391: if (errno == ENOENT)
392: errx(1, "%s: command not found", cmd);
1.1 tedu 393: err(1, "%s", cmd);
394: }