Line | Count | Source |
1 | | /* $OpenBSD: log.c,v 1.67 2026/02/14 00:18:34 jsg Exp $ */ |
2 | | /* |
3 | | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
4 | | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
5 | | * All rights reserved |
6 | | * |
7 | | * As far as I am concerned, the code I have written for this software |
8 | | * can be used freely for any purpose. Any derived versions of this |
9 | | * software must be clearly marked as such, and if the derived work is |
10 | | * incompatible with the protocol description in the RFC file, it must be |
11 | | * called by a name other than "ssh" or "Secure Shell". |
12 | | */ |
13 | | /* |
14 | | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
15 | | * |
16 | | * Redistribution and use in source and binary forms, with or without |
17 | | * modification, are permitted provided that the following conditions |
18 | | * are met: |
19 | | * 1. Redistributions of source code must retain the above copyright |
20 | | * notice, this list of conditions and the following disclaimer. |
21 | | * 2. Redistributions in binary form must reproduce the above copyright |
22 | | * notice, this list of conditions and the following disclaimer in the |
23 | | * documentation and/or other materials provided with the distribution. |
24 | | * |
25 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
26 | | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
27 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
28 | | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
29 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
30 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
31 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
32 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
33 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
34 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
35 | | */ |
36 | | |
37 | | #include "includes.h" |
38 | | |
39 | | #include <sys/types.h> |
40 | | |
41 | | #include <errno.h> |
42 | | #include <fcntl.h> |
43 | | #include <limits.h> |
44 | | #include <stdarg.h> |
45 | | #include <stdio.h> |
46 | | #include <stdlib.h> |
47 | | #include <string.h> |
48 | | #include <syslog.h> |
49 | | #include <unistd.h> |
50 | | #include <errno.h> |
51 | | #include "packet.h" /* needed for host and port look ups */ |
52 | | #ifdef HAVE_SYS_TIME_H |
53 | | # include <sys/time.h> /* to get current time */ |
54 | | #endif |
55 | | #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS) |
56 | | # include <vis.h> |
57 | | #endif |
58 | | |
59 | | #include "log.h" |
60 | | #include "match.h" |
61 | | |
62 | | static LogLevel log_level = SYSLOG_LEVEL_INFO; |
63 | | static int log_on_stderr = 1; |
64 | | static int log_stderr_fd = STDERR_FILENO; |
65 | | static int log_facility = LOG_AUTH; |
66 | | static const char *argv0; |
67 | | static log_handler_fn *log_handler; |
68 | | static void *log_handler_ctx; |
69 | | static char **log_verbose; |
70 | | static size_t nlog_verbose; |
71 | | extern char *__progname; |
72 | | |
73 | | extern struct ssh *active_state; |
74 | | |
75 | 0 | #define LOG_SYSLOG_VIS (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL) |
76 | 0 | #define LOG_STDERR_VIS (VIS_SAFE|VIS_OCTAL) |
77 | | |
78 | | /* textual representation of log-facilities/levels */ |
79 | | |
80 | | static struct { |
81 | | const char *name; |
82 | | SyslogFacility val; |
83 | | } log_facilities[] = { |
84 | | { "DAEMON", SYSLOG_FACILITY_DAEMON }, |
85 | | { "USER", SYSLOG_FACILITY_USER }, |
86 | | { "AUTH", SYSLOG_FACILITY_AUTH }, |
87 | | #ifdef LOG_AUTHPRIV |
88 | | { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV }, |
89 | | #endif |
90 | | { "LOCAL0", SYSLOG_FACILITY_LOCAL0 }, |
91 | | { "LOCAL1", SYSLOG_FACILITY_LOCAL1 }, |
92 | | { "LOCAL2", SYSLOG_FACILITY_LOCAL2 }, |
93 | | { "LOCAL3", SYSLOG_FACILITY_LOCAL3 }, |
94 | | { "LOCAL4", SYSLOG_FACILITY_LOCAL4 }, |
95 | | { "LOCAL5", SYSLOG_FACILITY_LOCAL5 }, |
96 | | { "LOCAL6", SYSLOG_FACILITY_LOCAL6 }, |
97 | | { "LOCAL7", SYSLOG_FACILITY_LOCAL7 }, |
98 | | { NULL, SYSLOG_FACILITY_NOT_SET } |
99 | | }; |
100 | | |
101 | | static struct { |
102 | | const char *name; |
103 | | LogLevel val; |
104 | | } log_levels[] = |
105 | | { |
106 | | { "QUIET", SYSLOG_LEVEL_QUIET }, |
107 | | { "FATAL", SYSLOG_LEVEL_FATAL }, |
108 | | { "ERROR", SYSLOG_LEVEL_ERROR }, |
109 | | { "INFO", SYSLOG_LEVEL_INFO }, |
110 | | { "VERBOSE", SYSLOG_LEVEL_VERBOSE }, |
111 | | { "DEBUG", SYSLOG_LEVEL_DEBUG1 }, |
112 | | { "DEBUG1", SYSLOG_LEVEL_DEBUG1 }, |
113 | | { "DEBUG2", SYSLOG_LEVEL_DEBUG2 }, |
114 | | { "DEBUG3", SYSLOG_LEVEL_DEBUG3 }, |
115 | | { NULL, SYSLOG_LEVEL_NOT_SET } |
116 | | }; |
117 | | |
118 | | LogLevel |
119 | | log_level_get(void) |
120 | 0 | { |
121 | 0 | return log_level; |
122 | 0 | } |
123 | | |
124 | | SyslogFacility |
125 | | log_facility_number(char *name) |
126 | 0 | { |
127 | 0 | int i; |
128 | |
|
129 | 0 | if (name != NULL) |
130 | 0 | for (i = 0; log_facilities[i].name; i++) |
131 | 0 | if (strcasecmp(log_facilities[i].name, name) == 0) |
132 | 0 | return log_facilities[i].val; |
133 | 0 | return SYSLOG_FACILITY_NOT_SET; |
134 | 0 | } |
135 | | |
136 | | const char * |
137 | | log_facility_name(SyslogFacility facility) |
138 | 0 | { |
139 | 0 | u_int i; |
140 | |
|
141 | 0 | for (i = 0; log_facilities[i].name; i++) |
142 | 0 | if (log_facilities[i].val == facility) |
143 | 0 | return log_facilities[i].name; |
144 | 0 | return NULL; |
145 | 0 | } |
146 | | |
147 | | LogLevel |
148 | | log_level_number(char *name) |
149 | 0 | { |
150 | 0 | int i; |
151 | |
|
152 | 0 | if (name != NULL) |
153 | 0 | for (i = 0; log_levels[i].name; i++) |
154 | 0 | if (strcasecmp(log_levels[i].name, name) == 0) |
155 | 0 | return log_levels[i].val; |
156 | 0 | return SYSLOG_LEVEL_NOT_SET; |
157 | 0 | } |
158 | | |
159 | | const char * |
160 | | log_level_name(LogLevel level) |
161 | 0 | { |
162 | 0 | u_int i; |
163 | |
|
164 | 0 | for (i = 0; log_levels[i].name != NULL; i++) |
165 | 0 | if (log_levels[i].val == level) |
166 | 0 | return log_levels[i].name; |
167 | 0 | return NULL; |
168 | 0 | } |
169 | | |
170 | | void |
171 | | log_verbose_add(const char *s) |
172 | 0 | { |
173 | 0 | char **tmp; |
174 | | |
175 | | /* Ignore failures here */ |
176 | 0 | if ((tmp = recallocarray(log_verbose, nlog_verbose, nlog_verbose + 1, |
177 | 0 | sizeof(*log_verbose))) != NULL) { |
178 | 0 | log_verbose = tmp; |
179 | 0 | if ((log_verbose[nlog_verbose] = strdup(s)) != NULL) |
180 | 0 | nlog_verbose++; |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | | void |
185 | | log_verbose_reset(void) |
186 | 0 | { |
187 | 0 | size_t i; |
188 | |
|
189 | 0 | for (i = 0; i < nlog_verbose; i++) |
190 | 0 | free(log_verbose[i]); |
191 | 0 | free(log_verbose); |
192 | 0 | log_verbose = NULL; |
193 | 0 | nlog_verbose = 0; |
194 | 0 | } |
195 | | |
196 | | /* |
197 | | * Initialize the log. |
198 | | */ |
199 | | |
200 | | void |
201 | | log_init(const char *av0, LogLevel level, SyslogFacility facility, |
202 | | int on_stderr) |
203 | 2.30k | { |
204 | | #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) |
205 | | struct syslog_data sdata = SYSLOG_DATA_INIT; |
206 | | #endif |
207 | | |
208 | 2.30k | argv0 = av0; |
209 | | |
210 | 2.30k | if (log_change_level(level) != 0) { |
211 | 0 | fprintf(stderr, "Unrecognized internal syslog level code %d\n", |
212 | 0 | (int) level); |
213 | 0 | exit(1); |
214 | 0 | } |
215 | | |
216 | 2.30k | log_handler = NULL; |
217 | 2.30k | log_handler_ctx = NULL; |
218 | | |
219 | 2.30k | log_on_stderr = on_stderr; |
220 | 2.30k | if (on_stderr) |
221 | 2.30k | return; |
222 | | |
223 | 0 | switch (facility) { |
224 | 0 | case SYSLOG_FACILITY_DAEMON: |
225 | 0 | log_facility = LOG_DAEMON; |
226 | 0 | break; |
227 | 0 | case SYSLOG_FACILITY_USER: |
228 | 0 | log_facility = LOG_USER; |
229 | 0 | break; |
230 | 0 | case SYSLOG_FACILITY_AUTH: |
231 | 0 | log_facility = LOG_AUTH; |
232 | 0 | break; |
233 | 0 | #ifdef LOG_AUTHPRIV |
234 | 0 | case SYSLOG_FACILITY_AUTHPRIV: |
235 | 0 | log_facility = LOG_AUTHPRIV; |
236 | 0 | break; |
237 | 0 | #endif |
238 | 0 | case SYSLOG_FACILITY_LOCAL0: |
239 | 0 | log_facility = LOG_LOCAL0; |
240 | 0 | break; |
241 | 0 | case SYSLOG_FACILITY_LOCAL1: |
242 | 0 | log_facility = LOG_LOCAL1; |
243 | 0 | break; |
244 | 0 | case SYSLOG_FACILITY_LOCAL2: |
245 | 0 | log_facility = LOG_LOCAL2; |
246 | 0 | break; |
247 | 0 | case SYSLOG_FACILITY_LOCAL3: |
248 | 0 | log_facility = LOG_LOCAL3; |
249 | 0 | break; |
250 | 0 | case SYSLOG_FACILITY_LOCAL4: |
251 | 0 | log_facility = LOG_LOCAL4; |
252 | 0 | break; |
253 | 0 | case SYSLOG_FACILITY_LOCAL5: |
254 | 0 | log_facility = LOG_LOCAL5; |
255 | 0 | break; |
256 | 0 | case SYSLOG_FACILITY_LOCAL6: |
257 | 0 | log_facility = LOG_LOCAL6; |
258 | 0 | break; |
259 | 0 | case SYSLOG_FACILITY_LOCAL7: |
260 | 0 | log_facility = LOG_LOCAL7; |
261 | 0 | break; |
262 | 0 | default: |
263 | 0 | fprintf(stderr, |
264 | 0 | "Unrecognized internal syslog facility code %d\n", |
265 | 0 | (int) facility); |
266 | 0 | exit(1); |
267 | 0 | } |
268 | | |
269 | | /* |
270 | | * If an external library (eg libwrap) attempts to use syslog |
271 | | * immediately after reexec, syslog may be pointing to the wrong |
272 | | * facility, so we force an open/close of syslog here. |
273 | | */ |
274 | | #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) |
275 | | openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata); |
276 | | closelog_r(&sdata); |
277 | | #else |
278 | 0 | openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility); |
279 | 0 | closelog(); |
280 | 0 | #endif |
281 | 0 | } |
282 | | |
283 | | int |
284 | | log_change_level(LogLevel new_log_level) |
285 | 2.30k | { |
286 | | /* no-op if log_init has not been called */ |
287 | 2.30k | if (argv0 == NULL) |
288 | 0 | return 0; |
289 | | |
290 | 2.30k | switch (new_log_level) { |
291 | 2.30k | case SYSLOG_LEVEL_QUIET: |
292 | 2.30k | case SYSLOG_LEVEL_FATAL: |
293 | 2.30k | case SYSLOG_LEVEL_ERROR: |
294 | 2.30k | case SYSLOG_LEVEL_INFO: |
295 | 2.30k | case SYSLOG_LEVEL_VERBOSE: |
296 | 2.30k | case SYSLOG_LEVEL_DEBUG1: |
297 | 2.30k | case SYSLOG_LEVEL_DEBUG2: |
298 | 2.30k | case SYSLOG_LEVEL_DEBUG3: |
299 | 2.30k | log_level = new_log_level; |
300 | 2.30k | return 0; |
301 | 0 | default: |
302 | 0 | return -1; |
303 | 2.30k | } |
304 | 2.30k | } |
305 | | |
306 | | int |
307 | | log_is_on_stderr(void) |
308 | 0 | { |
309 | 0 | return log_on_stderr && log_stderr_fd == STDERR_FILENO; |
310 | 0 | } |
311 | | |
312 | | /* redirect what would usually get written to stderr to specified file */ |
313 | | void |
314 | | log_redirect_stderr_to(const char *logfile) |
315 | 0 | { |
316 | 0 | int fd; |
317 | |
|
318 | 0 | if (logfile == NULL) { |
319 | 0 | if (log_stderr_fd != STDERR_FILENO) { |
320 | 0 | close(log_stderr_fd); |
321 | 0 | log_stderr_fd = STDERR_FILENO; |
322 | 0 | } |
323 | 0 | return; |
324 | 0 | } |
325 | | |
326 | 0 | if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600)) == -1) { |
327 | 0 | fprintf(stderr, "Couldn't open logfile %s: %s\n", logfile, |
328 | 0 | strerror(errno)); |
329 | 0 | exit(1); |
330 | 0 | } |
331 | 0 | log_stderr_fd = fd; |
332 | 0 | } |
333 | | |
334 | | #define MSGBUFSIZ 1024 |
335 | | |
336 | | void |
337 | | set_log_handler(log_handler_fn *handler, void *ctx) |
338 | 0 | { |
339 | 0 | log_handler = handler; |
340 | 0 | log_handler_ctx = ctx; |
341 | 0 | } |
342 | | |
343 | | static void |
344 | | do_log(LogLevel level, int force, const char *suffix, const char *fmt, |
345 | | va_list args) |
346 | 0 | { |
347 | | #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) |
348 | | struct syslog_data sdata = SYSLOG_DATA_INIT; |
349 | | #endif |
350 | 0 | char msgbuf[MSGBUFSIZ]; |
351 | 0 | char fmtbuf[MSGBUFSIZ]; |
352 | 0 | char *txt = NULL; |
353 | 0 | int pri = LOG_INFO; |
354 | 0 | int saved_errno = errno; |
355 | 0 | log_handler_fn *tmp_handler; |
356 | 0 | const char *progname = argv0 != NULL ? argv0 : __progname; |
357 | |
|
358 | 0 | if (!force && level > log_level) |
359 | 0 | return; |
360 | | |
361 | 0 | switch (level) { |
362 | 0 | case SYSLOG_LEVEL_FATAL: |
363 | 0 | if (!log_on_stderr) |
364 | 0 | txt = "fatal"; |
365 | 0 | pri = LOG_CRIT; |
366 | 0 | break; |
367 | 0 | case SYSLOG_LEVEL_ERROR: |
368 | 0 | if (!log_on_stderr) |
369 | 0 | txt = "error"; |
370 | 0 | pri = LOG_ERR; |
371 | 0 | break; |
372 | 0 | case SYSLOG_LEVEL_INFO: |
373 | 0 | pri = LOG_INFO; |
374 | 0 | break; |
375 | 0 | case SYSLOG_LEVEL_VERBOSE: |
376 | 0 | pri = LOG_INFO; |
377 | 0 | break; |
378 | 0 | case SYSLOG_LEVEL_DEBUG1: |
379 | 0 | txt = "debug1"; |
380 | 0 | pri = LOG_DEBUG; |
381 | 0 | break; |
382 | 0 | case SYSLOG_LEVEL_DEBUG2: |
383 | 0 | txt = "debug2"; |
384 | 0 | pri = LOG_DEBUG; |
385 | 0 | break; |
386 | 0 | case SYSLOG_LEVEL_DEBUG3: |
387 | 0 | txt = "debug3"; |
388 | 0 | pri = LOG_DEBUG; |
389 | 0 | break; |
390 | 0 | default: |
391 | 0 | txt = "internal error"; |
392 | 0 | pri = LOG_ERR; |
393 | 0 | break; |
394 | 0 | } |
395 | 0 | if (txt != NULL && log_handler == NULL) { |
396 | 0 | snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt); |
397 | 0 | vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args); |
398 | 0 | } else { |
399 | 0 | vsnprintf(msgbuf, sizeof(msgbuf), fmt, args); |
400 | 0 | } |
401 | 0 | if (suffix != NULL) { |
402 | 0 | snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", msgbuf, suffix); |
403 | 0 | strlcpy(msgbuf, fmtbuf, sizeof(msgbuf)); |
404 | 0 | } |
405 | 0 | strnvis(fmtbuf, msgbuf, sizeof(fmtbuf), |
406 | 0 | log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS); |
407 | 0 | if (log_handler != NULL) { |
408 | | /* Avoid recursion */ |
409 | 0 | tmp_handler = log_handler; |
410 | 0 | log_handler = NULL; |
411 | | /* Note: this sends the raw (i.e. no strnvis) log message */ |
412 | 0 | tmp_handler(level, force, msgbuf, log_handler_ctx); |
413 | 0 | log_handler = tmp_handler; |
414 | 0 | } else if (log_on_stderr) { |
415 | 0 | snprintf(msgbuf, sizeof msgbuf, "%s%s%.*s\r\n", |
416 | 0 | (log_on_stderr > 1) ? progname : "", |
417 | 0 | (log_on_stderr > 1) ? ": " : "", |
418 | 0 | (int)sizeof msgbuf - 3, fmtbuf); |
419 | 0 | (void)write(log_stderr_fd, msgbuf, strlen(msgbuf)); |
420 | 0 | } else { |
421 | | #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) |
422 | | openlog_r(progname, LOG_PID, log_facility, &sdata); |
423 | | syslog_r(pri, &sdata, "%.500s", fmtbuf); |
424 | | closelog_r(&sdata); |
425 | | #else |
426 | 0 | openlog(progname, LOG_PID, log_facility); |
427 | 0 | syslog(pri, "%.500s", fmtbuf); |
428 | 0 | closelog(); |
429 | 0 | #endif |
430 | 0 | } |
431 | 0 | errno = saved_errno; |
432 | 0 | } |
433 | | |
434 | | void |
435 | | sshlog(const char *file, const char *func, int line, int showfunc, |
436 | | LogLevel level, const char *suffix, const char *fmt, ...) |
437 | 8.02k | { |
438 | 8.02k | va_list args; |
439 | | |
440 | 8.02k | va_start(args, fmt); |
441 | 8.02k | sshlogv(file, func, line, showfunc, level, suffix, fmt, args); |
442 | 8.02k | va_end(args); |
443 | 8.02k | } |
444 | | |
445 | | void |
446 | | sshlogdie(const char *file, const char *func, int line, int showfunc, |
447 | | LogLevel level, const char *suffix, const char *fmt, ...) |
448 | 0 | { |
449 | 0 | va_list args; |
450 | |
|
451 | 0 | va_start(args, fmt); |
452 | 0 | sshlogv(file, func, line, showfunc, SYSLOG_LEVEL_INFO, |
453 | 0 | suffix, fmt, args); |
454 | 0 | va_end(args); |
455 | 0 | cleanup_exit(255); |
456 | 0 | } |
457 | | |
458 | | void |
459 | | sshlogv(const char *file, const char *func, int line, int showfunc, |
460 | | LogLevel level, const char *suffix, const char *fmt, va_list args) |
461 | 8.02k | { |
462 | 8.02k | char tag[128], fmt2[MSGBUFSIZ + 128]; |
463 | 8.02k | int forced = 0; |
464 | 8.02k | const char *cp; |
465 | 8.02k | size_t i; |
466 | | |
467 | | /* short circuit processing early if we're not going to log anything */ |
468 | 8.02k | if (nlog_verbose == 0 && level > log_level) |
469 | 8.02k | return; |
470 | | |
471 | 0 | snprintf(tag, sizeof(tag), "%.48s:%.48s():%d (bin=%s, pid=%ld)", |
472 | 0 | (cp = strrchr(file, '/')) == NULL ? file : cp + 1, func, line, |
473 | 0 | argv0 == NULL ? "UNKNOWN" : argv0, (long)getpid()); |
474 | 0 | for (i = 0; i < nlog_verbose; i++) { |
475 | 0 | if (match_pattern_list(tag, log_verbose[i], 0) == 1) { |
476 | 0 | forced = 1; |
477 | 0 | break; |
478 | 0 | } |
479 | 0 | } |
480 | |
|
481 | 0 | if (forced) |
482 | 0 | snprintf(fmt2, sizeof(fmt2), "%s: %s", tag, fmt); |
483 | 0 | else if (showfunc) |
484 | 0 | snprintf(fmt2, sizeof(fmt2), "%s: %s", func, fmt); |
485 | 0 | else |
486 | 0 | strlcpy(fmt2, fmt, sizeof(fmt2)); |
487 | |
|
488 | 0 | do_log(level, forced, suffix, fmt2, args); |
489 | 0 | } |
490 | | |
491 | | void |
492 | | sshlogdirect(LogLevel level, int forced, const char *fmt, ...) |
493 | 0 | { |
494 | 0 | va_list args; |
495 | |
|
496 | 0 | va_start(args, fmt); |
497 | 0 | do_log(level, forced, NULL, fmt, args); |
498 | 0 | va_end(args); |
499 | 0 | } |
500 | | |
501 | | |
502 | | /* |
503 | | * A simple system for ratelimiting aperiodic events such as logs, without |
504 | | * needing to be hooked into a mainloop/timer. A running total of events is |
505 | | * maintained and when it exceeds a threshold further events are dropped |
506 | | * until the rate falls back below that threshold. |
507 | | * |
508 | | * To prevent flipping in and out of rate-limiting, there is a hysteresis |
509 | | * timer that delays leaving the rate-limited state. |
510 | | * |
511 | | * While in the rate-limited state, events can be periodically allowed through |
512 | | * and the number of dropped events since the last log obtained. |
513 | | * |
514 | | * XXX a moving average rate of events might be a better approach here rather |
515 | | * than linear decay, which can suppress events for a while after large |
516 | | * bursts. |
517 | | */ |
518 | | |
519 | | /* #define RATELIMIT_DEBUG 1 */ |
520 | | |
521 | | #ifdef RATELIMIT_DEBUG |
522 | | # define RLDBG(x) do { \ |
523 | | printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \ |
524 | | printf x; \ |
525 | | printf("\n"); \ |
526 | | fflush(stdout); \ |
527 | | } while (0) |
528 | | #else |
529 | | # define RLDBG(x) |
530 | | #endif |
531 | | |
532 | | /* set up a ratelimit */ |
533 | | void |
534 | | log_ratelimit_init(struct log_ratelimit_ctx *rl, u_int threshold, |
535 | | u_int max_accum, u_int hysteresis, u_int log_every) |
536 | 0 | { |
537 | 0 | memset(rl, 0, sizeof(*rl)); |
538 | 0 | rl->threshold = threshold; |
539 | 0 | rl->max_accum = max_accum; |
540 | 0 | rl->hysteresis = hysteresis; |
541 | 0 | rl->log_every = log_every; |
542 | 0 | RLDBG(("called: rl=%p thresh=%u max=%u hys=%u log_every=%u", |
543 | 0 | rl, rl->threshold, rl->max_accum, rl->hysteresis, rl->log_every)); |
544 | 0 | } |
545 | | |
546 | | /* |
547 | | * check whether a log event should be dropped because of rate-limiting. |
548 | | * returns non-zero if the event should be dropped. If events_since_last |
549 | | * is supplied then, for periodic logs, it will be set to the number of |
550 | | * dropped events since the last message. |
551 | | */ |
552 | | int |
553 | | log_ratelimit(struct log_ratelimit_ctx *rl, time_t now, int *active, |
554 | | u_int *events_dropped) |
555 | 0 | { |
556 | 0 | time_t olast_event; |
557 | |
|
558 | 0 | RLDBG(("called: rl=%p thresh=%u max=%u hys=%u log_every=%u " |
559 | 0 | "accum=%u since=%ld since_last=%u", rl, rl->threshold, |
560 | 0 | rl->max_accum, rl->hysteresis, |
561 | 0 | rl->log_every, rl->accumulated_events, |
562 | 0 | rl->last_event == 0 ? -1 : (long)(now - rl->last_event), |
563 | 0 | rl->ratelimited_events)); |
564 | |
|
565 | 0 | if (now < 0) |
566 | 0 | return 0; |
567 | 0 | if (events_dropped != NULL) |
568 | 0 | *events_dropped = 0; |
569 | 0 | if (active != NULL) |
570 | 0 | *active = rl->ratelimit_active; |
571 | | |
572 | | /* First, decay accumulated events */ |
573 | 0 | if (rl->last_event <= 0) |
574 | 0 | rl->last_event = now; |
575 | 0 | if (now > rl->last_event) { |
576 | 0 | uint64_t n = now - rl->last_event; |
577 | |
|
578 | 0 | if (n > UINT_MAX) |
579 | 0 | n = UINT_MAX; |
580 | 0 | if (rl->accumulated_events < (u_int)n) |
581 | 0 | rl->accumulated_events = 0; |
582 | 0 | else |
583 | 0 | rl->accumulated_events -= (u_int)n; |
584 | 0 | RLDBG(("decay: accum=%u", rl->accumulated_events)); |
585 | 0 | } |
586 | 0 | rl->accumulated_events++; /* add this event */ |
587 | 0 | if (rl->accumulated_events > rl->max_accum) |
588 | 0 | rl->accumulated_events = rl->max_accum; |
589 | 0 | olast_event = rl->last_event; |
590 | 0 | rl->last_event = now; |
591 | 0 | RLDBG(("check threshold: accum=%u vs thresh=%u", |
592 | 0 | rl->accumulated_events, rl->threshold)); |
593 | | |
594 | | /* Are we under threshold? */ |
595 | 0 | if (rl->accumulated_events < rl->threshold) { |
596 | 0 | if (!rl->ratelimit_active) |
597 | 0 | return 0; |
598 | 0 | RLDBG(("under threshold: hys=%u since_hys=%ld since_last=%ld", |
599 | 0 | rl->hysteresis, rl->hysteresis_start == 0 ? -1 : |
600 | 0 | (long)(now - rl->hysteresis_start), |
601 | 0 | olast_event == 0 ? -1 : (long)(now - olast_event))); |
602 | 0 | if (rl->hysteresis_start == 0) { |
603 | | /* active, but under threshold; hysteresis */ |
604 | 0 | if (olast_event + rl->hysteresis < now) { |
605 | | /* hysteresis expired before this event */ |
606 | 0 | RLDBG(("hysteresis preexpired")); |
607 | 0 | goto inactive; |
608 | 0 | } |
609 | 0 | RLDBG(("start hysteresis")); |
610 | 0 | rl->hysteresis_start = now; |
611 | 0 | } else if (rl->hysteresis_start + rl->hysteresis < now) { |
612 | | /* Hysteresis period expired, transition to inactive */ |
613 | 0 | RLDBG(("complete hysteresis")); |
614 | 0 | inactive: |
615 | 0 | if (events_dropped != NULL) |
616 | 0 | *events_dropped = rl->ratelimited_events; |
617 | 0 | if (active != NULL) |
618 | 0 | *active = 0; |
619 | 0 | rl->ratelimit_active = 0; |
620 | 0 | rl->ratelimit_start = 0; |
621 | 0 | rl->last_log = 0; |
622 | 0 | rl->hysteresis_start = 0; |
623 | 0 | rl->ratelimited_events = 0; |
624 | 0 | return 0; |
625 | 0 | } |
626 | | /* ratelimiting active, but in hysteresis period */ |
627 | 0 | } else if (!rl->ratelimit_active) { |
628 | | /* Transition to rate-limiting */ |
629 | 0 | RLDBG(("start ratelimit")); |
630 | 0 | rl->ratelimit_active = 1; |
631 | 0 | rl->ratelimit_start = now; |
632 | 0 | rl->last_log = now; |
633 | 0 | rl->hysteresis_start = 0; |
634 | 0 | rl->ratelimited_events = 1; |
635 | 0 | if (active != NULL) |
636 | 0 | *active = 1; |
637 | 0 | return 1; |
638 | 0 | } else if (rl->hysteresis_start != 0) { |
639 | | /* active and over threshold; reset hysteresis timer */ |
640 | 0 | RLDBG(("clear hysteresis")); |
641 | 0 | rl->hysteresis_start = 0; |
642 | 0 | } |
643 | | |
644 | | /* over threshold or in hysteresis period; log periodically */ |
645 | 0 | if (active != NULL) |
646 | 0 | *active = 1; |
647 | 0 | RLDBG(("log_every=%u since_log=%ld", rl->log_every, |
648 | 0 | (long)(now - rl->last_log))); |
649 | 0 | if (rl->log_every > 0 && now >= rl->last_log + rl->log_every) { |
650 | 0 | RLDBG(("periodic: since_last=%u", rl->ratelimited_events)); |
651 | 0 | rl->last_log = now; |
652 | 0 | if (events_dropped != NULL) { |
653 | 0 | *events_dropped = rl->ratelimited_events; |
654 | 0 | rl->ratelimited_events = 0; |
655 | 0 | } |
656 | 0 | return 0; |
657 | 0 | } |
658 | | |
659 | | /* drop event */ |
660 | 0 | rl->ratelimited_events++; |
661 | 0 | RLDBG(("drop: ratelimited_events=%u", rl->ratelimited_events)); |
662 | 0 | return 1; |
663 | 0 | } |