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