dovecot-2.0: imap/pop3 proxy: Support SSL/TLS connections to rem...
dovecot at dovecot.org
dovecot at dovecot.org
Wed Apr 29 05:55:36 EEST 2009
details: http://hg.dovecot.org/dovecot-2.0/rev/96678e83eab6
changeset: 9165:96678e83eab6
user: Timo Sirainen <tss at iki.fi>
date: Tue Apr 28 22:55:03 2009 -0400
description:
imap/pop3 proxy: Support SSL/TLS connections to remote servers.
passdb can return ssl=yes, ssl=any-cert and starttls options.
diffstat:
13 files changed, 499 insertions(+), 216 deletions(-)
src/imap-login/client-authenticate.c | 54 +++++----
src/imap-login/client.h | 3
src/imap-login/imap-proxy.c | 119 ++++++++++++++------
src/imap-login/imap-proxy.h | 2
src/login-common/login-proxy.c | 139 +++++++++++++++++++-----
src/login-common/login-proxy.h | 32 ++++-
src/login-common/ssl-proxy-openssl.c | 196 ++++++++++++++++++++++------------
src/login-common/ssl-proxy.c | 9 +
src/login-common/ssl-proxy.h | 7 +
src/pop3-login/client-authenticate.c | 52 +++++----
src/pop3-login/client.h | 9 +
src/pop3-login/pop3-proxy.c | 91 ++++++++++-----
src/pop3-login/pop3-proxy.h | 2
diffs (truncated from 1208 to 300 lines):
diff -r 9b8bf57405c8 -r 96678e83eab6 src/imap-login/client-authenticate.c
--- a/src/imap-login/client-authenticate.c Tue Apr 28 19:57:10 2009 -0400
+++ b/src/imap-login/client-authenticate.c Tue Apr 28 22:55:03 2009 -0400
@@ -124,6 +124,8 @@ static bool client_handle_args(struct im
{
const char *reason = NULL, *host = NULL, *destuser = NULL, *pass = NULL;
const char *master_user = NULL;
+ const char *key, *value, *p;
+ enum login_proxy_ssl_flags ssl_flags = 0;
string_t *reply;
unsigned int port = 143;
bool proxy = FALSE, temp = FALSE, nologin = !success;
@@ -131,34 +133,40 @@ static bool client_handle_args(struct im
*nodelay_r = FALSE;
for (; *args != NULL; args++) {
- if (strcmp(*args, "nologin") == 0)
+ p = strchr(*args, '=');
+ if (p == NULL) {
+ key = *args;
+ value = "";
+ } else {
+ key = t_strdup_until(*args, p);
+ value = p + 1;
+ }
+ if (strcmp(key, "nologin") == 0)
nologin = TRUE;
- else if (strcmp(*args, "nodelay") == 0)
+ else if (strcmp(key, "nodelay") == 0)
*nodelay_r = TRUE;
- else if (strcmp(*args, "proxy") == 0)
+ else if (strcmp(key, "proxy") == 0)
proxy = TRUE;
- else if (strcmp(*args, "temp") == 0)
+ else if (strcmp(key, "temp") == 0)
temp = TRUE;
- else if (strcmp(*args, "authz") == 0)
+ else if (strcmp(key, "authz") == 0)
authz_failure = TRUE;
- else if (strncmp(*args, "reason=", 7) == 0)
- reason = *args + 7;
- else if (strncmp(*args, "host=", 5) == 0)
- host = *args + 5;
- else if (strncmp(*args, "port=", 5) == 0)
- port = atoi(*args + 5);
- else if (strncmp(*args, "destuser=", 9) == 0)
- destuser = *args + 9;
- else if (strncmp(*args, "pass=", 5) == 0)
- pass = *args + 5;
- else if (strncmp(*args, "master=", 7) == 0)
- master_user = *args + 7;
- else if (strncmp(*args, "user=", 5) == 0) {
+ else if (strcmp(key, "reason") == 0)
+ reason = value;
+ else if (strcmp(key, "host") == 0)
+ host = value;
+ else if (strcmp(key, "port") == 0)
+ port = atoi(value);
+ else if (strcmp(key, "destuser") == 0)
+ destuser = value;
+ else if (strcmp(key, "pass") == 0)
+ pass = value;
+ else if (strcmp(key, "master") == 0)
+ master_user = value;
+ else if (strcmp(key, "user") == 0) {
/* already handled in login-common */
- } else if (login_settings->auth_debug) {
- i_info("Ignoring unknown passdb extra field: %s",
- *args);
- }
+ } else if (login_settings->auth_debug)
+ i_info("Ignoring unknown passdb extra field: %s", key);
}
if (destuser == NULL)
@@ -173,7 +181,7 @@ static bool client_handle_args(struct im
if (!success)
return FALSE;
if (imap_proxy_new(client, host, port, destuser, master_user,
- pass) < 0)
+ pass, ssl_flags) < 0)
client_auth_failed(client, TRUE);
return TRUE;
}
diff -r 9b8bf57405c8 -r 96678e83eab6 src/imap-login/client.h
--- a/src/imap-login/client.h Tue Apr 28 19:57:10 2009 -0400
+++ b/src/imap-login/client.h Tue Apr 28 22:55:03 2009 -0400
@@ -29,7 +29,8 @@ struct imap_client {
unsigned int login_success:1;
unsigned int cmd_finished:1;
- unsigned int proxy_login_sent:1;
+ unsigned int proxy_sasl_ir:1;
+ unsigned int proxy_seen_banner:1;
unsigned int skip_line:1;
unsigned int input_blocked:1;
unsigned int destroyed:1;
diff -r 9b8bf57405c8 -r 96678e83eab6 src/imap-login/imap-proxy.c
--- a/src/imap-login/imap-proxy.c Tue Apr 28 19:57:10 2009 -0400
+++ b/src/imap-login/imap-proxy.c Tue Apr 28 22:55:03 2009 -0400
@@ -136,25 +136,8 @@ client_send_capability_if_needed(struct
str_printfa(str, "* CAPABILITY %s\r\n", capability);
}
-static int proxy_input_banner(struct imap_client *client,
- struct ostream *output, const char *line)
-{
- const char *const *capabilities = NULL;
- string_t *str;
-
- if (strncmp(line, "* OK ", 5) != 0) {
- client_syslog_err(&client->common, t_strdup_printf(
- "proxy: Remote returned invalid banner: %s",
- str_sanitize(line, 160)));
- return -1;
- }
-
- str = t_str_new(128);
- if (strncmp(line + 5, "[CAPABILITY ", 12) == 0) {
- capabilities = t_strsplit(t_strcut(line + 5 + 12, ']'), " ");
- if (str_array_icase_find(capabilities, "ID"))
- proxy_write_id(client, str);
- }
+static void proxy_write_login(struct imap_client *client, string_t *str)
+{
if (client->capability_command_used)
str_append(str, "C CAPABILITY\r\n");
@@ -166,8 +149,7 @@ static int proxy_input_banner(struct ima
imap_quote_append_string(str, client->proxy_password, FALSE);
proxy_free_password(client);
- } else if (capabilities != NULL &&
- str_array_icase_find(capabilities, "SASL-IR")) {
+ } else if (client->proxy_sasl_ir) {
/* master user login with SASL initial response support */
str_append(str, "L AUTHENTICATE PLAIN ");
get_plain_auth(client, str);
@@ -177,21 +159,60 @@ static int proxy_input_banner(struct ima
str_append(str, "L AUTHENTICATE PLAIN");
}
str_append(str, "\r\n");
+}
+
+static int proxy_input_banner(struct imap_client *client,
+ struct ostream *output, const char *line)
+{
+ enum login_proxy_ssl_flags ssl_flags;
+ const char *const *capabilities = NULL;
+ string_t *str;
+
+ if (strncmp(line, "* OK ", 5) != 0) {
+ client_syslog_err(&client->common, t_strdup_printf(
+ "proxy: Remote returned invalid banner: %s",
+ str_sanitize(line, 160)));
+ return -1;
+ }
+
+ str = t_str_new(128);
+ if (strncmp(line + 5, "[CAPABILITY ", 12) == 0) {
+ capabilities = t_strsplit(t_strcut(line + 5 + 12, ']'), " ");
+ if (str_array_icase_find(capabilities, "ID"))
+ proxy_write_id(client, str);
+ if (str_array_icase_find(capabilities, "SASL-IR"))
+ client->proxy_sasl_ir = TRUE;
+ }
+
+ ssl_flags = login_proxy_get_ssl_flags(client->proxy);
+ if ((ssl_flags & PROXY_SSL_FLAG_STARTTLS) != 0) {
+ if (capabilities != NULL &&
+ !str_array_icase_find(capabilities, "STARTTLS")) {
+ client_syslog_err(&client->common,
+ "proxy: Remote doesn't support STARTTLS");
+ return -1;
+ }
+ str_append(str, "S STARTTLS\r\n");
+ } else {
+ proxy_write_login(client, str);
+ }
+
(void)o_stream_send(output, str_data(str), str_len(str));
- client->proxy_login_sent = TRUE;
return 0;
}
-static int proxy_input_line(struct imap_client *client,
- struct ostream *output, const char *line)
-{
+static int proxy_input_line(struct imap_client *client, const char *line)
+{
+ struct ostream *output;
const char *capability;
string_t *str;
i_assert(!client->destroyed);
- if (!client->proxy_login_sent) {
+ output = login_proxy_get_ostream(client->proxy);
+ if (!client->proxy_seen_banner) {
/* this is a banner */
+ client->proxy_seen_banner = TRUE;
if (proxy_input_banner(client, output, line) < 0) {
proxy_failed(client, TRUE);
return -1;
@@ -206,6 +227,26 @@ static int proxy_input_line(struct imap_
(void)o_stream_send(output, str_data(str), str_len(str));
return 0;
+ } else if (strncmp(line, "S ", 2) == 0) {
+ if (strncmp(line, "S OK ", 5) != 0) {
+ /* STARTTLS failed */
+ client_syslog_err(&client->common, t_strdup_printf(
+ "proxy: Remote STARTTLS failed: %s",
+ str_sanitize(line + 5, 160)));
+ proxy_failed(client, TRUE);
+ return -1;
+ }
+ /* STARTTLS successful, begin TLS negotiation. */
+ if (login_proxy_starttls(client->proxy) < 0) {
+ proxy_failed(client, TRUE);
+ return -1;
+ }
+ /* i/ostreams changed. */
+ output = login_proxy_get_ostream(client->proxy);
+ str = t_str_new(128);
+ proxy_write_login(client, str);
+ (void)o_stream_send(output, str_data(str), str_len(str));
+ return 1;
} else if (strncmp(line, "L OK ", 5) == 0) {
/* Login successful. Send this line to client. */
capability = client->proxy_backend_capability;
@@ -307,17 +348,18 @@ static int proxy_input_line(struct imap_
}
}
-static void proxy_input(struct istream *input, struct ostream *output,
- struct imap_client *client)
-{
+static void proxy_input(struct imap_client *client)
+{
+ struct istream *input;
const char *line;
+ if (client->proxy == NULL) {
+ /* we're just freeing the proxy */
+ return;
+ }
+
+ input = login_proxy_get_istream(client->proxy);
if (input == NULL) {
- if (client->proxy == NULL) {
- /* we're just freeing the proxy */
- return;
- }
-
if (client->destroyed) {
/* we came here from client_destroy() */
return;
@@ -344,14 +386,14 @@ static void proxy_input(struct istream *
}
while ((line = i_stream_next_line(input)) != NULL) {
- if (proxy_input_line(client, output, line) != 0)
+ if (proxy_input_line(client, line) != 0)
break;
}
}
int imap_proxy_new(struct imap_client *client, const char *host,
unsigned int port, const char *user, const char *master_user,
- const char *password)
+ const char *password, enum login_proxy_ssl_flags ssl_flags)
{
i_assert(user != NULL);
i_assert(!client->destroyed);
@@ -375,14 +417,15 @@ int imap_proxy_new(struct imap_client *c
return -1;
}
- client->proxy = login_proxy_new(&client->common, host, port,
+ client->proxy = login_proxy_new(&client->common, host, port, ssl_flags,
proxy_input, client);
if (client->proxy == NULL) {
client_send_tagline(client, PROXY_FAILURE_MSG);
return -1;
}
- client->proxy_login_sent = FALSE;
+ client->proxy_sasl_ir = FALSE;
+ client->proxy_seen_banner = FALSE;
client->proxy_user = i_strdup(user);
client->proxy_master_user = i_strdup(master_user);
client->proxy_password = i_strdup(password);
diff -r 9b8bf57405c8 -r 96678e83eab6 src/imap-login/imap-proxy.h
--- a/src/imap-login/imap-proxy.h Tue Apr 28 19:57:10 2009 -0400
+++ b/src/imap-login/imap-proxy.h Tue Apr 28 22:55:03 2009 -0400
@@ -5,6 +5,6 @@
int imap_proxy_new(struct imap_client *client, const char *hosts,
unsigned int port, const char *user, const char *master_user,
More information about the dovecot-cvs
mailing list