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] "
50 #ifdef HAVE_BSD_AUTH_H
53 "[-C config] [-u user] command [args]\n");
58 arraylen(const char **arr)
72 parseuid(const char *s, uid_t *uid)
77 if ((pw = getpwnam(s)) != NULL) {
81 *uid = strtonum(s, 0, UID_MAX, &errstr);
88 uidcheck(const char *s, uid_t desired)
92 if (parseuid(s, &uid) != 0)
100 parsegid(const char *s, gid_t *gid)
105 if ((gr = getgrnam(s)) != NULL) {
109 *gid = strtonum(s, 0, GID_MAX, &errstr);
116 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
117 const char **cmdargs, struct rule *r)
121 if (r->ident[0] == ':') {
123 if (parsegid(r->ident + 1, &rgid) == -1)
125 for (i = 0; i < ngroups; i++) {
126 if (rgid == groups[i])
132 if (uidcheck(r->ident, uid) != 0)
135 if (r->target && uidcheck(r->target, target) != 0)
138 if (strcmp(r->cmd, cmd))
141 /* if arguments were given, they should match explicitly */
142 for (i = 0; r->cmdargs[i]; i++) {
145 if (strcmp(r->cmdargs[i], cmdargs[i]))
156 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
157 uid_t target, const char *cmd, const char **cmdargs)
162 for (i = 0; i < nrules; i++) {
163 if (match(uid, groups, ngroups, target, cmd,
169 return (*lastr)->action == PERMIT;
173 parseconfig(const char *filename, int checkperms)
176 extern int yyparse(void);
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)
226 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
229 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
231 errx(1, "Authorization failed");
233 char host[HOST_NAME_MAX + 1];
234 if (gethostname(host, sizeof(host)))
235 snprintf(host, sizeof(host), "?");
236 snprintf(cbuf, sizeof(cbuf),
237 "\rdoas (%.32s@%.32s) password: ", myname, host);
240 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
242 if (response == NULL && errno == ENOTTY) {
243 syslog(LOG_AUTHPRIV | LOG_NOTICE,
244 "tty required for %s", myname);
245 errx(1, "a tty is required");
247 if (!auth_userresponse(as, response, 0)) {
248 syslog(LOG_AUTHPRIV | LOG_NOTICE,
249 "failed auth for %s", myname);
250 errc(1, EPERM, NULL);
252 explicit_bzero(rbuf, sizeof(rbuf));
256 authuser(const char *myname, const char *login_style)
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 if (pledge("stdio rpath getpw tty recvfd proc exec id", NULL) == -1)
332 closefrom(STDERR_FILENO + 1);
336 #ifdef HAVE_BSD_AUTH_H
337 # define OPTSTRING "a:C:nsu:v"
339 # define OPTSTRING "+C:nsu:v"
342 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
344 #ifdef HAVE_BSD_AUTH_H
346 login_style = optarg;
353 if (parseuid(optarg, &target) != 0)
354 errx(1, "unknown user");
379 } else if ((!sflag && !argc) || (sflag && argc))
384 err(1, "getpwuid failed");
385 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
386 errx(1, "pw_name too long");
387 ngroups = getgroups(NGROUPS_MAX, groups);
389 err(1, "can't get groups");
390 groups[ngroups++] = getgid();
393 sh = getenv("SHELL");
394 if (sh == NULL || *sh == '\0') {
395 shargv[0] = strdup(pw->pw_shell);
396 if (shargv[0] == NULL)
405 checkconfig(confpath, argc, argv, uid, groups, ngroups,
407 exit(1); /* fail safe */
410 parseconfig("/etc/doas.conf", 1);
412 /* cmdline is used only for logging, no need to abort on truncate */
413 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
414 for (i = 1; i < argc; i++) {
415 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
417 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
422 if (!permit(uid, groups, ngroups, &rule, target, cmd,
423 (const char **)argv + 1)) {
424 syslog(LOG_AUTHPRIV | LOG_NOTICE,
425 "failed command for %s: %s", myname, cmdline);
426 errc(1, EPERM, NULL);
429 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
430 if (!(rule->options & NOPASS)) {
432 errx(1, "Authorization required");
434 authuser(myname, login_style);
436 #elif HAVE_PAM_APPL_H
437 pw = getpwuid(target);
439 errx(1, "no passwd entry for target");
441 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
442 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
443 errx(1, "Authorization failed");
446 #error "No authentication method"
447 #endif /* HAVE_BSD_AUTH_H */
449 if (pledge("stdio rpath getpw exec id", NULL) == -1)
452 pw = getpwuid(target);
454 errx(1, "no passwd entry for target");
456 #ifdef HAVE_BSD_AUTH_H
457 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
458 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
460 errx(1, "failed to set user context for target");
462 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
463 errx(1, "setresgid");
464 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
465 errx(1, "initgroups");
466 if (setresuid(target, target, target) != 0)
467 errx(1, "setresuid");
470 if (pledge("stdio rpath exec", NULL) == -1)
473 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
478 if (pledge("stdio exec", NULL) == -1)
481 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
482 myname, cmdline, pw->pw_name, cwd);
484 envp = prepenv(rule);
487 if (setenv("PATH", safepath, 1) == -1)
488 err(1, "failed to set PATH '%s'", safepath);
490 execvpe(cmd, argv, envp);
492 errx(1, "%s: command not found", cmd);