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 syslog(LOG_AUTHPRIV | LOG_NOTICE,
238 "failed auth for %s", myname);
239 errx(1, "Authorization failed");
241 explicit_bzero(rbuf, sizeof(rbuf));
245 ioctl(fd, TIOCSETVERAUTH, &secs);
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 };
260 char cmdline[LINE_MAX];
261 char myname[_PW_NAME_LEN + 1];
263 const struct rule *rule;
266 gid_t groups[NGROUPS_MAX + 1];
271 char cwdpath[PATH_MAX];
275 char *login_style = NULL;
280 closefrom(STDERR_FILENO + 1);
285 # define OPTSTRING "a:C:Lnsu:"
287 # define OPTSTRING "+C:Lnsu:"
290 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
294 login_style = optarg;
301 #if defined(USE_BSD_AUTH)
302 i = open("/dev/tty", O_RDWR);
304 ioctl(i, TIOCCLRVERAUTH);
306 #elif defined(USE_TIMESTAMP)
307 exit(timestamp_clear() == -1);
312 if (parseuid(optarg, &target) != 0)
313 errx(1, "unknown user");
332 } else if ((!sflag && !argc) || (sflag && argc))
337 err(1, "getpwuid failed");
338 if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
339 errx(1, "pw_name too long");
340 ngroups = getgroups(NGROUPS_MAX, groups);
342 err(1, "can't get groups");
343 groups[ngroups++] = getgid();
346 sh = getenv("SHELL");
347 if (sh == NULL || *sh == '\0') {
348 shargv[0] = strdup(pw->pw_shell);
349 if (shargv[0] == NULL)
358 checkconfig(confpath, argc, argv, uid, groups, ngroups,
360 exit(1); /* fail safe */
364 errx(1, "not installed setuid");
366 parseconfig("/etc/doas.conf", 1);
368 /* cmdline is used only for logging, no need to abort on truncate */
369 (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
370 for (i = 1; i < argc; i++) {
371 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
373 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
378 if (!permit(uid, groups, ngroups, &rule, target, cmd,
379 (const char **)argv + 1)) {
380 syslog(LOG_AUTHPRIV | LOG_NOTICE,
381 "failed command for %s: %s", myname, cmdline);
382 errc(1, EPERM, NULL);
385 #if defined(__OpenBSD__)
386 if (!(rule->options & NOPASS)) {
388 errx(1, "Authorization required");
390 authuser(myname, login_style, rule->options & PERSIST);
393 if (pledge("stdio rpath getpw exec id", NULL) == -1)
396 pw = getpwuid(target);
398 errx(1, "no passwd entry for target");
400 #elif defined(USE_SHADOW)
401 if (!(rule->options & NOPASS)) {
403 errx(1, "Authorization required");
405 shadowauth(myname, rule->options & PERSIST);
408 pw = getpwuid(target);
410 errx(1, "no passwd entry for target");
412 #elif defined(USE_PAM)
413 pw = getpwuid(target);
415 errx(1, "no passwd entry for target");
417 pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS,
418 rule->options & PERSIST);
422 if (!(rule->options & NOPASS)) {
423 errx(1, "Authorization required");
427 #ifdef HAVE_SETUSERCONTEXT
428 if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
429 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
431 errx(1, "failed to set user context for target");
433 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
435 if (initgroups(pw->pw_name, pw->pw_gid) != 0)
436 err(1, "initgroups");
437 if (setresuid(target, target, target) != 0)
442 if (pledge("stdio rpath exec", NULL) == -1)
446 if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
452 if (pledge("stdio exec", NULL) == -1)
456 syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
457 myname, cmdline, pw->pw_name, cwd);
459 envp = prepenv(rule);
462 if (setenv("PATH", safepath, 1) == -1)
463 err(1, "failed to set PATH '%s'", safepath);
465 execvpe(cmd, argv, envp);
467 errx(1, "%s: command not found", cmd);