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