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, 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)
165 yyfp = fopen(filename, "r");
167 err(1, checkperms ? "doas is not enabled, %s" :
168 "could not open config file %s", filename);
171 if (fstat(fileno(yyfp), &sb) != 0)
172 err(1, "fstat(\"%s\")", filename);
173 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
174 errx(1, "%s is writable by group or other", filename);
176 errx(1, "%s is not owned by root", filename);
186 checkconfig(const char *confpath, int argc, char **argv,
187 uid_t uid, gid_t *groups, int ngroups, uid_t target)
191 if (setresuid(uid, uid, uid) != 0)
194 parseconfig(confpath, 0);
198 if (permit(uid, groups, ngroups, &rule, target, argv[0],
199 (const char **)argv + 1)) {
200 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
208 #ifdef HAVE_BSD_AUTH_H
210 authuser(char *myname, char *login_style, int persist)
212 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
217 fd = open("/dev/tty", O_RDWR);
219 if (ioctl(fd, TIOCCHKVERAUTH) == 0)
223 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
225 errx(1, "Authorization failed");
227 char host[HOST_NAME_MAX + 1];
228 if (gethostname(host, sizeof(host)))
229 snprintf(host, sizeof(host), "?");
230 snprintf(cbuf, sizeof(cbuf),
231 "\rdoas (%.32s@%.32s) password: ", myname, host);
234 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
236 if (response == NULL && errno == ENOTTY) {
237 syslog(LOG_AUTHPRIV | LOG_NOTICE,
238 "tty required for %s", myname);
239 errx(1, "a tty is required");
241 if (!auth_userresponse(as, response, 0)) {
242 syslog(LOG_AUTHPRIV | LOG_NOTICE,
243 "failed auth for %s", myname);
244 errc(1, EPERM, NULL);
246 explicit_bzero(rbuf, sizeof(rbuf));
250 ioctl(fd, TIOCSETVERAUTH, &secs);
256 authuser(const char *myname, const char *login_style, int persist)
265 if (!(pw = getpwnam(myname)))
268 hash = pw->pw_passwd;
269 if (hash[0] == 'x' && hash[1] == '\0') {
271 if (!(sp = getspnam(myname)))
272 errx(1, "Authorization failed");
274 } else if (hash[0] != '*') {
275 errx(1, "Authorization failed");
278 char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
279 if (gethostname(host, sizeof(host)))
280 snprintf(host, sizeof(host), "?");
281 snprintf(cbuf, sizeof(cbuf),
282 "\rdoas (%.32s@%.32s) password: ", myname, host);
285 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
286 if (response == NULL && errno == ENOTTY) {
287 syslog(LOG_AUTHPRIV | LOG_NOTICE,
288 "tty required for %s", myname);
289 errx(1, "a tty is required");
291 if (!(encrypted = crypt(response, hash)))
293 if (strcmp(encrypted, hash) != 0) {
294 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
295 errx(1, "Authorization failed");
297 explicit_bzero(rbuf, sizeof(rbuf));
299 #endif /* HAVE_BSD_AUTH_H */
302 main(int argc, char **argv)
304 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
305 "/usr/local/bin:/usr/local/sbin";
306 const char *confpath = NULL;
307 char *shargv[] = { NULL, NULL };
310 char cmdline[LINE_MAX];
311 char myname[_PW_NAME_LEN + 1];
316 gid_t groups[NGROUPS_MAX + 1];
322 char cwdpath[PATH_MAX];
325 char *login_style = NULL;
329 closefrom(STDERR_FILENO + 1);
333 #ifdef HAVE_BSD_AUTH_H
334 # define OPTSTRING "a:C:Lnsu:v"
336 # define OPTSTRING "+C:Lnsu:v"
339 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
341 #ifdef HAVE_BSD_AUTH_H
343 login_style = optarg;
350 #ifdef TIOCCLRVERAUTH
351 i = open("/dev/tty", O_RDWR);
353 ioctl(i, TIOCCLRVERAUTH);
357 if (parseuid(optarg, &target) != 0)
358 errx(1, "unknown user");
383 } else if ((!sflag && !argc) || (sflag && argc))
388 err(1, "getpwuid failed");
389 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
390 errx(1, "pw_name too long");
391 ngroups = getgroups(NGROUPS_MAX, groups);
393 err(1, "can't get groups");
394 groups[ngroups++] = getgid();
397 sh = getenv("SHELL");
398 if (sh == NULL || *sh == '\0') {
399 shargv[0] = strdup(pw->pw_shell);
400 if (shargv[0] == NULL)
409 checkconfig(confpath, argc, argv, uid, groups, ngroups,
411 exit(1); /* fail safe */
414 parseconfig("/etc/doas.conf", 1);
416 /* cmdline is used only for logging, no need to abort on truncate */
417 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
418 for (i = 1; i < argc; i++) {
419 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
421 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
426 if (!permit(uid, groups, ngroups, &rule, target, cmd,
427 (const char **)argv + 1)) {
428 syslog(LOG_AUTHPRIV | LOG_NOTICE,
429 "failed command for %s: %s", myname, cmdline);
430 errc(1, EPERM, NULL);
433 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
434 if (!(rule->options & NOPASS)) {
436 errx(1, "Authorization required");
438 authuser(myname, login_style, rule->options & PERSIST);
440 #elif HAVE_PAM_APPL_H
441 pw = getpwuid(target);
443 errx(1, "no passwd entry for target");
445 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
446 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
447 errx(1, "Authorization failed");
450 #error "No authentication method"
451 #endif /* HAVE_BSD_AUTH_H */
453 if (pledge("stdio rpath getpw exec id", NULL) == -1)
456 pw = getpwuid(target);
458 errx(1, "no passwd entry for target");
460 #ifdef HAVE_BSD_AUTH_H
461 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
462 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
464 errx(1, "failed to set user context for target");
466 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
467 errx(1, "setresgid");
468 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
469 errx(1, "initgroups");
470 if (setresuid(target, target, target) != 0)
471 errx(1, "setresuid");
474 if (pledge("stdio rpath exec", NULL) == -1)
477 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
482 if (pledge("stdio exec", NULL) == -1)
485 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
486 myname, cmdline, pw->pw_name, cwd);
488 envp = prepenv(rule);
491 if (setenv("PATH", safepath, 1) == -1)
492 err(1, "failed to set PATH '%s'", safepath);
494 execvpe(cmd, argv, envp);
496 errx(1, "%s: command not found", cmd);