Debian Codegraph @

src/report.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 1999-2007, 2010-2013, 2015-2016, 2018-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 #include "aide.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28 #ifdef WITH_AUDIT
29 #include <libaudit.h>
30 #include <unistd.h>
31 #endif
32 #ifdef WITH_E2FSATTRS
33 #include "e2fsattrs.h"
34 #endif
35 #ifdef HAVE_SYSLOG
36 #include <syslog.h>
37 #endif
38 #include <stdarg.h>
39 #include <stdbool.h>
40 #include <stdio.h>
41 #include <sys/stat.h>
42 #include "hashsum.h"
43 #include "log.h"
44 #include "rx_rule.h"
45 #include "seltree_struct.h"
46 #ifdef HAVE_FSTYPE
47 #include "file.h"
48 #endif
49 #include "attributes.h"
50 #include "base64.h"
51 #include "conf_ast.h"
52 #include "db_config.h"
53 #include "list.h"
54 #include "url.h"
55 #include "db.h"
56 #include "db_line.h"
57 #include "be.h"
58 #include "util.h"
59 #include "report.h"
60 #include "report_plain.h"
61 #include "report_json.h"
62 /*for locale support*/
63 #include "locale-aide.h"
64 /*for locale support*/
65
66
67 /*************/
68 /* construction area for report lines */
69
70 #ifdef WITH_AUDIT
71 long nadd, nrem, nchg = 0;
72 #endif
73 int added_entries_reported, removed_entries_reported, changed_entries_reported = 0;
74
75
76 const ATTRIBUTE report_attrs_order[] = {
77 attr_ftype,
78 attr_linkname,
79 attr_size,
80 attr_bcount,
81 attr_perm,
82 attr_uid,
83 attr_gid,
84 attr_atime,
85 attr_mtime,
86 attr_ctime,
87 attr_inode,
88 attr_linkcount,
89 attr_allhashsums,
90 #ifdef WITH_ACL
91 attr_acl,
92 #endif
93 #ifdef WITH_XATTR
94 attr_xattrs,
95 #endif
96 #ifdef WITH_SELINUX
97 attr_selinux,
98 #endif
99 #ifdef WITH_E2FSATTRS
100 attr_e2fsattrs,
101 #endif
102 #ifdef WITH_CAPABILITIES
103 attr_capabilities,
104 #endif
105 #ifdef HAVE_FSTYPE
106 attr_fs_type,
107 #endif
108 };
109
110 int report_attrs_order_length = sizeof(report_attrs_order)/sizeof(ATTRIBUTE);
111
112 static DB_ATTR_TYPE get_attrs(ATTRIBUTE attr) {
113 switch(attr) {
114 case attr_allhashsums: return get_hashes(true);
115 case attr_size: return ATTR(attr_size)|ATTR(attr_sizeg);
116 default: return ATTR(attr);
117 }
118 }
119
120 report_options default_report_options = {
121 .format = REPORT_FORMAT_PLAIN,
122 .level = REPORT_LEVEL_CHANGED_ATTRIBUTES,
123 };
124
125 struct report_level {
126 REPORT_LEVEL report_level;
127 const char *name;
128 };
129
130 static struct report_level report_level_array[] = {
131 { REPORT_LEVEL_MINIMAL, "minimal" },
132 { REPORT_LEVEL_SUMMARY, "summary" },
133 { REPORT_LEVEL_DATABASE_ATTRIBUTES, "database_attributes" },
134 { REPORT_LEVEL_LIST_ENTRIES, "list_entries" },
135 { REPORT_LEVEL_CHANGED_ATTRIBUTES, "changed_attributes" },
136 { REPORT_LEVEL_ADDED_REMOVED_ATTRIBUTES, "added_removed_attributes" },
137 { REPORT_LEVEL_ADDED_REMOVED_ENTRIES, "added_removed_entries" },
138 { REPORT_LEVEL_UNKNOWN, NULL }
139 };
140
141 struct report_format {
142 REPORT_FORMAT report_format;
143 const char *name;
144 };
145
146 static struct report_format report_format_array[] = {
147 { REPORT_FORMAT_PLAIN, "plain" },
148 { REPORT_FORMAT_JSON, "json" },
149 { REPORT_FORMAT_UNKNOWN, NULL }
150 };
151
152 #ifdef WITH_XATTR
153 static size_t xstrnspn(const char *s1, size_t len, const char *srch)
154 {
155 const char *os1 = s1;
156
157 while (len-- && strchr(srch, *s1))
158 ++s1;
159
160 return (s1 - os1);
161 }
162
163 #define PRINTABLE_XATTR_VALS \
164 "0123456789" \
165 "abcdefghijklmnopqrstuvwxyz" \
166 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
167 ".-_:;,[]{}<>()!@#$%^&*|\\/?~"
168
169 static int xattrs2array(xattrs_type* xattrs, char* **values) {
170 int n = 0;
171 if (xattrs==NULL) { n=1; }
172 else { n=1+xattrs->num; }
173 *values = checked_malloc(n * sizeof(char*));
174 (*values)[0]=checked_malloc((6+floor(log10(n)))*sizeof(char));
175 snprintf((*values)[0], 6+floor(log10(n)), "num=%d", n-1);
176 if (n>1) {
177 size_t num = 0;
178 int width, length;
179 width = log10(xattrs->num); /* make them the same width */
180 while (num++ < xattrs->num) {
181 char *val = NULL;
182 size_t len = 0;
183 val = (char *)xattrs->ents[num - 1].val;
184 len = xstrnspn(val, xattrs->ents[num - 1].vsz, PRINTABLE_XATTR_VALS);
185 if ((len == xattrs->ents[num - 1].vsz) || ((len == (xattrs->ents[num - 1].vsz - 1)) && !val[len])) {
186 length = 8 + width + strlen(xattrs->ents[num - 1].key) + strlen(val);
187 (*values)[num]=checked_malloc(length *sizeof(char));
188 snprintf((*values)[num], length , "[%.*zd] %s = %s", width, num, xattrs->ents[num - 1].key, val);
189 } else {
190 val = encode_base64(xattrs->ents[num - 1].val, xattrs->ents[num - 1].vsz);
191 length = 10 + width + strlen(xattrs->ents[num - 1].key) + strlen(val);
192 (*values)[num]=checked_malloc( length *sizeof(char));
193 snprintf((*values)[num], length , "[%.*zd] %s <=> %s", width, num, xattrs->ents[num - 1].key, val);
194 free(val);
195 }
196 }
197 }
198 return n;
199 }
200 #endif
201
202 #ifdef WITH_ACL
203 static int acl2array(acl_type* acl, char* **values) {
204 int n = 0;
205 *values = NULL;
206 #ifdef WITH_POSIX_ACL
207 #define easy_posix_acl(x,y) \
208 if (acl->x) { \
209 i = k = 0; \
210 while (acl->x[i]) { \
211 if (acl->x[i]=='\n') { \
212 (*values)[j]=checked_malloc(4+(i-k)*sizeof(char)); \
213 snprintf((*values)[j], 4+(i-k), "%c: %s", y, &acl->x[k]); \
214 j++; \
215 k=i+1; \
216 } \
217 i++; \
218 } \
219 }
220 if (acl->acl_a || acl->acl_d) {
221 int j, k, i;
222 if (acl->acl_a) { i = 0; while (acl->acl_a[i]) { if (acl->acl_a[i++]=='\n') { n++; } } }
223 if (acl->acl_d) { i = 0; while (acl->acl_d[i]) { if (acl->acl_d[i++]=='\n') { n++; } } }
224 *values = checked_malloc(n * sizeof(char*));
225 j = 0;
226 easy_posix_acl(acl_a, 'A')
227 easy_posix_acl(acl_d, 'D')
228 }
229 #endif
230 return n;
231 }
232 #endif
233
234 char* get_file_type_string(mode_t mode) {
235 switch (mode & S_IFMT) {
236 case S_IFREG: return _("File");
237 case S_IFDIR: return _("Directory");
238 #ifdef S_IFIFO
239 case S_IFIFO: return _("FIFO");
240 #endif
241 case S_IFLNK: return _("Link");
242 case S_IFBLK: return _("Block device");
243 case S_IFCHR: return _("Character device");
244 #ifdef S_IFSOCK
245 case S_IFSOCK: return _("Socket");
246 #endif
247 #ifdef S_IFDOOR
248 case S_IFDOOR: return _("Door");
249 #endif
250 #ifdef S_IFPORT
251 case S_IFPORT: return _("Port");
252 #endif
253 default: return _("Unknown file type");
254 }
255 }
256
257 static int cmp_url(url_t* url1,url_t* url2){
258 return ((url1->type==url2->type)&&(strcmp(url1->value,url2->value)==0));
259 }
260
261 const char* get_report_level_string(REPORT_LEVEL report_level) {
262 return report_level_array[report_level-1].name;
263 }
264
265 REPORT_LEVEL get_report_level(char *str) {
266 struct report_level *level;
267
268 for (level = report_level_array; level->report_level != 0; level++) {
269 if (strcmp(str, level->name) == 0) {
270 return level->report_level;
271 }
272 }
273 return REPORT_LEVEL_UNKNOWN;
274 }
275
276 static const char* get_report_format_string(REPORT_FORMAT report_format) {
277 return report_format_array[report_format-1].name;
278 }
279
280 REPORT_FORMAT get_report_format(char *str) {
281 struct report_format *level;
282
283 for (level = report_format_array; level->report_format != 0; level++) {
284 if (strcmp(str, level->name) == 0) {
285 return level->report_format;
286 }
287 }
288 return REPORT_FORMAT_UNKNOWN;
289 }
290
291 static void report_vprintf(report_t*, const char *, va_list)
292 #ifdef __GNUC__
293 __attribute__ ((format (printf, 2, 0)))
294 #endif
295 ;
296
297 static void report_vprintf(report_t* r, const char *format, va_list ap) {
298 int retval;
299
300 if (!r->quiet || (r->nadd || r->nchg || r->nrem)) {
301 switch ((r->url)->type) {
302 #ifdef HAVE_SYSLOG
303 case url_syslog: {
304 #ifdef HAVE_VSYSLOG
305 vsyslog(AIDE_SYSLOG_PRIORITY,format,ap);
306 #else
307 char buf[1024];
308 vsnprintf(buf,1024,format,ap);
309 syslog(AIDE_SYSLOG_PRIORITY,"%s",buf);
310 #endif
311 break;
312 }
313 #endif
314 default : {
315 retval=vfprintf(r->fd, format, ap);
316 if(retval==0) {
317 log_msg(LOG_LEVEL_ERROR, "unable to write to '%s", (r->url)->value);
318 }
319 break;
320 }
321 }
322
323 }
324
325 }
326
327 void report_printf(report_t* r, const char* error_msg, ...) {
328 va_list ap;
329
330 va_start(ap, error_msg);
331 report_vprintf(r, error_msg, ap);
332 va_end(ap);
333
334 }
335
336 static int compare_report_t_by_report_level(const void *n1, const void *n2)
337 {
338 const report_t *x1 = n1;
339 const report_t *x2 = n2;
340 return x2->level - x1->level;
341 }
342
343 void log_report_urls(LOG_LEVEL log_level) {
344 list* l = NULL;
345
346 for (l=conf->report_urls; l; l=l->next) {
347 report_t* r = l->data;
348
349 log_msg(log_level, " %s%s%s (%p)", get_url_type_string((r->url)->type), (r->url)->value?":":"", (r->url)->value?(r->url)->value:"", (void*) r);
350
351 log_msg(log_level, " level: %s | format: %s | base16: %s | append: %s | quiet: %s | detailed_init: %s | summarize_changes: %s | grouped: %s", get_report_level_string(r->level), get_report_format_string(r->format), btoa(r->base16), btoa(r->append), btoa(r->quiet), btoa(r->detailed_init), btoa(r->summarize_changes), btoa(r->grouped));
352 char *str;
353 log_msg(log_level, " ignore_added_attrs: '%s'", str = diff_attributes(0, r->ignore_added_attrs));
354 free(str);
355 log_msg(log_level, " ignore_removed_attrs: '%s'", str = diff_attributes(0, r->ignore_removed_attrs));
356 free(str);
357 log_msg(log_level, " ignore_changed_attrs: '%s'", str = diff_attributes(0, r->ignore_changed_attrs));
358 free(str);
359 log_msg(log_level, " force_attrs: '%s'", str = diff_attributes(0, r->force_attrs));
360 free(str);
361 #ifdef WITH_E2FSATTRS
362 log_msg(log_level, " ignore_e2fsattrs: '%s'", str = get_e2fsattrs_string(r->ignore_e2fsattrs, true, 0));
363 free(str);
364 #endif
365 }
366 }
367
368 void print_report_config_options(report_t *report, void (*print_config_option)(report_t *, config_option, const char*)) {
369 if(conf->config_version) {
370 print_config_option(report, CONFIG_VERSION, conf->config_version);
371 }
372 if (conf->limit != NULL) {
373 print_config_option(report, LIMIT_CMDLINE_OPTION, conf->limit);
374 }
375 if (conf->action&(DO_INIT|DO_COMPARE) && conf->root_prefix_length > 0) {
376 print_config_option(report, ROOT_PREFIX_OPTION, conf->root_prefix);
377 }
378 if (report->level != default_report_options.level) {
379 print_config_option(report, REPORT_LEVEL_OPTION, get_report_level_string(report->level));
380 }
381 }
382
383 void print_report_report_options(report_t *report, void (*print_config_option)(report_t *, config_option, const char*)) {
384 char *str;
385 if (report->ignore_added_attrs) {
386 print_config_option(report, REPORT_IGNORE_ADDED_ATTRS_OPTION, str = diff_attributes(0, report->ignore_added_attrs));
387 free(str);
388 }
389 if (report->ignore_removed_attrs) {
390 print_config_option(report, REPORT_IGNORE_REMOVED_ATTRS_OPTION, str = diff_attributes(0, report->ignore_removed_attrs));
391 free(str);
392 }
393 if (report->ignore_changed_attrs) {
394 print_config_option(report, REPORT_IGNORE_CHANGED_ATTRS_OPTION, str = diff_attributes(0, report->ignore_changed_attrs));
395 free(str);
396 }
397 if (report->force_attrs && report->level >= REPORT_LEVEL_CHANGED_ATTRIBUTES) {
398 print_config_option(report, REPORT_FORCE_ATTRS_OPTION, str = diff_attributes(0, report->force_attrs));
399 free(str);
400 }
401 #ifdef WITH_E2FSATTRS
402 if (report->ignore_e2fsattrs) {
403 print_config_option(report, REPORT_IGNORE_E2FSATTRS_OPTION, str = get_e2fsattrs_string(report->ignore_e2fsattrs, true, 0) );
404 free(str);
405 }
406 #endif
407
408 }
409
410 char* get_summary_string(report_t* report) {
411 if(conf->action&(DO_COMPARE|DO_DIFF)) {
412 if (report->nadd||report->nrem||report->nchg) {
413 if (conf->action&DO_COMPARE) {
414 return _("AIDE found differences between database and filesystem!!");
415 } else {
416 return _("AIDE found differences between the two databases!!");
417 }
418 } else {
419 if (conf->action&DO_COMPARE) {
420 return _("AIDE found NO differences between database and filesystem. Looks okay!!");
421 } else {
422 return _("AIDE found NO differences between the two databases. Looks okay!!");
423 }
424 }
425 } else {
426 return _("AIDE successfully initialized database.");
427 }
428 }
429
430 bool add_report_url(url_t* url, int linenumber, char* filename, char* linebuf) {
431 list* report_urls=NULL;
432
433 if(url==NULL) {
434 return false;
435 } else if (url->type==url_stdin) {
436 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "unsupported report URL-type: '%s'", get_url_type_string(url->type))
437 return false;
438 }
439
440 for(report_urls=conf->report_urls; report_urls ; report_urls=report_urls->next) {
441 if (cmp_url((url_t*) report_urls->data, url)) {
442 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_WARNING, "report_url '%s' already defined (ignoring) ",url->value)
443 return true;
444 }
445 }
446
447
448 report_t* r = checked_malloc(sizeof(report_t));
449 r->url = url;
450 r->fd = NULL;
451 r->level = conf->report_level;
452 r->format = conf->report_format;
453
454 r->detailed_init = conf->report_detailed_init;
455 r->base16 = conf->report_base16;
456 r->quiet = conf->report_quiet;
457 r->append = conf->report_append;
458 r->summarize_changes = conf->report_summarize_changes;
459 r->grouped = conf->report_grouped;
460
461 r->ignore_added_attrs = conf->report_ignore_added_attrs;
462 r->ignore_removed_attrs = conf->report_ignore_removed_attrs;
463 r->ignore_changed_attrs = conf->report_ignore_changed_attrs;
464 r->force_attrs = conf->report_force_attrs;
465
466 r->ntotal = 0;
467 r->nadd = 0;
468 r->nrem = 0;
469 r->nchg = 0;
470
471 r->diff_attrs_entries = NULL;
472 r->num_diff_attrs_entries = 0;
473
474 r->linenumber = linenumber;
475 r->filename = filename;
476 r->linebuf = linebuf?checked_strdup(linebuf):NULL;
477
478 #ifdef WITH_E2FSATTRS
479 r->ignore_e2fsattrs = conf->report_ignore_e2fsattrs;
480 #endif
481
482 log_msg(LOG_LEVEL_DEBUG, _("add report_url (%p): url(: %s, level: %d"), (void*) r, (r->url)->raw, r->level);
483 conf->report_urls=list_sorted_insert(conf->report_urls, (void*) r, compare_report_t_by_report_level);
484 LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_CONFIG, "set 'report_url' to '%s'", url->raw)
485 return true;
486
487 }
488
489 bool init_report_urls(void) {
490 list* l = NULL;
491
492 for (l=conf->report_urls; l; l=l->next) {
493 report_t* r = l->data;
494 switch (r->url->type) {
495 #ifdef HAVE_SYSLOG
496 int sfac;
497 case url_syslog: {
498 sfac=syslog_facility_lookup(r->url->value);
499 openlog(AIDE_IDENT,AIDE_LOGOPT, sfac);
500 break;
501 }
502 #endif
503 default : {
504 r->fd=be_init(r->url, false, false, r->append, r->linenumber, r->filename, r->linebuf, NULL);
505 if(r->fd==NULL) {
506 return false;
507 }
508 break;
509 }
510 }
511
512 }
513 return true;
514 }
515
516 static int attributes2array(DB_ATTR_TYPE attrs, char* **values) {
517 *values = NULL;
518
519 int n = 0;
520 for (ATTRIBUTE i = 0; i < num_attrs; ++i) {
521 if (attributes[i].db_name && (1LLU<<i)&attrs) {
522 n++;
523 }
524 }
525
526 *values = checked_malloc(n * sizeof(char*));
527 n = 0;
528 for (ATTRIBUTE i = 0; i < num_attrs; ++i) {
529 if (attributes[i].db_name && (1LLU<<i)&attrs) {
530 size_t len = strlen(attributes[i].db_name)+1;
531 (*values)[n] = checked_malloc(len * sizeof(char));
532 snprintf((*values)[n], len, "%s", attributes[i].db_name);
533 n++;
534 }
535 }
536 return n;
537 }
538
539 int get_attribute_values(DB_ATTR_TYPE attr, db_line* line,
540 char* **values, report_t *r) {
541
542 #define easy_string(s) \
543 l = strlen(s)+1; \
544 *values[0] = checked_malloc(l * sizeof (char)); \
545 snprintf(*values[0], l, "%s",s);
546
547 #define easy_number(a,b,c) \
548 } else if (a&attr) { \
549 l = 2+floor(line->b?log10(line->b):0); \
550 *values[0] = checked_malloc(l * sizeof (char)); \
551 snprintf(*values[0], l, c,line->b);
552
553 #define easy_time(a,b) \
554 } else if (a&attr) { \
555 *values[0] = get_time_string(&(line->b)); \
556
557 if (line!=NULL && attr&get_hashes(true)) {
558 for (int i = 0 ; i < num_hashes ; ++i) {
559 if (ATTR(hashsums[i].attribute)&attr) {
560 if (line->hashsums[i]) {
561 *values = checked_malloc(1 * sizeof (char*));
562 if (r==NULL || r->base16) {
563 *values[0] = byte_to_base16(line->hashsums[i], hashsums[i].length);
564 } else {
565 *values[0] = encode_base64(line->hashsums[i], hashsums[i].length);
566 }
567 return 1;
568 } else {
569 return 0;
570 }
571 }
572 }
573 }
574
575 if (ATTR(attr_attr)&attr) {
576 return attributes2array(line->attr, values);
577 } else
578 if (line==NULL || !(line->attr&attr)) {
579 *values = NULL;
580 return 0;
581 #ifdef WITH_ACL
582 } else if (ATTR(attr_acl)&attr) {
583 return acl2array(line->acl, values);
584 #endif
585 #ifdef WITH_XATTR
586 } else if (ATTR(attr_xattrs)&attr) {
587 return xattrs2array(line->xattrs, values);
588 #endif
589 } else {
590 int l;
591 *values = checked_malloc(1 * sizeof (char*));
592 if (ATTR(attr_ftype)&attr) {
593 easy_string(get_file_type_string(line->perm))
594 #ifdef HAVE_FSTYPE
595 } else if (ATTR(attr_fs_type)&attr) {
596 *values[0] = get_fs_type_string_from_magic(line->fs_type);
597 #endif
598 } else if (ATTR(attr_linkname)&attr) {
599 easy_string(line->linkname)
600 easy_number((ATTR(attr_size)|ATTR(attr_sizeg)),size,"%lli")
601 } else if (ATTR(attr_perm)&attr) {
602 *values[0] = perm_to_char(line->perm);
603 easy_time(ATTR(attr_atime),atime)
604 easy_time(ATTR(attr_mtime),mtime)
605 easy_time(ATTR(attr_ctime),ctime)
606 easy_number(ATTR(attr_bcount),bcount,"%lli")
607 easy_number(ATTR(attr_uid),uid,"%li")
608 easy_number(ATTR(attr_gid),gid,"%li")
609 easy_number(ATTR(attr_inode),inode,"%li")
610 easy_number(ATTR(attr_linkcount),nlink,"%li")
611 #ifdef WITH_SELINUX
612 } else if (ATTR(attr_selinux)&attr) {
613 easy_string(line->cntx)
614 #endif
615 #ifdef WITH_E2FSATTRS
616 } else if (ATTR(attr_e2fsattrs)&attr) {
617 *values[0]=get_e2fsattrs_string(line->e2fsattrs, false, r?r->ignore_e2fsattrs:0L);
618 #endif
619 #ifdef WITH_CAPABILITIES
620 } else if (ATTR(attr_capabilities)&attr) {
621 easy_string(line->capabilities)
622 #endif
623 } else {
624 easy_string("unknown attribute")
625 }
626 return 1;
627 }
628 }
629
630 char* get_summarize_changes_string(report_t* report, seltree* node) {
631 int i;
632 char* summary = checked_malloc ((report_attrs_order_length+1) * sizeof (char));
633 if (node->checked&(NODE_ADDED|NODE_REMOVED)) {
634 summary[0]=get_f_type_char_from_perm(((node->checked&NODE_REMOVED)?node->old_data:node->new_data)->perm);
635 for(i=1;i<report_attrs_order_length;i++){
636 summary[i]=(node->checked&NODE_ADDED)?'+':'-';
637 }
638 } else if (node->checked&NODE_CHANGED) {
639 for(i=0;i<report_attrs_order_length;i++) {
640 char c, u, a, d, g, s;
641 DB_ATTR_TYPE attrs = get_attrs(report_attrs_order[i]);
642 c = attributes[report_attrs_order[i]].summary_char;
643 d = '-'; a = '+'; g = ':'; u = '.'; s = ' ';
644 switch (i) {
645 case 0:
646 summary[i]=get_f_type_char_from_perm((node->new_data)->perm);
647 continue;
648 case 2:
649 if (attrs&(node->changed_attrs&(~(report->ignore_removed_attrs))) && (node->old_data)->size > (node->new_data)->size) {
650 c = '<';
651 }
652 u = '=';
653 break;
654 }
655 if (attrs&node->changed_attrs&(report->force_attrs|(~(report->ignore_changed_attrs)))) {
656 summary[i]=c;
657 } else if (attrs&((node->old_data)->attr&~((node->new_data)->attr)&(report->force_attrs|~(report->ignore_removed_attrs)))) {
658 summary[i]=d;
659 } else if (attrs&~((node->old_data)->attr)&(node->new_data)->attr&(report->force_attrs|~(report->ignore_added_attrs))) {
660 summary[i]=a;
661 } else if (attrs& (
662 (((node->old_data)->attr&~((node->new_data)->attr)&report->ignore_removed_attrs))|
663 (~((node->old_data)->attr)&(node->new_data)->attr&report->ignore_added_attrs)|
664 (((node->old_data)->attr&(node->new_data)->attr)&report->ignore_changed_attrs)
665 ) ) {
666 summary[i]=g;
667 } else if (attrs&((node->old_data)->attr&(node->new_data)->attr)) {
668 summary[i]=u;
669 } else {
670 summary[i]=s;
671 }
672 }
673 }
674 summary[report_attrs_order_length]='\0';
675 return summary;
676 }
677
678
679
680 static DB_ATTR_TYPE get_report_attributes(seltree* node, report_t *report) {
681 db_line* oline = node->old_data;
682 db_line* nline = node->new_data;
683 DB_ATTR_TYPE attrs = node->changed_attrs;
684
685 DB_ATTR_TYPE report_attrs, changed_attrs;
686
687 changed_attrs = ~(report->ignore_changed_attrs)&(attrs
688 #ifdef WITH_E2FSATTRS
689 & (~ATTR(attr_e2fsattrs) | ( (attrs&ATTR(attr_e2fsattrs) && oline != NULL && nline != NULL && ~(report->ignore_e2fsattrs)&(oline->e2fsattrs^nline->e2fsattrs)) ? ATTR(attr_e2fsattrs) : 0 ) )
690 #endif
691 );
692
693 report_attrs = 0LL;
694 report_attrs |= changed_attrs;
695
696 if (report->level >= REPORT_LEVEL_ADDED_REMOVED_ATTRIBUTES && oline&&nline) {
697 report_attrs |= ~(oline->attr)&nline->attr&~(report->ignore_added_attrs); /* added attributes */
698 report_attrs |= oline->attr&~(nline->attr)&~(report->ignore_removed_attrs); /* removed attributes */
699 }
700 report_attrs |= report_attrs?report->force_attrs:0; /* forced attributes */
701
702 return report_attrs;
703 }
704
705 static void terse_report(seltree* node) {
706 list* l = NULL;
707
708 pthread_rwlock_rdlock(&node->rwlock);
709 for (l=conf->report_urls; l; l=l->next) {
710 report_t* r = l->data;
711
712 if ((node->checked&(DB_OLD|DB_NEW)) != 0) {
713 r->ntotal += ((node->checked&DB_NEW) != 0);
714 if (!(node->checked&DB_OLD)){
715 /* File is in new db but not old. (ADDED) */
716 /* unless it was moved in */
717 if ( (conf->action&DO_INIT && r->detailed_init) || (conf->action&(DO_COMPARE|DO_DIFF) && !((node->checked&NODE_ALLOW_NEW)||(node->checked&NODE_MOVED_IN))) ) {
718 #ifdef WITH_AUDIT
719 nadd++;
720 #endif
721 r->nadd++;
722 node->checked|=NODE_ADDED;
723 }
724 } else if (!(node->checked&DB_NEW)){
725 /* File is in old db but not new. (REMOVED) */
726 /* unless it was moved out */
727 if (!((node->checked&NODE_ALLOW_RM)||(node->checked&NODE_MOVED_OUT))) {
728 #ifdef WITH_AUDIT
729 nrem++;
730 #endif
731 r->nrem++;
732 node->checked|=NODE_REMOVED;
733 }
734 } else if ((node->old_data!=NULL)&&(node->new_data!=NULL)){
735 /* File is in both db's and the data is still there. (CHANGED) */
736 if (!(node->checked&(NODE_MOVED_IN|NODE_MOVED_OUT))){
737 if (r->level >= REPORT_LEVEL_LIST_ENTRIES
738 && ((node->old_data->attr&~(r->ignore_removed_attrs))^(node->new_data->attr&~(r->ignore_added_attrs)))&((node->old_data->attr)^(node->new_data->attr)) ) {
739 r->diff_attrs_entries = checked_realloc(r->diff_attrs_entries, (r->num_diff_attrs_entries+1) * sizeof(diff_attrs_entry));
740 r->diff_attrs_entries[r->num_diff_attrs_entries].entry = node->old_data->filename;
741 r->diff_attrs_entries[r->num_diff_attrs_entries].old_attrs = node->old_data->attr&~(r->ignore_removed_attrs);
742 r->diff_attrs_entries[r->num_diff_attrs_entries].new_attrs = node->new_data->attr&~(r->ignore_added_attrs);
743 r->num_diff_attrs_entries++;
744 }
745 DB_ATTR_TYPE changed_attrs = (node->changed_attrs)&~(r->ignore_changed_attrs);
746 if (changed_attrs
747 #ifdef WITH_E2FSATTRS
748 &~ATTR(attr_e2fsattrs) || (changed_attrs&ATTR(attr_e2fsattrs) && ~(r->ignore_e2fsattrs)&(node->old_data->e2fsattrs^node->new_data->e2fsattrs))
749 #endif
750 ) {
751 r->nchg++;
752 node->checked|=NODE_CHANGED;
753 }
754 #ifdef WITH_AUDIT
755 nchg++;
756 #endif
757 }else if (!((node->checked&NODE_ALLOW_NEW)||(node->checked&NODE_MOVED_IN))) {
758 #ifdef WITH_AUDIT
759 nadd++;
760 #endif
761 r->nadd++;
762 node->checked|=NODE_ADDED;
763 }else if (!((node->checked&NODE_ALLOW_RM)||(node->checked&NODE_MOVED_OUT))) {
764 #ifdef WITH_AUDIT
765 nrem++;
766 #endif
767 r->nrem++;
768 node->checked|=NODE_REMOVED;
769 }
770 }
771 }
772
773 added_entries_reported |= r->nadd != 0;
774 removed_entries_reported |= r->nrem != 0;
775 changed_entries_reported |= r->nchg != 0;
776 }
777
778 for(tree_node *x = tree_walk_first(node->children); x != NULL ; x = tree_walk_next(x)) {
779 terse_report(tree_get_data(x));
780 }
781 pthread_rwlock_unlock(&node->rwlock);
782 }
783
784 void print_report_entries(report_t *report, seltree* node, const int node_status, void (*print_line)(report_t*, char*, int, seltree*)) {
785
786 pthread_rwlock_rdlock(&node->rwlock);
787 if (node->checked&node_status) {
788 if (!(node->changed_attrs) || ~(report->ignore_changed_attrs)&(node->changed_attrs
789 #ifdef WITH_E2FSATTRS
790 & (~ATTR(attr_e2fsattrs) | (node->changed_attrs&ATTR(attr_e2fsattrs) && ~(report->ignore_e2fsattrs)&(node->old_data->e2fsattrs^node->new_data->e2fsattrs)?ATTR(attr_e2fsattrs):0))
791 #endif
792 )) {
793 print_line(report, ((node->checked&NODE_REMOVED)?node->old_data:node->new_data)->filename, node->checked, node);
794 }
795
796 }
797 for(tree_node *x = tree_walk_first(node->children); x != NULL ; x = tree_walk_next(x)) {
798 print_report_entries(report, tree_get_data(x), node_status, print_line);
799 }
800 pthread_rwlock_unlock(&node->rwlock);
801 }
802
803 void print_dbline_attrs(report_t * report, db_line* oline, db_line* nline, DB_ATTR_TYPE report_attrs, void (*print_attribute)(report_t *, db_line*, db_line*, ATTRIBUTE)) {
804 for (int j=0; j < report_attrs_order_length; ++j) {
805 switch(report_attrs_order[j]) {
806 case attr_allhashsums:
807 for (int i = 0 ; i < num_hashes ; ++i) {
808 if (ATTR(hashsums[i].attribute)&report_attrs) { print_attribute(report, oline, nline, hashsums[i].attribute); }
809 }
810 break;
811 case attr_size:
812 if (ATTR(attr_size)&report_attrs) { print_attribute(report, oline, nline, attr_size); }
813 if (ATTR(attr_sizeg)&report_attrs) { print_attribute(report, oline, nline, attr_sizeg); }
814 break;
815 default:
816 if (ATTR(report_attrs_order[j])&report_attrs) { print_attribute(report, oline, nline, report_attrs_order[j]); }
817 break;
818 }
819 }
820 }
821
822
823 void print_databases_attrs(report_t *report, void (*print_database_attributes)(report_t *, db_line*)) {
824 if (conf->database_in.db_line) {
825 print_database_attributes(report, conf->database_in.db_line);
826 }
827 if (conf->database_out.db_line) {
828 print_database_attributes(report, conf->database_out.db_line);
829 }
830 if (conf->database_new.db_line) {
831 print_database_attributes(report, conf->database_new.db_line);
832 }
833 }
834
835 void print_report_details(report_t *report, seltree* node, void (*print_attributes)(report_t *, db_line*, db_line*, DB_ATTR_TYPE)) {
836 pthread_rwlock_rdlock(&node->rwlock);
837 if (node->checked&NODE_CHANGED) {
838 print_attributes(report, node->old_data, node->new_data, get_report_attributes(node, report));
839 }
840 if (report->level >= REPORT_LEVEL_ADDED_REMOVED_ENTRIES) {
841 if (node->checked&NODE_ADDED) {
842 print_attributes(report, NULL, node->new_data, (node->new_data)->attr&~(report->ignore_added_attrs));
843 }
844 if (node->checked&NODE_REMOVED) {
845 print_attributes(report, node->old_data, NULL, (node->old_data)->attr&~(report->ignore_removed_attrs));
846 }
847 }
848 for(tree_node *x = tree_walk_first(node->children); x != NULL ; x = tree_walk_next(x)) {
849 print_report_details(report, tree_get_data(x), print_attributes);
850 }
851 pthread_rwlock_unlock(&node->rwlock);
852 }
853
854 #ifdef WITH_AUDIT
855 /* Something changed, send audit anomaly message */
856 void send_audit_report(void)
857 {
858 if(nadd!=0||nrem!=0||nchg!=0){
859 int fd=audit_open();
860 if (fd>=0){
861 char msg[64];
862
863 snprintf(msg, sizeof(msg), "added=%ld removed=%ld changed=%ld",
864 nadd, nrem, nchg);
865
866 if (audit_log_user_message(fd, AUDIT_ANOM_RBAC_INTEGRITY_FAIL,
867 msg, NULL, NULL, NULL, 0)<=0)
868 #ifdef HAVE_SYSLOG
869 syslog(LOG_ERR, "Failed sending audit message:%s", msg);
870 #else
871 ;
872 #endif
873 close(fd);
874 }
875 }
876 }
877 #endif /* WITH_AUDIT */
878
879
880 static void print_report(report_t * report, seltree * node, report_format_module module) {
881 if (module.print_report_header) {
882 module.print_report_header(report);
883 }
884
885 module.print_report_diff_attrs_entries(report);
886
887 if (report->level >= REPORT_LEVEL_SUMMARY) {
888 char *time = get_time_string(&(conf->start_time));
889 module.print_report_starttime_version(report, time, conf->aide_version);
890 free(time); time=NULL;
891 }
892
893 module.print_report_outline(report);
894
895 if (report->level >= REPORT_LEVEL_SUMMARY) {
896 if(conf->action&DO_INIT) {
897 module.print_report_new_database_written(report);
898 }
899 module.print_report_config_options(report);
900 }
901
902 if (report->level >= REPORT_LEVEL_LIST_ENTRIES) {
903 module.print_report_report_options(report);
904 }
905
906 if (report->level >= REPORT_LEVEL_SUMMARY) {
907 module.print_report_summary(report);
908 }
909
910 if (conf->action&(DO_COMPARE|DO_DIFF) || (conf->action&DO_INIT && report->detailed_init)) {
911 if (report->level >= REPORT_LEVEL_LIST_ENTRIES) {
912 if (report->grouped) {
913 if (report->nadd) {
914 module.print_report_entries(report, node, NODE_ADDED);
915 }
916 if (report->nrem) {
917 module.print_report_entries(report, node, NODE_REMOVED);
918 }
919 if (report->nchg) {
920 module.print_report_entries(report, node, NODE_CHANGED);
921 }
922 } else {
923 if (report->nadd || report->nrem || report->nchg) {
924 module.print_report_entries(report, node, NODE_ADDED|NODE_REMOVED|NODE_CHANGED);
925 }
926 }
927 }
928 if ( (report->nchg && report->level >= REPORT_LEVEL_CHANGED_ATTRIBUTES) || ( (report->nadd || report->nrem) && report->level >= REPORT_LEVEL_ADDED_REMOVED_ENTRIES) ) {
929 module.print_report_details(report, node);
930 }
931 }
932
933 if (report->level >= REPORT_LEVEL_DATABASE_ATTRIBUTES) {
934 if (conf->database_in.db_line || conf->database_out.db_line || conf->database_new.db_line) {
935
936 module.print_report_databases(report);
937 }
938 }
939
940 if (report->level >= REPORT_LEVEL_SUMMARY) {
941 char *time = get_time_string(&(conf->end_time));
942 long run_time = (long) difftime(conf->end_time, conf->start_time);
943 module.print_report_endtime_runtime(report, time, run_time);
944 free(time); time=NULL;
945 }
946 if (module.print_report_footer) {
947 module.print_report_footer(report);
948 }
949 }
950
951 int gen_report(seltree* node) {
952 list* report_list = NULL;
953
954 terse_report(node);
955
956 #ifdef WITH_AUDIT
957 send_audit_report();
958 #endif
959
960 for (report_list=conf->report_urls; report_list; report_list=report_list->next) {
961 report_t* report = report_list->data;
962 switch(report->format) {
963 case REPORT_FORMAT_PLAIN:
964 print_report(report, node, report_module_plain);
965 break;
966 case REPORT_FORMAT_JSON:
967 print_report(report, node, report_module_json);
968 break;
969 case REPORT_FORMAT_UNKNOWN:
970 /* skip unknown report format */
971 break;
972 }
973 }
974
975 return conf->action&(DO_COMPARE|DO_DIFF) ? (added_entries_reported)*1+(removed_entries_reported!=0)*2+(changed_entries_reported!=0)*4 : 0;
976 }
977
978 // vi: ts=8 sw=8