1 /* $OpenBSD: doas.c,v 1.33 2015/07/30 17:04:33 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>
40 fprintf(stderr, "doas: version %s built %s\n", VERSION, __DATE__);
47 fprintf(stderr, "usage: doas [-nsv] [-C config] [-u user] command [args]\n");
52 arraylen(const char **arr)
66 parseuid(const char *s, uid_t *uid)
71 if ((pw = getpwnam(s)) != NULL) {
75 *uid = strtonum(s, 0, UID_MAX, &errstr);
82 uidcheck(const char *s, uid_t desired)
86 if (parseuid(s, &uid) != 0)
94 parsegid(const char *s, gid_t *gid)
99 if ((gr = getgrnam(s)) != NULL) {
103 *gid = strtonum(s, 0, GID_MAX, &errstr);
110 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
111 const char **cmdargs, struct rule *r)
115 if (r->ident[0] == ':') {
117 if (parsegid(r->ident + 1, &rgid) == -1)
119 for (i = 0; i < ngroups; i++) {
120 if (rgid == groups[i])
126 if (uidcheck(r->ident, uid) != 0)
129 if (r->target && uidcheck(r->target, target) != 0)
132 if (strcmp(r->cmd, cmd))
135 /* if arguments were given, they should match explicitly */
136 for (i = 0; r->cmdargs[i]; i++) {
139 if (strcmp(r->cmdargs[i], cmdargs[i]))
150 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
151 uid_t target, const char *cmd, const char **cmdargs)
156 for (i = 0; i < nrules; i++) {
157 if (match(uid, groups, ngroups, target, cmd,
163 return (*lastr)->action == PERMIT;
167 parseconfig(const char *filename, int checkperms)
170 extern int yyparse(void);
173 yyfp = fopen(filename, "r");
175 warn("could not open config file");
180 if (fstat(fileno(yyfp), &sb) != 0)
181 err(1, "fstat(\"%s\")", filename);
182 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
183 errx(1, "%s is writable by group or other", filename);
185 errx(1, "%s is not owned by root", filename);
195 * Copy the environment variables in safeset from oldenvp to envp.
198 copyenvhelper(const char **oldenvp, const char **safeset, size_t nsafe,
203 for (i = 0; i < nsafe; i++) {
204 const char **oe = oldenvp;
206 size_t len = strlen(safeset[i]);
207 if (strncmp(*oe, safeset[i], len) == 0 &&
209 if (!(envp[ei++] = strdup(*oe)))
220 copyenv(const char **oldenvp, struct rule *rule)
222 const char *safeset[] = {
223 "DISPLAY", "HOME", "LOGNAME", "MAIL",
224 "PATH", "TERM", "USER", "USERNAME",
227 const char *badset[] = {
237 /* if there was no envvar whitelist, pass all except badset ones */
238 nbad = arraylen(badset);
239 if ((rule->options & KEEPENV) && !rule->envlist) {
241 size_t oldlen = arraylen(oldenvp);
242 envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
244 err(1, "reallocarray");
245 for (inew = iold = 0; iold < oldlen; iold++) {
247 for (ibad = 0; ibad < nbad; ibad++) {
248 size_t len = strlen(badset[ibad]);
249 if (strncmp(oldenvp[iold], badset[ibad], len) == 0 &&
250 oldenvp[iold][len] == '=') {
255 if (!(envp[inew] = strdup(oldenvp[iold])))
264 nsafe = arraylen(safeset);
265 if ((extra = rule->envlist)) {
267 nextras = arraylen(extra);
268 for (isafe = 0; isafe < nsafe; isafe++) {
270 for (iextras = 0; iextras < nextras; iextras++) {
271 if (strcmp(extra[iextras], safeset[isafe]) == 0) {
273 extra[iextras] = extra[nextras];
274 extra[nextras] = NULL;
281 envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
283 err(1, "can't allocate new environment");
286 ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
287 ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
296 fprintf(stderr, "Permission denied\n");
301 checkconfig(const char *confpath, int argc, char **argv,
302 uid_t uid, gid_t *groups, int ngroups, uid_t target)
306 setresuid(uid, uid, uid);
307 parseconfig(confpath, 0);
311 if (permit(uid, groups, ngroups, &rule, target, argv[0],
312 (const char **)argv + 1)) {
313 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
322 main(int argc, char **argv, char **envp)
324 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
325 "/usr/local/bin:/usr/local/sbin";
326 const char *confpath = NULL;
327 char *shargv[] = { NULL, NULL };
330 char cmdline[LINE_MAX];
331 char myname[_PW_NAME_LEN + 1];
336 gid_t groups[NGROUPS_MAX + 1];
345 while ((ch = getopt(argc, argv, "C:nsu:v")) != -1) {
351 if (parseuid(optarg, &target) != 0)
352 errx(1, "unknown user");
377 } else if ((!sflag && !argc) || (sflag && argc))
382 err(1, "getpwuid failed");
383 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
384 errx(1, "pw_name too long");
385 ngroups = getgroups(NGROUPS_MAX, groups);
387 err(1, "can't get groups");
388 groups[ngroups++] = getgid();
391 sh = getenv("SHELL");
392 if (sh == NULL || *sh == '\0')
393 shargv[0] = pw->pw_shell;
401 checkconfig(confpath, argc, argv, uid, groups, ngroups,
403 exit(1); /* fail safe */
406 parseconfig("/etc/doas.conf", 1);
408 /* cmdline is used only for logging, no need to abort on truncate */
409 (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
410 for (i = 1; i < argc; i++) {
411 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
413 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
418 if (!permit(uid, groups, ngroups, &rule, target, cmd,
419 (const char**)argv + 1)) {
420 syslog(LOG_AUTHPRIV | LOG_NOTICE,
421 "failed command for %s: %s", myname, cmdline);
425 if (!(rule->options & NOPASS)) {
427 errx(1, "Authorization required");
428 if (!auth_userokay(myname, NULL, NULL, NULL)) {
429 syslog(LOG_AUTHPRIV | LOG_NOTICE,
430 "failed password for %s", myname);
434 envp = copyenv((const char **)envp, rule);
436 pw = getpwuid(target);
438 errx(1, "no passwd entry for target");
439 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
440 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
442 errx(1, "failed to set user context for target");
444 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
445 myname, pw->pw_name, cmdline);
446 if (setenv("PATH", safepath, 1) == -1)
447 err(1, "failed to set PATH '%s'", safepath);
448 execvpe(cmd, argv, envp);
450 errx(1, "%s: command not found", cmd);