]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/server.cpp
bab3d9adb8e03a602f254726898b77e4b1a27c1e
[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                         continue;
136
137                 TreeServer* CheckDupe = Utils->FindServer(sname);
138                 if (CheckDupe)
139                 {
140                         this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
141                         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
142                         return false;
143                 }
144
145                 /*
146                  * They're in WAIT_AUTH_2 (having accepted our credentials).
147                  * Set our state to CONNECTED (since everything's peachy so far) and send our
148                  * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
149                  *
150                  * While we're at it, create a treeserver object so we know about them.
151                  *   -- w
152                  */
153                 this->LinkState = CONNECTED;
154
155                 TreeServer *Node = new TreeServer(this->Utils, this->Instance, sname, description, sid, Utils->TreeRoot, this, x->Hidden);
156
157                 if (Node->DuplicateID())
158                 {
159                         this->SendError("Server ID "+sid+" already exists on the network!");
160                         this->Instance->SNO->WriteToSnoMask('l',"Server \2"+assign(servername)+"\2 being introduced denied, server ID already exists on the network. Closing link.");
161                         return false;
162                 }
163
164                 Utils->TreeRoot->AddChild(Node);
165                 params[4] = ":" + params[4];
166                 Utils->DoOneToAllButSender(Instance->Config->GetSID(),"SERVER",params,sname);
167                 Node->bursting = true;
168                 this->DoBurst(Node);
169                 return true;
170         }
171
172         this->SendError("Invalid credentials");
173         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
174         return false;
175 }
176
177 /*
178  * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
179  *              -- w
180  */
181 bool TreeSocket::Inbound_Server(std::deque<std::string> &params)
182 {
183         if (params.size() < 5)
184         {
185                 SendError("Protocol error - Missing SID");
186                 return false;
187         }
188
189         irc::string servername = params[0].c_str();
190         std::string sname = params[0];
191         std::string password = params[1];
192         std::string sid = params[3];
193         std::string description = params[4];
194         int hops = atoi(params[2].c_str());
195
196         this->InboundServerName = sname;
197         this->InboundDescription = description;
198         this->InboundSID = sid;
199
200         if (!sentcapab)
201                 this->SendCapabilities();
202
203         if (hops)
204         {
205                 this->SendError("Server too far away for authentication");
206                 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
207                 return false;
208         }
209
210         if (!this->Instance->IsSID(sid))
211         {
212                 this->SendError("Invalid format server ID: "+sid+"!");
213                 return false;
214         }
215
216         for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
217         {
218                 if (x->Name != servername && x->Name != "*") // open link allowance
219                         continue;
220
221                 if (!ComparePass(this->MakePass(x->RecvPass, this->GetOurChallenge(), password)) ||
222                         x->RecvPass != password && !this->GetTheirChallenge().empty())
223                         continue;
224
225                 /* Check for fully initialized instances of the server by id */
226                 Instance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
227                 TreeServer* CheckDupeSID = Utils->FindServerID(sid);
228
229                 if (CheckDupeSID)
230                 {
231                         this->SendError("Server ID "+CheckDupeSID->GetID()+" already exists on server "+CheckDupeSID->GetName()+"!");
232                         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupeSID->GetID()+
233                                         "' already exists on server "+CheckDupeSID->GetName());
234                         return false;
235                 }
236
237                 /* Now check for fully initialized instances of the server by name */
238                 TreeServer* CheckDupe = Utils->FindServer(sname);
239                 if (CheckDupe)
240                 {
241                         this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
242                         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
243                         return false;
244                 }
245
246                 this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
247                 if (this->Hook)
248                 {
249                         std::string name = BufferedSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
250                         this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
251                 }
252
253                 // this is good. Send our details: Our server name and description and hopcount of 0,
254                 // along with the sendpass from this block.
255                 this->SendCapabilities();
256                 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
257                 // move to the next state, we are now waiting for THEM.
258                 this->LinkState = WAIT_AUTH_2;
259                 return true;
260         }
261
262         this->SendError("Invalid credentials");
263         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
264         return false;
265 }
266