]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_ssl_openssl.cpp
Note: FOR THE MOMENT, this is BROKEN. It wont run right until im done.
[user/henk/code/inspircd.git] / src / modules / extra / m_ssl_openssl.cpp
index 692098dce0ff8ad046b925fca61f4706409b159e..3819d835c1185a82b2c8564378e7134aaefc16df 100644 (file)
@@ -5,16 +5,20 @@
 #include <openssl/err.h>
 
 #include "inspircd_config.h"
+#include "configreader.h"
 #include "users.h"
 #include "channels.h"
 #include "modules.h"
 #include "helperfuncs.h"
 #include "socket.h"
 #include "hashcomp.h"
+#include "inspircd.h"
 
 /* $ModDesc: Provides SSL support for clients */
 /* $CompileFlags: -I/usr/include -I/usr/local/include */
-/* $LinkerFlags: -L/usr/local/lib -Wl,--rpath -Wl,/usr/local/lib -L/usr/lib -Wl,--rpath -Wl,/usr/lib -lssl -lcrypto */
+/* $LinkerFlags: -L/usr/local/lib -Wl,--rpath -Wl,/usr/local/lib -L/usr/lib -Wl,--rpath -Wl,/usr/lib -lssl */
+
+extern InspIRCd* ServerInstance;
 
 enum issl_status { ISSL_NONE, ISSL_HANDSHAKING, ISSL_OPEN };
 enum issl_io_status { ISSL_WRITE, ISSL_READ };
@@ -33,7 +37,7 @@ char* get_error()
        return ERR_error_string(ERR_get_error(), NULL);
 }
 
-class issl_session
+class issl_session : public classbase
 {
 public:
        SSL* sess;
@@ -56,10 +60,9 @@ public:
 class ModuleSSLOpenSSL : public Module
 {
        Server* Srv;
-       ServerConfig* SrvConf;
        ConfigReader* Conf;
        
-       CullList culllist;
+       CullList* culllist;
        
        std::vector<int> listenports;
        
@@ -68,6 +71,8 @@ class ModuleSSLOpenSSL : public Module
        
        SSL_CTX* ctx;
        
+       char* dummy;
+       
        std::string keyfile;
        std::string certfile;
        std::string cafile;
@@ -76,18 +81,19 @@ class ModuleSSLOpenSSL : public Module
        
  public:
        
-       ModuleSSLOpenSSL(Server* Me)
+       ModuleSSLOpenSSL(InspIRCd* Me)
                : Module::Module(Me)
        {
-               Srv = Me;
-               SrvConf = Srv->GetConfig();
+               
+
+               culllist = new CullList(ServerInstance);
                
                // Not rehashable...because I cba to reduce all the sizes of existing buffers.
-               inbufsize = SrvConf->NetBufferSize;
+               inbufsize = ServerInstance->Config->NetBufferSize;
                
                /* Global SSL library initialization*/
-      SSL_library_init();
-      SSL_load_error_strings();
+               SSL_library_init();
+               SSL_load_error_strings();
                
                /* Build our SSL context*/
                ctx = SSL_CTX_new( SSLv23_server_method() );
@@ -96,7 +102,7 @@ class ModuleSSLOpenSSL : public Module
                OnRehash("ssl");
        }
        
-       virtual void OnRehash(std::string param)
+       virtual void OnRehash(const std::string &param)
        {
                if(param != "ssl")
                        return;
@@ -105,7 +111,7 @@ class ModuleSSLOpenSSL : public Module
                        
                for(unsigned int i = 0; i < listenports.size(); i++)
                {
-                       SrvConf->DelIOHook(listenports[i]);
+                       ServerInstance->Config->DelIOHook(listenports[i]);
                }
                
                listenports.clear();
@@ -117,12 +123,17 @@ class ModuleSSLOpenSSL : 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 (ServerInstance->Config->AddIOHook(port, this))
+                               {
+                                       // We keep a record of which ports we're listening on with SSL
+                                       listenports.push_back(port);
                                
-                               log(DEFAULT, "m_ssl_openssl.so: Enabling SSL for port %d", port);
+                                       log(DEFAULT, "m_ssl_openssl.so: Enabling SSL for port %d", port);
+                               }
+                               else
+                               {
+                                       log(DEFAULT, "m_ssl_openssl.so: FAILED to enable SSL on port %d, maybe you have another ssl or similar module loaded?", port);
+                               }
                        }
                }
                
@@ -184,32 +195,34 @@ class ModuleSSLOpenSSL : public Module
                {
                        log(DEFAULT, "m_ssl_openssl.so: Can't read CA list from ", cafile.c_str());
                }
-                               
+
                FILE* dhpfile = fopen(dhfile.c_str(), "r");
                DH* ret;
 
                if(dhpfile == NULL)
                {
                        log(DEFAULT, "m_ssl_openssl.so Couldn't open DH file %s: %s", dhfile.c_str(), strerror(errno));
+                       throw ModuleException();
                }
                else
                {
                        ret = PEM_read_DHparams(dhpfile, NULL, NULL, NULL);
+               
+                       if(SSL_CTX_set_tmp_dh(ctx, ret) < 0)
+                       {
+                               log(DEFAULT, "m_ssl_openssl.so: Couldn't set DH parameters");
+                       }
                }
                
                fclose(dhpfile);
-    
-               if(SSL_CTX_set_tmp_dh(ctx, ret) < 0)
-               {
-                       log(DEFAULT, "m_ssl_openssl.so: Couldn't set DH parameters");
-               }
 
-               delete Conf;
+               DELETE(Conf);
        }
 
        virtual ~ModuleSSLOpenSSL()
        {
                SSL_CTX_free(ctx);
+               delete culllist;
        }
        
        virtual void OnCleanup(int target_type, void* item)
