1 /* $OpenBSD: log.c,v 1.41 2008/06/10 04:50:25 dtucker Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  *
7  * As far as I am concerned, the code I have written for this software
8  * can be used freely for any purpose.  Any derived versions of this
9  * software must be clearly marked as such, and if the derived work is
10  * incompatible with the protocol description in the RFC file, it must be
11  * called by a name other than "ssh" or "Secure Shell".
12  */
13 /*
14  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <sys/types.h>
38 
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <syslog.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <vis.h>
47 
48 #include "xmalloc.h"
49 #include "log.h"
50 
51 __RCSID("$MirOS: src/usr.bin/ssh/log.c,v 1.6 2008/12/16 20:55:22 tg Exp $");
52 
53 static LogLevel log_level = SYSLOG_LEVEL_INFO;
54 static int log_on_stderr = 1;
55 static int log_facility = LOG_AUTH;
56 static const char *argv0;
57 
58 extern char *__progname;
59 
60 /* textual representation of log-facilities/levels */
61 
62 static struct {
63 	const char *name;
64 	SyslogFacility val;
65 } log_facilities[] = {
66 	{ "DAEMON",	SYSLOG_FACILITY_DAEMON },
67 	{ "USER",	SYSLOG_FACILITY_USER },
68 	{ "AUTH",	SYSLOG_FACILITY_AUTH },
69 	{ "LOCAL0",	SYSLOG_FACILITY_LOCAL0 },
70 	{ "LOCAL1",	SYSLOG_FACILITY_LOCAL1 },
71 	{ "LOCAL2",	SYSLOG_FACILITY_LOCAL2 },
72 	{ "LOCAL3",	SYSLOG_FACILITY_LOCAL3 },
73 	{ "LOCAL4",	SYSLOG_FACILITY_LOCAL4 },
74 	{ "LOCAL5",	SYSLOG_FACILITY_LOCAL5 },
75 	{ "LOCAL6",	SYSLOG_FACILITY_LOCAL6 },
76 	{ "LOCAL7",	SYSLOG_FACILITY_LOCAL7 },
77 	{ NULL,		SYSLOG_FACILITY_NOT_SET }
78 };
79 
80 static struct {
81 	const char *name;
82 	LogLevel val;
83 } log_levels[] =
84 {
85 	{ "QUIET",	SYSLOG_LEVEL_QUIET },
86 	{ "FATAL",	SYSLOG_LEVEL_FATAL },
87 	{ "ERROR",	SYSLOG_LEVEL_ERROR },
88 	{ "INFO",	SYSLOG_LEVEL_INFO },
89 	{ "VERBOSE",	SYSLOG_LEVEL_VERBOSE },
90 	{ "DEBUG",	SYSLOG_LEVEL_DEBUG1 },
91 	{ "DEBUG1",	SYSLOG_LEVEL_DEBUG1 },
92 	{ "DEBUG2",	SYSLOG_LEVEL_DEBUG2 },
93 	{ "DEBUG3",	SYSLOG_LEVEL_DEBUG3 },
94 	{ NULL,		SYSLOG_LEVEL_NOT_SET }
95 };
96 
97 SyslogFacility
log_facility_number(char * name)98 log_facility_number(char *name)
99 {
100 	int i;
101 
102 	if (name != NULL)
103 		for (i = 0; log_facilities[i].name; i++)
104 			if (strcasecmp(log_facilities[i].name, name) == 0)
105 				return log_facilities[i].val;
106 	return SYSLOG_FACILITY_NOT_SET;
107 }
108 
109 const char *
log_facility_name(SyslogFacility facility)110 log_facility_name(SyslogFacility facility)
111 {
112 	u_int i;
113 
114 	for (i = 0;  log_facilities[i].name; i++)
115 		if (log_facilities[i].val == facility)
116 			return log_facilities[i].name;
117 	return NULL;
118 }
119 
120 LogLevel
log_level_number(char * name)121 log_level_number(char *name)
122 {
123 	int i;
124 
125 	if (name != NULL)
126 		for (i = 0; log_levels[i].name; i++)
127 			if (strcasecmp(log_levels[i].name, name) == 0)
128 				return log_levels[i].val;
129 	return SYSLOG_LEVEL_NOT_SET;
130 }
131 
132 const char *
log_level_name(LogLevel level)133 log_level_name(LogLevel level)
134 {
135 	u_int i;
136 
137 	for (i = 0; log_levels[i].name != NULL; i++)
138 		if (log_levels[i].val == level)
139 			return log_levels[i].name;
140 	return NULL;
141 }
142 
143 /* Error messages that should be logged. */
144 
145 void
error(const char * fmt,...)146 error(const char *fmt,...)
147 {
148 	va_list args;
149 
150 	va_start(args, fmt);
151 	do_log(SYSLOG_LEVEL_ERROR, fmt, args);
152 	va_end(args);
153 }
154 
155 void
sigdie(const char * fmt,...)156 sigdie(const char *fmt,...)
157 {
158 	va_list args;
159 
160 	va_start(args, fmt);
161 	do_log(SYSLOG_LEVEL_FATAL, fmt, args);
162 	va_end(args);
163 	_exit(1);
164 }
165 
166 
167 /* Log this message (information that usually should go to the log). */
168 
169 void
logit(const char * fmt,...)170 logit(const char *fmt,...)
171 {
172 	va_list args;
173 
174 	va_start(args, fmt);
175 	do_log(SYSLOG_LEVEL_INFO, fmt, args);
176 	va_end(args);
177 }
178 
179 /* More detailed messages (information that does not need to go to the log). */
180 
181 void
verbose(const char * fmt,...)182 verbose(const char *fmt,...)
183 {
184 	va_list args;
185 
186 	va_start(args, fmt);
187 	do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
188 	va_end(args);
189 }
190 
191 /* Debugging messages that should not be logged during normal operation. */
192 
193 void
debug(const char * fmt,...)194 debug(const char *fmt,...)
195 {
196 	va_list args;
197 
198 	va_start(args, fmt);
199 	do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
200 	va_end(args);
201 }
202 
203 void
debug2(const char * fmt,...)204 debug2(const char *fmt,...)
205 {
206 	va_list args;
207 
208 	va_start(args, fmt);
209 	do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
210 	va_end(args);
211 }
212 
213 void
debug3(const char * fmt,...)214 debug3(const char *fmt,...)
215 {
216 	va_list args;
217 
218 	va_start(args, fmt);
219 	do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
220 	va_end(args);
221 }
222 
223 /*
224  * Initialize the log.
225  */
226 
227 void
log_init(const char * av0,LogLevel level,SyslogFacility facility,int on_stderr)228 log_init(const char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
229 {
230 	argv0 = av0;
231 
232 	switch (level) {
233 	case SYSLOG_LEVEL_QUIET:
234 	case SYSLOG_LEVEL_FATAL:
235 	case SYSLOG_LEVEL_ERROR:
236 	case SYSLOG_LEVEL_INFO:
237 	case SYSLOG_LEVEL_VERBOSE:
238 	case SYSLOG_LEVEL_DEBUG1:
239 	case SYSLOG_LEVEL_DEBUG2:
240 	case SYSLOG_LEVEL_DEBUG3:
241 		log_level = level;
242 		break;
243 	default:
244 		fprintf(stderr, "Unrecognized internal syslog level code %d\n",
245 		    (int) level);
246 		exit(1);
247 	}
248 
249 	log_on_stderr = on_stderr;
250 	if (on_stderr)
251 		return;
252 
253 	switch (facility) {
254 	case SYSLOG_FACILITY_DAEMON:
255 		log_facility = LOG_DAEMON;
256 		break;
257 	case SYSLOG_FACILITY_USER:
258 		log_facility = LOG_USER;
259 		break;
260 	case SYSLOG_FACILITY_AUTH:
261 		log_facility = LOG_AUTH;
262 		break;
263 	case SYSLOG_FACILITY_LOCAL0:
264 		log_facility = LOG_LOCAL0;
265 		break;
266 	case SYSLOG_FACILITY_LOCAL1:
267 		log_facility = LOG_LOCAL1;
268 		break;
269 	case SYSLOG_FACILITY_LOCAL2:
270 		log_facility = LOG_LOCAL2;
271 		break;
272 	case SYSLOG_FACILITY_LOCAL3:
273 		log_facility = LOG_LOCAL3;
274 		break;
275 	case SYSLOG_FACILITY_LOCAL4:
276 		log_facility = LOG_LOCAL4;
277 		break;
278 	case SYSLOG_FACILITY_LOCAL5:
279 		log_facility = LOG_LOCAL5;
280 		break;
281 	case SYSLOG_FACILITY_LOCAL6:
282 		log_facility = LOG_LOCAL6;
283 		break;
284 	case SYSLOG_FACILITY_LOCAL7:
285 		log_facility = LOG_LOCAL7;
286 		break;
287 	default:
288 		fprintf(stderr,
289 		    "Unrecognized internal syslog facility code %d\n",
290 		    (int) facility);
291 		exit(1);
292 	}
293 }
294 
295 #define MSGBUFSIZ 1024
296 
297 void
do_log(LogLevel level,const char * fmt,va_list args)298 do_log(LogLevel level, const char *fmt, va_list args)
299 {
300 	struct syslog_data sdata = SYSLOG_DATA_INIT;
301 	char msgbuf[MSGBUFSIZ];
302 	char fmtbuf[MSGBUFSIZ];
303 	const char *txt = NULL;
304 	int pri = LOG_INFO;
305 	int saved_errno = errno;
306 
307 	if (level > log_level)
308 		return;
309 
310 	switch (level) {
311 	case SYSLOG_LEVEL_FATAL:
312 		if (!log_on_stderr)
313 			txt = "fatal";
314 		pri = LOG_CRIT;
315 		break;
316 	case SYSLOG_LEVEL_ERROR:
317 		if (!log_on_stderr)
318 			txt = "error";
319 		pri = LOG_ERR;
320 		break;
321 	case SYSLOG_LEVEL_INFO:
322 		pri = LOG_INFO;
323 		break;
324 	case SYSLOG_LEVEL_VERBOSE:
325 		pri = LOG_INFO;
326 		break;
327 	case SYSLOG_LEVEL_DEBUG1:
328 		txt = "debug1";
329 		pri = LOG_DEBUG;
330 		break;
331 	case SYSLOG_LEVEL_DEBUG2:
332 		txt = "debug2";
333 		pri = LOG_DEBUG;
334 		break;
335 	case SYSLOG_LEVEL_DEBUG3:
336 		txt = "debug3";
337 		pri = LOG_DEBUG;
338 		break;
339 	default:
340 		txt = "internal error";
341 		pri = LOG_ERR;
342 		break;
343 	}
344 	if (txt != NULL) {
345 		snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
346 		vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
347 	} else {
348 		vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
349 	}
350 	strnvis(fmtbuf, msgbuf, sizeof(fmtbuf), VIS_SAFE|VIS_OCTAL);
351 	if (log_on_stderr) {
352 		snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
353 		write(STDERR_FILENO, msgbuf, strlen(msgbuf));
354 	} else {
355 		openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
356 		syslog_r(pri, &sdata, "%.500s", fmtbuf);
357 		closelog_r(&sdata);
358 	}
359 	errno = saved_errno;
360 }
361