1 /* $OpenBSD: log.c,v 1.46 2015/07/08 19:04:21 markus 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 <sys/types.h> 38 39 #include <fcntl.h> 40 #include <stdarg.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <syslog.h> 45 #include <unistd.h> 46 #include <errno.h> 47 #include <vis.h> 48 49 #include "log.h" 50 51 static LogLevel log_level = SYSLOG_LEVEL_INFO; 52 static int log_on_stderr = 1; 53 static int log_stderr_fd = STDERR_FILENO; 54 static int log_facility = LOG_AUTH; 55 static char *argv0; 56 static log_handler_fn *log_handler; 57 static void *log_handler_ctx; 58 59 extern char *__progname; 60 61 /* textual representation of log-facilities/levels */ 62 63 static struct { 64 const char *name; 65 SyslogFacility val; 66 } log_facilities[] = { 67 { "DAEMON", SYSLOG_FACILITY_DAEMON }, 68 { "USER", SYSLOG_FACILITY_USER }, 69 { "AUTH", SYSLOG_FACILITY_AUTH }, 70 { "LOCAL0", SYSLOG_FACILITY_LOCAL0 }, 71 { "LOCAL1", SYSLOG_FACILITY_LOCAL1 }, 72 { "LOCAL2", SYSLOG_FACILITY_LOCAL2 }, 73 { "LOCAL3", SYSLOG_FACILITY_LOCAL3 }, 74 { "LOCAL4", SYSLOG_FACILITY_LOCAL4 }, 75 { "LOCAL5", SYSLOG_FACILITY_LOCAL5 }, 76 { "LOCAL6", SYSLOG_FACILITY_LOCAL6 }, 77 { "LOCAL7", SYSLOG_FACILITY_LOCAL7 }, 78 { NULL, SYSLOG_FACILITY_NOT_SET } 79 }; 80 81 static struct { 82 const char *name; 83 LogLevel val; 84 } log_levels[] = 85 { 86 { "QUIET", SYSLOG_LEVEL_QUIET }, 87 { "FATAL", SYSLOG_LEVEL_FATAL }, 88 { "ERROR", SYSLOG_LEVEL_ERROR }, 89 { "INFO", SYSLOG_LEVEL_INFO }, 90 { "VERBOSE", SYSLOG_LEVEL_VERBOSE }, 91 { "DEBUG", SYSLOG_LEVEL_DEBUG1 }, 92 { "DEBUG1", SYSLOG_LEVEL_DEBUG1 }, 93 { "DEBUG2", SYSLOG_LEVEL_DEBUG2 }, 94 { "DEBUG3", SYSLOG_LEVEL_DEBUG3 }, 95 { NULL, SYSLOG_LEVEL_NOT_SET } 96 }; 97 98 SyslogFacility 99 log_facility_number(char *name) 100 { 101 int i; 102 103 if (name != NULL) 104 for (i = 0; log_facilities[i].name; i++) 105 if (strcasecmp(log_facilities[i].name, name) == 0) 106 return log_facilities[i].val; 107 return SYSLOG_FACILITY_NOT_SET; 108 } 109 110 const char * 111 log_facility_name(SyslogFacility facility) 112 { 113 u_int i; 114 115 for (i = 0; log_facilities[i].name; i++) 116 if (log_facilities[i].val == facility) 117 return log_facilities[i].name; 118 return NULL; 119 } 120 121 LogLevel 122 log_level_number(char *name) 123 { 124 int i; 125 126 if (name != NULL) 127 for (i = 0; log_levels[i].name; i++) 128 if (strcasecmp(log_levels[i].name, name) == 0) 129 return log_levels[i].val; 130 return SYSLOG_LEVEL_NOT_SET; 131 } 132 133 const char * 134 log_level_name(LogLevel level) 135 { 136 u_int i; 137 138 for (i = 0; log_levels[i].name != NULL; i++) 139 if (log_levels[i].val == level) 140 return log_levels[i].name; 141 return NULL; 142 } 143 144 /* Error messages that should be logged. */ 145 146 void 147 error(const char *fmt,...) 148 { 149 va_list args; 150 151 va_start(args, fmt); 152 do_log(SYSLOG_LEVEL_ERROR, fmt, args); 153 va_end(args); 154 } 155 156 void 157 sigdie(const char *fmt,...) 158 { 159 va_list args; 160 161 va_start(args, fmt); 162 do_log(SYSLOG_LEVEL_FATAL, fmt, args); 163 va_end(args); 164 _exit(1); 165 } 166 167 168 /* Log this message (information that usually should go to the log). */ 169 170 void 171 logit(const char *fmt,...) 172 { 173 va_list args; 174 175 va_start(args, fmt); 176 do_log(SYSLOG_LEVEL_INFO, fmt, args); 177 va_end(args); 178 } 179 180 /* More detailed messages (information that does not need to go to the log). */ 181 182 void 183 verbose(const char *fmt,...) 184 { 185 va_list args; 186 187 va_start(args, fmt); 188 do_log(SYSLOG_LEVEL_VERBOSE, fmt, args); 189 va_end(args); 190 } 191 192 /* Debugging messages that should not be logged during normal operation. */ 193 194 void 195 debug(const char *fmt,...) 196 { 197 va_list args; 198 199 va_start(args, fmt); 200 do_log(SYSLOG_LEVEL_DEBUG1, fmt, args); 201 va_end(args); 202 } 203 204 void 205 debug2(const char *fmt,...) 206 { 207 va_list args; 208 209 va_start(args, fmt); 210 do_log(SYSLOG_LEVEL_DEBUG2, fmt, args); 211 va_end(args); 212 } 213 214 void 215 debug3(const char *fmt,...) 216 { 217 va_list args; 218 219 va_start(args, fmt); 220 do_log(SYSLOG_LEVEL_DEBUG3, fmt, args); 221 va_end(args); 222 } 223 224 /* 225 * Initialize the log. 226 */ 227 228 void 229 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr) 230 { 231 argv0 = av0; 232 233 switch (level) { 234 case SYSLOG_LEVEL_QUIET: 235 case SYSLOG_LEVEL_FATAL: 236 case SYSLOG_LEVEL_ERROR: 237 case SYSLOG_LEVEL_INFO: 238 case SYSLOG_LEVEL_VERBOSE: 239 case SYSLOG_LEVEL_DEBUG1: 240 case SYSLOG_LEVEL_DEBUG2: 241 case SYSLOG_LEVEL_DEBUG3: 242 log_level = level; 243 break; 244 default: 245 fprintf(stderr, "Unrecognized internal syslog level code %d\n", 246 (int) level); 247 exit(1); 248 } 249 250 log_handler = NULL; 251 log_handler_ctx = NULL; 252 253 log_on_stderr = on_stderr; 254 if (on_stderr) 255 return; 256 257 switch (facility) { 258 case SYSLOG_FACILITY_DAEMON: 259 log_facility = LOG_DAEMON; 260 break; 261 case SYSLOG_FACILITY_USER: 262 log_facility = LOG_USER; 263 break; 264 case SYSLOG_FACILITY_AUTH: 265 log_facility = LOG_AUTH; 266 break; 267 case SYSLOG_FACILITY_LOCAL0: 268 log_facility = LOG_LOCAL0; 269 break; 270 case SYSLOG_FACILITY_LOCAL1: 271 log_facility = LOG_LOCAL1; 272 break; 273 case SYSLOG_FACILITY_LOCAL2: 274 log_facility = LOG_LOCAL2; 275 break; 276 case SYSLOG_FACILITY_LOCAL3: 277 log_facility = LOG_LOCAL3; 278 break; 279 case SYSLOG_FACILITY_LOCAL4: 280 log_facility = LOG_LOCAL4; 281 break; 282 case SYSLOG_FACILITY_LOCAL5: 283 log_facility = LOG_LOCAL5; 284 break; 285 case SYSLOG_FACILITY_LOCAL6: 286 log_facility = LOG_LOCAL6; 287 break; 288 case SYSLOG_FACILITY_LOCAL7: 289 log_facility = LOG_LOCAL7; 290 break; 291 default: 292 fprintf(stderr, 293 "Unrecognized internal syslog facility code %d\n", 294 (int) facility); 295 exit(1); 296 } 297 } 298 299 void 300 log_change_level(LogLevel new_log_level) 301 { 302 /* no-op if log_init has not been called */ 303 if (argv0 == NULL) 304 return; 305 log_init(argv0, new_log_level, log_facility, log_on_stderr); 306 } 307 308 int 309 log_is_on_stderr(void) 310 { 311 return log_on_stderr; 312 } 313 314 /* redirect what would usually get written to stderr to specified file */ 315 void 316 log_redirect_stderr_to(const char *logfile) 317 { 318 int fd; 319 320 if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600)) == -1) { 321 fprintf(stderr, "Couldn't open logfile %s: %s\n", logfile, 322 strerror(errno)); 323 exit(1); 324 } 325 log_stderr_fd = fd; 326 } 327 328 #define MSGBUFSIZ 1024 329 330 void 331 set_log_handler(log_handler_fn *handler, void *ctx) 332 { 333 log_handler = handler; 334 log_handler_ctx = ctx; 335 } 336 337 void 338 do_log2(LogLevel level, const char *fmt,...) 339 { 340 va_list args; 341 342 va_start(args, fmt); 343 do_log(level, fmt, args); 344 va_end(args); 345 } 346 347 void 348 do_log(LogLevel level, const char *fmt, va_list args) 349 { 350 struct syslog_data sdata = SYSLOG_DATA_INIT; 351 char msgbuf[MSGBUFSIZ]; 352 char fmtbuf[MSGBUFSIZ]; 353 char *txt = NULL; 354 int pri = LOG_INFO; 355 int saved_errno = errno; 356 log_handler_fn *tmp_handler; 357 358 if (level > log_level) 359 return; 360 361 switch (level) { 362 case SYSLOG_LEVEL_FATAL: 363 if (!log_on_stderr) 364 txt = "fatal"; 365 pri = LOG_CRIT; 366 break; 367 case SYSLOG_LEVEL_ERROR: 368 if (!log_on_stderr) 369 txt = "error"; 370 pri = LOG_ERR; 371 break; 372 case SYSLOG_LEVEL_INFO: 373 pri = LOG_INFO; 374 break; 375 case SYSLOG_LEVEL_VERBOSE: 376 pri = LOG_INFO; 377 break; 378 case SYSLOG_LEVEL_DEBUG1: 379 txt = "debug1"; 380 pri = LOG_DEBUG; 381 break; 382 case SYSLOG_LEVEL_DEBUG2: 383 txt = "debug2"; 384 pri = LOG_DEBUG; 385 break; 386 case SYSLOG_LEVEL_DEBUG3: 387 txt = "debug3"; 388 pri = LOG_DEBUG; 389 break; 390 default: 391 txt = "internal error"; 392 pri = LOG_ERR; 393 break; 394 } 395 if (txt != NULL && log_handler == NULL) { 396 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt); 397 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args); 398 } else { 399 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args); 400 } 401 strnvis(fmtbuf, msgbuf, sizeof(fmtbuf), VIS_SAFE|VIS_OCTAL); 402 if (log_handler != NULL) { 403 /* Avoid recursion */ 404 tmp_handler = log_handler; 405 log_handler = NULL; 406 tmp_handler(level, fmtbuf, log_handler_ctx); 407 log_handler = tmp_handler; 408 } else if (log_on_stderr) { 409 snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf); 410 (void)write(log_stderr_fd, msgbuf, strlen(msgbuf)); 411 } else { 412 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata); 413 syslog_r(pri, &sdata, "%.500s", fmtbuf); 414 closelog_r(&sdata); 415 } 416 errno = saved_errno; 417 } 418