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.
21 #include <sys/types.h>
50 #define YYSTYPE yystype
60 static void yyerror(const char *, ...);
61 static int yylex(void);
64 arraylen(const char **arr)
77 %token TPERMIT TDENY TAS TCMD TARGS
78 %token TNOPASS TNOLOG TPERSIST TKEEPENV TSETENV
89 rule: action ident target cmd {
91 r = calloc(1, sizeof(*r));
93 errx(1, "can't allocate rule");
94 r->action = $1.action;
95 r->options = $1.options;
96 r->envlist = $1.envlist;
100 r->cmdargs = $4.cmdargs;
101 if (nrules == maxrules) {
106 if (!(rules = reallocarray(rules, maxrules,
108 errx(1, "can't allocate rules");
113 action: TPERMIT options {
115 $$.options = $2.options;
116 $$.envlist = $2.envlist;
123 options: /* none */ {
127 $$.options = $1.options | $2.options;
128 $$.envlist = $1.envlist;
129 if (($$.options & (NOPASS|PERSIST)) == (NOPASS|PERSIST)) {
130 yyerror("can't combine nopass and persist");
135 yyerror("can't have two setenv sections");
138 $$.envlist = $2.envlist;
148 $$.options = PERSIST;
151 $$.options = KEEPENV;
153 } | TSETENV '{' strlist '}' {
155 $$.envlist = $3.strlist;
158 strlist: /* empty */ {
159 if (!($$.strlist = calloc(1, sizeof(char *))))
160 errx(1, "can't allocate strlist");
161 } | strlist TSTRING {
162 int nstr = arraylen($1.strlist);
163 if (!($$.strlist = reallocarray($1.strlist, nstr + 2,
165 errx(1, "can't allocate strlist");
166 $$.strlist[nstr] = $2.str;
167 $$.strlist[nstr + 1] = NULL;
175 target: /* optional */ {
181 cmd: /* optional */ {
184 } | TCMD TSTRING args {
186 $$.cmdargs = $3.cmdargs;
192 $$.cmdargs = $2.strlist;
198 yyerror(const char *fmt, ...)
202 fprintf(stderr, "doas: ");
204 vfprintf(stderr, fmt, va);
206 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
210 static struct keyword {
215 { "permit", TPERMIT },
219 { "nopass", TNOPASS },
221 { "persist", TPERSIST },
222 { "keepenv", TKEEPENV },
223 { "setenv", TSETENV },
229 char buf[1024], *ebuf, *p, *str;
230 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
233 ebuf = buf + sizeof(buf);
236 /* skip whitespace first */
237 for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
240 /* check for special one-character constructions */
250 /* skip comments; NUL is allowed; no continuation */
251 while ((c = getc(yyfp)) != '\n')
261 /* parsing next word */
262 for (;; c = getc(yyfp), yylval.colno++) {
265 yyerror("unallowed character NUL in column %d",
276 yyerror("unterminated quotes in column %d",
286 yyerror("unterminated escape in column %d",
289 yyerror("unterminated quotes in column %d",
298 if (!escape && !quotes)
313 yyerror("too long line");
323 * There could be a number of reasons for empty buffer,
324 * and we handle all of them here, to avoid cluttering
329 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
334 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
335 if (strcmp(buf, keywords[i].word) == 0)
336 return keywords[i].token;
339 if ((str = strdup(buf)) == NULL)
340 err(1, "%s", __func__);