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