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