1 /* $OpenBSD: doas.c,v 1.32 2015/07/29 00:00:31 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>
39 fprintf(stderr, "usage: doas [-ns] [-C config] [-u user] command [args]\n");
44 arraylen(const char **arr)
56 parseuid(const char *s, uid_t *uid)
61 if ((pw = getpwnam(s)) != NULL) {
65 *uid = strtonum(s, 0, UID_MAX, &errstr);
72 uidcheck(const char *s, uid_t desired)
76 if (parseuid(s, &uid) != 0)
84 parsegid(const char *s, gid_t *gid)
89 if ((gr = getgrnam(s)) != NULL) {
93 *gid = strtonum(s, 0, GID_MAX, &errstr);
100 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
101 const char **cmdargs, struct rule *r)
105 if (r->ident[0] == ':') {
107 if (parsegid(r->ident + 1, &rgid) == -1)
109 for (i = 0; i < ngroups; i++) {
110 if (rgid == groups[i])
116 if (uidcheck(r->ident, uid) != 0)
119 if (r->target && uidcheck(r->target, target) != 0)
122 if (strcmp(r->cmd, cmd))
125 /* if arguments were given, they should match explicitly */
126 for (i = 0; r->cmdargs[i]; i++) {
129 if (strcmp(r->cmdargs[i], cmdargs[i]))
140 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
141 uid_t target, const char *cmd, const char **cmdargs)
146 for (i = 0; i < nrules; i++) {
147 if (match(uid, groups, ngroups, target, cmd,
153 return (*lastr)->action == PERMIT;
157 parseconfig(const char *filename, int checkperms)
160 extern int yyparse(void);
163 yyfp = fopen(filename, "r");
166 fprintf(stderr, "doas is not enabled.\n");
168 warn("could not open config file");
173 if (fstat(fileno(yyfp), &sb) != 0)
174 err(1, "fstat(\"%s\")", filename);
175 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
176 errx(1, "%s is writable by group or other", filename);
178 errx(1, "%s is not owned by root", filename);
188 * Copy the environment variables in safeset from oldenvp to envp.
191 copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
196 for (i = 0; i < nsafe; i++) {
197 const char **oe = oldenvp;
199 size_t len = strlen(safeset[i]);
200 if (strncmp(*oe, safeset[i], len) == 0 &&
202 if (!(envp[ei++] = strdup(*oe)))
213 copyenv(const char **oldenvp, struct rule *rule)
215 const char *safeset[] = {
216 "DISPLAY", "HOME", "LOGNAME", "MAIL",
217 "PATH", "TERM", "USER", "USERNAME",
220 const char *badset[] = {
230 /* if there was no envvar whitelist, pass all except badset ones */
231 nbad = arraylen(badset);
232 if ((rule->options & KEEPENV) && !rule->envlist) {
234 size_t oldlen = arraylen(oldenvp);
235 envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
237 err(1, "reallocarray");
238 for (inew = iold = 0; iold < oldlen; iold++) {
240 for (ibad = 0; ibad < nbad; ibad++) {
241 size_t len = strlen(badset[ibad]);
242 if (strncmp(oldenvp[iold], badset[ibad], len) == 0 &&
243 oldenvp[iold][len] == '=') {
248 if (!(envp[inew] = strdup(oldenvp[iold])))
257 nsafe = arraylen(safeset);
258 if ((extra = rule->envlist)) {
260 nextras = arraylen(extra);
261 for (isafe = 0; isafe < nsafe; isafe++) {
263 for (iextras = 0; iextras < nextras; iextras++) {
264 if (strcmp(extra[iextras], safeset[isafe]) == 0) {
266 extra[iextras] = extra[nextras];
267 extra[nextras] = NULL;
274 envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
276 err(1, "can't allocate new environment");
279 ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
280 ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
289 fprintf(stderr, "Permission denied\n");
294 checkconfig(const char *confpath, int argc, char **argv,
295 uid_t uid, gid_t *groups, int ngroups, uid_t target)
299 setresuid(uid, uid, uid);
300 parseconfig(confpath, 0);
304 if (permit(uid, groups, ngroups, &rule, target, argv[0],
305 (const char **)argv + 1)) {
306 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
315 main(int argc, char **argv, char **envp)
317 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
318 "/usr/local/bin:/usr/local/sbin";
319 const char *confpath = NULL;
320 char *shargv[] = { NULL, NULL };
323 char cmdline[LINE_MAX];
324 char myname[_PW_NAME_LEN + 1];
329 gid_t groups[NGROUPS_MAX + 1];
336 while ((ch = getopt(argc, argv, "C:nsu:")) != -1) {
342 if (parseuid(optarg, &target) != 0)
343 errx(1, "unknown user");
362 } else if ((!sflag && !argc) || (sflag && argc))
368 err(1, "getpwuid failed");
369 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
370 errx(1, "pw_name too long");
371 ngroups = getgroups(NGROUPS_MAX, groups);
373 err(1, "can't get groups");
374 groups[ngroups++] = getgid();
377 sh = getenv("SHELL");
378 if (sh == NULL || *sh == '\0')
379 shargv[0] = pw->pw_shell;
387 checkconfig(confpath, argc, argv, uid, groups, ngroups,
389 exit(1); /* fail safe */
392 parseconfig("/etc/doas.conf", 1);
394 /* cmdline is used only for logging, no need to abort on truncate */
395 (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
396 for (i = 1; i < argc; i++) {
397 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
399 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
404 if (!permit(uid, groups, ngroups, &rule, target, cmd,
405 (const char**)argv + 1)) {
406 syslog(LOG_AUTHPRIV | LOG_NOTICE,
407 "failed command for %s: %s", myname, cmdline);
411 if (!(rule->options & NOPASS)) {
413 errx(1, "Authorization required");
414 if (!auth_userokay(myname, NULL, NULL, NULL)) {
415 syslog(LOG_AUTHPRIV | LOG_NOTICE,
416 "failed password for %s", myname);
420 envp = copyenv((const char **)envp, rule);
422 pw = getpwuid(target);
424 errx(1, "no passwd entry for target");
425 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
426 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
428 errx(1, "failed to set user context for target");
430 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
431 myname, pw->pw_name, cmdline);
432 if (setenv("PATH", safepath, 1) == -1)
433 err(1, "failed to set PATH '%s'", safepath);
434 execvpe(cmd, argv, envp);
436 errx(1, "%s: command not found", cmd);