]> git.armaanb.net Git - opendoas.git/blob - doas.c
doas.c: put login_style in ifdef to compile on Linux
[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 #ifdef HAVE_BSD_AUTH_H
344         char *login_style = NULL;
345 #endif
346
347         setprogname("doas");
348
349         closefrom(STDERR_FILENO + 1);
350
351         uid = getuid();
352
353 #ifdef HAVE_BSD_AUTH_H
354 # define OPTSTRING "a:C:Lnsu:v"
355 #else
356 # define OPTSTRING "+C:Lnsu:v"
357 #endif
358
359         while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
360                 switch (ch) {
361 #ifdef HAVE_BSD_AUTH_H
362                 case 'a':
363                         login_style = optarg;
364                         break;
365 #endif
366                 case 'C':
367                         confpath = optarg;
368                         break;
369                 case 'L':
370 #ifdef TIOCCLRVERAUTH
371                         i = open("/dev/tty", O_RDWR);
372                         if (i != -1)
373                                 ioctl(i, TIOCCLRVERAUTH);
374                         exit(i == -1);
375 #elif PERSIST_TIMESTAMP
376                         exit(persist_clear() != 0);
377 #endif
378                 case 'u':
379                         if (parseuid(optarg, &target) != 0)
380                                 errx(1, "unknown user");
381                         break;
382                 case 'n':
383                         nflag = 1;
384                         break;
385                 case 's':
386                         sflag = 1;
387                         break;
388                 case 'v':
389                         vflag = 1;
390                         break;
391                 default:
392                         usage();
393                         break;
394                 }
395         }
396         argv += optind;
397         argc -= optind;
398
399         if (vflag)
400                 version();
401
402         if (confpath) {
403                 if (sflag)
404                         usage();
405         } else if ((!sflag && !argc) || (sflag && argc))
406                 usage();
407
408         pw = getpwuid(uid);
409         if (!pw)
410                 err(1, "getpwuid failed");
411         if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
412                 errx(1, "pw_name too long");
413         ngroups = getgroups(NGROUPS_MAX, groups);
414         if (ngroups == -1)
415                 err(1, "can't get groups");
416         groups[ngroups++] = getgid();
417
418         if (sflag) {
419                 sh = getenv("SHELL");
420                 if (sh == NULL || *sh == '\0') {
421                         shargv[0] = strdup(pw->pw_shell);
422                         if (shargv[0] == NULL)
423                                 err(1, NULL);
424                 } else
425                         shargv[0] = sh;
426                 argv = shargv;
427                 argc = 1;
428         }
429
430         if (confpath) {
431                 checkconfig(confpath, argc, argv, uid, groups, ngroups,
432                     target);
433                 exit(1);        /* fail safe */
434         }
435
436         if (geteuid())
437                 errx(1, "not installed setuid");
438
439         parseconfig("/etc/doas.conf", 1);
440
441         /* cmdline is used only for logging, no need to abort on truncate */
442         (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
443         for (i = 1; i < argc; i++) {
444                 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
445                         break;
446                 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
447                         break;
448         }
449
450         cmd = argv[0];
451         if (!permit(uid, groups, ngroups, &rule, target, cmd,
452             (const char **)argv + 1)) {
453                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
454                     "failed command for %s: %s", myname, cmdline);
455                 errc(1, EPERM, NULL);
456         }
457
458 #if defined(HAVE_BSD_AUTH_H) || defined(HAVE_SHADOW_H)
459         if (!(rule->options & NOPASS)) {
460                 if (nflag)
461                         errx(1, "Authorization required");
462
463                 authuser(myname, login_style, rule->options & PERSIST);
464         }
465 #elif HAVE_PAM_APPL_H
466         pw = getpwuid(target);
467         if (!pw)
468                 errx(1, "no passwd entry for target");
469
470         if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
471                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
472                 errx(1, "Authorization failed");
473         }
474 #else
475 #error "No authentication method"
476 #endif /* HAVE_BSD_AUTH_H */
477
478         if (pledge("stdio rpath getpw exec id", NULL) == -1)
479                 err(1, "pledge");
480
481         pw = getpwuid(target);
482         if (!pw)
483                 errx(1, "no passwd entry for target");
484
485 #ifdef HAVE_BSD_AUTH_H
486         if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
487             LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
488             LOGIN_SETUSER) != 0)
489                 errx(1, "failed to set user context for target");
490 #else
491         if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
492                 errx(1, "setresgid");
493         if (initgroups(pw->pw_name, pw->pw_gid) != 0)
494                 errx(1, "initgroups");
495         if (setresuid(target, target, target) != 0)
496                 errx(1, "setresuid");
497 #endif
498
499         if (pledge("stdio rpath exec", NULL) == -1)
500                 err(1, "pledge");
501
502         if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
503                 cwd = "(failed)";
504         else
505                 cwd = cwdpath;
506
507         if (pledge("stdio exec", NULL) == -1)
508                 err(1, "pledge");
509
510         syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
511             myname, cmdline, pw->pw_name, cwd);
512
513         envp = prepenv(rule);
514
515         if (rule->cmd) {
516                 if (setenv("PATH", safepath, 1) == -1)
517                         err(1, "failed to set PATH '%s'", safepath);
518         }
519         execvpe(cmd, argv, envp);
520         if (errno == ENOENT)
521                 errx(1, "%s: command not found", cmd);
522         err(1, "%s", cmd);
523 }