1 /* $OpenBSD: doas.c,v 1.33 2015/07/30 17:04:33 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)
58 parseuid(const char *s, uid_t *uid)
63 if ((pw = getpwnam(s)) != NULL) {
67 *uid = strtonum(s, 0, UID_MAX, &errstr);
74 uidcheck(const char *s, uid_t desired)
78 if (parseuid(s, &uid) != 0)
86 parsegid(const char *s, gid_t *gid)
91 if ((gr = getgrnam(s)) != NULL) {
95 *gid = strtonum(s, 0, GID_MAX, &errstr);
102 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
103 const char **cmdargs, struct rule *r)
107 if (r->ident[0] == ':') {
109 if (parsegid(r->ident + 1, &rgid) == -1)
111 for (i = 0; i < ngroups; i++) {
112 if (rgid == groups[i])
118 if (uidcheck(r->ident, uid) != 0)
121 if (r->target && uidcheck(r->target, target) != 0)
124 if (strcmp(r->cmd, cmd))
127 /* if arguments were given, they should match explicitly */
128 for (i = 0; r->cmdargs[i]; i++) {
131 if (strcmp(r->cmdargs[i], cmdargs[i]))
142 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
143 uid_t target, const char *cmd, const char **cmdargs)
148 for (i = 0; i < nrules; i++) {
149 if (match(uid, groups, ngroups, target, cmd,
155 return (*lastr)->action == PERMIT;
159 parseconfig(const char *filename, int checkperms)
162 extern int yyparse(void);
165 yyfp = fopen(filename, "r");
167 warn("could not open config file");
172 if (fstat(fileno(yyfp), &sb) != 0)
173 err(1, "fstat(\"%s\")", filename);
174 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
175 errx(1, "%s is writable by group or other", filename);
177 errx(1, "%s is not owned by root", filename);
187 * Copy the environment variables in safeset from oldenvp to envp.
190 copyenvhelper(const char **oldenvp, const char **safeset, size_t nsafe,
195 for (i = 0; i < nsafe; i++) {
196 const char **oe = oldenvp;
198 size_t len = strlen(safeset[i]);
199 if (strncmp(*oe, safeset[i], len) == 0 &&
201 if (!(envp[ei++] = strdup(*oe)))
212 copyenv(const char **oldenvp, struct rule *rule)
214 const char *safeset[] = {
215 "DISPLAY", "HOME", "LOGNAME", "MAIL",
216 "PATH", "TERM", "USER", "USERNAME",
219 const char *badset[] = {
229 /* if there was no envvar whitelist, pass all except badset ones */
230 nbad = arraylen(badset);
231 if ((rule->options & KEEPENV) && !rule->envlist) {
233 size_t oldlen = arraylen(oldenvp);
234 envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
236 err(1, "reallocarray");
237 for (inew = iold = 0; iold < oldlen; iold++) {
239 for (ibad = 0; ibad < nbad; ibad++) {
240 size_t len = strlen(badset[ibad]);
241 if (strncmp(oldenvp[iold], badset[ibad], len) == 0 &&
242 oldenvp[iold][len] == '=') {
247 if (!(envp[inew] = strdup(oldenvp[iold])))
256 nsafe = arraylen(safeset);
257 if ((extra = rule->envlist)) {
259 nextras = arraylen(extra);
260 for (isafe = 0; isafe < nsafe; isafe++) {
262 for (iextras = 0; iextras < nextras; iextras++) {
263 if (strcmp(extra[iextras], safeset[isafe]) == 0) {
265 extra[iextras] = extra[nextras];
266 extra[nextras] = NULL;
273 envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
275 err(1, "can't allocate new environment");
278 ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
279 ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
288 fprintf(stderr, "Permission denied\n");
293 checkconfig(const char *confpath, int argc, char **argv,
294 uid_t uid, gid_t *groups, int ngroups, uid_t target)
298 setresuid(uid, uid, uid);
299 parseconfig(confpath, 0);
303 if (permit(uid, groups, ngroups, &rule, target, argv[0],
304 (const char **)argv + 1)) {
305 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
314 main(int argc, char **argv, char **envp)
316 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
317 "/usr/local/bin:/usr/local/sbin";
318 const char *confpath = NULL;
319 char *shargv[] = { NULL, NULL };
322 char cmdline[LINE_MAX];
323 char myname[_PW_NAME_LEN + 1];
328 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))
367 err(1, "getpwuid failed");
368 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
369 errx(1, "pw_name too long");
370 ngroups = getgroups(NGROUPS_MAX, groups);
372 err(1, "can't get groups");
373 groups[ngroups++] = getgid();
376 sh = getenv("SHELL");
377 if (sh == NULL || *sh == '\0')
378 shargv[0] = pw->pw_shell;
386 checkconfig(confpath, argc, argv, uid, groups, ngroups,
388 exit(1); /* fail safe */
391 parseconfig("/etc/doas.conf", 1);
393 /* cmdline is used only for logging, no need to abort on truncate */
394 (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
395 for (i = 1; i < argc; i++) {
396 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
398 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
403 if (!permit(uid, groups, ngroups, &rule, target, cmd,
404 (const char**)argv + 1)) {
405 syslog(LOG_AUTHPRIV | LOG_NOTICE,
406 "failed command for %s: %s", myname, cmdline);
410 if (!(rule->options & NOPASS)) {
412 errx(1, "Authorization required");
413 if (!auth_userokay(myname, NULL, NULL, NULL)) {
414 syslog(LOG_AUTHPRIV | LOG_NOTICE,
415 "failed password for %s", myname);
419 envp = copyenv((const char **)envp, rule);
421 pw = getpwuid(target);
423 errx(1, "no passwd entry for target");
424 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
425 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
427 errx(1, "failed to set user context for target");
429 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
430 myname, pw->pw_name, cmdline);
431 if (setenv("PATH", safepath, 1) == -1)
432 err(1, "failed to set PATH '%s'", safepath);
433 execvpe(cmd, argv, envp);
435 errx(1, "%s: command not found", cmd);