From e9d6b15926214706bdc46ea1eccfab812956df1e Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Fri, 7 May 2010 12:55:33 +0200 Subject: [PATCH] First implementation of known_hosts with port read Conflicts: include/libssh/misc.h libssh/keyfiles.c libssh/misc.c --- include/libssh/misc.h | 1 + libssh/keyfiles.c | 13 ++++++++++--- libssh/misc.c | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/libssh/misc.h b/include/libssh/misc.h index ddd72fe7..666b0782 100644 --- a/include/libssh/misc.h +++ b/include/libssh/misc.h @@ -50,6 +50,7 @@ struct ssh_iterator *ssh_list_get_iterator(const struct ssh_list *list); int ssh_list_append(struct ssh_list *list, const void *data); int ssh_list_prepend(struct ssh_list *list, const void *data); void ssh_list_remove(struct ssh_list *list, struct ssh_iterator *iterator); +char *ssh_hostport(const char *host, int port); const void *_ssh_list_pop_head(struct ssh_list *list); diff --git a/libssh/keyfiles.c b/libssh/keyfiles.c index a25af893..49741f3a 100644 --- a/libssh/keyfiles.c +++ b/libssh/keyfiles.c @@ -1570,6 +1570,7 @@ int ssh_is_server_known(ssh_session session) { FILE *file = NULL; char **tokens; char *host; + char *hostport; const char *type; int match; int ret = SSH_SERVER_NOT_KNOWN; @@ -1593,8 +1594,11 @@ int ssh_is_server_known(ssh_session session) { } host = lowercase(session->host); - if (host == NULL) { - ssh_set_error(session, SSH_FATAL, "Not enough space!"); + hostport = ssh_hostport(host,session->port); + if (host == NULL || hostport == NULL) { + ssh_set_error_oom(session); + SAFE_FREE(host); + SAFE_FREE(hostport); leave_function(); return SSH_SERVER_ERROR; } @@ -1608,10 +1612,12 @@ int ssh_is_server_known(ssh_session session) { break; } match = match_hashed_host(session, host, tokens[0]); + if (match == 0){ + match = match_hostname(hostport, tokens[0], strlen(tokens[0])); + } if (match == 0) { match = match_hostname(host, tokens[0], strlen(tokens[0])); } - if (match) { /* We got a match. Now check the key type */ if (strcmp(session->current_crypto->server_pubkey_type, type) != 0) { @@ -1642,6 +1648,7 @@ int ssh_is_server_known(ssh_session session) { } while (1); SAFE_FREE(host); + SAFE_FREE(hostport); if (file != NULL) { fclose(file); } diff --git a/libssh/misc.c b/libssh/misc.c index 22fb1a65..7e936e15 100644 --- a/libssh/misc.c +++ b/libssh/misc.c @@ -118,6 +118,20 @@ int gettimeofday(struct timeval *__p, void *__t) { #define NSS_BUFLEN_PASSWD 4096 #endif +char *ssh_hostport(const char *host, int port){ + char *dest; + size_t len; + if(host==NULL) + return NULL; + /* 3 for []:, 5 for 65536 and 1 for nul */ + len=strlen(host) + 3 + 5 + 1; + dest=malloc(len); + if(dest==NULL) + return NULL; + snprintf(dest,len,"[%s]:%d",host,port); + return dest; +} + char *ssh_get_user_home_dir(void) { char *szPath = NULL; struct passwd pwd;