]> git.armaanb.net Git - opendoas.git/blob - doas.c
Further improve syntax error reporting in doas:
[opendoas.git] / doas.c
1 /* $OpenBSD: doas.c,v 1.20 2015/07/22 16:35:03 zhuk 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] [-C config] [-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     const char **cmdargs, 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) {
121                 if (strcmp(r->cmd, cmd))
122                         return 0;
123                 if (r->cmdargs) {
124                         /* if arguments were given, they should match explicitly */
125                         for (i = 0; r->cmdargs[i]; i++) {
126                                 if (!cmdargs[i])
127                                         return 0;
128                                 if (strcmp(r->cmdargs[i], cmdargs[i]))
129                                         return 0;
130                         }
131                         if (cmdargs[i])
132                                 return 0;
133                 }
134         }
135         return 1;
136 }
137
138 static int
139 permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
140     uid_t target, const char *cmd, const char **cmdargs)
141 {
142         int i;
143
144         *lastr = NULL;
145         for (i = 0; i < nrules; i++) {
146                 if (match(uid, groups, ngroups, target, cmd, cmdargs, rules[i]))
147                         *lastr = rules[i];
148         }
149         if (!*lastr)
150                 return 0;
151         return (*lastr)->action == PERMIT;
152 }
153
154 static void
155 parseconfig(const char *filename)
156 {
157         extern FILE *yyfp;
158         extern int yyparse(void);
159         struct stat sb;
160
161         yyfp = fopen(filename, "r");
162         if (!yyfp) {
163                 fprintf(stderr, "doas is not enabled.\n");
164                 exit(1);
165         }
166
167         if (fstat(fileno(yyfp), &sb) != 0)
168                 err(1, "fstat(\"%s\")", filename);
169         if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
170                 errx(1, "%s is writable by group or other", filename);
171         if (sb.st_uid != 0)
172                 errx(1, "%s is not owned by root", filename);
173
174         yyparse();
175         fclose(yyfp);
176         if (parse_errors)
177                 exit(1);
178 }
179
180 static int
181 copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
182     char **envp, int ei)
183 {
184         int i;
185
186         for (i = 0; i < nsafe; i++) {
187                 const char **oe = oldenvp;
188                 while (*oe) {
189                         size_t len = strlen(safeset[i]);
190                         if (strncmp(*oe, safeset[i], len) == 0 &&
191                             (*oe)[len] == '=') {
192                                 if (!(envp[ei++] = strdup(*oe)))
193                                         err(1, "strdup");
194                                 break;
195                         }
196                         oe++;
197                 }
198         }
199         return ei;
200 }
201
202 static char **
203 copyenv(const char **oldenvp, struct rule *rule)
204 {
205         const char *safeset[] = {
206                 "DISPLAY", "HOME", "LOGNAME", "MAIL",
207                 "PATH", "TERM", "USER", "USERNAME",
208                 NULL
209         };
210         const char *badset[] = {
211                 "ENV",
212                 NULL
213         };
214         char **envp;
215         const char **extra;
216         int ei;
217         int nsafe, nbad;
218         int nextras = 0;
219
220         nbad = arraylen(badset);
221         if ((rule->options & KEEPENV) && !rule->envlist) {
222                 size_t i, ii;
223                 size_t oldlen = arraylen(oldenvp);
224                 envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
225                 if (!envp)
226                         err(1, "reallocarray");
227                 for (ii = i = 0; i < oldlen; i++) {
228                         size_t j;
229                         for (j = 0; j < nbad; j++) {
230                                 size_t len = strlen(badset[j]);
231                                 if (strncmp(oldenvp[i], badset[j], len) == 0 &&
232                                     oldenvp[i][len] == '=') {
233                                         break;
234                                 }
235                         }
236                         if (j == nbad) {
237                                 if (!(envp[ii] = strdup(oldenvp[i])))
238                                         err(1, "strdup");
239                                 ii++;
240                         }
241                 }
242                 envp[ii] = NULL;
243                 return envp;
244         }
245
246         nsafe = arraylen(safeset);
247         if ((extra = rule->envlist)) {
248                 size_t i;
249                 nextras = arraylen(extra);
250                 for (i = 0; i < nsafe; i++) {
251                         size_t j;
252                         for (j = 0; j < nextras; j++) {
253                                 if (strcmp(extra[j], safeset[i]) == 0) {
254                                         extra[j--] = extra[nextras--];
255                                         extra[nextras] = NULL;
256                                 }
257                         }
258                 }
259         }
260
261         envp = reallocarray(NULL, nsafe + nextras + 1, sizeof(char *));
262         if (!envp)
263                 err(1, "can't allocate new environment");
264
265         ei = 0;
266         ei = copyenvhelper(oldenvp, safeset, nsafe, envp, ei);
267         ei = copyenvhelper(oldenvp, rule->envlist, nextras, envp, ei);
268         envp[ei] = NULL;
269
270         return envp;
271 }
272
273 static void __dead
274 fail(void)
275 {
276         fprintf(stderr, "Permission denied\n");
277         exit(1);
278 }
279
280 int
281 main(int argc, char **argv, char **envp)
282 {
283         const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
284             "/usr/local/bin:/usr/local/sbin";
285         char *shargv[] = { NULL, NULL };
286         char *sh;
287         const char *cmd;
288         char cmdline[LINE_MAX];
289         char myname[_PW_NAME_LEN + 1];
290         struct passwd *pw;
291         struct rule *rule;
292         uid_t uid;
293         uid_t target = 0;
294         gid_t groups[NGROUPS_MAX + 1];
295         int ngroups;
296         int i, ch;
297         int sflag = 0;
298
299         uid = getuid();
300         while ((ch = getopt(argc, argv, "C:su:")) != -1) {
301                 switch (ch) {
302                 case 'C':
303                         setresuid(uid, uid, uid);
304                         parseconfig(optarg);
305                         exit(0);
306                 case 'u':
307                         if (parseuid(optarg, &target) != 0)
308                                 errx(1, "unknown user");
309                         break;
310                 case 's':
311                         sflag = 1;
312                         break;
313                 default:
314                         usage();
315                         break;
316                 }
317         }
318         argv += optind;
319         argc -= optind;
320
321         if ((!sflag && !argc) || (sflag && argc))
322                 usage();
323
324         parseconfig("/etc/doas.conf");
325
326         pw = getpwuid(uid);
327         if (!pw)
328                 err(1, "getpwuid failed");
329         if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
330                 errx(1, "pw_name too long");
331         ngroups = getgroups(NGROUPS_MAX, groups);
332         if (ngroups == -1)
333                 err(1, "can't get groups");
334         groups[ngroups++] = getgid();
335
336         if (sflag) {
337                 sh = getenv("SHELL");
338                 if (sh == NULL || *sh == '\0')
339                         shargv[0] = pw->pw_shell;
340                 else
341                         shargv[0] = sh;
342                 argv = shargv;
343                 argc = 1;
344         }
345
346         cmd = argv[0];
347         if (strlcpy(cmdline, argv[0], sizeof(cmdline)) >= sizeof(cmdline))
348                 errx(1, "command line too long");
349         for (i = 1; i < argc; i++) {
350                 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
351                         errx(1, "command line too long");
352                 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
353                         errx(1, "command line too long");
354         }
355
356         if (!permit(uid, groups, ngroups, &rule, target, cmd,
357             (const char**)argv + 1)) {
358                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
359                     "failed command for %s: %s", myname, cmdline);
360                 fail();
361         }
362
363         if (!(rule->options & NOPASS)) {
364                 if (!auth_userokay(myname, NULL, NULL, NULL)) {
365                         syslog(LOG_AUTHPRIV | LOG_NOTICE,
366                             "failed password for %s", myname);
367                         fail();
368                 }
369         }
370         envp = copyenv((const char **)envp, rule);
371
372         pw = getpwuid(target);
373         if (!pw)
374                 errx(1, "no passwd entry for target");
375         if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
376             LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
377             LOGIN_SETUSER) != 0)
378                 errx(1, "failed to set user context for target");
379
380         syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
381             myname, pw->pw_name, cmdline);
382         if (setenv("PATH", safepath, 1) == -1)
383                 err(1, "failed to set PATH '%s'", safepath);
384         execvpe(cmd, argv, envp);
385         if (errno == ENOENT)
386                 errx(1, "%s: command not found", cmd);
387         err(1, "%s", cmd);
388 }