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