1 /* $OpenBSD: parse.y,v 1.10 2015/07/24 06:36:42 zhuk Exp $ */
3 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
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.
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.
19 #include <sys/types.h>
48 #define YYSTYPE yystype
58 static void yyerror(const char *, ...);
59 static int yylex(void);
62 arraylen(const char **arr)
75 %token TPERMIT TDENY TAS TCMD TARGS
76 %token TNOPASS TNOLOG TPERSIST TKEEPENV TSETENV
87 rule: action ident target cmd {
89 r = calloc(1, sizeof(*r));
91 errx(1, "can't allocate rule");
92 r->action = $1.action;
93 r->options = $1.options;
94 r->envlist = $1.envlist;
98 r->cmdargs = $4.cmdargs;
99 if (nrules == maxrules) {
104 if (!(rules = reallocarray(rules, maxrules,
106 errx(1, "can't allocate rules");
111 action: TPERMIT options {
113 $$.options = $2.options;
114 $$.envlist = $2.envlist;
121 options: /* none */ {
125 $$.options = $1.options | $2.options;
126 $$.envlist = $1.envlist;
127 if (($$.options & (NOPASS|PERSIST)) == (NOPASS|PERSIST)) {
128 yyerror("can't combine nopass and persist");
133 yyerror("can't have two setenv sections");
136 $$.envlist = $2.envlist;
146 $$.options = PERSIST;
149 $$.options = KEEPENV;
151 } | TSETENV '{' strlist '}' {
153 $$.envlist = $3.strlist;
156 strlist: /* empty */ {
157 if (!($$.strlist = calloc(1, sizeof(char *))))
158 errx(1, "can't allocate strlist");
159 } | strlist TSTRING {
160 int nstr = arraylen($1.strlist);
161 if (!($$.strlist = reallocarray($1.strlist, nstr + 2,
163 errx(1, "can't allocate strlist");
164 $$.strlist[nstr] = $2.str;
165 $$.strlist[nstr + 1] = NULL;
173 target: /* optional */ {
179 cmd: /* optional */ {
182 } | TCMD TSTRING args {
184 $$.cmdargs = $3.cmdargs;
190 $$.cmdargs = $2.strlist;
196 yyerror(const char *fmt, ...)
200 fprintf(stderr, "doas: ");
202 vfprintf(stderr, fmt, va);
204 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
208 static struct keyword {
213 { "permit", TPERMIT },
217 { "nopass", TNOPASS },
219 { "persist", TPERSIST },
220 { "keepenv", TKEEPENV },
221 { "setenv", TSETENV },
227 char buf[1024], *ebuf, *p, *str;
228 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
231 ebuf = buf + sizeof(buf);
234 /* skip whitespace first */
235 for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
238 /* check for special one-character constructions */
248 /* skip comments; NUL is allowed; no continuation */
249 while ((c = getc(yyfp)) != '\n')
259 /* parsing next word */
260 for (;; c = getc(yyfp), yylval.colno++) {
263 yyerror("unallowed character NUL in column %d",
274 yyerror("unterminated quotes in column %d",
284 yyerror("unterminated escape in column %d",
287 yyerror("unterminated quotes in column %d",
296 if (!escape && !quotes)
311 yyerror("too long line");
321 * There could be a number of reasons for empty buffer,
322 * and we handle all of them here, to avoid cluttering
327 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
332 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
333 if (strcmp(buf, keywords[i].word) == 0)
334 return keywords[i].token;
337 if ((str = strdup(buf)) == NULL)
338 err(1, "%s", __func__);