]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/server.cpp
Use irc::equals instead of strcasecmp where appropriate.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / server.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2017-2019 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
145                 return x;
146         }
147
148         this->SendError("Mismatched server name or password (check the other server's snomask output for details - e.g. user mode +s +Ll)");
149         ServerInstance->SNO->WriteToSnoMask('l', "Server connection from \002"+sname+"\002 denied, invalid link credentials");
150         return NULL;
151 }
152
153 /*
154  * This is used after the other side of a connection has accepted our credentials.
155  * They are then introducing themselves to us, BEFORE either of us burst. -- w
156  */
157 bool TreeSocket::Outbound_Reply_Server(CommandBase::Params& params)
158 {
159         const Link* x = AuthRemote(params);
160         if (x)
161         {
162                 /*
163                  * They're in WAIT_AUTH_2 (having accepted our credentials).
164                  * Set our state to CONNECTED (since everything's peachy so far) and send our
165                  * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
166                  *
167                  * While we're at it, create a treeserver object so we know about them.
168                  *   -- w
169                  */
170                 FinishAuth(params[0], params[3], params.back(), x->Hidden);
171
172                 return true;
173         }
174
175         return false;
176 }
177
178 bool TreeSocket::CheckDuplicate(const std::string& sname, const std::string& sid)
179 {
180         // Check if the server name is not in use by a server that's already fully connected
181         TreeServer* CheckDupe = Utils->FindServer(sname);
182         if (CheckDupe)
183         {
184                 std::string pname = CheckDupe->GetParent() ? CheckDupe->GetParent()->GetName() : "<ourself>";
185                 SendError("Server "+sname+" already exists on server "+pname+"!");
186                 ServerInstance->SNO->WriteToSnoMask('l', "Server connection from \002"+sname+"\002 denied, already exists on server "+pname);
187                 return false;
188         }
189
190         // Check if the id is not in use by a server that's already fully connected
191         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Looking for dupe SID %s", sid.c_str());
192         CheckDupe = Utils->FindServerID(sid);
193
194         if (CheckDupe)
195         {
196                 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.");
197                 ServerInstance->SNO->WriteToSnoMask('l', "Server connection from \002"+sname+"\002 denied, server ID '"+CheckDupe->GetId()+
198                                 "' already exists on server "+CheckDupe->GetName());
199                 return false;
200         }
201
202         return true;
203 }
204
205 /*
206  * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
207  *              -- w
208  */
209 bool TreeSocket::Inbound_Server(CommandBase::Params& params)
210 {
211         const Link* x = AuthRemote(params);
212         if (x)
213         {
214                 // Save these for later, so when they accept our credentials (indicated by BURST) we remember them
215                 this->capab->hidden = x->Hidden;
216                 this->capab->sid = params[3];
217                 this->capab->description = params.back();
218                 this->capab->name = params[0];
219
220                 // Send our details: Our server name and description and hopcount of 0,
221                 // along with the sendpass from this block.
222                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
223
224                 // move to the next state, we are now waiting for THEM.
225                 this->LinkState = WAIT_AUTH_2;
226                 return true;
227         }
228
229         return false;
230 }
231
232 CommandServer::Builder::Builder(TreeServer* server)
233         : CmdBuilder(server->GetParent(), "SERVER")
234 {
235         push(server->GetName());
236         push(server->GetId());
237         if (server->IsBursting())
238                 push_property("burst", ConvToStr(server->StartBurst));
239         push_property("hidden", ConvToStr(server->Hidden));
240         push_last(server->GetDesc());
241 }