1 /* $OpenBSD: doas.c,v 1.25 2015/07/26 19:49:11 zhuk 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>
22 #include <login_cap.h>
39 fprintf(stderr, "usage: doas [-s] [-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 strtogid(const char *s)
90 if ((gr = getgrnam(s)) != NULL)
92 gid = strtonum(s, 0, GID_MAX, &errstr);
99 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
100 const char **cmdargs, struct rule *r)
104 if (r->ident[0] == ':') {
105 gid_t rgid = strtogid(r->ident + 1);
108 for (i = 0; i < ngroups; i++) {
109 if (rgid == groups[i])
115 if (uidcheck(r->ident, uid) != 0)
118 if (r->target && uidcheck(r->target, target) != 0)
121 if (strcmp(r->cmd, cmd))
124 /* if arguments were given, they should match explicitly */
125 for (i = 0; r->cmdargs[i]; i++) {
128 if (strcmp(r->cmdargs[i], cmdargs[i]))
139 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
140 uid_t target, const char *cmd, const char **cmdargs)
145 for (i = 0; i < nrules; i++) {
146 if (match(uid, groups, ngroups, target, cmd, cmdargs, rules[i]))
151 return (*lastr)->action == PERMIT;
155 parseconfig(const char *filename, int checkperms)
158 extern int yyparse(void);
161 yyfp = fopen(filename, "r");
164 fprintf(stderr, "doas is not enabled.\n");
166 warn("could not open config file");
171 if (fstat(fileno(yyfp), &sb) != 0)
172 err(1, "fstat(\"%s\")", filename);
173 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
174 errx(1, "%s is writable by group or other", filename);
176 errx(1, "%s is not owned by root", filename);
186 copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
191 for (i = 0; i < nsafe; i++) {
192 const char **oe = oldenvp;
194 size_t len = strlen(safeset[i]);
195 if (strncmp(*oe, safeset[i], len) == 0 &&
197 if (!(envp[ei++] = strdup(*oe)))
208 copyenv(const char **oldenvp, struct rule *rule)
210 const char *safeset[] = {
211 "DISPLAY", "HOME", "LOGNAME", "MAIL",
212 "PATH", "TERM", "USER", "USERNAME",
215 const char *badset[] = {
225 nbad = arraylen(badset);
226 if ((rule->options & KEEPENV) && !rule->envlist) {
228 size_t oldlen = arraylen(oldenvp);
229 envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
231 err(1, "reallocarray");
232 for (ii = i = 0; i < oldlen; i++) {
234 for (j = 0; j < nbad; j++) {
235 size_t len = strlen(badset[j]);
236 if (strncmp(oldenvp[i], badset[j], len) == 0 &&
237 oldenvp[i][len] == '=') {
242 if (!(envp[ii] = strdup(oldenvp[i])))
251 nsafe = arraylen(safeset);
252 if ((extra = rule->envlist)) {
254 nextras = arraylen(extra);
255 for (i = 0; i < nsafe; i++) {
257 for (j = 0; j < nextras; j++) {
258 if (strcmp(extra[j], safeset[i]) == 0) {
259 extra[j--] = extra[nextras--];
260 extra[nextras] = NULL;
266 envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
268 err(1, "can't allocate new environment");
271 ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
272 ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
281 fprintf(stderr, "Permission denied\n");
286 checkconfig(const char *confpath, int argc, char **argv,
287 uid_t uid, gid_t *groups, int ngroups, uid_t target)
291 setresuid(uid, uid, uid);
292 parseconfig(confpath, 0);
296 if (permit(uid, groups, ngroups, &rule, target, argv[0],
297 (const char **)argv + 1)) {
298 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
307 main(int argc, char **argv, char **envp)
309 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
310 "/usr/local/bin:/usr/local/sbin";
311 const char *confpath = NULL;
312 char *shargv[] = { NULL, NULL };
315 char cmdline[LINE_MAX];
316 char myname[_PW_NAME_LEN + 1];
321 gid_t groups[NGROUPS_MAX + 1];
328 while ((ch = getopt(argc, argv, "C:nsu:")) != -1) {
334 if (parseuid(optarg, &target) != 0)
335 errx(1, "unknown user");
354 } else if ((!sflag && !argc) || (sflag && argc))
360 err(1, "getpwuid failed");
361 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
362 errx(1, "pw_name too long");
363 ngroups = getgroups(NGROUPS_MAX, groups);
365 err(1, "can't get groups");
366 groups[ngroups++] = getgid();
369 sh = getenv("SHELL");
370 if (sh == NULL || *sh == '\0')
371 shargv[0] = pw->pw_shell;
379 checkconfig(confpath, argc, argv, uid, groups, ngroups,
381 exit(1); /* fail safe */
384 parseconfig("/etc/doas.conf", 1);
386 /* cmdline is used only for logging, no need to abort on truncate */
387 (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
388 for (i = 1; i < argc; i++) {
389 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
391 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
396 if (!permit(uid, groups, ngroups, &rule, target, cmd,
397 (const char**)argv + 1)) {
398 syslog(LOG_AUTHPRIV | LOG_NOTICE,
399 "failed command for %s: %s", myname, cmdline);
403 if (!(rule->options & NOPASS)) {
405 errx(1, "Authorization required");
406 if (!auth_userokay(myname, NULL, NULL, NULL)) {
407 syslog(LOG_AUTHPRIV | LOG_NOTICE,
408 "failed password for %s", myname);
412 envp = copyenv((const char **)envp, rule);
414 pw = getpwuid(target);
416 errx(1, "no passwd entry for target");
417 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
418 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
420 errx(1, "failed to set user context for target");
422 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
423 myname, pw->pw_name, cmdline);
424 if (setenv("PATH", safepath, 1) == -1)
425 err(1, "failed to set PATH '%s'", safepath);
426 execvpe(cmd, argv, envp);
428 errx(1, "%s: command not found", cmd);