Debian Codegraph @

src/conf_yacc.y

raw
1 %code requires {
2 #include "conf_ast.h"
3 }
4 %{
5
6 /*
7 * AIDE (Advanced Intrusion Detection Environment)
8 *
9 * Copyright (C) 1999-2006, 2010-2013, 2015-2016, 2019-2025 Rami Lehti,
10 * Pablo Virolainen, Richard van den Berg, Hannes von Haugwitz
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27 #include <stdbool.h>
28 #include "attributes.h"
29 #include <stdlib.h>
30 #include <string.h>
31 #include "log.h"
32 #include "rx_rule.h"
33 #include "conf_lex.h"
34
35 DB_ATTR_TYPE retval=0;
36
37 #include "conf_ast.h"
38 extern int conflex(void);
39 void conferror(ast**, const char *);
40
41 %}
42 %union {
43 char* s;
44
45 config_option option;
46
47 bool_operator operator;
48
49 ast* ast;
50
51 if_condition* if_cond;
52 bool_expression* bool_expr;
53 attribute_expression* attr_expr;
54 restriction_expression* rs_expr;
55 ft_restriction_expression* ft_rs_expr;
56 string_expression* string_expr;
57 }
58
59 %token TDEFINE "@@define"
60 %token TUNDEFINE "@@undef"
61 %token TIFDEF "@@ifdef"
62 %token TIFNDEF "@@ifndef"
63 %token TIFNHOST "@@ifnhost"
64 %token TIFHOST "@@ifhost"
65 %token TIF "@@if"
66 %token TBOOLNOT "not"
67 %token <operator> TBOOLFUNC "boolean function"
68 %token <operator> TBOOLOP "boolean operator"
69 %token TELSE "@@else"
70 %token TENDIF "@@endif"
71 %token TINCLUDE "@@include"
72 %token TXINCLUDE "@@x_include"
73 %token TSETENV "@@x_include_setenv"
74 %token <s> TGROUP "group name"
75 %token <s> TSTRING "string"
76 %token <s> TEXPR "group"
77 %token <s> TVARIABLE "variable name"
78
79 %token TSPACE "whitespace"
80 %token TNEWLINE "new line"
81
82 /* File rule */
83
84 %token <s> TSELRXRULE "regular rule"
85 %token <s> TEQURXRULE "equals rule"
86 %token <s> TRECNEGRXRULE "recursive negative rule"
87 %token <s> TNONRECNEGRXRULE "non-recursive negative rule"
88
89 %token <option> CONFIGOPTION "configuration option"
90
91 %type <ast> statements statement config_statement include_statement x_include_setenv_statement if_statement define_statement undefine_statement group_statement rule_statement
92
93 %type <if_cond> if_condition
94 %type <bool_expr> bool_expression
95 %type <attr_expr> attribute_expression
96 %type <rs_expr> restriction_expression
97 %type <ft_rs_expr> ft_restriction_expression
98 %type <string_expr> string_expression string_fragment
99
100 %start config
101
102 %parse-param {ast** config_ast}
103 %define parse.error verbose
104
105 %%
106
107 config : %empty /* empty input */
108 | statements { *config_ast = $1; }
109
110 statements : statement TNEWLINE statements {
111 ast *temp = $1;
112 temp->next = $3;
113 $$ = $1; }
114 | statement TNEWLINE { $$ = $1; }
115 | statement { $$ = $1; }
116
117 statement: config_statement
118 | include_statement
119 | x_include_setenv_statement
120 | if_statement
121 | define_statement | undefine_statement
122 | group_statement
123 | rule_statement
124
125 attribute_expression: attribute_expression '+' TEXPR { $$ = new_attribute_expression(ATTR_OP_PLUS, $1, $3); }
126 | attribute_expression '-' TEXPR { $$ = new_attribute_expression(ATTR_OP_MINUS, $1, $3); }
127 | TEXPR { $$ = new_attribute_expression(ATTR_OP_GROUP, NULL, $1); }
128
129 ft_restriction_expression: ft_restriction_expression ',' TEXPR { $$ = new_ft_restriction_expression($1, $3); }
130 | TEXPR { $$ = new_ft_restriction_expression(NULL, $1); }
131
132 restriction_expression: ft_restriction_expression '=' TEXPR { $$ = new_restriction_expression($1, $3); }
133 | '=' TEXPR { $$ = new_restriction_expression(NULL, $2); }
134 | ft_restriction_expression { $$ = new_restriction_expression($1, NULL); }
135 | '0' { $$ = new_restriction_expression(NULL, NULL); }
136
137 define_statement: TDEFINE TVARIABLE { $$ = new_define_statement($2, NULL); }
138 | TDEFINE TVARIABLE string_expression { $$ = new_define_statement($2, $3); }
139
140 string_expression: string_fragment string_expression { $$ = new_string_concat($1, $2); }
141 | string_fragment { $$ = $1; }
142 string_fragment: TSTRING { $$ = new_string($1); }
143 | TVARIABLE { $$ = new_variable($1); }
144
145 undefine_statement: TUNDEFINE TVARIABLE { $$ = new_undefine_statement($2); }
146
147 config_statement: CONFIGOPTION '=' string_expression { $$ = new_string_option_statement($1, $3); }
148 | CONFIGOPTION '=' attribute_expression { $$ = new_attribute_option_statement($1, $3); }
149
150 group_statement: TGROUP '=' attribute_expression { $$ = new_group_statement($1, $3); }
151
152 include_statement: TINCLUDE TSPACE string_expression { $$ = new_include_statement($3, NULL, false, NULL); }
153 | TINCLUDE TSPACE string_expression TSPACE string_expression { $$ = new_include_statement($3, $5, false, NULL); }
154 | TINCLUDE TSPACE string_expression TSPACE string_expression TSPACE string_expression{ $$ = new_include_statement($3, $5, false, $7); }
155 | TXINCLUDE TSPACE string_expression { $$ = new_include_statement($3, NULL, true, NULL); }
156 | TXINCLUDE TSPACE string_expression TSPACE string_expression { $$ = new_include_statement($3, $5, true, NULL); }
157 | TXINCLUDE TSPACE string_expression TSPACE string_expression TSPACE string_expression { $$ = new_include_statement($3, $5, true, $7); }
158
159 x_include_setenv_statement: TSETENV TVARIABLE string_expression { $$ = new_x_include_setenv_statement($2, $3); }
160
161 if_statement: if_condition TNEWLINE statements TENDIF { $$ = new_if_statement($1, $3, NULL); }
162 | if_condition TNEWLINE statements TELSE TNEWLINE statements TENDIF { $$ = new_if_statement($1, $3, $6); }
163
164 if_condition: TIFDEF string_expression { $$=new_if_condition(new_string_bool_expression(BOOL_OP_DEFINED, $2, NULL)); }
165 | TIFNDEF string_expression { $$=new_if_condition(new_bool_expression(BOOL_OP_NOT, new_string_bool_expression(BOOL_OP_DEFINED, $2, NULL), NULL)); }
166 | TIFHOST string_expression { $$=new_if_condition(new_string_bool_expression(BOOL_OP_HOSTNAME, $2, NULL)); }
167 | TIFNHOST string_expression { $$=new_if_condition(new_bool_expression(BOOL_OP_NOT, new_string_bool_expression(BOOL_OP_HOSTNAME, $2, NULL), NULL)); }
168 | TIF bool_expression { $$=new_if_condition($2); }
169
170 bool_expression: TBOOLNOT bool_expression { $$ = new_bool_expression(BOOL_OP_NOT, $2, NULL); }
171 | TBOOLFUNC string_expression { $$ = new_string_bool_expression($1, $2, NULL); }
172 | string_expression TBOOLOP string_expression { $$ = new_string_bool_expression($2, $1, $3); }
173
174 rule_statement: TSELRXRULE string_expression attribute_expression { $$ = new_rule_statement(AIDE_SELECTIVE_RULE, $2, NULL, $3); }
175 | TEQURXRULE string_expression attribute_expression { $$ = new_rule_statement(AIDE_EQUAL_RULE, $2, NULL, $3); }
176 | TRECNEGRXRULE string_expression { $$ = new_rule_statement(AIDE_RECURSIVE_NEGATIVE_RULE, $2, NULL, NULL); }
177 | TNONRECNEGRXRULE string_expression { $$ = new_rule_statement(AIDE_NON_RECURSIVE_NEGATIVE_RULE, $2, NULL, NULL); }
178 | TSELRXRULE string_expression restriction_expression attribute_expression { $$ = new_rule_statement(AIDE_SELECTIVE_RULE, $2, $3, $4); }
179 | TEQURXRULE string_expression restriction_expression attribute_expression { $$ = new_rule_statement(AIDE_EQUAL_RULE, $2, $3, $4); }
180 | TRECNEGRXRULE string_expression restriction_expression { $$ = new_rule_statement(AIDE_RECURSIVE_NEGATIVE_RULE, $2, $3, NULL); }
181 | TNONRECNEGRXRULE string_expression restriction_expression { $$ = new_rule_statement(AIDE_NON_RECURSIVE_NEGATIVE_RULE, $2, $3, NULL); }
182 | TRECNEGRXRULE string_expression restriction_expression attribute_expression {
183 log_msg(LOG_LEVEL_ERROR, "%s:%d: recursive negative rule must not have an attribute expression (line: '%s')", conf_filename, conf_linenumber, conf_linebuf);
184 YYABORT;
185 }
186 | TNONRECNEGRXRULE string_expression restriction_expression attribute_expression {
187 log_msg(LOG_LEVEL_ERROR, "%s:%d: non-recursive negative rule must not have an attribute expression (line: '%s')", conf_filename, conf_linenumber, conf_linebuf);
188 YYABORT;
189 }
190
191 %%
192
193 void conferror(
194 ast** config_ast __attribute__((unused)),
195 const char *msg){
196 log_msg(LOG_LEVEL_ERROR, "%s:%d: %s (line: '%s')", conf_filename, conf_linenumber, msg, conf_linebuf);
197 }