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)
177 extern int yyparse(void);
180 yyfp = fopen(filename, "r");
182 err(1, checkperms ? "doas is not enabled, %s" :
183 "could not open config file %s", filename);
186 if (fstat(fileno(yyfp), &sb) != 0)
187 err(1, "fstat(\"%s\")", filename);
188 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
189 errx(1, "%s is writable by group or other", filename);
191 errx(1, "%s is not owned by root", filename);
201 checkconfig(const char *confpath, int argc, char **argv,
202 uid_t uid, gid_t *groups, int ngroups, uid_t target)
206 if (setresuid(uid, uid, uid) != 0)
209 parseconfig(confpath, 0);
213 if (permit(uid, groups, ngroups, &rule, target, argv[0],
214 (const char **)argv + 1)) {
215 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
223 #ifdef HAVE_BSD_AUTH_H
225 authuser(char *myname, char *login_style, int persist)
227 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
232 fd = open("/dev/tty", O_RDWR);
234 if (ioctl(fd, TIOCCHKVERAUTH) == 0)
238 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
240 errx(1, "Authorization failed");
242 char host[HOST_NAME_MAX + 1];
243 if (gethostname(host, sizeof(host)))
244 snprintf(host, sizeof(host), "?");
245 snprintf(cbuf, sizeof(cbuf),
246 "\rdoas (%.32s@%.32s) password: ", myname, host);
249 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
251 if (response == NULL && errno == ENOTTY) {
252 syslog(LOG_AUTHPRIV | LOG_NOTICE,
253 "tty required for %s", myname);
254 errx(1, "a tty is required");
256 if (!auth_userresponse(as, response, 0)) {
257 syslog(LOG_AUTHPRIV | LOG_NOTICE,
258 "failed auth for %s", myname);
259 errc(1, EPERM, NULL);
261 explicit_bzero(rbuf, sizeof(rbuf));
265 ioctl(fd, TIOCSETVERAUTH, &secs);
271 authuser(const char *myname, const char *login_style, int persist)
280 if (!(pw = getpwnam(myname)))
283 hash = pw->pw_passwd;
284 if (hash[0] == 'x' && hash[1] == '\0') {
286 if (!(sp = getspnam(myname)))
287 errx(1, "Authorization failed");
289 } else if (hash[0] != '*') {
290 errx(1, "Authorization failed");
293 char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
294 if (gethostname(host, sizeof(host)))
295 snprintf(host, sizeof(host), "?");
296 snprintf(cbuf, sizeof(cbuf),
297 "\rdoas (%.32s@%.32s) password: ", myname, host);
300 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
301 if (response == NULL && errno == ENOTTY) {
302 syslog(LOG_AUTHPRIV | LOG_NOTICE,
303 "tty required for %s", myname);
304 errx(1, "a tty is required");
306 if (!(encrypted = crypt(response, hash)))
308 if (strcmp(encrypted, hash) != 0) {
309 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
310 errx(1, "Authorization failed");
312 explicit_bzero(rbuf, sizeof(rbuf));
314 #endif /* HAVE_BSD_AUTH_H */
317 main(int argc, char **argv)
319 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
320 "/usr/local/bin:/usr/local/sbin";
321 const char *confpath = NULL;
322 char *shargv[] = { NULL, NULL };
325 char cmdline[LINE_MAX];
326 char myname[_PW_NAME_LEN + 1];
331 gid_t groups[NGROUPS_MAX + 1];
337 char cwdpath[PATH_MAX];
340 char *login_style = NULL;
344 closefrom(STDERR_FILENO + 1);
348 #ifdef HAVE_BSD_AUTH_H
349 # define OPTSTRING "a:C:Lnsu:v"
351 # define OPTSTRING "+C:Lnsu:v"
354 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
356 #ifdef HAVE_BSD_AUTH_H
358 login_style = optarg;
365 #ifdef TIOCCLRVERAUTH
366 i = open("/dev/tty", O_RDWR);
368 ioctl(i, TIOCCLRVERAUTH);
372 if (parseuid(optarg, &target) != 0)
373 errx(1, "unknown user");
398 } else if ((!sflag && !argc) || (sflag && argc))
403 err(1, "getpwuid failed");
404 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
405 errx(1, "pw_name too long");
406 ngroups = getgroups(NGROUPS_MAX, groups);
408 err(1, "can't get groups");
409 groups[ngroups++] = getgid();
412 sh = getenv("SHELL");
413 if (sh == NULL || *sh == '\0') {
414 shargv[0] = strdup(pw->pw_shell);
415 if (shargv[0] == NULL)
424 checkconfig(confpath, argc, argv, uid, groups, ngroups,
426 exit(1); /* fail safe */
429 parseconfig("/etc/doas.conf", 1);
431 /* cmdline is used only for logging, no need to abort on truncate */
432 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
433 for (i = 1; i < argc; i++) {
434 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
436 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
441 if (!permit(uid, groups, ngroups, &rule, target, cmd,
442 (const char **)argv + 1)) {
443 syslog(LOG_AUTHPRIV | LOG_NOTICE,
444 "failed command for %s: %s", myname, cmdline);
445 errc(1, EPERM, NULL);
448 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
449 if (!(rule->options & NOPASS)) {
451 errx(1, "Authorization required");
453 authuser(myname, login_style, rule->options & PERSIST);
455 #elif HAVE_PAM_APPL_H
456 pw = getpwuid(target);
458 errx(1, "no passwd entry for target");
460 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
461 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
462 errx(1, "Authorization failed");
465 #error "No authentication method"
466 #endif /* HAVE_BSD_AUTH_H */
468 if (pledge("stdio rpath getpw exec id", NULL) == -1)
471 pw = getpwuid(target);
473 errx(1, "no passwd entry for target");
475 #ifdef HAVE_BSD_AUTH_H
476 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
477 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
479 errx(1, "failed to set user context for target");
481 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
482 errx(1, "setresgid");
483 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
484 errx(1, "initgroups");
485 if (setresuid(target, target, target) != 0)
486 errx(1, "setresuid");
489 if (pledge("stdio rpath exec", NULL) == -1)
492 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
497 if (pledge("stdio exec", NULL) == -1)
500 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
501 myname, cmdline, pw->pw_name, cwd);
503 envp = prepenv(rule);
506 if (setenv("PATH", safepath, 1) == -1)
507 err(1, "failed to set PATH '%s'", safepath);
509 execvpe(cmd, argv, envp);
511 errx(1, "%s: command not found", cmd);