2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2014, 2017-2019 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 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
203 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
204 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
205 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
206 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
207 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
208 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
209 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
210 " MAXREAL="+ConvToStr(ServerInstance->Config->Limits.MaxReal)+
211 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
212 " MAXHOST="+ConvToStr(ServerInstance->Config->Limits.MaxHost)+
214 " CASEMAPPING="+ServerInstance->Config->CaseMapping+
215 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
216 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
217 // in 2.0, we advertise it here to not break linking to previous versions.
218 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
219 // to 1201 protocol servers irrespectively of its module flags.
220 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
223 this->WriteLine("CAPAB END");
226 /* Isolate and return the elements that are different between two comma seperated lists */
227 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
228 std::string& mleft, std::string& mright)
230 std::set<std::string> values;
231 irc::sepstream sepleft(one, sep);
232 irc::sepstream sepright(two, sep);
234 while (sepleft.GetToken(item))
238 while (sepright.GetToken(item))
240 if (!values.erase(item))
242 mright.push_back(sep);
246 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
248 mleft.push_back(sep);
253 bool TreeSocket::Capab(const CommandBase::Params& params)
255 if (params.size() < 1)
257 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
260 if (params[0] == "START")
262 capab->ModuleList.clear();
263 capab->OptModuleList.clear();
264 capab->CapKeys.clear();
265 if (params.size() > 1)
266 proto_version = ConvToNum<unsigned int>(params[1]);
268 if (proto_version < PROTO_OLDEST)
270 SendError("CAPAB negotiation failed: Server is using protocol version "
271 + (proto_version ? ConvToStr(proto_version) : "1201 or older")
272 + " which is too old to link with this server (protocol versions "
273 + ConvToStr(PROTO_OLDEST) + " to " + ConvToStr(PROTO_NEWEST) + " are supported)");
277 // We don't support the 2.1 protocol.
278 if (proto_version == PROTO_INSPIRCD_21_A0 || proto_version == PROTO_INSPIRCD_21_B2)
280 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
286 else if (params[0] == "END")
289 /* Compare ModuleList and check CapKeys */
290 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
292 std::string diffIneed, diffUneed;
293 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
294 if (diffIneed.length() || diffUneed.length())
296 reason = "Modules incorrectly matched on these servers.";
297 if (diffIneed.length())
298 reason += " Not loaded here:" + diffIneed;
299 if (diffUneed.length())
300 reason += " Not loaded there:" + diffUneed;
301 this->SendError("CAPAB negotiation failed: "+reason);
306 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
308 std::string diffIneed, diffUneed;
309 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
310 if (diffIneed.length() || diffUneed.length())
312 if (Utils->AllowOptCommon)
314 ServerInstance->SNO->WriteToSnoMask('l',
315 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
316 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
317 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
321 reason = "Optional modules incorrectly matched on these servers and <options:allowmismatch> is not enabled.";
322 if (diffIneed.length())
323 reason += " Not loaded here:" + diffIneed;
324 if (diffUneed.length())
325 reason += " Not loaded there:" + diffUneed;
326 this->SendError("CAPAB negotiation failed: "+reason);
332 if (!capab->ChanModes.empty())
334 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
336 std::string diffIneed, diffUneed;
337 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
338 if (diffIneed.length() || diffUneed.length())
340 reason = "Channel modes not matched on these servers.";
341 if (diffIneed.length())
342 reason += " Not loaded here:" + diffIneed;
343 if (diffUneed.length())
344 reason += " Not loaded there:" + diffUneed;
348 else if (proto_version == PROTO_INSPIRCD_20)
350 if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
352 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
353 reason = "One or more of the channel modes on the remote server are invalid on this server.";
356 else if (this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end())
358 if (this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
359 reason = "One or more of the prefixes on the remote server are invalid on this server.";
365 this->SendError("CAPAB negotiation failed: " + reason);
369 if (!capab->UserModes.empty())
371 if (capab->UserModes != BuildModeList(MODETYPE_USER))
373 std::string diffIneed, diffUneed;
374 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
375 if (diffIneed.length() || diffUneed.length())
377 reason = "User modes not matched on these servers.";
378 if (diffIneed.length())
379 reason += " Not loaded here:" + diffIneed;
380 if (diffUneed.length())
381 reason += " Not loaded there:" + diffUneed;
385 else if (proto_version == PROTO_INSPIRCD_20 && this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
387 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
388 reason = "One or more of the user modes on the remote server are invalid on this server.";
393 this->SendError("CAPAB negotiation failed: " + reason);
397 if (this->capab->CapKeys.find("CASEMAPPING") != this->capab->CapKeys.end())
399 const std::string casemapping = this->capab->CapKeys.find("CASEMAPPING")->second;
400 if (casemapping != ServerInstance->Config->CaseMapping)
402 reason = "The casemapping of the remote server differs to that of the local server."
403 " Local casemapping: " + ServerInstance->Config->CaseMapping +
404 " Remote casemapping: " + casemapping;
405 this->SendError("CAPAB negotiation failed: " + reason);
410 /* Challenge response, store their challenge for our password */
411 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
412 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
414 /* Challenge-response is on now */
415 this->SetTheirChallenge(n->second);
416 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
418 this->SendCapabilities(2);
419 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
424 // They didn't specify a challenge or we don't have sha256, we use plaintext
425 if (this->LinkState == CONNECTING)
427 this->SendCapabilities(2);
428 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
432 else if ((params[0] == "MODULES") && (params.size() == 2))
434 if (!capab->ModuleList.length())
436 capab->ModuleList = params[1];
440 capab->ModuleList.push_back(' ');
441 capab->ModuleList.append(params[1]);
444 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
446 if (!capab->OptModuleList.length())
448 capab->OptModuleList = params[1];
452 capab->OptModuleList.push_back(' ');
453 capab->OptModuleList.append(params[1]);
456 else if ((params[0] == "CHANMODES") && (params.size() == 2))
458 capab->ChanModes = params[1];
460 else if ((params[0] == "USERMODES") && (params.size() == 2))
462 capab->UserModes = params[1];
464 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
466 irc::spacesepstream capabs(params[1]);
468 while (capabs.GetToken(item))
470 /* Process each key/value pair */
471 std::string::size_type equals = item.find('=');
472 if (equals != std::string::npos)
474 std::string var(item, 0, equals);
475 std::string value(item, equals+1);
476 capab->CapKeys[var] = value;