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