]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/server.cpp
Update copyright headers.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / server.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
11  *
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.
15  *
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
19  * details.
20  *
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/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "modules/ssl.h"
28
29 #include "main.h"
30 #include "utils.h"
31 #include "link.h"
32 #include "treeserver.h"
33 #include "treesocket.h"
34 #include "commands.h"
35
36 /*
37  * Some server somewhere in the network introducing another server.
38  *      -- w
39  */
40 CmdResult CommandServer::HandleServer(TreeServer* ParentOfThis, Params& params)
41 {
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();
46
47         if (!InspIRCd::IsSID(sid))
48         {
49                 socket->SendError("Invalid format server ID: "+sid+"!");
50                 return CMD_FAILURE;
51         }
52         TreeServer* CheckDupe = Utils->FindServer(servername);
53         if (CheckDupe)
54         {
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());
57                 return CMD_FAILURE;
58         }
59         CheckDupe = Utils->FindServer(sid);
60         if (CheckDupe)
61         {
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());
64                 return CMD_FAILURE;
65         }
66
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);
70
71         HandleExtra(Node, params);
72
73         ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
74         return CMD_SUCCESS;
75 }
76
77 void CommandServer::HandleExtra(TreeServer* newserver, Params& params)
78 {
79         for (CommandBase::Params::const_iterator i = params.begin() + 2; i != params.end() - 1; ++i)
80         {
81                 const std::string& prop = *i;
82                 std::string::size_type p = prop.find('=');
83
84                 std::string key = prop;
85                 std::string val;
86                 if (p != std::string::npos)
87                 {
88                         key.erase(p);
89                         val.assign(prop, p+1, std::string::npos);
90                 }
91
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);
96         }
97 }
98
99 Link* TreeSocket::AuthRemote(const CommandBase::Params& params)
100 {
101         if (params.size() < 5)
102         {
103                 SendError("Protocol error - Not enough parameters for SERVER command");
104                 return NULL;
105         }
106
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();
111
112         this->SendCapabilities(2);
113
114         if (!ServerInstance->IsSID(sid))
115         {
116                 this->SendError("Invalid format server ID: "+sid+"!");
117                 return NULL;
118         }
119
120         for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
121         {
122                 Link* x = *i;
123                 if (!InspIRCd::Match(sname, x->Name))
124                         continue;
125
126                 if (!ComparePass(*x, password))
127                 {
128                         ServerInstance->SNO->WriteToSnoMask('l', "Invalid password on link: %s", x->Name.c_str());
129                         continue;
130                 }
131
132                 if (!CheckDuplicate(sname, sid))
133                         return NULL;
134
135                 ServerInstance->SNO->WriteToSnoMask('l', "Verified server connection " + linkID + " ("+description+")");
136
137                 const SSLIOHook* const ssliohook = SSLIOHook::IsSSL(this);
138                 if (ssliohook)
139                 {
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());
143                 }
144                 else if (!irc::sockets::cidr_mask("127.0.0.0/8").match(capab->remotesa) && !irc::sockets::cidr_mask("::1/128").match(capab->remotesa))
145                 {
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());
147                 }
148
149                 return x;
150         }
151
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");
154         return NULL;
155 }
156
157 /*
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
160  */
161 bool TreeSocket::Outbound_Reply_Server(CommandBase::Params& params)
162 {
163         const Link* x = AuthRemote(params);
164         if (x)
165         {
166                 /*
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.
170                  *
171                  * While we're at it, create a treeserver object so we know about them.
172                  *   -- w
173                  */
174                 FinishAuth(params[0], params[3], params.back(), x->Hidden);
175
176                 return true;
177         }
178
179         return false;
180 }
181
182 bool TreeSocket::CheckDuplicate(const std::string& sname, const std::string& sid)
183 {
184         // Check if the server name is not in use by a server that's already fully connected
185         TreeServer* CheckDupe = Utils->FindServer(sname);
186         if (CheckDupe)
187         {
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);
191                 return false;
192         }
193
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);
197
198         if (CheckDupe)
199         {
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());
203                 return false;
204         }
205
206         return true;
207 }
208
209 /*
210  * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
211  *              -- w
212  */
213 bool TreeSocket::Inbound_Server(CommandBase::Params& params)
214 {
215         const Link* x = AuthRemote(params);
216         if (x)
217         {
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];
223
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);
227
228                 // move to the next state, we are now waiting for THEM.
229                 this->LinkState = WAIT_AUTH_2;
230                 return true;
231         }
232
233         return false;
234 }
235
236 CommandServer::Builder::Builder(TreeServer* server)
237         : CmdBuilder(server->GetParent(), "SERVER")
238 {
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());
245 }