1 /* $OpenBSD: doas.c,v 1.15 2015/07/21 11:04:06 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] [-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)
158 extern int yyparse(void);
161 yyfp = fopen(filename, "r");
163 fprintf(stderr, "doas is not enabled.\n");
167 if (fstat(fileno(yyfp), &sb) != 0)
168 err(1, "fstat(\"%s\")", filename);
169 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
170 errx(1, "%s is writable by group or other", filename);
172 errx(1, "%s is not owned by root", filename);
179 copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
184 for (i = 0; i < nsafe; i++) {
185 const char **oe = oldenvp;
187 size_t len = strlen(safeset[i]);
188 if (strncmp(*oe, safeset[i], len) == 0 &&
190 if (!(envp[ei++] = strdup(*oe)))
201 copyenv(const char **oldenvp, struct rule *rule)
203 const char *safeset[] = {
204 "DISPLAY", "HOME", "LOGNAME", "MAIL",
205 "PATH", "TERM", "USER", "USERNAME",
208 const char *badset[] = {
218 nbad = arraylen(badset);
219 if ((rule->options & KEEPENV) && !rule->envlist) {
221 size_t oldlen = arraylen(oldenvp);
222 envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
224 err(1, "reallocarray");
225 for (ii = i = 0; i < oldlen; i++) {
227 for (j = 0; j < nbad; j++) {
228 size_t len = strlen(badset[j]);
229 if (strncmp(oldenvp[i], badset[j], len) == 0 &&
230 oldenvp[i][len] == '=') {
235 if (!(envp[ii] = strdup(oldenvp[i])))
244 nsafe = arraylen(safeset);
245 if ((extra = rule->envlist)) {
247 nextras = arraylen(extra);
248 for (i = 0; i < nsafe; i++) {
250 for (j = 0; j < nextras; j++) {
251 if (strcmp(extra[j], safeset[i]) == 0) {
252 extra[j--] = extra[nextras--];
253 extra[nextras] = NULL;
259 envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
261 err(1, "can't allocate new environment");
264 ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
265 ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
274 fprintf(stderr, "Permission denied\n");
279 main(int argc, char **argv, char **envp)
281 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
282 "/usr/local/bin:/usr/local/sbin";
283 char *shargv[] = { NULL, NULL };
286 char cmdline[LINE_MAX];
287 char myname[_PW_NAME_LEN + 1];
292 gid_t groups[NGROUPS_MAX + 1];
297 while ((ch = getopt(argc, argv, "C:su:")) != -1) {
304 if (parseuid(optarg, &target) != 0)
305 errx(1, "unknown user");
318 if ((!sflag && !argc) || (sflag && argc))
321 parseconfig("/etc/doas.conf");
326 err(1, "getpwuid failed");
327 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
328 errx(1, "pw_name too long");
329 ngroups = getgroups(NGROUPS_MAX, groups);
331 err(1, "can't get groups");
332 groups[ngroups++] = getgid();
335 sh = getenv("SHELL");
336 if (sh == NULL || *sh == '\0')
337 shargv[0] = pw->pw_shell;
345 if (strlcpy(cmdline, argv[0], sizeof(cmdline)) >= sizeof(cmdline))
346 errx(1, "command line too long");
347 for (i = 1; i < argc; i++) {
348 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
349 errx(1, "command line too long");
350 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
351 errx(1, "command line too long");
354 if (!permit(uid, groups, ngroups, &rule, target, cmd,
355 (const char**)argv + 1)) {
356 syslog(LOG_AUTHPRIV | LOG_NOTICE,
357 "failed command for %s: %s", myname, cmdline);
361 if (!(rule->options & NOPASS)) {
362 if (!auth_userokay(myname, NULL, NULL, NULL)) {
363 syslog(LOG_AUTHPRIV | LOG_NOTICE,
364 "failed password for %s", myname);
368 envp = copyenv((const char **)envp, rule);
370 pw = getpwuid(target);
372 errx(1, "no passwd entry for target");
373 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
374 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
376 errx(1, "failed to set user context for target");
378 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
379 myname, pw->pw_name, cmdline);
380 if (setenv("PATH", safepath, 1) == -1)
381 err(1, "failed to set PATH '%s'", safepath);
382 execvpe(cmd, argv, envp);
384 errx(1, "%s: command not found", cmd);