suricata
output-json-tftp.c
Go to the documentation of this file.
1 /* Copyright (C) 2017 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 ClĂ©ment Galland <clement.galland@epita.fr>
22  *
23  * Implement JSON/eve logging app-layer TFTP.
24  */
25 
26 
27 #include "suricata-common.h"
28 #include "debug.h"
29 #include "detect.h"
30 #include "pkt-var.h"
31 #include "conf.h"
32 
33 #include "threads.h"
34 #include "threadvars.h"
35 #include "tm-threads.h"
36 
37 #include "util-unittest.h"
38 #include "util-buffer.h"
39 #include "util-debug.h"
40 #include "util-byte.h"
41 
42 #include "output.h"
43 #include "output-json.h"
44 
45 #include "app-layer.h"
46 #include "app-layer-parser.h"
47 
48 #include "app-layer-tftp.h"
49 #include "output-json-tftp.h"
50 
51 #ifdef HAVE_RUST
52 
53 #include "rust.h"
54 #include "rust-tftp-log-gen.h"
55 
56 #ifdef HAVE_LIBJANSSON
57 
58 typedef struct LogTFTPFileCtx_ {
59  LogFileCtx *file_ctx;
60  uint32_t flags;
61 } LogTFTPFileCtx;
62 
63 typedef struct LogTFTPLogThread_ {
64  LogTFTPFileCtx *tftplog_ctx;
65  uint32_t count;
66  MemBuffer *buffer;
67 } LogTFTPLogThread;
68 
69 static int JsonTFTPLogger(ThreadVars *tv, void *thread_data,
70  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
71 {
72  LogTFTPLogThread *thread = thread_data;
73 
74  json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "tftp");
75  if (unlikely(js == NULL)) {
76  return TM_ECODE_FAILED;
77  }
78 
79  json_t *tftpjs = rs_tftp_log_json_request(tx);
80  if (unlikely(tftpjs == NULL)) {
81  goto error;
82  }
83 
84  json_object_set_new(js, "tftp", tftpjs);
85 
86  MemBufferReset(thread->buffer);
87  OutputJSONBuffer(js, thread->tftplog_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 OutputTFTPLogDeInitCtxSub(OutputCtx *output_ctx)
98 {
99  LogTFTPFileCtx *tftplog_ctx = (LogTFTPFileCtx *)output_ctx->data;
100  SCFree(tftplog_ctx);
101  SCFree(output_ctx);
102 }
103 
104 static OutputInitResult OutputTFTPLogInitSub(ConfNode *conf,
105  OutputCtx *parent_ctx)
106 {
107  OutputInitResult result = { NULL, false };
108  OutputJsonCtx *ajt = parent_ctx->data;
109 
110  LogTFTPFileCtx *tftplog_ctx = SCCalloc(1, sizeof(*tftplog_ctx));
111  if (unlikely(tftplog_ctx == NULL)) {
112  return result;
113  }
114  tftplog_ctx->file_ctx = ajt->file_ctx;
115 
116  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
117  if (unlikely(output_ctx == NULL)) {
118  SCFree(tftplog_ctx);
119  return result;
120  }
121  output_ctx->data = tftplog_ctx;
122  output_ctx->DeInit = OutputTFTPLogDeInitCtxSub;
123 
124  SCLogDebug("TFTP log sub-module initialized.");
125 
127 
128  result.ctx = output_ctx;
129  result.ok = true;
130  return result;
131 }
132 
133 #define OUTPUT_BUFFER_SIZE 65535
134 
135 static TmEcode JsonTFTPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
136 {
137  LogTFTPLogThread *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 EveLogTFTP. \"initdata\" is NULL.");
144  SCFree(thread);
145  return TM_ECODE_FAILED;
146  }
147 
148  thread->buffer = MemBufferCreateNew(OUTPUT_BUFFER_SIZE);
149  if (unlikely(thread->buffer == NULL)) {
150  SCFree(thread);
151  return TM_ECODE_FAILED;
152  }
153 
154  thread->tftplog_ctx = ((OutputCtx *)initdata)->data;
155  *data = (void *)thread;
156 
157  return TM_ECODE_OK;
158 }
159 
160 static TmEcode JsonTFTPLogThreadDeinit(ThreadVars *t, void *data)
161 {
162  LogTFTPLogThread *thread = (LogTFTPLogThread *)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 
173 void JsonTFTPLogRegister(void)
174 {
175  /* Register as an eve sub-module. */
176  OutputRegisterTxSubModule(LOGGER_JSON_TFTP, "eve-log", "JsonTFTPLog",
177  "eve-log.tftp", OutputTFTPLogInitSub,
178  ALPROTO_TFTP, JsonTFTPLogger,
179  JsonTFTPLogThreadInit, JsonTFTPLogThreadDeinit,
180  NULL);
181 
182  SCLogDebug("TFTP JSON logger registered.");
183 }
184 #else /* HAVE_RUST */
185 
186 void JsonTFTPLogRegister(void)
187 {
188 }
189 
190 #endif /* HAVE_RUST */
191 
192 #else /* HAVE_LIBJANSSON */
193 
195 {
196 }
197 
198 #endif /* HAVE_LIBJANSSON */
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
uint16_t flags
#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:205
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:236
#define OUTPUT_BUFFER_SIZE
Definition: log-dnslog.c:58
uint16_t tx_id
void * data
Definition: tm-modules.h:81
void JsonTFTPLogRegister(void)
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:327
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82