Debian Codegraph @

src/aide.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 1999-2006, 2010-2013, 2015-2017, 2019-2025 Rami Lehti,
5 * Pablo Virolainen, Mike Markley, Richard van den Berg,
6 * Hannes von Haugwitz
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "config.h"
24
25 #include <sys/stat.h>
26 #include <sys/ioctl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <dirent.h>
32 #include <time.h>
33 #include <stdbool.h>
34 #include <string.h>
35 #define PCRE2_CODE_UNIT_WIDTH 8
36 #include <pcre2.h>
37
38 #include <unistd.h>
39
40 #include "attributes.h"
41 #include "hashsum.h"
42 #include "file.h"
43 #include "url.h"
44 #include "commandconf.h"
45 #include "report.h"
46 #include "db_config.h"
47 #include "db_disk.h"
48 #include "db.h"
49 #include "log.h"
50 #include "progress.h"
51 #include "seltree.h"
52 #include "errorcodes.h"
53 #include "gen_list.h"
54 #include "getopt.h"
55 #include "util.h"
56 /*for locale support*/
57 #include "locale-aide.h"
58 /*for locale support*/
59 db_config* conf;
60 char* before = NULL;
61 char* after = NULL;
62
63 #ifndef MAXHOSTNAMELEN
64 #define MAXHOSTNAMELEN 256
65 #endif
66
67 static void usage(int exitvalue)
68 {
69 fprintf(stdout,
70 _("AIDE %s \n\n"
71 "Usage: aide [options] command\n\n"
72 "Commands:\n"
73 " -i, --init\t\tInitialize the database\n"
74 " -n, --dry-init\tTraverse the file system and match each file against rule tree\n"
75 " -C, --check\t\tCheck the database\n"
76 " -u, --update\t\tCheck and update the database non-interactively\n"
77 " -E, --compare\t\tCompare two databases\n"
78 " --list\t\tList the entries of the database in human readable format\n"
79 "\nMiscellaneous:\n"
80 " -D,\t\t\t--config-check\t\t\tTest the configuration file\n"
81 " -p FILE_TYPE:PATH\t--path-check=FILE_TYPE:PATH\tMatch file type and path against rule tree\n"
82 " -v,\t\t\t--version\t\t\tShow version of AIDE and compilation options\n"
83 " -h,\t\t\t--help\t\t\t\tShow this help message\n\n"
84 "Options:\n"
85 " -c CFGFILE\t--config=CFGFILE\tGet config options from CFGFILE\n"
86 " -l REGEX\t--limit=REGEX\t\tLimit command to entries matching REGEX\n"
87 " -B \"OPTION\"\t--before=\"OPTION\"\tBefore configuration file is read define OPTION\n"
88 " -A \"OPTION\"\t--after=\"OPTION\"\tAfter configuration file is read define OPTION\n"
89 " -L LEVEL\t--log-level=LEVEL\tSet log message level to LEVEL\n"
90 " -W WORKERS\t--workers=WORKERS\tNumber of simultaneous workers (threads) for file attribute processing (i.a. hashsum calculation)\n"
91 " \t\t--no-progress\t\tTurn progress off explicitly\n"
92 " \t\t--no-color\t\tTUrn color off explicitly\n"
93 ), conf->aide_version
94 );
95
96 exit(exitvalue);
97 }
98
99 static void sig_handler(int);
100
101 static void init_db_sighandler(void)
102 {
103 log_msg(LOG_LEVEL_DEBUG, "initialize signal handler for SIGINT");
104 signal(SIGINT,sig_handler);
105 log_msg(LOG_LEVEL_DEBUG, "initialize signal handler for SIGTERM");
106 signal(SIGTERM,sig_handler);
107 log_msg(LOG_LEVEL_DEBUG, "initialize signal handler for SIGHUP");
108 signal(SIGHUP,sig_handler);
109 }
110
111 static void sig_handler(int signum)
112 {
113 struct winsize winsize;
114 char *str;
115 switch(signum){
116 case SIGINT :
117 case SIGTERM :
118 case SIGHUP :
119 str = "\naide: received SIGINT, SIGTERM or SIGHUP. cleaning up...\n";
120 (void) !write(STDERR_FILENO ,str, strlen(str));
121 if (conf && (conf->database_out.fp != NULL
122 #ifdef WITH_ZLIB
123 || conf->database_out.gzp != NULL
124 #endif
125 )) {
126 if (conf->database_out.url->type == url_file && conf->database_out.flags&DB_FLAG_CREATED) {
127 str = "remove database_out: file:";
128 (void) !write(STDERR_FILENO, str, strlen(str));
129 (void) !write(STDERR_FILENO, conf->database_out.url->value, strlen(conf->database_out.url->value));
130 str = "\n";
131 (void) !write(STDERR_FILENO ,str, strlen(str));
132 unlink(conf->database_out.url->value);
133 }
134 }
135 _exit(SIGNAL_INTERRUPT_ERROR);
136 break;
137 case SIGUSR1 :
138 str = "\naide: received SIGUSR1, toggle debug level\n";
139 (void) !write(STDERR_FILENO ,str, strlen(str));
140 toogle_log_level(LOG_LEVEL_DEBUG);
141 break;
142 case SIGWINCH :
143 if(ioctl(STDERR_FILENO, TIOCGWINSZ, &winsize) == -1) {
144 conf->progress = 80;
145 } else {
146 conf->progress = winsize.ws_col;
147 }
148 break;
149 }
150 }
151
152 #define EXTRA_ATTR(attribute) fprintf(stdout, "%s: %s\n", attributes[attribute].config_name, extra_attributes&ATTR(attribute)?"yes":"no");
153
154 static void print_version(void)
155 {
156 fprintf(stdout, "AIDE %s\n\n", conf->aide_version );
157 fprintf(stdout, "Compile-time options:\n%s\n", AIDECOMPILEOPTIONS);
158 fprintf(stdout, "Default config values:\n");
159 fprintf(stdout, "config file: %s\n", conf->config_file?conf->config_file:"<none>");
160 #ifdef DEFAULT_DB
161 fprintf(stdout, "database_in: %s\n", DEFAULT_DB);
162 #else
163 fprintf(stdout, "database_in: <none>\n");
164 #endif
165 #ifdef DEFAULT_DB_OUT
166 fprintf(stdout, "database_out: %s\n", DEFAULT_DB_OUT),
167 #else
168 fprintf(stdout, "database_out: <none>\n"),
169 #endif
170
171 fprintf(stdout, "\nAvailable compiled-in attributes:\n");
172 DB_ATTR_TYPE extra_attributes = get_groupval("X");
173 EXTRA_ATTR(attr_acl)
174 EXTRA_ATTR(attr_xattrs)
175 EXTRA_ATTR(attr_selinux)
176 EXTRA_ATTR(attr_e2fsattrs)
177 EXTRA_ATTR(attr_capabilities)
178
179 fprintf(stdout, "\nAvailable hashsum attributes:\n");
180 DB_ATTR_TYPE available_hashsums = get_hashes(false);
181 for (int i = 0; i < num_hashes; ++i) {
182 fprintf(stdout, "%s: %s\n", attributes[hashsums[i].attribute].config_name, ATTR(hashsums[i].attribute)&available_hashsums?"yes":"no");
183 }
184
185 #ifdef HAVE_FSTYPE
186 fprintf(stdout, "\nAvailable file system type names:\n");
187 for (int i = 0 ; i < num_filesystems; ++i) {
188 fprintf(stdout, "%-20s%s", filesystems[i].str, i%4 == 3 || i+1 == num_filesystems?"\n":"");
189 }
190 #endif
191
192 fprintf(stdout, "\nDefault compound groups:\n");
193 char* predefined_groups[] = { "R", "L", ">", "H", "X" };
194 for (unsigned long i = 0 ; i < sizeof(predefined_groups)/sizeof(char*); ++i) {
195 char* str;
196 fprintf(stdout, "%s: %s\n", predefined_groups[i], str = diff_attributes(0, get_groupval(predefined_groups[i])));
197 free(str);
198 }
199
200 exit(0);
201 }
202
203 static char *append_line_to_config(char *config, char *line) {
204 size_t line_length = strlen(line);
205 if (config == NULL) {
206 int len = (line_length + 2)*sizeof(char);
207 config = checked_malloc(len);
208 snprintf(config, len, "%s\n", line);
209 } else {
210 int len = (strlen(config) + line_length + 2) * sizeof(char);
211 char *tmp = checked_malloc(len);
212 snprintf(tmp, len, "%s%s\n", config, line);
213 free(config);
214 config=tmp;
215 }
216 return config;
217 }
218
219 #define INVALID_ARGUMENT(option, format, ...) \
220 fprintf(stderr, "%s: (%s): " #format "\n", argv[0], option, __VA_ARGS__); \
221 exit(INVALID_ARGUMENT_ERROR);
222
223 #define ACTION_CASE(longopt, option, _action, desc) \
224 case option: { \
225 if(conf->action==0){ \
226 conf->action=_action; \
227 log_msg(LOG_LEVEL_INFO,"(%s): %s command", longopt, desc); \
228 } else { \
229 INVALID_ARGUMENT(longopt, %s, "cannot have multiple commands on a single commandline") \
230 } \
231 break; \
232 }
233
234 static void read_param(int argc,char**argv)
235 {
236 int i=0;
237
238 enum cmdline_args {
239 ARG_NO_PROGRESS = 1,
240 ARG_LIST = 2,
241 ARG_NO_COLOR = 3,
242 };
243
244 static struct option options[] =
245 {
246 { "help", no_argument, NULL, 'h' },
247 { "version", no_argument, NULL, 'v'},
248 { "config", required_argument, NULL, 'c'},
249 { "before", required_argument, NULL, 'B'},
250 { "after", required_argument, NULL, 'A'},
251 { "init", no_argument, NULL, 'i'},
252 { "dry-init", no_argument, NULL, 'n'},
253 { "check", no_argument, NULL, 'C'},
254 { "update", no_argument, NULL, 'u'},
255 { "config-check", no_argument, NULL, 'D'},
256 { "path-check", required_argument, NULL, 'p'},
257 { "limit", required_argument, NULL, 'l'},
258 { "log-level", required_argument, NULL, 'L'},
259 { "workers", required_argument, NULL, 'W'},
260 { "no-progress", no_argument, NULL, ARG_NO_PROGRESS},
261 { "no-color", no_argument, NULL, ARG_NO_COLOR},
262 { "compare", no_argument, NULL, 'E'},
263 { "list", no_argument, NULL, ARG_LIST},
264 { NULL,0,NULL,0 }
265 };
266
267 while(1){
268 int option = getopt_long(argc, argv, "hL:V::vc:l:p:B:A:W:riCuDEn", options, &i);
269 if(option==-1)
270 break;
271 switch(option)
272 {
273 case 'h':{
274 usage(0);
275 break;
276 }
277 case 'v':{
278 print_version();
279 break;
280 }
281 case 'c':{
282 conf->config_file=optarg;
283 log_msg(LOG_LEVEL_INFO,_("(--config): set config file to '%s'"), conf->config_file);
284 break;
285 }
286 case 'B': {
287 before = append_line_to_config(before, optarg);
288 log_msg(LOG_LEVEL_INFO,_("(--before): append '%s' to before config"), optarg);
289 break;
290 }
291 case 'A': {
292 after = append_line_to_config(after, optarg);
293 log_msg(LOG_LEVEL_INFO,_("(--after): append '%s' to after config"), optarg);
294 break;
295 }
296 case 'l': {
297 int pcre2_errorcode;
298 PCRE2_SIZE pcre2_erroffset;
299 int len = (strlen(optarg)+1) * sizeof(char);
300 conf->limit=checked_malloc(len);
301 strncpy(conf->limit, optarg, len);
302 if((conf->limit_crx=pcre2_compile((PCRE2_SPTR) conf->limit, PCRE2_ZERO_TERMINATED, PCRE2_UTF|PCRE2_ANCHORED, &pcre2_errorcode, &pcre2_erroffset, NULL)) == NULL) {
303 PCRE2_UCHAR pcre2_error[128];
304 pcre2_get_error_message(pcre2_errorcode, pcre2_error, 128);
305 char * limit_safe = stresc(conf->limit);
306 INVALID_ARGUMENT("--limit", error in regular expression '%s' at %zu: %s, limit_safe, pcre2_erroffset, pcre2_error)
307
308 }
309 conf->limit_md = pcre2_match_data_create_from_pattern(conf->limit_crx, NULL);
310 if (conf->limit_md == NULL) {
311 fprintf(stderr, "%s: (%s) pcre2_match_data_create_from_pattern: failed to allocate memory\n", argv[0], "--limit");
312 exit(MEMORY_ALLOCATION_FAILURE);
313 }
314
315 int pcre2_jit = pcre2_jit_compile(conf->limit_crx, PCRE2_JIT_PARTIAL_SOFT);
316 if (pcre2_jit < 0) {
317 PCRE2_UCHAR pcre2_error[128];
318 pcre2_get_error_message(pcre2_jit, pcre2_error, 128);
319 log_msg(LOG_LEVEL_DEBUG, "JIT compilation for limit '%s' failed: %s (fall back to interpreted matching)", conf->limit, pcre2_error);
320 } else {
321 log_msg(LOG_LEVEL_DEBUG, "JIT compilation for limit '%s' successful", conf->limit);
322 }
323
324 log_msg(LOG_LEVEL_INFO,_("(--limit): set limit to '%s'"), conf->limit);
325 break;
326 }
327 case 'L':{
328 LOG_LEVEL level = get_log_level_from_string(optarg);
329 if (level == LOG_LEVEL_UNSET) {
330 INVALID_ARGUMENT("--log-level", invalid log level '%s' (see man aide.conf for details), optarg)
331 } else {
332 set_log_level(level);
333 log_msg(LOG_LEVEL_INFO,"(--log-level): set log level to '%s'", optarg);
334 }
335 break;
336 }
337 case 'W':{
338 long num_workers = do_num_workers(optarg);
339 if (num_workers < 0) {
340 INVALID_ARGUMENT("--workers", invalid number of workers '%s', optarg)
341 }
342 conf->num_workers = num_workers;
343 log_msg(LOG_LEVEL_INFO,"(--workers): set number of workers to %ld (argument value: '%s')", conf->num_workers, optarg);
344 break;
345 }
346 case ARG_NO_PROGRESS:{
347 conf->progress = -1;
348 log_msg(LOG_LEVEL_INFO,"(--no-progress): disable progress bar");
349 break;
350 }
351 case ARG_NO_COLOR:{
352 conf->no_color = false;
353 log_msg(LOG_LEVEL_INFO,"(--no-color): disable colored log output");
354 break;
355 }
356 case 'p':{
357 if(conf->action==0){
358 conf->action=DO_DRY_RUN;
359 log_msg(LOG_LEVEL_INFO,"(--path-check): path check command");
360 if (optarg && *optarg) {
361 conf->check_file.type = get_f_type_from_char(*optarg);
362 if (conf->check_file.type == FT_NULL) {
363 INVALID_ARGUMENT("--path-check", invalid file type '%c' (see man aide for details), *optarg)
364 } else {
365 char * tmp = optarg+1;
366 #ifdef HAVE_FSTYPE
367 if (optarg[1] == '=') {
368 char* colon = strchr(optarg+2, ':');
369 if (colon) {
370 char* fs_type_str = NULL;
371 fs_type_str = checked_strndup(optarg+2, colon-(optarg+2));
372 if (*fs_type_str) {
373 conf->check_file.fs_type = get_fs_type_from_string(fs_type_str);
374 if (conf->check_file.fs_type == 0) {
375 INVALID_ARGUMENT("--path-check", invalid file system type '%s' (see man aide for details), fs_type_str)
376 }
377 free(fs_type_str);
378 tmp = colon;
379 } else {
380 INVALID_ARGUMENT("--path-check", %s, "missing file system type (see man aide for details)")
381 }
382 } else {
383 INVALID_ARGUMENT("--path-check", %s, "missing ':' (see man aide for details)")
384 }
385 }
386 #endif
387 if (*tmp == ':') {
388 char* path = tmp+1;
389 if (*path) {
390 if (*path == '/') {
391 conf->check_file.name = checked_strdup(path);
392 #ifdef HAVE_FSTYPE
393 char* fs_type_str = get_fs_type_string_from_magic(conf->check_file.fs_type);
394 log_msg(LOG_LEVEL_INFO,"(--path-check): set path to '%s' (f_type: %c, fs_type: '%s')", path, get_f_type_char_from_f_type(conf->check_file.type), fs_type_str);
395 free(fs_type_str);
396 #else
397 log_msg(LOG_LEVEL_INFO,"(--path-check): set path to '%s' (filetype: %c)", path , get_f_type_char_from_f_type(conf->check_file.type));
398 #endif
399 } else {
400 INVALID_ARGUMENT("--path-check", '%s' needs to be an absolute path, path)
401 }
402 } else {
403 INVALID_ARGUMENT("--path-check", %s, "missing path")
404 }
405 } else {
406 INVALID_ARGUMENT("--path-check", %s, "missing ':' (see man aide for details)")
407 }
408 }
409 } else {
410 INVALID_ARGUMENT("--path-check", %s, "missing file type (see man aide for details)")
411 }
412 } else {
413 INVALID_ARGUMENT("--path-check", %s, "cannot have multiple commands on a single commandline")
414 }
415 break;
416 }
417 ACTION_CASE("--init", 'i', DO_INIT, "database init")
418 ACTION_CASE("--dry-init", 'n', DO_INIT|DO_DRY_RUN, "dry init")
419 ACTION_CASE("--check", 'C', DO_COMPARE, "database check")
420 ACTION_CASE("--update", 'u', DO_INIT|DO_COMPARE, "database update")
421 ACTION_CASE("--compare", 'E', DO_DIFF, "database compare")
422 ACTION_CASE("--config-check", 'D', DO_DRY_RUN, "config check")
423 ACTION_CASE("--list", ARG_LIST, DO_LIST, "list")
424 default: /* '?' */
425 exit(INVALID_ARGUMENT_ERROR);
426 }
427 }
428
429 if(optind<argc){
430 fprintf(stderr, "%s: extra parameter: '%s'\n", argv[0], argv[optind]);
431 exit(INVALID_ARGUMENT_ERROR);
432 }
433 }
434
435 static void setdefaults_before_config(void)
436 {
437 DB_ATTR_TYPE X;
438
439 conf=(db_config*)checked_malloc(sizeof(db_config));
440 conf->defsyms=NULL;
441
442 /* Setting some defaults */
443
444 log_msg(LOG_LEVEL_INFO, "initialize rule tree");
445 conf->tree=init_tree();
446 conf->database_add_metadata=1;
447 conf->report_detailed_init=0;
448 conf->report_base16=0;
449 conf->report_quiet=0;
450 conf->report_append=false;
451 conf->report_ignore_added_attrs = 0;
452 conf->report_ignore_removed_attrs = 0;
453 conf->report_ignore_changed_attrs = 0;
454 conf->report_force_attrs = 0;
455 #ifdef WITH_E2FSATTRS
456 conf->report_ignore_e2fsattrs = 0UL;
457 #endif
458
459 conf->check_file.name = NULL;
460 conf->check_file.type = FT_NULL;
461 #ifdef HAVE_FSTYPE
462 conf->check_file.fs_type = 0;
463 #endif
464
465 conf->report_urls=NULL;
466 conf->report_level=default_report_options.level;
467 conf->report_format=default_report_options.format;
468
469 conf->config_file=
470 #ifdef CONFIG_FILE
471 CONFIG_FILE
472 #else
473 NULL
474 #endif
475 ;
476 conf->config_version=NULL;
477 conf->aide_version = AIDEVERSION;
478 conf->config_check_warn_unrestricted_rules = false;
479
480 #ifdef WITH_ACL
481 conf->no_acl_on_symlinks=0; /* zero means don't do ACLs on symlinks */
482 #endif
483 conf->db_out_attrs = ATTR(attr_filename)|ATTR(attr_attr)|ATTR(attr_perm)|ATTR(attr_inode);
484
485 conf->symlinks_found=0;
486
487 conf->database_in.url = NULL;
488 conf->database_in.filename=NULL;
489 conf->database_in.linenumber=0;
490 conf->database_in.linebuf=NULL;
491 conf->database_in.fp=NULL;
492 #ifdef WITH_ZLIB
493 conf->database_in.gzp = NULL;
494 #endif
495 conf->database_in.lineno = 0;
496 conf->database_in.fields = NULL;
497 conf->database_in.num_fields = 0;
498 conf->database_in.mdc = NULL;
499 conf->database_in.db_line = NULL;
500 conf->database_in.flags = DB_FLAG_NONE;
501
502 conf->database_out.url = NULL;
503 conf->database_out.filename=NULL;
504 conf->database_out.linenumber=0;
505 conf->database_out.linebuf=NULL;
506 conf->database_out.fp=NULL;
507 #ifdef WITH_ZLIB
508 conf->database_out.gzp = NULL;
509 #endif
510 conf->database_out.lineno = 0;
511 conf->database_out.fields = NULL;
512 conf->database_out.num_fields = 0;
513 conf->database_out.mdc = NULL;
514 conf->database_out.db_line = NULL;
515 conf->database_out.flags = DB_FLAG_NONE;
516
517 conf->database_new.url = NULL;
518 conf->database_new.filename=NULL;
519 conf->database_new.linenumber=0;
520 conf->database_new.linebuf=NULL;
521 conf->database_new.fp=NULL;
522 #ifdef WITH_ZLIB
523 conf->database_new.gzp = NULL;
524 #endif
525 conf->database_new.lineno = 0;
526 conf->database_new.fields = NULL;
527 conf->database_new.num_fields = 0;
528 conf->database_new.mdc = NULL;
529 conf->database_new.db_line = NULL;
530 conf->database_new.flags = DB_FLAG_NONE;
531
532 #ifdef WITH_ZLIB
533 conf->gzip_dbout=0;
534 #endif
535
536 conf->action=0;
537
538 conf->num_workers = -1;
539
540 conf->warn_dead_symlinks=0;
541
542 conf->report_grouped=1;
543
544 conf->report_summarize_changes=1;
545
546 conf->root_prefix=NULL;
547 conf->root_prefix_length=0;
548
549 conf->limit=NULL;
550 conf->limit_crx=NULL;
551
552 conf->groupsyms=NULL;
553
554 conf->start_time=time(NULL);
555
556 conf->progress = 0;
557 conf->no_color = true;
558
559 conf->print_details_width = 80;
560
561 log_msg(LOG_LEVEL_INFO, "define default attribute definitions");
562
563 for (ATTRIBUTE i = 0 ; i < num_attrs ; ++i) {
564 if (attributes[i].config_name) {
565 do_groupdef(attributes[i].config_name, attributes[i].attr);
566 }
567 }
568
569 X=0LLU;
570 #ifdef WITH_ACL
571 X|=ATTR(attr_acl);
572 #endif
573 #ifdef WITH_SELINUX
574 X|=ATTR(attr_selinux);
575 #endif
576 #ifdef WITH_XATTR
577 X|=ATTR(attr_xattrs);
578 #endif
579 #ifdef WITH_E2FSATTRS
580 X|=ATTR(attr_e2fsattrs);
581 #endif
582 #ifdef WITH_CAPABILITIES
583 X|=ATTR(attr_capabilities);
584 #endif
585
586 DB_ATTR_TYPE common_attrs = ATTR(attr_perm)|ATTR(attr_ftype)|ATTR(attr_inode)|ATTR(attr_linkcount)|ATTR(attr_uid)|ATTR(attr_gid);
587
588 DB_ATTR_TYPE GROUP_R_HASHES=0LLU;
589
590 GROUP_R_HASHES=ATTR(attr_sha3_256);
591
592 log_msg(LOG_LEVEL_INFO, "define default groups definitions");
593 do_groupdef("R",common_attrs|ATTR(attr_size)|ATTR(attr_linkname)|ATTR(attr_mtime)|ATTR(attr_ctime)|GROUP_R_HASHES|X);
594 do_groupdef("L",common_attrs|ATTR(attr_linkname)|X);
595 do_groupdef(">",common_attrs|ATTR(attr_size)|ATTR(attr_growing)|ATTR(attr_linkname)|X);
596 do_groupdef("H",get_hashes(false)&~DEPRECATED_HASHES);
597 do_groupdef("X",X);
598 do_groupdef("E",0);
599
600 conf->db_attrs = get_groupval("H");
601
602 }
603
604 static void setdefaults_after_config(void)
605 {
606 int linenumber=1;
607
608 #ifdef DEFAULT_DB
609 if(conf->database_in.url==NULL){
610 do_dbdef(DB_TYPE_IN, DEFAULT_DB, linenumber++, "(default)", NULL);
611 }
612 #endif
613 #ifdef DEFAULT_DB_OUT
614 if(conf->database_out.url==NULL){
615 do_dbdef(DB_TYPE_OUT, DEFAULT_DB_OUT, linenumber++, "(default)", NULL);
616 }
617 #endif
618
619 if(conf->root_prefix==NULL){
620 do_rootprefix("" , linenumber++, "(default)", NULL);
621 }
622
623 if(conf->report_urls==NULL){
624 do_repurldef("stdout" , linenumber++, "(default)", NULL);
625 }
626
627 if(conf->action==0){
628 conf->action=DO_COMPARE;
629 }
630
631 if(conf->num_workers < 0) {
632 conf->num_workers = 1;
633 log_msg(LOG_LEVEL_CONFIG, "(default): set 'num_workers' option to %lu", conf->num_workers);
634 }
635
636 if (is_log_level_unset()) {
637 set_log_level(LOG_LEVEL_WARNING);
638 };
639 }
640
641 static void list_attribute(db_line* entry, ATTRIBUTE attribute) {
642 char **value = NULL;
643 int num, i, c;
644 int p = conf->print_details_width-5-MAX_WIDTH_DETAILS_STRING;
645
646 DB_ATTR_TYPE attr = ATTR(attribute);
647 const char* name = attributes[attribute].details_string;
648
649 num=get_attribute_values(attr, entry, &value, NULL);
650
651 i = 0;
652 while (i<num) {
653 char *ovalue = stresc(value[i]);
654 int olen = strlen(ovalue);
655 int k = 0;
656 while (olen-p*k >= 0) {
657 c = k*(p-1);
658 fprintf(stdout," %-*s%c %.*s\n", MAX_WIDTH_DETAILS_STRING, (i+k)?"":name, (i+k)?' ':':', p-1, olen-c>0?&ovalue[c]:"");
659 k++;
660 }
661 ++i;
662 free(ovalue);
663 }
664 for(i=0; i < num; ++i) { free(value[i]); value[i]=NULL; } free(value); value=NULL;
665 }
666
667 int main(int argc,char**argv)
668 {
669 int errorno=0;
670
671 #ifdef WITH_LOCALE
672 setlocale(LC_ALL,"");
673 bindtextdomain(PACKAGE,LOCALEDIR);
674 textdomain(PACKAGE);
675 #endif
676 umask(0177);
677
678 init_db_sighandler();
679
680 init_hashsum_lib();
681
682 setdefaults_before_config();
683
684 log_msg(LOG_LEVEL_INFO, "read command line parameters");
685 read_param(argc,argv);
686
687 int stderr_isatty = isatty(STDERR_FILENO);
688 if (stderr_isatty == 0) {
689 log_msg(LOG_LEVEL_DEBUG, "isatty() failed for 'STDERR_FILENO': %s", strerror(errno));
690 }
691
692 if (conf->no_color) {
693 if (stderr_isatty) {
694 log_msg(LOG_LEVEL_DEBUG, "enable colored log output (stderr refers to a terminal)");
695 } else {
696 conf->no_color = false;
697 log_msg(LOG_LEVEL_INFO, "disable colored log output (stderr does not refer to a terminal)");
698 }
699 }
700 set_colored_log(conf->no_color);
701
702 if (!(conf->action&DO_DRY_RUN) && !(conf->action&DO_LIST)) {
703 if (conf->progress >= 0) {
704 if (stderr_isatty) {
705 log_msg(LOG_LEVEL_DEBUG, "enable progress bar (stderr refers to a terminal)");
706 if (progress_start()) {
707 log_msg(LOG_LEVEL_DEBUG, "initialize signal handler for SIGWINCH");
708 signal(SIGWINCH,sig_handler);
709 }
710 } else {
711 log_msg(LOG_LEVEL_INFO, "disable progress bar (stderr does not refer to a terminal)");
712 }
713 }
714 }
715
716 /* get hostname */
717 conf->hostname = checked_malloc(sizeof(char) * MAXHOSTNAMELEN + 1);
718 if (gethostname(conf->hostname,MAXHOSTNAMELEN) == -1) {
719 log_msg(LOG_LEVEL_WARNING,"gethostname failed: %s", strerror(errno));
720 free(conf->hostname);
721 conf->hostname = NULL;
722 } else {
723 log_msg(LOG_LEVEL_DEBUG, "hostname: '%s'", conf->hostname);
724 }
725
726 log_msg(LOG_LEVEL_INFO, "parse configuration");
727 progress_status(PROGRESS_CONFIG, NULL);
728 errorno=parse_config(before, conf->config_file, after);
729 if (errorno==RETFAIL){
730 exit(INVALID_CONFIGURELINE_ERROR);
731 }
732 free (before);
733 free (after);
734
735 setdefaults_after_config();
736
737 log_msg(LOG_LEVEL_DEBUG, "initialize signal handler for SIGUSR1");
738 signal(SIGUSR1,sig_handler);
739
740 log_msg(LOG_LEVEL_CONFIG, "report_urls:");
741 log_report_urls(LOG_LEVEL_CONFIG);
742
743 log_msg(LOG_LEVEL_RULE, "rule tree:");
744 log_tree(LOG_LEVEL_RULE, conf->tree, 0);
745
746 if (conf->action&DO_INIT && is_tree_empty(conf->tree)) {
747 log_msg(LOG_LEVEL_WARNING, "rule tree is empty, no files will be added to the database");
748 }
749
750 if (conf->check_file.name) {
751 match_t path_match = check_rxtree(conf->check_file, conf->tree, "disk (path-check)", true, NULL);
752 print_match(conf->check_file, path_match);
753 switch (path_match.result) {
754 case RESULT_PARTIAL_LIMIT_MATCH:
755 case RESULT_NO_LIMIT_MATCH:
756 exit(2);
757 case RESULT_EQUAL_MATCH:
758 case RESULT_SELECTIVE_MATCH:
759 exit(0);
760 case RESULT_NON_RECURSIVE_NEGATIVE_MATCH:
761 case RESULT_RECURSIVE_NEGATIVE_MATCH:
762 case RESULT_NEGATIVE_PARENT_MATCH:
763 case RESULT_NO_RULE_MATCH:
764 case RESULT_PARTIAL_MATCH:
765 case RESULT_PART_LIMIT_AND_NO_RECURSE_MATCH:
766 exit(1);
767 }
768 }
769
770 /* Let's do some sanity checks for the config */
771 if (conf->action&(DO_DIFF|DO_COMPARE|DO_LIST) && !(conf->database_in.url)) {
772 log_msg(LOG_LEVEL_ERROR,_("missing 'database_in', config option is required"));
773 exit(INVALID_ARGUMENT_ERROR);
774 }
775 if (!(conf->action&DO_DRY_RUN) && conf->action&DO_INIT && !(conf->database_out.url)) {
776 log_msg(LOG_LEVEL_ERROR,_("missing 'database_out', config option is required"));
777 exit(INVALID_ARGUMENT_ERROR);
778 }
779 if(conf->database_in.url && conf->database_out.url && cmpurl(conf->database_in.url,conf->database_out.url)==RETOK){
780 log_msg(LOG_LEVEL_NOTICE, "input and output database URLs are the same: '%s'", (conf->database_in.url)->value);
781 if((conf->action&DO_INIT)&&(conf->action&DO_COMPARE)){
782 log_msg(LOG_LEVEL_ERROR,_("input and output database urls cannot be the same "
783 "when doing database update"));
784 exit(INVALID_ARGUMENT_ERROR);
785 }
786 };
787 if(conf->action&DO_DIFF) {
788 if(!(conf->database_new.url)||!(conf->database_in.url)) {
789 log_msg(LOG_LEVEL_ERROR,_("must have both input databases defined for database compare"));
790 exit(INVALID_ARGUMENT_ERROR);
791 }
792 if(conf->database_in.url && conf->database_new.url && cmpurl(conf->database_in.url,conf->database_new.url)==RETOK){
793 log_msg(LOG_LEVEL_WARNING,_("both input databases for database compare are the same"));
794 }
795 }
796
797 /* ensure size attribute is added to db_out_attrs if sizeg or growing attribute is set */
798 if (conf->db_out_attrs & ATTR(attr_sizeg) || conf->db_out_attrs & ATTR(attr_growing)) {
799 conf->db_out_attrs |=ATTR(attr_size);
800 }
801
802 if (conf->action&DO_INIT && conf->action&DO_DRY_RUN) {
803 log_msg(LOG_LEVEL_INFO, "scan file system (dry-run)");
804 db_scan_disk(true);
805 exit (0);
806 }
807
808 if ((conf->action&DO_LIST)) {
809 if(db_init(&(conf->database_in), true, false)==RETFAIL) {
810 exit(IO_ERROR);
811 }
812 log_msg(LOG_LEVEL_INFO, "list entries from database: %s", (conf->database_in.url)->raw);
813 db_entry_t entry;
814 while((entry = db_readline(&(conf->database_in), false)).line != NULL) {
815 log_msg(LOG_LEVEL_RULE, "\u252c process '%s' (filetype: %c)", (entry.line)->filename, get_f_type_char_from_perm((entry.line)->perm));
816 char *entry_safe = stresc((entry.line)->filename);
817 fprintf(stdout, "%s\n", entry_safe);
818 free(entry_safe);
819 for (int j=0; j < report_attrs_order_length; ++j) {
820 switch(report_attrs_order[j]) {
821 case attr_allhashsums:
822 for (int i = 0 ; i < num_hashes ; ++i) {
823 if (ATTR(hashsums[i].attribute)&(entry.line)->attr) { list_attribute((entry.line), hashsums[i].attribute); }
824 }
825 break;
826 case attr_size:
827 if (ATTR(attr_size)&(entry.line)->attr) { list_attribute((entry.line), attr_size); }
828 if (ATTR(attr_sizeg)&(entry.line)->attr) { list_attribute((entry.line), attr_sizeg); }
829 break;
830 default:
831 if (ATTR(report_attrs_order[j])&(entry.line)->attr) { list_attribute((entry.line), report_attrs_order[j]); }
832 break;
833 }
834 }
835 list_attribute((entry.line), attr_attr);
836 fprintf(stdout, "\n");
837 log_msg(LOG_LEVEL_RULE, "\u2534 finish processing '%s'", (entry.line)->filename);
838 }
839 db_close();
840 exit (0);
841 }
842
843 if (!(conf->action&DO_DRY_RUN)) {
844
845 if (!init_report_urls()) {
846 exit(INVALID_CONFIGURELINE_ERROR);
847 }
848
849 if (conf->action&(DO_INIT|DO_COMPARE) && conf->root_prefix_length > 0) {
850 DIR *dir;
851 if((dir = opendir(conf->root_prefix)) != NULL) {
852 closedir(dir);
853 } else {
854 log_msg(LOG_LEVEL_ERROR,"opendir() for root_prefix %s failed: %s", conf->root_prefix, strerror(errno));
855 exit(INVALID_CONFIGURELINE_ERROR);
856 }
857 }
858 if(conf->action&DO_INIT){
859 if(db_init(&(conf->database_out), false,
860 #ifdef WITH_ZLIB
861 conf->gzip_dbout
862 #else
863 false
864 #endif
865 ) == RETFAIL) {
866 exit(IO_ERROR);
867 }
868 if(db_writespec(conf)==RETFAIL){
869 log_msg(LOG_LEVEL_ERROR,_("Error while writing database. Exiting.."));
870 exit(IO_ERROR);
871 }
872 }
873 if((conf->action&DO_COMPARE)||(conf->action&DO_DIFF)){
874 if(db_init(&(conf->database_in), true, false)==RETFAIL)
875 exit(IO_ERROR);
876 }
877 if(conf->action&DO_DIFF){
878 if(db_init(&(conf->database_new), true, false)==RETFAIL)
879 exit(IO_ERROR);
880 }
881
882 populate_tree(conf->tree);
883
884 if(conf->action&DO_INIT) {
885 progress_status(PROGRESS_WRITEDB, NULL);
886 log_msg(LOG_LEVEL_INFO, "write new entries to database: %s", (conf->database_out.url)->raw);
887 write_tree(conf->tree);
888 }
889 progress_stop();
890
891 db_close();
892
893 conf->end_time=time(NULL);
894
895 log_msg(LOG_LEVEL_INFO, "generate reports");
896
897 int exitcode = gen_report(conf->tree);
898
899 log_msg(LOG_LEVEL_INFO, "exit AIDE with exit code '%d'", exitcode);
900
901 exit(exitcode);
902 }
903 return RETOK;
904 }
905 // vi: ts=8 sw=8