suricata
output-json-smtp.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2015 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 Tom DeCanio <td@npulsetech.com>
22  *
23  * Implements SMTP JSON logging portion of the engine.
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-print.h"
37 #include "util-unittest.h"
38 
39 #include "util-debug.h"
40 
41 #include "output.h"
42 #include "app-layer-smtp.h"
43 #include "app-layer.h"
44 #include "app-layer-parser.h"
45 #include "util-privs.h"
46 #include "util-buffer.h"
47 #include "util-proto-name.h"
48 #include "util-logopenfile.h"
49 #include "util-time.h"
50 
51 #include "output-json.h"
52 #include "output-json-smtp.h"
54 
55 static json_t *JsonSmtpDataLogger(const Flow *f, void *state, void *vtx, uint64_t tx_id)
56 {
57  json_t *sjs = json_object();
58  SMTPTransaction *tx = vtx;
59  SMTPString *rcptto_str;
60  if (sjs == NULL) {
61  return NULL;
62  }
63  if (((SMTPState *)state)->helo) {
64  json_object_set_new(sjs, "helo",
65  SCJsonString((const char *)((SMTPState *)state)->helo));
66  }
67  if (tx->mail_from) {
68  json_object_set_new(sjs, "mail_from",
69  SCJsonString((const char *)tx->mail_from));
70  }
71  if (!TAILQ_EMPTY(&tx->rcpt_to_list)) {
72  json_t *js_rcptto = json_array();
73  if (likely(js_rcptto != NULL)) {
74  TAILQ_FOREACH(rcptto_str, &tx->rcpt_to_list, next) {
75  json_array_append_new(js_rcptto, SCJsonString((char *)rcptto_str->str));
76  }
77  json_object_set_new(sjs, "rcpt_to", js_rcptto);
78  }
79  }
80 
81  return sjs;
82 }
83 
84 static int JsonSmtpLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
85 {
86  SCEnter();
87  JsonEmailLogThread *jhl = (JsonEmailLogThread *)thread_data;
88 
89  json_t *js = CreateJSONHeaderWithTxId(p, LOG_DIR_FLOW, "smtp", tx_id);
90  if (unlikely(js == NULL))
91  return TM_ECODE_OK;
92 
93  /* reset */
94  MemBufferReset(jhl->buffer);
95 
96  JsonAddCommonOptions(&jhl->emaillog_ctx->cfg, p, f, js);
97 
98  json_t *sjs = JsonSmtpDataLogger(f, state, tx, tx_id);
99  if (sjs) {
100  json_object_set_new(js, "smtp", sjs);
101  }
102 
103  if (JsonEmailLogJson(jhl, js, p, f, state, tx, tx_id) == TM_ECODE_OK) {
104  OutputJSONBuffer(js, jhl->emaillog_ctx->file_ctx, &jhl->buffer);
105  }
106  json_object_del(js, "email");
107  if (sjs) {
108  json_object_del(js, "smtp");
109  }
110 
111  json_object_clear(js);
112  json_decref(js);
113 
115 
116 }
117 
118 json_t *JsonSMTPAddMetadata(const Flow *f, uint64_t tx_id)
119 {
120  SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
121  if (smtp_state) {
122  SMTPTransaction *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_SMTP, smtp_state, tx_id);
123 
124  if (tx) {
125  return JsonSmtpDataLogger(f, smtp_state, tx, tx_id);
126  }
127  }
128 
129  return NULL;
130 }
131 
132 static void OutputSmtpLogDeInitCtx(OutputCtx *output_ctx)
133 {
134  OutputJsonEmailCtx *email_ctx = output_ctx->data;
135  if (email_ctx != NULL) {
136  LogFileFreeCtx(email_ctx->file_ctx);
137  SCFree(email_ctx);
138  }
139  SCFree(output_ctx);
140 }
141 
142 static void OutputSmtpLogDeInitCtxSub(OutputCtx *output_ctx)
143 {
144  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
145  OutputJsonEmailCtx *email_ctx = output_ctx->data;
146  if (email_ctx != NULL) {
147  SCFree(email_ctx);
148  }
149  SCFree(output_ctx);
150 }
151 
152 #define DEFAULT_LOG_FILENAME "smtp.json"
153 static OutputInitResult OutputSmtpLogInit(ConfNode *conf)
154 {
155  OutputInitResult result = { NULL, false };
156  LogFileCtx *file_ctx = LogFileNewCtx();
157  if(file_ctx == NULL) {
158  SCLogError(SC_ERR_SMTP_LOG_GENERIC, "couldn't create new file_ctx");
159  return result;
160  }
161 
162  if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
163  LogFileFreeCtx(file_ctx);
164  return result;
165  }
166 
167  OutputJsonEmailCtx *email_ctx = SCMalloc(sizeof(OutputJsonEmailCtx));
168  if (unlikely(email_ctx == NULL)) {
169  LogFileFreeCtx(file_ctx);
170  return result;
171  }
172 
173  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
174  if (unlikely(output_ctx == NULL)) {
175  LogFileFreeCtx(file_ctx);
176  SCFree(email_ctx);
177  return result;
178  }
179 
180  email_ctx->file_ctx = file_ctx;
181 
182  output_ctx->data = email_ctx;
183  output_ctx->DeInit = OutputSmtpLogDeInitCtx;
184 
185  /* enable the logger for the app layer */
187 
188  result.ctx = output_ctx;
189  result.ok = true;
190  return result;
191 }
192 
193 static OutputInitResult OutputSmtpLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
194 {
195  OutputInitResult result = { NULL, false };
196  OutputJsonCtx *ojc = parent_ctx->data;
197 
198  OutputJsonEmailCtx *email_ctx = SCMalloc(sizeof(OutputJsonEmailCtx));
199  if (unlikely(email_ctx == NULL))
200  return result;
201 
202  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
203  if (unlikely(output_ctx == NULL)) {
204  SCFree(email_ctx);
205  return result;
206  }
207 
208  email_ctx->file_ctx = ojc->file_ctx;
209  email_ctx->cfg = ojc->cfg;
210 
211  OutputEmailInitConf(conf, email_ctx);
212 
213  output_ctx->data = email_ctx;
214  output_ctx->DeInit = OutputSmtpLogDeInitCtxSub;
215 
216  /* enable the logger for the app layer */
218 
219  result.ctx = output_ctx;
220  result.ok = true;
221  return result;
222 }
223 
224 static TmEcode JsonSmtpLogThreadInit(ThreadVars *t, const void *initdata, void **data)
225 {
227  if (unlikely(aft == NULL))
228  return TM_ECODE_FAILED;
229  memset(aft, 0, sizeof(JsonEmailLogThread));
230 
231  if(initdata == NULL)
232  {
233  SCLogDebug("Error getting context for EveLogSMTP. \"initdata\" argument NULL");
234  SCFree(aft);
235  return TM_ECODE_FAILED;
236  }
237 
238  /* Use the Ouptut Context (file pointer and mutex) */
239  aft->emaillog_ctx = ((OutputCtx *)initdata)->data;
240 
242  if (aft->buffer == NULL) {
243  SCFree(aft);
244  return TM_ECODE_FAILED;
245  }
246 
247  *data = (void *)aft;
248  return TM_ECODE_OK;
249 }
250 
251 static TmEcode JsonSmtpLogThreadDeinit(ThreadVars *t, void *data)
252 {
253  JsonEmailLogThread *aft = (JsonEmailLogThread *)data;
254  if (aft == NULL) {
255  return TM_ECODE_OK;
256  }
257 
258  MemBufferFree(aft->buffer);
259  /* clear memory */
260  memset(aft, 0, sizeof(JsonEmailLogThread));
261 
262  SCFree(aft);
263  return TM_ECODE_OK;
264 }
265 
266 void JsonSmtpLogRegister (void) {
267  /* register as separate module */
268  OutputRegisterTxModule(LOGGER_JSON_SMTP, "JsonSmtpLog", "smtp-json-log",
269  OutputSmtpLogInit, ALPROTO_SMTP, JsonSmtpLogger, JsonSmtpLogThreadInit,
270  JsonSmtpLogThreadDeinit, NULL);
271 
272  /* also register as child of eve-log */
273  OutputRegisterTxSubModule(LOGGER_JSON_SMTP, "eve-log", "JsonSmtpLog",
274  "eve-log.smtp", OutputSmtpLogInitSub, ALPROTO_SMTP, JsonSmtpLogger,
275  JsonSmtpLogThreadInit, JsonSmtpLogThreadDeinit, NULL);
276 }
tm-threads.h
SMTPState_
Definition: app-layer-smtp.h:109
OutputJsonEmailCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-email-common.h:31
JsonEmailLogThread_
Definition: output-json-email-common.h:34
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:81
SCFree
#define SCFree(a)
Definition: util-mem.h:322
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
LogFileNewCtx
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
Definition: util-logopenfile.c:517
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:335
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:2
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
threads.h
OutputJsonCtx_
Definition: output-json.h:78
Flow_
Flow data structure.
Definition: flow.h:340
LogFileCtx_
Definition: util-logopenfile.h:52
JsonSMTPAddMetadata
json_t * JsonSMTPAddMetadata(const Flow *f, uint64_t tx_id)
Definition: output-json-smtp.c:118
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:347
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
OutputRegisterTxModule
void OutputRegisterTxModule(LoggerId id, const char *name, const char *conf_name, OutputInitFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Register a tx output module.
Definition: output.c:400
util-privs.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
SC_ERR_SMTP_LOG_GENERIC
@ SC_ERR_SMTP_LOG_GENERIC
Definition: util-error.h:315
util-unittest.h
SCJsonString
json_t * SCJsonString(const char *val)
Definition: output-json.c:107
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1082
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
OutputCtx_
Definition: tm-modules.h:78
SCConfLogOpenGeneric
int SCConfLogOpenGeneric(ConfNode *conf, LogFileCtx *log_ctx, const char *default_filename, int rotate)
open a generic output "log file", which may be a regular file or a socket
Definition: util-logopenfile.c:305
JsonEmailLogJson
TmEcode JsonEmailLogJson(JsonEmailLogThread *aft, json_t *js, const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
Definition: output-json-email-common.c:374
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:42
DEFAULT_LOG_FILENAME
#define DEFAULT_LOG_FILENAME
Definition: output-json-smtp.c:152
output-json.h
ALPROTO_SMTP
@ ALPROTO_SMTP
Definition: app-layer-protos.h:32
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:337
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
SMTPTransaction_::mail_from
uint8_t * mail_from
Definition: app-layer-smtp.h:88
output-json-email-common.h
util-time.h
OutputInitResult_::ok
bool ok
Definition: output.h:43
app-layer-parser.h
SCMalloc
#define SCMalloc(a)
Definition: util-mem.h:222
OutputEmailInitConf
void OutputEmailInitConf(ConfNode *conf, OutputJsonEmailCtx *email_ctx)
Definition: output-json-email-common.c:413
Packet_
Definition: decode.h:408
SMTPTransaction_
Definition: app-layer-smtp.h:67
SCCalloc
#define SCCalloc(nm, a)
Definition: util-mem.h:253
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:77
util-proto-name.h
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1047
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:41
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
SMTPString_
Definition: app-layer-smtp.h:60
LogFileFreeCtx
int LogFileFreeCtx(LogFileCtx *lf_ctx)
LogFileFreeCtx() Destroy a LogFileCtx (Close the file and free memory)
Definition: util-logopenfile.c:539
JsonEmailLogThread_::buffer
MemBuffer * buffer
Definition: output-json-email-common.h:36
JsonAddCommonOptions
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:390
OutputRegisterTxSubModule
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:411
JsonSmtpLogRegister
void JsonSmtpLogRegister(void)
Definition: output-json-smtp.c:266
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:38
LOGGER_JSON_SMTP
@ LOGGER_JSON_SMTP
Definition: suricata-common.h:428
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:79
OutputJSONBuffer
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
OutputJsonEmailCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-email-common.h:28
likely
#define likely(expr)
Definition: util-optimize.h:32
SMTPString_::str
uint8_t * str
Definition: app-layer-smtp.h:61
app-layer-smtp.h
JsonEmailLogThread_::emaillog_ctx
OutputJsonEmailCtx * emaillog_ctx
Definition: output-json-email-common.h:35
output-json-smtp.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:341
OutputJsonEmailCtx_
Definition: output-json-email-common.h:27
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
CreateJSONHeaderWithTxId
json_t * CreateJSONHeaderWithTxId(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, uint64_t tx_id)
Definition: output-json.c:783
app-layer.h