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