]> git.armaanb.net Git - opendoas.git/blob - doas.c
add initial timestamp file support, disabled by default and only with shadow auth
[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                 errx(1, "Authorization failed");
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
265 #ifdef PERSIST_TIMESTAMP
266         int fd = -1;
267         int valid;
268         if (persist)
269                 fd = persist_open(&valid, 5 * 60);
270         if (fd != -1 && valid)
271                 goto good;
272 #else
273         (void)persist;
274 #endif
275
276         if (!(pw = getpwnam(myname)))
277                 err(1, "getpwnam");
278
279         hash = pw->pw_passwd;
280         if (hash[0] == 'x' && hash[1] == '\0') {
281                 struct spwd *sp;
282                 if (!(sp = getspnam(myname)))
283                         errx(1, "Authorization failed");
284                 hash = sp->sp_pwdp;
285         } else if (hash[0] != '*') {
286                 errx(1, "Authorization failed");
287         }
288
289         char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
290         if (gethostname(host, sizeof(host)))
291                 snprintf(host, sizeof(host), "?");
292         snprintf(cbuf, sizeof(cbuf),
293                         "\rdoas (%.32s@%.32s) password: ", myname, host);
294         challenge = cbuf;
295
296         response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
297         if (response == NULL && errno == ENOTTY) {
298                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
299                         "tty required for %s", myname);
300                 errx(1, "a tty is required");
301         }
302         if (!(encrypted = crypt(response, hash)))
303                 errx(1, "crypt");
304         if (strcmp(encrypted, hash) != 0) {
305                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
306                 errx(1, "Authorization failed");
307         }
308         explicit_bzero(rbuf, sizeof(rbuf));
309 #ifdef PERSIST_TIMESTAMP
310 good:
311         if (fd != -1) {
312                 persist_set(fd, 5 * 60);
313                 close(fd);
314         }
315 #endif
316 }
317 #endif /* HAVE_BSD_AUTH_H */
318
319 int
320 main(int argc, char **argv)
321 {
322         const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
323             "/usr/local/bin:/usr/local/sbin";
324         const char *confpath = NULL;
325         char *shargv[] = { NULL, NULL };
326         char *sh;
327         const char *cmd;
328         char cmdline[LINE_MAX];
329         char myname[_PW_NAME_LEN + 1];
330         struct passwd *pw;
331         const struct rule *rule;
332         uid_t uid;
333         uid_t target = 0;
334         gid_t groups[NGROUPS_MAX + 1];
335         int ngroups;
336         int i, ch;
337         int sflag = 0;
338         int nflag = 0;
339         int vflag = 0;
340         char cwdpath[PATH_MAX];
341         const char *cwd;
342         char **envp;
343         char *login_style = NULL;
344
345         setprogname("doas");
346
347         closefrom(STDERR_FILENO + 1);
348
349         uid = getuid();
350
351 #ifdef HAVE_BSD_AUTH_H
352 # define OPTSTRING "a:C:Lnsu:v"
353 #else
354 # define OPTSTRING "+C:Lnsu:v"
355 #endif
356
357         while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
358                 switch (ch) {
359 #ifdef HAVE_BSD_AUTH_H
360                 case 'a':
361                         login_style = optarg;
362                         break;
363 #endif
364                 case 'C':
365                         confpath = optarg;
366                         break;
367                 case 'L':
368 #ifdef TIOCCLRVERAUTH
369                         i = open("/dev/tty", O_RDWR);
370                         if (i != -1)
371                                 ioctl(i, TIOCCLRVERAUTH);
372                         exit(i == -1);
373 #elif PERSIST_TIMESTAMP
374                         exit(persist_clear() != 0);
375 #endif
376                 case 'u':
377                         if (parseuid(optarg, &target) != 0)
378                                 errx(1, "unknown user");
379                         break;
380                 case 'n':
381                         nflag = 1;
382                         break;
383                 case 's':
384                         sflag = 1;
385                         break;
386                 case 'v':
387                         vflag = 1;
388                         break;
389                 default:
390                         usage();
391                         break;
392                 }
393         }
394         argv += optind;
395         argc -= optind;
396
397         if (vflag)
398                 version();
399
400         if (confpath) {
401                 if (sflag)
402                         usage();
403         } else if ((!sflag && !argc) || (sflag && argc))
404                 usage();
405
406         pw = getpwuid(uid);
407         if (!pw)
408                 err(1, "getpwuid failed");
409         if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
410                 errx(1, "pw_name too long");
411         ngroups = getgroups(NGROUPS_MAX, groups);
412         if (ngroups == -1)
413                 err(1, "can't get groups");
414         groups[ngroups++] = getgid();
415
416         if (sflag) {
417                 sh = getenv("SHELL");
418                 if (sh == NULL || *sh == '\0') {
419                         shargv[0] = strdup(pw->pw_shell);
420                         if (shargv[0] == NULL)
421                                 err(1, NULL);
422                 } else
423                         shargv[0] = sh;
424                 argv = shargv;
425                 argc = 1;
426         }
427
428         if (confpath) {
429                 checkconfig(confpath, argc, argv, uid, groups, ngroups,
430                     target);
431                 exit(1);        /* fail safe */
432         }
433
434         if (geteuid())
435                 errx(1, "not installed setuid");
436
437         parseconfig("/etc/doas.conf", 1);
438
439         /* cmdline is used only for logging, no need to abort on truncate */
440         (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
441         for (i = 1; i < argc; i++) {
442                 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
443                         break;
444                 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
445                         break;
446         }
447
448         cmd = argv[0];
449         if (!permit(uid, groups, ngroups, &rule, target, cmd,
450             (const char **)argv + 1)) {
451                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
452                     "failed command for %s: %s", myname, cmdline);
453                 errc(1, EPERM, NULL);
454         }
455
456 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
457         if (!(rule->options & NOPASS)) {
458                 if (nflag)
459                         errx(1, "Authorization required");
460
461                 authuser(myname, login_style, rule->options & PERSIST);
462         }
463 #elif HAVE_PAM_APPL_H
464         pw = getpwuid(target);
465         if (!pw)
466                 errx(1, "no passwd entry for target");
467
468         if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
469                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
470                 errx(1, "Authorization failed");
471         }
472 #else
473 #error "No authentication method"
474 #endif /* HAVE_BSD_AUTH_H */
475
476         if (pledge("stdio rpath getpw exec id", NULL) == -1)
477                 err(1, "pledge");
478
479         pw = getpwuid(target);
480         if (!pw)
481                 errx(1, "no passwd entry for target");
482
483 #ifdef HAVE_BSD_AUTH_H
484         if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
485             LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
486             LOGIN_SETUSER) != 0)
487                 errx(1, "failed to set user context for target");
488 #else
489         if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
490                 errx(1, "setresgid");
491         if (initgroups(pw->pw_name, pw->pw_gid) != 0)
492                 errx(1, "initgroups");
493         if (setresuid(target, target, target) != 0)
494                 errx(1, "setresuid");
495 #endif
496
497         if (pledge("stdio rpath exec", NULL) == -1)
498                 err(1, "pledge");
499
500         if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
501                 cwd = "(failed)";
502         else
503                 cwd = cwdpath;
504
505         if (pledge("stdio exec", NULL) == -1)
506                 err(1, "pledge");
507
508         syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
509             myname, cmdline, pw->pw_name, cwd);
510
511         envp = prepenv(rule);
512
513         if (rule->cmd) {
514                 if (setenv("PATH", safepath, 1) == -1)
515                         err(1, "failed to set PATH '%s'", safepath);
516         }
517         execvpe(cmd, argv, envp);
518         if (errno == ENOENT)
519                 errx(1, "%s: command not found", cmd);
520         err(1, "%s", cmd);
521 }