]> git.armaanb.net Git - opendoas.git/blob - parse.y
revise environment handling. Add a setenv keyword for manipulating the environment...
[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 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                 } | TKEEPENV {
126                         $$.options = KEEPENV;
127                         $$.envlist = NULL;
128                 } | TKEEPENV '{' envlist '}' {
129                         $$.options = 0;
130                         if (!obsolete_warned) {
131                                 warnx("keepenv with list is obsolete");
132                                 obsolete_warned = 1;
133                         }
134                         $$.envlist = $3.envlist;
135                 } | TSETENV '{' envlist '}' {
136                         $$.options = 0;
137                         $$.envlist = $3.envlist;
138                 } ;
139
140 envlist:        /* empty */ {
141                         $$.envlist = NULL;
142                 } | envlist TSTRING {
143                         int nenv = arraylen($1.envlist);
144                         if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
145                             sizeof(char *))))
146                                 errx(1, "can't allocate envlist");
147                         $$.envlist[nenv] = $2.str;
148                         $$.envlist[nenv + 1] = NULL;
149                 }
150
151
152 ident:          TSTRING {
153                         $$.str = $1.str;
154                 } ;
155
156 target:         /* optional */ {
157                         $$.str = NULL;
158                 } | TAS TSTRING {
159                         $$.str = $2.str;
160                 } ;
161
162 cmd:            /* optional */ {
163                         $$.cmd = NULL;
164                         $$.cmdargs = NULL;
165                 } | TCMD TSTRING args {
166                         $$.cmd = $2.str;
167                         $$.cmdargs = $3.cmdargs;
168                 } ;
169
170 args:           /* empty */ {
171                         $$.cmdargs = NULL;
172                 } | TARGS argslist {
173                         $$.cmdargs = $2.cmdargs;
174                 } ;
175
176 argslist:       /* empty */ {
177                         $$.cmdargs = NULL;
178                 } | argslist TSTRING {
179                         int nargs = arraylen($1.cmdargs);
180                         if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
181                             sizeof(char *))))
182                                 errx(1, "can't allocate args");
183                         $$.cmdargs[nargs] = $2.str;
184                         $$.cmdargs[nargs + 1] = NULL;
185                 } ;
186
187 %%
188
189 void
190 yyerror(const char *fmt, ...)
191 {
192         va_list va;
193
194         va_start(va, fmt);
195         vfprintf(stderr, fmt, va);
196         va_end(va);
197         fprintf(stderr, " at line %d\n", yylval.lineno + 1);
198         parse_errors++;
199 }
200
201 struct keyword {
202         const char *word;
203         int token;
204 } keywords[] = {
205         { "deny", TDENY },
206         { "permit", TPERMIT },
207         { "as", TAS },
208         { "cmd", TCMD },
209         { "args", TARGS },
210         { "nopass", TNOPASS },
211         { "keepenv", TKEEPENV },
212         { "setenv", TSETENV },
213 };
214
215 int
216 yylex(void)
217 {
218         char buf[1024], *ebuf, *p, *str;
219         int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
220
221         p = buf;
222         ebuf = buf + sizeof(buf);
223
224 repeat:
225         /* skip whitespace first */
226         for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
227                 yylval.colno++;
228
229         /* check for special one-character constructions */
230         switch (c) {
231                 case '\n':
232                         yylval.colno = 0;
233                         yylval.lineno++;
234                         /* FALLTHROUGH */
235                 case '{':
236                 case '}':
237                         return c;
238                 case '#':
239                         /* skip comments; NUL is allowed; no continuation */
240                         while ((c = getc(yyfp)) != '\n')
241                                 if (c == EOF)
242                                         return 0;
243                         yylval.colno = 0;
244                         yylval.lineno++;
245                         return c;
246                 case EOF:
247                         return 0;
248         }
249
250         /* parsing next word */
251         for (;; c = getc(yyfp), yylval.colno++) {
252                 switch (c) {
253                 case '\0':
254                         yyerror("unallowed character NUL in column %d",
255                             yylval.colno + 1);
256                         escape = 0;
257                         continue;
258                 case '\\':
259                         escape = !escape;
260                         if (escape)
261                                 continue;
262                         break;
263                 case '\n':
264                         if (quotes)
265                                 yyerror("unterminated quotes in column %d",
266                                     qpos + 1);
267                         if (escape) {
268                                 nonkw = 1;
269                                 escape = 0;
270                                 continue;
271                         }
272                         goto eow;
273                 case EOF:
274                         if (escape)
275                                 yyerror("unterminated escape in column %d",
276                                     yylval.colno);
277                         if (quotes)
278                                 yyerror("unterminated quotes in column %d",
279                                     qpos + 1);
280                         goto eow;
281                         /* FALLTHROUGH */
282                 case '{':
283                 case '}':
284                 case '#':
285                 case ' ':
286                 case '\t':
287                         if (!escape && !quotes)
288                                 goto eow;
289                         break;
290                 case '"':
291                         if (!escape) {
292                                 quotes = !quotes;
293                                 if (quotes) {
294                                         nonkw = 1;
295                                         qpos = yylval.colno;
296                                 }
297                                 continue;
298                         }
299                 }
300                 *p++ = c;
301                 if (p == ebuf)
302                         yyerror("too long line");
303                 escape = 0;
304         }
305
306 eow:
307         *p = 0;
308         if (c != EOF)
309                 ungetc(c, yyfp);
310         if (p == buf) {
311                 /*
312                  * There could be a number of reasons for empty buffer,
313                  * and we handle all of them here, to avoid cluttering
314                  * the main loop.
315                  */
316                 if (c == EOF)
317                         return 0;
318                 else if (qpos == -1)    /* accept, e.g., empty args: cmd foo args "" */
319                         goto repeat;
320         }
321         if (!nonkw) {
322                 size_t i;
323                 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
324                         if (strcmp(buf, keywords[i].word) == 0)
325                                 return keywords[i].token;
326                 }
327         }
328         if ((str = strdup(buf)) == NULL)
329                 err(1, "strdup");
330         yylval.str = str;
331         return TSTRING;
332 }