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 #ifdef HAVE_BSD_AUTH_H
344 char *login_style = NULL;
349 closefrom(STDERR_FILENO + 1);
353 #ifdef HAVE_BSD_AUTH_H
354 # define OPTSTRING "a:C:Lnsu:v"
356 # define OPTSTRING "+C:Lnsu:v"
359 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
361 #ifdef HAVE_BSD_AUTH_H
363 login_style = optarg;
370 #ifdef TIOCCLRVERAUTH
371 i = open("/dev/tty", O_RDWR);
373 ioctl(i, TIOCCLRVERAUTH);
375 #elif PERSIST_TIMESTAMP
376 exit(persist_clear() != 0);
379 if (parseuid(optarg, &target) != 0)
380 errx(1, "unknown user");
405 } else if ((!sflag && !argc) || (sflag && argc))
410 err(1, "getpwuid failed");
411 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
412 errx(1, "pw_name too long");
413 ngroups = getgroups(NGROUPS_MAX, groups);
415 err(1, "can't get groups");
416 groups[ngroups++] = getgid();
419 sh = getenv("SHELL");
420 if (sh == NULL || *sh == '\0') {
421 shargv[0] = strdup(pw->pw_shell);
422 if (shargv[0] == NULL)
431 checkconfig(confpath, argc, argv, uid, groups, ngroups,
433 exit(1); /* fail safe */
437 errx(1, "not installed setuid");
439 parseconfig("/etc/doas.conf", 1);
441 /* cmdline is used only for logging, no need to abort on truncate */
442 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
443 for (i = 1; i < argc; i++) {
444 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
446 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
451 if (!permit(uid, groups, ngroups, &rule, target, cmd,
452 (const char **)argv + 1)) {
453 syslog(LOG_AUTHPRIV | LOG_NOTICE,
454 "failed command for %s: %s", myname, cmdline);
455 errc(1, EPERM, NULL);
458 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
459 if (!(rule->options & NOPASS)) {
461 errx(1, "Authorization required");
463 authuser(myname, login_style, rule->options & PERSIST);
465 #elif HAVE_PAM_APPL_H
466 pw = getpwuid(target);
468 errx(1, "no passwd entry for target");
470 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
471 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
472 errx(1, "Authorization failed");
475 #error "No authentication method"
476 #endif /* HAVE_BSD_AUTH_H */
478 if (pledge("stdio rpath getpw exec id", NULL) == -1)
481 pw = getpwuid(target);
483 errx(1, "no passwd entry for target");
485 #ifdef HAVE_BSD_AUTH_H
486 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
487 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
489 errx(1, "failed to set user context for target");
491 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
492 errx(1, "setresgid");
493 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
494 errx(1, "initgroups");
495 if (setresuid(target, target, target) != 0)
496 errx(1, "setresuid");
499 if (pledge("stdio rpath exec", NULL) == -1)
502 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
507 if (pledge("stdio exec", NULL) == -1)
510 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
511 myname, cmdline, pw->pw_name, cwd);
513 envp = prepenv(rule);
516 if (setenv("PATH", safepath, 1) == -1)
517 err(1, "failed to set PATH '%s'", safepath);
519 execvpe(cmd, argv, envp);
521 errx(1, "%s: command not found", cmd);