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