From 19b23a8b9288f0493ead538c5af8f18487c9b3ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Tvrd=C3=ADk?= Date: Wed, 23 Dec 2015 10:06:30 +0100 Subject: [PATCH] RPKI: load SSH Library using dlopen function --- lib/Modules | 2 + lib/libssh.c | 126 +++++++++++++++++++++++++++++++++++ lib/libssh.h | 133 +++++++++++++++++++++++++++++++++++++ lib/socket.h | 2 +- proto/rpki/rpki.c | 3 + proto/rpki/ssh_transport.c | 8 +++ proto/rpki/ssh_transport.h | 1 - sysdep/unix/io.c | 2 +- 8 files changed, 274 insertions(+), 3 deletions(-) create mode 100644 lib/libssh.c create mode 100644 lib/libssh.h diff --git a/lib/Modules b/lib/Modules index 745306d9..f9d6eb19 100644 --- a/lib/Modules +++ b/lib/Modules @@ -31,3 +31,5 @@ event.h checksum.c checksum.h alloca.h +libssh.c +libssh.h \ No newline at end of file diff --git a/lib/libssh.c b/lib/libssh.c new file mode 100644 index 00000000..098dd2f0 --- /dev/null +++ b/lib/libssh.c @@ -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 +#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; +} diff --git a/lib/libssh.h b/lib/libssh.h new file mode 100644 index 00000000..15e4579a --- /dev/null +++ b/lib/libssh.h @@ -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 +#include + +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_ */ diff --git a/lib/socket.h b/lib/socket.h index 55af85c6..c3d45830 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -10,10 +10,10 @@ #define _BIRD_SOCKET_H_ #include -#include // #include #include "lib/resource.h" +#include "lib/libssh.h" struct ssh_sock { char *username; /* (Required) SSH user name */ diff --git a/proto/rpki/rpki.c b/proto/rpki/rpki.c index fe8e0e15..da79e3f9 100644 --- a/proto/rpki/rpki.c +++ b/proto/rpki/rpki.c @@ -15,6 +15,9 @@ * 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 * from RTRlib. + * + * A SSH transport requires LibSSH library. LibSSH is loading dynamically using dlopen + * function. */ #undef LOCAL_DEBUG diff --git a/proto/rpki/ssh_transport.c b/proto/rpki/ssh_transport.c index 956ddfdf..06355c8d 100644 --- a/proto/rpki/ssh_transport.c +++ b/proto/rpki/ssh_transport.c @@ -16,6 +16,7 @@ #include #include "utils.h" #include "ssh_transport.h" +#include "lib/libssh.h" #include "rpki.h" @@ -30,6 +31,13 @@ int tr_ssh_open(void *socket) struct rpki_cache *cache = ssh_socket->cache; 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; s->type = SK_SSH_ACTIVE; s->ssh = mb_allocz(s->pool, sizeof(struct ssh_sock)); diff --git a/proto/rpki/ssh_transport.h b/proto/rpki/ssh_transport.h index 1b79221e..13c1621e 100644 --- a/proto/rpki/ssh_transport.h +++ b/proto/rpki/ssh_transport.h @@ -25,7 +25,6 @@ #ifndef SSH_TRANSPORT_H #define SSH_TRANSPORT_H -#include #include "transport.h" /** diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c index d6b53ca6..8e1e34a7 100644 --- a/sysdep/unix/io.c +++ b/sysdep/unix/io.c @@ -27,7 +27,6 @@ #include #include #include -#include #include "nest/bird.h" #include "lib/lists.h" @@ -36,6 +35,7 @@ #include "lib/socket.h" #include "lib/event.h" #include "lib/string.h" +#include "lib/libssh.h" #include "nest/iface.h" #include "lib/unix.h"