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