summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/net/udp/syslog.c
blob: d65d19ab835079b4812715f170ab23c8a0c4116a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

FILE_LICENCE ( GPL2_OR_LATER );

/** @file
 *
 * Syslog protocol
 *
 */

#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <byteswap.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/tcpip.h>
#include <ipxe/dhcp.h>
#include <ipxe/dhcpv6.h>
#include <ipxe/settings.h>
#include <ipxe/console.h>
#include <ipxe/lineconsole.h>
#include <ipxe/syslog.h>
#include <config/console.h>

/* Set default console usage if applicable */
#if ! ( defined ( CONSOLE_SYSLOG ) && CONSOLE_EXPLICIT ( CONSOLE_SYSLOG ) )
#undef CONSOLE_SYSLOG
#define CONSOLE_SYSLOG ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
#endif

/** The syslog server */
static union {
	struct sockaddr sa;
	struct sockaddr_tcpip st;
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;
} logserver = {
	.st = {
		.st_port = htons ( SYSLOG_PORT ),
	},
};

/** Syslog UDP interface operations */
static struct interface_operation syslogger_operations[] = {};

/** Syslog UDP interface descriptor */
static struct interface_descriptor syslogger_desc =
	INTF_DESC_PURE ( syslogger_operations );

/** The syslog UDP interface */
static struct interface syslogger = INTF_INIT ( syslogger_desc );

/******************************************************************************
 *
 * Console driver
 *
 ******************************************************************************
 */

/** Host name (for log messages) */
static char *syslog_hostname;

/** Domain name (for log messages) */
static char *syslog_domain;

/**
 * Transmit formatted syslog message
 *
 * @v xfer		Data transfer interface
 * @v severity		Severity
 * @v message		Message
 * @v terminator	Message terminator
 * @ret rc		Return status code
 */
int syslog_send ( struct interface *xfer, unsigned int severity,
		  const char *message, const char *terminator ) {
	const char *hostname = ( syslog_hostname ? syslog_hostname : "" );
	const char *domain = ( ( hostname[0] && syslog_domain ) ?
			       syslog_domain : "" );

	return xfer_printf ( xfer, "<%d>%s%s%s%sipxe: %s%s",
			     SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
					       severity ), hostname,
			     ( domain[0] ? "." : "" ), domain,
			     ( hostname[0] ? " " : "" ), message, terminator );
}

/******************************************************************************
 *
 * Console driver
 *
 ******************************************************************************
 */

/** Syslog line buffer */
static char syslog_buffer[SYSLOG_BUFSIZE];

/** Syslog severity */
static unsigned int syslog_severity = SYSLOG_DEFAULT_SEVERITY;

/**
 * Handle ANSI set syslog priority (private sequence)
 *
 * @v ctx		ANSI escape sequence context
 * @v count		Parameter count
 * @v params		List of graphic rendition aspects
 */
static void syslog_handle_priority ( struct ansiesc_context *ctx __unused,
				     unsigned int count __unused,
				     int params[] ) {
	if ( params[0] >= 0 ) {
		syslog_severity = params[0];
	} else {
		syslog_severity = SYSLOG_DEFAULT_SEVERITY;
	}
}

/** Syslog ANSI escape sequence handlers */
static struct ansiesc_handler syslog_handlers[] = {
	{ ANSIESC_LOG_PRIORITY, syslog_handle_priority },
	{ 0, NULL }
};

/** Syslog line console */
static struct line_console syslog_line = {
	.buffer = syslog_buffer,
	.len = sizeof ( syslog_buffer ),
	.ctx = {
		.handlers = syslog_handlers,
	},
};

/** Syslog recursion marker */
static int syslog_entered;

/**
 * Print a character to syslog console
 *
 * @v character		Character to be printed
 */
