Patch: fix for second attempt to auth gmail smtp



	Hi,

	The problem this patch is intended to solve is the following:
	* Try to connect to gmail smtp using a wrong password. The result is
the expected one, and we get the get password dialog in platform.
	* After setting the right password, it will try to connect again,
reusing the same connection. On setting up sasl, gmail smtp will return
a 535 error, and this way, connection will fail even before sending the
user/passwd pair.

	I would say that returning that 535 error as a response to AUTH LOGIN
command is not very standard. But that's how gmail smtp works.

	The solution implemented in the patch is just, on getting the 535 in
the AUTH LOGIN where we woudln't expect it, we disconnect and connect
again to smtp server, to start a connection attempt from scratch. We
handle exceptions in a way it won't ask again for password for the real
second attempt. This behavior solves the issue.

Changelog would be:
* libtinymail-camel/camel-lite/camel/providers/camel-smtp-transport.c:
On getting a 535 error after AUTH %s step of smtp_auth, we reconnect to
the smtp service, to start a clean attempt to auth. This makes gmail
smtp second attempt to login work as expected.o

-- 
José Dapena Paz <jdapena igalia com>
Igalia
Index: libtinymail-camel/camel-lite/camel/providers/smtp/camel-smtp-transport.c
===================================================================
--- libtinymail-camel/camel-lite/camel/providers/smtp/camel-smtp-transport.c	(revision 3774)
+++ libtinymail-camel/camel-lite/camel/providers/smtp/camel-smtp-transport.c	(working copy)
@@ -579,11 +579,13 @@
 
 			authenticated = smtp_auth (transport, authtype->authproto, ex);
 			if (!authenticated) {
-				errbuf = g_markup_printf_escaped (
-					_("Unable to authenticate "
-					  "to SMTP server.\n%s\n\n"),
-					camel_exception_get_description (ex));
-				camel_exception_clear (ex);
+				if (camel_exception_is_set (ex)) {
+					errbuf = g_markup_printf_escaped (
+									  _("Unable to authenticate "
+									    "to SMTP server.\n%s\n\n"),
+									  camel_exception_get_description (ex));
+					camel_exception_clear (ex);
+				}
 			}
 		}
 	} else 
@@ -1103,6 +1105,7 @@
 	char *cmdbuf, *respbuf = NULL, *challenge;
 	gboolean auth_challenge = FALSE;
 	CamelSasl *sasl = NULL;
+	gboolean avoid_exception = FALSE;
 
 	camel_operation_start_transient (NULL, _("SMTP Authentication"));
 
@@ -1144,7 +1147,16 @@
 
 		/* the server challenge/response should follow a 334 code */
 		if (strncmp (respbuf, "334", 3) != 0) {
-			smtp_set_exception (transport, FALSE, respbuf, _("AUTH command failed"), ex);
+			if (strncmp (respbuf, "535", 3) == 0) {
+				/* connection dialog is broken, we restart the connection */
+				camel_service_disconnect (CAMEL_SERVICE (transport), FALSE, NULL);
+				camel_exception_clear (ex);
+				if (connect_to_server_wrapper (CAMEL_SERVICE (transport), ex))
+					avoid_exception = TRUE;
+			} 
+			if (!avoid_exception)  {
+				smtp_set_exception (transport, FALSE, respbuf, _("AUTH command failed"), ex);
+			}
 			g_free (respbuf);
 			goto lose;
 		}
@@ -1183,9 +1195,12 @@
 
 	/* check that the server says we are authenticated */
 	if (!respbuf || strncmp (respbuf, "235", 3)) {
-		if (respbuf && auth_challenge && !strncmp (respbuf, "334", 3)) {
+		if (respbuf && !strncmp (respbuf, "334", 3)) {
 			/* broken server, but lets try and work around it anyway... */
 			goto broken_smtp_server;
+		} else if (respbuf && strncmp (respbuf, "535", 3)) {
+			g_free (CAMEL_SERVICE (transport)->url->passwd);
+			CAMEL_SERVICE (transport)->url->passwd = NULL;
 		}
 		g_free (respbuf);
 		goto lose;
@@ -1205,7 +1220,7 @@
 	smtp_debug ("<- %s\n", respbuf ? respbuf : "(null)");
 
  lose:
-	if (!camel_exception_is_set (ex)) {
+	if (!camel_exception_is_set (ex) && !avoid_exception) {
 		camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
 				     _("Bad authentication response from server.\n"));
 	}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]