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