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