These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / net / udp / syslog.c
1 /*
2  * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27  *
28  * Syslog protocol
29  *
30  */
31
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <byteswap.h>
36 #include <ipxe/xfer.h>
37 #include <ipxe/open.h>
38 #include <ipxe/tcpip.h>
39 #include <ipxe/dhcp.h>
40 #include <ipxe/dhcpv6.h>
41 #include <ipxe/settings.h>
42 #include <ipxe/console.h>
43 #include <ipxe/lineconsole.h>
44 #include <ipxe/syslog.h>
45 #include <config/console.h>
46
47 /* Set default console usage if applicable */
48 #if ! ( defined ( CONSOLE_SYSLOG ) && CONSOLE_EXPLICIT ( CONSOLE_SYSLOG ) )
49 #undef CONSOLE_SYSLOG
50 #define CONSOLE_SYSLOG ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
51 #endif
52
53 /** The syslog server */
54 static union {
55         struct sockaddr sa;
56         struct sockaddr_tcpip st;
57         struct sockaddr_in sin;
58         struct sockaddr_in6 sin6;
59 } logserver = {
60         .st = {
61                 .st_port = htons ( SYSLOG_PORT ),
62         },
63 };
64
65 /** Syslog UDP interface operations */
66 static struct interface_operation syslogger_operations[] = {};
67
68 /** Syslog UDP interface descriptor */
69 static struct interface_descriptor syslogger_desc =
70         INTF_DESC_PURE ( syslogger_operations );
71
72 /** The syslog UDP interface */
73 static struct interface syslogger = INTF_INIT ( syslogger_desc );
74
75 /******************************************************************************
76  *
77  * Console driver
78  *
79  ******************************************************************************
80  */
81
82 /** Host name (for log messages) */
83 static char *syslog_hostname;
84
85 /** Domain name (for log messages) */
86 static char *syslog_domain;
87
88 /**
89  * Transmit formatted syslog message
90  *
91  * @v xfer              Data transfer interface
92  * @v severity          Severity
93  * @v message           Message
94  * @v terminator        Message terminator
95  * @ret rc              Return status code
96  */
97 int syslog_send ( struct interface *xfer, unsigned int severity,
98                   const char *message, const char *terminator ) {
99         const char *hostname = ( syslog_hostname ? syslog_hostname : "" );
100         const char *domain = ( ( hostname[0] && syslog_domain ) ?
101                                syslog_domain : "" );
102
103         return xfer_printf ( xfer, "<%d>%s%s%s%sipxe: %s%s",
104                              SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
105                                                severity ), hostname,
106                              ( domain[0] ? "." : "" ), domain,
107                              ( hostname[0] ? " " : "" ), message, terminator );
108 }
109
110 /******************************************************************************
111  *
112  * Console driver
113  *
114  ******************************************************************************
115  */
116
117 /** Syslog line buffer */
118 static char syslog_buffer[SYSLOG_BUFSIZE];
119
120 /** Syslog severity */
121 static unsigned int syslog_severity = SYSLOG_DEFAULT_SEVERITY;
122
123 /**
124  * Handle ANSI set syslog priority (private sequence)
125  *
126  * @v ctx               ANSI escape sequence context
127  * @v count             Parameter count
128  * @v params            List of graphic rendition aspects
129  */
130 static void syslog_handle_priority ( struct ansiesc_context *ctx __unused,
131                                      unsigned int count __unused,
132                                      int params[] ) {
133         if ( params[0] >= 0 ) {
134                 syslog_severity = params[0];
135         } else {
136                 syslog_severity = SYSLOG_DEFAULT_SEVERITY;
137         }
138 }
139
140 /** Syslog ANSI escape sequence handlers */
141 static struct ansiesc_handler syslog_handlers[] = {
142         { ANSIESC_LOG_PRIORITY, syslog_handle_priority },
143         { 0, NULL }
144 };
145
146 /** Syslog line console */
147 static struct line_console syslog_line = {
148         .buffer = syslog_buffer,
149         .len = sizeof ( syslog_buffer ),
150         .ctx = {
151                 .handlers = syslog_handlers,
152         },
153 };
154
155 /** Syslog recursion marker */
156 static int syslog_entered;
157
158 /**
159  * Print a character to syslog console
160  *
161  * @v character         Character to be printed
162  */
163 static void syslog_putchar ( int character ) {
164         int rc;
165
166         /* Ignore if we are already mid-logging */
167         if ( syslog_entered )
168                 return;
169
170         /* Fill line buffer */
171         if ( line_putchar ( &syslog_line, character ) == 0 )
172                 return;
173
174         /* Guard against re-entry */
175         syslog_entered = 1;
176
177         /* Send log message */
178         if ( ( rc = syslog_send ( &syslogger, syslog_severity,
179                                   syslog_buffer, "" ) ) != 0 ) {
180                 DBG ( "SYSLOG could not send log message: %s\n",
181                       strerror ( rc ) );
182         }
183
184         /* Clear re-entry flag */
185         syslog_entered = 0;
186 }
187
188 /** Syslog console driver */
189 struct console_driver syslog_console __console_driver = {
190         .putchar = syslog_putchar,
191         .disabled = CONSOLE_DISABLED,
192         .usage = CONSOLE_SYSLOG,
193 };
194
195 /******************************************************************************
196  *
197  * Settings
198  *
199  ******************************************************************************
200  */
201
202 /** IPv4 syslog server setting */
203 const struct setting syslog_setting __setting ( SETTING_MISC, syslog ) = {
204         .name = "syslog",
205         .description = "Syslog server",
206         .tag = DHCP_LOG_SERVERS,
207         .type = &setting_type_ipv4,
208 };
209
210 /** IPv6 syslog server setting */
211 const struct setting syslog6_setting __setting ( SETTING_MISC, syslog6 ) = {
212         .name = "syslog6",
213         .description = "Syslog server",
214         .tag = DHCPV6_LOG_SERVERS,
215         .type = &setting_type_ipv6,
216         .scope = &ipv6_scope,
217 };
218
219 /**
220  * Strip invalid characters from host/domain name
221  *
222  * @v name              Name to strip
223  */
224 static void syslog_fix_name ( char *name ) {
225         char *fixed = name;
226         int c;
227
228         /* Do nothing if name does not exist */
229         if ( ! name )
230                 return;
231
232         /* Strip any non-printable or whitespace characters from the name */
233         do {
234                 c = *(name++);
235                 *fixed = c;
236                 if ( isprint ( c ) && ! isspace ( c ) )
237                         fixed++;
238         } while ( c );
239 }
240
241 /**
242  * Apply syslog settings
243  *
244  * @ret rc              Return status code
245  */
246 static int apply_syslog_settings ( void ) {
247         struct sockaddr old_logserver;
248         int rc;
249
250         /* Fetch hostname and domain name */
251         free ( syslog_hostname );
252         fetch_string_setting_copy ( NULL, &hostname_setting, &syslog_hostname );
253         syslog_fix_name ( syslog_hostname );
254         free ( syslog_domain );
255         fetch_string_setting_copy ( NULL, &domain_setting, &syslog_domain );
256         syslog_fix_name ( syslog_domain );
257
258         /* Fetch log server */
259         syslog_console.disabled = CONSOLE_DISABLED;
260         memcpy ( &old_logserver, &logserver, sizeof ( old_logserver ) );
261         logserver.sa.sa_family = 0;
262         if ( fetch_ipv6_setting ( NULL, &syslog6_setting,
263                                   &logserver.sin6.sin6_addr ) >= 0 ) {
264                 logserver.sin6.sin6_family = AF_INET6;
265         } else if ( fetch_ipv4_setting ( NULL, &syslog_setting,
266                                          &logserver.sin.sin_addr ) >= 0 ) {
267                 logserver.sin.sin_family = AF_INET;
268         }
269         if ( logserver.sa.sa_family ) {
270                 syslog_console.disabled = 0;
271                 DBG ( "SYSLOG using log server %s\n",
272                       sock_ntoa ( &logserver.sa ) );
273         }
274
275         /* Do nothing unless log server has changed */
276         if ( memcmp ( &logserver, &old_logserver, sizeof ( logserver ) ) == 0 )
277                 return 0;
278
279         /* Reset syslog connection */
280         intf_restart ( &syslogger, 0 );
281
282         /* Do nothing unless we have a log server */
283         if ( syslog_console.disabled ) {
284                 DBG ( "SYSLOG has no log server\n" );
285                 return 0;
286         }
287
288         /* Connect to log server */
289         if ( ( rc = xfer_open_socket ( &syslogger, SOCK_DGRAM,
290                                        &logserver.sa, NULL ) ) != 0 ) {
291                 DBG ( "SYSLOG cannot connect to log server: %s\n",
292                       strerror ( rc ) );
293                 return rc;
294         }
295
296         return 0;
297 }
298
299 /** Syslog settings applicator */
300 struct settings_applicator syslog_applicator __settings_applicator = {
301         .apply = apply_syslog_settings,
302 };