]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/server.cpp
Merge insp20
[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
23 #include "main.h"
24 #include "utils.h"
25 #include "link.h"
26 #include "treeserver.h"
27 #include "treesocket.h"
28 #include "commands.h"
29
30 /*
31  * Some server somewhere in the network introducing another server.
32  *      -- w
33  */
34 CmdResult CommandServer::HandleServer(TreeServer* ParentOfThis, std::vector<std::string>& params)
35 {
36         const std::string& servername = params[0];
37         const std::string& sid = params[1];
38         const std::string& description = params.back();
39         TreeSocket* socket = ParentOfThis->GetSocket();
40
41         if (!InspIRCd::IsSID(sid))
42         {
43                 socket->SendError("Invalid format server ID: "+sid+"!");
44                 return CMD_FAILURE;
45         }
46         TreeServer* CheckDupe = Utils->FindServer(servername);
47         if (CheckDupe)
48         {
49                 socket->SendError("Server "+servername+" already exists!");
50                 ServerInstance->SNO->WriteToSnoMask('L', "Server \2"+CheckDupe->GetName()+"\2 being introduced from \2" + ParentOfThis->GetName() + "\2 denied, already exists. Closing link with " + ParentOfThis->GetName());
51                 return CMD_FAILURE;
52         }
53         CheckDupe = Utils->FindServer(sid);
54         if (CheckDupe)
55         {
56                 socket->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.");
57                 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());
58                 return CMD_FAILURE;
59         }
60
61
62         Link* lnk = Utils->FindLink(servername);
63
64         TreeServer* Node = new TreeServer(servername, description, sid, ParentOfThis, ParentOfThis->GetSocket(), lnk ? lnk->Hidden : false);
65
66         HandleExtra(Node, params);
67
68         ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+ParentOfThis->GetName()+"\002 introduced server \002"+servername+"\002 ("+description+")");
69         return CMD_SUCCESS;
70 }
71
72 void CommandServer::HandleExtra(TreeServer* newserver, const std::vector<std::string>& params)
73 {
74         for (std::vector<std::string>::const_iterator i = params.begin() + 2; i != params.end() - 1; ++i)
75         {
76                 const std::string& prop = *i;
77                 std::string::size_type p = prop.find('=');
78
79                 std::string key = prop;
80                 std::string val;
81                 if (p != std::string::npos)
82                 {
83                         key.erase(p);
84                         val.assign(prop, p+1, std::string::npos);
85                 }
86
87                 if (key == "burst")
88                         newserver->BeginBurst(ConvToInt(val));
89         }
90 }
91
92 Link* TreeSocket::AuthRemote(const parameterlist& params)
93 {
94         if (params.size() < 5)
95         {
96                 SendError("Protocol error - Not enough parameters for SERVER command");
97                 return NULL;
98         }
99
100         irc::string servername = params[0].c_str();
101         const std::string& sname = params[0];
102         const std::string& password = params[1];
103         const std::string& sid = params[3];
104         const std::string& description = params.back();
105
106         this->SendCapabilities(2);
107
108         if (!ServerInstance->IsSID(sid))
109         {
110                 this->SendError("Invalid format server ID: "+sid+"!");
111                 return NULL;
112         }
113
114         for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
115         {
116                 Link* x = *i;
117                 if (x->Name != servername && x->Name != "*") // open link allowance
118                         continue;
119
120                 if (!ComparePass(*x, password))
121                 {
122                         ServerInstance->SNO->WriteToSnoMask('l',"Invalid password on link: %s", x->Name.c_str());
123                         continue;
124                 }
125
126                 if (!CheckDuplicate(sname, sid))
127                         return NULL;
128
129                 ServerInstance->SNO->WriteToSnoMask('l',"Verified server connection " + linkID + " ("+description+")");
130                 return x;
131         }
132
133         this->SendError("Mismatched server name or password (check the other server's snomask output for details - e.g. umode +s +Ll)");
134         ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
135         return NULL;
136 }
137
138 /*
139  * This is used after the other side of a connection has accepted our credentials.
140  * They are then introducing themselves to us, BEFORE either of us burst. -- w
141  */
142 bool TreeSocket::Outbound_Reply_Server(parameterlist &params)
143 {
144         const Link* x = AuthRemote(params);
145         if (x)
146         {
147                 /*
148                  * They're in WAIT_AUTH_2 (having accepted our credentials).
149                  * Set our state to CONNECTED (since everything's peachy so far) and send our
150                  * netburst to them, which will trigger their CONNECTED state, and BURST in reply.
151                  *
152                  * While we're at it, create a treeserver object so we know about them.
153                  *   -- w
154                  */
155                 FinishAuth(params[0], params[3], params.back(), x->Hidden);
156
157                 return true;
158         }
159
160         return false;
161 }
162
163 bool TreeSocket::CheckDuplicate(const std::string& sname, const std::string& sid)
164 {
165         /* Check for fully initialized instances of the server by name */
166         TreeServer* CheckDupe = Utils->FindServer(sname);
167         if (CheckDupe)
168         {
169                 std::string pname = CheckDupe->GetParent() ? CheckDupe->GetParent()->GetName() : "<ourself>";
170                 SendError("Server "+sname+" already exists on server "+pname+"!");
171                 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+pname);
172                 return false;
173         }
174
175         /* Check for fully initialized instances of the server by id */
176         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Looking for dupe SID %s", sid.c_str());
177         CheckDupe = Utils->FindServerID(sid);
178
179         if (CheckDupe)
180         {
181                 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.");
182                 ServerInstance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server ID '"+CheckDupe->GetID()+
183                                 "' already exists on server "+CheckDupe->GetName());
184                 return false;
185         }
186
187         return true;
188 }
189
190 /*
191  * Someone else is attempting to connect to us if this is called. Validate their credentials etc.
192  *              -- w
193  */
194 bool TreeSocket::Inbound_Server(parameterlist &params)
195 {
196         const Link* x = AuthRemote(params);
197         if (x)
198         {
199                 // Save these for later, so when they accept our credentials (indicated by BURST) we remember them
200                 this->capab->hidden = x->Hidden;
201                 this->capab->sid = params[3];
202                 this->capab->description = params.back();
203                 this->capab->name = params[0];
204
205                 // Send our details: Our server name and description and hopcount of 0,
206                 // along with the sendpass from this block.
207                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
208
209                 // move to the next state, we are now waiting for THEM.
210                 this->LinkState = WAIT_AUTH_2;
211                 return true;
212         }
213
214         return false;
215 }
216
217 CommandServer::Builder::Builder(TreeServer* server)
218         : CmdBuilder(server->GetParent()->GetID(), "SERVER")
219 {
220         push(server->GetName());
221         push(server->GetID());
222         if (server->IsBursting())
223                 push_property("burst", ConvToStr(server->StartBurst));
224         push_last(server->GetDesc());
225 }