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>
46 fprintf(stderr, "usage: doas [-Lns] [-C config] [-u user]"
52 parseuid(const char *s, uid_t *uid)
57 if ((pw = getpwnam(s)) != NULL) {
63 *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
70 uidcheck(const char *s, uid_t desired)
74 if (parseuid(s, &uid) != 0)
82 parsegid(const char *s, gid_t *gid)
87 if ((gr = getgrnam(s)) != NULL) {
93 *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
100 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
101 const char **cmdargs, struct rule *r)
105 if (r->ident[0] == ':') {
107 if (parsegid(r->ident + 1, &rgid) == -1)
109 for (i = 0; i < ngroups; i++) {
110 if (rgid == groups[i])
116 if (uidcheck(r->ident, uid) != 0)
119 if (r->target && uidcheck(r->target, target) != 0)
122 if (strcmp(r->cmd, cmd))
125 /* if arguments were given, they should match explicitly */
126 for (i = 0; r->cmdargs[i]; i++) {
129 if (strcmp(r->cmdargs[i], cmdargs[i]))
140 permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
141 uid_t target, const char *cmd, const char **cmdargs)
146 for (i = 0; i < nrules; i++) {
147 if (match(uid, groups, ngroups, target, cmd,
153 return (*lastr)->action == PERMIT;
157 parseconfig(const char *filename, int checkperms)
160 extern int yyparse(void);
163 yyfp = fopen(filename, "r");
165 err(1, checkperms ? "doas is not enabled, %s" :
166 "could not open config file %s", filename);
169 if (fstat(fileno(yyfp), &sb) != 0)
170 err(1, "fstat(\"%s\")", filename);
171 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
172 errx(1, "%s is writable by group or other", filename);
174 errx(1, "%s is not owned by root", filename);
184 checkconfig(const char *confpath, int argc, char **argv,
185 uid_t uid, gid_t *groups, int ngroups, uid_t target)
187 const struct rule *rule;
189 if (setresuid(uid, uid, uid) != 0)
192 parseconfig(confpath, 0);
196 if (permit(uid, groups, ngroups, &rule, target, argv[0],
197 (const char **)argv + 1)) {
198 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
207 mygetpwuid_r(uid_t uid, struct passwd *pwd, struct passwd **result)
211 static long pwsz = 0;
217 pwsz = sysconf(_SC_GETPW_R_SIZE_MAX);
219 buflen = pwsz > 0 ? pwsz : 1024;
221 buf = malloc(buflen);
225 while ((rv = getpwuid_r(uid, pwd, buf, buflen, result)) == ERANGE) {
231 buf = realloc(buf, buflen);
245 printf("%s\n", getinsult());
248 errx(1, "Authentication failed");
252 main(int argc, char **argv)
254 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
255 "/usr/local/bin:/usr/local/sbin";
256 const char *confpath = NULL;
257 char *shargv[] = { NULL, NULL };
261 char cmdline[LINE_MAX];
262 struct passwd mypwstore, targpwstore;
263 struct passwd *mypw, *targpw;
264 const struct rule *rule;
267 gid_t groups[NGROUPS_MAX + 1];
272 char cwdpath[PATH_MAX];
276 if (argc <= 0 || argv == NULL || argv[0] == NULL) {
277 fprintf(stderr, "doas: executed without argv\n");
283 closefrom(STDERR_FILENO + 1);
287 while ((ch = getopt(argc, argv, "+C:Lnsu:")) != -1) {
293 #if defined(USE_TIMESTAMP)
294 exit(timestamp_clear() == -1);
299 if (parseuid(optarg, &target) != 0)
300 errx(1, "unknown user");
319 } else if ((!sflag && !argc) || (sflag && argc))
322 rv = mygetpwuid_r(uid, &mypwstore, &mypw);
324 err(1, "getpwuid_r failed");
326 errx(1, "no passwd entry for self");
327 ngroups = getgroups(NGROUPS_MAX, groups);
329 err(1, "can't get groups");
330 groups[ngroups++] = getgid();
333 sh = getenv("SHELL");
334 if (sh == NULL || *sh == '\0') {
335 shargv[0] = mypw->pw_shell;
343 checkconfig(confpath, argc, argv, uid, groups, ngroups,
345 exit(1); /* fail safe */
349 errx(1, "not installed setuid");
351 parseconfig(DOAS_CONF, 1);
353 /* cmdline is used only for logging, no need to abort on truncate */
354 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
355 for (i = 1; i < argc; i++) {
356 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
358 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
363 if (!permit(uid, groups, ngroups, &rule, target, cmd,
364 (const char **)argv + 1)) {
365 syslog(LOG_AUTHPRIV | LOG_NOTICE,
366 "command not permitted for %s: %s", mypw->pw_name, cmdline);
367 errc(1, EPERM, NULL);
370 if (!(rule->options & NOPASS)) {
372 errx(1, "Authentication required");
374 int ret = shadowauth(mypw->pw_name, rule->options & PERSIST);
376 authfail(rule->options & INSULT);
379 if ((p = getenv("PATH")) != NULL)
380 formerpath = strdup(p);
381 if (formerpath == NULL)
385 if (setenv("PATH", safepath, 1) == -1)
386 err(1, "failed to set PATH '%s'", safepath);
389 rv = mygetpwuid_r(target, &targpwstore, &targpw);
391 err(1, "getpwuid_r failed");
393 errx(1, "no passwd entry for target");
395 #ifdef HAVE_LOGIN_CAP_H
396 if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
398 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
400 errx(1, "failed to set user context for target");
402 if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
404 if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
405 err(1, "initgroups");
406 if (setresuid(target, target, target) != 0)
408 if (setenv("PATH", safepath, 1) == -1)
409 err(1, "failed to set PATH '%s'", safepath);
412 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
417 if (!(rule->options & NOLOG)) {
418 syslog(LOG_AUTHPRIV | LOG_INFO,
419 "%s ran command %s as %s from %s",
420 mypw->pw_name, cmdline, targpw->pw_name, cwd);
423 envp = prepenv(rule, mypw, targpw);
425 /* setusercontext set path for the next process, so reset it for us */
427 if (setenv("PATH", safepath, 1) == -1)
428 err(1, "failed to set PATH '%s'", safepath);
430 if (setenv("PATH", formerpath, 1) == -1)
431 err(1, "failed to set PATH '%s'", formerpath);
433 execvpe(cmd, argv, envp);
435 errx(1, "%s: command not found", cmd);