]> git.armaanb.net Git - opendoas.git/blob - parse.y
add support for the verified auth ioctls using 'persist' rules. ok deraadt henning
[opendoas.git] / parse.y
1 /* $OpenBSD: parse.y,v 1.10 2015/07/24 06:36:42 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 %{
19 #include <sys/types.h>
20 #include <ctype.h>
21 #include <err.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "openbsd.h"
30
31 #include "doas.h"
32
33 typedef struct {
34         union {
35                 struct {
36                         int action;
37                         int options;
38                         const char *cmd;
39                         const char **cmdargs;
40                         const char **envlist;
41                 };
42                 const char *str;
43         };
44         int lineno;
45         int colno;
46 } yystype;
47 #define YYSTYPE yystype
48
49 FILE *yyfp;
50
51 struct rule **rules;
52 int nrules, maxrules;
53 int parse_errors = 0;
54 int obsolete_warned = 0;
55
56 void yyerror(const char *, ...);
57 int yylex(void);
58 int yyparse(void);
59
60 %}
61
62 %token TPERMIT TDENY TAS TCMD TARGS
63 %token TNOPASS TPERSIST TKEEPENV TSETENV
64 %token TSTRING
65
66 %%
67
68 grammar:        /* empty */
69                 | grammar '\n'
70                 | grammar rule '\n'
71                 | error '\n'
72                 ;
73
74 rule:           action ident target cmd {
75                         struct rule *r;
76                         r = calloc(1, sizeof(*r));
77                         if (!r)
78                                 errx(1, "can't allocate rule");
79                         r->action = $1.action;
80                         r->options = $1.options;
81                         r->envlist = $1.envlist;
82                         r->ident = $2.str;
83                         r->target = $3.str;
84                         r->cmd = $4.cmd;
85                         r->cmdargs = $4.cmdargs;
86                         if (nrules == maxrules) {
87                                 if (maxrules == 0)
88                                         maxrules = 63;
89                                 else
90                                         maxrules *= 2;
91                                 if (!(rules = reallocarray(rules, maxrules,
92                                     sizeof(*rules))))
93                                         errx(1, "can't allocate rules");
94                         }
95                         rules[nrules++] = r;
96                 } ;
97
98 action:         TPERMIT options {
99                         $$.action = PERMIT;
100                         $$.options = $2.options;
101                         $$.envlist = $2.envlist;
102                 } | TDENY {
103                         $$.action = DENY;
104                         $$.options = 0;
105                         $$.envlist = NULL;
106                 } ;
107
108 options:        /* none */ {
109                         $$.options = 0;
110                         $$.envlist = NULL;
111                 } | options option {
112                         $$.options = $1.options | $2.options;
113                         $$.envlist = $1.envlist;
114                         if ($2.envlist) {
115                                 if ($$.envlist) {
116                                         yyerror("can't have two setenv sections");
117                                         YYERROR;
118                                 } else
119                                         $$.envlist = $2.envlist;
120                         }
121                 } ;
122 option:         TNOPASS {
123                         $$.options = NOPASS;
124                         $$.envlist = NULL;
125                 } | TPERSIST {
126                         $$.options = PERSIST;
127                         $$.envlist = NULL;
128                 } | TKEEPENV {
129                         $$.options = KEEPENV;
130                         $$.envlist = NULL;
131                 } | TKEEPENV '{' envlist '}' {
132                         $$.options = 0;
133                         if (!obsolete_warned) {
134                                 warnx("keepenv with list is obsolete");
135                                 obsolete_warned = 1;
136                         }
137                         $$.envlist = $3.envlist;
138                 } | TSETENV '{' envlist '}' {
139                         $$.options = 0;
140                         $$.envlist = $3.envlist;
141                 } ;
142
143 envlist:        /* empty */ {
144                         $$.envlist = NULL;
145                 } | envlist TSTRING {
146                         int nenv = arraylen($1.envlist);
147                         if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
148                             sizeof(char *))))
149                                 errx(1, "can't allocate envlist");
150                         $$.envlist[nenv] = $2.str;
151                         $$.envlist[nenv + 1] = NULL;
152                 }
153
154
155 ident:          TSTRING {
156                         $$.str = $1.str;
157                 } ;
158
159 target:         /* optional */ {
160                         $$.str = NULL;
161                 } | TAS TSTRING {
162                         $$.str = $2.str;
163                 } ;
164
165 cmd:            /* optional */ {
166                         $$.cmd = NULL;
167                         $$.cmdargs = NULL;
168                 } | TCMD TSTRING args {
169                         $$.cmd = $2.str;
170                         $$.cmdargs = $3.cmdargs;
171                 } ;
172
173 args:           /* empty */ {
174                         $$.cmdargs = NULL;
175                 } | TARGS argslist {
176                         $$.cmdargs = $2.cmdargs;
177                 } ;
178
179 argslist:       /* empty */ {
180                         $$.cmdargs = NULL;
181                 } | argslist TSTRING {
182                         int nargs = arraylen($1.cmdargs);
183                         if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
184                             sizeof(char *))))
185                                 errx(1, "can't allocate args");
186                         $$.cmdargs[nargs] = $2.str;
187                         $$.cmdargs[nargs + 1] = NULL;
188                 } ;
189
190 %%
191
192 void
193 yyerror(const char *fmt, ...)
194 {
195         va_list va;
196
197         va_start(va, fmt);
198         vfprintf(stderr, fmt, va);
199         va_end(va);
200         fprintf(stderr, " at line %d\n", yylval.lineno + 1);
201         parse_errors++;
202 }
203
204 struct keyword {
205         const char *word;
206         int token;
207 } keywords[] = {
208         { "deny", TDENY },
209         { "permit", TPERMIT },
210         { "as", TAS },
211         { "cmd", TCMD },
212         { "args", TARGS },
213         { "nopass", TNOPASS },
214         { "persist", TPERSIST },
215         { "keepenv", TKEEPENV },
216         { "setenv", TSETENV },
217 };
218
219 int
220 yylex(void)
221 {
222         char buf[1024], *ebuf, *p, *str;
223         int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
224
225         p = buf;
226         ebuf = buf + sizeof(buf);
227
228 repeat:
229         /* skip whitespace first */
230         for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
231                 yylval.colno++;
232
233         /* check for special one-character constructions */
234         switch (c) {
235                 case '\n':
236                         yylval.colno = 0;
237                         yylval.lineno++;
238                         /* FALLTHROUGH */
239                 case '{':
240                 case '}':
241                         return c;
242                 case '#':
243                         /* skip comments; NUL is allowed; no continuation */
244                         while ((c = getc(yyfp)) != '\n')
245                                 if (c == EOF)
246                                         return 0;
247                         yylval.colno = 0;
248                         yylval.lineno++;
249                         return c;
250                 case EOF:
251                         return 0;
252         }
253
254         /* parsing next word */
255         for (;; c = getc(yyfp), yylval.colno++) {
256                 switch (c) {
257                 case '\0':
258                         yyerror("unallowed character NUL in column %d",
259                             yylval.colno + 1);
260                         escape = 0;
261                         continue;
262                 case '\\':
263                         escape = !escape;
264                         if (escape)
265                                 continue;
266                         break;
267                 case '\n':
268                         if (quotes)
269                                 yyerror("unterminated quotes in column %d",
270                                     qpos + 1);
271                         if (escape) {
272                                 nonkw = 1;
273                                 escape = 0;
274                                 continue;
275                         }
276                         goto eow;
277                 case EOF:
278                         if (escape)
279                                 yyerror("unterminated escape in column %d",
280                                     yylval.colno);
281                         if (quotes)
282                                 yyerror("unterminated quotes in column %d",
283                                     qpos + 1);
284                         goto eow;
285                         /* FALLTHROUGH */
286                 case '{':
287                 case '}':
288                 case '#':
289                 case ' ':
290                 case '\t':
291                         if (!escape && !quotes)
292                                 goto eow;
293                         break;
294                 case '"':
295                         if (!escape) {
296                                 quotes = !quotes;
297                                 if (quotes) {
298                                         nonkw = 1;
299                                         qpos = yylval.colno;
300                                 }
301                                 continue;
302                         }
303                 }
304                 *p++ = c;
305                 if (p == ebuf)
306                         yyerror("too long line");
307                 escape = 0;
308         }
309
310 eow:
311         *p = 0;
312         if (c != EOF)
313                 ungetc(c, yyfp);
314         if (p == buf) {
315                 /*
316                  * There could be a number of reasons for empty buffer,
317                  * and we handle all of them here, to avoid cluttering
318                  * the main loop.
319                  */
320                 if (c == EOF)
321                         return 0;
322                 else if (qpos == -1)    /* accept, e.g., empty args: cmd foo args "" */
323                         goto repeat;
324         }
325         if (!nonkw) {
326                 size_t i;
327                 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
328                         if (strcmp(buf, keywords[i].word) == 0)
329                                 return keywords[i].token;
330                 }
331         }
332         if ((str = strdup(buf)) == NULL)
333                 err(1, "strdup");
334         yylval.str = str;
335         return TSTRING;
336 }