Commit 00893d2f by Edward Thomson

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.
parent bce17198
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "netops.h" #include "netops.h"
#include "smart.h" #include "smart.h"
#include "streams/socket.h" #include "streams/socket.h"
#include "sysdir.h"
#include "git2/credential.h" #include "git2/credential.h"
#include "git2/sys/credential.h" #include "git2/sys/credential.h"
...@@ -421,7 +422,8 @@ static int request_creds(git_credential **out, ssh_subtransport *t, const char * ...@@ -421,7 +422,8 @@ static int request_creds(git_credential **out, ssh_subtransport *t, const char *
return 0; return 0;
} }
#define KNOWN_HOSTS_FILE ".ssh/known_hosts" #define SSH_DIR ".ssh"
#define KNOWN_HOSTS_FILE "known_hosts"
/* /*
* Load the known_hosts file. * Load the known_hosts file.
...@@ -430,16 +432,14 @@ static int request_creds(git_credential **out, ssh_subtransport *t, const char * ...@@ -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) 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; LIBSSH2_KNOWNHOSTS *known_hosts = NULL;
int error; int error;
GIT_ASSERT_ARG(hosts); GIT_ASSERT_ARG(hosts);
if ((error = git__getenv(&home, "HOME")) < 0) if ((error = git_sysdir_expand_homedir_file(&sshdir, SSH_DIR)) < 0 ||
return error; (error = git_str_joinpath(&path, git_str_cstr(&sshdir), KNOWN_HOSTS_FILE)) < 0)
if ((error = git_str_joinpath(&path, git_str_cstr(&home), KNOWN_HOSTS_FILE)) < 0)
goto out; goto out;
if ((known_hosts = libssh2_knownhost_init(session)) == NULL) { if ((known_hosts = libssh2_knownhost_init(session)) == NULL) {
...@@ -461,8 +461,8 @@ static int load_known_hosts(LIBSSH2_KNOWNHOSTS **hosts, LIBSSH2_SESSION *session ...@@ -461,8 +461,8 @@ static int load_known_hosts(LIBSSH2_KNOWNHOSTS **hosts, LIBSSH2_SESSION *session
out: out:
*hosts = known_hosts; *hosts = known_hosts;
git_str_clear(&home); git_str_dispose(&sshdir);
git_str_clear(&path); git_str_dispose(&path);
return error; return error;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment