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 #include "rust.h"
52 #include "rust-tftp-log-gen.h"
53 
54 typedef struct LogTFTPFileCtx_ {
56  uint32_t flags;
58 
59 typedef struct LogTFTPLogThread_ {
61  uint32_t count;
64 
65 static int JsonTFTPLogger(ThreadVars *tv, void *thread_data,
66  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
67 {
68  LogTFTPLogThread *thread = thread_data;
69 
70  json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "tftp");
71  if (unlikely(js == NULL)) {
72  return TM_ECODE_FAILED;
73  }
74 
75  json_t *tftpjs = rs_tftp_log_json_request(tx);
76  if (unlikely(tftpjs == NULL)) {
77  goto error;
78  }
79 
80  json_object_set_new(js, "tftp", tftpjs);
81 
82  MemBufferReset(thread->buffer);
83  OutputJSONBuffer(js, thread->tftplog_ctx->file_ctx, &thread->buffer);
84 
85  json_decref(js);
86  return TM_ECODE_OK;
87 
88 error:
89  json_decref(js);
90  return TM_ECODE_FAILED;
91 }
92 
93 static void OutputTFTPLogDeInitCtxSub(OutputCtx *output_ctx)
94 {
95  LogTFTPFileCtx *tftplog_ctx = (LogTFTPFileCtx *)output_ctx->data;
96  SCFree(tftplog_ctx);
97  SCFree(output_ctx);
98 }
99 
100 static OutputInitResult OutputTFTPLogInitSub(ConfNode *conf,
101  OutputCtx *parent_ctx)
102 {
103  OutputInitResult result = { NULL, false };
104  OutputJsonCtx *ajt = parent_ctx->data;
105 
106  LogTFTPFileCtx *tftplog_ctx = SCCalloc(1, sizeof(*tftplog_ctx));
107  if (unlikely(tftplog_ctx == NULL)) {
108  return result;
109  }
110  tftplog_ctx->file_ctx = ajt->file_ctx;
111 
112  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
113  if (unlikely(output_ctx == NULL)) {
114  SCFree(tftplog_ctx);
115  return result;
116  }
117  output_ctx->data = tftplog_ctx;
118  output_ctx->DeInit = OutputTFTPLogDeInitCtxSub;
119 
120  SCLogDebug("TFTP log sub-module initialized.");
121 
123 
124  result.ctx = output_ctx;
125  result.ok = true;
126  return result;
127 }
128 
129 static TmEcode JsonTFTPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
130 {
131  LogTFTPLogThread *thread = SCCalloc(1, sizeof(*thread));
132  if (unlikely(thread == NULL)) {
133  return TM_ECODE_FAILED;
134  }
135 
136  if (initdata == NULL) {
137  SCLogDebug("Error getting context for EveLogTFTP. \"initdata\" is NULL.");
138  SCFree(thread);
139  return TM_ECODE_FAILED;
140  }
141 
143  if (unlikely(thread->buffer == NULL)) {
144  SCFree(thread);
145  return TM_ECODE_FAILED;
146  }
147 
148  thread->tftplog_ctx = ((OutputCtx *)initdata)->data;
149  *data = (void *)thread;
150 
151  return TM_ECODE_OK;
152 }
153 
154 static TmEcode JsonTFTPLogThreadDeinit(ThreadVars *t, void *data)
155 {
156  LogTFTPLogThread *thread = (LogTFTPLogThread *)data;
157  if (thread == NULL) {
158  return TM_ECODE_OK;
159  }
160  if (thread->buffer != NULL) {
161  MemBufferFree(thread->buffer);
162  }
163  SCFree(thread);
164  return TM_ECODE_OK;
165 }
166 
168 {
169  /* Register as an eve sub-module. */
170  OutputRegisterTxSubModule(LOGGER_JSON_TFTP, "eve-log", "JsonTFTPLog",
171  "eve-log.tftp", OutputTFTPLogInitSub,
172  ALPROTO_TFTP, JsonTFTPLogger,
173  JsonTFTPLogThreadInit, JsonTFTPLogThreadDeinit,
174  NULL);
175 
176  SCLogDebug("TFTP JSON logger registered.");
177 }
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
#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
LogFileCtx * file_ctx
#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 LogTFTPLogThread_ LogTFTPLogThread
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
uint16_t tx_id
struct LogTFTPFileCtx_ LogTFTPFileCtx
void * data
Definition: tm-modules.h:81
LogTFTPFileCtx * tftplog_ctx
void JsonTFTPLogRegister(void)
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82