0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 17:51:53 +00:00

RPKI: load SSH Library using dlopen function

This commit is contained in:
Pavel Tvrdík 2015-12-23 10:06:30 +01:00
parent 82804d6add
commit 19b23a8b92
8 changed files with 274 additions and 3 deletions

View File

@ -31,3 +31,5 @@ event.h
checksum.c checksum.c
checksum.h checksum.h
alloca.h alloca.h
libssh.c
libssh.h

126
lib/libssh.c Normal file
View File

@ -0,0 +1,126 @@
/*
* BIRD -- Mockup of SSH Library for loading LibSSH using dlopen
*
* (c) 2015 CZ.NIC
*
* This file was part of SSH Library: http://www.libssh.org/
* (c) 2003-2009 by Aris Adamantiadis (SSH Library)
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include <dlfcn.h>
#include "nest/bird.h"
#include "lib/libssh.h"
#define FILENAME_OF_SHARED_OBJECT_LIBSSH "libssh.so"
static void *libssh;
/*
* @return NULL if success
* @return string with error if failed
*/
const char *
load_libssh(void)
{
char *err_buf;
libssh = dlopen(FILENAME_OF_SHARED_OBJECT_LIBSSH, RTLD_LAZY);
if (!libssh)
{
/* This would be probably often repeated problem */
char *help_msg = "You have to install libssh library.";
err_buf = mb_alloc(&root_pool, 512);
bsnprintf(err_buf, 512, "%s. %s", dlerror(), help_msg);
return err_buf;
}
dlerror(); /* Clear any existing error */
ssh_new = (ssh_session (*)(void)) dlsym(libssh, "ssh_new");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_set_blocking = (void (*)(ssh_session, int)) dlsym(libssh, "ssh_set_blocking");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_options_set = (int (*)(ssh_session, enum ssh_options_e, const void *)) dlsym(libssh, "ssh_options_set");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_connect = (int (*)(ssh_session)) dlsym(libssh, "ssh_connect");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_get_fd = (socket_t (*)(ssh_session)) dlsym(libssh, "ssh_get_fd");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_is_server_known = (int (*)(ssh_session)) dlsym(libssh, "ssh_is_server_known");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_userauth_publickey_auto = (int (*)(ssh_session, const char *, const char *)) dlsym(libssh, "ssh_userauth_publickey_auto");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_get_error = (const char * (*)(void *)) dlsym(libssh, "ssh_get_error");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_get_error_code = (int (*)(void *)) dlsym(libssh, "ssh_get_error_code");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_disconnect = (void (*)(ssh_session)) dlsym(libssh, "ssh_disconnect");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_free = (void (*)(ssh_session)) dlsym(libssh, "ssh_free");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_channel_new = (ssh_channel (*)(ssh_session)) dlsym(libssh, "ssh_channel_new");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_channel_is_open = (int (*)(ssh_channel)) dlsym(libssh, "ssh_channel_is_open");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_channel_close = (int (*)(ssh_channel)) dlsym(libssh, "ssh_channel_close");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_channel_free = (void (*)(ssh_channel)) dlsym(libssh, "ssh_channel_free");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_channel_open_session = (int (*)(ssh_channel)) dlsym(libssh, "ssh_channel_open_session");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_channel_request_subsystem = (int (*)(ssh_channel, const char *)) dlsym(libssh, "ssh_channel_request_subsystem");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_channel_read_nonblocking = (int (*)(ssh_channel, void *, uint32_t, int)) dlsym(libssh, "ssh_channel_read_nonblocking");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_channel_is_eof = (int (*)(ssh_channel)) dlsym(libssh, "ssh_channel_is_eof");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_channel_select = (int (*)(ssh_channel *, ssh_channel *, ssh_channel *, struct timeval *)) dlsym(libssh, "ssh_channel_select");
if ((err_buf = dlerror()) != NULL)
return err_buf;
ssh_channel_write = (int (*)(ssh_channel, const void *, uint32_t)) dlsym(libssh, "ssh_channel_write");
if ((err_buf = dlerror()) != NULL)
return err_buf;
return NULL;
}

133
lib/libssh.h Normal file
View File

