diff options
author | Jeffrey Stedfast <fejj@novell.com> | 2004-09-28 01:41:16 +0800 |
---|---|---|
committer | Jeffrey Stedfast <fejj@src.gnome.org> | 2004-09-28 01:41:16 +0800 |
commit | e5e0149bb66b663bba92c0eb8dd7dfbe6e33d9fa (patch) | |
tree | 652e325cf8c7c28bb03682bb9021ad79f0834986 /camel/providers | |
parent | c3a9a32b59ef4fb590e8a2232177630891b46b71 (diff) | |
download | gsoc2013-evolution-e5e0149bb66b663bba92c0eb8dd7dfbe6e33d9fa.tar.gz gsoc2013-evolution-e5e0149bb66b663bba92c0eb8dd7dfbe6e33d9fa.tar.zst gsoc2013-evolution-e5e0149bb66b663bba92c0eb8dd7dfbe6e33d9fa.zip |
Instead of doing a host-lookup ourselves, get it passed in to us as an
2004-09-22 Jeffrey Stedfast <fejj@novell.com>
* 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
Diffstat (limited to 'camel/providers')
-rw-r--r-- | camel/providers/imap/camel-imap-store.c | 167 | ||||
-rw-r--r-- | camel/providers/imap4/camel-imap4-store.c | 159 | ||||
-rw-r--r-- | camel/providers/imapp/camel-imapp-store.c | 36 | ||||
-rw-r--r-- | camel/providers/nntp/camel-nntp-store.c | 130 | ||||
-rw-r--r-- | camel/providers/pop3/camel-pop3-store.c | 161 | ||||
-rw-r--r-- | camel/providers/smtp/camel-smtp-transport.c | 165 | ||||
-rw-r--r-- | camel/providers/smtp/camel-smtp-transport.h | 8 |
7 files changed, 341 insertions, 485 deletions
diff --git a/camel/providers/imap/camel-imap-store.c b/camel/providers/imap/camel-imap-store.c index 54340ca783..bc89bae55b 100644 --- a/camel/providers/imap/camel-imap-store.c +++ b/camel/providers/imap/camel-imap-store.c @@ -503,76 +503,52 @@ imap_get_capability (CamelService *service, CamelException *ex) } 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 (CamelService *service, int ssl_mode, int try_starttls, CamelException *ex) +connect_to_server (CamelService *service, struct addrinfo *ai, int ssl_mode, CamelException *ex) { CamelImapStore *store = (CamelImapStore *) service; CamelImapResponse *response; CamelStream *tcp_stream; CamelSockOptData sockopt; gboolean force_imap4 = FALSE; - int clean_quit; - int ret; + int clean_quit, ret; char *buf; - struct addrinfo *ai, hints = { 0 }; - char *serv; - - /* FIXME: this connect stuff is duplicated everywhere */ - - if (service->url->port) { - serv = g_alloca(16); - sprintf(serv, "%d", service->url->port); - } else - serv = "imap"; - if (ssl_mode != USE_SSL_NEVER) { + 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 = "imaps"; tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, SSL_PORT_FLAGS); } #else - if (!try_starttls && service->url->port == 0) - serv = "imaps"; - 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) { + 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 %s): %s"), - service->url->host, serv, g_strerror (errno)); + _("Could not connect to %s: %s"), + service->url->host, + g_strerror (errno)); camel_object_unref (tcp_stream); @@ -667,32 +643,18 @@ connect_to_server (CamelService *service, int ssl_mode, int try_starttls, CamelE store->server_level = IMAP_LEVEL_IMAP4; } -#ifdef HAVE_SSL - if (ssl_mode == USE_SSL_WHEN_POSSIBLE) { - if (store->capabilities & IMAP_CAPABILITY_STARTTLS) - goto starttls; - } else if (ssl_mode == USE_SSL_ALWAYS) { - if (try_starttls) { - if (store->capabilities & IMAP_CAPABILITY_STARTTLS) { - /* attempt to toggle STARTTLS mode */ - goto starttls; - } else { - /* server doesn't support STARTTLS, abort */ - camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, - _("Failed to connect to IMAP server %s in secure mode: %s"), - service->url->host, _("SSL/TLS extension not supported.")); - /* we have the possibility of quitting cleanly here */ - clean_quit = TRUE; - goto exception; - } - } + if (ssl_mode != MODE_TLS) { + /* we're done */ + return TRUE; } -#endif /* HAVE_SSL */ - - return TRUE; -#ifdef HAVE_SSL - starttls: + if (!(store->capabilities & IMAP_CAPABILITY_STARTTLS)) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + _("Failed to connect to IMAP server %s in secure mode: %s"), + service->url->host, _("STARTTLS not supported")); + + goto exception; + } /* as soon as we send a STARTTLS command, all hope is lost of a clean QUIT if problems arise */ clean_quit = FALSE; @@ -757,7 +719,6 @@ connect_to_server (CamelService *service, int ssl_mode, int try_starttls, CamelE store->connected = FALSE; return FALSE; -#endif /* HAVE_SSL */ } static gboolean @@ -905,60 +866,54 @@ connect_to_server_process (CamelService *service, const char *cmd, CamelExceptio 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 (CamelService *service, CamelException *ex) { - const char *command; -#ifdef HAVE_SSL - const char *use_ssl; - int i, ssl_mode; -#endif - command = camel_url_get_param (service->url, "command"); - if (command) + const char *command, *ssl_mode; + struct addrinfo hints, *ai; + int mode, ret, i; + char *serv; + + if ((command = camel_url_get_param (service->url, "command"))) return connect_to_server_process (service, command, ex); - -#ifdef HAVE_SSL - use_ssl = camel_url_get_param (service->url, "use_ssl"); - if (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; - } else - ssl_mode = USE_SSL_NEVER; - - if (ssl_mode == USE_SSL_ALWAYS) { - /* First try the ssl port */ - if (!connect_to_server (service, 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); - return connect_to_server (service, ssl_mode, TRUE, ex); - } else { - return FALSE; - } - } - - return TRUE; - } else if (ssl_mode == USE_SSL_WHEN_POSSIBLE) { - /* If the server supports STARTTLS, use it */ - return connect_to_server (service, ssl_mode, TRUE, ex); + mode = ssl_options[i].mode; + serv = ssl_options[i].serv; } else { - /* User doesn't care about SSL */ - return connect_to_server (service, ssl_mode, FALSE, ex); + mode = MODE_CLEAR; + serv = "imap"; } -#else - return connect_to_server (service, USE_SSL_NEVER, 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; } extern CamelServiceAuthType camel_imap_password_authtype; 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; } diff --git a/camel/providers/imapp/camel-imapp-store.c b/camel/providers/imapp/camel-imapp-store.c index 3c0dac5456..00ba0d00ce 100644 --- a/camel/providers/imapp/camel-imapp-store.c +++ b/camel/providers/imapp/camel-imapp-store.c @@ -190,29 +190,32 @@ connect_to_server (CamelService *service, int ssl_mode, int try_starttls) CamelIMAPPStore *store = CAMEL_IMAPP_STORE (service); CamelStream * volatile tcp_stream = NULL; CamelIMAPPStream * volatile imap_stream = NULL; - struct hostent *h = NULL; - int ret, port; + int ret; CamelException *ex; ex = camel_exception_new(); CAMEL_TRY { + char *serv; + struct addrinfo *ai, hints = { 0 }; + /* parent class connect initialization */ CAMEL_SERVICE_CLASS (parent_class)->connect (service, ex); if (ex->id) camel_exception_throw_ex(ex); - h = camel_service_gethost(service, ex); - if (ex->id) - camel_exception_throw_ex(ex); - - port = service->url->port ? service->url->port : IMAP_PORT; - + if (service->url->port) { + serv = g_alloca(16); + sprintf(serv, "%d", service->url->port); + } else + serv = "imap"; + #ifdef HAVE_SSL if (camel_url_get_param (service->url, "use_ssl")) { if (try_starttls) tcp_stream = camel_tcp_stream_ssl_new_raw (service->session, service->url->host, STARTTLS_FLAGS); else { - port = service->url->port ? service->url->port : 995; + if (service->url->port == 0) + serv = "imaps"; tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, SSL_PORT_FLAGS); } } else { @@ -221,16 +224,21 @@ connect_to_server (CamelService *service, int ssl_mode, int try_starttls) #else tcp_stream = camel_tcp_stream_raw_new (); #endif /* HAVE_SSL */ - - ret = camel_tcp_stream_connect (CAMEL_TCP_STREAM (tcp_stream), h, port); - camel_free_host (h); + + hints.ai_socktype = SOCK_STREAM; + ai = camel_getaddrinfo(service->url->host, serv, &hints, ex); + if (ex->id) + camel_exception_throw_ex(ex); + + ret = camel_tcp_stream_connect(CAMEL_TCP_STREAM(tcp_stream), ai); + camel_freeaddrinfo(ai); if (ret == -1) { if (errno == EINTR) camel_exception_throw(CAMEL_EXCEPTION_USER_CANCEL, _("Connection cancelled")); else camel_exception_throw(CAMEL_EXCEPTION_SERVICE_UNAVAILABLE, - _("Could not connect to %s (port %d): %s"), - service->url->host, port, strerror(errno)); + _("Could not connect to %s (port %s): %s"), + service->url->host, serv, strerror(errno)); } imap_stream = (CamelIMAPPStream *)camel_imapp_stream_new(tcp_stream); diff --git a/camel/providers/nntp/camel-nntp-store.c b/camel/providers/nntp/camel-nntp-store.c index e1473f1de1..e8d8871674 100644 --- a/camel/providers/nntp/camel-nntp-store.c +++ b/camel/providers/nntp/camel-nntp-store.c @@ -81,12 +81,6 @@ nntp_can_work_offline(CamelDiscoStore *store) return TRUE; } -enum { - USE_SSL_NEVER, - USE_SSL_ALWAYS, - USE_SSL_WHEN_POSSIBLE -}; - static struct { const char *name; int type; @@ -153,8 +147,17 @@ xover_setup(CamelNNTPStore *store, CamelException *ex) return ret; } +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 ssl_mode, CamelException *ex) +connect_to_server (CamelService *service, struct addrinfo *ai, int ssl_mode, CamelException *ex) { CamelNNTPStore *store = (CamelNNTPStore *) service; CamelDiscoStore *disco_store = (CamelDiscoStore*) service; @@ -164,8 +167,6 @@ connect_to_server (CamelService *service, int ssl_mode, CamelException *ex) unsigned int len; int ret; char *path; - struct addrinfo *ai, hints = { 0 }; - char *serv; CAMEL_NNTP_STORE_LOCK(store, command_lock); @@ -182,42 +183,34 @@ connect_to_server (CamelService *service, int ssl_mode, CamelException *ex) camel_data_cache_set_expire_age (store->cache, 60*60*24*14); camel_data_cache_set_expire_access (store->cache, 60*60*24*5); } - - if (service->url->port) { - serv = g_alloca(16); - sprintf(serv, "%d", service->url->port); - } else - serv = "nntp"; + if (ssl_mode != MODE_CLEAR) { #ifdef HAVE_SSL - if (ssl_mode != USE_SSL_NEVER) { - if (service->url->port == 0) - serv = "nntps"; - tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, CAMEL_TCP_STREAM_SSL_ENABLE_SSL2 | CAMEL_TCP_STREAM_SSL_ENABLE_SSL3); - } else { - tcp_stream = camel_tcp_stream_raw_new (); - } + if (ssl_mode == MODE_TLS) { + tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, STARTTLS_FLAGS); + } else { + tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, SSL_PORT_FLAGS); + } #else - tcp_stream = camel_tcp_stream_raw_new (); -#endif /* HAVE_SSL */ - - hints.ai_socktype = SOCK_STREAM; - ai = camel_getaddrinfo(service->url->host, serv, &hints, ex); - if (ai == NULL) { - camel_object_unref(tcp_stream); + camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE, + _("Could not connect to %s: %s"), + service->url->host, _("SSL unavailable")); + goto fail; +#endif /* HAVE_SSL */ + } else { + tcp_stream = camel_tcp_stream_raw_new (); } - ret = camel_tcp_stream_connect(CAMEL_TCP_STREAM(tcp_stream), ai); - camel_freeaddrinfo(ai); - if (ret == -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 %s): %s"), - service->url->host, serv, g_strerror (errno)); + _("Could not connect to %s: %s"), + service->url->host, + g_strerror (errno)); camel_object_unref (tcp_stream); @@ -279,54 +272,51 @@ connect_to_server (CamelService *service, int ssl_mode, CamelException *ex) 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 }, + { "", "nntps", MODE_SSL }, /* really old (1.x) */ + { "always", "nntps", MODE_SSL }, + { "when-possible", "nntp", MODE_TLS }, + { "never", "nntp", MODE_CLEAR }, + { NULL, "nntp", MODE_CLEAR }, }; static gboolean nntp_connect_online (CamelService *service, CamelException *ex) { -#ifdef HAVE_SSL - const char *use_ssl; - int i, ssl_mode; + struct addrinfo hints, *ai; + const char *ssl_mode; + int mode, ret, i; + char *serv; - use_ssl = camel_url_get_param (service->url, "use_ssl"); - if (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; - } else - ssl_mode = USE_SSL_NEVER; - - if (ssl_mode == USE_SSL_ALWAYS) { - /* Connect via SSL */ - return connect_to_server (service, ssl_mode, ex); - } else if (ssl_mode == USE_SSL_WHEN_POSSIBLE) { - /* If the server supports SSL, use it */ - if (!connect_to_server (service, ssl_mode, ex)) { - if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_SERVICE_UNAVAILABLE) { - /* The ssl port seems to be unavailable, fall back to plain NNTP */ - camel_exception_clear (ex); - return connect_to_server (service, USE_SSL_NEVER, ex); - } else { - return FALSE; - } - } - - return TRUE; + mode = ssl_options[i].mode; + serv = ssl_options[i].serv; } else { - /* User doesn't care about SSL */ - return connect_to_server (service, ssl_mode, ex); + mode = MODE_CLEAR; + serv = "nntp"; } -#else - return connect_to_server (service, USE_SSL_NEVER, 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 diff --git a/camel/providers/pop3/camel-pop3-store.c b/camel/providers/pop3/camel-pop3-store.c index 6870aa1456..1c7b9d025d 100644 --- a/camel/providers/pop3/camel-pop3-store.c +++ b/camel/providers/pop3/camel-pop3-store.c @@ -135,16 +135,16 @@ finalize (CamelObject *object) } 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 (CamelService *service, int ssl_mode, int try_starttls, CamelException *ex) +connect_to_server (CamelService *service, struct addrinfo *ai, int ssl_mode, CamelException *ex) { CamelPOP3Store *store = CAMEL_POP3_STORE (service); CamelStream *tcp_stream; @@ -152,56 +152,34 @@ connect_to_server (CamelService *service, int ssl_mode, int try_starttls, CamelE guint32 flags = 0; int clean_quit; int ret; - struct addrinfo *ai, hints = { 0 }; - char *serv; - - if (service->url->port) { - serv = g_alloca(16); - sprintf(serv, "%d", service->url->port); - } else - serv = "pop3"; - - if (ssl_mode != USE_SSL_NEVER) { + + 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 = "pop3s"; tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, SSL_PORT_FLAGS); } #else - if (!try_starttls && service->url->port == 0) - serv = "pop3s"; - 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) { + 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 POP server %s (port %s): %s"), - service->url->host, serv, g_strerror (errno)); + _("Could not connect to %s: %s"), + service->url->host, + g_strerror (errno)); camel_object_unref (tcp_stream); @@ -219,41 +197,24 @@ connect_to_server (CamelService *service, int ssl_mode, int try_starttls, CamelE if (!(store->engine = camel_pop3_engine_new (tcp_stream, flags))) { camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, - _("Failed to read a valid greeting from POP server %s (port %s)"), - service->url->host, serv); + _("Failed to read a valid greeting from POP server %s"), + service->url->host); + return FALSE; } -#ifdef HAVE_SSL - if (store->engine) { - if (ssl_mode == USE_SSL_WHEN_POSSIBLE) { - if (store->engine->capa & CAMEL_POP3_CAP_STLS) - goto starttls; - } else if (ssl_mode == USE_SSL_ALWAYS) { - if (try_starttls) { - if (store->engine->capa & CAMEL_POP3_CAP_STLS) { - /* attempt to toggle STARTTLS mode */ - goto starttls; - } else { - /* server doesn't support STARTTLS, abort */ - camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, - _("Failed to connect to POP server %s in secure mode: %s"), - service->url->host, _("SSL/TLS extension not supported.")); - /* we have the possibility of quitting cleanly here */ - clean_quit = TRUE; - goto stls_exception; - } - } - } + if (ssl_mode != MODE_TLS) { + camel_object_unref (tcp_stream); + return TRUE; } -#endif /* HAVE_SSL */ - - camel_object_unref (tcp_stream); - return store->engine != NULL; + if (!(store->engine->capa & CAMEL_POP3_CAP_STLS)) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + _("Failed to connect to POP server %s in secure mode: %s"), + service->url->host, _("STLS not supported")); + goto stls_exception; + } -#ifdef HAVE_SSL - starttls: /* as soon as we send a STLS command, all hope is lost of a clean QUIT if problems arise */ clean_quit = FALSE; @@ -303,59 +264,55 @@ connect_to_server (CamelService *service, int ssl_mode, int try_starttls, CamelE store->engine = NULL; return FALSE; -#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 }, + { "", "pop3s", MODE_SSL }, /* really old (1.x) */ + { "always", "pop3s", MODE_SSL }, + { "when-possible", "pop3", MODE_TLS }, + { "never", "pop3", MODE_CLEAR }, + { NULL, "pop3", MODE_CLEAR }, }; static gboolean connect_to_server_wrapper (CamelService *service, CamelException *ex) { -#ifdef HAVE_SSL - const char *use_ssl; - int i, ssl_mode; + struct addrinfo hints, *ai; + const char *ssl_mode; + int mode, ret, i; + char *serv; - use_ssl = camel_url_get_param (service->url, "use_ssl"); - if (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; - } else - ssl_mode = USE_SSL_NEVER; - - if (ssl_mode == USE_SSL_ALWAYS) { - /* First try the ssl port */ - if (!connect_to_server (service, 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); - return connect_to_server (service, ssl_mode, TRUE, ex); - } else { - return FALSE; - } - } - - return TRUE; - } else if (ssl_mode == USE_SSL_WHEN_POSSIBLE) { - /* If the server supports STARTTLS, use it */ - return connect_to_server (service, ssl_mode, TRUE, ex); + mode = ssl_options[i].mode; + serv = ssl_options[i].serv; } else { - /* User doesn't care about SSL */ - return connect_to_server (service, ssl_mode, FALSE, ex); + mode = MODE_CLEAR; + serv = "pop3"; } -#else - return connect_to_server (service, USE_SSL_NEVER, 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; } extern CamelServiceAuthType camel_pop3_password_authtype; 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 diff --git a/camel/providers/smtp/camel-smtp-transport.h b/camel/providers/smtp/camel-smtp-transport.h index 87fcafb58b..7b5ad88f12 100644 --- a/camel/providers/smtp/camel-smtp-transport.h +++ b/camel/providers/smtp/camel-smtp-transport.h @@ -43,13 +43,7 @@ extern "C" { #define CAMEL_SMTP_TRANSPORT_ENHANCEDSTATUSCODES (1 << 2) #define CAMEL_SMTP_TRANSPORT_STARTTLS (1 << 3) -#define CAMEL_SMTP_TRANSPORT_USE_SSL_ALWAYS (1 << 4) -#define CAMEL_SMTP_TRANSPORT_USE_SSL_WHEN_POSSIBLE (1 << 5) - -#define CAMEL_SMTP_TRANSPORT_USE_SSL (CAMEL_SMTP_TRANSPORT_USE_SSL_ALWAYS | \ - CAMEL_SMTP_TRANSPORT_USE_SSL_WHEN_POSSIBLE) - -#define CAMEL_SMTP_TRANSPORT_AUTH_EQUAL (1 << 6) /* set if we are using authtypes from a broken AUTH= */ +#define CAMEL_SMTP_TRANSPORT_AUTH_EQUAL (1 << 4) /* set if we are using authtypes from a broken AUTH= */ typedef struct { CamelTransport parent_object; |