log.h revision 1.19 1 1.15 christos /* $NetBSD: log.h,v 1.19 2025/04/09 15:49:32 christos Exp $ */
2 1.19 christos /* $OpenBSD: log.h,v 1.35 2024/12/07 10:05:37 djm Exp $ */
3 1.1 christos
4 1.1 christos /*
5 1.1 christos * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
6 1.1 christos * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland
7 1.1 christos * All rights reserved
8 1.1 christos *
9 1.1 christos * As far as I am concerned, the code I have written for this software
10 1.1 christos * can be used freely for any purpose. Any derived versions of this
11 1.1 christos * software must be clearly marked as such, and if the derived work is
12 1.1 christos * incompatible with the protocol description in the RFC file, it must be
13 1.1 christos * called by a name other than "ssh" or "Secure Shell".
14 1.1 christos */
15 1.1 christos
16 1.1 christos #ifndef SSH_LOG_H
17 1.1 christos #define SSH_LOG_H
18 1.1 christos
19 1.15 christos #include <stdarg.h> /* va_list */
20 1.17 christos #include "ssherr.h" /* ssh_err() */
21 1.15 christos
22 1.1 christos /* Supported syslog facilities and levels. */
23 1.1 christos typedef enum {
24 1.1 christos SYSLOG_FACILITY_DAEMON,
25 1.1 christos SYSLOG_FACILITY_USER,
26 1.1 christos SYSLOG_FACILITY_AUTH,
27 1.1 christos SYSLOG_FACILITY_LOCAL0,
28 1.1 christos SYSLOG_FACILITY_LOCAL1,
29 1.1 christos SYSLOG_FACILITY_LOCAL2,
30 1.1 christos SYSLOG_FACILITY_LOCAL3,
31 1.1 christos SYSLOG_FACILITY_LOCAL4,
32 1.1 christos SYSLOG_FACILITY_LOCAL5,
33 1.1 christos SYSLOG_FACILITY_LOCAL6,
34 1.1 christos SYSLOG_FACILITY_LOCAL7,
35 1.1 christos SYSLOG_FACILITY_NOT_SET = -1
36 1.1 christos } SyslogFacility;
37 1.1 christos
38 1.1 christos typedef enum {
39 1.1 christos SYSLOG_LEVEL_QUIET,
40 1.1 christos SYSLOG_LEVEL_FATAL,
41 1.1 christos SYSLOG_LEVEL_ERROR,
42 1.1 christos SYSLOG_LEVEL_INFO,
43 1.1 christos SYSLOG_LEVEL_VERBOSE,
44 1.1 christos SYSLOG_LEVEL_DEBUG1,
45 1.1 christos SYSLOG_LEVEL_DEBUG2,
46 1.1 christos SYSLOG_LEVEL_DEBUG3,
47 1.1 christos SYSLOG_LEVEL_NOT_SET = -1
48 1.1 christos } LogLevel;
49 1.1 christos
50 1.17 christos typedef void (log_handler_fn)(LogLevel, int, const char *, void *);
51 1.4 christos
52 1.3 christos void log_init(const char *, LogLevel, SyslogFacility, int);
53 1.14 christos LogLevel log_level_get(void);
54 1.14 christos int log_change_level(LogLevel);
55 1.5 christos int log_is_on_stderr(void);
56 1.6 christos void log_redirect_stderr_to(const char *);
57 1.16 christos void log_verbose_add(const char *);
58 1.16 christos void log_verbose_reset(void);
59 1.1 christos
60 1.1 christos SyslogFacility log_facility_number(char *);
61 1.17 christos const char * log_facility_name(SyslogFacility);
62 1.1 christos LogLevel log_level_number(char *);
63 1.1 christos const char * log_level_name(LogLevel);
64 1.1 christos
65 1.16 christos void set_log_handler(log_handler_fn *, void *);
66 1.16 christos void cleanup_exit(int) __attribute__((noreturn));
67 1.1 christos
68 1.16 christos void sshlog(const char *, const char *, int, int,
69 1.16 christos LogLevel, const char *, const char *, ...)
70 1.16 christos __attribute__((format(printf, 7, 8)));
71 1.16 christos void sshlogv(const char *, const char *, int, int,
72 1.16 christos LogLevel, const char *, const char *, va_list);
73 1.16 christos void sshlogdie(const char *, const char *, int, int,
74 1.16 christos LogLevel, const char *, const char *, ...) __attribute__((noreturn))
75 1.16 christos __attribute__((format(printf, 7, 8)));
76 1.16 christos void sshfatal(const char *, const char *, int, int,
77 1.16 christos LogLevel, const char *, const char *, ...) __attribute__((noreturn))
78 1.16 christos __attribute__((format(printf, 7, 8)));
79 1.17 christos void sshlogdirect(LogLevel, int, const char *, ...)
80 1.17 christos __attribute__((format(printf, 3, 4)));
81 1.16 christos
82 1.19 christos struct log_ratelimit_ctx {
83 1.19 christos /* configuration */
84 1.19 christos u_int threshold; /* events per second */
85 1.19 christos u_int max_accum; /* max events to accumulate */
86 1.19 christos u_int hysteresis; /* seconds */
87 1.19 christos u_int log_every; /* seconds */
88 1.19 christos
89 1.19 christos /* state */
90 1.19 christos time_t last_event;
91 1.19 christos u_int accumulated_events; /* used for threshold comparisons */
92 1.19 christos
93 1.19 christos /* state while actively rate-limiting */
94 1.19 christos int ratelimit_active;
95 1.19 christos time_t ratelimit_start;
96 1.19 christos time_t last_log;
97 1.19 christos time_t hysteresis_start;
98 1.19 christos u_int ratelimited_events;
99 1.19 christos };
100 1.19 christos
101 1.19 christos void log_ratelimit_init(struct log_ratelimit_ctx *rl, u_int threshold,
102 1.19 christos u_int max_accum, u_int hysteresis, u_int log_every);
103 1.19 christos int log_ratelimit(struct log_ratelimit_ctx *rl, time_t now, int *active,
104 1.19 christos u_int *events_dropped);
105 1.19 christos
106 1.16 christos #define do_log2(level, ...) sshlog(__FILE__, __func__, __LINE__, 0, level, NULL, __VA_ARGS__)
107 1.16 christos #define debug3(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG3, NULL, __VA_ARGS__)
108 1.16 christos #define debug2(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG2, NULL, __VA_ARGS__)
109 1.16 christos #define debug(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG1, NULL, __VA_ARGS__)
110 1.16 christos #define verbose(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_VERBOSE, NULL, __VA_ARGS__)
111 1.16 christos #define logit(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_INFO, NULL, __VA_ARGS__)
112 1.16 christos #define error(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
113 1.16 christos #define fatal(...) sshfatal(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_FATAL, NULL, __VA_ARGS__)
114 1.16 christos #define logdie(...) sshlogdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
115 1.16 christos
116 1.16 christos /* Variants that prepend the caller's function */
117 1.16 christos #define do_log2_f(level, ...) sshlog(__FILE__, __func__, __LINE__, 1, level, NULL, __VA_ARGS__)
118 1.16 christos #define debug3_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG3, NULL, __VA_ARGS__)
119 1.16 christos #define debug2_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG2, NULL, __VA_ARGS__)
120 1.16 christos #define debug_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG1, NULL, __VA_ARGS__)
121 1.16 christos #define verbose_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_VERBOSE, NULL, __VA_ARGS__)
122 1.16 christos #define logit_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_INFO, NULL, __VA_ARGS__)
123 1.16 christos #define error_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
124 1.16 christos #define fatal_f(...) sshfatal(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_FATAL, NULL, __VA_ARGS__)
125 1.16 christos #define logdie_f(...) sshlogdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
126 1.16 christos
127 1.16 christos /* Variants that appends a ssh_err message */
128 1.16 christos #define do_log2_r(r, level, ...) sshlog(__FILE__, __func__, __LINE__, 0, level, ssh_err(r), __VA_ARGS__)
129 1.16 christos #define debug3_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG3, ssh_err(r), __VA_ARGS__)
130 1.16 christos #define debug2_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG2, ssh_err(r), __VA_ARGS__)
131 1.16 christos #define debug_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG1, ssh_err(r), __VA_ARGS__)
132 1.16 christos #define verbose_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_VERBOSE, ssh_err(r), __VA_ARGS__)
133 1.16 christos #define logit_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_INFO, ssh_err(r), __VA_ARGS__)
134 1.16 christos #define error_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
135 1.16 christos #define fatal_r(r, ...) sshfatal(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_FATAL, ssh_err(r), __VA_ARGS__)
136 1.16 christos #define logdie_r(r, ...) sshlogdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
137 1.16 christos #define do_log2_fr(r, level, ...) sshlog(__FILE__, __func__, __LINE__, 1, level, ssh_err(r), __VA_ARGS__)
138 1.16 christos #define debug3_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG3, ssh_err(r), __VA_ARGS__)
139 1.16 christos #define debug2_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG2, ssh_err(r), __VA_ARGS__)
140 1.16 christos #define debug_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG1, ssh_err(r), __VA_ARGS__)
141 1.16 christos #define verbose_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_VERBOSE, ssh_err(r), __VA_ARGS__)
142 1.16 christos #define logit_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_INFO, ssh_err(r), __VA_ARGS__)
143 1.16 christos #define error_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
144 1.16 christos #define fatal_fr(r, ...) sshfatal(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_FATAL, ssh_err(r), __VA_ARGS__)
145 1.16 christos #define logdie_fr(r, ...) sshlogdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
146 1.4 christos
147 1.1 christos #endif
148