AmendHub

Download:

jcs

/

subtext

/

amendments

/

585

logger: Add RFC3164 (BSD) syslog support

Log everything at system.notice for now

jcs made amendment 585 9 months ago
--- db.c Wed Jan 24 09:07:37 2024 +++ db.c Mon Feb 12 17:50:36 2024 @@ -69,6 +69,11 @@ struct struct_field config_fields[] = { offsetof(struct config, ipdb_path), 0, member_size(struct config, ipdb_path), CONFIG_REQUIRES_IPDB_REINIT }, + + { "Syslog Server IP", CONFIG_TYPE_IP, + offsetof(struct config, syslog_ip), + 0, 1, + CONFIG_REQUIRES_SYSLOG_REINIT }, { "Modem Port", CONFIG_TYPE_SHORT, offsetof(struct config, modem_port), @@ -564,6 +569,19 @@ db_migrate(struct db *tdb, short is_new, Str255 fullpa DB_TEXT_MENU_OPTIONS_ID, 0); } + break; + } + case 21: { + /* 20->21, syslog ip */ + struct config new_config = { 0 }; + + bile_read(tdb->bile, DB_CONFIG_RTYPE, 1, (char *)&new_config, + sizeof(new_config)); + + new_config.syslog_ip = 0; + + bile_write(tdb->bile, DB_CONFIG_RTYPE, 1, &new_config, + sizeof(new_config)); break; } } --- db.h Tue Nov 28 10:43:34 2023 +++ db.h Mon Feb 12 17:38:18 2024 @@ -19,7 +19,7 @@ #include <time.h> -#define DB_CUR_VERS 21 +#define DB_CUR_VERS 22 #define SUBTEXT_CREATOR 'SUBT' #define DB_TYPE 'STDB' @@ -96,6 +96,7 @@ struct config { short max_sysop_idle_minutes; char ipdb_path[255]; short modem_rings; + unsigned long syslog_ip; }; extern struct struct_field config_fields[]; --- logger.c Wed Nov 8 22:06:14 2023 +++ logger.c Mon Feb 12 23:21:01 2024 @@ -20,11 +20,20 @@ #include "focusable.h" #include "logger.h" #include "subtext.h" +#include "tcp.h" #include "user.h" #include "util.h" struct logger *logger = NULL; +static UDPiopb syslog_pb; +static StreamPtr syslog_stream = 0; +static char *syslog_rcv_buf = NULL; +#define SYSLOG_RCV_BUF_SIZE 2048 +static wdsEntry syslog_wds[2]; +static char *syslog_send_buf = NULL; +#define SYSLOG_SEND_BUF_SIZE 1024 + void logger_layout(Rect *init_bounds); void logger_key_down(struct focusable *focusable, EventRecord *event); void logger_mouse_down(struct focusable *focusable, EventRecord *event); @@ -35,6 +44,8 @@ void logger_resume(struct focusable *focusable, EventR bool logger_quit(struct focusable *focusable); void logger_flush_buffer(bool force); +void syslog_vprintf(const char *format, va_list ap); + void logger_init(void) { @@ -333,6 +344,12 @@ logger_printf(const char *format, ...) va_start(va, format); len = logger_vprintf(format, va); va_end(va); + + if (db->config.syslog_ip) { + va_start(va, format); + syslog_vprintf(format, va); + va_end(va); + } return len; } @@ -371,7 +388,7 @@ logger_vprintf(const char *format, va_list ap) if (logger->autoflush) logger_flush_buffer(false); - + return len; } @@ -466,4 +483,78 @@ no_overflow: logger->buffered_logs_len = 0; logger->buffered_logs[0] = '\0'; -} +} + +/* RFC3164 */ +void +syslog_vprintf(const char *format, va_list ap) +{ + size_t len; + time_t now = Time; + + if (!db->config.syslog_ip || syslog_send_buf == NULL) + return; + + /* system (3) priority, notice (5) severity */ + len = strftime(syslog_send_buf, SYSLOG_SEND_BUF_SIZE, + "<29>%b %d %H:%M:%S", localtime(&now)); + len += snprintf(syslog_send_buf + len, SYSLOG_SEND_BUF_SIZE - len, + " %s subtext: ", db->config.hostname); + + len += vsnprintf(syslog_send_buf + len, SYSLOG_SEND_BUF_SIZE - len, + format, ap); + + syslog_wds[0].ptr = (Ptr)syslog_send_buf; + syslog_wds[0].length = len; + syslog_wds[1].ptr = 0; + syslog_wds[1].length = 0; + + _UDPSend(&syslog_pb, syslog_stream, syslog_wds, + db->config.syslog_ip, 514, NULL, NULL, false); +} + +void +syslog_init(void) +{ + short error; + + if (!db->config.syslog_ip) + return; + + if (_TCPInit() != noErr) + panic("Failed initializing MacTCP"); + + syslog_rcv_buf = xmalloc(SYSLOG_RCV_BUF_SIZE); + if (syslog_rcv_buf == NULL) + panic("Failed allocating syslog buf"); + error = _UDPCreate(&syslog_pb, &syslog_stream, + (Ptr)syslog_rcv_buf, SYSLOG_RCV_BUF_SIZE, NULL, NULL, + NULL, false); + if (error) + panic("UDPCreate failed: %d", error); + + syslog_send_buf = xmalloc(SYSLOG_SEND_BUF_SIZE); + if (syslog_send_buf == NULL) + panic("Failed allocating syslog send buf"); +} + +void +syslog_deinit(void) +{ + if (syslog_stream) { + _UDPRelease(&syslog_pb, syslog_stream, NULL, NULL, false); + syslog_stream = 0; + } + + if (syslog_rcv_buf) + xfree(&syslog_rcv_buf); + if (syslog_send_buf) + xfree(&syslog_send_buf); +} + +bool +syslog_reinit(void) +{ + syslog_deinit(); + syslog_init(); +} --- logger.h Sat Oct 1 21:29:46 2022 +++ logger.h Mon Feb 12 17:53:25 2024 @@ -42,4 +42,8 @@ size_t logger_vprintf(const char *format, va_list ap); size_t logger_printf(const char *format, ...); void logger_update_title(void); +void syslog_init(void); +void syslog_deinit(void); +bool syslog_reinit(void); + #endif --- main.c Tue Jan 10 10:50:01 2023 +++ main.c Mon Feb 12 23:12:08 2024 @@ -107,6 +107,8 @@ main(void) logger_init(); logger_update_title(); + if (db->config.syslog_ip) + syslog_init(); zone_size = (unsigned long)CurStackBase - (unsigned long)ApplZone; stack_size = (unsigned long)CurStackBase - (unsigned long)ApplLimit; @@ -273,6 +275,8 @@ handle_exit(void) serial_atexit(); if (db->ipdb) ipdb_close(&db->ipdb); + if (db->config.syslog_ip) + syslog_deinit(); db_close(db); } --- settings.h Mon Nov 27 21:04:22 2023 +++ settings.h Mon Feb 12 17:49:57 2024 @@ -33,7 +33,8 @@ enum { CONFIG_REQUIRES_SERIAL_REINIT, CONFIG_REQUIRES_TELNET_REINIT, CONFIG_REQUIRES_BINKP_REINIT, - CONFIG_REQUIRES_IPDB_REINIT + CONFIG_REQUIRES_IPDB_REINIT, + CONFIG_REQUIRES_SYSLOG_REINIT }; struct struct_field { --- sysop.c Tue Jan 23 16:30:33 2024 +++ sysop.c Mon Feb 12 21:27:04 2024 @@ -21,6 +21,7 @@ #include "ansi.h" #include "binkp.h" #include "board.h" +#include "logger.h" #include "serial_local.h" #include "session.h" #include "sysop.h" @@ -268,6 +269,8 @@ next_field: if (db->config.ipdb_path[0]) db->ipdb = ipdb_open(db->config.ipdb_path); } + if (reinits & (1 << CONFIG_REQUIRES_SYSLOG_REINIT)) + syslog_reinit(); xfree(&new_config); --- telnet.c Sat Dec 2 20:26:57 2023 +++ telnet.c Mon Feb 12 23:14:53 2024 @@ -161,9 +161,6 @@ telnet_init(void) if (!db->config.telnet_port) return; - if (!did_initial_log) - logger_printf("[telnet] Initializing MacTCP"); - if (_TCPInit() != noErr) panic("Failed initializing MacTCP");