3 * SPDX-License-Identifier: BSD-2-Clause
5 * Copyright (c) 2011 James Gritton
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
41 static struct cfjail *current_jail;
42 static struct cfjail *global_jail;
53 %token <cs> STR STR1 VAR VAR1
61 %lex-param { void *scanner }
62 %parse-param { void *scanner }
67 * A config file is a list of jails and parameters. Parameters are
68 * added to the current jail, otherwise to a global pesudo-jail.
74 if (!special_param($2, scanner)) {
75 struct cfjail *j = current_jail;
78 if (global_jail == NULL) {
79 global_jail = add_jail();
80 global_jail->name = estrdup("*");
84 TAILQ_INSERT_TAIL(&j->params, $2, tq);
90 jail : jail_name '{' conf '}'
92 current_jail = current_jail->cfparent;
98 struct cfjail *j = add_jail();
100 if (current_jail == NULL)
104 * A nested jail definition becomes
105 * a hierarchically-named sub-jail.
107 size_t parentlen = strlen(current_jail->name);
108 j->name = emalloc(parentlen + strlen($1) + 2);
109 strcpy(j->name, current_jail->name);
110 j->name[parentlen++] = '.';
111 strcpy(j->name + parentlen, $1);
114 j->cfparent = current_jail;
120 * Parameters have a name and an optional list of value strings,
121 * which may have "+=" or "=" preceding them.
130 TAILQ_CONCAT(&$$->val, $3, tq);
136 TAILQ_CONCAT(&$$->val, $3, tq);
137 $$->flags |= PF_APPEND;
143 TAILQ_CONCAT(&$$->val, $2, tq);
144 $$->flags |= PF_NAMEVAL;
151 * A parameter has a fixed name. A variable definition looks just like a
152 * parameter except that the name is a variable.
156 $$ = emalloc(sizeof(struct cfparam));
158 TAILQ_INIT(&$$->val);
163 $$ = emalloc(sizeof(struct cfparam));
165 TAILQ_INIT(&$$->val);
172 $$ = emalloc(sizeof(struct cfstrings));
174 TAILQ_INSERT_TAIL($$, $1, tq);
179 TAILQ_INSERT_TAIL($$, $3, tq);
184 * Strings may be passed in pieces, because of quoting and/or variable
185 * interpolation. Reassemble them into a single string.
189 $$ = emalloc(sizeof(struct cfstring));
191 $$->len = strlen($1);
192 STAILQ_INIT(&$$->vars);
198 $$ = emalloc(sizeof(struct cfstring));
201 STAILQ_INIT(&$$->vars);
202 v = emalloc(sizeof(struct cfvar));
205 STAILQ_INSERT_TAIL(&$$->vars, v, tq);
213 $$->s = erealloc($$->s, $$->len + len1 + 1);
214 strcpy($$->s + $$->len, $2);
223 v = emalloc(sizeof(struct cfvar));
226 STAILQ_INSERT_TAIL(&$$->vars, v, tq);
232 extern int YYLEX_DECL();
237 if (!yyget_text(scanner))
238 warnx("%s line %d: %s",
239 yyget_extra(scanner)->cfname, yyget_lineno(scanner), s);
240 else if (!yyget_text(scanner)[0])
241 warnx("%s: unexpected EOF",
242 yyget_extra(scanner)->cfname);
244 warnx("%s line %d: %s: %s",
245 yyget_extra(scanner)->cfname, yyget_lineno(scanner),
246 yyget_text(scanner), s);
249 /* Handle special parameters (i.e. the include directive).
250 * Return true if the parameter was specially handled.
253 special_param(struct cfparam *p, void *scanner)
255 if ((p->flags & (PF_VAR | PF_APPEND | PF_NAMEVAL)) != PF_NAMEVAL
256 || strcmp(p->name, ".include"))
259 TAILQ_FOREACH(s, &p->val, tq) {
260 if (STAILQ_EMPTY(&s->vars))
261 include_config(scanner, s->s);
264 "variables not permitted in '.include' filename",
265 yyget_extra(scanner)->cfname,
266 yyget_lineno(scanner));
267 yyget_extra(scanner)->error = 1;
270 free_param_strings(p);