client.c revision 8372fc7efb6d64dff2e5f55fb4a3822c56869cfe
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2002-2008 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "common.h"
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#include "buffer.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "ioloop.h"
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#include "istream.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "ostream.h"
89b548af722113acb5d63dfffb44423cb60f91e4Timo Sirainen#include "process-title.h"
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen#include "safe-memset.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "str.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "strescape.h"
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen#include "imap-parser.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "imap-id.h"
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen#include "client.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "client-authenticate.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "auth-client.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "ssl-proxy.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "imap-proxy.h"
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen#include <stdlib.h>
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen/* max. size of output buffer. if it gets full, the client is disconnected.
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen SASL authentication gives the largest output. */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen#define MAX_OUTBUF_SIZE 4096
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen/* maximum length for IMAP command line. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define MAX_IMAP_LINE 8192
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Disconnect client when it sends too many bad commands */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define CLIENT_MAX_BAD_COMMANDS 10
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* When max. number of simultaneous connections is reached, few of the
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen oldest connections are disconnected. Since we have to go through all of the
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen clients, it's faster if we disconnect multiple clients. */
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen#define CLIENT_DESTROY_OLDEST_COUNT 16
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen/* If we've been waiting auth server to respond for over this many milliseconds,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen send a "waiting" message. */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen#define AUTH_WAITING_TIMEOUT_MSECS (30*1000)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen#if CLIENT_LOGIN_IDLE_TIMEOUT_MSECS >= AUTH_REQUEST_TIMEOUT*1000
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# error client idle timeout must be smaller than authentication timeout
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen#endif
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen#define AUTH_WAITING_MSG \
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "* OK Waiting for authentication process to respond.."
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenconst char *login_protocol = "IMAP";
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenconst char *capability_string = CAPABILITY_STRING;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainenstatic void client_set_title(struct imap_client *client)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const char *addr;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!verbose_proctitle || !process_per_connection)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen addr = net_ip2addr(&client->common.ip);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen if (addr == NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen addr = "??";
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen process_title_set(t_strdup_printf(client->common.tls ?
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen "[%s TLS]" : "[%s]", addr));
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void client_open_streams(struct imap_client *client, int fd)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen{
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen client->common.input =
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen i_stream_create_fd(fd, LOGIN_MAX_INBUF_SIZE, FALSE);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen client->output = o_stream_create_fd(fd, MAX_OUTBUF_SIZE, FALSE);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client->parser = imap_parser_create(client->common.input,
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen client->output, MAX_IMAP_LINE);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Skip incoming data until newline is found,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen returns TRUE if newline was found. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenbool client_skip_line(struct imap_client *client)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen const unsigned char *data;
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen size_t i, data_size;
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen data = i_stream_get_data(client->common.input, &data_size);
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen for (i = 0; i < data_size; i++) {
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen if (data[i] == '\n') {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen i_stream_skip(client->common.input, i+1);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return TRUE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen return FALSE;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen}
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainenstatic const char *get_capability(struct imap_client *client, bool full)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen{
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen const char *auths;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen auths = client_authenticate_get_capabilities(client->common.secured);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return t_strconcat(full ? capability_string : CAPABILITY_BANNER_STRING,
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen (ssl_initialized && !client->common.tls) ?
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen " STARTTLS" : "",
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen disable_plaintext_auth && !client->common.secured ?
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen " LOGINDISABLED" : "", auths, NULL);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen}
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenstatic int cmd_capability(struct imap_client *client)
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen{
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen client_send_line(client, t_strconcat(
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "* CAPABILITY ", get_capability(client, TRUE), NULL));
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen client_send_tagline(client, "OK Capability completed.");
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return 1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenstatic void client_start_tls(struct imap_client *client)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen{
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen int fd_ssl;
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen client_ref(client);
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen connection_queue_add(1);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (!client_unref(client) || client->destroyed)
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen return;
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen fd_ssl = ssl_proxy_new(client->common.fd, &client->common.ip,
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen &client->common.proxy);
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen if (fd_ssl == -1) {
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen client_send_line(client, "* BYE TLS initialization failed.");
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen client_destroy(client,
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen "Disconnected: TLS initialization failed.");
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen return;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen client->common.tls = TRUE;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen client->common.secured = TRUE;
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen client_set_title(client);
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen client->common.fd = fd_ssl;
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen i_stream_unref(&client->common.input);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen o_stream_unref(&client->output);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen imap_parser_destroy(&client->parser);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen /* CRLF is lost from buffer when streams are reopened. */
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen client->skip_line = FALSE;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen client_open_streams(client, fd_ssl);
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen client->io = io_add(client->common.fd, IO_READ, client_input, client);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen}
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenstatic int client_output_starttls(struct imap_client *client)
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen{
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen int ret;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if ((ret = o_stream_flush(client->output)) < 0) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen client_destroy(client, "Disconnected");
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen return 1;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (ret > 0) {
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen o_stream_unset_flush_callback(client->output);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen client_start_tls(client);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen return 1;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen}
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenstatic int cmd_starttls(struct imap_client *client)
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen{
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen if (client->common.tls) {
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen client_send_tagline(client, "BAD TLS is already active.");
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen return 1;
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen }
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (!ssl_initialized) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen client_send_tagline(client, "BAD TLS support isn't enabled.");
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen return 1;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen /* remove input handler, SSL proxy gives us a new fd. we also have to
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen remove it in case we have to wait for buffer to be flushed */
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (client->io != NULL)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen io_remove(&client->io);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen client_send_tagline(client, "OK Begin TLS negotiation now.");
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen /* uncork the old fd */
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen o_stream_uncork(client->output);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (o_stream_flush(client->output) <= 0) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen /* the buffer has to be flushed */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen o_stream_set_flush_pending(client->output, TRUE);
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen o_stream_set_flush_callback(client->output,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen client_output_starttls, client);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen } else {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen client_start_tls(client);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen }
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen return 1;
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen}
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenstatic void
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenclient_update_info(struct imap_client *client, const struct imap_arg *args)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen{
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen const char *key, *value;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (args->type != IMAP_ARG_LIST)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen args = IMAP_ARG_LIST_ARGS(args);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen while (args->type == IMAP_ARG_STRING &&
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen args[1].type == IMAP_ARG_STRING) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen key = IMAP_ARG_STR_NONULL(&args[0]);
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen value = IMAP_ARG_STR_NONULL(&args[1]);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (strcasecmp(key, "x-originating-ip") == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen (void)net_addr2ip(value, &client->common.ip);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen else if (strcasecmp(key, "x-originating-port") == 0)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen client->common.remote_port = atoi(value);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen else if (strcasecmp(key, "x-connected-ip") == 0)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen (void)net_addr2ip(value, &client->common.local_ip);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen else if (strcasecmp(key, "x-connected-port") == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen client->common.local_port = atoi(value);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen args += 2;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen}
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic int cmd_id(struct imap_client *client, const struct imap_arg *args)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen{
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen const char *env, *value;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (!client->id_logged) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen client->id_logged = TRUE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (client->common.trusted)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen client_update_info(client, args);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen env = getenv("IMAP_ID_LOG");
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen value = imap_id_args_get_log_reply(args, env);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (value != NULL) {
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen client_syslog(&client->common,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen t_strdup_printf("ID sent: %s", value));
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen env = getenv("IMAP_ID_SEND");
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen client_send_line(client, t_strdup_printf("* ID %s",
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen imap_id_reply_generate(env)));
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen client_send_tagline(client, "OK ID completed.");
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen return 1;
053843989f13d9013b265fb401a4bde7e0e6568eTimo Sirainen}
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenstatic int cmd_noop(struct imap_client *client)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen{
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen client_send_tagline(client, "OK NOOP completed.");
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return 1;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen}
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenstatic int cmd_logout(struct imap_client *client)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen{
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen client_send_line(client, "* BYE Logging out");
ab286a8b58306eb8d22fc18342b6c199fd428e1eTimo Sirainen client_send_tagline(client, "OK Logout completed.");
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen client_destroy(client, "Aborted login");
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return 1;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen}
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainenstatic int cmd_enable(struct imap_client *client)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen{
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen client_send_line(client, "* ENABLED");
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen client_send_tagline(client,
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen "OK ENABLE ignored in non-authenticated state.");
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return 1;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen}
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic int client_command_execute(struct imap_client *client, const char *cmd,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen const struct imap_arg *args)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen{
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen cmd = t_str_ucase(cmd);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (strcmp(cmd, "LOGIN") == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return cmd_login(client, args);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (strcmp(cmd, "AUTHENTICATE") == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return cmd_authenticate(client, args);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (strcmp(cmd, "CAPABILITY") == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return cmd_capability(client);
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen if (strcmp(cmd, "STARTTLS") == 0)
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen return cmd_starttls(client);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (strcmp(cmd, "ID") == 0)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return cmd_id(client, args);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (strcmp(cmd, "NOOP") == 0)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return cmd_noop(client);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (strcmp(cmd, "LOGOUT") == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return cmd_logout(client);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (strcmp(cmd, "ENABLE") == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return cmd_enable(client);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen return -1;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen}
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenstatic bool client_handle_input(struct imap_client *client)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen{
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen const struct imap_arg *args;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen const char *msg;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen int ret;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen bool fatal;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen i_assert(!client->common.authenticating);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (client->cmd_finished) {
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen /* clear the previous command from memory. don't do this
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen immediately after handling command since we need the
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen cmd_tag to stay some time after authentication commands. */
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen client->cmd_tag = NULL;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen client->cmd_name = NULL;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen imap_parser_reset(client->parser);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* remove \r\n */
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen if (client->skip_line) {
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen if (!client_skip_line(client))
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen return FALSE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen client->skip_line = FALSE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen client->cmd_finished = FALSE;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (client->cmd_tag == NULL) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen client->cmd_tag = imap_parser_read_word(client->parser);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (client->cmd_tag == NULL)
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen return FALSE; /* need more data */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (client->cmd_name == NULL) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen client->cmd_name = imap_parser_read_word(client->parser);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (client->cmd_name == NULL)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return FALSE; /* need more data */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen switch (imap_parser_read_args(client->parser, 0, 0, &args)) {
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen case -1:
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen /* error */
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen msg = imap_parser_get_error(client->parser, &fatal);
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen if (fatal) {
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen client_send_line(client, t_strconcat("* BYE ",
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen msg, NULL));
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen client_destroy(client,
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen t_strconcat("Disconnected: ", msg, NULL));
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen return FALSE;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen }
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen client_send_tagline(client, t_strconcat("BAD ", msg, NULL));
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen client->cmd_finished = TRUE;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen client->skip_line = TRUE;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen return TRUE;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen case -2:
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* not enough data */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return FALSE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* we read the entire line - skip over the CRLF */
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (!client_skip_line(client))
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen i_unreached();
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (*client->cmd_tag == '\0')
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ret = -1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen else
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen ret = client_command_execute(client, client->cmd_name, args);
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen client->cmd_finished = TRUE;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen if (ret < 0) {
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen if (*client->cmd_tag == '\0')
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen client->cmd_tag = "*";
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (++client->bad_counter >= CLIENT_MAX_BAD_COMMANDS) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_send_line(client,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen "* BYE Too many invalid IMAP commands.");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_destroy(client,
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen "Disconnected: Too many invalid commands");
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return FALSE;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_send_tagline(client,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen "BAD Error in IMAP command received by server.");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return ret != 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenbool client_read(struct imap_client *client)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen switch (i_stream_read(client->common.input)) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen case -2:
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* buffer full */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_send_line(client, "* BYE Input buffer full, aborting");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_destroy(client, "Disconnected: Input buffer full");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return FALSE;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen case -1:
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* disconnected */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_destroy(client, "Disconnected");
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen return FALSE;
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen case 0:
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* nothing new read */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return TRUE;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen default:
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* something was read */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen timeout_reset(client->to_idle_disconnect);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return TRUE;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen}
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenvoid client_input(struct imap_client *client)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!client_read(client))
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen client_ref(client);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (!auth_client_is_connected(auth_client)) {
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* we're not yet connected to auth process -
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen don't allow any commands */
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen client_send_line(client, AUTH_WAITING_MSG);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen if (client->to_auth_waiting != NULL)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen timeout_remove(&client->to_auth_waiting);
b4931fc08faa0079f32f29286f4c3abd0d8788bcTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen client->input_blocked = TRUE;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen } else {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen o_stream_cork(client->output);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen while (client_handle_input(client)) ;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen o_stream_uncork(client->output);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen client_unref(client);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenvoid client_destroy_oldest(void)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen struct client *client;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen struct imap_client *destroy_buf[CLIENT_DESTROY_OLDEST_COUNT];
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int i, destroy_count;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* find the oldest clients and put them to destroy-buffer */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen memset(destroy_buf, 0, sizeof(destroy_buf));
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen destroy_count = max_connections > CLIENT_DESTROY_OLDEST_COUNT*2 ?
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen CLIENT_DESTROY_OLDEST_COUNT : I_MIN(max_connections/2, 1);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen for (client = clients; client != NULL; client = client->next) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen struct imap_client *imap_client = (struct imap_client *)client;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen for (i = 0; i < destroy_count; i++) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (destroy_buf[i] == NULL ||
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen destroy_buf[i]->created > imap_client->created) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* @UNSAFE */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen memmove(destroy_buf+i+1, destroy_buf+i,
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen sizeof(destroy_buf) -
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen (i+1) * sizeof(struct imap_client *));
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen destroy_buf[i] = imap_client;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen break;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* then kill them */
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen for (i = 0; i < destroy_count; i++) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (destroy_buf[i] == NULL)
871c7b8969e8627dc4c8b3e56fd126f948e6bce6Timo Sirainen break;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen client_destroy(destroy_buf[i],
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen "Disconnected: Connection queue full");
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void client_send_greeting(struct imap_client *client)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen string_t *greet;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen greet = t_str_new(128);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen str_append(greet, "* OK ");
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen str_printfa(greet, "[CAPABILITY %s] ", get_capability(client, FALSE));
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_append(greet, greeting);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_send_line(client, str_c(greet));
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client->greeting_sent = TRUE;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void client_idle_disconnect_timeout(struct imap_client *client)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_send_line(client, "* BYE Disconnected for inactivity.");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_destroy(client, "Disconnected: Inactivity");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void client_auth_waiting_timeout(struct imap_client *client)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_send_line(client, AUTH_WAITING_MSG);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen timeout_remove(&client->to_auth_waiting);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenvoid client_set_auth_waiting(struct imap_client *client)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen i_assert(client->to_auth_waiting == NULL);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client->to_auth_waiting =
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen timeout_add(AUTH_WAITING_TIMEOUT_MSECS,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen client_auth_waiting_timeout, client);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen}
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenstruct client *client_create(int fd, bool ssl, const struct ip_addr *local_ip,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen const struct ip_addr *ip)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen{
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen struct imap_client *client;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen i_assert(fd != -1);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen connection_queue_add(1);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* always use nonblocking I/O */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen net_set_nonblock(fd, TRUE);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client = i_new(struct imap_client, 1);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen client->created = ioloop_time;
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen client->refcount = 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client->common.local_ip = *local_ip;
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen client->common.ip = *ip;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client->common.fd = fd;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client->common.tls = ssl;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client->common.trusted = client_is_trusted(&client->common);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client->common.secured = ssl || client->common.trusted ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen net_ip_compare(ip, local_ip);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen client_open_streams(client, fd);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client->io = io_add(fd, IO_READ, client_input, client);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_link(&client->common);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen main_ref();
8830fab191cab8440281eb641dfdd93974b2933bTimo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (auth_client_is_connected(auth_client))
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen client_send_greeting(client);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_set_auth_waiting(client);
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen client_set_title(client);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen client->to_idle_disconnect =
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen timeout_add(CLIENT_LOGIN_IDLE_TIMEOUT_MSECS,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_idle_disconnect_timeout, client);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return &client->common;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainenvoid client_destroy(struct imap_client *client, const char *reason)
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen{
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen if (client->destroyed)
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen return;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen client->destroyed = TRUE;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen if (!client->login_success && reason != NULL) {
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen reason = t_strconcat(reason, " ",
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen client_get_extra_disconnect_reason(&client->common),
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (reason != NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_syslog(&client->common, reason);
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen client_unlink(&client->common);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (client->common.input != NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_stream_close(client->common.input);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (client->output != NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen o_stream_close(client->output);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (client->common.master_tag != 0)
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen master_request_abort(&client->common);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (client->common.auth_request != NULL) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen i_assert(client->common.authenticating);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen sasl_server_auth_client_error(&client->common, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_assert(!client->common.authenticating);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (client->io != NULL)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen io_remove(&client->io);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (client->to_idle_disconnect != NULL)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen timeout_remove(&client->to_idle_disconnect);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (client->to_auth_waiting != NULL)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen timeout_remove(&client->to_auth_waiting);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (client->to_authfail_delay != NULL)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen timeout_remove(&client->to_authfail_delay);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (client->common.fd != -1) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen net_disconnect(client->common.fd);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen client->common.fd = -1;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (client->proxy_password != NULL) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen safe_memset(client->proxy_password, 0,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen strlen(client->proxy_password));
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen i_free(client->proxy_password);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen client->proxy_password = NULL;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen }
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen i_free_and_null(client->proxy_user);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen i_free_and_null(client->proxy_master_user);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (client->proxy != NULL)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen login_proxy_free(&client->proxy);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (client->common.proxy != NULL) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ssl_proxy_free(client->common.proxy);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client->common.proxy = NULL;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen client_unref(client);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen main_listen_start();
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen main_unref();
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen}
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainenvoid client_destroy_success(struct imap_client *client, const char *reason)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen{
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen client->login_success = TRUE;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen client_destroy(client, reason);
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen}
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainenvoid client_destroy_internal_failure(struct imap_client *client)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen{
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen client_send_line(client, "* BYE Internal login failure. "
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen "Refer to server log for more information.");
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen client_destroy(client, "Internal login failure");
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen}
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenvoid client_ref(struct imap_client *client)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client->refcount++;
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen}
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainenbool client_unref(struct imap_client *client)
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen{
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen i_assert(client->refcount > 0);
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen if (--client->refcount > 0)
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen return TRUE;
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen
5a4ab3d6e108a899c8b51bebd0094a37b738d5a1Timo Sirainen i_assert(client->destroyed);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen imap_parser_destroy(&client->parser);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (client->common.input != NULL)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen i_stream_unref(&client->common.input);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (client->output != NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen o_stream_unref(&client->output);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen i_free(client->common.virtual_user);
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen i_free(client->common.auth_mech_name);
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen i_free(client);
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen return FALSE;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen}
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainenvoid client_send_line(struct imap_client *client, const char *line)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen{
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen struct const_iovec iov[2];
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen ssize_t ret;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen iov[0].iov_base = line;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen iov[0].iov_len = strlen(line);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen iov[1].iov_base = "\r\n";
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen iov[1].iov_len = 2;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen ret = o_stream_sendv(client->output, iov, 2);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (ret < 0 || (size_t)ret != iov[0].iov_len + iov[1].iov_len) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* either disconnection or buffer full. in either case we
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen want this connection destroyed. however destroying it here
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen might break things if client is still tried to be accessed
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen without being referenced.. */
cd65dfcedc603fabbf644be793efb09096f6c1b5Timo Sirainen i_stream_close(client->common.input);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen}
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenvoid client_send_tagline(struct imap_client *client, const char *line)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen{
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen client_send_line(client, t_strconcat(client->cmd_tag, " ", line, NULL));
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainenvoid clients_notify_auth_connected(void)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen{
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen struct client *client;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen for (client = clients; client != NULL; client = client->next) {
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen struct imap_client *imap_client = (struct imap_client *)client;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (imap_client->to_auth_waiting != NULL)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen timeout_remove(&imap_client->to_auth_waiting);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (!imap_client->greeting_sent)
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen client_send_greeting(imap_client);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (imap_client->input_blocked) {
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen imap_client->input_blocked = FALSE;
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen client_input(imap_client);
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen }
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen }
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen}
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainenvoid clients_destroy_all(void)
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen{
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct client *client;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen for (client = clients; client != NULL; client = client->next) {
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen struct imap_client *imap_client = (struct imap_client *)client;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen client_destroy(imap_client, "Disconnected: Shutting down");
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen}
c485524d09c650ff6e6c552129d4257ac6145a8bTimo Sirainen
c485524d09c650ff6e6c552129d4257ac6145a8bTimo Sirainenvoid clients_init(void)
c485524d09c650ff6e6c552129d4257ac6145a8bTimo Sirainen{
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* Nothing to initialize for IMAP */
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen}
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen
28dd1eeb9fdb93051bb5899c80cf300d69ba9f9eTimo Sirainenvoid clients_deinit(void)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen{
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen clients_destroy_all();
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen}
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen