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>
23 #ifdef HAVE_LOGIN_CAP_H
24 #include <login_cap.h>
43 fprintf(stderr, "usage: doas [-Lns] [-C config] [-u user]"
49 parseuid(const char *s, uid_t *uid)
54 if ((pw = getpwnam(s)) != NULL) {
60 *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
67 uidcheck(const char *s, uid_t desired)
71 if (parseuid(s, &uid) != 0)
79 parsegid(const char *s, gid_t *gid)
84 if ((gr = getgrnam(s)) != NULL) {
90 *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
97 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
98 const char **cmdargs, struct rule *r)
102 if (r->ident[0] == ':') {
104 if (parsegid(r->ident + 1, &rgid) == -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)
119 if (strcmp(r->cmd, cmd))
122 /* if arguments were given, they should match explicitly */
123 for (i = 0; r->cmdargs[i]; i++) {
126 if (strcmp(r->cmdargs[i], cmdargs[i]))
137 permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
138 uid_t target, const char *cmd, const char **cmdargs)
143 for (i = 0; i < nrules; i++) {
144 if (match(uid, groups, ngroups, target, cmd,
150 return (*lastr)->action == PERMIT;
154 parseconfig(const char *filename, int checkperms)
157 extern int yyparse(void);
160 yyfp = fopen(filename, "r");
162 err(1, checkperms ? "doas is not enabled, %s" :
163 "could not open config file %s", filename);
166 if (fstat(fileno(yyfp), &sb) != 0)
167 err(1, "fstat(\"%s\")", filename);
168 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
169 errx(1, "%s is writable by group or other", filename);
171 errx(1, "%s is not owned by root", filename);
181 checkconfig(const char *confpath, int argc, char **argv,
182 uid_t uid, gid_t *groups, int ngroups, uid_t target)
184 const struct rule *rule;
186 if (setresuid(uid, uid, uid) != 0)
189 parseconfig(confpath, 0);
193 if (permit(uid, groups, ngroups, &rule, target, argv[0],
194 (const char **)argv + 1)) {
195 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
204 mygetpwuid_r(uid_t uid, struct passwd *pwd, struct passwd **result)
208 static long pwsz = 0;
214 pwsz = sysconf(_SC_GETPW_R_SIZE_MAX);
216 buflen = pwsz > 0 ? pwsz : 1024;
218 buf = malloc(buflen);
222 while ((rv = getpwuid_r(uid, pwd, buf, buflen, result)) == ERANGE) {
228 buf = realloc(buf, buflen);
237 main(int argc, char **argv)
239 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
240 "/usr/local/bin:/usr/local/sbin";
241 const char *confpath = NULL;
242 char *shargv[] = { NULL, NULL };
246 char cmdline[LINE_MAX];
247 struct passwd mypwstore, targpwstore;
248 struct passwd *mypw, *targpw;
249 const struct rule *rule;
252 gid_t groups[NGROUPS_MAX + 1];
257 char cwdpath[PATH_MAX];
263 closefrom(STDERR_FILENO + 1);
267 while ((ch = getopt(argc, argv, "+C:Lnsu:")) != -1) {
273 #if defined(USE_TIMESTAMP)
274 exit(timestamp_clear() == -1);
279 if (parseuid(optarg, &target) != 0)
280 errx(1, "unknown user");
299 } else if ((!sflag && !argc) || (sflag && argc))
302 rv = mygetpwuid_r(uid, &mypwstore, &mypw);
304 err(1, "getpwuid_r failed");
306 errx(1, "no passwd entry for self");
307 ngroups = getgroups(NGROUPS_MAX, groups);
309 err(1, "can't get groups");
310 groups[ngroups++] = getgid();
313 sh = getenv("SHELL");
314 if (sh == NULL || *sh == '\0') {
315 shargv[0] = mypw->pw_shell;
323 checkconfig(confpath, argc, argv, uid, groups, ngroups,
325 exit(1); /* fail safe */
329 errx(1, "not installed setuid");
331 parseconfig(DOAS_CONF, 1);
333 /* cmdline is used only for logging, no need to abort on truncate */
334 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
335 for (i = 1; i < argc; i++) {
336 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
338 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
343 if (!permit(uid, groups, ngroups, &rule, target, cmd,
344 (const char **)argv + 1)) {
345 syslog(LOG_AUTHPRIV | LOG_NOTICE,
346 "command not permitted for %s: %s", mypw->pw_name, cmdline);
347 errc(1, EPERM, NULL);
350 #if defined(USE_SHADOW)
351 if (!(rule->options & NOPASS)) {
353 errx(1, "Authorization required");
355 shadowauth(mypw->pw_name, rule->options & PERSIST);
357 #elif !defined(USE_PAM)
358 /* no authentication provider, only allow NOPASS rules */
360 if (!(rule->options & NOPASS))
361 errx(1, "Authorization required");
364 if ((p = getenv("PATH")) != NULL)
365 formerpath = strdup(p);
366 if (formerpath == NULL)
370 if (setenv("PATH", safepath, 1) == -1)
371 err(1, "failed to set PATH '%s'", safepath);
374 rv = mygetpwuid_r(target, &targpwstore, &targpw);
376 err(1, "getpwuid_r failed");
378 errx(1, "no passwd entry for target");
381 pamauth(targpw->pw_name, mypw->pw_name, !nflag, rule->options & NOPASS,
382 rule->options & PERSIST);
385 #ifdef HAVE_LOGIN_CAP_H
386 if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
387 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
389 errx(1, "failed to set user context for target");
391 if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
393 if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
394 err(1, "initgroups");
395 if (setresuid(target, target, target) != 0)
399 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
404 if (!(rule->options & NOLOG)) {
405 syslog(LOG_AUTHPRIV | LOG_INFO,
406 "%s ran command %s as %s from %s",
407 mypw->pw_name, cmdline, targpw->pw_name, cwd);
410 envp = prepenv(rule, mypw, targpw);
412 /* setusercontext set path for the next process, so reset it for us */
414 if (setenv("PATH", safepath, 1) == -1)
415 err(1, "failed to set PATH '%s'", safepath);
417 if (setenv("PATH", formerpath, 1) == -1)
418 err(1, "failed to set PATH '%s'", formerpath);
420 execvpe(cmd, argv, envp);
422 errx(1, "%s: command not found", cmd);