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 const std::string& servername = params[0];
37 const std::string& sid = params[1];
38 const std::string& description = params.back();
39 TreeSocket* socket = ParentOfThis->GetSocket();
41 if (!InspIRCd::IsSID(sid))
43 socket->SendError("Invalid format server ID: "+sid+"!");
46 TreeServer* CheckDupe = Utils->FindServer(servername);
49 socket->SendError("Server "+servername+" already exists!");
50 ServerInstance->SNO->WriteToSnoMask('L', "Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
53 CheckDupe = Utils->FindServer(sid);
56 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.");
57 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());
62 Link* lnk = Utils->FindLink(servername);
64 TreeServer* Node = new TreeServer(servername, description, sid, ParentOfThis, ParentOfThis->GetSocket(), lnk ? lnk->Hidden : false);
66 ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
70 Link* TreeSocket::AuthRemote(const parameterlist& params)
72 if (params.size() < 5)
74 SendError("Protocol error - Not enough parameters for SERVER command");
78 irc::string servername = params[0].c_str();
79 const std::string& sname = params[0];
80 const std::string& password = params[1];
81 const std::string& sid = params[3];
82 const std::string& description = params.back();
84 this->SendCapabilities(2);
86 if (!ServerInstance->IsSID(sid))
88 this->SendError("Invalid format server ID: "+sid+"!");
92 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
95 if (x->Name != servername && x->Name != "*") // open link allowance
98 if (!ComparePass(*x, password))
100 ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
104 if (!CheckDuplicate(sname, sid))
107 ServerInstance->SNO->WriteToSnoMask('l',"Verified server connection " + linkID + " ("+description+")");
111 this->SendError("Mismatched server name or password (check the other server's snomask output for details - e.g. umode +s +Ll)");
112 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
117 * This is used after the other side of a connection has accepted our credentials.
118 * They are then introducing themselves to us, BEFORE either of us burst. -- w
120 bool TreeSocket::Outbound_Reply_Server(parameterlist ¶ms)
122 const Link* x = AuthRemote(params);
126 * They're in WAIT_AUTH_2 (having accepted our credentials).
127 * Set our state to CONNECTED (since everything's peachy so far) and send our
128 * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
130 * While we're at it, create a treeserver object so we know about them.
133 FinishAuth(params[0], params[3], params.back(), x->Hidden);
141 bool TreeSocket::CheckDuplicate(const std::string& sname, const std::string& sid)
143 /* Check for fully initialized instances of the server by name */
144 TreeServer* CheckDupe = Utils->FindServer(sname);
147 std::string pname = CheckDupe->GetParent() ? CheckDupe->GetParent()->GetName() : "<ourself>";
148 SendError("Server "+sname+" already exists on server "+pname+"!");
149 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+pname);
153 /* Check for fully initialized instances of the server by id */
154 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Looking for dupe SID %s", sid.c_str());
155 CheckDupe = Utils->FindServerID(sid);
159 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.");
160 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupe->GetID()+
161 "' already exists on server "+CheckDupe->GetName());
169 * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
172 bool TreeSocket::Inbound_Server(parameterlist ¶ms)
174 const Link* x = AuthRemote(params);
177 // Save these for later, so when they accept our credentials (indicated by BURST) we remember them
178 this->capab->hidden = x->Hidden;
179 this->capab->sid = params[3];
180 this->capab->description = params.back();
181 this->capab->name = params[0];
183 // Send our details: Our server name and description and hopcount of 0,
184 // along with the sendpass from this block.
185 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
187 // move to the next state, we are now waiting for THEM.
188 this->LinkState = WAIT_AUTH_2;
195 CommandServer::Builder::Builder(TreeServer* server)
196 : CmdBuilder(server->GetParent()->GetID(), "SERVER")
198 push(server->GetName());
199 push(server->GetID());
200 push_last(server->GetDesc());