]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/server.cpp
Send SVSNICKs during nick collision to prevent servers that do not fully implement...
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / server.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
17 #include "socket.h"
18 #include "xline.h"
19 #include "transport.h"
20 #include "socketengine.h"
21
22 #include "m_spanningtree/main.h"
23 #include "m_spanningtree/utils.h"
24 #include "m_spanningtree/link.h"
25 #include "m_spanningtree/treeserver.h"
26 #include "m_spanningtree/treesocket.h"
27
28 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h m_spanningtree/link.h */
29
30 /*
31  * Some server somewhere in the network introducing another server.
32  *      -- w
33  */
34 bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string> &params)
35 {
36         if (params.size() < 5)
37         {
38                 SendError("Protocol error - Not enough parameters for SERVER command");
39                 return false;
40         }
41
42         std::string servername = params[0];
43         // password is not used for a remote server
44         // hopcount is not used (ever)
45         std::string sid = params[3];
46         std::string description = params[4];
47         TreeServer* ParentOfThis = Utils->FindServer(prefix);
48
49         if (!ParentOfThis)
50         {
51                 this->SendError("Protocol error - Introduced remote server from unknown server "+ParentOfThis->GetName());
52                 return false;
53         }
54         if (!this->ServerInstance->IsSID(sid))
55         {
56                 this->SendError("Invalid format server ID: "+sid+"!");
57                 return false;
58         }
59         TreeServer* CheckDupe = Utils->FindServer(servername);
60         if (CheckDupe)
61         {
62                 this->SendError("Server "+servername+" already exists!");
63                 this->ServerInstance->SNO->WriteToSnoMask('L', "Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
64                 return false;
65         }
66         CheckDupe = Utils->FindServer(sid);
67         if (CheckDupe)
68         {
69                 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.");
70                 this->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());
71                 return false;
72         }
73
74
75         Link* lnk = Utils->FindLink(servername);
76
77         TreeServer *Node = new TreeServer(this->Utils, this->ServerInstance, servername, description, sid, ParentOfThis,NULL, lnk ? lnk->Hidden : false);
78
79         ParentOfThis->AddChild(Node);
80         params[4] = ":" + params[4];
81         Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
82         this->ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
83         return true;
84 }
85
86
87 /*
88  * This is used after the other side of a connection has accepted our credentials.
89  * They are then introducing themselves to us, BEFORE either of us burst. -- w
90  */
91 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> &params)
92 {
93         if (params.size() < 5)
94         {
95                 SendError("Protocol error - Not enough parameters for SERVER command");
96                 return false;
97         }
98
99         irc::string servername = params[0].c_str();
100         std::string sname = params[0];
101         std::string password = params[1];
102         std::string sid = params[3];
103         std::string description = params[4];
104         int hops = atoi(params[2].c_str());
105
106         this->InboundServerName = sname;
107         this->InboundDescription = description;
108         this->InboundSID = sid;
109
110         if (!sentcapab)
111                 this->SendCapabilities();
112
113         if (hops)
114         {
115                 this->SendError("Server too far away for authentication");
116                 this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
117                 return false;
118         }
119
120         if (!this->ServerInstance->IsSID(sid))
121         {
122                 this->SendError("Invalid format server ID: "+sid+"!");
123                 return false;
124         }
125
126         for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
127         {
128                 if (x->Name != servername && x->Name != "*") // open link allowance
129                         continue;
130
131                 if (!ComparePass(this->MakePass(x->RecvPass, this->GetOurChallenge()), password) &&
132                         (x->RecvPass != password && this->GetTheirChallenge().empty()))
133                 {
134                         this->ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
135                         continue;
136                 }
137
138                 TreeServer* CheckDupe = Utils->FindServer(sname);
139                 if (CheckDupe)
140                 {
141                         this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
142                         this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
143                         return false;
144                 }
145                 CheckDupe = Utils->FindServer(sid);
146                 if (CheckDupe)
147                 {
148                         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.");
149                         this->ServerInstance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
150                         return false;
151                 }
152
153                 /*
154                  * They're in WAIT_AUTH_2 (having accepted our credentials).
155                  * Set our state to CONNECTED (since everything's peachy so far) and send our
156                  * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
157                  *
158                  * While we're at it, create a treeserver object so we know about them.
159                  *   -- w
160                  */
161                 this->LinkState = CONNECTED;
162
163                 Utils->timeoutlist.erase(this);
164
165                 TreeServer *Node = new TreeServer(this->Utils, this->ServerInstance, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
166
167                 Utils->TreeRoot->AddChild(Node);
168                 params[4] = ":" + params[4];
169
170
171                 /* IMPORTANT: Take password/hmac hash OUT of here before we broadcast the introduction! */
172                 params[1] = "*";
173                 Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",params,sname);
174
175                 Node->bursting = true;
176                 this->DoBurst(Node);
177                 return true;
178         }
179
180         this->SendError("Invalid credentials (check the other server's linking snomask for more information)");
181         this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
182         return false;
183 }
184
185 /*
186  * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
187  *              -- w
188  */
189 bool TreeSocket::Inbound_Server(std::deque<std::string> &params)
190 {
191         if (params.size() < 5)
192         {
193                 SendError("Protocol error - Missing SID");
194                 return false;
195         }
196
197         irc::string servername = params[0].c_str();
198         std::string sname = params[0];
199         std::string password = params[1];
200         std::string sid = params[3];
201         std::string description = params[4];
202         int hops = atoi(params[2].c_str());
203
204         this->InboundServerName = sname;
205         this->InboundDescription = description;
206         this->InboundSID = sid;
207
208         if (!sentcapab)
209                 this->SendCapabilities();
210
211         if (hops)
212         {
213                 this->SendError("Server too far away for authentication");
214                 this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
215                 return false;
216         }
217
218         if (!this->ServerInstance->IsSID(sid))
219         {
220                 this->SendError("Invalid format server ID: "+sid+"!");
221                 return false;
222         }
223
224         for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
225         {
226                 if (x->Name != servername && x->Name != "*") // open link allowance
227                         continue;
228
229                 if (!ComparePass(this->MakePass(x->RecvPass, this->GetOurChallenge()), password) &&
230                         (x->RecvPass != password && this->GetTheirChallenge().empty()))
231                 {
232                         this->ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
233                         continue;
234                 }
235
236                 /* Now check for fully initialized ServerInstances of the server by name */
237                 TreeServer* CheckDupe = Utils->FindServer(sname);
238                 if (CheckDupe)
239                 {
240                         this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
241                         this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
242                         return false;
243                 }
244
245                 /* Check for fully initialized instances of the server by id */
246                 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
247                 CheckDupe = Utils->FindServerID(sid);
248
249                 if (CheckDupe)
250                 {
251                         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.");
252                         this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupe->GetID()+
253                                         "' already exists on server "+CheckDupe->GetName());
254                         return false;
255                 }
256
257
258                 this->ServerInstance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
259                 if (this->Hook)
260                 {
261                         std::string name = BufferedSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
262                         this->ServerInstance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
263                 }
264
265                 // this is good. Send our details: Our server name and description and hopcount of 0,
266                 // along with the sendpass from this block.
267                 this->SendCapabilities();
268                 this->WriteLine(std::string("SERVER ")+this->ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+this->ServerInstance->Config->ServerDesc);
269                 // move to the next state, we are now waiting for THEM.
270                 this->LinkState = WAIT_AUTH_2;
271                 return true;
272         }
273
274         this->SendError("Invalid credentials");
275         this->ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
276         return false;
277 }
278