Debian Codegraph @

src/conf_eval.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 "config.h"
22 #include "aide.h"
23
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #define PCRE2_CODE_UNIT_WIDTH 8
29 #include <pcre2.h>
30 #include "attributes.h"
31 #include "conf_ast.h"
32 #include "db_config.h"
33 #include "hashsum.h"
34 #include "list.h"
35 #include "report.h"
36
37 #include "conf_eval.h"
38 #include "conf_yacc.h"
39 #include "conf_lex.h"
40
41 #include "log.h"
42 #include "errorcodes.h"
43 #include "db.h"
44 #include "rx_rule.h"
45 #include "util.h"
46
47 #include "commandconf.h"
48
49 #include "symboltable.h"
50
51 #include <stdlib.h>
52 #include <stddef.h>
53 #include <sys/wait.h>
54 #include <dirent.h>
55 #include <errno.h>
56 #include <unistd.h>
57
58 LOG_LEVEL eval_log_level = LOG_LEVEL_TRACE;
59
60 bool log_level_set_in_config = false;
61
62 list* x_include_environment = NULL;
63
64 #define BOOL_CONFIG_OPTION_CASE(id, option) \
65 case id: \
66 b = string_expression_to_bool(statement.e, linenumber, filename, linebuf); \
67 conf->option = b; \
68 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set '%s' to '%s'", #option, btoa(conf->option)) \
69 break;
70
71 #define ATTRIBUTE_CONFIG_OPTION_CASE(id, option) \
72 case id: \
73 attr = eval_attribute_expression(statement.a, linenumber, filename, linebuf); \
74 conf->option=attr; \
75 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set '%s' to '%s'", #option, str = diff_attributes(0, attr) ) \
76 free(str); \
77 break;
78
79 #define DATABASE_CONFIG_OPTION_CASE(id, dbtype) \
80 case id: \
81 str = eval_string_expression(statement.e, linenumber, filename, linebuf); \
82 if (!do_dbdef(dbtype, str, linenumber, filename, linebuf)) { \
83 exit(INVALID_CONFIGURELINE_ERROR); \
84 } \
85 free(str); \
86 break;
87
88 static char* eval_string_expression(struct string_expression* expression, int linenumber, char *filename, char* linebuf) {
89 char *str = NULL, *right, *left;
90 int length = 0;
91 list *entry;
92 switch (expression->op) {
93 case STR_OP_STR:
94 str = checked_strdup(expression->str);
95 break;
96 case STR_OP_VARIABLE:
97 entry = NULL;
98 if ((entry = list_find(expression->str, conf->defsyms))) {
99 str = checked_strdup(((symba*)entry->data)->value?((symba*)entry->data)->value:"");
100 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "variable substitution: replace '@@%c%s%c' with '%s'", '{', expression->str, '}', str)
101 } else if (strcmp(expression->str, "AIDE_VERSION") == 0) {
102 str = checked_strdup(conf->aide_version);
103 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "variable substitution: replace '@@%c%s%c' with '%s'", '{', expression->str, '}', str)
104 } else if (strcmp(expression->str, "HOSTNAME") == 0 && conf->hostname) {
105 str = checked_strdup(conf->hostname);
106 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "variable substitution: replace '@@%c%s%c' with '%s'", '{', expression->str, '}', str)
107 } else {
108 str = checked_strdup("");
109 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "variable substitution: variable '%s' not defined (use empty string)", expression->str)
110 }
111 break;
112 case STR_OP_CONCAT:
113 left = eval_string_expression(expression->left, linenumber, filename, linebuf);
114 right = eval_string_expression(expression->right, linenumber, filename, linebuf);
115 length = strlen(left)+strlen(right);
116 str = checked_malloc(length+1);
117 strncpy(str, left, length+1);
118 strncat(str, right, length-strlen(left)+1);
119 log_msg(eval_log_level, "eval(%p): string concat '%s' + '%s' evaluates to %s", (void*) expression, left, right, str);
120 free(left);
121 free(right);
122 break;
123 }
124 return str;
125 }
126
127 static bool string_expression_to_bool(string_expression *e, int linenumber, char *filename, char* linebuf) {
128 bool b = false;
129 char *str = eval_string_expression(e, linenumber, filename, linebuf);
130 if (strcmp(str, "true") == 0 || strcmp(str, "yes") == 0) {
131 b = true;
132 } else if (strcmp(str, "false") != 0 && strcmp(str, "no") != 0) {
133 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "unrecognized bool value: '%s' (expecting %s)", str, "'true', 'yes','false' or 'no'")
134 exit(INVALID_CONFIGURELINE_ERROR);
135 }
136 free(str);
137 return b;
138 }
139
140 static DB_ATTR_TYPE eval_attribute_expression(struct attribute_expression* expression, int linenumber, char *filename, char* linebuf) {
141 DB_ATTR_TYPE attr = 0, attr_r;
142
143 if (expression != NULL) {
144 if (expression->left) {
145 attr = eval_attribute_expression(expression->left, linenumber, filename, linebuf);
146 }
147
148 attr_r = get_groupval(expression->right);
149 if(attr_r == DB_ATTR_UNDEF) {
150 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "group '%s' is not defined", expression->right)
151 exit(INVALID_CONFIGURELINE_ERROR);
152 }
153 log_msg(eval_log_level, "eval(%p): attribute group '%s' evaluates to %llu", (void*) expression, expression->right, attr_r);
154 switch (expression->op) {
155 case ATTR_OP_GROUP:
156 attr = attr_r;
157 break;
158 case ATTR_OP_PLUS:
159 attr |= attr_r;
160 break;
161 case ATTR_OP_MINUS:
162 attr &= (~attr_r);
163 break;
164 }
165 log_msg(eval_log_level, "eval(%p): attribute expression (op: %d, left: %p, right: '%s') evaluates to %llu", (void*) expression, expression->op, (void*) expression->left, expression->right, attr);
166 } else {
167 log_msg(eval_log_level, "eval(%p): attribute expression is NULL and evaluates to %llu", (void*) expression, attr);
168 }
169 return attr;
170 }
171
172 static void set_database_attr_option(DB_ATTR_TYPE attr, int linenumber, char *filename, char* linebuf) {
173 char *str;
174
175 DB_ATTR_TYPE hashes = get_hashes(true);
176 if (attr&(~hashes)) {
177 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "invalid attribute(s): %s", str = diff_attributes(0, attr&(~hashes)));
178 free(str);
179 exit(INVALID_CONFIGURELINE_ERROR);
180 }
181 attr &= validate_hashes(attr, linenumber, filename, linebuf);
182 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set 'database_attrs' option to: %s", str = diff_attributes(0, attr));
183 free(str);
184 conf->db_attrs = attr;
185 }
186
187 static void eval_config_statement(config_option_statement statement, int linenumber, char *filename, char* linebuf) {
188 char *str;
189 bool b;
190 DB_ATTR_TYPE attr;
191 switch (statement.option) {
192 ATTRIBUTE_CONFIG_OPTION_CASE(REPORT_IGNORE_ADDED_ATTRS_OPTION, report_ignore_added_attrs)
193 ATTRIBUTE_CONFIG_OPTION_CASE(REPORT_IGNORE_REMOVED_ATTRS_OPTION, report_ignore_removed_attrs)
194 ATTRIBUTE_CONFIG_OPTION_CASE(REPORT_IGNORE_CHANGED_ATTRS_OPTION, report_ignore_changed_attrs)
195 ATTRIBUTE_CONFIG_OPTION_CASE(REPORT_FORCE_ATTRS_OPTION, report_force_attrs)
196 case REPORT_URL_OPTION:
197 str = eval_string_expression(statement.e, linenumber, filename, linebuf);
198 if (!do_repurldef(str, linenumber, filename, linebuf)) {
199 exit(INVALID_CONFIGURELINE_ERROR);
200 }
201 free(str);
202 break;
203 case ROOT_PREFIX_OPTION:
204 /* not to be freed, reused in do_rootprefix */
205 str = eval_string_expression(statement.e, linenumber, filename, linebuf);
206 if(do_rootprefix(str, linenumber, filename, linebuf) == false) {
207 free(str);
208 }
209 break;
210 DATABASE_CONFIG_OPTION_CASE(DATABASE_IN_OPTION, DB_TYPE_IN)
211 DATABASE_CONFIG_OPTION_CASE(DATABASE_OUT_OPTION, DB_TYPE_OUT)
212 DATABASE_CONFIG_OPTION_CASE(DATABASE_NEW_OPTION, DB_TYPE_NEW)
213 case DATABASE_ATTRIBUTES_OPTION:
214 set_database_attr_option(
215 eval_attribute_expression(statement.a, linenumber, filename, linebuf),
216 linenumber, filename, linebuf);
217 break;
218 case DATABASE_GZIP_OPTION:
219 #ifdef WITH_ZLIB
220 b = string_expression_to_bool(statement.e, linenumber, filename, linebuf);
221 conf->gzip_dbout=b;
222 #else
223 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "%s", "gzip support not compiled in, recompile AIDE with '--with-zlib'")
224 exit(INVALID_CONFIGURELINE_ERROR);
225 #endif
226 break;
227 BOOL_CONFIG_OPTION_CASE(DATABASE_ADD_METADATA_OPTION, database_add_metadata)
228 case ACL_NO_SYMLINK_FOLLOW_OPTION:
229 #ifdef WITH_ACL
230 b = string_expression_to_bool(statement.e, linenumber, filename, linebuf);
231 conf->no_acl_on_symlinks=b;
232 #else
233 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "%s", "acl support not compiled in, recompile AIDE with '--with-posix-acl'")
234 exit(INVALID_CONFIGURELINE_ERROR);
235 #endif
236 break;
237 case REPORT_IGNORE_E2FSATTRS_OPTION:
238 #ifdef WITH_E2FSATTRS
239 str = eval_string_expression(statement.e, linenumber, filename, linebuf);
240 do_report_ignore_e2fsattrs(str, linenumber, filename, linebuf);
241 free(str);
242 #else
243 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "%s", "e2fsattrs support not compiled in, recompile AIDE with '--with-e2fsattrs'")
244 exit(INVALID_CONFIGURELINE_ERROR);
245 #endif
246 break;
247 BOOL_CONFIG_OPTION_CASE(REPORT_BASE16_OPTION, report_base16)
248 BOOL_CONFIG_OPTION_CASE(REPORT_DETAILED_INIT_OPTION, report_detailed_init)
249 BOOL_CONFIG_OPTION_CASE(REPORT_GROUPED_OPTION, report_grouped)
250 BOOL_CONFIG_OPTION_CASE(REPORT_QUIET_OPTION, report_quiet)
251 BOOL_CONFIG_OPTION_CASE(REPORT_APPEND_OPTION, report_append)
252 BOOL_CONFIG_OPTION_CASE(REPORT_SUMMARIZE_CHANGES_OPTION, report_summarize_changes)
253 BOOL_CONFIG_OPTION_CASE(WARN_DEAD_SYMLINKS_OPTION, warn_dead_symlinks)
254 BOOL_CONFIG_OPTION_CASE(CONFIG_CHECK_WARN_UNRESTRICTED_RULES, config_check_warn_unrestricted_rules)
255 case REPORT_LEVEL_OPTION:
256 str = eval_string_expression(statement.e, linenumber, filename, linebuf);
257 if(!do_reportlevel(str, linenumber, filename, linebuf)) {
258 exit(INVALID_CONFIGURELINE_ERROR);
259 }
260 free(str);
261 break;
262 case REPORT_FORMAT_OPTION:
263 str = eval_string_expression(statement.e, linenumber, filename, linebuf);
264 REPORT_FORMAT report_format = get_report_format(str);
265 if (report_format != REPORT_FORMAT_UNKNOWN) {
266 conf->report_format = report_format;
267 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set 'report_format' option to '%s' (raw: %d)", str, report_format)
268 } else {
269 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "invalid report format: '%s'", str);
270 exit(INVALID_CONFIGURELINE_ERROR);
271 }
272 free(str);
273 break;
274 case LOG_LEVEL_OPTION:
275 str = eval_string_expression(statement.e, linenumber, filename, linebuf);
276 LOG_LEVEL level = get_log_level_from_string(str);
277 if (level == LOG_LEVEL_UNSET) {
278 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "invalid log level: '%s'", str);
279 exit(INVALID_CONFIGURELINE_ERROR);
280 } else {
281 if (is_log_level_unset() || ( conf->action&DO_DRY_RUN && !log_level_set_in_config )) {
282 if (!(conf->action&DO_DRY_RUN)) {
283 set_log_level(level);
284 } else {
285 log_level_set_in_config = true;
286 }
287 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set 'log_level' option to '%s'", str)
288 } else {
289 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "'log_level' option already set (ignore new value '%s')", str)
290 }
291 }
292 free(str);
293 break;
294 case CONFIG_VERSION:
295 /* not to be freed, used directly for config_version */
296 str = eval_string_expression(statement.e, linenumber, filename, linebuf);
297 conf->config_version = str;
298 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set 'config_version' option to '%s'", str)
299 break;
300 case LIMIT_CMDLINE_OPTION:
301 /* command-line options are ignored here */
302 break;
303 case NUM_WORKERS:
304 str = eval_string_expression(statement.e, linenumber, filename, linebuf);
305
306 if (conf->num_workers < 0) {
307 long num_workers = do_num_workers(str);
308 if (num_workers < 0) {
309 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "invalid number of workers: '%s'", str);
310 exit(INVALID_CONFIGURELINE_ERROR);
311 }
312 conf->num_workers = num_workers;
313 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set 'num_workers' option to %ld (config value: '%s')", conf->num_workers, str)
314 } else {
315 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "'num_workers' option already set (ignore new value '%s')", str)
316 }
317 break;
318 }
319 }
320
321 static void parse_version(const char * version_str, long version[3], int linenumber, char *filename, char* linebuf) {
322 char * endptr = NULL;
323 int i = 0;
324
325 const char *ptr = version_str;
326 while (*ptr && i < 3) {
327 version[i] = strtol(ptr, &endptr, 10);
328 if ( ptr == endptr || errno==ERANGE || version[i] < 0) {
329 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "invalid version string: '%s', expected MAJOR.MINOR.PATCH", version_str);
330 exit(INVALID_CONFIGURELINE_ERROR);
331 }
332 if (*endptr != '.') {
333 break;
334 }
335 ptr = endptr + 1;
336 i++;
337 }
338 if (endptr && *endptr) {
339 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_DEBUG, "ignore suffix '%s' of version string '%s'", endptr, version_str);
340 }
341
342 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_DEBUG, "parse version string '%s' to major: %ld, minor: %ld, patch: %ld version", version_str, version[0], version[1], version[2] );
343 }
344
345 static bool compare_version_ge(const char *left_version_str, const char *right_version_str, int linenumber, char *filename, char* linebuf) {
346 long left_version[3] = { 0 };
347 long right_version[3] = { 0 };
348 parse_version(left_version_str, left_version, linenumber, filename, linebuf);
349 parse_version(right_version_str, right_version, linenumber, filename, linebuf);
350
351 for (int i = 0 ; i <= 2 ; ++i) {
352 if (left_version[i] < right_version[i]) {
353 return false;
354 } else if (left_version[i] > right_version[i]) {
355 return true;
356 }
357 }
358 return true;
359 }
360
361 static bool eval_bool_expression(struct bool_expression* expression, int linenumber, char *filename, char* linebuf) {
362 bool result = false;
363 union {
364 bool _bool;
365 char *_str;
366 } left;
367 union {
368 bool _bool;
369 char *_str;
370 } right;
371
372 switch (expression->op) {
373 case BOOL_OP_EXISTS:
374 left._str = eval_string_expression(expression->left._str, linenumber, filename, linebuf);
375 int retval = access(left._str, F_OK);
376 result = (retval == 0);
377 log_msg(LOG_LEVEL_DEBUG, "access('%s', F_OK) returns %d: (%s)", left._str, retval, result?"Success":strerror(errno));
378 log_msg(eval_log_level, "eval(%p): bool exists '%s': %s", (void*) expression, left._str, btoa(result));
379 free(left._str);
380 break;
381 case BOOL_OP_DEFINED:
382 left._str = eval_string_expression(expression->left._str, linenumber, filename, linebuf);
383 result = list_find(left._str, conf->defsyms) != NULL;
384 log_msg(eval_log_level, "eval(%p): bool defined '%s': %s", (void*) expression, left._str, btoa(result));
385 free(left._str);
386 break;
387 case BOOL_OP_HOSTNAME:
388 left._str = eval_string_expression(expression->left._str, linenumber, filename, linebuf);
389 if (conf->hostname) {
390 result = strcmp(left._str, conf->hostname) == 0;
391 } else {
392 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_WARNING, "%s", "hostname not available; ifhost and ifnhost always evaluate to 'false'")
393 }
394 log_msg(eval_log_level, "eval(%p): bool hostname '%s' (hostname: '%s'): %s", (void*) expression, left._str, conf->hostname, btoa(result));
395 free(left._str);
396 break;
397 case BOOL_OP_NOT:
398 left._bool = eval_bool_expression(expression->left._bool, linenumber, filename, linebuf);
399 result = !left._bool;
400 log_msg(eval_log_level, "eval(%p): bool !%s: %s", (void*) expression, btoa(left._bool), btoa(result));
401 break;
402 case BOOL_OP_VERSION_GE:
403 left._str = eval_string_expression(expression->left._str, linenumber, filename, linebuf);
404 right._str = eval_string_expression(expression->right._str, linenumber, filename, linebuf);
405 result = compare_version_ge(left._str, right._str, linenumber, filename, linebuf);
406 log_msg(eval_log_level, "eval(%p): bool %s version_ge %s: %s", (void*) expression, left._str, right._str, btoa(result));
407 free(left._str);
408 free(right._str);
409 break;
410 }
411 return result;
412 }
413
414 static void eval_group_statement(group_statement statement, int linenumber, char *filename, char* linebuf) {
415 DB_ATTR_TYPE attr, prev_attr;
416 char *str;
417 attr = eval_attribute_expression(statement.expr, linenumber, filename, linebuf);
418 if ((prev_attr = do_groupdef(statement.name, attr))) {
419 char *str2;
420 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "redefine group '%s' with value '%s' (previous value: '%s')", statement.name, str = diff_attributes(0, attr), str2 = diff_attributes(0, prev_attr))
421 free(str2);
422 } else {
423 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "define group '%s' with value '%s'", statement.name, str = diff_attributes(0, attr))
424 }
425 free(str);
426 }
427
428 static bool evaL_if_condition(if_condition* c) {
429 log_msg(eval_log_level, "eval(%p): if condition", (void*) c);
430 bool cond_result = eval_bool_expression(c->expression, c->linenumber, c->filename, c->linebuf);
431 log_msg(LOG_LEVEL_CONFIG,"%s:%d: if condition results to '%s' (line: '%s')", c->filename, c->linenumber, btoa(cond_result), c->linebuf);
432 return cond_result;
433 }
434
435 static void eval_if_statement(if_statement statement, int include_depth, char* rule_prefix, int linenumber, char *filename, char* linebuf) {
436 bool condition = evaL_if_condition(statement.condition);
437
438 if (condition) {
439 log_msg(eval_log_level, "eval(%p): if branch", (void*) statement.if_branch);
440 if (statement.if_branch) {
441 eval_config(statement.if_branch, include_depth, rule_prefix);
442 }
443 } else {
444 log_msg(eval_log_level, "eval(%p): else branch", (void*) statement.else_branch);
445 if (statement.else_branch) {
446 eval_config(statement.else_branch, include_depth, rule_prefix);
447 }
448 }
449 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "%s", "endif")
450 }
451
452 static void eval_define_statement(define_statement statement, int linenumber, char *filename, char* linebuf) {
453 do_define(statement.name,
454 /* not to be freed, reused in do_define */
455 statement.value?eval_string_expression(statement.value, linenumber, filename, linebuf):NULL,
456 linenumber, filename, linebuf);
457 }
458
459 static void eval_undefine_statement(undefine_statement statement, int linenumber, char *filename, char* linebuf) {
460 do_undefine(statement.name, linenumber, filename, linebuf);
461 }
462
463 static int dirfilter(const struct dirent *d) {
464 return (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0);
465 }
466
467 static void eval_x_include_setenv_statement(x_include_setenv_statement statement, int linenumber, char *filename, char* linebuf) {
468 symba* s=NULL;
469 list* l=NULL;
470
471 char* value = eval_string_expression(statement.value, linenumber, filename, linebuf);
472
473 if(!(l=list_find(statement.variable,x_include_environment))) {
474 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "add '%s' with value '%s' to @@x_include environment", statement.variable, value)
475 s=(symba*)checked_malloc(sizeof(symba));
476 s->name=checked_strdup(statement.variable);
477 s->value=value;
478 x_include_environment=list_append(x_include_environment,(void*)s);
479 } else {
480 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "overwrite '%s' variable with value '%s' in @@x_include environment (previous value: '%s')", statement.variable, value, ((symba*)l->data)->value)
481 free(((symba*)l->data)->value);
482 ((symba*)l->data)->value=NULL;
483 ((symba*)l->data)->value=value;
484 }
485 }
486
487 static void include_file(const char* file, bool execute, int include_depth, char* rule_prefix) {
488 if (execute) {
489 int p_stdout[2];
490 int p_stderr[2];
491 pid_t pid;
492
493 if (pipe(p_stdout)==-1) {
494 log_msg(LOG_LEVEL_ERROR, "%s: pipe stdout failed: %s", file, strerror(errno));
495 exit(EXEC_ERROR);
496 }
497 if (pipe(p_stderr)==-1) {
498 log_msg(LOG_LEVEL_ERROR, "%s: pipe stderr failed: %s", file, strerror(errno));
499 exit(EXEC_ERROR);
500 }
501 if ((pid = fork()) == -1) {
502 log_msg(LOG_LEVEL_ERROR, "%s: fork failed: %s", file, strerror(errno));
503 exit(EXEC_ERROR);
504 }
505
506 if(pid == 0) {
507 /* child */
508 close(p_stdout[0]);
509 dup2 (p_stdout[1], STDOUT_FILENO);
510 close(p_stdout[1]);
511 close(p_stderr[0]);
512 dup2 (p_stderr[1], STDERR_FILENO);
513 close(p_stderr[1]);
514
515 list *entry;
516 for(entry = x_include_environment ; entry != NULL; entry = entry->next) {
517 char* name = ((symba*)entry->data)->name;
518 char* value = ((symba*)entry->data)->value;
519 if (setenv(name, value, 0) < 0) {
520 fprintf(stderr, "aide: @@x_include: setenv for '%s' failed: %s\n", name, strerror(errno));
521 exit(EXEC_ERROR);
522 }
523 }
524
525 execl(file, file, (char*) NULL);
526 } else {
527 /* parent */
528 close(p_stdout[1]);
529 close(p_stderr[1]);
530
531 char* config_str = pipe2string(p_stdout[0]);
532 close(p_stdout[0]);
533 char* child_stderr = pipe2string(p_stderr[0]);
534 close(p_stderr[0]);
535
536 int wstatus;
537 waitpid(pid, &wstatus, 0);
538 if (child_stderr || !WIFEXITED(wstatus) || WEXITSTATUS(wstatus)) {
539 char* newline;
540
541 while (child_stderr && *child_stderr != '\0') {
542 newline = strchr(child_stderr, '\n');
543 if (newline != NULL) {
544 log_msg(LOG_LEVEL_ERROR, "%s: stderr> %.*s", file, (int)(newline-child_stderr), child_stderr);
545 child_stderr = newline+1;
546 } else {
547 log_msg(LOG_LEVEL_ERROR, "%s: stderr> %s", file, child_stderr);
548 break;
549 }
550 }
551 log_msg(LOG_LEVEL_ERROR, "%s: execution failed (exit status: %d)", file, WEXITSTATUS(wstatus));
552 exit(EXEC_ERROR);
553 }
554
555 if (config_str) {
556 ast* config_ast = NULL;
557 int len = (strlen(file) + 10) * sizeof(char);
558 char * source_name = checked_malloc(len);
559 snprintf(source_name, len, "%s (stdout)", file);
560 conf_lex_string(source_name, config_str);
561 if(confparse(&config_ast)){
562 exit(INVALID_CONFIGURELINE_ERROR);
563 }
564 conf_lex_delete_buffer();
565 free(source_name);
566 eval_config(config_ast, include_depth, rule_prefix);
567 deep_free(config_ast);
568 }
569 free(config_str);
570 }
571 } else {
572 ast* config_ast = NULL;
573 conf_lex_file(file);
574 if(confparse(&config_ast)){
575 exit(INVALID_CONFIGURELINE_ERROR);
576 }
577 conf_lex_delete_buffer();
578 eval_config(config_ast, include_depth, rule_prefix);
579 deep_free(config_ast);
580 }
581 }
582
583 void check_permissions(const char* path, struct stat *st, int linenumber, char *filename, char* linebuf) {
584 if ((st->st_uid != geteuid() && st->st_uid != 0) || (st->st_mode & 002) != 0 || (st->st_mode & 020) != 0) {
585 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "'@@x_include': bad ownership or modes for '%s' (please ensure it is neither group- nor world-writable and owned by the current user or root)", path)
586 exit(INVALID_CONFIGURELINE_ERROR);
587 }
588 }
589
590 static void include_directory(const char* dir, const char* rx, bool execute, char* new_rule_prefix, int include_depth, char* rule_prefix, int linenumber, char *filename, char* linebuf) {
591 LOG_CONFIG_FORMAT_LINE_PREFIX(LOG_LEVEL_CONFIG, "include directory '%s' (regex: '%s', execute: %s , prefix: '%s', depth: %d)", dir, rx, btoa(execute), new_rule_prefix?new_rule_prefix:"", include_depth)
592
593 struct dirent **namelist;
594 int n;
595
596 int pcre2_errorcode;
597 PCRE2_SIZE pcre2_erroffset;
598 pcre2_code* crx;
599
600 if((crx = pcre2_compile((PCRE2_SPTR) rx, PCRE2_ZERO_TERMINATED, PCRE2_UTF, &pcre2_errorcode, &pcre2_erroffset, NULL)) == NULL) {
601 PCRE2_UCHAR pcre2_error[128];
602 pcre2_get_error_message(pcre2_errorcode, pcre2_error, 128);
603 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "'%s': error in regular expression '%s' at %zu: %s", execute?"@@x_include":"@@include", rx, pcre2_erroffset, pcre2_error)
604 exit(INVALID_CONFIGURELINE_ERROR);
605 }
606 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(crx, NULL);
607 if (match_data == NULL) {
608 log_msg(LOG_LEVEL_ERROR, "pcre2_match_data_create_from_pattern: failed to allocate memory");
609 exit(MEMORY_ALLOCATION_FAILURE);
610 }
611
612 struct stat fs;
613
614 if (execute) {
615 if (stat(dir,&fs) == -1) {
616 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "'@@x_include': stat for '%s' failed: %s", dir, strerror(errno))
617 exit(INVALID_CONFIGURELINE_ERROR);
618 }
619 check_permissions(dir, &fs, linenumber, filename, linebuf);
620 }
621
622 n = scandir(dir, &namelist, dirfilter, alphasort);
623 if (n == -1) {
624 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "'%s': failed to open directory '%s': %s", execute?"@@x_include":"@@include", dir, strerror(errno))
625 exit(INVALID_CONFIGURELINE_ERROR);
626 }
627
628 if (new_rule_prefix && *new_rule_prefix != '/') {
629 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "'%s': invalid rule prefix '%s': unexpected first character '%c', expected '/'", execute?"@@x_include":"@@include", new_rule_prefix, *new_rule_prefix)
630 exit(INVALID_CONFIGURELINE_ERROR);
631 }
632 /* not to be freed, reused in add_rx_rule_to_tree */
633 char* nested_rule_prefix = NULL;
634 if (rule_prefix && new_rule_prefix) {
635 int length = strlen(rule_prefix)+strlen(new_rule_prefix);
636 nested_rule_prefix = checked_malloc(length+1);
637 strncpy(nested_rule_prefix, rule_prefix, length+1);
638 strncat(nested_rule_prefix, new_rule_prefix, length-strlen(rule_prefix)+1);
639 log_msg(LOG_LEVEL_DEBUG, "concatenate current prefix '%s' and new prefix '%s': '%s'", rule_prefix, new_rule_prefix, nested_rule_prefix);
640 free(new_rule_prefix);
641 } else if (rule_prefix) {
642 nested_rule_prefix = rule_prefix;
643 } else if (new_rule_prefix) {
644 nested_rule_prefix = new_rule_prefix;
645 }
646
647 int dir_len = strlen(dir);
648 for (int i = 0 ; i < n ; ++i) {
649
650 int len = (dir_len+strlen(namelist[i]->d_name)+2)*sizeof(char);
651 char * filepath = checked_malloc(len);
652 snprintf(filepath, len, "%s/%s", dir, namelist[i]->d_name);
653 if (stat(filepath,&fs) == -1) {
654 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "'%s': stat for '%s' failed: %s", execute?"@@x_include":"@@include", filepath, strerror(errno))
655 exit(INVALID_CONFIGURELINE_ERROR);
656 }
657 if (S_ISREG(fs.st_mode)) {
658 int match=pcre2_match(crx, (PCRE2_SPTR) namelist[i]->d_name, PCRE2_ZERO_TERMINATED, 0, 0, match_data, NULL);
659 if(match < 0) {
660 log_msg(LOG_LEVEL_DEBUG,"%s: skip '%s' (reason: file name does not match regex '%s')", dir, namelist[i]->d_name, rx);
661 } else {
662 int exec = execute && S_IXUSR&fs.st_mode;
663 if (exec) {
664 check_permissions(filepath, &fs, linenumber, filename, linebuf);
665 }
666 log_msg(LOG_LEVEL_CONFIG,"%s: %s '%s'", dir, exec?"execute":"include", namelist[i]->d_name);
667 include_file(filepath, exec, include_depth, nested_rule_prefix);
668 }
669 } else {
670 log_msg(LOG_LEVEL_DEBUG,"%s: skip '%s' (reason: file is not a regular file)", dir, namelist[i]->d_name);
671 }
672
673 free(filepath);
674 free(namelist[i]);
675 }
676 free(namelist);
677 pcre2_match_data_free(match_data);
678 pcre2_code_free(crx);
679 }
680
681 static void eval_include_statement(include_statement statement, int include_depth, char* rule_prefix, int linenumber, char *filename, char* linebuf) {
682 if (include_depth >= 16) {
683 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "%s", "include files are nested too deeply")
684 exit(INVALID_CONFIGURELINE_ERROR);
685 }
686 char* path = eval_string_expression(statement.path, linenumber, filename, linebuf);
687 char* rx = statement.rx?eval_string_expression(statement.rx, linenumber, filename, linebuf):NULL;
688
689 if (rx) {
690 char* new_rule_prefix = NULL;
691 if (statement.prefix) {
692 new_rule_prefix = eval_string_expression(statement.prefix, linenumber, filename, linebuf);
693 int prefix_length = strlen(new_rule_prefix);
694 if (prefix_length && new_rule_prefix[prefix_length-1] == '/') {
695 new_rule_prefix[prefix_length-1] = '\0';
696 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "%s: removed trailing '/' from prefix: '%s'", statement.execute?"@@x_include":"@@include", new_rule_prefix);
697 }
698 }
699 include_directory(path, rx, statement.execute, new_rule_prefix, include_depth, rule_prefix, linenumber, filename, linebuf);
700 free(rx);
701 } else {
702 struct stat fs;
703 if (lstat(path,&fs) == -1) {
704 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "'@@include': lstat for '%s' failed: %s", path, strerror(errno))
705 exit(INVALID_CONFIGURELINE_ERROR);
706 }
707 if (S_ISREG(fs.st_mode)) {
708 LOG_CONFIG_FORMAT_LINE_PREFIX(LOG_LEVEL_CONFIG, "include file '%s' (depth: %d)", path, include_depth)
709 include_file(path, statement.execute && S_IXUSR&fs.st_mode, include_depth, rule_prefix);
710 } else {
711 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "'@@include': '%s' is not a regular file", path);
712 exit(INVALID_CONFIGURELINE_ERROR);
713 }
714 }
715 }
716
717 static FT_TYPE eval_ft_restriction_expression(ft_restriction_expression *expression, int linenumber, char *filename, char* linebuf) {
718 FT_TYPE rs = FT_NULL, rs_r;
719
720 if (expression) {
721 rs_r = (strlen(expression->right) == 1)?get_f_type_from_char(*(expression->right)):FT_NULL;
722 log_msg(eval_log_level, "eval(%p): file type restriction '%s' evaluates to %u", (void*) expression, expression->right, rs_r);
723 if (rs_r == FT_NULL) {
724 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "invalid restriction '%s'", expression->right)
725 exit(INVALID_CONFIGURELINE_ERROR);
726 }
727 if (expression->left == NULL) {
728 rs = rs_r;
729 } else {
730 rs = eval_ft_restriction_expression(expression->left, linenumber, filename, linebuf) | rs_r;
731 }
732 } else {
733 log_msg(eval_log_level, "eval(%p): file type restriction is NULL, returning %u", (void*) expression, rs);
734 }
735 return rs;
736 }
737
738 static rx_restriction_t eval_restriction_expression(restriction_expression *expression, int linenumber, char *filename, char* linebuf) {
739 rx_restriction_t rs = { .f_type = FT_NULL,
740 #ifdef HAVE_FSTYPE
741 .fs_type = 0
742 #endif
743 };
744
745 if (expression) {
746 if (
747 expression->fs_type == NULL &&
748 expression->f_types == NULL) {
749 log_msg(eval_log_level, "eval(%p): restriction is '0', returning {"
750 #ifdef HAVE_FSTYPE
751 "fs_type 0x%lx, "
752 #endif
753 "file type: %u)", (void*) expression,
754 #ifdef HAVE_FSTYPE
755 rs.fs_type,
756 #endif
757 rs.f_type);
758 } else {
759 rs.f_type = eval_ft_restriction_expression(expression->f_types, linenumber, filename, linebuf);
760 if (expression->fs_type != NULL) {
761 #ifdef HAVE_FSTYPE
762 rs.fs_type = get_fs_type_from_string(expression->fs_type);
763 if (rs.fs_type == 0) {
764 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "invalid file system restriction '%s'", expression->fs_type)
765 exit(INVALID_CONFIGURELINE_ERROR);
766 }
767 log_msg(eval_log_level, "eval(%p): file system restriction '%s' evaluates to 0x%lx", (void*) expression, expression->fs_type, rs.fs_type);
768 #else
769 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "%s", "file system type restriction is not supported on non-Linux platforms")
770 exit(INVALID_CONFIGURELINE_ERROR);
771 #endif
772 }
773 log_msg(eval_log_level, "eval(%p): restriction {"
774 #ifdef HAVE_FSTYPE
775 "fs_type 0x%lx, "
776 #endif
777 "file type: %u)", (void*) expression,
778 #ifdef HAVE_FSTYPE
779 rs.fs_type,
780 #endif
781 rs.f_type);
782 }
783 } else {
784 log_msg(eval_log_level, "eval(%p): restriction is NULL, returning {"
785 #ifdef HAVE_FSTYPE
786 "fs_type 0x%lx, "
787 #endif
788 "file type: %u)", (void*) expression,
789 #ifdef HAVE_FSTYPE
790 rs.fs_type,
791 #endif
792 rs.f_type);
793 }
794 return rs;
795 }
796
797 static void eval_rule_statement(rule_statement statement, char* rule_prefix, int linenumber, char *filename, char* linebuf) {
798 char *rx = eval_string_expression(statement.path, linenumber, filename, linebuf);
799 if (conf->action&DO_DRY_RUN && conf->config_check_warn_unrestricted_rules && !statement.restriction) {
800 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_WARNING, "%s '%s' is unrestricted", get_rule_type_long_string(statement.type), rx)
801 }
802 if(!add_rx_rule_to_tree(
803 rx,
804 rule_prefix,
805 eval_restriction_expression(statement.restriction, linenumber, filename, linebuf),
806 eval_attribute_expression(statement.attributes, linenumber, filename, linebuf),
807 statement.type,
808 conf->tree,
809 linenumber, filename, linebuf)) {
810 exit(INVALID_CONFIGURELINE_ERROR);
811 }
812 free(rx);
813 }
814
815 void eval_config(ast* config_ast, int include_depth, char *rule_prefix) {
816 ast* node = NULL;
817 for(node = config_ast; node != NULL; node = node->next) {
818 log_msg(eval_log_level, "eval(%p): ast node (next: %p)", (void*) node, (void*) node->next);
819 switch (node->type) {
820 case config_option_type:
821 eval_config_statement(node->statement._config, node->linenumber, node->filename, node->linebuf);
822 break;
823 case include_statement_type:
824 eval_include_statement(node->statement._include, include_depth+1, rule_prefix, node->linenumber, node->filename, node->linebuf);
825 break;
826 case x_include_setenv_statement_type:
827 eval_x_include_setenv_statement(node->statement._x_include_setenv, node->linenumber, node->filename, node->linebuf);
828 break;
829 case if_statement_type:
830 eval_if_statement(node->statement._if, include_depth, rule_prefix, node->linenumber, node->filename, node->linebuf);
831 break;
832 case define_statement_type:
833 eval_define_statement(node->statement._define, node->linenumber, node->filename, node->linebuf);
834 break;
835 case undefine_statement_type:
836 eval_undefine_statement(node->statement._undefine, node->linenumber, node->filename, node->linebuf);
837 break;
838 case group_statement_type:
839 eval_group_statement(node->statement._group, node->linenumber, node->filename, node->linebuf);
840 break;
841 case rule_statement_type:
842 eval_rule_statement(node->statement._rule, rule_prefix, node->linenumber, node->filename, node->linebuf);
843 break;
844 }
845 }
846 }