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) && ((ComparePass(this->MakePass(x->RecvPass,this->GetOurChallenge()),password)) || (x->RecvPass == password && (this->GetTheirChallenge().empty()))))
132 TreeServer* CheckDupe = Utils->FindServer(sname);
135 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
136 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
141 * They're in WAIT_AUTH_2 (having accepted our credentials).
142 * Set our state to CONNECTED (since everything's peachy so far) and send our
143 * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
145 * While we're at it, create a treeserver object so we know about them.
148 this->LinkState = CONNECTED;
150 TreeServer *Node = new TreeServer(this->Utils, this->Instance, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
152 if (Node->DuplicateID())
154 this->SendError("Server ID "+sid+" already exists on the network!");
155 this->Instance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
159 Utils->TreeRoot->AddChild(Node);
160 params[4] = ":" + params[4];
161 Utils->DoOneToAllButSender(Instance->Config->GetSID(),"SERVER",params,sname);
162 Node->bursting = true;
167 this->SendError("Invalid credentials");
168 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
173 * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
176 bool TreeSocket::Inbound_Server(std::deque<std::string> ¶ms)
178 if (params.size() < 5)
180 SendError("Protocol error - Missing SID");
184 irc::string servername = params[0].c_str();
185 std::string sname = params[0];
186 std::string password = params[1];
187 std::string sid = params[3];
188 std::string description = params[4];
189 int hops = atoi(params[2].c_str());
191 this->InboundServerName = sname;
192 this->InboundDescription = description;
193 this->InboundSID = sid;
196 this->SendCapabilities();
200 this->SendError("Server too far away for authentication");
201 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
205 if (!this->Instance->IsSID(sid))
207 this->SendError("Invalid format server ID: "+sid+"!");
211 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
213 if ((x->Name == servername) && ((ComparePass(this->MakePass(x->RecvPass,this->GetOurChallenge()),password) || x->RecvPass == password && (this->GetTheirChallenge().empty()))))
215 /* Check for fully initialized instances of the server by id */
216 Instance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
217 TreeServer* CheckDupeSID = Utils->FindServerID(sid);
220 this->SendError("Server ID "+CheckDupeSID->GetID()+" already exists on server "+CheckDupeSID->GetName()+"!");
221 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupeSID->GetID()+
222 "' already exists on server "+CheckDupeSID->GetName());
225 /* Now check for fully initialized instances of the server by name */
226 TreeServer* CheckDupe = Utils->FindServer(sname);
229 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
230 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
233 this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
236 std::string name = BufferedSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
237 this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
240 // this is good. Send our details: Our server name and description and hopcount of 0,
241 // along with the sendpass from this block.
242 this->SendCapabilities();
243 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
244 // move to the next state, we are now waiting for THEM.
245 this->LinkState = WAIT_AUTH_2;
249 this->SendError("Invalid credentials");
250 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");