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 "socketengine.h"
22 #include "treeserver.h"
23 #include "treesocket.h"
25 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h m_spanningtree/link.h */
28 * Some server somewhere in the network introducing another server.
31 bool TreeSocket::RemoteServer(const std::string &prefix, parameterlist ¶ms)
33 if (params.size() < 5)
35 SendError("Protocol error - Not enough parameters for SERVER command");
39 std::string servername = params[0];
40 // password is not used for a remote server
41 // hopcount is not used (ever)
42 std::string sid = params[3];
43 std::string description = params[4];
44 TreeServer* ParentOfThis = Utils->FindServer(prefix);
48 this->SendError("Protocol error - Introduced remote server from unknown server "+ParentOfThis->GetName());
51 if (!ServerInstance->IsSID(sid))
53 this->SendError("Invalid format server ID: "+sid+"!");
56 TreeServer* CheckDupe = Utils->FindServer(servername);
59 this->SendError("Server "+servername+" already exists!");
60 ServerInstance->SNO->WriteToSnoMask('L', "Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
63 CheckDupe = Utils->FindServer(sid);
66 this->SendError("Server ID "+sid+" already exists! You may want to specify the server ID for the server manually with <server:id> so they do not conflict.");
67 ServerInstance->SNO->WriteToSnoMask('L', "Server \2"+servername+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, server ID already exists on the network. Closing link with " + ParentOfThis->GetName());
72 Link* lnk = Utils->FindLink(servername);
74 TreeServer *Node = new TreeServer(Utils, servername, description, sid, ParentOfThis,NULL, lnk ? lnk->Hidden : false);
76 ParentOfThis->AddChild(Node);
77 params[4] = ":" + params[4];
78 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
79 ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
85 * This is used after the other side of a connection has accepted our credentials.
86 * They are then introducing themselves to us, BEFORE either of us burst. -- w
88 bool TreeSocket::Outbound_Reply_Server(parameterlist ¶ms)
90 if (params.size() < 5)
92 SendError("Protocol error - Not enough parameters for SERVER command");
96 irc::string servername = params[0].c_str();
97 std::string sname = params[0];
98 std::string password = params[1];
99 std::string sid = params[3];
100 std::string description = params[4];
101 int hops = atoi(params[2].c_str());
103 this->InboundServerName = sname;
104 this->InboundDescription = description;
105 this->InboundSID = sid;
107 this->SendCapabilities(2);
111 this->SendError("Server too far away for authentication");
112 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
116 if (!ServerInstance->IsSID(sid))
118 this->SendError("Invalid format server ID: "+sid+"!");
122 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
125 if (x->Name != servername && x->Name != "*") // open link allowance
128 if (!ComparePass(*x, password))
130 ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
134 TreeServer* CheckDupe = Utils->FindServer(sname);
137 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
138 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
141 CheckDupe = Utils->FindServer(sid);
144 this->SendError("Server ID "+sid+" already exists on the network! You may want to specify the server ID for the server manually with <server:id> so they do not conflict.");
145 ServerInstance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
150 * They're in WAIT_AUTH_2 (having accepted our credentials).
151 * Set our state to CONNECTED (since everything's peachy so far) and send our
152 * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
154 * While we're at it, create a treeserver object so we know about them.
157 this->LinkState = CONNECTED;
159 Utils->timeoutlist.erase(this);
161 TreeServer *Node = new TreeServer(Utils, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
163 Utils->TreeRoot->AddChild(Node);
164 params[4] = ":" + params[4];
167 /* IMPORTANT: Take password/hmac hash OUT of here before we broadcast the introduction! */
169 Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",params,sname);
175 this->SendError("Invalid credentials (check the other server's linking snomask for more information)");
176 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
181 * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
184 bool TreeSocket::Inbound_Server(parameterlist ¶ms)
186 if (params.size() < 5)
188 SendError("Protocol error - Missing SID");
192 irc::string servername = params[0].c_str();
193 std::string sname = params[0];
194 std::string password = params[1];
195 std::string sid = params[3];
196 std::string description = params[4];
197 int hops = atoi(params[2].c_str());
199 this->InboundServerName = sname;
200 this->InboundDescription = description;
201 this->InboundSID = sid;
203 this->SendCapabilities(2);
207 this->SendError("Server too far away for authentication");
208 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
212 if (!ServerInstance->IsSID(sid))
214 this->SendError("Invalid format server ID: "+sid+"!");
218 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
221 if (x->Name != servername && x->Name != "*") // open link allowance
224 if (!ComparePass(*x, password))
226 ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
230 /* Now check for fully initialized ServerInstances of the server by name */
231 TreeServer* CheckDupe = Utils->FindServer(sname);
234 this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
235 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
239 /* Check for fully initialized instances of the server by id */
240 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
241 CheckDupe = Utils->FindServerID(sid);
245 this->SendError("Server ID "+CheckDupe->GetID()+" already exists on server "+CheckDupe->GetName()+"! You may want to specify the server ID for the server manually with <server:id> so they do not conflict.");
246 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupe->GetID()+
247 "' already exists on server "+CheckDupe->GetName());
252 ServerInstance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->IP)+"] ("+description+")");
254 // this is good. Send our details: Our server name and description and hopcount of 0,
255 // along with the sendpass from this block.
256 this->SendCapabilities(2);
257 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
258 // move to the next state, we are now waiting for THEM.
259 this->LinkState = WAIT_AUTH_2;
263 this->SendError("Invalid credentials");
264 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");