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