@@ -218,26 +231,26 @@ class ModuleSSLOpenSSL : public Module
                {
                        userrec* user = (userrec*)item;
                        
-                       if(user->GetExt("ssl") && IS_LOCAL(user) && isin(user->port, listenports))
+                       if(user->GetExt("ssl", dummy) && IS_LOCAL(user) && isin(user->GetPort(), listenports))
                        {
                                // 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_openssl.so: Adding user %s to cull list", user->nick);
-                               culllist.AddItem(user, "SSL module unloading");
+                               culllist->AddItem(user, "SSL module unloading");
                        }
                }
        }
        
-       virtual void OnUnloadModule(Module* mod, std::string name)
+       virtual void OnUnloadModule(Module* mod, const std::string &name)
        {
                if(mod == this)
                {
                        // We're being unloaded, kill all the users added to the cull list in OnCleanup
-                       int numusers = culllist.Apply();
+                       int numusers = culllist->Apply();
                        log(DEBUG, "m_ssl_openssl.so: Killed %d users for unload of OpenSSL SSL module", numusers);
                        
                        for(unsigned int i = 0; i < listenports.size(); i++)
-                               SrvConf->DelIOHook(listenports[i]);
+                               ServerInstance->Config->DelIOHook(listenports[i]);
                }
        }
        
@@ -249,10 +262,10 @@ class ModuleSSLOpenSSL : public Module
        void Implements(char* List)
        {
                List[I_OnRawSocketAccept] = List[I_OnRawSocketClose] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnCleanup] = 1;
-               List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUnloadModule] = List[I_OnRehash] = List[I_OnWhois] = 1;
+               List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUnloadModule] = List[I_OnRehash] = List[I_OnWhois] = List[I_OnGlobalConnect] = 1;
        }
 
-       virtual void OnRawSocketAccept(int fd, std::string ip, int localport)
+       virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
        {
                issl_session* session = &sessions[fd];
        
@@ -357,9 +370,6 @@ class ModuleSSLOpenSSL : public Module
                                                session->inbufoffset = 0;
                                        }
                                
-                                       log(DEBUG, "m_ssl_openssl.so: OnRawSocketRead: Passing %d bytes up to insp:");
-                                       Srv->Log(DEBUG, std::string(buffer, readresult));
-                               
                                        return 1;
                                }
                                else
@@ -372,7 +382,7 @@ class ModuleSSLOpenSSL : public Module
                return -1;
        }
        