@ -0,0 +1,133 @@
/*
* BIRD -- Mockup headers of SSH Library for loading LibSSH using dlopen
*
* (c) 2015 CZ.NIC
*
* This file was part of SSH Library: http://www.libssh.org/
* (c) 2003-2009 by Aris Adamantiadis (SSH Library)
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#ifndef _BIRD_LIBSSH_H_
#define _BIRD_LIBSSH_H_
#include <unistd.h>
#include <inttypes.h>
typedef struct ssh_session_struct* ssh_session;
typedef struct ssh_channel_struct* ssh_channel;
/* Error return codes */
#define SSH_OK 0 /* No error */
#define SSH_ERROR -1 /* Error of some kind */
#define SSH_AGAIN -2 /* The nonblocking call must be repeated */
#define SSH_EOF -127 /* We have already a eof */
enum ssh_server_known_e {
SSH_SERVER_ERROR=-1,
SSH_SERVER_NOT_KNOWN=0,
SSH_SERVER_KNOWN_OK,
SSH_SERVER_KNOWN_CHANGED,
SSH_SERVER_FOUND_OTHER,
SSH_SERVER_FILE_NOT_FOUND
};
enum ssh_auth_e {
SSH_AUTH_SUCCESS=0,
SSH_AUTH_DENIED,
SSH_AUTH_PARTIAL,
SSH_AUTH_INFO,
SSH_AUTH_AGAIN,
SSH_AUTH_ERROR=-1
};
enum ssh_error_types_e {
SSH_NO_ERROR=0,
SSH_REQUEST_DENIED,
SSH_FATAL,
SSH_EINTR
};
enum ssh_options_e {
SSH_OPTIONS_HOST,
SSH_OPTIONS_PORT,
SSH_OPTIONS_PORT_STR,
SSH_OPTIONS_FD,
SSH_OPTIONS_USER,
SSH_OPTIONS_SSH_DIR,
SSH_OPTIONS_IDENTITY,
SSH_OPTIONS_ADD_IDENTITY,
SSH_OPTIONS_KNOWNHOSTS,
SSH_OPTIONS_TIMEOUT,
SSH_OPTIONS_TIMEOUT_USEC,
SSH_OPTIONS_SSH1,
SSH_OPTIONS_SSH2,
SSH_OPTIONS_LOG_VERBOSITY,
SSH_OPTIONS_LOG_VERBOSITY_STR,
SSH_OPTIONS_CIPHERS_C_S,
SSH_OPTIONS_CIPHERS_S_C,
SSH_OPTIONS_COMPRESSION_C_S,
SSH_OPTIONS_COMPRESSION_S_C,
SSH_OPTIONS_PROXYCOMMAND,
SSH_OPTIONS_BINDADDR,
SSH_OPTIONS_STRICTHOSTKEYCHECK,
SSH_OPTIONS_COMPRESSION,
SSH_OPTIONS_COMPRESSION_LEVEL,
SSH_OPTIONS_KEY_EXCHANGE,
SSH_OPTIONS_HOSTKEYS,
SSH_OPTIONS_GSSAPI_SERVER_IDENTITY,
SSH_OPTIONS_GSSAPI_CLIENT_IDENTITY,
SSH_OPTIONS_GSSAPI_DELEGATE_CREDENTIALS,
SSH_OPTIONS_HMAC_C_S,
SSH_OPTIONS_HMAC_S_C,
};
enum {
/** No logging at all
*/
SSH_LOG_NOLOG=0,
/** Only warnings
*/
SSH_LOG_WARNING,
/** High level protocol information
*/
SSH_LOG_PROTOCOL,
/** Lower level protocol infomations, packet level
*/
SSH_LOG_PACKET,
/** Every function path
*/
SSH_LOG_FUNCTIONS
};
#ifndef socket_t
typedef int socket_t;
#endif
ssh_session (*ssh_new)(void);
void (*ssh_set_blocking)(ssh_session session, int blocking);
int (*ssh_options_set)(ssh_session session, enum ssh_options_e type, const void *value);
int (*ssh_connect)(ssh_session session);
socket_t (*ssh_get_fd)(ssh_session session);
int (*ssh_is_server_known)(ssh_session session);
int (*ssh_userauth_publickey_auto)(ssh_session session, const char *username, const char *passphrase);
const char * (*ssh_get_error)(void *error);
int (*ssh_get_error_code)(void *error);
void (*ssh_disconnect)(ssh_session session);
void (*ssh_free)(ssh_session session);
ssh_channel (*ssh_channel_new)(ssh_session session);
int (*ssh_channel_is_open)(ssh_channel channel);
int (*ssh_channel_close)(ssh_channel channel);
void (*ssh_channel_free)(ssh_channel channel);
int (*ssh_channel_open_session)(ssh_channel channel);
int (*ssh_channel_request_subsystem)(ssh_channel channel, const char *subsystem);
int (*ssh_channel_read_nonblocking)(ssh_channel channel, void *dest, uint32_t count, int is_stderr);
int (*ssh_channel_is_eof)(ssh_channel channel);
int (*ssh_channel_select)(ssh_channel *readchans, ssh_channel *writechans, ssh_channel *exceptchans, struct timeval * timeout);
int (*ssh_channel_write)(ssh_channel channel, const void *data, uint32_t len);
const char *load_libssh(void);
#endif /* _BIRD_LIBSSH_H_ */

