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