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/>.
25 #include "treeserver.h"
26 #include "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, parameterlist ¶ms)
36 if (params.size() < 5)
38 SendError("Protocol error - Not enough parameters for SERVER command");
42 std::string servername = params[0];
43 // password is not used for a remote server
44 // hopcount is not used (ever)
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 "+prefix);
54 if (!InspIRCd::IsSID(sid))
56 this->SendError("Invalid format server ID: "+sid+"!");
59 TreeServer* CheckDupe = Utils->FindServer(servername);
62 this->SendError("Server "+servername+" already exists!");
63 ServerInstance->SNO->WriteToSnoMask('L', "Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
66 CheckDupe = Utils->FindServer(sid);
69 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.");
70 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());
75 Link* lnk = Utils->FindLink(servername);
77 TreeServer *Node = new TreeServer(Utils, servername, description, sid, ParentOfThis,NULL, lnk ? lnk->Hidden : false);
79 ParentOfThis->AddChild(Node);
80 params[4] = ":" + params[4];
81 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
82 ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
88 * This is used after the other side of a connection has accepted our credentials.
89 * They are then introducing themselves to us, BEFORE either of us burst. -- w
91 bool TreeSocket::Outbound_Reply_Server(parameterlist ¶ms)
93 if (params.size() < 5)
95 SendError("Protocol error - Not enough parameters for SERVER command");
99 irc::string servername = params[0].c_str();
100 std::string sname = params[0];
101 std::string password = params[1];
102 std::string sid = params[3];
103 std::string description = params[4];
105 this->SendCapabilities(2);
107 if (!ServerInstance->IsSID(sid))
109 this->SendError("Invalid format server ID: "+sid+"!");
113 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
116 if (x->Name != servername && x->Name != "*") // open link allowance
119 if (!ComparePass(*x, password))
121 ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
125 TreeServer* CheckDupe = Utils->FindServer(sname);
128 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
129 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
132 CheckDupe = Utils->FindServer(sid);
135 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.");
136 ServerInstance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
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 Utils->timeoutlist.erase(this);
153 MyRoot = new TreeServer(Utils, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
154 Utils->TreeRoot->AddChild(MyRoot);
155 this->DoBurst(MyRoot);
157 params[4] = ":" + params[4];
159 /* IMPORTANT: Take password/hmac hash OUT of here before we broadcast the introduction! */
161 Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",params,sname);
166 this->SendError("Invalid credentials (check the other server's linking snomask for more information)");
167 ServerInstance->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(parameterlist ¶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];
189 this->SendCapabilities(2);
191 if (!ServerInstance->IsSID(sid))
193 this->SendError("Invalid format server ID: "+sid+"!");
197 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
200 if (x->Name != servername && x->Name != "*") // open link allowance
203 if (!ComparePass(*x, password))
205 ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
209 /* Now check for fully initialized ServerInstances of the server by name */
210 TreeServer* CheckDupe = Utils->FindServer(sname);
213 std::string pname = CheckDupe->GetParent() ? CheckDupe->GetParent()->GetName() : "<ourself>";
214 SendError("Server "+sname+" already exists on server "+pname+"!");
215 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+pname);
219 /* Check for fully initialized instances of the server by id */
220 ServerInstance->Logs->Log("m_spanningtree",LOG_DEBUG,"Looking for dupe SID %s", sid.c_str());
221 CheckDupe = Utils->FindServerID(sid);
225 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.");
226 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupe->GetID()+
227 "' already exists on server "+CheckDupe->GetName());
231 ServerInstance->SNO->WriteToSnoMask('l',"Verified incoming server connection " + linkID + " ("+description+")");
234 // this is good. Send our details: Our server name and description and hopcount of 0,
235 // along with the sendpass from this block.
236 this->SendCapabilities(2);
237 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
238 // move to the next state, we are now waiting for THEM.
239 MyRoot = new TreeServer(Utils, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
240 Utils->TreeRoot->AddChild(MyRoot);
242 this->LinkState = WAIT_AUTH_2;
246 this->SendError("Invalid credentials");
247 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");