]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
Fixes by misspell-fixer
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / capab.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
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
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
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
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         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)+
213                         extra+
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")
221                         );
222
223         this->WriteLine("CAPAB END");
224 }
225
226 /* Isolate and return the elements that are different between two comma separated lists */
227 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
228                 std::string& mleft, std::string& mright)
229 {
230         std::set<std::string> values;
231         irc::sepstream sepleft(one, sep);
232         irc::sepstream sepright(two, sep);
233         std::string item;
234         while (sepleft.GetToken(item))
235         {
236                 values.insert(item);
237         }
238         while (sepright.GetToken(item))
239         {
240                 if (!values.erase(item))
241                 {
242                         mright.push_back(sep);
243                         mright.append(item);
244                 }
245         }
246         for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
247         {
248                 mleft.push_back(sep);
249                 mleft.append(*i);
250         }
251 }
252
253 bool TreeSocket::Capab(const CommandBase::Params& params)
254 {
255         if (params.size() < 1)
256         {
257                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
258                 return false;
259         }
260         if (params[0] == "START")
261         {
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]);
267
268                 if (proto_version < PROTO_OLDEST)
269                 {
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)");
274                         return false;
275                 }
276
277                 // We don't support the 2.1 protocol.
278                 if (proto_version == PROTO_INSPIRCD_21_A0 || proto_version == PROTO_INSPIRCD_21_B2)
279                 {
280                         SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
281                         return false;
282                 }
283
284                 SendCapabilities(2);
285         }
286         else if (params[0] == "END")
287         {
288                 std::string reason;
289                 /* Compare ModuleList and check CapKeys */
290                 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
291                 {
292                         std::string diffIneed, diffUneed;
293                         ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
294                         if (diffIneed.length() || diffUneed.length())
295                         {
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);
302                                 return false;
303                         }
304                 }
305
306                 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
307                 {
308                         std::string diffIneed, diffUneed;
309                         ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
310                         if (diffIneed.length() || diffUneed.length())
311                         {
312                                 if (Utils->AllowOptCommon)
313                                 {
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());
318                                 }
319                                 else
320                                 {
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);
327                                         return false;
328                                 }
329                         }
330                 }
331
332                 if (!capab->ChanModes.empty())
333                 {
334                         if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
335                         {
336                                 std::string diffIneed, diffUneed;
337                                 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
338                                 if (diffIneed.length() || diffUneed.length())
339                                 {
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;
345                                 }
346                         }
347                 }
348                 else if (proto_version == PROTO_INSPIRCD_20)
349                 {
350                         if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
351                         {
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.";
354                         }
355
356                         else if (this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end())
357                         {
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.";
360                         }
361                 }
362
363                 if (!reason.empty())
364                 {
365                         this->SendError("CAPAB negotiation failed: " + reason);
366                         return false;
367                 }
368
369                 if (!capab->UserModes.empty())
370                 {
371                         if (capab->UserModes != BuildModeList(MODETYPE_USER))
372                         {
373                                 std::string diffIneed, diffUneed;
374                                 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
375                                 if (diffIneed.length() || diffUneed.length())
376                                 {
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;
382                                 }
383                         }
384                 }
385                 else if (proto_version == PROTO_INSPIRCD_20 && this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
386                 {
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.";
389                 }
390
391                 if (!reason.empty())
392                 {
393                         this->SendError("CAPAB negotiation failed: " + reason);
394                         return false;
395                 }
396
397                 if (this->capab->CapKeys.find("CASEMAPPING") != this->capab->CapKeys.end())
398                 {
399                         const std::string casemapping = this->capab->CapKeys.find("CASEMAPPING")->second;
400                         if (casemapping != ServerInstance->Config->CaseMapping)
401                         {
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);
406                                 return false;
407                         }
408                 }
409
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")))
413                 {
414                         /* Challenge-response is on now */
415                         this->SetTheirChallenge(n->second);
416                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
417                         {
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);
420                         }
421                 }
422                 else
423                 {
424                         // They didn't specify a challenge or we don't have sha256, we use plaintext
425                         if (this->LinkState == CONNECTING)
426                         {
427                                 this->SendCapabilities(2);
428                                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
429                         }
430                 }
431         }
432         else if ((params[0] == "MODULES") && (params.size() == 2))
433         {
434                 if (!capab->ModuleList.length())
435                 {
436                         capab->ModuleList = params[1];
437                 }
438                 else
439                 {
440                         capab->ModuleList.push_back(' ');
441                         capab->ModuleList.append(params[1]);
442                 }
443         }
444         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
445         {
446                 if (!capab->OptModuleList.length())
447                 {
448                         capab->OptModuleList = params[1];
449                 }
450                 else
451                 {
452                         capab->OptModuleList.push_back(' ');
453                         capab->OptModuleList.append(params[1]);
454                 }
455         }
456         else if ((params[0] == "CHANMODES") && (params.size() == 2))
457         {
458                 capab->ChanModes = params[1];
459         }
460         else if ((params[0] == "USERMODES") && (params.size() == 2))
461         {
462                 capab->UserModes = params[1];
463         }
464         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
465         {
466                 irc::spacesepstream capabs(params[1]);
467                 std::string item;
468                 while (capabs.GetToken(item))
469                 {
470                         /* Process each key/value pair */
471                         std::string::size_type equals = item.find('=');
472                         if (equals != std::string::npos)
473                         {
474                                 std::string var(item, 0, equals);
475                                 std::string value(item, equals+1);
476                                 capab->CapKeys[var] = value;
477                         }
478                 }
479         }
480         return true;
481 }