]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree/treesocket2.cpp
Add config <options:disablehmac> to support disabling of HMAC, and tidy up to detect...
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket2.cpp
index d287d4724f3f9ffdae14bdd492346b3d950d6eb4..6c0418a0bd9a9bb414c86bc2741be8f5d4049aa1 100644 (file)
@@ -1,3 +1,16 @@
+/*       +------------------------------------+
+ *       | Inspire Internet Relay Chat Daemon |
+ *       +------------------------------------+
+ *
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
+ * This program is free but copyrighted software; see
+ *            the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
 #include "configreader.h"
 #include "users.h"
 #include "channels.h"
@@ -38,6 +51,61 @@ bool TreeSocket::Error(std::deque<std::string> &params)
        return false;
 }
 
+bool TreeSocket::Modules(const std::string &prefix, std::deque<std::string> &params)
+{
+       if (params.empty())
+               return true;
+
+       if (!this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
+       {
+               /* Pass it on, not for us */
+               Utils->DoOneToOne(prefix, "MODULES", params, params[0]);
+               return true;
+       }
+
+       char strbuf[MAXBUF];
+       std::deque<std::string> par;
+       par.push_back(prefix);
+       par.push_back("");
+
+       userrec* source = this->Instance->FindNick(prefix);
+       if (!source)
+               return true;
+
+       for (unsigned int i = 0; i < Instance->Config->module_names.size(); i++)
+       {
+               Version V = Instance->modules[i]->GetVersion();
+               char modulename[MAXBUF];
+               char flagstate[MAXBUF];
+               *flagstate = 0;
+               if (V.Flags & VF_STATIC)
+                       strlcat(flagstate,", static",MAXBUF);
+               if (V.Flags & VF_VENDOR)
+                       strlcat(flagstate,", vendor",MAXBUF);
+               if (V.Flags & VF_COMMON)
+                       strlcat(flagstate,", common",MAXBUF);
+               if (V.Flags & VF_SERVICEPROVIDER)
+                       strlcat(flagstate,", service provider",MAXBUF);
+               if (!flagstate[0])
+                       strcpy(flagstate,"  <no flags>");
+               strlcpy(modulename,Instance->Config->module_names[i].c_str(),256);
+               if (*source->oper)
+               {
+                       snprintf(strbuf, MAXBUF, "::%s 900 %s :0x%08lx %d.%d.%d.%d %s (%s)",Instance->Config->ServerName,source->nick,(long unsigned int)Instance->modules[i],V.Major,V.Minor,V.Revision,V.Build,ServerConfig::CleanFilename(modulename),flagstate+2);
+               }
+               else
+               {
+                       snprintf(strbuf, MAXBUF, "::%s 900 %s :%s",Instance->Config->ServerName,source->nick,ServerConfig::CleanFilename(modulename));
+               }
+               par[1] = strbuf;
+               Utils->DoOneToOne(Instance->Config->ServerName, "PUSH", par, source->server);
+       }
+       snprintf(strbuf, MAXBUF, "::%s 901 %s :End of MODULES list", Instance->Config->ServerName, source->nick);
+       par[1] = strbuf;
+       Utils->DoOneToOne(Instance->Config->ServerName, "PUSH", par, source->server);
+       return true;
+}
+
 /** remote MOTD. leet, huh? */
 bool TreeSocket::Motd(const std::string &prefix, std::deque<std::string> &params)
 {
@@ -207,6 +275,21 @@ bool TreeSocket::ForceNick(const std::string &prefix, std::deque<std::string> &p
        return true;
 }
 
+bool TreeSocket::OperQuit(const std::string &prefix, std::deque<std::string> &params)
+{
+       if (params.size() < 1)
+               return true;
+
+       userrec* u = this->Instance->FindNick(prefix);
+
+       if (u)
+       {
+               Utils->DoOneToAllButSender(prefix,"OPERQUIT",params,prefix);
+               u->SetOperQuit(params[0]);
+       }
+       return true;
+}
+
 /*
  * Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
  * then let that server do the dirty work (squit it!). Example:
@@ -265,7 +348,7 @@ bool TreeSocket::ServiceJoin(const std::string &prefix, std::deque<std::string>
        {
                /* only join if it's local, otherwise just pass it on! */
                if (IS_LOCAL(u))
-                       chanrec::JoinUser(this->Instance, u, params[1].c_str(), false);
+                       chanrec::JoinUser(this->Instance, u, params[1].c_str(), false, "", Instance->Time());
                Utils->DoOneToAllButSender(prefix,"SVSJOIN",params,prefix);
        }
        return true;
@@ -330,6 +413,7 @@ bool TreeSocket::LocalPong(const std::string &prefix, std::deque<std::string> &p
                if (ServerSource)
                {
                        ServerSource->SetPingFlag();
+                       ServerSource->rtt = Instance->Time() - ServerSource->LastPing;
                }
        }
        else
