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