-       virtual int OnRawSocketWrite(int fd, char* buffer, int count)
+       virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
        {               
                issl_session* session = &sessions[fd];
 
@@ -426,9 +436,6 @@ class ModuleSSLOpenSSL : public Module
        
        int DoWrite(issl_session* session)
        {
-               log(DEBUG, "m_ssl_openssl.so: DoWrite: Trying to write %d bytes:", session->outbuf.size());
-               Srv->Log(DEBUG, session->outbuf);
-                       
                int ret = SSL_write(session->sess, session->outbuf.data(), session->outbuf.size());
                
                if(ret == 0)
@@ -523,19 +530,20 @@ class ModuleSSLOpenSSL : public Module
        // :kenny.chatspike.net 320 Om Epy|AFK :is a Secure Connection
        virtual void OnWhois(userrec* source, userrec* dest)
        {
-               if(dest->GetExt("ssl"))
+               // Bugfix, only send this numeric for *our* SSL users
+               if(dest->GetExt("ssl", dummy) || (IS_LOCAL(dest) &&  isin(dest->GetPort(), listenports)))
                {
-                       WriteServ(source->fd, "320 %s %s :is a Secure Connection", source->nick, dest->nick);
+                       source->WriteServ("320 %s %s :is using a secure connection", source->nick, dest->nick);
                }
        }
        
-       virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, std::string extname)
+       virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
        {
                // check if the linking module wants to know about OUR metadata
                if(extname == "ssl")
                {
                        // check if this user has an swhois field to send
-                       if(user->GetExt(extname))
+                       if(user->GetExt(extname, dummy))
                        {
                                // call this function in the linking module, let it format the data how it
                                // sees fit, and send it on its way. We dont need or want to know how.
@@ -544,14 +552,14 @@ class ModuleSSLOpenSSL : public Module
                }
        }
        
-       virtual void OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata)
+       virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
        {
                // check if its our metadata key, and its associated with a user
                if ((target_type == TYPE_USER) && (extname == "ssl"))
                {
                        userrec* dest = (userrec*)target;
                        // if they dont already have an ssl flag, accept the remote server's
-                       if (!dest->GetExt(extname))
+                       if (!dest->GetExt(extname, dummy))
                        {
                                dest->Extend(extname, "ON");
                        }
@@ -562,7 +570,7 @@ class ModuleSSLOpenSSL : public Module
        {               
                int ret = SSL_accept(session->sess);
       
-      if(ret < 0)
+               if(ret < 0)
                {
                        int err = SSL_get_error(session->sess, ret);
                                
@@ -593,7 +601,12 @@ class ModuleSSLOpenSSL : public Module
                        log(DEBUG, "m_ssl_openssl.so: Handshake completed");
                        
                        // This will do for setting the ssl flag...it could be done earlier if it's needed. But this seems neater.
-                       Srv->FindDescriptor(session->fd)->Extend("ssl", "ON");
+                       userrec* u = Srv->FindDescriptor(session->fd);
+                       if (u)
+                       {
+                               if (!u->GetExt("ssl", dummy))
+                                       u->Extend("ssl", "ON");
+                       }
                        
                        session->status = ISSL_OPEN;
                        
@@ -603,6 +616,24 @@ class ModuleSSLOpenSSL : public Module
                }
        }
        
+       virtual void OnGlobalConnect(userrec* user)
+       {
+               // This occurs AFTER OnUserConnect so we can be sure the
+               // protocol module has propogated the NICK message.
+               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();                          // Trigger the event. We don't care what module picks it up.
+                       DELETE(event);
+                       DELETE(metadata);
+               }
+       }
+       
        void MakePollWrite(issl_session* session)
        {
                OnRawSocketWrite(session->fd, NULL, 0);
@@ -639,7 +670,7 @@ class ModuleSSLOpenSSLFactory : public ModuleFactory
        {
        }
        
-       virtual Module * CreateModule(Server* Me)
+       virtual Module * CreateModule(InspIRCd* Me)
        {
                return new ModuleSSLOpenSSL(Me);
        }
@@ -650,4 +681,3 @@ extern "C" void * init_module( void )
 {
        return new ModuleSSLOpenSSLFactory;
 }
-