suricata
output-eve.c
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 #include "suricata-common.h"
19 #include "output-eve.h"
20 #include "util-debug.h"
21 
22 typedef struct EveUserCallback_ {
24  void *user;
27 
28 static EveUserCallback *eve_user_callbacks = NULL;
29 
30 static TAILQ_HEAD(, SCEveFileType_) output_types = TAILQ_HEAD_INITIALIZER(output_types);
31 
33 {
34  EveUserCallback *cb = SCCalloc(1, sizeof(*cb));
35  if (cb == NULL) {
36  return false;
37  }
38  cb->Callback = fn;
39  cb->user = user;
40  if (eve_user_callbacks == NULL) {
41  eve_user_callbacks = cb;
42  } else {
43  EveUserCallback *current = eve_user_callbacks;
44  while (current->next != NULL) {
45  current = current->next;
46  }
47  current->next = cb;
48  }
49  return true;
50 }
51 
52 void SCEveRunCallbacks(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb)
53 {
54  EveUserCallback *cb = eve_user_callbacks;
55  while (cb != NULL) {
56  cb->Callback(tv, p, f, jb, cb->user);
57  cb = cb->next;
58  }
59 }
60 
61 static bool IsBuiltinTypeName(const char *name)
62 {
63  const char *builtin[] = {
64  "regular",
65  "unix_dgram",
66  "unix_stream",
67  "redis",
68  NULL,
69  };
70  for (int i = 0;; i++) {
71  if (builtin[i] == NULL) {
72  break;
73  }
74  if (strcmp(builtin[i], name) == 0) {
75  return true;
76  }
77  }
78  return false;
79 }
80 
82 {
83  SCEveFileType *plugin = NULL;
84  TAILQ_FOREACH (plugin, &output_types, entries) {
85  if (strcmp(name, plugin->name) == 0) {
86  return plugin;
87  }
88  }
89  return NULL;
90 }
91 
92 /**
93  * \brief Register an Eve file type.
94  *
95  * \retval true if registered successfully, false if the file type name
96  * conflicts with a built-in or previously registered
97  * file type.
98  */
100 {
101  /* First check that the name doesn't conflict with a built-in filetype. */
102  if (IsBuiltinTypeName(plugin->name)) {
103  SCLogError("Eve file type name conflicts with built-in type: %s", plugin->name);
104  return false;
105  }
106 
107  /* Now check against previously registered file types. */
108  SCEveFileType *existing = NULL;
109  TAILQ_FOREACH (existing, &output_types, entries) {
110  if (strcmp(existing->name, plugin->name) == 0) {
111  SCLogError("Eve file type name conflicts with previously registered type: %s",
112  plugin->name);
113  return false;
114  }
115  }
116 
117  SCLogDebug("Registering EVE file type plugin %s", plugin->name);
118  TAILQ_INSERT_TAIL(&output_types, plugin, entries);
119  return true;
120 }
EveUserCallback
struct EveUserCallback_ EveUserCallback
SCEveRegisterCallback
bool SCEveRegisterCallback(SCEveUserCallbackFn fn, void *user)
Register a callback for adding extra information to EVE logs.
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
name
const char * name
Definition: detect-engine-proto.c:48
Flow_
Flow data structure.
Definition: flow.h:347
EveUserCallback_::user
void * user
Definition: output-eve.c:24
EveUserCallback_::Callback
SCEveUserCallbackFn Callback
Definition: output-eve.c:23
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
TAILQ_INSERT_TAIL
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:294
TAILQ_HEAD_INITIALIZER
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:236
util-debug.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SCRegisterEveFileType
bool SCRegisterEveFileType(SCEveFileType *plugin)
Register an Eve file type.
Definition: output-eve.c:99
Packet_
Definition: decode.h:505
SCEveFileType_::name
const char * name
The name of the output, used in the configuration.
Definition: output-eve-bindgen.h:125
EveUserCallback_::next
struct EveUserCallback_ * next
Definition: output-eve.c:25
suricata-common.h
EveUserCallback_
Definition: output-eve.c:22
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:33
output-eve.h
EVE logging subsystem.
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
TAILQ_HEAD
#define TAILQ_HEAD(name, type)
Definition: queue.h:230
SCEveFindFileType
SCEveFileType * SCEveFindFileType(const char *name)
Definition: output-eve.c:81
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCEveUserCallbackFn
void(* SCEveUserCallbackFn)(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb, void *user)
Function type for EVE callbacks.
Definition: output-eve.h:240
SCEveRunCallbacks
void SCEveRunCallbacks(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb)
Definition: output-eve.c:52
SCEveFileType_
Structure used to define an EVE output file type.
Definition: output-eve-bindgen.h:110