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>
42 fprintf(stderr, "doas: version %s built %s\n", VERSION, __DATE__);
49 fprintf(stderr, "usage: doas [-nsv] [-a style] [-C config] [-u user]"
55 arraylen(const char **arr)
69 parseuid(const char *s, uid_t *uid)
74 if ((pw = getpwnam(s)) != NULL) {
78 *uid = strtonum(s, 0, UID_MAX, &errstr);
85 uidcheck(const char *s, uid_t desired)
89 if (parseuid(s, &uid) != 0)
97 parsegid(const char *s, gid_t *gid)
102 if ((gr = getgrnam(s)) != NULL) {
106 *gid = strtonum(s, 0, GID_MAX, &errstr);
113 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
114 const char **cmdargs, struct rule *r)
118 if (r->ident[0] == ':') {
120 if (parsegid(r->ident + 1, &rgid) == -1)
122 for (i = 0; i < ngroups; i++) {
123 if (rgid == groups[i])
129 if (uidcheck(r->ident, uid) != 0)
132 if (r->target && uidcheck(r->target, target) != 0)
135 if (strcmp(r->cmd, cmd))
138 /* if arguments were given, they should match explicitly */
139 for (i = 0; r->cmdargs[i]; i++) {
142 if (strcmp(r->cmdargs[i], cmdargs[i]))
153 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
154 uid_t target, const char *cmd, const char **cmdargs)
159 for (i = 0; i < nrules; i++) {
160 if (match(uid, groups, ngroups, target, cmd,
166 return (*lastr)->action == PERMIT;
170 parseconfig(const char *filename, int checkperms)
173 extern int yyparse(void);
176 yyfp = fopen(filename, "r");
178 err(1, checkperms ? "doas is not enabled, %s" :
179 "could not open config file %s", filename);
182 if (fstat(fileno(yyfp), &sb) != 0)
183 err(1, "fstat(\"%s\")", filename);
184 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
185 errx(1, "%s is writable by group or other", filename);
187 errx(1, "%s is not owned by root", filename);
197 checkconfig(const char *confpath, int argc, char **argv,
198 uid_t uid, gid_t *groups, int ngroups, uid_t target)
202 if (setresuid(uid, uid, uid) != 0)
205 parseconfig(confpath, 0);
209 if (permit(uid, groups, ngroups, &rule, target, argv[0],
210 (const char **)argv + 1)) {
211 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
220 main(int argc, char **argv)
222 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
223 "/usr/local/bin:/usr/local/sbin";
224 const char *confpath = NULL;
225 char *shargv[] = { NULL, NULL };
228 char cmdline[LINE_MAX];
229 char myname[_PW_NAME_LEN + 1];
234 gid_t groups[NGROUPS_MAX + 1];
240 char cwdpath[PATH_MAX];
243 #ifdef HAVE_BSD_AUTH_H
244 char *login_style = NULL;
249 if (pledge("stdio rpath getpw tty recvfd proc exec id", NULL) == -1)
252 /* closefrom(STDERR_FILENO + 1); */
256 #ifdef HAVE_BSD_AUTH_H
257 # define OPTSTRING "a:C:nsu:v"
259 # define OPTSTRING "+C:nsu:v"
262 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
264 #ifdef HAVE_BSD_AUTH_H
266 login_style = optarg;
273 if (parseuid(optarg, &target) != 0)
274 errx(1, "unknown user");
299 } else if ((!sflag && !argc) || (sflag && argc))
304 err(1, "getpwuid failed");
305 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
306 errx(1, "pw_name too long");
307 ngroups = getgroups(NGROUPS_MAX, groups);
309 err(1, "can't get groups");
310 groups[ngroups++] = getgid();
313 sh = getenv("SHELL");
314 if (sh == NULL || *sh == '\0') {
315 shargv[0] = strdup(pw->pw_shell);
316 if (shargv[0] == NULL)
325 checkconfig(confpath, argc, argv, uid, groups, ngroups,
327 exit(1); /* fail safe */
330 parseconfig("/etc/doas.conf", 1);
332 /* cmdline is used only for logging, no need to abort on truncate */
333 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
334 for (i = 1; i < argc; i++) {
335 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
337 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
342 if (!permit(uid, groups, ngroups, &rule, target, cmd,
343 (const char **)argv + 1)) {
344 syslog(LOG_AUTHPRIV | LOG_NOTICE,
345 "failed command for %s: %s", myname, cmdline);
346 errc(1, EPERM, NULL);
349 #ifdef HAVE_BSD_AUTH_H
350 if (!(rule->options & NOPASS)) {
352 errx(1, "Authorization required");
354 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
357 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
359 errx(1, "Authorization failed");
361 char host[HOST_NAME_MAX + 1];
362 if (gethostname(host, sizeof(host)))
363 snprintf(host, sizeof(host), "?");
364 snprintf(cbuf, sizeof(cbuf),
365 "\rdoas (%.32s@%.32s) password: ", myname, host);
368 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
370 if (response == NULL && errno == ENOTTY) {
371 syslog(LOG_AUTHPRIV | LOG_NOTICE,
372 "tty required for %s", myname);
373 errx(1, "a tty is required");
375 if (!auth_userresponse(as, response, 0)) {
376 syslog(LOG_AUTHPRIV | LOG_NOTICE,
377 "failed auth for %s", myname);
378 errc(1, EPERM, NULL);
380 explicit_bzero(rbuf, sizeof(rbuf));
382 #elif HAVE_PAM_APPL_H
383 pw = getpwuid(target);
385 errx(1, "no passwd entry for target");
387 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
388 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
389 errc(1, EPERM, NULL);
394 if (!(rule->options & NOPASS)) {
396 errx(1, "Authorization required");
398 pass = pw->pw_passwd;
399 if (pass[0] == 'x' && pass[1] == '\0') {
401 if (!(sp = getspnam(myname)))
402 errx(1, "Authorization failed");
406 char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
407 if (gethostname(host, sizeof(host)))
408 snprintf(host, sizeof(host), "?");
409 snprintf(cbuf, sizeof(cbuf),
410 "\rdoas (%.32s@%.32s) password: ", myname, host);
413 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
414 if (response == NULL && errno == ENOTTY) {
415 syslog(LOG_AUTHPRIV | LOG_NOTICE,
416 "tty required for %s", myname);
417 errx(1, "a tty is required");
419 if (strcmp(crypt(response, pass), pass) != 0) {
420 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
421 errc(1, EPERM, NULL);
423 explicit_bzero(rbuf, sizeof(rbuf));
426 if (!(rule->options & NOPASS))
427 errx(1, "Authorization required");
428 #endif /* HAVE_BSD_AUTH_H */
430 if (pledge("stdio rpath getpw exec id", NULL) == -1)
433 pw = getpwuid(target);
435 errx(1, "no passwd entry for target");
437 #ifdef HAVE_BSD_AUTH_H
438 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
439 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
441 errx(1, "failed to set user context for target");
443 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
444 errx(1, "setresgid");
445 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
446 errx(1, "initgroups");
447 if (setresuid(target, target, target) != 0)
448 errx(1, "setresuid");
451 if (pledge("stdio rpath exec", NULL) == -1)
454 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
459 if (pledge("stdio exec", NULL) == -1)
462 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
463 myname, cmdline, pw->pw_name, cwd);
465 envp = prepenv(rule);
468 if (setenv("PATH", safepath, 1) == -1)
469 err(1, "failed to set PATH '%s'", safepath);
471 execvpe(cmd, argv, envp);
473 errx(1, "%s: command not found", cmd);