]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
4286300e5a472c2dc9bd8c51b0871329538aef4b
[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         // 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                         extra+
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")
229                         );
230
231         this->WriteLine("CAPAB END");
232 }
233
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)
237 {
238         std::set<std::string> values;
239         irc::sepstream sepleft(one, sep);
240         irc::sepstream sepright(two, sep);
241         std::string item;
242         while (sepleft.GetToken(item))
243         {
244                 values.insert(item);
245         }
246         while (sepright.GetToken(item))
247         {
248                 if (!values.erase(item))
249                 {
250                         mright.push_back(sep);
251                         mright.append(item);
252                 }
253         }
254         for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
255         {
256                 mleft.push_back(sep);
257                 mleft.append(*i);
258         }
259 }
260
261 bool TreeSocket::Capab(const CommandBase::Params& params)
262 {
263         if (params.size() < 1)
264         {
265                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
266                 return false;
267         }
268         if (params[0] == "START")
269         {
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]);
275
276                 if (proto_version < PROTO_OLDEST)
277                 {
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)");
282                         return false;
283                 }
284
285                 // We don't support the 2.1 protocol.
286                 if (proto_version == PROTO_INSPIRCD_21_A0 || proto_version == PROTO_INSPIRCD_21_B2)
287                 {
288                         SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
289                         return false;
290                 }
291
292                 SendCapabilities(2);
293         }
294         else if (params[0] == "END")
295         {
296                 std::string reason;
297                 /* Compare ModuleList and check CapKeys */
298                 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
299                 {
300                         std::string diffIneed, diffUneed;
301                         ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
302                         if (diffIneed.length() || diffUneed.length())
303                         {
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);
310                                 return false;
311                         }
312                 }
313
314                 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
315                 {
316                         std::string diffIneed, diffUneed;
317                         ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
318                         if (diffIneed.length() || diffUneed.length())
319                         {
320                                 if (Utils->AllowOptCommon)
321                                 {
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());
326                                 }
327                                 else
328                                 {
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);
335                                         return false;
336                                 }
337                         }
338                 }
339
340                 if (!capab->ChanModes.empty())
341                 {
342                         if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
343                         {
344                                 std::string diffIneed, diffUneed;
345                                 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
346                                 if (diffIneed.length() || diffUneed.length())
347                                 {
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;
353                                 }
354                         }
355                 }
356                 else if (proto_version == PROTO_INSPIRCD_20)
357                 {
358                         if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
359                         {
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.";
362                         }
363
364                         else if (this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end())
365                         {
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.";
368                         }
369                 }
370
371                 if (!reason.empty())
372                 {
373                         this->SendError("CAPAB negotiation failed: " + reason);
374                         return false;
375                 }
376
377                 if (!capab->UserModes.empty())
378                 {
379                         if (capab->UserModes != BuildModeList(MODETYPE_USER))
380                         {
381                                 std::string diffIneed, diffUneed;
382                                 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
383                                 if (diffIneed.length() || diffUneed.length())
384                                 {
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;
390                                 }
391                         }
392                 }
393                 else if (proto_version == PROTO_INSPIRCD_20 && this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
394                 {
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.";
397                 }
398
399                 if (!reason.empty())
400                 {
401                         this->SendError("CAPAB negotiation failed: " + reason);
402                         return false;
403                 }
404
405                 if (this->capab->CapKeys.find("CASEMAPPING") != this->capab->CapKeys.end())
406                 {
407                         const std::string casemapping = this->capab->CapKeys.find("CASEMAPPING")->second;
408                         if (casemapping != ServerInstance->Config->CaseMapping)
409                         {
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);
414                                 return false;
415                         }
416                 }
417
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")))
421                 {
422                         /* Challenge-response is on now */
423                         this->SetTheirChallenge(n->second);
424                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
425                         {
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);
428                         }
429                 }
430                 else
431                 {
432                         // They didn't specify a challenge or we don't have sha256, we use plaintext
433                         if (this->LinkState == CONNECTING)
434                         {
435                                 this->SendCapabilities(2);
436                                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
437                         }
438                 }
439         }
440         else if ((params[0] == "MODULES") && (params.size() == 2))
441         {
442                 if (!capab->ModuleList.length())
443                 {
444                         capab->ModuleList = params[1];
445                 }
446                 else
447                 {
448                         capab->ModuleList.push_back(' ');
449                         capab->ModuleList.append(params[1]);
450                 }
451         }
452         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
453         {
454                 if (!capab->OptModuleList.length())
455                 {
456                         capab->OptModuleList = params[1];
457                 }
458                 else
459                 {
460                         capab->OptModuleList.push_back(' ');
461                         capab->OptModuleList.append(params[1]);
462                 }
463         }
464         else if ((params[0] == "CHANMODES") && (params.size() == 2))
465         {
466                 capab->ChanModes = params[1];
467         }
468         else if ((params[0] == "USERMODES") && (params.size() == 2))
469         {
470                 capab->UserModes = params[1];
471         }
472         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
473         {
474                 irc::spacesepstream capabs(params[1]);
475                 std::string item;
476                 while (capabs.GetToken(item))
477                 {
478                         /* Process each key/value pair */
479                         std::string::size_type equals = item.find('=');
480                         if (equals != std::string::npos)
481                         {
482                                 std::string var(item, 0, equals);
483                                 std::string value(item, equals+1);
484                                 capab->CapKeys[var] = value;
485                         }
486                 }
487         }
488         return true;
489 }