aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/smtp/camel-smtp-transport.c
diff options
context:
space:
mode:
Diffstat (limited to 'camel/providers/smtp/camel-smtp-transport.c')
-rw-r--r--camel/providers/smtp/camel-smtp-transport.c165
1 files changed, 73 insertions, 92 deletions
diff --git a/camel/providers/smtp/camel-smtp-transport.c b/camel/providers/smtp/camel-smtp-transport.c
index c3a9e9ed15..a6e555badf 100644
--- a/camel/providers/smtp/camel-smtp-transport.c
+++ b/camel/providers/smtp/camel-smtp-transport.c
@@ -145,18 +145,7 @@ smtp_construct (CamelService *service, CamelSession *session,
CamelProvider *provider, CamelURL *url,
CamelException *ex)
{
- CamelSmtpTransport *smtp_transport = CAMEL_SMTP_TRANSPORT (service);
- const char *use_ssl;
-
CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
-
- if ((use_ssl = camel_url_get_param (url, "use_ssl"))) {
- /* Note: previous versions would use "" to toggle use_ssl to 'on' */
- if (!*use_ssl || !strcmp (use_ssl, "always"))
- smtp_transport->flags |= CAMEL_SMTP_TRANSPORT_USE_SSL_ALWAYS;
- else if (!strcmp (use_ssl, "when-possible"))
- smtp_transport->flags |= CAMEL_SMTP_TRANSPORT_USE_SSL_WHEN_POSSIBLE;
- }
}
static const char *
@@ -228,70 +217,56 @@ smtp_error_string (int error)
}
}
+enum {
+ MODE_CLEAR,
+ MODE_SSL,
+ MODE_TLS,
+};
+
#define SSL_PORT_FLAGS (CAMEL_TCP_STREAM_SSL_ENABLE_SSL2 | CAMEL_TCP_STREAM_SSL_ENABLE_SSL3)
#define STARTTLS_FLAGS (CAMEL_TCP_STREAM_SSL_ENABLE_TLS)
static gboolean
-connect_to_server (CamelService *service, int try_starttls, CamelException *ex)
+connect_to_server (CamelService *service, struct addrinfo *ai, int ssl_mode, CamelException *ex)
{
CamelSmtpTransport *transport = CAMEL_SMTP_TRANSPORT (service);
CamelStream *tcp_stream;
char *respbuf = NULL;
int ret;
- struct addrinfo *ai, hints = { 0 };
- char *serv;
if (!CAMEL_SERVICE_CLASS (parent_class)->connect (service, ex))
return FALSE;
/* set some smtp transport defaults */
- transport->flags &= CAMEL_SMTP_TRANSPORT_USE_SSL; /* reset all but ssl flags */
+ transport->flags = 0;
transport->authtypes = NULL;
-
- if (service->url->port) {
- serv = g_alloca(16);
- sprintf(serv, "%d", service->url->port);
- } else
- serv = "smtp";
- if (transport->flags & CAMEL_SMTP_TRANSPORT_USE_SSL) {
+ if (ssl_mode != MODE_CLEAR) {
#ifdef HAVE_SSL
- if (try_starttls) {
- tcp_stream = camel_tcp_stream_ssl_new_raw (service->session, service->url->host, STARTTLS_FLAGS);
+ if (ssl_mode == MODE_TLS) {
+ tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, STARTTLS_FLAGS);
} else {
- if (service->url->port == 0)
- serv = "smtps";
tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, SSL_PORT_FLAGS);
}
#else
- if (!try_starttls && service->url->port == 0)
- serv = "smtps";
-
camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
- _("Could not connect to %s (port %s): %s"),
- service->url->host, serv,
- _("SSL unavailable"));
-
+ _("Could not connect to %s: %s"),
+ service->url->host, _("SSL unavailable"));
+
return FALSE;
#endif /* HAVE_SSL */
} else {
tcp_stream = camel_tcp_stream_raw_new ();
}
-
- hints.ai_socktype = SOCK_STREAM;
- ai = camel_getaddrinfo(service->url->host, serv, &hints, ex);
- if (ai == NULL) {
- camel_object_unref(tcp_stream);
- return FALSE;
- }
- ret = camel_tcp_stream_connect(CAMEL_TCP_STREAM(tcp_stream), ai);
- camel_freeaddrinfo(ai);
- if (ret == -1) {
- camel_exception_setv (ex, errno == EINTR ? CAMEL_EXCEPTION_USER_CANCEL : CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
- _("Could not connect to %s (port %s): %s"),
- service->url->host, serv,
- g_strerror (errno));
+ if ((ret = camel_tcp_stream_connect ((CamelTcpStream *) tcp_stream, ai)) == -1) {
+ if (errno == EINTR)
+ camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
+ _("Connection cancelled"));
+ else
+ camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
+ _("Could not connect to %s: %s"),
+ service->url->host, g_strerror (errno));
camel_object_unref (tcp_stream);
@@ -335,30 +310,19 @@ connect_to_server (CamelService *service, int try_starttls, CamelException *ex)
/* clear any EHLO/HELO exception and assume that any SMTP errors encountered were non-fatal */
camel_exception_clear (ex);
-#ifdef HAVE_SSL
- if (transport->flags & CAMEL_SMTP_TRANSPORT_USE_SSL_WHEN_POSSIBLE) {
- /* try_starttls is always TRUE here */
- if (transport->flags & CAMEL_SMTP_TRANSPORT_STARTTLS)
- goto starttls;
- } else if (transport->flags & CAMEL_SMTP_TRANSPORT_USE_SSL_ALWAYS) {
- if (try_starttls) {
- if (transport->flags & CAMEL_SMTP_TRANSPORT_STARTTLS) {
- goto starttls;
- } else {
- /* server doesn't support STARTTLS, abort */
- camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
- _("Failed to connect to SMTP server %s in secure mode: %s"),
- service->url->host, _("server does not appear to support SSL"));
- goto exception_cleanup;
- }
- }
+ if (ssl_mode != MODE_TLS) {
+ /* we're done */
+ return TRUE;
}
-#endif /* HAVE_SSL */
- return TRUE;
+ if (!(transport->flags & CAMEL_SMTP_TRANSPORT_STARTTLS)) {
+ camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
+ _("Failed to connect to SMTP server %s in secure mode: %s"),
+ service->url->host, _("STARTTLS not supported"));
+
+ goto exception_cleanup;
+ }
-#ifdef HAVE_SSL
- starttls:
d(fprintf (stderr, "sending : STARTTLS\r\n"));
if (camel_stream_write (tcp_stream, "STARTTLS\r\n", 10) == -1) {
camel_exception_setv (ex, errno == EINTR ? CAMEL_EXCEPTION_USER_CANCEL : CAMEL_EXCEPTION_SYSTEM,
@@ -408,38 +372,55 @@ connect_to_server (CamelService *service, int try_starttls, CamelException *ex)
transport->connected = FALSE;
return FALSE;
-#endif /* HAVE_SSL */
}
+static struct {
+ char *value;
+ char *serv;
+ int mode;
+} ssl_options[] = {
+ { "", "smtps", MODE_SSL }, /* really old (1.x) */
+ { "always", "smtps", MODE_SSL },
+ { "when-possible", "smtp", MODE_TLS },
+ { "never", "smtp", MODE_CLEAR },
+ { NULL, "smtp", MODE_CLEAR },
+};
+
static gboolean
connect_to_server_wrapper (CamelService *service, CamelException *ex)
{
-#ifdef HAVE_SSL
- CamelSmtpTransport *transport = (CamelSmtpTransport *) service;
+ struct addrinfo hints, *ai;
+ const char *ssl_mode;
+ int mode, ret, i;
+ char *serv;
- if (transport->flags & CAMEL_SMTP_TRANSPORT_USE_SSL_ALWAYS) {
- /* First try connecting to the SSL port */
- if (!connect_to_server (service, FALSE, ex)) {
- if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_SERVICE_UNAVAILABLE) {
- /* Seems the SSL port is unavailable, lets try STARTTLS */
- camel_exception_clear (ex);
- return connect_to_server (service, TRUE, ex);
- } else {
- return FALSE;
- }
- }
-
- return TRUE;
- } else if (transport->flags & CAMEL_SMTP_TRANSPORT_USE_SSL_WHEN_POSSIBLE) {
- /* If the server supports STARTTLS, use it */
- return connect_to_server (service, TRUE, ex);
+ if ((ssl_mode = camel_url_get_param (service->url, "use_ssl"))) {
+ for (i = 0; ssl_options[i].value; i++)
+ if (!strcmp (ssl_options[i].value, ssl_mode))
+ break;
+ mode = ssl_options[i].mode;
+ serv = ssl_options[i].serv;
} else {
- /* User doesn't care about SSL */
- return connect_to_server (service, FALSE, ex);
+ mode = MODE_CLEAR;
+ serv = "smtp";
}
-#else
- return connect_to_server (service, FALSE, ex);
-#endif
+
+ if (service->url->port) {
+ serv = g_alloca (16);
+ sprintf (serv, "%d", service->url->port);
+ }
+
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_family = PF_UNSPEC;
+ if (!(ai = camel_getaddrinfo (service->url->host, serv, &hints, ex)))
+ return FALSE;
+
+ ret = connect_to_server (service, ai, mode, ex);
+
+ camel_freeaddrinfo (ai);
+
+ return ret;
}
static gboolean