From 3e8bdb122f12f6606476b6a87f5d3e5295343079 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 29 Apr 2019 10:07:31 +0200 Subject: [PATCH] knownhosts: Check if the hosts file exists Fixes T135 Reported-by: Jan Pazdziora Signed-off-by: Andreas Schneider Reviewed-by: Jakub Jelen --- src/knownhosts.c | 20 ++++++++++++++++++++ tests/unittests/torture_knownhosts_parsing.c | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/src/knownhosts.c b/src/knownhosts.c index 3ea22970..44cfbc0b 100644 --- a/src/knownhosts.c +++ b/src/knownhosts.c @@ -557,6 +557,7 @@ enum ssh_known_hosts_e ssh_session_has_known_hosts_entry(ssh_session session) struct ssh_list *entry_list = NULL; struct ssh_iterator *it = NULL; char *host_port = NULL; + bool ok; int rc; if (session->opts.knownhosts == NULL) { @@ -569,6 +570,25 @@ enum ssh_known_hosts_e ssh_session_has_known_hosts_entry(ssh_session session) } } + if (session->opts.knownhosts == NULL && + session->opts.global_knownhosts == NULL) { + return SSH_KNOWN_HOSTS_NOT_FOUND; + } + + if (session->opts.knownhosts != NULL) { + ok = ssh_file_readaccess_ok(session->opts.knownhosts); + if (!ok) { + return SSH_KNOWN_HOSTS_NOT_FOUND; + } + } + + if (session->opts.global_knownhosts != NULL) { + ok = ssh_file_readaccess_ok(session->opts.global_knownhosts); + if (!ok) { + return SSH_KNOWN_HOSTS_NOT_FOUND; + } + } + host_port = ssh_session_get_host_port(session); if (host_port == NULL) { return SSH_KNOWN_HOSTS_ERROR; diff --git a/tests/unittests/torture_knownhosts_parsing.c b/tests/unittests/torture_knownhosts_parsing.c index 1641db79..bd51fb1e 100644 --- a/tests/unittests/torture_knownhosts_parsing.c +++ b/tests/unittests/torture_knownhosts_parsing.c @@ -256,6 +256,7 @@ static void torture_knownhosts_read_file(void **state) ssh_list_free(entry_list); } +#ifndef _WIN32 /* There is no /dev/null on Windows */ static void torture_knownhosts_host_exists(void **state) { const char *knownhosts_file = *state; @@ -365,6 +366,7 @@ torture_knownhosts_algorithms_global(void **state) ssh_free(session); } +#endif int torture_run_tests(void) { int rc; @@ -378,6 +380,7 @@ int torture_run_tests(void) { cmocka_unit_test_setup_teardown(torture_knownhosts_read_file, setup_knownhosts_file, teardown_knownhosts_file), +#ifndef _WIN32 cmocka_unit_test_setup_teardown(torture_knownhosts_host_exists, setup_knownhosts_file, teardown_knownhosts_file), @@ -390,6 +393,7 @@ int torture_run_tests(void) { cmocka_unit_test_setup_teardown(torture_knownhosts_algorithms_global, setup_knownhosts_file, teardown_knownhosts_file), +#endif }; ssh_init();