2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2014, 2017-2020 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>
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.
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
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/>.
30 #include "treeserver.h"
41 static CompatMod compatmods[] =
43 { "m_watch.so", VF_OPTCOMMON }
46 std::string TreeSocket::MyModules(int filter)
48 const ModuleManager::ModuleMap& modlist = ServerInstance->Modules->GetModules();
50 std::string capabilities;
51 for (ModuleManager::ModuleMap::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
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")
59 bool do_compat_include = false;
60 if (proto_version < PROTO_INSPIRCD_30)
62 for (size_t j = 0; j < sizeof(compatmods)/sizeof(compatmods[0]); j++)
64 if ((compatmods[j].listflag & filter) && (mod->ModuleSourceFile == compatmods[j].name))
66 do_compat_include = true;
72 Version v = mod->GetVersion();
73 if ((!do_compat_include) && (!(v.Flags & filter)))
76 capabilities.push_back(' ');
77 capabilities.append(i->first);
78 if (!v.link_data.empty())
80 capabilities.push_back('=');
81 capabilities.append(v.link_data);
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");
90 if (capabilities.empty())
93 return capabilities.substr(1);
96 std::string TreeSocket::BuildModeList(ModeType mtype)
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)
102 const ModeHandler* const mh = i->second;
103 const PrefixMode* const pm = mh->IsPrefixMode();
105 if (proto_version >= PROTO_INSPIRCD_30)
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:");
114 mdesc.append("simple:");
116 mdesc.append(mh->name);
117 mdesc.push_back('=');
121 mdesc.push_back(pm->GetPrefix());
123 mdesc.push_back(mh->GetModeChar());
124 modes.push_back(mdesc);
126 std::sort(modes.begin(), modes.end());
127 return stdalgo::string::join(modes);
130 void TreeSocket::SendCapabilities(int phase)
132 if (capab->capab_phase >= phase)
135 if (capab->capab_phase < 1 && phase >= 1)
136 WriteLine("CAPAB START " + ConvToStr(PROTO_NEWEST));
138 capab->capab_phase = phase;
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 */
147 std::string line = "CAPAB MODULES :";
148 while (modulelist.GetToken(item))
150 if (line.length() + item.length() + 1 > 509)
152 this->WriteLine(line);
153 line = "CAPAB MODULES :";
156 if (line != "CAPAB MODULES :")
161 if (line != "CAPAB MODULES :")
162 this->WriteLine(line);
164 line = "CAPAB MODSUPPORT :";
165 while (optmodulelist.GetToken(item))
167 if (line.length() + item.length() + 1 > 509)
169 this->WriteLine(line);
170 line = "CAPAB MODSUPPORT :";
173 if (line != "CAPAB MODSUPPORT :")
178 if (line != "CAPAB MODSUPPORT :")
179 this->WriteLine(line);
181 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
182 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
185 /* Do we have sha256 available? If so, we send a challenge */
186 if (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256"))
188 SetOurChallenge(ServerInstance->GenRandomStr(20));
189 extra = " CHALLENGE=" + this->GetOurChallenge();
192 // 2.0 needs these keys.
193 if (proto_version == PROTO_INSPIRCD_20)
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());
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);
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)+
222 " CASEMAPPING="+ServerInstance->Config->CaseMapping+
223 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
224 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
225 // in 2.0, we advertise it here to not break linking to previous versions.
226 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
227 // to 1201 protocol servers irrespectively of its module flags.
228 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
231 this->WriteLine("CAPAB END");
234 /* Isolate and return the elements that are different between two comma separated lists */
235 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
236 std::string& mleft, std::string& mright)
238 std::set<std::string> values;
239 irc::sepstream sepleft(one, sep);
240 irc::sepstream sepright(two, sep);
242 while (sepleft.GetToken(item))
246 while (sepright.GetToken(item))
248 if (!values.erase(item))
250 mright.push_back(sep);
254 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
256 mleft.push_back(sep);
261 bool TreeSocket::Capab(const CommandBase::Params& params)
263 if (params.size() < 1)
265 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
268 if (params[0] == "START")
270 capab->ModuleList.clear();
271 capab->OptModuleList.clear();
272 capab->CapKeys.clear();
273 if (params.size() > 1)
274 proto_version = ConvToNum<unsigned int>(params[1]);
276 if (proto_version < PROTO_OLDEST)
278 SendError("CAPAB negotiation failed: Server is using protocol version "
279 + (proto_version ? ConvToStr(proto_version) : "1201 or older")
280 + " which is too old to link with this server (protocol versions "
281 + ConvToStr(PROTO_OLDEST) + " to " + ConvToStr(PROTO_NEWEST) + " are supported)");
285 // We don't support the 2.1 protocol.
286 if (proto_version == PROTO_INSPIRCD_21_A0 || proto_version == PROTO_INSPIRCD_21_B2)
288 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
294 else if (params[0] == "END")
297 /* Compare ModuleList and check CapKeys */
298 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
300 std::string diffIneed, diffUneed;
301 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
302 if (diffIneed.length() || diffUneed.length())
304 reason = "Modules incorrectly matched on these servers.";
305 if (diffIneed.length())
306 reason += " Not loaded here:" + diffIneed;
307 if (diffUneed.length())
308 reason += " Not loaded there:" + diffUneed;
309 this->SendError("CAPAB negotiation failed: "+reason);
314 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
316 std::string diffIneed, diffUneed;
317 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
318 if (diffIneed.length() || diffUneed.length())
320 if (Utils->AllowOptCommon)
322 ServerInstance->SNO->WriteToSnoMask('l',
323 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
324 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
325 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
329 reason = "Optional modules incorrectly matched on these servers and <options:allowmismatch> is not enabled.";
330 if (diffIneed.length())
331 reason += " Not loaded here:" + diffIneed;
332 if (diffUneed.length())
333 reason += " Not loaded there:" + diffUneed;
334 this->SendError("CAPAB negotiation failed: "+reason);
340 if (!capab->ChanModes.empty())
342 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
344 std::string diffIneed, diffUneed;
345 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
346 if (diffIneed.length() || diffUneed.length())
348 reason = "Channel modes not matched on these servers.";
349 if (diffIneed.length())
350 reason += " Not loaded here:" + diffIneed;
351 if (diffUneed.length())
352 reason += " Not loaded there:" + diffUneed;
356 else if (proto_version == PROTO_INSPIRCD_20)
358 if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
360 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
361 reason = "One or more of the channel modes on the remote server are invalid on this server.";
364 else if (this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end())
366 if (this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
367 reason = "One or more of the prefixes on the remote server are invalid on this server.";
373 this->SendError("CAPAB negotiation failed: " + reason);
377 if (!capab->UserModes.empty())
379 if (capab->UserModes != BuildModeList(MODETYPE_USER))
381 std::string diffIneed, diffUneed;
382 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
383 if (diffIneed.length() || diffUneed.length())
385 reason = "User modes not matched on these servers.";
386 if (diffIneed.length())
387 reason += " Not loaded here:" + diffIneed;
388 if (diffUneed.length())
389 reason += " Not loaded there:" + diffUneed;
393 else if (proto_version == PROTO_INSPIRCD_20 && this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
395 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
396 reason = "One or more of the user modes on the remote server are invalid on this server.";
401 this->SendError("CAPAB negotiation failed: " + reason);
405 if (this->capab->CapKeys.find("CASEMAPPING") != this->capab->CapKeys.end())
407 const std::string casemapping = this->capab->CapKeys.find("CASEMAPPING")->second;
408 if (casemapping != ServerInstance->Config->CaseMapping)
410 reason = "The casemapping of the remote server differs to that of the local server."
411 " Local casemapping: " + ServerInstance->Config->CaseMapping +
412 " Remote casemapping: " + casemapping;
413 this->SendError("CAPAB negotiation failed: " + reason);
418 /* Challenge response, store their challenge for our password */
419 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
420 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
422 /* Challenge-response is on now */
423 this->SetTheirChallenge(n->second);
424 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
426 this->SendCapabilities(2);
427 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
432 // They didn't specify a challenge or we don't have sha256, we use plaintext
433 if (this->LinkState == CONNECTING)
435 this->SendCapabilities(2);
436 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
440 else if ((params[0] == "MODULES") && (params.size() == 2))
442 if (!capab->ModuleList.length())
444 capab->ModuleList = params[1];
448 capab->ModuleList.push_back(' ');
449 capab->ModuleList.append(params[1]);
452 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
454 if (!capab->OptModuleList.length())
456 capab->OptModuleList = params[1];
460 capab->OptModuleList.push_back(' ');
461 capab->OptModuleList.append(params[1]);
464 else if ((params[0] == "CHANMODES") && (params.size() == 2))
466 capab->ChanModes = params[1];
468 else if ((params[0] == "USERMODES") && (params.size() == 2))
470 capab->UserModes = params[1];
472 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
474 irc::spacesepstream capabs(params[1]);
476 while (capabs.GetToken(item))
478 /* Process each key/value pair */
479 std::string::size_type equals = item.find('=');
480 if (equals != std::string::npos)
482 std::string var(item, 0, equals);
483 std::string value(item, equals+1);
484 capab->CapKeys[var] = value;