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