summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2023-01-22 21:10:58 +0000
committerEdward Thomson <ethomson@edwardthomson.com>2023-01-23 12:58:00 +0000
commitadc72e10284eb43119954fa4bcc7ff4689017145 (patch)
treea85cbbc1b4243b53f590dc6fe6ef71d0005997ff
parent5fdbc29d0131568a50584223b45f2a4a782ab97d (diff)
downloadlibgit2-adc72e10284eb43119954fa4bcc7ff4689017145.tar.gz
ssh: support windows `known_hosts` files
Use `git_sysdir_find_homedir_file` to identify the path to the home directory's `.ssh/known_hosts`; this takes Windows paths into account by preferring `HOME`, then falling back to `HOMEPATH` and `USERPROFILE` directories.
-rw-r--r--src/libgit2/transports/ssh.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/libgit2/transports/ssh.c b/src/libgit2/transports/ssh.c
index 85e779744..2e7f7b3e0 100644
--- a/src/libgit2/transports/ssh.c
+++ b/src/libgit2/transports/ssh.c
@@ -16,6 +16,7 @@
#include "netops.h"
#include "smart.h"
#include "streams/socket.h"
+#include "sysdir.h"
#include "git2/credential.h"
#include "git2/sys/credential.h"
@@ -421,7 +422,8 @@ static int request_creds(git_credential **out, ssh_subtransport *t, const char *
return 0;
}
-#define KNOWN_HOSTS_FILE ".ssh/known_hosts"
+#define SSH_DIR ".ssh"
+#define KNOWN_HOSTS_FILE "known_hosts"
/*
* Load the known_hosts file.
@@ -430,16 +432,14 @@ static int request_creds(git_credential **out, ssh_subtransport *t, const char *
*/
static int load_known_hosts(LIBSSH2_KNOWNHOSTS **hosts, LIBSSH2_SESSION *session)
{
- git_str path = GIT_STR_INIT, home = GIT_STR_INIT;
+ git_str path = GIT_STR_INIT, sshdir = GIT_STR_INIT;
LIBSSH2_KNOWNHOSTS *known_hosts = NULL;
int error;
GIT_ASSERT_ARG(hosts);
- if ((error = git__getenv(&home, "HOME")) < 0)
- return error;
-
- if ((error = git_str_joinpath(&path, git_str_cstr(&home), KNOWN_HOSTS_FILE)) < 0)
+ if ((error = git_sysdir_expand_homedir_file(&sshdir, SSH_DIR)) < 0 ||
+ (error = git_str_joinpath(&path, git_str_cstr(&sshdir), KNOWN_HOSTS_FILE)) < 0)
goto out;
if ((known_hosts = libssh2_knownhost_init(session)) == NULL) {
@@ -461,7 +461,7 @@ static int load_known_hosts(LIBSSH2_KNOWNHOSTS **hosts, LIBSSH2_SESSION *session
out:
*hosts = known_hosts;
- git_str_clear(&home);
+ git_str_clear(&sshdir);
git_str_clear(&path);
return error;