* Connection
*/
-static void
-smtp_server_connection_input(struct connection *_conn);
-static int
-smtp_server_connection_output(struct smtp_server_connection *conn);
+static void smtp_server_connection_input(struct connection *_conn);
+static int smtp_server_connection_output(struct smtp_server_connection *conn);
static void
smtp_server_connection_disconnect(struct smtp_server_connection *conn,
const char *reason) ATTR_NULL(2);
bool cmd_locked = FALSE;
if (conn->conn.io == NULL) {
- /* only resume when we actually can */
+ /* Only resume when we actually can */
if (conn->input_locked || conn->input_broken ||
conn->disconnected)
return;
conn->server->set.max_pipelined_commands)
return;
- /* is queued command still blocking input? */
+ /* Is queued command still blocking input? */
cmd = conn->command_queue_head;
while (cmd != NULL) {
if (cmd->input_locked) {
if (cmd_locked)
return;
- /* restore input handler */
+ /* Restore input handler */
connection_input_resume(&conn->conn);
}
}
void smtp_server_connection_set_streams(struct smtp_server_connection *conn,
- struct istream *input, struct ostream *output)
+ struct istream *input,
+ struct ostream *output)
{
struct istream *old_input = conn->conn.input;
struct ostream *old_output = conn->conn.output;
}
void smtp_server_connection_set_ssl_streams(struct smtp_server_connection *conn,
- struct istream *input, struct ostream *output)
+ struct istream *input,
+ struct ostream *output)
{
conn->ssl_secured = TRUE;
conn->set.capabilities &= ~SMTP_CAPABILITY_STARTTLS;
static void
smtp_server_connection_idle_timeout(struct smtp_server_connection *conn)
{
- smtp_server_connection_terminate(&conn,
- "4.4.2", "Disconnected for inactivity");
+ smtp_server_connection_terminate(
+ &conn, "4.4.2", "Disconnected for inactivity");
}
void smtp_server_connection_timeout_stop(struct smtp_server_connection *conn)
break;
case SMTP_SERVER_COMMAND_STATE_PROCESSING:
if (cmd->input_captured) {
- /* command updates timeout internally */
+ /* Command updates timeout internally */
return;
}
smtp_server_connection_timeout_stop(conn);
}
}
-static void
-smtp_server_connection_ready(struct smtp_server_connection *conn)
+static void smtp_server_connection_ready(struct smtp_server_connection *conn)
{
conn->raw_input = conn->conn.input;
conn->raw_output = conn->conn.output;
smtp_server_connection_update_rawlog(conn);
conn->smtp_parser = smtp_command_parser_init(conn->conn.input,
- &conn->set.command_limits);
+ &conn->set.command_limits);
o_stream_set_flush_callback(conn->conn.output,
- smtp_server_connection_output, conn);
+ smtp_server_connection_output, conn);
o_stream_cork(conn->conn.output);
if (conn->authenticated) {
/* RFC 4954, Section 4:
Should the client successfully complete the exchange, the
SMTP server issues a 235 reply. */
- smtp_server_connection_send_line(conn,
- "235 2.7.0 Logged in.");
+ smtp_server_connection_send_line(
+ conn, "235 2.7.0 Logged in.");
} else {
- smtp_server_connection_send_line(conn,
- "220 %s %s", conn->set.hostname,
+ smtp_server_connection_send_line(
+ conn, "220 %s %s", conn->set.hostname,
conn->set.login_greeting);
}
if (!conn->corked)
smtp_server_connection_ref(tmp_conn);
smtp_server_command_execute(cmd, cmd_params);
if (!smtp_server_connection_unref(&tmp_conn)) {
- /* the command start callback managed to get this connection
+ /* The command start callback managed to get this connection
destroyed */
smtp_server_command_unref(&cmd);
return FALSE;
return 0;
}
- if (ssl_iostream_server_context_cache_get(conn->set.ssl,
- &conn->ssl_ctx, &error) < 0) {
- *error_r = t_strdup_printf("Couldn't initialize SSL context: %s",
- error);
+ if (ssl_iostream_server_context_cache_get(conn->set.ssl, &conn->ssl_ctx,
+ &error) < 0) {
+ *error_r = t_strdup_printf(
+ "Couldn't initialize SSL context: %s", error);
return -1;
}
return 0;
e_debug(conn->event, "Starting SSL handshake");
if (conn->raw_input != conn->conn.input) {
- /* recreate rawlog after STARTTLS */
+ /* Recreate rawlog after STARTTLS */
i_stream_ref(conn->raw_input);
o_stream_ref(conn->raw_output);
i_stream_destroy(&conn->conn.input);
smtp_server_connection_input_halt(conn);
if (conn->ssl_ctx == NULL) {
- ret = master_service_ssl_init(master_service,
- &conn->conn.input, &conn->conn.output,
+ ret = master_service_ssl_init(
+ master_service, &conn->conn.input, &conn->conn.output,
&conn->ssl_iostream, &error);
} else {
- ret = io_stream_create_ssl_server(conn->ssl_ctx,
- conn->set.ssl, &conn->conn.input, &conn->conn.output,
+ ret = io_stream_create_ssl_server(
+ conn->ssl_ctx, conn->set.ssl,
+ &conn->conn.input, &conn->conn.output,
&conn->ssl_iostream, &error);
}
if (ret < 0) {
const char *cmd_name, *cmd_params, *error;
int ret;
- /* check whether we are continuing a command */
+ /* Check whether we are continuing a command */
pending_command = NULL;
if (conn->command_queue_tail != NULL) {
- pending_command = (conn->command_queue_tail->state ==
- SMTP_SERVER_COMMAND_STATE_SUBMITTED_REPLY ?
- conn->command_queue_tail : NULL);
+ pending_command =
+ ((conn->command_queue_tail->state ==
+ SMTP_SERVER_COMMAND_STATE_SUBMITTED_REPLY) ?
+ conn->command_queue_tail : NULL);
}
smtp_server_connection_timeout_reset(conn);
- /* parse commands */
+ /* Parse commands */
ret = 1;
while (!conn->closing && ret != 0) {
- while ((ret = smtp_command_parse_next(conn->smtp_parser,
- &cmd_name, &cmd_params, &error_code, &error)) > 0) {
+ while ((ret = smtp_command_parse_next(
+ conn->smtp_parser, &cmd_name, &cmd_params,
+ &error_code, &error)) > 0) {
if (pending_command != NULL) {
- /* previous command is now fully read and ready
+ /* Previous command is now fully read and ready
to reply */
smtp_server_command_ready_to_reply(pending_command);
pending_command = NULL;
conn->stats.command_count++;
- /* handle command
- cmd may be destroyed after this */
+ /* Handle command (cmd may be destroyed after this) */
if (!smtp_server_connection_handle_command(conn,
cmd_name, cmd_params))
return;
if (conn->disconnected)
return;
- /* client indicated it will close after this command;
+ /* Client indicated it will close after this command;
stop trying to read more. */
if (conn->closing)
break;
}
if (conn->command_queue_tail != NULL) {
- pending_command = (conn->command_queue_tail->state ==
- SMTP_SERVER_COMMAND_STATE_SUBMITTED_REPLY ?
- conn->command_queue_tail : NULL);
+ pending_command =
+ ((conn->command_queue_tail->state ==
+ SMTP_SERVER_COMMAND_STATE_SUBMITTED_REPLY) ?
+ conn->command_queue_tail : NULL);
}
}
if (ret < 0 && conn->conn.input->eof) {
int stream_errno = conn->conn.input->stream_errno;
if (stream_errno != 0 && stream_errno != EPIPE &&
- stream_errno != ECONNRESET) {
+ stream_errno != ECONNRESET) {
e_error(conn->event,
"Connection lost: read(%s) failed: %s",
i_stream_get_name(conn->conn.input),
"Connection lost: Remote disconnected");
if (conn->command_queue_head == NULL) {
- /* no pending commands; close */
+ /* No pending commands; close */
smtp_server_connection_close(&conn,
"Remote closed connection");
} else if (conn->command_queue_head->state <
SMTP_SERVER_COMMAND_STATE_SUBMITTED_REPLY) {
- /* unfinished command; close */
+ /* Unfinished command; close */
smtp_server_connection_close(&conn,
"Remote closed connection unexpectedly");
} else {
- /* a command is still processing;
+ /* A command is still processing;
only drop input io for now */
conn->input_broken = TRUE;
smtp_server_connection_input_halt(conn);
/* fall through */
case SMTP_COMMAND_PARSE_ERROR_BAD_COMMAND:
cmd = smtp_server_command_new_invalid(conn);
- smtp_server_command_fail(cmd,
- 500, "5.5.2", "Invalid command syntax");
+ smtp_server_command_fail(
+ cmd, 500, "5.5.2",
+ "Invalid command syntax");
break;
case SMTP_COMMAND_PARSE_ERROR_LINE_TOO_LONG:
cmd = smtp_server_command_new_invalid(conn);
- smtp_server_command_fail(cmd,
- 500, "5.5.2", "Line too long");
+ smtp_server_command_fail(
+ cmd, 500, "5.5.2", "Line too long");
break;
case SMTP_COMMAND_PARSE_ERROR_DATA_TOO_LARGE:
/* Command data size exceeds the absolute limit;
}
if (ret == 0 && pending_command != NULL &&
- !smtp_command_parser_pending_data(conn->smtp_parser)) {
- /* previous command is now fully read and ready to
+ !smtp_command_parser_pending_data(conn->smtp_parser)) {
+ /* Previous command is now fully read and ready to
reply */
smtp_server_command_ready_to_reply(pending_command);
}
if (conn->command_queue_count >
- conn->server->set.max_pipelined_commands) {
+ conn->server->set.max_pipelined_commands) {
smtp_server_connection_input_halt(conn);
return;
}
smtp_server_connection_ref(conn);
conn->handling_input = TRUE;
if (conn->callbacks != NULL &&
- conn->callbacks->conn_cmd_input_pre != NULL) {
+ conn->callbacks->conn_cmd_input_pre != NULL)
conn->callbacks->conn_cmd_input_pre(conn->context);
- }
smtp_server_connection_handle_input(conn);
if (conn->callbacks != NULL &&
- conn->callbacks->conn_cmd_input_post != NULL) {
+ conn->callbacks->conn_cmd_input_post != NULL)
conn->callbacks->conn_cmd_input_post(conn->context);
- }
conn->handling_input = FALSE;
smtp_server_connection_unref(&conn);
}
output->stream_errno != ECONNRESET) {
e_error(conn->event, "Connection lost: write(%s) failed: %s",
o_stream_get_name(output), o_stream_get_error(output));
- smtp_server_connection_close(&conn,
- "Write failure");
+ smtp_server_connection_close(&conn, "Write failure");
} else {
e_debug(conn->event, "Connection lost: Remote disconnected");
- smtp_server_connection_close(&conn,
- "Remote closed connection unexpectedly");
+ smtp_server_connection_close(
+ &conn, "Remote closed connection unexpectedly");
}
}
cmd = conn->command_queue_head;
if (cmd == NULL) {
- /* no commands pending */
+ /* No commands pending */
e_debug(conn->event, "No more commands pending");
return FALSE;
}
if (!smtp_server_command_completed(&cmd))
return TRUE;
- /* send command replies */
+ /* Send command replies */
// FIXME: handle LMTP DATA command with enormous number of recipients;
// i.e. don't keep filling output stream with replies indefinitely.
for (i = 0; i < cmd->replies_expected; i++) {
static void
smtp_server_connection_send_replies(struct smtp_server_connection *conn)
{
- /* send more replies until no more replies remain, the output
+ /* Send more replies until no more replies remain, the output
blocks again, or the connection is closed */
while (!conn->disconnected && smtp_server_connection_next_reply(conn));
smtp_server_connection_timeout_update(conn);
- /* accept more commands if possible */
+ /* Accept more commands if possible */
smtp_server_connection_input_resume(conn);
}
return 1;
}
-static int
-smtp_server_connection_output(struct smtp_server_connection *conn)
+static int smtp_server_connection_output(struct smtp_server_connection *conn)
{
int ret;
smtp_server_connection_ref(conn);
o_stream_cork(conn->conn.output);
- if ((ret=smtp_server_connection_flush(conn)) > 0) {
+ ret = smtp_server_connection_flush(conn);
+ if (ret > 0) {
smtp_server_connection_timeout_reset(conn);
smtp_server_connection_send_replies(conn);
}
if (ret >= 0 && !conn->corked && conn->conn.output != NULL) {
- if ((ret=o_stream_uncork_flush(conn->conn.output)) < 0)
+ ret = o_stream_uncork_flush(conn->conn.output);
+ if (ret < 0)
smtp_server_connection_handle_output_error(conn);
}
smtp_server_connection_unref(&conn);
return ret;
}
-void smtp_server_connection_trigger_output(
- struct smtp_server_connection *conn)
+void smtp_server_connection_trigger_output(struct smtp_server_connection *conn)
{
if (conn->conn.output != NULL) {
e_debug(conn->event, "Trigger output");
}
}
-
/*
*
*/
static const struct connection_vfuncs smtp_server_connection_vfuncs = {
.destroy = smtp_server_connection_destroy,
- .input = smtp_server_connection_input
+ .input = smtp_server_connection_input,
};
-struct connection_list *
-smtp_server_connection_list_init(void)
+struct connection_list *smtp_server_connection_list_init(void)
{
return connection_list_init(&smtp_server_connection_set,
- &smtp_server_connection_vfuncs);
+ &smtp_server_connection_vfuncs);
}
static struct event *
smtp_server_event_init(server, conn_event);
} else
conn_event = event_create(server->event);
- event_set_append_log_prefix(
- conn_event, t_strdup_printf(
- "%s-server: ",
- smtp_protocol_name(server->set.protocol)));
+ event_set_append_log_prefix(conn_event, t_strdup_printf(
+ "%s-server: ", smtp_protocol_name(server->set.protocol)));
event_set_forced_debug(conn_event, (set != NULL && set->debug));
return conn_event;
conn->callbacks = callbacks;
conn->context = context;
- /* merge settings with global server settings */
+ /* Merge settings with global server settings */
conn->set = server->set;
if (set != NULL) {
conn->set.protocol = server->set.protocol;
set->max_message_size == (uoff_t)-1) {
conn->set.command_limits.max_data_size = UOFF_T_MAX;
} else if (conn->set.command_limits.max_data_size != 0) {
- /* explicit limit given */
+ /* Explicit limit given */
} else if (set->max_message_size >
(UOFF_T_MAX - SMTP_SERVER_DEFAULT_MAX_SIZE_EXCESS_LIMIT)) {
- /* very high limit */
+ /* Very high limit */
conn->set.command_limits.max_data_size = UOFF_T_MAX;
} else {
- /* absolute maximum before connection is closed in DATA
+ /* Absolute maximum before connection is closed in DATA
command */
conn->set.command_limits.max_data_size =
set->max_message_size +
}
struct smtp_server_connection *
-smtp_server_connection_create(struct smtp_server *server,
- int fd_in, int fd_out,
+smtp_server_connection_create(
+ struct smtp_server *server, int fd_in, int fd_out,
const struct ip_addr *remote_ip, in_port_t remote_port,
bool ssl_start, const struct smtp_server_settings *set,
const struct smtp_server_callbacks *callbacks, void *context)
if (ssl_start)
conn->set.capabilities &= ~SMTP_CAPABILITY_STARTTLS;
- /* halt input until started */
+ /* Halt input until started */
smtp_server_connection_halt(conn);
e_debug(conn->event, "Connection created");
}
struct smtp_server_connection *
-smtp_server_connection_create_from_streams(struct smtp_server *server,
+smtp_server_connection_create_from_streams(
+ struct smtp_server *server,
struct istream *input, struct ostream *output,
const struct ip_addr *remote_ip, in_port_t remote_port,
const struct smtp_server_settings *set,
conn->event = conn->conn.event;
event_unref(&conn_event);
- /* halt input until started */
+ /* Halt input until started */
smtp_server_connection_halt(conn);
e_debug(conn->event, "Connection created");
e_debug(conn->event, "Disconnected: %s", reason);
conn->disconnect_reason = i_strdup(reason);
- /* preserve statistics */
+ /* Preserve statistics */
smtp_server_connection_update_stats(conn);
- /* drop transaction */
+ /* Drop transaction */
smtp_server_connection_reset_state(conn);
- /* clear command queue */
+ /* Clear command queue */
cmd = conn->command_queue_head;
while (cmd != NULL) {
cmd_next = cmd->next;
ssl_iostream_context_unref(&conn->ssl_ctx);
if (conn->callbacks != NULL &&
- conn->callbacks->conn_disconnect != NULL) {
- /* the callback may close the fd, so remove IO before that */
+ conn->callbacks->conn_disconnect != NULL) {
+ /* The callback may close the fd, so remove IO before that */
io_remove(&conn->conn.io);
- conn->callbacks->conn_disconnect(conn->context,
- reason);
+ conn->callbacks->conn_disconnect(conn->context, reason);
}
if (!conn->created_from_streams)
e_debug(conn->event, "Connection destroy");
- if (conn->callbacks != NULL &&
- conn->callbacks->conn_destroy != NULL)
+ if (conn->callbacks != NULL && conn->callbacks->conn_destroy != NULL)
conn->callbacks->conn_destroy(conn->context);
connection_deinit(&conn->conn);
} T_END;
va_end(args);
- /* send immediately */
+ /* Send immediately */
if (o_stream_is_corked(conn->conn.output)) {
o_stream_uncork(conn->conn.output);
o_stream_cork(conn->conn.output);
}
void smtp_server_connection_login(struct smtp_server_connection *conn,
- const char *username, const char *helo,
- const unsigned char *pdata, unsigned int pdata_len,
- bool ssl_secured)
+ const char *username, const char *helo,
+ const unsigned char *pdata,
+ unsigned int pdata_len, bool ssl_secured)
{
i_assert(!conn->started);
i_assert(conn->username == NULL);
i_stream_destroy(&conn->state.data_chain_input);
conn->state.data_chain = NULL;
- /* reset state */
+ /* Reset state */
i_zero(&conn->state);
smtp_server_connection_set_state(conn, SMTP_SERVER_STATE_READY, NULL);
}
proxy_data->timeout_secs = conn->proxy_timeout_secs;
}
-void smtp_server_connection_set_proxy_data(struct smtp_server_connection *conn,
+void smtp_server_connection_set_proxy_data(
+ struct smtp_server_connection *conn,
const struct smtp_proxy_data *proxy_data)
{
if (proxy_data->source_ip.family != 0)
conn->proxy_timeout_secs = proxy_data->timeout_secs;
if (conn->callbacks != NULL &&
- conn->callbacks->conn_proxy_data_updated != NULL) {
+ conn->callbacks->conn_proxy_data_updated != NULL) {
struct smtp_proxy_data full_data;
i_zero(&full_data);