/*
* 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);
}
}
- /*
- * 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.
}
}
- /*
- * 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.