]> git.armaanb.net Git - opendoas.git/blob - doas.c
check that badlisted env has = after the name
[opendoas.git] / doas.c
1 /* $OpenBSD: doas.c,v 1.12 2015/07/20 00:57:53 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 <login_cap.h>
23 #include <bsd_auth.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <err.h>
28 #include <unistd.h>
29 #include <pwd.h>
30 #include <grp.h>
31 #include <syslog.h>
32 #include <errno.h>
33
34 #include "doas.h"
35
36 static void __dead
37 usage(void)
38 {
39         fprintf(stderr, "usage: doas [-s] [-u user] command [args]\n");
40         exit(1);
41 }
42
43 size_t
44 arraylen(const char **arr)
45 {
46         size_t cnt = 0;
47
48         while (*arr) {
49                 cnt++;
50                 arr++;
51         }
52         return cnt;
53 }
54
55 static int
56 parseuid(const char *s, uid_t *uid)
57 {
58         struct passwd *pw;
59         const char *errstr;
60
61         if ((pw = getpwnam(s)) != NULL) {
62                 *uid = pw->pw_uid;
63                 return 0;
64         }
65         *uid = strtonum(s, 0, UID_MAX, &errstr);
66         if (errstr)
67                 return -1;
68         return 0;
69 }
70
71 static int
72 uidcheck(const char *s, uid_t desired)
73 {
74         uid_t uid;
75
76         if (parseuid(s, &uid) != 0)
77                 return -1;
78         if (uid != desired)
79                 return -1;
80         return 0;
81 }
82
83 static gid_t
84 strtogid(const char *s)
85 {
86         struct group *gr;
87         const char *errstr;
88         gid_t gid;
89
90         if ((gr = getgrnam(s)) != NULL)
91                 return gr->gr_gid;
92         gid = strtonum(s, 0, GID_MAX, &errstr);
93         if (errstr)
94                 return -1;
95         return gid;
96 }
97
98 static int
99 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
100     struct rule *r)
101 {
102         int i;
103
104         if (r->ident[0] == ':') {
105                 gid_t rgid = strtogid(r->ident + 1);
106                 if (rgid == -1)
107                         return 0;
108                 for (i = 0; i < ngroups; i++) {
109                         if (rgid == groups[i])
110                                 break;
111                 }
112                 if (i == ngroups)
113                         return 0;
114         } else {
115                 if (uidcheck(r->ident, uid) != 0)
116                         return 0;
117         }
118         if (r->target && uidcheck(r->target, target) != 0)
119                 return 0;
120         if (r->cmd && strcmp(r->cmd, cmd) != 0)
121                 return 0;
122         return 1;
123 }
124
125 static int
126 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
127     uid_t target, const char *cmd)
128 {
129         int i;
130
131         *lastr = NULL;
132         for (i = 0; i < nrules; i++) {
133                 if (match(uid, groups, ngroups, target, cmd, rules[i]))
134                         *lastr = rules[i];
135         }
136         if (!*lastr)
137                 return 0;
138         return (*lastr)->action == PERMIT;
139 }
140
141 static void
142 parseconfig(const char *filename)
143 {
144         extern FILE *yyfp;
145         extern int yyparse(void);
146         struct stat sb;
147
148         yyfp = fopen(filename, "r");
149         if (!yyfp) {
150                 fprintf(stderr, "doas is not enabled.\n");
151                 exit(1);
152         }
153
154         if (fstat(fileno(yyfp), &sb) != 0)
155                 err(1, "fstat(\"%s\")", filename);
156         if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
157                 errx(1, "%s is writable by group or other", filename);
158         if (sb.st_uid != 0)
159                 errx(1, "%s is not owned by root", filename);
160
161         yyparse();
162         fclose(yyfp);
163 }
164
165 static int
166 copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
167     char **envp, int ei)
168 {
169         int i;
170
171         for (i = 0; i < nsafe; i++) {
172                 const char **oe = oldenvp;
173                 while (*oe) {
174                         size_t len = strlen(safeset[i]);
175                         if (strncmp(*oe, safeset[i], len) == 0 &&
176                             (*oe)[len] == '=') {
177                                 if (!(envp[ei++] = strdup(*oe)))
178                                         err(1, "strdup");
179                                 break;
180                         }
181                         oe++;
182                 }
183         }
184         return ei;
185 }
186
187 static char **
188 copyenv(const char **oldenvp, struct rule *rule)
189 {
190         const char *safeset[] = {
191                 "DISPLAY", "HOME", "LOGNAME", "MAIL", "SHELL",
192                 "PATH", "TERM", "USER", "USERNAME",
193                 NULL
194         };
195         const char *badset[] = {
196                 "ENV",
197                 NULL
198         };
199         char **envp;
200         const char **extra;
201         int ei;
202         int nsafe, nbad;
203         int nextras = 0;
204         
205         nbad = arraylen(badset);
206         if ((rule->options & KEEPENV) && !rule->envlist) {
207                 size_t i, ii;
208                 size_t oldlen = arraylen(oldenvp);
209                 envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
210                 if (!envp)
211                         err(1, "reallocarray");
212                 for (ii = i = 0; i < oldlen; i++) {
213                         size_t j;
214                         for (j = 0; j < nbad; j++) {
215                                 size_t len = strlen(badset[j]);
216                                 if (strncmp(oldenvp[i], badset[j], len) == 0 &&
217                                     oldenvp[i][len] == '=') {
218                                         break;
219                                 }
220                         }
221                         if (j == nbad) {
222                                 if (!(envp[ii] = strdup(oldenvp[i])))
223                                         err(1, "strdup");
224                                 ii++;
225                         }
226                 }
227                 envp[ii] = NULL;
228                 return envp;
229         }
230
231         nsafe = arraylen(safeset);
232         if ((extra = rule->envlist)) {
233                 size_t i;
234                 nextras = arraylen(extra);
235                 for (i = 0; i < nsafe; i++) {
236                         size_t j;
237                         for (j = 0; j < nextras; j++) {
238                                 if (strcmp(extra[j], safeset[i]) == 0) {
239                                         extra[j--] = extra[nextras--];
240                                         extra[nextras] = NULL;
241                                 }
242                         }
243                 }
244         }
245
246         envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
247         if (!envp)
248                 err(1, "can't allocate new environment");
249
250         ei = 0;
251         ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
252         ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
253         envp[ei] = NULL;
254
255         return envp;
256 }
257
258 static void __dead
259 fail(void)
260 {
261         fprintf(stderr, "Permission denied\n");
262         exit(1);
263 }
264
265 int
266 main(int argc, char **argv, char **envp)
267 {
268         const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
269             "/usr/local/bin:/usr/local/sbin";
270         char *shargv[] = { NULL, NULL };
271         char *sh;
272         const char *cmd;
273         char cmdline[LINE_MAX];
274         char myname[_PW_NAME_LEN + 1];
275         struct passwd *pw;
276         struct rule *rule;
277         uid_t uid;
278         uid_t target = 0;
279         gid_t groups[NGROUPS_MAX + 1];
280         int ngroups;
281         int i, ch;
282         int sflag = 0;
283
284         parseconfig("/etc/doas.conf");
285
286         while ((ch = getopt(argc, argv, "su:")) != -1) {
287                 switch (ch) {
288                 case 'u':
289                         if (parseuid(optarg, &target) != 0)
290                                 errx(1, "unknown user");
291                         break;
292                 case 's':
293                         sflag = 1;
294                         break;
295                 default:
296                         usage();
297                         break;
298                 }
299         }
300         argv += optind;
301         argc -= optind;
302
303         if ((!sflag && !argc) || (sflag && argc))
304                 usage();
305
306         uid = getuid();
307         pw = getpwuid(uid);
308         if (!pw)
309                 err(1, "getpwuid failed");
310         if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
311                 errx(1, "pw_name too long");
312         ngroups = getgroups(NGROUPS_MAX, groups);
313         if (ngroups == -1)
314                 err(1, "can't get groups");
315         groups[ngroups++] = getgid();
316
317         if (sflag) {
318                 sh = getenv("SHELL");
319                 if (sh == NULL || *sh == '\0')
320                         shargv[0] = pw->pw_shell;
321                 else
322                         shargv[0] = sh;
323                 argv = shargv;
324                 argc = 1;
325         }
326
327         cmd = argv[0];
328         if (strlcpy(cmdline, argv[0], sizeof(cmdline)) >= sizeof(cmdline))
329                 errx(1, "command line too long");
330         for (i = 1; i < argc; i++) {
331                 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
332                         errx(1, "command line too long");
333                 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
334                         errx(1, "command line too long");
335         }
336
337         if (!permit(uid, groups, ngroups, &rule, target, cmd)) {
338                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
339                     "failed command for %s: %s", myname, cmdline);
340                 fail();
341         }
342
343         if (!(rule->options & NOPASS)) {
344                 if (!auth_userokay(myname, NULL, NULL, NULL)) {
345                         syslog(LOG_AUTHPRIV | LOG_NOTICE,
346                             "failed password for %s", myname);
347                         fail();
348                 }
349         }
350         envp = copyenv((const char **)envp, rule);
351
352         pw = getpwuid(target);
353         if (!pw)
354                 errx(1, "no passwd entry for target");
355         if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
356             LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
357             LOGIN_SETUSER) != 0)
358                 errx(1, "failed to set user context for target");
359
360         syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
361             myname, pw->pw_name, cmdline);
362         if (setenv("PATH", safepath, 1) == -1)
363                 err(1, "failed to set PATH '%s'", safepath);
364         execvpe(cmd, argv, envp);
365         if (errno == ENOENT)
366                 errx(1, "%s: command not found", cmd);
367         err(1, "%s", cmd);
368 }