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