Commit 62a4c94cd911a5c1ff54c4787c05d0876d30f07b

Stefan Sperling 2020-03-20T15:01:15

support non-default port numbers with SSH and tweak URI parser port handling

diff --git a/got/got.c b/got/got.c
index 30f18d7..9d94bcc 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1013,7 +1013,8 @@ cmd_clone(int argc, char *argv[])
 		goto done;
 
 	if (verbosity >= 0)
-		printf("Connected to %s:%s\n", host, port);
+		printf("Connected to %s%s%s\n", host,
+		    port ? ":" : "", port ? port : "");
 
 	/* Create a config file git-fetch(1) can understand. */
 	gitconfig_path = got_repo_get_path_gitconfig(repo);
@@ -1384,7 +1385,8 @@ cmd_fetch(int argc, char *argv[])
 		goto done;
 
 	if (verbosity >= 0)
-		printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
+		printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
+		    port ? ":" : "", port ? port : "");
 
 	fpa.last_scaled_size[0] = '\0';
 	fpa.last_p_indexed = -1;
diff --git a/lib/fetch.c b/lib/fetch.c
index 8edac2b..b5b03f3 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -89,24 +89,29 @@ dial_ssh(int *fetchfd, const char *host, const char *port, const char *path,
 	const struct got_error *error = NULL;
 	int pid, pfd[2];
 	char cmd[64];
-	char *argv[9];
+	char *argv[11];
 	int i = 0;
 
 	*fetchfd = -1;
 
+	if (port == NULL)
+		port = "22";
+
 	argv[0] = GOT_FETCH_PATH_SSH;
+	argv[1] = "-p";
+	argv[2] = (char *)port;
 	if (verbosity == -1) {
-		argv[1 + i++] = "-q";
+		argv[3 + i++] = "-q";
 	} else {
 		/* ssh(1) allows up to 3 "-v" options. */
 		for (i = 0; i < MIN(3, verbosity); i++)
-			argv[1 + i] = "-v";
+			argv[3 + i] = "-v";
 	}
-	argv[1 + i] = "--";
-	argv[2 + i] = (char *)host;
-	argv[3 + i] = (char *)cmd;
-	argv[4 + i] = (char *)path;
-	argv[5 + i] = NULL;
+	argv[3 + i] = "--";
+	argv[4 + i] = (char *)host;
+	argv[5 + i] = (char *)cmd;
+	argv[6 + i] = (char *)path;
+	argv[7 + i] = NULL;
 
 	if (pipe(pfd) == -1)
 		return got_error_from_errno("pipe");
@@ -146,6 +151,9 @@ dial_git(int *fetchfd, const char *host, const char *port, const char *path,
 
 	*fetchfd = -1;
 
+	if (port == NULL)
+		port = GOT_DEFAULT_GIT_PORT_STR;
+
 	memset(&hints, 0, sizeof hints);
 	hints.ai_family = AF_UNSPEC;
 	hints.ai_socktype = SOCK_STREAM;
@@ -241,11 +249,6 @@ got_fetch_parse_uri(char **proto, char **host, char **port,
 			err = got_error_from_errno("strdup");
 			goto done;
 		}
-		*port = strdup("22");
-		if (*port == NULL) {
-			err = got_error_from_errno("strdup");
-			goto done;
-		}
 		s = (char *)uri;
 		q = strchr(s, ':');
 		if (q == NULL) {
@@ -296,10 +299,6 @@ got_fetch_parse_uri(char **proto, char **host, char **port,
 				err = got_error_from_errno("strndup");
 				goto done;
 			}
-			if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
-				err = got_error_from_errno("asprintf");
-				goto done;
-			}
 		}
 	}
 
diff --git a/regress/fetch/fetch_test.c b/regress/fetch/fetch_test.c
index 31b97a8..1d4429c 100644
--- a/regress/fetch/fetch_test.c
+++ b/regress/fetch/fetch_test.c
@@ -83,23 +83,23 @@ fetch_parse_uri(void)
 		    NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
 
 		{ "git://127.0.0.1/git/myrepo",
-		    "git", "127.0.0.1", GOT_DEFAULT_GIT_PORT_STR,
+		    "git", "127.0.0.1", NULL,
 		    "/git/myrepo", "myrepo", GOT_ERR_OK },
 		{ "http://127.0.0.1/git/myrepo",
-		    "http", "127.0.0.1", GOT_DEFAULT_GIT_PORT_STR,
+		    "http", "127.0.0.1", NULL,
 		    "/git/myrepo", "myrepo", GOT_ERR_OK },
 		{ "gopher://127.0.0.1/git/myrepo",
-		    "gopher", "127.0.0.1", GOT_DEFAULT_GIT_PORT_STR,
+		    "gopher", "127.0.0.1", NULL,
 		    "/git/myrepo", "myrepo", GOT_ERR_OK },
 
 		{ "git://127.0.0.1:22/git/myrepo",
 		    "git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
 		    GOT_ERR_OK },
 		{ "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
-		    "git", "127.0.0.1", GOT_DEFAULT_GIT_PORT_STR,
+		    "git", "127.0.0.1", NULL,
 		    "/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
 		{ "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
-		    "https", "127.0.0.1", GOT_DEFAULT_GIT_PORT_STR,
+		    "https", "127.0.0.1", NULL,
 		    "/git/repos/foo/../bar/myrepo.git", "myrepo",
 		    GOT_ERR_OK },
 
@@ -111,13 +111,13 @@ fetch_parse_uri(void)
 		    GOT_ERR_OK },
 
 		{ "127.0.0.1:git/myrepo",
-		    "ssh", "127.0.0.1", "22", "git/myrepo", "myrepo",
+		    "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
 		    GOT_ERR_OK },
 		{ "127.0.0.1:/git/myrepo",
-		    "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
+		    "ssh", "127.0.0.1", NULL, "/git/myrepo", "myrepo",
 		    GOT_ERR_OK },
 		{ "127.0.0.1:22/git/myrepo",
-		    "ssh", "127.0.0.1", "22", "22/git/myrepo", "myrepo",
+		    "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
 		    GOT_ERR_OK },
 	};
 	int i;