📄 spo_alert_syslog.c
字号:
/*** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>**** 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** (at your option) 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*//* $Id$ *//* spo_alert_syslog * * Purpose: * * This module sends alerts to the syslog service. * * Arguments: * * Logging mechanism? * * Effect: * * Alerts are written to the syslog service with in the facility indicated by * the module arguments. * * Comments: * * First try * */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <sys/types.h>#include <syslog.h>#include <stdlib.h>#ifdef HAVE_STRINGS_H#include <strings.h>#endif#ifndef WIN32#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#endif /* !WIN32 */#include "decode.h"#include "event.h"#include "rules.h"#include "plugbase.h"#include "spo_plugbase.h"#include "debug.h"#include "parser.h"#include "mstring.h"#include "util.h"#include "snort.h"typedef struct _SyslogData{ int facility; int priority; int options;} SyslogData;void AlertSyslogInit(u_char *);SyslogData *ParseSyslogArgs(char *);void AlertSyslog(Packet *, char *, void *, Event *);void AlertSyslogCleanExit(int, void *);void AlertSyslogRestart(int, void *);/* * Function: SetupSyslog() * * Purpose: Registers the output plugin keyword and initialization * function into the output plugin list. This is the function that * gets called from InitOutputPlugins() in plugbase.c. * * Arguments: None. * * Returns: void function * */void AlertSyslogSetup(void){ /* link the preprocessor keyword to the init function in the preproc list */ RegisterOutputPlugin("alert_syslog", NT_OUTPUT_ALERT, AlertSyslogInit); DEBUG_WRAP(DebugMessage(DEBUG_INIT,"Output plugin: Alert-Syslog is setup...\n"););}/* * Function: AlertSyslogInit(u_char *) * * Purpose: Calls the argument parsing function, performs final setup on data * structs, links the preproc function into the function list. * * Arguments: args => ptr to argument string * * Returns: void function * */void AlertSyslogInit(u_char *args){ SyslogData *data; DEBUG_WRAP(DebugMessage(DEBUG_INIT, "Output: Alert-Syslog Initialized\n");); pv.alert_plugin_active = 1; /* parse the argument list from the rules file */ data = ParseSyslogArgs(args); if (pv.daemon_flag) data->options |= LOG_PID; openlog("snort", data->options, data->facility); DEBUG_WRAP(DebugMessage(DEBUG_INIT,"Linking syslog alert function to call list...\n");); /* Set the preprocessor function into the function list */ AddFuncToOutputList(AlertSyslog, NT_OUTPUT_ALERT, data); AddFuncToCleanExitList(AlertSyslogCleanExit, data); AddFuncToRestartList(AlertSyslogRestart, data);}/* * Function: ParseSyslogArgs(char *) * * Purpose: Process the preprocessor arguements from the rules file and * initialize the preprocessor's data struct. This function doesn't * have to exist if it makes sense to parse the args in the init * function. * * Arguments: args => argument list * * Returns: void function * */SyslogData *ParseSyslogArgs(char *args){#ifdef WIN32 char *DEFAULT_SYSLOG_HOST = "127.0.0.1"; int DEFAULT_SYSLOG_PORT = 514; char **config_toks; char **host_toks; char *host_string = args; int num_config_toks, num_host_toks;#endif char **facility_toks; char *facility_string = args; int num_facility_toks = 0; int i = 0; SyslogData *data; char *tmp; data = (SyslogData *)SnortAlloc(sizeof(SyslogData)); /* default values for syslog output */ data->options = 0; data->facility = LOG_AUTH; data->priority = LOG_INFO; if(args == NULL) { /* horrible kludge to catch default initialization */ if(file_name != NULL) { LogMessage("%s(%d) => No arguments to alert_syslog preprocessor!\n", file_name, file_line); } return data; } /* * NON-WIN32: Config should be in the format: * output alert_syslog: LOG_AUTH LOG_ALERT * * WIN32: Config can be in any of these formats: * output alert_syslog: LOG_AUTH LOG_ALERT * output alert_syslog: host=hostname, LOG_AUTH LOG_ALERT * output alert_syslog: host=hostname:port, LOG_AUTH LOG_ALERT */#ifdef WIN32 /* split the host/port part from the facilities/priorities part */ config_toks = mSplit(args, ",", 2, &num_config_toks, '\\'); switch( num_config_toks ) { case 1: /* config consists of only facility/priority info */ LogMessage("alert_syslog output processor is defaulting to syslog " "server on %s port %d!\n", DEFAULT_SYSLOG_HOST, DEFAULT_SYSLOG_PORT); strncpy(pv.syslog_server, DEFAULT_SYSLOG_HOST, STD_BUF-1); pv.syslog_server_port = DEFAULT_SYSLOG_PORT; facility_string = config_toks[0]; break; case 2: /* config consists of host info, and facility/priority info */ host_string = config_toks[0]; facility_string = config_toks[1]; /* split host_string into "host" vs. "server" vs. "port" */ host_toks = mSplit(host_string, "=:", 3, &num_host_toks, 0); if(num_host_toks > 0 && strcmp(host_toks[0], "host") != 0 ) { FatalError("%s(%d) => Badly formed alert_syslog 'host' " "argument ('%s')\n", file_name, file_line, host_string); } /* check for empty strings */ if((num_host_toks >= 1 && strlen(host_toks[0]) == 0) || (num_host_toks >= 2 && strlen(host_toks[1]) == 0) || (num_host_toks >= 3 && strlen(host_toks[2]) == 0)) { FatalError("%s(%d) => Badly formed alert_syslog 'host' " "argument ('%s')\n", file_name, file_line, host_string); } switch(num_host_toks) { case 2: /* ie, host=localhost (defaults to port 514) */ strncpy(pv.syslog_server, host_toks[1], STD_BUF-1); pv.syslog_server_port = DEFAULT_SYSLOG_PORT; /* default */ break; case 3: /* ie. host=localhost:514 */ strncpy(pv.syslog_server, host_toks[1], STD_BUF-1); pv.syslog_server_port = atoi(host_toks[2]); if( pv.syslog_server_port == 0 ) { pv.syslog_server_port = DEFAULT_SYSLOG_PORT; /*default*/ LogMessage("WARNING %s(%d) => alert_syslog port " "appears to be non-numeric ('%s'). Defaulting " "to port %d!\n", file_name, file_line, host_toks[2], DEFAULT_SYSLOG_PORT); } break; default: /* badly formed, should never occur */ FatalError("%s(%d) => Badly formed alert_syslog 'host' " "argument ('%s')\n", file_name, file_line, host_string); } mSplitFree(&host_toks, num_host_toks); break; default: FatalError("%s(%d) => Badly formed alert_syslog arguments ('%s')\n", file_name, file_line, args); } DEBUG_WRAP(DebugMessage(DEBUG_INIT, "Logging alerts to syslog " "server %s on port %d\n", pv.syslog_server, pv.syslog_server_port);); mSplitFree(&config_toks, num_facility_toks);#endif /* WIN32 */ /* tokenize the facility/priority argument list */ facility_toks = mSplit(facility_string, " |", 31, &num_facility_toks, '\\'); for(i = 0; i < num_facility_toks; i++) { if(*facility_toks[i] == '$') { if((tmp = VarGet(facility_toks[i]+1)) == NULL) { FatalError("%s(%d) => Undefined variable %s\n", file_name, file_line, facility_toks[i]); } } else { tmp = facility_toks[i]; } /* possible openlog options */#ifdef LOG_CONS if(!strcasecmp("LOG_CONS", tmp)) { data->options |= LOG_CONS; } else#endif#ifdef LOG_NDELAY if(!strcasecmp("LOG_NDELAY", tmp)) { data->options |= LOG_NDELAY; } else#endif#ifdef LOG_PERROR if(!strcasecmp("LOG_PERROR", tmp))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -