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