You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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