]> git.armaanb.net Git - opendoas.git/blob - doas.c
The string with path to shell could be taken directly from struct passwd. At some...
[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 recvfd 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] = strdup(pw->pw_shell);
316                         if (shargv[0] == NULL)
317                                 err(1, NULL);
318                 } else
319                         shargv[0] = sh;
320                 argv = shargv;
321                 argc = 1;
322         }
323
324         if (confpath) {
325                 checkconfig(confpath, argc, argv, uid, groups, ngroups,
326                     target);
327                 exit(1);        /* fail safe */
328         }
329
330         parseconfig("/etc/doas.conf", 1);
331
332         /* cmdline is used only for logging, no need to abort on truncate */
333         (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
334         for (i = 1; i < argc; i++) {
335                 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
336                         break;
337                 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
338                         break;
339         }
340
341         cmd = argv[0];
342         if (!permit(uid, groups, ngroups, &rule, target, cmd,
343             (const char **)argv + 1)) {
344                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
345                     "failed command for %s: %s", myname, cmdline);
346                 errc(1, EPERM, NULL);
347         }
348
349 #ifdef HAVE_BSD_AUTH_H
350         if (!(rule->options & NOPASS)) {
351                 if (nflag)
352                         errx(1, "Authorization required");
353
354                 char *challenge = NULL, *response, rbuf[1024], cbuf[128];
355                 auth_session_t *as;
356
357                 if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
358                     &challenge)))
359                         errx(1, "Authorization failed");
360                 if (!challenge) {
361                         char host[HOST_NAME_MAX + 1];
362                         if (gethostname(host, sizeof(host)))
363                                 snprintf(host, sizeof(host), "?");
364                         snprintf(cbuf, sizeof(cbuf),
365                             "\rdoas (%.32s@%.32s) password: ", myname, host);
366                         challenge = cbuf;
367                 }
368                 response = readpassphrase(challenge, rbuf, sizeof(rbuf),
369                     RPP_REQUIRE_TTY);
370                 if (response == NULL && errno == ENOTTY) {
371                         syslog(LOG_AUTHPRIV | LOG_NOTICE,
372                             "tty required for %s", myname);
373                         errx(1, "a tty is required");
374                 }
375                 if (!auth_userresponse(as, response, 0)) {
376                         syslog(LOG_AUTHPRIV | LOG_NOTICE,
377                             "failed auth for %s", myname);
378                         errc(1, EPERM, NULL);
379                 }
380                 explicit_bzero(rbuf, sizeof(rbuf));
381         }
382 #elif HAVE_PAM_APPL_H
383         pw = getpwuid(target);
384         if (!pw)
385                 errx(1, "no passwd entry for target");
386
387         if (!pamauth(pw->pw_name, myname, !nflag, rule->options & NOPASS)) {
388                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
389                 errc(1, EPERM, NULL);
390         }
391 #elif HAVE_SHADOW_H
392         const char *pass;
393
394         if (!(rule->options & NOPASS)) {
395                 if (nflag)
396                         errx(1, "Authorization required");
397
398                 pass = pw->pw_passwd;
399                 if (pass[0] == 'x' && pass[1] == '\0') {
400                         struct spwd *sp;
401                         if (!(sp = getspnam(myname)))
402                                 errx(1, "Authorization failed");
403                         pass = sp->sp_pwdp;
404                 }
405
406                 char *challenge, *response, rbuf[1024], cbuf[128], host[HOST_NAME_MAX + 1];
407                 if (gethostname(host, sizeof(host)))
408                         snprintf(host, sizeof(host), "?");
409                 snprintf(cbuf, sizeof(cbuf),
410                                 "\rdoas (%.32s@%.32s) password: ", myname, host);
411                 challenge = cbuf;
412
413                 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
414                 if (response == NULL && errno == ENOTTY) {
415                         syslog(LOG_AUTHPRIV | LOG_NOTICE,
416                             "tty required for %s", myname);
417                         errx(1, "a tty is required");
418                 }
419                 if (strcmp(crypt(response, pass), pass) != 0) {
420                         syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
421                         errc(1, EPERM, NULL);
422                 }
423                 explicit_bzero(rbuf, sizeof(rbuf));
424         }
425 #else
426         if (!(rule->options & NOPASS))
427                 errx(1, "Authorization required");
428 #endif /* HAVE_BSD_AUTH_H */
429
430         if (pledge("stdio rpath getpw exec id", NULL) == -1)
431                 err(1, "pledge");
432
433         pw = getpwuid(target);
434         if (!pw)
435                 errx(1, "no passwd entry for target");
436
437 #ifdef HAVE_BSD_AUTH_H
438         if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
439             LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
440             LOGIN_SETUSER) != 0)
441                 errx(1, "failed to set user context for target");
442 #else
443         if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
444                 errx(1, "setresgid");
445         if (initgroups(pw->pw_name, pw->pw_gid) != 0)
446                 errx(1, "initgroups");
447         if (setresuid(target, target, target) != 0)
448                 errx(1, "setresuid");
449 #endif
450
451         if (pledge("stdio rpath exec", NULL) == -1)
452                 err(1, "pledge");
453
454         if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
455                 cwd = "(failed)";
456         else
457                 cwd = cwdpath;
458
459         if (pledge("stdio exec", NULL) == -1)
460                 err(1, "pledge");
461
462         syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
463             myname, cmdline, pw->pw_name, cwd);
464
465         envp = prepenv(rule);
466
467         if (rule->cmd) {
468                 if (setenv("PATH", safepath, 1) == -1)
469                         err(1, "failed to set PATH '%s'", safepath);
470         }
471         execvpe(cmd, argv, envp);
472         if (errno == ENOENT)
473                 errx(1, "%s: command not found", cmd);
474         err(1, "%s", cmd);
475 }