Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 83f0be1

Browse files
ansasakicryptomilk
authored andcommitted
knownhosts: Do not fail if global known_hosts file is inaccessible
Previously, if the global known_hosts file (default: /etc/ssh/ssh_known_hosts) was inaccessible, the check for known hosts failed. This makes the check to fail if both files are inaccessible. Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org> Reviewed-by: Jakub Jelen <jjelen@redhat.com> (cherry picked from commit 4adb13d)
1 parent 3bc5f88 commit 83f0be1

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

src/knownhosts.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -638,38 +638,54 @@ enum ssh_known_hosts_e ssh_session_has_known_hosts_entry(ssh_session session)
638638
struct ssh_list *entry_list = NULL;
639639
struct ssh_iterator *it = NULL;
640640
char *host_port = NULL;
641-
bool ok;
641+
bool global_known_hosts_found = false;
642+
bool known_hosts_found = false;
642643
int rc;
643644

644645
if (session->opts.knownhosts == NULL) {
645646
if (ssh_options_apply(session) < 0) {
646647
ssh_set_error(session,
647648
SSH_REQUEST_DENIED,
648-
"Can't find a known_hosts file");
649+
"Cannot find a known_hosts file");
649650

650651
return SSH_KNOWN_HOSTS_NOT_FOUND;
651652
}
652653
}
653654

654655
if (session->opts.knownhosts == NULL &&
655656
session->opts.global_knownhosts == NULL) {
657+
ssh_set_error(session,
658+
SSH_REQUEST_DENIED,
659+
"No path set for a known_hosts file");
660+
656661
return SSH_KNOWN_HOSTS_NOT_FOUND;
657662
}
658663

659664
if (session->opts.knownhosts != NULL) {
660-
ok = ssh_file_readaccess_ok(session->opts.knownhosts);
661-
if (!ok) {
662-
return SSH_KNOWN_HOSTS_NOT_FOUND;
665+
known_hosts_found = ssh_file_readaccess_ok(session->opts.knownhosts);
666+
if (!known_hosts_found) {
667+
SSH_LOG(SSH_LOG_WARN, "Cannot access file %s",
668+
session->opts.knownhosts);
663669
}
664670
}
665671

666672
if (session->opts.global_knownhosts != NULL) {
667-
ok = ssh_file_readaccess_ok(session->opts.global_knownhosts);
668-
if (!ok) {
669-
return SSH_KNOWN_HOSTS_NOT_FOUND;
673+
global_known_hosts_found =
674+
ssh_file_readaccess_ok(session->opts.global_knownhosts);
675+
if (!global_known_hosts_found) {
676+
SSH_LOG(SSH_LOG_WARN, "Cannot access file %s",
677+
session->opts.global_knownhosts);
670678
}
671679
}
672680

681+
if ((!known_hosts_found) && (!global_known_hosts_found)) {
682+
ssh_set_error(session,
683+
SSH_REQUEST_DENIED,
684+
"Cannot find a known_hosts file");
685+
686+
return SSH_KNOWN_HOSTS_NOT_FOUND;
687+
}
688+
673689
host_port = ssh_session_get_host_port(session);
674690
if (host_port == NULL) {
675691
return SSH_KNOWN_HOSTS_ERROR;
@@ -682,7 +698,7 @@ enum ssh_known_hosts_e ssh_session_has_known_hosts_entry(ssh_session session)
682698
if (rc != 0) {
683699
SAFE_FREE(host_port);
684700
ssh_list_free(entry_list);
685-
return SSH_KNOWN_HOSTS_UNKNOWN;
701+
return SSH_KNOWN_HOSTS_ERROR;
686702
}
687703
}
688704

@@ -693,7 +709,7 @@ enum ssh_known_hosts_e ssh_session_has_known_hosts_entry(ssh_session session)
693709
SAFE_FREE(host_port);
694710
if (rc != 0) {
695711
ssh_list_free(entry_list);
696-
return SSH_KNOWN_HOSTS_UNKNOWN;
712+
return SSH_KNOWN_HOSTS_ERROR;
697713
}
698714
}
699715

0 commit comments

Comments
 (0)