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 void EveSmtpDataLogger(const Flow *f, void *state, void *vtx, uint64_t tx_id, JsonBuilder *js)
56 {
57  SMTPTransaction *tx = vtx;
58  SMTPString *rcptto_str;
59  if (((SMTPState *)state)->helo) {
60  jb_set_string(js, "helo", (const char *)((SMTPState *)state)->helo);
61  }
62  if (tx->mail_from) {
63  jb_set_string(js, "mail_from", (const char *)tx->mail_from);
64  }
65  if (!TAILQ_EMPTY(&tx->rcpt_to_list)) {
66  jb_open_array(js, "rcpt_to");
67  TAILQ_FOREACH(rcptto_str, &tx->rcpt_to_list, next) {
68  jb_append_string(js, (char *)rcptto_str->str);
69  }
70  jb_close(js);
71  }
72 }
73 
74 static int JsonSmtpLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
75 {
76  SCEnter();
77  JsonEmailLogThread *jhl = (JsonEmailLogThread *)thread_data;
78 
79  JsonBuilder *jb = CreateEveHeaderWithTxId(p, LOG_DIR_FLOW, "smtp", NULL, tx_id);
80  if (unlikely(jb == NULL))
81  return TM_ECODE_OK;
82  EveAddCommonOptions(&jhl->emaillog_ctx->cfg, p, f, jb);
83 
84  /* reset */
85  MemBufferReset(jhl->buffer);
86 
87  jb_open_object(jb, "smtp");
88  EveSmtpDataLogger(f, state, tx, tx_id, jb);
89  jb_close(jb);
90 
91  if (EveEmailLogJson(jhl, jb, p, f, state, tx, tx_id) == TM_ECODE_OK) {
93  }
94 
95  jb_free(jb);
96 
98 
99 }
100 
101 bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
102 {
103  SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
104  if (smtp_state) {
105  SMTPTransaction *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_SMTP, smtp_state, tx_id);
106  if (tx) {
107  EveSmtpDataLogger(f, smtp_state, tx, tx_id, js);
108  return true;
109  }
110  }
111 
112  return false;
113 }
114 
115 static void OutputSmtpLogDeInitCtx(OutputCtx *output_ctx)
116 {
117  OutputJsonEmailCtx *email_ctx = output_ctx->data;
118  if (email_ctx != NULL) {
119  LogFileFreeCtx(email_ctx->file_ctx);
120  SCFree(email_ctx);
121  }
122  SCFree(output_ctx);
123 }
124 
125 static void OutputSmtpLogDeInitCtxSub(OutputCtx *output_ctx)
126 {
127  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
128  OutputJsonEmailCtx *email_ctx = output_ctx->data;
129  if (email_ctx != NULL) {
130  SCFree(email_ctx);
131  }
132  SCFree(output_ctx);
133 }
134 
135 #define DEFAULT_LOG_FILENAME "smtp.json"
136 static OutputInitResult OutputSmtpLogInit(ConfNode *conf)
137 {
138  OutputInitResult result = { NULL, false };
139  LogFileCtx *file_ctx = LogFileNewCtx();
140  if(file_ctx == NULL) {
141  SCLogError(SC_ERR_SMTP_LOG_GENERIC, "couldn't create new file_ctx");
142  return result;
143  }
144 
145  if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
146  LogFileFreeCtx(file_ctx);
147  return result;
148  }
149 
150  OutputJsonEmailCtx *email_ctx = SCMalloc(sizeof(OutputJsonEmailCtx));
151  if (unlikely(email_ctx == NULL)) {
152  LogFileFreeCtx(file_ctx);
153  return result;
154  }
155 
156  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
157  if (unlikely(output_ctx == NULL)) {
158  LogFileFreeCtx(file_ctx);
159  SCFree(email_ctx);
160  return result;
161  }
162 
163  email_ctx->file_ctx = file_ctx;
164 
165  output_ctx->data = email_ctx;
166  output_ctx->DeInit = OutputSmtpLogDeInitCtx;
167 
168  /* enable the logger for the app layer */
170 
171  result.ctx = output_ctx;
172  result.ok = true;
173  return result;
174 }
175 
176 static OutputInitResult OutputSmtpLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
177 {
178  OutputInitResult result = { NULL, false };
179  OutputJsonCtx *ojc = parent_ctx->data;
180 
181  OutputJsonEmailCtx *email_ctx = SCMalloc(sizeof(OutputJsonEmailCtx));
182  if (unlikely(email_ctx == NULL))
183  return result;
184 
185  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
186  if (unlikely(output_ctx == NULL)) {
187  SCFree(email_ctx);
188  return result;
189  }
190 
191  email_ctx->file_ctx = ojc->file_ctx;
192  email_ctx->cfg = ojc->cfg;
193 
194  OutputEmailInitConf(conf, email_ctx);
195 
196  output_ctx->data = email_ctx;
197  output_ctx->DeInit = OutputSmtpLogDeInitCtxSub;
198 
199  /* enable the logger for the app layer */
201 
202  result.ctx = output_ctx;
203  result.ok = true;
204  return result;
205 }
206 
207 static TmEcode JsonSmtpLogThreadInit(ThreadVars *t, const void *initdata, void **data)
208 {
210  if (unlikely(aft == NULL))
211  return TM_ECODE_FAILED;
212  memset(aft, 0, sizeof(JsonEmailLogThread));
213 
214  if(initdata == NULL)
215  {
216  SCLogDebug("Error getting context for EveLogSMTP. \"initdata\" argument NULL");
217  SCFree(aft);
218  return TM_ECODE_FAILED;
219  }
220 
221  /* Use the Ouptut Context (file pointer and mutex) */
222  aft->emaillog_ctx = ((OutputCtx *)initdata)->data;
223 
225  if (aft->buffer == NULL) {
226  SCFree(aft);
227  return TM_ECODE_FAILED;
228  }
229 
230  *data = (void *)aft;
231  return TM_ECODE_OK;
232 }
233 
234 static TmEcode JsonSmtpLogThreadDeinit(ThreadVars *t, void *data)
235 {
236  JsonEmailLogThread *aft = (JsonEmailLogThread *)data;
237  if (aft == NULL) {
238  return TM_ECODE_OK;
239  }
240 
241  MemBufferFree(aft->buffer);
242  /* clear memory */
243  memset(aft, 0, sizeof(JsonEmailLogThread));
244 
245  SCFree(aft);
246  return TM_ECODE_OK;
247 }
248 
249 void JsonSmtpLogRegister (void) {
250  /* register as separate module */
251  OutputRegisterTxModule(LOGGER_JSON_SMTP, "JsonSmtpLog", "smtp-json-log",
252  OutputSmtpLogInit, ALPROTO_SMTP, JsonSmtpLogger, JsonSmtpLogThreadInit,
253  JsonSmtpLogThreadDeinit, NULL);
254 
255  /* also register as child of eve-log */
256  OutputRegisterTxSubModule(LOGGER_JSON_SMTP, "eve-log", "JsonSmtpLog",
257  "eve-log.smtp", OutputSmtpLogInitSub, ALPROTO_SMTP, JsonSmtpLogger,
258  JsonSmtpLogThreadInit, JsonSmtpLogThreadDeinit, NULL);
259 }
tm-threads.h
SMTPState_
Definition: app-layer-smtp.h:109
EveSMTPAddMetadata
bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-smtp.c:101
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:111
EveEmailLogJson
TmEcode EveEmailLogJson(JsonEmailLogThread *aft, JsonBuilder *js, const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
Definition: output-json-email-common.c:364
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:298
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:62
threads.h
OutputJsonCtx_
Definition: output-json.h:108
Flow_
Flow data structure.
Definition: flow.h:343
LogFileCtx_
Definition: util-logopenfile.h:52
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:399
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:1429
util-privs.h
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:673
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
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1080
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
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:42
DEFAULT_LOG_FILENAME
#define DEFAULT_LOG_FILENAME
Definition: output-json-smtp.c:135
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:300
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
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
OutputEmailInitConf
void OutputEmailInitConf(ConfNode *conf, OutputJsonEmailCtx *email_ctx)
Definition: output-json-email-common.c:401
Packet_
Definition: decode.h:411
SMTPTransaction_
Definition: app-layer-smtp.h:67
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:1037
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:257
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
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:410
JsonSmtpLogRegister
void JsonSmtpLogRegister(void)
Definition: output-json-smtp.c:249
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:39
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
LOGGER_JSON_SMTP
@ LOGGER_JSON_SMTP
Definition: suricata-common.h:451
SCFree
#define SCFree(p)
Definition: util-mem.h:61
CreateEveHeaderWithTxId
JsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id)
Definition: output-json.c:1374
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:109
OutputJsonEmailCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-email-common.h:28
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
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
OutputJsonEmailCtx_
Definition: output-json-email-common.h:27
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h