View File

@ -10,10 +10,10 @@
#define _BIRD_SOCKET_H_ #define _BIRD_SOCKET_H_
#include <errno.h> #include <errno.h>
#include <libssh/libssh.h>
// #include <sys/socket.h> // #include <sys/socket.h>
#include "lib/resource.h" #include "lib/resource.h"
#include "lib/libssh.h"
struct ssh_sock { struct ssh_sock {
char *username; /* (Required) SSH user name */ char *username; /* (Required) SSH user name */

View File

@ -15,6 +15,9 @@
* is based on the RTRlib (http://rpki.realmv6.org/). The BIRD takes over * is based on the RTRlib (http://rpki.realmv6.org/). The BIRD takes over
* |packets.c|, |rtr.c|, |transport.c|, |tcp_transport.c| and |ssh_transport.c| files * |packets.c|, |rtr.c|, |transport.c|, |tcp_transport.c| and |ssh_transport.c| files
* from RTRlib. * from RTRlib.
*
* A SSH transport requires LibSSH library. LibSSH is loading dynamically using dlopen
* function.
*/ */
#undef LOCAL_DEBUG #undef LOCAL_DEBUG

View File

@ -16,6 +16,7 @@
#include <sys/time.h> #include <sys/time.h>
#include "utils.h" #include "utils.h"
#include "ssh_transport.h" #include "ssh_transport.h"
#include "lib/libssh.h"
#include "rpki.h" #include "rpki.h"
@ -30,6 +31,13 @@ int tr_ssh_open(void *socket)
struct rpki_cache *cache = ssh_socket->cache; struct rpki_cache *cache = ssh_socket->cache;
struct rpki_proto *p = cache->p; struct rpki_proto *p = cache->p;
const char *err_msg;
if((err_msg = load_libssh()) != NULL)
{
RPKI_ERROR(p, "%s", err_msg);
return TR_ERROR;
}
sock *s = cache->sk; sock *s = cache->sk;
s->type = SK_SSH_ACTIVE; s->type = SK_SSH_ACTIVE;
s->ssh = mb_allocz(s->pool, sizeof(struct ssh_sock)); s->ssh = mb_allocz(s->pool, sizeof(struct ssh_sock));

View File

@ -25,7 +25,6 @@
#ifndef SSH_TRANSPORT_H #ifndef SSH_TRANSPORT_H
#define SSH_TRANSPORT_H #define SSH_TRANSPORT_H
#include <libssh/libssh.h>
#include "transport.h" #include "transport.h"
/** /**

View File

@ -27,7 +27,6 @@
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <netinet/udp.h> #include <netinet/udp.h>
#include <netinet/icmp6.h> #include <netinet/icmp6.h>
#include <libssh/libssh.h>
#include "nest/bird.h" #include "nest/bird.h"
#include "lib/lists.h" #include "lib/lists.h"
@ -36,6 +35,7 @@
#include "lib/socket.h" #include "lib/socket.h"
#include "lib/event.h" #include "lib/event.h"
#include "lib/string.h" #include "lib/string.h"
#include "lib/libssh.h"
#include "nest/iface.h" #include "nest/iface.h"
#include "lib/unix.h" #include "lib/unix.h"