Debian Codegraph @

src/conf_ast.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 2019-2025 Hannes von Haugwitz
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <stdlib.h>
22 #include <stdbool.h>
23
24 #include "conf_ast.h"
25 #include "conf_lex.h"
26 #include "log.h"
27 #include "rx_rule.h"
28 #include "util.h"
29
30 LOG_LEVEL ast_log_level = LOG_LEVEL_TRACE;
31
32 config_option_t config_options[] = {
33 { ACL_NO_SYMLINK_FOLLOW_OPTION, "acl_no_symlink_follow", NULL },
34 { DATABASE_ADD_METADATA_OPTION, NULL, NULL },
35 { DATABASE_ATTRIBUTES_OPTION, NULL, NULL },
36 { DATABASE_GZIP_OPTION, NULL, NULL },
37 { DATABASE_IN_OPTION, NULL, NULL },
38 { DATABASE_OUT_OPTION, NULL, NULL },
39 { DATABASE_NEW_OPTION, NULL, NULL },
40 { LOG_LEVEL_OPTION, NULL, NULL },
41 { REPORT_BASE16_OPTION, NULL, NULL },
42 { REPORT_DETAILED_INIT_OPTION, NULL, NULL },
43 { REPORT_FORCE_ATTRS_OPTION, "report_force_attrs", "Forced attributes" },
44 { REPORT_GROUPED_OPTION, NULL, NULL },
45 { REPORT_IGNORE_ADDED_ATTRS_OPTION, "report_ignore_added_attrs", "Ignored added attributes" },
46 { REPORT_IGNORE_REMOVED_ATTRS_OPTION, "report_ignore_removed_attrs", "Ignored removed attributes" },
47 { REPORT_IGNORE_CHANGED_ATTRS_OPTION, "report_ignore_changed_attrs", "Ignored changed attributes" },
48 { REPORT_IGNORE_E2FSATTRS_OPTION, "report_ignore_e2fsattrs", "Ignored e2fs attributes" },
49 { REPORT_LEVEL_OPTION, "report_level", "Report level" },
50 { REPORT_QUIET_OPTION, NULL, NULL },
51 { REPORT_APPEND_OPTION, NULL, NULL },
52 { REPORT_SUMMARIZE_CHANGES_OPTION, NULL, NULL },
53 { REPORT_URL_OPTION, NULL, NULL },
54 { ROOT_PREFIX_OPTION, "root_prefix", "Root prefix" },
55 { WARN_DEAD_SYMLINKS_OPTION, NULL, NULL },
56 { CONFIG_VERSION, "config_version", "Config version used" },
57 { CONFIG_CHECK_WARN_UNRESTRICTED_RULES, NULL, NULL },
58 { REPORT_FORMAT_OPTION, NULL, NULL },
59 { LIMIT_CMDLINE_OPTION, "limit", "Limit" },
60 { NUM_WORKERS, NULL, NULL },
61 };
62
63 static ast* new_ast_node(void) {
64 ast* a = checked_malloc(sizeof(ast));
65
66 a->linenumber = conf_linenumber;
67 a->filename = conf_filename;
68 a->linebuf = conf_linebuf;
69 a->next = NULL;
70
71 return a;
72 }
73
74 ast* new_string_option_statement(config_option option, string_expression* value) {
75 ast* a = new_ast_node();
76
77 a->type = config_option_type;
78 a->statement._config.option = option;
79 a->statement._config.a = NULL;
80 a->statement._config.e = value;
81 log_msg(ast_log_level, "ast: new string option statement (%p): option: %d, value: %p", (void*) a, option, (void*) value);
82 return a;
83 }
84
85 ast* new_attribute_option_statement(config_option option, attribute_expression* value) {
86 ast* a = new_ast_node();
87
88 a->type = config_option_type;
89 a->statement._config.option = option;
90 a->statement._config.a = value;
91 a->statement._config.e = NULL;
92 log_msg(ast_log_level, "ast: new attribute option statement (%p): option: %d, value: %p", (void*) a, option, (void*) value);
93 return a;
94 }
95
96 ast* new_include_statement(string_expression* path, string_expression* rx, bool execute, string_expression* prefix) {
97 ast* a = new_ast_node();
98
99 a->type = include_statement_type;
100 a->statement._include.path = path;
101 a->statement._include.rx = rx;
102 a->statement._include.execute = execute;
103 a->statement._include.prefix = prefix;
104
105 log_msg(ast_log_level, "ast: new include statement (%p): path: %p, rx: %p, execute: %s, prefix: %p", (void*) a, (void*) path, (void*) rx, btoa(execute), (void*) prefix);
106 return a;
107 }
108
109 ast* new_x_include_setenv_statement(char *variable, string_expression *value) {
110 ast* a = new_ast_node();
111
112 a->type = x_include_setenv_statement_type;
113 a->statement._x_include_setenv.variable = variable;
114 a->statement._x_include_setenv.value = value;
115 log_msg(ast_log_level, "ast: new x_include_setenv statement (%p): variable: '%s', value: %p", (void*) a, variable, (void*) value);
116 return a;
117 }
118
119 ast* new_define_statement(char *name, string_expression *value) {
120 ast* a = new_ast_node();
121
122 a->type = define_statement_type;
123 a->statement._define.name = name;
124 a->statement._define.value = value;
125 log_msg(ast_log_level, "ast: new define statement (%p): name: '%s', value: %p", (void*) a, name, (void*) value);
126 return a;
127 }
128
129 ast* new_undefine_statement(char *name) {
130 ast* a = new_ast_node();
131
132 a->type = undefine_statement_type;
133 a->statement._undefine.name = name;
134 log_msg(ast_log_level, "ast: new undefine statement (%p): name: '%s'", (void*) a, name);
135 return a;
136 }
137
138 ast* new_group_statement(char* name, attribute_expression* expr) {
139 ast* a = new_ast_node();
140
141 a->type = group_statement_type;
142 a->statement._group.name = name;
143 a->statement._group.expr = expr;
144 log_msg(ast_log_level, "ast: new group statement (%p): name: '%s', expr: %p", (void*) a, name, (void*) expr);
145 return a;
146 }
147
148 bool_expression* new_string_bool_expression(bool_operator op, string_expression* left, string_expression* right) {
149 bool_expression* e = checked_malloc(sizeof(bool_expression));
150 e->op = op;
151 e->left._str = left;
152 e->right._str = right;
153 log_msg(ast_log_level, "ast: new string bool expression (%p): op: %d, left: %p, right: %p", (void*) e, op, (void*) left, (void*) right);
154 return e;
155 }
156
157 bool_expression* new_bool_expression(bool_operator op, bool_expression* left, bool_expression* right) {
158 bool_expression* e = checked_malloc(sizeof(bool_expression));
159 e->op = op;
160 e->left._bool = left;
161 e->right._bool = right;
162 log_msg(ast_log_level, "ast: new bool expression (%p): op: %d, left: %p, right: %p", (void*) e, op, (void*) left, (void*) right);
163 return e;
164 }
165
166 if_condition* new_if_condition(bool_expression* expression) {
167 if_condition* c = checked_malloc(sizeof(if_condition));
168
169 c->linenumber = conf_linenumber;
170 c->filename = conf_filename;
171 c->linebuf = conf_linebuf;
172
173 c->expression = expression;
174
175 log_msg(ast_log_level, "ast: if condition (%p): expression: %p", (void*) c, (void*) expression);
176 return c;
177 }
178
179 attribute_expression* new_attribute_expression(attribute_operator op, attribute_expression* left, char* right) {
180 attribute_expression* e = checked_malloc(sizeof(attribute_expression));
181 e->op = op;
182 e->left = left;
183 e->right = right;
184 log_msg(ast_log_level, "ast: new attribute expression (%p): op: %d, left: %p, right: '%s'", (void*) e, op, (void*) left, right);
185 return e;
186 }
187
188 ft_restriction_expression* new_ft_restriction_expression(ft_restriction_expression* left, char* right) {
189 ft_restriction_expression* e = checked_malloc(sizeof(ft_restriction_expression));
190 e->right = right;
191 e->left = left;
192 log_msg(ast_log_level, "ast: new file type restriction expression (%p): left: %p, right: '%s'", (void*) e, (void*) left, right);
193 return e;
194 }
195
196 restriction_expression* new_restriction_expression(ft_restriction_expression* f_types, char* fs_type) {
197 restriction_expression* e = checked_malloc(sizeof(restriction_expression));
198 e->f_types = f_types;
199 e->fs_type = fs_type;
200 log_msg(ast_log_level, "ast: new restriction expression (%p): f_types: '%p', fs_type: %s", (void*) e, (void*) f_types, (fs_type != NULL) ? fs_type : "(null)");
201 return e;
202 }
203
204 ast* new_if_statement(struct if_condition* condition, struct ast* if_branch, struct ast* else_branch) {
205 ast* e = new_ast_node();
206
207 e->type = if_statement_type;
208 e->statement._if.condition = condition;
209 e->statement._if.if_branch = if_branch;
210 e->statement._if.else_branch = else_branch;
211 log_msg(ast_log_level, "ast: new if statement (%p): condition: %p, if_branch: %p, else_branch: %p", (void*) e, (void*) condition, (void*) if_branch, (void*) else_branch);
212 return e;
213 }
214
215 ast* new_rule_statement(AIDE_RULE_TYPE rule_type, string_expression* path, restriction_expression* restriction, attribute_expression* attrs) {
216 ast* e = new_ast_node();
217
218 e->type = rule_statement_type;
219 e->statement._rule.type = rule_type;
220 e->statement._rule.path = path;
221 e->statement._rule.restriction = restriction;
222 e->statement._rule.attributes = attrs;
223 log_msg(ast_log_level, "ast: new rule statement (%p): type: %s, path: %p, restriction: %p, attributes: %p", (void*) e, get_rule_type_long_string(rule_type), (void*) path, (void*) restriction, (void*) attrs);
224 return e;
225 }
226
227 string_expression* new_string(char *str) {
228 string_expression* e = checked_malloc(sizeof(string_expression));
229
230 e->op = STR_OP_STR;
231 e->str = str;
232 e->left = NULL;
233 e->right = NULL;
234 log_msg(ast_log_level, "ast: new string (%p): str: '%s'", (void*) e, str);
235 return e;
236 }
237 string_expression* new_variable(char *name) {
238 string_expression* e = checked_malloc(sizeof(string_expression));
239
240 e->op = STR_OP_VARIABLE;
241 e->str = name;
242 e->left = NULL;
243 e->right = NULL;
244 log_msg(ast_log_level, "ast: new variable (%p): name: '%s'", (void*) e, name);
245 return e;
246 }
247 string_expression* new_string_concat(string_expression* left, string_expression* right) {
248 string_expression* e = checked_malloc(sizeof(string_expression));
249 e->op = STR_OP_CONCAT;
250 e->str = NULL;
251 e->left = left;
252 e->right = right;
253 log_msg(ast_log_level, "ast: new string concat (%p): left: %p, right: %p", (void*) e, (void*) left, (void*) right);
254 return e;
255 }
256
257 void free_string(char * s) {
258 if (s == NULL) {
259 return;
260 }
261 log_msg(ast_log_level, "ast: free string %p", (void*) s);
262 free(s);
263 }
264
265 void free_attribute_expression(attribute_expression *a) {
266 if (a == NULL) {
267 return;
268 }
269 free_attribute_expression(a->left);
270 free_string(a->right);
271 log_msg(ast_log_level, "ast: free attribute expression %p", (void*) a);
272 free(a);
273 }
274
275 void free_string_expression(string_expression *s) {
276 if (s == NULL) {
277 return;
278 }
279 free_string_expression(s->left);
280 free_string_expression(s->right);
281 free_string(s->str);
282 log_msg(ast_log_level, "ast: free string expression %p", (void*) s);
283 free(s);
284 }
285
286 void free_bool_expression(bool_expression *b) {
287 if (b == NULL) {
288 return;
289 }
290 switch (b->op) {
291 case BOOL_OP_NOT:
292 free_bool_expression(b->left._bool);
293 break;
294 case BOOL_OP_EXISTS:
295 case BOOL_OP_DEFINED:
296 case BOOL_OP_HOSTNAME:
297 free_string_expression(b->left._str);
298 break;
299 case BOOL_OP_VERSION_GE:
300 free_string_expression(b->left._str);
301 free_string_expression(b->right._str);
302 break;
303 }
304 log_msg(ast_log_level, "ast: free bool expression %p", (void*) b);
305 free(b);
306 }
307
308 void free_if_condition(if_condition *c) {
309 free_bool_expression(c->expression);
310 free_string(c->linebuf);
311 log_msg(ast_log_level, "ast: free if condition %p", (void*) c);
312 free(c);
313 }
314
315 void free_ft_restriction_expression(ft_restriction_expression *r) {
316 if (r == NULL) {
317 return;
318 }
319 free_ft_restriction_expression(r->left);
320 free_string(r->right);
321 log_msg(ast_log_level, "ast: free file type restriction expression %p", (void*) r);
322 free(r);
323 }
324
325 void free_restriction_expression(restriction_expression *r) {
326 if (r == NULL) {
327 return;
328 }
329 #ifdef HAVE_FSTYPE
330 free_string(r->fs_type);
331 #endif
332 free_ft_restriction_expression(r->f_types);
333 log_msg(ast_log_level, "ast: free restriction expression %p", (void*) r);
334 free(r);
335 }
336
337 void deep_free(ast* config_ast) {
338 if (config_ast == NULL) {
339 return;
340 }
341 ast* node = NULL;
342 for(node = config_ast; node != NULL; ) {
343 switch (node->type) {
344 case config_option_type:
345 free_attribute_expression(node->statement._config.a);
346 free_string_expression(node->statement._config.e);
347 break;
348 case define_statement_type:
349 free_string_expression(node->statement._define.value);
350 free_string(node->statement._define.name);
351 break;
352 case group_statement_type:
353 free_attribute_expression(node->statement._group.expr);
354 free_string(node->statement._group.name);
355 break;
356 case if_statement_type:
357 free_if_condition(node->statement._if.condition);
358 deep_free(node->statement._if.if_branch);
359 deep_free(node->statement._if.else_branch);
360 break;
361 case include_statement_type:
362 free_string_expression(node->statement._include.path);
363 free_string_expression(node->statement._include.rx);
364 free_string_expression(node->statement._include.prefix);
365 break;
366 case x_include_setenv_statement_type:
367 free_string_expression(node->statement._x_include_setenv.value);
368 free_string(node->statement._x_include_setenv.variable);
369 break;
370 case rule_statement_type:
371 free_string_expression(node->statement._rule.path);
372 free_restriction_expression(node->statement._rule.restriction);
373 free_attribute_expression(node->statement._rule.attributes);
374 break;
375 case undefine_statement_type:
376 free_string(node->statement._define.name);
377 break;
378 }
379 free(node->linebuf);
380 ast* to_be_freed = node;
381 node = node->next;
382 log_msg(ast_log_level, "ast: free ast node %p (next: %p)", (void*) to_be_freed, (void*) node);
383 free(to_be_freed);
384 }
385 }