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