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 #define OUTPUT_BUFFER_SIZE 65535
136 
137 static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, const void *initdata, void **data)
138 {
139  LogTemplateLogThread *thread = SCCalloc(1, sizeof(*thread));
140  if (unlikely(thread == NULL)) {
141  return TM_ECODE_FAILED;
142  }
143 
144  if (initdata == NULL) {
145  SCLogDebug("Error getting context for EveLogTemplate. \"initdata\" is NULL.");
146  SCFree(thread);
147  return TM_ECODE_FAILED;
148  }
149 
150  thread->buffer = MemBufferCreateNew(OUTPUT_BUFFER_SIZE);
151  if (unlikely(thread->buffer == NULL)) {
152  SCFree(thread);
153  return TM_ECODE_FAILED;
154  }
155 
156  thread->templatelog_ctx = ((OutputCtx *)initdata)->data;
157  *data = (void *)thread;
158 
159  return TM_ECODE_OK;
160 }
161 
162 static TmEcode JsonTemplateLogThreadDeinit(ThreadVars *t, void *data)
163 {
164  LogTemplateLogThread *thread = (LogTemplateLogThread *)data;
165  if (thread == NULL) {
166  return TM_ECODE_OK;
167  }
168  if (thread->buffer != NULL) {
169  MemBufferFree(thread->buffer);
170  }
171  SCFree(thread);
172  return TM_ECODE_OK;
173 }
174 
176 {
177  /* TEMPLATE_START_REMOVE */
178  if (ConfGetNode("app-layer.protocols.template-rust") == NULL) {
179  return;
180  }
181  /* TEMPLATE_END_REMOVE */
182  /* Register as an eve sub-module. */
184  "JsonTemplateRustLog", "eve-log.template-rust",
185  OutputTemplateLogInitSub, ALPROTO_TEMPLATE_RUST, JsonTemplateLogger,
186  JsonTemplateLogThreadInit, JsonTemplateLogThreadDeinit, NULL);
187 
188  SCLogNotice("Template JSON logger registered.");
189 }
190 
191 #else /* No JSON support. */
192 
194 {
195 }
196 
197 #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:197
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:399
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define SCFree(a)
Definition: util-mem.h:228
#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
#define OUTPUT_BUFFER_SIZE
Definition: log-httplog.c:58
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82