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