2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "socketengine.h"
29 #include "treeserver.h"
30 #include "treesocket.h"
32 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h m_spanningtree/link.h */
35 * Some server somewhere in the network introducing another server.
38 bool TreeSocket::RemoteServer(const std::string &prefix, parameterlist ¶ms)
40 if (params.size() < 5)
42 SendError("Protocol error - Not enough parameters for SERVER command");
46 std::string servername = params[0];
47 // password is not used for a remote server
48 // hopcount is not used (ever)
49 std::string sid = params[3];
50 std::string description = params[4];
51 TreeServer* ParentOfThis = Utils->FindServer(prefix);
55 this->SendError("Protocol error - Introduced remote server from unknown server "+prefix);
58 if (!InspIRCd::IsSID(sid))
60 this->SendError("Invalid format server ID: "+sid+"!");
63 TreeServer* CheckDupe = Utils->FindServer(servername);
66 this->SendError("Server "+servername+" already exists!");
67 ServerInstance->SNO->WriteToSnoMask('L', "Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
70 CheckDupe = Utils->FindServer(sid);
73 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.");
74 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());
79 Link* lnk = Utils->FindLink(servername);
81 TreeServer *Node = new TreeServer(Utils, servername, description, sid, ParentOfThis,NULL, lnk ? lnk->Hidden : false);
83 ParentOfThis->AddChild(Node);
84 params[4] = ":" + params[4];
85 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
86 ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
92 * This is used after the other side of a connection has accepted our credentials.
93 * They are then introducing themselves to us, BEFORE either of us burst. -- w
95 bool TreeSocket::Outbound_Reply_Server(parameterlist ¶ms)
97 if (params.size() < 5)
99 SendError("Protocol error - Not enough parameters for SERVER command");
103 irc::string servername = params[0].c_str();
104 std::string sname = params[0];
105 std::string password = params[1];
106 std::string sid = params[3];
107 std::string description = params[4];
108 int hops = atoi(params[2].c_str());
110 this->SendCapabilities(2);
114 this->SendError("Server too far away for authentication");
115 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
119 if (!ServerInstance->IsSID(sid))
121 this->SendError("Invalid format server ID: "+sid+"!");
125 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
128 if (x->Name != servername && x->Name != "*") // open link allowance
131 if (!ComparePass(*x, password))
133 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 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
144 CheckDupe = Utils->FindServer(sid);
147 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.");
148 ServerInstance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
153 * They're in WAIT_AUTH_2 (having accepted our credentials).
154 * Set our state to CONNECTED (since everything's peachy so far) and send our
155 * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
157 * While we're at it, create a treeserver object so we know about them.
160 this->LinkState = CONNECTED;
162 Utils->timeoutlist.erase(this);
165 MyRoot = new TreeServer(Utils, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
166 Utils->TreeRoot->AddChild(MyRoot);
167 this->DoBurst(MyRoot);
169 params[4] = ":" + params[4];
171 /* IMPORTANT: Take password/hmac hash OUT of here before we broadcast the introduction! */
173 Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",params,sname);
178 this->SendError("Invalid credentials (check the other server's linking snomask for more information)");
179 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
184 * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
187 bool TreeSocket::Inbound_Server(parameterlist ¶ms)
189 if (params.size() < 5)
191 SendError("Protocol error - Missing SID");
195 irc::string servername = params[0].c_str();
196 std::string sname = params[0];
197 std::string password = params[1];
198 std::string sid = params[3];
199 std::string description = params[4];
200 int hops = atoi(params[2].c_str());
202 this->SendCapabilities(2);
206 this->SendError("Server too far away for authentication");
207 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
211 if (!ServerInstance->IsSID(sid))
213 this->SendError("Invalid format server ID: "+sid+"!");
217 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
220 if (x->Name != servername && x->Name != "*") // open link allowance
223 if (!ComparePass(*x, password))
225 ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
229 /* Now check for fully initialized ServerInstances of the server by name */
230 TreeServer* CheckDupe = Utils->FindServer(sname);
233 std::string pname = CheckDupe->GetParent() ? CheckDupe->GetParent()->GetName() : "<ourself>";
234 SendError("Server "+sname+" already exists on server "+pname+"!");
235 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+pname);
239 /* Check for fully initialized instances of the server by id */
240 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
241 CheckDupe = Utils->FindServerID(sid);
245 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.");
246 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupe->GetID()+
247 "' already exists on server "+CheckDupe->GetName());
251 ServerInstance->SNO->WriteToSnoMask('l',"Verified incoming server connection " + linkID + " ("+description+")");
254 // this is good. Send our details: Our server name and description and hopcount of 0,
255 // along with the sendpass from this block.
256 this->SendCapabilities(2);
257 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
258 // move to the next state, we are now waiting for THEM.
259 MyRoot = new TreeServer(Utils, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
260 Utils->TreeRoot->AddChild(MyRoot);
262 this->LinkState = WAIT_AUTH_2;
266 this->SendError("Invalid credentials");
267 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");