client-common.h revision a75907609d7c410c9e17beedfafbf28b4439fa8a
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch#ifndef CLIENT_COMMON_H
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#define CLIENT_COMMON_H
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen#include "network.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "login-proxy.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "sasl-server.h"
7af70f7646264a2f52b361f9ca78f08681acc4e2Stephan Bosch
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainen/* max. size of input buffer. this means:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch IMAP: Max. length of a single parameter
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch POP3: Max. length of a command line (spec says 512 would be enough)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch*/
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#define LOGIN_MAX_INBUF_SIZE 1024
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch/* max. size of output buffer. if it gets full, the client is disconnected.
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch SASL authentication gives the largest output. */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#define LOGIN_MAX_OUTBUF_SIZE 4096
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch/* Disconnect client after idling this many milliseconds */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#define CLIENT_LOGIN_IDLE_TIMEOUT_MSECS (3*60*1000)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#define AUTH_SERVER_WAITING_MSG \
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Waiting for authentication process to respond.."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#define AUTH_MASTER_WAITING_MSG \
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Waiting for authentication master process to respond.."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainenenum client_cmd_reply {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch CLIENT_CMD_REPLY_OK,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch CLIENT_CMD_REPLY_AUTH_FAILED,
6a90041707f1290c8970a3bacb0f8f928aeaaba6Stephan Bosch CLIENT_CMD_REPLY_AUTHZ_FAILED,
6a90041707f1290c8970a3bacb0f8f928aeaaba6Stephan Bosch CLIENT_CMD_REPLY_AUTH_FAIL_TEMP,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch CLIENT_CMD_REPLY_AUTH_FAIL_REASON,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch CLIENT_CMD_REPLY_AUTH_FAIL_NOSSL,
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch CLIENT_CMD_REPLY_BAD,
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch CLIENT_CMD_REPLY_BYE,
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch CLIENT_CMD_REPLY_STATUS,
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch CLIENT_CMD_REPLY_STATUS_BAD
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch};
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Boschstruct client_auth_reply {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const char *master_user, *reason;
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch /* for proxying */
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch const char *host, *destuser, *password;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch unsigned int port;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int proxy_timeout_msecs;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch enum login_proxy_ssl_flags ssl_flags;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int proxy:1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int temp:1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int nologin:1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int nodelay:1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int authz_failure:1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch};
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
da300472555d9afdb0bcb767456f731cf5c2f6aaStephan Boschstruct client_vfuncs {
f9d2a1f21ad65262bc630f0834d7eead06a1bac3Timo Sirainen struct client *(*alloc)(pool_t pool);
f9d2a1f21ad65262bc630f0834d7eead06a1bac3Timo Sirainen void (*create)(struct client *client);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch void (*destroy)(struct client *client);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch void (*send_greeting)(struct client *client);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch void (*starttls)(struct client *client);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch void (*input)(struct client *client);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch void (*send_line)(struct client *client, enum client_cmd_reply reply,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const char *text);
6dad0888fcec8372f230941c70d8940b8c203b32Stephan Bosch bool (*auth_handle_reply)(struct client *client,
6a90041707f1290c8970a3bacb0f8f928aeaaba6Stephan Bosch const struct client_auth_reply *reply);
6a90041707f1290c8970a3bacb0f8f928aeaaba6Stephan Bosch void (*auth_send_challenge)(struct client *client, const char *data);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch int (*auth_parse_response)(struct client *client);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch void (*proxy_reset)(struct client *client);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch int (*proxy_parse_line)(struct client *client, const char *line);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch};
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainen
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainenstruct client {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct client *prev, *next;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch pool_t pool;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct client_vfuncs v;
1bc12a53ddc6696bb209fb79d7cc66262d2ea621Timo Sirainen
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch time_t created;
1bc12a53ddc6696bb209fb79d7cc66262d2ea621Timo Sirainen int refcount;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
6a90041707f1290c8970a3bacb0f8f928aeaaba6Stephan Bosch struct ip_addr local_ip;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct ip_addr ip;
1c2f122ae93d3316f6746f255f6659b510527cc8Stephan Bosch unsigned int local_port, remote_port;
1c2f122ae93d3316f6746f255f6659b510527cc8Stephan Bosch struct ssl_proxy *ssl_proxy;
1c2f122ae93d3316f6746f255f6659b510527cc8Stephan Bosch const struct login_settings *set;
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainen
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch int fd;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct istream *input;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct ostream *output;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct io *io;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct timeout *to_authfail_delay, *to_auth_waiting;
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainen struct timeout *to_idle_disconnect;
1ec26e0b70ac7f8a4e3dfbc59aa77f572651d5aeStephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned char *master_data_prefix;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int master_data_prefix_len;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
6a90041707f1290c8970a3bacb0f8f928aeaaba6Stephan Bosch struct login_proxy *login_proxy;
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainen char *proxy_user, *proxy_master_user, *proxy_password;
1bc12a53ddc6696bb209fb79d7cc66262d2ea621Timo Sirainen
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch char *auth_mech_name;
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainen struct auth_request *auth_request;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch string_t *auth_response;
1ec26e0b70ac7f8a4e3dfbc59aa77f572651d5aeStephan Bosch
6a90041707f1290c8970a3bacb0f8f928aeaaba6Stephan Bosch unsigned int master_tag;
1ec26e0b70ac7f8a4e3dfbc59aa77f572651d5aeStephan Bosch sasl_server_callback_t *sasl_callback;
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainen
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int bad_counter;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int auth_attempts;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch pid_t mail_pid;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch char *virtual_user;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int destroyed:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int input_blocked:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int login_success:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int greeting_sent:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int starttls:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int tls:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int secured:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int trusted:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int proxying:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int authenticating:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int auth_tried_disabled_plaintext:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen unsigned int auth_initializing:1;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen /* ... */
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen};
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenextern struct client *clients;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenextern struct client_vfuncs client_vfuncs;
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenstruct client *client_create(int fd, bool ssl, pool_t pool,
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen const struct login_settings *set,
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen const struct ip_addr *local_ip,
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen const struct ip_addr *remote_ip);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenvoid client_destroy(struct client *client, const char *reason);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenvoid client_destroy_success(struct client *client, const char *reason);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenvoid client_destroy_internal_failure(struct client *client);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenvoid client_ref(struct client *client);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenbool client_unref(struct client *client);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen
7384b4e78eaab44693c985192276e31322155e32Stephan Boschvoid client_cmd_starttls(struct client *client);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschunsigned int clients_get_count(void) ATTR_PURE;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainenvoid client_set_title(struct client *client);
7384b4e78eaab44693c985192276e31322155e32Stephan Boschvoid client_log(struct client *client, const char *msg);
7384b4e78eaab44693c985192276e31322155e32Stephan Boschvoid client_log_err(struct client *client, const char *msg);
6ee9ce5ed955a1283dc22ad28980bf9cc23d4c4eStephan Boschconst char *client_get_extra_disconnect_reason(struct client *client);
6ee9ce5ed955a1283dc22ad28980bf9cc23d4c4eStephan Boschbool client_is_trusted(struct client *client);
6ee9ce5ed955a1283dc22ad28980bf9cc23d4c4eStephan Boschvoid client_auth_failed(struct client *client, bool nodelay);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschbool client_read(struct client *client);
7384b4e78eaab44693c985192276e31322155e32Stephan Boschvoid client_input(struct client *client);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid client_send_line(struct client *client, enum client_cmd_reply reply,
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch const char *text);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid client_send_raw_data(struct client *client, const void *data, size_t size);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid client_send_raw(struct client *client, const char *data);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid client_set_auth_waiting(struct client *client);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenvoid client_auth_send_challenge(struct client *client, const char *data);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenint client_auth_parse_response(struct client *client);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenint client_auth_begin(struct client *client, const char *mech_name,
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch const char *init_resp);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschbool client_check_plaintext_auth(struct client *client, bool pass_sent);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid client_proxy_finish_destroy_client(struct client *client);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid client_proxy_log_failure(struct client *client, const char *line);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid client_proxy_failed(struct client *client, bool send_line);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid clients_notify_auth_connected(void);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid client_destroy_oldest(void);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid clients_destroy_all(void);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid clients_init(void);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Boschvoid clients_deinit(void);
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch#endif
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch