1
1

Bug: [ 1592645 ] Public key can not be readed from file

While posting a public key file to a remote server via cut-n-paste it
is possible that the key won't have the proper ending.  It might not have
the standard carriage return or line feed.  It might even have extra
spaces.  This patch is based on the originators original patch, but is
more extensive.  If reading the file ends in EOF, remove that character.
Then if there are spaces at the end of the file remove them also.

This does not fix the posibility of the same error in a multi-key file, but
it is a start.
Этот коммит содержится в:
James Housley 2006-11-13 11:33:03 +00:00
родитель 7063d24724
Коммит bebd14a011

Просмотреть файл

@ -37,6 +37,8 @@
#include "libssh2_priv.h" #include "libssh2_priv.h"
#include <ctype.h>
/* Needed for struct iovec on some platforms */ /* Needed for struct iovec on some platforms */
#ifdef HAVE_SYS_UIO_H #ifdef HAVE_SYS_UIO_H
#include <sys/uio.h> #include <sys/uio.h>
@ -239,7 +241,8 @@ static int libssh2_file_read_publickey(LIBSSH2_SESSION *session, unsigned char *
{ {
FILE *fd; FILE *fd;
char *pubkey = NULL, c, *sp1, *sp2, *tmp; char *pubkey = NULL, c, *sp1, *sp2, *tmp;
int pubkey_len = 0, tmp_len; size_t pubkey_len = 0;
unsigned int tmp_len;
#ifdef LIBSSH2_DEBUG_USERAUTH #ifdef LIBSSH2_DEBUG_USERAUTH
_libssh2_debug(session, LIBSSH2_DBG_AUTH, "Loading public key file: %s", pubkeyfile); _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Loading public key file: %s", pubkeyfile);
@ -252,6 +255,10 @@ static int libssh2_file_read_publickey(LIBSSH2_SESSION *session, unsigned char *
} }
while (!feof(fd) && (c = fgetc(fd)) != '\r' && c != '\n') pubkey_len++; while (!feof(fd) && (c = fgetc(fd)) != '\r' && c != '\n') pubkey_len++;
rewind(fd); rewind(fd);
if (feof(fd)) {
/* the last character was EOF */
pubkey_len--;
}
if (pubkey_len <= 1) { if (pubkey_len <= 1) {
libssh2_error(session, LIBSSH2_ERROR_FILE, "Invalid data in public key file", 0); libssh2_error(session, LIBSSH2_ERROR_FILE, "Invalid data in public key file", 0);
@ -272,7 +279,10 @@ static int libssh2_file_read_publickey(LIBSSH2_SESSION *session, unsigned char *
return -1; return -1;
} }
fclose(fd); fclose(fd);
while (pubkey_len && (pubkey[pubkey_len-1] == '\r' || pubkey[pubkey_len-1] == '\n')) pubkey_len--; /*
* Remove trailing whitespace
*/
while (pubkey_len && isspace(pubkey[pubkey_len-1])) pubkey_len--;
if (!pubkey_len) { if (!pubkey_len) {
libssh2_error(session, LIBSSH2_ERROR_FILE, "Missing public key data", 0); libssh2_error(session, LIBSSH2_ERROR_FILE, "Missing public key data", 0);