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