From 4667a39f7f575778cb5fa5ae03154531438c50a6 Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sat, 22 May 2010 10:40:43 -0400 Subject: [PATCH] Added a port specification to the knownhosts entry when not using port 22. --- src/ssh.c | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/ssh.c b/src/ssh.c index d6ac3a8b..3b7276cb 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -491,6 +491,29 @@ gpg_error_t _setup_ssh_authlist(pwm_t *pwm) return _setup_ssh_auth(pwm); } +static char *host_to_openssh_host(const char *host, int port) +{ + char *buf; + + if (port != 22) { + buf = pwmd_malloc(256); + snprintf(buf, 256, "[%s]:%i", host, port); + } + else + buf = pwmd_strdup(host); + + return buf; +} + +static void add_knownhost(pwm_t *pwm, const char *host, const char *key, + size_t len, int type, struct libssh2_knownhost **dst) +{ + char *buf = host_to_openssh_host(host, pwm->tcp_conn->port); + + libssh2_knownhost_add(pwm->tcp_conn->kh, buf, NULL, key, len, type, dst); + pwmd_free(buf); +} + static gpg_error_t check_known_hosts(pwm_t *pwm) { size_t len; @@ -514,6 +537,16 @@ static gpg_error_t check_known_hosts(pwm_t *pwm) (char *)key, len, LIBSSH2_KNOWNHOST_TYPE_PLAIN|LIBSSH2_KNOWNHOST_KEYENC_RAW, &pwm->tcp_conn->hostent); + + if (n == LIBSSH2_KNOWNHOST_CHECK_NOTFOUND) { + char *buf = host_to_openssh_host(pwm->tcp_conn->host, pwm->tcp_conn->port); + + n = libssh2_knownhost_check(pwm->tcp_conn->kh, buf, (char *)key, len, + LIBSSH2_KNOWNHOST_TYPE_PLAIN|LIBSSH2_KNOWNHOST_KEYENC_RAW, + &pwm->tcp_conn->hostent); + pwmd_free(buf); + } + type = type == LIBSSH2_HOSTKEY_TYPE_RSA ? LIBSSH2_KNOWNHOST_KEY_SSHRSA : LIBSSH2_KNOWNHOST_KEY_SSHDSS; @@ -531,12 +564,9 @@ static gpg_error_t check_known_hosts(pwm_t *pwm) if (rc) return rc; } - - libssh2_knownhost_add(pwm->tcp_conn->kh, pwm->tcp_conn->host, NULL, - key, len, + add_knownhost(pwm, pwm->tcp_conn->host, key, len, LIBSSH2_KNOWNHOST_TYPE_PLAIN | - LIBSSH2_KNOWNHOST_KEYENC_RAW | - type, + LIBSSH2_KNOWNHOST_KEYENC_RAW | type, &pwm->tcp_conn->hostent); /* Adds both the IP and hostname. */ @@ -560,11 +590,9 @@ static gpg_error_t check_known_hosts(pwm_t *pwm) } if (!match) - libssh2_knownhost_add(pwm->tcp_conn->kh, p, NULL, - key, len, + add_knownhost(pwm, p, key, len, LIBSSH2_KNOWNHOST_TYPE_PLAIN | - LIBSSH2_KNOWNHOST_KEYENC_RAW | - type, + LIBSSH2_KNOWNHOST_KEYENC_RAW | type, &pwm->tcp_conn->hostent_ip); } -- 2.11.4.GIT