Skip to content

Commit 5581266

Browse files
committed
smtp: don't create transaction for trailing quit
Also ensures that a quit or rset without a helo still creates a tx. Ticket: OISF#8728
1 parent 6e4acda commit 5581266

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

src/app-layer-smtp.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
/* All other commands are represented by this var */
9696
#define SMTP_COMMAND_OTHER_CMD 5
9797
#define SMTP_COMMAND_RSET 6
98+
#define SMTP_COMMAND_QUIT 7
9899

99100
#define SMTP_DEFAULT_MAX_TX 256
100101

@@ -1084,6 +1085,11 @@ static int SMTPProcessReply(
10841085
!(state->parser_state & SMTP_PARSER_STATE_PARSING_MULTILINE_REPLY)) {
10851086
SMTPTransactionComplete(reply_tx);
10861087
}
1088+
} else if (IsReplyToCommand(state, SMTP_COMMAND_QUIT)) {
1089+
if (reply_code == SMTP_REPLY_221 && reply_tx &&
1090+
!(state->parser_state & SMTP_PARSER_STATE_PARSING_MULTILINE_REPLY)) {
1091+
SMTPTransactionComplete(reply_tx);
1092+
}
10871093
} else {
10881094
/* we don't care for any other command for now */
10891095
}
@@ -1280,8 +1286,9 @@ static int SMTPProcessRequest(
12801286
if (line->len == 0 && line->delim_len == 0) {
12811287
return 0;
12821288
}
1283-
if (state->curr_tx == NULL ||
1284-
(SMTPTransactionRequestIsComplete(state->curr_tx) && !NoNewTx(state, line))) {
1289+
const bool no_new_tx = NoNewTx(state, line);
1290+
if ((state->curr_tx == NULL && (state->tx_cnt == 0 || !no_new_tx)) ||
1291+
(SMTPTransactionRequestIsComplete(state->curr_tx) && !no_new_tx)) {
12851292
tx = SMTPTransactionCreate(state);
12861293
if (tx == NULL)
12871294
return -1;
@@ -1297,7 +1304,9 @@ static int SMTPProcessRequest(
12971304
if (frame != NULL && state->curr_tx) {
12981305
AppLayerFrameSetTxId(frame, state->curr_tx->tx_id);
12991306
}
1300-
tx->tx_data.updated_ts = true;
1307+
if (tx != NULL) {
1308+
tx->tx_data.updated_ts = true;
1309+
}
13011310

13021311
state->toserver_data_count += (line->len + line->delim_len);
13031312

@@ -1381,6 +1390,8 @@ static int SMTPProcessRequest(
13811390
// Resets chunk index in case of connection reuse
13821391
state->bdat_chunk_idx = 0;
13831392
state->current_command = SMTP_COMMAND_RSET;
1393+
} else if (line->len >= 4 && SCMemcmpLowercase("quit", line->buf, 4) == 0) {
1394+
state->current_command = SMTP_COMMAND_QUIT;
13841395
} else {
13851396
state->current_command = SMTP_COMMAND_OTHER_CMD;
13861397
}
@@ -2866,7 +2877,7 @@ static int SMTPParserTest02(void)
28662877
goto end;
28672878
}
28682879
if (smtp_state->cmds_cnt != 1 || smtp_state->cmds_idx != 0 ||
2869-
smtp_state->cmds[0] != SMTP_COMMAND_OTHER_CMD ||
2880+
smtp_state->cmds[0] != SMTP_COMMAND_QUIT ||
28702881
smtp_state->parser_state != SMTP_PARSER_STATE_FIRST_REPLY_SEEN) {
28712882
printf("smtp parser in inconsistent state\n");
28722883
goto end;
@@ -3348,7 +3359,7 @@ static int SMTPParserTest05(void)
33483359
goto end;
33493360
}
33503361
if (smtp_state->cmds_cnt != 1 || smtp_state->cmds_idx != 0 ||
3351-
smtp_state->cmds[0] != SMTP_COMMAND_OTHER_CMD ||
3362+
smtp_state->cmds[0] != SMTP_COMMAND_QUIT ||
33523363
smtp_state->parser_state !=
33533364
(SMTP_PARSER_STATE_FIRST_REPLY_SEEN | SMTP_PARSER_STATE_PIPELINING_SERVER)) {
33543365
printf("smtp parser in inconsistent state\n");
@@ -4366,7 +4377,7 @@ static int SMTPParserTest14(void)
43664377
goto end;
43674378
}
43684379
if (smtp_state->cmds_cnt != 1 || smtp_state->cmds_idx != 0 ||
4369-
smtp_state->cmds[0] != SMTP_COMMAND_OTHER_CMD ||
4380+
smtp_state->cmds[0] != SMTP_COMMAND_QUIT ||
43704381
smtp_state->parser_state != SMTP_PARSER_STATE_FIRST_REPLY_SEEN) {
43714382
printf("smtp parser in inconsistent state l.%d\n", __LINE__);
43724383
goto end;

0 commit comments

Comments
 (0)