]> git.armaanb.net Git - opendoas.git/blob - doas.c
Change binary permissions to 4755. Closes #26
[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 #include <fcntl.h>
33
34 #include "includes.h"
35 #include "doas.h"
36
37 static void __dead
38 usage(void)
39 {
40         fprintf(stderr, "usage: doas [-Lns] [-C config] [-u user]"
41             " command [args]\n");
42         exit(1);
43 }
44
45 static int
46 parseuid(const char *s, uid_t *uid)
47 {
48         struct passwd *pw;
49         const char *errstr;
50
51         if ((pw = getpwnam(s)) != NULL) {
52                 *uid = pw->pw_uid;
53                 if (*uid == UID_MAX)
54                         return -1;
55                 return 0;
56         }
57         *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
58         if (errstr)
59                 return -1;
60         return 0;
61 }
62
63 static int
64 uidcheck(const char *s, uid_t desired)
65 {
66         uid_t uid;
67
68         if (parseuid(s, &uid) != 0)
69                 return -1;
70         if (uid != desired)
71                 return -1;
72         return 0;
73 }
74
75 static int
76 parsegid(const char *s, gid_t *gid)
77 {
78         struct group *gr;
79         const char *errstr;
80
81         if ((gr = getgrnam(s)) != NULL) {
82                 *gid = gr->gr_gid;
83                 if (*gid == GID_MAX)
84                         return -1;
85                 return 0;
86         }
87         *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
88         if (errstr)
89                 return -1;
90         return 0;
91 }
92
93 static int
94 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
95     const char **cmdargs, struct rule *r)
96 {
97         int i;
98
99         if (r->ident[0] == ':') {
100                 gid_t rgid;
101                 if (parsegid(r->ident + 1, &rgid) == -1)
102                         return 0;
103                 for (i = 0; i < ngroups; i++) {
104                         if (rgid == groups[i])
105                                 break;
106                 }
107                 if (i == ngroups)
108                         return 0;
109         } else {
110                 if (uidcheck(r->ident, uid) != 0)
111                         return 0;
112         }
113         if (r->target && uidcheck(r->target, target) != 0)
114                 return 0;
115         if (r->cmd) {
116                 if (strcmp(r->cmd, cmd))
117                         return 0;
118                 if (r->cmdargs) {
119                         /* if arguments were given, they should match explicitly */
120                         for (i = 0; r->cmdargs[i]; i++) {
121                                 if (!cmdargs[i])
122                                         return 0;
123                                 if (strcmp(r->cmdargs[i], cmdargs[i]))
124                                         return 0;
125                         }
126                         if (cmdargs[i])
127                                 return 0;
128                 }
129         }
130         return 1;
131 }
132
133 static int
134 permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
135     uid_t target, const char *cmd, const char **cmdargs)
136 {
137         int i;
138
139         *lastr = NULL;
140         for (i = 0; i < nrules; i++) {
141                 if (match(uid, groups, ngroups, target, cmd,
142                     cmdargs, rules[i]))
143                         *lastr = rules[i];
144         }
145         if (!*lastr)
146                 return 0;
147         return (*lastr)->action == PERMIT;
148 }
149
150 static void
151 parseconfig(const char *filename, int checkperms)
152 {
153         extern FILE *yyfp;
154         extern int yyparse(void);
155         struct stat sb;
156
157         yyfp = fopen(filename, "r");
158         if (!yyfp)
159                 err(1, checkperms ? "doas is not enabled, %s" :
160                     "could not open config file %s", filename);
161
162         if (checkperms) {
163                 if (fstat(fileno(yyfp), &sb) != 0)
164                         err(1, "fstat(\"%s\")", filename);
165                 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
166                         errx(1, "%s is writable by group or other", filename);
167                 if (sb.st_uid != 0)
168                         errx(1, "%s is not owned by root", filename);
169         }
170
171         yyparse();
172         fclose(yyfp);
173         if (parse_errors)
174                 exit(1);
175 }
176
177 static void __dead
178 checkconfig(const char *confpath, int argc, char **argv,
179     uid_t uid, gid_t *groups, int ngroups, uid_t target)
180 {
181         const struct rule *rule;
182
183         if (setresuid(uid, uid, uid) != 0)
184                 err(1, "setresuid");
185
186         parseconfig(confpath, 0);
187         if (!argc)
188                 exit(0);
189
190         if (permit(uid, groups, ngroups, &rule, target, argv[0],
191             (const char **)argv + 1)) {
192                 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
193                 exit(0);
194         } else {
195                 printf("deny\n");
196                 exit(1);
197         }
198 }
199
200 int
201 mygetpwuid_r(uid_t uid, struct passwd *pwd, struct passwd **result)
202 {
203         int rv;
204         char *buf;
205         static long pwsz = 0;
206         size_t buflen;
207
208         if (pwsz == 0)
209                 pwsz = sysconf(_SC_GETPW_R_SIZE_MAX);
210
211         buflen = pwsz > 0 ? pwsz : 1024;
212
213         buf = malloc(buflen);
214         if (buf == NULL)
215                 return errno;
216
217         while ((rv = getpwuid_r(uid, pwd, buf, buflen, result)) == ERANGE) {
218                 size_t newsz;
219                 newsz = buflen * 2;
220                 if (newsz < buflen)
221                         return rv;
222                 buflen = newsz;
223                 buf = realloc(buf, buflen);
224                 if (buf == NULL)
225                         return errno;
226         }
227
228         return rv;
229 }
230
231 int
232 main(int argc, char **argv)
233 {
234         const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
235             "/usr/local/bin:/usr/local/sbin";
236         const char *confpath = NULL;
237         char *shargv[] = { NULL, NULL };
238         char *sh;
239         const char *p;
240         const char *cmd;
241         char cmdline[LINE_MAX];
242         struct passwd mypwstore, targpwstore;
243         struct passwd *mypw, *targpw;
244         const struct rule *rule;
245         uid_t uid;
246         uid_t target = 0;
247         gid_t groups[NGROUPS_MAX + 1];
248         int ngroups;
249         int i, ch, rv;
250         int sflag = 0;
251         int nflag = 0;
252         char cwdpath[PATH_MAX];
253         const char *cwd;
254         char **envp;
255
256         setprogname("doas");
257
258         closefrom(STDERR_FILENO + 1);
259
260         uid = getuid();
261
262         while ((ch = getopt(argc, argv, "+C:Lnsu:")) != -1) {
263                 switch (ch) {
264                 case 'C':
265                         confpath = optarg;
266                         break;
267                 case 'L':
268 #if defined(USE_TIMESTAMP)
269                         exit(timestamp_clear() == -1);
270 #else
271                         exit(0);
272 #endif
273                 case 'u':
274                         if (parseuid(optarg, &target) != 0)
275                                 errx(1, "unknown user");
276                         break;
277                 case 'n':
278                         nflag = 1;
279                         break;
280                 case 's':
281                         sflag = 1;
282                         break;
283                 default:
284                         usage();
285                         break;
286                 }
287         }
288         argv += optind;
289         argc -= optind;
290
291         if (confpath) {
292                 if (sflag)
293                         usage();
294         } else if ((!sflag && !argc) || (sflag && argc))
295                 usage();
296
297         rv = mygetpwuid_r(uid, &mypwstore, &mypw);
298         if (rv != 0)
299                 err(1, "getpwuid_r failed");
300         if (mypw == NULL)
301                 errx(1, "no passwd entry for self");
302         ngroups = getgroups(NGROUPS_MAX, groups);
303         if (ngroups == -1)
304                 err(1, "can't get groups");
305         groups[ngroups++] = getgid();
306
307         if (sflag) {
308                 sh = getenv("SHELL");
309                 if (sh == NULL || *sh == '\0') {
310                         shargv[0] = mypw->pw_shell;
311                 } else
312                         shargv[0] = sh;
313                 argv = shargv;
314                 argc = 1;
315         }
316
317         if (confpath) {
318                 checkconfig(confpath, argc, argv, uid, groups, ngroups,
319                     target);
320                 exit(1);        /* fail safe */
321         }
322
323         if (geteuid())
324                 errx(1, "not installed setuid");
325
326         parseconfig("/etc/doas.conf", 1);
327
328         /* cmdline is used only for logging, no need to abort on truncate */
329         (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
330         for (i = 1; i < argc; i++) {
331                 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
332                         break;
333                 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
334                         break;
335         }
336
337         cmd = argv[0];
338         if (!permit(uid, groups, ngroups, &rule, target, cmd,
339             (const char **)argv + 1)) {
340                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
341                     "failed command for %s: %s", mypw->pw_name, cmdline);
342                 errc(1, EPERM, NULL);
343         }
344
345 #if defined(USE_SHADOW)
346         if (!(rule->options & NOPASS)) {
347                 if (nflag)
348                         errx(1, "Authorization required");
349
350                 shadowauth(mypw->pw_name, rule->options & PERSIST);
351         }
352 #elif !defined(USE_PAM)
353         /* no authentication provider, only allow NOPASS rules */
354         (void) nflag;
355         if (!(rule->options & NOPASS))
356                 errx(1, "Authorization required");
357 #endif
358
359         if ((p = getenv("PATH")) != NULL)
360                 formerpath = strdup(p);
361         if (formerpath == NULL)
362                 formerpath = "";
363
364         if (rule->cmd) {
365                 if (setenv("PATH", safepath, 1) == -1)
366                         err(1, "failed to set PATH '%s'", safepath);
367         }
368
369         rv = mygetpwuid_r(target, &targpwstore, &targpw);
370         if (rv != 0)
371                 err(1, "getpwuid_r failed");
372         if (targpw == NULL)
373                 errx(1, "no passwd entry for target");
374
375 #if defined(USE_PAM)
376         pamauth(targpw->pw_name, mypw->pw_name, !nflag, rule->options & NOPASS,
377             rule->options & PERSIST);
378 #endif
379
380         if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
381                 err(1, "setresgid");
382         if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
383                 err(1, "initgroups");
384         if (setresuid(target, target, target) != 0)
385                 err(1, "setresuid");
386
387         if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
388                 cwd = "(failed)";
389         else
390                 cwd = cwdpath;
391
392         syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
393             mypw->pw_name, cmdline, targpw->pw_name, cwd);
394
395         envp = prepenv(rule, mypw, targpw);
396
397         /* setusercontext set path for the next process, so reset it for us */
398         if (rule->cmd) {
399                 if (setenv("PATH", safepath, 1) == -1)
400                         err(1, "failed to set PATH '%s'", safepath);
401         } else {
402                 if (setenv("PATH", formerpath, 1) == -1)
403                         err(1, "failed to set PATH '%s'", formerpath);
404         }
405         execvpe(cmd, argv, envp);
406         if (errno == ENOENT)
407                 errx(1, "%s: command not found", cmd);
408         err(1, "%s", cmd);
409 }