libssh2_knownhost_add() got an additional argument: 'store' so that an

application can get a pointer back to the internal representation of the host
it just added. Useful for example when the app wants to add a host, and then
convert that exact same host to a line for storing in a known host file.
'store' can also be set to NULL to simple not care.
This commit is contained in:
Daniel Stenberg 2009-05-29 18:40:29 +02:00
parent 517909d37a
commit e52a1057fd
3 changed files with 45 additions and 33 deletions

View File

@ -10,7 +10,8 @@ libssh2_knownhost_add - add a known host
int libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, int libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
char *host, char *salt, char *host, char *salt,
char *key, size_t keylen, char *key, size_t keylen,
int typemask); int typemask,
struct libssh2_knownhost **store);
.SH DESCRIPTION .SH DESCRIPTION
Adds a known host to the collection of known hosts identified by the 'hosts' Adds a known host to the collection of known hosts identified by the 'hosts'
handle. handle.
@ -42,6 +43,10 @@ LIBSSH2_KNOWNHOST_KEYENC_RAW or LIBSSH2_KNOWNHOST_KEYENC_BASE64.
The key is using one of these algorithms: The key is using one of these algorithms:
LIBSSH2_KNOWNHOST_KEY_RSA1, LIBSSH2_KNOWNHOST_KEY_SSHRSA or LIBSSH2_KNOWNHOST_KEY_RSA1, LIBSSH2_KNOWNHOST_KEY_SSHRSA or
LIBSSH2_KNOWNHOST_KEY_SSHDSS. LIBSSH2_KNOWNHOST_KEY_SSHDSS.
\fIstore\fP should point to a pointer that gets filled in to point to the
known host data after the addition. NULL can be passed if you don't care about
this pointer.
.SH RETURN VALUE .SH RETURN VALUE
Returns a regular libssh2 error code, where negative values are error codes Returns a regular libssh2 error code, where negative values are error codes
and 0 indicates success. and 0 indicates success.

View File

@ -670,6 +670,14 @@ LIBSSH2_API
const char *libssh2_version(int req_version_num); const char *libssh2_version(int req_version_num);
struct libssh2_knownhost {
unsigned int magic; /* magic stored by the library */
void *node; /* handle to the internal representation of this host */
char *name; /* this is NULL if no plain text host name exists */
char *key; /* key in base64/printable format */
int typemask;
};
/* /*
* libssh2_knownhost_init * libssh2_knownhost_init
* *
@ -721,7 +729,8 @@ LIBSSH2_API int
libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
const char *host, const char *host,
const char *salt, const char *salt,
const char *key, size_t keylen, int typemask); const char *key, size_t keylen, int typemask,
struct libssh2_knownhost **store);
/* /*
* libssh2_knownhost_check * libssh2_knownhost_check
@ -741,13 +750,6 @@ libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
* LIBSSH2_KNOWNHOST_CHECK_* values, see below * LIBSSH2_KNOWNHOST_CHECK_* values, see below
* *
*/ */
struct libssh2_knownhost {
unsigned int magic; /* magic stored by the library */
void *node; /* handle to the internal representation of this host */
char *name; /* this is NULL if no plain text host name exists */
char *key; /* key in base64/printable format */
int typemask;
};
#define LIBSSH2_KNOWNHOST_CHECK_MATCH 0 #define LIBSSH2_KNOWNHOST_CHECK_MATCH 0
#define LIBSSH2_KNOWNHOST_CHECK_MISMATCH 1 #define LIBSSH2_KNOWNHOST_CHECK_MISMATCH 1

View File

@ -95,6 +95,27 @@ libssh2_knownhost_init(LIBSSH2_SESSION *session)
return knh; return knh;
} }
#define KNOWNHOST_MAGIC 0xdeadcafe
/*
* knownhost_to_external()
*
* Copies data from the internal to the external representation struct.
*
*/
static struct libssh2_knownhost *knownhost_to_external(struct known_host *node)
{
struct libssh2_knownhost *ext = &node->external;
ext->magic = KNOWNHOST_MAGIC;
ext->node = node;
ext->name = ((node->typemask & LIBSSH2_KNOWNHOST_TYPE_MASK) ==
LIBSSH2_KNOWNHOST_TYPE_PLAIN)? node->name:NULL;
ext->key = node->key;
ext->typemask = node->typemask;
return ext;
}
/* /*
* libssh2_knownhost_add * libssh2_knownhost_add
* *
@ -119,7 +140,7 @@ LIBSSH2_API int
libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
const char *host, const char *salt, const char *host, const char *salt,
const char *key, size_t keylen, const char *key, size_t keylen,
int typemask) int typemask, struct libssh2_knownhost **store)
{ {
struct known_host *entry = struct known_host *entry =
LIBSSH2_ALLOC(hosts->session, sizeof(struct known_host)); LIBSSH2_ALLOC(hosts->session, sizeof(struct known_host));
@ -190,33 +211,15 @@ libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
/* add this new host to the big list of known hosts */ /* add this new host to the big list of known hosts */
_libssh2_list_add(&hosts->head, &entry->node); _libssh2_list_add(&hosts->head, &entry->node);
if(store)
*store = knownhost_to_external(entry);
return LIBSSH2_ERROR_NONE; return LIBSSH2_ERROR_NONE;
error: error:
free_host(hosts->session, entry); free_host(hosts->session, entry);
return rc; return rc;
} }
#define KNOWNHOST_MAGIC 0xdeadcafe
/*
* knownhost_to_external()
*
* Copies data from the internal to the external representation struct.
*
*/
static struct libssh2_knownhost *knownhost_to_external(struct known_host *node)
{
struct libssh2_knownhost *ext = &node->external;
ext->magic = KNOWNHOST_MAGIC;
ext->node = node;
ext->name = ((node->typemask & LIBSSH2_KNOWNHOST_TYPE_MASK) ==
LIBSSH2_KNOWNHOST_TYPE_PLAIN)? node->name:NULL;
ext->key = node->key;
ext->typemask = node->typemask;
return ext;
}
/* /*
* libssh2_knownhost_check * libssh2_knownhost_check
* *
@ -498,7 +501,8 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
hostbuf[seplen]=0; hostbuf[seplen]=0;
rc = libssh2_knownhost_add(hosts, hostbuf, salt, key, keylen, rc = libssh2_knownhost_add(hosts, hostbuf, salt, key, keylen,
type | LIBSSH2_KNOWNHOST_KEYENC_BASE64); type | LIBSSH2_KNOWNHOST_KEYENC_BASE64,
NULL);
if(rc) if(rc)
return rc; return rc;
} }
@ -507,7 +511,8 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
hostbuf[hostlen]=0; hostbuf[hostlen]=0;
rc = libssh2_knownhost_add(hosts, hostbuf, salt, key, keylen, rc = libssh2_knownhost_add(hosts, hostbuf, salt, key, keylen,
type | LIBSSH2_KNOWNHOST_KEYENC_BASE64); type | LIBSSH2_KNOWNHOST_KEYENC_BASE64,
NULL);
return rc; return rc;
} }