]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_ssl_gnutls.cpp
Fix spacing in calls to LogManager::Log.
[user/henk/code/inspircd.git] / src / modules / extra / m_ssl_gnutls.cpp
index e329186a5a7bde574bc16da145b8cb5f7e290670..5ae530608d494819c13fb7963569cfba952343f6 100644 (file)
@@ -25,8 +25,8 @@
 #include <gcrypt.h>
 #include <gnutls/gnutls.h>
 #include <gnutls/x509.h>
-#include "ssl.h"
-#include "m_cap.h"
+#include "modules/ssl.h"
+#include "modules/cap.h"
 
 #ifdef _WIN32
 # pragma comment(lib, "libgnutls.lib")
@@ -214,7 +214,6 @@ class ModuleSSLGnuTLS : public Module
        }
 
  public:
-
        ModuleSSLGnuTLS()
                : starttls(this), capHandler(this, "tls"), iohook(this, "ssl/gnutls", SERVICE_IOHOOK)
        {
@@ -234,7 +233,7 @@ class ModuleSSLGnuTLS : public Module
                dh_alloc = false;
        }
 
-       void init()
+       void init() CXX11_OVERRIDE
        {
                // Needs the flag as it ignores a plain /rehash
                OnModuleRehash(NULL,"ssl");
@@ -251,7 +250,7 @@ class ModuleSSLGnuTLS : public Module
                ServerInstance->Modules->AddService(starttls);
        }
 
