suricata
output-json-smtp.c
Go to the documentation of this file.
1/* Copyright (C) 2007-2021 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 "detect.h"
28#include "pkt-var.h"
29#include "conf.h"
30
31#include "threads.h"
32#include "threadvars.h"
33#include "tm-threads.h"
34
35#include "util-print.h"
36#include "util-unittest.h"
37
38#include "util-debug.h"
39
40#include "output.h"
41#include "app-layer-smtp.h"
42#include "app-layer.h"
43#include "app-layer-parser.h"
44#include "util-privs.h"
45#include "util-buffer.h"
46#include "util-proto-name.h"
47#include "util-logopenfile.h"
48#include "util-time.h"
49
50#include "output-json.h"
51#include "output-json-smtp.h"
53
54static void EveSmtpDataLogger(void *state, void *vtx, SCJsonBuilder *js)
55{
56 SMTPTransaction *tx = vtx;
57 SMTPString *rcptto_str;
58 if (((SMTPState *)state)->helo) {
59 SCJbSetString(js, "helo", (const char *)((SMTPState *)state)->helo);
60 }
61 if (tx->mail_from) {
62 SCJbSetString(js, "mail_from", (const char *)tx->mail_from);
63 }
64 if (!TAILQ_EMPTY(&tx->rcpt_to_list)) {
65 SCJbOpenArray(js, "rcpt_to");
66 TAILQ_FOREACH(rcptto_str, &tx->rcpt_to_list, next) {
67 SCJbAppendString(js, (char *)rcptto_str->str);
68 }
69 SCJbClose(js);
70 }
71}
72
73static int JsonSmtpLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
74{
75 SCEnter();
76 JsonEmailLogThread *jhl = (JsonEmailLogThread *)thread_data;
77
78 SCJsonBuilder *jb = CreateEveHeaderWithTxId(
79 p, LOG_DIR_FLOW, "smtp", NULL, tx_id, jhl->emaillog_ctx->eve_ctx);
80 if (unlikely(jb == NULL))
81 return TM_ECODE_OK;
82
83 SCJbOpenObject(jb, "smtp");
84 EveSmtpDataLogger(state, tx, jb);
85 SCJbClose(jb);
86
87 EveEmailLogJson(jhl, jb, p, f, state, tx, tx_id);
88 OutputJsonBuilderBuffer(tv, p, p->flow, jb, jhl->ctx);
89
90 SCJbFree(jb);
91
93
94}
95
96bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
97{
98 SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
99 if (smtp_state) {
100 SMTPTransaction *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_SMTP, smtp_state, tx_id);
101 if (tx) {
102 EveSmtpDataLogger(smtp_state, tx, js);
103 return true;
104 }
105 }
106
107 return false;
108}
109
110static void OutputSmtpLogDeInitCtxSub(OutputCtx *output_ctx)
111{
112 SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
113 OutputJsonEmailCtx *email_ctx = output_ctx->data;
114 if (email_ctx != NULL) {
115 SCFree(email_ctx);
116 }
117 SCFree(output_ctx);
118}
119
120static OutputInitResult OutputSmtpLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
121{
122 OutputInitResult result = { NULL, false };
123 OutputJsonCtx *ojc = parent_ctx->data;
124
125 OutputJsonEmailCtx *email_ctx = SCCalloc(1, sizeof(OutputJsonEmailCtx));
126 if (unlikely(email_ctx == NULL))
127 return result;
128
129 OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
130 if (unlikely(output_ctx == NULL)) {
131 SCFree(email_ctx);
132 return result;
133 }
134
135 email_ctx->eve_ctx = ojc;
136
137 OutputEmailInitConf(conf, email_ctx);
138
139 output_ctx->data = email_ctx;
140 output_ctx->DeInit = OutputSmtpLogDeInitCtxSub;
141
142 /* enable the logger for the app layer */
144
145 result.ctx = output_ctx;
146 result.ok = true;
147 return result;
148}
149
150static TmEcode JsonSmtpLogThreadInit(ThreadVars *t, const void *initdata, void **data)
151{
153 if (unlikely(aft == NULL))
154 return TM_ECODE_FAILED;
155
156 if(initdata == NULL) {
157 SCLogDebug("Error getting context for EveLogSMTP. \"initdata\" argument NULL");
158 goto error_exit;
159 }
160
161 /* Use the Output Context (file pointer and mutex) */
162 aft->emaillog_ctx = ((OutputCtx *)initdata)->data;
163
165 if (aft->ctx == NULL) {
166 goto error_exit;
167 }
168
169 *data = (void *)aft;
170 return TM_ECODE_OK;
171
172error_exit:
173 SCFree(aft);
174 return TM_ECODE_FAILED;
175}
176
177static TmEcode JsonSmtpLogThreadDeinit(ThreadVars *t, void *data)
178{
180 if (aft == NULL) {
181 return TM_ECODE_OK;
182 }
183 FreeEveThreadCtx(aft->ctx);
184
185 /* clear memory */
186 memset(aft, 0, sizeof(JsonEmailLogThread));
187
188 SCFree(aft);
189 return TM_ECODE_OK;
190}
191
193 /* register as child of eve-log */
194 OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonSmtpLog", "eve-log.smtp",
195 OutputSmtpLogInitSub, ALPROTO_SMTP, JsonSmtpLogger, JsonSmtpLogThreadInit,
196 JsonSmtpLogThreadDeinit);
197}
struct HtpBodyChunk_ * next
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
@ ALPROTO_SMTP
ThreadVars * tv
@ LOG_DIR_FLOW
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
void OutputEmailInitConf(SCConfNode *conf, OutputJsonEmailCtx *email_ctx)
TmEcode EveEmailLogJson(JsonEmailLogThread *aft, SCJsonBuilder *js, const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
void JsonSmtpLogRegister(void)
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
SCJsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id, OutputJsonCtx *eve_ctx)
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)
Definition output.c:406
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:252
#define TAILQ_EMPTY(head)
Definition queue.h:248
Flow data structure.
Definition flow.h:356
OutputJsonEmailCtx * emaillog_ctx
OutputJsonThreadCtx * ctx
void * data
Definition tm-modules.h:91
void(* DeInit)(struct OutputCtx_ *)
Definition tm-modules.h:94
OutputCtx * ctx
Definition output.h:47
struct Flow_ * flow
Definition decode.h:546
uint8_t * str
Per thread variable structure.
Definition threadvars.h:58
@ LOGGER_JSON_TX
@ TM_ECODE_FAILED
@ TM_ECODE_OK
#define SCEnter(...)
Definition util-debug.h:277
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCReturnInt(x)
Definition util-debug.h:281
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define unlikely(expr)