From 86ceb337ec340c7db9b060b90bfab05a08b8251b Mon Sep 17 00:00:00 2001 From: Torsten Bögershausen Date: Sat, 21 Feb 2015 16:52:48 +0100 Subject: connect.c: allow ssh://user@[2001:db8::1]/repo.git MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ssh:// syntax was added in 2386d658 (Add first cut at "git protocol" connect logic., 2005-07-13), it accepted ssh://user@2001:db8::1/repo.git, which is now legacy. Over the years the parser was improved to support [] and port numbers, but the combination of ssh://user@[2001:db8::1]:222/repo.git did never work. The only only way to use a user name, a literall IPV6 address and a port number was ssh://[user@2001:db8::1]:222/repo.git (Thanks to Christian Taube for reporting this long standing issue) New users would use ssh://user@[2001:db8::1]:222/repo.git, so change the parser to handle it correctly. Support the old legacy URLs as well, to be backwards compatible, and avoid regressions for users which upgrade an existing installation to a later Git version. Signed-off-by: Torsten Bögershausen Signed-off-by: Junio C Hamano --- connect.c | 63 ++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 25 deletions(-) (limited to 'connect.c') diff --git a/connect.c b/connect.c index d47d0ec604..b608976cca 100644 --- a/connect.c +++ b/connect.c @@ -274,28 +274,44 @@ static enum protocol get_protocol(const char *name) die("I don't handle protocol '%s'", name); } +static char *host_end(char **hoststart, int removebrackets) +{ + char *host = *hoststart; + char *end; + char *start = strstr(host, "@["); + if (start) + start++; /* Jump over '@' */ + else + start = host; + if (start[0] == '[') { + end = strchr(start + 1, ']'); + if (end) { + if (removebrackets) { + *end = 0; + memmove(start, start + 1, end - start); + end++; + } + } else + end = host; + } else + end = host; + return end; +} + #define STR_(s) # s #define STR(s) STR_(s) static void get_host_and_port(char **host, const char **port) { char *colon, *end; - - if (*host[0] == '[') { - end = strchr(*host + 1, ']'); - if (end) { - *end = 0; - end++; - (*host)++; - } else - end = *host; - } else - end = *host; + end = host_end(host, 1); colon = strchr(end, ':'); - if (colon) { - *colon = 0; - *port = colon + 1; + long portnr = strtol(colon + 1, &end, 10); + if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) { + *colon = 0; + *port = colon + 1; + } } } @@ -547,13 +563,16 @@ static struct child_process *git_proxy_connect(int fd[2], char *host) return proxy; } -static const char *get_port_numeric(const char *p) +static char *get_port(char *host) { char *end; + char *p = strchr(host, ':'); + if (p) { long port = strtol(p + 1, &end, 10); if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) { - return p; + *p = '\0'; + return p+1; } } @@ -595,14 +614,7 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host, * Don't do destructive transforms as protocol code does * '[]' unwrapping in get_host_and_port() */ - if (host[0] == '[') { - end = strchr(host + 1, ']'); - if (end) { - end++; - } else - end = host; - } else - end = host; + end = host_end(&host, 0); if (protocol == PROTO_LOCAL) path = end; @@ -705,7 +717,8 @@ struct child_process *git_connect(int fd[2], const char *url, char *ssh_host = hostandport; const char *port = NULL; get_host_and_port(&ssh_host, &port); - port = get_port_numeric(port); + if (!port) + port = get_port(ssh_host); if (!ssh) ssh = "ssh"; -- cgit 1.2.3-korg From 3f55ccab8e0fec73c8e38b909e9bb4963bfb8f6a Mon Sep 17 00:00:00 2001 From: Torsten Bögershausen Date: Sat, 21 Feb 2015 16:52:55 +0100 Subject: t5500: show user name and host in diag-url MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The URL for ssh may have include a username before the hostname, like ssh://user@host/repo. When literal IPV6 addresses are used together with a username, the substring "user@[::1]" must be converted into "user@::1". Make that conversion visible for the user, and write userandhost in the diagnostics Signed-off-by: Torsten Bögershausen Signed-off-by: Junio C Hamano --- connect.c | 35 +++++++++++++++++++++++------------ t/t5500-fetch-pack.sh | 51 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 56 insertions(+), 30 deletions(-) (limited to 'connect.c') diff --git a/connect.c b/connect.c index b608976cca..84f8156a4c 100644 --- a/connect.c +++ b/connect.c @@ -675,7 +675,7 @@ struct child_process *git_connect(int fd[2], const char *url, signal(SIGCHLD, SIG_DFL); protocol = parse_connect_url(url, &hostandport, &path); - if (flags & CONNECT_DIAG_URL) { + if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) { printf("Diag: url=%s\n", url ? url : "NULL"); printf("Diag: protocol=%s\n", prot_name(protocol)); printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL"); @@ -719,18 +719,29 @@ struct child_process *git_connect(int fd[2], const char *url, get_host_and_port(&ssh_host, &port); if (!port) port = get_port(ssh_host); - - if (!ssh) ssh = "ssh"; - - argv_array_push(&conn->args, ssh); - if (putty && !strcasestr(ssh, "tortoiseplink")) - argv_array_push(&conn->args, "-batch"); - if (port) { - /* P is for PuTTY, p is for OpenSSH */ - argv_array_push(&conn->args, putty ? "-P" : "-p"); - argv_array_push(&conn->args, port); + if (flags & CONNECT_DIAG_URL) { + printf("Diag: url=%s\n", url ? url : "NULL"); + printf("Diag: protocol=%s\n", prot_name(protocol)); + printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL"); + printf("Diag: port=%s\n", port ? port : "NONE"); + printf("Diag: path=%s\n", path ? path : "NULL"); + + free(hostandport); + free(path); + return NULL; + } else { + if (!ssh) ssh = "ssh"; + + argv_array_push(&conn->args, ssh); + if (putty && !strcasestr(ssh, "tortoiseplink")) + argv_array_push(&conn->args, "-batch"); + if (port) { + /* P is for PuTTY, p is for OpenSSH */ + argv_array_push(&conn->args, putty ? "-P" : "-p"); + argv_array_push(&conn->args, port); + } + argv_array_push(&conn->args, ssh_host); } - argv_array_push(&conn->args, ssh_host); } else { /* remove repo-local variables from the environment */ conn->env = local_repo_env; diff --git a/t/t5500-fetch-pack.sh b/t/t5500-fetch-pack.sh index 5b2b1c2c13..bd37f040b6 100755 --- a/t/t5500-fetch-pack.sh +++ b/t/t5500-fetch-pack.sh @@ -541,13 +541,30 @@ check_prot_path () { test_cmp expected actual } -check_prot_host_path () { - cat >expected <<-EOF && +check_prot_host_port_path () { + local diagport + case "$2" in + *ssh*) + pp=ssh + uah=userandhost + ehost=$(echo $3 | tr -d "[]") + diagport="Diag: port=$4" + ;; + *) + pp=$p + uah=hostandport + ehost=$(echo $3$4 | sed -e "s/22$/:22/" -e "s/NONE//") + diagport="" + ;; + esac + cat >exp <<-EOF && Diag: url=$1 - Diag: protocol=$2 - Diag: hostandport=$3 - Diag: path=$4 + Diag: protocol=$pp + Diag: $uah=$ehost + $diagport + Diag: path=$5 EOF + grep -v "^$" exp >expected git fetch-pack --diag-url "$1" >actual && test_cmp expected actual } @@ -557,22 +574,20 @@ do # git or ssh with scheme for p in "ssh+git" "git+ssh" git ssh do - for h in host host:12 [::1] [::1]:23 + for h in host user@host user@[::1] user@::1 do - case "$p" in - *ssh*) - pp=ssh - ;; - *) - pp=$p - ;; - esac test_expect_success "fetch-pack --diag-url $p://$h/$r" ' - check_prot_host_path $p://$h/$r $pp "$h" "/$r" + check_prot_host_port_path $p://$h/$r $p "$h" NONE "/$r" ' # "/~" -> "~" conversion test_expect_success "fetch-pack --diag-url $p://$h/~$r" ' - check_prot_host_path $p://$h/~$r $pp "$h" "~$r" + check_prot_host_port_path $p://$h/~$r $p "$h" NONE "~$r" + ' + done + for h in host User@host User@[::1] + do + test_expect_success "fetch-pack --diag-url $p://$h:22/$r" ' + check_prot_host_port_path $p://$h:22/$r $p "$h" 22 "/$r" ' done done @@ -603,11 +618,11 @@ do for h in host [::1] do test_expect_success "fetch-pack --diag-url $h:$r" ' - check_prot_path $h:$r $p "$r" + check_prot_host_port_path $h:$r $p "$h" NONE "$r" ' # Do "/~" -> "~" conversion test_expect_success "fetch-pack --diag-url $h:/~$r" ' - check_prot_host_path $h:/~$r $p "$h" "~$r" + check_prot_host_port_path $h:/~$r $p "$h" NONE "~$r" ' done done -- cgit 1.2.3-korg