]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree.cpp
Encryption debug
[user/henk/code/inspircd.git] / src / modules / m_spanningtree.cpp
index 334310b8c6a26c4bcf22b4220944cf31142d6b10..49493b423970d4f75f7e50130d1229ca2816d7d1 100644 (file)
@@ -41,6 +41,7 @@ using namespace std;
 #include "xline.h"
 #include "typedefs.h"
 #include "cull_list.h"
+#include "aes.h"
 
 #ifdef GCC3
 #define nspace __gnu_cxx
@@ -439,6 +440,7 @@ class Link
         std::string RecvPass;
         unsigned long AutoConnect;
         time_t NextConnectTime;
+        std::string EncryptionKey;
 };
 
 /* The usual stuff for inspircd modules,
@@ -536,6 +538,8 @@ class TreeSocket : public InspSocket
        time_t NextPing;
        bool LastPingWasGood;
        bool bursting;
+       AES* ctx;
+       unsigned int keylength;
        
  public:
 
@@ -567,6 +571,26 @@ class TreeSocket : public InspSocket
        {
                this->LinkState = WAIT_AUTH_1;
        }
+
+       void InitAES(std::string key)
+       {
+               if (key == "")
+                       return;
+
+               ctx = new AES();
+               log(DEBUG,"Initialized AES key %s",key.c_str());
+               // key must be 16, 24, 32 etc bytes (multiple of 8)
+               keylength = key.length();
+               if (!(keylength == 16 || keylength == 24 || keylength == 32))
+               {
+                       log(DEBUG,"Key length not 16, 24 or 32 characters!");
+               }
+               else
+               {
+                       ctx->MakeKey(key.c_str(), "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
+                               \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", keylength, keylength);
+               }
+       }
        
        /* When an outbound connection finishes connecting, we receive
         * this event, and must send our SERVER string to the other
@@ -1137,6 +1161,16 @@ class TreeSocket : public InspSocket
                                /* Process this one, abort if it
                                 * didnt return true.
                                 */
+                               if (this->ctx)
+                               {
+                                       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);
+                                       ret = result;
+                               }
                                if (!this->ProcessLine(ret))
                                {
                                        return false;
@@ -1149,6 +1183,27 @@ class TreeSocket : public InspSocket
        int WriteLine(std::string line)
        {
                log(DEBUG,"OUT: %s",line.c_str());
+               if (this->ctx)
+               {
+                       log(DEBUG,"AES context");
+                       char result[1024];
+                       char result64[1024];
+                       if (this->keylength)
+                       {
+                               while (line.length() % this->keylength != 0)
+                               {
+                                       // pad it to be a multiple of the key length
+                                       line = line + "\0";
+                               }
+                       }
+                       ctx->Encrypt(line.c_str(), result, line.length(),0);
+                       to64frombits((unsigned char*)result64,
+                                       (unsigned char*)result,
+                                       line.length());
+                       line = result64;
+                       log(DEBUG,"Encrypted: %s",line.c_str());
+                       //int from64tobits(char *out, const char *in, int maxlen);
+               }
                return this->Write(line + "\r\n");
        }
 
@@ -1847,6 +1902,16 @@ class TreeSocket : public InspSocket
                                else if (command == "ENDBURST")
                                {
                                        this->bursting = false;
+                                       std::string sserv = this->myhost;
+                                       if (this->InboundServerName != "")
+                                               sserv = this->InboundServerName;
+                                       for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
+                                       {
+                                               if ((x->EncryptionKey != "") && (x->Name == sserv))
+                                               {
+                                                       this->InitAES(x->EncryptionKey);
+                                               }
+                                       }
                                        return true;
                                }
                                else
@@ -2138,9 +2203,18 @@ void ReadConfiguration(bool rebind)
                L.SendPass = Conf->ReadValue("link","sendpass",j);
                L.RecvPass = Conf->ReadValue("link","recvpass",j);
                L.AutoConnect = Conf->ReadInteger("link","autoconnect",j,true);
+               L.EncryptionKey =  Conf->ReadValue("link","encryptionkey",j);
                L.NextConnectTime = time(NULL) + L.AutoConnect;
-               LinkBlocks.push_back(L);
-               log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
+               /* Bugfix by brain, do not allow people to enter bad configurations */
+               if ((L.RecvPass != "") && (L.SendPass != "") && (L.Name != "") && (L.Port))
+               {
+                       LinkBlocks.push_back(L);
+                       log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
+               }
+               else
+               {
+                       log(DEFAULT,"m_spanningtree: Invalid configuration for server '%s', ignored!",L.Name.c_str());
+               }
        }
        delete Conf;
 }