1 /* $OpenBSD: doas.c,v 1.53 2016/06/05 00:46:34 djm 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>
39 fprintf(stderr, "doas: version %s built %s\n", VERSION, __DATE__);
46 fprintf(stderr, "usage: doas [-nsv] [-a style] [-C config] [-u user]"
52 arraylen(const char **arr)
66 parseuid(const char *s, uid_t *uid)
71 if ((pw = getpwnam(s)) != NULL) {
75 *uid = strtonum(s, 0, UID_MAX, &errstr);
82 uidcheck(const char *s, uid_t desired)
86 if (parseuid(s, &uid) != 0)
94 parsegid(const char *s, gid_t *gid)
99 if ((gr = getgrnam(s)) != NULL) {
103 *gid = strtonum(s, 0, GID_MAX, &errstr);
110 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
111 const char **cmdargs, struct rule *r)
115 if (r->ident[0] == ':') {
117 if (parsegid(r->ident + 1, &rgid) == -1)
119 for (i = 0; i < ngroups; i++) {
120 if (rgid == groups[i])
126 if (uidcheck(r->ident, uid) != 0)
129 if (r->target && uidcheck(r->target, target) != 0)
132 if (strcmp(r->cmd, cmd))
135 /* if arguments were given, they should match explicitly */
136 for (i = 0; r->cmdargs[i]; i++) {
139 if (strcmp(r->cmdargs[i], cmdargs[i]))
150 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
151 uid_t target, const char *cmd, const char **cmdargs)
156 for (i = 0; i < nrules; i++) {
157 if (match(uid, groups, ngroups, target, cmd,
163 return (*lastr)->action == PERMIT;
167 parseconfig(const char *filename, int checkperms)
170 extern int yyparse(void);
173 yyfp = fopen(filename, "r");
175 err(1, checkperms ? "doas is not enabled, %s" :
176 "could not open config file %s", filename);
179 if (fstat(fileno(yyfp), &sb) != 0)
180 err(1, "fstat(\"%s\")", filename);
181 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
182 errx(1, "%s is writable by group or other", filename);
184 errx(1, "%s is not owned by root", filename);
194 * Copy the environment variables in safeset from oldenvp to envp.
197 copyenvhelper(const char **oldenvp, const char **safeset, size_t nsafe,
202 for (i = 0; i < nsafe; i++) {
203 const char **oe = oldenvp;
205 size_t len = strlen(safeset[i]);
206 if (strncmp(*oe, safeset[i], len) == 0 &&
208 if (!(envp[ei++] = strdup(*oe)))
219 copyenv(const char **oldenvp, struct rule *rule)
221 const char *safeset[] = {
222 "DISPLAY", "HOME", "LOGNAME", "MAIL",
223 "PATH", "TERM", "USER", "USERNAME",
226 const char *badset[] = {
236 /* if there was no envvar whitelist, pass all except badset ones */
237 nbad = arraylen(badset);
238 if ((rule->options & KEEPENV) && !rule->envlist) {
240 size_t oldlen = arraylen(oldenvp);
241 envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
243 err(1, "reallocarray");
244 for (inew = iold = 0; iold < oldlen; iold++) {
246 for (ibad = 0; ibad < nbad; ibad++) {
247 size_t len = strlen(badset[ibad]);
248 if (strncmp(oldenvp[iold], badset[ibad], len) == 0 &&
249 oldenvp[iold][len] == '=') {
254 if (!(envp[inew] = strdup(oldenvp[iold])))
263 nsafe = arraylen(safeset);
264 if ((extra = rule->envlist)) {
266 nextras = arraylen(extra);
267 for (isafe = 0; isafe < nsafe; isafe++) {
269 for (iextras = 0; iextras < nextras; iextras++) {
270 if (strcmp(extra[iextras], safeset[isafe]) == 0) {
272 extra[iextras] = extra[nextras];
273 extra[nextras] = NULL;
280 envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
282 err(1, "can't allocate new environment");
285 ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
286 ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
292 /* find index of 'name' in environment envp */
294 findenv(const char **envp, const char *name, size_t namelen)
298 for (i = 0 ; envp[i] != NULL; i++) {
299 if (strlen(envp[i]) < namelen + 1)
301 if (strncmp(envp[i], name, namelen) == 0 &&
302 envp[i][namelen] == '=')
308 /* merge rule->setenvlist into environment list; frees oldenvp */
310 dosetenv(char **oldenvp, struct rule *rule)
312 size_t n, i, nset, nold;
313 char **envp, *cp, *cp2;
316 if (!(rule->options & SETENV))
319 nset = arraylen(rule->setenvlist);
320 nold = arraylen((const char**)oldenvp);
322 /* insert new variables */
325 for (i = 0; i < nset; i++) {
326 if ((cp = strchr(rule->setenvlist[i], '=')) == NULL)
327 errx(1, "invalid setenv"); /* shouldn't happen */
328 if (cp[1] == '\0' || cp - rule->setenvlist[i] > INT_MAX)
329 continue; /* skip variables with empty values */
330 if ((envp = reallocarray(envp, n + 2, sizeof(*envp))) == NULL)
331 errx(1, "reallocarray failed");
333 /* FOO=$BAR: lookup and copy */
334 if ((cp2 = getenv(cp + 2)) == NULL)
335 continue; /* not found; skip */
336 if (asprintf(&(envp[n++]), "%.*s=%s",
337 (int)(cp - rule->setenvlist[i]),
338 rule->setenvlist[i], cp2) == -1)
339 errx(1, "asprintf failed");
343 if ((envp[n++] = strdup(rule->setenvlist[i])) == NULL)
344 errx(1, "strdup failed");
347 /* move old variables, dropping ones already set */
348 for (i = 0; i < nold; i++) {
349 if ((cp = strchr(oldenvp[i], '=')) == NULL)
350 errx(1, "invalid env"); /* shouldn't happen */
351 found = findenv(rule->setenvlist, oldenvp[i], cp - oldenvp[i]);
353 free(oldenvp[i]); /* discard */
355 if ((envp = reallocarray(envp, n + 2,
356 sizeof(*envp))) == NULL)
357 errx(1, "reallocarray failed");
358 envp[n++] = oldenvp[i]; /* move */
368 checkconfig(const char *confpath, int argc, char **argv,
369 uid_t uid, gid_t *groups, int ngroups, uid_t target)
373 if (setresuid(uid, uid, uid) != 0)
376 parseconfig(confpath, 0);
380 if (permit(uid, groups, ngroups, &rule, target, argv[0],
381 (const char **)argv + 1)) {
382 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
391 main(int argc, char **argv, char **envp)
393 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
394 "/usr/local/bin:/usr/local/sbin";
395 const char *confpath = NULL;
396 char *shargv[] = { NULL, NULL };
399 char cmdline[LINE_MAX];
400 char myname[_PW_NAME_LEN + 1];
405 gid_t groups[NGROUPS_MAX + 1];
411 char cwdpath[PATH_MAX];
413 #ifdef HAVE_BSD_AUTH_H
414 char *login_style = NULL;
419 if (pledge("stdio rpath getpw tty proc exec id", NULL) == -1)
422 /* closefrom(STDERR_FILENO + 1); */
426 #ifdef HAVE_BSD_AUTH_H
427 # define OPTSTRING "a:C:nsu:v"
429 # define OPTSTRING "C:nsu:v"
432 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
434 #ifdef HAVE_BSD_AUTH_H
436 login_style = optarg;
443 if (parseuid(optarg, &target) != 0)
444 errx(1, "unknown user");
469 } else if ((!sflag && !argc) || (sflag && argc))
474 err(1, "getpwuid failed");
475 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
476 errx(1, "pw_name too long");
477 ngroups = getgroups(NGROUPS_MAX, groups);
479 err(1, "can't get groups");
480 groups[ngroups++] = getgid();
483 sh = getenv("SHELL");
484 if (sh == NULL || *sh == '\0')
485 shargv[0] = pw->pw_shell;
493 checkconfig(confpath, argc, argv, uid, groups, ngroups,
495 exit(1); /* fail safe */
498 parseconfig("/etc/doas.conf", 1);
500 /* cmdline is used only for logging, no need to abort on truncate */
501 (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
502 for (i = 1; i < argc; i++) {
503 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
505 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
510 if (!permit(uid, groups, ngroups, &rule, target, cmd,
511 (const char**)argv + 1)) {
512 syslog(LOG_AUTHPRIV | LOG_NOTICE,
513 "failed command for %s: %s", myname, cmdline);
514 errc(1, EPERM, NULL);
517 #ifdef HAVE_BSD_AUTH_H
518 if (!(rule->options & NOPASS)) {
520 errx(1, "Authorization required");
522 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
525 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
527 errx(1, "Authorization failed");
529 char host[HOST_NAME_MAX + 1];
530 if (gethostname(host, sizeof(host)))
531 snprintf(host, sizeof(host), "?");
532 snprintf(cbuf, sizeof(cbuf),
533 "\rdoas (%.32s@%.32s) password: ", myname, host);
536 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
538 if (response == NULL && errno == ENOTTY) {
539 syslog(LOG_AUTHPRIV | LOG_NOTICE,
540 "tty required for %s", myname);
541 errx(1, "a tty is required");
543 if (!auth_userresponse(as, response, 0)) {
544 syslog(LOG_AUTHPRIV | LOG_NOTICE,
545 "failed auth for %s", myname);
546 errc(1, EPERM, NULL);
548 explicit_bzero(rbuf, sizeof(rbuf));
550 #elif HAVE_PAM_APPL_H
551 if (!doas_pam(myname, !nflag, rule->options & NOPASS)) {
552 syslog(LOG_AUTHPRIV | LOG_NOTICE,
553 "failed auth for %s", myname);
554 errc(1, EPERM, NULL);
557 if (!(rule->options & NOPASS)) {
558 errx(1, "Authorization required");
559 #endif /* HAVE_BSD_AUTH_H */
561 if (pledge("stdio rpath getpw exec id", NULL) == -1)
564 pw = getpwuid(target);
566 errx(1, "no passwd entry for target");
568 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
569 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
571 errx(1, "failed to set user context for target");
573 if (pledge("stdio rpath exec", NULL) == -1)
576 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
581 if (pledge("stdio exec", NULL) == -1)
584 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
585 myname, cmdline, pw->pw_name, cwd);
587 envp = copyenv((const char **)envp, rule);
589 envp = dosetenv(envp, rule);
592 if (setenv("PATH", safepath, 1) == -1)
593 err(1, "failed to set PATH '%s'", safepath);
595 execvpe(cmd, argv, envp);
597 errx(1, "%s: command not found", cmd);