]> andersk Git - gssapi-openssh.git/blobdiff - openssh/log.c
fix bug in openssh patch
[gssapi-openssh.git] / openssh / log.c
index 39bc8b5d75a3aca0a5bcdb8a68f5745a3c34450c..7f8867482d95e55d9cd6ff471cf504163c31d775 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenBSD: log.c,v 1.39 2006/08/18 09:13:25 deraadt Exp $ */
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: log.c,v 1.18 2001/06/26 17:27:23 markus Exp $");
 
-#include "log.h"
-#include "xmalloc.h"
+#include <sys/types.h>
 
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <syslog.h>
+#include <unistd.h>
+#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
+# include <vis.h>
+#endif
+
+#include "xmalloc.h"
+#include "log.h"
 
 static LogLevel log_level = SYSLOG_LEVEL_INFO;
 static int log_on_stderr = 1;
@@ -48,6 +58,9 @@ static char *argv0;
 
 extern char *__progname;
 
+#define LOG_SYSLOG_VIS (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
+#define LOG_STDERR_VIS (VIS_SAFE|VIS_OCTAL)
+
 /* textual representation of log-facilities/levels */
 
 static struct {
@@ -68,7 +81,7 @@ static struct {
        { "LOCAL5",     SYSLOG_FACILITY_LOCAL5 },
        { "LOCAL6",     SYSLOG_FACILITY_LOCAL6 },
        { "LOCAL7",     SYSLOG_FACILITY_LOCAL7 },
-       { NULL, 0 }
+       { NULL,         SYSLOG_FACILITY_NOT_SET }
 };
 
 static struct {
@@ -85,61 +98,66 @@ static struct {
        { "DEBUG1",     SYSLOG_LEVEL_DEBUG1 },
        { "DEBUG2",     SYSLOG_LEVEL_DEBUG2 },
        { "DEBUG3",     SYSLOG_LEVEL_DEBUG3 },
-       { NULL, 0 }
+       { NULL,         SYSLOG_LEVEL_NOT_SET }
 };
 
-static void     do_log(LogLevel level, const char *fmt, va_list args);
-
 SyslogFacility
 log_facility_number(char *name)
 {
        int i;
+
        if (name != NULL)
                for (i = 0; log_facilities[i].name; i++)
                        if (strcasecmp(log_facilities[i].name, name) == 0)
                                return log_facilities[i].val;
-       return (SyslogFacility) - 1;
+       return SYSLOG_FACILITY_NOT_SET;
 }
 
 LogLevel
 log_level_number(char *name)
 {
        int i;
+
        if (name != NULL)
                for (i = 0; log_levels[i].name; i++)
                        if (strcasecmp(log_levels[i].name, name) == 0)
                                return log_levels[i].val;
-       return (LogLevel) - 1;
+       return SYSLOG_LEVEL_NOT_SET;
 }
-/* Fatal messages.  This function never returns. */
+
+/* Error messages that should be logged. */
 
 void
-fatal(const char *fmt,...)
+error(const char *fmt,...)
 {
        va_list args;
+
        va_start(args, fmt);
-       do_log(SYSLOG_LEVEL_FATAL, fmt, args);
+       do_log(SYSLOG_LEVEL_ERROR, fmt, args);
        va_end(args);
-       fatal_cleanup();
 }
 
-/* Error messages that should be logged. */
-
 void
-error(const char *fmt,...)
+sigdie(const char *fmt,...)
 {
+#ifdef DO_LOG_SAFE_IN_SIGHAND
        va_list args;
+
        va_start(args, fmt);
-       do_log(SYSLOG_LEVEL_ERROR, fmt, args);
+       do_log(SYSLOG_LEVEL_FATAL, fmt, args);
        va_end(args);
+#endif
+       _exit(1);
 }
 
+
 /* Log this message (information that usually should go to the log). */
 
 void
-log(const char *fmt,...)
+logit(const char *fmt,...)
 {
        va_list args;
+
        va_start(args, fmt);
        do_log(SYSLOG_LEVEL_INFO, fmt, args);
        va_end(args);
@@ -151,6 +169,7 @@ void
 verbose(const char *fmt,...)
 {
        va_list args;
+
        va_start(args, fmt);
        do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
        va_end(args);
@@ -162,6 +181,7 @@ void
 debug(const char *fmt,...)
 {
        va_list args;
+
        va_start(args, fmt);
        do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
        va_end(args);
@@ -171,6 +191,7 @@ void
 debug2(const char *fmt,...)
 {
        va_list args;
+
        va_start(args, fmt);
        do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
        va_end(args);
@@ -180,75 +201,12 @@ void
 debug3(const char *fmt,...)
 {
        va_list args;
+
        va_start(args, fmt);
        do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
        va_end(args);
 }
 
-/* Fatal cleanup */
-
-struct fatal_cleanup {
-       struct fatal_cleanup *next;
-       void (*proc) (void *);
-       void *context;
-};
-
-static struct fatal_cleanup *fatal_cleanups = NULL;
-
-/* Registers a cleanup function to be called by fatal() before exiting. */
-
-void
-fatal_add_cleanup(void (*proc) (void *), void *context)
-{
-       struct fatal_cleanup *cu;
-
-       cu = xmalloc(sizeof(*cu));
-       cu->proc = proc;
-       cu->context = context;
-       cu->next = fatal_cleanups;
-       fatal_cleanups = cu;
-}
-
-/* Removes a cleanup frunction to be called at fatal(). */
-
-void
-fatal_remove_cleanup(void (*proc) (void *context), void *context)
-{
-       struct fatal_cleanup **cup, *cu;
-
-       for (cup = &fatal_cleanups; *cup; cup = &cu->next) {
-               cu = *cup;
-               if (cu->proc == proc && cu->context == context) {
-                       *cup = cu->next;
-                       xfree(cu);
-                       return;
-               }
-       }
-       fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx",
-           (u_long) proc, (u_long) context);
-}
-
-/* Cleanup and exit */
-void
-fatal_cleanup(void)
-{
-       struct fatal_cleanup *cu, *next_cu;
-       static int called = 0;
-
-       if (called)
-               exit(255);
-       called = 1;
-       /* Call cleanup functions. */
-       for (cu = fatal_cleanups; cu; cu = next_cu) {
-               next_cu = cu->next;
-               debug("Calling cleanup 0x%lx(0x%lx)",
-                     (u_long) cu->proc, (u_long) cu->context);
-               (*cu->proc) (cu->context);
-       }
-       exit(255);
-}
-
-
 /*
  * Initialize the log.
  */
@@ -256,6 +214,10 @@ fatal_cleanup(void)
 void
 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
 {
+#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
+       struct syslog_data sdata = SYSLOG_DATA_INIT;
+#endif
+
        argv0 = av0;
 
        switch (level) {
@@ -324,13 +286,29 @@ log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
                    (int) facility);
                exit(1);
        }
+
+       /*
+        * If an external library (eg libwrap) attempts to use syslog
+        * immediately after reexec, syslog may be pointing to the wrong
+        * facility, so we force an open/close of syslog here.
+        */
+#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
+       openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
+       closelog_r(&sdata);
+#else
+       openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
+       closelog();
+#endif
 }
 
 #define MSGBUFSIZ 1024
 
-static void
+void
 do_log(LogLevel level, const char *fmt, va_list args)
 {
+#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
+       struct syslog_data sdata = SYSLOG_DATA_INIT;
+#endif
        char msgbuf[MSGBUFSIZ];
        char fmtbuf[MSGBUFSIZ];
        char *txt = NULL;
@@ -379,11 +357,20 @@ do_log(LogLevel level, const char *fmt, va_list args)
        } else {
                vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
        }
+       strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
+           log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
        if (log_on_stderr) {
-               fprintf(stderr, "%s\r\n", msgbuf);
+               snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
+               write(STDERR_FILENO, msgbuf, strlen(msgbuf));
        } else {
+#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
+               openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
+               syslog_r(pri, &sdata, "%.500s", fmtbuf);
+               closelog_r(&sdata);
+#else
                openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
-               syslog(pri, "%.500s", msgbuf);
+               syslog(pri, "%.500s", fmtbuf);
                closelog();
+#endif
        }
 }
This page took 0.283932 seconds and 4 git commands to generate.