Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Shared versions of debug(), log(), etc. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3 | */ |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 4 | |
| 5 | #include "includes.h" |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 6 | RCSID("$OpenBSD: log.c,v 1.7 2000/01/04 00:07:59 markus Exp $"); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 7 | |
| 8 | #include "ssh.h" |
| 9 | #include "xmalloc.h" |
| 10 | |
| 11 | /* Fatal messages. This function never returns. */ |
| 12 | |
| 13 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 14 | fatal(const char *fmt,...) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 15 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 16 | va_list args; |
| 17 | va_start(args, fmt); |
| 18 | do_log(SYSLOG_LEVEL_FATAL, fmt, args); |
| 19 | va_end(args); |
| 20 | fatal_cleanup(); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | /* Error messages that should be logged. */ |
| 24 | |
| 25 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 26 | error(const char *fmt,...) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 27 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 28 | va_list args; |
| 29 | va_start(args, fmt); |
| 30 | do_log(SYSLOG_LEVEL_ERROR, fmt, args); |
| 31 | va_end(args); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* Log this message (information that usually should go to the log). */ |
| 35 | |
| 36 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 37 | log(const char *fmt,...) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 38 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 39 | va_list args; |
| 40 | va_start(args, fmt); |
| 41 | do_log(SYSLOG_LEVEL_INFO, fmt, args); |
| 42 | va_end(args); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* More detailed messages (information that does not need to go to the log). */ |
| 46 | |
| 47 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 48 | verbose(const char *fmt,...) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 49 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 50 | va_list args; |
| 51 | va_start(args, fmt); |
| 52 | do_log(SYSLOG_LEVEL_VERBOSE, fmt, args); |
| 53 | va_end(args); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* Debugging messages that should not be logged during normal operation. */ |
| 57 | |
| 58 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 59 | debug(const char *fmt,...) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 60 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 61 | va_list args; |
| 62 | va_start(args, fmt); |
| 63 | do_log(SYSLOG_LEVEL_DEBUG, fmt, args); |
| 64 | va_end(args); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* Fatal cleanup */ |
| 68 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 69 | struct fatal_cleanup { |
| 70 | struct fatal_cleanup *next; |
| 71 | void (*proc) (void *); |
| 72 | void *context; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static struct fatal_cleanup *fatal_cleanups = NULL; |
| 76 | |
| 77 | /* Registers a cleanup function to be called by fatal() before exiting. */ |
| 78 | |
| 79 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 80 | fatal_add_cleanup(void (*proc) (void *), void *context) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 81 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 82 | struct fatal_cleanup *cu; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 83 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 84 | cu = xmalloc(sizeof(*cu)); |
| 85 | cu->proc = proc; |
| 86 | cu->context = context; |
| 87 | cu->next = fatal_cleanups; |
| 88 | fatal_cleanups = cu; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Removes a cleanup frunction to be called at fatal(). */ |
| 92 | |
| 93 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | fatal_remove_cleanup(void (*proc) (void *context), void *context) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 95 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 96 | struct fatal_cleanup **cup, *cu; |
| 97 | |
| 98 | for (cup = &fatal_cleanups; *cup; cup = &cu->next) { |
| 99 | cu = *cup; |
| 100 | if (cu->proc == proc && cu->context == context) { |
| 101 | *cup = cu->next; |
| 102 | xfree(cu); |
| 103 | return; |
| 104 | } |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 105 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 106 | fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n", |
| 107 | (unsigned long) proc, (unsigned long) context); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* Cleanup and exit */ |
| 111 | void |
| 112 | fatal_cleanup(void) |
| 113 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 114 | struct fatal_cleanup *cu, *next_cu; |
| 115 | static int called = 0; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 116 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | if (called) |
| 118 | exit(255); |
| 119 | called = 1; |
| 120 | /* Call cleanup functions. */ |
| 121 | for (cu = fatal_cleanups; cu; cu = next_cu) { |
| 122 | next_cu = cu->next; |
| 123 | debug("Calling cleanup 0x%lx(0x%lx)", |
| 124 | (unsigned long) cu->proc, (unsigned long) cu->context); |
| 125 | (*cu->proc) (cu->context); |
| 126 | } |
| 127 | exit(255); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 128 | } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 129 | |
| 130 | /* textual representation of log-facilities/levels */ |
| 131 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 132 | static struct { |
| 133 | const char *name; |
| 134 | SyslogFacility val; |
| 135 | } log_facilities[] = { |
| 136 | { "DAEMON", SYSLOG_FACILITY_DAEMON }, |
| 137 | { "USER", SYSLOG_FACILITY_USER }, |
| 138 | { "AUTH", SYSLOG_FACILITY_AUTH }, |
| 139 | { "LOCAL0", SYSLOG_FACILITY_LOCAL0 }, |
| 140 | { "LOCAL1", SYSLOG_FACILITY_LOCAL1 }, |
| 141 | { "LOCAL2", SYSLOG_FACILITY_LOCAL2 }, |
| 142 | { "LOCAL3", SYSLOG_FACILITY_LOCAL3 }, |
| 143 | { "LOCAL4", SYSLOG_FACILITY_LOCAL4 }, |
| 144 | { "LOCAL5", SYSLOG_FACILITY_LOCAL5 }, |
| 145 | { "LOCAL6", SYSLOG_FACILITY_LOCAL6 }, |
| 146 | { "LOCAL7", SYSLOG_FACILITY_LOCAL7 }, |
| 147 | { NULL, 0 } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 148 | }; |
| 149 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | static struct { |
| 151 | const char *name; |
| 152 | LogLevel val; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 153 | } log_levels[] = |
| 154 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 155 | { "QUIET", SYSLOG_LEVEL_QUIET }, |
| 156 | { "FATAL", SYSLOG_LEVEL_FATAL }, |
| 157 | { "ERROR", SYSLOG_LEVEL_ERROR }, |
| 158 | { "INFO", SYSLOG_LEVEL_INFO }, |
| 159 | { "VERBOSE", SYSLOG_LEVEL_VERBOSE }, |
| 160 | { "DEBUG", SYSLOG_LEVEL_DEBUG }, |
| 161 | { NULL, 0 } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | SyslogFacility |
| 165 | log_facility_number(char *name) |
| 166 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 167 | int i; |
| 168 | if (name != NULL) |
| 169 | for (i = 0; log_facilities[i].name; i++) |
| 170 | if (strcasecmp(log_facilities[i].name, name) == 0) |
| 171 | return log_facilities[i].val; |
| 172 | return (SyslogFacility) - 1; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | LogLevel |
| 176 | log_level_number(char *name) |
| 177 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 178 | int i; |
| 179 | if (name != NULL) |
| 180 | for (i = 0; log_levels[i].name; i++) |
| 181 | if (strcasecmp(log_levels[i].name, name) == 0) |
| 182 | return log_levels[i].val; |
| 183 | return (LogLevel) - 1; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 184 | } |