1 /* $OpenBSD: doas.c,v 1.11 2015/07/20 00:54:01 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>
22 #include <login_cap.h>
39 fprintf(stderr, "usage: doas [-s] [-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,
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)
120 if (r->cmd && strcmp(r->cmd, cmd) != 0)
126 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
127 uid_t target, const char *cmd)
132 for (i = 0; i < nrules; i++) {
133 if (match(uid, groups, ngroups, target, cmd, rules[i]))
138 return (*lastr)->action == PERMIT;
142 parseconfig(const char *filename)
145 extern int yyparse(void);
148 yyfp = fopen(filename, "r");
150 fprintf(stderr, "doas is not enabled.\n");
154 if (fstat(fileno(yyfp), &sb) != 0)
155 err(1, "fstat(\"%s\")", filename);
156 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
157 errx(1, "%s is writable by group or other", filename);
159 errx(1, "%s is not owned by root", filename);
166 copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
171 for (i = 0; i < nsafe; i++) {
172 const char **oe = oldenvp;
174 size_t len = strlen(safeset[i]);
175 if (strncmp(*oe, safeset[i], len) == 0 &&
177 if (!(envp[ei++] = strdup(*oe)))
188 copyenv(const char **oldenvp, struct rule *rule)
190 const char *safeset[] = {
191 "DISPLAY", "HOME", "LOGNAME", "MAIL", "SHELL",
192 "PATH", "TERM", "USER", "USERNAME",
195 const char *badset[] = {
205 nbad = arraylen(badset);
206 if ((rule->options & KEEPENV) && !rule->envlist) {
208 size_t oldlen = arraylen(oldenvp);
209 envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
211 err(1, "reallocarray");
212 for (ii = i = 0; i < oldlen; i++) {
214 for (j = 0; j < nbad; j++) {
215 size_t len = strlen(badset[j]);
216 if (strncmp(oldenvp[i], badset[j], len) == 0) {
221 if (!(envp[ii] = strdup(oldenvp[i])))
230 nsafe = arraylen(safeset);
231 if ((extra = rule->envlist)) {
233 nextras = arraylen(extra);
234 for (i = 0; i < nsafe; i++) {
236 for (j = 0; j < nextras; j++) {
237 if (strcmp(extra[j], safeset[i]) == 0) {
238 extra[j--] = extra[nextras--];
239 extra[nextras] = NULL;
245 envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
247 err(1, "can't allocate new environment");
250 ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
251 ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
260 fprintf(stderr, "Permission denied\n");
265 main(int argc, char **argv, char **envp)
267 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
268 "/usr/local/bin:/usr/local/sbin";
269 char *shargv[] = { NULL, NULL };
272 char cmdline[LINE_MAX];
273 char myname[_PW_NAME_LEN + 1];
278 gid_t groups[NGROUPS_MAX + 1];
283 parseconfig("/etc/doas.conf");
285 while ((ch = getopt(argc, argv, "su:")) != -1) {
288 if (parseuid(optarg, &target) != 0)
289 errx(1, "unknown user");
302 if ((!sflag && !argc) || (sflag && argc))
308 err(1, "getpwuid failed");
309 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
310 errx(1, "pw_name too long");
311 ngroups = getgroups(NGROUPS_MAX, groups);
313 err(1, "can't get groups");
314 groups[ngroups++] = getgid();
317 sh = getenv("SHELL");
318 if (sh == NULL || *sh == '\0')
319 shargv[0] = pw->pw_shell;
327 if (strlcpy(cmdline, argv[0], sizeof(cmdline)) >= sizeof(cmdline))
328 errx(1, "command line too long");
329 for (i = 1; i < argc; i++) {
330 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
331 errx(1, "command line too long");
332 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
333 errx(1, "command line too long");
336 if (!permit(uid, groups, ngroups, &rule, target, cmd)) {
337 syslog(LOG_AUTHPRIV | LOG_NOTICE,
338 "failed command for %s: %s", myname, cmdline);
342 if (!(rule->options & NOPASS)) {
343 if (!auth_userokay(myname, NULL, NULL, NULL)) {
344 syslog(LOG_AUTHPRIV | LOG_NOTICE,
345 "failed password for %s", myname);
349 envp = copyenv((const char **)envp, rule);
351 pw = getpwuid(target);
353 errx(1, "no passwd entry for target");
354 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
355 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
357 errx(1, "failed to set user context for target");
359 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
360 myname, pw->pw_name, cmdline);
361 if (setenv("PATH", safepath, 1) == -1)
362 err(1, "failed to set PATH '%s'", safepath);
363 execvpe(cmd, argv, envp);
365 errx(1, "%s: command not found", cmd);