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>
43 fprintf(stderr, "usage: doas [-Lns] "
44 #ifdef HAVE_BSD_AUTH_H
47 "[-C config] [-u user] command [args]\n");
52 parseuid(const char *s, uid_t *uid)
57 if ((pw = getpwnam(s)) != NULL) {
61 *uid = strtonum(s, 0, UID_MAX, &errstr);
68 uidcheck(const char *s, uid_t desired)
72 if (parseuid(s, &uid) != 0)
80 parsegid(const char *s, gid_t *gid)
85 if ((gr = getgrnam(s)) != NULL) {
89 *gid = strtonum(s, 0, GID_MAX, &errstr);
96 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
97 const char **cmdargs, struct rule *r)
101 if (r->ident[0] == ':') {
103 if (parsegid(r->ident + 1, &rgid) == -1)
105 for (i = 0; i < ngroups; i++) {
106 if (rgid == groups[i])
112 if (uidcheck(r->ident, uid) != 0)
115 if (r->target && uidcheck(r->target, target) != 0)
118 if (strcmp(r->cmd, cmd))
121 /* if arguments were given, they should match explicitly */
122 for (i = 0; r->cmdargs[i]; i++) {
125 if (strcmp(r->cmdargs[i], cmdargs[i]))
136 permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
137 uid_t target, const char *cmd, const char **cmdargs)
142 for (i = 0; i < nrules; i++) {
143 if (match(uid, groups, ngroups, target, cmd,
149 return (*lastr)->action == PERMIT;
153 parseconfig(const char *filename, int checkperms)
156 extern int yyparse(void);
159 yyfp = fopen(filename, "r");
161 err(1, checkperms ? "doas is not enabled, %s" :
162 "could not open config file %s", filename);
165 if (fstat(fileno(yyfp), &sb) != 0)
166 err(1, "fstat(\"%s\")", filename);
167 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
168 errx(1, "%s is writable by group or other", filename);
170 errx(1, "%s is not owned by root", filename);
180 checkconfig(const char *confpath, int argc, char **argv,
181 uid_t uid, gid_t *groups, int ngroups, uid_t target)
183 const struct rule *rule;
185 if (setresuid(uid, uid, uid) != 0)
188 parseconfig(confpath, 0);
192 if (permit(uid, groups, ngroups, &rule, target, argv[0],
193 (const char **)argv + 1)) {
194 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
202 #ifdef HAVE_BSD_AUTH_H
204 authuser(char *myname, char *login_style, int persist)
206 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
211 fd = open("/dev/tty", O_RDWR);
213 if (ioctl(fd, TIOCCHKVERAUTH) == 0)
217 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
219 errx(1, "Authorization failed");
221 char host[HOST_NAME_MAX + 1];
222 if (gethostname(host, sizeof(host)))
223 snprintf(host, sizeof(host), "?");
224 snprintf(cbuf, sizeof(cbuf),
225 "\rdoas (%.32s@%.32s) password: ", myname, host);
228 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
230 if (response == NULL && errno == ENOTTY) {
231 syslog(LOG_AUTHPRIV | LOG_NOTICE,
232 "tty required for %s", myname);
233 errx(1, "a tty is required");
235 if (!auth_userresponse(as, response, 0)) {
236 syslog(LOG_AUTHPRIV | LOG_NOTICE,
237 "failed auth for %s", myname);
238 errx(1, "Authorization failed");
240 explicit_bzero(rbuf, sizeof(rbuf));
244 ioctl(fd, TIOCSETVERAUTH, &secs);
250 authuser(const char *myname, const char *login_style, int persist)
258 #ifdef PERSIST_TIMESTAMP
262 fd = persist_open(&valid, 5 * 60);
263 if (fd != -1 && valid)
269 if (!(pw = getpwnam(myname)))
272 hash = pw->pw_passwd;
273 if (hash[0] == 'x' && hash[1] == '\0') {
275 if (!(sp = getspnam(myname)))
276 errx(1, "Authorization failed");
278 } else if (hash[0] != '*') {
279 errx(1, "Authorization failed");
282 char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
283 if (gethostname(host, sizeof(host)))
284 snprintf(host, sizeof(host), "?");
285 snprintf(cbuf, sizeof(cbuf),
286 "\rdoas (%.32s@%.32s) password: ", myname, host);
289 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
290 if (response == NULL && errno == ENOTTY) {
291 syslog(LOG_AUTHPRIV | LOG_NOTICE,
292 "tty required for %s", myname);
293 errx(1, "a tty is required");
295 if (!(encrypted = crypt(response, hash)))
297 if (strcmp(encrypted, hash) != 0) {
298 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
299 errx(1, "Authorization failed");
301 explicit_bzero(rbuf, sizeof(rbuf));
302 #ifdef PERSIST_TIMESTAMP
305 persist_set(fd, 5 * 60);
310 #endif /* HAVE_BSD_AUTH_H */
313 main(int argc, char **argv)
315 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
316 "/usr/local/bin:/usr/local/sbin";
317 const char *confpath = NULL;
318 char *shargv[] = { NULL, NULL };
321 char cmdline[LINE_MAX];
322 char myname[_PW_NAME_LEN + 1];
324 const struct rule *rule;
327 gid_t groups[NGROUPS_MAX + 1];
332 char cwdpath[PATH_MAX];
335 #ifdef HAVE_BSD_AUTH_H
336 char *login_style = NULL;
341 closefrom(STDERR_FILENO + 1);
345 #ifdef HAVE_BSD_AUTH_H
346 # define OPTSTRING "a:C:Lnsu:v"
348 # define OPTSTRING "+C:Lnsu:v"
351 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
353 #ifdef HAVE_BSD_AUTH_H
355 login_style = optarg;
362 #ifdef TIOCCLRVERAUTH
363 i = open("/dev/tty", O_RDWR);
365 ioctl(i, TIOCCLRVERAUTH);
367 #elif PERSIST_TIMESTAMP
368 exit(persist_clear() != 0);
371 if (parseuid(optarg, &target) != 0)
372 errx(1, "unknown user");
391 } else if ((!sflag && !argc) || (sflag && argc))
396 err(1, "getpwuid failed");
397 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
398 errx(1, "pw_name too long");
399 ngroups = getgroups(NGROUPS_MAX, groups);
401 err(1, "can't get groups");
402 groups[ngroups++] = getgid();
405 sh = getenv("SHELL");
406 if (sh == NULL || *sh == '\0') {
407 shargv[0] = strdup(pw->pw_shell);
408 if (shargv[0] == NULL)
417 checkconfig(confpath, argc, argv, uid, groups, ngroups,
419 exit(1); /* fail safe */
423 errx(1, "not installed setuid");
425 parseconfig("/etc/doas.conf", 1);
427 /* cmdline is used only for logging, no need to abort on truncate */
428 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
429 for (i = 1; i < argc; i++) {
430 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
432 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
437 if (!permit(uid, groups, ngroups, &rule, target, cmd,
438 (const char **)argv + 1)) {
439 syslog(LOG_AUTHPRIV | LOG_NOTICE,
440 "failed command for %s: %s", myname, cmdline);
441 errc(1, EPERM, NULL);
444 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
445 if (!(rule->options & NOPASS)) {
447 errx(1, "Authorization required");
449 authuser(myname, login_style, rule->options & PERSIST);
451 #elif HAVE_PAM_APPL_H
452 pw = getpwuid(target);
454 errx(1, "no passwd entry for target");
456 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
457 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
458 errx(1, "Authorization failed");
461 #error "No authentication method"
462 #endif /* HAVE_BSD_AUTH_H */
464 if (pledge("stdio rpath getpw exec id", NULL) == -1)
467 pw = getpwuid(target);
469 errx(1, "no passwd entry for target");
471 #ifdef HAVE_BSD_AUTH_H
472 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
473 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
475 errx(1, "failed to set user context for target");
477 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
478 errx(1, "setresgid");
479 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
480 errx(1, "initgroups");
481 if (setresuid(target, target, target) != 0)
482 errx(1, "setresuid");
485 if (pledge("stdio rpath exec", NULL) == -1)
488 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
493 if (pledge("stdio exec", NULL) == -1)
496 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
497 myname, cmdline, pw->pw_name, cwd);
499 envp = prepenv(rule);
502 if (setenv("PATH", safepath, 1) == -1)
503 err(1, "failed to set PATH '%s'", safepath);
505 execvpe(cmd, argv, envp);
507 errx(1, "%s: command not found", cmd);