Skip to content

Commit cb2ed18

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 79226ea commit cb2ed18

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

@@ -1088,6 +1089,11 @@ static int SMTPProcessReply(
10881089
!(state->parser_state & SMTP_PARSER_STATE_PARSING_MULTILINE_REPLY)) {
10891090
SMTPTransactionComplete(reply_tx);
10901091
}
1092+
} else if (IsReplyToCommand(state, SMTP_COMMAND_QUIT)) {
1093+
if (reply_code == SMTP_REPLY_221 && reply_tx &&
1094+
!(state->parser_state & SMTP_PARSER_STATE_PARSING_MULTILINE_REPLY)) {
1095+
SMTPTransactionComplete(reply_tx);
1096+
}
10911097
} else {
10921098
/* we don't care for any other command for now */
10931099
}
@@ -1284,8 +1290,9 @@ static int SMTPProcessRequest(
12841290
if (line->len == 0 && line->delim_len == 0) {
12851291
return 0;
12861292
}
1287-
if (state->curr_tx == NULL ||
1288-
(SMTPTransactionRequestIsComplete(state->curr_tx) && !NoNewTx(state, line))) {
1293+
const bool no_new_tx = NoNewTx(state, line);
1294+
if ((state->curr_tx == NULL && (state->tx_cnt == 0 || !no_new_tx)) ||
1295+
(SMTPTransactionRequestIsComplete(state->curr_tx) && !no_new_tx)) {
12891296
tx = SMTPTransactionCreate(state);
12901297
if (tx == NULL)
12911298
return -1;
@@ -1301,7 +1308,9 @@ static int SMTPProcessRequest(
13011308
if (frame != NULL && state->curr_tx) {
13021309
AppLayerFrameSetTxId(frame, state->curr_tx->tx_id);
13031310
}
1304-
tx->tx_data.updated_ts = true;
1311+
if (tx != NULL) {
1312+
tx->tx_data.updated_ts = true;
1313+
}
13051314

13061315
state->toserver_data_count += (line->len + line->delim_len);
13071316

@@ -1385,6 +1394,8 @@ static int SMTPProcessRequest(
13851394
// Resets chunk index in case of connection reuse
13861395
state->bdat_chunk_idx = 0;
13871396
state->current_command = SMTP_COMMAND_RSET;
1397+
} else if (line->len >= 4 && SCMemcmpLowercase("quit", line->buf, 4) == 0) {
1398+
state->current_command = SMTP_COMMAND_QUIT;
13881399
} else {
13891400
state->current_command = SMTP_COMMAND_OTHER_CMD;
13901401
}
@@ -2870,7 +2881,7 @@ static int SMTPParserTest02(void)
28702881
goto end;
28712882
}
28722883
if (smtp_state->cmds_cnt != 1 || smtp_state->cmds_idx != 0 ||
2873-
smtp_state->cmds[0] != SMTP_COMMAND_OTHER_CMD ||
2884+
smtp_state->cmds[0] != SMTP_COMMAND_QUIT ||
28742885
smtp_state->parser_state != SMTP_PARSER_STATE_FIRST_REPLY_SEEN) {
28752886
printf("smtp parser in inconsistent state\n");
28762887
goto end;
@@ -3352,7 +3363,7 @@ static int SMTPParserTest05(void)
33523363
goto end;
33533364
}
33543365
if (smtp_state->cmds_cnt != 1 || smtp_state->cmds_idx != 0 ||
3355-
smtp_state->cmds[0] != SMTP_COMMAND_OTHER_CMD ||
3366+
smtp_state->cmds[0] != SMTP_COMMAND_QUIT ||
33563367
smtp_state->parser_state !=
33573368
(SMTP_PARSER_STATE_FIRST_REPLY_SEEN | SMTP_PARSER_STATE_PIPELINING_SERVER)) {
33583369
printf("smtp parser in inconsistent state\n");
@@ -4370,7 +4381,7 @@ static int SMTPParserTest14(void)
43704381
goto end;
43714382
}
43724383
if (smtp_state->cmds_cnt != 1 || smtp_state->cmds_idx != 0 ||
4373-
smtp_state->cmds[0] != SMTP_COMMAND_OTHER_CMD ||
4384+
smtp_state->cmds[0] != SMTP_COMMAND_QUIT ||
43744385
smtp_state->parser_state != SMTP_PARSER_STATE_FIRST_REPLY_SEEN) {
43754386
printf("smtp parser in inconsistent state l.%d\n", __LINE__);
43764387
goto end;

0 commit comments

Comments
 (0)