suricata
output-json-snmp.c
Go to the documentation of this file.
1 /* Copyright (C) 2018-2019 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  * \file
20  *
21  * \author Pierre Chifflier <chifflier@wzdftpd.net>
22  *
23  * Implement JSON/eve logging app-layer SNMP.
24  */
25 
26 #include "suricata-common.h"
27 #include "debug.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "threadvars.h"
34 #include "tm-threads.h"
35 
36 #include "util-unittest.h"
37 #include "util-buffer.h"
38 #include "util-debug.h"
39 #include "util-byte.h"
40 
41 #include "output.h"
42 #include "output-json.h"
43 
44 #include "app-layer.h"
45 #include "app-layer-parser.h"
46 
47 #include "app-layer-snmp.h"
48 #include "output-json-snmp.h"
49 
50 #ifdef HAVE_RUST
51 #ifdef HAVE_LIBJANSSON
52 
53 #include "rust.h"
54 #include "rust-snmp-log-gen.h"
55 
56 typedef struct LogSNMPFileCtx_ {
57  LogFileCtx *file_ctx;
58  OutputJsonCommonSettings cfg;
59 } LogSNMPFileCtx;
60 
61 typedef struct LogSNMPLogThread_ {
62  LogSNMPFileCtx *snmplog_ctx;
63  MemBuffer *buffer;
64 } LogSNMPLogThread;
65 
66 static int JsonSNMPLogger(ThreadVars *tv, void *thread_data,
67  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
68 {
69  SNMPTransaction *snmptx = tx;
70  LogSNMPLogThread *thread = thread_data;
71  json_t *js, *snmpjs;
72 
73  js = CreateJSONHeader(p, LOG_DIR_PACKET, "snmp");
74  if (unlikely(js == NULL)) {
75  return TM_ECODE_FAILED;
76  }
77 
78  JsonAddCommonOptions(&thread->snmplog_ctx->cfg, p, f, js);
79 
80  snmpjs = rs_snmp_log_json_response(state, snmptx);
81  if (unlikely(snmpjs == NULL)) {
82  goto error;
83  }
84  json_object_set_new(js, "snmp", snmpjs);
85 
86  MemBufferReset(thread->buffer);
87  OutputJSONBuffer(js, thread->snmplog_ctx->file_ctx, &thread->buffer);
88 
89  json_decref(js);
90  return TM_ECODE_OK;
91 
92 error:
93  json_decref(js);
94  return TM_ECODE_FAILED;
95 }
96 
97 static void OutputSNMPLogDeInitCtxSub(OutputCtx *output_ctx)
98 {
99  LogSNMPFileCtx *snmplog_ctx = (LogSNMPFileCtx *)output_ctx->data;
100  SCFree(snmplog_ctx);
101  SCFree(output_ctx);
102 }
103 
104 static OutputInitResult OutputSNMPLogInitSub(ConfNode *conf,
105  OutputCtx *parent_ctx)
106 {
107  OutputInitResult result = { NULL, false };
108  OutputJsonCtx *ajt = parent_ctx->data;
109 
110  LogSNMPFileCtx *snmplog_ctx = SCCalloc(1, sizeof(*snmplog_ctx));
111  if (unlikely(snmplog_ctx == NULL)) {
112  return result;
113  }
114  snmplog_ctx->file_ctx = ajt->file_ctx;
115  snmplog_ctx->cfg = ajt->cfg;
116 
117  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
118  if (unlikely(output_ctx == NULL)) {
119  SCFree(snmplog_ctx);
120  return result;
121  }
122  output_ctx->data = snmplog_ctx;
123  output_ctx->DeInit = OutputSNMPLogDeInitCtxSub;
124 
125  SCLogDebug("SNMP log sub-module initialized.");
126 
128 
129  result.ctx = output_ctx;
130  result.ok = true;
131  return result;
132 }
133 
134 #define OUTPUT_BUFFER_SIZE 65535
135 
136 static TmEcode JsonSNMPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
137 {
138  LogSNMPLogThread *thread = SCCalloc(1, sizeof(*thread));
139  if (unlikely(thread == NULL)) {
140  return TM_ECODE_FAILED;
141  }
142 
143  if (initdata == NULL) {
144  SCLogDebug("Error getting context for EveLogSNMP. \"initdata\" is NULL.");
145  SCFree(thread);
146  return TM_ECODE_FAILED;
147  }
148 
149  thread->buffer = MemBufferCreateNew(OUTPUT_BUFFER_SIZE);
150  if (unlikely(thread->buffer == NULL)) {
151  SCFree(thread);
152  return TM_ECODE_FAILED;
153  }
154 
155  thread->snmplog_ctx = ((OutputCtx *)initdata)->data;
156  *data = (void *)thread;
157 
158  return TM_ECODE_OK;
159 }
160 
161 static TmEcode JsonSNMPLogThreadDeinit(ThreadVars *t, void *data)
162 {
163  LogSNMPLogThread *thread = (LogSNMPLogThread *)data;
164  if (thread == NULL) {
165  return TM_ECODE_OK;
166  }
167  if (thread->buffer != NULL) {
168  MemBufferFree(thread->buffer);
169  }
170  SCFree(thread);
171  return TM_ECODE_OK;
172 }
173 
174 void JsonSNMPLogRegister(void)
175 {
176  /* Register as an eve sub-module. */
177  OutputRegisterTxSubModule(LOGGER_JSON_SNMP, "eve-log", "JsonSNMPLog",
178  "eve-log.snmp", OutputSNMPLogInitSub, ALPROTO_SNMP,
179  JsonSNMPLogger, JsonSNMPLogThreadInit,
180  JsonSNMPLogThreadDeinit, NULL);
181 
182  SCLogDebug("SNMP JSON logger registered.");
183 }
184 
185 #else /* No JSON support. */
186 
187 void JsonSNMPLogRegister(void)
188 {
189 }
190 
191 #endif /* HAVE_LIBJANSSON */
192 #else /* No rust support. */
193 
195 {
196 }
197 
198 #endif /* HAVE_RUST */
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define SCLogDebug(...)
Definition: util-debug.h:335
#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
uint16_t tx_id
void JsonSNMPLogRegister(void)
void * data
Definition: tm-modules.h:81
Per thread variable structure.
Definition: threadvars.h:57
#define OUTPUT_BUFFER_SIZE
Definition: log-httplog.c:58
Flow data structure.
Definition: flow.h:325
struct SNMPTransaction_ SNMPTransaction
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82