@@ -472,7 +556,8 @@ bool TreeSocket::AddLine(const std::string &prefix, std::deque<std::string> &par
        {
                if (atoi(params[4].c_str()))
                {
-                       this->Instance->SNO->WriteToSnoMask('x',"%s Added %cLINE on %s to expire in %lu seconds (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),atoi(params[4].c_str()),params[5].c_str());
+                       time_t c_requires_crap = ConvToInt(params[4]) + Instance->Time();
+                       this->Instance->SNO->WriteToSnoMask('x',"%s Added %cLINE on %s to expire on %s (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),Instance->TimeString(c_requires_crap).c_str(),params[5].c_str());
                }
                else
                {
@@ -587,28 +672,17 @@ bool TreeSocket::HandleSetTime(const std::string &prefix, std::deque<std::string
        if ((params.size() == 2) && (params[1] == "FORCE"))
                force = true;
 
-       time_t rts = atoi(params[0].c_str());
-       time_t us = Instance->Time(true);
-
-       if (rts == us)
-       {
-               Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
-       }
-       else if (force || (rts < us))
-       {
-               int old = Instance->SetTimeDelta(rts - us);
-               Instance->Log(DEBUG, "%s TS (diff %d) from %s applied (old delta was %d)", (force) ? "Forced" : "Lower", rts - us, prefix.c_str(), old);
+       time_t them = atoi(params[0].c_str());
+       time_t us = Instance->Time(false);
 
-               Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
-       }
-       else
-       {
-               Instance->Log(DEBUG, "Higher TS (diff %d) from %s overridden", us - rts, prefix.c_str());
+       time_t diff = them - us;
 
-               std::deque<std::string> oparams;
-               oparams.push_back(ConvToStr(us));
+       Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
 
-               Utils->DoOneToMany(prefix, "TIMESET", oparams);
+       if (force || (them != us))
+       {
+               time_t old = Instance->SetTimeDelta(diff);
+               Instance->Log(DEBUG, "TS (diff %d) from %s applied (old delta was %d)", diff, prefix.c_str(), old);
        }
 
        return true;
@@ -728,10 +802,11 @@ bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string>
        if (CheckDupe)
        {
                this->WriteLine("ERROR :Server "+servername+" already exists!");
-               this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+servername+"\2 denied, already exists");
+               this->Instance->SNO->WriteToSnoMask('l',"Server \2"+servername+"\2 being introduced from \2" + prefix + "\2 denied, already exists. Closing link with " + prefix);
                return false;
        }
-       TreeServer* Node = new TreeServer(this->Utils,this->Instance,servername,description,ParentOfThis,NULL);
+       Link* lnk = Utils->FindLink(servername);
+       TreeServer* Node = new TreeServer(this->Utils,this->Instance,servername,description,ParentOfThis,NULL, lnk ? lnk->Hidden : false);
        ParentOfThis->AddChild(Node);
        params[3] = ":" + params[3];
        Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
@@ -739,6 +814,24 @@ bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string>
        return true;
 }
 
+bool TreeSocket::ComparePass(const std::string &ours, const std::string &theirs)
+{
+       if ((!strncmp(ours.c_str(), "HMAC-SHA256:", 12)) || (!strncmp(theirs.c_str(), "HMAC-SHA256:", 12)))
+       {
+               /* One or both of us specified hmac sha256, but we don't have sha256 module loaded!
+                * We can't allow this password as valid.
+                */
+               if (!Instance->FindModule("m_sha256.so") || !Utils->ChallengeResponse)
+                               return false;
+               else
+                       /* Straight string compare of hashes */
+                       return ours == theirs;
+       }
+       else
+               /* Straight string compare of plaintext */
+               return ours == theirs;
+}
+
 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> &params)
 {
        if (params.size() < 4)
@@ -758,7 +851,7 @@ bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> &params)
        std::string description = params[3];
        for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
        {
-               if ((x->Name == servername) && (x->RecvPass == password))
+               if ((x->Name == servername) && (ComparePass(this->MakePass(x->RecvPass,this->GetOurChallenge()),password)))
                {
                        TreeServer* CheckDupe = Utils->FindServer(sname);
                        if (CheckDupe)
@@ -775,7 +868,7 @@ bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> &params)
                        // we should add the details of this server now
                        // to the servers tree, as a child of the root
                        // node.
-                       TreeServer* Node = new TreeServer(this->Utils,this->Instance,sname,description,Utils->TreeRoot,this);
+                       TreeServer* Node = new TreeServer(this->Utils,this->Instance,sname,description,Utils->TreeRoot,this,x->Hidden);
                        Utils->TreeRoot->AddChild(Node);
                        params[3] = ":" + params[3];
                        Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,sname);
@@ -807,7 +900,7 @@ bool TreeSocket::Inbound_Server(std::deque<std::string> &params)
        std::string description = params[3];
        for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
        {
-               if ((x->Name == servername) && (x->RecvPass == password))
+               if ((x->Name == servername) && (ComparePass(this->MakePass(x->RecvPass,this->GetOurChallenge()),password)))
                {
                        TreeServer* CheckDupe = Utils->FindServer(sname);
                        if (CheckDupe)
@@ -827,7 +920,7 @@ bool TreeSocket::Inbound_Server(std::deque<std::string> &params)
                        this->InboundDescription = description;
                        // this is good. Send our details: Our server name and description and hopcount of 0,
                        // along with the sendpass from this block.
-                       this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+x->SendPass+" 0 :"+this->Instance->Config->ServerDesc);
+                       this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 :"+this->Instance->Config->ServerDesc);
                        // move to the next state, we are now waiting for THEM.
                        this->LinkState = WAIT_AUTH_2;
                        return true;
@@ -843,7 +936,7 @@ void TreeSocket::Split(const std::string &line, std::deque<std::string> &n)
        n.clear();
        irc::tokenstream tokens(line);
        std::string param;
-       while ((param = tokens.GetToken()) != "")
+       while (tokens.GetToken(param))
                n.push_back(param);
        return;
 }
@@ -932,34 +1025,30 @@ bool TreeSocket::ProcessLine(std::string &line)
                        {
                                if (params.size() && Utils->EnableTimeSync)
                                {
-                                       /* If a time stamp is provided, apply synchronization */
-                                       bool force = false;
+                                       bool we_have_delta = (Instance->Time(false) != Instance->Time(true));
                                        time_t them = atoi(params[0].c_str());
-                                       time_t us = Instance->Time(false);
-                                       int delta = them - us;
-                                       if ((params.size() == 2) && (params[1] == "FORCE"))
-                                               force = true;
-                                       if ((delta < -600) || (delta > 600))
+                                       time_t delta = them - Instance->Time(false);
+                                       if ((delta < -300) || (delta > 300))
                                        {
-                                               this->Instance->SNO->WriteToSnoMask('l',"\2ERROR\2: Your clocks are out by %d seconds (this is more than ten minutes). Link aborted, \2PLEASE SYNC YOUR CLOCKS!\2",abs(delta));
-                                               this->WriteLine("ERROR :Your clocks are out by "+ConvToStr(abs(delta))+" seconds (this is more than ten minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
+                                               Instance->SNO->WriteToSnoMask('l',"\2ERROR\2: Your clocks are out by %d seconds (this is more than five minutes). Link aborted, \2PLEASE SYNC YOUR CLOCKS!\2",abs(delta));
+                                               WriteLine("ERROR :Your clocks are out by "+ConvToStr(abs(delta))+" seconds (this is more than ten minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
                                                return false;
                                        }
+                                       else if ((delta < -30) || (delta > 30))
+                                       {
+                                               Instance->SNO->WriteToSnoMask('l',"\2WARNING\2: Your clocks are out by %d seconds. Please consider synching your clocks.", abs(delta));
+                                       }
 
-                                       if (force || (us > them))
+                                       if (!Utils->MasterTime && !we_have_delta)
                                        {
-                                               this->Instance->SetTimeDelta(them - us);
+                                               this->Instance->SetTimeDelta(delta);
                                                // Send this new timestamp to any other servers
                                                Utils->DoOneToMany(Utils->TreeRoot->GetName(), "TIMESET", params);
                                        }
-                                       else
-                                       {
-                                               // Override the timestamp
-                                               this->WriteLine(":" + Utils->TreeRoot->GetName() + " TIMESET " + ConvToStr(us));
-                                       }
                                }
                                this->LinkState = CONNECTED;
-                               Node = new TreeServer(this->Utils,this->Instance,InboundServerName,InboundDescription,Utils->TreeRoot,this);
+                               Link* lnk = Utils->FindLink(InboundServerName);
+                               Node = new TreeServer(this->Utils,this->Instance, InboundServerName, InboundDescription, Utils->TreeRoot, this, lnk ? lnk->Hidden : false);
                                Utils->TreeRoot->AddChild(Node);
                                params.clear();
                                params.push_back(InboundServerName);
@@ -998,6 +1087,10 @@ bool TreeSocket::ProcessLine(std::string &line)
                        {
                                return this->Error(params);
                        }
+                       else if (command == "CAPAB")
+                       {
+                               return this->Capab(params);
+                       }
                break;
                case CONNECTED:
                        // This is the 'authenticated' state, when all passwords
@@ -1039,7 +1132,7 @@ bool TreeSocket::ProcessLine(std::string &line)
                        /* Yes, know, this is a mess. Its reasonably fast though as we're
                         * working with std::string here.
                         */
-                       if ((command == "NICK") && (params.size() > 1))
+                       if ((command == "NICK") && (params.size() >= 8))
                        {
                                return this->IntroduceClient(prefix,params);
                        }
@@ -1055,6 +1148,10 @@ bool TreeSocket::ProcessLine(std::string &line)
                        {
                                return this->Motd(prefix, params);
                        }
+                       else if (command == "MODULES")
+                       {
+                               return this->Modules(prefix, params);
+                       }
                        else if (command == "ADMIN")
                        {
                                return this->Admin(prefix, params);
@@ -1157,6 +1254,10 @@ bool TreeSocket::ProcessLine(std::string &line)
                                }
                                return this->ForceNick(prefix,params);
                        }
+                       else if (command == "OPERQUIT")
+                       {
+                               return this->OperQuit(prefix,params);
+                       }
                        else if (command == "RSQUIT")
                        {
                                return this->RemoteSquit(prefix, params);