Skip to content

Commit

Permalink
lib-smtp: server: Make sure command object is not used after it is de…
Browse files Browse the repository at this point in the history
…stroyed in hook.
  • Loading branch information
stephanbosch committed Nov 1, 2018
1 parent a882b18 commit eeca44f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
30 changes: 20 additions & 10 deletions src/lib-smtp/smtp-server-command.c
Expand Up @@ -421,32 +421,41 @@ void smtp_server_command_ready_to_reply(struct smtp_server_command *cmd)
smtp_server_connection_trigger_output(cmd->context.conn);
}

void smtp_server_command_next_to_reply(struct smtp_server_command *cmd)
bool smtp_server_command_next_to_reply(struct smtp_server_command **_cmd)
{
struct smtp_server_command *cmd = *_cmd;

smtp_server_command_debug(&cmd->context, "Next to reply");

smtp_server_command_call_hooks(&cmd, SMTP_SERVER_COMMAND_HOOK_NEXT);
return smtp_server_command_call_hooks(
_cmd, SMTP_SERVER_COMMAND_HOOK_NEXT);
}

static void
smtp_server_command_replied(struct smtp_server_command *cmd)
static bool
smtp_server_command_replied(struct smtp_server_command **_cmd)
{
struct smtp_server_command *cmd = *_cmd;

if (cmd->replies_submitted < cmd->replies_expected)
return;
return TRUE;

smtp_server_command_debug(&cmd->context, "Replied");

smtp_server_command_call_hooks(&cmd, SMTP_SERVER_COMMAND_HOOK_REPLIED);
return smtp_server_command_call_hooks(
_cmd, SMTP_SERVER_COMMAND_HOOK_REPLIED);
}

void smtp_server_command_completed(struct smtp_server_command *cmd)
bool smtp_server_command_completed(struct smtp_server_command **_cmd)
{
struct smtp_server_command *cmd = *_cmd;

if (cmd->replies_submitted < cmd->replies_expected)
return;
return TRUE;

smtp_server_command_debug(&cmd->context, "Completed");

smtp_server_command_call_hooks(&cmd, SMTP_SERVER_COMMAND_HOOK_COMPLETED);
return smtp_server_command_call_hooks(
_cmd, SMTP_SERVER_COMMAND_HOOK_COMPLETED);
}

static bool
Expand All @@ -456,7 +465,8 @@ smtp_server_command_handle_reply(struct smtp_server_command *cmd)

smtp_server_connection_ref(conn);

smtp_server_command_replied(cmd);
if (!smtp_server_command_replied(&cmd))
return smtp_server_connection_unref(&conn);

/* submit reply */
switch (cmd->state) {
Expand Down
7 changes: 4 additions & 3 deletions src/lib-smtp/smtp-server-connection.c
Expand Up @@ -333,7 +333,7 @@ smtp_server_connection_handle_command(struct smtp_server_connection *conn,
}

if (cmd != NULL && conn->command_queue_head == cmd)
smtp_server_command_next_to_reply(cmd);
(void)smtp_server_command_next_to_reply(&cmd);

smtp_server_connection_timeout_update(conn);
return (cmd == NULL || !cmd->input_locked);
Expand Down Expand Up @@ -668,14 +668,15 @@ smtp_server_connection_next_reply(struct smtp_server_connection *conn)
}

if (cmd->state < SMTP_SERVER_COMMAND_STATE_READY_TO_REPLY) {
smtp_server_command_next_to_reply(cmd);
(void)smtp_server_command_next_to_reply(&cmd);
return FALSE;
}

i_assert(cmd->state == SMTP_SERVER_COMMAND_STATE_READY_TO_REPLY &&
array_is_created(&cmd->replies));

smtp_server_command_completed(cmd);
if (!smtp_server_command_completed(&cmd))
return TRUE;

/* send command replies */
// FIXME: handle LMTP DATA command with enormous number of recipients;
Expand Down
5 changes: 3 additions & 2 deletions src/lib-smtp/smtp-server-private.h
Expand Up @@ -265,10 +265,11 @@ void smtp_server_command_submit_reply(struct smtp_server_command *cmd);
int smtp_server_connection_flush(struct smtp_server_connection *conn);

void smtp_server_command_ready_to_reply(struct smtp_server_command *cmd);
void smtp_server_command_next_to_reply(struct smtp_server_command *cmd);
void smtp_server_command_completed(struct smtp_server_command *cmd);
void smtp_server_command_finished(struct smtp_server_command *cmd);

bool smtp_server_command_next_to_reply(struct smtp_server_command **_cmd);
bool smtp_server_command_completed(struct smtp_server_command **_cmd);

static inline bool
smtp_server_command_is_complete(struct smtp_server_command *cmd)
{
Expand Down

0 comments on commit eeca44f

Please sign in to comment.