]> git.armaanb.net Git - opendoas.git/blob - doas.c
a little const here and there to prevent rules from changing
[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, const 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         yyparse();
181         fclose(yyfp);
182         if (parse_errors)
183                 exit(1);
184 }
185
186 static void __dead
187 checkconfig(const char *confpath, int argc, char **argv,
188     uid_t uid, gid_t *groups, int ngroups, uid_t target)
189 {
190         const struct rule *rule;
191
192         if (setresuid(uid, uid, uid) != 0)
193                 err(1, "setresuid");
194
195         parseconfig(confpath, 0);
196         if (!argc)
197                 exit(0);
198
199         if (permit(uid, groups, ngroups, &rule, target, argv[0],
200             (const char **)argv + 1)) {
201                 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
202                 exit(0);
203         } else {
204                 printf("deny\n");
205                 exit(1);
206         }
207 }
208
209 #ifdef HAVE_BSD_AUTH_H
210 static void
211 authuser(char *myname, char *login_style, int persist)
212 {
213         char *challenge = NULL, *response, rbuf[1024], cbuf[128];
214         auth_session_t *as;
215         int fd = -1;
216
217         if (persist)
218                 fd = open("/dev/tty", O_RDWR);
219         if (fd != -1) {
220                 if (ioctl(fd, TIOCCHKVERAUTH) == 0)
221                         goto good;
222         }
223
224         if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
225             &challenge)))
226                 errx(1, "Authorization failed");
227         if (!challenge) {
228                 char host[HOST_NAME_MAX + 1];
229                 if (gethostname(host, sizeof(host)))
230                         snprintf(host, sizeof(host), "?");
231                 snprintf(cbuf, sizeof(cbuf),
232                     "\rdoas (%.32s@%.32s) password: ", myname, host);
233                 challenge = cbuf;
234         }
235         response = readpassphrase(challenge, rbuf, sizeof(rbuf),
236             RPP_REQUIRE_TTY);
237         if (response == NULL && errno == ENOTTY) {
238                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
239                     "tty required for %s", myname);
240                 errx(1, "a tty is required");
241         }
242         if (!auth_userresponse(as, response, 0)) {
243                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
244                     "failed auth for %s", myname);
245                 errc(1, EPERM, NULL);
246         }
247         explicit_bzero(rbuf, sizeof(rbuf));
248 good:
249         if (fd != -1) {
250                 int secs = 5 * 60;
251                 ioctl(fd, TIOCSETVERAUTH, &secs);
252                 close(fd);
253         }
254 }
255 #elif HAVE_SHADOW_H
256 static void
257 authuser(const char *myname, const char *login_style, int persist)
258 {
259         const char *hash;
260         char *encrypted;
261         struct passwd *pw;
262
263         (void)login_style;
264         (void)persist;
265
266         if (!(pw = getpwnam(myname)))
267                 err(1, "getpwnam");
268
269         hash = pw->pw_passwd;
270         if (hash[0] == 'x' && hash[1] == '\0') {
271                 struct spwd *sp;
272                 if (!(sp = getspnam(myname)))
273                         errx(1, "Authorization failed");
274                 hash = sp->sp_pwdp;
275         } else if (hash[0] != '*') {
276                 errx(1, "Authorization failed");
277         }
278
279         char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
280         if (gethostname(host, sizeof(host)))
281                 snprintf(host, sizeof(host), "?");
282         snprintf(cbuf, sizeof(cbuf),
283                         "\rdoas (%.32s@%.32s) password: ", myname, host);
284         challenge = cbuf;
285
286         response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
287         if (response == NULL && errno == ENOTTY) {
288                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
289                         "tty required for %s", myname);
290                 errx(1, "a tty is required");
291         }
292         if (!(encrypted = crypt(response, hash)))
293                 errx(1, "crypt");
294         if (strcmp(encrypted, hash) != 0) {
295                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
296                 errx(1, "Authorization failed");
297         }
298         explicit_bzero(rbuf, sizeof(rbuf));
299 }
300 #endif /* HAVE_BSD_AUTH_H */
301
302 int
303 main(int argc, char **argv)
304 {
305         const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
306             "/usr/local/bin:/usr/local/sbin";
307         const char *confpath = NULL;
308         char *shargv[] = { NULL, NULL };
309         char *sh;
310         const char *cmd;
311         char cmdline[LINE_MAX];
312         char myname[_PW_NAME_LEN + 1];
313         struct passwd *pw;
314         const struct rule *rule;
315         uid_t uid;
316         uid_t target = 0;
317         gid_t groups[NGROUPS_MAX + 1];
318         int ngroups;
319         int i, ch;
320         int sflag = 0;
321         int nflag = 0;
322         int vflag = 0;
323         char cwdpath[PATH_MAX];
324         const char *cwd;
325         char **envp;
326         char *login_style = NULL;
327
328         setprogname("doas");
329
330         closefrom(STDERR_FILENO + 1);
331
332         uid = getuid();
333
334 #ifdef HAVE_BSD_AUTH_H
335 # define OPTSTRING "a:C:Lnsu:v"
336 #else
337 # define OPTSTRING "+C:Lnsu:v"
338 #endif
339
340         while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
341                 switch (ch) {
342 #ifdef HAVE_BSD_AUTH_H
343                 case 'a':
344                         login_style = optarg;
345                         break;
346 #endif
347                 case 'C':
348                         confpath = optarg;
349                         break;
350                 case 'L':
351 #ifdef TIOCCLRVERAUTH
352                         i = open("/dev/tty", O_RDWR);
353                         if (i != -1)
354                                 ioctl(i, TIOCCLRVERAUTH);
355                         exit(i == -1);
356 #endif
357                 case 'u':
358                         if (parseuid(optarg, &target) != 0)
359                                 errx(1, "unknown user");
360                         break;
361                 case 'n':
362                         nflag = 1;
363                         break;
364                 case 's':
365                         sflag = 1;
366                         break;
367                 case 'v':
368                         vflag = 1;
369                         break;
370                 default:
371                         usage();
372                         break;
373                 }
374         }
375         argv += optind;
376         argc -= optind;
377
378         if (vflag)
379                 version();
380
381         if (confpath) {
382                 if (sflag)
383                         usage();
384         } else if ((!sflag && !argc) || (sflag && argc))
385                 usage();
386
387         pw = getpwuid(uid);
388         if (!pw)
389                 err(1, "getpwuid failed");
390         if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
391                 errx(1, "pw_name too long");
392         ngroups = getgroups(NGROUPS_MAX, groups);
393         if (ngroups == -1)
394                 err(1, "can't get groups");
395         groups[ngroups++] = getgid();
396
397         if (sflag) {
398                 sh = getenv("SHELL");
399                 if (sh == NULL || *sh == '\0') {
400                         shargv[0] = strdup(pw->pw_shell);
401                         if (shargv[0] == NULL)
402                                 err(1, NULL);
403                 } else
404                         shargv[0] = sh;
405                 argv = shargv;
406                 argc = 1;
407         }
408
409         if (confpath) {
410                 checkconfig(confpath, argc, argv, uid, groups, ngroups,
411                     target);
412                 exit(1);        /* fail safe */
413         }
414
415         if (geteuid())
416                 errx(1, "not installed setuid");
417
418         parseconfig("/etc/doas.conf", 1);
419
420         /* cmdline is used only for logging, no need to abort on truncate */
421         (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
422         for (i = 1; i < argc; i++) {
423                 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
424                         break;
425                 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
426                         break;
427         }
428
429         cmd = argv[0];
430         if (!permit(uid, groups, ngroups, &rule, target, cmd,
431             (const char **)argv + 1)) {
432                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
433                     "failed command for %s: %s", myname, cmdline);
434                 errc(1, EPERM, NULL);
435         }
436
437 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
438         if (!(rule->options & NOPASS)) {
439                 if (nflag)
440                         errx(1, "Authorization required");
441
442                 authuser(myname, login_style, rule->options & PERSIST);
443         }
444 #elif HAVE_PAM_APPL_H
445         pw = getpwuid(target);
446         if (!pw)
447                 errx(1, "no passwd entry for target");
448
449         if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
450                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
451                 errx(1, "Authorization failed");
452         }
453 #else
454 #error "No authentication method"
455 #endif /* HAVE_BSD_AUTH_H */
456
457         if (pledge("stdio rpath getpw exec id", NULL) == -1)
458                 err(1, "pledge");
459
460         pw = getpwuid(target);
461         if (!pw)
462                 errx(1, "no passwd entry for target");
463
464 #ifdef HAVE_BSD_AUTH_H
465         if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
466             LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
467             LOGIN_SETUSER) != 0)
468                 errx(1, "failed to set user context for target");
469 #else
470         if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
471                 errx(1, "setresgid");
472         if (initgroups(pw->pw_name, pw->pw_gid) != 0)
473                 errx(1, "initgroups");
474         if (setresuid(target, target, target) != 0)
475                 errx(1, "setresuid");
476 #endif
477
478         if (pledge("stdio rpath exec", NULL) == -1)
479                 err(1, "pledge");
480
481         if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
482                 cwd = "(failed)";
483         else
484                 cwd = cwdpath;
485
486         if (pledge("stdio exec", NULL) == -1)
487                 err(1, "pledge");
488
489         syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
490             myname, cmdline, pw->pw_name, cwd);
491
492         envp = prepenv(rule);
493
494         if (rule->cmd) {
495                 if (setenv("PATH", safepath, 1) == -1)
496                         err(1, "failed to set PATH '%s'", safepath);
497         }
498         execvpe(cmd, argv, envp);
499         if (errno == ENOENT)
500                 errx(1, "%s: command not found", cmd);
501         err(1, "%s", cmd);
502 }