1 /*
2 * InspIRCd -- Internet Relay Chat Daemon
3 *
4 * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2014, 2017-2021 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org>
7 * Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
8 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
9 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10 * Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
11 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
12 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
13 *
14 * This file is part of InspIRCd. InspIRCd is free software: you can
15 * redistribute it and/or modify it under the terms of the GNU General Public
16 * License as published by the Free Software Foundation, version 2.
17 *
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27
28 #include "inspircd.h"
29
30 #include "treeserver.h"
31 #include "utils.h"
32 #include "link.h"
33 #include "main.h"
34
35 struct CompatMod
36 {
37 const char* name;
38 ModuleFlags listflag;
39 };
40
41 static CompatMod compatmods[] =
42 {
43 { "m_watch.so", VF_OPTCOMMON }
44 };
45
MyModules(int filter)46 std::string TreeSocket::MyModules(int filter)
47 {
48 const ModuleManager::ModuleMap& modlist = ServerInstance->Modules->GetModules();
49
50 std::string capabilities;
51 for (ModuleManager::ModuleMap::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
52 {
53 Module* const mod = i->second;
54 // 3.0 advertises its settings for the benefit of services
55 // 2.0 would bork on this
56 if (proto_version < PROTO_INSPIRCD_30 && mod->ModuleSourceFile == "m_kicknorejoin.so")
57 continue;
58
59 bool do_compat_include = false;
60 if (proto_version < PROTO_INSPIRCD_30)
61 {
62 for (size_t j = 0; j < sizeof(compatmods)/sizeof(compatmods[0]); j++)
63 {
64 if ((compatmods[j].listflag & filter) && (mod->ModuleSourceFile == compatmods[j].name))
65 {
66 do_compat_include = true;
67 break;
68 }
69 }
70 }
71
72 Version v = mod->GetVersion();
73 if ((!do_compat_include) && (!(v.Flags & filter)))
74 continue;
75
76 capabilities.push_back(' ');
77 capabilities.append(i->first);
78 if (!v.link_data.empty())
79 {
80 capabilities.push_back('=');
81 capabilities.append(v.link_data);
82 }
83 }
84
85 // If we are linked in a 2.0 server and have an ascii casemapping
86 // advertise it as m_ascii.so from inspircd-extras
87 if ((filter & VF_COMMON) && ServerInstance->Config->CaseMapping == "ascii" && proto_version == PROTO_INSPIRCD_20)
88 capabilities.append(" m_ascii.so");
89
90 if (capabilities.empty())
91 return capabilities;
92
93 return capabilities.substr(1);
94 }
95
BuildModeList(ModeType mtype)96 std::string TreeSocket::BuildModeList(ModeType mtype)
97 {
98 std::vector<std::string> modes;
99 const ModeParser::ModeHandlerMap& mhs = ServerInstance->Modes.GetModes(mtype);
100 for (ModeParser::ModeHandlerMap::const_iterator i = mhs.begin(); i != mhs.end(); ++i)
101 {
102 const ModeHandler* const mh = i->second;
103 const PrefixMode* const pm = mh->IsPrefixMode();
104 std::string mdesc;
105 if (proto_version >= PROTO_INSPIRCD_30)
106 {
107 if (pm)
108 mdesc.append("prefix:").append(ConvToStr(pm->GetPrefixRank())).push_back(':');
109 else if (mh->IsListMode())
110 mdesc.append("list:");
111 else if (mh->NeedsParam(true))
112 mdesc.append(mh->NeedsParam(false) ? "param:" : "param-set:");
113 else
114 mdesc.append("simple:");
115 }
116 mdesc.append(mh->name);
117 mdesc.push_back('=');
118 if (pm)
119 {
120 if (pm->GetPrefix())
121 mdesc.push_back(pm->GetPrefix());
122 }
123 mdesc.push_back(mh->GetModeChar());
124 modes.push_back(mdesc);
125 }
126 std::sort(modes.begin(), modes.end());
127 return stdalgo::string::join(modes);
128 }
129
SendCapabilities(int phase)130 void TreeSocket::SendCapabilities(int phase)
131 {
132 if (capab->capab_phase >= phase)
133 return;
134
135 if (capab->capab_phase < 1 && phase >= 1)
136 WriteLine("CAPAB START " + ConvToStr(PROTO_NEWEST));
137
138 capab->capab_phase = phase;
139 if (phase < 2)
140 return;
141
142 const char sep = ' ';
143 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
144 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
145 /* Send module names, split at 509 length */
146 std::string item;
147 std::string line = "CAPAB MODULES :";
148 while (modulelist.GetToken(item))
149 {
150 if (line.length() + item.length() + 1 > 509)
151 {
152 this->WriteLine(line);
153 line = "CAPAB MODULES :";
154 }
155
156 if (line != "CAPAB MODULES :")
157 line.push_back(sep);
158
159 line.append(item);
160 }
161 if (line != "CAPAB MODULES :")
162 this->WriteLine(line);
163
164 line = "CAPAB MODSUPPORT :";
165 while (optmodulelist.GetToken(item))
166 {
167 if (line.length() + item.length() + 1 > 509)
168 {
169 this->WriteLine(line);
170 line = "CAPAB MODSUPPORT :";
171 }
172
173 if (line != "CAPAB MODSUPPORT :")
174 line.push_back(sep);
175
176 line.append(item);
177 }
178 if (line != "CAPAB MODSUPPORT :")
179 this->WriteLine(line);
180
181 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
182 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
183
184 std::string extra;
185 /* Do we have sha256 available? If so, we send a challenge */
186 if (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256"))
187 {
188 SetOurChallenge(ServerInstance->GenRandomStr(20));
189 extra = " CHALLENGE=" + this->GetOurChallenge();
190 }
191
192 // 2.0 needs these keys.
193 if (proto_version == PROTO_INSPIRCD_20)
194 {
195 extra.append(" PROTOCOL="+ConvToStr(proto_version))
196 .append(" MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxReal))
197 .append(" CHANMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
198 .append(" USERMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_USER))
199 .append(" PREFIX="+ ServerInstance->Modes->BuildPrefixes());
200 }
201
202 // HACK: Allow services to know what extbans exist. This will be
203 // replaced by CAPAB EXTBANS in the next protocol version.
204 std::map<std::string, std::string> tokens;
205 FOREACH_MOD(On005Numeric, (tokens));
206 std::map<std::string, std::string>::const_iterator eiter = tokens.find("EXTBAN");
207 if (eiter != tokens.end())
208 extra.append(" EXTBANS=" + eiter->second);
209
210 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
211 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
212 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
213 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
214 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
215 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
216 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
217 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
218 " MAXREAL="+ConvToStr(ServerInstance->Config->Limits.MaxReal)+
219 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
220 " MAXHOST="+ConvToStr(ServerInstance->Config->Limits.MaxHost)+
221 " MAXLINE="+ConvToStr(ServerInstance->Config->Limits.MaxLine)+
222 extra+
223 " CASEMAPPING="+ServerInstance->Config->CaseMapping+
224 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
225 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
226 // in 2.0, we advertise it here to not break linking to previous versions.
227 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
228 // to 1201 protocol servers irrespectively of its module flags.
229 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
230 );
231
232 this->WriteLine("CAPAB END");
233 }
234
235 /* Isolate and return the elements that are different between two comma separated lists */
ListDifference(const std::string & one,const std::string & two,char sep,std::string & mleft,std::string & mright)236 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
237 std::string& mleft, std::string& mright)
238 {
239 std::set<std::string> values;
240 irc::sepstream sepleft(one, sep);
241 irc::sepstream sepright(two, sep);
242 std::string item;
243 while (sepleft.GetToken(item))
244 {
245 values.insert(item);
246 }
247 while (sepright.GetToken(item))
248 {
249 if (!values.erase(item))
250 {
251 mright.push_back(sep);
252 mright.append(item);
253 }
254 }
255 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
256 {
257 mleft.push_back(sep);
258 mleft.append(*i);
259 }
260 }
261
Capab(const CommandBase::Params & params)262 bool TreeSocket::Capab(const CommandBase::Params& params)
263 {
264 if (params.size() < 1)
265 {
266 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
267 return false;
268 }
269 if (params[0] == "START")
270 {
271 capab->ModuleList.clear();
272 capab->OptModuleList.clear();
273 capab->CapKeys.clear();
274 if (params.size() > 1)
275 proto_version = ConvToNum<unsigned int>(params[1]);
276
277 if (proto_version < PROTO_OLDEST)
278 {
279 SendError("CAPAB negotiation failed: Server is using protocol version "
280 + (proto_version ? ConvToStr(proto_version) : "1201 or older")
281 + " which is too old to link with this server (protocol versions "
282 + ConvToStr(PROTO_OLDEST) + " to " + ConvToStr(PROTO_NEWEST) + " are supported)");
283 return false;
284 }
285
286 // We don't support the 2.1 protocol.
287 if (proto_version == PROTO_INSPIRCD_21_A0 || proto_version == PROTO_INSPIRCD_21_B2)
288 {
289 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
290 return false;
291 }
292
293 SendCapabilities(2);
294 }
295 else if (params[0] == "END")
296 {
297 std::string reason;
298 /* Compare ModuleList and check CapKeys */
299 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
300 {
301 std::string diffIneed, diffUneed;
302 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
303 if (diffIneed.length() || diffUneed.length())
304 {
305 reason = "Modules incorrectly matched on these servers.";
306 if (diffIneed.length())
307 reason += " Not loaded here:" + diffIneed;
308 if (diffUneed.length())
309 reason += " Not loaded there:" + diffUneed;
310 this->SendError("CAPAB negotiation failed: "+reason);
311 return false;
312 }
313 }
314
315 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
316 {
317 std::string diffIneed, diffUneed;
318 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
319 if (diffIneed.length() || diffUneed.length())
320 {
321 if (Utils->AllowOptCommon)
322 {
323 ServerInstance->SNO->WriteToSnoMask('l',
324 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
325 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
326 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
327 }
328 else
329 {
330 reason = "Optional modules incorrectly matched on these servers and <options:allowmismatch> is not enabled.";
331 if (diffIneed.length())
332 reason += " Not loaded here:" + diffIneed;
333 if (diffUneed.length())
334 reason += " Not loaded there:" + diffUneed;
335 this->SendError("CAPAB negotiation failed: "+reason);
336 return false;
337 }
338 }
339 }
340
341 if (!capab->ChanModes.empty())
342 {
343 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
344 {
345 std::string diffIneed, diffUneed;
346 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
347 if (diffIneed.length() || diffUneed.length())
348 {
349 reason = "Channel modes not matched on these servers.";
350 if (diffIneed.length())
351 reason += " Not loaded here:" + diffIneed;
352 if (diffUneed.length())
353 reason += " Not loaded there:" + diffUneed;
354 }
355 }
356 }
357 else if (proto_version == PROTO_INSPIRCD_20)
358 {
359 if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
360 {
361 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
362 reason = "One or more of the channel modes on the remote server are invalid on this server.";
363 }
364
365 else if (this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end())
366 {
367 if (this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
368 reason = "One or more of the prefixes on the remote server are invalid on this server.";
369 }
370 }
371
372 if (!reason.empty())
373 {
374 this->SendError("CAPAB negotiation failed: " + reason);
375 return false;
376 }
377
378 if (!capab->UserModes.empty())
379 {
380 if (capab->UserModes != BuildModeList(MODETYPE_USER))
381 {
382 std::string diffIneed, diffUneed;
383 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
384 if (diffIneed.length() || diffUneed.length())
385 {
386 reason = "User modes not matched on these servers.";
387 if (diffIneed.length())
388 reason += " Not loaded here:" + diffIneed;
389 if (diffUneed.length())
390 reason += " Not loaded there:" + diffUneed;
391 }
392 }
393 }
394 else if (proto_version == PROTO_INSPIRCD_20 && this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
395 {
396 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
397 reason = "One or more of the user modes on the remote server are invalid on this server.";
398 }
399
400 if (!reason.empty())
401 {
402 this->SendError("CAPAB negotiation failed: " + reason);
403 return false;
404 }
405
406 if (this->capab->CapKeys.find("CASEMAPPING") != this->capab->CapKeys.end())
407 {
408 const std::string casemapping = this->capab->CapKeys.find("CASEMAPPING")->second;
409 if (casemapping != ServerInstance->Config->CaseMapping)
410 {
411 reason = "The casemapping of the remote server differs to that of the local server."
412 " Local casemapping: " + ServerInstance->Config->CaseMapping +
413 " Remote casemapping: " + casemapping;
414 this->SendError("CAPAB negotiation failed: " + reason);
415 return false;
416 }
417 }
418
419 /* Challenge response, store their challenge for our password */
420 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
421 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
422 {
423 /* Challenge-response is on now */
424 this->SetTheirChallenge(n->second);
425 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
426 {
427 this->SendCapabilities(2);
428 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
429 }
430 }
431 else
432 {
433 // They didn't specify a challenge or we don't have sha256, we use plaintext
434 if (this->LinkState == CONNECTING)
435 {
436 this->SendCapabilities(2);
437 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
438 }
439 }
440 }
441 else if ((params[0] == "MODULES") && (params.size() == 2))
442 {
443 if (!capab->ModuleList.length())
444 {
445 capab->ModuleList = params[1];
446 }
447 else
448 {
449 capab->ModuleList.push_back(' ');
450 capab->ModuleList.append(params[1]);
451 }
452 }
453 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
454 {
455 if (!capab->OptModuleList.length())
456 {
457 capab->OptModuleList = params[1];
458 }
459 else
460 {
461 capab->OptModuleList.push_back(' ');
462 capab->OptModuleList.append(params[1]);
463 }
464 }
465 else if ((params[0] == "CHANMODES") && (params.size() == 2))
466 {
467 capab->ChanModes = params[1];
468 }
469 else if ((params[0] == "USERMODES") && (params.size() == 2))
470 {
471 capab->UserModes = params[1];
472 }
473 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
474 {
475 irc::spacesepstream capabs(params[1]);
476 std::string item;
477 while (capabs.GetToken(item))
478 {
479 /* Process each key/value pair */
480 std::string::size_type equals = item.find('=');
481 if (equals != std::string::npos)
482 {
483 std::string var(item, 0, equals);
484 std::string value(item, equals+1);
485 capab->CapKeys[var] = value;
486 }
487 }
488 }
489 return true;
490 }
491