1 /* $OpenBSD: doas.c,v 1.5 2015/07/16 22:11:01 nicm 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>
38 fprintf(stderr, "usage: doas [-u user] command [args]\n");
43 arraylen(const char **arr)
54 parseuid(const char *s, uid_t *uid)
59 if ((pw = getpwnam(s)) != NULL) {
63 *uid = strtonum(s, 0, UID_MAX, &errstr);
70 uidcheck(const char *s, uid_t desired)
74 if (parseuid(s, &uid) != 0)
82 strtogid(const char *s)
88 if ((gr = getgrnam(s)) != NULL)
90 gid = strtonum(s, 0, GID_MAX, &errstr);
97 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
102 if (r->ident[0] == ':') {
103 gid_t rgid = strtogid(r->ident + 1);
106 for (i = 0; i < ngroups; i++) {
107 if (rgid == groups[i])
113 if (uidcheck(r->ident, uid) != 0)
116 if (r->target && uidcheck(r->target, target) != 0)
118 if (r->cmd && strcmp(r->cmd, cmd) != 0)
124 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
125 uid_t target, const char *cmd)
130 for (i = 0; i < nrules; i++) {
131 if (match(uid, groups, ngroups, target, cmd, rules[i]))
136 return (*lastr)->action == PERMIT;
140 parseconfig(const char *filename)
143 extern int yyparse(void);
146 yyfp = fopen(filename, "r");
148 fprintf(stderr, "doas is not enabled.\n");
152 if (fstat(fileno(yyfp), &sb) != 0)
153 err(1, "fstat(\"%s\")", filename);
154 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
155 errx(1, "%s is writable by group or other", filename);
157 errx(1, "%s is not owned by root", filename);
164 copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
168 for (i = 0; i < nsafe; i++) {
169 const char **oe = oldenvp;
171 size_t len = strlen(safeset[i]);
172 if (strncmp(*oe, safeset[i], len) == 0 &&
174 if (!(envp[ei++] = strdup(*oe)))
185 copyenv(const char **oldenvp, struct rule *rule)
187 const char *safeset[] = {
188 "DISPLAY", "HOME", "LOGNAME", "MAIL", "SHELL",
189 "PATH", "TERM", "USER", "USERNAME",
199 if ((rule->options & KEEPENV) && !rule->envlist) {
200 j = arraylen(oldenvp);
201 envp = reallocarray(NULL, j + 1, sizeof(char *));
203 err(1, "reallocarray");
204 for (i = 0; i < j; i++) {
205 if (!(envp[i] = strdup(oldenvp[i])))
212 nsafe = arraylen(safeset);
213 if ((extra = rule->envlist)) {
214 nextras = arraylen(extra);
215 for (i = 0; i < nsafe; i++) {
216 for (j = 0; j < nextras; j++) {
217 if (strcmp(extra[j], safeset[i]) == 0) {
218 extra[j--] = extra[nextras--];
219 extra[nextras] = NULL;
225 envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
227 err(1, "can't allocate new environment");
230 ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
231 ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
240 fprintf(stderr, "Permission denied\n");
245 main(int argc, char **argv, char **envp)
249 uid_t uid, target = 0;
250 gid_t groups[NGROUPS_MAX + 1];
256 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
257 "/usr/local/bin:/usr/local/sbin";
259 parseconfig("/etc/doas.conf");
261 while ((ch = getopt(argc, argv, "u:")) != -1) {
264 if (parseuid(optarg, &target) != 0)
265 errx(1, "unknown user");
279 strlcpy(cmdline, argv[0], sizeof(cmdline));
280 for (i = 1; i < argc; i++) {
281 strlcat(cmdline, " ", sizeof(cmdline));
282 strlcat(cmdline, argv[i], sizeof(cmdline));
288 err(1, "getpwuid failed");
289 strlcpy(myname, pw->pw_name, sizeof(myname));
290 ngroups = getgroups(NGROUPS_MAX, groups);
292 err(1, "can't get groups");
293 groups[ngroups++] = getgid();
295 if (!permit(uid, groups, ngroups, &rule, target, cmd)) {
296 syslog(LOG_AUTHPRIV | LOG_NOTICE,
297 "failed command for %s: %s", myname, cmdline);
301 if (!(rule->options & NOPASS)) {
302 if (!auth_userokay(myname, NULL, NULL, NULL)) {
303 syslog(LOG_AUTHPRIV | LOG_NOTICE,
304 "failed password for %s", myname);
308 envp = copyenv((const char **)envp, rule);
310 pw = getpwuid(target);
312 errx(1, "no passwd entry for target");
313 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
314 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
316 errx(1, "failed to set user context for target");
318 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
319 myname, pw->pw_name, cmdline);
320 setenv("PATH", safepath, 1);
321 execvpe(cmd, argv, envp);