suricata
output-json-template-rust.c
Go to the documentation of this file.
1 /* Copyright (C) 2018 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 
56 #if defined(HAVE_LIBJANSSON) && defined(HAVE_RUST)
57 
58 #include "rust-applayertemplate-logger-gen.h"
59 
60 typedef struct LogTemplateFileCtx_ {
61  LogFileCtx *file_ctx;
62  uint32_t flags;
63 } LogTemplateFileCtx;
64 
65 typedef struct LogTemplateLogThread_ {
66  LogTemplateFileCtx *templatelog_ctx;
67  uint32_t count;
68  MemBuffer *buffer;
69 } LogTemplateLogThread;
70 
71 static int JsonTemplateLogger(ThreadVars *tv, void *thread_data,
72  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
73 {
74  SCLogNotice("JsonTemplateLogger");
75  LogTemplateLogThread *thread = thread_data;
76 
77  json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "template-rust");
78  if (unlikely(js == NULL)) {
79  return TM_ECODE_FAILED;
80  }
81 
82  json_t *template_js = rs_template_logger_log(tx);
83  if (unlikely(template_js == NULL)) {
84  goto error;
85  }
86  json_object_set_new(js, "template", template_js);
87 
88  MemBufferReset(thread->buffer);
89  OutputJSONBuffer(js, thread->templatelog_ctx->file_ctx, &thread->buffer);
90  json_decref(js);
91 
92  return TM_ECODE_OK;
93 
94 error:
95  json_decref(js);
96  return TM_ECODE_FAILED;
97 }
98 
99 static void OutputTemplateLogDeInitCtxSub(OutputCtx *output_ctx)
100 {
101  LogTemplateFileCtx *templatelog_ctx = (LogTemplateFileCtx *)output_ctx->data;
102  SCFree(templatelog_ctx);
103  SCFree(output_ctx);
104 }
105 
106 static OutputInitResult OutputTemplateLogInitSub(ConfNode *conf,
107  OutputCtx *parent_ctx)
108 {
109  OutputInitResult result = { NULL, false };
110  OutputJsonCtx *ajt = parent_ctx->data;
111 
112  LogTemplateFileCtx *templatelog_ctx = SCCalloc(1, sizeof(*templatelog_ctx));
113  if (unlikely(templatelog_ctx == NULL)) {
114  return result;
115  }
116  templatelog_ctx->file_ctx = ajt->file_ctx;
117 
118  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
119  if (unlikely(output_ctx == NULL)) {
120  SCFree(templatelog_ctx);
121  return result;
122  }
123  output_ctx->data = templatelog_ctx;
124  output_ctx->DeInit = OutputTemplateLogDeInitCtxSub;
125 
126  SCLogNotice("Template log sub-module initialized.");
127 
129 
130  result.ctx = output_ctx;
131  result.ok = true;
132  return result;
133 }
134 
135 static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, const void *initdata, void **data)
136 {
137  LogTemplateLogThread *thread = SCCalloc(1, sizeof(*thread));
138  if (unlikely(thread == NULL)) {
139  return TM_ECODE_FAILED;
140  }
141 
142  if (initdata == NULL) {
143  SCLogDebug("Error getting context for EveLogTemplate. \"initdata\" is NULL.");
144  SCFree(thread);
145  return TM_ECODE_FAILED;
146  }
147 
148  thread->buffer = MemBufferCreateNew(JSON_OUTPUT_BUFFER_SIZE);
149  if (unlikely(thread->buffer == NULL)) {
150  SCFree(thread);
151  return TM_ECODE_FAILED;
152  }
153 
154  thread->templatelog_ctx = ((OutputCtx *)initdata)->data;
155  *data = (void *)thread;
156 
157  return TM_ECODE_OK;
158 }
159 
160 static TmEcode JsonTemplateLogThreadDeinit(ThreadVars *t, void *data)
161 {
162  LogTemplateLogThread *thread = (LogTemplateLogThread *)data;
163  if (thread == NULL) {
164  return TM_ECODE_OK;
165  }
166  if (thread->buffer != NULL) {
167  MemBufferFree(thread->buffer);
168  }
169  SCFree(thread);
170  return TM_ECODE_OK;
171 }
172 
174 {
175  /* TEMPLATE_START_REMOVE */
176  if (ConfGetNode("app-layer.protocols.template-rust") == NULL) {
177  return;
178  }
179  /* TEMPLATE_END_REMOVE */
180  /* Register as an eve sub-module. */
182  "JsonTemplateRustLog", "eve-log.template-rust",
183  OutputTemplateLogInitSub, ALPROTO_TEMPLATE_RUST, JsonTemplateLogger,
184  JsonTemplateLogThreadInit, JsonTemplateLogThreadDeinit, NULL);
185 
186  SCLogNotice("Template JSON logger registered.");
187 }
188 
189 #else /* No JSON support. */
190 
192 {
193 }
194 
195 #endif /* HAVE_LIBJANSSON */
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
uint16_t flags
#define SCLogDebug(...)
Definition: util-debug.h:335
void JsonTemplateRustLogRegister(void)
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
#define SCCalloc(nm, a)
Definition: util-mem.h:253
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
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:400
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define SCFree(a)
Definition: util-mem.h:322
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:269
uint16_t tx_id
void * data
Definition: tm-modules.h:81
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:176
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82