]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree.cpp
Allowed capab unencrypted in an encrypted connection
[user/henk/code/inspircd.git] / src / modules / m_spanningtree.cpp
index 5df518ab11d46c57f22d02f2c8e7b9600bc2527a..57872b65d63e7a209678addd6aa35550ec357963 100644 (file)
@@ -95,8 +95,6 @@ extern int MODCOUNT;
 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
 
 /* We need to import these from the core for use in netbursts */
-/*typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
-typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;*/
 extern user_hash clientlist;
 extern chan_hash chanlist;
 
@@ -191,6 +189,7 @@ class TreeServer
                UserCount = OperCount = 0;
                VersionString = Srv->GetVersion();
                Route = NULL;
+               Socket = NULL; /* Fix by brain */
                AddHashEntry();
        }
 
@@ -294,17 +293,17 @@ class TreeServer
 
        std::string GetName()
        {
-               return this->ServerName;
+               return ServerName;
        }
 
        std::string GetDesc()
        {
-               return this->ServerDesc;
+               return ServerDesc;
        }
 
        std::string GetVersion()
        {
-               return this->VersionString;
+               return VersionString;
        }
 
        void SetNextPingTime(time_t t)
@@ -315,7 +314,7 @@ class TreeServer
 
        time_t NextPingTime()
        {
-               return this->NextPing;
+               return NextPing;
        }
 
        bool AnsweredLastPing()
@@ -330,22 +329,22 @@ class TreeServer
 
        int GetUserCount()
        {
-               return this->UserCount;
+               return UserCount;
        }
 
        int GetOperCount()
        {
-               return this->OperCount;
+               return OperCount;
        }
 
        TreeSocket* GetSocket()
        {
-               return this->Socket;
+               return Socket;
        }
 
        TreeServer* GetParent()
        {
-               return this->Parent;
+               return Parent;
        }
 
        void SetVersion(std::string Version)
@@ -570,6 +569,7 @@ class TreeSocket : public InspSocket
                : InspSocket(newfd, ip)
        {
                this->LinkState = WAIT_AUTH_1;
+               this->SendCapabilities();
        }
 
        void InitAES(std::string key,std::string SName)
@@ -610,6 +610,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))
@@ -678,6 +679,56 @@ class TreeSocket : public InspSocket
                }
        }
 
+       std::string MyCapabilities()
+       {
+               ServerConfig* Config = Srv->GetConfig();
+               std::vector<std::string> modlist;
+               std::string capabilities = "";
+
+                for (int i = 0; i <= MODCOUNT; i++)
+                {
+                       if ((modules[i]->GetVersion().Flags & VF_STATIC) || (modules[i]->GetVersion().Flags & VF_COMMON))
+                               modlist.push_back(Config->module_names[i]);
+                }
+               sort(modlist.begin(),modlist.end());
+               for (unsigned int i = 0; i < modlist.size(); i++)
+               {
+                       if (i)
+                               capabilities = capabilities + ",";
+                       capabilities = capabilities + modlist[i];
+               }
+               return capabilities;
+       }
+       
+       void SendCapabilities()
+       {
+               this->WriteLine("CAPAB "+MyCapabilities());
+       }
+
+       bool Capab(std::deque<std::string> params)
+       {
+               if (params.size() != 1)
+               {
+                       this->WriteLine("ERROR :Invalid number of parameters for CAPAB");
+                       return false;
+               }
+               if (params[0] != this->MyCapabilities())
+               {
+                       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.");
+                       this->WriteLine("ERROR :CAPAB mismatch; My capabilities: '"+this->MyCapabilities()+"'");
+                       return false;
+               }
+               return true;
+       }
+
        /* This function forces this server to quit, removing this server
         * and any users on it (and servers and users below that, etc etc).
         * It's very slow and pretty clunky, but luckily unless your network
@@ -714,7 +765,7 @@ class TreeSocket : public InspSocket
         */
        void Squit(TreeServer* Current,std::string reason)
        {
-               if (Current)
+               if ((Current) && (Current != TreeRoot))
                {
                        std::deque<std::string> params;
                        params.push_back(Current->GetName());
@@ -1179,12 +1230,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))
                                {
@@ -1201,8 +1256,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)
@@ -1211,10 +1266,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);
@@ -1775,6 +1833,10 @@ class TreeSocket : public InspSocket
                                        this->WriteLine("ERROR :Client connections to this port are prohibited.");
                                        return false;
                                }
+                               else if (command == "CAPAB")
+                               {
+                                       return this->Capab(params);
+                               }
                                else
                                {
                                        this->WriteLine("ERROR :Invalid command in negotiation phase.");
@@ -1808,6 +1870,10 @@ class TreeSocket : public InspSocket
                                {
                                        return this->Error(params);
                                }
+                               else if (command == "CAPAB")
+                               {
+                                       return this->Capab(params);
+                               }
                                
                        break;
                        case LISTENER:
@@ -2044,6 +2110,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)
@@ -2419,9 +2486,15 @@ class ModuleSpanningTree : public Module
                TreeServer* s = FindServerMask(parameters[0]);
                if (s)
                {
+                       if (s == TreeRoot)
+                       {
+                                WriteServ(user->fd,"NOTICE %s :*** SQUIT: Foolish mortal, you cannot make a server SQUIT itself! (%s matches local server name)",user->nick,parameters[0]);
+                               return 1;
+                       }
                        TreeSocket* sock = s->GetSocket();
                        if (sock)
                        {
+                               log(DEBUG,"Splitting server %s",s->GetName().c_str());
                                WriteOpers("*** SQUIT: Server \002%s\002 removed from network by %s",parameters[0],user->nick);
                                sock->Squit(s,"Server quit by "+std::string(user->nick)+"!"+std::string(user->ident)+"@"+std::string(user->host));
                                sock->Close();
@@ -2565,8 +2638,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);