1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "treesocket.h"
18 #include "treeserver.h"
22 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
25 std::string TreeSocket::MyModules(int filter)
27 std::vector<std::string> modlist = this->ServerInstance->Modules->GetAllModuleNames(filter);
28 std::string capabilities;
29 sort(modlist.begin(),modlist.end());
30 for (unsigned int i = 0; i < modlist.size(); i++)
33 capabilities = capabilities + ",";
34 capabilities = capabilities + modlist[i];
39 void TreeSocket::SendCapabilities()
45 irc::commasepstream modulelist(MyModules(VF_COMMON));
46 irc::commasepstream optmodulelist(MyModules(VF_OPTCOMMON));
47 this->WriteLine("CAPAB START");
49 /* Send module names, split at 509 length */
51 std::string line = "CAPAB MODULES ";
52 while (modulelist.GetToken(item))
54 if (line.length() + item.length() + 1 > 509)
56 this->WriteLine(line);
57 line = "CAPAB MODULES ";
60 if (line != "CAPAB MODULES ")
65 if (line != "CAPAB MODULES ")
66 this->WriteLine(line);
68 line = "CAPAB MODSUPPORT ";
69 while (optmodulelist.GetToken(item))
71 if (line.length() + item.length() + 1 > 509)
73 this->WriteLine(line);
74 line = "CAPAB MODSUPPORT ";
77 if (line != "CAPAB MODSUPPORT ")
82 if (line != "CAPAB MODSUPPORT ")
83 this->WriteLine(line);
91 /* Do we have sha256 available? If so, we send a challenge */
92 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
94 this->SetOurChallenge(RandString(20));
95 extra = " CHALLENGE=" + this->GetOurChallenge();
98 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
99 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
100 " HALFOP="+ConvToStr(ServerInstance->Config->AllowHalfop)+
101 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
102 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
103 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
104 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
105 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
106 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
107 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
108 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
109 " IP6NATIVE="+ConvToStr(ip6)+
111 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
112 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
113 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
114 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
117 this->WriteLine("CAPAB END");
120 /* Check a comma seperated list for an item */
121 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
123 irc::commasepstream seplist(list);
126 while (seplist.GetToken(item2))
134 /* Isolate and return the elements that are different between two comma seperated lists */
135 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
137 irc::commasepstream list_one(one);
140 while (list_one.GetToken(item))
142 if (!HasItem(two, item))
151 bool TreeSocket::Capab(const parameterlist ¶ms)
153 if (params.size() < 1)
155 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
158 if (params[0] == "START")
161 OptModuleList.clear();
164 else if (params[0] == "END")
167 /* Compare ModuleList and check CapKeys */
168 if ((this->ModuleList != this->MyModules(VF_COMMON)) && (this->ModuleList.length()))
170 std::string diffIneed = ListDifference(this->ModuleList, this->MyModules(VF_COMMON));
171 std::string diffUneed = ListDifference(this->MyModules(VF_COMMON), this->ModuleList);
172 if (diffIneed.length() == 0 && diffUneed.length() == 0)
174 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
178 reason = "Modules incorrectly matched on these servers.";
179 if (diffIneed.length())
180 reason += " Not loaded here:" + diffIneed;
181 if (diffUneed.length())
182 reason += " Not loaded there:" + diffUneed;
184 this->SendError("CAPAB negotiation failed: "+reason);
187 if (this->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->OptModuleList.length())
189 std::string diffIneed = ListDifference(this->OptModuleList, this->MyModules(VF_OPTCOMMON));
190 std::string diffUneed = ListDifference(this->MyModules(VF_OPTCOMMON), this->OptModuleList);
191 if (diffIneed.length() == 0 && diffUneed.length() == 0)
193 reason = "Optional Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
195 else if (Utils->AllowOptCommon)
197 ServerInstance->SNO->WriteToSnoMask('l',
198 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
199 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
200 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
204 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
205 if (diffIneed.length())
206 reason += " Not loaded here:" + diffIneed;
207 if (diffUneed.length())
208 reason += " Not loaded there:" + diffUneed;
209 this->SendError("CAPAB negotiation failed: "+reason);
214 if (this->CapKeys.find("PROTOCOL") == this->CapKeys.end())
216 reason = "Protocol version not specified";
220 int otherProto = atoi(CapKeys.find("PROTOCOL")->second.c_str());
221 if (otherProto < MinCompatProtocol)
223 reason = "Server is using protocol version " + ConvToStr(otherProto) +
224 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
225 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
229 if(this->CapKeys.find("PREFIX") != this->CapKeys.end() && this->CapKeys.find("PREFIX")->second != this->ServerInstance->Modes->BuildPrefixes())
230 reason = "One or more of the prefixes on the remote server are invalid on this server.";
232 if(this->CapKeys.find("CHANMODES") != this->CapKeys.end() && this->CapKeys.find("CHANMODES")->second != this->ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
233 reason = "One or more of the channel modes on the remote server are invalid on this server.";
235 if(this->CapKeys.find("USERMODES") != this->CapKeys.end() && this->CapKeys.find("USERMODES")->second != this->ServerInstance->Modes->GiveModeList(MASK_USER))
236 reason = "One or more of the user modes on the remote server are invalid on this server.";
239 /* Challenge response, store their challenge for our password */
240 std::map<std::string,std::string>::iterator n = this->CapKeys.find("CHALLENGE");
241 if (Utils->ChallengeResponse && (n != this->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
243 /* Challenge-response is on now */
244 this->SetTheirChallenge(n->second);
245 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
247 this->SendCapabilities();
248 this->WriteLine(std::string("SERVER ")+this->ServerInstance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 "+
249 ServerInstance->Config->GetSID()+" :"+this->ServerInstance->Config->ServerDesc);
254 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
255 if (this->LinkState == CONNECTING)
257 this->SendCapabilities();
258 this->WriteLine(std::string("SERVER ")+this->ServerInstance->Config->ServerName+" "+OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+this->ServerInstance->Config->ServerDesc);
264 this->SendError("CAPAB negotiation failed: "+reason);
268 else if ((params[0] == "MODULES") && (params.size() == 2))
270 if (!this->ModuleList.length())
272 this->ModuleList.append(params[1]);
276 this->ModuleList.append(",");
277 this->ModuleList.append(params[1]);
280 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
282 if (!this->OptModuleList.length())
284 this->OptModuleList.append(params[1]);
288 this->OptModuleList.append(",");
289 this->OptModuleList.append(params[1]);
292 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
294 irc::tokenstream capabs(params[1]);
297 while ((more = capabs.GetToken(item)))
299 /* Process each key/value pair */
300 std::string::size_type equals = item.rfind('=');
301 if (equals != std::string::npos)
303 std::string var = item.substr(0, equals);
304 std::string value = item.substr(equals+1, item.length());
305 CapKeys[var] = value;