smtp-server.h revision 9249c80276766fdc4d31fc3eebc22fba7d53d77c
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync#ifndef SMTP_SERVER_H
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync#define SMTP_SERVER_H
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync#include "smtp-common.h"
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync#include "smtp-command.h"
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync#include "smtp-params.h"
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_address;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_reply;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_command;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_helo_data;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_esmtp_param;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_cmd_ehlo;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_cmd_mail;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_cmd_ctx;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_command;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_reply;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync/*
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync * Types
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncenum smtp_server_state {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync SMTP_SERVER_STATE_GREETING = 0,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync SMTP_SERVER_STATE_XCLIENT,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync SMTP_SERVER_STATE_HELO,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync SMTP_SERVER_STATE_STARTTLS,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync SMTP_SERVER_STATE_AUTH,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync SMTP_SERVER_STATE_READY,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync SMTP_SERVER_STATE_MAIL_FROM,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync SMTP_SERVER_STATE_RCPT_TO,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync SMTP_SERVER_STATE_DATA,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync};
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncextern const char *const smtp_server_state_names[];
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_helo_data {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync const char *domain;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bool domain_valid:1; /* valid domain/literal specified */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bool old_smtp:1; /* client sent HELO rather than EHLO */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync};
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync/*
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync * Transaction
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_recipient {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_address *path;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_params_rcpt params;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync void *context;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync};
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncARRAY_DEFINE_TYPE(smtp_server_recipient, struct smtp_server_recipient *);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_transaction {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync pool_t pool;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_connection *conn;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync const char *id;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct timeval timestamp;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_address *mail_from;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_params_mail params;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync ARRAY_TYPE(smtp_server_recipient) rcpt_to;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync void *context;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync};
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncvoid smtp_server_transaction_fail_data(
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_transaction *trans,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *data_cmd,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync unsigned int status, const char *enh_code,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync const char *fmt, va_list args) ATTR_FORMAT(5, 0);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncvoid smtp_server_transaction_write_trace_record(string_t *str,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_transaction *trans);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync/*
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync * Callbacks
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_cmd_helo {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_helo_data helo;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bool first:1; /* this is the first */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bool changed:1; /* this EHLO/HELO/LHLO is the first or
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync different from a previous one */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync};
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_cmd_mail {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_address *path;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_params_mail params;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct timeval timestamp;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync};
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_cmd_rcpt {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_address *path;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_params_rcpt params;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* called once the recipient is definitively added to the transaction */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync void (*hook_finished)(struct smtp_server_cmd_ctx *cmd,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_transaction *trans,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_recipient *rcpt,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync unsigned int index);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync void *trans_context;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync};
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_cmd_auth {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync const char *sasl_mech;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync const char *initial_response;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync};
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstruct smtp_server_callbacks {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* Command callbacks:
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync These are used to override/implement the behavior of the various core
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync SMTP commands. Commands are handled asynchronously, which means that
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync the command is not necessarily finished when the callback ends. A
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync command is finished either when 1 is returned or a reply is submitted
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync for it. When a callback returns 0, the command implementation is
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync waiting for an external event and when it returns -1 an error
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync occurred. When 1 is returned, a default success reply is set if no
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync reply was submitted. Not submitting an error reply when -1 is
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync returned causes an assert fail. Except for RCPT and DATA, all these
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync callbacks are optional to implement; appropriate default behavior is
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync provided.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The SMTP server API takes care of transaction state checking.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync However, until all previous commands are handled, a transaction
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync command cannot rely on the transaction state being final. Use
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync cmd->hook_next to get notified when all previous commands are
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync finished and the current command is next in line to reply.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync If the implementation does not need asynchronous behavior, set
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync max_pipelined_commands=1 and don't return 0 from any command handler.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* HELO/EHLO/LHLO */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync int (*conn_cmd_helo)(void *conn_ctx,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *cmd,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_helo *data);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* STARTTLS */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync int (*conn_cmd_starttls)(void *conn_ctx,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *cmd);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* AUTH */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync int (*conn_cmd_auth)(void *conn_ctx,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *cmd,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_auth *data);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync int (*conn_cmd_auth_continue)(void *conn_ctx,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *cmd, const char *response);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* MAIL */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync int (*conn_cmd_mail)(void *conn_ctx,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *cmd,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_mail *data);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* RCPT */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync int (*conn_cmd_rcpt)(void *conn_ctx,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *cmd,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_rcpt *data);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* RSET */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync int (*conn_cmd_rset)(void *conn_ctx,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *cmd);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* DATA */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync int (*conn_cmd_data_begin)(void *conn_ctx,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *cmd,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_transaction *trans,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct istream *data_input);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync int (*conn_cmd_data_continue)(void *conn_ctx,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *cmd,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_transaction *trans);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* VRFY */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync int (*conn_cmd_vrfy)(void *conn_ctx,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync struct smtp_server_cmd_ctx *cmd,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync const char *param);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* NOOP */
int (*conn_cmd_noop)(void *conn_ctx,
struct smtp_server_cmd_ctx *cmd);
/* QUIT */
int (*conn_cmd_quit)(void *conn_ctx,
struct smtp_server_cmd_ctx *cmd);
/* XCLIENT */
void (*conn_cmd_xclient)(void *conn_ctx,
struct smtp_server_cmd_ctx *cmd,
struct smtp_proxy_data *data);
/* Command input callbacks:
These can be used to do stuff before and after a pipelined group of
commands is read.
*/
void (*conn_cmd_input_pre)(void *context);
void (*conn_cmd_input_post)(void *context);
/* Transaction events */
void (*conn_trans_free)(void *context,
struct smtp_server_transaction *trans);
/* Protocol state events */
void (*conn_state_changed)(void *context,
enum smtp_server_state newstate);
/* Proxy data */
void (*conn_proxy_data_updated)(void *conn_ctx,
const struct smtp_proxy_data *data);
/* Connection */
int (*conn_start_tls)(void *conn_ctx,
struct istream **input, struct ostream **output);
void (*conn_disconnect)(void *context, const char *reason);
void (*conn_destroy)(void *context);
/* Security */
bool (*conn_is_trusted)(void *context);
};
/*
* Server
*/
struct smtp_server_settings {
enum smtp_protocol protocol;
enum smtp_capability capabilities;
const char *hostname;
const char *login_greeting;
const char *rawlog_dir;
/* The maximum time in milliseconds a client is allowed to be idle
before it is disconnected. */
unsigned int max_client_idle_time_msecs;
/* maximum number of commands in pipeline per connection (default = 1)
*/
unsigned int max_pipelined_commands;
/* maximum number of sequential bad commands */
unsigned int max_bad_commands;
/* maximum number of recipients in a transaction
(0 means unlimited, which is the default) */
unsigned int max_recipients;
/* command limits */
struct smtp_command_limits command_limits;
/* accept these additional custom XCLIENT fields */
const char *const *xclient_extensions;
/* The kernel send/receive buffer sizes used for the connection sockets.
Configuring this is mainly useful for the test suite. The kernel
defaults are used when these settings are 0. */
size_t socket_send_buffer_size;
size_t socket_recv_buffer_size;
bool debug:1;
bool auth_optional:1;
bool tls_required:1;
bool rcpt_domain_optional:1;
bool param_extensions:1;
};
struct smtp_server_stats {
unsigned int command_count, reply_count;
uoff_t input, output;
};
struct smtp_server *smtp_server_init(const struct smtp_server_settings *set);
void smtp_server_deinit(struct smtp_server **_server);
/*
* Connection
*/
/* Create connection. It is still inactive and needs to be started with
one of the functions below. */
struct smtp_server_connection *
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,
const struct smtp_server_settings *set,
const struct smtp_server_callbacks *callbacks, void *context)
ATTR_NULL(4, 6, 8);
struct smtp_server_connection *
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,
const struct smtp_server_callbacks *callbacks, void *context)
ATTR_NULL(4, 6, 8);
void smtp_server_connection_ref(struct smtp_server_connection *conn);
bool smtp_server_connection_unref(struct smtp_server_connection **_conn);
/* Start the connection. Establishes SSL layer immediately if instructed,
and sends the greeting once the connection is ready for commands. */
void smtp_server_connection_start(struct smtp_server_connection *conn,
bool ssl_start);
/* Start the connection with state and data from login service */
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);
void smtp_server_connection_input_lock(struct smtp_server_connection *conn);
void smtp_server_connection_input_unlock(struct smtp_server_connection *conn);
void smtp_server_connection_set_streams(
struct smtp_server_connection *_conn,
struct istream *input, struct ostream *output);
void smtp_server_connection_close(struct smtp_server_connection **_conn,
const char *reason) ATTR_NULL(2);
void smtp_server_connection_terminate(struct smtp_server_connection **_conn,
const char *enh_code, const char *reason)
ATTR_NULL(3);
bool smtp_server_connection_data_check_state(struct smtp_server_cmd_ctx *cmd);
void smtp_server_connection_data_chunk_init(struct smtp_server_cmd_ctx *cmd);
int smtp_server_connection_data_chunk_add(struct smtp_server_cmd_ctx *cmd,
struct istream *chunk, uoff_t chunk_size, bool chunk_last,
bool client_input);
enum smtp_server_state
smtp_server_connection_get_state(struct smtp_server_connection *conn);
const char *
smtp_server_connection_get_security_string(struct smtp_server_connection *conn);
struct smtp_server_transaction *
smtp_server_connection_get_transaction(struct smtp_server_connection *conn);
const char *
smtp_server_connection_get_transaction_id(struct smtp_server_connection *conn);
const struct smtp_server_stats *
smtp_server_connection_get_stats(struct smtp_server_connection *conn);
void *smtp_server_connection_get_context(struct smtp_server_connection *conn)
ATTR_PURE;
enum smtp_protocol
smtp_server_connection_get_protocol(struct smtp_server_connection *conn)
ATTR_PURE;
const char *
smtp_server_connection_get_protocol_name(struct smtp_server_connection *conn);
struct smtp_server_helo_data *
smtp_server_connection_get_helo_data(struct smtp_server_connection *conn);
void smtp_server_connection_get_proxy_data(struct smtp_server_connection *conn,
struct smtp_proxy_data *proxy_data);
void smtp_server_connection_set_capabilities(
struct smtp_server_connection *conn, enum smtp_capability capabilities);
bool smtp_server_connection_is_ssl_secured(struct smtp_server_connection *conn);
bool smtp_server_connection_is_trusted(struct smtp_server_connection *conn);
/*
* Command
*/
enum smtp_server_command_flags {
SMTP_SERVER_CMD_FLAG_PRETLS = BIT(0),
SMTP_SERVER_CMD_FLAG_PREAUTH = BIT(1)
};
/* Commands are handled asynchronously, which means that the command is not
necessary finished when the start function ends. A command is finished
when a reply is submitted for it. Several command hooks are available to
get notified about events in the command's life cycle.
*/
typedef void smtp_server_cmd_input_callback_t(struct smtp_server_cmd_ctx *cmd);
typedef void smtp_server_cmd_start_func_t(struct smtp_server_cmd_ctx *cmd,
const char *params);
typedef void smtp_server_cmd_func_t(struct smtp_server_cmd_ctx *cmd);
struct smtp_server_cmd_ctx {
pool_t pool;
const char *name;
struct smtp_server *server;
struct smtp_server_connection *conn;
struct smtp_server_command *cmd;
/* public hooks */
/* next: command is next to reply but has not submittted all replies
yet */
smtp_server_cmd_func_t *hook_next;
/* replied: command has submitted all replies */
smtp_server_cmd_func_t *hook_replied;
/* completed: server is about to send last replies for this command */
smtp_server_cmd_func_t *hook_completed;
/* destroy: command is about to be destroyed */
smtp_server_cmd_func_t *hook_destroy;
void *context;
};
/* The core SMTP commands are pre-registered. Special connection callbacks are
provided for the core SMTP commands. Only use this command registration API
when custom/extension SMTP commands are required.
*/
void smtp_server_command_register(struct smtp_server *server, const char *name,
smtp_server_cmd_start_func_t *func,
enum smtp_server_command_flags);
void smtp_server_command_unregister(struct smtp_server *server,
const char *name);
void smtp_server_command_set_reply_count(struct smtp_server_command *cmd,
unsigned int count);
void smtp_server_command_fail(struct smtp_server_command *cmd,
unsigned int status, const char *enh_code,
const char *fmt, ...) ATTR_FORMAT(4, 5);
struct smtp_server_reply *
smtp_server_command_get_reply(struct smtp_server_command *cmd,
unsigned int idx);
bool smtp_server_command_reply_status_equals(struct smtp_server_command *cmd,
unsigned int status);
bool smtp_server_command_is_replied(struct smtp_server_command *cmd);
bool smtp_server_command_replied_success(struct smtp_server_command *cmd);
void smtp_server_command_input_lock(struct smtp_server_cmd_ctx *cmd);
void smtp_server_command_input_unlock(struct smtp_server_cmd_ctx *cmd);
void smtp_server_command_input_capture(struct smtp_server_cmd_ctx *cmd,
smtp_server_cmd_input_callback_t *callback);
/* AUTH */
void smtp_server_cmd_auth_send_challenge(struct smtp_server_cmd_ctx *cmd,
const char *challenge);
void smtp_server_cmd_auth_success(struct smtp_server_cmd_ctx *cmd,
const char *username, const char *success_msg)
ATTR_NULL(3);
/*
* Reply
*/
struct smtp_server_reply *
smtp_server_reply_create_index(struct smtp_server_command *cmd,
unsigned int index, unsigned int status, const char *enh_code)
ATTR_NULL(3);
struct smtp_server_reply *
smtp_server_reply_create(struct smtp_server_command *cmd,
unsigned int status, const char *enh_code) ATTR_NULL(3);
struct smtp_server_reply *
smtp_server_reply_create_forward(struct smtp_server_command *cmd,
unsigned int index, const struct smtp_reply *from);
void smtp_server_reply_add_text(struct smtp_server_reply *reply,
const char *line);
void smtp_server_reply_submit(struct smtp_server_reply *reply);
/* Submit a reply for the command at the specified index (> 0 only if more than
a single reply is expected). */
void smtp_server_reply_indexv(struct smtp_server_cmd_ctx *_cmd,
unsigned int index, unsigned int status, const char *enh_code,
const char *fmt, va_list args) ATTR_FORMAT(5, 0);
void smtp_server_reply_index(struct smtp_server_cmd_ctx *_cmd,
unsigned int index, unsigned int status, const char *enh_code,
const char *fmt, ...) ATTR_FORMAT(5, 6);
/* Submit the reply for the specified command. */
void smtp_server_reply(struct smtp_server_cmd_ctx *_cmd,
unsigned int status, const char *enh_code, const char *fmt, ...)
ATTR_FORMAT(4, 5);
/* Forward a reply for the command at the specified index (> 0 only if more
than a single reply is expected). */
void smtp_server_reply_index_forward(struct smtp_server_cmd_ctx *cmd,
unsigned int index, const struct smtp_reply *from);
/* Forward the reply for the specified command. */
void smtp_server_reply_forward(struct smtp_server_cmd_ctx *cmd,
const struct smtp_reply *from);
/* Submit the same message for all expected replies for this command. */
void smtp_server_reply_all(struct smtp_server_cmd_ctx *_cmd,
unsigned int status, const char *enh_code,
const char *fmt, ...) ATTR_FORMAT(4, 5);
/* Submit and send the same message for all expected replies for this command
early; i.e., no matter whether all command data is received completely. */
void smtp_server_reply_early(struct smtp_server_cmd_ctx *_cmd,
unsigned int status, const char *enh_code,
const char *fmt, ...) ATTR_FORMAT(4, 5);
/* Reply the command with a 221 bye message */
void smtp_server_reply_quit(struct smtp_server_cmd_ctx *_cmd);
void smtp_server_switch_ioloop(struct smtp_server *server);
/* EHLO */
struct smtp_server_reply *
smtp_server_reply_create_ehlo(struct smtp_server_command *cmd);
void smtp_server_reply_ehlo_add(struct smtp_server_reply *reply,
const char *keyword);
void smtp_server_reply_ehlo_add_param(struct smtp_server_reply *reply,
const char *keyword, const char *param_fmt, ...) ATTR_FORMAT(3, 4);
void smtp_server_reply_ehlo_add_xclient(struct smtp_server_reply *reply);
#endif