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/>.
26 #include "treeserver.h"
27 #include "treesocket.h"
31 * Some server somewhere in the network introducing another server.
34 CmdResult CommandServer::HandleServer(TreeServer* ParentOfThis, std::vector<std::string>& params)
36 std::string servername = params[0];
37 // password is not used for a remote server
38 // hopcount is not used (ever)
39 std::string sid = params[3];
40 std::string description = params[4];
41 TreeSocket* socket = ParentOfThis->GetSocket();
43 if (!InspIRCd::IsSID(sid))
45 socket->SendError("Invalid format server ID: "+sid+"!");
48 TreeServer* CheckDupe = Utils->FindServer(servername);
51 socket->SendError("Server "+servername+" already exists!");
52 ServerInstance->SNO->WriteToSnoMask('L', "Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
55 CheckDupe = Utils->FindServer(sid);
58 socket->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.");
59 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());
64 Link* lnk = Utils->FindLink(servername);
66 TreeServer* Node = new TreeServer(servername, description, sid, ParentOfThis, ParentOfThis->GetSocket(), lnk ? lnk->Hidden : false);
68 ParentOfThis->AddChild(Node);
69 ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
75 * This is used after the other side of a connection has accepted our credentials.
76 * They are then introducing themselves to us, BEFORE either of us burst. -- w
78 bool TreeSocket::Outbound_Reply_Server(parameterlist ¶ms)
80 if (params.size() < 5)
82 SendError("Protocol error - Not enough parameters for SERVER command");
86 irc::string servername = params[0].c_str();
87 std::string sname = params[0];
88 std::string password = params[1];
89 std::string sid = params[3];
90 std::string description = params[4];
92 this->SendCapabilities(2);
94 if (!ServerInstance->IsSID(sid))
96 this->SendError("Invalid format server ID: "+sid+"!");
100 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
103 if (x->Name != servername && x->Name != "*") // open link allowance
106 if (!ComparePass(*x, password))
108 ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
112 TreeServer* CheckDupe = Utils->FindServer(sname);
115 std::string pname = CheckDupe->GetParent() ? CheckDupe->GetParent()->GetName() : "<ourself>";
116 SendError("Server "+sname+" already exists on server "+pname+"!");
117 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+pname);
120 CheckDupe = Utils->FindServer(sid);
123 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.");
124 ServerInstance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
129 * They're in WAIT_AUTH_2 (having accepted our credentials).
130 * Set our state to CONNECTED (since everything's peachy so far) and send our
131 * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
133 * While we're at it, create a treeserver object so we know about them.
136 this->LinkState = CONNECTED;
138 Utils->timeoutlist.erase(this);
141 MyRoot = new TreeServer(sname, description, sid, Utils->TreeRoot, this, x->Hidden);
142 Utils->TreeRoot->AddChild(MyRoot);
143 this->DoBurst(MyRoot);
145 params[4] = ":" + params[4];
147 /* IMPORTANT: Take password/hmac hash OUT of here before we broadcast the introduction! */
149 Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",params,MyRoot);
154 this->SendError("Invalid credentials (check the other server's linking snomask for more information)");
155 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
159 bool TreeSocket::CheckDuplicate(const std::string& sname, const std::string& sid)
161 /* Check for fully initialized instances of the server by name */
162 TreeServer* CheckDupe = Utils->FindServer(sname);
165 std::string pname = CheckDupe->GetParent() ? CheckDupe->GetParent()->GetName() : "<ourself>";
166 SendError("Server "+sname+" already exists on server "+pname+"!");
167 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+pname);
171 /* Check for fully initialized instances of the server by id */
172 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Looking for dupe SID %s", sid.c_str());
173 CheckDupe = Utils->FindServerID(sid);
177 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.");
178 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupe->GetID()+
179 "' already exists on server "+CheckDupe->GetName());
187 * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
190 bool TreeSocket::Inbound_Server(parameterlist ¶ms)
192 if (params.size() < 5)
194 SendError("Protocol error - Missing SID");
198 irc::string servername = params[0].c_str();
199 std::string sname = params[0];
200 std::string password = params[1];
201 std::string sid = params[3];
202 std::string description = params[4];
204 this->SendCapabilities(2);
206 if (!ServerInstance->IsSID(sid))
208 this->SendError("Invalid format server ID: "+sid+"!");
212 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
215 if (x->Name != servername && x->Name != "*") // open link allowance
218 if (!ComparePass(*x, password))
220 ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
224 if (!CheckDuplicate(sname, sid))
227 ServerInstance->SNO->WriteToSnoMask('l',"Verified incoming server connection " + linkID + " ("+description+")");
229 this->SendCapabilities(2);
231 // Save these for later, so when they accept our credentials (indicated by BURST) we remember them
232 this->capab->hidden = x->Hidden;
233 this->capab->sid = sid;
234 this->capab->description = description;
235 this->capab->name = sname;
237 // Send our details: Our server name and description and hopcount of 0,
238 // along with the sendpass from this block.
239 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
241 // move to the next state, we are now waiting for THEM.
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");