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