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