X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_spanningtree%2Ftreesocket2.cpp;h=42fb708a9589052bbc2c0a84381978c8fa884eca;hb=c23d09f65084e6088111dc974f0e290b042de89d;hp=42921058c995b4194df51579de8ed6370ede5344;hpb=d1b146808e53494f42cd6c89f81a6106b7bde2c2;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_spanningtree/treesocket2.cpp b/src/modules/m_spanningtree/treesocket2.cpp index 42921058c..42fb708a9 100644 --- a/src/modules/m_spanningtree/treesocket2.cpp +++ b/src/modules/m_spanningtree/treesocket2.cpp @@ -1,25 +1,32 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ +/* + * InspIRCd -- Internet Relay Chat Daemon * - * InspIRCd: (C) 2002-2010 InspIRCd Development Team - * See: http://wiki.inspircd.org/Credits + * Copyright (C) 2007-2008, 2012 Robin Burchell + * Copyright (C) 2009-2010 Daniel De Graaf + * Copyright (C) 2007-2008 Craig Edwards + * Copyright (C) 2008 Pippijn van Steenhoven + * Copyright (C) 2008 Thomas Stagner + * Copyright (C) 2007 Dennis Friis * - * This program is free but copyrighted software; see - * the file COPYING for details. + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. * - * --------------------------------------------------- + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + #include "inspircd.h" -#include "socket.h" -#include "xline.h" -#include "socketengine.h" #include "main.h" #include "utils.h" #include "treeserver.h" -#include "link.h" #include "treesocket.h" #include "resolvers.h" @@ -36,7 +43,7 @@ void TreeSocket::Split(const std::string& line, std::string& prefix, std::string if (!tokens.GetToken(prefix)) return; - + if (prefix[0] == ':') { prefix = prefix.substr(1); @@ -73,7 +80,7 @@ void TreeSocket::ProcessLine(std::string &line) std::string command; parameterlist params; - ServerInstance->Logs->Log("m_spanningtree",DEBUG, "S[%d] I %s", this->GetFd(), line.c_str()); + ServerInstance->Logs->Log("m_spanningtree", LOG_RAWIO, "S[%d] I %s", this->GetFd(), line.c_str()); Split(line, prefix, command, params); @@ -82,8 +89,6 @@ void TreeSocket::ProcessLine(std::string &line) switch (this->LinkState) { - TreeServer* Node; - case WAIT_AUTH_1: /* * State WAIT_AUTH_1: @@ -120,7 +125,7 @@ void TreeSocket::ProcessLine(std::string &line) } else { - this->SendError(std::string("Invalid command in negotiation phase: ") + command.c_str()); + this->SendError("Invalid command in negotiation phase: " + command); } break; case WAIT_AUTH_2: @@ -142,7 +147,7 @@ void TreeSocket::ProcessLine(std::string &line) { if (params.size()) { - time_t them = atoi(params[0].c_str()); + time_t them = ConvToInt(params[0]); time_t delta = them - ServerInstance->Time(); if ((delta < -600) || (delta > 600)) { @@ -155,30 +160,32 @@ void TreeSocket::ProcessLine(std::string &line) ServerInstance->SNO->WriteGlobalSno('l',"\2WARNING\2: Your clocks are out by %d seconds. Please consider synching your clocks.", abs((long)delta)); } } - this->LinkState = CONNECTED; + // Check for duplicate server name/sid again, it's possible that a new + // server was introduced while we were waiting for them to send BURST. + // (we do not reserve their server name/sid when they send SERVER, we do it now) + if (!CheckDuplicate(capab->name, capab->sid)) + return; + + this->LinkState = CONNECTED; Utils->timeoutlist.erase(this); - if (myautoconnect) - { - myautoconnect->position = -1; - myautoconnect = NULL; - } - Link* lnk = Utils->FindLink(InboundServerName); + linkID = capab->name; - Node = new TreeServer(this->Utils, InboundServerName, InboundDescription, InboundSID, Utils->TreeRoot, this, lnk ? lnk->Hidden : false); + MyRoot = new TreeServer(Utils, capab->name, capab->description, capab->sid, Utils->TreeRoot, this, capab->hidden); + Utils->TreeRoot->AddChild(MyRoot); + + MyRoot->bursting = true; + this->DoBurst(MyRoot); - Utils->TreeRoot->AddChild(Node); parameterlist sparams; - sparams.push_back(InboundServerName); + sparams.push_back(MyRoot->GetName()); sparams.push_back("*"); - sparams.push_back("1"); - sparams.push_back(InboundSID); - sparams.push_back(":"+InboundDescription); - Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",sparams,InboundServerName); - Utils->DoOneToAllButSender(prefix, "BURST", params, InboundServerName); - Node->bursting = true; - this->DoBurst(Node); + sparams.push_back("0"); + sparams.push_back(MyRoot->GetID()); + sparams.push_back(":" + MyRoot->GetDesc()); + Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(), "SERVER", sparams, MyRoot->GetName()); + Utils->DoOneToAllButSender(MyRoot->GetID(), "BURST", params, MyRoot->GetName()); } else if (command == "ERROR") { @@ -229,20 +236,15 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, User* who = ServerInstance->FindUUID(prefix); std::string direction; - if (who) - { - direction = who->server; - } - else + if (!who) { TreeServer* ServerSource = Utils->FindServer(prefix); if (prefix.empty()) - ServerSource = Utils->FindServer(GetName()); + ServerSource = MyRoot; if (ServerSource) { who = ServerSource->ServerUser; - direction = prefix; } else { @@ -250,13 +252,30 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, * due to various race conditions such as the KILL message for a user somehow * crossing the users QUIT further upstream from the server. Thanks jilles! */ - ServerInstance->Logs->Log("m_spanningtree", DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.", - command.c_str(), prefix.c_str()); - return; + + if ((prefix.length() == UIDGenerator::UUID_LENGTH) && (isdigit(prefix[0])) && + ((command == "FMODE") || (command == "MODE") || (command == "KICK") || (command == "TOPIC") || (command == "KILL") || (command == "ADDLINE") || (command == "DELLINE"))) + { + /* Special case, we cannot drop these commands as they've been committed already on a + * part of the network by the time we receive them, so in this scenario pretend the + * command came from a server to avoid desync. + */ + + who = ServerInstance->FindUUID(prefix.substr(0, 3)); + if (!who) + who = this->MyRoot->ServerUser; + } + else + { + ServerInstance->Logs->Log("m_spanningtree", LOG_DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.", + command.c_str(), prefix.c_str()); + return; + } } } // Make sure prefix is still good + direction = who->server; prefix = who->uuid; /* @@ -279,8 +298,8 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, if ((!route_back_again) || (route_back_again->GetSocket() != this)) { if (route_back_again) - ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Protocol violation: Fake direction '%s' from connection '%s'", - prefix.c_str(),this->GetName().c_str()); + ServerInstance->Logs->Log("m_spanningtree", LOG_DEBUG, "Protocol violation: Fake direction '%s' from connection '%s'", + prefix.c_str(),linkID.c_str()); return; } @@ -291,6 +310,12 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, if (command == "SVSMODE") // This isn't in an "else if" so we still force FMODE for changes on channels. command = "MODE"; + if (proto_version < ProtocolVersion) + { + if (!PreProcessOldProtocolMessage(who, command, params)) + return; + } + // TODO move all this into Commands if (command == "MAP") { @@ -361,7 +386,8 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, { if (params.size() >= 2) { - ServerInstance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + who->nick + ": "+ params[1]); + ServerInstance->SNO->WriteToSnoMask(params[0][0], "From " + who->nick + ": "+ params[1]); + params[1] = ":" + params[1]; Utils->DoOneToAllButSender(prefix, command, params, prefix); } } @@ -398,7 +424,7 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, { if (params.size() != 2) { - SendError("Protocol violation: NICK message without TS - :"+std::string(who->uuid)+" NICK "+params[0]); + SendError("Protocol violation: Wrong number of parameters for NICK message"); return; } @@ -408,8 +434,14 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, return; } + if ((isdigit(params[0][0])) && (params[0] != who->uuid)) + { + SendError("Protocol violation: User changing nick to an invalid UID - " + params[0]); + return; + } + /* Update timestamp on user when they change nicks */ - who->age = atoi(params[1].c_str()); + who->age = ConvToInt(params[1]); /* * On nick messages, check that the nick doesnt already exist here. @@ -431,65 +463,81 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, return; } } - who->ForceNickChange(params[0].c_str()); + who->ForceNickChange(params[0]); Utils->RouteCommand(route_back_again, command, params, who); } else { Command* cmd = ServerInstance->Parser->GetHandler(command); - CmdResult res = CMD_INVALID; - if (cmd && params.size() >= cmd->min_params) + + if (!cmd) + { + irc::stringjoiner pmlist(params); + ServerInstance->Logs->Log("m_spanningtree", LOG_SPARSE, "Unrecognised S2S command :%s %s %s", + who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str()); + SendError("Unrecognised command '" + command + "' -- possibly loaded mismatched modules"); + return; + } + + if (params.size() < cmd->min_params) { - res = cmd->Handle(params, who); + irc::stringjoiner pmlist(params); + ServerInstance->Logs->Log("m_spanningtree", LOG_SPARSE, "Insufficient parameters for S2S command :%s %s %s", + who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str()); + SendError("Insufficient parameters for command '" + command + "'"); + return; } + if ((!params.empty()) && (params.back().empty()) && (!cmd->allow_empty_last_param)) + { + // the last param is empty and the command handler doesn't allow that, check if there will be enough params if we drop the last + if (params.size()-1 < cmd->min_params) + return; + params.pop_back(); + } + + CmdResult res = cmd->Handle(params, who); + if (res == CMD_INVALID) - SendError("Unrecognised or malformed command '" + command + "' -- possibly loaded mismatched modules"); - if (res == CMD_SUCCESS) + { + irc::stringjoiner pmlist(params); + ServerInstance->Logs->Log("m_spanningtree", LOG_SPARSE, "Error handling S2S command :%s %s %s", + who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str()); + SendError("Error handling '" + command + "' -- possibly loaded mismatched modules"); + } + else if (res == CMD_SUCCESS) Utils->RouteCommand(route_back_again, command, params, who); } } -std::string TreeSocket::GetName() -{ - std::string sourceserv = this->myhost; - if (!this->InboundServerName.empty()) - { - sourceserv = this->InboundServerName; - } - return sourceserv; -} - void TreeSocket::OnTimeout() { - ServerInstance->SNO->WriteGlobalSno('l', "CONNECT: Connection to \002%s\002 timed out.", myhost.c_str()); + ServerInstance->SNO->WriteGlobalSno('l', "CONNECT: Connection to \002%s\002 timed out.", linkID.c_str()); } void TreeSocket::Close() { + if (fd != -1) + ServerInstance->GlobalCulls.AddItem(this); this->BufferedSocket::Close(); SetError("Remote host closed connection"); // Connection closed. // If the connection is fully up (state CONNECTED) // then propogate a netsplit to all peers. - std::string quitserver = this->myhost; - if (!this->InboundServerName.empty()) - { - quitserver = this->InboundServerName; - } - TreeServer* s = Utils->FindServer(quitserver); - if (s && s->GetSocket() == this) - { - Squit(s,getError()); - } + if (MyRoot) + Squit(MyRoot,getError()); - if (!quitserver.empty()) + if (!ConnectionFailureShown) { - ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\2%s\2' failed.",quitserver.c_str()); + ConnectionFailureShown = true; + ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\2%s\2' failed.",linkID.c_str()); time_t server_uptime = ServerInstance->Time() - this->age; if (server_uptime) - ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\2%s\2' was established for %s", quitserver.c_str(), Utils->Creator->TimeToStr(server_uptime).c_str()); + { + std::string timestr = Utils->Creator->TimeToStr(server_uptime); + ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\2%s\2' was established for %s", linkID.c_str(), timestr.c_str()); + } } }