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 #ifdef HAVE_LIBJANSSON
56 
57 static json_t *JsonSmtpDataLogger(const Flow *f, void *state, void *vtx, uint64_t tx_id)
58 {
59  json_t *sjs = json_object();
60  SMTPTransaction *tx = vtx;
61  SMTPString *rcptto_str;
62  if (sjs == NULL) {
63  return NULL;
64  }
65  if (((SMTPState *)state)->helo) {
66  json_object_set_new(sjs, "helo",
67  SCJsonString((const char *)((SMTPState *)state)->helo));
68  }
69  if (tx->mail_from) {
70  json_object_set_new(sjs, "mail_from",
71  SCJsonString((const char *)tx->mail_from));
72  }
73  if (!TAILQ_EMPTY(&tx->rcpt_to_list)) {
74  json_t *js_rcptto = json_array();
75  if (likely(js_rcptto != NULL)) {
76  TAILQ_FOREACH(rcptto_str, &tx->rcpt_to_list, next) {
77  json_array_append_new(js_rcptto, SCJsonString((char *)rcptto_str->str));
78  }
79  json_object_set_new(sjs, "rcpt_to", js_rcptto);
80  }
81  }
82 
83  return sjs;
84 }
85 
86 static int JsonSmtpLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
87 {
88  SCEnter();
89  JsonEmailLogThread *jhl = (JsonEmailLogThread *)thread_data;
90 
91  json_t *js = CreateJSONHeaderWithTxId(p, LOG_DIR_FLOW, "smtp", tx_id);
92  if (unlikely(js == NULL))
93  return TM_ECODE_OK;
94 
95  /* reset */
96  MemBufferReset(jhl->buffer);
97 
98  JsonAddCommonOptions(&jhl->emaillog_ctx->cfg, p, f, js);
99 
100  json_t *sjs = JsonSmtpDataLogger(f, state, tx, tx_id);
101  if (sjs) {
102  json_object_set_new(js, "smtp", sjs);
103  }
104 
105  if (JsonEmailLogJson(jhl, js, p, f, state, tx, tx_id) == TM_ECODE_OK) {
106  OutputJSONBuffer(js, jhl->emaillog_ctx->file_ctx, &jhl->buffer);
107  }
108  json_object_del(js, "email");
109  if (sjs) {
110  json_object_del(js, "smtp");
111  }
112 
113  json_object_clear(js);
114  json_decref(js);
115 
117 
118 }
119 
120 json_t *JsonSMTPAddMetadata(const Flow *f, uint64_t tx_id)
121 {
122  SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
123  if (smtp_state) {
124  SMTPTransaction *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_SMTP, smtp_state, tx_id);
125 
126  if (tx) {
127  return JsonSmtpDataLogger(f, smtp_state, tx, tx_id);
128  }
129  }
130 
131  return NULL;
132 }
133 
134 static void OutputSmtpLogDeInitCtx(OutputCtx *output_ctx)
135 {
136  OutputJsonEmailCtx *email_ctx = output_ctx->data;
137  if (email_ctx != NULL) {
138  LogFileFreeCtx(email_ctx->file_ctx);
139  SCFree(email_ctx);
140  }
141  SCFree(output_ctx);
142 }
143 
144 static void OutputSmtpLogDeInitCtxSub(OutputCtx *output_ctx)
145 {
146  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
147  OutputJsonEmailCtx *email_ctx = output_ctx->data;
148  if (email_ctx != NULL) {
149  SCFree(email_ctx);
150  }
151  SCFree(output_ctx);
152 }
153 
154 #define DEFAULT_LOG_FILENAME "smtp.json"
155 static OutputInitResult OutputSmtpLogInit(ConfNode *conf)
156 {
157  OutputInitResult result = { NULL, false };
158  LogFileCtx *file_ctx = LogFileNewCtx();
159  if(file_ctx == NULL) {
160  SCLogError(SC_ERR_SMTP_LOG_GENERIC, "couldn't create new file_ctx");
161  return result;
162  }
163 
164  if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
165  LogFileFreeCtx(file_ctx);
166  return result;
167  }
168 
169  OutputJsonEmailCtx *email_ctx = SCMalloc(sizeof(OutputJsonEmailCtx));
170  if (unlikely(email_ctx == NULL)) {
171  LogFileFreeCtx(file_ctx);
172  return result;
173  }
174 
175  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
176  if (unlikely(output_ctx == NULL)) {
177  LogFileFreeCtx(file_ctx);
178  SCFree(email_ctx);
179  return result;
180  }
181 
182  email_ctx->file_ctx = file_ctx;
183 
184  output_ctx->data = email_ctx;
185  output_ctx->DeInit = OutputSmtpLogDeInitCtx;
186 
187  /* enable the logger for the app layer */
189 
190  result.ctx = output_ctx;
191  result.ok = true;
192  return result;
193 }
194 
195 static OutputInitResult OutputSmtpLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
196 {
197  OutputInitResult result = { NULL, false };
198  OutputJsonCtx *ojc = parent_ctx->data;
199 
200  OutputJsonEmailCtx *email_ctx = SCMalloc(sizeof(OutputJsonEmailCtx));
201  if (unlikely(email_ctx == NULL))
202  return result;
203 
204  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
205  if (unlikely(output_ctx == NULL)) {
206  SCFree(email_ctx);
207  return result;
208  }
209 
210  email_ctx->file_ctx = ojc->file_ctx;
211  email_ctx->cfg = ojc->cfg;
212 
213  OutputEmailInitConf(conf, email_ctx);
214 
215  output_ctx->data = email_ctx;
216  output_ctx->DeInit = OutputSmtpLogDeInitCtxSub;
217 
218  /* enable the logger for the app layer */
220 
221  result.ctx = output_ctx;
222  result.ok = true;
223  return result;
224 }
225 
226 static TmEcode JsonSmtpLogThreadInit(ThreadVars *t, const void *initdata, void **data)
227 {
228  JsonEmailLogThread *aft = SCMalloc(sizeof(JsonEmailLogThread));
229  if (unlikely(aft == NULL))
230  return TM_ECODE_FAILED;
231  memset(aft, 0, sizeof(JsonEmailLogThread));
232 
233  if(initdata == NULL)
234  {
235  SCLogDebug("Error getting context for EveLogSMTP. \"initdata\" argument NULL");
236  SCFree(aft);
237  return TM_ECODE_FAILED;
238  }
239 
240  /* Use the Ouptut Context (file pointer and mutex) */
241  aft->emaillog_ctx = ((OutputCtx *)initdata)->data;
242 
243  aft->buffer = MemBufferCreateNew(JSON_OUTPUT_BUFFER_SIZE);
244  if (aft->buffer == NULL) {
245  SCFree(aft);
246  return TM_ECODE_FAILED;
247  }
248 
249  *data = (void *)aft;
250  return TM_ECODE_OK;
251 }
252 
253 static TmEcode JsonSmtpLogThreadDeinit(ThreadVars *t, void *data)
254 {
255  JsonEmailLogThread *aft = (JsonEmailLogThread *)data;
256  if (aft == NULL) {
257  return TM_ECODE_OK;
258  }
259 
260  MemBufferFree(aft->buffer);
261  /* clear memory */
262  memset(aft, 0, sizeof(JsonEmailLogThread));
263 
264  SCFree(aft);
265  return TM_ECODE_OK;
266 }
267 
268 void JsonSmtpLogRegister (void) {
269  /* register as separate module */
270  OutputRegisterTxModule(LOGGER_JSON_SMTP, "JsonSmtpLog", "smtp-json-log",
271  OutputSmtpLogInit, ALPROTO_SMTP, JsonSmtpLogger, JsonSmtpLogThreadInit,
272  JsonSmtpLogThreadDeinit, NULL);
273 
274  /* also register as child of eve-log */
275  OutputRegisterTxSubModule(LOGGER_JSON_SMTP, "eve-log", "JsonSmtpLog",
276  "eve-log.smtp", OutputSmtpLogInitSub, ALPROTO_SMTP, JsonSmtpLogger,
277  JsonSmtpLogThreadInit, JsonSmtpLogThreadDeinit, NULL);
278 }
279 
280 #else
281 
283 {
284 }
285 
286 #endif
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
uint8_t * mail_from
#define SCLogDebug(...)
Definition: util-debug.h:335
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
struct HtpBodyChunk_ * next
#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:253
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
#define SCEnter(...)
Definition: util-debug.h:337
uint8_t * str
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
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
#define SCReturnInt(x)
Definition: util-debug.h:341
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:400
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define DEFAULT_LOG_FILENAME
#define SCMalloc(a)
Definition: util-mem.h:222
int LogFileFreeCtx(LogFileCtx *lf_ctx)
LogFileFreeCtx() Destroy a LogFileCtx (Close the file and free memory)
#define SCFree(a)
Definition: util-mem.h:322
uint16_t tx_id
void * data
Definition: tm-modules.h:81
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:389
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1065
void JsonSmtpLogRegister(void)
#define TAILQ_EMPTY(head)
Definition: queue.h:347
Per thread variable structure.
Definition: threadvars.h:57
#define likely(expr)
Definition: util-optimize.h:32
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82