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>
24 # include <login_cap.h>
25 # include <readpassphrase.h>
44 fprintf(stderr, "usage: doas [-Lns] "
48 "[-C config] [-u user] command [args]\n");
53 parseuid(const char *s, uid_t *uid)
58 if ((pw = getpwnam(s)) != NULL) {
62 *uid = strtonum(s, 0, UID_MAX, &errstr);
69 uidcheck(const char *s, uid_t desired)
73 if (parseuid(s, &uid) != 0)
81 parsegid(const char *s, gid_t *gid)
86 if ((gr = getgrnam(s)) != NULL) {
90 *gid = strtonum(s, 0, GID_MAX, &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" : "");
205 authuser(char *myname, char *login_style, int persist)
207 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
212 fd = open("/dev/tty", O_RDWR);
214 if (ioctl(fd, TIOCCHKVERAUTH) == 0)
218 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
220 errx(1, "Authorization failed");
222 char host[HOST_NAME_MAX + 1];
223 if (gethostname(host, sizeof(host)))
224 snprintf(host, sizeof(host), "?");
225 snprintf(cbuf, sizeof(cbuf),
226 "\rdoas (%.32s@%.32s) password: ", myname, host);
229 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
231 if (response == NULL && errno == ENOTTY) {
232 syslog(LOG_AUTHPRIV | LOG_NOTICE,
233 "tty required for %s", myname);
234 errx(1, "a tty is required");
236 if (!auth_userresponse(as, response, 0)) {
237 explicit_bzero(rbuf, sizeof(rbuf));
238 syslog(LOG_AUTHPRIV | LOG_NOTICE,
239 "failed auth for %s", myname);
240 errx(1, "Authorization failed");
242 explicit_bzero(rbuf, sizeof(rbuf));
246 ioctl(fd, TIOCSETVERAUTH, &secs);
253 main(int argc, char **argv)
255 const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
256 "/usr/local/bin:/usr/local/sbin";
257 const char *confpath = NULL;
258 char *shargv[] = { NULL, NULL };
261 char cmdline[LINE_MAX];
263 char mypwbuf[_PW_BUF_LEN], targpwbuf[_PW_BUF_LEN];
265 char *mypwbuf = NULL, *targpwbuf = NULL;
267 struct passwd mypwstore, targpwstore;
268 struct passwd *mypw, *targpw;
269 const struct rule *rule;
272 gid_t groups[NGROUPS_MAX + 1];
277 char cwdpath[PATH_MAX];
281 char *login_style = NULL;
286 closefrom(STDERR_FILENO + 1);
291 # define OPTSTRING "a:C:Lnsu:"
293 # define OPTSTRING "+C:Lnsu:"
296 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
300 login_style = optarg;
307 #if defined(USE_BSD_AUTH)
308 i = open("/dev/tty", O_RDWR);
310 ioctl(i, TIOCCLRVERAUTH);
312 #elif defined(USE_TIMESTAMP)
313 exit(timestamp_clear() == -1);
318 if (parseuid(optarg, &target) != 0)
319 errx(1, "unknown user");
338 } else if ((!sflag && !argc) || (sflag && argc))
342 rv = getpwuid_r(uid, &mypwstore, mypwbuf, sizeof(mypwbuf), &mypw);
344 err(1, "getpwuid_r failed");
346 for (size_t sz = 1024; sz <= 16*1024; sz *= 2) {
347 mypwbuf = reallocarray(mypwbuf, sz, sizeof (char));
349 errx(1, "can't allocate mypwbuf");
350 rv = getpwuid_r(uid, &mypwstore, mypwbuf, sz, &mypw);
355 err(1, "getpwuid_r failed");
358 errx(1, "no passwd entry for self");
359 ngroups = getgroups(NGROUPS_MAX, groups);
361 err(1, "can't get groups");
362 groups[ngroups++] = getgid();
365 sh = getenv("SHELL");
366 if (sh == NULL || *sh == '\0') {
367 shargv[0] = mypw->pw_shell;
375 checkconfig(confpath, argc, argv, uid, groups, ngroups,
377 exit(1); /* fail safe */
381 errx(1, "not installed setuid");
383 parseconfig("/etc/doas.conf", 1);
385 /* cmdline is used only for logging, no need to abort on truncate */
386 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
387 for (i = 1; i < argc; i++) {
388 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
390 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
395 if (!permit(uid, groups, ngroups, &rule, target, cmd,
396 (const char **)argv + 1)) {
397 syslog(LOG_AUTHPRIV | LOG_NOTICE,
398 "failed command for %s: %s", mypw->pw_name, cmdline);
399 errc(1, EPERM, NULL);
402 #if defined(__OpenBSD__) || defined(USE_SHADOW)
403 if (!(rule->options & NOPASS)) {
405 errx(1, "Authorization required");
408 authuser(mypw->pw_name, login_style, rule->options & PERSIST);
410 shadowauth(mypw->pw_name, rule->options & PERSIST);
415 if (pledge("stdio rpath getpw exec id", NULL) == -1)
419 #elif !defined(USE_PAM)
421 if (!(rule->options & NOPASS)) {
422 errx(1, "Authorization required");
424 #endif /* !(__OpenBSD__ || USE_SHADOW) && !USE_PAM */
427 rv = getpwuid_r(target, &targpwstore, targpwbuf, sizeof(targpwbuf), &targpw);
429 errx(1, "no passwd entry for target");
431 for (size_t sz = 1024; sz <= 16*1024; sz *= 2) {
432 targpwbuf = reallocarray(targpwbuf, sz, sizeof (char));
433 if (targpwbuf == NULL)
434 errx(1, "can't allocate targpwbuf");
435 rv = getpwuid_r(target, &targpwstore, targpwbuf, sz, &targpw);
440 err(1, "getpwuid_r failed");
443 err(1, "getpwuid_r failed");
446 pamauth(targpw->pw_name, mypw->pw_name, !nflag, rule->options & NOPASS,
447 rule->options & PERSIST);
450 #ifdef HAVE_SETUSERCONTEXT
451 if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
452 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
454 errx(1, "failed to set user context for target");
456 if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
458 if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
459 err(1, "initgroups");
460 if (setresuid(target, target, target) != 0)
465 if (pledge("stdio rpath exec", NULL) == -1)
469 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
475 if (pledge("stdio exec", NULL) == -1)
479 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
480 mypw->pw_name, cmdline, targpw->pw_name, cwd);
482 envp = prepenv(rule);
485 if (setenv("PATH", safepath, 1) == -1)
486 err(1, "failed to set PATH '%s'", safepath);
488 execvpe(cmd, argv, envp);
490 errx(1, "%s: command not found", cmd);