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