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