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