mirror of
https://github.com/libssh2/libssh2.git
synced 2025-11-21 14:00:51 +03:00
Style improvements to knownhost error handling.
Made improvements as suggested by Peter Stuge: http://www.libssh2.org/mail/libssh2-devel-archive-2010-02/0161.shtml.
This commit is contained in:
@@ -152,17 +152,15 @@ libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
char *ptr;
|
char *ptr;
|
||||||
unsigned int ptrlen;
|
unsigned int ptrlen;
|
||||||
|
|
||||||
if(!entry) {
|
if(!entry)
|
||||||
return libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
|
return libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
|
||||||
"Unable to allocate memory for known host "
|
"Unable to allocate memory for known host "
|
||||||
"entry", 0);
|
"entry", 0);
|
||||||
}
|
|
||||||
|
|
||||||
if(!(typemask & LIBSSH2_KNOWNHOST_KEY_MASK)) {
|
|
||||||
/* make sure we have a key type set */
|
/* make sure we have a key type set */
|
||||||
|
if(!(typemask & LIBSSH2_KNOWNHOST_KEY_MASK))
|
||||||
return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL,
|
return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL,
|
||||||
"No key type set", 0);
|
"No key type set", 0);
|
||||||
}
|
|
||||||
|
|
||||||
memset(entry, 0, sizeof(struct known_host));
|
memset(entry, 0, sizeof(struct known_host));
|
||||||
|
|
||||||
@@ -370,10 +368,9 @@ libssh2_knownhost_del(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
struct known_host *node;
|
struct known_host *node;
|
||||||
|
|
||||||
/* check that this was retrieved the right way or get out */
|
/* check that this was retrieved the right way or get out */
|
||||||
if(!entry || (entry->magic != KNOWNHOST_MAGIC)) {
|
if(!entry || (entry->magic != KNOWNHOST_MAGIC))
|
||||||
return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL,
|
return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL,
|
||||||
"Invalid host information", 0);
|
"Invalid host information", 0);
|
||||||
}
|
|
||||||
|
|
||||||
/* get the internal node pointer */
|
/* get the internal node pointer */
|
||||||
node = entry->node;
|
node = entry->node;
|
||||||
@@ -467,12 +464,11 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
if(*p=='|') {
|
if(*p=='|') {
|
||||||
const char *hash = NULL;
|
const char *hash = NULL;
|
||||||
size_t saltlen = p - salt;
|
size_t saltlen = p - salt;
|
||||||
if(saltlen >= (sizeof(saltbuf)-1)) { /* weird length */
|
if(saltlen >= (sizeof(saltbuf)-1)) /* weird length */
|
||||||
return libssh2_error(hosts->session,
|
return libssh2_error(hosts->session,
|
||||||
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
||||||
"Failed to parse known_hosts line "
|
"Failed to parse known_hosts line "
|
||||||
"(unexpectedly long salt)", 0);
|
"(unexpectedly long salt)", 0);
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(saltbuf, salt, saltlen);
|
memcpy(saltbuf, salt, saltlen);
|
||||||
saltbuf[saltlen] = 0; /* zero terminate */
|
saltbuf[saltlen] = 0; /* zero terminate */
|
||||||
@@ -491,12 +487,11 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
/* make some checks that the lengths seem sensible */
|
/* make some checks that the lengths seem sensible */
|
||||||
if((keylen < 20) ||
|
if((keylen < 20) ||
|
||||||
(seplen >= sizeof(hostbuf)-1) ||
|
(seplen >= sizeof(hostbuf)-1) ||
|
||||||
(hostlen >= sizeof(hostbuf)-1)) {
|
(hostlen >= sizeof(hostbuf)-1))
|
||||||
return libssh2_error(hosts->session,
|
return libssh2_error(hosts->session,
|
||||||
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
||||||
"Failed to parse known_hosts line "
|
"Failed to parse known_hosts line "
|
||||||
"(unexpected length)", 0);
|
"(unexpected length)", 0);
|
||||||
}
|
|
||||||
|
|
||||||
switch(key[0]) {
|
switch(key[0]) {
|
||||||
case '0': case '1': case '2': case '3': case '4':
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
@@ -515,12 +510,11 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
type |= LIBSSH2_KNOWNHOST_KEY_SSHDSS;
|
type |= LIBSSH2_KNOWNHOST_KEY_SSHDSS;
|
||||||
else if(!strncmp(key, "ssh-rsa", 7))
|
else if(!strncmp(key, "ssh-rsa", 7))
|
||||||
type |= LIBSSH2_KNOWNHOST_KEY_SSHRSA;
|
type |= LIBSSH2_KNOWNHOST_KEY_SSHRSA;
|
||||||
else {
|
else
|
||||||
/* unknown key type */
|
/* unknown key type */
|
||||||
return libssh2_error(hosts->session,
|
return libssh2_error(hosts->session,
|
||||||
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
||||||
"Unknown key type", 0);
|
"Unknown key type", 0);
|
||||||
}
|
|
||||||
|
|
||||||
key += 7;
|
key += 7;
|
||||||
keylen -= 7;
|
keylen -= 7;
|
||||||
@@ -601,12 +595,11 @@ libssh2_knownhost_readline(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
size_t keylen;
|
size_t keylen;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH) {
|
if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH)
|
||||||
return libssh2_error(hosts->session,
|
return libssh2_error(hosts->session,
|
||||||
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
||||||
"Unsupported type of known-host information "
|
"Unsupported type of known-host information "
|
||||||
"store", 0);
|
"store", 0);
|
||||||
}
|
|
||||||
|
|
||||||
cp = line;
|
cp = line;
|
||||||
|
|
||||||
@@ -637,12 +630,10 @@ libssh2_knownhost_readline(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
len--;
|
len--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!*cp || !len)
|
if(!*cp || !len) /* illegal line */
|
||||||
/* illegal line */ {
|
|
||||||
return libssh2_error(hosts->session,
|
return libssh2_error(hosts->session,
|
||||||
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
||||||
"Failed to parse known_hosts line", 0);
|
"Failed to parse known_hosts line", 0);
|
||||||
}
|
|
||||||
|
|
||||||
keyp = cp; /* the key starts here */
|
keyp = cp; /* the key starts here */
|
||||||
keylen = len;
|
keylen = len;
|
||||||
@@ -682,12 +673,11 @@ libssh2_knownhost_readfile(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
int num = 0;
|
int num = 0;
|
||||||
char buf[2048];
|
char buf[2048];
|
||||||
|
|
||||||
if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH) {
|
if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH)
|
||||||
return libssh2_error(hosts->session,
|
return libssh2_error(hosts->session,
|
||||||
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
||||||
"Unsupported type of known-host information "
|
"Unsupported type of known-host information "
|
||||||
"store", 0);
|
"store", 0);
|
||||||
}
|
|
||||||
|
|
||||||
file = fopen(filename, "r");
|
file = fopen(filename, "r");
|
||||||
if(file) {
|
if(file) {
|
||||||
@@ -698,10 +688,10 @@ libssh2_knownhost_readfile(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
}
|
}
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
return libssh2_error(hosts->session, LIBSSH2_ERROR_FILE,
|
return libssh2_error(hosts->session, LIBSSH2_ERROR_FILE,
|
||||||
"Failed to open file", 0);
|
"Failed to open file", 0);
|
||||||
}
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -735,12 +725,11 @@ knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
|
|
||||||
/* we only support this single file type for now, bail out on all other
|
/* we only support this single file type for now, bail out on all other
|
||||||
attempts */
|
attempts */
|
||||||
if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH) {
|
if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH)
|
||||||
return libssh2_error(hosts->session,
|
return libssh2_error(hosts->session,
|
||||||
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
||||||
"Unsupported type of known-host information "
|
"Unsupported type of known-host information "
|
||||||
"store", 0);
|
"store", 0);
|
||||||
}
|
|
||||||
|
|
||||||
tindex = (node->typemask & LIBSSH2_KNOWNHOST_KEY_MASK) >>
|
tindex = (node->typemask & LIBSSH2_KNOWNHOST_KEY_MASK) >>
|
||||||
LIBSSH2_KNOWNHOST_KEY_SHIFT;
|
LIBSSH2_KNOWNHOST_KEY_SHIFT;
|
||||||
@@ -754,11 +743,10 @@ knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
char *saltalloc;
|
char *saltalloc;
|
||||||
nlen = _libssh2_base64_encode(hosts->session, node->name,
|
nlen = _libssh2_base64_encode(hosts->session, node->name,
|
||||||
node->name_len, &namealloc);
|
node->name_len, &namealloc);
|
||||||
if(!nlen) {
|
if(!nlen)
|
||||||
return libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
|
return libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
|
||||||
"Unable to allocate memory for "
|
"Unable to allocate memory for "
|
||||||
"base64-encoded host name", 0);
|
"base64-encoded host name", 0);
|
||||||
}
|
|
||||||
|
|
||||||
nlen = _libssh2_base64_encode(hosts->session,
|
nlen = _libssh2_base64_encode(hosts->session,
|
||||||
node->salt, node->salt_len,
|
node->salt, node->salt_len,
|
||||||
@@ -776,10 +764,9 @@ knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
if(nlen <= buflen)
|
if(nlen <= buflen)
|
||||||
sprintf(buf, "|1|%s|%s%s %s\n", saltalloc, namealloc, keytype,
|
sprintf(buf, "|1|%s|%s%s %s\n", saltalloc, namealloc, keytype,
|
||||||
node->key);
|
node->key);
|
||||||
else {
|
else
|
||||||
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||||
"Known-host write buffer too small", 0);
|
"Known-host write buffer too small", 0);
|
||||||
}
|
|
||||||
|
|
||||||
free(namealloc);
|
free(namealloc);
|
||||||
free(saltalloc);
|
free(saltalloc);
|
||||||
@@ -790,11 +777,10 @@ knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
if(nlen <= buflen)
|
if(nlen <= buflen)
|
||||||
/* these types have the plain name */
|
/* these types have the plain name */
|
||||||
sprintf(buf, "%s%s %s\n", node->name, keytype, node->key);
|
sprintf(buf, "%s%s %s\n", node->name, keytype, node->key);
|
||||||
else {
|
else
|
||||||
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||||
"Known-host write buffer too small", 0);
|
"Known-host write buffer too small", 0);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* we report the full length of the data with the trailing zero excluded */
|
/* we report the full length of the data with the trailing zero excluded */
|
||||||
*outlen = nlen-1;
|
*outlen = nlen-1;
|
||||||
@@ -819,10 +805,9 @@ libssh2_knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
{
|
{
|
||||||
struct known_host *node;
|
struct known_host *node;
|
||||||
|
|
||||||
if(known->magic != KNOWNHOST_MAGIC) {
|
if(known->magic != KNOWNHOST_MAGIC)
|
||||||
return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL,
|
return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL,
|
||||||
"Invalid host information", 0);
|
"Invalid host information", 0);
|
||||||
}
|
|
||||||
|
|
||||||
node = known->node;
|
node = known->node;
|
||||||
|
|
||||||
@@ -845,18 +830,16 @@ libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts,
|
|||||||
|
|
||||||
/* we only support this single file type for now, bail out on all other
|
/* we only support this single file type for now, bail out on all other
|
||||||
attempts */
|
attempts */
|
||||||
if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH) {
|
if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH)
|
||||||
return libssh2_error(hosts->session,
|
return libssh2_error(hosts->session,
|
||||||
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
|
||||||
"Unsupported type of known-host information "
|
"Unsupported type of known-host information "
|
||||||
"store", 0);
|
"store", 0);
|
||||||
}
|
|
||||||
|
|
||||||
file = fopen(filename, "w");
|
file = fopen(filename, "w");
|
||||||
if(!file) {
|
if(!file)
|
||||||
return libssh2_error(hosts->session, LIBSSH2_ERROR_FILE,
|
return libssh2_error(hosts->session, LIBSSH2_ERROR_FILE,
|
||||||
"Failed to open file", 0);
|
"Failed to open file", 0);
|
||||||
}
|
|
||||||
|
|
||||||
for(node = _libssh2_list_first(&hosts->head);
|
for(node = _libssh2_list_first(&hosts->head);
|
||||||
node;
|
node;
|
||||||
|
|||||||
22
src/misc.c
22
src/misc.c
@@ -49,8 +49,6 @@
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#ifdef LIBSSH2DEBUG
|
|
||||||
|
|
||||||
int libssh2_error(LIBSSH2_SESSION* session, int errcode, char* errmsg,
|
int libssh2_error(LIBSSH2_SESSION* session, int errcode, char* errmsg,
|
||||||
int should_free)
|
int should_free)
|
||||||
{
|
{
|
||||||
@@ -61,30 +59,14 @@ int libssh2_error(LIBSSH2_SESSION* session, int errcode, char* errmsg,
|
|||||||
session->err_msglen = strlen(errmsg);
|
session->err_msglen = strlen(errmsg);
|
||||||
session->err_should_free = should_free;
|
session->err_should_free = should_free;
|
||||||
session->err_code = errcode;
|
session->err_code = errcode;
|
||||||
|
#ifdef LIBSSH2DEBUG
|
||||||
_libssh2_debug(session, LIBSSH2_TRACE_ERROR, "%d - %s", session->err_code,
|
_libssh2_debug(session, LIBSSH2_TRACE_ERROR, "%d - %s", session->err_code,
|
||||||
session->err_msg);
|
session->err_msg);
|
||||||
|
#endif
|
||||||
|
|
||||||
return errcode;
|
return errcode;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else /* ! LIBSSH2DEBUG */
|
|
||||||
|
|
||||||
int libssh2_error(LIBSSH2_SESSION* session, int errcode, char* errmsg,
|
|
||||||
int should_free)
|
|
||||||
{
|
|
||||||
if (session->err_msg && session->err_should_free) {
|
|
||||||
LIBSSH2_FREE(session, session->err_msg);
|
|
||||||
}
|
|
||||||
session->err_msg = errmsg;
|
|
||||||
session->err_msglen = strlen(errmsg);
|
|
||||||
session->err_should_free = should_free;
|
|
||||||
session->err_code = errcode;
|
|
||||||
|
|
||||||
return errcode;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* ! LIBSSH2DEBUG */
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
static int wsa2errno(void)
|
static int wsa2errno(void)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user