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