xref: /titanic_41/usr/src/cmd/ssh/libssh/common/log.c (revision c5024742c2f7d10880eae26cc592353b20a58f4a)
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  *
6  * As far as I am concerned, the code I have written for this software
7  * can be used freely for any purpose.  Any derived versions of this
8  * software must be clearly marked as such, and if the derived work is
9  * incompatible with the protocol description in the RFC file, it must be
10  * called by a name other than "ssh" or "Secure Shell".
11  */
12 /*
13  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*
36  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
37  * Use is subject to license terms.
38  */
39 
40 #include "includes.h"
41 RCSID("$OpenBSD: log.c,v 1.24 2002/07/19 15:43:33 markus Exp $");
42 
43 #pragma ident	"%Z%%M%	%I%	%E% SMI"
44 
45 #include "log.h"
46 #include "xmalloc.h"
47 
48 #include <syslog.h>
49 
50 static LogLevel log_level = SYSLOG_LEVEL_INFO;
51 static int log_on_stderr = 1;
52 static int log_facility = LOG_AUTH;
53 static char *argv0;
54 
55 extern char *__progname;
56 
57 static const char *log_txt_prefix = "";
58 
59 /* textual representation of log-facilities/levels */
60 
61 static struct {
62 	const char *name;
63 	SyslogFacility val;
64 } log_facilities[] = {
65 	{ "DAEMON",	SYSLOG_FACILITY_DAEMON },
66 	{ "USER",	SYSLOG_FACILITY_USER },
67 	{ "AUTH",	SYSLOG_FACILITY_AUTH },
68 #ifdef LOG_AUTHPRIV
69 	{ "AUTHPRIV",	SYSLOG_FACILITY_AUTHPRIV },
70 #endif
71 	{ "LOCAL0",	SYSLOG_FACILITY_LOCAL0 },
72 	{ "LOCAL1",	SYSLOG_FACILITY_LOCAL1 },
73 	{ "LOCAL2",	SYSLOG_FACILITY_LOCAL2 },
74 	{ "LOCAL3",	SYSLOG_FACILITY_LOCAL3 },
75 	{ "LOCAL4",	SYSLOG_FACILITY_LOCAL4 },
76 	{ "LOCAL5",	SYSLOG_FACILITY_LOCAL5 },
77 	{ "LOCAL6",	SYSLOG_FACILITY_LOCAL6 },
78 	{ "LOCAL7",	SYSLOG_FACILITY_LOCAL7 },
79 	{ NULL,		SYSLOG_FACILITY_NOT_SET }
80 };
81 
82 static struct {
83 	const char *name;
84 	LogLevel val;
85 } log_levels[] =
86 {
87 	{ "QUIET",	SYSLOG_LEVEL_QUIET },
88 	{ "FATAL",	SYSLOG_LEVEL_FATAL },
89 	{ "ERROR",	SYSLOG_LEVEL_ERROR },
90 	{ "NOTICE",	SYSLOG_LEVEL_NOTICE },
91 	{ "INFO",	SYSLOG_LEVEL_INFO },
92 	{ "VERBOSE",	SYSLOG_LEVEL_VERBOSE },
93 	{ "DEBUG",	SYSLOG_LEVEL_DEBUG1 },
94 	{ "DEBUG1",	SYSLOG_LEVEL_DEBUG1 },
95 	{ "DEBUG2",	SYSLOG_LEVEL_DEBUG2 },
96 	{ "DEBUG3",	SYSLOG_LEVEL_DEBUG3 },
97 	{ NULL,		SYSLOG_LEVEL_NOT_SET }
98 };
99 
100 SyslogFacility
101 log_facility_number(char *name)
102 {
103 	int i;
104 
105 	if (name != NULL)
106 		for (i = 0; log_facilities[i].name; i++)
107 			if (strcasecmp(log_facilities[i].name, name) == 0)
108 				return log_facilities[i].val;
109 	return SYSLOG_FACILITY_NOT_SET;
110 }
111 
112 LogLevel
113 log_level_number(char *name)
114 {
115 	int i;
116 
117 	if (name != NULL)
118 		for (i = 0; log_levels[i].name; i++)
119 			if (strcasecmp(log_levels[i].name, name) == 0)
120 				return log_levels[i].val;
121 	return SYSLOG_LEVEL_NOT_SET;
122 }
123 
124 void
125 set_log_txt_prefix(const char *txt)
126 {
127 	log_txt_prefix = txt;
128 }
129 
130 /* Error messages that should be logged. */
131 
132 void
133 error(const char *fmt,...)
134 {
135 	va_list args;
136 
137 	va_start(args, fmt);
138 	do_log(SYSLOG_LEVEL_ERROR, fmt, args);
139 	va_end(args);
140 }
141 
142 void
143 notice(const char *fmt,...)
144 {
145 	va_list args;
146 
147 	va_start(args, fmt);
148 	do_log(SYSLOG_LEVEL_NOTICE, fmt, args);
149 	va_end(args);
150 }
151 
152 /* Log this message (information that usually should go to the log). */
153 
154 void
155 log(const char *fmt,...)
156 {
157 	va_list args;
158 
159 	va_start(args, fmt);
160 	do_log(SYSLOG_LEVEL_INFO, fmt, args);
161 	va_end(args);
162 }
163 
164 /* More detailed messages (information that does not need to go to the log). */
165 
166 void
167 verbose(const char *fmt,...)
168 {
169 	va_list args;
170 
171 	va_start(args, fmt);
172 	do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
173 	va_end(args);
174 }
175 
176 /* Debugging messages that should not be logged during normal operation. */
177 
178 void
179 debug(const char *fmt,...)
180 {
181 	va_list args;
182 
183 	va_start(args, fmt);
184 	do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
185 	va_end(args);
186 }
187 
188 void
189 debug2(const char *fmt,...)
190 {
191 	va_list args;
192 
193 	va_start(args, fmt);
194 	do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
195 	va_end(args);
196 }
197 
198 void
199 debug3(const char *fmt,...)
200 {
201 	va_list args;
202 
203 	va_start(args, fmt);
204 	do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
205 	va_end(args);
206 }
207 
208 /* Fatal cleanup */
209 
210 struct fatal_cleanup {
211 	struct fatal_cleanup *next;
212 	void (*proc) (void *);
213 	void *context;
214 };
215 
216 static struct fatal_cleanup *fatal_cleanups = NULL;
217 
218 /* Registers a cleanup function to be called by fatal() before exiting. */
219 
220 void
221 fatal_add_cleanup(void (*proc) (void *), void *context)
222 {
223 	struct fatal_cleanup *cu;
224 
225 	cu = xmalloc(sizeof(*cu));
226 	cu->proc = proc;
227 	cu->context = context;
228 	cu->next = fatal_cleanups;
229 	fatal_cleanups = cu;
230 }
231 
232 /* Removes a cleanup frunction to be called at fatal(). */
233 
234 void
235 fatal_remove_cleanup(void (*proc) (void *context), void *context)
236 {
237 	struct fatal_cleanup **cup, *cu;
238 
239 	for (cup = &fatal_cleanups; *cup; cup = &cu->next) {
240 		cu = *cup;
241 		if (cu->proc == proc && cu->context == context) {
242 			*cup = cu->next;
243 			xfree(cu);
244 			return;
245 		}
246 	}
247 	debug3("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx",
248 	    (u_long) proc, (u_long) context);
249 }
250 
251 /* Remove all cleanups, to be called after fork() */
252 void
253 fatal_remove_all_cleanups(void)
254 {
255 	struct fatal_cleanup *cu, *next_cu;
256 
257 	for (cu = fatal_cleanups; cu; cu = next_cu) {
258 		next_cu = cu->next;
259 		xfree(cu);
260 	}
261 
262 	fatal_cleanups = NULL;
263 }
264 
265 /* Cleanup and exit */
266 void
267 fatal_cleanup(void)
268 {
269 	struct fatal_cleanup *cu, *next_cu;
270 	static int called = 0;
271 
272 	if (called)
273 		exit(255);
274 	called = 1;
275 	/* Call cleanup functions. */
276 	for (cu = fatal_cleanups; cu; cu = next_cu) {
277 		next_cu = cu->next;
278 		debug("Calling cleanup 0x%lx(0x%lx)",
279 		    (u_long) cu->proc, (u_long) cu->context);
280 		(*cu->proc) (cu->context);
281 	}
282 	exit(255);
283 }
284 
285 
286 /*
287  * Initialize the log.
288  */
289 
290 void
291 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
292 {
293 	argv0 = av0;
294 
295 	switch (level) {
296 	case SYSLOG_LEVEL_QUIET:
297 	case SYSLOG_LEVEL_FATAL:
298 	case SYSLOG_LEVEL_ERROR:
299 	case SYSLOG_LEVEL_NOTICE:
300 	case SYSLOG_LEVEL_INFO:
301 	case SYSLOG_LEVEL_VERBOSE:
302 	case SYSLOG_LEVEL_DEBUG1:
303 	case SYSLOG_LEVEL_DEBUG2:
304 	case SYSLOG_LEVEL_DEBUG3:
305 		log_level = level;
306 		break;
307 	default:
308 		fprintf(stderr, "Unrecognized internal syslog level code %d\n",
309 		    (int) level);
310 		exit(1);
311 	}
312 
313 	log_on_stderr = on_stderr;
314 	if (on_stderr)
315 		return;
316 
317 	switch (facility) {
318 	case SYSLOG_FACILITY_DAEMON:
319 		log_facility = LOG_DAEMON;
320 		break;
321 	case SYSLOG_FACILITY_USER:
322 		log_facility = LOG_USER;
323 		break;
324 	case SYSLOG_FACILITY_AUTH:
325 		log_facility = LOG_AUTH;
326 		break;
327 #ifdef LOG_AUTHPRIV
328 	case SYSLOG_FACILITY_AUTHPRIV:
329 		log_facility = LOG_AUTHPRIV;
330 		break;
331 #endif
332 	case SYSLOG_FACILITY_LOCAL0:
333 		log_facility = LOG_LOCAL0;
334 		break;
335 	case SYSLOG_FACILITY_LOCAL1:
336 		log_facility = LOG_LOCAL1;
337 		break;
338 	case SYSLOG_FACILITY_LOCAL2:
339 		log_facility = LOG_LOCAL2;
340 		break;
341 	case SYSLOG_FACILITY_LOCAL3:
342 		log_facility = LOG_LOCAL3;
343 		break;
344 	case SYSLOG_FACILITY_LOCAL4:
345 		log_facility = LOG_LOCAL4;
346 		break;
347 	case SYSLOG_FACILITY_LOCAL5:
348 		log_facility = LOG_LOCAL5;
349 		break;
350 	case SYSLOG_FACILITY_LOCAL6:
351 		log_facility = LOG_LOCAL6;
352 		break;
353 	case SYSLOG_FACILITY_LOCAL7:
354 		log_facility = LOG_LOCAL7;
355 		break;
356 	default:
357 		fprintf(stderr,
358 		    "Unrecognized internal syslog facility code %d\n",
359 		    (int) facility);
360 		exit(1);
361 	}
362 }
363 
364 #define MSGBUFSIZ 1024
365 
366 /* PRINTFLIKE2 */
367 void
368 do_log(LogLevel level, const char *fmt, va_list args)
369 {
370 	char msgbuf[MSGBUFSIZ];
371 	char fmtbuf[MSGBUFSIZ];
372 	char *txt = NULL;
373 	int pri = LOG_INFO;
374 	int do_gettext = log_on_stderr; /*
375 					 * Localize user messages - not
376 					 * syslog()ed messages.
377 					 */
378 
379 	if (level > log_level)
380 		return;
381 
382 	switch (level) {
383 	case SYSLOG_LEVEL_FATAL:
384 		if (!log_on_stderr)
385 			txt = "fatal";
386 		pri = LOG_CRIT;
387 		break;
388 	case SYSLOG_LEVEL_ERROR:
389 		if (!log_on_stderr)
390 			txt = "error";
391 		pri = LOG_ERR;
392 		break;
393 	case SYSLOG_LEVEL_NOTICE:
394 		pri = LOG_NOTICE;
395 		break;
396 	case SYSLOG_LEVEL_INFO:
397 		pri = LOG_INFO;
398 		break;
399 	case SYSLOG_LEVEL_VERBOSE:
400 		pri = LOG_INFO;
401 		break;
402 	case SYSLOG_LEVEL_DEBUG1:
403 		txt = "debug1";
404 		pri = LOG_DEBUG;
405 		/*
406 		 * Don't localize debug messages - such are not intended
407 		 * for users but for support staff whose preferred
408 		 * language is unknown, therefore we default to the
409 		 * language used in the source code: English.
410 		 */
411 		do_gettext = 0;
412 		break;
413 	case SYSLOG_LEVEL_DEBUG2:
414 		txt = "debug2";
415 		pri = LOG_DEBUG;
416 		do_gettext = 0;	    /* Don't localize debug messages. */
417 		break;
418 	case SYSLOG_LEVEL_DEBUG3:
419 		txt = "debug3";
420 		pri = LOG_DEBUG;
421 		do_gettext = 0;	    /* Don't localize debug messages. */
422 		break;
423 	default:
424 		txt = "internal error";
425 		pri = LOG_ERR;
426 		break;
427 	}
428 	if (txt != NULL) {
429 		snprintf(fmtbuf, sizeof(fmtbuf), "%s%s: %s", log_txt_prefix,
430 			 do_gettext ? gettext(txt) : txt,
431 			 do_gettext ? gettext(fmt) : fmt);
432 		vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
433 	} else {
434 		vsnprintf(msgbuf, sizeof(msgbuf),
435 			  do_gettext ? gettext(fmt) : fmt,
436 			  args);
437 	}
438 	if (log_on_stderr) {
439 		fprintf(stderr, "%s\r\n", msgbuf);
440 	} else {
441 		openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
442 		syslog(pri, "%.500s", msgbuf);
443 		closelog();
444 	}
445 }
446