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.

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