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>
31 __FBSDID("$FreeBSD$");
43 static struct cfjail *current_jail;
44 static struct cfjail *global_jail;
55 %token <cs> STR STR1 VAR VAR1
63 %lex-param { void *scanner }
64 %parse-param { void *scanner }
69 * A config file is a list of jails and parameters. Parameters are
70 * added to the current jail, otherwise to a global pesudo-jail.
76 if (!special_param($2, scanner)) {
77 struct cfjail *j = current_jail;
80 if (global_jail == NULL) {
81 global_jail = add_jail();
82 global_jail->name = estrdup("*");
86 TAILQ_INSERT_TAIL(&j->params, $2, tq);
92 jail : jail_name '{' conf '}'
94 current_jail = current_jail->cfparent;
100 struct cfjail *j = add_jail();
102 if (current_jail == NULL)
106 * A nested jail definition becomes
107 * a hierarchically-named sub-jail.
109 size_t parentlen = strlen(current_jail->name);
110 j->name = emalloc(parentlen + strlen($1) + 2);
111 strcpy(j->name, current_jail->name);
112 j->name[parentlen++] = '.';
113 strcpy(j->name + parentlen, $1);
116 j->cfparent = current_jail;
122 * Parameters have a name and an optional list of value strings,
123 * which may have "+=" or "=" preceding them.
132 TAILQ_CONCAT(&$$->val, $3, tq);
138 TAILQ_CONCAT(&$$->val, $3, tq);
139 $$->flags |= PF_APPEND;
145 TAILQ_CONCAT(&$$->val, $2, tq);
146 $$->flags |= PF_NAMEVAL;
153 * A parameter has a fixed name. A variable definition looks just like a
154 * parameter except that the name is a variable.
158 $$ = emalloc(sizeof(struct cfparam));
160 TAILQ_INIT(&$$->val);
165 $$ = emalloc(sizeof(struct cfparam));
167 TAILQ_INIT(&$$->val);
174 $$ = emalloc(sizeof(struct cfstrings));
176 TAILQ_INSERT_TAIL($$, $1, tq);
181 TAILQ_INSERT_TAIL($$, $3, tq);
186 * Strings may be passed in pieces, because of quoting and/or variable
187 * interpolation. Reassemble them into a single string.
191 $$ = emalloc(sizeof(struct cfstring));
193 $$->len = strlen($1);
194 STAILQ_INIT(&$$->vars);
200 $$ = emalloc(sizeof(struct cfstring));
203 STAILQ_INIT(&$$->vars);
204 v = emalloc(sizeof(struct cfvar));
207 STAILQ_INSERT_TAIL(&$$->vars, v, tq);
215 $$->s = erealloc($$->s, $$->len + len1 + 1);
216 strcpy($$->s + $$->len, $2);
225 v = emalloc(sizeof(struct cfvar));
228 STAILQ_INSERT_TAIL(&$$->vars, v, tq);
234 extern int YYLEX_DECL();
239 if (!yyget_text(scanner))
240 warnx("%s line %d: %s",
241 yyget_extra(scanner)->cfname, yyget_lineno(scanner), s);
242 else if (!yyget_text(scanner)[0])
243 warnx("%s: unexpected EOF",
244 yyget_extra(scanner)->cfname);
246 warnx("%s line %d: %s: %s",
247 yyget_extra(scanner)->cfname, yyget_lineno(scanner),
248 yyget_text(scanner), s);
251 /* Handle special parameters (i.e. the include directive).
252 * Return true if the parameter was specially handled.
255 special_param(struct cfparam *p, void *scanner)
257 if ((p->flags & (PF_VAR | PF_APPEND | PF_NAMEVAL)) != PF_NAMEVAL
258 || strcmp(p->name, ".include"))
261 TAILQ_FOREACH(s, &p->val, tq) {
262 if (STAILQ_EMPTY(&s->vars))
263 include_config(scanner, s->s);
266 "variables not permitted in '.include' filename",
267 yyget_extra(scanner)->cfname,
268 yyget_lineno(scanner));
269 yyget_extra(scanner)->error = 1;
272 free_param_strings(p);