From e5e0149bb66b663bba92c0eb8dd7dfbe6e33d9fa Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Mon, 27 Sep 2004 17:41:16 +0000 Subject: Instead of doing a host-lookup ourselves, get it passed in to us as an 2004-09-22 Jeffrey Stedfast * providers/imap/camel-imap-store.c (connect_to_server): Instead of doing a host-lookup ourselves, get it passed in to us as an argument. Also simplified a bit (try_starttls is no longer an option). (connect_to_server_wrapper): Simplified (we no longer have fallback cases for SSL stuff). Also, perform host lookup here. * providers/imap4/camel-imap4-store.c: Same changes as above. * providers/pop3/camel-pop3-store.c: Same. * providers/smtp/camel-smtp-transport.c: Same. Other changes include making the code consistant with the other providers. * providers/nntp/camel-nntp-store.c: Same as pop/imap. svn path=/trunk/; revision=27398 --- camel/providers/imap4/camel-imap4-store.c | 159 ++++++++++++------------------ 1 file changed, 65 insertions(+), 94 deletions(-) (limited to 'camel/providers/imap4/camel-imap4-store.c') diff --git a/camel/providers/imap4/camel-imap4-store.c b/camel/providers/imap4/camel-imap4-store.c index ee96800ed5..1e9e742f29 100644 --- a/camel/providers/imap4/camel-imap4-store.c +++ b/camel/providers/imap4/camel-imap4-store.c @@ -181,39 +181,33 @@ imap4_get_name (CamelService *service, gboolean brief) } enum { - USE_SSL_NEVER, - USE_SSL_ALWAYS, - USE_SSL_WHEN_POSSIBLE + 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 (CamelIMAP4Engine *engine, struct hostent *host, int ssl_mode, int try_starttls, CamelException *ex) +connect_to_server (CamelIMAP4Engine *engine, struct addrinfo *ai, int ssl_mode, CamelException *ex) { CamelService *service = engine->service; CamelStream *tcp_stream; - int port, ret; - - port = service->url->port ? service->url->port : 143; + CamelIMAP4Command *ic; + int id, ret; - if (ssl_mode) { + if (ssl_mode != MODE_CLEAR) { #ifdef HAVE_SSL - if (try_starttls) { + if (ssl_mode == MODE_TLS) { tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, STARTTLS_FLAGS); } else { - port = service->url->port ? service->url->port : 993; tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, SSL_PORT_FLAGS); } #else - if (!try_starttls) - port = service->url->port ? service->url->port : 993; - camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE, - _("Could not connect to %s (port %d): %s"), - service->url->host, port, - _("SSL unavailable")); + _("Could not connect to %s: %s"), + service->url->host, _("SSL unavailable")); return FALSE; #endif /* HAVE_SSL */ @@ -221,14 +215,14 @@ connect_to_server (CamelIMAP4Engine *engine, struct hostent *host, int ssl_mode, tcp_stream = camel_tcp_stream_raw_new (); } - if ((ret = camel_tcp_stream_connect ((CamelTcpStream *) tcp_stream, host, port)) == -1) { + 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 (port %d): %s"), - service->url->host, port, + _("Could not connect to %s: %s"), + service->url->host, g_strerror (errno)); camel_object_unref (tcp_stream); @@ -242,111 +236,88 @@ connect_to_server (CamelIMAP4Engine *engine, struct hostent *host, int ssl_mode, if (camel_imap4_engine_capability (engine, ex) == -1) return FALSE; -#ifdef HAVE_SSL - if (ssl_mode == USE_SSL_WHEN_POSSIBLE) { - /* try_starttls is always TRUE here */ - if (engine->capa & CAMEL_IMAP4_CAPABILITY_STARTTLS) - goto starttls; - } else if (ssl_mode == USE_SSL_ALWAYS) { - if (try_starttls) { - if (engine->capa & CAMEL_IMAP4_CAPABILITY_STARTTLS) { - goto starttls; - } else { - /* server doesn't support STARTTLS, abort */ - camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE, - _("Failed to connect to IMAP server %s in secure mode: " - "Server does not support STARTTLS"), - service->url->host); - return FALSE; - } - } + if (ssl_mode != MODE_TLS) { + /* we're done */ + return TRUE; } -#endif /* HAVE_SSL */ - return TRUE; + if (!(engine->capa & CAMEL_IMAP4_CAPABILITY_STARTTLS)) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + _("Failed to connect to IMAP server %s in secure mode: %s"), + service->url->host, _("SSL negotiations failed")); + + return FALSE; + } -#ifdef HAVE_SSL - starttls: + ic = camel_imap4_engine_prequeue (engine, NULL, "STARTTLS\r\n"); + while ((id = camel_imap4_engine_iterate (engine)) < ic->id && id != -1) + ; - if (1) { - CamelIMAP4Command *ic; - int id; - - ic = camel_imap4_engine_prequeue (engine, NULL, "STARTTLS\r\n"); - while ((id = camel_imap4_engine_iterate (engine)) < ic->id && id != -1) - ; - - if (id == -1 || ic->result != CAMEL_IMAP4_RESULT_OK) { - if (ic->result != CAMEL_IMAP4_RESULT_OK) { - camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, - _("Failed to connect to IMAP server %s in secure mode: %s"), - service->url->host, _("Unknown error")); - } else { - camel_exception_xfer (ex, &ic->ex); - } - - camel_imap4_command_unref (ic); - - return FALSE; + if (id == -1 || ic->result != CAMEL_IMAP4_RESULT_OK) { + if (ic->result != CAMEL_IMAP4_RESULT_OK) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + _("Failed to connect to IMAP server %s in secure mode: %s"), + service->url->host, _("Unknown error")); + } else { + camel_exception_xfer (ex, &ic->ex); } camel_imap4_command_unref (ic); + + return FALSE; } + camel_imap4_command_unref (ic); + return TRUE; -#endif /* HAVE_SSL */ } static struct { char *value; + char *serv; int mode; } ssl_options[] = { - { "", USE_SSL_ALWAYS }, - { "always", USE_SSL_ALWAYS }, - { "when-possible", USE_SSL_WHEN_POSSIBLE }, - { "never", USE_SSL_NEVER }, - { NULL, USE_SSL_NEVER }, + { "", "imaps", MODE_SSL }, /* really old (1.x) */ + { "always", "imaps", MODE_SSL }, + { "when-possible", "imap", MODE_TLS }, + { "never", "imap", MODE_CLEAR }, + { NULL, "imap", MODE_CLEAR }, }; static gboolean connect_to_server_wrapper (CamelIMAP4Engine *engine, CamelException *ex) { CamelService *service = engine->service; - const char *use_ssl; - struct hostent *h; - int ssl_mode; - int ret, i; - - if (!(h = camel_service_gethost (service, ex))) - return FALSE; + struct addrinfo *ai, hints; + const char *ssl_mode; + int mode, ret, i; + char *serv; - if ((use_ssl = camel_url_get_param (service->url, "use_ssl"))) { + 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, use_ssl)) + if (!strcmp (ssl_options[i].value, ssl_mode)) break; - ssl_mode = ssl_options[i].mode; + mode = ssl_options[i].mode; + serv = ssl_options[i].serv; } else { - ssl_mode = USE_SSL_NEVER; + mode = MODE_CLEAR; + serv = "imap"; } - if (ssl_mode == USE_SSL_ALWAYS) { - /* First try the ssl port */ - if (!(ret = connect_to_server (engine, h, ssl_mode, FALSE, ex))) { - if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_SERVICE_UNAVAILABLE) { - /* The ssl port seems to be unavailable, lets try STARTTLS */ - camel_exception_clear (ex); - ret = connect_to_server (engine, h, ssl_mode, TRUE, ex); - } - } - } else if (ssl_mode == USE_SSL_WHEN_POSSIBLE) { - /* If the server supports STARTTLS, use it */ - ret = connect_to_server (engine, h, ssl_mode, TRUE, ex); - } else { - /* User doesn't care about SSL */ - ret = connect_to_server (engine, h, USE_SSL_NEVER, FALSE, ex); + if (service->url->port) { + serv = g_alloca (16); + sprintf (serv, "%d", service->url->port); } - camel_free_host (h); + 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 (engine, ai, mode, ex); + + camel_freeaddrinfo (ai); return ret; } -- cgit