1 /* $OpenBSD: parse.y,v 1.1 2015/07/16 20:44:21 tedu 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>
40 #define YYSTYPE yystype
49 %token TPERMIT TDENY TAS TCMD
50 %token TNOPASS TKEEPENV
60 rule: action ident target cmd {
62 r = calloc(1, sizeof(*r));
64 errx(1, "can't allocate rule");
65 r->action = $1.action;
66 r->options = $1.options;
67 r->envlist = $1.envlist;
71 if (nrules == maxrules) {
76 if (!(rules = reallocarray(rules, maxrules, sizeof(*rules))))
77 errx(1, "can't allocate rules");
82 action: TPERMIT options {
84 $$.options = $2.options;
85 $$.envlist = $2.envlist;
92 $$.options = $1.options | $2.options;
93 $$.envlist = $1.envlist;
96 errx(1, "can't have two keepenv sections");
98 $$.envlist = $2.envlist;
104 $$.options = KEEPENV;
105 } | TKEEPENV '{' envlist '}' {
106 $$.options = KEEPENV;
107 $$.envlist = $3.envlist;
110 envlist: /* empty */ {
111 if (!($$.envlist = calloc(1, sizeof(char *))))
112 errx(1, "can't allocate envlist");
113 } | envlist TSTRING {
114 int nenv = arraylen($1.envlist);
115 if (!($$.envlist = reallocarray($1.envlist, nenv + 2, sizeof(char *))))
116 errx(1, "can't allocate envlist");
117 $$.envlist[nenv] = $2.str;
118 $$.envlist[nenv + 1] = NULL;
126 target: /* optional */ {
132 cmd: /* optional */ {
141 yyerror(const char *fmt, ...)
146 fprintf(stderr, "doas: ");
147 vfprintf(stderr, fmt, va);
148 fprintf(stderr, "\n");
158 { "permit", TPERMIT },
161 { "nopass", TNOPASS },
162 { "keepenv", TKEEPENV },
168 char buf[1024], *ebuf, *p, *str;
172 ebuf = buf + sizeof(buf);
173 while ((c = getc(yyfp)) == ' ' || c == '\t')
181 while ((c = getc(yyfp)) != '\n' && c != EOF)
182 ; /* skip comments */
198 yyerror("too much stuff");
204 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
205 if (strcmp(buf, keywords[i].word) == 0)
206 return keywords[i].token;
208 if ((str = strdup(buf)) == NULL)