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