]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_ssl_openssl.cpp
Newly revamped ziplinks module, work of psychon.. resolves (a lot) of problems with...
[user/henk/code/inspircd.git] / src / modules / extra / m_ssl_openssl.cpp
index c8fdefe627c181ea9a4495cde33a1a4b7f4893fa..da6dd2f5eb6948cae9391b2ef5ee509c0003310f 100644 (file)
 #endif
 
 /* $ModDesc: Provides SSL support for clients */
-/* $CompileFlags: pkgconfversion("openssl","0.9.7") pkgconfincludes("openssl","/openssl/ssl.h","") */
-/* $LinkerFlags: rpath("pkg-config --libs openssl") pkgconflibs("openssl","/libssl.so","-lssl -lcrypto -ldl") */
+
+/* $LinkerFlags: if("USE_FREEBSD_BASE_SSL") -lssl -lcrypto */
+/* $CompileFlags: if(!"USE_FREEBSD_BASE_SSL") pkgconfversion("openssl","0.9.7") pkgconfincludes("openssl","/openssl/ssl.h","") */
+/* $LinkerFlags: if(!"USE_FREEBSD_BASE_SSL") rpath("pkg-config --libs openssl") pkgconflibs("openssl","/libssl.so","-lssl -lcrypto -ldl") */
+
 /* $ModDep: transport.h */
 /* $NoPedantic */
 /* $CopyInstall: conf/key.pem $(CONPATH) */
 /* $CopyInstall: conf/cert.pem $(CONPATH) */
 
+
 enum issl_status { ISSL_NONE, ISSL_HANDSHAKING, ISSL_OPEN };
 enum issl_io_status { ISSL_WRITE, ISSL_READ };
 
@@ -107,7 +111,7 @@ class ModuleSSLOpenSSL : public Module
        std::vector<std::string> listenports;
 
        int inbufsize;
-       issl_session sessions[MAX_DESCRIPTORS];
+       issl_session* sessions;
 
        SSL_CTX* ctx;
        SSL_CTX* clictx;
@@ -133,6 +137,8 @@ class ModuleSSLOpenSSL : public Module
        {
                ServerInstance->Modules->PublishInterface("BufferedSocketHook", this);
 
+               sessions = new issl_session[ServerInstance->SE->GetMaxFds()];
+
                // Not rehashable...because I cba to reduce all the sizes of existing buffers.
                inbufsize = ServerInstance->Config->NetBufferSize;
 
@@ -146,6 +152,9 @@ class ModuleSSLOpenSSL : public Module
                ctx = SSL_CTX_new( SSLv23_server_method() );
                clictx = SSL_CTX_new( SSLv23_client_method() );
 
+               SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
+               SSL_CTX_set_mode(clictx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
+
                SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, OnVerify);
                SSL_CTX_set_verify(clictx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, OnVerify);
 
@@ -156,7 +165,7 @@ class ModuleSSLOpenSSL : public Module
                ServerInstance->Modules->Attach(eventlist, this, 16);
        }
 
