]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_ssl_gnutls.cpp
Add checks for AddIOHook failing
[user/henk/code/inspircd.git] / src / modules / extra / m_ssl_gnutls.cpp
index c0bc93c0eb61ca3b35c57d64891d61a3c2ebd9f7..977c27dda6ce230083b3a08d6eb979718557d9b3 100644 (file)
@@ -1,16 +1,6 @@
 #include <string>
 #include <vector>
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <string.h>
-#include <unistd.h>
-
 #include <gnutls/gnutls.h>
 
 #include "inspircd_config.h"
 
 /* $ModDesc: Provides SSL support for clients */
 /* $CompileFlags: `libgnutls-config --cflags` */
-/* $LinkerFlags: -L/usr/local/lib -Wl,--rpath -Wl,/usr/local/lib -L/usr/lib -Wl,--rpath -Wl,/usr/lib -lgnutls */
+/* $LinkerFlags: `libgnutls-config --libs` */
 
 enum issl_status { ISSL_NONE, ISSL_HANDSHAKING_READ, ISSL_HANDSHAKING_WRITE, ISSL_HANDSHAKEN, ISSL_CLOSING, ISSL_CLOSED };
 
-bool isin(int port, std::vector<int> portlist)
+bool isin(int port, const std::vector<int> &portlist)
 {
        for(unsigned int i = 0; i < portlist.size(); i++)
                if(portlist[i] == port)
@@ -47,7 +37,7 @@ public:
        int fd;
 };
 
-class ModuleSSL : public Module
+class ModuleSSLGnuTLS : public Module
 {
        Server* Srv;
        ServerConfig* SrvConf;
@@ -71,12 +61,11 @@ class ModuleSSL : public Module
        
  public:
        
-       ModuleSSL(Server* Me)
+       ModuleSSLGnuTLS(Server* Me)
                : Module::Module(Me)
        {
                Srv = Me;
                SrvConf = Srv->GetConfig();
-               Conf = new ConfigReader;
                
                // Not rehashable...because I cba to reduce all the sizes of existing buffers.
                inbufsize = SrvConf->NetBufferSize;
@@ -90,15 +79,18 @@ class ModuleSSL : public Module
                if(gnutls_dh_params_init(&dh_params) < 0)
                        log(DEFAULT, "m_ssl_gnutls.so: Failed to initialise DH parameters");
 
-               OnRehash("");
-
+               // Needs the flag as it ignores a plain /rehash
+               OnRehash("ssl");
+               
                // Void return, guess we assume success
                gnutls_certificate_set_dh_params(x509_cred, dh_params);
        }
        
        virtual void OnRehash(std::string param)
        {
-               delete Conf;
+               if(param != "ssl")
+                       return;
+       
                Conf = new ConfigReader;
                
                for(unsigned int i = 0; i < listenports.size(); i++)
@@ -115,12 +107,17 @@ class ModuleSSL : public Module
                        {
                                // Get the port we're meant to be listening on with SSL
                                unsigned int port = Conf->ReadInteger("bind", "port", i, true);
-                               SrvConf->AddIOHook(port, this);
-                               
-                               // We keep a record of which ports we're listening on with SSL
-                               listenports.push_back(port);
+                               if(SrvConf->AddIOHook(port, this))
+                               {
+                                       // We keep a record of which ports we're listening on with SSL
+                                       listenports.push_back(port);
                                
-                               log(DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %d", port);
+                                       log(DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %d", port);
+                               }
+                               else
+                               {
+                                       log(DEFAULT, "m_ssl_gnutls.so: FAILED to enable SSL on port %d, maybe you have another ssl or similar module loaded?", port);
+                               }
                        }
                }
                
@@ -173,6 +170,12 @@ class ModuleSSL : public Module
                if(gnutls_certificate_set_x509_key_file (x509_cred, certfile.c_str(), keyfile.c_str(), GNUTLS_X509_FMT_PEM) < 0)
                        log(DEFAULT, "m_ssl_gnutls.so: Failed to set X.509 certificate and key files: %s and %s", certfile.c_str(), keyfile.c_str());   
                        
+               // This may be on a large (once a day or week) timer eventually.
+               GenerateDHParams();
+       }
+       
+       void GenerateDHParams()
+       {
                // Generate Diffie Hellman parameters - for use with DHE
                // kx algorithms. These should be discarded and regenerated
                // once a day, once a week or once a month. Depending on the
@@ -182,7 +185,7 @@ class ModuleSSL : public Module
                        log(DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits)", dh_bits);
        }
        
-       virtual ~ModuleSSL()
+       virtual ~ModuleSSLGnuTLS()
        {
                delete Conf;
                gnutls_dh_params_deinit(dh_params);
@@ -196,9 +199,9 @@ class ModuleSSL : public Module
                {
                        userrec* user = (userrec*)item;
                        
-                       if(user->GetExt("ssl") && isin(user->port, listenports))
+                       if(user->GetExt("ssl") && IS_LOCAL(user) && isin(user->port, listenports))
                        {
-                               // User is using SSL, and they're using one of *our* SSL ports.
+                               // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
                                // Potentially there could be multiple SSL modules loaded at once on different ports.
                                log(DEBUG, "m_ssl_gnutls.so: Adding user %s to cull list", user->nick);
                                culllist.AddItem(user, "SSL module unloading");
@@ -251,6 +254,7 @@ class ModuleSSL : public Module
 
        virtual void OnRawSocketClose(int fd)
        {
+               log(DEBUG, "OnRawSocketClose: %d", fd);
                CloseSession(&sessions[fd]);
        }
        
@@ -553,26 +557,26 @@ class ModuleSSL : public Module
        }
 };
 
-class ModuleSSLFactory : public ModuleFactory
+class ModuleSSLGnuTLSFactory : public ModuleFactory
 {
  public:
-       ModuleSSLFactory()
+       ModuleSSLGnuTLSFactory()
        {
        }
        
-       ~ModuleSSLFactory()
+       ~ModuleSSLGnuTLSFactory()
        {
        }
        
        virtual Module * CreateModule(Server* Me)
        {
-               return new ModuleSSL(Me);
+               return new ModuleSSLGnuTLS(Me);
        }
 };
 
 
 extern "C" void * init_module( void )
 {
-       return new ModuleSSLFactory;
+       return new ModuleSSLGnuTLSFactory;
 }