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