1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "../transport.h"
18 #include "socketengine.h"
23 #include "treeserver.h"
24 #include "treesocket.h"
26 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h m_spanningtree/link.h */
29 * Some server somewhere in the network introducing another server.
32 bool TreeSocket::RemoteServer(const std::string &prefix, parameterlist ¶ms)
34 if (params.size() < 5)
36 SendError("Protocol error - Not enough parameters for SERVER command");
40 std::string servername = params[0];
41 // password is not used for a remote server
42 // hopcount is not used (ever)
43 std::string sid = params[3];
44 std::string description = params[4];
45 TreeServer* ParentOfThis = Utils->FindServer(prefix);
49 this->SendError("Protocol error - Introduced remote server from unknown server "+ParentOfThis->GetName());
52 if (!ServerInstance->IsSID(sid))
54 this->SendError("Invalid format server ID: "+sid+"!");
57 TreeServer* CheckDupe = Utils->FindServer(servername);
60 this->SendError("Server "+servername+" already exists!");
61 ServerInstance->SNO->WriteToSnoMask('L', "Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
64 CheckDupe = Utils->FindServer(sid);
67 this->SendError("Server ID "+sid+" already exists! You may want to specify the server ID for the server manually with <server:id> so they do not conflict.");
68 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());
73 Link* lnk = Utils->FindLink(servername);
75 TreeServer *Node = new TreeServer(Utils, servername, description, sid, ParentOfThis,NULL, lnk ? lnk->Hidden : false);
77 ParentOfThis->AddChild(Node);
78 params[4] = ":" + params[4];
79 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
80 ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
86 * This is used after the other side of a connection has accepted our credentials.
87 * They are then introducing themselves to us, BEFORE either of us burst. -- w
89 bool TreeSocket::Outbound_Reply_Server(parameterlist ¶ms)
91 if (params.size() < 5)
93 SendError("Protocol error - Not enough parameters for SERVER command");
97 irc::string servername = params[0].c_str();
98 std::string sname = params[0];
99 std::string password = params[1];
100 std::string sid = params[3];
101 std::string description = params[4];
102 int hops = atoi(params[2].c_str());
104 this->InboundServerName = sname;
105 this->InboundDescription = description;
106 this->InboundSID = sid;
108 this->SendCapabilities(2);
112 this->SendError("Server too far away for authentication");
113 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
117 if (!ServerInstance->IsSID(sid))
119 this->SendError("Invalid format server ID: "+sid+"!");
123 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
126 if (x->Name != servername && x->Name != "*") // open link allowance
129 if (!ComparePass(*x, password))
131 ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
135 TreeServer* CheckDupe = Utils->FindServer(sname);
138 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
139 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
142 CheckDupe = Utils->FindServer(sid);
145 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.");
146 ServerInstance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
151 * They're in WAIT_AUTH_2 (having accepted our credentials).
152 * Set our state to CONNECTED (since everything's peachy so far) and send our
153 * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
155 * While we're at it, create a treeserver object so we know about them.
158 this->LinkState = CONNECTED;
160 Utils->timeoutlist.erase(this);
162 TreeServer *Node = new TreeServer(Utils, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
164 Utils->TreeRoot->AddChild(Node);
165 params[4] = ":" + params[4];
168 /* IMPORTANT: Take password/hmac hash OUT of here before we broadcast the introduction! */
170 Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",params,sname);
176 this->SendError("Invalid credentials (check the other server's linking snomask for more information)");
177 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
182 * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
185 bool TreeSocket::Inbound_Server(parameterlist ¶ms)
187 if (params.size() < 5)
189 SendError("Protocol error - Missing SID");
193 irc::string servername = params[0].c_str();
194 std::string sname = params[0];
195 std::string password = params[1];
196 std::string sid = params[3];
197 std::string description = params[4];
198 int hops = atoi(params[2].c_str());
200 this->InboundServerName = sname;
201 this->InboundDescription = description;
202 this->InboundSID = sid;
204 this->SendCapabilities(2);
208 this->SendError("Server too far away for authentication");
209 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
213 if (!ServerInstance->IsSID(sid))
215 this->SendError("Invalid format server ID: "+sid+"!");
219 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
222 if (x->Name != servername && x->Name != "*") // open link allowance
225 if (!ComparePass(*x, password))
227 ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
231 /* Now check for fully initialized ServerInstances of the server by name */
232 TreeServer* CheckDupe = Utils->FindServer(sname);
235 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
236 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
240 /* Check for fully initialized instances of the server by id */
241 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
242 CheckDupe = Utils->FindServerID(sid);
246 this->SendError("Server ID "+CheckDupe->GetID()+" already exists on server "+CheckDupe->GetName()+"! You may want to specify the server ID for the server manually with <server:id> so they do not conflict.");
247 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupe->GetID()+
248 "' already exists on server "+CheckDupe->GetName());
253 ServerInstance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->IP)+"] ("+description+")");
254 if (this->GetIOHook())
256 std::string name = BufferedSocketNameRequest(Utils->Creator, this->GetIOHook()).Send();
257 ServerInstance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->IP)+"] using transport \2"+name+"\2");
260 // this is good. Send our details: Our server name and description and hopcount of 0,
261 // along with the sendpass from this block.
262 this->SendCapabilities(2);
263 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
264 // move to the next state, we are now waiting for THEM.
265 this->LinkState = WAIT_AUTH_2;
269 this->SendError("Invalid credentials");
270 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");