Kaynağa Gözat

* Load SSL on-demand at runtime via dlopen() if needed

Bryan Drewery 15 yıl önce
ebeveyn
işleme
6eaadb538d
6 değiştirilmiş dosya ile 75 ekleme ve 2 silme
  1. 1 0
      autotools/includes/acinclude.m4
  2. 3 0
      config.h.in
  3. 4 0
      configure
  4. 4 1
      src/net.c
  5. 46 1
      src/ssl.c
  6. 17 0
      src/ssl.h

+ 1 - 0
autotools/includes/acinclude.m4

@@ -547,6 +547,7 @@ LIBS="$save_LIBS"
 
 AC_SUBST(SSL_INCLUDES)
 AC_SUBST(SSL_LIBS)
+AC_DEFINE_UNQUOTED(EGG_SSL_EXT, 1, [Defines whether or not SSL is supported])dnl
 ])
 
 dnl  EGG_HEADER_STDC()

+ 3 - 0
config.h.in

@@ -13,6 +13,9 @@
 /* big endian */
 #undef B_ENDIAN
 
+/* Defines whether or not SSL is supported */
+#undef EGG_SSL_EXT
+
 /* Defines the current pack version */
 #undef EGG_VERSION
 

+ 4 - 0
configure

@@ -5379,6 +5379,10 @@ LIBS="$save_LIBS"
 
 
 
+cat >>confdefs.h <<_ACEOF
+#define EGG_SSL_EXT 1
+_ACEOF
+
 
 
 #AC_SUBST(ZLIB)dnl

+ 4 - 1
src/net.c

@@ -623,7 +623,10 @@ int open_telnet_raw(int sock, const char *ipIn, port_t sport, bool proxy_on, int
 int net_switch_to_ssl(int sock) {
   int i = 0;
 
-  load_ssl();
+  if (load_ssl()) {
+    debug0("Error while switching to SSL - error loading library");
+    return 0;
+  }
 
   debug0("net_switch_to_ssl()");
   sleep(3); // Give some time to let the connect() go through.

+ 46 - 1
src/ssl.c

@@ -33,6 +33,7 @@
 
 #include "ssl.h"
 
+void *libssl_handle = NULL;
 #ifdef EGG_SSL_EXT
 SSL_CTX *ssl_ctx = NULL;
 char	*tls_rand_file = NULL;
@@ -41,11 +42,52 @@ int     ssl_use = 0; /* kyotou */
 
 static int seed_PRNG(void);
 
+static int load_symbols(void *handle) {
+  const char *dlsym_error = NULL;
+
+  DLSYM_GLOBAL(handle, SSL_get_error);
+  DLSYM_GLOBAL(handle, SSL_connect);
+  DLSYM_GLOBAL(handle, SSL_CTX_free);
+  DLSYM_GLOBAL(handle, SSL_CTX_new);
+  DLSYM_GLOBAL(handle, SSL_free);
+  DLSYM_GLOBAL(handle, SSL_library_init);
+  DLSYM_GLOBAL(handle, SSL_load_error_strings);
+  DLSYM_GLOBAL(handle, SSL_new);
+  DLSYM_GLOBAL(handle, SSL_pending);
+  DLSYM_GLOBAL(handle, SSL_read);
+  DLSYM_GLOBAL(handle, SSL_set_fd);
+  DLSYM_GLOBAL(handle, SSL_shutdown);
+  DLSYM_GLOBAL(handle, SSLv23_client_method);
+  DLSYM_GLOBAL(handle, SSL_write);
+
+  return 0;
+}
+
+
 int load_ssl() {
   if (ssl_ctx) {
     return 0;
   }
 
+  sdprintf("Loading libssl");
+
+  bd::Array<bd::String> libs_list(bd::String("libssl.so libssl.so.0.9.8 libssl.so.7 libssl.so.6").split(' '));
+
+  for (size_t i = 0; i < libs_list.length(); ++i) {
+    dlerror(); // Clear Errors
+    libssl_handle = dlopen(bd::String(libs_list[i]).c_str(), RTLD_LAZY);
+    if (libssl_handle) {
+      sdprintf("Found libssl: %s", bd::String(libs_list[i]).c_str());
+      break;
+    }
+  }
+  if (!libssl_handle) {
+    sdprintf("Unable to find libssl");
+    return(1);
+  }
+
+  load_symbols(libssl_handle);
+
 #ifdef EGG_SSL_EXT
   /* good place to init ssl stuff */
   SSL_load_error_strings();
@@ -61,7 +103,7 @@ int load_ssl() {
 }
 
 int unload_ssl() {
-  if (ssl_ctx) {
+  if (libssl_handle) {
 #ifdef EGG_SSL_EXT
     /* cleanup mess when quiting */
     if (ssl_ctx) {
@@ -71,6 +113,9 @@ int unload_ssl() {
     if (tls_rand_file)
       RAND_write_file(tls_rand_file);
 #endif
+
+    dlclose(libssl_handle);
+    libssl_handle = NULL;
     return 0;
   }
   return 1;

+ 17 - 0
src/ssl.h

@@ -14,6 +14,23 @@
 # endif
 #endif
 
+typedef int (*SSL_get_error_t)(const SSL *, int);
+typedef void (*SSL_free_t)(SSL *);
+typedef int (*SSL_connect_t)(SSL *);
+typedef int (*SSL_read_t)(SSL *, void*, int);
+typedef int (*SSL_write_t)(SSL *, const void*, int);
+typedef SSL* (*SSL_new_t)(SSL_CTX *);
+typedef const SSL_METHOD* (*SSLv23_client_method_t)(void);
+typedef int (*SSL_shutdown_t)(SSL *);
+typedef int (*SSL_set_fd_t)(SSL *, int);
+typedef int (*SSL_pending_t)(const SSL *);
+typedef void (*SSL_load_error_strings_t)(void);
+typedef int (*SSL_library_init_t)(void);
+typedef void (*SSL_CTX_free_t)(SSL_CTX *);
+typedef SSL_CTX* (*SSL_CTX_new_t)(const SSL_METHOD *);
+
+#include ".defs/ssl_defs.h"
+
 int load_ssl();
 int unload_ssl();