[evolution-data-server/camel-socks-proxy] Sanitize error handling in the SOCKS5/SOCKS4 code paths



commit d3e86ff97b5acaa1151f116172b0dc60a5c117c8
Author: Federico Mena Quintero <federico novell com>
Date:   Mon Sep 20 10:37:57 2010 -0500

    Sanitize error handling in the SOCKS5/SOCKS4 code paths
    
    Provide more detailed errors based on the replies of the SOCKS server.
    
    Don't leak some CamelExceptions in case of retries.
    
    Signed-off-by: Federico Mena Quintero <federico novell com>

 camel/camel-tcp-stream-raw.c |   21 ++++++++++++++++-----
 1 files changed, 16 insertions(+), 5 deletions(-)
---
diff --git a/camel/camel-tcp-stream-raw.c b/camel/camel-tcp-stream-raw.c
index c273c1d..f44965d 100644
--- a/camel/camel-tcp-stream-raw.c
+++ b/camel/camel-tcp-stream-raw.c
@@ -735,10 +735,10 @@ connect_to_socks4_proxy (CamelTcpStreamRaw *raw, const gchar *proxy_host, gint p
 	PRFileDesc *fd;
 	gchar request[9];
 	struct sockaddr_in *sin;
-	gchar reply[8];
+	gchar reply[8]; /* note that replies are 8 bytes, even if only the first 2 are used */
 	gint save_errno;
 
-	g_assert (connect_addr->ai_addr->sa_family == AF_INET); /* FMQ: check for AF_INET in the caller */
+	g_assert (connect_addr->ai_addr->sa_family == AF_INET);
 
 	fd = connect_to_proxy (raw, proxy_host, proxy_port, ex);
 	if (!fd)
@@ -761,16 +761,24 @@ connect_to_socks4_proxy (CamelTcpStreamRaw *raw, const gchar *proxy_host, gint p
 	d (g_print ("  reading SOCKS4 reply\n"));
 	if (read_from_prfd (fd, reply, sizeof (reply)) != sizeof (reply)) {
 		d (g_print ("  failed: %d\n", errno));
+		camel_exception_set (ex, CAMEL_EXCEPTION_PROXY_NOT_SUPPORTED, _("The proxy host does not support SOCKS4"));
 		goto error;
 	}
 
-	if (!(reply[0] == 0		/* first byte of reply is 0 */
-	      && reply[1] == 90)) {	/* 90 means "request granted" */
+	if (reply[0] != 0) { /* version of reply code is 0 */
 		errno = ECONNREFUSED;
-		d (g_print ("  proxy replied with code %d\n", reply[1]));
+		camel_exception_set (ex, CAMEL_EXCEPTION_PROXY_NOT_SUPPORTED, _("The proxy host does not support SOCKS4"));
 		goto error;
 	}
 
+	if (reply[1] != 90) {   /* 90 means "request granted" */
+               errno = ECONNREFUSED;
+	       camel_exception_set (ex, CAMEL_EXCEPTION_PROXY_CANT_AUTHENTICATE,
+				    _("The proxy host denied our request: code %d"),
+				    reply[1]);
+               goto error;
+        }
+
 	/* We are now proxied; we are ready to send "normal" data through the socket */
 
 	d (g_print ("  success\n"));
@@ -1117,6 +1125,9 @@ stream_connect (CamelTcpStream *stream, const char *host, const char *service, g
 			goto out;
 		}
 
+		if (ai->next != NULL)
+			camel_exception_clear (ex); /* Only preserve the error from the last try, in case no tries are successful */
+
 		ai = ai->ai_next;
 	}
 



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