Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 1 | /* |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
| 5 | * |
| 6 | * As far as I am concerned, the code I have written for this software |
| 7 | * can be used freely for any purpose. Any derived versions of this |
| 8 | * software must be clearly marked as such, and if the derived work is |
| 9 | * incompatible with the protocol description in the RFC file, it must be |
| 10 | * called by a name other than "ssh" or "Secure Shell". |
| 11 | */ |
| 12 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 13 | * Shared versions of debug(), log(), etc. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 14 | * |
| 15 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * 1. Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer in the |
| 24 | * documentation and/or other materials provided with the distribution. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 28 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 29 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 31 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 36 | */ |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 37 | |
| 38 | #include "includes.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 39 | RCSID("$OpenBSD: log.c,v 1.15 2001/01/21 19:05:51 markus Exp $"); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 40 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 41 | #include "log.h" |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 42 | #include "xmalloc.h" |
| 43 | |
| 44 | /* Fatal messages. This function never returns. */ |
| 45 | |
| 46 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 47 | fatal(const char *fmt,...) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 48 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 49 | va_list args; |
| 50 | va_start(args, fmt); |
| 51 | do_log(SYSLOG_LEVEL_FATAL, fmt, args); |
| 52 | va_end(args); |
| 53 | fatal_cleanup(); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* Error messages that should be logged. */ |
| 57 | |
| 58 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 59 | error(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_ERROR, fmt, args); |
| 64 | va_end(args); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* Log this message (information that usually should go to the log). */ |
| 68 | |
| 69 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 70 | log(const char *fmt,...) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 71 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 72 | va_list args; |
| 73 | va_start(args, fmt); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 74 | do_log(SYSLOG_LEVEL_INFO, fmt, args); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 75 | va_end(args); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* More detailed messages (information that does not need to go to the log). */ |
| 79 | |
| 80 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 81 | verbose(const char *fmt,...) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 82 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 83 | va_list args; |
| 84 | va_start(args, fmt); |
| 85 | do_log(SYSLOG_LEVEL_VERBOSE, fmt, args); |
| 86 | va_end(args); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* Debugging messages that should not be logged during normal operation. */ |
| 90 | |
| 91 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 92 | debug(const char *fmt,...) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 93 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | va_list args; |
| 95 | va_start(args, fmt); |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 96 | do_log(SYSLOG_LEVEL_DEBUG1, fmt, args); |
| 97 | va_end(args); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | debug2(const char *fmt,...) |
| 102 | { |
| 103 | va_list args; |
| 104 | va_start(args, fmt); |
| 105 | do_log(SYSLOG_LEVEL_DEBUG2, fmt, args); |
| 106 | va_end(args); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | debug3(const char *fmt,...) |
| 111 | { |
| 112 | va_list args; |
| 113 | va_start(args, fmt); |
| 114 | do_log(SYSLOG_LEVEL_DEBUG3, fmt, args); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | va_end(args); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* Fatal cleanup */ |
| 119 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 120 | struct fatal_cleanup { |
| 121 | struct fatal_cleanup *next; |
| 122 | void (*proc) (void *); |
| 123 | void *context; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | static struct fatal_cleanup *fatal_cleanups = NULL; |
| 127 | |
| 128 | /* Registers a cleanup function to be called by fatal() before exiting. */ |
| 129 | |
| 130 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 131 | fatal_add_cleanup(void (*proc) (void *), void *context) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 132 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | struct fatal_cleanup *cu; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 134 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 135 | cu = xmalloc(sizeof(*cu)); |
| 136 | cu->proc = proc; |
| 137 | cu->context = context; |
| 138 | cu->next = fatal_cleanups; |
| 139 | fatal_cleanups = cu; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* Removes a cleanup frunction to be called at fatal(). */ |
| 143 | |
| 144 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 145 | fatal_remove_cleanup(void (*proc) (void *context), void *context) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 146 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 147 | struct fatal_cleanup **cup, *cu; |
| 148 | |
| 149 | for (cup = &fatal_cleanups; *cup; cup = &cu->next) { |
| 150 | cu = *cup; |
| 151 | if (cu->proc == proc && cu->context == context) { |
| 152 | *cup = cu->next; |
| 153 | xfree(cu); |
| 154 | return; |
| 155 | } |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 156 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 157 | fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n", |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 158 | (u_long) proc, (u_long) context); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* Cleanup and exit */ |
| 162 | void |
| 163 | fatal_cleanup(void) |
| 164 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 165 | struct fatal_cleanup *cu, *next_cu; |
| 166 | static int called = 0; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 167 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 168 | if (called) |
| 169 | exit(255); |
| 170 | called = 1; |
| 171 | /* Call cleanup functions. */ |
| 172 | for (cu = fatal_cleanups; cu; cu = next_cu) { |
| 173 | next_cu = cu->next; |
| 174 | debug("Calling cleanup 0x%lx(0x%lx)", |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 175 | (u_long) cu->proc, (u_long) cu->context); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 176 | (*cu->proc) (cu->context); |
| 177 | } |
| 178 | exit(255); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 179 | } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 180 | |
| 181 | /* textual representation of log-facilities/levels */ |
| 182 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 183 | static struct { |
| 184 | const char *name; |
| 185 | SyslogFacility val; |
| 186 | } log_facilities[] = { |
| 187 | { "DAEMON", SYSLOG_FACILITY_DAEMON }, |
| 188 | { "USER", SYSLOG_FACILITY_USER }, |
| 189 | { "AUTH", SYSLOG_FACILITY_AUTH }, |
Damien Miller | 6dbfef6 | 2000-11-29 13:51:06 +1100 | [diff] [blame] | 190 | #ifdef LOG_AUTHPRIV |
| 191 | { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV }, |
| 192 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 193 | { "LOCAL0", SYSLOG_FACILITY_LOCAL0 }, |
| 194 | { "LOCAL1", SYSLOG_FACILITY_LOCAL1 }, |
| 195 | { "LOCAL2", SYSLOG_FACILITY_LOCAL2 }, |
| 196 | { "LOCAL3", SYSLOG_FACILITY_LOCAL3 }, |
| 197 | { "LOCAL4", SYSLOG_FACILITY_LOCAL4 }, |
| 198 | { "LOCAL5", SYSLOG_FACILITY_LOCAL5 }, |
| 199 | { "LOCAL6", SYSLOG_FACILITY_LOCAL6 }, |
| 200 | { "LOCAL7", SYSLOG_FACILITY_LOCAL7 }, |
| 201 | { NULL, 0 } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 202 | }; |
| 203 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 204 | static struct { |
| 205 | const char *name; |
| 206 | LogLevel val; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 207 | } log_levels[] = |
| 208 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 209 | { "QUIET", SYSLOG_LEVEL_QUIET }, |
| 210 | { "FATAL", SYSLOG_LEVEL_FATAL }, |
| 211 | { "ERROR", SYSLOG_LEVEL_ERROR }, |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 212 | { "INFO", SYSLOG_LEVEL_INFO }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 213 | { "VERBOSE", SYSLOG_LEVEL_VERBOSE }, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 214 | { "DEBUG", SYSLOG_LEVEL_DEBUG1 }, |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 215 | { "DEBUG1", SYSLOG_LEVEL_DEBUG1 }, |
| 216 | { "DEBUG2", SYSLOG_LEVEL_DEBUG2 }, |
| 217 | { "DEBUG3", SYSLOG_LEVEL_DEBUG3 }, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 218 | { NULL, 0 } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | SyslogFacility |
| 222 | log_facility_number(char *name) |
| 223 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 224 | int i; |
| 225 | if (name != NULL) |
| 226 | for (i = 0; log_facilities[i].name; i++) |
| 227 | if (strcasecmp(log_facilities[i].name, name) == 0) |
| 228 | return log_facilities[i].val; |
| 229 | return (SyslogFacility) - 1; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | LogLevel |
| 233 | log_level_number(char *name) |
| 234 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 235 | int i; |
| 236 | if (name != NULL) |
| 237 | for (i = 0; log_levels[i].name; i++) |
| 238 | if (strcasecmp(log_levels[i].name, name) == 0) |
| 239 | return log_levels[i].val; |
| 240 | return (LogLevel) - 1; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 241 | } |