-       void OnRehash(User* user)
+       void OnRehash(User* user) CXX11_OVERRIDE
        {
                sslports.clear();
 
@@ -271,7 +270,7 @@ class ModuleSSLGnuTLS : public Module
                                        continue;
 
                                const std::string& portid = port->bind_desc;
-                               ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %s", portid.c_str());
+                               ServerInstance->Logs->Log("m_ssl_gnutls", LOG_DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %s", portid.c_str());
 
                                if (port->bind_tag->getString("type", "clients") == "clients" && port->bind_addr != "127.0.0.1")
                                {
@@ -291,7 +290,7 @@ class ModuleSSLGnuTLS : public Module
                }
        }
 
-       void OnModuleRehash(User* user, const std::string &param)
+       void OnModuleRehash(User* user, const std::string &param) CXX11_OVERRIDE
        {
                if(param != "ssl")
                        return;
@@ -335,6 +334,7 @@ class ModuleSSLGnuTLS : public Module
                {
                        gnutls_dh_params_deinit(dh_params);
                        dh_alloc = false;
+                       dh_params = NULL;
                }
 
                if (cred_alloc)
@@ -350,13 +350,13 @@ class ModuleSSLGnuTLS : public Module
                ret = gnutls_certificate_allocate_credentials(&x509_cred);
                cred_alloc = (ret >= 0);
                if (!cred_alloc)
-                       ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to allocate certificate credentials: %s", gnutls_strerror(ret));
+                       ServerInstance->Logs->Log("m_ssl_gnutls", LOG_DEBUG, "m_ssl_gnutls.so: Failed to allocate certificate credentials: %s", gnutls_strerror(ret));
 
                if((ret =gnutls_certificate_set_x509_trust_file(x509_cred, cafile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
-                       ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to set X.509 trust file '%s': %s", cafile.c_str(), gnutls_strerror(ret));
+                       ServerInstance->Logs->Log("m_ssl_gnutls", LOG_DEBUG, "m_ssl_gnutls.so: Failed to set X.509 trust file '%s': %s", cafile.c_str(), gnutls_strerror(ret));
 
                if((ret = gnutls_certificate_set_x509_crl_file (x509_cred, crlfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
-                       ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to set X.509 CRL file '%s': %s", crlfile.c_str(), gnutls_strerror(ret));
+                       ServerInstance->Logs->Log("m_ssl_gnutls", LOG_DEBUG, "m_ssl_gnutls.so: Failed to set X.509 CRL file '%s': %s", crlfile.c_str(), gnutls_strerror(ret));
 
                FileReader reader;
 
@@ -405,13 +405,13 @@ class ModuleSSLGnuTLS : public Module
                if ((ret = gnutls_priority_init(&priority, priocstr, &prioerror)) < 0)
                {
                        // gnutls did not understand the user supplied string, log and fall back to the default priorities
-                       ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to set priorities to \"%s\": %s Syntax error at position %u, falling back to default (NORMAL)", priorities.c_str(), gnutls_strerror(ret), (unsigned int) (prioerror - priocstr));
+                       ServerInstance->Logs->Log("m_ssl_gnutls", LOG_DEFAULT, "m_ssl_gnutls.so: Failed to set priorities to \"%s\": %s Syntax error at position %u, falling back to default (NORMAL)", priorities.c_str(), gnutls_strerror(ret), (unsigned int) (prioerror - priocstr));
                        gnutls_priority_init(&priority, "NORMAL", NULL);
                }
 
                #else
                if (priorities != "NORMAL")
-                       ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: You've set <gnutls:priority> to a value other than the default, but this is only supported with GnuTLS v2.1.7 or newer. Your GnuTLS version is older than that so the option will have no effect.");
+                       ServerInstance->Logs->Log("m_ssl_gnutls", LOG_DEFAULT, "m_ssl_gnutls.so: You've set <gnutls:priority> to a value other than the default, but this is only supported with GnuTLS v2.1.7 or newer. Your GnuTLS version is older than that so the option will have no effect.");
                #endif
 
                #if(GNUTLS_VERSION_MAJOR < 2 || ( GNUTLS_VERSION_MAJOR == 2 && GNUTLS_VERSION_MINOR < 12 ) )
@@ -422,10 +422,30 @@ class ModuleSSLGnuTLS : public Module
                ret = gnutls_dh_params_init(&dh_params);
                dh_alloc = (ret >= 0);
                if (!dh_alloc)
-                       ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to initialise DH parameters: %s", gnutls_strerror(ret));
+               {
+                       ServerInstance->Logs->Log("m_ssl_gnutls", LOG_DEFAULT, "m_ssl_gnutls.so: Failed to initialise DH parameters: %s", gnutls_strerror(ret));
+                       return;
+               }
 
-               // This may be on a large (once a day or week) timer eventually.
-               GenerateDHParams();
+               std::string dhfile = Conf->getString("dhfile");
+               if (!dhfile.empty())
+               {
+                       // Try to load DH params from file
+                       reader.LoadFile(dhfile);
+                       std::string dhstring = reader.Contents();
+                       gnutls_datum_t dh_datum = { (unsigned char*)dhstring.data(), static_cast<unsigned int>(dhstring.length()) };
+
+                       if ((ret = gnutls_dh_params_import_pkcs3(dh_params, &dh_datum, GNUTLS_X509_FMT_PEM)) < 0)
+                       {
+                               // File unreadable or GnuTLS was unhappy with the contents, generate the DH primes now
+                               ServerInstance->Logs->Log("m_ssl_gnutls", LOG_DEFAULT, "m_ssl_gnutls.so: Generating DH parameters because I failed to load them from file '%s': %s", dhfile.c_str(), gnutls_strerror(ret));
+                               GenerateDHParams();
+                       }
+               }
+               else
+               {
+                       GenerateDHParams();
+               }
        }
 
        void GenerateDHParams()
@@ -441,7 +461,7 @@ class ModuleSSLGnuTLS : public Module
                int ret;
 
                if((ret = gnutls_dh_params_generate2(dh_params, dh_bits)) < 0)
-                       ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
+                       ServerInstance->Logs->Log("m_ssl_gnutls", LOG_DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
        }
 
        ~ModuleSSLGnuTLS()
@@ -464,7 +484,7 @@ class ModuleSSLGnuTLS : public Module
                ServerInstance->GenRandom = &ServerInstance->HandleGenRandom;
        }
 
-       void OnCleanup(int target_type, void* item)
+       void OnCleanup(int target_type, void* item) CXX11_OVERRIDE
        {
                if(target_type == TYPE_USER)
                {
@@ -479,21 +499,20 @@ class ModuleSSLGnuTLS : public Module
                }
        }
 
-       Version GetVersion()
+       Version GetVersion() CXX11_OVERRIDE
        {
                return Version("Provides SSL support for clients", VF_VENDOR);
        }
 
-
-       void On005Numeric(std::string &output)
+       void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
        {
                if (!sslports.empty())
-                       output.append(" SSL=" + sslports);
+                       tokens["SSL"] = sslports;
                if (starttls.enabled)
-                       output.append(" STARTTLS");
+                       tokens["STARTTLS"];
        }
 
-       void OnHookIO(StreamSocket* user, ListenSocket* lsb)
+       void OnHookIO(StreamSocket* user, ListenSocket* lsb) CXX11_OVERRIDE
        {
                if (!user->GetIOHook() && lsb->bind_tag->getString("ssl") == "gnutls")
                {
@@ -502,7 +521,7 @@ class ModuleSSLGnuTLS : public Module
                }
        }
 
-       void OnRequest(Request& request)
+       void OnRequest(Request& request) CXX11_OVERRIDE
        {
                if (strcmp("GET_SSL_CERT", request.id) == 0)
                {
@@ -535,7 +554,7 @@ class ModuleSSLGnuTLS : public Module
                Handshake(session, user);
        }
 
-       void OnStreamSocketAccept(StreamSocket* user, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
+       void OnStreamSocketAccept(StreamSocket* user, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) CXX11_OVERRIDE
        {
                issl_session* session = &sessions[user->GetFd()];
 
@@ -546,17 +565,17 @@ class ModuleSSLGnuTLS : public Module
                InitSession(user, true);
        }
 
-       void OnStreamSocketConnect(StreamSocket* user)
+       void OnStreamSocketConnect(StreamSocket* user) CXX11_OVERRIDE
        {
                InitSession(user, false);
        }
 
-       void OnStreamSocketClose(StreamSocket* user)
+       void OnStreamSocketClose(StreamSocket* user) CXX11_OVERRIDE
        {
                CloseSession(&sessions[user->GetFd()]);
        }
 
-       int OnStreamSocketRead(StreamSocket* user, std::string& recvq)
+       int OnStreamSocketRead(StreamSocket* user, std::string& recvq) CXX11_OVERRIDE
        {
                issl_session* session = &sessions[user->GetFd()];
 
@@ -614,7 +633,7 @@ class ModuleSSLGnuTLS : public Module
                return 0;
        }
 
-       int OnStreamSocketWrite(StreamSocket* user, std::string& sendq)
+       int OnStreamSocketWrite(StreamSocket* user, std::string& sendq) CXX11_OVERRIDE
        {
                issl_session* session = &sessions[user->GetFd()];
 
@@ -713,7 +732,7 @@ class ModuleSSLGnuTLS : public Module
                }
        }
 
-       void OnUserConnect(LocalUser* user)
+       void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
        {
                if (user->eh.GetIOHook() == this)
                {
@@ -726,10 +745,10 @@ class ModuleSSLGnuTLS : public Module
 
                                ssl_cert* cert = sessions[user->eh.GetFd()].cert;
                                if (cert->fingerprint.empty())
-                                       user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), cipher.c_str());
+                                       user->WriteNotice("*** You are connected using SSL cipher '" + cipher + "'");
                                else
-                                       user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\""
-                                               " and your SSL fingerprint is %s", user->nick.c_str(), cipher.c_str(), cert->fingerprint.c_str());
+                                       user->WriteNotice("*** You are connected using SSL cipher '" + cipher +
+                                               "' and your SSL fingerprint is " + cert->fingerprint);
                        }
                }
        }
@@ -827,7 +846,7 @@ class ModuleSSLGnuTLS : public Module
                }
                else
                {
-                       certinfo->fingerprint = irc::hex(digest, digest_size);
+                       certinfo->fingerprint = BinToHex(digest, digest_size);
                }
 
                /* Beware here we do not check for errors.
@@ -841,7 +860,7 @@ info_done_dealloc:
                gnutls_x509_crt_deinit(cert);
        }
 
-       void OnEvent(Event& ev)
+       void OnEvent(Event& ev) CXX11_OVERRIDE
        {
                if (starttls.enabled)
                        capHandler.HandleEvent(ev);