2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
28 * Encrypted syslog protocol
35 #include <ipxe/xfer.h>
36 #include <ipxe/open.h>
37 #include <ipxe/tcpip.h>
38 #include <ipxe/dhcp.h>
39 #include <ipxe/settings.h>
40 #include <ipxe/console.h>
41 #include <ipxe/lineconsole.h>
43 #include <ipxe/syslog.h>
44 #include <config/console.h>
46 /* Set default console usage if applicable */
47 #if ! ( defined ( CONSOLE_SYSLOGS ) && CONSOLE_EXPLICIT ( CONSOLE_SYSLOGS ) )
48 #undef CONSOLE_SYSLOGS
49 #define CONSOLE_SYSLOGS ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
52 struct console_driver syslogs_console __console_driver
;
54 /** The encrypted syslog server */
55 static struct sockaddr_tcpip logserver
= {
56 .st_port
= htons ( SYSLOG_PORT
),
60 * Handle encrypted syslog TLS interface close
63 * @v rc Reason for close
65 static void syslogs_close ( struct interface
*intf __unused
, int rc
) {
67 DBG ( "SYSLOGS console disconnected: %s\n", strerror ( rc
) );
71 * Handle encrypted syslog TLS interface window change
75 static void syslogs_window_changed ( struct interface
*intf
) {
77 /* Mark console as enabled when window first opens, indicating
78 * that TLS negotiation is complete. (Do not disable console
79 * when window closes again, since TCP will close the window
80 * whenever there is unACKed data.)
82 if ( xfer_window ( intf
) ) {
83 if ( syslogs_console
.disabled
)
84 DBG ( "SYSLOGS console connected\n" );
85 syslogs_console
.disabled
= 0;
89 /** Encrypted syslog TLS interface operations */
90 static struct interface_operation syslogs_operations
[] = {
91 INTF_OP ( xfer_window_changed
, struct interface
*,
92 syslogs_window_changed
),
93 INTF_OP ( intf_close
, struct interface
*, syslogs_close
),
96 /** Encrypted syslog TLS interface descriptor */
97 static struct interface_descriptor syslogs_desc
=
98 INTF_DESC_PURE ( syslogs_operations
);
100 /** The encrypted syslog TLS interface */
101 static struct interface syslogs
= INTF_INIT ( syslogs_desc
);
103 /******************************************************************************
107 ******************************************************************************
110 /** Encrypted syslog line buffer */
111 static char syslogs_buffer
[SYSLOG_BUFSIZE
];
113 /** Encrypted syslog severity */
114 static unsigned int syslogs_severity
= SYSLOG_DEFAULT_SEVERITY
;
117 * Handle ANSI set encrypted syslog priority (private sequence)
119 * @v ctx ANSI escape sequence context
120 * @v count Parameter count
121 * @v params List of graphic rendition aspects
123 static void syslogs_handle_priority ( struct ansiesc_context
*ctx __unused
,
124 unsigned int count __unused
,
126 if ( params
[0] >= 0 ) {
127 syslogs_severity
= params
[0];
129 syslogs_severity
= SYSLOG_DEFAULT_SEVERITY
;
133 /** Encrypted syslog ANSI escape sequence handlers */
134 static struct ansiesc_handler syslogs_handlers
[] = {
135 { ANSIESC_LOG_PRIORITY
, syslogs_handle_priority
},
139 /** Encrypted syslog line console */
140 static struct line_console syslogs_line
= {
141 .buffer
= syslogs_buffer
,
142 .len
= sizeof ( syslogs_buffer
),
144 .handlers
= syslogs_handlers
,
148 /** Encrypted syslog recursion marker */
149 static int syslogs_entered
;
152 * Print a character to encrypted syslog console
154 * @v character Character to be printed
156 static void syslogs_putchar ( int character
) {
159 /* Ignore if we are already mid-logging */
160 if ( syslogs_entered
)
163 /* Fill line buffer */
164 if ( line_putchar ( &syslogs_line
, character
) == 0 )
167 /* Guard against re-entry */
170 /* Send log message */
171 if ( ( rc
= syslog_send ( &syslogs
, syslogs_severity
,
172 syslogs_buffer
, "\n" ) ) != 0 ) {
173 DBG ( "SYSLOGS could not send log message: %s\n",
177 /* Clear re-entry flag */
181 /** Encrypted syslog console driver */
182 struct console_driver syslogs_console __console_driver
= {
183 .putchar
= syslogs_putchar
,
184 .disabled
= CONSOLE_DISABLED
,
185 .usage
= CONSOLE_SYSLOGS
,
188 /******************************************************************************
192 ******************************************************************************
195 /** Encrypted syslog server setting */
196 const struct setting syslogs_setting
__setting ( SETTING_MISC
, syslogs
) = {
198 .description
= "Encrypted syslog server",
199 .tag
= DHCP_EB_SYSLOGS_SERVER
,
200 .type
= &setting_type_string
,
204 * Apply encrypted syslog settings
206 * @ret rc Return status code
208 static int apply_syslogs_settings ( void ) {
209 static char *old_server
;
211 struct interface
*socket
;
214 /* Fetch log server */
215 fetch_string_setting_copy ( NULL
, &syslogs_setting
, &server
);
217 /* Do nothing unless log server has changed */
218 if ( ( ( server
== NULL
) && ( old_server
== NULL
) ) ||
219 ( ( server
!= NULL
) && ( old_server
!= NULL
) &&
220 ( strcmp ( server
, old_server
) == 0 ) ) ) {
227 /* Reset encrypted syslog connection */
228 syslogs_console
.disabled
= CONSOLE_DISABLED
;
229 intf_restart ( &syslogs
, 0 );
231 /* Do nothing unless we have a log server */
233 DBG ( "SYSLOGS has no log server\n" );
239 if ( ( rc
= add_tls ( &syslogs
, server
, &socket
) ) != 0 ) {
240 DBG ( "SYSLOGS cannot create TLS filter: %s\n",
245 /* Connect to log server */
246 if ( ( rc
= xfer_open_named_socket ( socket
, SOCK_STREAM
,
247 (( struct sockaddr
*) &logserver
),
248 server
, NULL
) ) != 0 ) {
249 DBG ( "SYSLOGS cannot connect to log server: %s\n",
251 goto err_open_named_socket
;
253 DBG ( "SYSLOGS using log server %s\n", server
);
255 /* Record log server */
262 err_open_named_socket
:
270 /** Encrypted syslog settings applicator */
271 struct settings_applicator syslogs_applicator __settings_applicator
= {
272 .apply
= apply_syslogs_settings
,