libssh2: save non-standard port to known_hosts

Reported-by: dyingc on github
Fixes #21863

Closes #21874
This commit is contained in:
Viktor Szakats 2026-06-05 15:56:17 +02:00
parent 5df33efab4
commit 39d5cead0d
No known key found for this signature in database

View file

@ -429,31 +429,42 @@ static CURLcode ssh_knownhost(struct Curl_easy *data,
case CURLKHSTAT_FINE_ADD_TO_FILE:
/* proceed */
if(keycheck != LIBSSH2_KNOWNHOST_CHECK_MATCH) {
/* the found host+key did not match but has been told to be fine
anyway so we add it in memory */
int addrc = libssh2_knownhost_addc(sshc->kh,
conn->origin->hostname, NULL,
remotekey, keylen,
NULL, 0,
LIBSSH2_KNOWNHOST_TYPE_PLAIN |
LIBSSH2_KNOWNHOST_KEYENC_RAW |
keybit, NULL);
if(addrc)
infof(data, "WARNING: adding the known host %s failed",
conn->origin->hostname);
else if(rc == CURLKHSTAT_FINE_ADD_TO_FILE ||
rc == CURLKHSTAT_FINE_REPLACE) {
/* now we write the entire in-memory list of known hosts to the
known_hosts file */
int wrc =
libssh2_knownhost_writefile(sshc->kh,
data->set.str[STRING_SSH_KNOWNHOSTS],
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
if(wrc) {
infof(data, "WARNING: writing %s failed",
data->set.str[STRING_SSH_KNOWNHOSTS]);
int addrc;
const char *hostbuf;
char *hostport = NULL;
if(conn->origin->port != PORT_SSH) {
hostbuf = hostport = curl_maprintf("[%s]:%u", conn->origin->hostname,
conn->origin->port);
if(!hostbuf)
infof(data, "WARNING: failed allocating buffer for [host]:port");
}
else
hostbuf = conn->origin->hostname;
if(hostbuf) {
/* the found host+key did not match but has been told to be fine
anyway so we add it in memory */
addrc = libssh2_knownhost_addc(sshc->kh, hostbuf, NULL,
remotekey, keylen, NULL, 0,
LIBSSH2_KNOWNHOST_TYPE_PLAIN |
LIBSSH2_KNOWNHOST_KEYENC_RAW |
keybit, NULL);
if(addrc)
infof(data, "WARNING: adding the known host %s failed", hostbuf);
else if(rc == CURLKHSTAT_FINE_ADD_TO_FILE ||
rc == CURLKHSTAT_FINE_REPLACE) {
/* now we write the entire in-memory list of known hosts to the
known_hosts file */
int wrc =
libssh2_knownhost_writefile(sshc->kh,
data->set.str[STRING_SSH_KNOWNHOSTS],
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
if(wrc) {
infof(data, "WARNING: writing %s failed",
data->set.str[STRING_SSH_KNOWNHOSTS]);
}
}
}
curlx_free(hostport);
}
break;
}