iPXE
syslog.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Syslog protocol
00029  *
00030  */
00031 
00032 #include <stdint.h>
00033 #include <stdlib.h>
00034 #include <ctype.h>
00035 #include <byteswap.h>
00036 #include <ipxe/xfer.h>
00037 #include <ipxe/open.h>
00038 #include <ipxe/tcpip.h>
00039 #include <ipxe/dhcp.h>
00040 #include <ipxe/dhcpv6.h>
00041 #include <ipxe/settings.h>
00042 #include <ipxe/console.h>
00043 #include <ipxe/lineconsole.h>
00044 #include <ipxe/syslog.h>
00045 #include <config/console.h>
00046 
00047 /* Set default console usage if applicable */
00048 #if ! ( defined ( CONSOLE_SYSLOG ) && CONSOLE_EXPLICIT ( CONSOLE_SYSLOG ) )
00049 #undef CONSOLE_SYSLOG
00050 #define CONSOLE_SYSLOG ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
00051 #endif
00052 
00053 /** The syslog server */
00054 static union {
00055         struct sockaddr sa;
00056         struct sockaddr_tcpip st;
00057         struct sockaddr_in sin;
00058         struct sockaddr_in6 sin6;
00059 } logserver = {
00060         .st = {
00061                 .st_port = htons ( SYSLOG_PORT ),
00062         },
00063 };
00064 
00065 /** Syslog UDP interface operations */
00066 static struct interface_operation syslogger_operations[] = {};
00067 
00068 /** Syslog UDP interface descriptor */
00069 static struct interface_descriptor syslogger_desc =
00070         INTF_DESC_PURE ( syslogger_operations );
00071 
00072 /** The syslog UDP interface */
00073 static struct interface syslogger = INTF_INIT ( syslogger_desc );
00074 
00075 /******************************************************************************
00076  *
00077  * Console driver
00078  *
00079  ******************************************************************************
00080  */
00081 
00082 /** Host name (for log messages) */
00083 static char *syslog_hostname;
00084 
00085 /** Domain name (for log messages) */
00086 static char *syslog_domain;
00087 
00088 /**
00089  * Transmit formatted syslog message
00090  *
00091  * @v xfer              Data transfer interface
00092  * @v severity          Severity
00093  * @v message           Message
00094  * @v terminator        Message terminator
00095  * @ret rc              Return status code
00096  */
00097 int syslog_send ( struct interface *xfer, unsigned int severity,
00098                   const char *message, const char *terminator ) {
00099         const char *hostname = ( syslog_hostname ? syslog_hostname : "" );
00100         const char *domain = ( ( hostname[0] && syslog_domain ) ?
00101                                syslog_domain : "" );
00102 
00103         return xfer_printf ( xfer, "<%d>%s%s%s%sipxe: %s%s",
00104                              SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
00105                                                severity ), hostname,
00106                              ( domain[0] ? "." : "" ), domain,
00107                              ( hostname[0] ? " " : "" ), message, terminator );
00108 }
00109 
00110 /******************************************************************************
00111  *
00112  * Console driver
00113  *
00114  ******************************************************************************
00115  */
00116 
00117 /** Syslog line buffer */
00118 static char syslog_buffer[SYSLOG_BUFSIZE];
00119 
00120 /** Syslog severity */
00121 static unsigned int syslog_severity = SYSLOG_DEFAULT_SEVERITY;
00122 
00123 /**
00124  * Handle ANSI set syslog priority (private sequence)
00125  *
00126  * @v ctx               ANSI escape sequence context
00127  * @v count             Parameter count
00128  * @v params            List of graphic rendition aspects
00129  */
00130 static void syslog_handle_priority ( struct ansiesc_context *ctx __unused,
00131                                      unsigned int count __unused,
00132                                      int params[] ) {
00133         if ( params[0] >= 0 ) {
00134                 syslog_severity = params[0];
00135         } else {
00136                 syslog_severity = SYSLOG_DEFAULT_SEVERITY;
00137         }
00138 }
00139 
00140 /** Syslog ANSI escape sequence handlers */
00141 static struct ansiesc_handler syslog_handlers[] = {
00142         { ANSIESC_LOG_PRIORITY, syslog_handle_priority },
00143         { 0, NULL }
00144 };
00145 
00146 /** Syslog line console */
00147 static struct line_console syslog_line = {
00148         .buffer = syslog_buffer,
00149         .len = sizeof ( syslog_buffer ),
00150         .ctx = {
00151                 .handlers = syslog_handlers,
00152         },
00153 };
00154 
00155 /** Syslog recursion marker */
00156 static int syslog_entered;
00157 
00158 /**
00159  * Print a character to syslog console
00160  *
00161  * @v character         Character to be printed
00162  */
00163 static void syslog_putchar ( int character ) {
00164         int rc;
00165 
00166         /* Ignore if we are already mid-logging */
00167         if ( syslog_entered )
00168                 return;
00169 
00170         /* Fill line buffer */
00171         if ( line_putchar ( &syslog_line, character ) == 0 )
00172                 return;
00173 
00174         /* Guard against re-entry */
00175         syslog_entered = 1;
00176 
00177         /* Send log message */
00178         if ( ( rc = syslog_send ( &syslogger, syslog_severity,
00179                                   syslog_buffer, "" ) ) != 0 ) {
00180                 DBG ( "SYSLOG could not send log message: %s\n",
00181                       strerror ( rc ) );
00182         }
00183 
00184         /* Clear re-entry flag */
00185         syslog_entered = 0;
00186 }
00187 
00188 /** Syslog console driver */
00189 struct console_driver syslog_console __console_driver = {
00190         .putchar = syslog_putchar,
00191         .disabled = CONSOLE_DISABLED,
00192         .usage = CONSOLE_SYSLOG,
00193 };
00194 
00195 /******************************************************************************
00196  *
00197  * Settings
00198  *
00199  ******************************************************************************
00200  */
00201 
00202 /** IPv4 syslog server setting */
00203 const struct setting syslog_setting __setting ( SETTING_MISC, syslog ) = {
00204         .name = "syslog",
00205         .description = "Syslog server",
00206         .tag = DHCP_LOG_SERVERS,
00207         .type = &setting_type_ipv4,
00208 };
00209 
00210 /** IPv6 syslog server setting */
00211 const struct setting syslog6_setting __setting ( SETTING_MISC, syslog6 ) = {
00212         .name = "syslog6",
00213         .description = "Syslog server",
00214         .tag = DHCPV6_LOG_SERVERS,
00215         .type = &setting_type_ipv6,
00216         .scope = &dhcpv6_scope,
00217 };
00218 
00219 /**
00220  * Strip invalid characters from host/domain name
00221  *
00222  * @v name              Name to strip
00223  */
00224 static void syslog_fix_name ( char *name ) {
00225         char *fixed = name;
00226         int c;
00227 
00228         /* Do nothing if name does not exist */
00229         if ( ! name )
00230                 return;
00231 
00232         /* Strip any non-printable or whitespace characters from the name */
00233         do {
00234                 c = *(name++);
00235                 *fixed = c;
00236                 if ( isprint ( c ) && ! isspace ( c ) )
00237                         fixed++;
00238         } while ( c );
00239 }
00240 
00241 /**
00242  * Apply syslog settings
00243  *
00244  * @ret rc              Return status code
00245  */
00246 static int apply_syslog_settings ( void ) {
00247         struct sockaddr old_logserver;
00248         int rc;
00249 
00250         /* Fetch hostname and domain name */
00251         free ( syslog_hostname );
00252         fetch_string_setting_copy ( NULL, &hostname_setting, &syslog_hostname );
00253         syslog_fix_name ( syslog_hostname );
00254         free ( syslog_domain );
00255         fetch_string_setting_copy ( NULL, &domain_setting, &syslog_domain );
00256         syslog_fix_name ( syslog_domain );
00257 
00258         /* Fetch log server */
00259         syslog_console.disabled = CONSOLE_DISABLED;
00260         memcpy ( &old_logserver, &logserver, sizeof ( old_logserver ) );
00261         logserver.sa.sa_family = 0;
00262         if ( fetch_ipv6_setting ( NULL, &syslog6_setting,
00263                                   &logserver.sin6.sin6_addr ) >= 0 ) {
00264                 logserver.sin6.sin6_family = AF_INET6;
00265         } else if ( fetch_ipv4_setting ( NULL, &syslog_setting,
00266                                          &logserver.sin.sin_addr ) >= 0 ) {
00267                 logserver.sin.sin_family = AF_INET;
00268         }
00269         if ( logserver.sa.sa_family ) {
00270                 syslog_console.disabled = 0;
00271                 DBG ( "SYSLOG using log server %s\n",
00272                       sock_ntoa ( &logserver.sa ) );
00273         }
00274 
00275         /* Do nothing unless log server has changed */
00276         if ( memcmp ( &logserver, &old_logserver, sizeof ( logserver ) ) == 0 )
00277                 return 0;
00278 
00279         /* Reset syslog connection */
00280         intf_restart ( &syslogger, 0 );
00281 
00282         /* Do nothing unless we have a log server */
00283         if ( syslog_console.disabled ) {
00284                 DBG ( "SYSLOG has no log server\n" );
00285                 return 0;
00286         }
00287 
00288         /* Connect to log server */
00289         if ( ( rc = xfer_open_socket ( &syslogger, SOCK_DGRAM,
00290                                        &logserver.sa, NULL ) ) != 0 ) {
00291                 DBG ( "SYSLOG cannot connect to log server: %s\n",
00292                       strerror ( rc ) );
00293                 return rc;
00294         }
00295 
00296         return 0;
00297 }
00298 
00299 /** Syslog settings applicator */
00300 struct settings_applicator syslog_applicator __settings_applicator = {
00301         .apply = apply_syslog_settings,
00302 };