| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 2020,2022,2023,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 "config.h" |
| 22 | #include <stdarg.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <pthread.h> |
| 28 | |
| 29 | #include "log.h" |
| 30 | #include "errorcodes.h" |
| 31 | #include "util.h" |
| 32 | |
| 33 | LOG_LEVEL prev_log_level = LOG_LEVEL_UNSET; |
| 34 | LOG_LEVEL log_level = LOG_LEVEL_UNSET; |
| 35 | |
| 36 | typedef struct log_cache { |
| 37 | LOG_LEVEL level; |
| 38 | char *message; |
| 39 | } log_cache; |
| 40 | |
| 41 | log_cache *cached_lines = NULL; |
| 42 | int ncachedlines = 0; |
| 43 | |
| 44 | int colored_log = -1; |
| 45 | |
| 46 | pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 47 | |
| 48 | struct log_level { |
| 49 | LOG_LEVEL log_level; |
| 50 | const char *name; |
| 51 | const char *log_string; |
| 52 | const char *colored_log_string; |
| 53 | }; |
| 54 | |
| 55 | static struct log_level log_level_array[] = { |
| 56 | { LOG_LEVEL_ERROR, "error", " ERROR", COLOR_B_RED " ERROR" COLOR_RESET }, |
| 57 | { LOG_LEVEL_WARNING, "warning", "WARNING", COLOR_B_YELLOW "WARNING" COLOR_RESET }, |
| 58 | { LOG_LEVEL_NOTICE, "notice", " NOTICE", COLOR_L_ORANGE " NOTICE" COLOR_RESET }, |
| 59 | { LOG_LEVEL_INFO, "info", " INFO", COLOR_L_GREEN " INFO" COLOR_RESET }, |
| 60 | { LOG_LEVEL_COMPARE, "compare", "COMPARE", COLOR_B_BLUE "COMPARE" COLOR_RESET }, |
| 61 | { LOG_LEVEL_RULE, "rule", " RULE", COLOR_L_BLUE " RULE" COLOR_RESET }, |
| 62 | { LOG_LEVEL_CONFIG, "config", " CONFIG", COLOR_L_CYAN " CONFIG" COLOR_RESET }, |
| 63 | { LOG_LEVEL_DEBUG, "debug", " DEBUG", COLOR_B_PURPLE " DEBUG" COLOR_RESET }, |
| 64 | { LOG_LEVEL_LIMIT, "limit", " LIMIT", COLOR_L_GRAY " LIMIT" COLOR_RESET }, |
| 65 | { LOG_LEVEL_THREAD, "thread", " THREAD", COLOR_B_CYAN " THREAD" COLOR_RESET }, |
| 66 | { LOG_LEVEL_TRACE, "trace", " TRACE", COLOR_L_PURPLE " TRACE" COLOR_RESET }, |
| 67 | { 0, NULL, NULL , NULL }, |
| 68 | }; |
| 69 | |
| 70 | static const char* get_log_string(LOG_LEVEL level) { |
| 71 | if (colored_log) { |
| 72 | return log_level_array[level-1].colored_log_string; |
| 73 | } else { |
| 74 | return log_level_array[level-1].log_string; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static void cache_line(LOG_LEVEL, const char*, va_list) |
| 79 | #ifdef __GNUC__ |
| 80 | __attribute__ ((format (printf, 2, 0))) |
| 81 | #endif |
| 82 | ; |
| 83 | static void cache_line(LOG_LEVEL level, const char* format, va_list ap) { |
| 84 | int n; |
| 85 | |
| 86 | cached_lines = realloc(cached_lines, (ncachedlines+1) * sizeof(log_cache)); /* freed in log_cached_lines() */ |
| 87 | if (cached_lines == NULL) { |
| 88 | stderr_msg("%s: realloc: failed to allocate memory\n", get_log_string(LOG_LEVEL_ERROR)); |
| 89 | exit(MEMORY_ALLOCATION_FAILURE); |
| 90 | } |
| 91 | |
| 92 | cached_lines[ncachedlines].level = level; |
| 93 | cached_lines[ncachedlines].message = NULL; |
| 94 | |
| 95 | va_list aq; |
| 96 | va_copy(aq, ap); |
| 97 | n = vsnprintf(NULL, 0, format, aq) + 1; |
| 98 | va_end(aq); |
| 99 | |
| 100 | int size = n * sizeof(char); |
| 101 | cached_lines[ncachedlines].message = malloc(size); /* freed in log_cached_lines() */ |
| 102 | if (cached_lines[ncachedlines].message == NULL) { |
| 103 | stderr_msg("%s: malloc: failed to allocate %d bytes of memory\n", get_log_string(LOG_LEVEL_ERROR), size); |
| 104 | exit(MEMORY_ALLOCATION_FAILURE); |
| 105 | } |
| 106 | |
| 107 | vsnprintf(cached_lines[ncachedlines].message, n, format, ap); |
| 108 | ncachedlines++; |
| 109 | } |
| 110 | |
| 111 | const char * get_log_level_name(LOG_LEVEL level) { |
| 112 | return level?log_level_array[level-1].name:NULL; |
| 113 | } |
| 114 | |
| 115 | static void log_cached_lines(void) { |
| 116 | pthread_mutex_lock(&log_mutex); |
| 117 | for(int i = 0; i < ncachedlines; ++i) { |
| 118 | LOG_LEVEL level = cached_lines[i].level; |
| 119 | if (level == LOG_LEVEL_ERROR || level <= log_level) { |
| 120 | char *msg_safe = stresc(cached_lines[i].message); |
| 121 | stderr_msg("%s: %s\n", get_log_string(level), msg_safe); |
| 122 | free(msg_safe); |
| 123 | } |
| 124 | free(cached_lines[i].message); |
| 125 | } |
| 126 | ncachedlines = 0; |
| 127 | free(cached_lines); |
| 128 | pthread_mutex_unlock(&log_mutex); |
| 129 | } |
| 130 | |
| 131 | static void vlog_msg(LOG_LEVEL, const char*, va_list) |
| 132 | #ifdef __GNUC__ |
| 133 | __attribute__ ((format (printf, 2, 0))) |
| 134 | #endif |
| 135 | ; |
| 136 | static void vlog_msg(LOG_LEVEL level,const char* format, va_list ap) { |
| 137 | if (level != LOG_LEVEL_ERROR && (log_level == LOG_LEVEL_UNSET || colored_log < 0)) { |
| 138 | pthread_mutex_lock(&log_mutex); |
| 139 | cache_line(level, format, ap); |
| 140 | pthread_mutex_unlock(&log_mutex); |
| 141 | } else if (level == LOG_LEVEL_ERROR || level <= log_level) { |
| 142 | va_list aq; |
| 143 | va_copy(aq, ap); |
| 144 | int n = vsnprintf(NULL, 0, format, aq) + 1; |
| 145 | va_end(aq); |
| 146 | |
| 147 | int size = n * sizeof(char); |
| 148 | char *msg_unsafe = malloc(size); |
| 149 | if (msg_unsafe == NULL) { |
| 150 | stderr_msg("%s: malloc: failed to allocate %d bytes of memory\n", get_log_string(LOG_LEVEL_ERROR), size); |
| 151 | exit(MEMORY_ALLOCATION_FAILURE); |
| 152 | } |
| 153 | |
| 154 | vsnprintf(msg_unsafe, n, format, ap); |
| 155 | char *msg_safe = stresc(msg_unsafe); |
| 156 | free(msg_unsafe); |
| 157 | stderr_msg("%s: %s\n", get_log_string(level), msg_safe); |
| 158 | free(msg_safe); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | bool is_log_level_unset(void) { |
| 163 | return log_level == LOG_LEVEL_UNSET; |
| 164 | } |
| 165 | |
| 166 | LOG_LEVEL get_log_level_from_string(char* val) { |
| 167 | struct log_level *level; |
| 168 | |
| 169 | for (level = log_level_array; level->log_level != 0; level++) { |
| 170 | if (strcmp(val, level->name) == 0) { |
| 171 | return level->log_level; |
| 172 | } |
| 173 | } |
| 174 | return LOG_LEVEL_UNSET; |
| 175 | } |
| 176 | |
| 177 | void set_colored_log(bool color) { |
| 178 | colored_log = color; |
| 179 | if (ncachedlines && log_level != LOG_LEVEL_UNSET) { |
| 180 | log_cached_lines(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void set_log_level(LOG_LEVEL level) { |
| 185 | log_level = level; |
| 186 | if (colored_log >= 0 && ncachedlines && log_level != LOG_LEVEL_UNSET) { |
| 187 | log_cached_lines(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | LOG_LEVEL toogle_log_level(LOG_LEVEL level) { |
| 192 | if (prev_log_level != LOG_LEVEL_UNSET && log_level != level) { |
| 193 | set_log_level(level); |
| 194 | } else if (log_level != level || prev_log_level != LOG_LEVEL_UNSET) { |
| 195 | if (prev_log_level == LOG_LEVEL_UNSET) { |
| 196 | prev_log_level = log_level; |
| 197 | set_log_level(level); |
| 198 | } else { |
| 199 | set_log_level(prev_log_level); |
| 200 | prev_log_level = LOG_LEVEL_UNSET; |
| 201 | } |
| 202 | } |
| 203 | return log_level; |
| 204 | } |
| 205 | |
| 206 | void log_msg(LOG_LEVEL level, const char* format, ...) { |
| 207 | va_list argp; |
| 208 | va_start(argp, format); |
| 209 | vlog_msg(level, format, argp); |
| 210 | va_end(argp); |
| 211 | } |