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");
165 warn("could not open config file");
170 if (fstat(fileno(yyfp), &sb) != 0)
171 err(1, "fstat(\"%s\")", filename);
172 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
173 errx(1, "%s is writable by group or other", filename);
175 errx(1, "%s is not owned by root", filename);
185 * Copy the environment variables in safeset from oldenvp to envp.
188 copyenvhelper(const char **oldenvp, const char **safeset, size_t nsafe,
193 for (i = 0; i < nsafe; i++) {
194 const char **oe = oldenvp;
196 size_t len = strlen(safeset[i]);
197 if (strncmp(*oe, safeset[i], len) == 0 &&
199 if (!(envp[ei++] = strdup(*oe)))
210 copyenv(const char **oldenvp, struct rule *rule)
212 const char *safeset[] = {
213 "DISPLAY", "HOME", "LOGNAME", "MAIL",
214 "PATH", "TERM", "USER", "USERNAME",
217 const char *badset[] = {
227 /* if there was no envvar whitelist, pass all except badset ones */
228 nbad = arraylen(badset);
229 if ((rule->options & KEEPENV) && !rule->envlist) {
231 size_t oldlen = arraylen(oldenvp);
232 envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
234 err(1, "reallocarray");
235 for (inew = iold = 0; iold < oldlen; iold++) {
237 for (ibad = 0; ibad < nbad; ibad++) {
238 size_t len = strlen(badset[ibad]);
239 if (strncmp(oldenvp[iold], badset[ibad], len) == 0 &&
240 oldenvp[iold][len] == '=') {
245 if (!(envp[inew] = strdup(oldenvp[iold])))
254 nsafe = arraylen(safeset);
255 if ((extra = rule->envlist)) {
257 nextras = arraylen(extra);
258 for (isafe = 0; isafe < nsafe; isafe++) {
260 for (iextras = 0; iextras < nextras; iextras++) {
261 if (strcmp(extra[iextras], safeset[isafe]) == 0) {
263 extra[iextras] = extra[nextras];
264 extra[nextras] = NULL;
271 envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
273 err(1, "can't allocate new environment");
276 ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
277 ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
286 fprintf(stderr, "Permission denied\n");
291 checkconfig(const char *confpath, int argc, char **argv,
292 uid_t uid, gid_t *groups, int ngroups, uid_t target)
296 setresuid(uid, uid, uid);
297 parseconfig(confpath, 0);
301 if (permit(uid, groups, ngroups, &rule, target, argv[0],
302 (const char **)argv + 1)) {
303 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
312 main(int argc, char **argv, char **envp)
314 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
315 "/usr/local/bin:/usr/local/sbin";
316 const char *confpath = NULL;
317 char *shargv[] = { NULL, NULL };
320 char cmdline[LINE_MAX];
321 char myname[_PW_NAME_LEN + 1];
326 gid_t groups[NGROUPS_MAX + 1];
333 while ((ch = getopt(argc, argv, "C:nsu:")) != -1) {
339 if (parseuid(optarg, &target) != 0)
340 errx(1, "unknown user");
359 } else if ((!sflag && !argc) || (sflag && argc))
365 err(1, "getpwuid failed");
366 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
367 errx(1, "pw_name too long");
368 ngroups = getgroups(NGROUPS_MAX, groups);
370 err(1, "can't get groups");
371 groups[ngroups++] = getgid();
374 sh = getenv("SHELL");
375 if (sh == NULL || *sh == '\0')
376 shargv[0] = pw->pw_shell;
384 checkconfig(confpath, argc, argv, uid, groups, ngroups,
386 exit(1); /* fail safe */
389 parseconfig("/etc/doas.conf", 1);
391 /* cmdline is used only for logging, no need to abort on truncate */
392 (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
393 for (i = 1; i < argc; i++) {
394 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
396 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
401 if (!permit(uid, groups, ngroups, &rule, target, cmd,
402 (const char**)argv + 1)) {
403 syslog(LOG_AUTHPRIV | LOG_NOTICE,
404 "failed command for %s: %s", myname, cmdline);
408 if (!(rule->options & NOPASS)) {
410 errx(1, "Authorization required");
411 if (!auth_userokay(myname, NULL, NULL, NULL)) {
412 syslog(LOG_AUTHPRIV | LOG_NOTICE,
413 "failed password for %s", myname);
417 envp = copyenv((const char **)envp, rule);
419 pw = getpwuid(target);
421 errx(1, "no passwd entry for target");
422 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
423 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
425 errx(1, "failed to set user context for target");
427 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
428 myname, pw->pw_name, cmdline);
429 if (setenv("PATH", safepath, 1) == -1)
430 err(1, "failed to set PATH '%s'", safepath);
431 execvpe(cmd, argv, envp);
433 errx(1, "%s: command not found", cmd);