2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
9 * Copyright (C) 2008-2010 Craig Edwards <brain@inspircd.org>
10 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "modules/ssl.h"
32 #include "treeserver.h"
33 #include "treesocket.h"
37 * Some server somewhere in the network introducing another server.
40 CmdResult CommandServer::HandleServer(TreeServer* ParentOfThis, Params& params)
42 const std::string& servername = params[0];
43 const std::string& sid = params[1];
44 const std::string& description = params.back();
45 TreeSocket* socket = ParentOfThis->GetSocket();
47 if (!InspIRCd::IsSID(sid))
49 socket->SendError("Invalid format server ID: "+sid+"!");
52 TreeServer* CheckDupe = Utils->FindServer(servername);
55 socket->SendError("Server "+servername+" already exists!");
56 ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+CheckDupe->GetName()+"\002 being introduced from \002" + ParentOfThis->GetName() + "\002 denied, already exists. Closing link with " + ParentOfThis->GetName());
59 CheckDupe = Utils->FindServer(sid);
62 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.");
63 ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+servername+"\002 being introduced from \002" + ParentOfThis->GetName() + "\002 denied, server ID already exists on the network. Closing link with " + ParentOfThis->GetName());
67 TreeServer* route = ParentOfThis->GetRoute();
68 Link* lnk = Utils->FindLink(route->GetName());
69 TreeServer* Node = new TreeServer(servername, description, sid, ParentOfThis, ParentOfThis->GetSocket(), lnk ? lnk->Hidden : false);
71 HandleExtra(Node, params);
73 ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
77 void CommandServer::HandleExtra(TreeServer* newserver, Params& params)
79 for (CommandBase::Params::const_iterator i = params.begin() + 2; i != params.end() - 1; ++i)
81 const std::string& prop = *i;
82 std::string::size_type p = prop.find('=');
84 std::string key = prop;
86 if (p != std::string::npos)
89 val.assign(prop, p+1, std::string::npos);
92 if (irc::equals(key, "burst"))
93 newserver->BeginBurst(ConvToNum<uint64_t>(val));
94 else if (irc::equals(key, "hidden"))
95 newserver->Hidden = ConvToNum<bool>(val);
99 Link* TreeSocket::AuthRemote(const CommandBase::Params& params)
101 if (params.size() < 5)
103 SendError("Protocol error - Not enough parameters for SERVER command");
107 const std::string& sname = params[0];
108 const std::string& password = params[1];
109 const std::string& sid = params[3];
110 const std::string& description = params.back();
112 this->SendCapabilities(2);
114 if (!ServerInstance->IsSID(sid))
116 this->SendError("Invalid format server ID: "+sid+"!");
120 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
123 if (!InspIRCd::Match(sname, x->Name))
126 if (!ComparePass(*x, password))
128 ServerInstance->SNO->WriteToSnoMask('l', "Invalid password on link: %s", x->Name.c_str());
132 if (!CheckDuplicate(sname, sid))
135 ServerInstance->SNO->WriteToSnoMask('l', "Verified server connection " + linkID + " ("+description+")");
137 const SSLIOHook* const ssliohook = SSLIOHook::IsSSL(this);
140 std::string ciphersuite;
141 ssliohook->GetCiphersuite(ciphersuite);
142 ServerInstance->SNO->WriteToSnoMask('l', "Negotiated ciphersuite %s on link %s", ciphersuite.c_str(), x->Name.c_str());
144 else if (!irc::sockets::cidr_mask("127.0.0.0/8").match(capab->remotesa) && !irc::sockets::cidr_mask("::1/128").match(capab->remotesa))
146 ServerInstance->SNO->WriteGlobalSno('l', "Server connection to %s is not using SSL (TLS). This is VERY INSECURE and will not be allowed in the next major version of InspIRCd.", x->Name.c_str());
152 this->SendError("Mismatched server name or password (check the other server's snomask output for details - e.g. user mode +s +Ll)");
153 ServerInstance->SNO->WriteToSnoMask('l', "Server connection from \002"+sname+"\002 denied, invalid link credentials");
158 * This is used after the other side of a connection has accepted our credentials.
159 * They are then introducing themselves to us, BEFORE either of us burst. -- w
161 bool TreeSocket::Outbound_Reply_Server(CommandBase::Params& params)
163 const Link* x = AuthRemote(params);
167 * They're in WAIT_AUTH_2 (having accepted our credentials).
168 * Set our state to CONNECTED (since everything's peachy so far) and send our
169 * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
171 * While we're at it, create a treeserver object so we know about them.
174 FinishAuth(params[0], params[3], params.back(), x->Hidden);
182 bool TreeSocket::CheckDuplicate(const std::string& sname, const std::string& sid)
184 // Check if the server name is not in use by a server that's already fully connected
185 TreeServer* CheckDupe = Utils->FindServer(sname);
188 std::string pname = CheckDupe->GetParent() ? CheckDupe->GetParent()->GetName() : "<ourself>";
189 SendError("Server "+sname+" already exists on server "+pname+"!");
190 ServerInstance->SNO->WriteToSnoMask('l', "Server connection from \002"+sname+"\002 denied, already exists on server "+pname);
194 // Check if the id is not in use by a server that's already fully connected
195 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Looking for dupe SID %s", sid.c_str());
196 CheckDupe = Utils->FindServerID(sid);
200 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.");
201 ServerInstance->SNO->WriteToSnoMask('l', "Server connection from \002"+sname+"\002 denied, server ID '"+CheckDupe->GetId()+
202 "' already exists on server "+CheckDupe->GetName());
210 * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
213 bool TreeSocket::Inbound_Server(CommandBase::Params& params)
215 const Link* x = AuthRemote(params);
218 // Save these for later, so when they accept our credentials (indicated by BURST) we remember them
219 this->capab->hidden = x->Hidden;
220 this->capab->sid = params[3];
221 this->capab->description = params.back();
222 this->capab->name = params[0];
224 // Send our details: Our server name and description and hopcount of 0,
225 // along with the sendpass from this block.
226 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
228 // move to the next state, we are now waiting for THEM.
229 this->LinkState = WAIT_AUTH_2;
236 CommandServer::Builder::Builder(TreeServer* server)
237 : CmdBuilder(server->GetParent(), "SERVER")
239 push(server->GetName());
240 push(server->GetId());
241 if (server->IsBursting())
242 push_property("burst", ConvToStr(server->StartBurst));
243 push_property("hidden", ConvToStr(server->Hidden));
244 push_last(server->GetDesc());