mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-09 09:54:25 +09:00
knownhosts: Check if the hosts file exists
Fixes T135 Reported-by: Jan Pazdziora <jpazdziora@redhat.com> Signed-off-by: Andreas Schneider <asn@cryptomilk.org> Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
@@ -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_list *entry_list = NULL;
|
||||||
struct ssh_iterator *it = NULL;
|
struct ssh_iterator *it = NULL;
|
||||||
char *host_port = NULL;
|
char *host_port = NULL;
|
||||||
|
bool ok;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
if (session->opts.knownhosts == NULL) {
|
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);
|
host_port = ssh_session_get_host_port(session);
|
||||||
if (host_port == NULL) {
|
if (host_port == NULL) {
|
||||||
return SSH_KNOWN_HOSTS_ERROR;
|
return SSH_KNOWN_HOSTS_ERROR;
|
||||||
|
|||||||
@@ -256,6 +256,7 @@ static void torture_knownhosts_read_file(void **state)
|
|||||||
ssh_list_free(entry_list);
|
ssh_list_free(entry_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef _WIN32 /* There is no /dev/null on Windows */
|
||||||
static void torture_knownhosts_host_exists(void **state)
|
static void torture_knownhosts_host_exists(void **state)
|
||||||
{
|
{
|
||||||
const char *knownhosts_file = *state;
|
const char *knownhosts_file = *state;
|
||||||
@@ -365,6 +366,7 @@ torture_knownhosts_algorithms_global(void **state)
|
|||||||
|
|
||||||
ssh_free(session);
|
ssh_free(session);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int torture_run_tests(void) {
|
int torture_run_tests(void) {
|
||||||
int rc;
|
int rc;
|
||||||
@@ -378,6 +380,7 @@ int torture_run_tests(void) {
|
|||||||
cmocka_unit_test_setup_teardown(torture_knownhosts_read_file,
|
cmocka_unit_test_setup_teardown(torture_knownhosts_read_file,
|
||||||
setup_knownhosts_file,
|
setup_knownhosts_file,
|
||||||
teardown_knownhosts_file),
|
teardown_knownhosts_file),
|
||||||
|
#ifndef _WIN32
|
||||||
cmocka_unit_test_setup_teardown(torture_knownhosts_host_exists,
|
cmocka_unit_test_setup_teardown(torture_knownhosts_host_exists,
|
||||||
setup_knownhosts_file,
|
setup_knownhosts_file,
|
||||||
teardown_knownhosts_file),
|
teardown_knownhosts_file),
|
||||||
@@ -390,6 +393,7 @@ int torture_run_tests(void) {
|
|||||||
cmocka_unit_test_setup_teardown(torture_knownhosts_algorithms_global,
|
cmocka_unit_test_setup_teardown(torture_knownhosts_algorithms_global,
|
||||||
setup_knownhosts_file,
|
setup_knownhosts_file,
|
||||||
teardown_knownhosts_file),
|
teardown_knownhosts_file),
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
ssh_init();
|
ssh_init();
|
||||||
|
|||||||
Reference in New Issue
Block a user