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