Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * log-server.c |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 10 | * Created: Mon Mar 20 21:19:30 1995 ylo |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 11 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | * Server-side versions of debug(), log(), etc. These normally send the output |
| 13 | * to the system log. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 14 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 15 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
| 17 | #include "includes.h" |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 18 | RCSID("$OpenBSD: log-server.c,v 1.15 2000/06/20 01:39:42 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 19 | |
| 20 | #include <syslog.h> |
| 21 | #include "packet.h" |
| 22 | #include "xmalloc.h" |
| 23 | #include "ssh.h" |
| 24 | |
Damien Miller | 3f90587 | 1999-11-15 17:10:57 +1100 | [diff] [blame] | 25 | #ifdef HAVE___PROGNAME |
| 26 | extern char *__progname; |
| 27 | #else /* HAVE___PROGNAME */ |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 28 | static const char *__progname = "sshd"; |
Damien Miller | 3f90587 | 1999-11-15 17:10:57 +1100 | [diff] [blame] | 29 | #endif /* HAVE___PROGNAME */ |
| 30 | |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 31 | static LogLevel log_level = SYSLOG_LEVEL_INFO; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | static int log_on_stderr = 0; |
Damien Miller | 2ccf661 | 1999-11-15 15:25:10 +1100 | [diff] [blame] | 33 | static int log_facility = LOG_AUTH; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 34 | |
| 35 | /* Initialize the log. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 36 | * av0 program name (should be argv[0]) |
| 37 | * on_stderr print also on stderr |
| 38 | * level logging level |
| 39 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 41 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 42 | log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 43 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 44 | switch (level) { |
| 45 | case SYSLOG_LEVEL_QUIET: |
| 46 | case SYSLOG_LEVEL_ERROR: |
| 47 | case SYSLOG_LEVEL_FATAL: |
| 48 | case SYSLOG_LEVEL_INFO: |
| 49 | case SYSLOG_LEVEL_VERBOSE: |
| 50 | case SYSLOG_LEVEL_DEBUG: |
| 51 | log_level = level; |
| 52 | break; |
| 53 | default: |
| 54 | fprintf(stderr, "Unrecognized internal syslog level code %d\n", |
| 55 | (int) level); |
| 56 | exit(1); |
| 57 | } |
| 58 | switch (facility) { |
| 59 | case SYSLOG_FACILITY_DAEMON: |
| 60 | log_facility = LOG_DAEMON; |
| 61 | break; |
| 62 | case SYSLOG_FACILITY_USER: |
| 63 | log_facility = LOG_USER; |
| 64 | break; |
| 65 | case SYSLOG_FACILITY_AUTH: |
| 66 | log_facility = LOG_AUTH; |
| 67 | break; |
| 68 | case SYSLOG_FACILITY_LOCAL0: |
| 69 | log_facility = LOG_LOCAL0; |
| 70 | break; |
| 71 | case SYSLOG_FACILITY_LOCAL1: |
| 72 | log_facility = LOG_LOCAL1; |
| 73 | break; |
| 74 | case SYSLOG_FACILITY_LOCAL2: |
| 75 | log_facility = LOG_LOCAL2; |
| 76 | break; |
| 77 | case SYSLOG_FACILITY_LOCAL3: |
| 78 | log_facility = LOG_LOCAL3; |
| 79 | break; |
| 80 | case SYSLOG_FACILITY_LOCAL4: |
| 81 | log_facility = LOG_LOCAL4; |
| 82 | break; |
| 83 | case SYSLOG_FACILITY_LOCAL5: |
| 84 | log_facility = LOG_LOCAL5; |
| 85 | break; |
| 86 | case SYSLOG_FACILITY_LOCAL6: |
| 87 | log_facility = LOG_LOCAL6; |
| 88 | break; |
| 89 | case SYSLOG_FACILITY_LOCAL7: |
| 90 | log_facility = LOG_LOCAL7; |
| 91 | break; |
| 92 | default: |
| 93 | fprintf(stderr, "Unrecognized internal syslog facility code %d\n", |
| 94 | (int) facility); |
| 95 | exit(1); |
| 96 | } |
| 97 | log_on_stderr = on_stderr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 98 | } |
| 99 | |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 100 | #define MSGBUFSIZ 1024 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 102 | void |
| 103 | do_log(LogLevel level, const char *fmt, va_list args) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 104 | { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 105 | char msgbuf[MSGBUFSIZ]; |
| 106 | char fmtbuf[MSGBUFSIZ]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 107 | char *txt = NULL; |
| 108 | int pri = LOG_INFO; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 109 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 110 | if (level > log_level) |
| 111 | return; |
| 112 | switch (level) { |
| 113 | case SYSLOG_LEVEL_ERROR: |
| 114 | txt = "error"; |
| 115 | pri = LOG_ERR; |
| 116 | break; |
| 117 | case SYSLOG_LEVEL_FATAL: |
| 118 | txt = "fatal"; |
| 119 | pri = LOG_ERR; |
| 120 | break; |
| 121 | case SYSLOG_LEVEL_INFO: |
| 122 | case SYSLOG_LEVEL_VERBOSE: |
| 123 | pri = LOG_INFO; |
| 124 | break; |
| 125 | case SYSLOG_LEVEL_DEBUG: |
| 126 | txt = "debug"; |
| 127 | pri = LOG_DEBUG; |
| 128 | break; |
| 129 | default: |
| 130 | txt = "internal error"; |
| 131 | pri = LOG_ERR; |
| 132 | break; |
| 133 | } |
| 134 | if (txt != NULL) { |
| 135 | snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt); |
| 136 | vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args); |
| 137 | } else { |
| 138 | vsnprintf(msgbuf, sizeof(msgbuf), fmt, args); |
| 139 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 140 | if (log_on_stderr) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | fprintf(stderr, "%s\n", msgbuf); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 142 | } else { |
| 143 | openlog(__progname, LOG_PID, log_facility); |
| 144 | syslog(pri, "%.500s", msgbuf); |
| 145 | closelog(); |
| 146 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 147 | } |