diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/modules/m_spanningtree/main.cpp | 85 | ||||
-rw-r--r-- | src/modules/m_spanningtree/resolvers.cpp | 66 | ||||
-rw-r--r-- | src/modules/m_spanningtree/resolvers.h | 22 | ||||
-rw-r--r-- | src/modules/m_spanningtree/timesynctimer.cpp | 28 | ||||
-rw-r--r-- | src/modules/m_spanningtree/timesynctimer.h | 23 | ||||
-rw-r--r-- | src/modules/m_spanningtree/treesocket1.cpp (renamed from src/modules/m_spanningtree/treesocket.cpp) | 1427 | ||||
-rw-r--r-- | src/modules/m_spanningtree/treesocket2.cpp | 1448 |
7 files changed, 1589 insertions, 1510 deletions
diff --git a/src/modules/m_spanningtree/main.cpp b/src/modules/m_spanningtree/main.cpp index 83a6b4e19..0ddb6b291 100644 --- a/src/modules/m_spanningtree/main.cpp +++ b/src/modules/m_spanningtree/main.cpp @@ -25,6 +25,8 @@ #include "xline.h" #include "transport.h" +#include "m_spanningtree/timesynctimer.h" +#include "m_spanningtree/resolvers.h" #include "m_spanningtree/main.h" #include "m_spanningtree/utils.h" #include "m_spanningtree/treeserver.h" @@ -72,79 +74,6 @@ class cmd_rconnect : public command_t } }; -/** This class is used to resolve server hostnames during /connect and autoconnect. - * As of 1.1, the resolver system is seperated out from InspSocket, so we must do this - * resolver step first ourselves if we need it. This is totally nonblocking, and will - * callback to OnLookupComplete or OnError when completed. Once it has completed we - * will have an IP address which we can then use to continue our connection. - */ -class ServernameResolver : public Resolver -{ - private: - /** A copy of the Link tag info for what we're connecting to. - * We take a copy, rather than using a pointer, just in case the - * admin takes the tag away and rehashes while the domain is resolving. - */ - Link MyLink; - SpanningTreeUtilities* Utils; - public: - ServernameResolver(Module* me, SpanningTreeUtilities* Util, InspIRCd* Instance, const std::string &hostname, Link x, bool &cached) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), MyLink(x), Utils(Util) - { - /* Nothing in here, folks */ - } - - void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached) - { - /* Initiate the connection, now that we have an IP to use. - * Passing a hostname directly to InspSocket causes it to - * just bail and set its FD to -1. - */ - TreeServer* CheckDupe = Utils->FindServer(MyLink.Name.c_str()); - if (!CheckDupe) /* Check that nobody tried to connect it successfully while we were resolving */ - { - - if ((!MyLink.Hook.empty()) && (Utils->hooks.find(MyLink.Hook.c_str()) == Utils->hooks.end())) - return; - - TreeSocket* newsocket = new TreeSocket(this->Utils, ServerInstance, result,MyLink.Port,false,MyLink.Timeout ? MyLink.Timeout : 10,MyLink.Name.c_str(), - MyLink.Hook.empty() ? NULL : Utils->hooks[MyLink.Hook.c_str()]); - if (newsocket->GetFd() > -1) - { - /* We're all OK */ - } - else - { - /* Something barfed, show the opers */ - ServerInstance->SNO->WriteToSnoMask('l',"CONNECT: Error connecting \002%s\002: %s.",MyLink.Name.c_str(),strerror(errno)); - delete newsocket; - Utils->DoFailOver(&MyLink); - } - } - } - - void OnError(ResolverError e, const std::string &errormessage) - { - /* Ooops! */ - ServerInstance->SNO->WriteToSnoMask('l',"CONNECT: Error connecting \002%s\002: Unable to resolve hostname - %s",MyLink.Name.c_str(),errormessage.c_str()); - Utils->DoFailOver(&MyLink); - } -}; - -/** Create a timer which recurs every second, we inherit from InspTimer. - * InspTimer is only one-shot however, so at the end of each Tick() we simply - * insert another of ourselves into the pending queue :) - */ -class TimeSyncTimer : public InspTimer -{ - private: - InspIRCd *Instance; - ModuleSpanningTree *Module; - public: - TimeSyncTimer(InspIRCd *Instance, ModuleSpanningTree *Mod); - virtual void Tick(time_t TIME); -}; - - ModuleSpanningTree::ModuleSpanningTree(InspIRCd* Me) : Module::Module(Me), max_local(0), max_global(0) { @@ -1360,16 +1289,6 @@ Priority ModuleSpanningTree::Prioritize() return PRIORITY_LAST; } -TimeSyncTimer::TimeSyncTimer(InspIRCd *Inst, ModuleSpanningTree *Mod) : InspTimer(43200, Inst->Time(), true), Instance(Inst), Module(Mod) -{ -} - -void TimeSyncTimer::Tick(time_t TIME) -{ - Module->BroadcastTimeSync(); -} - - class ModuleSpanningTreeFactory : public ModuleFactory { public: diff --git a/src/modules/m_spanningtree/resolvers.cpp b/src/modules/m_spanningtree/resolvers.cpp new file mode 100644 index 000000000..360ed2d52 --- /dev/null +++ b/src/modules/m_spanningtree/resolvers.cpp @@ -0,0 +1,66 @@ +#include "configreader.h" +#include "users.h" +#include "channels.h" +#include "modules.h" +#include "commands/cmd_whois.h" +#include "commands/cmd_stats.h" +#include "socket.h" +#include "inspircd.h" +#include "wildcard.h" +#include "xline.h" +#include "transport.h" + +#include "m_spanningtree/resolvers.h" +#include "m_spanningtree/main.h" +#include "m_spanningtree/utils.h" +#include "m_spanningtree/treeserver.h" +#include "m_spanningtree/link.h" +#include "m_spanningtree/treesocket.h" + +/** This class is used to resolve server hostnames during /connect and autoconnect. + * As of 1.1, the resolver system is seperated out from InspSocket, so we must do this + * resolver step first ourselves if we need it. This is totally nonblocking, and will + * callback to OnLookupComplete or OnError when completed. Once it has completed we + * will have an IP address which we can then use to continue our connection. + */ +ServernameResolver::ServernameResolver(Module* me, SpanningTreeUtilities* Util, InspIRCd* Instance, const std::string &hostname, Link x, bool &cached) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), MyLink(x), Utils(Util) +{ + /* Nothing in here, folks */ +} + +void ServernameResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached) +{ + /* Initiate the connection, now that we have an IP to use. + * Passing a hostname directly to InspSocket causes it to + * just bail and set its FD to -1. + */ + TreeServer* CheckDupe = Utils->FindServer(MyLink.Name.c_str()); + if (!CheckDupe) /* Check that nobody tried to connect it successfully while we were resolving */ + { + + if ((!MyLink.Hook.empty()) && (Utils->hooks.find(MyLink.Hook.c_str()) == Utils->hooks.end())) + return; + + TreeSocket* newsocket = new TreeSocket(this->Utils, ServerInstance, result,MyLink.Port,false,MyLink.Timeout ? MyLink.Timeout : 10,MyLink.Name.c_str(), + MyLink.Hook.empty() ? NULL : Utils->hooks[MyLink.Hook.c_str()]); + if (newsocket->GetFd() > -1) + { + /* We're all OK */ + } + else + { + /* Something barfed, show the opers */ + ServerInstance->SNO->WriteToSnoMask('l',"CONNECT: Error connecting \002%s\002: %s.",MyLink.Name.c_str(),strerror(errno)); + delete newsocket; + Utils->DoFailOver(&MyLink); + } + } +} + +void ServernameResolver::OnError(ResolverError e, const std::string &errormessage) +{ + /* Ooops! */ + ServerInstance->SNO->WriteToSnoMask('l',"CONNECT: Error connecting \002%s\002: Unable to resolve hostname - %s",MyLink.Name.c_str(),errormessage.c_str()); + Utils->DoFailOver(&MyLink); +} + diff --git a/src/modules/m_spanningtree/resolvers.h b/src/modules/m_spanningtree/resolvers.h index 1998511a1..0efa8113f 100644 --- a/src/modules/m_spanningtree/resolvers.h +++ b/src/modules/m_spanningtree/resolvers.h @@ -14,6 +14,7 @@ #include "transport.h" #include "m_spanningtree/utils.h" +#include "m_spanningtree/link.h" /** Handle resolving of server IPs for the cache */ @@ -39,4 +40,25 @@ class SecurityIPResolver : public Resolver } }; +/** This class is used to resolve server hostnames during /connect and autoconnect. + * As of 1.1, the resolver system is seperated out from InspSocket, so we must do this + * resolver step first ourselves if we need it. This is totally nonblocking, and will + * callback to OnLookupComplete or OnError when completed. Once it has completed we + * will have an IP address which we can then use to continue our connection. + */ +class ServernameResolver : public Resolver +{ + private: + /** A copy of the Link tag info for what we're connecting to. + * We take a copy, rather than using a pointer, just in case the + * admin takes the tag away and rehashes while the domain is resolving. + */ + Link MyLink; + SpanningTreeUtilities* Utils; + public: + ServernameResolver(Module* me, SpanningTreeUtilities* Util, InspIRCd* Instance, const std::string &hostname, Link x, bool &cached); + void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached); + void OnError(ResolverError e, const std::string &errormessage); +}; + #endif diff --git a/src/modules/m_spanningtree/timesynctimer.cpp b/src/modules/m_spanningtree/timesynctimer.cpp new file mode 100644 index 000000000..09cf638e0 --- /dev/null +++ b/src/modules/m_spanningtree/timesynctimer.cpp @@ -0,0 +1,28 @@ +#include "configreader.h" +#include "users.h" +#include "channels.h" +#include "modules.h" +#include "commands/cmd_whois.h" +#include "commands/cmd_stats.h" +#include "socket.h" +#include "inspircd.h" +#include "wildcard.h" +#include "xline.h" +#include "transport.h" + +#include "m_spanningtree/timesynctimer.h" +#include "m_spanningtree/main.h" +#include "m_spanningtree/utils.h" +#include "m_spanningtree/treeserver.h" +#include "m_spanningtree/link.h" +#include "m_spanningtree/treesocket.h" + +TimeSyncTimer::TimeSyncTimer(InspIRCd *Inst, ModuleSpanningTree *Mod) : InspTimer(43200, Inst->Time(), true), Instance(Inst), Module(Mod) +{ +} + +void TimeSyncTimer::Tick(time_t TIME) +{ + Module->BroadcastTimeSync(); +} + diff --git a/src/modules/m_spanningtree/timesynctimer.h b/src/modules/m_spanningtree/timesynctimer.h new file mode 100644 index 000000000..0354ac99a --- /dev/null +++ b/src/modules/m_spanningtree/timesynctimer.h @@ -0,0 +1,23 @@ +#ifndef __TIMESYNC_H__ +#define __TIMESYNC_H__ + +#include "timer.h" + +class ModuleSpanningTree; +class InspIRCd; + +/** Create a timer which recurs every second, we inherit from InspTimer. + * InspTimer is only one-shot however, so at the end of each Tick() we simply + * insert another of ourselves into the pending queue :) + */ +class TimeSyncTimer : public InspTimer +{ + private: + InspIRCd *Instance; + ModuleSpanningTree *Module; + public: + TimeSyncTimer(InspIRCd *Instance, ModuleSpanningTree *Mod); + virtual void Tick(time_t TIME); +}; + +#endif diff --git a/src/modules/m_spanningtree/treesocket.cpp b/src/modules/m_spanningtree/treesocket1.cpp index d283dc1f9..1ca72eee5 100644 --- a/src/modules/m_spanningtree/treesocket.cpp +++ b/src/modules/m_spanningtree/treesocket1.cpp @@ -1449,1430 +1449,3 @@ bool TreeSocket::OnDataReady() return (data && !*data); } -int TreeSocket::WriteLine(std::string line) -{ - Instance->Log(DEBUG, "-> %s", line.c_str()); - line.append("\r\n"); - return this->Write(line); -} - -/* Handle ERROR command */ -bool TreeSocket::Error(std::deque<std::string> ¶ms) -{ - if (params.size() < 1) - return false; - this->Instance->SNO->WriteToSnoMask('l',"ERROR from %s: %s",(InboundServerName != "" ? InboundServerName.c_str() : myhost.c_str()),params[0].c_str()); - /* we will return false to cause the socket to close. */ - return false; -} - -/** remote MOTD. leet, huh? */ -bool TreeSocket::Motd(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() > 0) - { - if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0])) - { - /* It's for our server */ - string_list results; - userrec* source = this->Instance->FindNick(prefix); - - if (source) - { - std::deque<std::string> par; - par.push_back(prefix); - par.push_back(""); - - if (!Instance->Config->MOTD.size()) - { - par[1] = std::string("::")+Instance->Config->ServerName+" 422 "+source->nick+" :Message of the day file is missing."; - Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); - return true; - } - - par[1] = std::string("::")+Instance->Config->ServerName+" 375 "+source->nick+" :"+Instance->Config->ServerName+" message of the day"; - Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); - - for (unsigned int i = 0; i < Instance->Config->MOTD.size(); i++) - { - par[1] = std::string("::")+Instance->Config->ServerName+" 372 "+source->nick+" :- "+Instance->Config->MOTD[i]; - Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); - } - - par[1] = std::string("::")+Instance->Config->ServerName+" 376 "+source->nick+" End of message of the day."; - Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); - } - } - else - { - /* Pass it on */ - userrec* source = this->Instance->FindNick(prefix); - if (source) - Utils->DoOneToOne(prefix, "MOTD", params, params[0]); - } - } - return true; -} - -/** remote ADMIN. leet, huh? */ -bool TreeSocket::Admin(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() > 0) - { - if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0])) - { - /* It's for our server */ - string_list results; - userrec* source = this->Instance->FindNick(prefix); - if (source) - { - std::deque<std::string> par; - par.push_back(prefix); - par.push_back(""); - par[1] = std::string("::")+Instance->Config->ServerName+" 256 "+source->nick+" :Administrative info for "+Instance->Config->ServerName; - Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); - par[1] = std::string("::")+Instance->Config->ServerName+" 257 "+source->nick+" :Name - "+Instance->Config->AdminName; - Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); - par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :Nickname - "+Instance->Config->AdminNick; - Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); - par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :E-Mail - "+Instance->Config->AdminEmail; - Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); - } - } - else - { - /* Pass it on */ - userrec* source = this->Instance->FindNick(prefix); - if (source) - Utils->DoOneToOne(prefix, "ADMIN", params, params[0]); - } - } - return true; -} - -bool TreeSocket::Stats(const std::string &prefix, std::deque<std::string> ¶ms) -{ - /* Get the reply to a STATS query if it matches this servername, - * and send it back as a load of PUSH queries - */ - if (params.size() > 1) - { - if (this->Instance->MatchText(this->Instance->Config->ServerName, params[1])) - { - /* It's for our server */ - string_list results; - userrec* source = this->Instance->FindNick(prefix); - if (source) - { - std::deque<std::string> par; - par.push_back(prefix); - par.push_back(""); - DoStats(this->Instance, *(params[0].c_str()), source, results); - for (size_t i = 0; i < results.size(); i++) - { - par[1] = "::" + results[i]; - Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); - } - } - } - else - { - /* Pass it on */ - userrec* source = this->Instance->FindNick(prefix); - if (source) - Utils->DoOneToOne(prefix, "STATS", params, params[1]); - } - } - return true; -} - - -/** Because the core won't let users or even SERVERS set +o, - * we use the OPERTYPE command to do this. - */ -bool TreeSocket::OperType(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() != 1) - return true; - std::string opertype = params[0]; - userrec* u = this->Instance->FindNick(prefix); - if (u) - { - u->modes[UM_OPERATOR] = 1; - this->Instance->all_opers.push_back(u); - strlcpy(u->oper,opertype.c_str(),NICKMAX-1); - Utils->DoOneToAllButSender(u->nick,"OPERTYPE",params,u->server); - this->Instance->SNO->WriteToSnoMask('o',"From %s: User %s (%s@%s) is now an IRC operator of type %s",u->server, u->nick,u->ident,u->host,irc::Spacify(opertype.c_str())); - } - return true; -} - -/** Because Andy insists that services-compatible servers must - * implement SVSNICK and SVSJOIN, that's exactly what we do :p - */ -bool TreeSocket::ForceNick(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 3) - return true; - - userrec* u = this->Instance->FindNick(params[0]); - - if (u) - { - Utils->DoOneToAllButSender(prefix,"SVSNICK",params,prefix); - if (IS_LOCAL(u)) - { - std::deque<std::string> par; - par.push_back(params[1]); - if (!u->ForceNickChange(params[1].c_str())) - { - userrec::QuitUser(this->Instance, u, "Nickname collision"); - return true; - } - u->age = atoi(params[2].c_str()); - } - } - 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: - * A -> B -> C -> D: oper on A squits D, A routes to B, B routes to C, C notices D connected locally, kills it. -- w00t - */ -bool TreeSocket::RemoteSquit(const std::string &prefix, std::deque<std::string> ¶ms) -{ - /* ok.. :w00t RSQUIT jupe.barafranca.com :reason here */ - if (params.size() < 2) - return true; - - TreeServer* s = Utils->FindServerMask(params[0]); - - if (s) - { - if (s == Utils->TreeRoot) - { - this->Instance->SNO->WriteToSnoMask('l',"What the fuck, I recieved a remote SQUIT for myself? :< (from %s", prefix.c_str()); - return true; - } - - TreeSocket* sock = s->GetSocket(); - - if (sock) - { - /* it's locally connected, KILL IT! */ - Instance->SNO->WriteToSnoMask('l',"RSQUIT: Server \002%s\002 removed from network by %s: %s", params[0].c_str(), prefix.c_str(), params[1].c_str()); - sock->Squit(s,"Server quit by " + prefix + ": " + params[1]); - Instance->SE->DelFd(sock); - sock->Close(); - delete sock; - } - else - { - /* route the rsquit */ - params[1] = ":" + params[1]; - Utils->DoOneToOne(prefix, "RSQUIT", params, params[0]); - } - } - else - { - /* mother fucker! it doesn't exist */ - } - - return true; -} - -bool TreeSocket::ServiceJoin(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 2) - return true; - - userrec* u = this->Instance->FindNick(params[0]); - - if (u) - { - /* 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); - Utils->DoOneToAllButSender(prefix,"SVSJOIN",params,prefix); - } - return true; -} - -bool TreeSocket::RemoteRehash(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 1) - return false; - - std::string servermask = params[0]; - - if (this->Instance->MatchText(this->Instance->Config->ServerName,servermask)) - { - this->Instance->SNO->WriteToSnoMask('l',"Remote rehash initiated by \002"+prefix+"\002."); - this->Instance->RehashServer(); - Utils->ReadConfiguration(false); - InitializeDisabledCommands(Instance->Config->DisabledCommands, Instance); - } - Utils->DoOneToAllButSender(prefix,"REHASH",params,prefix); - return true; -} - -bool TreeSocket::RemoteKill(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() != 2) - return true; - - std::string nick = params[0]; - userrec* u = this->Instance->FindNick(prefix); - userrec* who = this->Instance->FindNick(nick); - - if (who) - { - /* Prepend kill source, if we don't have one */ - std::string sourceserv = prefix; - if (u) - { - sourceserv = u->server; - } - if (*(params[1].c_str()) != '[') - { - params[1] = "[" + sourceserv + "] Killed (" + params[1] +")"; - } - std::string reason = params[1]; - params[1] = ":" + params[1]; - Utils->DoOneToAllButSender(prefix,"KILL",params,sourceserv); - who->Write(":%s KILL %s :%s (%s)", sourceserv.c_str(), who->nick, sourceserv.c_str(), reason.c_str()); - userrec::QuitUser(this->Instance,who,reason); - } - return true; -} - -bool TreeSocket::LocalPong(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 1) - return true; - - if (params.size() == 1) - { - TreeServer* ServerSource = Utils->FindServer(prefix); - if (ServerSource) - { - ServerSource->SetPingFlag(); - } - } - else - { - std::string forwardto = params[1]; - if (forwardto == this->Instance->Config->ServerName) - { - /* - * this is a PONG for us - * if the prefix is a user, check theyre local, and if they are, - * dump the PONG reply back to their fd. If its a server, do nowt. - * Services might want to send these s->s, but we dont need to yet. - */ - userrec* u = this->Instance->FindNick(prefix); - if (u) - { - u->WriteServ("PONG %s %s",params[0].c_str(),params[1].c_str()); - } - } - else - { - // not for us, pass it on :) - Utils->DoOneToOne(prefix,"PONG",params,forwardto); - } - } - - return true; -} - -bool TreeSocket::MetaData(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 3) - return true; - TreeServer* ServerSource = Utils->FindServer(prefix); - if (ServerSource) - { - if (params[0] == "*") - { - FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_OTHER,NULL,params[1],params[2])); - } - else if (*(params[0].c_str()) == '#') - { - chanrec* c = this->Instance->FindChan(params[0]); - if (c) - { - FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_CHANNEL,c,params[1],params[2])); - } - } - else if (*(params[0].c_str()) != '#') - { - userrec* u = this->Instance->FindNick(params[0]); - if (u) - { - FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_USER,u,params[1],params[2])); - } - } - } - - params[2] = ":" + params[2]; - Utils->DoOneToAllButSender(prefix,"METADATA",params,prefix); - return true; -} - -bool TreeSocket::ServerVersion(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 1) - return true; - - TreeServer* ServerSource = Utils->FindServer(prefix); - - if (ServerSource) - { - ServerSource->SetVersion(params[0]); - } - params[0] = ":" + params[0]; - Utils->DoOneToAllButSender(prefix,"VERSION",params,prefix); - return true; -} - -bool TreeSocket::ChangeHost(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 1) - return true; - userrec* u = this->Instance->FindNick(prefix); - - if (u) - { - u->ChangeDisplayedHost(params[0].c_str()); - Utils->DoOneToAllButSender(prefix,"FHOST",params,u->server); - } - return true; -} - -bool TreeSocket::AddLine(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 6) - return true; - bool propogate = false; - if (!this->bursting) - Utils->lines_to_apply = 0; - switch (*(params[0].c_str())) - { - case 'Z': - propogate = Instance->XLines->add_zline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); - Instance->XLines->zline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); - if (propogate) - Utils->lines_to_apply |= APPLY_ZLINES; - break; - case 'Q': - propogate = Instance->XLines->add_qline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); - Instance->XLines->qline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); - if (propogate) - Utils->lines_to_apply |= APPLY_QLINES; - break; - case 'E': - propogate = Instance->XLines->add_eline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); - Instance->XLines->eline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); - break; - case 'G': - propogate = Instance->XLines->add_gline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); - Instance->XLines->gline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); - if (propogate) - Utils->lines_to_apply |= APPLY_GLINES; - break; - case 'K': - propogate = Instance->XLines->add_kline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); - if (propogate) - Utils->lines_to_apply |= APPLY_KLINES; - break; - default: - /* Just in case... */ - this->Instance->SNO->WriteToSnoMask('x',"\2WARNING\2: Invalid xline type '"+params[0]+"' sent by server "+prefix+", ignored!"); - propogate = false; - break; - } - /* Send it on its way */ - if (propogate) - { - 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()); - } - else - { - this->Instance->SNO->WriteToSnoMask('x',"%s Added permenant %cLINE on %s (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),params[5].c_str()); - } - params[5] = ":" + params[5]; - Utils->DoOneToAllButSender(prefix,"ADDLINE",params,prefix); - } - if (!this->bursting) - { - Instance->XLines->apply_lines(Utils->lines_to_apply); - Utils->lines_to_apply = 0; - } - return true; -} - -bool TreeSocket::ChangeName(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 1) - return true; - userrec* u = this->Instance->FindNick(prefix); - if (u) - { - u->ChangeName(params[0].c_str()); - params[0] = ":" + params[0]; - Utils->DoOneToAllButSender(prefix,"FNAME",params,u->server); - } - return true; -} - -bool TreeSocket::Whois(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 1) - return true; - userrec* u = this->Instance->FindNick(prefix); - if (u) - { - // an incoming request - if (params.size() == 1) - { - userrec* x = this->Instance->FindNick(params[0]); - if ((x) && (IS_LOCAL(x))) - { - userrec* x = this->Instance->FindNick(params[0]); - char signon[MAXBUF]; - char idle[MAXBUF]; - snprintf(signon,MAXBUF,"%lu",(unsigned long)x->signon); - snprintf(idle,MAXBUF,"%lu",(unsigned long)abs((x->idle_lastmsg)-Instance->Time(true))); - std::deque<std::string> par; - par.push_back(prefix); - par.push_back(signon); - par.push_back(idle); - // ours, we're done, pass it BACK - Utils->DoOneToOne(params[0],"IDLE",par,u->server); - } - else - { - // not ours pass it on - Utils->DoOneToOne(prefix,"IDLE",params,x->server); - } - } - else if (params.size() == 3) - { - std::string who_did_the_whois = params[0]; - userrec* who_to_send_to = this->Instance->FindNick(who_did_the_whois); - if ((who_to_send_to) && (IS_LOCAL(who_to_send_to))) - { - // an incoming reply to a whois we sent out - std::string nick_whoised = prefix; - unsigned long signon = atoi(params[1].c_str()); - unsigned long idle = atoi(params[2].c_str()); - if ((who_to_send_to) && (IS_LOCAL(who_to_send_to))) - do_whois(this->Instance,who_to_send_to,u,signon,idle,nick_whoised.c_str()); - } - else - { - // not ours, pass it on - Utils->DoOneToOne(prefix,"IDLE",params,who_to_send_to->server); - } - } - } - return true; -} - -bool TreeSocket::Push(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 2) - return true; - userrec* u = this->Instance->FindNick(params[0]); - if (!u) - return true; - if (IS_LOCAL(u)) - { - u->Write(params[1]); - } - else - { - // continue the raw onwards - params[1] = ":" + params[1]; - Utils->DoOneToOne(prefix,"PUSH",params,u->server); - } - return true; -} - -bool TreeSocket::HandleSetTime(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (!params.size() || !Utils->EnableTimeSync) - return true; - - bool force = false; - - 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); - - Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix); - } - else - { - Instance->Log(DEBUG, "Higher TS (diff %d) from %s overridden", us - rts, prefix.c_str()); - - std::deque<std::string> oparams; - oparams.push_back(ConvToStr(us)); - - Utils->DoOneToMany(prefix, "TIMESET", oparams); - } - - return true; -} - -bool TreeSocket::Time(const std::string &prefix, std::deque<std::string> ¶ms) -{ - // :source.server TIME remote.server sendernick - // :remote.server TIME source.server sendernick TS - if (params.size() == 2) - { - // someone querying our time? - if (this->Instance->Config->ServerName == params[0]) - { - userrec* u = this->Instance->FindNick(params[1]); - if (u) - { - params.push_back(ConvToStr(Instance->Time(false))); - params[0] = prefix; - Utils->DoOneToOne(this->Instance->Config->ServerName,"TIME",params,params[0]); - } - } - else - { - // not us, pass it on - userrec* u = this->Instance->FindNick(params[1]); - if (u) - Utils->DoOneToOne(prefix,"TIME",params,params[0]); - } - } - else if (params.size() == 3) - { - // a response to a previous TIME - userrec* u = this->Instance->FindNick(params[1]); - if ((u) && (IS_LOCAL(u))) - { - time_t rawtime = atol(params[2].c_str()); - struct tm * timeinfo; - timeinfo = localtime(&rawtime); - char tms[26]; - snprintf(tms,26,"%s",asctime(timeinfo)); - tms[24] = 0; - u->WriteServ("391 %s %s :%s",u->nick,prefix.c_str(),tms); - } - else - { - if (u) - Utils->DoOneToOne(prefix,"TIME",params,u->server); - } - } - return true; -} - -bool TreeSocket::LocalPing(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 1) - return true; - if (params.size() == 1) - { - std::string stufftobounce = params[0]; - this->WriteLine(std::string(":")+this->Instance->Config->ServerName+" PONG "+stufftobounce); - return true; - } - else - { - std::string forwardto = params[1]; - if (forwardto == this->Instance->Config->ServerName) - { - // this is a ping for us, send back PONG to the requesting server - params[1] = params[0]; - params[0] = forwardto; - Utils->DoOneToOne(forwardto,"PONG",params,params[1]); - } - else - { - // not for us, pass it on :) - Utils->DoOneToOne(prefix,"PING",params,forwardto); - } - return true; - } -} - -bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 1) - return true; - chanrec* c = Instance->FindChan(params[0]); - if (c) - { - irc::modestacker modestack(false); - CUList *ulist = c->GetUsers(); - const char* y[127]; - std::deque<std::string> stackresult; - std::string x; - for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++) - { - std::string modesequence = Instance->Modes->ModeString(i->second, c); - if (modesequence.length()) - { - irc::spacesepstream sep(modesequence); - std::string modeletters = sep.GetToken(); - while (!modeletters.empty()) - { - char mletter = *(modeletters.begin()); - modestack.Push(mletter,sep.GetToken()); - modeletters.erase(modeletters.begin()); - } - } - } - - while (modestack.GetStackedLine(stackresult)) - { - stackresult.push_front(ConvToStr(c->age)); - stackresult.push_front(c->name); - Utils->DoOneToMany(Instance->Config->ServerName, "FMODE", stackresult); - stackresult.erase(stackresult.begin() + 1); - for (size_t z = 0; z < stackresult.size(); z++) - { - y[z] = stackresult[z].c_str(); - } - userrec* n = new userrec(Instance); - n->SetFd(FD_MAGIC_NUMBER); - Instance->SendMode(y, stackresult.size(), n); - delete n; - } - } - return true; -} - -bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms) -{ - if (params.size() < 4) - return false; - std::string servername = params[0]; - std::string password = params[1]; - // hopcount is not used for a remote server, we calculate this ourselves - std::string description = params[3]; - TreeServer* ParentOfThis = Utils->FindServer(prefix); - if (!ParentOfThis) - { - this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix); - return false; - } - TreeServer* CheckDupe = Utils->FindServer(servername); - if (CheckDupe) - { - this->WriteLine("ERROR :Server "+servername+" already exists!"); - this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+servername+"\2 denied, already exists"); - return false; - } - TreeServer* Node = new TreeServer(this->Utils,this->Instance,servername,description,ParentOfThis,NULL); - ParentOfThis->AddChild(Node); - params[3] = ":" + params[3]; - Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix); - this->Instance->SNO->WriteToSnoMask('l',"Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")"); - return true; -} - -bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> ¶ms) -{ - if (params.size() < 4) - return false; - - irc::string servername = params[0].c_str(); - std::string sname = params[0]; - std::string password = params[1]; - int hops = atoi(params[2].c_str()); - - if (hops) - { - this->WriteLine("ERROR :Server too far away for authentication"); - this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication"); - return false; - } - 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)) - { - TreeServer* CheckDupe = Utils->FindServer(sname); - if (CheckDupe) - { - this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!"); - this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName()); - return false; - } - // Begin the sync here. this kickstarts the - // other side, waiting in WAIT_AUTH_2 state, - // into starting their burst, as it shows - // that we're happy. - this->LinkState = CONNECTED; - // 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); - Utils->TreeRoot->AddChild(Node); - params[3] = ":" + params[3]; - Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,sname); - this->bursting = true; - this->DoBurst(Node); - return true; - } - } - this->WriteLine("ERROR :Invalid credentials"); - this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials"); - return false; -} - -bool TreeSocket::Inbound_Server(std::deque<std::string> ¶ms) -{ - if (params.size() < 4) - return false; - irc::string servername = params[0].c_str(); - std::string sname = params[0]; - std::string password = params[1]; - int hops = atoi(params[2].c_str()); - - if (hops) - { - this->WriteLine("ERROR :Server too far away for authentication"); - this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication"); - return false; - } - 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)) - { - TreeServer* CheckDupe = Utils->FindServer(sname); - if (CheckDupe) - { - this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!"); - this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName()); - return false; - } - this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")"); - if (this->Hook) - { - std::string name = InspSocketNameRequest((Module*)Utils->Creator, this->Hook).Send(); - this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2"); - } - - this->InboundServerName = sname; - 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); - // move to the next state, we are now waiting for THEM. - this->LinkState = WAIT_AUTH_2; - return true; - } - } - this->WriteLine("ERROR :Invalid credentials"); - this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials"); - return false; -} - -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()) != "") - n.push_back(param); - return; -} - -bool TreeSocket::ProcessLine(std::string &line) -{ - std::deque<std::string> params; - irc::string command; - std::string prefix; - - line = line.substr(0, line.find_first_of("\r\n")); - - if (line.empty()) - return true; - - Instance->Log(DEBUG, "<- %s", line.c_str()); - - this->Split(line.c_str(),params); - - if ((params[0][0] == ':') && (params.size() > 1)) - { - prefix = params[0].substr(1); - params.pop_front(); - } - command = params[0].c_str(); - params.pop_front(); - switch (this->LinkState) - { - TreeServer* Node; - - case WAIT_AUTH_1: - // Waiting for SERVER command from remote server. Server initiating - // the connection sends the first SERVER command, listening server - // replies with theirs if its happy, then if the initiator is happy, - // it starts to send its net sync, which starts the merge, otherwise - // it sends an ERROR. - if (command == "PASS") - { - /* Silently ignored */ - } - else if (command == "SERVER") - { - return this->Inbound_Server(params); - } - else if (command == "ERROR") - { - return this->Error(params); - } - else if (command == "USER") - { - this->WriteLine("ERROR :Client connections to this port are prohibited."); - return false; - } - else if (command == "CAPAB") - { - return this->Capab(params); - } - else if ((command == "U") || (command == "S")) - { - this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!"); - return false; - } - else - { - std::string error("ERROR :Invalid command in negotiation phase: "); - error.append(command.c_str()); - this->WriteLine(error); - return false; - } - break; - case WAIT_AUTH_2: - // Waiting for start of other side's netmerge to say they liked our - // password. - if (command == "SERVER") - { - // cant do this, they sent it to us in the WAIT_AUTH_1 state! - // silently ignore. - return true; - } - else if ((command == "U") || (command == "S")) - { - this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!"); - return false; - } - else if (command == "BURST") - { - if (params.size() && Utils->EnableTimeSync) - { - /* If a time stamp is provided, apply synchronization */ - bool force = false; - time_t them = atoi(params[0].c_str()); - time_t us = Instance->Time(true); - int delta = them - us; - if ((params.size() == 2) && (params[1] == "FORCE")) - force = true; - if ((delta < -600) || (delta > 600)) - { - 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!"); - return false; - } - - if (force || (us > them)) - { - this->Instance->SetTimeDelta(them - us); - // 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); - Utils->TreeRoot->AddChild(Node); - params.clear(); - params.push_back(InboundServerName); - params.push_back("*"); - params.push_back("1"); - params.push_back(":"+InboundDescription); - Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,InboundServerName); - this->bursting = true; - this->DoBurst(Node); - } - else if (command == "ERROR") - { - return this->Error(params); - } - else if (command == "CAPAB") - { - return this->Capab(params); - } - - break; - case LISTENER: - this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!"); - return false; - break; - case CONNECTING: - if (command == "SERVER") - { - // another server we connected to, which was in WAIT_AUTH_1 state, - // has just sent us their credentials. If we get this far, theyre - // happy with OUR credentials, and they are now in WAIT_AUTH_2 state. - // if we're happy with this, we should send our netburst which - // kickstarts the merge. - return this->Outbound_Reply_Server(params); - } - else if (command == "ERROR") - { - return this->Error(params); - } - break; - case CONNECTED: - // This is the 'authenticated' state, when all passwords - // have been exchanged and anything past this point is taken - // as gospel. - - if (prefix != "") - { - std::string direction = prefix; - userrec* t = this->Instance->FindNick(prefix); - if (t) - { - direction = t->server; - } - TreeServer* route_back_again = Utils->BestRouteTo(direction); - if ((!route_back_again) || (route_back_again->GetSocket() != this)) - { - if (route_back_again) - Instance->Log(DEBUG,"Protocol violation: Fake direction in command '%s' from connection '%s'",line.c_str(),this->GetName().c_str()); - return true; - } - /* Fix by brain: - * When there is activity on the socket, reset the ping counter so - * that we're not wasting bandwidth pinging an active server. - */ - route_back_again->SetNextPingTime(time(NULL) + 60); - route_back_again->SetPingFlag(); - } - - if (command == "SVSMODE") - { - /* Services expects us to implement - * SVSMODE. In inspircd its the same as - * MODE anyway. - */ - command = "MODE"; - } - std::string target = ""; - /* Yes, know, this is a mess. Its reasonably fast though as we're - * working with std::string here. - */ - if ((command == "NICK") && (params.size() > 1)) - { - return this->IntroduceClient(prefix,params); - } - else if (command == "FJOIN") - { - return this->ForceJoin(prefix,params); - } - else if (command == "STATS") - { - return this->Stats(prefix, params); - } - else if (command == "MOTD") - { - return this->Motd(prefix, params); - } - else if (command == "ADMIN") - { - return this->Admin(prefix, params); - } - else if (command == "SERVER") - { - return this->RemoteServer(prefix,params); - } - else if (command == "ERROR") - { - return this->Error(params); - } - else if (command == "OPERTYPE") - { - return this->OperType(prefix,params); - } - else if (command == "FMODE") - { - return this->ForceMode(prefix,params); - } - else if (command == "KILL") - { - return this->RemoteKill(prefix,params); - } - else if (command == "FTOPIC") - { - return this->ForceTopic(prefix,params); - } - else if (command == "REHASH") - { - return this->RemoteRehash(prefix,params); - } - else if (command == "METADATA") - { - return this->MetaData(prefix,params); - } - else if (command == "REMSTATUS") - { - return this->RemoveStatus(prefix,params); - } - else if (command == "PING") - { - /* - * We just got a ping from a server that's bursting. - * This can't be right, so set them to not bursting, and - * apply their lines. - */ - if (this->bursting) - { - this->bursting = false; - Instance->XLines->apply_lines(Utils->lines_to_apply); - Utils->lines_to_apply = 0; - } - if (prefix == "") - { - prefix = this->GetName(); - } - return this->LocalPing(prefix,params); - } - else if (command == "PONG") - { - /* - * We just got a pong from a server that's bursting. - * This can't be right, so set them to not bursting, and - * apply their lines. - */ - if (this->bursting) - { - this->bursting = false; - Instance->XLines->apply_lines(Utils->lines_to_apply); - Utils->lines_to_apply = 0; - } - if (prefix == "") - { - prefix = this->GetName(); - } - return this->LocalPong(prefix,params); - } - else if (command == "VERSION") - { - return this->ServerVersion(prefix,params); - } - else if (command == "FHOST") - { - return this->ChangeHost(prefix,params); - } - else if (command == "FNAME") - { - return this->ChangeName(prefix,params); - } - else if (command == "ADDLINE") - { - return this->AddLine(prefix,params); - } - else if (command == "SVSNICK") - { - if (prefix == "") - { - prefix = this->GetName(); - } - return this->ForceNick(prefix,params); - } - else if (command == "RSQUIT") - { - return this->RemoteSquit(prefix, params); - } - else if (command == "IDLE") - { - return this->Whois(prefix,params); - } - else if (command == "PUSH") - { - return this->Push(prefix,params); - } - else if (command == "TIMESET") - { - return this->HandleSetTime(prefix, params); - } - else if (command == "TIME") - { - return this->Time(prefix,params); - } - else if ((command == "KICK") && (Utils->IsServer(prefix))) - { - std::string sourceserv = this->myhost; - if (params.size() == 3) - { - userrec* user = this->Instance->FindNick(params[1]); - chanrec* chan = this->Instance->FindChan(params[0]); - if (user && chan) - { - if (!chan->ServerKickUser(user, params[2].c_str(), false)) - /* Yikes, the channels gone! */ - delete chan; - } - } - if (this->InboundServerName != "") - { - sourceserv = this->InboundServerName; - } - return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); - } - else if (command == "SVSJOIN") - { - if (prefix == "") - { - prefix = this->GetName(); - } - return this->ServiceJoin(prefix,params); - } - else if (command == "SQUIT") - { - if (params.size() == 2) - { - this->Squit(Utils->FindServer(params[0]),params[1]); - } - return true; - } - else if (command == "OPERNOTICE") - { - std::string sourceserv = this->myhost; - if (this->InboundServerName != "") - sourceserv = this->InboundServerName; - if (params.size() >= 1) - Instance->WriteOpers("*** From " + sourceserv + ": " + params[0]); - return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); - } - else if (command == "MODENOTICE") - { - std::string sourceserv = this->myhost; - if (this->InboundServerName != "") - sourceserv = this->InboundServerName; - if (params.size() >= 2) - { - Instance->WriteMode(params[0].c_str(), WM_AND, "*** From %s: %s", sourceserv.c_str(), params[1].c_str()); - } - return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); - } - else if (command == "SNONOTICE") - { - std::string sourceserv = this->myhost; - if (this->InboundServerName != "") - sourceserv = this->InboundServerName; - if (params.size() >= 2) - { - Instance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + sourceserv + ": "+ params[1]); - } - return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); - } - else if (command == "ENDBURST") - { - this->bursting = false; - Instance->XLines->apply_lines(Utils->lines_to_apply); - Utils->lines_to_apply = 0; - std::string sourceserv = this->myhost; - if (this->InboundServerName != "") - { - sourceserv = this->InboundServerName; - } - this->Instance->SNO->WriteToSnoMask('l',"Received end of netburst from \2%s\2",sourceserv.c_str()); - - Event rmode((char*)sourceserv.c_str(), (Module*)Utils->Creator, "new_server"); - rmode.Send(Instance); - - return true; - } - else - { - // not a special inter-server command. - // Emulate the actual user doing the command, - // this saves us having a huge ugly parser. - userrec* who = this->Instance->FindNick(prefix); - std::string sourceserv = this->myhost; - if (this->InboundServerName != "") - { - sourceserv = this->InboundServerName; - } - if ((!who) && (command == "MODE")) - { - if (Utils->IsServer(prefix)) - { - const char* modelist[127]; - for (size_t i = 0; i < params.size(); i++) - modelist[i] = params[i].c_str(); - userrec* fake = new userrec(Instance); - fake->SetFd(FD_MAGIC_NUMBER); - this->Instance->SendMode(modelist, params.size(), fake); - - delete fake; - /* Hot potato! pass it on! */ - return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); - } - } - if (who) - { - if ((command == "NICK") && (params.size() > 0)) - { - /* On nick messages, check that the nick doesnt - * already exist here. If it does, kill their copy, - * and our copy. - */ - userrec* x = this->Instance->FindNick(params[0]); - if ((x) && (x != who)) - { - std::deque<std::string> p; - p.push_back(params[0]); - p.push_back("Nickname collision ("+prefix+" -> "+params[0]+")"); - Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p); - p.clear(); - p.push_back(prefix); - p.push_back("Nickname collision"); - Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p); - userrec::QuitUser(this->Instance,x,"Nickname collision ("+prefix+" -> "+params[0]+")"); - userrec* y = this->Instance->FindNick(prefix); - if (y) - { - userrec::QuitUser(this->Instance,y,"Nickname collision"); - } - return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); - } - } - // its a user - target = who->server; - const char* strparams[127]; - for (unsigned int q = 0; q < params.size(); q++) - { - strparams[q] = params[q].c_str(); - } - switch (this->Instance->CallCommandHandler(command.c_str(), strparams, params.size(), who)) - { - case CMD_INVALID: - this->WriteLine("ERROR :Unrecognised command '"+std::string(command.c_str())+"' -- possibly loaded mismatched modules"); - return false; - break; - case CMD_FAILURE: - return true; - break; - default: - /* CMD_SUCCESS and CMD_USER_DELETED fall through here */ - break; - } - } - else - { - // its not a user. Its either a server, or somethings screwed up. - if (Utils->IsServer(prefix)) - target = this->Instance->Config->ServerName; - else - return true; - } - return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); - - } - return true; - break; - } - return true; -} - -std::string TreeSocket::GetName() -{ - std::string sourceserv = this->myhost; - if (this->InboundServerName != "") - { - sourceserv = this->InboundServerName; - } - return sourceserv; -} - -void TreeSocket::OnTimeout() -{ - if (this->LinkState == CONNECTING) - { - this->Instance->SNO->WriteToSnoMask('l',"CONNECT: Connection to \002"+myhost+"\002 timed out."); - Link* MyLink = Utils->FindLink(myhost); - if (MyLink) - Utils->DoFailOver(MyLink); - } -} - -void TreeSocket::OnClose() -{ - // 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 != "") - { - quitserver = this->InboundServerName; - } - TreeServer* s = Utils->FindServer(quitserver); - if (s) - { - Squit(s,"Remote host closed the connection"); - } - - if (quitserver != "") - this->Instance->SNO->WriteToSnoMask('l',"Connection to '\2%s\2' failed.",quitserver.c_str()); -} - -int TreeSocket::OnIncomingConnection(int newsock, char* ip) -{ - /* To prevent anyone from attempting to flood opers/DDoS by connecting to the server port, - * or discovering if this port is the server port, we don't allow connections from any - * IPs for which we don't have a link block. - */ - bool found = false; - - found = (std::find(Utils->ValidIPs.begin(), Utils->ValidIPs.end(), ip) != Utils->ValidIPs.end()); - if (!found) - { - for (vector<std::string>::iterator i = Utils->ValidIPs.begin(); i != Utils->ValidIPs.end(); i++) - if (irc::sockets::MatchCIDR(ip, (*i).c_str())) - found = true; - - if (!found) - { - this->Instance->SNO->WriteToSnoMask('l',"Server connection from %s denied (no link blocks with that IP address)", ip); - close(newsock); - return false; - } - } - - TreeSocket* s = new TreeSocket(this->Utils, this->Instance, newsock, ip, this->Hook); - s = s; /* Whinge whinge whinge, thats all GCC ever does. */ - return true; -} diff --git a/src/modules/m_spanningtree/treesocket2.cpp b/src/modules/m_spanningtree/treesocket2.cpp new file mode 100644 index 000000000..eefffe1f9 --- /dev/null +++ b/src/modules/m_spanningtree/treesocket2.cpp @@ -0,0 +1,1448 @@ +#include "configreader.h" +#include "users.h" +#include "channels.h" +#include "modules.h" +#include "commands/cmd_whois.h" +#include "commands/cmd_stats.h" +#include "socket.h" +#include "inspircd.h" +#include "wildcard.h" +#include "xline.h" +#include "transport.h" +#include "socketengine.h" + +#include "m_spanningtree/main.h" +#include "m_spanningtree/utils.h" +#include "m_spanningtree/treeserver.h" +#include "m_spanningtree/link.h" +#include "m_spanningtree/treesocket.h" +#include "m_spanningtree/resolvers.h" +#include "m_spanningtree/handshaketimer.h" + +int TreeSocket::WriteLine(std::string line) +{ + Instance->Log(DEBUG, "-> %s", line.c_str()); + line.append("\r\n"); + return this->Write(line); +} + +/* Handle ERROR command */ +bool TreeSocket::Error(std::deque<std::string> ¶ms) +{ + if (params.size() < 1) + return false; + this->Instance->SNO->WriteToSnoMask('l',"ERROR from %s: %s",(InboundServerName != "" ? InboundServerName.c_str() : myhost.c_str()),params[0].c_str()); + /* we will return false to cause the socket to close. */ + return false; +} + +/** remote MOTD. leet, huh? */ +bool TreeSocket::Motd(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() > 0) + { + if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0])) + { + /* It's for our server */ + string_list results; + userrec* source = this->Instance->FindNick(prefix); + + if (source) + { + std::deque<std::string> par; + par.push_back(prefix); + par.push_back(""); + + if (!Instance->Config->MOTD.size()) + { + par[1] = std::string("::")+Instance->Config->ServerName+" 422 "+source->nick+" :Message of the day file is missing."; + Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); + return true; + } + + par[1] = std::string("::")+Instance->Config->ServerName+" 375 "+source->nick+" :"+Instance->Config->ServerName+" message of the day"; + Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); + + for (unsigned int i = 0; i < Instance->Config->MOTD.size(); i++) + { + par[1] = std::string("::")+Instance->Config->ServerName+" 372 "+source->nick+" :- "+Instance->Config->MOTD[i]; + Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); + } + + par[1] = std::string("::")+Instance->Config->ServerName+" 376 "+source->nick+" End of message of the day."; + Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); + } + } + else + { + /* Pass it on */ + userrec* source = this->Instance->FindNick(prefix); + if (source) + Utils->DoOneToOne(prefix, "MOTD", params, params[0]); + } + } + return true; +} + +/** remote ADMIN. leet, huh? */ +bool TreeSocket::Admin(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() > 0) + { + if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0])) + { + /* It's for our server */ + string_list results; + userrec* source = this->Instance->FindNick(prefix); + if (source) + { + std::deque<std::string> par; + par.push_back(prefix); + par.push_back(""); + par[1] = std::string("::")+Instance->Config->ServerName+" 256 "+source->nick+" :Administrative info for "+Instance->Config->ServerName; + Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); + par[1] = std::string("::")+Instance->Config->ServerName+" 257 "+source->nick+" :Name - "+Instance->Config->AdminName; + Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); + par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :Nickname - "+Instance->Config->AdminNick; + Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); + par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :E-Mail - "+Instance->Config->AdminEmail; + Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); + } + } + else + { + /* Pass it on */ + userrec* source = this->Instance->FindNick(prefix); + if (source) + Utils->DoOneToOne(prefix, "ADMIN", params, params[0]); + } + } + return true; +} + +bool TreeSocket::Stats(const std::string &prefix, std::deque<std::string> ¶ms) +{ + /* Get the reply to a STATS query if it matches this servername, + * and send it back as a load of PUSH queries + */ + if (params.size() > 1) + { + if (this->Instance->MatchText(this->Instance->Config->ServerName, params[1])) + { + /* It's for our server */ + string_list results; + userrec* source = this->Instance->FindNick(prefix); + if (source) + { + std::deque<std::string> par; + par.push_back(prefix); + par.push_back(""); + DoStats(this->Instance, *(params[0].c_str()), source, results); + for (size_t i = 0; i < results.size(); i++) + { + par[1] = "::" + results[i]; + Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server); + } + } + } + else + { + /* Pass it on */ + userrec* source = this->Instance->FindNick(prefix); + if (source) + Utils->DoOneToOne(prefix, "STATS", params, params[1]); + } + } + return true; +} + + +/** Because the core won't let users or even SERVERS set +o, + * we use the OPERTYPE command to do this. + */ +bool TreeSocket::OperType(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() != 1) + return true; + std::string opertype = params[0]; + userrec* u = this->Instance->FindNick(prefix); + if (u) + { + u->modes[UM_OPERATOR] = 1; + this->Instance->all_opers.push_back(u); + strlcpy(u->oper,opertype.c_str(),NICKMAX-1); + Utils->DoOneToAllButSender(u->nick,"OPERTYPE",params,u->server); + this->Instance->SNO->WriteToSnoMask('o',"From %s: User %s (%s@%s) is now an IRC operator of type %s",u->server, u->nick,u->ident,u->host,irc::Spacify(opertype.c_str())); + } + return true; +} + +/** Because Andy insists that services-compatible servers must + * implement SVSNICK and SVSJOIN, that's exactly what we do :p + */ +bool TreeSocket::ForceNick(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 3) + return true; + + userrec* u = this->Instance->FindNick(params[0]); + + if (u) + { + Utils->DoOneToAllButSender(prefix,"SVSNICK",params,prefix); + if (IS_LOCAL(u)) + { + std::deque<std::string> par; + par.push_back(params[1]); + if (!u->ForceNickChange(params[1].c_str())) + { + userrec::QuitUser(this->Instance, u, "Nickname collision"); + return true; + } + u->age = atoi(params[2].c_str()); + } + } + 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: + * A -> B -> C -> D: oper on A squits D, A routes to B, B routes to C, C notices D connected locally, kills it. -- w00t + */ +bool TreeSocket::RemoteSquit(const std::string &prefix, std::deque<std::string> ¶ms) +{ + /* ok.. :w00t RSQUIT jupe.barafranca.com :reason here */ + if (params.size() < 2) + return true; + + TreeServer* s = Utils->FindServerMask(params[0]); + + if (s) + { + if (s == Utils->TreeRoot) + { + this->Instance->SNO->WriteToSnoMask('l',"What the fuck, I recieved a remote SQUIT for myself? :< (from %s", prefix.c_str()); + return true; + } + + TreeSocket* sock = s->GetSocket(); + + if (sock) + { + /* it's locally connected, KILL IT! */ + Instance->SNO->WriteToSnoMask('l',"RSQUIT: Server \002%s\002 removed from network by %s: %s", params[0].c_str(), prefix.c_str(), params[1].c_str()); + sock->Squit(s,"Server quit by " + prefix + ": " + params[1]); + Instance->SE->DelFd(sock); + sock->Close(); + delete sock; + } + else + { + /* route the rsquit */ + params[1] = ":" + params[1]; + Utils->DoOneToOne(prefix, "RSQUIT", params, params[0]); + } + } + else + { + /* mother fucker! it doesn't exist */ + } + + return true; +} + +bool TreeSocket::ServiceJoin(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 2) + return true; + + userrec* u = this->Instance->FindNick(params[0]); + + if (u) + { + /* 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); + Utils->DoOneToAllButSender(prefix,"SVSJOIN",params,prefix); + } + return true; +} + +bool TreeSocket::RemoteRehash(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 1) + return false; + + std::string servermask = params[0]; + + if (this->Instance->MatchText(this->Instance->Config->ServerName,servermask)) + { + this->Instance->SNO->WriteToSnoMask('l',"Remote rehash initiated by \002"+prefix+"\002."); + this->Instance->RehashServer(); + Utils->ReadConfiguration(false); + InitializeDisabledCommands(Instance->Config->DisabledCommands, Instance); + } + Utils->DoOneToAllButSender(prefix,"REHASH",params,prefix); + return true; +} + +bool TreeSocket::RemoteKill(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() != 2) + return true; + + std::string nick = params[0]; + userrec* u = this->Instance->FindNick(prefix); + userrec* who = this->Instance->FindNick(nick); + + if (who) + { + /* Prepend kill source, if we don't have one */ + std::string sourceserv = prefix; + if (u) + { + sourceserv = u->server; + } + if (*(params[1].c_str()) != '[') + { + params[1] = "[" + sourceserv + "] Killed (" + params[1] +")"; + } + std::string reason = params[1]; + params[1] = ":" + params[1]; + Utils->DoOneToAllButSender(prefix,"KILL",params,sourceserv); + who->Write(":%s KILL %s :%s (%s)", sourceserv.c_str(), who->nick, sourceserv.c_str(), reason.c_str()); + userrec::QuitUser(this->Instance,who,reason); + } + return true; +} + +bool TreeSocket::LocalPong(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 1) + return true; + + if (params.size() == 1) + { + TreeServer* ServerSource = Utils->FindServer(prefix); + if (ServerSource) + { + ServerSource->SetPingFlag(); + } + } + else + { + std::string forwardto = params[1]; + if (forwardto == this->Instance->Config->ServerName) + { + /* + * this is a PONG for us + * if the prefix is a user, check theyre local, and if they are, + * dump the PONG reply back to their fd. If its a server, do nowt. + * Services might want to send these s->s, but we dont need to yet. + */ + userrec* u = this->Instance->FindNick(prefix); + if (u) + { + u->WriteServ("PONG %s %s",params[0].c_str(),params[1].c_str()); + } + } + else + { + // not for us, pass it on :) + Utils->DoOneToOne(prefix,"PONG",params,forwardto); + } + } + + return true; +} + +bool TreeSocket::MetaData(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 3) + return true; + TreeServer* ServerSource = Utils->FindServer(prefix); + if (ServerSource) + { + if (params[0] == "*") + { + FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_OTHER,NULL,params[1],params[2])); + } + else if (*(params[0].c_str()) == '#') + { + chanrec* c = this->Instance->FindChan(params[0]); + if (c) + { + FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_CHANNEL,c,params[1],params[2])); + } + } + else if (*(params[0].c_str()) != '#') + { + userrec* u = this->Instance->FindNick(params[0]); + if (u) + { + FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_USER,u,params[1],params[2])); + } + } + } + + params[2] = ":" + params[2]; + Utils->DoOneToAllButSender(prefix,"METADATA",params,prefix); + return true; +} + +bool TreeSocket::ServerVersion(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 1) + return true; + + TreeServer* ServerSource = Utils->FindServer(prefix); + + if (ServerSource) + { + ServerSource->SetVersion(params[0]); + } + params[0] = ":" + params[0]; + Utils->DoOneToAllButSender(prefix,"VERSION",params,prefix); + return true; +} + +bool TreeSocket::ChangeHost(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 1) + return true; + userrec* u = this->Instance->FindNick(prefix); + + if (u) + { + u->ChangeDisplayedHost(params[0].c_str()); + Utils->DoOneToAllButSender(prefix,"FHOST",params,u->server); + } + return true; +} + +bool TreeSocket::AddLine(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 6) + return true; + bool propogate = false; + if (!this->bursting) + Utils->lines_to_apply = 0; + switch (*(params[0].c_str())) + { + case 'Z': + propogate = Instance->XLines->add_zline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); + Instance->XLines->zline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); + if (propogate) + Utils->lines_to_apply |= APPLY_ZLINES; + break; + case 'Q': + propogate = Instance->XLines->add_qline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); + Instance->XLines->qline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); + if (propogate) + Utils->lines_to_apply |= APPLY_QLINES; + break; + case 'E': + propogate = Instance->XLines->add_eline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); + Instance->XLines->eline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); + break; + case 'G': + propogate = Instance->XLines->add_gline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); + Instance->XLines->gline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); + if (propogate) + Utils->lines_to_apply |= APPLY_GLINES; + break; + case 'K': + propogate = Instance->XLines->add_kline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); + if (propogate) + Utils->lines_to_apply |= APPLY_KLINES; + break; + default: + /* Just in case... */ + this->Instance->SNO->WriteToSnoMask('x',"\2WARNING\2: Invalid xline type '"+params[0]+"' sent by server "+prefix+", ignored!"); + propogate = false; + break; + } + /* Send it on its way */ + if (propogate) + { + 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()); + } + else + { + this->Instance->SNO->WriteToSnoMask('x',"%s Added permenant %cLINE on %s (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),params[5].c_str()); + } + params[5] = ":" + params[5]; + Utils->DoOneToAllButSender(prefix,"ADDLINE",params,prefix); + } + if (!this->bursting) + { + Instance->XLines->apply_lines(Utils->lines_to_apply); + Utils->lines_to_apply = 0; + } + return true; +} + +bool TreeSocket::ChangeName(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 1) + return true; + userrec* u = this->Instance->FindNick(prefix); + if (u) + { + u->ChangeName(params[0].c_str()); + params[0] = ":" + params[0]; + Utils->DoOneToAllButSender(prefix,"FNAME",params,u->server); + } + return true; +} + +bool TreeSocket::Whois(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 1) + return true; + userrec* u = this->Instance->FindNick(prefix); + if (u) + { + // an incoming request + if (params.size() == 1) + { + userrec* x = this->Instance->FindNick(params[0]); + if ((x) && (IS_LOCAL(x))) + { + userrec* x = this->Instance->FindNick(params[0]); + char signon[MAXBUF]; + char idle[MAXBUF]; + snprintf(signon,MAXBUF,"%lu",(unsigned long)x->signon); + snprintf(idle,MAXBUF,"%lu",(unsigned long)abs((x->idle_lastmsg)-Instance->Time(true))); + std::deque<std::string> par; + par.push_back(prefix); + par.push_back(signon); + par.push_back(idle); + // ours, we're done, pass it BACK + Utils->DoOneToOne(params[0],"IDLE",par,u->server); + } + else + { + // not ours pass it on + Utils->DoOneToOne(prefix,"IDLE",params,x->server); + } + } + else if (params.size() == 3) + { + std::string who_did_the_whois = params[0]; + userrec* who_to_send_to = this->Instance->FindNick(who_did_the_whois); + if ((who_to_send_to) && (IS_LOCAL(who_to_send_to))) + { + // an incoming reply to a whois we sent out + std::string nick_whoised = prefix; + unsigned long signon = atoi(params[1].c_str()); + unsigned long idle = atoi(params[2].c_str()); + if ((who_to_send_to) && (IS_LOCAL(who_to_send_to))) + do_whois(this->Instance,who_to_send_to,u,signon,idle,nick_whoised.c_str()); + } + else + { + // not ours, pass it on + Utils->DoOneToOne(prefix,"IDLE",params,who_to_send_to->server); + } + } + } + return true; +} + +bool TreeSocket::Push(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 2) + return true; + userrec* u = this->Instance->FindNick(params[0]); + if (!u) + return true; + if (IS_LOCAL(u)) + { + u->Write(params[1]); + } + else + { + // continue the raw onwards + params[1] = ":" + params[1]; + Utils->DoOneToOne(prefix,"PUSH",params,u->server); + } + return true; +} + +bool TreeSocket::HandleSetTime(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (!params.size() || !Utils->EnableTimeSync) + return true; + + bool force = false; + + 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); + + Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix); + } + else + { + Instance->Log(DEBUG, "Higher TS (diff %d) from %s overridden", us - rts, prefix.c_str()); + + std::deque<std::string> oparams; + oparams.push_back(ConvToStr(us)); + + Utils->DoOneToMany(prefix, "TIMESET", oparams); + } + + return true; +} + +bool TreeSocket::Time(const std::string &prefix, std::deque<std::string> ¶ms) +{ + // :source.server TIME remote.server sendernick + // :remote.server TIME source.server sendernick TS + if (params.size() == 2) + { + // someone querying our time? + if (this->Instance->Config->ServerName == params[0]) + { + userrec* u = this->Instance->FindNick(params[1]); + if (u) + { + params.push_back(ConvToStr(Instance->Time(false))); + params[0] = prefix; + Utils->DoOneToOne(this->Instance->Config->ServerName,"TIME",params,params[0]); + } + } + else + { + // not us, pass it on + userrec* u = this->Instance->FindNick(params[1]); + if (u) + Utils->DoOneToOne(prefix,"TIME",params,params[0]); + } + } + else if (params.size() == 3) + { + // a response to a previous TIME + userrec* u = this->Instance->FindNick(params[1]); + if ((u) && (IS_LOCAL(u))) + { + time_t rawtime = atol(params[2].c_str()); + struct tm * timeinfo; + timeinfo = localtime(&rawtime); + char tms[26]; + snprintf(tms,26,"%s",asctime(timeinfo)); + tms[24] = 0; + u->WriteServ("391 %s %s :%s",u->nick,prefix.c_str(),tms); + } + else + { + if (u) + Utils->DoOneToOne(prefix,"TIME",params,u->server); + } + } + return true; +} + +bool TreeSocket::LocalPing(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 1) + return true; + if (params.size() == 1) + { + std::string stufftobounce = params[0]; + this->WriteLine(std::string(":")+this->Instance->Config->ServerName+" PONG "+stufftobounce); + return true; + } + else + { + std::string forwardto = params[1]; + if (forwardto == this->Instance->Config->ServerName) + { + // this is a ping for us, send back PONG to the requesting server + params[1] = params[0]; + params[0] = forwardto; + Utils->DoOneToOne(forwardto,"PONG",params,params[1]); + } + else + { + // not for us, pass it on :) + Utils->DoOneToOne(prefix,"PING",params,forwardto); + } + return true; + } +} + +bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 1) + return true; + chanrec* c = Instance->FindChan(params[0]); + if (c) + { + irc::modestacker modestack(false); + CUList *ulist = c->GetUsers(); + const char* y[127]; + std::deque<std::string> stackresult; + std::string x; + for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++) + { + std::string modesequence = Instance->Modes->ModeString(i->second, c); + if (modesequence.length()) + { + irc::spacesepstream sep(modesequence); + std::string modeletters = sep.GetToken(); + while (!modeletters.empty()) + { + char mletter = *(modeletters.begin()); + modestack.Push(mletter,sep.GetToken()); + modeletters.erase(modeletters.begin()); + } + } + } + + while (modestack.GetStackedLine(stackresult)) + { + stackresult.push_front(ConvToStr(c->age)); + stackresult.push_front(c->name); + Utils->DoOneToMany(Instance->Config->ServerName, "FMODE", stackresult); + stackresult.erase(stackresult.begin() + 1); + for (size_t z = 0; z < stackresult.size(); z++) + { + y[z] = stackresult[z].c_str(); + } + userrec* n = new userrec(Instance); + n->SetFd(FD_MAGIC_NUMBER); + Instance->SendMode(y, stackresult.size(), n); + delete n; + } + } + return true; +} + +bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms) +{ + if (params.size() < 4) + return false; + std::string servername = params[0]; + std::string password = params[1]; + // hopcount is not used for a remote server, we calculate this ourselves + std::string description = params[3]; + TreeServer* ParentOfThis = Utils->FindServer(prefix); + if (!ParentOfThis) + { + this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix); + return false; + } + TreeServer* CheckDupe = Utils->FindServer(servername); + if (CheckDupe) + { + this->WriteLine("ERROR :Server "+servername+" already exists!"); + this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+servername+"\2 denied, already exists"); + return false; + } + TreeServer* Node = new TreeServer(this->Utils,this->Instance,servername,description,ParentOfThis,NULL); + ParentOfThis->AddChild(Node); + params[3] = ":" + params[3]; + Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix); + this->Instance->SNO->WriteToSnoMask('l',"Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")"); + return true; +} + +bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> ¶ms) +{ + if (params.size() < 4) + return false; + + irc::string servername = params[0].c_str(); + std::string sname = params[0]; + std::string password = params[1]; + int hops = atoi(params[2].c_str()); + + if (hops) + { + this->WriteLine("ERROR :Server too far away for authentication"); + this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication"); + return false; + } + 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)) + { + TreeServer* CheckDupe = Utils->FindServer(sname); + if (CheckDupe) + { + this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!"); + this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName()); + return false; + } + // Begin the sync here. this kickstarts the + // other side, waiting in WAIT_AUTH_2 state, + // into starting their burst, as it shows + // that we're happy. + this->LinkState = CONNECTED; + // 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); + Utils->TreeRoot->AddChild(Node); + params[3] = ":" + params[3]; + Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,sname); + this->bursting = true; + this->DoBurst(Node); + return true; + } + } + this->WriteLine("ERROR :Invalid credentials"); + this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials"); + return false; +} + +bool TreeSocket::Inbound_Server(std::deque<std::string> ¶ms) +{ + if (params.size() < 4) + return false; + irc::string servername = params[0].c_str(); + std::string sname = params[0]; + std::string password = params[1]; + int hops = atoi(params[2].c_str()); + + if (hops) + { + this->WriteLine("ERROR :Server too far away for authentication"); + this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication"); + return false; + } + 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)) + { + TreeServer* CheckDupe = Utils->FindServer(sname); + if (CheckDupe) + { + this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!"); + this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName()); + return false; + } + this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")"); + if (this->Hook) + { + std::string name = InspSocketNameRequest((Module*)Utils->Creator, this->Hook).Send(); + this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2"); + } + + this->InboundServerName = sname; + 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); + // move to the next state, we are now waiting for THEM. + this->LinkState = WAIT_AUTH_2; + return true; + } + } + this->WriteLine("ERROR :Invalid credentials"); + this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials"); + return false; +} + +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()) != "") + n.push_back(param); + return; +} + +bool TreeSocket::ProcessLine(std::string &line) +{ + std::deque<std::string> params; + irc::string command; + std::string prefix; + + line = line.substr(0, line.find_first_of("\r\n")); + + if (line.empty()) + return true; + + Instance->Log(DEBUG, "<- %s", line.c_str()); + + this->Split(line.c_str(),params); + + if ((params[0][0] == ':') && (params.size() > 1)) + { + prefix = params[0].substr(1); + params.pop_front(); + } + command = params[0].c_str(); + params.pop_front(); + switch (this->LinkState) + { + TreeServer* Node; + + case WAIT_AUTH_1: + // Waiting for SERVER command from remote server. Server initiating + // the connection sends the first SERVER command, listening server + // replies with theirs if its happy, then if the initiator is happy, + // it starts to send its net sync, which starts the merge, otherwise + // it sends an ERROR. + if (command == "PASS") + { + /* Silently ignored */ + } + else if (command == "SERVER") + { + return this->Inbound_Server(params); + } + else if (command == "ERROR") + { + return this->Error(params); + } + else if (command == "USER") + { + this->WriteLine("ERROR :Client connections to this port are prohibited."); + return false; + } + else if (command == "CAPAB") + { + return this->Capab(params); + } + else if ((command == "U") || (command == "S")) + { + this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!"); + return false; + } + else + { + std::string error("ERROR :Invalid command in negotiation phase: "); + error.append(command.c_str()); + this->WriteLine(error); + return false; + } + break; + case WAIT_AUTH_2: + // Waiting for start of other side's netmerge to say they liked our + // password. + if (command == "SERVER") + { + // cant do this, they sent it to us in the WAIT_AUTH_1 state! + // silently ignore. + return true; + } + else if ((command == "U") || (command == "S")) + { + this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!"); + return false; + } + else if (command == "BURST") + { + if (params.size() && Utils->EnableTimeSync) + { + /* If a time stamp is provided, apply synchronization */ + bool force = false; + time_t them = atoi(params[0].c_str()); + time_t us = Instance->Time(true); + int delta = them - us; + if ((params.size() == 2) && (params[1] == "FORCE")) + force = true; + if ((delta < -600) || (delta > 600)) + { + 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!"); + return false; + } + + if (force || (us > them)) + { + this->Instance->SetTimeDelta(them - us); + // 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); + Utils->TreeRoot->AddChild(Node); + params.clear(); + params.push_back(InboundServerName); + params.push_back("*"); + params.push_back("1"); + params.push_back(":"+InboundDescription); + Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,InboundServerName); + this->bursting = true; + this->DoBurst(Node); + } + else if (command == "ERROR") + { + return this->Error(params); + } + else if (command == "CAPAB") + { + return this->Capab(params); + } + + break; + case LISTENER: + this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!"); + return false; + break; + case CONNECTING: + if (command == "SERVER") + { + // another server we connected to, which was in WAIT_AUTH_1 state, + // has just sent us their credentials. If we get this far, theyre + // happy with OUR credentials, and they are now in WAIT_AUTH_2 state. + // if we're happy with this, we should send our netburst which + // kickstarts the merge. + return this->Outbound_Reply_Server(params); + } + else if (command == "ERROR") + { + return this->Error(params); + } + break; + case CONNECTED: + // This is the 'authenticated' state, when all passwords + // have been exchanged and anything past this point is taken + // as gospel. + + if (prefix != "") + { + std::string direction = prefix; + userrec* t = this->Instance->FindNick(prefix); + if (t) + { + direction = t->server; + } + TreeServer* route_back_again = Utils->BestRouteTo(direction); + if ((!route_back_again) || (route_back_again->GetSocket() != this)) + { + if (route_back_again) + Instance->Log(DEBUG,"Protocol violation: Fake direction in command '%s' from connection '%s'",line.c_str(),this->GetName().c_str()); + return true; + } + /* Fix by brain: + * When there is activity on the socket, reset the ping counter so + * that we're not wasting bandwidth pinging an active server. + */ + route_back_again->SetNextPingTime(time(NULL) + 60); + route_back_again->SetPingFlag(); + } + + if (command == "SVSMODE") + { + /* Services expects us to implement + * SVSMODE. In inspircd its the same as + * MODE anyway. + */ + command = "MODE"; + } + std::string target = ""; + /* Yes, know, this is a mess. Its reasonably fast though as we're + * working with std::string here. + */ + if ((command == "NICK") && (params.size() > 1)) + { + return this->IntroduceClient(prefix,params); + } + else if (command == "FJOIN") + { + return this->ForceJoin(prefix,params); + } + else if (command == "STATS") + { + return this->Stats(prefix, params); + } + else if (command == "MOTD") + { + return this->Motd(prefix, params); + } + else if (command == "ADMIN") + { + return this->Admin(prefix, params); + } + else if (command == "SERVER") + { + return this->RemoteServer(prefix,params); + } + else if (command == "ERROR") + { + return this->Error(params); + } + else if (command == "OPERTYPE") + { + return this->OperType(prefix,params); + } + else if (command == "FMODE") + { + return this->ForceMode(prefix,params); + } + else if (command == "KILL") + { + return this->RemoteKill(prefix,params); + } + else if (command == "FTOPIC") + { + return this->ForceTopic(prefix,params); + } + else if (command == "REHASH") + { + return this->RemoteRehash(prefix,params); + } + else if (command == "METADATA") + { + return this->MetaData(prefix,params); + } + else if (command == "REMSTATUS") + { + return this->RemoveStatus(prefix,params); + } + else if (command == "PING") + { + /* + * We just got a ping from a server that's bursting. + * This can't be right, so set them to not bursting, and + * apply their lines. + */ + if (this->bursting) + { + this->bursting = false; + Instance->XLines->apply_lines(Utils->lines_to_apply); + Utils->lines_to_apply = 0; + } + if (prefix == "") + { + prefix = this->GetName(); + } + return this->LocalPing(prefix,params); + } + else if (command == "PONG") + { + /* + * We just got a pong from a server that's bursting. + * This can't be right, so set them to not bursting, and + * apply their lines. + */ + if (this->bursting) + { + this->bursting = false; + Instance->XLines->apply_lines(Utils->lines_to_apply); + Utils->lines_to_apply = 0; + } + if (prefix == "") + { + prefix = this->GetName(); + } + return this->LocalPong(prefix,params); + } + else if (command == "VERSION") + { + return this->ServerVersion(prefix,params); + } + else if (command == "FHOST") + { + return this->ChangeHost(prefix,params); + } + else if (command == "FNAME") + { + return this->ChangeName(prefix,params); + } + else if (command == "ADDLINE") + { + return this->AddLine(prefix,params); + } + else if (command == "SVSNICK") + { + if (prefix == "") + { + prefix = this->GetName(); + } + return this->ForceNick(prefix,params); + } + else if (command == "RSQUIT") + { + return this->RemoteSquit(prefix, params); + } + else if (command == "IDLE") + { + return this->Whois(prefix,params); + } + else if (command == "PUSH") + { + return this->Push(prefix,params); + } + else if (command == "TIMESET") + { + return this->HandleSetTime(prefix, params); + } + else if (command == "TIME") + { + return this->Time(prefix,params); + } + else if ((command == "KICK") && (Utils->IsServer(prefix))) + { + std::string sourceserv = this->myhost; + if (params.size() == 3) + { + userrec* user = this->Instance->FindNick(params[1]); + chanrec* chan = this->Instance->FindChan(params[0]); + if (user && chan) + { + if (!chan->ServerKickUser(user, params[2].c_str(), false)) + /* Yikes, the channels gone! */ + delete chan; + } + } + if (this->InboundServerName != "") + { + sourceserv = this->InboundServerName; + } + return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); + } + else if (command == "SVSJOIN") + { + if (prefix == "") + { + prefix = this->GetName(); + } + return this->ServiceJoin(prefix,params); + } + else if (command == "SQUIT") + { + if (params.size() == 2) + { + this->Squit(Utils->FindServer(params[0]),params[1]); + } + return true; + } + else if (command == "OPERNOTICE") + { + std::string sourceserv = this->myhost; + if (this->InboundServerName != "") + sourceserv = this->InboundServerName; + if (params.size() >= 1) + Instance->WriteOpers("*** From " + sourceserv + ": " + params[0]); + return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); + } + else if (command == "MODENOTICE") + { + std::string sourceserv = this->myhost; + if (this->InboundServerName != "") + sourceserv = this->InboundServerName; + if (params.size() >= 2) + { + Instance->WriteMode(params[0].c_str(), WM_AND, "*** From %s: %s", sourceserv.c_str(), params[1].c_str()); + } + return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); + } + else if (command == "SNONOTICE") + { + std::string sourceserv = this->myhost; + if (this->InboundServerName != "") + sourceserv = this->InboundServerName; + if (params.size() >= 2) + { + Instance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + sourceserv + ": "+ params[1]); + } + return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); + } + else if (command == "ENDBURST") + { + this->bursting = false; + Instance->XLines->apply_lines(Utils->lines_to_apply); + Utils->lines_to_apply = 0; + std::string sourceserv = this->myhost; + if (this->InboundServerName != "") + { + sourceserv = this->InboundServerName; + } + this->Instance->SNO->WriteToSnoMask('l',"Received end of netburst from \2%s\2",sourceserv.c_str()); + + Event rmode((char*)sourceserv.c_str(), (Module*)Utils->Creator, "new_server"); + rmode.Send(Instance); + + return true; + } + else + { + // not a special inter-server command. + // Emulate the actual user doing the command, + // this saves us having a huge ugly parser. + userrec* who = this->Instance->FindNick(prefix); + std::string sourceserv = this->myhost; + if (this->InboundServerName != "") + { + sourceserv = this->InboundServerName; + } + if ((!who) && (command == "MODE")) + { + if (Utils->IsServer(prefix)) + { + const char* modelist[127]; + for (size_t i = 0; i < params.size(); i++) + modelist[i] = params[i].c_str(); + userrec* fake = new userrec(Instance); + fake->SetFd(FD_MAGIC_NUMBER); + this->Instance->SendMode(modelist, params.size(), fake); + + delete fake; + /* Hot potato! pass it on! */ + return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); + } + } + if (who) + { + if ((command == "NICK") && (params.size() > 0)) + { + /* On nick messages, check that the nick doesnt + * already exist here. If it does, kill their copy, + * and our copy. + */ + userrec* x = this->Instance->FindNick(params[0]); + if ((x) && (x != who)) + { + std::deque<std::string> p; + p.push_back(params[0]); + p.push_back("Nickname collision ("+prefix+" -> "+params[0]+")"); + Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p); + p.clear(); + p.push_back(prefix); + p.push_back("Nickname collision"); + Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p); + userrec::QuitUser(this->Instance,x,"Nickname collision ("+prefix+" -> "+params[0]+")"); + userrec* y = this->Instance->FindNick(prefix); + if (y) + { + userrec::QuitUser(this->Instance,y,"Nickname collision"); + } + return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); + } + } + // its a user + target = who->server; + const char* strparams[127]; + for (unsigned int q = 0; q < params.size(); q++) + { + strparams[q] = params[q].c_str(); + } + switch (this->Instance->CallCommandHandler(command.c_str(), strparams, params.size(), who)) + { + case CMD_INVALID: + this->WriteLine("ERROR :Unrecognised command '"+std::string(command.c_str())+"' -- possibly loaded mismatched modules"); + return false; + break; + case CMD_FAILURE: + return true; + break; + default: + /* CMD_SUCCESS and CMD_USER_DELETED fall through here */ + break; + } + } + else + { + // its not a user. Its either a server, or somethings screwed up. + if (Utils->IsServer(prefix)) + target = this->Instance->Config->ServerName; + else + return true; + } + return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); + + } + return true; + break; + } + return true; +} + +std::string TreeSocket::GetName() +{ + std::string sourceserv = this->myhost; + if (this->InboundServerName != "") + { + sourceserv = this->InboundServerName; + } + return sourceserv; +} + +void TreeSocket::OnTimeout() +{ + if (this->LinkState == CONNECTING) + { + this->Instance->SNO->WriteToSnoMask('l',"CONNECT: Connection to \002"+myhost+"\002 timed out."); + Link* MyLink = Utils->FindLink(myhost); + if (MyLink) + Utils->DoFailOver(MyLink); + } +} + +void TreeSocket::OnClose() +{ + // 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 != "") + { + quitserver = this->InboundServerName; + } + TreeServer* s = Utils->FindServer(quitserver); + if (s) + { + Squit(s,"Remote host closed the connection"); + } + + if (quitserver != "") + this->Instance->SNO->WriteToSnoMask('l',"Connection to '\2%s\2' failed.",quitserver.c_str()); +} + +int TreeSocket::OnIncomingConnection(int newsock, char* ip) +{ + /* To prevent anyone from attempting to flood opers/DDoS by connecting to the server port, + * or discovering if this port is the server port, we don't allow connections from any + * IPs for which we don't have a link block. + */ + bool found = false; + + found = (std::find(Utils->ValidIPs.begin(), Utils->ValidIPs.end(), ip) != Utils->ValidIPs.end()); + if (!found) + { + for (vector<std::string>::iterator i = Utils->ValidIPs.begin(); i != Utils->ValidIPs.end(); i++) + if (irc::sockets::MatchCIDR(ip, (*i).c_str())) + found = true; + + if (!found) + { + this->Instance->SNO->WriteToSnoMask('l',"Server connection from %s denied (no link blocks with that IP address)", ip); + close(newsock); + return false; + } + } + + TreeSocket* s = new TreeSocket(this->Utils, this->Instance, newsock, ip, this->Hook); + s = s; /* Whinge whinge whinge, thats all GCC ever does. */ + return true; +} |