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));
63 r->action = $1.action;
64 r->options = $1.options;
65 r->envlist = $1.envlist;
69 if (nrules == maxrules) {
74 if (!(rules = reallocarray(rules, maxrules, sizeof(*rules))))
75 errx(1, "can't allocate rules");
80 action: TPERMIT options {
82 $$.options = $2.options;
83 $$.envlist = $2.envlist;
90 $$.options = $1.options | $2.options;
91 $$.envlist = $1.envlist;
94 errx(1, "can't have two keepenv sections");
96 $$.envlist = $2.envlist;
102 $$.options = KEEPENV;
103 } | TKEEPENV '{' envlist '}' {
104 $$.options = KEEPENV;
105 $$.envlist = $3.envlist;
108 envlist: /* empty */ {
109 if (!($$.envlist = calloc(1, sizeof(char *))))
110 errx(1, "can't allocate envlist");
111 } | envlist TSTRING {
112 int nenv = arraylen($1.envlist);
113 if (!($$.envlist = reallocarray($1.envlist, nenv + 2, sizeof(char *))))
114 errx(1, "can't allocate envlist");
115 $$.envlist[nenv] = $2.str;
116 $$.envlist[nenv + 1] = NULL;
124 target: /* optional */ {
130 cmd: /* optional */ {
139 yyerror(const char *fmt, ...)
144 fprintf(stderr, "doas: ");
145 vfprintf(stderr, fmt, va);
146 fprintf(stderr, "\n");
156 { "permit", TPERMIT },
159 { "nopass", TNOPASS },
160 { "keepenv", TKEEPENV },
166 char buf[1024], *ebuf, *p, *str;
170 ebuf = buf + sizeof(buf);
171 while ((c = getc(yyfp)) == ' ' || c == '\t')
179 while ((c = getc(yyfp)) != '\n' && c != EOF)
180 ; /* skip comments */
196 yyerror("too much stuff");
202 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
203 if (strcmp(buf, keywords[i].word) == 0)
204 return keywords[i].token;
206 if ((str = strdup(buf)) == NULL)