]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
66d448e55d1a837b30b82cf728e068d85a7aef38
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / capab.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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 = this->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 = capabilities + ",";
37                 capabilities = capabilities + modlist[i];
38         }
39         return capabilities;
40 }
41
42 void TreeSocket::SendCapabilities(int phase)
43 {
44         if (capab_phase >= phase)
45                 return;
46
47         if (capab_phase < 1 && phase >= 1)
48                 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
49
50         capab_phase = phase;
51         if (phase < 2)
52                 return;
53
54         irc::commasepstream modulelist(MyModules(VF_COMMON));
55         irc::commasepstream optmodulelist(MyModules(VF_OPTCOMMON));
56         /* Send module names, split at 509 length */
57         std::string item;
58         std::string line = "CAPAB MODULES ";
59         while (modulelist.GetToken(item))
60         {
61                 if (line.length() + item.length() + 1 > 509)
62                 {
63                         this->WriteLine(line);
64                         line = "CAPAB MODULES ";
65                 }
66
67                 if (line != "CAPAB MODULES ")
68                         line.append(",");
69
70                 line.append(item);
71         }
72         if (line != "CAPAB MODULES ")
73                 this->WriteLine(line);
74
75         line = "CAPAB MODSUPPORT ";
76         while (optmodulelist.GetToken(item))
77         {
78                 if (line.length() + item.length() + 1 > 509)
79                 {
80                         this->WriteLine(line);
81                         line = "CAPAB MODSUPPORT ";
82                 }
83
84                 if (line != "CAPAB MODSUPPORT ")
85                         line.append(",");
86
87                 line.append(item);
88         }
89         if (line != "CAPAB MODSUPPORT ")
90                 this->WriteLine(line);
91
92
93         int ip6 = 0;
94 #ifdef IPV6
95         ip6 = 1;
96 #endif
97         std::string extra;
98         /* Do we have sha256 available? If so, we send a challenge */
99         if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
100         {
101                 this->SetOurChallenge(RandString(20));
102                 extra = " CHALLENGE=" + this->GetOurChallenge();
103         }
104
105         this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
106                         ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
107                         " HALFOP="+ConvToStr(ServerInstance->Config->AllowHalfop)+
108                         " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
109                         " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
110                         " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
111                         " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
112                         " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
113                         " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
114                         " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
115                         " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
116                         " IP6NATIVE="+ConvToStr(ip6)+
117                         " IP6SUPPORT=1"+
118                         " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
119                         " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
120                         " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
121                         " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
122                         " SVSPART=1");
123
124         this->WriteLine("CAPAB END");
125 }
126
127 /* Check a comma seperated list for an item */
128 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
129 {
130         irc::commasepstream seplist(list);
131         std::string item2;
132
133         while (seplist.GetToken(item2))
134         {
135                 if (item2 == item)
136                         return true;
137         }
138         return false;
139 }
140
141 /* Isolate and return the elements that are different between two comma seperated lists */
142 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
143 {
144         irc::commasepstream list_one(one);
145         std::string item;
146         std::string result;
147         while (list_one.GetToken(item))
148         {
149                 if (!HasItem(two, item))
150                 {
151                         result.append(" ");
152                         result.append(item);
153                 }
154         }
155         return result;
156 }
157
158 bool TreeSocket::Capab(const parameterlist &params)
159 {
160         if (params.size() < 1)
161         {
162                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
163                 return false;
164         }
165         if (params[0] == "START")
166         {
167                 ModuleList.clear();
168                 OptModuleList.clear();
169                 CapKeys.clear();
170                 if (params.size() > 1)
171                         proto_version = atoi(params[1].c_str());
172                 SendCapabilities(2);
173         }
174         else if (params[0] == "END")
175         {
176                 std::string reason;
177                 /* Compare ModuleList and check CapKeys */
178                 if ((this->ModuleList != this->MyModules(VF_COMMON)) && (this->ModuleList.length()))
179                 {
180                         std::string diffIneed = ListDifference(this->ModuleList, this->MyModules(VF_COMMON));
181                         std::string diffUneed = ListDifference(this->MyModules(VF_COMMON), this->ModuleList);
182                         if (diffIneed.length() == 0 && diffUneed.length() == 0)
183                         {
184                                 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
185                         }
186                         else
187                         {
188                                 reason = "Modules incorrectly matched on these servers.";
189                                 if (diffIneed.length())
190                                         reason += " Not loaded here:" + diffIneed;
191                                 if (diffUneed.length())
192                                         reason += " Not loaded there:" + diffUneed;
193                         }
194                         this->SendError("CAPAB negotiation failed: "+reason);
195                         return false;
196                 }
197                 if (this->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->OptModuleList.length())
198                 {
199                         std::string diffIneed = ListDifference(this->OptModuleList, this->MyModules(VF_OPTCOMMON));
200                         std::string diffUneed = ListDifference(this->MyModules(VF_OPTCOMMON), this->OptModuleList);
201                         if (diffIneed.length() == 0 && diffUneed.length() == 0)
202                         {
203                                 reason = "Optional Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
204                         }
205                         else if (Utils->AllowOptCommon)
206                         {
207                                 ServerInstance->SNO->WriteToSnoMask('l',
208                                         "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
209                                         diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
210                                         diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
211                         }
212                         else
213                         {
214                                 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
215                                 if (diffIneed.length())
216                                         reason += " Not loaded here:" + diffIneed;
217                                 if (diffUneed.length())
218                                         reason += " Not loaded there:" + diffUneed;
219                                 this->SendError("CAPAB negotiation failed: "+reason);
220                                 return false;
221                         }
222                 }
223
224                 if (this->CapKeys.find("PROTOCOL") == this->CapKeys.end())
225                 {
226                         reason = "Protocol version not specified";
227                 }
228                 else
229                 {
230                         proto_version = atoi(CapKeys.find("PROTOCOL")->second.c_str());
231                         if (proto_version < MinCompatProtocol)
232                         {
233                                 reason = "Server is using protocol version " + ConvToStr(proto_version) +
234                                         " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
235                                         + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
236                         }
237                 }
238
239                 if(this->CapKeys.find("PREFIX") != this->CapKeys.end() && this->CapKeys.find("PREFIX")->second != this->ServerInstance->Modes->BuildPrefixes())
240                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
241
242                 if(this->CapKeys.find("CHANMODES") != this->CapKeys.end() && this->CapKeys.find("CHANMODES")->second != this->ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
243                         reason = "One or more of the channel modes on the remote server are invalid on this server.";
244
245                 if(this->CapKeys.find("USERMODES") != this->CapKeys.end() && this->CapKeys.find("USERMODES")->second != this->ServerInstance->Modes->GiveModeList(MASK_USER))
246                         reason = "One or more of the user modes on the remote server are invalid on this server.";
247
248
249                 /* Challenge response, store their challenge for our password */
250                 std::map<std::string,std::string>::iterator n = this->CapKeys.find("CHALLENGE");
251                 if (Utils->ChallengeResponse && (n != this->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
252                 {
253                         /* Challenge-response is on now */
254                         this->SetTheirChallenge(n->second);
255                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
256                         {
257                                 this->SendCapabilities(2);
258                                 this->WriteLine(std::string("SERVER ")+this->ServerInstance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 "+
259                                                 ServerInstance->Config->GetSID()+" :"+this->ServerInstance->Config->ServerDesc);
260                         }
261                 }
262                 else
263                 {
264                         /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
265                         if (this->LinkState == CONNECTING)
266                         {
267                                 this->SendCapabilities(2);
268                                 this->WriteLine(std::string("SERVER ")+this->ServerInstance->Config->ServerName+" "+OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+this->ServerInstance->Config->ServerDesc);
269                         }
270                 }
271
272                 if (reason.length())
273                 {
274                         this->SendError("CAPAB negotiation failed: "+reason);
275                         return false;
276                 }
277         }
278         else if ((params[0] == "MODULES") && (params.size() == 2))
279         {
280                 if (!this->ModuleList.length())
281                 {
282                         this->ModuleList.append(params[1]);
283                 }
284                 else
285                 {
286                         this->ModuleList.append(",");
287                         this->ModuleList.append(params[1]);
288                 }
289         }
290         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
291         {
292                 if (!this->OptModuleList.length())
293                 {
294                         this->OptModuleList.append(params[1]);
295                 }
296                 else
297                 {
298                         this->OptModuleList.append(",");
299                         this->OptModuleList.append(params[1]);
300                 }
301         }
302         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
303         {
304                 irc::tokenstream capabs(params[1]);
305                 std::string item;
306                 bool more = true;
307                 while ((more = capabs.GetToken(item)))
308                 {
309                         /* Process each key/value pair */
310                         std::string::size_type equals = item.rfind('=');
311                         if (equals != std::string::npos)
312                         {
313                                 std::string var = item.substr(0, equals);
314                                 std::string value = item.substr(equals+1, item.length());
315                                 CapKeys[var] = value;
316                         }
317                 }
318         }
319         return true;
320 }
321