/src/suricata8/src/util-debug.c
Line | Count | Source |
1 | | /* Copyright (C) 2007-2021 Open Information Security Foundation |
2 | | * |
3 | | * You can copy, redistribute or modify this Program under the terms of |
4 | | * the GNU General Public License version 2 as published by the Free |
5 | | * Software Foundation. |
6 | | * |
7 | | * This program is distributed in the hope that it will be useful, |
8 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | | * GNU General Public License for more details. |
11 | | * |
12 | | * You should have received a copy of the GNU General Public License |
13 | | * version 2 along with this program; if not, write to the Free Software |
14 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
15 | | * 02110-1301, USA. |
16 | | */ |
17 | | |
18 | | /** |
19 | | * \file |
20 | | * |
21 | | * \author Anoop Saldanha <anoopsaldanha@gmail.com> |
22 | | * |
23 | | * Debug utility functions |
24 | | */ |
25 | | |
26 | | #include "suricata-common.h" |
27 | | #include "util-debug.h" |
28 | | |
29 | | #include "output.h" |
30 | | |
31 | | #include "suricata.h" |
32 | | |
33 | | #include "util-conf.h" |
34 | | #include "util-enum.h" |
35 | | #include "util-path.h" |
36 | | #include "util-syslog.h" |
37 | | #include "util-time.h" |
38 | | |
39 | | // clang-format off |
40 | | /* holds the string-enum mapping for the enums held in the table SCLogLevel */ |
41 | | SCEnumCharMap sc_log_level_map[] = { |
42 | | { "Not set", SC_LOG_NOTSET }, |
43 | | { "None", SC_LOG_NONE }, |
44 | | { "Error", SC_LOG_ERROR }, |
45 | | { "Warning", SC_LOG_WARNING }, |
46 | | { "Notice", SC_LOG_NOTICE }, |
47 | | { "Info", SC_LOG_INFO }, |
48 | | { "Perf", SC_LOG_PERF }, |
49 | | { "Config", SC_LOG_CONFIG }, |
50 | | { "Debug", SC_LOG_DEBUG }, |
51 | | { NULL, -1 } |
52 | | }; |
53 | | |
54 | | SCEnumCharMap sc_log_slevel_map[] = { |
55 | | { "Not set", SC_LOG_NOTSET }, |
56 | | { "None", SC_LOG_NONE }, |
57 | | { "E", SC_LOG_ERROR }, |
58 | | { "W", SC_LOG_WARNING }, |
59 | | { "i", SC_LOG_NOTICE }, |
60 | | { "i", SC_LOG_INFO }, |
61 | | { "i", SC_LOG_PERF }, |
62 | | { "i", SC_LOG_CONFIG }, |
63 | | { "d", SC_LOG_DEBUG }, |
64 | | { NULL, -1 } |
65 | | }; |
66 | | |
67 | | /* holds the string-enum mapping for the enums held in the table SCLogOPIface */ |
68 | | SCEnumCharMap sc_log_op_iface_map[ ] = { |
69 | | { "Console", SC_LOG_OP_IFACE_CONSOLE }, |
70 | | { "File", SC_LOG_OP_IFACE_FILE }, |
71 | | { "Syslog", SC_LOG_OP_IFACE_SYSLOG }, |
72 | | { NULL, -1 } |
73 | | }; |
74 | | // clang-format on |
75 | | |
76 | | #if defined (OS_WIN32) |
77 | | /** |
78 | | * \brief Used for synchronous output on WIN32 |
79 | | */ |
80 | | static SCMutex sc_log_stream_lock; |
81 | | #endif /* OS_WIN32 */ |
82 | | |
83 | | /** |
84 | | * \brief Transform the module name into display module name for logging |
85 | | */ |
86 | | static const char *SCTransformModule(const char *module_name, int *dn_len); |
87 | | |
88 | | /** |
89 | | * \brief Holds the config state for the logging module |
90 | | */ |
91 | | static SCLogConfig *sc_log_config = NULL; |
92 | | |
93 | | /** |
94 | | * \brief Returns the full path given a file and configured log dir |
95 | | */ |
96 | | static char *SCLogGetLogFilename(const char *); |
97 | | |
98 | | /** |
99 | | * \brief Holds the global log level. Is the same as sc_log_config->log_level |
100 | | */ |
101 | | SCLogLevel sc_log_global_log_level; |
102 | | |
103 | | /** |
104 | | * \brief Used to indicate whether the logging module has been init or not |
105 | | */ |
106 | | int sc_log_module_initialized = 0; |
107 | | |
108 | | /** |
109 | | * \brief Used to indicate whether the logging module has been cleaned or not |
110 | | */ |
111 | | int sc_log_module_cleaned = 0; |
112 | | |
113 | | /** |
114 | | * \brief Maps the SC logging level to the syslog logging level |
115 | | * |
116 | | * \param The SC logging level that has to be mapped to the syslog_log_level |
117 | | * |
118 | | * \retval syslog_log_level The mapped syslog_api_log_level, for the logging |
119 | | * module api's internal log_level |
120 | | */ |
121 | | static inline int SCLogMapLogLevelToSyslogLevel(int log_level) |
122 | 0 | { |
123 | 0 | int syslog_log_level = 0; |
124 | |
|
125 | 0 | switch (log_level) { |
126 | 0 | case SC_LOG_ERROR: |
127 | 0 | syslog_log_level = LOG_ERR; |
128 | 0 | break; |
129 | 0 | case SC_LOG_WARNING: |
130 | 0 | syslog_log_level = LOG_WARNING; |
131 | 0 | break; |
132 | 0 | case SC_LOG_NOTICE: |
133 | 0 | syslog_log_level = LOG_NOTICE; |
134 | 0 | break; |
135 | 0 | case SC_LOG_INFO: |
136 | 0 | syslog_log_level = LOG_INFO; |
137 | 0 | break; |
138 | 0 | case SC_LOG_CONFIG: |
139 | 0 | case SC_LOG_DEBUG: |
140 | 0 | case SC_LOG_PERF: |
141 | 0 | syslog_log_level = LOG_DEBUG; |
142 | 0 | break; |
143 | 0 | default: |
144 | 0 | syslog_log_level = LOG_EMERG; |
145 | 0 | break; |
146 | 0 | } |
147 | | |
148 | 0 | return syslog_log_level; |
149 | 0 | } |
150 | | |
151 | | /** |
152 | | * \brief Output function that logs a character string out to a file descriptor |
153 | | * |
154 | | * \param fd Pointer to the file descriptor |
155 | | * \param msg Pointer to the character string that should be logged |
156 | | */ |
157 | | static inline void SCLogPrintToStream(FILE *fd, char *msg) |
158 | 11.5M | { |
159 | | /* Would only happen if the log file failed to re-open during rotation. */ |
160 | 11.5M | if (fd == NULL) { |
161 | 0 | return; |
162 | 0 | } |
163 | | |
164 | | #if defined (OS_WIN32) |
165 | | SCMutexLock(&sc_log_stream_lock); |
166 | | #endif /* OS_WIN32 */ |
167 | | |
168 | 11.5M | if (fprintf(fd, "%s\n", msg) < 0) |
169 | 0 | printf("Error writing to stream using fprintf\n"); |
170 | | |
171 | 11.5M | fflush(fd); |
172 | | |
173 | | #if defined (OS_WIN32) |
174 | | SCMutexUnlock(&sc_log_stream_lock); |
175 | | #endif /* OS_WIN32 */ |
176 | 11.5M | } |
177 | | |
178 | | /** |
179 | | * \brief Output function that logs a character string through the syslog iface |
180 | | * |
181 | | * \param syslog_log_level Holds the syslog_log_level that the message should be |
182 | | * logged as |
183 | | * \param msg Pointer to the char string, that should be logged |
184 | | * |
185 | | * \todo syslog is thread-safe according to POSIX manual and glibc code, but we |
186 | | * we will have to look into non POSIX compliant boxes like freeBSD |
187 | | */ |
188 | | static inline void SCLogPrintToSyslog(int syslog_log_level, const char *msg) |
189 | 0 | { |
190 | | //static struct syslog_data data = SYSLOG_DATA_INIT; |
191 | | //syslog_r(syslog_log_level, NULL, "%s", msg); |
192 | |
|
193 | 0 | syslog(syslog_log_level, "%s", msg); |
194 | 0 | } |
195 | | |
196 | | /** |
197 | | */ |
198 | | static int SCLogMessageJSON(SCTime_t tval, char *buffer, size_t buffer_size, SCLogLevel log_level, |
199 | | const char *file, unsigned line, const char *function, const char *module, |
200 | | const char *message) |
201 | 0 | { |
202 | 0 | SCJsonBuilder *js = SCJbNewObject(); |
203 | 0 | if (unlikely(js == NULL)) |
204 | 0 | goto error; |
205 | | |
206 | 0 | char timebuf[64]; |
207 | 0 | CreateIsoTimeString(tval, timebuf, sizeof(timebuf)); |
208 | 0 | SCJbSetString(js, "timestamp", timebuf); |
209 | |
|
210 | 0 | const char *s = SCMapEnumValueToName(log_level, sc_log_level_map); |
211 | 0 | if (s != NULL) { |
212 | 0 | SCJbSetString(js, "log_level", s); |
213 | 0 | } else { |
214 | 0 | JB_SET_STRING(js, "log_level", "INVALID"); |
215 | 0 | } |
216 | |
|
217 | 0 | JB_SET_STRING(js, "event_type", "engine"); |
218 | 0 | SCJbOpenObject(js, "engine"); |
219 | |
|
220 | 0 | if (message) |
221 | 0 | SCJbSetString(js, "message", message); |
222 | |
|
223 | 0 | if (t_thread_name[0] != '\0') { |
224 | 0 | SCJbSetString(js, "thread_name", t_thread_name); |
225 | 0 | } |
226 | |
|
227 | 0 | if (module) { |
228 | | /* Determine how much of module name to display */ |
229 | 0 | int dn_len = 0; |
230 | 0 | const char *dn_name; |
231 | 0 | dn_name = SCTransformModule(module, &dn_len); |
232 | 0 | SCJbSetString(js, "module", dn_name); |
233 | 0 | } |
234 | |
|
235 | 0 | if (log_level >= SC_LOG_DEBUG) { |
236 | 0 | if (function) |
237 | 0 | SCJbSetString(js, "function", function); |
238 | |
|
239 | 0 | if (file) |
240 | 0 | SCJbSetString(js, "file", file); |
241 | |
|
242 | 0 | if (line > 0) |
243 | 0 | SCJbSetUint(js, "line", line); |
244 | 0 | } |
245 | 0 | SCJbClose(js); // engine |
246 | |
|
247 | 0 | SCJbClose(js); |
248 | 0 | memcpy(buffer, SCJbPtr(js), MIN(buffer_size, SCJbLen(js))); |
249 | |
|
250 | 0 | SCJbFree(js); |
251 | |
|
252 | 0 | return 0; |
253 | | |
254 | 0 | error: |
255 | 0 | return -1; |
256 | 0 | } |
257 | | |
258 | | static const int transform_max_segs = 2; /* The maximum segment count to display */ |
259 | | /* |
260 | | * \brief Return a display name for the given module name for logging. |
261 | | * |
262 | | * The transformation is dependent upon the source code module names |
263 | | * that use the dash character to separate incremental refinements of |
264 | | * the subsystem. |
265 | | * |
266 | | * The transformation uses the local constant "transform_max_segs" to determine |
267 | | * how many segments to display; the transformed name will never consist |
268 | | * of more than this many segments. |
269 | | * |
270 | | * E.g., "detect-http-content-len" ==> "detect-http" when the max is 2 |
271 | | * |
272 | | * \param module_name The source code module name to be transformed. |
273 | | * \param dn_len The number of characters in the display name to print. |
274 | | * |
275 | | * \retval Pointer to the display name |
276 | | */ |
277 | | static const char *SCTransformModule(const char *module_name, int *dn_len) |
278 | 11.5M | { |
279 | | /* |
280 | | * special case for source code module names beginning with: |
281 | | * Prefixes skipped |
282 | | * tm-* |
283 | | * util-* |
284 | | * source-* |
285 | | * No transformation |
286 | | * app-layer-* |
287 | | */ |
288 | 11.5M | if (strncmp("tm-", module_name, 3) == 0) { |
289 | 0 | *dn_len = (int)strlen(module_name) - 3; |
290 | 0 | return module_name + 3; |
291 | 11.5M | } else if (strncmp("util-", module_name, 5) == 0) { |
292 | 617k | *dn_len = (int)strlen(module_name) - 5; |
293 | 617k | return module_name + 5; |
294 | 10.9M | } else if (strncmp("source-pcap-file", module_name, 16) == 0) { |
295 | 36.5k | *dn_len = (int)strlen("pcap"); |
296 | 36.5k | return "pcap"; |
297 | 10.8M | } else if (strncmp("source-", module_name, 7) == 0) { |
298 | 0 | *dn_len = (int)strlen(module_name) - 7; |
299 | 0 | return module_name + 7; |
300 | 10.8M | } else if (strncmp("runmode-", module_name, 8) == 0) { |
301 | 0 | *dn_len = (int)strlen(module_name) - 8; |
302 | 0 | return module_name + 8; |
303 | 10.8M | } else if (strncmp("app-layer-", module_name, 10) == 0) { |
304 | 249k | *dn_len = (int)strlen(module_name); |
305 | 249k | return module_name; |
306 | 10.6M | } else if (strncmp("detect-engine", module_name, 13) == 0) { |
307 | 5.25M | *dn_len = (int)strlen("detect"); |
308 | 5.25M | return "detect"; |
309 | 5.25M | } |
310 | | |
311 | 5.39M | int seg_cnt = 0; |
312 | | |
313 | 5.39M | char *last; |
314 | 5.39M | char *w = (char *)module_name; |
315 | 10.8M | while (w && (w = strchr(w, '-')) != NULL && seg_cnt < transform_max_segs) { |
316 | 5.49M | seg_cnt++; |
317 | 5.49M | last = w; |
318 | 5.49M | w++; /* skip past '-' */ |
319 | 5.49M | } |
320 | | |
321 | 5.39M | if (seg_cnt < transform_max_segs) |
322 | 4.84M | *dn_len = (int)strlen(module_name); |
323 | 555k | else |
324 | 555k | *dn_len = (int)(last - module_name); |
325 | | |
326 | 5.39M | return module_name; |
327 | 11.5M | } |
328 | | |
329 | | /** |
330 | | * \brief Adds the global log_format to the outgoing buffer |
331 | | * |
332 | | * \param log_level log_level of the message that has to be logged |
333 | | * \param msg Buffer containing the outgoing message |
334 | | * \param file File_name from where the message originated |
335 | | * \param function Function_name from where the message originated |
336 | | * \param line Line_no from where the messaged originated |
337 | | * |
338 | | * \retval 0 on success; else a negative value on error |
339 | | */ |
340 | | static SCError SCLogMessageGetBuffer(SCTime_t tval, bool color, SCLogOPType type, char *buffer, |
341 | | size_t buffer_size, const char *log_format, const SCLogLevel log_level, const char *file, |
342 | | const unsigned int line, const char *function, const char *module, const char *message) |
343 | 1.66M | { |
344 | 1.66M | if (type == SC_LOG_OP_TYPE_JSON) |
345 | 0 | return SCLogMessageJSON( |
346 | 0 | tval, buffer, buffer_size, log_level, file, line, function, module, message); |
347 | | |
348 | 1.66M | char *temp = buffer; |
349 | 1.66M | const char *s = NULL; |
350 | 1.66M | struct tm *tms = NULL; |
351 | | |
352 | 1.66M | const char *redb = ""; |
353 | 1.66M | const char *red = ""; |
354 | 1.66M | const char *yellowb = ""; |
355 | 1.66M | const char *yellow = ""; |
356 | 1.66M | const char *green = ""; |
357 | 1.66M | const char *blue = ""; |
358 | 1.66M | const char *reset = ""; |
359 | 1.66M | if (color) { |
360 | 0 | redb = "\x1b[1;31m"; |
361 | 0 | red = "\x1b[31m"; |
362 | 0 | yellowb = "\x1b[1;33m"; |
363 | 0 | yellow = "\x1b[33m"; |
364 | 0 | green = "\x1b[32m"; |
365 | 0 | blue = "\x1b[34m"; |
366 | 0 | reset = "\x1b[0m"; |
367 | 0 | } |
368 | | /* no of characters_written(cw) by snprintf */ |
369 | 1.66M | int cw = 0; |
370 | | |
371 | 1.66M | DEBUG_VALIDATE_BUG_ON(sc_log_module_initialized != 1); |
372 | | |
373 | | /* make a copy of the format string as it will be modified below */ |
374 | 1.66M | const int add_M = strstr(log_format, "%M") == NULL; |
375 | 1.66M | char local_format[strlen(log_format) + add_M * 2 + 1]; |
376 | 1.66M | strlcpy(local_format, log_format, sizeof(local_format)); |
377 | 1.66M | if (add_M) |
378 | 0 | strlcat(local_format, "%M", sizeof(local_format)); |
379 | 1.66M | char *temp_fmt = local_format; |
380 | 1.66M | char *substr = temp_fmt; |
381 | 1.66M | struct tm local_tm; |
382 | | |
383 | 11.6M | while ((temp_fmt = strchr(temp_fmt, SC_LOG_FMT_PREFIX))) { |
384 | 9.96M | if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { |
385 | 3 | return 0; |
386 | 3 | } |
387 | 9.96M | switch(temp_fmt[1]) { |
388 | 0 | case SC_LOG_FMT_TIME: |
389 | 0 | temp_fmt[0] = '\0'; |
390 | |
|
391 | 0 | tms = SCLocalTime(SCTIME_SECS(tval), &local_tm); |
392 | |
|
393 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
394 | 0 | "%s%s%04d-%02d-%02d %02d:%02d:%02d%s", substr, green, tms->tm_year + 1900, |
395 | 0 | tms->tm_mon + 1, tms->tm_mday, tms->tm_hour, tms->tm_min, tms->tm_sec, |
396 | 0 | reset); |
397 | 0 | if (cw < 0) |
398 | 0 | return -1; |
399 | 0 | temp += cw; |
400 | 0 | temp_fmt++; |
401 | 0 | substr = temp_fmt; |
402 | 0 | substr++; |
403 | 0 | break; |
404 | | |
405 | 0 | case SC_LOG_FMT_TIME_LEGACY: |
406 | 0 | temp_fmt[0] = '\0'; |
407 | |
|
408 | 0 | tms = SCLocalTime(SCTIME_SECS(tval), &local_tm); |
409 | |
|
410 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
411 | 0 | "%s%s%d/%d/%04d -- %02d:%02d:%02d%s", |
412 | 0 | substr, green, tms->tm_mday, tms->tm_mon + 1, |
413 | 0 | tms->tm_year + 1900, tms->tm_hour, tms->tm_min, |
414 | 0 | tms->tm_sec, reset); |
415 | 0 | if (cw < 0) |
416 | 0 | return -1; |
417 | 0 | temp += cw; |
418 | 0 | temp_fmt++; |
419 | 0 | substr = temp_fmt; |
420 | 0 | substr++; |
421 | 0 | break; |
422 | | |
423 | 0 | case SC_LOG_FMT_PID: |
424 | 0 | temp_fmt[0] = '\0'; |
425 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
426 | 0 | "%s%s%u%s", substr, yellow, getpid(), reset); |
427 | 0 | if (cw < 0) |
428 | 0 | return -1; |
429 | 0 | temp += cw; |
430 | 0 | temp_fmt++; |
431 | 0 | substr = temp_fmt; |
432 | 0 | substr++; |
433 | 0 | break; |
434 | | |
435 | 0 | case SC_LOG_FMT_TID: |
436 | 0 | temp_fmt[0] = '\0'; |
437 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
438 | 0 | "%s%s%lu%s", substr, yellow, SCGetThreadIdLong(), reset); |
439 | 0 | if (cw < 0) |
440 | 0 | return -1; |
441 | 0 | temp += cw; |
442 | 0 | temp_fmt++; |
443 | 0 | substr = temp_fmt; |
444 | 0 | substr++; |
445 | 0 | break; |
446 | | |
447 | 0 | case SC_LOG_FMT_THREAD_NAME: |
448 | 0 | case SC_LOG_FMT_TM: |
449 | 0 | temp_fmt[0] = '\0'; |
450 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s%s%s", substr, |
451 | 0 | yellow, t_thread_name, reset); |
452 | 0 | if (cw < 0) |
453 | 0 | return -1; |
454 | 0 | temp += cw; |
455 | 0 | temp_fmt++; |
456 | 0 | substr = temp_fmt; |
457 | 0 | substr++; |
458 | 0 | break; |
459 | | |
460 | 1.66M | case SC_LOG_FMT_LOG_LEVEL: |
461 | 1.66M | temp_fmt[0] = '\0'; |
462 | 1.66M | s = SCMapEnumValueToName(log_level, sc_log_level_map); |
463 | 1.66M | if (s != NULL) { |
464 | 1.66M | if (log_level <= SC_LOG_ERROR) |
465 | 1.36M | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
466 | 1.36M | "%s%s%s%s", substr, redb, s, reset); |
467 | 296k | else if (log_level == SC_LOG_WARNING) |
468 | 72.3k | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
469 | 72.3k | "%s%s%s%s", substr, red, s, reset); |
470 | 224k | else if (log_level == SC_LOG_NOTICE) |
471 | 36.3k | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
472 | 36.3k | "%s%s%s%s", substr, yellowb, s, reset); |
473 | 187k | else |
474 | 187k | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s%s%s", |
475 | 187k | substr, yellow, s, reset); |
476 | 1.66M | } else { |
477 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s", substr, |
478 | 0 | "INVALID"); |
479 | 0 | } |
480 | 1.66M | if (cw < 0) |
481 | 0 | return -1; |
482 | 1.66M | temp += cw; |
483 | 1.66M | temp_fmt++; |
484 | 1.66M | substr = temp_fmt; |
485 | 1.66M | substr++; |
486 | 1.66M | break; |
487 | | |
488 | 0 | case SC_LOG_FMT_LOG_SLEVEL: |
489 | 0 | temp_fmt[0] = '\0'; |
490 | 0 | s = SCMapEnumValueToName(log_level, sc_log_slevel_map); |
491 | 0 | if (s != NULL) { |
492 | 0 | if (log_level <= SC_LOG_ERROR) |
493 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s%s%s", |
494 | 0 | substr, redb, s, reset); |
495 | 0 | else if (log_level == SC_LOG_WARNING) |
496 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s%s%s", |
497 | 0 | substr, red, s, reset); |
498 | 0 | else if (log_level == SC_LOG_NOTICE) |
499 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s%s%s", |
500 | 0 | substr, yellowb, s, reset); |
501 | 0 | else |
502 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
503 | 0 | "%s%s%s%s", substr, yellow, s, reset); |
504 | 0 | } else { |
505 | 0 | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
506 | 0 | "%s%s", substr, "INVALID"); |
507 | 0 | } |
508 | 0 | if (cw < 0) |
509 | 0 | return -1; |
510 | 0 | temp += cw; |
511 | 0 | temp_fmt++; |
512 | 0 | substr = temp_fmt; |
513 | 0 | substr++; |
514 | 0 | break; |
515 | | |
516 | 1.66M | case SC_LOG_FMT_FILE_NAME: |
517 | 1.66M | temp_fmt[0] = '\0'; |
518 | 1.66M | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
519 | 1.66M | "%s%s%s%s", substr, blue, file, reset); |
520 | 1.66M | if (cw < 0) |
521 | 0 | return -1; |
522 | 1.66M | temp += cw; |
523 | 1.66M | temp_fmt++; |
524 | 1.66M | substr = temp_fmt; |
525 | 1.66M | substr++; |
526 | 1.66M | break; |
527 | | |
528 | 1.66M | case SC_LOG_FMT_LINE: |
529 | 1.66M | temp_fmt[0] = '\0'; |
530 | 1.66M | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
531 | 1.66M | "%s%s%u%s", substr, green, line, reset); |
532 | 1.66M | if (cw < 0) |
533 | 0 | return -1; |
534 | 1.66M | temp += cw; |
535 | 1.66M | temp_fmt++; |
536 | 1.66M | substr = temp_fmt; |
537 | 1.66M | substr++; |
538 | 1.66M | break; |
539 | | |
540 | 1.66M | case SC_LOG_FMT_SUBSYSTEM: |
541 | 1.66M | temp_fmt[0] = '\0'; |
542 | | |
543 | | /* Determine how much of module name to display */ |
544 | 1.66M | int dn_len = 0; |
545 | 1.66M | const char *dn_name = "unknown"; |
546 | 1.66M | if (module) { |
547 | 1.66M | dn_name = SCTransformModule(module, &dn_len); |
548 | 1.66M | } |
549 | | |
550 | 1.66M | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s%s%s", substr, |
551 | 1.66M | green, dn_name, reset); |
552 | 1.66M | if (cw < 0) |
553 | 0 | return -1; |
554 | 1.66M | temp += cw; |
555 | 1.66M | temp_fmt++; |
556 | 1.66M | substr = temp_fmt; |
557 | 1.66M | substr++; |
558 | 1.66M | break; |
559 | | |
560 | 1.66M | case SC_LOG_FMT_FUNCTION: |
561 | 1.66M | temp_fmt[0] = '\0'; |
562 | 1.66M | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), |
563 | 1.66M | "%s%s%s%s", substr, green, function, reset); |
564 | 1.66M | if (cw < 0) |
565 | 0 | return -1; |
566 | 1.66M | temp += cw; |
567 | 1.66M | temp_fmt++; |
568 | 1.66M | substr = temp_fmt; |
569 | 1.66M | substr++; |
570 | 1.66M | break; |
571 | | |
572 | 1.66M | case SC_LOG_FMT_MESSAGE: { |
573 | 1.66M | temp_fmt[0] = '\0'; |
574 | 1.66M | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s", substr); |
575 | 1.66M | if (cw < 0) { |
576 | 0 | return -1; |
577 | 0 | } |
578 | 1.66M | temp += cw; |
579 | 1.66M | if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { |
580 | 0 | return 0; |
581 | 0 | } |
582 | 1.66M | const char *hi = ""; |
583 | 1.66M | if (log_level <= SC_LOG_ERROR) |
584 | 1.36M | hi = red; |
585 | 296k | else if (log_level <= SC_LOG_NOTICE) |
586 | 108k | hi = yellow; |
587 | 1.66M | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s%s", hi, message, |
588 | 1.66M | reset); |
589 | 1.66M | if (cw < 0) { |
590 | 0 | return -1; |
591 | 0 | } |
592 | 1.66M | temp += cw; |
593 | 1.66M | if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { |
594 | 52 | return 0; |
595 | 52 | } |
596 | 1.66M | temp_fmt++; |
597 | 1.66M | substr = temp_fmt; |
598 | 1.66M | substr++; |
599 | 1.66M | break; |
600 | 1.66M | } |
601 | 9.96M | } |
602 | 9.96M | temp_fmt++; |
603 | 9.96M | } |
604 | 1.66M | if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { |
605 | 15 | return 0; |
606 | 15 | } |
607 | 1.66M | cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s", substr); |
608 | 1.66M | if (cw < 0) { |
609 | 0 | return -1; |
610 | 0 | } |
611 | 1.66M | if (sc_log_config->op_filter_regex != NULL) { |
612 | 0 | if (pcre2_match(sc_log_config->op_filter_regex, (PCRE2_SPTR8)buffer, strlen(buffer), 0, 0, |
613 | 0 | sc_log_config->op_filter_regex_match, NULL) < 0) { |
614 | 0 | return -1; // bit hacky, but just return !0 |
615 | 0 | } |
616 | 0 | } |
617 | | |
618 | 1.66M | return 0; |
619 | 1.66M | } |
620 | | |
621 | | /** \internal |
622 | | * \brief try to reopen file |
623 | | * \note no error reporting here, as we're called by SCLogMessage |
624 | | * \retval status 0 ok, -1 error */ |
625 | | static int SCLogReopen(SCLogOPIfaceCtx *op_iface_ctx) |
626 | 0 | { |
627 | 0 | if (op_iface_ctx->file == NULL) { |
628 | 0 | return 0; |
629 | 0 | } |
630 | | |
631 | 0 | if (op_iface_ctx->file_d != NULL) { |
632 | 0 | fclose(op_iface_ctx->file_d); |
633 | 0 | } |
634 | 0 | op_iface_ctx->file_d = fopen(op_iface_ctx->file, "a"); |
635 | 0 | if (op_iface_ctx->file_d == NULL) { |
636 | 0 | return -1; |
637 | 0 | } |
638 | 0 | return 0; |
639 | 0 | } |
640 | | |
641 | | /** |
642 | | * \brief Adds the global log_format to the outgoing buffer |
643 | | * |
644 | | * \param log_level log_level of the message that has to be logged |
645 | | * \param msg Buffer containing the outgoing message |
646 | | * \param file File_name from where the message originated |
647 | | * \param function Function_name from where the message originated |
648 | | * \param line Line_no from where the messaged originated |
649 | | * |
650 | | * \retval SC_OK on success; else an error code |
651 | | */ |
652 | | SCError SCLogMessage(const SCLogLevel log_level, const char *file, const unsigned int line, |
653 | | const char *function, const char *module, const char *message) |
654 | 11.5M | { |
655 | 11.5M | char buffer[SC_LOG_MAX_LOG_MSG_LEN] = ""; |
656 | 11.5M | SCLogOPIfaceCtx *op_iface_ctx = NULL; |
657 | | |
658 | 11.5M | if (sc_log_module_initialized != 1) { |
659 | 0 | printf("Logging module not initialized. Call SCLogInitLogModule() " |
660 | 0 | "first before using the debug API\n"); |
661 | 0 | return SC_OK; |
662 | 0 | } |
663 | | |
664 | | /* get ts here so we log the same ts to each output */ |
665 | 11.5M | struct timeval tval; |
666 | 11.5M | gettimeofday(&tval, NULL); |
667 | 11.5M | SCTime_t ts = SCTIME_FROM_TIMEVAL(&tval); |
668 | | |
669 | 11.5M | op_iface_ctx = sc_log_config->op_ifaces; |
670 | 23.1M | while (op_iface_ctx != NULL) { |
671 | 11.5M | if (log_level != SC_LOG_NOTSET && log_level > op_iface_ctx->log_level) { |
672 | 0 | op_iface_ctx = op_iface_ctx->next; |
673 | 0 | continue; |
674 | 0 | } |
675 | | |
676 | 11.5M | switch (op_iface_ctx->iface) { |
677 | 6 | case SC_LOG_OP_IFACE_CONSOLE: |
678 | 6 | if (SCLogMessageGetBuffer(ts, op_iface_ctx->use_color, op_iface_ctx->type, buffer, |
679 | 6 | sizeof(buffer), |
680 | 6 | op_iface_ctx->log_format ? op_iface_ctx->log_format |
681 | 6 | : sc_log_config->log_format, |
682 | 6 | log_level, file, line, function, module, message) == 0) { |
683 | 6 | SCLogPrintToStream((log_level == SC_LOG_ERROR)? stderr: stdout, buffer); |
684 | 6 | } |
685 | 6 | break; |
686 | 11.5M | case SC_LOG_OP_IFACE_FILE: |
687 | 11.5M | if (SCLogMessageGetBuffer(ts, 0, op_iface_ctx->type, buffer, sizeof(buffer), |
688 | 11.5M | op_iface_ctx->log_format ? op_iface_ctx->log_format |
689 | 11.5M | : sc_log_config->log_format, |
690 | 11.5M | log_level, file, line, function, module, message) == 0) { |
691 | 11.5M | int r = 0; |
692 | 11.5M | SCMutexLock(&op_iface_ctx->fp_mutex); |
693 | 11.5M | if (op_iface_ctx->rotation_flag) { |
694 | 0 | r = SCLogReopen(op_iface_ctx); |
695 | 0 | op_iface_ctx->rotation_flag = 0; |
696 | 0 | } |
697 | 11.5M | SCLogPrintToStream(op_iface_ctx->file_d, buffer); |
698 | 11.5M | SCMutexUnlock(&op_iface_ctx->fp_mutex); |
699 | | |
700 | | /* report error outside of lock to avoid recursion */ |
701 | 11.5M | if (r == -1) { |
702 | 0 | SCLogError("re-opening file \"%s\" failed: %s", op_iface_ctx->file, |
703 | 0 | strerror(errno)); |
704 | 0 | } |
705 | 11.5M | } |
706 | 11.5M | break; |
707 | 0 | case SC_LOG_OP_IFACE_SYSLOG: |
708 | 0 | if (SCLogMessageGetBuffer(ts, 0, op_iface_ctx->type, buffer, sizeof(buffer), |
709 | 0 | op_iface_ctx->log_format ? op_iface_ctx->log_format |
710 | 0 | : sc_log_config->log_format, |
711 | 0 | log_level, file, line, function, module, message) == 0) { |
712 | 0 | SCLogPrintToSyslog(SCLogMapLogLevelToSyslogLevel(log_level), buffer); |
713 | 0 | } |
714 | 0 | break; |
715 | 0 | default: |
716 | 0 | break; |
717 | 11.5M | } |
718 | 11.5M | op_iface_ctx = op_iface_ctx->next; |
719 | 11.5M | } |
720 | 11.5M | return SC_OK; |
721 | 11.5M | } |
722 | | |
723 | | void SCLog(int x, const char *file, const char *func, const int line, const char *module, |
724 | | const char *fmt, ...) |
725 | 3.23M | { |
726 | 3.23M | if (sc_log_global_log_level >= x && |
727 | 842k | (sc_log_fg_filters_present == 0 || |
728 | 0 | SCLogMatchFGFilterWL(file, func, line) == 1 || |
729 | 0 | SCLogMatchFGFilterBL(file, func, line) == 1) && |
730 | 842k | (sc_log_fd_filters_present == 0 || |
731 | 0 | SCLogMatchFDFilter(func) == 1)) |
732 | 842k | { |
733 | 842k | char msg[SC_LOG_MAX_LOG_MSG_LEN]; |
734 | 842k | va_list ap; |
735 | 842k | va_start(ap, fmt); |
736 | 842k | vsnprintf(msg, sizeof(msg), fmt, ap); |
737 | 842k | va_end(ap); |
738 | 842k | SCLogMessage(x, file, line, func, module, msg); |
739 | 842k | } |
740 | 3.23M | } |
741 | | |
742 | | void SCLogErr(int x, const char *file, const char *func, const int line, const char *module, |
743 | | const char *fmt, ...) |
744 | 10.4M | { |
745 | 10.4M | if (sc_log_global_log_level >= x && |
746 | 10.4M | (sc_log_fg_filters_present == 0 || |
747 | 0 | SCLogMatchFGFilterWL(file, func, line) == 1 || |
748 | 0 | SCLogMatchFGFilterBL(file, func, line) == 1) && |
749 | 10.4M | (sc_log_fd_filters_present == 0 || |
750 | 0 | SCLogMatchFDFilter(func) == 1)) |
751 | 10.4M | { |
752 | 10.4M | char msg[SC_LOG_MAX_LOG_MSG_LEN]; |
753 | 10.4M | va_list ap; |
754 | 10.4M | va_start(ap, fmt); |
755 | 10.4M | vsnprintf(msg, sizeof(msg), fmt, ap); |
756 | 10.4M | va_end(ap); |
757 | 10.4M | SCLogMessage(x, file, line, func, module, msg); |
758 | 10.4M | } |
759 | 10.4M | } |
760 | | |
761 | | /** |
762 | | * \brief Returns whether debug messages are enabled to be logged or not |
763 | | * |
764 | | * \retval 1 if debug messages are enabled to be logged |
765 | | * \retval 0 if debug messages are not enabled to be logged |
766 | | */ |
767 | | int SCLogDebugEnabled(void) |
768 | 163k | { |
769 | | #ifdef DEBUG |
770 | | if (sc_log_global_log_level == SC_LOG_DEBUG) |
771 | | return 1; |
772 | | else |
773 | | return 0; |
774 | | #else |
775 | 163k | return 0; |
776 | 163k | #endif |
777 | 163k | } |
778 | | |
779 | | /** |
780 | | * \brief Allocates an output buffer for an output interface. Used when we |
781 | | * want the op_interface log_format to override the global_log_format. |
782 | | * Currently not used. |
783 | | * |
784 | | * \retval buffer Pointer to the newly created output_buffer |
785 | | */ |
786 | | SCLogOPBuffer *SCLogAllocLogOPBuffer(void) |
787 | 0 | { |
788 | 0 | SCLogOPBuffer *buffer = NULL; |
789 | |
|
790 | 0 | if ( (buffer = SCMalloc(sc_log_config->op_ifaces_cnt * |
791 | 0 | sizeof(SCLogOPBuffer))) == NULL) { |
792 | 0 | FatalError("Fatal error encountered in SCLogAllocLogOPBuffer. Exiting..."); |
793 | 0 | } |
794 | | |
795 | 0 | SCLogOPIfaceCtx *op_iface_ctx = sc_log_config->op_ifaces; |
796 | 0 | for (int i = 0; i < sc_log_config->op_ifaces_cnt; i++, op_iface_ctx = op_iface_ctx->next) { |
797 | 0 | buffer[i].log_format = op_iface_ctx->log_format; |
798 | 0 | buffer[i].temp = buffer[i].msg; |
799 | 0 | } |
800 | |
|
801 | 0 | return buffer; |
802 | 0 | } |
803 | | |
804 | | /*----------------------The logging module initialization code--------------- */ |
805 | | |
806 | | /** |
807 | | * \brief Returns a new output_interface_context |
808 | | * |
809 | | * \retval iface_ctx Pointer to a newly allocated output_interface_context |
810 | | * \initonly |
811 | | */ |
812 | | static inline SCLogOPIfaceCtx *SCLogAllocLogOPIfaceCtx(void) |
813 | 87 | { |
814 | 87 | SCLogOPIfaceCtx *iface_ctx = NULL; |
815 | | |
816 | 87 | if ((iface_ctx = SCCalloc(1, sizeof(SCLogOPIfaceCtx))) == NULL) { |
817 | 0 | FatalError("Fatal error encountered in SCLogallocLogOPIfaceCtx. Exiting..."); |
818 | 0 | } |
819 | | |
820 | 87 | return iface_ctx; |
821 | 87 | } |
822 | | |
823 | | /** |
824 | | * \brief Initializes the file output interface |
825 | | * |
826 | | * \param file Path to the file used for logging purposes |
827 | | * \param log_format Pointer to the log_format for this op interface, that |
828 | | * overrides the global_log_format |
829 | | * \param log_level Override of the global_log_level by this interface |
830 | | * |
831 | | * \retval iface_ctx Pointer to the file output interface context created |
832 | | * \initonly |
833 | | */ |
834 | | static inline SCLogOPIfaceCtx *SCLogInitFileOPIface(const char *file, uint32_t userid, |
835 | | uint32_t groupid, const char *log_format, int log_level, SCLogOPType type) |
836 | 85 | { |
837 | 85 | SCLogOPIfaceCtx *iface_ctx = SCLogAllocLogOPIfaceCtx(); |
838 | 85 | if (iface_ctx == NULL) { |
839 | 0 | FatalError("Fatal error encountered in SCLogInitFileOPIface. Exiting..."); |
840 | 0 | } |
841 | | |
842 | 85 | if (file == NULL) { |
843 | 0 | goto error; |
844 | 0 | } |
845 | | |
846 | 85 | iface_ctx->iface = SC_LOG_OP_IFACE_FILE; |
847 | 85 | iface_ctx->type = type; |
848 | | |
849 | 85 | if ( (iface_ctx->file_d = fopen(file, "a")) == NULL) { |
850 | 0 | SCLogWarning("error opening file %s: %s", file, strerror(errno)); |
851 | 0 | goto error; |
852 | 0 | } |
853 | | |
854 | 85 | #ifndef OS_WIN32 |
855 | 85 | if (userid != 0 || groupid != 0) { |
856 | 0 | if (fchown(fileno(iface_ctx->file_d), userid, groupid) == -1) { |
857 | 0 | SCLogWarning("Failed to change ownership of file %s: %s", file, strerror(errno)); |
858 | 0 | } |
859 | 0 | } |
860 | 85 | #endif |
861 | | |
862 | 85 | if ((iface_ctx->file = SCStrdup(file)) == NULL) { |
863 | 0 | goto error; |
864 | 0 | } |
865 | | |
866 | 85 | if (log_format != NULL && (iface_ctx->log_format = SCStrdup(log_format)) == NULL) { |
867 | 0 | goto error; |
868 | 0 | } |
869 | | |
870 | 85 | SCMutexInit(&iface_ctx->fp_mutex, NULL); |
871 | 85 | OutputRegisterFileRotationFlag(&iface_ctx->rotation_flag); |
872 | | |
873 | 85 | iface_ctx->log_level = log_level; |
874 | | |
875 | 85 | return iface_ctx; |
876 | | |
877 | 0 | error: |
878 | 0 | if (iface_ctx->file != NULL) { |
879 | 0 | SCFree((char *)iface_ctx->file); |
880 | 0 | iface_ctx->file = NULL; |
881 | 0 | } |
882 | 0 | if (iface_ctx->log_format != NULL) { |
883 | 0 | SCFree((char *)iface_ctx->log_format); |
884 | 0 | iface_ctx->log_format = NULL; |
885 | 0 | } |
886 | 0 | if (iface_ctx->file_d != NULL) { |
887 | 0 | fclose(iface_ctx->file_d); |
888 | 0 | iface_ctx->file_d = NULL; |
889 | 0 | } |
890 | 0 | SCFree(iface_ctx); |
891 | 0 | return NULL; |
892 | 85 | } |
893 | | |
894 | | /** |
895 | | * \brief Initializes the console output interface and deals with possible |
896 | | * env var overrides. |
897 | | * |
898 | | * \param log_format Pointer to the log_format for this op interface, that |
899 | | * overrides the global_log_format |
900 | | * \param log_level Override of the global_log_level by this interface |
901 | | * |
902 | | * \retval iface_ctx Pointer to the console output interface context created |
903 | | * \initonly |
904 | | */ |
905 | | static inline SCLogOPIfaceCtx *SCLogInitConsoleOPIface(const char *log_format, |
906 | | SCLogLevel log_level, SCLogOPType type) |
907 | 2 | { |
908 | 2 | SCLogOPIfaceCtx *iface_ctx = SCLogAllocLogOPIfaceCtx(); |
909 | | |
910 | 2 | if (iface_ctx == NULL) { |
911 | 0 | FatalError("Fatal error encountered in SCLogInitConsoleOPIface. Exiting..."); |
912 | 0 | } |
913 | | |
914 | 2 | iface_ctx->iface = SC_LOG_OP_IFACE_CONSOLE; |
915 | 2 | iface_ctx->type = type; |
916 | | |
917 | | /* console log format is overridden by envvars */ |
918 | 2 | const char *tmp_log_format = log_format; |
919 | 2 | const char *s = getenv(SC_LOG_ENV_LOG_FORMAT); |
920 | 2 | if (s != NULL) { |
921 | | #if 0 |
922 | | printf("Overriding setting for \"console.format\" because of env " |
923 | | "var SC_LOG_FORMAT=\"%s\".\n", s); |
924 | | #endif |
925 | 0 | tmp_log_format = s; |
926 | 0 | } |
927 | | |
928 | 2 | if (tmp_log_format != NULL && |
929 | 0 | (iface_ctx->log_format = SCStrdup(tmp_log_format)) == NULL) { |
930 | 0 | printf("Error allocating memory\n"); |
931 | 0 | exit(EXIT_FAILURE); |
932 | 0 | } |
933 | | |
934 | | /* console log level is overridden by envvars */ |
935 | 2 | SCLogLevel tmp_log_level = log_level; |
936 | 2 | s = getenv(SC_LOG_ENV_LOG_LEVEL); |
937 | 2 | if (s != NULL) { |
938 | 0 | SCLogLevel l = SCMapEnumNameToValue(s, sc_log_level_map); |
939 | 0 | if (l > SC_LOG_NOTSET && l < SC_LOG_LEVEL_MAX) { |
940 | | #if 0 |
941 | | printf("Overriding setting for \"console.level\" because of env " |
942 | | "var SC_LOG_LEVEL=\"%s\".\n", s); |
943 | | #endif |
944 | 0 | tmp_log_level = l; |
945 | 0 | } |
946 | 0 | } |
947 | 2 | iface_ctx->log_level = tmp_log_level; |
948 | | |
949 | 2 | #ifndef OS_WIN32 |
950 | 2 | if (isatty(fileno(stdout)) && isatty(fileno(stderr))) { |
951 | 0 | iface_ctx->use_color = true; |
952 | 0 | } |
953 | 2 | #endif |
954 | | |
955 | 2 | return iface_ctx; |
956 | 2 | } |
957 | | |
958 | | /** |
959 | | * \brief Initializes the syslog output interface |
960 | | * |
961 | | * \param facility The facility code for syslog |
962 | | * \param log_format Pointer to the log_format for this op interface, that |
963 | | * overrides the global_log_format |
964 | | * \param log_level Override of the global_log_level by this interface |
965 | | * |
966 | | * \retval iface_ctx Pointer to the syslog output interface context created |
967 | | */ |
968 | | static inline SCLogOPIfaceCtx *SCLogInitSyslogOPIface(int facility, |
969 | | const char *log_format, |
970 | | SCLogLevel log_level, |
971 | | SCLogOPType type) |
972 | 0 | { |
973 | 0 | SCLogOPIfaceCtx *iface_ctx = SCLogAllocLogOPIfaceCtx(); |
974 | |
|
975 | 0 | if ( iface_ctx == NULL) { |
976 | 0 | FatalError("Fatal error encountered in SCLogInitSyslogOPIface. Exiting..."); |
977 | 0 | } |
978 | | |
979 | 0 | iface_ctx->iface = SC_LOG_OP_IFACE_SYSLOG; |
980 | 0 | iface_ctx->type = type; |
981 | |
|
982 | 0 | if (facility == -1) |
983 | 0 | facility = SC_LOG_DEF_SYSLOG_FACILITY; |
984 | 0 | iface_ctx->facility = facility; |
985 | |
|
986 | 0 | if (log_format != NULL && |
987 | 0 | (iface_ctx->log_format = SCStrdup(log_format)) == NULL) { |
988 | 0 | printf("Error allocating memory\n"); |
989 | 0 | exit(EXIT_FAILURE); |
990 | 0 | } |
991 | | |
992 | 0 | iface_ctx->log_level = log_level; |
993 | |
|
994 | 0 | openlog(NULL, LOG_NDELAY, iface_ctx->facility); |
995 | |
|
996 | 0 | return iface_ctx; |
997 | 0 | } |
998 | | |
999 | | /** |
1000 | | * \brief Frees the output_interface context supplied as an argument |
1001 | | * |
1002 | | * \param iface_ctx Pointer to the op_interface_context to be freed |
1003 | | */ |
1004 | | static inline void SCLogFreeLogOPIfaceCtx(SCLogOPIfaceCtx *iface_ctx) |
1005 | 0 | { |
1006 | 0 | SCLogOPIfaceCtx *temp = NULL; |
1007 | |
|
1008 | 0 | while (iface_ctx != NULL) { |
1009 | 0 | temp = iface_ctx; |
1010 | |
|
1011 | 0 | if (iface_ctx->file_d != NULL) { |
1012 | 0 | fclose(iface_ctx->file_d); |
1013 | 0 | SCMutexDestroy(&iface_ctx->fp_mutex); |
1014 | 0 | } |
1015 | |
|
1016 | 0 | if (iface_ctx->file != NULL) |
1017 | 0 | SCFree((void *)iface_ctx->file); |
1018 | |
|
1019 | 0 | if (iface_ctx->log_format != NULL) |
1020 | 0 | SCFree((void *)iface_ctx->log_format); |
1021 | |
|
1022 | 0 | if (iface_ctx->iface == SC_LOG_OP_IFACE_SYSLOG) { |
1023 | 0 | closelog(); |
1024 | 0 | } |
1025 | |
|
1026 | 0 | iface_ctx = iface_ctx->next; |
1027 | |
|
1028 | 0 | SCFree(temp); |
1029 | 0 | } |
1030 | 0 | } |
1031 | | |
1032 | | /** |
1033 | | * \brief Internal function used to set the logging module global_log_level |
1034 | | * during the initialization phase |
1035 | | * |
1036 | | * \param sc_lid The initialization data supplied. |
1037 | | * \param sc_lc The logging module context which has to be updated. |
1038 | | */ |
1039 | | static inline void SCLogSetLogLevel(SCLogInitData *sc_lid, SCLogConfig *sc_lc) |
1040 | 87 | { |
1041 | 87 | SCLogLevel log_level = SC_LOG_NOTSET; |
1042 | 87 | const char *s = NULL; |
1043 | | |
1044 | | /* envvar overrides config */ |
1045 | 87 | s = getenv(SC_LOG_ENV_LOG_LEVEL); |
1046 | 87 | if (s != NULL) { |
1047 | 0 | log_level = SCMapEnumNameToValue(s, sc_log_level_map); |
1048 | 87 | } else if (sc_lid != NULL) { |
1049 | 0 | log_level = sc_lid->global_log_level; |
1050 | 0 | } |
1051 | | |
1052 | | /* deal with the global_log_level to be used */ |
1053 | 87 | if (log_level > SC_LOG_NOTSET && log_level < SC_LOG_LEVEL_MAX) |
1054 | 0 | sc_lc->log_level = log_level; |
1055 | 87 | else { |
1056 | 87 | sc_lc->log_level = SC_LOG_DEF_LOG_LEVEL; |
1057 | 87 | #ifndef UNITTESTS |
1058 | 87 | if (sc_lid != NULL) { |
1059 | 0 | printf("Warning: Invalid/No global_log_level assigned by user. Falling " |
1060 | 0 | "back on the default_log_level \"%s\"\n", |
1061 | 0 | SCMapEnumValueToName(sc_lc->log_level, sc_log_level_map)); |
1062 | 0 | } |
1063 | 87 | #endif |
1064 | 87 | } |
1065 | | |
1066 | | /* we also set it to a global var, as it is easier to access it */ |
1067 | 87 | sc_log_global_log_level = sc_lc->log_level; |
1068 | 87 | } |
1069 | | |
1070 | | SCLogLevel SCLogGetLogLevel(void) |
1071 | 0 | { |
1072 | 0 | return sc_log_global_log_level; |
1073 | 0 | } |
1074 | | |
1075 | | static inline const char *SCLogGetDefaultLogFormat(const SCLogLevel lvl) |
1076 | 87 | { |
1077 | 87 | const char *prog_ver = GetProgramVersion(); |
1078 | 87 | if (strstr(prog_ver, "RELEASE") != NULL) { |
1079 | 0 | if (lvl <= SC_LOG_NOTICE) |
1080 | 0 | return SC_LOG_DEF_LOG_FORMAT_REL_NOTICE; |
1081 | 0 | else if (lvl <= SC_LOG_INFO) |
1082 | 0 | return SC_LOG_DEF_LOG_FORMAT_REL_INFO; |
1083 | 0 | else if (lvl <= SC_LOG_CONFIG) |
1084 | 0 | return SC_LOG_DEF_LOG_FORMAT_REL_CONFIG; |
1085 | 0 | } |
1086 | 87 | return SC_LOG_DEF_LOG_FORMAT_DEBUG; |
1087 | 87 | } |
1088 | | |
1089 | | /** |
1090 | | * \brief Internal function used to set the logging module global_log_format |
1091 | | * during the initialization phase |
1092 | | * |
1093 | | * \param sc_lid The initialization data supplied. |
1094 | | * \param sc_lc The logging module context which has to be updated. |
1095 | | */ |
1096 | | static inline void SCLogSetLogFormat(SCLogInitData *sc_lid, SCLogConfig *sc_lc) |
1097 | 87 | { |
1098 | 87 | const char *format = NULL; |
1099 | | |
1100 | | /* envvar overrides config */ |
1101 | 87 | format = getenv(SC_LOG_ENV_LOG_FORMAT); |
1102 | 87 | if (format == NULL) { |
1103 | 87 | if (sc_lid != NULL) { |
1104 | 0 | format = sc_lid->global_log_format; |
1105 | 0 | } |
1106 | 87 | } |
1107 | | |
1108 | | /* deal with the global log format to be used */ |
1109 | 87 | if (format == NULL || strlen(format) > SC_LOG_MAX_LOG_FORMAT_LEN) { |
1110 | 87 | format = SCLogGetDefaultLogFormat(sc_lc->log_level); |
1111 | 87 | #ifndef UNITTESTS |
1112 | 87 | if (sc_lid != NULL) { |
1113 | 0 | printf("Warning: Invalid/No global_log_format supplied by user or format " |
1114 | 0 | "length exceeded limit of \"%d\" characters. Falling back on " |
1115 | 0 | "default log_format \"%s\"\n", SC_LOG_MAX_LOG_FORMAT_LEN, |
1116 | 0 | format); |
1117 | 0 | } |
1118 | 87 | #endif |
1119 | 87 | } |
1120 | | |
1121 | 87 | if (format != NULL && (sc_lc->log_format = SCStrdup(format)) == NULL) { |
1122 | 0 | printf("Error allocating memory\n"); |
1123 | 0 | exit(EXIT_FAILURE); |
1124 | 0 | } |
1125 | 87 | } |
1126 | | |
1127 | | /** |
1128 | | * \brief Internal function used to set the logging module global_op_ifaces |
1129 | | * during the initialization phase |
1130 | | * |
1131 | | * \param sc_lid The initialization data supplied. |
1132 | | * \param sc_lc The logging module context which has to be updated. |
1133 | | */ |
1134 | | static inline void SCLogSetOPIface(SCLogInitData *sc_lid, SCLogConfig *sc_lc) |
1135 | 87 | { |
1136 | 87 | SCLogOPIfaceCtx *op_ifaces_ctx = NULL; |
1137 | 87 | int op_iface = 0; |
1138 | 87 | const char *s = NULL; |
1139 | | |
1140 | 87 | if (sc_lid != NULL && sc_lid->op_ifaces != NULL) { |
1141 | 0 | sc_lc->op_ifaces = sc_lid->op_ifaces; |
1142 | 0 | sc_lid->op_ifaces = NULL; |
1143 | 0 | sc_lc->op_ifaces_cnt = sc_lid->op_ifaces_cnt; |
1144 | 87 | } else { |
1145 | 87 | s = getenv(SC_LOG_ENV_LOG_OP_IFACE); |
1146 | 87 | if (s != NULL) { |
1147 | 85 | op_iface = SCMapEnumNameToValue(s, sc_log_op_iface_map); |
1148 | | |
1149 | 85 | if(op_iface < 0 || op_iface >= SC_LOG_OP_IFACE_MAX) { |
1150 | 0 | op_iface = SC_LOG_DEF_LOG_OP_IFACE; |
1151 | 0 | #ifndef UNITTESTS |
1152 | 0 | printf("Warning: Invalid output interface supplied by user. " |
1153 | 0 | "Falling back on default_output_interface \"%s\"\n", |
1154 | 0 | SCMapEnumValueToName(op_iface, sc_log_op_iface_map)); |
1155 | 0 | #endif |
1156 | 0 | } |
1157 | 85 | } |
1158 | 2 | else { |
1159 | 2 | op_iface = SC_LOG_DEF_LOG_OP_IFACE; |
1160 | 2 | #ifndef UNITTESTS |
1161 | 2 | if (sc_lid != NULL) { |
1162 | 0 | printf("Warning: Output_interface not supplied by user. Falling " |
1163 | 0 | "back on default_output_interface \"%s\"\n", |
1164 | 0 | SCMapEnumValueToName(op_iface, sc_log_op_iface_map)); |
1165 | 0 | } |
1166 | 2 | #endif |
1167 | 2 | } |
1168 | | |
1169 | 87 | switch (op_iface) { |
1170 | 2 | case SC_LOG_OP_IFACE_CONSOLE: |
1171 | 2 | op_ifaces_ctx = SCLogInitConsoleOPIface(NULL, SC_LOG_LEVEL_MAX,0); |
1172 | 2 | break; |
1173 | 85 | case SC_LOG_OP_IFACE_FILE: |
1174 | 85 | s = getenv(SC_LOG_ENV_LOG_FILE); |
1175 | 85 | if (s == NULL) { |
1176 | 0 | char *str = SCLogGetLogFilename(SC_LOG_DEF_LOG_FILE); |
1177 | 0 | if (str != NULL) { |
1178 | 0 | op_ifaces_ctx = SCLogInitFileOPIface(str, 0, 0, NULL, SC_LOG_LEVEL_MAX, 0); |
1179 | 0 | SCFree(str); |
1180 | 0 | } |
1181 | 85 | } else { |
1182 | 85 | op_ifaces_ctx = SCLogInitFileOPIface(s, 0, 0, NULL, SC_LOG_LEVEL_MAX, 0); |
1183 | 85 | } |
1184 | 85 | break; |
1185 | 0 | case SC_LOG_OP_IFACE_SYSLOG: |
1186 | 0 | s = getenv(SC_LOG_ENV_LOG_FACILITY); |
1187 | 0 | if (s == NULL) |
1188 | 0 | s = SC_LOG_DEF_SYSLOG_FACILITY_STR; |
1189 | |
|
1190 | 0 | op_ifaces_ctx = SCLogInitSyslogOPIface(SCMapEnumNameToValue(s, SCSyslogGetFacilityMap()), NULL, -1,0); |
1191 | 0 | break; |
1192 | 87 | } |
1193 | 87 | sc_lc->op_ifaces = op_ifaces_ctx; |
1194 | 87 | sc_lc->op_ifaces_cnt++; |
1195 | 87 | } |
1196 | 87 | } |
1197 | | |
1198 | | /** |
1199 | | * \brief Internal function used to set the logging module op_filter |
1200 | | * during the initialization phase |
1201 | | * |
1202 | | * \param sc_lid The initialization data supplied. |
1203 | | * \param sc_lc The logging module context which has to be updated. |
1204 | | */ |
1205 | | static inline void SCLogSetOPFilter(SCLogInitData *sc_lid, SCLogConfig *sc_lc) |
1206 | 87 | { |
1207 | 87 | const char *filter = NULL; |
1208 | | |
1209 | 87 | int opts = 0; |
1210 | 87 | int en; |
1211 | 87 | PCRE2_SIZE eo = 0; |
1212 | | |
1213 | | /* envvar overrides */ |
1214 | 87 | filter = getenv(SC_LOG_ENV_LOG_OP_FILTER); |
1215 | 87 | if (filter == NULL) { |
1216 | 87 | if (sc_lid != NULL) { |
1217 | 0 | filter = sc_lid->op_filter; |
1218 | 0 | } |
1219 | 87 | } |
1220 | | |
1221 | 87 | if (filter != NULL && strcmp(filter, "") != 0) { |
1222 | 0 | sc_lc->op_filter = SCStrdup(filter); |
1223 | 0 | if (sc_lc->op_filter == NULL) { |
1224 | 0 | printf("pcre filter alloc failed\n"); |
1225 | 0 | return; |
1226 | 0 | } |
1227 | 0 | sc_lc->op_filter_regex = |
1228 | 0 | pcre2_compile((PCRE2_SPTR8)filter, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL); |
1229 | 0 | if (sc_lc->op_filter_regex == NULL) { |
1230 | 0 | SCFree(sc_lc->op_filter); |
1231 | 0 | PCRE2_UCHAR errbuffer[256]; |
1232 | 0 | pcre2_get_error_message(en, errbuffer, sizeof(errbuffer)); |
1233 | 0 | printf("pcre2 compile of \"%s\" failed at offset %d : %s\n", filter, (int)eo, |
1234 | 0 | errbuffer); |
1235 | 0 | return; |
1236 | 0 | } |
1237 | 0 | sc_lc->op_filter_regex_match = |
1238 | 0 | pcre2_match_data_create_from_pattern(sc_lc->op_filter_regex, NULL); |
1239 | 0 | } |
1240 | 87 | } |
1241 | | |
1242 | | /** |
1243 | | * \brief Returns a pointer to a new SCLogInitData. This is a public interface |
1244 | | * intended to be used after the logging parameters are read from the |
1245 | | * conf file |
1246 | | * |
1247 | | * \retval sc_lid Pointer to the newly created SCLogInitData |
1248 | | * \initonly |
1249 | | */ |
1250 | | SCLogInitData *SCLogAllocLogInitData(void) |
1251 | 0 | { |
1252 | 0 | SCLogInitData *sc_lid = NULL; |
1253 | |
|
1254 | 0 | if ((sc_lid = SCCalloc(1, sizeof(SCLogInitData))) == NULL) |
1255 | 0 | return NULL; |
1256 | | |
1257 | 0 | return sc_lid; |
1258 | 0 | } |
1259 | | |
1260 | | #ifdef UNITTESTS |
1261 | | #ifndef OS_WIN32 |
1262 | | /** |
1263 | | * \brief Frees a SCLogInitData |
1264 | | * |
1265 | | * \param sc_lid Pointer to the SCLogInitData to be freed |
1266 | | */ |
1267 | | static void SCLogFreeLogInitData(SCLogInitData *sc_lid) |
1268 | | { |
1269 | | if (sc_lid != NULL) { |
1270 | | SCLogFreeLogOPIfaceCtx(sc_lid->op_ifaces); |
1271 | | SCFree(sc_lid); |
1272 | | } |
1273 | | } |
1274 | | #endif |
1275 | | #endif |
1276 | | |
1277 | | /** |
1278 | | * \brief Frees the logging module context |
1279 | | */ |
1280 | | static inline void SCLogFreeLogConfig(SCLogConfig *sc_lc) |
1281 | 87 | { |
1282 | 87 | if (sc_lc != NULL) { |
1283 | 0 | if (sc_lc->startup_message != NULL) |
1284 | 0 | SCFree(sc_lc->startup_message); |
1285 | 0 | if (sc_lc->log_format != NULL) |
1286 | 0 | SCFree(sc_lc->log_format); |
1287 | 0 | if (sc_lc->op_filter != NULL) |
1288 | 0 | SCFree(sc_lc->op_filter); |
1289 | |
|
1290 | 0 | if (sc_lc->op_filter_regex != NULL) |
1291 | 0 | pcre2_code_free(sc_lc->op_filter_regex); |
1292 | 0 | if (sc_lc->op_filter_regex_match) |
1293 | 0 | pcre2_match_data_free(sc_lc->op_filter_regex_match); |
1294 | |
|
1295 | 0 | SCLogFreeLogOPIfaceCtx(sc_lc->op_ifaces); |
1296 | 0 | SCFree(sc_lc); |
1297 | 0 | } |
1298 | 87 | } |
1299 | | |
1300 | | /** |
1301 | | * \brief Appends an output_interface to the output_interface list sent in head |
1302 | | * |
1303 | | * \param iface_ctx Pointer to the output_interface that has to be added to head |
1304 | | * \param head Pointer to the output_interface list |
1305 | | */ |
1306 | | void SCLogAppendOPIfaceCtx(SCLogOPIfaceCtx *iface_ctx, SCLogInitData *sc_lid) |
1307 | 0 | { |
1308 | 0 | SCLogOPIfaceCtx *temp = NULL, *prev = NULL; |
1309 | 0 | SCLogOPIfaceCtx **head = &sc_lid->op_ifaces; |
1310 | |
|
1311 | 0 | if (iface_ctx == NULL) { |
1312 | | #ifdef DEBUG |
1313 | | printf("Argument(s) to SCLogAppendOPIfaceCtx() NULL\n"); |
1314 | | #endif |
1315 | 0 | return; |
1316 | 0 | } |
1317 | | |
1318 | 0 | temp = *head; |
1319 | 0 | while (temp != NULL) { |
1320 | 0 | prev = temp; |
1321 | 0 | temp = temp->next; |
1322 | 0 | } |
1323 | |
|
1324 | 0 | if (prev == NULL) |
1325 | 0 | *head = iface_ctx; |
1326 | 0 | else |
1327 | 0 | prev->next = iface_ctx; |
1328 | |
|
1329 | 0 | sc_lid->op_ifaces_cnt++; |
1330 | 0 | } |
1331 | | |
1332 | | #ifdef UNITTESTS |
1333 | | #ifndef OS_WIN32 |
1334 | | /** |
1335 | | * \internal |
1336 | | * \brief Creates a new output interface based on the arguments sent. The kind |
1337 | | * of output interface to be created is decided by the iface_name arg. |
1338 | | * If iface_name is "file", the arg argument will hold the filename to be |
1339 | | * used for logging purposes. If iface_name is "syslog", the arg |
1340 | | * argument holds the facility code. If iface_name is "console", arg is |
1341 | | * NULL. |
1342 | | * |
1343 | | * \param iface_name Interface name. Can be "console", "file" or "syslog" |
1344 | | * \param log_format Override for the global_log_format |
1345 | | * \param log_level Override for the global_log_level |
1346 | | * \param log_level Parameter required by a particular interface. Explained in |
1347 | | * the function description |
1348 | | * |
1349 | | * \retval iface_ctx Pointer to the newly created output interface |
1350 | | */ |
1351 | | static SCLogOPIfaceCtx *SCLogInitOPIfaceCtx( |
1352 | | const char *iface_name, const char *log_format, int log_level, const char *arg) |
1353 | | { |
1354 | | int iface = SCMapEnumNameToValue(iface_name, sc_log_op_iface_map); |
1355 | | |
1356 | | if (log_level < SC_LOG_NONE || log_level > SC_LOG_DEBUG) { |
1357 | | printf("Warning: Supplied log_level_override for op_interface \"%s\" " |
1358 | | "is invalid. Defaulting to not specifying an override\n", |
1359 | | iface_name); |
1360 | | log_level = SC_LOG_NOTSET; |
1361 | | } |
1362 | | |
1363 | | switch (iface) { |
1364 | | case SC_LOG_OP_IFACE_CONSOLE: |
1365 | | return SCLogInitConsoleOPIface(log_format, log_level, SC_LOG_OP_TYPE_REGULAR); |
1366 | | case SC_LOG_OP_IFACE_FILE: |
1367 | | return SCLogInitFileOPIface(arg, 0, 0, log_format, log_level, SC_LOG_OP_TYPE_REGULAR); |
1368 | | case SC_LOG_OP_IFACE_SYSLOG: |
1369 | | return SCLogInitSyslogOPIface(SCMapEnumNameToValue(arg, SCSyslogGetFacilityMap()), |
1370 | | log_format, log_level, SC_LOG_OP_TYPE_REGULAR); |
1371 | | default: |
1372 | | #ifdef DEBUG |
1373 | | printf("Output Interface \"%s\" not supported by the logging module", |
1374 | | iface_name); |
1375 | | #endif |
1376 | | return NULL; |
1377 | | } |
1378 | | } |
1379 | | #endif |
1380 | | #endif |
1381 | | |
1382 | | /** |
1383 | | * \brief Initializes the logging module. |
1384 | | * |
1385 | | * \param sc_lid The initialization data for the logging module. If sc_lid is |
1386 | | * NULL, we would stick to the default configuration for the |
1387 | | * logging subsystem. |
1388 | | * \initonly |
1389 | | */ |
1390 | | void SCLogInitLogModule(SCLogInitData *sc_lid) |
1391 | 87 | { |
1392 | | /* De-initialize the logging context, if it has already init by the |
1393 | | * environment variables at the start of the engine */ |
1394 | 87 | SCLogDeInitLogModule(); |
1395 | | |
1396 | | #if defined (OS_WIN32) |
1397 | | if (SCMutexInit(&sc_log_stream_lock, NULL) != 0) { |
1398 | | FatalError("Failed to initialize log mutex."); |
1399 | | } |
1400 | | #endif /* OS_WIN32 */ |
1401 | | |
1402 | | /* sc_log_config is a global variable */ |
1403 | 87 | if ((sc_log_config = SCCalloc(1, sizeof(SCLogConfig))) == NULL) { |
1404 | 0 | FatalError("Fatal error encountered in SCLogInitLogModule. Exiting..."); |
1405 | 0 | } |
1406 | | |
1407 | 87 | SCLogSetLogLevel(sc_lid, sc_log_config); |
1408 | 87 | SCLogSetLogFormat(sc_lid, sc_log_config); |
1409 | 87 | SCLogSetOPIface(sc_lid, sc_log_config); |
1410 | 87 | SCLogSetOPFilter(sc_lid, sc_log_config); |
1411 | | |
1412 | 87 | sc_log_module_initialized = 1; |
1413 | 87 | sc_log_module_cleaned = 0; |
1414 | | |
1415 | | //SCOutputPrint(sc_did->startup_message); |
1416 | | |
1417 | 87 | SCSetRustLogLevel(sc_log_global_log_level); |
1418 | 87 | } |
1419 | | |
1420 | | void SCLogLoadConfig(int daemon, int verbose, uint32_t userid, uint32_t groupid) |
1421 | 0 | { |
1422 | 0 | SCConfNode *outputs; |
1423 | 0 | SCLogInitData *sc_lid; |
1424 | 0 | int have_logging = 0; |
1425 | 0 | int max_level = 0; |
1426 | 0 | SCLogLevel min_level = 0; |
1427 | | |
1428 | | /* If verbose logging was requested, set the minimum as |
1429 | | * SC_LOG_NOTICE plus the extra verbosity. */ |
1430 | 0 | if (verbose) { |
1431 | 0 | min_level = SC_LOG_NOTICE + verbose; |
1432 | 0 | } |
1433 | |
|
1434 | 0 | outputs = SCConfGetNode("logging.outputs"); |
1435 | 0 | if (outputs == NULL) { |
1436 | 0 | SCLogDebug("No logging.output configuration section found."); |
1437 | 0 | return; |
1438 | 0 | } |
1439 | | |
1440 | 0 | sc_lid = SCLogAllocLogInitData(); |
1441 | 0 | if (sc_lid == NULL) { |
1442 | 0 | SCLogDebug("Could not allocate memory for log init data"); |
1443 | 0 | return; |
1444 | 0 | } |
1445 | | |
1446 | | /* Get default log level and format. */ |
1447 | 0 | const char *default_log_level_s = NULL; |
1448 | 0 | if (SCConfGetNonNull("logging.default-log-level", &default_log_level_s) == 1) { |
1449 | 0 | SCLogLevel default_log_level = |
1450 | 0 | SCMapEnumNameToValue(default_log_level_s, sc_log_level_map); |
1451 | 0 | if (default_log_level == -1) { |
1452 | 0 | SCLogError("Invalid default log level: %s", default_log_level_s); |
1453 | 0 | exit(EXIT_FAILURE); |
1454 | 0 | } |
1455 | 0 | sc_lid->global_log_level = MAX(min_level, default_log_level); |
1456 | 0 | } else { |
1457 | 0 | sc_lid->global_log_level = MAX(min_level, SC_LOG_NOTICE); |
1458 | 0 | } |
1459 | | |
1460 | 0 | if (SCConfGetNonNull("logging.default-log-format", &sc_lid->global_log_format) != 1) |
1461 | 0 | sc_lid->global_log_format = SCLogGetDefaultLogFormat(sc_lid->global_log_level); |
1462 | |
|
1463 | 0 | (void)SCConfGet("logging.default-output-filter", &sc_lid->op_filter); |
1464 | |
|
1465 | 0 | SCConfNode *seq_node, *output; |
1466 | 0 | TAILQ_FOREACH(seq_node, &outputs->head, next) { |
1467 | 0 | SCLogLevel level = sc_lid->global_log_level; |
1468 | 0 | SCLogOPIfaceCtx *op_iface_ctx = NULL; |
1469 | 0 | const char *format; |
1470 | 0 | const char *level_s; |
1471 | |
|
1472 | 0 | output = SCConfNodeLookupChild(seq_node, seq_node->val); |
1473 | 0 | if (output == NULL) |
1474 | 0 | continue; |
1475 | | |
1476 | | /* By default an output is enabled. */ |
1477 | 0 | const char *enabled = SCConfNodeLookupChildValue(output, "enabled"); |
1478 | 0 | if (enabled != NULL && SCConfValIsFalse(enabled)) |
1479 | 0 | continue; |
1480 | | |
1481 | 0 | SCLogOPType type = SC_LOG_OP_TYPE_REGULAR; |
1482 | 0 | const char *type_s = SCConfNodeLookupChildValue(output, "type"); |
1483 | 0 | if (type_s != NULL) { |
1484 | 0 | if (strcmp(type_s, "regular") == 0) |
1485 | 0 | type = SC_LOG_OP_TYPE_REGULAR; |
1486 | 0 | else if (strcmp(type_s, "json") == 0) { |
1487 | 0 | type = SC_LOG_OP_TYPE_JSON; |
1488 | 0 | } |
1489 | 0 | } |
1490 | |
|
1491 | 0 | format = SCConfNodeLookupChildValue(output, "format"); |
1492 | |
|
1493 | 0 | level_s = SCConfNodeLookupChildValue(output, "level"); |
1494 | 0 | if (level_s != NULL) { |
1495 | 0 | level = SCMapEnumNameToValue(level_s, sc_log_level_map); |
1496 | 0 | if (level == -1) { |
1497 | 0 | SCLogError("Invalid log level: %s", level_s); |
1498 | 0 | exit(EXIT_FAILURE); |
1499 | 0 | } |
1500 | 0 | max_level = MAX(max_level, level); |
1501 | 0 | } |
1502 | | |
1503 | | /* Increase the level of extra verbosity was requested. */ |
1504 | 0 | level = MAX(min_level, level); |
1505 | |
|
1506 | 0 | if (strcmp(output->name, "console") == 0) { |
1507 | 0 | op_iface_ctx = SCLogInitConsoleOPIface(format, level, type); |
1508 | 0 | } |
1509 | 0 | else if (strcmp(output->name, "file") == 0) { |
1510 | 0 | if (format == NULL) { |
1511 | 0 | format = SC_LOG_DEF_FILE_FORMAT; |
1512 | 0 | } |
1513 | |
|
1514 | 0 | const char *filename = SCConfNodeLookupChildValue(output, "filename"); |
1515 | 0 | if (filename == NULL) { |
1516 | 0 | FatalError("Logging to file requires a filename"); |
1517 | 0 | } |
1518 | 0 | char *path = NULL; |
1519 | 0 | if (!(PathIsAbsolute(filename))) { |
1520 | 0 | path = SCLogGetLogFilename(filename); |
1521 | 0 | } else { |
1522 | 0 | path = SCStrdup(filename); |
1523 | 0 | } |
1524 | 0 | if (path == NULL) |
1525 | 0 | FatalError("failed to setup output to file"); |
1526 | 0 | have_logging = 1; |
1527 | 0 | op_iface_ctx = SCLogInitFileOPIface(path, userid, groupid, format, level, type); |
1528 | 0 | SCFree(path); |
1529 | 0 | } |
1530 | 0 | else if (strcmp(output->name, "syslog") == 0) { |
1531 | 0 | int facility = SC_LOG_DEF_SYSLOG_FACILITY; |
1532 | 0 | const char *facility_s = SCConfNodeLookupChildValue(output, "facility"); |
1533 | 0 | if (facility_s != NULL) { |
1534 | 0 | facility = SCMapEnumNameToValue(facility_s, SCSyslogGetFacilityMap()); |
1535 | 0 | if (facility == -1) { |
1536 | 0 | SCLogWarning("Invalid syslog " |
1537 | 0 | "facility: \"%s\", now using \"%s\" as syslog " |
1538 | 0 | "facility", |
1539 | 0 | facility_s, SC_LOG_DEF_SYSLOG_FACILITY_STR); |
1540 | 0 | facility = SC_LOG_DEF_SYSLOG_FACILITY; |
1541 | 0 | } |
1542 | 0 | } |
1543 | 0 | SCLogDebug("Initializing syslog logging with format \"%s\"", format); |
1544 | 0 | have_logging = 1; |
1545 | 0 | op_iface_ctx = SCLogInitSyslogOPIface(facility, format, level, type); |
1546 | 0 | } |
1547 | 0 | else { |
1548 | 0 | SCLogWarning("invalid logging method: %s, ignoring", output->name); |
1549 | 0 | } |
1550 | 0 | if (op_iface_ctx != NULL) { |
1551 | 0 | SCLogAppendOPIfaceCtx(op_iface_ctx, sc_lid); |
1552 | 0 | } |
1553 | 0 | } |
1554 | | |
1555 | 0 | if (daemon && (have_logging == 0)) { |
1556 | 0 | SCLogWarning("no logging compatible with daemon mode selected," |
1557 | 0 | " suricata won't be able to log. Please update " |
1558 | 0 | " 'logging.outputs' in the YAML."); |
1559 | 0 | } |
1560 | | |
1561 | | /* Set the global log level to that of the max level used. */ |
1562 | 0 | sc_lid->global_log_level = MAX(sc_lid->global_log_level, max_level); |
1563 | 0 | SCLogInitLogModule(sc_lid); |
1564 | |
|
1565 | 0 | SCLogDebug("sc_log_global_log_level: %d", sc_log_global_log_level); |
1566 | 0 | SCLogDebug("sc_lc->log_format: %s", sc_log_config->log_format); |
1567 | 0 | SCLogDebug("SCLogSetOPFilter: filter: %s", sc_log_config->op_filter); |
1568 | |
|
1569 | 0 | SCFree(sc_lid); |
1570 | 0 | } |
1571 | | |
1572 | | /** |
1573 | | * \brief Returns a full file path given a filename uses log dir specified in |
1574 | | * conf or DEFAULT_LOG_DIR |
1575 | | * |
1576 | | * \param filearg The relative filename for which we want a full path include |
1577 | | * log directory |
1578 | | * |
1579 | | * \retval log_filename The fullpath of the logfile to open |
1580 | | */ |
1581 | | static char *SCLogGetLogFilename(const char *filearg) |
1582 | 0 | { |
1583 | 0 | const char *log_dir = SCConfigGetLogDirectory(); |
1584 | 0 | char *log_filename = SCMalloc(PATH_MAX); |
1585 | 0 | if (unlikely(log_filename == NULL)) |
1586 | 0 | return NULL; |
1587 | 0 | snprintf(log_filename, PATH_MAX, "%s/%s", log_dir, filearg); |
1588 | 0 | return log_filename; |
1589 | 0 | } |
1590 | | |
1591 | | /** |
1592 | | * \brief De-Initializes the logging module |
1593 | | */ |
1594 | | void SCLogDeInitLogModule(void) |
1595 | 87 | { |
1596 | 87 | SCLogFreeLogConfig(sc_log_config); |
1597 | | |
1598 | | /* reset the global logging_module variables */ |
1599 | 87 | sc_log_global_log_level = 0; |
1600 | 87 | sc_log_module_initialized = 0; |
1601 | 87 | sc_log_module_cleaned = 1; |
1602 | 87 | sc_log_config = NULL; |
1603 | | |
1604 | | /* de-init the FD filters */ |
1605 | 87 | SCLogReleaseFDFilters(); |
1606 | | /* de-init the FG filters */ |
1607 | 87 | SCLogReleaseFGFilters(); |
1608 | | |
1609 | | #if defined (OS_WIN32) |
1610 | | SCMutexDestroy(&sc_log_stream_lock); |
1611 | | #endif /* OS_WIN32 */ |
1612 | 87 | } |
1613 | | |
1614 | | void SCFatalErrorOnInitStatic(const char *arg) |
1615 | 0 | { |
1616 | 0 | FatalErrorOnInit("%s", arg); |
1617 | 0 | } |
1618 | | |
1619 | | //------------------------------------Unit_Tests-------------------------------- |
1620 | | |
1621 | | /* The logging engine should be tested to the maximum extent possible, since |
1622 | | * logging code would be used throughout the codebase, and hence we can't afford |
1623 | | * to have a single bug here(not that you can afford to have a bug |
1624 | | * elsewhere ;) ). Please report a bug, if you get a slightest hint of a bug |
1625 | | * from the logging module. |
1626 | | */ |
1627 | | |
1628 | | #ifdef UNITTESTS |
1629 | | |
1630 | | static int SCLogTestInit01(void) |
1631 | | { |
1632 | | #ifndef OS_WIN32 |
1633 | | /* unset any environment variables set for the logging module */ |
1634 | | unsetenv(SC_LOG_ENV_LOG_LEVEL); |
1635 | | unsetenv(SC_LOG_ENV_LOG_OP_IFACE); |
1636 | | unsetenv(SC_LOG_ENV_LOG_FORMAT); |
1637 | | |
1638 | | SCLogInitLogModule(NULL); |
1639 | | |
1640 | | FAIL_IF_NULL(sc_log_config); |
1641 | | |
1642 | | FAIL_IF_NOT(SC_LOG_DEF_LOG_LEVEL == sc_log_config->log_level); |
1643 | | FAIL_IF_NOT(sc_log_config->op_ifaces != NULL && |
1644 | | SC_LOG_DEF_LOG_OP_IFACE == sc_log_config->op_ifaces->iface); |
1645 | | FAIL_IF_NOT(sc_log_config->log_format != NULL && |
1646 | | strcmp(SCLogGetDefaultLogFormat(sc_log_config->log_level), |
1647 | | sc_log_config->log_format) == 0); |
1648 | | |
1649 | | SCLogDeInitLogModule(); |
1650 | | |
1651 | | setenv(SC_LOG_ENV_LOG_LEVEL, "Debug", 1); |
1652 | | setenv(SC_LOG_ENV_LOG_OP_IFACE, "Console", 1); |
1653 | | setenv(SC_LOG_ENV_LOG_FORMAT, "%n- %l", 1); |
1654 | | |
1655 | | SCLogInitLogModule(NULL); |
1656 | | |
1657 | | FAIL_IF_NOT(SC_LOG_DEBUG == sc_log_config->log_level); |
1658 | | FAIL_IF_NOT(sc_log_config->op_ifaces != NULL && |
1659 | | SC_LOG_OP_IFACE_CONSOLE == sc_log_config->op_ifaces->iface); |
1660 | | FAIL_IF_NOT(sc_log_config->log_format != NULL && |
1661 | | !strcmp("%n- %l", sc_log_config->log_format)); |
1662 | | |
1663 | | unsetenv(SC_LOG_ENV_LOG_LEVEL); |
1664 | | unsetenv(SC_LOG_ENV_LOG_OP_IFACE); |
1665 | | unsetenv(SC_LOG_ENV_LOG_FORMAT); |
1666 | | |
1667 | | SCLogDeInitLogModule(); |
1668 | | #endif |
1669 | | PASS; |
1670 | | } |
1671 | | |
1672 | | static int SCLogTestInit02(void) |
1673 | | { |
1674 | | #ifndef OS_WIN32 |
1675 | | SCLogInitData *sc_lid = NULL; |
1676 | | SCLogOPIfaceCtx *sc_iface_ctx = NULL; |
1677 | | char *logfile = SCLogGetLogFilename("boo.txt"); |
1678 | | sc_lid = SCLogAllocLogInitData(); |
1679 | | FAIL_IF_NULL(sc_lid); |
1680 | | sc_lid->startup_message = "Test02"; |
1681 | | sc_lid->global_log_level = SC_LOG_DEBUG; |
1682 | | sc_lid->op_filter = "boo"; |
1683 | | sc_iface_ctx = SCLogInitOPIfaceCtx("file", "%m - %d", SC_LOG_WARNING, logfile); |
1684 | | SCLogAppendOPIfaceCtx(sc_iface_ctx, sc_lid); |
1685 | | sc_iface_ctx = SCLogInitOPIfaceCtx("console", NULL, SC_LOG_ERROR, |
1686 | | NULL); |
1687 | | SCLogAppendOPIfaceCtx(sc_iface_ctx, sc_lid); |
1688 | | |
1689 | | SCLogInitLogModule(sc_lid); |
1690 | | |
1691 | | FAIL_IF_NULL(sc_log_config); |
1692 | | |
1693 | | FAIL_IF_NOT(SC_LOG_DEBUG == sc_log_config->log_level); |
1694 | | FAIL_IF_NOT(sc_log_config->op_ifaces != NULL && |
1695 | | SC_LOG_OP_IFACE_FILE == sc_log_config->op_ifaces->iface); |
1696 | | FAIL_IF_NOT(sc_log_config->op_ifaces != NULL && |
1697 | | sc_log_config->op_ifaces->next != NULL && |
1698 | | SC_LOG_OP_IFACE_CONSOLE == sc_log_config->op_ifaces->next->iface); |
1699 | | FAIL_IF_NOT(sc_log_config->log_format != NULL && |
1700 | | strcmp(SCLogGetDefaultLogFormat(sc_log_config->log_level), |
1701 | | sc_log_config->log_format) == 0); |
1702 | | FAIL_IF_NOT(sc_log_config->op_ifaces != NULL && |
1703 | | sc_log_config->op_ifaces->log_format != NULL && |
1704 | | strcmp("%m - %d", sc_log_config->op_ifaces->log_format) == 0); |
1705 | | FAIL_IF_NOT(sc_log_config->op_ifaces != NULL && |
1706 | | sc_log_config->op_ifaces->next != NULL && |
1707 | | sc_log_config->op_ifaces->next->log_format == NULL); |
1708 | | |
1709 | | SCLogFreeLogInitData(sc_lid); |
1710 | | SCLogDeInitLogModule(); |
1711 | | |
1712 | | sc_lid = SCLogAllocLogInitData(); |
1713 | | FAIL_IF_NULL(sc_lid); |
1714 | | sc_lid->startup_message = "Test02"; |
1715 | | sc_lid->global_log_level = SC_LOG_DEBUG; |
1716 | | sc_lid->op_filter = "boo"; |
1717 | | sc_lid->global_log_format = "kaboo"; |
1718 | | |
1719 | | SCLogInitLogModule(sc_lid); |
1720 | | |
1721 | | FAIL_IF_NULL(sc_log_config); |
1722 | | |
1723 | | FAIL_IF_NOT(SC_LOG_DEBUG == sc_log_config->log_level); |
1724 | | FAIL_IF_NOT(sc_log_config->op_ifaces != NULL && |
1725 | | SC_LOG_OP_IFACE_CONSOLE == sc_log_config->op_ifaces->iface); |
1726 | | FAIL_IF_NOT(sc_log_config->op_ifaces != NULL && |
1727 | | sc_log_config->op_ifaces->next == NULL); |
1728 | | FAIL_IF_NOT(sc_log_config->log_format != NULL && |
1729 | | strcmp("kaboo", sc_log_config->log_format) == 0); |
1730 | | FAIL_IF_NOT(sc_log_config->op_ifaces != NULL && |
1731 | | sc_log_config->op_ifaces->log_format == NULL); |
1732 | | FAIL_IF_NOT(sc_log_config->op_ifaces != NULL && |
1733 | | sc_log_config->op_ifaces->next == NULL); |
1734 | | |
1735 | | SCLogFreeLogInitData(sc_lid); |
1736 | | SCLogDeInitLogModule(); |
1737 | | SCFree(logfile); |
1738 | | #endif |
1739 | | PASS; |
1740 | | } |
1741 | | |
1742 | | static int SCLogTestInit03(void) |
1743 | | { |
1744 | | SCLogInitLogModule(NULL); |
1745 | | |
1746 | | SCLogAddFGFilterBL(NULL, "bamboo", -1); |
1747 | | SCLogAddFGFilterBL(NULL, "soo", -1); |
1748 | | SCLogAddFGFilterBL(NULL, "dummy", -1); |
1749 | | |
1750 | | FAIL_IF_NOT(SCLogPrintFGFilters() == 3); |
1751 | | |
1752 | | SCLogAddFGFilterBL(NULL, "dummy1", -1); |
1753 | | SCLogAddFGFilterBL(NULL, "dummy2", -1); |
1754 | | |
1755 | | FAIL_IF_NOT(SCLogPrintFGFilters() == 5); |
1756 | | |
1757 | | SCLogDeInitLogModule(); |
1758 | | |
1759 | | PASS; |
1760 | | } |
1761 | | |
1762 | | static int SCLogTestInit04(void) |
1763 | | { |
1764 | | SCLogInitLogModule(NULL); |
1765 | | |
1766 | | SCLogAddFDFilter("bamboo"); |
1767 | | SCLogAddFDFilter("soo"); |
1768 | | SCLogAddFDFilter("foo"); |
1769 | | SCLogAddFDFilter("roo"); |
1770 | | |
1771 | | FAIL_IF_NOT(SCLogPrintFDFilters() == 4); |
1772 | | |
1773 | | SCLogAddFDFilter("loo"); |
1774 | | SCLogAddFDFilter("soo"); |
1775 | | |
1776 | | FAIL_IF_NOT(SCLogPrintFDFilters() == 5); |
1777 | | |
1778 | | SCLogRemoveFDFilter("bamboo"); |
1779 | | SCLogRemoveFDFilter("soo"); |
1780 | | SCLogRemoveFDFilter("foo"); |
1781 | | SCLogRemoveFDFilter("noo"); |
1782 | | |
1783 | | FAIL_IF_NOT(SCLogPrintFDFilters() == 2); |
1784 | | |
1785 | | SCLogDeInitLogModule(); |
1786 | | |
1787 | | PASS; |
1788 | | } |
1789 | | |
1790 | | static int SCLogTestInit05(void) |
1791 | | { |
1792 | | char str[4096]; |
1793 | | memset(str, 'A', sizeof(str)); |
1794 | | str[sizeof(str) - 1] = '\0'; |
1795 | | SCLogInfo("%s", str); |
1796 | | |
1797 | | PASS; |
1798 | | } |
1799 | | |
1800 | | #endif /* UNITTESTS */ |
1801 | | |
1802 | | void SCLogRegisterTests(void) |
1803 | 0 | { |
1804 | |
|
1805 | | #ifdef UNITTESTS |
1806 | | |
1807 | | UtRegisterTest("SCLogTestInit01", SCLogTestInit01); |
1808 | | UtRegisterTest("SCLogTestInit02", SCLogTestInit02); |
1809 | | UtRegisterTest("SCLogTestInit03", SCLogTestInit03); |
1810 | | UtRegisterTest("SCLogTestInit04", SCLogTestInit04); |
1811 | | UtRegisterTest("SCLogTestInit05", SCLogTestInit05); |
1812 | | |
1813 | | #endif /* UNITTESTS */ |
1814 | |
|
1815 | 0 | return; |
1816 | 0 | } |