| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 2022-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 <stdlib.h> |
| 22 | #include "config.h" |
| 23 | #include "aide.h" |
| 24 | #include "attributes.h" |
| 25 | #include "conf_ast.h" |
| 26 | #include "db.h" |
| 27 | #include "db_config.h" |
| 28 | #include "db_line.h" |
| 29 | #include "report.h" |
| 30 | #include "seltree.h" |
| 31 | #include "stdbool.h" |
| 32 | #include "url.h" |
| 33 | |
| 34 | #define JSON_FMT_ARRAY_BEGIN "%*c\"%s\": [\n" |
| 35 | #define JSON_FMT_ARRAY_ELEMENT_INNER "%*c\"%s\",\n" |
| 36 | #define JSON_FMT_ARRAY_ELEMENT_LAST "%*c\"%s\"\n" |
| 37 | #define JSON_FMT_ARRAY_ELEMENT_PLAIN "%*c\"%s\"" |
| 38 | #define JSON_FMT_ARRAY_END "%*c],\n" |
| 39 | #define JSON_FMT_ARRAY_END_PLAIN "%*c]" |
| 40 | #define JSON_FMT_LONG "%*c\"%s\": %ld,\n" |
| 41 | #define JSON_FMT_LONG_LAST "%*c\"%s\": %ld\n" |
| 42 | #define JSON_FMT_OBJECT_BEGIN "%*c\"%s\": {\n" |
| 43 | #define JSON_FMT_OBJECT_END "%*c},\n" |
| 44 | #define JSON_FMT_OBJECT_END_PLAIN "%*c}" |
| 45 | #define JSON_FMT_STRING_COMMA "%*c\"%s\": \"%s\",\n" |
| 46 | #define JSON_FMT_STRING_LAST "%*c\"%s\": \"%s\"\n" |
| 47 | #define JSON_FMT_STRING_PLAIN "%*c\"%s\": \"%s\"" |
| 48 | |
| 49 | bool line_first = true; |
| 50 | bool database_first = true; |
| 51 | bool databases_first = true; |
| 52 | bool details_first = true; |
| 53 | bool attributes_first = true; |
| 54 | |
| 55 | static int _escape_json_string(const char *src, char *escaped_string) { |
| 56 | size_t i; |
| 57 | int n = 0; |
| 58 | |
| 59 | for (i = 0; i < strlen(src); ++i) { |
| 60 | switch(src[i]) { |
| 61 | case '\n': |
| 62 | if (escaped_string) { escaped_string[n] = '\\'; } |
| 63 | n++; |
| 64 | if (escaped_string) { escaped_string[n] = 'n'; } |
| 65 | n++; |
| 66 | break; |
| 67 | case '\t': |
| 68 | if (escaped_string) { escaped_string[n] = '\\'; } |
| 69 | n++; |
| 70 | if (escaped_string) { escaped_string[n] = 't'; } |
| 71 | n++; |
| 72 | break; |
| 73 | case '\b': |
| 74 | if (escaped_string) { escaped_string[n] = '\\'; } |
| 75 | n++; |
| 76 | if (escaped_string) { escaped_string[n] = 'b'; } |
| 77 | n++; |
| 78 | break; |
| 79 | case '\f': |
| 80 | if (escaped_string) { escaped_string[n] = '\\'; } |
| 81 | n++; |
| 82 | if (escaped_string) { escaped_string[n] = 'f'; } |
| 83 | n++; |
| 84 | break; |
| 85 | case '\r': |
| 86 | if (escaped_string) { escaped_string[n] = '\\'; } |
| 87 | n++; |
| 88 | if (escaped_string) { escaped_string[n] = 'r'; } |
| 89 | n++; |
| 90 | break; |
| 91 | case '"': |
| 92 | case '\\': |
| 93 | if (escaped_string) { escaped_string[n] = '\\'; } |
| 94 | n++; |
| 95 | if (escaped_string) { escaped_string[n] = src[i]; } |
| 96 | n++; |
| 97 | break; |
| 98 | default: |
| 99 | if (src[i] >= 0 && (src[i] < 0x1f || src[i] == 0x7f)) { |
| 100 | if (escaped_string) { snprintf(&escaped_string[n], 7, "\\u%04x", src[i]); } |
| 101 | n += 6; |
| 102 | } else { |
| 103 | if (escaped_string) { escaped_string[n] = src[i]; } |
| 104 | n++; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | if (escaped_string) { escaped_string[n] = '\0'; } |
| 109 | n++; |
| 110 | |
| 111 | return n; |
| 112 | } |
| 113 | |
| 114 | static char *_get_escaped_json_string(const char *src) { |
| 115 | char *str = NULL; |
| 116 | int n = _escape_json_string(src, str); |
| 117 | str = checked_malloc(n); |
| 118 | _escape_json_string(src, str); |
| 119 | return str; |
| 120 | } |
| 121 | |
| 122 | static void _print_config_option(report_t *report, config_option option, const char* value) { |
| 123 | char *escaped_value = _get_escaped_json_string(value); |
| 124 | report_printf(report, JSON_FMT_STRING_COMMA, 2, ' ', config_options[option].config_name, escaped_value); |
| 125 | free(escaped_value); |
| 126 | } |
| 127 | |
| 128 | static char* _get_value_format(ATTRIBUTE attribute) { |
| 129 | switch(attribute) { |
| 130 | case attr_uid: |
| 131 | case attr_gid: |
| 132 | case attr_size: |
| 133 | case attr_sizeg: |
| 134 | case attr_inode: |
| 135 | case attr_bcount: |
| 136 | case attr_linkcount: |
| 137 | return "%*c\"%s\": %s"; |
| 138 | default: |
| 139 | return "%*c\"%s\": \"%s\""; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static void print_line_json(report_t* report, char* filename, int node_checked, seltree* node) { |
| 144 | if (line_first) { line_first=false; } |
| 145 | else { report_printf(report,",\n"); } |
| 146 | |
| 147 | char *escaped_filename = _get_escaped_json_string(filename); |
| 148 | |
| 149 | if(report->summarize_changes) { |
| 150 | char* summary = get_summarize_changes_string(report, node); |
| 151 | report_printf(report, JSON_FMT_STRING_PLAIN, 4, ' ', escaped_filename, summary); |
| 152 | free(summary); summary=NULL; |
| 153 | } else if (!report->grouped) { |
| 154 | char* change_type; |
| 155 | if (node_checked&NODE_ADDED) { |
| 156 | change_type = "added"; |
| 157 | } else if (node_checked&NODE_REMOVED) { |
| 158 | change_type = "removed"; |
| 159 | } else { |
| 160 | change_type = "changed"; |
| 161 | } |
| 162 | report_printf(report, JSON_FMT_STRING_PLAIN, 4, ' ', escaped_filename, change_type); |
| 163 | } else { |
| 164 | report_printf(report, JSON_FMT_ARRAY_ELEMENT_PLAIN, 4, ' ', escaped_filename); |
| 165 | } |
| 166 | free(escaped_filename); |
| 167 | } |
| 168 | |
| 169 | static void _print_attribute_value(report_t *report, const char* name, ATTRIBUTE attribute, char **value, int num, int ident) { |
| 170 | if (num) { |
| 171 | if (num > 1) { |
| 172 | report_printf(report, JSON_FMT_ARRAY_BEGIN, ident, ' ', name); |
| 173 | for (int i = 0 ; i < num ; i++) { |
| 174 | char *escaped_value = _get_escaped_json_string(value[i]); |
| 175 | report_printf(report, i+1<num?JSON_FMT_ARRAY_ELEMENT_INNER:JSON_FMT_ARRAY_ELEMENT_LAST, ident+2, ' ', escaped_value); |
| 176 | free(escaped_value); |
| 177 | } |
| 178 | report_printf(report, JSON_FMT_ARRAY_END_PLAIN, ident, ' '); |
| 179 | } else { |
| 180 | char *escaped_value = _get_escaped_json_string(value[0]); |
| 181 | report_printf(report, _get_value_format(attribute), ident, ' ', name, escaped_value); |
| 182 | free(escaped_value); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static void _print_attribute(report_t *report, db_line* oline, db_line* nline, ATTRIBUTE attribute) { |
| 188 | char **ovalue = NULL; |
| 189 | char **nvalue = NULL; |
| 190 | int onumber, nnumber, i; |
| 191 | |
| 192 | if (attributes_first) { attributes_first=false; } |
| 193 | else { report_printf(report,",\n"); } |
| 194 | |
| 195 | DB_ATTR_TYPE attr = ATTR(attribute); |
| 196 | |
| 197 | const char* json_name = attributes[attribute].field_name; |
| 198 | report_printf(report, JSON_FMT_OBJECT_BEGIN, 6, ' ', json_name); |
| 199 | |
| 200 | onumber=get_attribute_values(attr, oline, &ovalue, report); |
| 201 | nnumber=get_attribute_values(attr, nline, &nvalue, report); |
| 202 | |
| 203 | _print_attribute_value(report, "old", attribute, ovalue, onumber, 8); |
| 204 | for(i=0; i < onumber; ++i) { free(ovalue[i]); ovalue[i]=NULL; } free(ovalue); ovalue=NULL; |
| 205 | |
| 206 | if (onumber && nnumber) { report_printf(report,",\n"); } |
| 207 | |
| 208 | _print_attribute_value(report, "new", attribute, nvalue, nnumber, 8); |
| 209 | for(i=0; i < nnumber; ++i) { free(nvalue[i]); nvalue[i]=NULL; } free(nvalue); nvalue=NULL; |
| 210 | |
| 211 | report_printf(report,"\n"); |
| 212 | report_printf(report, JSON_FMT_OBJECT_END_PLAIN, 6, ' '); |
| 213 | } |
| 214 | |
| 215 | static void _print_database_attribute(report_t *report, db_line* oline, __attribute__((unused)) db_line* nline, ATTRIBUTE attribute) { |
| 216 | char **value; |
| 217 | int num, i; |
| 218 | |
| 219 | if (database_first) { database_first=false; } |
| 220 | else { report_printf(report,",\n"); } |
| 221 | |
| 222 | DB_ATTR_TYPE attr = ATTR(attribute); |
| 223 | |
| 224 | const char* json_name = attributes[attribute].field_name; |
| 225 | |
| 226 | num=get_attribute_values(attr, oline, &value, report); |
| 227 | |
| 228 | _print_attribute_value(report, json_name, attribute, value, num, 6); |
| 229 | for(i=0; i < num; ++i) { free(value[i]); value[i]=NULL; } free(value); value=NULL; |
| 230 | } |
| 231 | |
| 232 | static void _print_database_attributes(report_t *report, db_line* db) { |
| 233 | if (databases_first) { databases_first=false; } |
| 234 | else { report_printf(report,",\n"); } |
| 235 | |
| 236 | char *escaped_filename = _get_escaped_json_string(db->filename); |
| 237 | |
| 238 | database_first = true; |
| 239 | report_printf(report, JSON_FMT_OBJECT_BEGIN, 4, ' ', escaped_filename); |
| 240 | print_dbline_attrs(report, db, NULL, db->attr, _print_database_attribute); |
| 241 | report_printf(report,"\n"); |
| 242 | report_printf(report, JSON_FMT_OBJECT_END_PLAIN, 4, ' '); |
| 243 | free(escaped_filename); |
| 244 | } |
| 245 | |
| 246 | static void _print_report_dbline_attributes(report_t *report, db_line* oline, db_line* nline, DB_ATTR_TYPE report_attrs) { |
| 247 | if (report_attrs) { |
| 248 | if (details_first) { details_first=false; } |
| 249 | else { report_printf(report,",\n"); } |
| 250 | |
| 251 | char *escaped_filename = _get_escaped_json_string((nline==NULL?oline:nline)->filename); |
| 252 | report_printf(report, JSON_FMT_OBJECT_BEGIN, 4, ' ', escaped_filename); |
| 253 | attributes_first = true; |
| 254 | print_dbline_attrs(report, oline, nline, report_attrs, _print_attribute); |
| 255 | report_printf(report,"\n"); |
| 256 | report_printf(report, JSON_FMT_OBJECT_END_PLAIN, 4, ' '); |
| 257 | free(escaped_filename); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | static void print_report_header_json(report_t *report) { |
| 262 | report_printf(report, "{\n"); |
| 263 | } |
| 264 | static void print_report_footer_json(report_t *report) { |
| 265 | report_printf(report, "}\n"); |
| 266 | } |
| 267 | static void print_report_outline_json(report_t *report) { |
| 268 | report_printf(report, report->level >= REPORT_LEVEL_SUMMARY?JSON_FMT_STRING_COMMA:JSON_FMT_STRING_LAST , 2, ' ', "outline", get_summary_string(report)); |
| 269 | } |
| 270 | static void print_report_starttime_version_json(report_t *report, const char* start_time, const char* aide_version) { |
| 271 | report_printf(report, JSON_FMT_STRING_COMMA, 2, ' ', "start_time", start_time); |
| 272 | report_printf(report, JSON_FMT_STRING_COMMA, 2, ' ', "aide_version", aide_version); |
| 273 | } |
| 274 | static void print_report_endtime_runtime_json(report_t* report, const char* end_time, long run_time) { |
| 275 | report_printf(report, JSON_FMT_STRING_COMMA, 2, ' ', "end_time", end_time); |
| 276 | report_printf(report, JSON_FMT_LONG_LAST, 2, ' ', "run_time_seconds", run_time); |
| 277 | } |
| 278 | |
| 279 | static void print_report_config_options_json(report_t *report) { |
| 280 | print_report_config_options(report, _print_config_option); |
| 281 | } |
| 282 | |
| 283 | static void print_report_report_options_json(report_t *report) { |
| 284 | print_report_report_options(report, _print_config_option); |
| 285 | } |
| 286 | |
| 287 | static void print_report_summary_json(report_t *report) { |
| 288 | report_printf(report, JSON_FMT_OBJECT_BEGIN, 2, ' ', "number_of_entries"); |
| 289 | if (conf->action&(DO_COMPARE|DO_DIFF) && (report->nadd||report->nrem||report->nchg)) { |
| 290 | report_printf(report, JSON_FMT_LONG, 4, ' ', "total", report->ntotal); |
| 291 | report_printf(report, JSON_FMT_LONG, 4, ' ', "added", report->nadd); |
| 292 | report_printf(report, JSON_FMT_LONG, 4, ' ', "removed", report->nrem); |
| 293 | report_printf(report, JSON_FMT_LONG_LAST, 4, ' ', "changed", report->nchg); |
| 294 | } else { |
| 295 | report_printf(report, JSON_FMT_LONG_LAST, 4, ' ', "total", report->ntotal); |
| 296 | } |
| 297 | report_printf(report, JSON_FMT_OBJECT_END, 2, ' '); |
| 298 | } |
| 299 | |
| 300 | static void print_report_new_database_written_json(report_t *report) { |
| 301 | report_printf(report, JSON_FMT_STRING_COMMA, 2, ' ', "new_database", conf->database_out.url->value); |
| 302 | } |
| 303 | |
| 304 | static void print_report_details_json(report_t *report, seltree* node) { |
| 305 | details_first = true; |
| 306 | report_printf(report, JSON_FMT_OBJECT_BEGIN, 2, ' ', "details"); |
| 307 | print_report_details(report, node, _print_report_dbline_attributes); |
| 308 | report_printf(report,"\n"); |
| 309 | report_printf(report, JSON_FMT_OBJECT_END, 2, ' '); |
| 310 | } |
| 311 | |
| 312 | static void print_report_databases_json(report_t *report) { |
| 313 | databases_first = true; |
| 314 | report_printf(report, JSON_FMT_OBJECT_BEGIN, 2, ' ', "databases"); |
| 315 | print_databases_attrs(report, _print_database_attributes); |
| 316 | report_printf(report,"\n"); |
| 317 | report_printf(report, JSON_FMT_OBJECT_END, 2, ' '); |
| 318 | } |
| 319 | |
| 320 | static void print_report_entries_json(report_t *report, seltree* node, const int filter) { |
| 321 | line_first = true; |
| 322 | switch (filter) { |
| 323 | case NODE_ADDED: |
| 324 | report_printf(report, report->summarize_changes||!report->grouped?JSON_FMT_OBJECT_BEGIN:JSON_FMT_ARRAY_BEGIN, 2, ' ', "added"); |
| 325 | break; |
| 326 | case NODE_REMOVED: |
| 327 | report_printf(report, report->summarize_changes||!report->grouped?JSON_FMT_OBJECT_BEGIN:JSON_FMT_ARRAY_BEGIN, 2, ' ', "removed"); |
| 328 | break; |
| 329 | case NODE_CHANGED: |
| 330 | report_printf(report, report->summarize_changes||!report->grouped?JSON_FMT_OBJECT_BEGIN:JSON_FMT_ARRAY_BEGIN, 2, ' ', "changed"); |
| 331 | break; |
| 332 | default: |
| 333 | report_printf(report, report->summarize_changes||!report->grouped?JSON_FMT_OBJECT_BEGIN:JSON_FMT_ARRAY_BEGIN, 2, ' ', "entries"); |
| 334 | break; |
| 335 | } |
| 336 | print_report_entries(report, node, filter, print_line_json); |
| 337 | report_printf(report,"\n"); |
| 338 | report_printf(report, report->summarize_changes||!report->grouped?JSON_FMT_OBJECT_END:JSON_FMT_ARRAY_END, 2, ' '); |
| 339 | } |
| 340 | |
| 341 | static void print_report_diff_attrs_entries_json(report_t *report) { |
| 342 | if (report->num_diff_attrs_entries) { |
| 343 | report_printf(report, JSON_FMT_OBJECT_BEGIN, 2, ' ', "different_attributes"); |
| 344 | for(int i = 0; i < report->num_diff_attrs_entries; ++i) { |
| 345 | char *escaped_filename = _get_escaped_json_string(report->diff_attrs_entries[i].entry); |
| 346 | char *attrs = diff_attributes(report->diff_attrs_entries[i].old_attrs, report->diff_attrs_entries[i].new_attrs); |
| 347 | report_printf(report, i + 1 < report->num_diff_attrs_entries ? JSON_FMT_STRING_COMMA : JSON_FMT_STRING_LAST, 4, ' ', |
| 348 | escaped_filename, attrs); |
| 349 | free(escaped_filename); |
| 350 | free(attrs); |
| 351 | } |
| 352 | report->num_diff_attrs_entries = 0; |
| 353 | free(report->diff_attrs_entries); |
| 354 | report_printf(report, JSON_FMT_OBJECT_END, 2, ' '); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | report_format_module report_module_json = { |
| 359 | .print_report_config_options = print_report_config_options_json, |
| 360 | .print_report_databases = print_report_databases_json, |
| 361 | .print_report_details = print_report_details_json, |
| 362 | .print_report_diff_attrs_entries = print_report_diff_attrs_entries_json, |
| 363 | .print_report_endtime_runtime = print_report_endtime_runtime_json, |
| 364 | .print_report_entries = print_report_entries_json, |
| 365 | .print_report_footer = print_report_footer_json, |
| 366 | .print_report_header = print_report_header_json, |
| 367 | .print_report_new_database_written = print_report_new_database_written_json, |
| 368 | .print_report_outline = print_report_outline_json, |
| 369 | .print_report_report_options = print_report_report_options_json, |
| 370 | .print_report_starttime_version = print_report_starttime_version_json, |
| 371 | .print_report_summary = print_report_summary_json, |
| 372 | }; |