]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/server.cpp
0ff0e30f62f3485a8478616045465a77dc6c87df
[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 "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 - Missing SID");
39                 return false;
40         }
41
42         std::string servername = params[0];
43         std::string password = params[1];
44         // hopcount is not used for a remote server, we calculate this ourselves
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->Instance->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 "+CheckDupe->GetName()+" already exists!");
63                 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());
64                 return false;
65         }
66
67         Link* lnk = Utils->FindLink(servername);
68
69         TreeServer *Node = new TreeServer(this->Utils, this->Instance, servername, description, sid, ParentOfThis,NULL, lnk ? lnk->Hidden : false);
70
71         if (Node->DuplicateID())
72         {
73                 this->SendError("Server ID "+servername+" already exists on the network!");
74                 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());
75                 return false;
76         }
77
78         ParentOfThis->AddChild(Node);
79         params[4] = ":" + params[4];
80         Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
81         this->Instance->SNO->WriteToSnoMask('l',"Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
82         return true;
83 }
84
85
86 /*
87  * This is used after the other side of a connection has accepted our credentials.
88  * They are then introducing themselves to us, BEFORE either of us burst. -- w
89  */
90 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> &params)
91 {
92         if (params.size() < 5)
93         {
94                 SendError("Protocol error - Missing SID");
95                 return false;
96         }
97
98         irc::string servername = params[0].c_str();
99         std::string sname = params[0];
100         std::string password = params[1];
101         std::string sid = params[3];
102         std::string description = params[4];
103         int hops = atoi(params[2].c_str());
104
105         this->InboundServerName = sname;
106         this->InboundDescription = description;
107         this->InboundSID = sid;
108
109         if (!sentcapab)
110                 this->SendCapabilities();
111
112         if (hops)
113         {
114                 this->SendError("Server too far away for authentication");
115                 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
116                 return false;
117         }
118
119         if (!this->Instance->IsSID(sid))
120         {
121                 this->SendError("Invalid format server ID: "+sid+"!");
122                 return false;
123         }
124
125         for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
126         {
127                 if (x->Name != servername && x->Name != "*") // open link allowance
128                         continue;
129
130                 if (!ComparePass(this->MakePass(x->RecvPass, this->GetOurChallenge()), password) &&
131                         (x->RecvPass != password && this->GetTheirChallenge().empty()))
132                 {
133                         this->Instance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
134                         continue;
135                 }
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
167
168                 /* IMPORTANT: Take password/hmac hash OUT of here before we broadcast the introduction! */
169                 params[1] = "*";
170                 Utils->DoOneToAllButSender(Instance->Config->GetSID(),"SERVER",params,sname);
171
172                 Node->bursting = true;
173                 this->DoBurst(Node);
174                 return true;
175         }
176
177         this->SendError("Invalid credentials (check the other server's linking snomask for more information)");
178         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
179         return false;
180 }
181
182 /*
183  * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
184  *              -- w
185  */
186 bool TreeSocket::Inbound_Server(std::deque<std::string> &params)
187 {
188         if (params.size() < 5)
189         {
190                 SendError("Protocol error - Missing SID");
191                 return false;
192         }
193
194         irc::string servername = params[0].c_str();
195         std::string sname = params[0];
196         std::string password = params[1];
197         std::string sid = params[3];
198         std::string description = params[4];
199         int hops = atoi(params[2].c_str());
200
201         this->InboundServerName = sname;
202         this->InboundDescription = description;
203         this->InboundSID = sid;
204
205         if (!sentcapab)
206                 this->SendCapabilities();
207
208         if (hops)
209         {
210                 this->SendError("Server too far away for authentication");
211                 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
212                 return false;
213         }
214
215         if (!this->Instance->IsSID(sid))
216         {
217                 this->SendError("Invalid format server ID: "+sid+"!");
218                 return false;
219         }
220
221         for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
222         {
223                 if (x->Name != servername && x->Name != "*") // open link allowance
224                         continue;
225
226                 if (!ComparePass(this->MakePass(x->RecvPass, this->GetOurChallenge()), password) &&
227                         (x->RecvPass != password && this->GetTheirChallenge().empty()))
228                 {
229                         this->Instance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
230                         continue;
231                 }
232
233                 /* Check for fully initialized instances of the server by id */
234                 Instance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
235                 TreeServer* CheckDupeSID = Utils->FindServerID(sid);
236
237                 if (CheckDupeSID)
238                 {
239                         this->SendError("Server ID "+CheckDupeSID->GetID()+" already exists on server "+CheckDupeSID->GetName()+"!");
240                         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupeSID->GetID()+
241                                         "' already exists on server "+CheckDupeSID->GetName());
242                         return false;
243                 }
244
245                 /* Now check for fully initialized instances of the server by name */
246                 TreeServer* CheckDupe = Utils->FindServer(sname);
247                 if (CheckDupe)
248                 {
249                         this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
250                         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
251                         return false;
252                 }
253
254                 this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
255                 if (this->Hook)
256                 {
257                         std::string name = BufferedSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
258                         this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
259                 }
260
261                 // this is good. Send our details: Our server name and description and hopcount of 0,
262                 // along with the sendpass from this block.
263                 this->SendCapabilities();
264                 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
265                 // move to the next state, we are now waiting for THEM.
266                 this->LinkState = WAIT_AUTH_2;
267                 return true;
268         }
269
270         this->SendError("Invalid credentials");
271         this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
272         return false;
273 }
274