13b31921jamie%{
23b31921jamie/*-
37551d83pfg * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
47551d83pfg *
58d425bfjamie * Copyright (c) 2011 James Gritton
63b31921jamie * All rights reserved.
73b31921jamie *
83b31921jamie * Redistribution and use in source and binary forms, with or without
93b31921jamie * modification, are permitted provided that the following conditions
103b31921jamie * are met:
113b31921jamie * 1. Redistributions of source code must retain the above copyright
123b31921jamie *    notice, this list of conditions and the following disclaimer.
133b31921jamie * 2. Redistributions in binary form must reproduce the above copyright
143b31921jamie *    notice, this list of conditions and the following disclaimer in the
153b31921jamie *    documentation and/or other materials provided with the distribution.
163b31921jamie *
173b31921jamie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
183b31921jamie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193b31921jamie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203b31921jamie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
213b31921jamie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223b31921jamie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233b31921jamie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243b31921jamie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253b31921jamie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263b31921jamie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273b31921jamie * SUCH DAMAGE.
283b31921jamie */
293b31921jamie
303b31921jamie#include <sys/cdefs.h>
313b31921jamie__FBSDID("$FreeBSD$");
323b31921jamie
333b31921jamie#include <stdlib.h>
343b31921jamie#include <string.h>
353b31921jamie
363b31921jamie#include "jailp.h"
373b31921jamie
383b31921jamie#ifdef DEBUG
393b31921jamie#define YYDEBUG 1
403b31921jamie#endif
413b31921jamie%}
423b31921jamie
433b31921jamie%union {
443b31921jamie	struct cfjail		*j;
453b31921jamie	struct cfparams		*pp;
463b31921jamie	struct cfparam		*p;
473b31921jamie	struct cfstrings	*ss;
483b31921jamie	struct cfstring		*s;
493b31921jamie	char			*cs;
503b31921jamie}
513b31921jamie
523b31921jamie%token      PLEQ
533b31921jamie%token <cs> STR STR1 VAR VAR1
543b31921jamie
553b31921jamie%type <j>  jail
563b31921jamie%type <pp> param_l
573b31921jamie%type <p>  param name
583b31921jamie%type <ss> value
593b31921jamie%type <s>  string
603b31921jamie
613b31921jamie%%
623b31921jamie
633b31921jamie/*
643b31921jamie * A config file is a series of jails (containing parameters) and jail-less
65c62d640pfg * parameters which really belong to a global pseudo-jail.
663b31921jamie */
673b31921jamieconf	:
683b31921jamie	;
693b31921jamie	| conf jail
703b31921jamie	;
713b31921jamie	| conf param ';'
723b31921jamie	{
733b31921jamie		struct cfjail *j;
743b31921jamie
753b31921jamie		j = TAILQ_LAST(&cfjails, cfjails);
763b31921jamie		if (!j || strcmp(j->name, "*")) {
773b31921jamie			j = add_jail();
783b31921jamie			j->name = estrdup("*");
793b31921jamie		}
803b31921jamie		TAILQ_INSERT_TAIL(&j->params, $2, tq);
813b31921jamie	}
823b31921jamie	| conf ';'
833b31921jamie
843b31921jamiejail	: STR '{' param_l '}'
853b31921jamie	{
863b31921jamie		$$ = add_jail();
873b31921jamie		$$->name = $1;
883b31921jamie		TAILQ_CONCAT(&$$->params, $3, tq);
893b31921jamie		free($3);
903b31921jamie	}
913b31921jamie	;
923b31921jamie
933b31921jamieparam_l	:
943b31921jamie	{
953b31921jamie		$$ = emalloc(sizeof(struct cfparams));
963b31921jamie		TAILQ_INIT($$);
973b31921jamie	}
983b31921jamie	| param_l param ';'
993b31921jamie	{
1003b31921jamie		$$ = $1;
1013b31921jamie		TAILQ_INSERT_TAIL($$, $2, tq);
1023b31921jamie	}
1033b31921jamie	| param_l ';'
1043b31921jamie	;
1053b31921jamie
1063b31921jamie/*
1073b31921jamie * Parameters have a name and an optional list of value strings,
10821a3003eadler * which may have "+=" or "=" preceding them.
1093b31921jamie */
1103b31921jamieparam	: name
1113b31921jamie	{
1123b31921jamie		$$ = $1;
1133b31921jamie	}
1143b31921jamie	| name '=' value
1153b31921jamie	{
1163b31921jamie		$$ = $1;
1170e5ec9djamie		TAILQ_CONCAT(&$$->val, $3, tq);
1183b31921jamie		free($3);
1193b31921jamie	}
1203b31921jamie	| name PLEQ value
1213b31921jamie	{
1223b31921jamie		$$ = $1;
1230e5ec9djamie		TAILQ_CONCAT(&$$->val, $3, tq);
1243b31921jamie		$$->flags |= PF_APPEND;
1253b31921jamie		free($3);
1263b31921jamie	}
1273b31921jamie	| name value
1283b31921jamie	{
1293b31921jamie		$$ = $1;
1300e5ec9djamie		TAILQ_CONCAT(&$$->val, $2, tq);
1313b31921jamie		free($2);
1323b31921jamie	}
1333b31921jamie	| error
1343b31921jamie	{
1353b31921jamie	}
1363b31921jamie	;
1373b31921jamie
1383b31921jamie/*
1393b31921jamie * A parameter has a fixed name.  A variable definition looks just like a
1403b31921jamie * parameter except that the name is a variable.
1413b31921jamie */
1423b31921jamiename	: STR
1433b31921jamie	{
1443b31921jamie		$$ = emalloc(sizeof(struct cfparam));
1453b31921jamie		$$->name = $1;
1460e5ec9djamie		TAILQ_INIT(&$$->val);
1473b31921jamie		$$->flags = 0;
1483b31921jamie	}
1493b31921jamie	| VAR
1503b31921jamie	{
1513b31921jamie		$$ = emalloc(sizeof(struct cfparam));
1523b31921jamie		$$->name = $1;
1530e5ec9djamie		TAILQ_INIT(&$$->val);
1543b31921jamie		$$->flags = PF_VAR;
1553b31921jamie	}
1563b31921jamie	;
1573b31921jamie
1583b31921jamievalue	: string
1593b31921jamie	{
1603b31921jamie		$$ = emalloc(sizeof(struct cfstrings));
1610e5ec9djamie		TAILQ_INIT($$);
1620e5ec9djamie		TAILQ_INSERT_TAIL($$, $1, tq);
1633b31921jamie	}
1643b31921jamie	| value ',' string
1653b31921jamie	{
1663b31921jamie		$$ = $1;
1670e5ec9djamie		TAILQ_INSERT_TAIL($$, $3, tq);
1683b31921jamie	}
1693b31921jamie	;
1703b31921jamie
1713b31921jamie/*
1723b31921jamie * Strings may be passed in pieces, because of quoting and/or variable
1733b31921jamie * interpolation.  Reassemble them into a single string.
1743b31921jamie */
1753b31921jamiestring	: STR
1763b31921jamie	{
1773b31921jamie		$$ = emalloc(sizeof(struct cfstring));
1783b31921jamie		$$->s = $1;
1793b31921jamie		$$->len = strlen($1);
1803b31921jamie		STAILQ_INIT(&$$->vars);
1813b31921jamie	}
1823b31921jamie	| VAR
1833b31921jamie	{
1843b31921jamie		struct cfvar *v;
1853b31921jamie
1863b31921jamie		$$ = emalloc(sizeof(struct cfstring));
1873b31921jamie		$$->s = estrdup("");
1883b31921jamie		$$->len = 0;
1893b31921jamie		STAILQ_INIT(&$$->vars);
1903b31921jamie		v = emalloc(sizeof(struct cfvar));
1913b31921jamie		v->name = $1;
1923b31921jamie		v->pos = 0;
1933b31921jamie		STAILQ_INSERT_TAIL(&$$->vars, v, tq);
1943b31921jamie	}
1953b31921jamie	| string STR1
1963b31921jamie	{
1973b31921jamie		size_t len1;
1983b31921jamie
1993b31921jamie		$$ = $1;
2003b31921jamie		len1 = strlen($2);
2013b31921jamie		$$->s = erealloc($$->s, $$->len + len1 + 1);
2023b31921jamie		strcpy($$->s + $$->len, $2);
2033b31921jamie		free($2);
2043b31921jamie		$$->len += len1;
2053b31921jamie	}
2063b31921jamie	| string VAR1
2073b31921jamie	{
2083b31921jamie		struct cfvar *v;
2093b31921jamie
2103b31921jamie		$$ = $1;
2113b31921jamie		v = emalloc(sizeof(struct cfvar));
2123b31921jamie		v->name = $2;
2133b31921jamie		v->pos = $$->len;
2143b31921jamie		STAILQ_INSERT_TAIL(&$$->vars, v, tq);
2153b31921jamie	}
2163b31921jamie	;
2173b31921jamie
2183b31921jamie%%
219