Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / net / tcp / syslogs.c
1 /*
2  * Copyright (C) 2012 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  * Encrypted syslog protocol
25  *
26  */
27
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <byteswap.h>
31 #include <ipxe/xfer.h>
32 #include <ipxe/open.h>
33 #include <ipxe/tcpip.h>
34 #include <ipxe/dhcp.h>
35 #include <ipxe/settings.h>
36 #include <ipxe/console.h>
37 #include <ipxe/lineconsole.h>
38 #include <ipxe/tls.h>
39 #include <ipxe/syslog.h>
40 #include <config/console.h>
41
42 /* Set default console usage if applicable */
43 #if ! ( defined ( CONSOLE_SYSLOGS ) && CONSOLE_EXPLICIT ( CONSOLE_SYSLOGS ) )
44 #undef CONSOLE_SYSLOGS
45 #define CONSOLE_SYSLOGS ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
46 #endif
47
48 struct console_driver syslogs_console __console_driver;
49
50 /** The encrypted syslog server */
51 static struct sockaddr_tcpip logserver = {
52         .st_port = htons ( SYSLOG_PORT ),
53 };
54
55 /**
56  * Handle encrypted syslog TLS interface close
57  *
58  * @v intf              Interface
59  * @v rc                Reason for close
60  */
61 static void syslogs_close ( struct interface *intf __unused, int rc ) {
62
63         DBG ( "SYSLOGS console disconnected: %s\n", strerror ( rc ) );
64 }
65
66 /**
67  * Handle encrypted syslog TLS interface window change
68  *
69  * @v intf              Interface
70  */
71 static void syslogs_window_changed ( struct interface *intf ) {
72
73         /* Mark console as enabled when window first opens, indicating
74          * that TLS negotiation is complete.  (Do not disable console
75          * when window closes again, since TCP will close the window
76          * whenever there is unACKed data.)
77          */
78         if ( xfer_window ( intf ) ) {
79                 if ( syslogs_console.disabled )
80                         DBG ( "SYSLOGS console connected\n" );
81                 syslogs_console.disabled = 0;
82         }
83 }
84
85 /** Encrypted syslog TLS interface operations */
86 static struct interface_operation syslogs_operations[] = {
87         INTF_OP ( xfer_window_changed, struct interface *,
88                   syslogs_window_changed ),
89         INTF_OP ( intf_close, struct interface *, syslogs_close ),
90 };
91
92 /** Encrypted syslog TLS interface descriptor */
93 static struct interface_descriptor syslogs_desc =
94         INTF_DESC_PURE ( syslogs_operations );
95
96 /** The encrypted syslog TLS interface */
97 static struct interface syslogs = INTF_INIT ( syslogs_desc );
98
99 /******************************************************************************
100  *
101  * Console driver
102  *
103  ******************************************************************************
104  */
105
106 /** Encrypted syslog line buffer */
107 static char syslogs_buffer[SYSLOG_BUFSIZE];
108
109 /** Encrypted syslog severity */
110 static unsigned int syslogs_severity = SYSLOG_DEFAULT_SEVERITY;
111
112 /**
113  * Handle ANSI set encrypted syslog priority (private sequence)
114  *
115  * @v ctx               ANSI escape sequence context
116  * @v count             Parameter count
117  * @v params            List of graphic rendition aspects
118  */
119 static void syslogs_handle_priority ( struct ansiesc_context *ctx __unused,
120                                       unsigned int count __unused,
121                                       int params[] ) {
122         if ( params[0] >= 0 ) {
123                 syslogs_severity = params[0];
124         } else {
125                 syslogs_severity = SYSLOG_DEFAULT_SEVERITY;
126         }
127 }
128
129 /** Encrypted syslog ANSI escape sequence handlers */
130 static struct ansiesc_handler syslogs_handlers[] = {
131         { ANSIESC_LOG_PRIORITY, syslogs_handle_priority },
132         { 0, NULL }
133 };
134
135 /** Encrypted syslog line console */
136 static struct line_console syslogs_line = {
137         .buffer = syslogs_buffer,
138         .len = sizeof ( syslogs_buffer ),
139         .ctx = {
140                 .handlers = syslogs_handlers,
141         },
142 };
143
144 /** Encrypted syslog recursion marker */
145 static int syslogs_entered;
146
147 /**
148  * Print a character to encrypted syslog console
149  *
150  * @v character         Character to be printed
151  */
152 static void syslogs_putchar ( int character ) {
153         int rc;
154
155         /* Ignore if we are already mid-logging */
156         if ( syslogs_entered )
157                 return;
158
159         /* Fill line buffer */
160         if ( line_putchar ( &syslogs_line, character ) == 0 )
161                 return;
162
163         /* Guard against re-entry */
164         syslogs_entered = 1;
165
166         /* Send log message */
167         if ( ( rc = syslog_send ( &syslogs, syslogs_severity,
168                                   syslogs_buffer, "\n" ) ) != 0 ) {
169                 DBG ( "SYSLOGS could not send log message: %s\n",
170                       strerror ( rc ) );
171         }
172
173         /* Clear re-entry flag */
174         syslogs_entered = 0;
175 }
176
177 /** Encrypted syslog console driver */
178 struct console_driver syslogs_console __console_driver = {
179         .putchar = syslogs_putchar,
180         .disabled = CONSOLE_DISABLED,
181         .usage = CONSOLE_SYSLOGS,
182 };
183
184 /******************************************************************************
185  *
186  * Settings
187  *
188  ******************************************************************************
189  */
190
191 /** Encrypted syslog server setting */
192 const struct setting syslogs_setting __setting ( SETTING_MISC, syslogs ) = {
193         .name = "syslogs",
194         .description = "Encrypted syslog server",
195         .tag = DHCP_EB_SYSLOGS_SERVER,
196         .type = &setting_type_string,
197 };
198
199 /**
200  * Apply encrypted syslog settings
201  *
202  * @ret rc              Return status code
203  */
204 static int apply_syslogs_settings ( void ) {
205         static char *old_server;
206         char *server;
207         struct interface *socket;
208         int rc;
209
210         /* Fetch log server */
211         fetch_string_setting_copy ( NULL, &syslogs_setting, &server );
212
213         /* Do nothing unless log server has changed */
214         if ( ( ( server == NULL ) && ( old_server == NULL ) ) ||
215              ( ( server != NULL ) && ( old_server != NULL ) &&
216                ( strcmp ( server, old_server ) == 0 ) ) ) {
217                 rc = 0;
218                 goto out_no_change;
219         }
220         free ( old_server );
221         old_server = NULL;
222
223         /* Reset encrypted syslog connection */
224         syslogs_console.disabled = CONSOLE_DISABLED;
225         intf_restart ( &syslogs, 0 );
226
227         /* Do nothing unless we have a log server */
228         if ( ! server ) {
229                 DBG ( "SYSLOGS has no log server\n" );
230                 rc = 0;
231                 goto out_no_server;
232         }
233
234         /* Add TLS filter */
235         if ( ( rc = add_tls ( &syslogs, server, &socket ) ) != 0 ) {
236                 DBG ( "SYSLOGS cannot create TLS filter: %s\n",
237                       strerror ( rc ) );
238                 goto err_add_tls;
239         }
240
241         /* Connect to log server */
242         if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
243                                              (( struct sockaddr *) &logserver ),
244                                              server, NULL ) ) != 0 ) {
245                 DBG ( "SYSLOGS cannot connect to log server: %s\n",
246                       strerror ( rc ) );
247                 goto err_open_named_socket;
248         }
249         DBG ( "SYSLOGS using log server %s\n", server );
250
251         /* Record log server */
252         old_server = server;
253         server = NULL;
254
255         /* Success */
256         rc = 0;
257
258  err_open_named_socket:
259  err_add_tls:
260  out_no_server:
261  out_no_change:
262         free ( server );
263         return rc;
264 }
265
266 /** Encrypted syslog settings applicator */
267 struct settings_applicator syslogs_applicator __settings_applicator = {
268         .apply = apply_syslogs_settings,
269 };