]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/server.cpp
Extra debug
[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
52         if (!ParentOfThis)
53         {
54                 this->SendError("Protocol error - Introduced remote server from unknown server "+ParentOfThis->GetName());
55                 return false;
56         }
57         if (!this->Instance->IsSID(sid))
58         {
59                 this->SendError("Invalid format server ID: "+sid+"!");
60                 return false;
61         }
62         TreeServer* CheckDupe = Utils->FindServer(servername);
63         if (CheckDupe)
64         {
65                 this->SendError("Server "+CheckDupe->GetName()+" already exists!");
66                 this->Instance->SNO->WriteToSnoMask('l',"Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
67                 return false;
68         }
69
70         Link* lnk = Utils->FindLink(servername);
71
72         TreeServer *Node = new TreeServer(this->Utils, this->Instance, servername, description, sid, ParentOfThis,NULL, lnk ? lnk->Hidden : false);
73
74         if (Node->DuplicateID())
75         {
76                 this->SendError("Server ID "+servername+" already exists on the network!");
77                 this->Instance->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());
78                 return false;
79         }
80
81         ParentOfThis->AddChild(Node);
82         params[4] = ":" + params[4];
83         Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
84         this->Instance->SNO->WriteToSnoMask('l',"Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
85         return true;
86 }
87
88
89 /*
90  * This is used after the other side of a connection has accepted our credentials.
91  * They are then introducing themselves to us, BEFORE either of us burst. -- w
92  */
93 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> &params)
94 {
95         if (params.size() < 5)
96         {
97                 SendError("Protocol error - Missing SID");
98                 return false;
99         }
100
101         irc::string servername = params[0].c_str();
102         std::string sname = params[0];
103         std::string password = params[1];
104         std::string sid = params[3];
105         std::string description = params[4];
106         int hops = atoi(params[2].c_str());
107
108         this->InboundServerName = sname;
109         this->InboundDescription = description;
110         this->InboundSID = sid;
111
112         if (!sentcapab)
113                 this->SendCapabilities();
114
115         if (hops)
116         {
117                 this->SendError("Server too far away for authentication");
118                 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
119                 return false;
120         }
121
122         if (!this->Instance->IsSID(sid))
123         {
124                 this->SendError("Invalid format server ID: "+sid+"!");
125                 return false;
126         }
127
128         for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
129         {
130                 if (x->Name != servername && x->Name != "*") // open link allowance
131                         continue;
132
133                 if (!ComparePass(this->MakePass(x->RecvPass, this->GetOurChallenge()), password) &&
134                         (x->RecvPass != password && this->GetTheirChallenge().empty()))
135                 {
136                         this->Instance->SNO->WriteToSnoMask('l',"Invalid password on link: %s %s %s", x->Name.c_str(), x->RecvPass.c_str(), password.c_str());
137                         continue;
138                 }
139
140                 TreeServer* CheckDupe = Utils->FindServer(sname);
141                 if (CheckDupe)
142                 {
143                         this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
144                         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
145                         return false;
146                 }
147
148                 /*
149                  * They're in WAIT_AUTH_2 (having accepted our credentials).
150                  * Set our state to CONNECTED (since everything's peachy so far) and send our
151                  * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
152                  *
153                  * While we're at it, create a treeserver object so we know about them.
154                  *   -- w
155                  */
156                 this->LinkState = CONNECTED;
157
158                 TreeServer *Node = new TreeServer(this->Utils, this->Instance, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
159
160                 if (Node->DuplicateID())
161                 {
162                         this->SendError("Server ID "+sid+" already exists on the network!");
163                         this->Instance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
164                         return false;
165                 }
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(Instance->Config->GetSID(),"SERVER",params,sname);
174
175                 Node->bursting = true;
176                 this->DoBurst(Node);
177                 return true;
178         }
179
180         this->SendError("Invalid credentials");
181         this->Instance->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->Instance->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->Instance->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->Instance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
233                         continue;
234                 }
235
236                 /* Check for fully initialized instances of the server by id */
237                 Instance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
238                 TreeServer* CheckDupeSID = Utils->FindServerID(sid);
239
240                 if (CheckDupeSID)
241                 {
242                         this->SendError("Server ID "+CheckDupeSID->GetID()+" already exists on server "+CheckDupeSID->GetName()+"!");
243                         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupeSID->GetID()+
244                                         "' already exists on server "+CheckDupeSID->GetName());
245                         return false;
246                 }
247
248                 /* Now check for fully initialized instances of the server by name */
249                 TreeServer* CheckDupe = Utils->FindServer(sname);
250                 if (CheckDupe)
251                 {
252                         this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
253                         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
254                         return false;
255                 }
256
257                 this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
258                 if (this->Hook)
259                 {
260                         std::string name = BufferedSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
261                         this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
262                 }
263
264                 // this is good. Send our details: Our server name and description and hopcount of 0,
265                 // along with the sendpass from this block.
266                 this->SendCapabilities();
267                 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
268                 // move to the next state, we are now waiting for THEM.
269                 this->LinkState = WAIT_AUTH_2;
270                 return true;
271         }
272
273         this->SendError("Invalid credentials");
274         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
275         return false;
276 }
277