]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
7ebee4bda6eba72adea98de74118ecc924ecb391
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / capab.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "xline.h"
16
17 #include "treesocket.h"
18 #include "treeserver.h"
19 #include "utils.h"
20 #include "main.h"
21
22 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
23
24 std::string TreeSocket::MyModules(int filter)
25 {
26         std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
27
28         if (filter == VF_COMMON && proto_version != ProtocolVersion)
29                 CompatAddModules(modlist);
30
31         std::string capabilities;
32         sort(modlist.begin(),modlist.end());
33         for (unsigned int i = 0; i < modlist.size(); i++)
34         {
35                 if (i)
36                         capabilities.push_back(',');
37                 capabilities.append(modlist[i]);
38                 Module* m = ServerInstance->Modules->Find(modlist[i]);
39                 if (m && proto_version >= 1202)
40                 {
41                         Version v = m->GetVersion();
42                         if (!v.link_data.empty())
43                         {
44                                 capabilities.push_back('=');
45                                 capabilities.append(v.link_data);
46                         }
47                 }
48         }
49         return capabilities;
50 }
51
52 static std::string BuildModeList(ModeType type)
53 {
54         std::string line;
55         for(char c='A'; c <= 'z'; c++)
56         {
57                 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
58                 if (mh)
59                 {
60                         if (!line.empty())
61                                 line.push_back(',');
62                         line.append(mh->name);
63                         line.push_back('=');
64                         if (mh->GetPrefix())
65                                 line.push_back(mh->GetPrefix());
66                         line.push_back(c);
67                 }
68         }
69         return line;
70 }
71
72 void TreeSocket::SendCapabilities(int phase)
73 {
74         if (capab->capab_phase >= phase)
75                 return;
76
77         if (capab->capab_phase < 1 && phase >= 1)
78                 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
79
80         capab->capab_phase = phase;
81         if (phase < 2)
82                 return;
83
84         irc::commasepstream modulelist(MyModules(VF_COMMON));
85         irc::commasepstream optmodulelist(MyModules(VF_OPTCOMMON));
86         /* Send module names, split at 509 length */
87         std::string item;
88         std::string line = "CAPAB MODULES ";
89         while (modulelist.GetToken(item))
90         {
91                 if (line.length() + item.length() + 1 > 509)
92                 {
93                         this->WriteLine(line);
94                         line = "CAPAB MODULES ";
95                 }
96
97                 if (line != "CAPAB MODULES ")
98                         line.append(",");
99
100                 line.append(item);
101         }
102         if (line != "CAPAB MODULES ")
103                 this->WriteLine(line);
104
105         line = "CAPAB MODSUPPORT ";
106         while (optmodulelist.GetToken(item))
107         {
108                 if (line.length() + item.length() + 1 > 509)
109                 {
110                         this->WriteLine(line);
111                         line = "CAPAB MODSUPPORT ";
112                 }
113
114                 if (line != "CAPAB MODSUPPORT ")
115                         line.append(",");
116
117                 line.append(item);
118         }
119         if (line != "CAPAB MODSUPPORT ")
120                 this->WriteLine(line);
121
122         line = "CAPAB CHANMODES " + BuildModeList(MODETYPE_CHANNEL);
123         if (line != "CAPAB CHANMODES ")
124                 this->WriteLine(line);
125
126         line = "CAPAB USERMODES " + BuildModeList(MODETYPE_USER);
127         if (line != "CAPAB USERMODES ")
128                 this->WriteLine(line);
129
130         int ip6 = 0;
131 #ifdef IPV6
132         ip6 = 1;
133 #endif
134         std::string extra;
135         /* Do we have sha256 available? If so, we send a challenge */
136         if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
137         {
138                 this->SetOurChallenge(RandString(20));
139                 extra = " CHALLENGE=" + this->GetOurChallenge();
140         }
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                         " IP6NATIVE="+ConvToStr(ip6)+
153                         " IP6SUPPORT=1"+
154                         " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
155                         " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
156                         " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
157                         " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
158                         " SVSPART=1");
159
160         this->WriteLine("CAPAB END");
161 }
162
163 /* Check a comma seperated list for an item */
164 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
165 {
166         irc::commasepstream seplist(list);
167         std::string item2;
168
169         while (seplist.GetToken(item2))
170         {
171                 if (item2 == item)
172                         return true;
173         }
174         return false;
175 }
176
177 /* Isolate and return the elements that are different between two comma seperated lists */
178 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
179 {
180         irc::commasepstream list_one(one);
181         std::string item;
182         std::string result;
183         while (list_one.GetToken(item))
184         {
185                 if (!HasItem(two, item))
186                 {
187                         result.append(" ");
188                         result.append(item);
189                 }
190         }
191         return result;
192 }
193
194 bool TreeSocket::Capab(const parameterlist &params)
195 {
196         if (params.size() < 1)
197         {
198                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
199                 return false;
200         }
201         if (params[0] == "START")
202         {
203                 capab->ModuleList.clear();
204                 capab->OptModuleList.clear();
205                 capab->CapKeys.clear();
206                 if (params.size() > 1)
207                         proto_version = atoi(params[1].c_str());
208                 SendCapabilities(2);
209         }
210         else if (params[0] == "END")
211         {
212                 std::string reason;
213                 /* Compare ModuleList and check CapKeys */
214                 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
215                 {
216                         std::string diffIneed = ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON));
217                         std::string diffUneed = ListDifference(this->MyModules(VF_COMMON), this->capab->ModuleList);
218                         if (diffIneed.length() == 0 && diffUneed.length() == 0)
219                         {
220                                 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
221                         }
222                         else
223                         {
224                                 reason = "Modules incorrectly matched on these servers.";
225                                 if (diffIneed.length())
226                                         reason += " Not loaded here:" + diffIneed;
227                                 if (diffUneed.length())
228                                         reason += " Not loaded there:" + diffUneed;
229                         }
230                         this->SendError("CAPAB negotiation failed: "+reason);
231                         return false;
232                 }
233                 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
234                 {
235                         std::string diffIneed = ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON));
236                         std::string diffUneed = ListDifference(this->MyModules(VF_OPTCOMMON), this->capab->OptModuleList);
237                         if (diffIneed.length() == 0 && diffUneed.length() == 0)
238                         {
239                                 reason = "Optional Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
240                         }
241                         else if (Utils->AllowOptCommon)
242                         {
243                                 ServerInstance->SNO->WriteToSnoMask('l',
244                                         "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
245                                         diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
246                                         diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
247                         }
248                         else
249                         {
250                                 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
251                                 if (diffIneed.length())
252                                         reason += " Not loaded here:" + diffIneed;
253                                 if (diffUneed.length())
254                                         reason += " Not loaded there:" + diffUneed;
255                                 this->SendError("CAPAB negotiation failed: "+reason);
256                                 return false;
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 = ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL));
283                                 std::string diffUneed = ListDifference(BuildModeList(MODETYPE_CHANNEL), capab->ChanModes);
284                                 reason = "Channel modes not matched on these servers.";
285                                 if (diffIneed.length())
286                                         reason += " Not loaded here:" + diffIneed;
287                                 if (diffUneed.length())
288                                         reason += " Not loaded there:" + diffUneed;
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(MASK_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 = ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER));
302                                 std::string diffUneed = ListDifference(BuildModeList(MODETYPE_USER), capab->UserModes);
303                                 reason = "User modes not matched on these servers.";
304                                 if (diffIneed.length())
305                                         reason += " Not loaded here:" + diffIneed;
306                                 if (diffUneed.length())
307                                         reason += " Not loaded there:" + diffUneed;
308                         }
309                 }
310                 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
311                 {
312                         if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
313                                 reason = "One or more of the user modes on the remote server are invalid on this server.";
314                 }
315
316                 /* Challenge response, store their challenge for our password */
317                 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
318                 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
319                 {
320                         /* Challenge-response is on now */
321                         this->SetTheirChallenge(n->second);
322                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
323                         {
324                                 this->SendCapabilities(2);
325                                 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(capab->OutboundPass, this->GetTheirChallenge())+" 0 "+
326                                                 ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
327                         }
328                 }
329                 else
330                 {
331                         /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
332                         if (this->LinkState == CONNECTING)
333                         {
334                                 this->SendCapabilities(2);
335                                 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+capab->OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
336                         }
337                 }
338
339                 if (reason.length())
340                 {
341                         this->SendError("CAPAB negotiation failed: "+reason);
342                         return false;
343                 }
344         }
345         else if ((params[0] == "MODULES") && (params.size() == 2))
346         {
347                 if (!this->capab->ModuleList.length())
348                 {
349                         this->capab->ModuleList.append(params[1]);
350                 }
351                 else
352                 {
353                         this->capab->ModuleList.append(",");
354                         this->capab->ModuleList.append(params[1]);
355                 }
356         }
357         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
358         {
359                 if (!this->capab->OptModuleList.length())
360                 {
361                         this->capab->OptModuleList.append(params[1]);
362                 }
363                 else
364                 {
365                         this->capab->OptModuleList.append(",");
366                         this->capab->OptModuleList.append(params[1]);
367                 }
368         }
369         else if ((params[0] == "CHANMODES") && (params.size() == 2))
370         {
371                 capab->ChanModes = params[1];
372         }
373         else if ((params[0] == "USERMODES") && (params.size() == 2))
374         {
375                 capab->UserModes = params[1];
376         }
377         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
378         {
379                 irc::tokenstream capabs(params[1]);
380                 std::string item;
381                 bool more = true;
382                 while ((more = capabs.GetToken(item)))
383                 {
384                         /* Process each key/value pair */
385                         std::string::size_type equals = item.rfind('=');
386                         if (equals != std::string::npos)
387                         {
388                                 std::string var = item.substr(0, equals);
389                                 std::string value = item.substr(equals+1, item.length());
390                                 capab->CapKeys[var] = value;
391                         }
392                 }
393         }
394         return true;
395 }
396