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);
53 this->SendError("Protocol error - Introduced remote server from unknown server "+prefix);
56 if (!this->Instance->IsSID(sid))
58 this->SendError("Invalid format server ID: "+sid+"!");
61 TreeServer* CheckDupe = Utils->FindServer(servername);
64 this->SendError("Server "+servername+" already exists!");
65 this->Instance->SNO->WriteToSnoMask('l',"Server \2"+servername+"\2 being introduced from \2" + prefix + "\2 denied, already exists. Closing link with " + prefix);
69 Link* lnk = Utils->FindLink(servername);
71 TreeServer *Node = new TreeServer(this->Utils, this->Instance, servername, description, sid, ParentOfThis,NULL, lnk ? lnk->Hidden : false);
73 if (Node->DuplicateID())
75 this->SendError("Server ID "+sid+" already exists on the network!");
76 this->Instance->SNO->WriteToSnoMask('l',"Server \2"+servername+"\2 being introduced from \2" + prefix + "\2 denied, server ID already exists on the network. Closing link with " + prefix);
80 ParentOfThis->AddChild(Node);
81 params[4] = ":" + params[4];
82 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
83 this->Instance->SNO->WriteToSnoMask('l',"Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
89 * This is used after the other side of a connection has accepted our credentials.
90 * They are then introducing themselves to us, BEFORE either of us burst. -- w
92 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> ¶ms)
94 if (params.size() < 5)
96 SendError("Protocol error - Missing SID");
100 irc::string servername = params[0].c_str();
101 std::string sname = params[0];
102 std::string password = params[1];
103 std::string sid = params[3];
104 std::string description = params[4];
105 int hops = atoi(params[2].c_str());
107 this->InboundServerName = sname;
108 this->InboundDescription = description;
109 this->InboundSID = sid;
112 this->SendCapabilities();
116 this->SendError("Server too far away for authentication");
117 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
121 if (!this->Instance->IsSID(sid))
123 this->SendError("Invalid format server ID: "+sid+"!");
127 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
129 if ((x->Name == servername) && ((ComparePass(this->MakePass(x->RecvPass,this->GetOurChallenge()),password)) || (x->RecvPass == password && (this->GetTheirChallenge().empty()))))
131 TreeServer* CheckDupe = Utils->FindServer(sname);
134 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
135 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
140 * They're in WAIT_AUTH_2 (having accepted our credentials).
141 * Set our state to CONNECTED (since everything's peachy so far) and send our
142 * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
144 * While we're at it, create a treeserver object so we know about them.
147 this->LinkState = CONNECTED;
149 TreeServer *Node = new TreeServer(this->Utils, this->Instance, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
151 if (Node->DuplicateID())
153 this->SendError("Server ID "+sid+" already exists on the network!");
154 this->Instance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
158 Utils->TreeRoot->AddChild(Node);
159 params[4] = ":" + params[4];
160 Utils->DoOneToAllButSender(Instance->Config->GetSID(),"SERVER",params,sname);
161 Node->bursting = true;
166 this->SendError("Invalid credentials");
167 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
172 * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
175 bool TreeSocket::Inbound_Server(std::deque<std::string> ¶ms)
177 if (params.size() < 5)
179 SendError("Protocol error - Missing SID");
183 irc::string servername = params[0].c_str();
184 std::string sname = params[0];
185 std::string password = params[1];
186 std::string sid = params[3];
187 std::string description = params[4];
188 int hops = atoi(params[2].c_str());
190 this->InboundServerName = sname;
191 this->InboundDescription = description;
192 this->InboundSID = sid;
195 this->SendCapabilities();
199 this->SendError("Server too far away for authentication");
200 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
204 if (!this->Instance->IsSID(sid))
206 this->SendError("Invalid format server ID: "+sid+"!");
210 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
212 if ((x->Name == servername) && ((ComparePass(this->MakePass(x->RecvPass,this->GetOurChallenge()),password) || x->RecvPass == password && (this->GetTheirChallenge().empty()))))
214 /* Check for fully initialized instances of the server by id */
215 Instance->Log(DEBUG,"Looking for dupe SID %s", sid.c_str());
216 TreeServer* CheckDupeSID = Utils->FindServerID(sid);
219 this->SendError("Server ID "+CheckDupeSID->GetID()+" already exists on server "+CheckDupeSID->GetName()+"!");
220 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupeSID->GetID()+
221 "' already exists on server "+CheckDupeSID->GetName());
224 /* Now check for fully initialized instances of the server by name */
225 TreeServer* CheckDupe = Utils->FindServer(sname);
228 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
229 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
232 this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
235 std::string name = BufferedSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
236 this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
239 // this is good. Send our details: Our server name and description and hopcount of 0,
240 // along with the sendpass from this block.
241 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
242 // move to the next state, we are now waiting for THEM.
243 this->LinkState = WAIT_AUTH_2;
247 this->SendError("Invalid credentials");
248 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");