static void syslog_putchar ( int character ) {
	int rc;

	/* Ignore if we are already mid-logging */
	if ( syslog_entered )
		return;

	/* Fill line buffer */
	if ( line_putchar ( &syslog_line, character ) == 0 )
		return;

	/* Guard against re-entry */
	syslog_entered = 1;

	/* Send log message */
	if ( ( rc = syslog_send ( &syslogger, syslog_severity,
				  syslog_buffer, "" ) ) != 0 ) {
		DBG ( "SYSLOG could not send log message: %s\n",
		      strerror ( rc ) );
	}

	/* Clear re-entry flag */
	syslog_entered = 0;
}

/** Syslog console driver */
struct console_driver syslog_console __console_driver = {
	.putchar = syslog_putchar,
	.disabled = CONSOLE_DISABLED,
	.usage = CONSOLE_SYSLOG,
};

/******************************************************************************
 *
 * Settings
 *
 ******************************************************************************
 */

/** IPv4 syslog server setting */
const struct setting syslog_setting __setting ( SETTING_MISC, syslog ) = {
	.name = "syslog",
	.description = "Syslog server",
	.tag = DHCP_LOG_SERVERS,
	.type = &setting_type_ipv4,
};

/** IPv6 syslog server setting */
const struct setting syslog6_setting __setting ( SETTING_MISC, syslog6 ) = {
	.name = "syslog6",
	.description = "Syslog server",
	.tag = DHCPV6_LOG_SERVERS,
	.type = &setting_type_ipv6,
	.scope = &ipv6_scope,
};

/**
 * Strip invalid characters from host/domain name
 *
 * @v name		Name to strip
 */
static void syslog_fix_name ( char *name ) {
	char *fixed = name;
	int c;

	/* Do nothing if name does not exist */
	if ( ! name )
		return;

	/* Strip any non-printable or whitespace characters from the name */
	do {
		c = *(name++);
		*fixed = c;
		if ( isprint ( c ) && ! isspace ( c ) )
			fixed++;
	} while ( c );
}

/**
 * Apply syslog settings
 *
 * @ret rc		Return status code
 */
static int apply_syslog_settings ( void ) {
	struct sockaddr old_logserver;
	int rc;

	/* Fetch hostname and domain name */
	free ( syslog_hostname );
	fetch_string_setting_copy ( NULL, &hostname_setting, &syslog_hostname );
	syslog_fix_name ( syslog_hostname );
	free ( syslog_domain );
	fetch_string_setting_copy ( NULL, &domain_setting, &syslog_domain );
	syslog_fix_name ( syslog_domain );

	/* Fetch log server */
	syslog_console.disabled = CONSOLE_DISABLED;
	memcpy ( &old_logserver, &logserver, sizeof ( old_logserver ) );
	logserver.sa.sa_family = 0;
	if ( fetch_ipv6_setting ( NULL, &syslog6_setting,
				  &logserver.sin6.sin6_addr ) >= 0 ) {
		logserver.sin6.sin6_family = AF_INET6;
	} else if ( fetch_ipv4_setting ( NULL, &syslog_setting,
					 &logserver.sin.sin_addr ) >= 0 ) {
		logserver.sin.sin_family = AF_INET;
	}
	if ( logserver.sa.sa_family ) {
		syslog_console.disabled = 0;
		DBG ( "SYSLOG using log server %s\n",
		      sock_ntoa ( &logserver.sa ) );
	}

	/* Do nothing unless log server has changed */
	if ( memcmp ( &logserver, &old_logserver, sizeof ( logserver ) ) == 0 )
		return 0;

	/* Reset syslog connection */
	intf_restart ( &syslogger, 0 );

	/* Do nothing unless we have a log server */
	if ( syslog_console.disabled ) {
		DBG ( "SYSLOG has no log server\n" );
		return 0;
	}

	/* Connect to log server */
	if ( ( rc = xfer_open_socket ( &syslogger, SOCK_DGRAM,
				       &logserver.sa, NULL ) ) != 0 ) {
		DBG ( "SYSLOG cannot connect to log server: %s\n",
		      strerror ( rc ) );
		return rc;
	}

	return 0;
}

/** Syslog settings applicator */
struct settings_applicator syslog_applicator __settings_applicator = {
	.apply = apply_syslog_settings,
};