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