client-common.h revision 660b99a7059824676b2b8d6f79b8e15d47df25a2
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen#ifndef CLIENT_COMMON_H
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen#define CLIENT_COMMON_H
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
c3a636e4c9ae776e0eed06b6d7ad1ccfb6003afdTimo Sirainen#include "network.h"
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen#include "login-proxy.h"
fa2a11210f20fb8998ed656f75e163191c8047e6Timo Sirainen#include "sasl-server.h"
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* max. size of input buffer. this means:
abf015c9682f0f723db87a7c97bc284ef814818fTimo Sirainen
fa2a11210f20fb8998ed656f75e163191c8047e6Timo Sirainen IMAP: Max. length of a single parameter
fa2a11210f20fb8998ed656f75e163191c8047e6Timo Sirainen POP3: Max. length of a command line (spec says 512 would be enough)
fa2a11210f20fb8998ed656f75e163191c8047e6Timo Sirainen*/
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen#define LOGIN_MAX_INBUF_SIZE 1024
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* max. size of output buffer. if it gets full, the client is disconnected.
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen SASL authentication gives the largest output. */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen#define LOGIN_MAX_OUTBUF_SIZE 4096
5d03d9f439e41c90215a3c938ffebe4c2a8ae257Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* Disconnect client after idling this many milliseconds */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen#define CLIENT_LOGIN_IDLE_TIMEOUT_MSECS (3*60*1000)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen#define AUTH_SERVER_WAITING_MSG \
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "Waiting for authentication process to respond.."
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen#define AUTH_MASTER_WAITING_MSG \
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "Waiting for authentication master process to respond.."
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenenum client_cmd_reply {
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen CLIENT_CMD_REPLY_OK,
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainen CLIENT_CMD_REPLY_AUTH_FAILED,
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainen CLIENT_CMD_REPLY_AUTHZ_FAILED,
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainen CLIENT_CMD_REPLY_AUTH_FAIL_TEMP,
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainen CLIENT_CMD_REPLY_AUTH_FAIL_REASON,
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainen CLIENT_CMD_REPLY_AUTH_FAIL_NOSSL,
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainen CLIENT_CMD_REPLY_BAD,
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainen CLIENT_CMD_REPLY_BYE,
c3a636e4c9ae776e0eed06b6d7ad1ccfb6003afdTimo Sirainen CLIENT_CMD_REPLY_STATUS,
c3a636e4c9ae776e0eed06b6d7ad1ccfb6003afdTimo Sirainen CLIENT_CMD_REPLY_STATUS_BAD
c3a636e4c9ae776e0eed06b6d7ad1ccfb6003afdTimo Sirainen};
c3a636e4c9ae776e0eed06b6d7ad1ccfb6003afdTimo Sirainen
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainenstruct client_auth_reply {
5d03d9f439e41c90215a3c938ffebe4c2a8ae257Timo Sirainen const char *master_user, *reason;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen /* for proxying */
fa2433aebcf3fccfa30ca9eed9b1a9166cf92ee2Timo Sirainen const char *host, *destuser, *password;
fa2433aebcf3fccfa30ca9eed9b1a9166cf92ee2Timo Sirainen unsigned int port;
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen unsigned int proxy_timeout_msecs;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen enum login_proxy_ssl_flags ssl_flags;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
5d03d9f439e41c90215a3c938ffebe4c2a8ae257Timo Sirainen unsigned int proxy:1;
5d03d9f439e41c90215a3c938ffebe4c2a8ae257Timo Sirainen unsigned int temp:1;
5d03d9f439e41c90215a3c938ffebe4c2a8ae257Timo Sirainen unsigned int nologin:1;
5d03d9f439e41c90215a3c938ffebe4c2a8ae257Timo Sirainen unsigned int authz_failure:1;
5d03d9f439e41c90215a3c938ffebe4c2a8ae257Timo Sirainen};
5d03d9f439e41c90215a3c938ffebe4c2a8ae257Timo Sirainen
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainenstruct client_vfuncs {
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct client *(*alloc)(pool_t pool);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen void (*create)(struct client *client, void **other_sets);
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen void (*destroy)(struct client *client);
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen void (*send_greeting)(struct client *client);
d1fff80640050631b06bfab904a34b2ad24601e8Timo Sirainen void (*starttls)(struct client *client);
d1fff80640050631b06bfab904a34b2ad24601e8Timo Sirainen void (*input)(struct client *client);
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen void (*send_line)(struct client *client, enum client_cmd_reply reply,
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen const char *text);
2fb9ae42f9e36388ec6db24188b9108434043fd0Timo Sirainen bool (*auth_handle_reply)(struct client *client,
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen const struct client_auth_reply *reply);
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen void (*auth_send_challenge)(struct client *client, const char *data);
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen int (*auth_parse_response)(struct client *client);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen void (*proxy_reset)(struct client *client);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen int (*proxy_parse_line)(struct client *client, const char *line);
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen};
2eb2cf8eeb763bd5ca9b6848dce32f0303e88ec1Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenstruct client {
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct client *prev, *next;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen pool_t pool;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct client_vfuncs v;
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen time_t created;
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen int refcount;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct ip_addr local_ip;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct ip_addr ip;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int local_port, remote_port;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct ssl_proxy *ssl_proxy;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen const struct login_settings *set;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen int fd;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct istream *input;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct ostream *output;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct io *io;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct timeout *to_auth_waiting;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct timeout *to_idle_disconnect;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned char *master_data_prefix;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int master_data_prefix_len;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct login_proxy *login_proxy;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen char *proxy_user, *proxy_master_user, *proxy_password;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen char *auth_mech_name;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen struct auth_client_request *auth_request;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen string_t *auth_response;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int master_tag;
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen sasl_server_callback_t *sasl_callback;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen unsigned int bad_counter;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int auth_attempts;
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen pid_t mail_pid;
b34fdb68d376d85b4880da4a4bdf67ae726a381bTimo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen char *virtual_user;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int destroyed:1;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int input_blocked:1;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int login_success:1;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int greeting_sent:1;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int starttls:1;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int tls:1;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int secured:1;
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen unsigned int trusted:1;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int authenticating:1;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int auth_tried_disabled_plaintext:1;
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen unsigned int auth_tried_unsupported_mech:1;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int auth_try_aborted:1;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int auth_initializing:1;
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen /* ... */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen};
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainenextern struct client *clients;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenextern struct client_vfuncs client_vfuncs;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenstruct client *
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenclient_create(int fd, bool ssl, pool_t pool,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen const struct login_settings *set, void **other_sets,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen const struct ip_addr *local_ip, const struct ip_addr *remote_ip);
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainenvoid client_destroy(struct client *client, const char *reason);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid client_destroy_success(struct client *client, const char *reason);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid client_destroy_internal_failure(struct client *client);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainenvoid client_ref(struct client *client);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenbool client_unref(struct client **client);
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainenvoid client_cmd_starttls(struct client *client);
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainenunsigned int clients_get_count(void) ATTR_PURE;
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainen
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainenvoid client_set_title(struct client *client);
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainenvoid client_log(struct client *client, const char *msg);
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainenvoid client_log_err(struct client *client, const char *msg);
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainenconst char *client_get_extra_disconnect_reason(struct client *client);
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainenbool client_is_trusted(struct client *client);
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainenvoid client_auth_failed(struct client *client);
542e28b384a6b26695f3e8de38fd5727d06f3333Timo Sirainen
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainenbool client_read(struct client *client);
714d59900e44e6e2dd744bd7b76862e1d11a4f61Timo Sirainenvoid client_input(struct client *client);
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainen
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainenvoid client_send_line(struct client *client, enum client_cmd_reply reply,
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainen const char *text);
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainenvoid client_send_raw_data(struct client *client, const void *data, size_t size);
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainenvoid client_send_raw(struct client *client, const char *data);
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainen
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainenvoid client_set_auth_waiting(struct client *client);
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainenvoid client_auth_send_challenge(struct client *client, const char *data);
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainenint client_auth_parse_response(struct client *client);
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainenint client_auth_begin(struct client *client, const char *mech_name,
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainen const char *init_resp);
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainenbool client_check_plaintext_auth(struct client *client, bool pass_sent);
18c209a06941ef583b08b173dadfbe4571995bf9Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid client_proxy_finish_destroy_client(struct client *client);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid client_proxy_log_failure(struct client *client, const char *line);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid client_proxy_failed(struct client *client, bool send_line);
367e28a16854ee9f7247b2518f36f5e9163fcc10Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid clients_notify_auth_connected(void);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid client_destroy_oldest(void);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid clients_destroy_all(void);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid clients_init(void);
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainenvoid clients_deinit(void);
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen#endif
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen