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 #include "rust-applayertemplate-logger-gen.h"
57 
58 typedef struct LogTemplateFileCtx_ {
60  uint32_t flags;
62 
63 typedef struct LogTemplateLogThread_ {
65  uint32_t count;
68 
69 static int JsonTemplateLogger(ThreadVars *tv, void *thread_data,
70  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
71 {
72  SCLogNotice("JsonTemplateLogger");
73  LogTemplateLogThread *thread = thread_data;
74 
75  json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "template-rust");
76  if (unlikely(js == NULL)) {
77  return TM_ECODE_FAILED;
78  }
79 
80  json_t *template_js = rs_template_logger_log(tx);
81  if (unlikely(template_js == NULL)) {
82  goto error;
83  }
84  json_object_set_new(js, "template", template_js);
85 
86  MemBufferReset(thread->buffer);
87  OutputJSONBuffer(js, thread->templatelog_ctx->file_ctx, &thread->buffer);
88  json_decref(js);
89 
90  return TM_ECODE_OK;
91 
92 error:
93  json_decref(js);
94  return TM_ECODE_FAILED;
95 }
96 
97 static void OutputTemplateLogDeInitCtxSub(OutputCtx *output_ctx)
98 {
99  LogTemplateFileCtx *templatelog_ctx = (LogTemplateFileCtx *)output_ctx->data;
100  SCFree(templatelog_ctx);
101  SCFree(output_ctx);
102 }
103 
104 static OutputInitResult OutputTemplateLogInitSub(ConfNode *conf,
105  OutputCtx *parent_ctx)
106 {
107  OutputInitResult result = { NULL, false };
108  OutputJsonCtx *ajt = parent_ctx->data;
109 
110  LogTemplateFileCtx *templatelog_ctx = SCCalloc(1, sizeof(*templatelog_ctx));
111  if (unlikely(templatelog_ctx == NULL)) {
112  return result;
113  }
114  templatelog_ctx->file_ctx = ajt->file_ctx;
115 
116  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
117  if (unlikely(output_ctx == NULL)) {
118  SCFree(templatelog_ctx);
119  return result;
120  }
121  output_ctx->data = templatelog_ctx;
122  output_ctx->DeInit = OutputTemplateLogDeInitCtxSub;
123 
124  SCLogNotice("Template log sub-module initialized.");
125 
127 
128  result.ctx = output_ctx;
129  result.ok = true;
130  return result;
131 }
132 
133 static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, const void *initdata, void **data)
134 {
135  LogTemplateLogThread *thread = SCCalloc(1, sizeof(*thread));
136  if (unlikely(thread == NULL)) {
137  return TM_ECODE_FAILED;
138  }
139 
140  if (initdata == NULL) {
141  SCLogDebug("Error getting context for EveLogTemplate. \"initdata\" is NULL.");
142  SCFree(thread);
143  return TM_ECODE_FAILED;
144  }
145 
147  if (unlikely(thread->buffer == NULL)) {
148  SCFree(thread);
149  return TM_ECODE_FAILED;
150  }
151 
152  thread->templatelog_ctx = ((OutputCtx *)initdata)->data;
153  *data = (void *)thread;
154 
155  return TM_ECODE_OK;
156 }
157 
158 static TmEcode JsonTemplateLogThreadDeinit(ThreadVars *t, void *data)
159 {
160  LogTemplateLogThread *thread = (LogTemplateLogThread *)data;
161  if (thread == NULL) {
162  return TM_ECODE_OK;
163  }
164  if (thread->buffer != NULL) {
165  MemBufferFree(thread->buffer);
166  }
167  SCFree(thread);
168  return TM_ECODE_OK;
169 }
170 
172 {
173  /* TEMPLATE_START_REMOVE */
174  if (ConfGetNode("app-layer.protocols.template-rust") == NULL) {
175  return;
176  }
177  /* TEMPLATE_END_REMOVE */
178  /* Register as an eve sub-module. */
180  "JsonTemplateRustLog", "eve-log.template-rust",
181  OutputTemplateLogInitSub, ALPROTO_TEMPLATE_RUST, JsonTemplateLogger,
182  JsonTemplateLogThreadInit, JsonTemplateLogThreadDeinit, NULL);
183 
184  SCLogNotice("Template JSON logger registered.");
185 }
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
#define SCLogDebug(...)
Definition: util-debug.h:335
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
struct LogTemplateLogThread_ LogTemplateLogThread
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)
LogFileCtx * file_ctx
Definition: output-json.h:79
struct LogTemplateFileCtx_ LogTemplateFileCtx
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:402
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
LogTemplateFileCtx * templatelog_ctx
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82