1 /* $OpenBSD: doas.c,v 1.52 2016/04/28 04:48:56 tedu Exp $ */
3 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
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.
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.
18 #include <sys/types.h>
20 #include <sys/ioctl.h>
24 # include <login_cap.h>
25 # include <readpassphrase.h>
44 fprintf(stderr, "usage: doas [-Lns] "
48 "[-C config] [-u user] command [args]\n");
53 parseuid(const char *s, uid_t *uid)
58 if ((pw = getpwnam(s)) != NULL) {
64 *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
71 uidcheck(const char *s, uid_t desired)
75 if (parseuid(s, &uid) != 0)
83 parsegid(const char *s, gid_t *gid)
88 if ((gr = getgrnam(s)) != NULL) {
94 *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
101 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
102 const char **cmdargs, struct rule *r)
106 if (r->ident[0] == ':') {
108 if (parsegid(r->ident + 1, &rgid) == -1)
110 for (i = 0; i < ngroups; i++) {
111 if (rgid == groups[i])
117 if (uidcheck(r->ident, uid) != 0)
120 if (r->target && uidcheck(r->target, target) != 0)
123 if (strcmp(r->cmd, cmd))
126 /* if arguments were given, they should match explicitly */
127 for (i = 0; r->cmdargs[i]; i++) {
130 if (strcmp(r->cmdargs[i], cmdargs[i]))
141 permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
142 uid_t target, const char *cmd, const char **cmdargs)
147 for (i = 0; i < nrules; i++) {
148 if (match(uid, groups, ngroups, target, cmd,
154 return (*lastr)->action == PERMIT;
158 parseconfig(const char *filename, int checkperms)
161 extern int yyparse(void);
164 yyfp = fopen(filename, "r");
166 err(1, checkperms ? "doas is not enabled, %s" :
167 "could not open config file %s", filename);
170 if (fstat(fileno(yyfp), &sb) != 0)
171 err(1, "fstat(\"%s\")", filename);
172 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
173 errx(1, "%s is writable by group or other", filename);
175 errx(1, "%s is not owned by root", filename);
185 checkconfig(const char *confpath, int argc, char **argv,
186 uid_t uid, gid_t *groups, int ngroups, uid_t target)
188 const struct rule *rule;
190 if (setresuid(uid, uid, uid) != 0)
193 parseconfig(confpath, 0);
197 if (permit(uid, groups, ngroups, &rule, target, argv[0],
198 (const char **)argv + 1)) {
199 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
209 authuser(char *myname, char *login_style, int persist)
211 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
216 fd = open("/dev/tty", O_RDWR);
218 if (ioctl(fd, TIOCCHKVERAUTH) == 0)
222 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
224 errx(1, "Authorization failed");
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);
233 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
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");
240 if (!auth_userresponse(as, response, 0)) {
241 explicit_bzero(rbuf, sizeof(rbuf));
242 syslog(LOG_AUTHPRIV | LOG_NOTICE,
243 "failed auth for %s", myname);
244 errx(1, "Authorization failed");
246 explicit_bzero(rbuf, sizeof(rbuf));
250 ioctl(fd, TIOCSETVERAUTH, &secs);
258 unveilcommands(const char *ipath, const char *cmd)
260 char *path = NULL, *p;
263 if (strchr(cmd, '/') != NULL) {
264 if (unveil(cmd, "x") != -1)
273 path = strdup(ipath);
278 for (p = path; p && *p; ) {
280 char *cp = strsep(&p, ":");
283 int r = snprintf(buf, sizeof buf, "%s/%s", cp, cmd);
284 if (r >= 0 && r < sizeof buf) {
285 if (unveil(buf, "x") != -1)
297 main(int argc, char **argv)
299 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
300 "/usr/local/bin:/usr/local/sbin";
301 const char *confpath = NULL;
302 char *shargv[] = { NULL, NULL };
306 char cmdline[LINE_MAX];
308 char mypwbuf[_PW_BUF_LEN], targpwbuf[_PW_BUF_LEN];
310 char *mypwbuf = NULL, *targpwbuf = NULL;
312 struct passwd mypwstore, targpwstore;
313 struct passwd *mypw, *targpw;
314 const struct rule *rule;
317 gid_t groups[NGROUPS_MAX + 1];
322 char cwdpath[PATH_MAX];
326 char *login_style = NULL;
331 closefrom(STDERR_FILENO + 1);
336 # define OPTSTRING "a:C:Lnsu:"
338 # define OPTSTRING "+C:Lnsu:"
341 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
345 login_style = optarg;
352 #if defined(USE_BSD_AUTH)
353 i = open("/dev/tty", O_RDWR);
355 ioctl(i, TIOCCLRVERAUTH);
357 #elif defined(USE_TIMESTAMP)
358 exit(timestamp_clear() == -1);
363 if (parseuid(optarg, &target) != 0)
364 errx(1, "unknown user");
383 } else if ((!sflag && !argc) || (sflag && argc))
387 rv = getpwuid_r(uid, &mypwstore, mypwbuf, sizeof(mypwbuf), &mypw);
389 err(1, "getpwuid_r failed");
391 for (size_t sz = 1024; sz <= 16*1024; sz *= 2) {
392 mypwbuf = reallocarray(mypwbuf, sz, sizeof (char));
394 errx(1, "can't allocate mypwbuf");
395 rv = getpwuid_r(uid, &mypwstore, mypwbuf, sz, &mypw);
400 err(1, "getpwuid_r failed");
403 errx(1, "no passwd entry for self");
404 ngroups = getgroups(NGROUPS_MAX, groups);
406 err(1, "can't get groups");
407 groups[ngroups++] = getgid();
410 sh = getenv("SHELL");
411 if (sh == NULL || *sh == '\0') {
412 shargv[0] = mypw->pw_shell;
420 checkconfig(confpath, argc, argv, uid, groups, ngroups,
422 exit(1); /* fail safe */
426 errx(1, "not installed setuid");
428 parseconfig("/etc/doas.conf", 1);
430 /* cmdline is used only for logging, no need to abort on truncate */
431 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
432 for (i = 1; i < argc; i++) {
433 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
435 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
440 if (!permit(uid, groups, ngroups, &rule, target, cmd,
441 (const char **)argv + 1)) {
442 syslog(LOG_AUTHPRIV | LOG_NOTICE,
443 "failed command for %s: %s", mypw->pw_name, cmdline);
444 errc(1, EPERM, NULL);
447 #if defined(__OpenBSD__) || defined(USE_SHADOW)
448 if (!(rule->options & NOPASS)) {
450 errx(1, "Authorization required");
453 authuser(mypw->pw_name, login_style, rule->options & PERSIST);
455 shadowauth(mypw->pw_name, rule->options & PERSIST);
459 if ((p = getenv("PATH")) != NULL)
460 formerpath = strdup(p);
461 if (formerpath == NULL)
465 if (unveil(_PATH_LOGIN_CONF, "r") == -1 ||
466 unveil(_PATH_LOGIN_CONF ".db", "r") == -1)
470 if (setenv("PATH", safepath, 1) == -1)
471 err(1, "failed to set PATH '%s'", safepath);
474 if (unveilcommands(getenv("PATH"), cmd) == 0)
477 if (pledge("stdio rpath getpw exec id", NULL) == -1)
481 #elif !defined(USE_PAM)
483 if (!(rule->options & NOPASS)) {
484 errx(1, "Authorization required");
486 #endif /* !(__OpenBSD__ || USE_SHADOW) && !USE_PAM */
489 rv = getpwuid_r(target, &targpwstore, targpwbuf, sizeof(targpwbuf), &targpw);
491 errx(1, "no passwd entry for target");
493 for (size_t sz = 1024; sz <= 16*1024; sz *= 2) {
494 targpwbuf = reallocarray(targpwbuf, sz, sizeof (char));
495 if (targpwbuf == NULL)
496 errx(1, "can't allocate targpwbuf");
497 rv = getpwuid_r(target, &targpwstore, targpwbuf, sz, &targpw);
502 err(1, "getpwuid_r failed");
505 err(1, "getpwuid_r failed");
508 pamauth(targpw->pw_name, mypw->pw_name, !nflag, rule->options & NOPASS,
509 rule->options & PERSIST);
512 #ifdef HAVE_SETUSERCONTEXT
513 if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
515 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
517 errx(1, "failed to set user context for target");
519 if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
521 if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
522 err(1, "initgroups");
523 if (setresuid(target, target, target) != 0)
528 if (pledge("stdio rpath exec", NULL) == -1)
532 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
538 if (pledge("stdio exec", NULL) == -1)
542 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
543 mypw->pw_name, cmdline, targpw->pw_name, cwd);
545 envp = prepenv(rule, mypw, targpw);
547 /* setusercontext set path for the next process, so reset it for us */
549 if (setenv("PATH", safepath, 1) == -1)
550 err(1, "failed to set PATH '%s'", safepath);
552 if (setenv("PATH", formerpath, 1) == -1)
553 err(1, "failed to set PATH '%s'", formerpath);
555 execvpe(cmd, argv, envp);
557 errx(1, "%s: command not found", cmd);