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