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>
39 fprintf(stderr, "doas: version %s built %s\n", VERSION, __DATE__);
46 fprintf(stderr, "usage: doas [-nsv] [-a style] [-C config] [-u user]"
52 arraylen(const char **arr)
66 parseuid(const char *s, uid_t *uid)
71 if ((pw = getpwnam(s)) != NULL) {
75 *uid = strtonum(s, 0, UID_MAX, &errstr);
82 uidcheck(const char *s, uid_t desired)
86 if (parseuid(s, &uid) != 0)
94 parsegid(const char *s, gid_t *gid)
99 if ((gr = getgrnam(s)) != NULL) {
103 *gid = strtonum(s, 0, GID_MAX, &errstr);
110 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
111 const char **cmdargs, struct rule *r)
115 if (r->ident[0] == ':') {
117 if (parsegid(r->ident + 1, &rgid) == -1)
119 for (i = 0; i < ngroups; i++) {
120 if (rgid == groups[i])
126 if (uidcheck(r->ident, uid) != 0)
129 if (r->target && uidcheck(r->target, target) != 0)
132 if (strcmp(r->cmd, cmd))
135 /* if arguments were given, they should match explicitly */
136 for (i = 0; r->cmdargs[i]; i++) {
139 if (strcmp(r->cmdargs[i], cmdargs[i]))
150 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
151 uid_t target, const char *cmd, const char **cmdargs)
156 for (i = 0; i < nrules; i++) {
157 if (match(uid, groups, ngroups, target, cmd,
163 return (*lastr)->action == PERMIT;
167 parseconfig(const char *filename, int checkperms)
170 extern int yyparse(void);
173 yyfp = fopen(filename, "r");
175 err(1, checkperms ? "doas is not enabled, %s" :
176 "could not open config file %s", filename);
179 if (fstat(fileno(yyfp), &sb) != 0)
180 err(1, "fstat(\"%s\")", filename);
181 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
182 errx(1, "%s is writable by group or other", filename);
184 errx(1, "%s is not owned by root", filename);
194 checkconfig(const char *confpath, int argc, char **argv,
195 uid_t uid, gid_t *groups, int ngroups, uid_t target)
199 if (setresuid(uid, uid, uid) != 0)
202 parseconfig(confpath, 0);
206 if (permit(uid, groups, ngroups, &rule, target, argv[0],
207 (const char **)argv + 1)) {
208 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
217 main(int argc, char **argv)
219 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
220 "/usr/local/bin:/usr/local/sbin";
221 const char *confpath = NULL;
222 char *shargv[] = { NULL, NULL };
225 char cmdline[LINE_MAX];
226 char myname[_PW_NAME_LEN + 1];
231 gid_t groups[NGROUPS_MAX + 1];
237 char cwdpath[PATH_MAX];
240 #ifdef HAVE_BSD_AUTH_H
241 char *login_style = NULL;
246 if (pledge("stdio rpath getpw tty proc exec id", NULL) == -1)
249 /* closefrom(STDERR_FILENO + 1); */
253 #ifdef HAVE_BSD_AUTH_H
254 # define OPTSTRING "a:C:nsu:v"
256 # define OPTSTRING "C:nsu:v"
259 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
261 #ifdef HAVE_BSD_AUTH_H
263 login_style = optarg;
270 if (parseuid(optarg, &target) != 0)
271 errx(1, "unknown user");
296 } else if ((!sflag && !argc) || (sflag && argc))
301 err(1, "getpwuid failed");
302 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
303 errx(1, "pw_name too long");
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] = pw->pw_shell;
320 checkconfig(confpath, argc, argv, uid, groups, ngroups,
322 exit(1); /* fail safe */
325 parseconfig("/etc/doas.conf", 1);
327 /* cmdline is used only for logging, no need to abort on truncate */
328 (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
329 for (i = 1; i < argc; i++) {
330 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
332 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
337 if (!permit(uid, groups, ngroups, &rule, target, cmd,
338 (const char**)argv + 1)) {
339 syslog(LOG_AUTHPRIV | LOG_NOTICE,
340 "failed command for %s: %s", myname, cmdline);
341 errc(1, EPERM, NULL);
344 pw = getpwuid(target);
346 errx(1, "no passwd entry for target");
348 #ifdef HAVE_BSD_AUTH_H
349 if (!(rule->options & NOPASS)) {
351 errx(1, "Authorization required");
353 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
356 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
358 errx(1, "Authorization failed");
360 char host[HOST_NAME_MAX + 1];
361 if (gethostname(host, sizeof(host)))
362 snprintf(host, sizeof(host), "?");
363 snprintf(cbuf, sizeof(cbuf),
364 "\rdoas (%.32s@%.32s) password: ", myname, host);
367 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
369 if (response == NULL && errno == ENOTTY) {
370 syslog(LOG_AUTHPRIV | LOG_NOTICE,
371 "tty required for %s", myname);
372 errx(1, "a tty is required");
374 if (!auth_userresponse(as, response, 0)) {
375 syslog(LOG_AUTHPRIV | LOG_NOTICE,
376 "failed auth for %s", myname);
377 errc(1, EPERM, NULL);
379 explicit_bzero(rbuf, sizeof(rbuf));
381 #elif HAVE_PAM_APPL_H
382 if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
383 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
384 errc(1, EPERM, NULL);
387 if (!(rule->options & NOPASS)) {
388 errx(1, "Authorization required");
389 #endif /* HAVE_BSD_AUTH_H */
391 if (pledge("stdio rpath getpw exec id", NULL) == -1)
394 #ifdef HAVE_BSD_AUTH_H
395 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
396 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
398 errx(1, "failed to set user context for target");
400 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
402 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
403 errx(1, "initgroups");
404 if (setresuid(target, target, target) != 0)
408 if (pledge("stdio rpath exec", NULL) == -1)
411 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
416 if (pledge("stdio exec", NULL) == -1)
419 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
420 myname, cmdline, pw->pw_name, cwd);
422 envp = prepenv(rule);
425 if (setenv("PATH", safepath, 1) == -1)
426 err(1, "failed to set PATH '%s'", safepath);
428 execvpe(cmd, argv, envp);
430 errx(1, "%s: command not found", cmd);