]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/server.cpp
m_spanningtree Fix IS_LOCAL() check in OnRehash handler
[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                 Utils->TreeRoot->AddChild(MyRoot);
167                 this->DoBurst(MyRoot);
168
169                 params[4] = ":" + params[4];
170
171                 /* IMPORTANT: Take password/hmac hash OUT of here before we broadcast the introduction! */
172                 params[1] = "*";
173                 Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",params,sname);
174
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 bool TreeSocket::CheckDuplicate(const std::string& sname, const std::string& sid)
184 {
185         /* Check for fully initialized instances of the server by name */
186         TreeServer* CheckDupe = Utils->FindServer(sname);
187         if (CheckDupe)
188         {
189                 std::string pname = CheckDupe->GetParent() ? CheckDupe->GetParent()->GetName() : "<ourself>";
190                 SendError("Server "+sname+" already exists on server "+pname+"!");
191                 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+pname);
192                 return false;
193         }
194
195         /* Check for fully initialized instances of the server by id */
196         ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Looking for dupe SID %s", sid.c_str());
197         CheckDupe = Utils->FindServerID(sid);
198
199         if (CheckDupe)
200         {
201                 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.");
202                 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupe->GetID()+
203                                 "' already exists on server "+CheckDupe->GetName());
204                 return false;
205         }
206
207         return true;
208 }
209
210 /*
211  * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
212  *              -- w
213  */
214 bool TreeSocket::Inbound_Server(parameterlist &params)
215 {
216         if (params.size() < 5)
217         {
218                 SendError("Protocol error - Missing SID");
219                 return false;
220         }
221
222         irc::string servername = params[0].c_str();
223         std::string sname = params[0];
224         std::string password = params[1];
225         std::string sid = params[3];
226         std::string description = params[4];
227         int hops = atoi(params[2].c_str());
228
229         this->SendCapabilities(2);
230
231         if (hops)
232         {
233                 this->SendError("Server too far away for authentication");
234                 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
235                 return false;
236         }
237
238         if (!ServerInstance->IsSID(sid))
239         {
240                 this->SendError("Invalid format server ID: "+sid+"!");
241                 return false;
242         }
243
244         for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
245         {
246                 Link* x = *i;
247                 if (x->Name != servername && x->Name != "*") // open link allowance
248                         continue;
249
250                 if (!ComparePass(*x, password))
251                 {
252                         ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
253                         continue;
254                 }
255
256                 if (!CheckDuplicate(sname, sid))
257                         return false;
258
259                 ServerInstance->SNO->WriteToSnoMask('l',"Verified incoming server connection " + linkID + " ("+description+")");
260
261                 this->SendCapabilities(2);
262
263                 // Save these for later, so when they accept our credentials (indicated by BURST) we remember them
264                 this->capab->hidden = x->Hidden;
265                 this->capab->sid = sid;
266                 this->capab->description = description;
267                 this->capab->name = sname;
268
269                 // Send our details: Our server name and description and hopcount of 0,
270                 // along with the sendpass from this block.
271                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
272
273                 // move to the next state, we are now waiting for THEM.
274                 this->LinkState = WAIT_AUTH_2;
275                 return true;
276         }
277
278         this->SendError("Invalid credentials");
279         ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
280         return false;
281 }
282