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