Coverage Report

Created: 2025-07-23 07:29

/src/suricata7/src/output-json-mqtt.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2020-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 Sascha Steinbiss <sascha@steinbiss.name>
22
 */
23
24
#include "suricata-common.h"
25
#include "detect.h"
26
#include "pkt-var.h"
27
#include "conf.h"
28
29
#include "threads.h"
30
#include "threadvars.h"
31
#include "tm-threads.h"
32
33
#include "util-unittest.h"
34
#include "util-buffer.h"
35
#include "util-debug.h"
36
#include "util-byte.h"
37
38
#include "output.h"
39
#include "output-json.h"
40
41
#include "app-layer.h"
42
#include "app-layer-parser.h"
43
44
#include "app-layer-mqtt.h"
45
#include "output-json-mqtt.h"
46
#include "rust.h"
47
48
566
#define MQTT_LOG_PASSWORDS BIT_U32(0)
49
566
#define MQTT_DEFAULTS (MQTT_LOG_PASSWORDS)
50
51
typedef struct LogMQTTFileCtx_ {
52
    uint32_t    flags;
53
    OutputJsonCtx *eve_ctx;
54
} LogMQTTFileCtx;
55
56
typedef struct LogMQTTLogThread_ {
57
    LogMQTTFileCtx *mqttlog_ctx;
58
    uint32_t        count;
59
    OutputJsonThreadCtx *ctx;
60
} LogMQTTLogThread;
61
62
bool JsonMQTTAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
63
1.76k
{
64
1.76k
    MQTTState *state = FlowGetAppState(f);
65
1.76k
    if (state) {
66
1.76k
        MQTTTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_MQTT, state, tx_id);
67
1.76k
        if (tx) {
68
566
            return rs_mqtt_logger_log(tx, MQTT_DEFAULTS, js);
69
566
        }
70
1.76k
    }
71
72
1.20k
    return false;
73
1.76k
}
74
75
static int JsonMQTTLogger(ThreadVars *tv, void *thread_data,
76
    const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
77
0
{
78
0
    LogMQTTLogThread *thread = thread_data;
79
0
    enum OutputJsonLogDirection dir;
80
81
0
    if (rs_mqtt_tx_is_toclient((MQTTTransaction*) tx)) {
82
0
        dir = LOG_DIR_FLOW_TOCLIENT;
83
0
    } else {
84
0
        dir = LOG_DIR_FLOW_TOSERVER;
85
0
    }
86
87
0
    JsonBuilder *js = CreateEveHeader(p, dir, "mqtt", NULL, thread->mqttlog_ctx->eve_ctx);
88
0
    if (unlikely(js == NULL)) {
89
0
        return TM_ECODE_FAILED;
90
0
    }
91
92
0
    if (!rs_mqtt_logger_log(tx, thread->mqttlog_ctx->flags, js))
93
0
        goto error;
94
95
0
    OutputJsonBuilderBuffer(js, thread->ctx);
96
0
    jb_free(js);
97
98
0
    return TM_ECODE_OK;
99
100
0
error:
101
0
    jb_free(js);
102
0
    return TM_ECODE_FAILED;
103
0
}
104
105
static void OutputMQTTLogDeInitCtxSub(OutputCtx *output_ctx)
106
0
{
107
0
    LogMQTTFileCtx *mqttlog_ctx = (LogMQTTFileCtx *)output_ctx->data;
108
0
    SCFree(mqttlog_ctx);
109
0
    SCFree(output_ctx);
110
0
}
111
112
static void JsonMQTTLogParseConfig(ConfNode *conf, LogMQTTFileCtx *mqttlog_ctx)
113
0
{
114
0
    const char *query = ConfNodeLookupChildValue(conf, "passwords");
115
0
    if (query != NULL) {
116
0
        if (ConfValIsTrue(query)) {
117
0
            mqttlog_ctx->flags |= MQTT_LOG_PASSWORDS;
118
0
        } else {
119
0
            mqttlog_ctx->flags &= ~MQTT_LOG_PASSWORDS;
120
0
        }
121
0
    } else {
122
0
        mqttlog_ctx->flags |= MQTT_LOG_PASSWORDS;
123
0
    }
124
0
}
125
126
static OutputInitResult OutputMQTTLogInitSub(ConfNode *conf,
127
    OutputCtx *parent_ctx)
128
0
{
129
0
    OutputInitResult result = { NULL, false };
130
0
    OutputJsonCtx *ajt = parent_ctx->data;
131
132
0
    LogMQTTFileCtx *mqttlog_ctx = SCCalloc(1, sizeof(*mqttlog_ctx));
133
0
    if (unlikely(mqttlog_ctx == NULL)) {
134
0
        return result;
135
0
    }
136
0
    mqttlog_ctx->eve_ctx = ajt;
137
138
0
    OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
139
0
    if (unlikely(output_ctx == NULL)) {
140
0
        SCFree(mqttlog_ctx);
141
0
        return result;
142
0
    }
143
0
    output_ctx->data = mqttlog_ctx;
144
0
    output_ctx->DeInit = OutputMQTTLogDeInitCtxSub;
145
146
0
    JsonMQTTLogParseConfig(conf, mqttlog_ctx);
147
148
0
    AppLayerParserRegisterLogger(IPPROTO_TCP, ALPROTO_MQTT);
149
150
0
    result.ctx = output_ctx;
151
0
    result.ok = true;
152
0
    return result;
153
0
}
154
155
static TmEcode JsonMQTTLogThreadInit(ThreadVars *t, const void *initdata, void **data)
156
0
{
157
0
    LogMQTTLogThread *thread = SCCalloc(1, sizeof(*thread));
158
0
    if (unlikely(thread == NULL)) {
159
0
        return TM_ECODE_FAILED;
160
0
    }
161
162
0
    if (initdata == NULL) {
163
0
        SCLogDebug("Error getting context for EveLogMQTT.  \"initdata\" is NULL.");
164
0
        SCFree(thread);
165
0
        return TM_ECODE_FAILED;
166
0
    }
167
168
0
    thread->mqttlog_ctx = ((OutputCtx *)initdata)->data;
169
0
    thread->ctx = CreateEveThreadCtx(t, thread->mqttlog_ctx->eve_ctx);
170
0
    if (unlikely(thread->ctx == NULL)) {
171
0
        SCFree(thread);
172
0
        return TM_ECODE_FAILED;
173
0
    }
174
175
0
    *data = (void *)thread;
176
177
0
    return TM_ECODE_OK;
178
0
}
179
180
static TmEcode JsonMQTTLogThreadDeinit(ThreadVars *t, void *data)
181
0
{
182
0
    LogMQTTLogThread *thread = (LogMQTTLogThread *)data;
183
0
    if (thread == NULL) {
184
0
        return TM_ECODE_OK;
185
0
    }
186
0
    FreeEveThreadCtx(thread->ctx);
187
0
    SCFree(thread);
188
0
    return TM_ECODE_OK;
189
0
}
190
191
void JsonMQTTLogRegister(void)
192
71
{
193
71
    OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonMQTTLog", "eve-log.mqtt",
194
71
            OutputMQTTLogInitSub, ALPROTO_MQTT, JsonMQTTLogger, JsonMQTTLogThreadInit,
195
71
            JsonMQTTLogThreadDeinit, NULL);
196
71
}