Debian Codegraph @

src/commandconf.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 1999-2006, 2010-2011, 2013, 2015-2016, 2019-2025 Rami Lehti,
5 * Pablo Virolainen, Richard van den Berg, Hannes von Haugwitz
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "aide.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <stdbool.h>
28 #include <zlib.h>
29 #include "attributes.h"
30 #include "conf_ast.h"
31 #include "config.h"
32 #include "errorcodes.h"
33 #include "hashsum.h"
34 #include "list.h"
35 #include "rx_rule.h"
36 #include "url.h"
37 #include "commandconf.h"
38 #include "conf_lex.h"
39 #include "log.h"
40 #include "conf_yacc.h"
41 #include "db.h"
42 #include "db_config.h"
43 #include "report.h"
44 #include "symboltable.h"
45 #include "md.h"
46 #include "util.h"
47 #include "conf_eval.h"
48 #include "seltree.h"
49 /*for locale support*/
50 #include "locale-aide.h"
51 /*for locale support*/
52 #ifdef WITH_CURL
53 #include "fopen.h"
54 #endif
55 #ifdef WITH_E2FSATTRS
56 #include "e2fsattrs.h"
57 #endif
58 #include <math.h>
59 #include <errno.h>
60 #include <unistd.h>
61
62 #define BUFSIZE 4096
63 #define ZBUFSIZE 16384
64
65 url_t* parse_url(char* val, int linenumber, char* filename, char* linebuf)
66 {
67 url_t* u=NULL;
68 char* r=NULL;
69 char* val_copy=NULL;
70 int i=0;
71
72 u=checked_malloc(sizeof(url_t));
73
74 /* We don't want to modify the original hence strdup(val) */
75 val_copy=checked_strdup(val);
76 for(r=val_copy;r[0]!=':'&&r[0]!='\0';r++);
77
78 if(r[0]!='\0'){
79 r[0]='\0';
80 r++;
81 }
82
83 u->type = get_url_type(val_copy);
84 if (u->type) {
85 switch (u->type) {
86 case url_file : {
87 if(r[0]=='/'&&(r+1)[0]=='/'&&(r+2)[0]=='/'){
88 u->value=checked_strdup(r+2);
89 break;
90 }
91 if(r[0]=='/'&&(r+1)[0]=='/'&&(r+2)[0]!='/'){
92 char* t=r+2;
93 r+=2;
94 for(i=0;r[0]!='/'&&r[0]!='\0';r++,i++);
95 if(r[0]=='\0'){
96 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "invalid file-URL '%s': no path after hostname", val)
97 free(val_copy);
98 free(u);
99 return NULL;
100 }
101 if( (strcmp(t,"localhost") != 0) && !( conf->hostname && strcmp(t,conf->hostname)==0)){
102 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "invalid file-URL '%s': cannot use hostname other than 'localhost' or '%s'", val, conf->hostname);
103 free(u);
104 free(val_copy);
105 return NULL;
106 }
107 u->value=checked_strdup(r);
108 r[0]='\0';
109
110 break;
111 }
112 u->value=checked_strdup(r);
113 break;
114 }
115 case url_ftp :
116 case url_https :
117 case url_http : {
118 #ifdef WITH_CURL
119 u->value=checked_strdup(val);
120 #else
121 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "%s", "http, https and ftp URL support not compiled in, recompile AIDE with '--with-curl'")
122 free(val_copy);
123 free(u);
124 return NULL;
125 #endif /* WITH CURL */
126 break;
127 }
128 case url_fd:
129 case url_stdin:
130 case url_stdout:
131 case url_stderr: {
132 u->value=checked_strdup(r);
133 break;
134 }
135 case url_syslog : {
136 #ifdef HAVE_SYSLOG
137 u->value=checked_strdup(r);
138 #else
139 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "%s", "syslog url support not compiled in, recompile AIDE with syslog support")
140 free(val_copy);
141 free(u);
142 return NULL;
143 #endif
144 break;
145 }
146 }
147 } else {
148 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "unknown URL-type: '%s'", val_copy);
149 free(val_copy);
150 free(u);
151 return NULL;
152 }
153
154 free(val_copy);
155
156 u->raw = checked_strdup(val);
157
158 return u;
159 }
160
161 int parse_config(char *before, char *config, char* after) {
162 if(before==NULL && after==NULL && (config==NULL||strcmp(config,"")==0)){
163 log_msg(LOG_LEVEL_ERROR,_("missing configuration (use '--config' '--before' or '--after' command line parameter)"));
164 return RETFAIL;
165 }
166
167 ast* config_ast = NULL;
168 if (before) {
169 conf_lex_string("(--before)", before);
170 if(confparse(&config_ast)){
171 return RETFAIL;
172 }
173 conf_lex_delete_buffer();
174 eval_config(config_ast, 0, NULL);
175 deep_free(config_ast);
176 config_ast = NULL;
177 }
178 if (config) {
179 conf_lex_file(config);
180 if(confparse(&config_ast)){
181 return RETFAIL;
182 }
183 conf_lex_delete_buffer();
184 eval_config(config_ast, 0, NULL);
185 deep_free(config_ast);
186 config_ast = NULL;
187 }
188 if (after) {
189 conf_lex_string("(--after)", after);
190 if(confparse(&config_ast)){
191 return RETFAIL;
192 }
193 conf_lex_delete_buffer();
194 eval_config(config_ast, 0, NULL);
195 deep_free(config_ast);
196 config_ast = NULL;
197 }
198 return RETOK;
199 }
200
201 int conf_input_wrapper(char* buf, int max_size, FILE* in)
202 {
203 int retval=0;
204 retval=fread(buf,1,max_size,in);
205 return retval;
206 }
207
208 void do_define(char* name, char* value, int linenumber, char* filename, char* linebuf)
209 {
210 symba* s=NULL;
211 list* l=NULL;
212
213 if(!(l=list_find(name,conf->defsyms))){
214 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "define '%s' with value '%s'", name, value)
215 s=(symba*)checked_malloc(sizeof(symba));
216 s->name=checked_strdup(name);
217 s->value=value;
218 conf->defsyms=list_append(conf->defsyms,(void*)s);
219 }
220 else {
221 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "redefine '%s' with value '%s' (previous value: '%s')", name, value, ((symba*)l->data)->value)
222 free(((symba*)l->data)->value);
223 ((symba*)l->data)->value=NULL;
224 ((symba*)l->data)->value=value;
225 }
226 }
227
228 void do_undefine(char* name, int linenumber, char* filename, char* linebuf)
229 {
230 list*r=NULL;
231
232 if((r=list_find(name,conf->defsyms))){
233 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "undefine '%s' (value: '%s')", name, ((symba*)r->data)->value)
234 free(((symba*)r->data)->name);
235 free(((symba*)r->data)->value);
236 free((symba*)r->data);
237 r->data=NULL;
238 conf->defsyms=list_delete_item(r);
239 } else {
240 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "variable '%s' to be undefined not found", name);
241 }
242 }
243
244 bool add_rx_rule_to_tree(char* rx, char* rule_prefix, rx_restriction_t restriction, DB_ATTR_TYPE attr, int type, seltree *tree, int linenumber, char* filename, char* linebuf) {
245
246 rx_rule* r=NULL;
247
248 bool retval = false;
249
250 char *attr_str = NULL;
251 char *rs_str = NULL;
252
253 char *regex;
254 char *node_path = NULL;
255 if (rule_prefix) {
256 int length = strlen(rule_prefix)+strlen(rx);
257 regex = checked_malloc(length+1);
258 strncpy(regex, rule_prefix, length+1);
259 strncat(regex, rx, length-strlen(rule_prefix)+1);
260 log_msg(LOG_LEVEL_DEBUG, "prepend regex '%s' with prefix '%s': '%s'", rx, rule_prefix, regex);
261 } else {
262 regex = checked_strdup(rx);
263 }
264
265 if ((r = add_rx_to_tree(regex, restriction, type, tree, linenumber, filename, linebuf, &node_path)) == NULL) {
266 retval = false;
267 }else {
268 r->prefix = rule_prefix;
269
270 attr &= validate_hashes(attr, linenumber, filename, linebuf);
271
272 DB_ATTR_TYPE unsupported_attrs = attr&
273 (0
274 #ifndef WITH_ACL
275 |ATTR(attr_acl)
276 #endif
277 #ifndef WITH_SELINUX
278 |ATTR(attr_selinux)
279 #endif
280 #ifndef WITH_XATTR
281 |ATTR(attr_xattrs)
282 #endif
283 #ifndef WITH_E2FSATTRS
284 |ATTR(attr_e2fsattrs)
285 #endif
286 #ifndef WITH_CAPABILITIES
287 |ATTR(attr_capabilities)
288 #endif
289 #ifndef HAVE_FSTYPE
290 |ATTR(attr_fs_type)
291 #endif
292 )
293 ;
294 if (unsupported_attrs) {
295 char *str;
296 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_WARNING, "ignoring not compiiled-in attribute(s): %s", str = diff_attributes(0, unsupported_attrs));
297 free(str);
298 attr &= ~unsupported_attrs;
299 }
300
301 r->attr=attr;
302 if (attr&ATTR(attr_sizeg)) {
303 log_msg(LOG_LEVEL_WARNING, "%s:%d: Using 'S' attribute is DEPRECATED and will be removed in the release after next. Update your config and use 'growing+s' instead (line: '%s')", filename, linenumber, linebuf);
304 }
305 if (attr&ATTR(attr_compressed) && !(attr&get_hashes(false))) {
306 log_msg(LOG_LEVEL_WARNING, "%s:%d: ignore 'comprressed' attribute (no hashsum attributes are set) (line: '%s')", filename, linenumber, linebuf);
307 }
308 conf->db_out_attrs |= attr;
309
310 LOG_CONFIG_FORMAT_LINE_PREFIX(LOG_LEVEL_CONFIG, "add %s '%s%s %s %s' to node '%s'", get_rule_type_long_string(type), get_rule_type_char(type), r->rx, rs_str = get_restriction_string(r->restriction), attr_str = diff_attributes(0, r->attr), node_path)
311 free(rs_str);
312 free(attr_str);
313
314 retval = true;
315 }
316 return retval;
317 }
318
319 DB_ATTR_TYPE do_groupdef(char* group,DB_ATTR_TYPE value)
320 {
321 log_msg(LOG_LEVEL_DEBUG, "define attribute group '%s' with value %llu", group, value);
322 list* r=NULL;
323 symba* s=NULL;
324
325 if((r=list_find(group,conf->groupsyms))){
326 DB_ATTR_TYPE prev_value = ((symba*)r->data)->ival;
327 ((symba*)r->data)->ival=value;
328 return prev_value;
329 }
330 /* This is a new group */
331 s=checked_malloc(sizeof(symba));
332 s->name=checked_strdup(group);
333 s->ival=value;
334 conf->groupsyms=list_append(conf->groupsyms,(void*)s);
335 return 0;
336 }
337
338 DB_ATTR_TYPE get_groupval(char* group)
339 {
340 list* r=NULL;
341
342 if((r=list_find(group,conf->groupsyms))){
343 return (((symba*)r->data)->ival);
344 }
345 return DB_ATTR_UNDEF;
346 }
347
348 bool do_dbdef(DB_TYPE dbtype ,char* val, int linenumber, char* filename, char* linebuf)
349 {
350 url_t* u=NULL;
351 database *db = NULL;
352 char *db_option_name = NULL;
353
354 switch(dbtype) {
355 case DB_TYPE_IN: {
356 db_option_name = "database_in";
357 db = &(conf->database_in);
358 break;
359 }
360 case DB_TYPE_OUT: {
361 db_option_name = "database_out";
362 db = &(conf->database_out);
363 break;
364 }
365 case DB_TYPE_NEW: {
366 db_option_name = "database_new";
367 db = &(conf->database_new);
368 break;
369 }
370 }
371
372 if(db->url == NULL){
373 if ((u=parse_url(val, linenumber, filename, linebuf)) != NULL) {
374 /* FIXME Check the URL if you add support for databases that cannot be
375 * both input and output urls */
376 switch (dbtype) {
377 case DB_TYPE_IN:
378 case DB_TYPE_NEW:{
379 switch (u->type) {
380 case url_stdout:
381 case url_stderr:
382 case url_syslog: {
383 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "'%s': unsupported URL-type: '%s'", db_option_name, get_url_type_string(u->type))
384 return false;
385 }
386 case url_stdin:
387 case url_ftp:
388 case url_http:
389 case url_https:
390 case url_fd:
391 case url_file:
392 /* url type is supported */
393 break;
394 }
395 break;
396 }
397 case DB_TYPE_OUT: {
398 switch (u->type) {
399 case url_stdin:
400 case url_stderr:
401 case url_syslog: {
402 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "'%s': unsupported URL-type: '%s'", db_option_name, get_url_type_string(u->type))
403 return false;
404 }
405 case url_stdout:
406 case url_ftp:
407 case url_http:
408 case url_https:
409 case url_fd:
410 case url_file:
411 /* url type is supported */
412 break;
413 }
414 break;
415 }
416 }
417 db->url = u;
418 db->linenumber = linenumber;
419 db->filename = filename;
420 db->linebuf = linebuf?checked_strdup(linebuf):NULL;
421 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set '%s' option to '%s'", db_option_name, u->raw)
422 } else {
423 return false;
424 }
425 } else {
426 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "'%s' option already set to '%s' (ignore new value '%s')", db_option_name, (db->url)->raw, val);
427 }
428 return true;
429 }
430
431 bool do_repurldef(char* val, int linenumber, char* filename, char* linebuf) {
432 url_t* u = parse_url(val, linenumber, filename, linebuf);
433 return add_report_url(u, linenumber, filename, linebuf);
434 }
435
436 bool do_reportlevel(char* val, int linenumber, char* filename, char* linebuf) {
437 REPORT_LEVEL report_level = REPORT_LEVEL_UNKNOWN;
438
439 report_level = get_report_level(val);
440 if (report_level) {
441 conf->report_level = report_level;
442 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set 'report_level' option to '%s' (raw: %d)", val, report_level)
443 return true;
444 } else {
445 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "invalid report level: '%s'", val);
446 return false;
447 }
448 }
449
450 bool do_rootprefix(char* val, int linenumber, char* filename, char* linebuf) {
451 if (conf->root_prefix == NULL) {
452 conf->root_prefix=val;
453 conf->root_prefix_length=strlen(conf->root_prefix);
454 if (conf->root_prefix_length && conf->root_prefix[conf->root_prefix_length-1] == '/') {
455 conf->root_prefix[--conf->root_prefix_length] = '\0';
456 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "removed trailing '/' from root prefix: '%s'", conf->root_prefix);
457 }
458 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set 'root_prefix' option to '%s'", conf->root_prefix)
459 return true;
460 } else {
461 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "'root_prefix' option already set to '%s' (ignore new value '%s')", conf->root_prefix, val);
462 return false;
463 }
464 }
465
466 long do_num_workers(const char *str) {
467 char *err;
468 long number = strtol(str,&err,10);
469 if (err[0] == '%' && err[1] == '\0') {
470 if (number >= 0 && number <= 100) {
471 long num_of_processors = sysconf(_SC_NPROCESSORS_ONLN);
472 return ceill(num_of_processors * number / 100.0);
473 } else {
474 return -1;
475 }
476 } else if(*err != '\0' || number < 0 || errno == ERANGE) {
477 return -1;
478 }
479 return number;
480 }
481
482 #ifdef WITH_E2FSATTRS
483 void do_report_ignore_e2fsattrs(char* val, int linenumber, char* filename, char* linebuf) {
484 conf->report_ignore_e2fsattrs = 0UL;
485 if (strcmp(val, "0") != 0) {
486 while (*val) {
487 unsigned long flag = e2fsattrs_get_flag(*val);
488 if (flag) {
489 conf->report_ignore_e2fsattrs |= flag;
490 } else {
491 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "ignore invalid ext2 file attribute: '%c'", *val)
492 }
493 val++;
494 }
495 }
496 }
497 #endif