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"
19 #include "transport.h"
20 #include "socketengine.h"
22 #include "m_spanningtree/main.h"
23 #include "m_spanningtree/utils.h"
24 #include "m_spanningtree/link.h"
25 #include "m_spanningtree/treeserver.h"
26 #include "m_spanningtree/treesocket.h"
28 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h m_spanningtree/link.h */
31 * Some server somewhere in the network introducing another server.
34 bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms)
36 if (params.size() < 5)
38 SendError("Protocol error - Missing SID");
42 std::string servername = params[0];
43 std::string password = params[1];
44 // hopcount is not used for a remote server, we calculate this ourselves
45 std::string sid = params[3];
46 std::string description = params[4];
47 TreeServer* ParentOfThis = Utils->FindServer(prefix);
51 this->SendError("Protocol error - Introduced remote server from unknown server "+ParentOfThis->GetName());
54 if (!this->ServerInstance->IsSID(sid))
56 this->SendError("Invalid format server ID: "+sid+"!");
59 TreeServer* CheckDupe = Utils->FindServer(servername);
62 this->SendError("Server "+CheckDupe->GetName()+" already exists!");
63 this->ServerInstance->SNO->WriteToSnoMask('L', "Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
67 Link* lnk = Utils->FindLink(servername);
69 TreeServer *Node = new TreeServer(this->Utils, this->ServerInstance, servername, description, sid, ParentOfThis,NULL, lnk ? lnk->Hidden : false);
71 if (Node->DuplicateID())
73 this->SendError("Server ID "+servername+" already exists on the network! You may want to specify the server ID for the server manually with <server:id> so they do not conflict.");
74 this->ServerInstance->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());
78 ParentOfThis->AddChild(Node);
79 params[4] = ":" + params[4];
80 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
81 this->ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
87 * This is used after the other side of a connection has accepted our credentials.
88 * They are then introducing themselves to us, BEFORE either of us burst. -- w
90 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> ¶ms)
92 if (params.size() < 5)
94 SendError("Protocol error - Missing SID");
98 irc::string servername = params[0].c_str();
99 std::string sname = params[0];
100 std::string password = params[1];
101 std::string sid = params[3];
102 std::string description = params[4];
103 int hops = atoi(params[2].c_str());
105 this->InboundServerName = sname;
106 this->InboundDescription = description;
107 this->InboundSID = sid;
110 this->SendCapabilities();
114 this->SendError("Server too far away for authentication");
115 this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
119 if (!this->ServerInstance->IsSID(sid))
121 this->SendError("Invalid format server ID: "+sid+"!");
125 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
127 if (x->Name != servername && x->Name != "*") // open link allowance
130 if (!ComparePass(this->MakePass(x->RecvPass, this->GetOurChallenge()), password) &&
131 (x->RecvPass != password && this->GetTheirChallenge().empty()))
133 this->ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
137 TreeServer* CheckDupe = Utils->FindServer(sname);
140 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
141 this->ServerInstance->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 Utils->timeoutlist.erase(this);
157 TreeServer *Node = new TreeServer(this->Utils, this->ServerInstance, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
159 if (Node->DuplicateID())
161 this->SendError("Server ID "+sid+" already exists on the network! You may want to specify the server ID for the server manually with <server:id> so they do not conflict.");
162 this->ServerInstance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
166 Utils->TreeRoot->AddChild(Node);
167 params[4] = ":" + params[4];
170 /* IMPORTANT: Take password/hmac hash OUT of here before we broadcast the introduction! */
172 Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",params,sname);
174 Node->bursting = true;
179 this->SendError("Invalid credentials (check the other server's linking snomask for more information)");
180 this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
185 * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
188 bool TreeSocket::Inbound_Server(std::deque<std::string> ¶ms)
190 if (params.size() < 5)
192 SendError("Protocol error - Missing SID");
196 irc::string servername = params[0].c_str();
197 std::string sname = params[0];
198 std::string password = params[1];
199 std::string sid = params[3];
200 std::string description = params[4];
201 int hops = atoi(params[2].c_str());
203 this->InboundServerName = sname;
204 this->InboundDescription = description;
205 this->InboundSID = sid;
208 this->SendCapabilities();
212 this->SendError("Server too far away for authentication");
213 this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
217 if (!this->ServerInstance->IsSID(sid))
219 this->SendError("Invalid format server ID: "+sid+"!");
223 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
225 if (x->Name != servername && x->Name != "*") // open link allowance
228 if (!ComparePass(this->MakePass(x->RecvPass, this->GetOurChallenge()), password) &&
229 (x->RecvPass != password && this->GetTheirChallenge().empty()))
231 this->ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
235 /* Check for fully initialized instances of the server by id */
236 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
237 TreeServer* CheckDupeSID = Utils->FindServerID(sid);
241 this->SendError("Server ID "+CheckDupeSID->GetID()+" already exists on server "+CheckDupeSID->GetName()+"! You may want to specify the server ID for the server manually with <server:id> so they do not conflict.");
242 this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupeSID->GetID()+
243 "' already exists on server "+CheckDupeSID->GetName());
247 /* Now check for fully initialized ServerInstances of the server by name */
248 TreeServer* CheckDupe = Utils->FindServer(sname);
251 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
252 this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
256 this->ServerInstance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
259 std::string name = BufferedSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
260 this->ServerInstance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
263 // this is good. Send our details: Our server name and description and hopcount of 0,
264 // along with the sendpass from this block.
265 this->SendCapabilities();
266 this->WriteLine(std::string("SERVER ")+this->ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+this->ServerInstance->Config->ServerDesc);
267 // move to the next state, we are now waiting for THEM.
268 this->LinkState = WAIT_AUTH_2;
272 this->SendError("Invalid credentials");
273 this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");