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" : "");
223 main(int argc, char **argv)
225 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
226 "/usr/local/bin:/usr/local/sbin";
227 const char *confpath = NULL;
228 char *shargv[] = { NULL, NULL };
231 char cmdline[LINE_MAX];
232 char myname[_PW_NAME_LEN + 1];
237 gid_t groups[NGROUPS_MAX + 1];
243 char cwdpath[PATH_MAX];
246 #ifdef HAVE_BSD_AUTH_H
247 char *login_style = NULL;
252 if (pledge("stdio rpath getpw tty recvfd proc exec id", NULL) == -1)
255 /* closefrom(STDERR_FILENO + 1); */
259 #ifdef HAVE_BSD_AUTH_H
260 # define OPTSTRING "a:C:nsu:v"
262 # define OPTSTRING "+C:nsu:v"
265 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
267 #ifdef HAVE_BSD_AUTH_H
269 login_style = optarg;
276 if (parseuid(optarg, &target) != 0)
277 errx(1, "unknown user");
302 } else if ((!sflag && !argc) || (sflag && argc))
307 err(1, "getpwuid failed");
308 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
309 errx(1, "pw_name too long");
310 ngroups = getgroups(NGROUPS_MAX, groups);
312 err(1, "can't get groups");
313 groups[ngroups++] = getgid();
316 sh = getenv("SHELL");
317 if (sh == NULL || *sh == '\0') {
318 shargv[0] = strdup(pw->pw_shell);
319 if (shargv[0] == NULL)
328 checkconfig(confpath, argc, argv, uid, groups, ngroups,
330 exit(1); /* fail safe */
333 parseconfig("/etc/doas.conf", 1);
335 /* cmdline is used only for logging, no need to abort on truncate */
336 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
337 for (i = 1; i < argc; i++) {
338 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
340 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
345 if (!permit(uid, groups, ngroups, &rule, target, cmd,
346 (const char **)argv + 1)) {
347 syslog(LOG_AUTHPRIV | LOG_NOTICE,
348 "failed command for %s: %s", myname, cmdline);
349 errc(1, EPERM, NULL);
352 #ifdef HAVE_BSD_AUTH_H
353 if (!(rule->options & NOPASS)) {
355 errx(1, "Authorization required");
357 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
360 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
362 errx(1, "Authorization failed");
364 char host[HOST_NAME_MAX + 1];
365 if (gethostname(host, sizeof(host)))
366 snprintf(host, sizeof(host), "?");
367 snprintf(cbuf, sizeof(cbuf),
368 "\rdoas (%.32s@%.32s) password: ", myname, host);
371 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
373 if (response == NULL && errno == ENOTTY) {
374 syslog(LOG_AUTHPRIV | LOG_NOTICE,
375 "tty required for %s", myname);
376 errx(1, "a tty is required");
378 if (!auth_userresponse(as, response, 0)) {
379 syslog(LOG_AUTHPRIV | LOG_NOTICE,
380 "failed auth for %s", myname);
381 errc(1, EPERM, NULL);
383 explicit_bzero(rbuf, sizeof(rbuf));
385 #elif HAVE_PAM_APPL_H
386 pw = getpwuid(target);
388 errx(1, "no passwd entry for target");
390 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
391 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
392 errc(1, EPERM, NULL);
397 if (!(rule->options & NOPASS)) {
399 errx(1, "Authorization required");
401 pass = pw->pw_passwd;
402 if (pass[0] == 'x' && pass[1] == '\0') {
404 if (!(sp = getspnam(myname)))
405 errx(1, "Authorization failed");
409 char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
410 if (gethostname(host, sizeof(host)))
411 snprintf(host, sizeof(host), "?");
412 snprintf(cbuf, sizeof(cbuf),
413 "\rdoas (%.32s@%.32s) password: ", myname, host);
416 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
417 if (response == NULL && errno == ENOTTY) {
418 syslog(LOG_AUTHPRIV | LOG_NOTICE,
419 "tty required for %s", myname);
420 errx(1, "a tty is required");
422 if (strcmp(crypt(response, pass), pass) != 0) {
423 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
424 errc(1, EPERM, NULL);
426 explicit_bzero(rbuf, sizeof(rbuf));
429 if (!(rule->options & NOPASS))
430 errx(1, "Authorization required");
431 #endif /* HAVE_BSD_AUTH_H */
433 if (pledge("stdio rpath getpw exec id", NULL) == -1)
436 pw = getpwuid(target);
438 errx(1, "no passwd entry for target");
440 #ifdef HAVE_BSD_AUTH_H
441 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
442 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
444 errx(1, "failed to set user context for target");
446 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
447 errx(1, "setresgid");
448 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
449 errx(1, "initgroups");
450 if (setresuid(target, target, target) != 0)
451 errx(1, "setresuid");
454 if (pledge("stdio rpath exec", NULL) == -1)
457 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
462 if (pledge("stdio exec", NULL) == -1)
465 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
466 myname, cmdline, pw->pw_name, cwd);
468 envp = prepenv(rule);
471 if (setenv("PATH", safepath, 1) == -1)
472 err(1, "failed to set PATH '%s'", safepath);
474 execvpe(cmd, argv, envp);
476 errx(1, "%s: command not found", cmd);