suricata
output-json-template-rust.c
Go to the documentation of this file.
1 /* Copyright (C) 2018-2022 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  * TODO: Update \author in this file and in output-json-template.h.
20  * TODO: Remove SCLogNotice statements, or convert to debug.
21  * TODO: Implement your app-layers logging.
22  */
23 
24 /**
25  * \file
26  *
27  * \author FirstName LastName <yourname@domain>
28  *
29  * Implement JSON/eve logging app-layer Template.
30  */
31 
32 #include "suricata-common.h"
33 #include "detect.h"
34 #include "pkt-var.h"
35 #include "conf.h"
36 
37 #include "threads.h"
38 #include "threadvars.h"
39 #include "tm-threads.h"
40 
41 #include "util-unittest.h"
42 #include "util-buffer.h"
43 #include "util-debug.h"
44 #include "util-byte.h"
45 
46 #include "output.h"
47 #include "output-json.h"
48 
49 #include "app-layer.h"
50 #include "app-layer-parser.h"
51 
53 #include "rust.h"
54 
55 typedef struct LogTemplateFileCtx_ {
56  uint32_t flags;
59 
60 typedef struct LogTemplateLogThread_ {
64 
65 static int JsonTemplateLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f,
66  void *state, void *tx, uint64_t tx_id)
67 {
68  SCLogNotice("JsonTemplateLogger");
69  LogTemplateLogThread *thread = thread_data;
70 
71  JsonBuilder *js =
72  CreateEveHeader(p, LOG_DIR_PACKET, "template", NULL, thread->templatelog_ctx->eve_ctx);
73  if (unlikely(js == NULL)) {
74  return TM_ECODE_FAILED;
75  }
76 
77  jb_open_object(js, "template");
78  if (!rs_template_logger_log(tx, js)) {
79  goto error;
80  }
81  jb_close(js);
82 
83  OutputJsonBuilderBuffer(js, thread->ctx);
84  jb_free(js);
85 
86  return TM_ECODE_OK;
87 
88 error:
89  jb_free(js);
90  return TM_ECODE_FAILED;
91 }
92 
93 static void OutputTemplateLogDeInitCtxSub(OutputCtx *output_ctx)
94 {
95  LogTemplateFileCtx *templatelog_ctx = (LogTemplateFileCtx *)output_ctx->data;
96  SCFree(templatelog_ctx);
97  SCFree(output_ctx);
98 }
99 
100 static OutputInitResult OutputTemplateLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
101 {
102  OutputInitResult result = { NULL, false };
103  OutputJsonCtx *ajt = parent_ctx->data;
104 
105  LogTemplateFileCtx *templatelog_ctx = SCCalloc(1, sizeof(*templatelog_ctx));
106  if (unlikely(templatelog_ctx == NULL)) {
107  return result;
108  }
109  templatelog_ctx->eve_ctx = ajt;
110 
111  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
112  if (unlikely(output_ctx == NULL)) {
113  SCFree(templatelog_ctx);
114  return result;
115  }
116  output_ctx->data = templatelog_ctx;
117  output_ctx->DeInit = OutputTemplateLogDeInitCtxSub;
118 
119  SCLogNotice("Template log sub-module initialized.");
120 
122 
123  result.ctx = output_ctx;
124  result.ok = true;
125  return result;
126 }
127 
128 static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, const void *initdata, void **data)
129 {
130  LogTemplateLogThread *thread = SCCalloc(1, sizeof(*thread));
131  if (unlikely(thread == NULL)) {
132  return TM_ECODE_FAILED;
133  }
134 
135  if (initdata == NULL) {
136  SCLogDebug("Error getting context for EveLogTemplate. \"initdata\" is NULL.");
137  goto error_exit;
138  }
139 
140  thread->templatelog_ctx = ((OutputCtx *)initdata)->data;
141  thread->ctx = CreateEveThreadCtx(t, thread->templatelog_ctx->eve_ctx);
142  if (!thread->ctx) {
143  goto error_exit;
144  }
145  *data = (void *)thread;
146 
147  return TM_ECODE_OK;
148 
149 error_exit:
150  SCFree(thread);
151  return TM_ECODE_FAILED;
152 }
153 
154 static TmEcode JsonTemplateLogThreadDeinit(ThreadVars *t, void *data)
155 {
156  LogTemplateLogThread *thread = (LogTemplateLogThread *)data;
157  if (thread == NULL) {
158  return TM_ECODE_OK;
159  }
160  FreeEveThreadCtx(thread->ctx);
161  SCFree(thread);
162  return TM_ECODE_OK;
163 }
164 
166 {
167  /* TEMPLATE_START_REMOVE */
168  if (ConfGetNode("app-layer.protocols.template") == NULL) {
169  return;
170  }
171  /* TEMPLATE_END_REMOVE */
172  /* Register as an eve sub-module. */
173  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonTemplateLog", "eve-log.template",
174  OutputTemplateLogInitSub, ALPROTO_TEMPLATE, JsonTemplateLogger,
175  JsonTemplateLogThreadInit, JsonTemplateLogThreadDeinit, NULL);
176 
177  SCLogNotice("Template JSON logger registered.");
178 }
LogTemplateLogThread
struct LogTemplateLogThread_ LogTemplateLogThread
LogTemplateLogThread_::templatelog_ctx
LogTemplateFileCtx * templatelog_ctx
Definition: output-json-template-rust.c:61
util-byte.h
tm-threads.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:176
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:356
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:934
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
rust.h
LogTemplateFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-template-rust.c:57
JsonTemplateRustLogRegister
void JsonTemplateRustLogRegister(void)
Definition: output-json-template-rust.c:165
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:89
util-debug.h
LogTemplateLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-template-rust.c:62
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:477
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:796
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
app-layer-parser.h
Packet_
Definition: decode.h:428
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:83
LogTemplateFileCtx
struct LogTemplateFileCtx_ LogTemplateFileCtx
OutputInitResult_
Definition: output.h:46
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:39
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
LogTemplateFileCtx_
Definition: output-json-template-rust.c:55
LogTemplateFileCtx_::flags
uint32_t flags
Definition: output-json-template-rust.c:56
OutputRegisterTxSubModule
void OutputRegisterTxSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:411
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:456
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
output-json-template-rust.h
ALPROTO_TEMPLATE
@ ALPROTO_TEMPLATE
Definition: app-layer-protos.h:59
LogTemplateLogThread_
Definition: output-json-template-rust.c:60
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:230
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
output.h
app-layer.h