openssh-5.9p1-xauthlocalhostname.diff
[platform/upstream/openssh.git] / log.c
1 /* $OpenBSD: log.c,v 1.45 2013/05/16 09:08:41 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 "includes.h"
38
39 #include <sys/types.h>
40
41 #include <fcntl.h>
42 #include <stdarg.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
50 # include <vis.h>
51 #endif
52
53 #include "xmalloc.h"
54 #include "log.h"
55 #include <signal.h>
56
57 static LogLevel log_level = SYSLOG_LEVEL_INFO;
58 static int log_on_stderr = 1;
59 static int log_stderr_fd = STDERR_FILENO;
60 static int log_facility = LOG_AUTH;
61 static char *argv0;
62 static log_handler_fn *log_handler;
63 static void *log_handler_ctx;
64
65 extern char *__progname;
66
67 #define LOG_SYSLOG_VIS  (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
68 #define LOG_STDERR_VIS  (VIS_SAFE|VIS_OCTAL)
69
70 /* textual representation of log-facilities/levels */
71
72 static struct {
73         const char *name;
74         SyslogFacility val;
75 } log_facilities[] = {
76         { "DAEMON",     SYSLOG_FACILITY_DAEMON },
77         { "USER",       SYSLOG_FACILITY_USER },
78         { "AUTH",       SYSLOG_FACILITY_AUTH },
79 #ifdef LOG_AUTHPRIV
80         { "AUTHPRIV",   SYSLOG_FACILITY_AUTHPRIV },
81 #endif
82         { "LOCAL0",     SYSLOG_FACILITY_LOCAL0 },
83         { "LOCAL1",     SYSLOG_FACILITY_LOCAL1 },
84         { "LOCAL2",     SYSLOG_FACILITY_LOCAL2 },
85         { "LOCAL3",     SYSLOG_FACILITY_LOCAL3 },
86         { "LOCAL4",     SYSLOG_FACILITY_LOCAL4 },
87         { "LOCAL5",     SYSLOG_FACILITY_LOCAL5 },
88         { "LOCAL6",     SYSLOG_FACILITY_LOCAL6 },
89         { "LOCAL7",     SYSLOG_FACILITY_LOCAL7 },
90         { NULL,         SYSLOG_FACILITY_NOT_SET }
91 };
92
93 static struct {
94         const char *name;
95         LogLevel val;
96 } log_levels[] =
97 {
98         { "QUIET",      SYSLOG_LEVEL_QUIET },
99         { "FATAL",      SYSLOG_LEVEL_FATAL },
100         { "ERROR",      SYSLOG_LEVEL_ERROR },
101         { "INFO",       SYSLOG_LEVEL_INFO },
102         { "VERBOSE",    SYSLOG_LEVEL_VERBOSE },
103         { "DEBUG",      SYSLOG_LEVEL_DEBUG1 },
104         { "DEBUG1",     SYSLOG_LEVEL_DEBUG1 },
105         { "DEBUG2",     SYSLOG_LEVEL_DEBUG2 },
106         { "DEBUG3",     SYSLOG_LEVEL_DEBUG3 },
107         { NULL,         SYSLOG_LEVEL_NOT_SET }
108 };
109
110 SyslogFacility
111 log_facility_number(char *name)
112 {
113         int i;
114
115         if (name != NULL)
116                 for (i = 0; log_facilities[i].name; i++)
117                         if (strcasecmp(log_facilities[i].name, name) == 0)
118                                 return log_facilities[i].val;
119         return SYSLOG_FACILITY_NOT_SET;
120 }
121
122 const char *
123 log_facility_name(SyslogFacility facility)
124 {
125         u_int i;
126
127         for (i = 0;  log_facilities[i].name; i++)
128                 if (log_facilities[i].val == facility)
129                         return log_facilities[i].name;
130         return NULL;
131 }
132
133 LogLevel
134 log_level_number(char *name)
135 {
136         int i;
137
138         if (name != NULL)
139                 for (i = 0; log_levels[i].name; i++)
140                         if (strcasecmp(log_levels[i].name, name) == 0)
141                                 return log_levels[i].val;
142         return SYSLOG_LEVEL_NOT_SET;
143 }
144
145 const char *
146 log_level_name(LogLevel level)
147 {
148         u_int i;
149
150         for (i = 0; log_levels[i].name != NULL; i++)
151                 if (log_levels[i].val == level)
152                         return log_levels[i].name;
153         return NULL;
154 }
155
156 /* Error messages that should be logged. */
157
158 void
159 error(const char *fmt,...)
160 {
161         va_list args;
162
163         va_start(args, fmt);
164         do_log(SYSLOG_LEVEL_ERROR, fmt, args);
165         va_end(args);
166 }
167
168 void
169 sigdie(const char *fmt,...)
170 {
171 #ifdef DO_LOG_SAFE_IN_SIGHAND
172         va_list args;
173
174         va_start(args, fmt);
175         do_log(SYSLOG_LEVEL_FATAL, fmt, args);
176         va_end(args);
177 #endif
178         _exit(1);
179 }
180
181
182 /* Log this message (information that usually should go to the log). */
183
184 void
185 logit(const char *fmt,...)
186 {
187         va_list args;
188
189         va_start(args, fmt);
190         do_log(SYSLOG_LEVEL_INFO, fmt, args);
191         va_end(args);
192 }
193
194 /* More detailed messages (information that does not need to go to the log). */
195
196 void
197 verbose(const char *fmt,...)
198 {
199         va_list args;
200
201         va_start(args, fmt);
202         do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
203         va_end(args);
204 }
205
206 /* Debugging messages that should not be logged during normal operation. */
207
208 void
209 debug(const char *fmt,...)
210 {
211         va_list args;
212
213         va_start(args, fmt);
214         do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
215         va_end(args);
216 }
217
218 void
219 debug2(const char *fmt,...)
220 {
221         va_list args;
222
223         va_start(args, fmt);
224         do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
225         va_end(args);
226 }
227
228 void
229 debug3(const char *fmt,...)
230 {
231         va_list args;
232
233         va_start(args, fmt);
234         do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
235         va_end(args);
236 }
237
238 /*
239  * Initialize the log.
240  */
241
242 void
243 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
244 {
245 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
246         struct syslog_data sdata = SYSLOG_DATA_INIT;
247 #endif
248
249         argv0 = av0;
250
251         switch (level) {
252         case SYSLOG_LEVEL_QUIET:
253         case SYSLOG_LEVEL_FATAL:
254         case SYSLOG_LEVEL_ERROR:
255         case SYSLOG_LEVEL_INFO:
256         case SYSLOG_LEVEL_VERBOSE:
257         case SYSLOG_LEVEL_DEBUG1:
258         case SYSLOG_LEVEL_DEBUG2:
259         case SYSLOG_LEVEL_DEBUG3:
260                 log_level = level;
261                 break;
262         default:
263                 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
264                     (int) level);
265                 exit(1);
266         }
267
268         log_handler = NULL;
269         log_handler_ctx = NULL;
270
271         log_on_stderr = on_stderr;
272         if (on_stderr)
273                 return;
274
275         switch (facility) {
276         case SYSLOG_FACILITY_DAEMON:
277                 log_facility = LOG_DAEMON;
278                 break;
279         case SYSLOG_FACILITY_USER:
280                 log_facility = LOG_USER;
281                 break;
282         case SYSLOG_FACILITY_AUTH:
283                 log_facility = LOG_AUTH;
284                 break;
285 #ifdef LOG_AUTHPRIV
286         case SYSLOG_FACILITY_AUTHPRIV:
287                 log_facility = LOG_AUTHPRIV;
288                 break;
289 #endif
290         case SYSLOG_FACILITY_LOCAL0:
291                 log_facility = LOG_LOCAL0;
292                 break;
293         case SYSLOG_FACILITY_LOCAL1:
294                 log_facility = LOG_LOCAL1;
295                 break;
296         case SYSLOG_FACILITY_LOCAL2:
297                 log_facility = LOG_LOCAL2;
298                 break;
299         case SYSLOG_FACILITY_LOCAL3:
300                 log_facility = LOG_LOCAL3;
301                 break;
302         case SYSLOG_FACILITY_LOCAL4:
303                 log_facility = LOG_LOCAL4;
304                 break;
305         case SYSLOG_FACILITY_LOCAL5:
306                 log_facility = LOG_LOCAL5;
307                 break;
308         case SYSLOG_FACILITY_LOCAL6:
309                 log_facility = LOG_LOCAL6;
310                 break;
311         case SYSLOG_FACILITY_LOCAL7:
312                 log_facility = LOG_LOCAL7;
313                 break;
314         default:
315                 fprintf(stderr,
316                     "Unrecognized internal syslog facility code %d\n",
317                     (int) facility);
318                 exit(1);
319         }
320
321         /*
322          * If an external library (eg libwrap) attempts to use syslog
323          * immediately after reexec, syslog may be pointing to the wrong
324          * facility, so we force an open/close of syslog here.
325          */
326 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
327         openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
328         closelog_r(&sdata);
329 #else
330         openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
331         closelog();
332 #endif
333 }
334
335 void
336 log_change_level(LogLevel new_log_level)
337 {
338         /* no-op if log_init has not been called */
339         if (argv0 == NULL)
340                 return;
341         log_init(argv0, new_log_level, log_facility, log_on_stderr);
342 }
343
344 int
345 log_is_on_stderr(void)
346 {
347         return log_on_stderr;
348 }
349
350 /* redirect what would usually get written to stderr to specified file */
351 void
352 log_redirect_stderr_to(const char *logfile)
353 {
354         int fd;
355
356         if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600)) == -1) {
357                 fprintf(stderr, "Couldn't open logfile %s: %s\n", logfile,
358                      strerror(errno));
359                 exit(1);
360         }
361         log_stderr_fd = fd;
362 }
363
364 #define MSGBUFSIZ 1024
365
366 void
367 set_log_handler(log_handler_fn *handler, void *ctx)
368 {
369         log_handler = handler;
370         log_handler_ctx = ctx;
371 }
372
373 void
374 do_log2(LogLevel level, const char *fmt,...)
375 {
376         va_list args;
377
378         va_start(args, fmt);
379         do_log(level, fmt, args);
380         va_end(args);
381 }
382
383 void
384 do_log(LogLevel level, const char *fmt, va_list args)
385 {
386 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
387         struct syslog_data sdata = SYSLOG_DATA_INIT;
388 #endif
389         char msgbuf[MSGBUFSIZ];
390         char fmtbuf[MSGBUFSIZ];
391         char *txt = NULL;
392         int pri = LOG_INFO;
393         sigset_t nset, oset;
394         int saved_errno = errno;
395         log_handler_fn *tmp_handler;
396
397         if (level > log_level)
398                 return;
399
400         switch (level) {
401         case SYSLOG_LEVEL_FATAL:
402                 if (!log_on_stderr)
403                         txt = "fatal";
404                 pri = LOG_CRIT;
405                 break;
406         case SYSLOG_LEVEL_ERROR:
407                 if (!log_on_stderr)
408                         txt = "error";
409                 pri = LOG_ERR;
410                 break;
411         case SYSLOG_LEVEL_INFO:
412                 pri = LOG_INFO;
413                 break;
414         case SYSLOG_LEVEL_VERBOSE:
415                 pri = LOG_INFO;
416                 break;
417         case SYSLOG_LEVEL_DEBUG1:
418                 txt = "debug1";
419                 pri = LOG_DEBUG;
420                 break;
421         case SYSLOG_LEVEL_DEBUG2:
422                 txt = "debug2";
423                 pri = LOG_DEBUG;
424                 break;
425         case SYSLOG_LEVEL_DEBUG3:
426                 txt = "debug3";
427                 pri = LOG_DEBUG;
428                 break;
429         default:
430                 txt = "internal error";
431                 pri = LOG_ERR;
432                 break;
433         }
434         if (txt != NULL && log_handler == NULL) {
435                 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
436                 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
437         } else {
438                 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
439         }
440         strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
441             log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
442         if (log_handler != NULL) {
443                 /* Avoid recursion */
444                 tmp_handler = log_handler;
445                 log_handler = NULL;
446                 tmp_handler(level, fmtbuf, log_handler_ctx);
447                 log_handler = tmp_handler;
448         } else if (log_on_stderr) {
449                 snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
450                 (void)write(log_stderr_fd, msgbuf, strlen(msgbuf));
451         } else {
452                 /* Prevent a race between the grace_alarm
453                 * which writes a log message and terminates
454                 * and main sshd code that leads to deadlock
455                 * as syslog is not async safe.
456                 */
457                 sigemptyset(&nset);
458                 sigaddset(&nset, SIGALRM);
459                 sigprocmask(SIG_BLOCK, &nset, &oset);
460 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
461                 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
462                 syslog_r(pri, &sdata, "%.500s", fmtbuf);
463                 closelog_r(&sdata);
464 #else
465                 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
466                 syslog(pri, "%.500s", fmtbuf);
467                 closelog();
468 #endif
469                 sigprocmask(SIG_SETMASK, &oset, NULL);
470         }
471         errno = saved_errno;
472 }