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 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] = pw->pw_shell;
323 checkconfig(confpath, argc, argv, uid, groups, ngroups,
325 exit(1); /* fail safe */
328 parseconfig("/etc/doas.conf", 1);
330 /* cmdline is used only for logging, no need to abort on truncate */
331 (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
332 for (i = 1; i < argc; i++) {
333 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
335 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
340 if (!permit(uid, groups, ngroups, &rule, target, cmd,
341 (const char**)argv + 1)) {
342 syslog(LOG_AUTHPRIV | LOG_NOTICE,
343 "failed command for %s: %s", myname, cmdline);
344 errc(1, EPERM, NULL);
347 #ifdef HAVE_BSD_AUTH_H
348 if (!(rule->options & NOPASS)) {
350 errx(1, "Authorization required");
352 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
355 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
357 errx(1, "Authorization failed");
359 char host[HOST_NAME_MAX + 1];
360 if (gethostname(host, sizeof(host)))
361 snprintf(host, sizeof(host), "?");
362 snprintf(cbuf, sizeof(cbuf),
363 "\rdoas (%.32s@%.32s) password: ", myname, host);
366 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
368 if (response == NULL && errno == ENOTTY) {
369 syslog(LOG_AUTHPRIV | LOG_NOTICE,
370 "tty required for %s", myname);
371 errx(1, "a tty is required");
373 if (!auth_userresponse(as, response, 0)) {
374 syslog(LOG_AUTHPRIV | LOG_NOTICE,
375 "failed auth for %s", myname);
376 errc(1, EPERM, NULL);
378 explicit_bzero(rbuf, sizeof(rbuf));
380 #elif HAVE_PAM_APPL_H
381 pw = getpwuid(target);
383 errx(1, "no passwd entry for target");
385 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
386 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
387 errc(1, EPERM, NULL);
392 if (!(rule->options & NOPASS)) {
394 errx(1, "Authorization required");
396 pass = pw->pw_passwd;
397 if (pass[0] == 'x' && pass[1] == '\0') {
399 if (!(sp = getspnam(myname)))
400 errx(1, "Authorization failed");
404 char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
405 if (gethostname(host, sizeof(host)))
406 snprintf(host, sizeof(host), "?");
407 snprintf(cbuf, sizeof(cbuf),
408 "\rdoas (%.32s@%.32s) password: ", myname, host);
411 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
412 if (response == NULL && errno == ENOTTY) {
413 syslog(LOG_AUTHPRIV | LOG_NOTICE,
414 "tty required for %s", myname);
415 errx(1, "a tty is required");
417 if (strcmp(crypt(response, pass), pass) != 0) {
418 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
419 errc(1, EPERM, NULL);
421 explicit_bzero(rbuf, sizeof(rbuf));
424 if (!(rule->options & NOPASS))
425 errx(1, "Authorization required");
426 #endif /* HAVE_BSD_AUTH_H */
428 if (pledge("stdio rpath getpw exec id", NULL) == -1)
431 pw = getpwuid(target);
433 errx(1, "no passwd entry for target");
435 #ifdef HAVE_BSD_AUTH_H
436 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
437 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
439 errx(1, "failed to set user context for target");
442 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
443 errx(1, "setresgid");
444 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
445 errx(1, "initgroups");
446 if (setresuid(target, target, target) != 0)
447 errx(1, "setresuid");
450 if (pledge("stdio rpath exec", NULL) == -1)
453 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
458 if (pledge("stdio exec", NULL) == -1)
461 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
462 myname, cmdline, pw->pw_name, cwd);
464 envp = prepenv(rule);
467 if (setenv("PATH", safepath, 1) == -1)
468 err(1, "failed to set PATH '%s'", safepath);
470 execvpe(cmd, argv, envp);
472 errx(1, "%s: command not found", cmd);