• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/util-lua-smtp.c
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)
UNCOV
45
{
×
UNCOV
46
    if (!(LuaStateNeedProto(L, ALPROTO_SMTP))) {
×
47
        return LuaCallbackError(L, "error: protocol not SMTP");
×
48
    }
×
49

UNCOV
50
    Flow *flow = LuaStateGetFlow(L);
×
UNCOV
51
    if (flow == NULL) {
×
52
        return LuaCallbackError(L, "error: no flow found");
×
53
    }
×
54

UNCOV
55
    SMTPState *state = (SMTPState *)FlowGetAppState(flow);
×
UNCOV
56
    if (state == NULL) {
×
57
        return LuaCallbackError(L, "error: no SMTP state");
×
58
    }
×
59

UNCOV
60
    SMTPTransaction *tx = state->curr_tx;
×
UNCOV
61
    if (tx == NULL) {
×
62
        return LuaCallbackError(L, "error: no SMTP transaction found");
×
63
    }
×
64

UNCOV
65
    struct LuaSmtpTx *lua_tx = (struct LuaSmtpTx *)lua_newuserdata(L, sizeof(*lua_tx));
×
UNCOV
66
    if (lua_tx == NULL) {
×
67
        return LuaCallbackError(L, "error: fail to allocate user data");
×
68
    }
×
UNCOV
69
    lua_tx->tx = tx;
×
70

UNCOV
71
    luaL_getmetatable(L, smtp_tx_mt);
×
UNCOV
72
    lua_setmetatable(L, -2);
×
73

UNCOV
74
    return 1;
×
UNCOV
75
}
×
76

77
static int LuaSmtpTxGetMimeField(lua_State *L)
UNCOV
78
{
×
UNCOV
79
    struct LuaSmtpTx *tx = luaL_checkudata(L, 1, smtp_tx_mt);
×
80

UNCOV
81
    if (tx->tx->mime_state == NULL) {
×
82
        return LuaCallbackError(L, "no mime state");
×
83
    }
×
84

UNCOV
85
    const char *name = luaL_checkstring(L, 2);
×
UNCOV
86
    if (name == NULL) {
×
87
        return LuaCallbackError(L, "2nd argument missing, empty or wrong type");
×
88
    }
×
89

UNCOV
90
    const uint8_t *field_value;
×
UNCOV
91
    uint32_t field_len;
×
UNCOV
92
    if (SCMimeSmtpGetHeader(tx->tx->mime_state, name, &field_value, &field_len)) {
×
UNCOV
93
        return LuaPushStringBuffer(L, field_value, field_len);
×
UNCOV
94
    }
×
95

96
    return LuaCallbackError(L, "request mime field not found");
×
UNCOV
97
}
×
98

99
static int LuaSmtpTxGetMimeList(lua_State *L)
UNCOV
100
{
×
UNCOV
101
    struct LuaSmtpTx *tx = luaL_checkudata(L, 1, smtp_tx_mt);
×
102

UNCOV
103
    if (tx->tx->mime_state == NULL) {
×
104
        return LuaCallbackError(L, "no mime state");
×
105
    }
×
106

UNCOV
107
    const uint8_t *field_name;
×
UNCOV
108
    uint32_t field_len;
×
UNCOV
109
    int num = 1;
×
UNCOV
110
    lua_newtable(L);
×
UNCOV
111
    while (SCMimeSmtpGetHeaderName(tx->tx->mime_state, &field_name, &field_len, (uint32_t)num)) {
×
UNCOV
112
        if (field_len != 0) {
×
UNCOV
113
            lua_pushinteger(L, num++);
×
UNCOV
114
            LuaPushStringBuffer(L, field_name, field_len);
×
UNCOV
115
            lua_settable(L, -3);
×
UNCOV
116
        }
×
UNCOV
117
    }
×
UNCOV
118
    return 1;
×
UNCOV
119
}
×
120

121
static int LuaSmtpTxGetMailFrom(lua_State *L)
UNCOV
122
{
×
UNCOV
123
    struct LuaSmtpTx *tx = luaL_checkudata(L, 1, smtp_tx_mt);
×
124

UNCOV
125
    if (tx->tx->mail_from == NULL || tx->tx->mail_from_len == 0) {
×
UNCOV
126
        lua_pushnil(L);
×
UNCOV
127
        return 1;
×
UNCOV
128
    }
×
129

UNCOV
130
    return LuaPushStringBuffer(L, tx->tx->mail_from, tx->tx->mail_from_len);
×
UNCOV
131
}
×
132

133
static int LuaSmtpTxGetRcptList(lua_State *L)
UNCOV
134
{
×
UNCOV
135
    struct LuaSmtpTx *tx = luaL_checkudata(L, 1, smtp_tx_mt);
×
136

137
    /* Create a new table in luastate for rcpt list */
UNCOV
138
    lua_newtable(L);
×
139
    /* rcpt var for iterator */
UNCOV
140
    int u = 1;
×
UNCOV
141
    SMTPString *rcpt;
×
142

UNCOV
143
    TAILQ_FOREACH (rcpt, &tx->tx->rcpt_to_list, next) {
×
UNCOV
144
        lua_pushinteger(L, u++);
×
UNCOV
145
        LuaPushStringBuffer(L, rcpt->str, rcpt->len);
×
UNCOV
146
        lua_settable(L, -3);
×
UNCOV
147
    }
×
148

UNCOV
149
    return 1;
×
UNCOV
150
}
×
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)
UNCOV
166
{
×
UNCOV
167
    luaL_newmetatable(L, smtp_tx_mt);
×
UNCOV
168
    lua_pushvalue(L, -1);
×
UNCOV
169
    lua_setfield(L, -2, "__index");
×
UNCOV
170
    luaL_setfuncs(L, smtptxlib, 0);
×
171

UNCOV
172
    luaL_newlib(L, smtplib);
×
UNCOV
173
    return 1;
×
UNCOV
174
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc