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