-        virtual void OnHookUserIO(User* user, const std::string &targetip)
+       virtual void OnHookUserIO(User* user, const std::string &targetip)
        {
                if (!user->io && isin(targetip,user->GetPort(), listenports))
                {
@@ -169,43 +178,43 @@ class ModuleSSLOpenSSL : public Module
        {
                ConfigReader Conf(ServerInstance);
 
-                listenports.clear();
-                clientactive = 0;
-                sslports.clear();
-
-                for(int index = 0; index < Conf.Enumerate("bind"); index++)
-                {
-                        // For each <bind> tag
-                        std::string x = Conf.ReadValue("bind", "type", index);
-                        if(((x.empty()) || (x == "clients")) && (Conf.ReadValue("bind", "ssl", index) == "openssl"))
-                        {
-                                // Get the port we're meant to be listening on with SSL
-                                std::string port = Conf.ReadValue("bind", "port", index);
-                                std::string addr = Conf.ReadValue("bind", "address", index);
-
-                                irc::portparser portrange(port, false);
-                                long portno = -1;
-                                while ((portno = portrange.GetToken()))
-                                {
-                                        clientactive++;
-                                        try
-                                        {
-                                                listenports.push_back(addr + ":" + ConvToStr(portno));
-
-                                                for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
-                                                        if ((ServerInstance->Config->ports[i]->GetPort() == portno) && (ServerInstance->Config->ports[i]->GetIP() == addr))
-                                                                ServerInstance->Config->ports[i]->SetDescription("ssl");
-                                                ServerInstance->Logs->Log("m_ssl_openssl",DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %ld", portno);
-
-                                                sslports.append((addr.empty() ? "*" : addr)).append(":").append(ConvToStr(portno)).append(";");
-                                        }
-                                        catch (ModuleException &e)
-                                        {
-                                                ServerInstance->Logs->Log("m_ssl_openssl",DEFAULT, "m_ssl_gnutls.so: FAILED to enable SSL on port %ld: %s. Maybe it's already hooked by the same port on a different IP, or you have an other SSL or similar module loaded?", portno, e.GetReason());
-                                        }
-                                }
-                        }
-                }
+               listenports.clear();
+               clientactive = 0;
+               sslports.clear();
+
+               for(int index = 0; index < Conf.Enumerate("bind"); index++)
+               {
+                       // For each <bind> tag
+                       std::string x = Conf.ReadValue("bind", "type", index);
+                       if(((x.empty()) || (x == "clients")) && (Conf.ReadValue("bind", "ssl", index) == "openssl"))
+                       {
+                               // Get the port we're meant to be listening on with SSL
+                               std::string port = Conf.ReadValue("bind", "port", index);
+                               std::string addr = Conf.ReadValue("bind", "address", index);
+
+                               irc::portparser portrange(port, false);
+                               long portno = -1;
+                               while ((portno = portrange.GetToken()))
+                               {
+                                       clientactive++;
+                                       try
+                                       {
+                                               listenports.push_back(addr + ":" + ConvToStr(portno));
+
+                                               for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
+                                                       if ((ServerInstance->Config->ports[i]->GetPort() == portno) && (ServerInstance->Config->ports[i]->GetIP() == addr))
+                                                               ServerInstance->Config->ports[i]->SetDescription("ssl");
+                                               ServerInstance->Logs->Log("m_ssl_openssl",DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %ld", portno);
+
+                                               sslports.append((addr.empty() ? "*" : addr)).append(":").append(ConvToStr(portno)).append(";");
+                                       }
+                                       catch (ModuleException &e)
+                                       {
+                                               ServerInstance->Logs->Log("m_ssl_openssl",DEFAULT, "m_ssl_gnutls.so: FAILED to enable SSL on port %ld: %s. Maybe it's already hooked by the same port on a different IP, or you have an other SSL or similar module loaded?", portno, e.GetReason());
+                                       }
+                               }
+                       }
+               }
 
                if (!sslports.empty())
                        sslports.erase(sslports.end() - 1);
@@ -238,16 +247,16 @@ class ModuleSSLOpenSSL : public Module
                        dhfile = "dhparams.pem";
 
                // Prepend relative paths with the path to the config directory.
-               if (cafile[0] != '/')
+               if ((cafile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(cafile)))
                        cafile = confdir + cafile;
 
-               if (certfile[0] != '/')
+               if ((certfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(certfile)))
                        certfile = confdir + certfile;
 
-               if (keyfile[0] != '/')
+               if ((keyfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(keyfile)))
                        keyfile = confdir + keyfile;
 
-               if (dhfile[0] != '/')
+               if ((dhfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(dhfile)))
                        dhfile = confdir + dhfile;
 
                /* Load our keys and certificates
@@ -302,6 +311,8 @@ class ModuleSSLOpenSSL : public Module
        {
                SSL_CTX_free(ctx);
                SSL_CTX_free(clictx);
+               ServerInstance->Modules->UnpublishInterface("BufferedSocketHook", this);
+               delete[] sessions;
        }
 
        virtual void OnCleanup(int target_type, void* item)
@@ -310,11 +321,12 @@ class ModuleSSLOpenSSL : public Module
                {
                        User* user = (User*)item;
 
-                       if (user->GetExt("ssl", dummy) && IS_LOCAL(user) && user->io == this)
+                       if (user->io == this)
                        {
                                // 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.
-                               User::QuitUser(ServerInstance, user, "SSL module unloading");
+                               ServerInstance->Users->QuitUser(user, "SSL module unloading");
+                               user->io = NULL;
                        }
                        if (user->GetExt("ssl_cert", dummy))
                        {
@@ -323,8 +335,6 @@ class ModuleSSLOpenSSL : public Module
                                delete tofree;
                                user->Shrink("ssl_cert");
                        }
-
-                       user->io = NULL;
                }
        }
 
@@ -343,7 +353,7 @@ class ModuleSSLOpenSSL : public Module
 
        virtual Version GetVersion()
        {
-               return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
+               return Version("$Id$", VF_VENDOR, API_VERSION);
        }
 
 
@@ -396,7 +406,7 @@ class ModuleSSLOpenSSL : public Module
        virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
        {
                /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
-               if ((fd < 0) || (fd > MAX_DESCRIPTORS))
+               if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
                        return;
 
                issl_session* session = &sessions[fd];
@@ -422,8 +432,8 @@ class ModuleSSLOpenSSL : public Module
 
        virtual void OnRawSocketConnect(int fd)
        {
-                /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
-               if ((fd < 0) || (fd > MAX_DESCRIPTORS))
+               /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
+               if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() -1))
                        return;
 
                issl_session* session = &sessions[fd];
@@ -450,7 +460,7 @@ class ModuleSSLOpenSSL : public Module
        virtual void OnRawSocketClose(int fd)
        {
                /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
-               if ((fd < 0) || (fd > MAX_DESCRIPTORS))
+               if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
                        return;
 
                CloseSession(&sessions[fd]);
@@ -469,7 +479,7 @@ class ModuleSSLOpenSSL : public Module
        virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
        {
                /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
-               if ((fd < 0) || (fd > MAX_DESCRIPTORS))
+               if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
                        return 0;
 
                issl_session* session = &sessions[fd];
@@ -519,7 +529,7 @@ class ModuleSSLOpenSSL : public Module
                                        {
                                                memcpy(buffer, session->inbuf, count);
                                                // Move the stuff left in inbuf to the beginning of it
-                                               memcpy(session->inbuf, session->inbuf + count, (session->inbufoffset - count));
+                                               memmove(session->inbuf, session->inbuf + count, (session->inbufoffset - count));
                                                // Now we need to set session->inbufoffset to the amount of data still waiting to be handed to insp.
                                                session->inbufoffset -= count;
                                                // Insp uses readresult as the count of how much data there is in buffer, so:
@@ -546,7 +556,7 @@ class ModuleSSLOpenSSL : public Module
        virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
        {
                /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
-               if ((fd < 0) || (fd > MAX_DESCRIPTORS))
+               if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
                        return 0;
 
                issl_session* session = &sessions[fd];
@@ -628,7 +638,7 @@ class ModuleSSLOpenSSL : public Module
        {
                // Is this right? Not sure if the unencrypted data is garaunteed to be the same length.
                // Read into the inbuffer, offset from the beginning by the amount of data we have that insp hasn't taken yet.
-               
+
                int ret = SSL_read(session->sess, session->inbuf + session->inbufoffset, inbufsize - session->inbufoffset);
 
                if (ret == 0)
@@ -679,7 +689,7 @@ class ModuleSSLOpenSSL : public Module
                // Bugfix, only send this numeric for *our* SSL users
                if (dest->GetExt("ssl", dummy) || ((IS_LOCAL(dest) && dest->io == this)))
                {
-                       ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick, dest->nick);
+                       ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
                }
        }
 
@@ -778,18 +788,11 @@ class ModuleSSLOpenSSL : public Module
                if ((user->GetExt("ssl", dummy)) && (IS_LOCAL(user)))
                {
                        // Tell whatever protocol module we're using that we need to inform other servers of this metadata NOW.
-                       std::deque<std::string>* metadata = new std::deque<std::string>;
-                       metadata->push_back(user->nick);
-                       metadata->push_back("ssl");             // The metadata id
-                       metadata->push_back("ON");              // The value to send
-                       Event* event = new Event((char*)metadata,(Module*)this,"send_metadata");
-                       event->Send(ServerInstance);            // Trigger the event. We don't care what module picks it up.
-                       delete event;
-                       delete metadata;
+                       ServerInstance->PI->SendMetaData(user, TYPE_USER, "SSL", "on");
 
                        VerifyCertificate(&sessions[user->GetFd()], user);
                        if (sessions[user->GetFd()].sess)
-                               user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick, SSL_get_cipher(sessions[user->GetFd()].sess));
+                               user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), SSL_get_cipher(sessions[user->GetFd()].sess));
                }
        }
 
@@ -885,12 +888,29 @@ class ModuleSSLOpenSSL : public Module
 
                X509_free(cert);
        }
+
+       void Prioritize()
+       {
+               Module* server = ServerInstance->Modules->Find("m_spanningtree.so");
+               ServerInstance->Modules->SetPriority(this, I_OnPostConnect, PRIO_AFTER, &server);
+       }
+
 };
 
 static int error_callback(const char *str, size_t len, void *u)
 {
        ModuleSSLOpenSSL* mssl = (ModuleSSLOpenSSL*)u;
        mssl->PublicInstance->Logs->Log("m_ssl_openssl",DEFAULT, "SSL error: " + std::string(str, len - 1));
+
+       //
+       // XXX: Remove this line, it causes valgrind warnings...
+       //
+       // MD_update(&m, buf, j);
+       //
+       //
+       // ... ONLY JOKING! :-)
+       //
+
        return 0;
 }