The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

capab.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #include "inspircd.h"
00015 #include "xline.h"
00016 
00017 #include "m_spanningtree/treesocket.h"
00018 #include "m_spanningtree/treeserver.h"
00019 #include "m_spanningtree/utils.h"
00020 #include "m_spanningtree/main.h"
00021 
00022 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
00023 
00024 
00025 std::string TreeSocket::MyCapabilities()
00026 {
00027         std::vector<std::string> modlist = this->Instance->Modules->GetAllModuleNames(VF_COMMON);
00028         std::string capabilities;
00029         sort(modlist.begin(),modlist.end());
00030         for (unsigned int i = 0; i < modlist.size(); i++)
00031         {
00032                 if (i)
00033                         capabilities = capabilities + ",";
00034                 capabilities = capabilities + modlist[i];
00035         }
00036         return capabilities;
00037 }
00038 
00039 void TreeSocket::SendCapabilities()
00040 {
00041         if (sentcapab)
00042                 return;
00043 
00044         sentcapab = true;
00045         irc::commasepstream modulelist(MyCapabilities());
00046         this->WriteLine("CAPAB START");
00047 
00048         /* Send module names, split at 509 length */
00049         std::string item;
00050         std::string line = "CAPAB MODULES ";
00051         while (modulelist.GetToken(item))
00052         {
00053                 if (line.length() + item.length() + 1 > 509)
00054                 {
00055                         this->WriteLine(line);
00056                         line = "CAPAB MODULES ";
00057                 }
00058 
00059                 if (line != "CAPAB MODULES ")
00060                         line.append(",");
00061 
00062                 line.append(item);
00063         }
00064         if (line != "CAPAB MODULES ")
00065                 this->WriteLine(line);
00066 
00067         int ip6 = 0;
00068         int ip6support = 0;
00069 #ifdef IPV6
00070         ip6 = 1;
00071 #endif
00072 #ifdef SUPPORT_IP6LINKS
00073         ip6support = 1;
00074 #endif
00075         std::string extra;
00076         /* Do we have sha256 available? If so, we send a challenge */
00077         if (Utils->ChallengeResponse && (Instance->Modules->Find("m_sha256.so")))
00078         {
00079                 this->SetOurChallenge(RandString(20));
00080                 extra = " CHALLENGE=" + this->GetOurChallenge();
00081         }
00082 
00083         this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
00084                         ":NICKMAX="+ConvToStr(Instance->Config->Limits.NickMax)+
00085                         " HALFOP="+ConvToStr(Instance->Config->AllowHalfop)+
00086                         " CHANMAX="+ConvToStr(Instance->Config->Limits.ChanMax)+
00087                         " MAXMODES="+ConvToStr(Instance->Config->Limits.MaxModes)+
00088                         " IDENTMAX="+ConvToStr(Instance->Config->Limits.IdentMax)+
00089                         " MAXQUIT="+ConvToStr(Instance->Config->Limits.MaxQuit)+
00090                         " MAXTOPIC="+ConvToStr(Instance->Config->Limits.MaxTopic)+
00091                         " MAXKICK="+ConvToStr(Instance->Config->Limits.MaxKick)+
00092                         " MAXGECOS="+ConvToStr(Instance->Config->Limits.MaxGecos)+
00093                         " MAXAWAY="+ConvToStr(Instance->Config->Limits.MaxAway)+
00094                         " IP6NATIVE="+ConvToStr(ip6)+
00095                         " IP6SUPPORT="+ConvToStr(ip6support)+
00096                         " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
00097                         " PREFIX="+Instance->Modes->BuildPrefixes()+
00098                         " CHANMODES="+Instance->Modes->ChanModes()+
00099                         " SVSPART=1");
00100 
00101         this->WriteLine("CAPAB END");
00102 }
00103 
00104 /* Check a comma seperated list for an item */
00105 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
00106 {
00107         irc::commasepstream seplist(list);
00108         std::string item2;
00109 
00110         while (seplist.GetToken(item2))
00111         {
00112                 if (item2 == item)
00113                         return true;
00114         }
00115         return false;
00116 }
00117 
00118 /* Isolate and return the elements that are different between two comma seperated lists */
00119 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
00120 {
00121         irc::commasepstream list_one(one);
00122         std::string item;
00123         std::string result;
00124         while (list_one.GetToken(item))
00125         {
00126                 if (!HasItem(two, item))
00127                 {
00128                         result.append(" ");
00129                         result.append(item);
00130                 }
00131         }
00132         return result;
00133 }
00134 
00135 bool TreeSocket::Capab(const std::deque<std::string> &params)
00136 {
00137         if (params.size() < 1)
00138         {
00139                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
00140                 return false;
00141         }
00142         if (params[0] == "START")
00143         {
00144                 this->ModuleList.clear();
00145                 this->CapKeys.clear();
00146         }
00147         else if (params[0] == "END")
00148         {
00149                 std::string reason;
00150                 int ip6support = 0;
00151 #ifdef SUPPORT_IP6LINKS
00152                 ip6support = 1;
00153 #endif
00154                 /* Compare ModuleList and check CapKeys...
00155                  * Maybe this could be tidier? -- Brain
00156                  */
00157                 if ((this->ModuleList != this->MyCapabilities()) && (this->ModuleList.length()))
00158                 {
00159                         std::string diff = ListDifference(this->ModuleList, this->MyCapabilities());
00160                         if (!diff.length())
00161                         {
00162                                 diff = "your server:" + ListDifference(this->MyCapabilities(), this->ModuleList);
00163                         }
00164                         else
00165                         {
00166                                 diff = "this server:" + diff;
00167                         }
00168                         if (diff.length() == 12)
00169                                 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
00170                         else
00171                                 reason = "Modules loaded on these servers are not correctly matched, these modules are not loaded on " + diff;
00172                 }
00173 
00174                 if (((this->CapKeys.find("IP6SUPPORT") == this->CapKeys.end()) && (ip6support)) || ((this->CapKeys.find("IP6SUPPORT") != this->CapKeys.end()) && (this->CapKeys.find("IP6SUPPORT")->second != ConvToStr(ip6support))))
00175                         reason = "We don't both support linking to IPV6 servers";
00176                 if (((this->CapKeys.find("IP6NATIVE") != this->CapKeys.end()) && (this->CapKeys.find("IP6NATIVE")->second == "1")) && (!ip6support))
00177                         reason = "The remote server is IPV6 native, and we don't support linking to IPV6 servers";
00178                 if (((this->CapKeys.find("PROTOCOL") == this->CapKeys.end()) || ((this->CapKeys.find("PROTOCOL") != this->CapKeys.end()) && (this->CapKeys.find("PROTOCOL")->second != ConvToStr(ProtocolVersion)))))
00179                 {
00180                         if (this->CapKeys.find("PROTOCOL") != this->CapKeys.end())
00181                                 reason = "Mismatched protocol versions "+this->CapKeys.find("PROTOCOL")->second+" and "+ConvToStr(ProtocolVersion);
00182                         else
00183                                 reason = "Protocol version not specified";
00184                 }
00185 
00186                 if(this->CapKeys.find("PREFIX") != this->CapKeys.end() && this->CapKeys.find("PREFIX")->second != this->Instance->Modes->BuildPrefixes())
00187                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
00188 
00189                 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))))
00190                         reason = "We don't both have halfop support enabled/disabled identically";
00191 
00192                 /* Challenge response, store their challenge for our password */
00193                 std::map<std::string,std::string>::iterator n = this->CapKeys.find("CHALLENGE");
00194                 if (Utils->ChallengeResponse && (n != this->CapKeys.end()) && (Instance->Modules->Find("m_sha256.so")))
00195                 {
00196                         /* Challenge-response is on now */
00197                         this->SetTheirChallenge(n->second);
00198                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
00199                         {
00200                                 this->SendCapabilities();
00201                                 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 "+
00202                                                 Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
00203                         }
00204                 }
00205                 else
00206                 {
00207                         /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
00208                         if (this->LinkState == CONNECTING)
00209                         {
00210                                 this->SendCapabilities();
00211                                 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+OutboundPass+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
00212                         }
00213                 }
00214 
00215                 if (reason.length())
00216                 {
00217                         this->SendError("CAPAB negotiation failed: "+reason);
00218                         return false;
00219                 }
00220         }
00221         else if ((params[0] == "MODULES") && (params.size() == 2))
00222         {
00223                 if (!this->ModuleList.length())
00224                 {
00225                         this->ModuleList.append(params[1]);
00226                 }
00227                 else
00228                 {
00229                         this->ModuleList.append(",");
00230                         this->ModuleList.append(params[1]);
00231                 }
00232         }
00233 
00234         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
00235         {
00236                 irc::tokenstream capabs(params[1]);
00237                 std::string item;
00238                 bool more = true;
00239                 while ((more = capabs.GetToken(item)))
00240                 {
00241                         /* Process each key/value pair */
00242                         std::string::size_type equals = item.rfind('=');
00243                         if (equals != std::string::npos)
00244                         {
00245                                 std::string var = item.substr(0, equals);
00246                                 std::string value = item.substr(equals+1, item.length());
00247                                 CapKeys[var] = value;
00248                         }
00249                 }
00250         }
00251         return true;
00252 }
00253