]> git.armaanb.net Git - opendoas.git/blob - doas.c
the sudo timeout was 5 minutes i believe, so we'll match that.
[opendoas.git] / doas.c
1 /* $OpenBSD: doas.c,v 1.52 2016/04/28 04:48:56 tedu Exp $ */
2 /*
3  * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
4  *
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.
8  *
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.
16  */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/ioctl.h>
21
22 #include <limits.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <err.h>
27 #include <unistd.h>
28 #include <pwd.h>
29 #include <grp.h>
30 #include <syslog.h>
31 #include <errno.h>
32 #if HAVE_SHADOW_H
33 #include <shadow.h>
34 #endif
35
36 #include "includes.h"
37
38 #include "doas.h"
39
40 static void __dead
41 version(void)
42 {
43         fprintf(stderr, "doas: version %s built %s\n", VERSION, __DATE__);
44         exit(1);
45 }
46
47 static void __dead
48 usage(void)
49 {
50         fprintf(stderr, "usage: doas [-Lnsv] "
51 #ifdef HAVE_BSD_AUTH_H
52             "[-a style] "
53 #endif
54             "[-C config] [-u user] command [args]\n");
55         exit(1);
56 }
57
58 size_t
59 arraylen(const char **arr)
60 {
61         size_t cnt = 0;
62
63         if (arr) {
64                 while (*arr) {
65                         cnt++;
66                         arr++;
67                 }
68         }
69         return cnt;
70 }
71
72 static int
73 parseuid(const char *s, uid_t *uid)
74 {
75         struct passwd *pw;
76         const char *errstr;
77
78         if ((pw = getpwnam(s)) != NULL) {
79                 *uid = pw->pw_uid;
80                 return 0;
81         }
82         *uid = strtonum(s, 0, UID_MAX, &errstr);
83         if (errstr)
84                 return -1;
85         return 0;
86 }
87
88 static int
89 uidcheck(const char *s, uid_t desired)
90 {
91         uid_t uid;
92
93         if (parseuid(s, &uid) != 0)
94                 return -1;
95         if (uid != desired)
96                 return -1;
97         return 0;
98 }
99
100 static int
101 parsegid(const char *s, gid_t *gid)
102 {
103         struct group *gr;
104         const char *errstr;
105
106         if ((gr = getgrnam(s)) != NULL) {
107                 *gid = gr->gr_gid;
108                 return 0;
109         }
110         *gid = strtonum(s, 0, GID_MAX, &errstr);
111         if (errstr)
112                 return -1;
113         return 0;
114 }
115
116 static int
117 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
118     const char **cmdargs, struct rule *r)
119 {
120         int i;
121
122         if (r->ident[0] == ':') {
123                 gid_t rgid;
124                 if (parsegid(r->ident + 1, &rgid) == -1)
125                         return 0;
126                 for (i = 0; i < ngroups; i++) {
127                         if (rgid == groups[i])
128                                 break;
129                 }
130                 if (i == ngroups)
131                         return 0;
132         } else {
133                 if (uidcheck(r->ident, uid) != 0)
134                         return 0;
135         }
136         if (r->target && uidcheck(r->target, target) != 0)
137                 return 0;
138         if (r->cmd) {
139                 if (strcmp(r->cmd, cmd))
140                         return 0;
141                 if (r->cmdargs) {
142                         /* if arguments were given, they should match explicitly */
143                         for (i = 0; r->cmdargs[i]; i++) {
144                                 if (!cmdargs[i])
145                                         return 0;
146                                 if (strcmp(r->cmdargs[i], cmdargs[i]))
147                                         return 0;
148                         }
149                         if (cmdargs[i])
150                                 return 0;
151                 }
152         }
153         return 1;
154 }
155
156 static int
157 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
158     uid_t target, const char *cmd, const char **cmdargs)
159 {
160         int i;
161
162         *lastr = NULL;
163         for (i = 0; i < nrules; i++) {
164                 if (match(uid, groups, ngroups, target, cmd,
165                     cmdargs, rules[i]))
166                         *lastr = rules[i];
167         }
168         if (!*lastr)
169                 return 0;
170         return (*lastr)->action == PERMIT;
171 }
172
173 static void
174 parseconfig(const char *filename, int checkperms)
175 {
176         extern FILE *yyfp;
177         extern int yyparse(void);
178         struct stat sb;
179
180         yyfp = fopen(filename, "r");
181         if (!yyfp)
182                 err(1, checkperms ? "doas is not enabled, %s" :
183                     "could not open config file %s", filename);
184
185         if (checkperms) {
186                 if (fstat(fileno(yyfp), &sb) != 0)
187                         err(1, "fstat(\"%s\")", filename);
188                 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
189                         errx(1, "%s is writable by group or other", filename);
190                 if (sb.st_uid != 0)
191                         errx(1, "%s is not owned by root", filename);
192         }
193
194         yyparse();
195         fclose(yyfp);
196         if (parse_errors)
197                 exit(1);
198 }
199
200 static void __dead
201 checkconfig(const char *confpath, int argc, char **argv,
202     uid_t uid, gid_t *groups, int ngroups, uid_t target)
203 {
204         struct rule *rule;
205
206         if (setresuid(uid, uid, uid) != 0)
207                 err(1, "setresuid");
208
209         parseconfig(confpath, 0);
210         if (!argc)
211                 exit(0);
212
213         if (permit(uid, groups, ngroups, &rule, target, argv[0],
214             (const char **)argv + 1)) {
215                 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
216                 exit(0);
217         } else {
218                 printf("deny\n");
219                 exit(1);
220         }
221 }
222
223 #ifdef HAVE_BSD_AUTH_H
224 static void
225 authuser(char *myname, char *login_style, int persist)
226 {
227         char *challenge = NULL, *response, rbuf[1024], cbuf[128];
228         auth_session_t *as;
229         int fd = -1;
230
231         if (persist)
232                 fd = open("/dev/tty", O_RDWR);
233         if (fd != -1) {
234                 if (ioctl(fd, TIOCCHKVERAUTH) == 0)
235                         goto good;
236         }
237
238         if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
239             &challenge)))
240                 errx(1, "Authorization failed");
241         if (!challenge) {
242                 char host[HOST_NAME_MAX + 1];
243                 if (gethostname(host, sizeof(host)))
244                         snprintf(host, sizeof(host), "?");
245                 snprintf(cbuf, sizeof(cbuf),
246                     "\rdoas (%.32s@%.32s) password: ", myname, host);
247                 challenge = cbuf;
248         }
249         response = readpassphrase(challenge, rbuf, sizeof(rbuf),
250             RPP_REQUIRE_TTY);
251         if (response == NULL && errno == ENOTTY) {
252                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
253                     "tty required for %s", myname);
254                 errx(1, "a tty is required");
255         }
256         if (!auth_userresponse(as, response, 0)) {
257                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
258                     "failed auth for %s", myname);
259                 errc(1, EPERM, NULL);
260         }
261         explicit_bzero(rbuf, sizeof(rbuf));
262 good:
263         if (fd != -1) {
264                 int secs = 5 * 60;
265                 ioctl(fd, TIOCSETVERAUTH, &secs);
266                 close(fd);
267         }
268 }
269 #elif HAVE_SHADOW_H
270 static void
271 authuser(const char *myname, const char *login_style, int persist)
272 {
273         const char *hash;
274         char *encrypted;
275         struct passwd *pw;
276
277         (void)login_style;
278         (void)persist;
279
280         if (!(pw = getpwnam(myname)))
281                 err(1, "getpwnam");
282
283         hash = pw->pw_passwd;
284         if (hash[0] == 'x' && hash[1] == '\0') {
285                 struct spwd *sp;
286                 if (!(sp = getspnam(myname)))
287                         errx(1, "Authorization failed");
288                 hash = sp->sp_pwdp;
289         } else if (hash[0] != '*') {
290                 errx(1, "Authorization failed");
291         }
292
293         char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
294         if (gethostname(host, sizeof(host)))
295                 snprintf(host, sizeof(host), "?");
296         snprintf(cbuf, sizeof(cbuf),
297                         "\rdoas (%.32s@%.32s) password: ", myname, host);
298         challenge = cbuf;
299
300         response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
301         if (response == NULL && errno == ENOTTY) {
302                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
303                         "tty required for %s", myname);
304                 errx(1, "a tty is required");
305         }
306         if (!(encrypted = crypt(response, hash)))
307                 errx(1, "crypt");
308         if (strcmp(encrypted, hash) != 0) {
309                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
310                 errx(1, "Authorization failed");
311         }
312         explicit_bzero(rbuf, sizeof(rbuf));
313 }
314 #endif /* HAVE_BSD_AUTH_H */
315
316 int
317 main(int argc, char **argv)
318 {
319         const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
320             "/usr/local/bin:/usr/local/sbin";
321         const char *confpath = NULL;
322         char *shargv[] = { NULL, NULL };
323         char *sh;
324         const char *cmd;
325         char cmdline[LINE_MAX];
326         char myname[_PW_NAME_LEN + 1];
327         struct passwd *pw;
328         struct rule *rule;
329         uid_t uid;
330         uid_t target = 0;
331         gid_t groups[NGROUPS_MAX + 1];
332         int ngroups;
333         int i, ch;
334         int sflag = 0;
335         int nflag = 0;
336         int vflag = 0;
337         char cwdpath[PATH_MAX];
338         const char *cwd;
339         char **envp;
340         char *login_style = NULL;
341
342         setprogname("doas");
343
344         closefrom(STDERR_FILENO + 1);
345
346         uid = getuid();
347
348 #ifdef HAVE_BSD_AUTH_H
349 # define OPTSTRING "a:C:Lnsu:v"
350 #else
351 # define OPTSTRING "+C:Lnsu:v"
352 #endif
353
354         while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
355                 switch (ch) {
356 #ifdef HAVE_BSD_AUTH_H
357                 case 'a':
358                         login_style = optarg;
359                         break;
360 #endif
361                 case 'C':
362                         confpath = optarg;
363                         break;
364                 case 'L':
365 #ifdef TIOCCLRVERAUTH
366                         i = open("/dev/tty", O_RDWR);
367                         if (i != -1)
368                                 ioctl(i, TIOCCLRVERAUTH);
369                         exit(i == -1);
370 #endif
371                 case 'u':
372                         if (parseuid(optarg, &target) != 0)
373                                 errx(1, "unknown user");
374                         break;
375                 case 'n':
376                         nflag = 1;
377                         break;
378                 case 's':
379                         sflag = 1;
380                         break;
381                 case 'v':
382                         vflag = 1;
383                         break;
384                 default:
385                         usage();
386                         break;
387                 }
388         }
389         argv += optind;
390         argc -= optind;
391
392         if (vflag)
393                 version();
394
395         if (confpath) {
396                 if (sflag)
397                         usage();
398         } else if ((!sflag && !argc) || (sflag && argc))
399                 usage();
400
401         pw = getpwuid(uid);
402         if (!pw)
403                 err(1, "getpwuid failed");
404         if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
405                 errx(1, "pw_name too long");
406         ngroups = getgroups(NGROUPS_MAX, groups);
407         if (ngroups == -1)
408                 err(1, "can't get groups");
409         groups[ngroups++] = getgid();
410
411         if (sflag) {
412                 sh = getenv("SHELL");
413                 if (sh == NULL || *sh == '\0') {
414                         shargv[0] = strdup(pw->pw_shell);
415                         if (shargv[0] == NULL)
416                                 err(1, NULL);
417                 } else
418                         shargv[0] = sh;
419                 argv = shargv;
420                 argc = 1;
421         }
422
423         if (confpath) {
424                 checkconfig(confpath, argc, argv, uid, groups, ngroups,
425                     target);
426                 exit(1);        /* fail safe */
427         }
428
429         parseconfig("/etc/doas.conf", 1);
430
431         /* cmdline is used only for logging, no need to abort on truncate */
432         (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
433         for (i = 1; i < argc; i++) {
434                 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
435                         break;
436                 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
437                         break;
438         }
439
440         cmd = argv[0];
441         if (!permit(uid, groups, ngroups, &rule, target, cmd,
442             (const char **)argv + 1)) {
443                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
444                     "failed command for %s: %s", myname, cmdline);
445                 errc(1, EPERM, NULL);
446         }
447
448 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
449         if (!(rule->options & NOPASS)) {
450                 if (nflag)
451                         errx(1, "Authorization required");
452
453                 authuser(myname, login_style, rule->options & PERSIST);
454         }
455 #elif HAVE_PAM_APPL_H
456         pw = getpwuid(target);
457         if (!pw)
458                 errx(1, "no passwd entry for target");
459
460         if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
461                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
462                 errx(1, "Authorization failed");
463         }
464 #else
465 #error "No authentication method"
466 #endif /* HAVE_BSD_AUTH_H */
467
468         if (pledge("stdio rpath getpw exec id", NULL) == -1)
469                 err(1, "pledge");
470
471         pw = getpwuid(target);
472         if (!pw)
473                 errx(1, "no passwd entry for target");
474
475 #ifdef HAVE_BSD_AUTH_H
476         if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
477             LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
478             LOGIN_SETUSER) != 0)
479                 errx(1, "failed to set user context for target");
480 #else
481         if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
482                 errx(1, "setresgid");
483         if (initgroups(pw->pw_name, pw->pw_gid) != 0)
484                 errx(1, "initgroups");
485         if (setresuid(target, target, target) != 0)
486                 errx(1, "setresuid");
487 #endif
488
489         if (pledge("stdio rpath exec", NULL) == -1)
490                 err(1, "pledge");
491
492         if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
493                 cwd = "(failed)";
494         else
495                 cwd = cwdpath;
496
497         if (pledge("stdio exec", NULL) == -1)
498                 err(1, "pledge");
499
500         syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
501             myname, cmdline, pw->pw_name, cwd);
502
503         envp = prepenv(rule);
504
505         if (rule->cmd) {
506                 if (setenv("PATH", safepath, 1) == -1)
507                         err(1, "failed to set PATH '%s'", safepath);
508         }
509         execvpe(cmd, argv, envp);
510         if (errno == ENOENT)
511                 errx(1, "%s: command not found", cmd);
512         err(1, "%s", cmd);
513 }