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 arraylen(const char **arr)
73 parseuid(const char *s, uid_t *uid)
78 if ((pw = getpwnam(s)) != NULL) {
82 *uid = strtonum(s, 0, UID_MAX, &errstr);
89 uidcheck(const char *s, uid_t desired)
93 if (parseuid(s, &uid) != 0)
101 parsegid(const char *s, gid_t *gid)
106 if ((gr = getgrnam(s)) != NULL) {
110 *gid = strtonum(s, 0, GID_MAX, &errstr);
117 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
118 const char **cmdargs, struct rule *r)
122 if (r->ident[0] == ':') {
124 if (parsegid(r->ident + 1, &rgid) == -1)
126 for (i = 0; i < ngroups; i++) {
127 if (rgid == groups[i])
133 if (uidcheck(r->ident, uid) != 0)
136 if (r->target && uidcheck(r->target, target) != 0)
139 if (strcmp(r->cmd, cmd))
142 /* if arguments were given, they should match explicitly */
143 for (i = 0; r->cmdargs[i]; i++) {
146 if (strcmp(r->cmdargs[i], cmdargs[i]))
157 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
158 uid_t target, const char *cmd, const char **cmdargs)
163 for (i = 0; i < nrules; i++) {
164 if (match(uid, groups, ngroups, target, cmd,
170 return (*lastr)->action == PERMIT;
174 parseconfig(const char *filename, int checkperms)
179 yyfp = fopen(filename, "r");
181 err(1, checkperms ? "doas is not enabled, %s" :
182 "could not open config file %s", filename);
185 if (fstat(fileno(yyfp), &sb) != 0)
186 err(1, "fstat(\"%s\")", filename);
187 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
188 errx(1, "%s is writable by group or other", filename);
190 errx(1, "%s is not owned by root", filename);
200 checkconfig(const char *confpath, int argc, char **argv,
201 uid_t uid, gid_t *groups, int ngroups, uid_t target)
205 if (setresuid(uid, uid, uid) != 0)
208 parseconfig(confpath, 0);
212 if (permit(uid, groups, ngroups, &rule, target, argv[0],
213 (const char **)argv + 1)) {
214 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
222 #ifdef HAVE_BSD_AUTH_H
224 authuser(char *myname, char *login_style, int persist)
226 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
231 fd = open("/dev/tty", O_RDWR);
233 if (ioctl(fd, TIOCCHKVERAUTH) == 0)
237 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
239 errx(1, "Authorization failed");
241 char host[HOST_NAME_MAX + 1];
242 if (gethostname(host, sizeof(host)))
243 snprintf(host, sizeof(host), "?");
244 snprintf(cbuf, sizeof(cbuf),
245 "\rdoas (%.32s@%.32s) password: ", myname, host);
248 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
250 if (response == NULL && errno == ENOTTY) {
251 syslog(LOG_AUTHPRIV | LOG_NOTICE,
252 "tty required for %s", myname);
253 errx(1, "a tty is required");
255 if (!auth_userresponse(as, response, 0)) {
256 syslog(LOG_AUTHPRIV | LOG_NOTICE,
257 "failed auth for %s", myname);
258 errc(1, EPERM, NULL);
260 explicit_bzero(rbuf, sizeof(rbuf));
264 ioctl(fd, TIOCSETVERAUTH, &secs);
270 authuser(const char *myname, const char *login_style, int persist)
279 if (!(pw = getpwnam(myname)))
282 hash = pw->pw_passwd;
283 if (hash[0] == 'x' && hash[1] == '\0') {
285 if (!(sp = getspnam(myname)))
286 errx(1, "Authorization failed");
288 } else if (hash[0] != '*') {
289 errx(1, "Authorization failed");
292 char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
293 if (gethostname(host, sizeof(host)))
294 snprintf(host, sizeof(host), "?");
295 snprintf(cbuf, sizeof(cbuf),
296 "\rdoas (%.32s@%.32s) password: ", myname, host);
299 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
300 if (response == NULL && errno == ENOTTY) {
301 syslog(LOG_AUTHPRIV | LOG_NOTICE,
302 "tty required for %s", myname);
303 errx(1, "a tty is required");
305 if (!(encrypted = crypt(response, hash)))
307 if (strcmp(encrypted, hash) != 0) {
308 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
309 errx(1, "Authorization failed");
311 explicit_bzero(rbuf, sizeof(rbuf));
313 #endif /* HAVE_BSD_AUTH_H */
316 main(int argc, char **argv)
318 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
319 "/usr/local/bin:/usr/local/sbin";
320 const char *confpath = NULL;
321 char *shargv[] = { NULL, NULL };
324 char cmdline[LINE_MAX];
325 char myname[_PW_NAME_LEN + 1];
330 gid_t groups[NGROUPS_MAX + 1];
336 char cwdpath[PATH_MAX];
339 char *login_style = NULL;
343 closefrom(STDERR_FILENO + 1);
347 #ifdef HAVE_BSD_AUTH_H
348 # define OPTSTRING "a:C:Lnsu:v"
350 # define OPTSTRING "+C:Lnsu:v"
353 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
355 #ifdef HAVE_BSD_AUTH_H
357 login_style = optarg;
364 #ifdef TIOCCLRVERAUTH
365 i = open("/dev/tty", O_RDWR);
367 ioctl(i, TIOCCLRVERAUTH);
371 if (parseuid(optarg, &target) != 0)
372 errx(1, "unknown user");
397 } else if ((!sflag && !argc) || (sflag && argc))
402 err(1, "getpwuid failed");
403 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
404 errx(1, "pw_name too long");
405 ngroups = getgroups(NGROUPS_MAX, groups);
407 err(1, "can't get groups");
408 groups[ngroups++] = getgid();
411 sh = getenv("SHELL");
412 if (sh == NULL || *sh == '\0') {
413 shargv[0] = strdup(pw->pw_shell);
414 if (shargv[0] == NULL)
423 checkconfig(confpath, argc, argv, uid, groups, ngroups,
425 exit(1); /* fail safe */
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", myname, cmdline);
444 errc(1, EPERM, NULL);
447 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
448 if (!(rule->options & NOPASS)) {
450 errx(1, "Authorization required");
452 authuser(myname, login_style, rule->options & PERSIST);
454 #elif HAVE_PAM_APPL_H
455 pw = getpwuid(target);
457 errx(1, "no passwd entry for target");
459 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
460 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
461 errx(1, "Authorization failed");
464 #error "No authentication method"
465 #endif /* HAVE_BSD_AUTH_H */
467 if (pledge("stdio rpath getpw exec id", NULL) == -1)
470 pw = getpwuid(target);
472 errx(1, "no passwd entry for target");
474 #ifdef HAVE_BSD_AUTH_H
475 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
476 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
478 errx(1, "failed to set user context for target");
480 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
481 errx(1, "setresgid");
482 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
483 errx(1, "initgroups");
484 if (setresuid(target, target, target) != 0)
485 errx(1, "setresuid");
488 if (pledge("stdio rpath exec", NULL) == -1)
491 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
496 if (pledge("stdio exec", NULL) == -1)
499 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
500 myname, cmdline, pw->pw_name, cwd);
502 envp = prepenv(rule);
505 if (setenv("PATH", safepath, 1) == -1)
506 err(1, "failed to set PATH '%s'", safepath);
508 execvpe(cmd, argv, envp);
510 errx(1, "%s: command not found", cmd);