1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "m_spanningtree/treesocket.h"
18 #include "m_spanningtree/treeserver.h"
19 #include "m_spanningtree/utils.h"
20 #include "m_spanningtree/main.h"
22 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
25 std::string TreeSocket::MyCapabilities()
27 std::vector<std::string> modlist = this->Instance->Modules->GetAllModuleNames(VF_COMMON);
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(MyCapabilities());
46 this->WriteLine("CAPAB START");
48 /* Send module names, split at 509 length */
50 std::string line = "CAPAB MODULES ";
51 while (modulelist.GetToken(item))
53 if (line.length() + item.length() + 1 > 509)
55 this->WriteLine(line);
56 line = "CAPAB MODULES ";
59 if (line != "CAPAB MODULES ")
64 if (line != "CAPAB MODULES ")
65 this->WriteLine(line);
72 #ifdef SUPPORT_IP6LINKS
76 /* Do we have sha256 available? If so, we send a challenge */
77 if (Utils->ChallengeResponse && (Instance->Modules->Find("m_sha256.so")))
79 this->SetOurChallenge(RandString(20));
80 extra = " CHALLENGE=" + this->GetOurChallenge();
83 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
84 ":NICKMAX="+ConvToStr(NICKMAX)+
85 " HALFOP="+ConvToStr(this->Instance->Config->AllowHalfop)+
86 " CHANMAX="+ConvToStr(CHANMAX)+
87 " MAXMODES="+ConvToStr(MAXMODES)+
88 " IDENTMAX="+ConvToStr(IDENTMAX)+
89 " MAXQUIT="+ConvToStr(MAXQUIT)+
90 " MAXTOPIC="+ConvToStr(MAXTOPIC)+
91 " MAXKICK="+ConvToStr(MAXKICK)+
92 " MAXGECOS="+ConvToStr(MAXGECOS)+
93 " MAXAWAY="+ConvToStr(MAXAWAY)+
94 " IP6NATIVE="+ConvToStr(ip6)+
95 " IP6SUPPORT="+ConvToStr(ip6support)+
96 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
97 " PREFIX="+Instance->Modes->BuildPrefixes()+
98 " CHANMODES="+Instance->Modes->ChanModes()+
101 this->WriteLine("CAPAB END");
104 /* Check a comma seperated list for an item */
105 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
107 irc::commasepstream seplist(list);
110 while (seplist.GetToken(item2))
118 /* Isolate and return the elements that are different between two comma seperated lists */
119 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
121 irc::commasepstream list_one(one);
124 while (list_one.GetToken(item))
126 if (!HasItem(two, item))
135 bool TreeSocket::Capab(const std::deque<std::string> ¶ms)
137 if (params.size() < 1)
139 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
142 if (params[0] == "START")
144 this->ModuleList.clear();
145 this->CapKeys.clear();
147 else if (params[0] == "END")
151 #ifdef SUPPORT_IP6LINKS
154 /* Compare ModuleList and check CapKeys...
155 * Maybe this could be tidier? -- Brain
157 if ((this->ModuleList != this->MyCapabilities()) && (this->ModuleList.length()))
159 std::string diff = ListDifference(this->ModuleList, this->MyCapabilities());
162 diff = "your server:" + ListDifference(this->MyCapabilities(), this->ModuleList);
166 diff = "this server:" + diff;
168 if (diff.length() == 12)
169 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
171 reason = "Modules loaded on these servers are not correctly matched, these modules are not loaded on " + diff;
174 cap_validation valid_capab[] = {
175 {"Maximum nickname lengths differ or remote nickname length not specified", "NICKMAX", NICKMAX},
176 {"Maximum ident lengths differ or remote ident length not specified", "IDENTMAX", IDENTMAX},
177 {"Maximum channel lengths differ or remote channel length not specified", "CHANMAX", CHANMAX},
178 {"Maximum modes per line differ or remote modes per line not specified", "MAXMODES", MAXMODES},
179 {"Maximum quit lengths differ or remote quit length not specified", "MAXQUIT", MAXQUIT},
180 {"Maximum topic lengths differ or remote topic length not specified", "MAXTOPIC", MAXTOPIC},
181 {"Maximum kick lengths differ or remote kick length not specified", "MAXKICK", MAXKICK},
182 {"Maximum GECOS (fullname) lengths differ or remote GECOS length not specified", "MAXGECOS", MAXGECOS},
183 {"Maximum awaymessage lengths differ or remote awaymessage length not specified", "MAXAWAY", MAXAWAY},
187 if (((this->CapKeys.find("IP6SUPPORT") == this->CapKeys.end()) && (ip6support)) || ((this->CapKeys.find("IP6SUPPORT") != this->CapKeys.end()) && (this->CapKeys.find("IP6SUPPORT")->second != ConvToStr(ip6support))))
188 reason = "We don't both support linking to IPV6 servers";
189 if (((this->CapKeys.find("IP6NATIVE") != this->CapKeys.end()) && (this->CapKeys.find("IP6NATIVE")->second == "1")) && (!ip6support))
190 reason = "The remote server is IPV6 native, and we don't support linking to IPV6 servers";
191 if (((this->CapKeys.find("PROTOCOL") == this->CapKeys.end()) || ((this->CapKeys.find("PROTOCOL") != this->CapKeys.end()) && (this->CapKeys.find("PROTOCOL")->second != ConvToStr(ProtocolVersion)))))
193 if (this->CapKeys.find("PROTOCOL") != this->CapKeys.end())
194 reason = "Mismatched protocol versions "+this->CapKeys.find("PROTOCOL")->second+" and "+ConvToStr(ProtocolVersion);
196 reason = "Protocol version not specified";
199 if(this->CapKeys.find("PREFIX") != this->CapKeys.end() && this->CapKeys.find("PREFIX")->second != this->Instance->Modes->BuildPrefixes())
200 reason = "One or more of the prefixes on the remote server are invalid on this server.";
202 if (((this->CapKeys.find("HALFOP") == this->CapKeys.end()) && (Instance->Config->AllowHalfop)) || ((this->CapKeys.find("HALFOP") != this->CapKeys.end()) && (this->CapKeys.find("HALFOP")->second != ConvToStr(Instance->Config->AllowHalfop))))
203 reason = "We don't both have halfop support enabled/disabled identically";
205 for (int x = 0; valid_capab[x].size; ++x)
207 if (((this->CapKeys.find(valid_capab[x].key) == this->CapKeys.end()) || ((this->CapKeys.find(valid_capab[x].key) != this->CapKeys.end()) &&
208 (this->CapKeys.find(valid_capab[x].key)->second != ConvToStr(valid_capab[x].size)))))
209 reason = valid_capab[x].reason;
212 /* Challenge response, store their challenge for our password */
213 std::map<std::string,std::string>::iterator n = this->CapKeys.find("CHALLENGE");
214 if (Utils->ChallengeResponse && (n != this->CapKeys.end()) && (Instance->Modules->Find("m_sha256.so")))
216 /* Challenge-response is on now */
217 this->SetTheirChallenge(n->second);
218 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
220 this->SendCapabilities();
221 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 "+
222 Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
227 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
228 if (this->LinkState == CONNECTING)
230 this->SendCapabilities();
231 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+OutboundPass+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
237 this->SendError("CAPAB negotiation failed: "+reason);
241 else if ((params[0] == "MODULES") && (params.size() == 2))
243 if (!this->ModuleList.length())
245 this->ModuleList.append(params[1]);
249 this->ModuleList.append(",");
250 this->ModuleList.append(params[1]);
254 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
256 irc::tokenstream capabs(params[1]);
259 while ((more = capabs.GetToken(item)))
261 /* Process each key/value pair */
262 std::string::size_type equals = item.rfind('=');
263 if (equals != std::string::npos)
265 std::string var = item.substr(0, equals);
266 std::string value = item.substr(equals+1, item.length());
267 CapKeys[var] = value;