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];
262 char myname[_PW_NAME_LEN + 1];
264 char mypwbuf[_PW_BUF_LEN], targpwbuf[_PW_BUF_LEN];
266 char *mypwbuf = NULL, *targpwbuf = NULL;
268 struct passwd mypwstore, targpwstore;
269 struct passwd *mypw, *targpw;
270 const struct rule *rule;
273 gid_t groups[NGROUPS_MAX + 1];
278 char cwdpath[PATH_MAX];
282 char *login_style = NULL;
287 closefrom(STDERR_FILENO + 1);
292 # define OPTSTRING "a:C:Lnsu:"
294 # define OPTSTRING "+C:Lnsu:"
297 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
301 login_style = optarg;
308 #if defined(USE_BSD_AUTH)
309 i = open("/dev/tty", O_RDWR);
311 ioctl(i, TIOCCLRVERAUTH);
313 #elif defined(USE_TIMESTAMP)
314 exit(timestamp_clear() == -1);
319 if (parseuid(optarg, &target) != 0)
320 errx(1, "unknown user");
339 } else if ((!sflag && !argc) || (sflag && argc))
343 rv = getpwuid_r(uid, &mypwstore, mypwbuf, sizeof(mypwbuf), &mypw);
344 if (rv != 0 || mypw == NULL)
345 err(1, "getpwuid_r failed");
347 for (size_t sz = 1024; sz <= 16*1024; sz *= 2) {
348 mypwbuf = reallocarray(mypwbuf, sz, sizeof (char));
350 errx(1, "can't allocate mypwbuf");
351 rv = getpwuid_r(uid, &mypwstore, mypwbuf, sz, &mypw);
355 if (rv != 0 || mypw == NULL)
356 err(1, "getpwuid_r failed");
358 if (strlcpy(myname, mypw->pw_name, sizeof(myname)) >= sizeof(myname))
359 errx(1, "pw_name too long");
360 ngroups = getgroups(NGROUPS_MAX, groups);
362 err(1, "can't get groups");
363 groups[ngroups++] = getgid();
366 sh = getenv("SHELL");
367 if (sh == NULL || *sh == '\0') {
368 shargv[0] = strdup(mypw->pw_shell);
369 if (shargv[0] == NULL)
378 checkconfig(confpath, argc, argv, uid, groups, ngroups,
380 exit(1); /* fail safe */
384 errx(1, "not installed setuid");
386 parseconfig("/etc/doas.conf", 1);
388 /* cmdline is used only for logging, no need to abort on truncate */
389 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
390 for (i = 1; i < argc; i++) {
391 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
393 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
398 if (!permit(uid, groups, ngroups, &rule, target, cmd,
399 (const char **)argv + 1)) {
400 syslog(LOG_AUTHPRIV | LOG_NOTICE,
401 "failed command for %s: %s", myname, cmdline);
402 errc(1, EPERM, NULL);
405 #if defined(__OpenBSD__) || defined(USE_SHADOW)
406 if (!(rule->options & NOPASS)) {
408 errx(1, "Authorization required");
411 authuser(myname, login_style, rule->options & PERSIST);
413 shadowauth(myname, rule->options & PERSIST);
418 if (pledge("stdio rpath getpw exec id", NULL) == -1)
422 #elif !defined(USE_PAM)
424 if (!(rule->options & NOPASS)) {
425 errx(1, "Authorization required");
427 #endif /* !(__OpenBSD__ || USE_SHADOW) && !USE_PAM */
430 rv = getpwuid_r(target, &targpwstore, targpwbuf, sizeof(targpwbuf), &targpw);
431 if (rv != 0 || targpw == NULL)
432 errx(1, "no passwd entry for target");
434 for (size_t sz = 1024; sz <= 16*1024; sz *= 2) {
435 targpwbuf = reallocarray(targpwbuf, sz, sizeof (char));
436 if (targpwbuf == NULL)
437 errx(1, "can't allocate targpwbuf");
438 rv = getpwuid_r(target, &targpwstore, targpwbuf, sz, &targpw);
442 if (rv != 0 || targpw == NULL)
443 err(1, "getpwuid_r failed");
447 pamauth(targpw->pw_name, myname, !nflag, rule->options & NOPASS,
448 rule->options & PERSIST);
451 #ifdef HAVE_SETUSERCONTEXT
452 if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
453 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
455 errx(1, "failed to set user context for target");
457 if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
459 if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
460 err(1, "initgroups");
461 if (setresuid(target, target, target) != 0)
466 if (pledge("stdio rpath exec", NULL) == -1)
470 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
476 if (pledge("stdio exec", NULL) == -1)
480 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
481 myname, cmdline, targpw->pw_name, cwd);
483 envp = prepenv(rule);
486 if (setenv("PATH", safepath, 1) == -1)
487 err(1, "failed to set PATH '%s'", safepath);
489 execvpe(cmd, argv, envp);
491 errx(1, "%s: command not found", cmd);