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.
18 #include <sys/types.h>
20 #include <sys/ioctl.h>
40 fprintf(stderr, "usage: doas [-Lns] [-C config] [-u user]"
46 parseuid(const char *s, uid_t *uid)
51 if ((pw = getpwnam(s)) != NULL) {
57 *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
64 uidcheck(const char *s, uid_t desired)
68 if (parseuid(s, &uid) != 0)
76 parsegid(const char *s, gid_t *gid)
81 if ((gr = getgrnam(s)) != NULL) {
87 *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
94 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
95 const char **cmdargs, struct rule *r)
99 if (r->ident[0] == ':') {
101 if (parsegid(r->ident + 1, &rgid) == -1)
103 for (i = 0; i < ngroups; i++) {
104 if (rgid == groups[i])
110 if (uidcheck(r->ident, uid) != 0)
113 if (r->target && uidcheck(r->target, target) != 0)
116 if (strcmp(r->cmd, cmd))
119 /* if arguments were given, they should match explicitly */
120 for (i = 0; r->cmdargs[i]; i++) {
123 if (strcmp(r->cmdargs[i], cmdargs[i]))
134 permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
135 uid_t target, const char *cmd, const char **cmdargs)
140 for (i = 0; i < nrules; i++) {
141 if (match(uid, groups, ngroups, target, cmd,
147 return (*lastr)->action == PERMIT;
151 parseconfig(const char *filename, int checkperms)
154 extern int yyparse(void);
157 yyfp = fopen(filename, "r");
159 err(1, checkperms ? "doas is not enabled, %s" :
160 "could not open config file %s", filename);
163 if (fstat(fileno(yyfp), &sb) != 0)
164 err(1, "fstat(\"%s\")", filename);
165 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
166 errx(1, "%s is writable by group or other", filename);
168 errx(1, "%s is not owned by root", filename);
178 checkconfig(const char *confpath, int argc, char **argv,
179 uid_t uid, gid_t *groups, int ngroups, uid_t target)
181 const struct rule *rule;
183 if (setresuid(uid, uid, uid) != 0)
186 parseconfig(confpath, 0);
190 if (permit(uid, groups, ngroups, &rule, target, argv[0],
191 (const char **)argv + 1)) {
192 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
201 mygetpwuid_r(uid_t uid, struct passwd *pwd, struct passwd **result)
205 static long pwsz = 0;
211 pwsz = sysconf(_SC_GETPW_R_SIZE_MAX);
213 buflen = pwsz > 0 ? pwsz : 1024;
215 buf = malloc(buflen);
219 while ((rv = getpwuid_r(uid, pwd, buf, buflen, result)) == ERANGE) {
225 buf = realloc(buf, buflen);
234 main(int argc, char **argv)
236 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
237 "/usr/local/bin:/usr/local/sbin";
238 const char *confpath = NULL;
239 char *shargv[] = { NULL, NULL };
243 char cmdline[LINE_MAX];
244 struct passwd mypwstore, targpwstore;
245 struct passwd *mypw, *targpw;
246 const struct rule *rule;
249 gid_t groups[NGROUPS_MAX + 1];
254 char cwdpath[PATH_MAX];
260 closefrom(STDERR_FILENO + 1);
264 while ((ch = getopt(argc, argv, "+C:Lnsu:")) != -1) {
270 #if defined(USE_TIMESTAMP)
271 exit(timestamp_clear() == -1);
276 if (parseuid(optarg, &target) != 0)
277 errx(1, "unknown user");
296 } else if ((!sflag && !argc) || (sflag && argc))
299 rv = mygetpwuid_r(uid, &mypwstore, &mypw);
301 err(1, "getpwuid_r failed");
303 errx(1, "no passwd entry for self");
304 ngroups = getgroups(NGROUPS_MAX, groups);
306 err(1, "can't get groups");
307 groups[ngroups++] = getgid();
310 sh = getenv("SHELL");
311 if (sh == NULL || *sh == '\0') {
312 shargv[0] = mypw->pw_shell;
320 checkconfig(confpath, argc, argv, uid, groups, ngroups,
322 exit(1); /* fail safe */
326 errx(1, "not installed setuid");
328 parseconfig(DOAS_CONF, 1);
330 /* cmdline is used only for logging, no need to abort on truncate */
331 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
332 for (i = 1; i < argc; i++) {
333 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
335 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
340 if (!permit(uid, groups, ngroups, &rule, target, cmd,
341 (const char **)argv + 1)) {
342 syslog(LOG_AUTHPRIV | LOG_NOTICE,
343 "command not permitted for %s: %s", mypw->pw_name, cmdline);
344 errc(1, EPERM, NULL);
347 #if defined(USE_SHADOW)
348 if (!(rule->options & NOPASS)) {
350 errx(1, "Authorization required");
352 shadowauth(mypw->pw_name, rule->options & PERSIST);
354 #elif !defined(USE_PAM)
355 /* no authentication provider, only allow NOPASS rules */
357 if (!(rule->options & NOPASS))
358 errx(1, "Authorization required");
361 if ((p = getenv("PATH")) != NULL)
362 formerpath = strdup(p);
363 if (formerpath == NULL)
367 if (setenv("PATH", safepath, 1) == -1)
368 err(1, "failed to set PATH '%s'", safepath);
371 rv = mygetpwuid_r(target, &targpwstore, &targpw);
373 err(1, "getpwuid_r failed");
375 errx(1, "no passwd entry for target");
378 pamauth(targpw->pw_name, mypw->pw_name, !nflag, rule->options & NOPASS,
379 rule->options & PERSIST);
382 if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
384 if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
385 err(1, "initgroups");
386 if (setresuid(target, target, target) != 0)
389 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
394 if (!(rule->options & NOLOG)) {
395 syslog(LOG_AUTHPRIV | LOG_INFO,
396 "%s ran command %s as %s from %s",
397 mypw->pw_name, cmdline, targpw->pw_name, cwd);
400 envp = prepenv(rule, mypw, targpw);
402 /* setusercontext set path for the next process, so reset it for us */
404 if (setenv("PATH", safepath, 1) == -1)
405 err(1, "failed to set PATH '%s'", safepath);
407 if (setenv("PATH", formerpath, 1) == -1)
408 err(1, "failed to set PATH '%s'", formerpath);
410 execvpe(cmd, argv, envp);
412 errx(1, "%s: command not found", cmd);