suricata
output-eve.h
Go to the documentation of this file.
1 /* Copyright (C) 2024 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  * \brief EVE logging subsystem
22  *
23  * This file will attempt to the main module for EVE logging
24  * sub-system. Currently most of the API resides in output-json.[ch],
25  * but due to some circular dependencies between EVE, and LogFileCtx,
26  * it made it hard to add EVE filetype modules there until some
27  * include issues are figured out.
28  */
29 
30 #ifndef SURICATA_OUTPUT_EVE_H
31 #define SURICATA_OUTPUT_EVE_H
32 
33 #include "suricata-common.h"
34 #include "rust.h"
35 #include "conf.h"
36 
37 typedef uint32_t ThreadId;
38 
39 /**
40  * \brief Structure used to define an EVE output file type plugin.
41  *
42  * EVE filetypes implement an object with a file-like interface and
43  * are used to output EVE log records to files, syslog, or
44  * database. They can be built-in such as the syslog (see
45  * SyslogInitialize()) and nullsink (see NullLogInitialize()) outputs,
46  * registered by a library user or dynamically loaded as a plugin.
47  *
48  * The life cycle of an EVE filetype is:
49  * - Init: called once for each EVE instance using this filetype
50  * - ThreadInit: called once for each output thread
51  * - Write: called for each log record
52  * - ThreadInit: called once for each output thread on exit
53  * - Deinit: called once for each EVE instance using this filetype on exit
54  *
55  * Examples:
56  * - built-in syslog: \ref src/output-eve-syslog.c
57  * - built-in nullsink: \ref src/output-eve-null.c
58  * - example plugin: \ref examples/plugins/c-json-filetype/filetype.c
59  *
60  * ### Multi-Threaded Note:
61  *
62  * The EVE logging system can be configured by the Suricata user to
63  * run in threaded or non-threaded modes. In the default non-threaded
64  * mode, ThreadInit will only be called once and the filetype does not
65  * need to be concerned with threads.
66  *
67  * However, in **threaded** mode, ThreadInit will be called multiple
68  * times and the filetype needs to be thread aware and thread-safe. If
69  * utilizing a unique resource such as a file for each thread then you
70  * may be naturally thread safe. However, if sharing a single file
71  * handle across all threads then your filetype will have to take care
72  * of locking, etc.
73  */
74 typedef struct SCEveFileType_ {
75  /**
76  * \brief The name of the output, used in the configuration.
77  *
78  * This name is used by the configuration file to specify the EVE
79  * filetype used.
80  *
81  * For example:
82  *
83  * \code{.yaml}
84  * outputs:
85  * - eve-log:
86  * filetype: my-output-name
87  * \endcode
88  */
89  const char *name;
90 
91  /**
92  * \brief Function to initialize this filetype.
93  *
94  * \param conf The ConfNode of the `eve-log` configuration
95  * section this filetype is being initialized for
96  *
97  * \param threaded Flag to specify if the EVE sub-systems is in
98  * threaded mode or not
99  *
100  * \param init_data An output pointer for filetype specific data
101  *
102  * \retval 0 on success, -1 on failure
103  */
104  int (*Init)(const ConfNode *conf, const bool threaded, void **init_data);
105 
106  /**
107  * \brief Initialize thread specific data.
108  *
109  * Initialize any thread specific data. For example, if
110  * implementing a file output you might open the files here, so
111  * you have one output file per thread.
112  *
113  * \param init_data Data setup during Init
114  *
115  * \param thread_id A unique ID to differentiate this thread from
116  * others. If EVE is not in threaded mode this will be called
117  * one with a ThreadId of 0. In threaded mode the ThreadId of
118  * 0 correlates to the main Suricata thread.
119  *
120  * \param thread_data Output pointer for any data required by this
121  * thread.
122  *
123  * \retval 0 on success, -1 on failure
124  */
125  int (*ThreadInit)(const void *init_data, const ThreadId thread_id, void **thread_data);
126 
127  /**
128  * \brief Called for each EVE log record.
129  *
130  * The Write function is called for each log EVE log record. The
131  * provided buffer contains a fully formatted EVE record in JSON
132  * format.
133  *
134  * \param buffer The fully formatted JSON EVE log record
135  *
136  * \param buffer_len The length of the buffer
137  *
138  * \param init_data The data setup in the call to Init
139  *
140  * \param thread_data The data setup in the call to ThreadInit
141  *
142  * \retval 0 on success, -1 on failure
143  */
144  int (*Write)(
145  const char *buffer, const int buffer_len, const void *init_data, void *thread_data);
146 
147  /**
148  * \brief Called to deinitialize each thread.
149  *
150  * This function will be called for each thread. It is where any
151  * resources allocated in ThreadInit should be released.
152  *
153  * \param init_data The data setup in Init
154  *
155  * \param thread_data The data setup in ThreadInit
156  */
157  void (*ThreadDeinit)(const void *init_data, void *thread_data);
158 
159  /**
160  * \brief Final call to deinitialize this filetype.
161  *
162  * Called, usually on exit to deinitialize and free any resources
163  * allocated during Init.
164  *
165  * \param init_data Data setup in the call to Init.
166  */
167  void (*Deinit)(void *init_data);
168 
169  /* Internal list management. */
172 
174 
175 SCEveFileType *SCEveFindFileType(const char *name);
176 
177 /** \brief Function type for EVE callbacks.
178  *
179  * The function type for callbacks registered with
180  * SCEveRegisterCallback. This function will be called with the
181  * JsonBuilder just prior to the top-level object being closed. New
182  * fields maybe added, however there is no way to alter existing
183  * objects already added to the JsonBuilder.
184  *
185  * \param tv The ThreadVars for the thread performing the logging.
186  * \param p Packet if available.
187  * \param f Flow if available.
188  * \param user User data provided during callback registration.
189  */
190 typedef void (*SCEveUserCallbackFn)(
191  ThreadVars *tv, const Packet *p, Flow *f, JsonBuilder *jb, void *user);
192 
193 /** \brief Register a callback for adding extra information to EVE logs.
194  *
195  * Allow users to register a callback for each EVE log. The callback
196  * is called just before the root object on the JsonBuilder is to be
197  * closed.
198  *
199  * New objects and fields can be append, but exist entries cannot be modified.
200  *
201  * Packet and Flow will be provided if available, but will other be
202  * NULL.
203  *
204  * Limitations: At this time the callbacks will only be called for EVE
205  * loggers that use JsonBuilder, notably this means it won't be called
206  * for stats records at this time.
207  *
208  * \returns true if callback is registered, false is not due to memory
209  * allocation error.
210  */
212 
213 /** \internal
214  *
215  * Run EVE callbacks.
216  */
217 void SCEveRunCallbacks(ThreadVars *tv, const Packet *p, Flow *f, JsonBuilder *jb);
218 
219 #endif
SCEveUserCallbackFn
void(* SCEveUserCallbackFn)(ThreadVars *tv, const Packet *p, Flow *f, JsonBuilder *jb, void *user)
Function type for EVE callbacks.
Definition: output-eve.h:190
SCEveRegisterCallback
bool SCEveRegisterCallback(SCEveUserCallbackFn fn, void *user)
Register a callback for adding extra information to EVE logs.
SCEveFindFileType
SCEveFileType * SCEveFindFileType(const char *name)
Definition: output-eve.c:82
SCEveFileType_::name
const char * name
The name of the output, used in the configuration.
Definition: output-eve.h:89
Flow_
Flow data structure.
Definition: flow.h:356
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:144
rust.h
ThreadId
uint32_t ThreadId
Definition: output-eve.h:37
SCEveFileType_::ThreadDeinit
void(* ThreadDeinit)(const void *init_data, void *thread_data)
Called to deinitialize each thread.
Definition: output-eve.h:157
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SCRegisterEveFileType
bool SCRegisterEveFileType(SCEveFileType *)
Register an Eve file type.
Definition: output-eve.c:100
Packet_
Definition: decode.h:473
conf.h
suricata-common.h
SCEveFileType_::Deinit
void(* Deinit)(void *init_data)
Final call to deinitialize this filetype.
Definition: output-eve.h:167
SCEveFileType_::TAILQ_ENTRY
TAILQ_ENTRY(SCEveFileType_) entries
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
SCEveFileType
struct SCEveFileType_ SCEveFileType
Structure used to define an EVE output file type plugin.
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:104
SCEveFileType_::ThreadInit
int(* ThreadInit)(const void *init_data, const ThreadId thread_id, void **thread_data)
Initialize thread specific data.
Definition: output-eve.h:125
SCEveRunCallbacks
void SCEveRunCallbacks(ThreadVars *tv, const Packet *p, Flow *f, JsonBuilder *jb)
Definition: output-eve.c:53
SCEveFileType_
Structure used to define an EVE output file type plugin.
Definition: output-eve.h:74