smtp-server-private.h revision 5d5ad796fc34170333bb52c05614425a0dd7e182
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#ifndef SMTP_SERVER_PRIVATE_H
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#define SMTP_SERVER_PRIVATE_H
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#include "connection.h"
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#include "smtp-server.h"
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#define SMTP_SERVER_COMMAND_POOL_MAX (8 * 1024)
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#define SMTP_SERVER_DEFAULT_MAX_COMMAND_LINE (4 * 1024)
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#define SMTP_SERVER_DEFAULT_MAX_BAD_COMMANDS 10
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen#define SMTP_SERVER_DEFAULT_CAPABILITIES \
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen (SMTP_CAPABILITY_SIZE | SMTP_CAPABILITY_ENHANCEDSTATUSCODES | \
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen SMTP_CAPABILITY_8BITMIME | SMTP_CAPABILITY_CHUNKING)
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstruct smtp_server_reply;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstruct smtp_server_command;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstruct smtp_server_connection;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo SirainenARRAY_DEFINE_TYPE(smtp_server_reply, struct smtp_server_reply);
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenenum smtp_server_command_state {
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* New command; callback to command start handler executing. */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen SMTP_SERVER_COMMAND_STATE_NEW = 0,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* This command is being processed; command data is fully read, but no
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen reply is yet submitted */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen SMTP_SERVER_COMMAND_STATE_PROCESSING,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* A reply is submitted for this command. If not all command data was
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen read by the handler, it is first skipped on the input. If this is a
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen multi-reply command (LMTP->DATA), not all replies may be submitted
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen yet. */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen SMTP_SERVER_COMMAND_STATE_SUBMITTED_REPLY,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* Request is ready for sending reply; a reply is submitted and the
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen command payload is fully read. If this is a multi-reply command
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen (LMTP->DATA), not all replies may be submitted yet. In that case the
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen command state goes back to PROCESSING once the all submitted replies
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen are sent. */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen SMTP_SERVER_COMMAND_STATE_READY_TO_REPLY,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* The reply for the command is sent */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen SMTP_SERVER_COMMAND_STATE_FINISHED,
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* Request is aborted; still lingering due to references */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen SMTP_SERVER_COMMAND_STATE_ABORTED
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen};
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstruct smtp_server_reply {
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct smtp_server_command *command;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int index;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int status;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen const char *status_prefix;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen string_t *text;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen size_t last_line;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen bool submitted:1;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen bool sent:1;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen};
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstruct smtp_server_command_reg {
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen const char *name;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen enum smtp_server_command_flags flags;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen smtp_server_cmd_start_func_t *func;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen};
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstruct smtp_server_command {
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct smtp_server_cmd_ctx context;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen const struct smtp_server_command_reg *reg;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int refcount;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen enum smtp_server_command_state state;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct smtp_server_command *prev, *next;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen ARRAY_TYPE(smtp_server_reply) replies;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int replies_expected;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int replies_submitted;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* private hooks */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* next: command is next to reply but has not submittted all replies yet */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen smtp_server_cmd_func_t *hook_next;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* replied: command has submitted all replies */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen smtp_server_cmd_func_t *hook_replied;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* completed: server is about to send last replies for this command */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen smtp_server_cmd_func_t *hook_completed;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* destroy: command is about to be destroyed */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen smtp_server_cmd_func_t *hook_destroy;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen /* private context data */
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen void *data;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen bool input_locked:1;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen bool input_captured:1;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen bool reply_early:1;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen};
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstruct smtp_server_state_data {
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen enum smtp_server_state state;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen time_t timestamp;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int pending_mail_cmds, pending_rcpt_cmds;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct smtp_server_transaction *trans;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct istream *data_input, *data_chain_input;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct istream_chain *data_chain;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int data_chunks;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen bool data_failed:1;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen};
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainenstruct smtp_server_connection {
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct connection conn;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct smtp_server *server;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen pool_t pool;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int refcount;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct smtp_server_settings set;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen const struct smtp_server_callbacks *callbacks;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen void *context;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int socket_family;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct ip_addr remote_ip;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen in_port_t remote_port;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen pid_t remote_pid;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen uid_t remote_uid;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen enum smtp_proxy_protocol proxy_proto;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int proxy_ttl_plus_1;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int proxy_timeout_secs;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct smtp_server_helo_data helo, *pending_helo;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen char *helo_domain, *helo_login, *username;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int id;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct timeout *to_idle;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct istream *raw_input;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct ostream *raw_output;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct ssl_iostream *ssl_iostream;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct smtp_command_parser *smtp_parser;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen struct smtp_server_command *command_queue_head, *command_queue_tail;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int command_queue_count;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen unsigned int bad_counter;
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen
6509aa7306649042e828fef9e2f280f4535ea137Timo Sirainen char *disconnect_reason;
struct smtp_server_state_data state;
struct smtp_server_stats stats;
bool started:1;
bool halted:1;
bool ssl_start:1;
bool ssl_secured:1;
bool authenticated:1;
bool created_from_streams:1;
bool corked:1;
bool disconnected:1;
bool closing:1;
bool closed:1;
bool input_broken:1;
bool input_locked:1;
bool handling_input:1;
bool rawlog_checked:1;
bool rawlog_enabled:1;
};
struct smtp_server {
pool_t pool;
struct smtp_server_settings set;
struct ioloop *ioloop;
ARRAY(struct smtp_server_command_reg) commands_reg;
struct connection_list *conn_list;
bool commands_unsorted:1;
};
static inline const char *
smtp_server_command_label(struct smtp_server_command *cmd)
{
if (cmd->context.name == NULL)
return "[INVALID]";
return cmd->context.name;
}
static inline const char *
smtp_server_connection_label(struct smtp_server_connection *conn)
{
return conn->conn.name;
}
bool smtp_server_connection_pending_command_data(
struct smtp_server_connection *conn);
/*
* Reply
*/
void smtp_server_reply_free(struct smtp_server_command *cmd);
int smtp_server_reply_send(struct smtp_server_reply *resp,
const char **error_r);
int smtp_server_reply_send_more(struct smtp_server_reply *resp,
const char **error_r);
const char *smtp_server_reply_get_one_line(struct smtp_server_reply *reply);
/*
* Command
*/
void smtp_server_commands_init(struct smtp_server *server);
void smtp_server_command_debug(struct smtp_server_cmd_ctx *cmd,
const char *format, ...) ATTR_FORMAT(2, 3);
struct smtp_server_command *
smtp_server_command_alloc(struct smtp_server_connection *conn);
struct smtp_server_command *
smtp_server_command_new(struct smtp_server_connection *conn,
const char *name, const char *params);
void smtp_server_command_ref(struct smtp_server_command *cmd);
bool smtp_server_command_unref(struct smtp_server_command **_cmd);
void smtp_server_command_abort(struct smtp_server_command **_cmd);
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);
static inline bool
smtp_server_command_is_complete(struct smtp_server_command *cmd)
{
struct smtp_server_connection *conn = cmd->context.conn;
return (conn->input_broken || (cmd->next != NULL) || cmd->reply_early ||
!smtp_server_connection_pending_command_data(conn));
}
void smtp_server_cmd_ehlo(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_helo(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_xclient(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_starttls(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_auth(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_mail(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_rcpt(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_data(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_bdat(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_rset(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_noop(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_vrfy(struct smtp_server_cmd_ctx *cmd,
const char *params);
void smtp_server_cmd_quit(struct smtp_server_cmd_ctx *cmd,
const char *params);
/*
* Connection
*/
typedef void smtp_server_input_callback_t(void *context);
void smtp_server_connection_debug(struct smtp_server_connection *conn,
const char *format, ...) ATTR_FORMAT(2, 3);
void smtp_server_connection_error(struct smtp_server_connection *conn,
const char *format, ...) ATTR_FORMAT(2, 3);
struct connection_list *smtp_server_connection_list_init(void);
void smtp_server_connection_switch_ioloop(struct smtp_server_connection *conn);
void smtp_server_connection_trigger_output(struct smtp_server_connection *conn);
bool smtp_server_connection_pending_payload(struct smtp_server_connection *conn);
void smtp_server_connection_cork(struct smtp_server_connection *conn);
void smtp_server_connection_uncork(struct smtp_server_connection *conn);
void smtp_server_connection_input_halt(struct smtp_server_connection *conn);
void smtp_server_connection_input_resume(struct smtp_server_connection *conn);
void smtp_server_connection_input_capture(
struct smtp_server_connection *conn,
smtp_server_input_callback_t *callback, void *context);
#define smtp_server_connection_input_capture(conn, callback, context) \
smtp_server_connection_input_capture(conn + \
CALLBACK_TYPECHECK(callback, void (*)(typeof(context))), \
(smtp_server_input_callback_t *)callback, context)
void smtp_server_connection_timeout_stop(struct smtp_server_connection *conn);
void smtp_server_connection_timeout_start(struct smtp_server_connection *conn);
void smtp_server_connection_timeout_reset(struct smtp_server_connection *conn);
void smtp_server_connection_send_line(struct smtp_server_connection *conn,
const char *fmt, ...) ATTR_FORMAT(2, 3);
void smtp_server_connection_reply_immediate(
struct smtp_server_connection *conn, unsigned int status,
const char *fmt, ...) ATTR_FORMAT(3, 4);
void smtp_server_connection_reset_state(struct smtp_server_connection *conn);
void smtp_server_connection_set_state(struct smtp_server_connection *conn,
enum smtp_server_state state);
int smtp_server_connection_ssl_init(struct smtp_server_connection *conn);
void smtp_server_connection_clear(struct smtp_server_connection *conn);
struct smtp_server_transaction *
smtp_server_connection_get_transaction(struct smtp_server_connection *conn);
void smtp_server_connection_set_proxy_data(struct smtp_server_connection *conn,
const struct smtp_proxy_data *proxy_data);
/*
* Transaction
*/
struct smtp_server_transaction *
smtp_server_transaction_create(struct smtp_server_connection *conn,
const struct smtp_address *mail_from,
const struct smtp_params_mail *params,
const struct timeval *timestamp);
void smtp_server_transaction_free(struct smtp_server_transaction **_trans);
struct smtp_server_recipient *
smtp_server_transaction_add_rcpt(struct smtp_server_transaction *trans,
const struct smtp_address *rcpt_to,
const struct smtp_params_rcpt *params);
bool smtp_server_transaction_has_rcpt(struct smtp_server_transaction *trans);
unsigned int
smtp_server_transaction_rcpt_count(struct smtp_server_transaction *trans);
#endif