]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree.cpp
Review and optimize
[user/henk/code/inspircd.git] / src / modules / m_spanningtree.cpp
index fe316977b41e79e79585005f0a32b313ed1b8139..8a4599565e3a3993938ab77135a7032afc3c7d95 100644 (file)
@@ -552,6 +552,7 @@ class TreeSocket : public InspSocket
        {
                myhost = host;
                this->LinkState = LISTENER;
+               this->ctx = NULL;
        }
 
        TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
@@ -559,6 +560,7 @@ class TreeSocket : public InspSocket
        {
                myhost = ServerName;
                this->LinkState = CONNECTING;
+               this->ctx = NULL;
        }
 
        /* When a listening socket gives us a new file descriptor,
@@ -569,9 +571,16 @@ class TreeSocket : public InspSocket
                : InspSocket(newfd, ip)
        {
                this->LinkState = WAIT_AUTH_1;
+               this->ctx = NULL;
                this->SendCapabilities();
        }
 
+       ~TreeSocket()
+       {
+               if (ctx)
+                       delete ctx;
+       }
+
        void InitAES(std::string key,std::string SName)
        {
                if (key == "")
@@ -610,6 +619,7 @@ class TreeSocket : public InspSocket
                        {
                                if (x->Name == this->myhost)
                                {
+                                       this->SendCapabilities();
                                        if (x->EncryptionKey != "")
                                        {
                                                if (!(x->EncryptionKey.length() == 16 || x->EncryptionKey.length() == 24 || x->EncryptionKey.length() == 32))
@@ -622,7 +632,6 @@ class TreeSocket : public InspSocket
                                                        this->InitAES(x->EncryptionKey,x->Name);
                                                }
                                        }
-                                       this->SendCapabilities();
                                        /* found who we're supposed to be connecting to, send the neccessary gubbins. */
                                        this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
                                        return true;
@@ -714,7 +723,12 @@ class TreeSocket : public InspSocket
                }
                if (params[0] != this->MyCapabilities())
                {
-                       WriteOpers("*** \2ERROR\2: Server '%s' does not have the same set of modules loaded, cannot link!");
+                       std::string quitserver = this->myhost;
+                       if (this->InboundServerName != "")
+                       {
+                               quitserver = this->InboundServerName;
+                       }
+                       WriteOpers("*** \2ERROR\2: Server '%s' does not have the same set of modules loaded, cannot link!",quitserver.c_str());
                        WriteOpers("*** Our networked module set is: '%s'",this->MyCapabilities().c_str());
                        WriteOpers("*** Other server's networked module set is: '%s'",params[0].c_str());
                        WriteOpers("*** These lists must match exactly on both servers. Please correct these errors, and try again.");
@@ -1225,12 +1239,16 @@ class TreeSocket : public InspSocket
                                        char out[1024];
                                        char result[1024];
                                        log(DEBUG,"Original string '%s'",ret.c_str());
-                                       int nbytes = from64tobits(out, ret.c_str(), 1024);
-                                       log(DEBUG,"m_spanningtree: decrypt %d bytes",nbytes);
-                                       ctx->Decrypt(out, result, nbytes, 0);
-                                       for (int t = 0; t < nbytes; t++)
-                                               if (result[t] == '\7') result[t] = 0;
-                                       ret = result;
+                                       /* ERROR + CAPAB is still allowed unencryped */
+                                       if ((ret.substr(0,7) != "ERROR :") && (ret.substr(0,6) != "CAPAB "))
+                                       {
+                                               int nbytes = from64tobits(out, ret.c_str(), 1024);
+                                               log(DEBUG,"m_spanningtree: decrypt %d bytes",nbytes);
+                                               ctx->Decrypt(out, result, nbytes, 0);
+                                               for (int t = 0; t < nbytes; t++)
+                                                       if (result[t] == '\7') result[t] = 0;
+                                               ret = result;
+                                       }
                                }
                                if (!this->ProcessLine(ret))
                                {
@@ -1247,8 +1265,8 @@ class TreeSocket : public InspSocket
                if (this->ctx)
                {
                        log(DEBUG,"AES context");
-                       char result[1024];
-                       char result64[1024];
+                       char result[10240];
+                       char result64[10240];
                        if (this->keylength)
                        {
                                while (line.length() % this->keylength != 0)
@@ -1257,10 +1275,13 @@ class TreeSocket : public InspSocket
                                        line = line + "\7";
                                }
                        }
-                       ctx->Encrypt(line.c_str(), result, line.length(),0);
+                       unsigned int ll = line.length();
+                       log(DEBUG,"Plaintext line with padding = %d chars",ll);
+                       ctx->Encrypt(line.c_str(), result, ll, 0);
+                       log(DEBUG,"Encrypted.");
                        to64frombits((unsigned char*)result64,
                                        (unsigned char*)result,
-                                       line.length());
+                                       ll);
                        line = result64;
                        log(DEBUG,"Encrypted: %s",line.c_str());
                        //int from64tobits(char *out, const char *in, int maxlen);
@@ -2098,6 +2119,7 @@ class TreeSocket : public InspSocket
                {
                        Squit(s,"Remote host closed the connection");
                }
+               WriteOpers("Server '\2%s\2[%s]' closed the connection.",quitserver.c_str(),this->GetIP().c_str());
        }
 
        virtual int OnIncomingConnection(int newsock, char* ip)
@@ -2625,8 +2647,12 @@ class ModuleSpanningTree : public Module
                return false;
        }
 
-       virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
+       virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated)
        {
+               /* If the command doesnt appear to be valid, we dont want to mess with it. */
+               if (!validated)
+                       return 0;
+
                if (command == "CONNECT")
                {
                        return this->HandleConnect(parameters,pcnt,user);