suricata
output-json-template-rust.c
Go to the documentation of this file.
1 /* Copyright (C) 2018-2020 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 "debug.h"
34 #include "detect.h"
35 #include "pkt-var.h"
36 #include "conf.h"
37 
38 #include "threads.h"
39 #include "threadvars.h"
40 #include "tm-threads.h"
41 
42 #include "util-unittest.h"
43 #include "util-buffer.h"
44 #include "util-debug.h"
45 #include "util-byte.h"
46 
47 #include "output.h"
48 #include "output-json.h"
49 
50 #include "app-layer.h"
51 #include "app-layer-parser.h"
52 
55 #include "rust.h"
56 
57 typedef struct LogTemplateFileCtx_ {
59  uint32_t flags;
61 
62 typedef struct LogTemplateLogThread_ {
67 
68 static int JsonTemplateLogger(ThreadVars *tv, void *thread_data,
69  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
70 {
71  SCLogNotice("JsonTemplateLogger");
72  LogTemplateLogThread *thread = thread_data;
73 
74  JsonBuilder *js = CreateEveHeader(p, LOG_DIR_PACKET, "template-rust", NULL);
75  if (unlikely(js == NULL)) {
76  return TM_ECODE_FAILED;
77  }
78 
79  jb_open_object(js, "template");
80  if (!rs_template_logger_log(tx, js)) {
81  goto error;
82  }
83  jb_close(js);
84 
85  MemBufferReset(thread->buffer);
86  OutputJsonBuilderBuffer(js, thread->file_ctx, &thread->buffer);
87  jb_free(js);
88 
89  return TM_ECODE_OK;
90 
91 error:
92  jb_free(js);
93  return TM_ECODE_FAILED;
94 }
95 
96 static void OutputTemplateLogDeInitCtxSub(OutputCtx *output_ctx)
97 {
98  LogTemplateFileCtx *templatelog_ctx = (LogTemplateFileCtx *)output_ctx->data;
99  SCFree(templatelog_ctx);
100  SCFree(output_ctx);
101 }
102 
103 static OutputInitResult OutputTemplateLogInitSub(ConfNode *conf,
104  OutputCtx *parent_ctx)
105 {
106  OutputInitResult result = { NULL, false };
107  OutputJsonCtx *ajt = parent_ctx->data;
108 
109  LogTemplateFileCtx *templatelog_ctx = SCCalloc(1, sizeof(*templatelog_ctx));
110  if (unlikely(templatelog_ctx == NULL)) {
111  return result;
112  }
113  templatelog_ctx->file_ctx = ajt->file_ctx;
114 
115  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
116  if (unlikely(output_ctx == NULL)) {
117  SCFree(templatelog_ctx);
118  return result;
119  }
120  output_ctx->data = templatelog_ctx;
121  output_ctx->DeInit = OutputTemplateLogDeInitCtxSub;
122 
123  SCLogNotice("Template log sub-module initialized.");
124 
126 
127  result.ctx = output_ctx;
128  result.ok = true;
129  return result;
130 }
131 
132 static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, const void *initdata, void **data)
133 {
134  LogTemplateLogThread *thread = SCCalloc(1, sizeof(*thread));
135  if (unlikely(thread == NULL)) {
136  return TM_ECODE_FAILED;
137  }
138 
139  if (initdata == NULL) {
140  SCLogDebug("Error getting context for EveLogTemplate. \"initdata\" is NULL.");
141  goto error_exit;
142  }
143 
145  if (unlikely(thread->buffer == NULL)) {
146  goto error_exit;
147  }
148 
149  thread->templatelog_ctx = ((OutputCtx *)initdata)->data;
150  thread->file_ctx = LogFileEnsureExists(thread->templatelog_ctx->file_ctx, t->id);
151  if (!thread->file_ctx) {
152  goto error_exit;
153  }
154  *data = (void *)thread;
155 
156  return TM_ECODE_OK;
157 
158 error_exit:
159  if (thread->buffer != NULL) {
160  MemBufferFree(thread->buffer);
161  }
162  SCFree(thread);
163  return TM_ECODE_FAILED;
164 }
165 
166 static TmEcode JsonTemplateLogThreadDeinit(ThreadVars *t, void *data)
167 {
168  LogTemplateLogThread *thread = (LogTemplateLogThread *)data;
169  if (thread == NULL) {
170  return TM_ECODE_OK;
171  }
172  if (thread->buffer != NULL) {
173  MemBufferFree(thread->buffer);
174  }
175  SCFree(thread);
176  return TM_ECODE_OK;
177 }
178 
180 {
181  /* TEMPLATE_START_REMOVE */
182  if (ConfGetNode("app-layer.protocols.template-rust") == NULL) {
183  return;
184  }
185  /* TEMPLATE_END_REMOVE */
186  /* Register as an eve sub-module. */
188  "JsonTemplateRustLog", "eve-log.template-rust",
189  OutputTemplateLogInitSub, ALPROTO_TEMPLATE_RUST, JsonTemplateLogger,
190  JsonTemplateLogThreadInit, JsonTemplateLogThreadDeinit, NULL);
191 
192  SCLogNotice("Template JSON logger registered.");
193 }
LogTemplateLogThread
struct LogTemplateLogThread_ LogTemplateLogThread
LogTemplateLogThread_::templatelog_ctx
LogTemplateFileCtx * templatelog_ctx
Definition: output-json-template-rust.c:63
util-byte.h
LogTemplateLogThread_::buffer
MemBuffer * buffer
Definition: output-json-template-rust.c:65
tm-threads.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:175
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:63
threads.h
OutputJsonCtx_
Definition: output-json.h:101
Flow_
Flow data structure.
Definition: flow.h:347
LogFileCtx_
Definition: util-logopenfile.h:60
LOGGER_JSON_TEMPLATE_RUST
@ LOGGER_JSON_TEMPLATE_RUST
Definition: suricata-common.h:465
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:978
rust.h
JsonTemplateRustLogRegister
void JsonTemplateRustLogRegister(void)
Definition: output-json-template-rust.c:179
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:78
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:846
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:456
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(LogFileCtx *parent_ctx, int thread_id)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:659
OutputInitResult_::ok
bool ok
Definition: output.h:45
app-layer-parser.h
ThreadVars_::id
int id
Definition: threadvars.h:87
Packet_
Definition: decode.h:414
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
MemBuffer_
Definition: util-buffer.h:27
LogTemplateFileCtx
struct LogTemplateFileCtx_ LogTemplateFileCtx
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:43
app-layer-template-rust.h
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:57
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
LogTemplateFileCtx_::flags
uint32_t flags
Definition: output-json-template-rust.c:59
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:409
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
LogTemplateLogThread_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-template-rust.c:64
util-buffer.h
output-json-template-rust.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:102
LogTemplateLogThread_
Definition: output-json-template-rust.c:62
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:232
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
LogTemplateFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-template-rust.c:58
app-layer.h
ALPROTO_TEMPLATE_RUST
@ ALPROTO_TEMPLATE_RUST
Definition: app-layer-protos.h:57