]> asedeno.scripts.mit.edu Git - PuTTY.git/commitdiff
Avoid misidentifying unbracketed IPv6 literals as host:port.
authorSimon Tatham <anakin@pobox.com>
Sat, 25 Jan 2014 15:58:57 +0000 (15:58 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 25 Jan 2014 15:58:57 +0000 (15:58 +0000)
Both GUI PuTTY front ends have a piece of logic whereby a string is
interpreted as host:port if there's _one_ colon in it, but if there's
more than one colon then it's assumed to be an IPv6 literal with no
trailing port number. This permits the PuTTY command line to take
strings such as 'host', 'host:22' or '[::1]:22', but also cope with a
bare v6 literal such as '::1'.

This logic is also required in the two Plink front ends and in the
processing of CONF_loghost for host key indexing in ssh.c, but was
missing in all those places. Add it.

[originally from svn r10121]

ssh.c
unix/uxplink.c
windows/winplink.c

diff --git a/ssh.c b/ssh.c
index b86f0de1e41c72f3409c35972c4770c36163cdcd..f8fc14348b8e70eb75b143b962c85267361aa221 100644 (file)
--- a/ssh.c
+++ b/ssh.c
@@ -3419,10 +3419,11 @@ static const char *connect_to_host(Ssh ssh, char *host, int port,
 
        /*
         * A colon suffix on the hostname string also lets us affect
-        * savedport.
+        * savedport. (Unless there are multiple colons, in which case
+        * we assume this is an unbracketed IPv6 literal.)
         */
        colon = host_strrchr(tmphost, ':');
-       if (colon) {
+       if (colon && colon == host_strchr(tmphost, ':')) {
            *colon++ = '\0';
            if (*colon)
                ssh->savedport = atoi(colon);
index 961fcf9a7cbb9fbc532dabf46494edd4c568e3d3..ee45dc045883782753643928c3b092b181df290b 100644 (file)
@@ -843,10 +843,21 @@ int main(int argc, char **argv)
            }
        }
 
-       /*
-        * Trim off a colon suffix if it's there.
-        */
-       host[host_strcspn(host, ":")] = '\0';
+        /*
+         * Trim a colon suffix off the hostname if it's there. In
+         * order to protect unbracketed IPv6 address literals
+         * against this treatment, we do not do this if there's
+         * _more_ than one colon.
+         */
+        {
+            char *c = host_strchr(host, ':');
+            if (c) {
+                char *d = host_strchr(c+1, ':');
+                if (!d)
+                    *c = '\0';
+            }
+        }
 
        /*
         * Remove any remaining whitespace.
index 48232b389280fdf9c48f43753ce60bf34f41116d..451eff3b8907d3ed98445c0ced3a8a954b6e0dbd 100644 (file)
@@ -520,10 +520,21 @@ int main(int argc, char **argv)
            }
        }
 
-       /*
-        * Trim off a colon suffix if it's there.
-        */
-       host[host_strcspn(host, ":")] = '\0';
+        /*
+         * Trim a colon suffix off the hostname if it's there. In
+         * order to protect unbracketed IPv6 address literals
+         * against this treatment, we do not do this if there's
+         * _more_ than one colon.
+         */
+        {
+            char *c = host_strchr(host, ':');
+            if (c) {
+                char *d = host_strchr(c+1, ':');
+                if (!d)
+                    *c = '\0';
+            }
+        }
 
        /*
         * Remove any remaining whitespace.