blob: 3c53d9c596305a43564de2c91efae1aa8b5644c8 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Server-side versions of debug(), log(), etc. These normally send the output
6 * to the system log.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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 Miller95def091999-11-25 00:26:21 +110036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039RCSID("$OpenBSD: log-server.c,v 1.20 2001/01/21 19:05:50 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
41#include <syslog.h>
42#include "packet.h"
43#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000046static LogLevel log_level = SYSLOG_LEVEL_INFO;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047static int log_on_stderr = 0;
Damien Miller2ccf6611999-11-15 15:25:10 +110048static int log_facility = LOG_AUTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
50/* Initialize the log.
Damien Miller95def091999-11-25 00:26:21 +110051 * av0 program name (should be argv[0])
52 * on_stderr print also on stderr
53 * level logging level
54 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
Damien Miller4af51302000-04-16 11:18:38 +100056void
Damien Miller95def091999-11-25 00:26:21 +110057log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058{
Damien Miller95def091999-11-25 00:26:21 +110059 switch (level) {
60 case SYSLOG_LEVEL_QUIET:
Damien Miller95def091999-11-25 00:26:21 +110061 case SYSLOG_LEVEL_FATAL:
Ben Lindstroma383baa2001-01-08 06:13:41 +000062 case SYSLOG_LEVEL_ERROR:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000063 case SYSLOG_LEVEL_INFO:
Damien Miller95def091999-11-25 00:26:21 +110064 case SYSLOG_LEVEL_VERBOSE:
Damien Millere4340be2000-09-16 13:29:08 +110065 case SYSLOG_LEVEL_DEBUG1:
66 case SYSLOG_LEVEL_DEBUG2:
67 case SYSLOG_LEVEL_DEBUG3:
Damien Miller95def091999-11-25 00:26:21 +110068 log_level = level;
69 break;
70 default:
71 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
72 (int) level);
73 exit(1);
74 }
75 switch (facility) {
76 case SYSLOG_FACILITY_DAEMON:
77 log_facility = LOG_DAEMON;
78 break;
79 case SYSLOG_FACILITY_USER:
80 log_facility = LOG_USER;
81 break;
82 case SYSLOG_FACILITY_AUTH:
83 log_facility = LOG_AUTH;
84 break;
Damien Miller6dbfef62000-11-29 13:51:06 +110085#ifdef LOG_AUTHPRIV
86 case SYSLOG_FACILITY_AUTHPRIV:
87 log_facility = LOG_AUTHPRIV;
88 break;
89#endif
Damien Miller95def091999-11-25 00:26:21 +110090 case SYSLOG_FACILITY_LOCAL0:
91 log_facility = LOG_LOCAL0;
92 break;
93 case SYSLOG_FACILITY_LOCAL1:
94 log_facility = LOG_LOCAL1;
95 break;
96 case SYSLOG_FACILITY_LOCAL2:
97 log_facility = LOG_LOCAL2;
98 break;
99 case SYSLOG_FACILITY_LOCAL3:
100 log_facility = LOG_LOCAL3;
101 break;
102 case SYSLOG_FACILITY_LOCAL4:
103 log_facility = LOG_LOCAL4;
104 break;
105 case SYSLOG_FACILITY_LOCAL5:
106 log_facility = LOG_LOCAL5;
107 break;
108 case SYSLOG_FACILITY_LOCAL6:
109 log_facility = LOG_LOCAL6;
110 break;
111 case SYSLOG_FACILITY_LOCAL7:
112 log_facility = LOG_LOCAL7;
113 break;
114 default:
115 fprintf(stderr, "Unrecognized internal syslog facility code %d\n",
116 (int) facility);
117 exit(1);
118 }
119 log_on_stderr = on_stderr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120}
121
Damien Miller98c7ad62000-03-09 21:27:49 +1100122#define MSGBUFSIZ 1024
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller5ce662a1999-11-11 17:57:39 +1100124void
125do_log(LogLevel level, const char *fmt, va_list args)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126{
Damien Miller98c7ad62000-03-09 21:27:49 +1100127 char msgbuf[MSGBUFSIZ];
128 char fmtbuf[MSGBUFSIZ];
Damien Miller95def091999-11-25 00:26:21 +1100129 char *txt = NULL;
130 int pri = LOG_INFO;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000131 extern char *__progname;
Damien Miller5ce662a1999-11-11 17:57:39 +1100132
Damien Miller95def091999-11-25 00:26:21 +1100133 if (level > log_level)
134 return;
135 switch (level) {
Ben Lindstroma383baa2001-01-08 06:13:41 +0000136 case SYSLOG_LEVEL_FATAL:
137 txt = "fatal";
138 pri = LOG_CRIT;
139 break;
Damien Miller95def091999-11-25 00:26:21 +1100140 case SYSLOG_LEVEL_ERROR:
141 txt = "error";
142 pri = LOG_ERR;
143 break;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000144 case SYSLOG_LEVEL_INFO:
145 pri = LOG_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100146 break;
Damien Miller95def091999-11-25 00:26:21 +1100147 case SYSLOG_LEVEL_VERBOSE:
148 pri = LOG_INFO;
149 break;
Damien Millere4340be2000-09-16 13:29:08 +1100150 case SYSLOG_LEVEL_DEBUG1:
151 txt = "debug1";
152 pri = LOG_DEBUG;
153 break;
154 case SYSLOG_LEVEL_DEBUG2:
155 txt = "debug2";
156 pri = LOG_DEBUG;
157 break;
158 case SYSLOG_LEVEL_DEBUG3:
159 txt = "debug3";
Damien Miller95def091999-11-25 00:26:21 +1100160 pri = LOG_DEBUG;
161 break;
162 default:
163 txt = "internal error";
164 pri = LOG_ERR;
165 break;
166 }
167 if (txt != NULL) {
168 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
169 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
170 } else {
171 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
172 }
Damien Millerb38eff82000-04-01 11:09:21 +1000173 if (log_on_stderr) {
Damien Miller95def091999-11-25 00:26:21 +1100174 fprintf(stderr, "%s\n", msgbuf);
Damien Millerb38eff82000-04-01 11:09:21 +1000175 } else {
176 openlog(__progname, LOG_PID, log_facility);
177 syslog(pri, "%.500s", msgbuf);
178 closelog();
179 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180}