| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 1999-2006, 2010, 2011, 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 <stdlib.h> |
| 24 | #include "config.h" |
| 25 | #include "aide.h" |
| 26 | #include "attributes.h" |
| 27 | #include "conf_ast.h" |
| 28 | #include "db.h" |
| 29 | #include "db_config.h" |
| 30 | #include "db_line.h" |
| 31 | #include "report.h" |
| 32 | #include "seltree.h" |
| 33 | #include "stdbool.h" |
| 34 | #include "url.h" |
| 35 | #include "locale-aide.h" |
| 36 | |
| 37 | #define PLAIN_REPORT_HEADLINE_FMT "\n\n---------------------------------------------------\n%s:\n---------------------------------------------------\n" |
| 38 | |
| 39 | bool first = true; |
| 40 | |
| 41 | static void print_report_dbline_attributes_plain(report_t *, db_line*, db_line*, DB_ATTR_TYPE); |
| 42 | |
| 43 | static char* _get_not_grouped_list_string(report_t *report) { |
| 44 | if (report->nadd && report->nrem && report->nchg) { return _("Added, removed and changed entries"); } |
| 45 | else if (report->nadd && report->nrem) { return _("Added and removed entries"); } |
| 46 | else if (report->nadd && report->nchg) { return _("Added and changed entries"); } |
| 47 | else if (report->nrem && report->nchg) { return _("Removed and changed entries"); } |
| 48 | else if (report->nadd) { return _("Added entries"); } |
| 49 | else if (report->nrem) { return _("Removed entries"); } |
| 50 | else { return _("Changed entries"); } |
| 51 | } |
| 52 | |
| 53 | static void _print_config_option(report_t *report, config_option option, const char* value) { |
| 54 | if (first) { first=false; } |
| 55 | else { report_printf(report," | "); } |
| 56 | char *value_safe = stresc(value); |
| 57 | report_printf(report, "%s: %s", config_options[option].report_string, value_safe); |
| 58 | free(value_safe); |
| 59 | } |
| 60 | |
| 61 | static void _print_report_option(report_t *report, config_option option, const char* value) { |
| 62 | report_printf(report, "%s: %s\n", config_options[option].report_string, value); |
| 63 | } |
| 64 | |
| 65 | static void _print_attribute(report_t *report, db_line* oline, db_line* nline, ATTRIBUTE attribute) { |
| 66 | char **ovalues = NULL; |
| 67 | char **nvalues = NULL; |
| 68 | int onumber, nnumber, i, c; |
| 69 | int p = (conf->print_details_width-(4 + MAX_WIDTH_DETAILS_STRING))/2; |
| 70 | |
| 71 | DB_ATTR_TYPE attr = ATTR(attribute); |
| 72 | const char* name = attributes[attribute].details_string; |
| 73 | |
| 74 | onumber=get_attribute_values(attr, oline, &ovalues, report); |
| 75 | nnumber=get_attribute_values(attr, nline, &nvalues, report); |
| 76 | |
| 77 | i = 0; |
| 78 | while (i<onumber || i<nnumber) { |
| 79 | char *ovalue = NULL; |
| 80 | char *nvalue = NULL; |
| 81 | int olen = 0; |
| 82 | int nlen = 0; |
| 83 | if (i<onumber){ |
| 84 | ovalue = stresc(ovalues[i]); |
| 85 | olen = strlen(ovalue); |
| 86 | } |
| 87 | if (i<nnumber) { |
| 88 | nvalue = stresc(nvalues[i]); |
| 89 | nlen = strlen(nvalue); |
| 90 | } |
| 91 | int k = 0; |
| 92 | while (olen-p*k >= 0 || nlen-p*k >= 0) { |
| 93 | c = k*(p-1); |
| 94 | if (!onumber) { |
| 95 | report_printf(report," %-*s%c %-*c %.*s\n", MAX_WIDTH_DETAILS_STRING, (i+k)?"":name, (i+k)?' ':':', p, ' ', p-1, nlen-c>0?&nvalue[c]:""); |
| 96 | } else if (!nnumber) { |
| 97 | report_printf(report," %-*s%c %.*s\n", MAX_WIDTH_DETAILS_STRING, (i+k)?"":name, (i+k)?' ':':', p-1, olen-c>0?&ovalue[c]:""); |
| 98 | } else { |
| 99 | report_printf(report," %-*s%c %-*.*s| %.*s\n", MAX_WIDTH_DETAILS_STRING, (i+k)?"":name, (i+k)?' ':':', p, p-1, olen-c>0?&ovalue[c]:"", p-1, nlen-c>0?&nvalue[c]:""); |
| 100 | } |
| 101 | k++; |
| 102 | } |
| 103 | ++i; |
| 104 | free(ovalue); |
| 105 | free(nvalue); |
| 106 | } |
| 107 | for(i=0; i < onumber; ++i) { free(ovalues[i]); ovalues[i]=NULL; } free(ovalues); ovalues=NULL; |
| 108 | for(i=0; i < nnumber; ++i) { free(nvalues[i]); nvalues[i]=NULL; } free(nvalues); nvalues=NULL; |
| 109 | } |
| 110 | |
| 111 | static void _print_database_attributes(report_t *report, db_line* db) { |
| 112 | print_report_dbline_attributes_plain(report, db, NULL, db->attr); |
| 113 | } |
| 114 | |
| 115 | static void print_report_outline_plain(report_t *report) { |
| 116 | report_printf(report, "%s\n", get_summary_string(report)); |
| 117 | } |
| 118 | |
| 119 | static void print_report_new_database_written_plain(report_t *report) { |
| 120 | report_printf(report,_("New AIDE database written to %s\n"),conf->database_out.url->value); |
| 121 | } |
| 122 | |
| 123 | static void print_report_config_options_plain(report_t *report) { |
| 124 | first = true; |
| 125 | print_report_config_options(report, _print_config_option); |
| 126 | if (!first) { report_printf(report, "\n"); } |
| 127 | } |
| 128 | |
| 129 | static void print_report_report_options_plain(report_t *report) { |
| 130 | print_report_report_options(report, _print_report_option); |
| 131 | } |
| 132 | |
| 133 | static void print_report_starttime_version_plain(report_t *report, const char* start_time, const char* aide_version) { |
| 134 | report_printf(report, _("Start timestamp: %s (AIDE %s)\n"), start_time, aide_version); |
| 135 | } |
| 136 | |
| 137 | static void print_report_endtime_runtime_plain(report_t* report, const char* end_time, long run_time) { |
| 138 | report_printf(report, _("\n\nEnd timestamp: %s (run time: %ldm %lds)\n"), end_time, run_time/60, run_time%60); |
| 139 | } |
| 140 | |
| 141 | static void print_report_summary_plain(report_t *report) { |
| 142 | if(conf->action&(DO_COMPARE|DO_DIFF) && (report->nadd||report->nrem||report->nchg)) { |
| 143 | report_printf(report,_("\nSummary:\n Total number of entries:\t%li\n Added entries:\t\t%li\n" |
| 144 | " Removed entries:\t\t%li\n Changed entries:\t\t%li"), report->ntotal, report->nadd, report->nrem, report->nchg); |
| 145 | } else { |
| 146 | report_printf(report, _("\nNumber of entries:\t%li"), report->ntotal); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static void print_line_plain(report_t* report, char* filename, int node_checked, seltree* node) { |
| 151 | char *filename_safe = stresc(filename); |
| 152 | if(report->summarize_changes) { |
| 153 | char* summary = get_summarize_changes_string(report, node); |
| 154 | report_printf(report, "\n%s: %s", summary, filename_safe); |
| 155 | free(summary); summary=NULL; |
| 156 | } else { |
| 157 | if (node_checked&NODE_ADDED) { |
| 158 | report_printf(report, _("\nadded: %s"), filename_safe); |
| 159 | } else if (node_checked&NODE_REMOVED) { |
| 160 | report_printf(report, _("\nremoved: %s"), filename_safe); |
| 161 | } else if (node_checked&NODE_CHANGED) { |
| 162 | report_printf(report, _("\nchanged: %s"), filename_safe); |
| 163 | } |
| 164 | } |
| 165 | free(filename_safe); |
| 166 | } |
| 167 | |
| 168 | static void print_report_dbline_attributes_plain(report_t *report, db_line* oline, db_line* nline, DB_ATTR_TYPE report_attrs) { |
| 169 | if (report_attrs) { |
| 170 | db_line* line = nline==NULL?oline:nline; |
| 171 | report_printf(report, "\n"); |
| 172 | if (line->perm) { |
| 173 | report_printf(report, "%s: ", get_file_type_string(line->perm)); |
| 174 | } |
| 175 | char *filename_safe = stresc(line->filename); |
| 176 | report_printf(report, "%s\n", filename_safe); |
| 177 | free(filename_safe); |
| 178 | |
| 179 | print_dbline_attrs(report, oline, nline, report_attrs, _print_attribute); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static void print_report_databases_plain(report_t *report) { |
| 184 | report_printf(report, PLAIN_REPORT_HEADLINE_FMT,_("The attributes of the (uncompressed) database(s)")); |
| 185 | print_databases_attrs(report, _print_database_attributes); |
| 186 | } |
| 187 | |
| 188 | static void print_report_entries_plain(report_t *report, seltree* node, const int filter) { |
| 189 | switch (filter) { |
| 190 | case NODE_ADDED: |
| 191 | report_printf(report, PLAIN_REPORT_HEADLINE_FMT,_("Added entries")); |
| 192 | break; |
| 193 | case NODE_REMOVED: |
| 194 | report_printf(report, PLAIN_REPORT_HEADLINE_FMT,_("Removed entries")); |
| 195 | break; |
| 196 | case NODE_CHANGED: |
| 197 | report_printf(report, PLAIN_REPORT_HEADLINE_FMT,_("Changed entries")); |
| 198 | break; |
| 199 | default: |
| 200 | report_printf(report, PLAIN_REPORT_HEADLINE_FMT, _get_not_grouped_list_string(report)); |
| 201 | break; |
| 202 | } |
| 203 | print_report_entries(report, node, filter, print_line_plain); |
| 204 | } |
| 205 | |
| 206 | static void print_report_details_plain(report_t *report, seltree* node) { |
| 207 | report_printf(report, PLAIN_REPORT_HEADLINE_FMT,_("Detailed information about changes")); |
| 208 | print_report_details(report, node, print_report_dbline_attributes_plain); |
| 209 | } |
| 210 | |
| 211 | static void print_report_diff_attrs_entries_plain(report_t *report) { |
| 212 | for(int i = 0; i < report->num_diff_attrs_entries; ++i) { |
| 213 | char *str = NULL; |
| 214 | char *entry_safe = stresc(report->diff_attrs_entries[i].entry); |
| 215 | report_printf(report, "Entry %s in databases has different attributes: %s\n", |
| 216 | entry_safe, |
| 217 | str= diff_attributes(report->diff_attrs_entries[i].old_attrs, report->diff_attrs_entries[i].new_attrs)); |
| 218 | free(entry_safe); |
| 219 | free(str); |
| 220 | } |
| 221 | report->num_diff_attrs_entries = 0; |
| 222 | free(report->diff_attrs_entries); |
| 223 | } |
| 224 | |
| 225 | report_format_module report_module_plain = { |
| 226 | .print_report_config_options = print_report_config_options_plain, |
| 227 | .print_report_databases = print_report_databases_plain, |
| 228 | .print_report_details = print_report_details_plain, |
| 229 | .print_report_diff_attrs_entries = print_report_diff_attrs_entries_plain, |
| 230 | .print_report_endtime_runtime = print_report_endtime_runtime_plain, |
| 231 | .print_report_entries = print_report_entries_plain, |
| 232 | .print_report_footer = NULL, |
| 233 | .print_report_header = NULL, |
| 234 | .print_report_new_database_written = print_report_new_database_written_plain, |
| 235 | .print_report_outline = print_report_outline_plain, |
| 236 | .print_report_report_options = print_report_report_options_plain, |
| 237 | .print_report_starttime_version = print_report_starttime_version_plain, |
| 238 | .print_report_summary = print_report_summary_plain, |
| 239 | }; |