1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
20 #include "transport.h"
21 #include "socketengine.h"
23 #include "m_spanningtree/main.h"
24 #include "m_spanningtree/utils.h"
25 #include "m_spanningtree/treeserver.h"
26 #include "m_spanningtree/link.h"
27 #include "m_spanningtree/treesocket.h"
28 #include "m_spanningtree/resolvers.h"
29 #include "m_spanningtree/handshaketimer.h"
31 /* $ModDep: m_spanningtree/timesynctimer.h m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h */
34 * Some server somewhere in the network introducing another server.
37 bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms)
39 if (params.size() < 5)
41 SendError("Protocol error - Missing SID");
45 std::string servername = params[0];
46 std::string password = params[1];
47 // hopcount is not used for a remote server, we calculate this ourselves
48 std::string sid = params[3];
49 std::string description = params[4];
50 TreeServer* ParentOfThis = Utils->FindServer(prefix);
54 this->SendError("Protocol error - Introduced remote server from unknown server "+ParentOfThis->GetName());
57 if (!this->Instance->IsSID(sid))
59 this->SendError("Invalid format server ID: "+sid+"!");
62 TreeServer* CheckDupe = Utils->FindServer(servername);
65 this->SendError("Server "+CheckDupe->GetName()+" already exists!");
66 this->Instance->SNO->WriteToSnoMask('l',"Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
70 Link* lnk = Utils->FindLink(servername);
72 TreeServer *Node = new TreeServer(this->Utils, this->Instance, servername, description, sid, ParentOfThis,NULL, lnk ? lnk->Hidden : false);
74 if (Node->DuplicateID())
76 this->SendError("Server ID "+servername+" already exists on the network!");
77 this->Instance->SNO->WriteToSnoMask('l',"Server \2"+servername+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, server ID already exists on the network. Closing link with " + ParentOfThis->GetName());
81 ParentOfThis->AddChild(Node);
82 params[4] = ":" + params[4];
83 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
84 this->Instance->SNO->WriteToSnoMask('l',"Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
90 * This is used after the other side of a connection has accepted our credentials.
91 * They are then introducing themselves to us, BEFORE either of us burst. -- w
93 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> ¶ms)
95 if (params.size() < 5)
97 SendError("Protocol error - Missing SID");
101 irc::string servername = params[0].c_str();
102 std::string sname = params[0];
103 std::string password = params[1];
104 std::string sid = params[3];
105 std::string description = params[4];
106 int hops = atoi(params[2].c_str());
108 this->InboundServerName = sname;
109 this->InboundDescription = description;
110 this->InboundSID = sid;
113 this->SendCapabilities();
117 this->SendError("Server too far away for authentication");
118 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
122 if (!this->Instance->IsSID(sid))
124 this->SendError("Invalid format server ID: "+sid+"!");
128 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
130 if (x->Name != servername && x->Name != "*") // open link allowance
133 if (!ComparePass(this->MakePass(x->RecvPass, this->GetOurChallenge()), password) ||
134 (x->RecvPass != password && !this->GetTheirChallenge().empty()))
137 TreeServer* CheckDupe = Utils->FindServer(sname);
140 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
141 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
146 * They're in WAIT_AUTH_2 (having accepted our credentials).
147 * Set our state to CONNECTED (since everything's peachy so far) and send our
148 * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
150 * While we're at it, create a treeserver object so we know about them.
153 this->LinkState = CONNECTED;
155 TreeServer *Node = new TreeServer(this->Utils, this->Instance, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
157 if (Node->DuplicateID())
159 this->SendError("Server ID "+sid+" already exists on the network!");
160 this->Instance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
164 Utils->TreeRoot->AddChild(Node);
165 params[4] = ":" + params[4];
166 Utils->DoOneToAllButSender(Instance->Config->GetSID(),"SERVER",params,sname);
167 Node->bursting = true;
172 this->SendError("Invalid credentials");
173 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
178 * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
181 bool TreeSocket::Inbound_Server(std::deque<std::string> ¶ms)
183 if (params.size() < 5)
185 SendError("Protocol error - Missing SID");
189 irc::string servername = params[0].c_str();
190 std::string sname = params[0];
191 std::string password = params[1];
192 std::string sid = params[3];
193 std::string description = params[4];
194 int hops = atoi(params[2].c_str());
196 this->InboundServerName = sname;
197 this->InboundDescription = description;
198 this->InboundSID = sid;
201 this->SendCapabilities();
205 this->SendError("Server too far away for authentication");
206 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
210 if (!this->Instance->IsSID(sid))
212 this->SendError("Invalid format server ID: "+sid+"!");
216 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
218 if (x->Name != servername && x->Name != "*") // open link allowance
221 if (!ComparePass(this->MakePass(x->RecvPass, this->GetOurChallenge()), password) ||
222 (x->RecvPass != password && !this->GetTheirChallenge().empty()))
225 /* Check for fully initialized instances of the server by id */
226 Instance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
227 TreeServer* CheckDupeSID = Utils->FindServerID(sid);
231 this->SendError("Server ID "+CheckDupeSID->GetID()+" already exists on server "+CheckDupeSID->GetName()+"!");
232 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupeSID->GetID()+
233 "' already exists on server "+CheckDupeSID->GetName());
237 /* Now check for fully initialized instances of the server by name */
238 TreeServer* CheckDupe = Utils->FindServer(sname);
241 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
242 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
246 this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
249 std::string name = BufferedSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
250 this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
253 // this is good. Send our details: Our server name and description and hopcount of 0,
254 // along with the sendpass from this block.
255 this->SendCapabilities();
256 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
257 // move to the next state, we are now waiting for THEM.
258 this->LinkState = WAIT_AUTH_2;
262 this->SendError("Invalid credentials");
263 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");