suricata
output-eve-syslog.c
Go to the documentation of this file.
1 /* Copyright (C) 2021 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Mike Pomraning <mpomraning@qualys.com>
22  * \author Jeff Lucovsky <jeff@lucovsky.org>
23  *
24  * File-like output for logging: syslog
25  */
26 
27 #include "suricata-common.h" /* errno.h, string.h, etc. */
28 #include "output.h" /* DEFAULT_LOG_* */
29 #include "output-eve.h"
30 #include "output-eve-syslog.h"
31 #include "util-syslog.h"
32 
33 #ifdef OS_WIN32
34 void SyslogInitialize(void)
35 {
36 }
37 #else /* !OS_WIN32 */
38 #define OUTPUT_NAME "syslog"
39 
40 typedef struct Context_ {
43 
44 static int SyslogInit(const ConfNode *conf, const bool threaded, void **init_data)
45 {
46  Context *context = SCCalloc(1, sizeof(Context));
47  if (context == NULL) {
48  SCLogError("Unable to allocate context for %s", OUTPUT_NAME);
49  return -1;
50  }
51  const char *facility_s = ConfNodeLookupChildValue(conf, "facility");
52  if (facility_s == NULL) {
54  }
55 
56  int facility = SCMapEnumNameToValue(facility_s, SCSyslogGetFacilityMap());
57  if (facility == -1) {
58  SCLogWarning("Invalid syslog facility: \"%s\","
59  " now using \"%s\" as syslog facility",
62  }
63 
64  const char *level_s = ConfNodeLookupChildValue(conf, "level");
65  if (level_s != NULL) {
66  int level = SCMapEnumNameToValue(level_s, SCSyslogGetLogLevelMap());
67  if (level != -1) {
68  context->alert_syslog_level = level;
69  }
70  }
71 
72  const char *ident = ConfNodeLookupChildValue(conf, "identity");
73  /* if null we just pass that to openlog, which will then
74  * figure it out by itself. */
75 
76  openlog(ident, LOG_PID | LOG_NDELAY, facility);
77  SCLogNotice("Syslog: facility %s, level %s, ident %s", facility_s, level_s, ident);
78  *init_data = context;
79  return 0;
80 }
81 
82 static int SyslogWrite(
83  const char *buffer, const int buffer_len, const void *init_data, void *thread_data)
84 {
85  const Context *context = init_data;
86  syslog(context->alert_syslog_level, "%s", (const char *)buffer);
87 
88  return 0;
89 }
90 
91 static void SyslogDeInit(void *init_data)
92 {
93  if (init_data) {
94  closelog();
95  SCFree(init_data);
96  }
97 }
98 
99 void SyslogInitialize(void)
100 {
101  SCEveFileType *file_type = SCCalloc(1, sizeof(SCEveFileType));
102 
103  if (file_type == NULL) {
104  FatalError("Unable to allocate memory for eve file type %s", OUTPUT_NAME);
105  }
106 
107  file_type->name = OUTPUT_NAME;
108  file_type->Init = SyslogInit;
109  file_type->Deinit = SyslogDeInit;
110  file_type->Write = SyslogWrite;
111  if (!SCRegisterEveFileType(file_type)) {
112  FatalError("Failed to register EVE file type: %s", OUTPUT_NAME);
113  }
114 }
115 #endif /* !OS_WIN32 */
syslog
#define syslog(__pri, __fmt, __param)
Definition: win32-syslog.h:78
SCEveFileType_::name
const char * name
The name of the output, used in the configuration.
Definition: output-eve.h:88
Context_
Definition: output-eve-syslog.c:40
util-syslog.h
SCEveFileType_::Write
int(* Write)(const char *buffer, const int buffer_len, const void *init_data, void *thread_data)
Called for each EVE log record.
Definition: output-eve.h:143
closelog
#define closelog()
Definition: win32-syslog.h:75
DEFAULT_ALERT_SYSLOG_FACILITY
#define DEFAULT_ALERT_SYSLOG_FACILITY
Definition: util-syslog.h:35
SCSyslogGetFacilityMap
SCEnumCharMap * SCSyslogGetFacilityMap(void)
returns the syslog facility enum map
Definition: util-syslog.c:57
DEFAULT_ALERT_SYSLOG_FACILITY_STR
#define DEFAULT_ALERT_SYSLOG_FACILITY_STR
Definition: util-syslog.h:34
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
SyslogInitialize
void SyslogInitialize(void)
Definition: output-eve-syslog.c:99
SCRegisterEveFileType
bool SCRegisterEveFileType(SCEveFileType *plugin)
Register an Eve file type.
Definition: output-eve.c:61
SCSyslogGetLogLevelMap
SCEnumCharMap * SCSyslogGetLogLevelMap(void)
returns the syslog facility enum map
Definition: util-syslog.c:75
SCMapEnumNameToValue
int SCMapEnumNameToValue(const char *enum_name, SCEnumCharMap *table)
Maps a string name to an enum value from the supplied table. Please specify the last element of any m...
Definition: util-enum.c:40
suricata-common.h
OUTPUT_NAME
#define OUTPUT_NAME
Definition: output-eve-syslog.c:38
output-eve-syslog.h
SCEveFileType_::Deinit
void(* Deinit)(void *init_data)
Final call to deinitialize this filetype.
Definition: output-eve.h:166
FatalError
#define FatalError(...)
Definition: util-debug.h:502
output-eve.h
EVE logging subsystem.
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
SCEveFileType_::Init
int(* Init)(const ConfNode *conf, const bool threaded, void **init_data)
Function to initialize this filetype.
Definition: output-eve.h:103
Context
struct Context_ Context
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:237
openlog
#define openlog(__ident, __option, __facility)
Definition: win32-syslog.h:76
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
Context_::alert_syslog_level
int alert_syslog_level
Definition: output-eve-syslog.c:41
output.h
SCEveFileType_
Structure used to define an EVE output file type plugin.
Definition: output-eve.h:73
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:814