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