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;
209 pwsz = sysconf(_SC_GETPW_R_SIZE_MAX);
211 buflen = pwsz > 0 ? pwsz : 1024;
213 buf = malloc(buflen);
217 while ((rv = getpwuid_r(uid, pwd, buf, buflen, result)) == ERANGE) {
223 buf = realloc(buf, buflen);
232 main(int argc, char **argv)
234 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
235 "/usr/local/bin:/usr/local/sbin";
236 const char *confpath = NULL;
237 char *shargv[] = { NULL, NULL };
241 char cmdline[LINE_MAX];
242 struct passwd mypwstore, targpwstore;
243 struct passwd *mypw, *targpw;
244 const struct rule *rule;
247 gid_t groups[NGROUPS_MAX + 1];
252 char cwdpath[PATH_MAX];
258 closefrom(STDERR_FILENO + 1);
262 while ((ch = getopt(argc, argv, "+C:Lnsu:")) != -1) {
268 #if defined(USE_TIMESTAMP)
269 exit(timestamp_clear() == -1);
274 if (parseuid(optarg, &target) != 0)
275 errx(1, "unknown user");
294 } else if ((!sflag && !argc) || (sflag && argc))
297 rv = mygetpwuid_r(uid, &mypwstore, &mypw);
299 err(1, "getpwuid_r failed");
301 errx(1, "no passwd entry for self");
302 ngroups = getgroups(NGROUPS_MAX, groups);
304 err(1, "can't get groups");
305 groups[ngroups++] = getgid();
308 sh = getenv("SHELL");
309 if (sh == NULL || *sh == '\0') {
310 shargv[0] = mypw->pw_shell;
318 checkconfig(confpath, argc, argv, uid, groups, ngroups,
320 exit(1); /* fail safe */
324 errx(1, "not installed setuid");
326 parseconfig("/etc/doas.conf", 1);
328 /* cmdline is used only for logging, no need to abort on truncate */
329 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
330 for (i = 1; i < argc; i++) {
331 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
333 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
338 if (!permit(uid, groups, ngroups, &rule, target, cmd,
339 (const char **)argv + 1)) {
340 syslog(LOG_AUTHPRIV | LOG_NOTICE,
341 "failed command for %s: %s", mypw->pw_name, cmdline);
342 errc(1, EPERM, NULL);
345 #if defined(USE_SHADOW)
346 if (!(rule->options & NOPASS)) {
348 errx(1, "Authorization required");
350 shadowauth(mypw->pw_name, rule->options & PERSIST);
352 #elif !defined(USE_PAM)
353 /* no authentication provider, only allow NOPASS rules */
355 if (!(rule->options & NOPASS))
356 errx(1, "Authorization required");
359 if ((p = getenv("PATH")) != NULL)
360 formerpath = strdup(p);
361 if (formerpath == NULL)
365 if (setenv("PATH", safepath, 1) == -1)
366 err(1, "failed to set PATH '%s'", safepath);
369 rv = mygetpwuid_r(target, &targpwstore, &targpw);
371 err(1, "getpwuid_r failed");
373 errx(1, "no passwd entry for target");
376 pamauth(targpw->pw_name, mypw->pw_name, !nflag, rule->options & NOPASS,
377 rule->options & PERSIST);
380 if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
382 if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
383 err(1, "initgroups");
384 if (setresuid(target, target, target) != 0)
387 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
392 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
393 mypw->pw_name, cmdline, targpw->pw_name, cwd);
395 envp = prepenv(rule, mypw, targpw);
397 /* setusercontext set path for the next process, so reset it for us */
399 if (setenv("PATH", safepath, 1) == -1)
400 err(1, "failed to set PATH '%s'", safepath);
402 if (setenv("PATH", formerpath, 1) == -1)
403 err(1, "failed to set PATH '%s'", formerpath);
405 execvpe(cmd, argv, envp);
407 errx(1, "%s: command not found", cmd);