1 /* $OpenBSD: doas.c,v 1.52 2016/04/28 04:48:56 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.
20 #include <sys/types.h>
22 #include <sys/ioctl.h>
25 #ifdef HAVE_LOGIN_CAP_H
26 #include <login_cap.h>
45 fprintf(stderr, "usage: doas [-Lns] [-C config] [-u user]"
51 parseuid(const char *s, uid_t *uid)
56 if ((pw = getpwnam(s)) != NULL) {
62 *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
69 uidcheck(const char *s, uid_t desired)
73 if (parseuid(s, &uid) != 0)
81 parsegid(const char *s, gid_t *gid)
86 if ((gr = getgrnam(s)) != NULL) {
92 *gid = strtonum(s, 0, GID_MAX - 1, &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] == ':') {
106 if (parsegid(r->ident + 1, &rgid) == -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, const 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,
152 return (*lastr)->action == PERMIT;
156 parseconfig(const char *filename, int checkperms)
159 extern int yyparse(void);
162 yyfp = fopen(filename, "r");
164 err(1, checkperms ? "doas is not enabled, %s" :
165 "could not open config file %s", filename);
168 if (fstat(fileno(yyfp), &sb) != 0)
169 err(1, "fstat(\"%s\")", filename);
170 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
171 errx(1, "%s is writable by group or other", filename);
173 errx(1, "%s is not owned by root", filename);
183 checkconfig(const char *confpath, int argc, char **argv,
184 uid_t uid, gid_t *groups, int ngroups, uid_t target)
186 const struct rule *rule;
188 if (setresuid(uid, uid, uid) != 0)
191 parseconfig(confpath, 0);
195 if (permit(uid, groups, ngroups, &rule, target, argv[0],
196 (const char **)argv + 1)) {
197 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
206 mygetpwuid_r(uid_t uid, struct passwd *pwd, struct passwd **result)
210 static long pwsz = 0;
216 pwsz = sysconf(_SC_GETPW_R_SIZE_MAX);
218 buflen = pwsz > 0 ? pwsz : 1024;
220 buf = malloc(buflen);
224 while ((rv = getpwuid_r(uid, pwd, buf, buflen, result)) == ERANGE) {
230 buf = realloc(buf, buflen);
239 main(int argc, char **argv)
241 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
242 "/usr/local/bin:/usr/local/sbin";
243 const char *confpath = NULL;
244 char *shargv[] = { NULL, NULL };
248 char cmdline[LINE_MAX];
249 struct passwd mypwstore, targpwstore;
250 struct passwd *mypw, *targpw;
251 const struct rule *rule;
254 gid_t groups[NGROUPS_MAX + 1];
259 char cwdpath[PATH_MAX];
265 closefrom(STDERR_FILENO + 1);
269 while ((ch = getopt(argc, argv, "+C:Lnsu:")) != -1) {
275 #if defined(USE_TIMESTAMP)
276 exit(timestamp_clear() == -1);
281 if (parseuid(optarg, &target) != 0)
282 errx(1, "unknown user");
301 } else if ((!sflag && !argc) || (sflag && argc))
304 rv = mygetpwuid_r(uid, &mypwstore, &mypw);
306 err(1, "getpwuid_r failed");
308 errx(1, "no passwd entry for self");
309 ngroups = getgroups(NGROUPS_MAX, groups);
311 err(1, "can't get groups");
312 groups[ngroups++] = getgid();
315 sh = getenv("SHELL");
316 if (sh == NULL || *sh == '\0') {
317 shargv[0] = mypw->pw_shell;
325 checkconfig(confpath, argc, argv, uid, groups, ngroups,
327 exit(1); /* fail safe */
331 errx(1, "not installed setuid");
333 parseconfig(DOAS_CONF, 1);
335 /* cmdline is used only for logging, no need to abort on truncate */
336 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
337 for (i = 1; i < argc; i++) {
338 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
340 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
345 if (!permit(uid, groups, ngroups, &rule, target, cmd,
346 (const char **)argv + 1)) {
347 syslog(LOG_AUTHPRIV | LOG_NOTICE,
348 "command not permitted for %s: %s", mypw->pw_name, cmdline);
349 errc(1, EPERM, NULL);
352 #if defined(USE_SHADOW)
353 if (!(rule->options & NOPASS)) {
355 errx(1, "Authentication required");
357 shadowauth(mypw->pw_name, rule->options & PERSIST);
359 #elif !defined(USE_PAM)
360 /* no authentication provider, only allow NOPASS rules */
362 if (!(rule->options & NOPASS))
363 errx(1, "Authentication required");
366 if ((p = getenv("PATH")) != NULL)
367 formerpath = strdup(p);
368 if (formerpath == NULL)
372 if (setenv("PATH", safepath, 1) == -1)
373 err(1, "failed to set PATH '%s'", safepath);
376 rv = mygetpwuid_r(target, &targpwstore, &targpw);
378 err(1, "getpwuid_r failed");
380 errx(1, "no passwd entry for target");
383 pamauth(targpw->pw_name, mypw->pw_name, !nflag, rule->options & NOPASS,
384 rule->options & PERSIST);
387 #ifdef HAVE_LOGIN_CAP_H
388 if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
390 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
392 errx(1, "failed to set user context for target");
394 if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
396 if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
397 err(1, "initgroups");
398 if (setresuid(target, target, target) != 0)
400 if (setenv("PATH", safepath, 1) == -1)
401 err(1, "failed to set PATH '%s'", safepath);
404 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
409 if (!(rule->options & NOLOG)) {
410 syslog(LOG_AUTHPRIV | LOG_INFO,
411 "%s ran command %s as %s from %s",
412 mypw->pw_name, cmdline, targpw->pw_name, cwd);
415 envp = prepenv(rule, mypw, targpw);
417 /* setusercontext set path for the next process, so reset it for us */
419 if (setenv("PATH", safepath, 1) == -1)
420 err(1, "failed to set PATH '%s'", safepath);
422 if (setenv("PATH", formerpath, 1) == -1)
423 err(1, "failed to set PATH '%s'", formerpath);
425 execvpe(cmd, argv, envp);
427 errx(1, "%s: command not found", cmd);