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, "doas: version %s built %s\n", VERSION, __DATE__);
50 fprintf(stderr, "usage: doas [-Lnsv] "
51 #ifdef HAVE_BSD_AUTH_H
54 "[-C config] [-u user] command [args]\n");
59 parseuid(const char *s, uid_t *uid)
64 if ((pw = getpwnam(s)) != NULL) {
68 *uid = strtonum(s, 0, UID_MAX, &errstr);
75 uidcheck(const char *s, uid_t desired)
79 if (parseuid(s, &uid) != 0)
87 parsegid(const char *s, gid_t *gid)
92 if ((gr = getgrnam(s)) != NULL) {
96 *gid = strtonum(s, 0, GID_MAX, &errstr);
103 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
104 const char **cmdargs, struct rule *r)
108 if (r->ident[0] == ':') {
110 if (parsegid(r->ident + 1, &rgid) == -1)
112 for (i = 0; i < ngroups; i++) {
113 if (rgid == groups[i])
119 if (uidcheck(r->ident, uid) != 0)
122 if (r->target && uidcheck(r->target, target) != 0)
125 if (strcmp(r->cmd, cmd))
128 /* if arguments were given, they should match explicitly */
129 for (i = 0; r->cmdargs[i]; i++) {
132 if (strcmp(r->cmdargs[i], cmdargs[i]))
143 permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
144 uid_t target, const char *cmd, const char **cmdargs)
149 for (i = 0; i < nrules; i++) {
150 if (match(uid, groups, ngroups, target, cmd,
156 return (*lastr)->action == PERMIT;
160 parseconfig(const char *filename, int checkperms)
163 extern int yyparse(void);
166 yyfp = fopen(filename, "r");
168 err(1, checkperms ? "doas is not enabled, %s" :
169 "could not open config file %s", filename);
172 if (fstat(fileno(yyfp), &sb) != 0)
173 err(1, "fstat(\"%s\")", filename);
174 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
175 errx(1, "%s is writable by group or other", filename);
177 errx(1, "%s is not owned by root", filename);
187 checkconfig(const char *confpath, int argc, char **argv,
188 uid_t uid, gid_t *groups, int ngroups, uid_t target)
190 const struct rule *rule;
192 if (setresuid(uid, uid, uid) != 0)
195 parseconfig(confpath, 0);
199 if (permit(uid, groups, ngroups, &rule, target, argv[0],
200 (const char **)argv + 1)) {
201 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
209 #ifdef HAVE_BSD_AUTH_H
211 authuser(char *myname, char *login_style, int persist)
213 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
218 fd = open("/dev/tty", O_RDWR);
220 if (ioctl(fd, TIOCCHKVERAUTH) == 0)
224 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
226 errx(1, "Authorization failed");
228 char host[HOST_NAME_MAX + 1];
229 if (gethostname(host, sizeof(host)))
230 snprintf(host, sizeof(host), "?");
231 snprintf(cbuf, sizeof(cbuf),
232 "\rdoas (%.32s@%.32s) password: ", myname, host);
235 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
237 if (response == NULL && errno == ENOTTY) {
238 syslog(LOG_AUTHPRIV | LOG_NOTICE,
239 "tty required for %s", myname);
240 errx(1, "a tty is required");
242 if (!auth_userresponse(as, response, 0)) {
243 syslog(LOG_AUTHPRIV | LOG_NOTICE,
244 "failed auth for %s", myname);
245 errx(1, "Authorization failed");
247 explicit_bzero(rbuf, sizeof(rbuf));
251 ioctl(fd, TIOCSETVERAUTH, &secs);
257 authuser(const char *myname, const char *login_style, int persist)
265 #ifdef PERSIST_TIMESTAMP
269 fd = persist_open(&valid, 5 * 60);
270 if (fd != -1 && valid)
276 if (!(pw = getpwnam(myname)))
279 hash = pw->pw_passwd;
280 if (hash[0] == 'x' && hash[1] == '\0') {
282 if (!(sp = getspnam(myname)))
283 errx(1, "Authorization failed");
285 } else if (hash[0] != '*') {
286 errx(1, "Authorization failed");
289 char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
290 if (gethostname(host, sizeof(host)))
291 snprintf(host, sizeof(host), "?");
292 snprintf(cbuf, sizeof(cbuf),
293 "\rdoas (%.32s@%.32s) password: ", myname, host);
296 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
297 if (response == NULL && errno == ENOTTY) {
298 syslog(LOG_AUTHPRIV | LOG_NOTICE,
299 "tty required for %s", myname);
300 errx(1, "a tty is required");
302 if (!(encrypted = crypt(response, hash)))
304 if (strcmp(encrypted, hash) != 0) {
305 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
306 errx(1, "Authorization failed");
308 explicit_bzero(rbuf, sizeof(rbuf));
309 #ifdef PERSIST_TIMESTAMP
312 persist_set(fd, 5 * 60);
317 #endif /* HAVE_BSD_AUTH_H */
320 main(int argc, char **argv)
322 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
323 "/usr/local/bin:/usr/local/sbin";
324 const char *confpath = NULL;
325 char *shargv[] = { NULL, NULL };
328 char cmdline[LINE_MAX];
329 char myname[_PW_NAME_LEN + 1];
331 const struct rule *rule;
334 gid_t groups[NGROUPS_MAX + 1];
340 char cwdpath[PATH_MAX];
343 char *login_style = NULL;
347 closefrom(STDERR_FILENO + 1);
351 #ifdef HAVE_BSD_AUTH_H
352 # define OPTSTRING "a:C:Lnsu:v"
354 # define OPTSTRING "+C:Lnsu:v"
357 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
359 #ifdef HAVE_BSD_AUTH_H
361 login_style = optarg;
368 #ifdef TIOCCLRVERAUTH
369 i = open("/dev/tty", O_RDWR);
371 ioctl(i, TIOCCLRVERAUTH);
373 #elif PERSIST_TIMESTAMP
374 exit(persist_clear() != 0);
377 if (parseuid(optarg, &target) != 0)
378 errx(1, "unknown user");
403 } else if ((!sflag && !argc) || (sflag && argc))
408 err(1, "getpwuid failed");
409 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
410 errx(1, "pw_name too long");
411 ngroups = getgroups(NGROUPS_MAX, groups);
413 err(1, "can't get groups");
414 groups[ngroups++] = getgid();
417 sh = getenv("SHELL");
418 if (sh == NULL || *sh == '\0') {
419 shargv[0] = strdup(pw->pw_shell);
420 if (shargv[0] == NULL)
429 checkconfig(confpath, argc, argv, uid, groups, ngroups,
431 exit(1); /* fail safe */
435 errx(1, "not installed setuid");
437 parseconfig("/etc/doas.conf", 1);
439 /* cmdline is used only for logging, no need to abort on truncate */
440 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
441 for (i = 1; i < argc; i++) {
442 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
444 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
449 if (!permit(uid, groups, ngroups, &rule, target, cmd,
450 (const char **)argv + 1)) {
451 syslog(LOG_AUTHPRIV | LOG_NOTICE,
452 "failed command for %s: %s", myname, cmdline);
453 errc(1, EPERM, NULL);
456 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
457 if (!(rule->options & NOPASS)) {
459 errx(1, "Authorization required");
461 authuser(myname, login_style, rule->options & PERSIST);
463 #elif HAVE_PAM_APPL_H
464 pw = getpwuid(target);
466 errx(1, "no passwd entry for target");
468 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
469 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
470 errx(1, "Authorization failed");
473 #error "No authentication method"
474 #endif /* HAVE_BSD_AUTH_H */
476 if (pledge("stdio rpath getpw exec id", NULL) == -1)
479 pw = getpwuid(target);
481 errx(1, "no passwd entry for target");
483 #ifdef HAVE_BSD_AUTH_H
484 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
485 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
487 errx(1, "failed to set user context for target");
489 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
490 errx(1, "setresgid");
491 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
492 errx(1, "initgroups");
493 if (setresuid(target, target, target) != 0)
494 errx(1, "setresuid");
497 if (pledge("stdio rpath exec", NULL) == -1)
500 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
505 if (pledge("stdio exec", NULL) == -1)
508 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
509 myname, cmdline, pw->pw_name, cwd);
511 envp = prepenv(rule);
514 if (setenv("PATH", safepath, 1) == -1)
515 err(1, "failed to set PATH '%s'", safepath);
517 execvpe(cmd, argv, envp);
519 errx(1, "%s: command not found", cmd);