Skip to content

Commit 00893d2

Browse files
committed
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.
1 parent bce1719 commit 00893d2

File tree

1 file changed

+8
-8
lines changed
  • src/libgit2/transports

1 file changed

+8
-8
lines changed

src/libgit2/transports/ssh.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "netops.h"
1717
#include "smart.h"
1818
#include "streams/socket.h"
19+
#include "sysdir.h"
1920

2021
#include "git2/credential.h"
2122
#include "git2/sys/credential.h"
@@ -421,7 +422,8 @@ static int request_creds(git_credential **out, ssh_subtransport *t, const char *
421422
return 0;
422423
}
423424

424-
#define KNOWN_HOSTS_FILE ".ssh/known_hosts"
425+
#define SSH_DIR ".ssh"
426+
#define KNOWN_HOSTS_FILE "known_hosts"
425427

426428
/*
427429
* Load the known_hosts file.
@@ -430,16 +432,14 @@ static int request_creds(git_credential **out, ssh_subtransport *t, const char *
430432
*/
431433
static int load_known_hosts(LIBSSH2_KNOWNHOSTS **hosts, LIBSSH2_SESSION *session)
432434
{
433-
git_str path = GIT_STR_INIT, home = GIT_STR_INIT;
435+
git_str path = GIT_STR_INIT, sshdir = GIT_STR_INIT;
434436
LIBSSH2_KNOWNHOSTS *known_hosts = NULL;
435437
int error;
436438

437439
GIT_ASSERT_ARG(hosts);
438440

439-
if ((error = git__getenv(&home, "HOME")) < 0)
440-
return error;
441-
442-
if ((error = git_str_joinpath(&path, git_str_cstr(&home), KNOWN_HOSTS_FILE)) < 0)
441+
if ((error = git_sysdir_expand_homedir_file(&sshdir, SSH_DIR)) < 0 ||
442+
(error = git_str_joinpath(&path, git_str_cstr(&sshdir), KNOWN_HOSTS_FILE)) < 0)
443443
goto out;
444444

445445
if ((known_hosts = libssh2_knownhost_init(session)) == NULL) {
@@ -461,8 +461,8 @@ static int load_known_hosts(LIBSSH2_KNOWNHOSTS **hosts, LIBSSH2_SESSION *session
461461
out:
462462
*hosts = known_hosts;
463463

464-
git_str_clear(&home);
465-
git_str_clear(&path);
464+
git_str_dispose(&sshdir);
465+
git_str_dispose(&path);
466466

467467
return error;
468468
}

0 commit comments

Comments
 (0)