Coverage Report

Created: 2026-09-06 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/util-lua-smtp.c
Line
Count
Source
1
/* Copyright (C) 2014-2025 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 casec Bachelors group
22
 *  \author Lauritz Prag Sømme <lauritz24@me.com>
23
 *  \author Levi Tobiassen <levi.tobiassen@gmail.com>
24
 *  \author Stian Hoel Bergseth <stian.bergseth@hig.no>
25
 *  \author Vinjar Hillestad <vinjar.hillestad@hig.no>
26
 */
27
28
#include "suricata-common.h"
29
#include "app-layer-smtp.h"
30
31
#include "util-lua.h"
32
#include "util-lua-common.h"
33
#include "util-lua-smtp.h"
34
35
#include "lua.h"
36
#include "lauxlib.h"
37
38
static const char smtp_tx_mt[] = "suricata:smtp:tx";
39
40
struct LuaSmtpTx {
41
    SMTPTransaction *tx;
42
};
43
44
static int LuaSmtpGetTx(lua_State *L)
45
0
{
46
0
    if (!(LuaStateNeedProto(L, ALPROTO_SMTP))) {
47
0
        return LuaCallbackError(L, "error: protocol not SMTP");
48
0
    }
49
50
0
    Flow *flow = LuaStateGetFlow(L);
51
0
    if (flow == NULL) {
52
0
        return LuaCallbackError(L, "error: no flow found");
53
0
    }
54
55
0
    SMTPState *state = (SMTPState *)FlowGetAppState(flow);
56
0
    if (state == NULL) {
57
0
        return LuaCallbackError(L, "error: no SMTP state");
58
0
    }
59
60
0
    SMTPTransaction *tx = state->curr_tx;
61
0
    if (tx == NULL) {
62
0
        return LuaCallbackError(L, "error: no SMTP transaction found");
63
0
    }
64
65
0
    struct LuaSmtpTx *lua_tx = (struct LuaSmtpTx *)lua_newuserdata(L, sizeof(*lua_tx));
66
0
    if (lua_tx == NULL) {
67
0
        return LuaCallbackError(L, "error: fail to allocate user data");
68
0
    }
69
0
    lua_tx->tx = tx;
70
71
0
    luaL_getmetatable(L, smtp_tx_mt);
72
0
    lua_setmetatable(L, -2);
73
74
0
    return 1;
75
0
}
76
77
static int LuaSmtpTxGetMimeField(lua_State *L)
78
0
{
79
0
    struct LuaSmtpTx *tx = luaL_checkudata(L, 1, smtp_tx_mt);
80
81
0
    if (tx->tx->mime_state == NULL) {
82
0
        return LuaCallbackError(L, "no mime state");
83
0
    }
84
85
0
    const char *name = luaL_checkstring(L, 2);
86
0
    if (name == NULL) {
87
0
        return LuaCallbackError(L, "2nd argument missing, empty or wrong type");
88
0
    }
89
90
0
    const uint8_t *field_value;
91
0
    uint32_t field_len;
92
0
    if (SCMimeSmtpGetHeader(tx->tx->mime_state, name, &field_value, &field_len)) {
93
0
        return LuaPushStringBuffer(L, field_value, field_len);
94
0
    }
95
96
0
    return LuaCallbackError(L, "request mime field not found");
97
0
}
98
99
static int LuaSmtpTxGetMimeList(lua_State *L)
100
0
{
101
0
    struct LuaSmtpTx *tx = luaL_checkudata(L, 1, smtp_tx_mt);
102
103
0
    if (tx->tx->mime_state == NULL) {
104
0
        return LuaCallbackError(L, "no mime state");
105
0
    }
106
107
0
    const uint8_t *field_name;
108
0
    uint32_t field_len;
109
0
    int num = 1;
110
0
    lua_newtable(L);
111
0
    while (SCMimeSmtpGetHeaderName(tx->tx->mime_state, &field_name, &field_len, (uint32_t)num)) {
112
0
        if (field_len != 0) {
113
0
            lua_pushinteger(L, num++);
114
0
            LuaPushStringBuffer(L, field_name, field_len);
115
0
            lua_settable(L, -3);
116
0
        }
117
0
    }
118
0
    return 1;
119
0
}
120
121
static int LuaSmtpTxGetMailFrom(lua_State *L)
122
0
{
123
0
    struct LuaSmtpTx *tx = luaL_checkudata(L, 1, smtp_tx_mt);
124
125
0
    if (tx->tx->mail_from == NULL || tx->tx->mail_from_len == 0) {
126
0
        lua_pushnil(L);
127
0
        return 1;
128
0
    }
129
130
0
    return LuaPushStringBuffer(L, tx->tx->mail_from, tx->tx->mail_from_len);
131
0
}
132
133
static int LuaSmtpTxGetRcptList(lua_State *L)
134
0
{
135
0
    struct LuaSmtpTx *tx = luaL_checkudata(L, 1, smtp_tx_mt);
136
137
    /* Create a new table in luastate for rcpt list */
138
0
    lua_newtable(L);
139
    /* rcpt var for iterator */
140
0
    int u = 1;
141
0
    SMTPString *rcpt;
142
143
0
    TAILQ_FOREACH (rcpt, &tx->tx->rcpt_to_list, next) {
144
0
        lua_pushinteger(L, u++);
145
0
        LuaPushStringBuffer(L, rcpt->str, rcpt->len);
146
0
        lua_settable(L, -3);
147
0
    }
148
149
0
    return 1;
150
0
}
151
152
static const struct luaL_Reg smtptxlib[] = {
153
    { "get_mime_field", LuaSmtpTxGetMimeField },
154
    { "get_mime_list", LuaSmtpTxGetMimeList },
155
    { "get_mail_from", LuaSmtpTxGetMailFrom },
156
    { "get_rcpt_list", LuaSmtpTxGetRcptList },
157
    { NULL, NULL },
158
};
159
160
static const struct luaL_Reg smtplib[] = {
161
    { "get_tx", LuaSmtpGetTx },
162
    { NULL, NULL },
163
};
164
165
int SCLuaLoadSmtpLib(lua_State *L)
166
0
{
167
0
    luaL_newmetatable(L, smtp_tx_mt);
168
0
    lua_pushvalue(L, -1);
169
0
    lua_setfield(L, -2, "__index");
170
0
    luaL_setfuncs(L, smtptxlib, 0);
171
172
0
    luaL_newlib(L, smtplib);
173
0
    return 1;
174
0
}