]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket1.cpp
Warn about non-local plaintext server connections.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket1.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
9  *   Copyright (C) 2007-2008, 2010 Craig Edwards <brain@inspircd.org>
10  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28 #include "iohook.h"
29
30 #include "main.h"
31 #include "utils.h"
32 #include "treeserver.h"
33 #include "link.h"
34 #include "treesocket.h"
35 #include "commands.h"
36
37 /** Constructor for outgoing connections.
38  * Because most of the I/O gubbins are encapsulated within
39  * BufferedSocket, we just call DoConnect() for most of the action,
40  * and only do minor initialization tasks ourselves.
41  */
42 TreeSocket::TreeSocket(Link* link, Autoconnect* myac, const irc::sockets::sockaddrs& dest)
43         : linkID(link->Name)
44         , LinkState(CONNECTING)
45         , MyRoot(NULL)
46         , proto_version(0)
47         , burstsent(false)
48         , age(ServerInstance->Time())
49 {
50         capab = new CapabData;
51         capab->link = link;
52         capab->ac = myac;
53         capab->capab_phase = 0;
54         capab->remotesa = dest;
55
56         irc::sockets::sockaddrs bind;
57         memset(&bind, 0, sizeof(bind));
58         if (!link->Bind.empty() && (dest.family() == AF_INET || dest.family() == AF_INET6))
59         {
60                 if (!irc::sockets::aptosa(link->Bind, 0, bind))
61                 {
62                         state = I_ERROR;
63                         SetError("Bind address '" + link->Bind + "' is not a valid IPv4 or IPv6 address");
64                         TreeSocket::OnError(I_ERR_BIND);
65                         return;
66                 }
67                 else if (bind.family() != dest.family())
68                 {
69                         state = I_ERROR;
70                         SetError("Bind address '" + bind.addr() + "' is not the same address family as destination address '" + dest.addr() + "'");
71                         TreeSocket::OnError(I_ERR_BIND);
72                         return;
73                 }
74         }
75
76         DoConnect(dest, bind, link->Timeout);
77         Utils->timeoutlist[this] = std::pair<std::string, unsigned int>(linkID, link->Timeout);
78         SendCapabilities(1);
79 }
80
81 /** Constructor for incoming connections
82  */
83 TreeSocket::TreeSocket(int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
84         : BufferedSocket(newfd)
85         , linkID("inbound from " + client->addr())
86         , LinkState(WAIT_AUTH_1)
87         , MyRoot(NULL)
88         , proto_version(0)
89         , burstsent(false)
90         , age(ServerInstance->Time())
91 {
92         capab = new CapabData;
93         capab->capab_phase = 0;
94         capab->remotesa = *client;
95
96         for (ListenSocket::IOHookProvList::iterator i = via->iohookprovs.begin(); i != via->iohookprovs.end(); ++i)
97         {
98                 ListenSocket::IOHookProvRef& iohookprovref = *i;
99                 if (!iohookprovref)
100                         continue;
101
102                 iohookprovref->OnAccept(this, client, server);
103                 // IOHook could have encountered a fatal error, e.g. if the TLS ClientHello was already in the queue and there was no common TLS version
104                 if (!getError().empty())
105                 {
106                         TreeSocket::OnError(I_ERR_OTHER);
107                         return;
108                 }
109         }
110
111         SendCapabilities(1);
112
113         Utils->timeoutlist[this] = std::pair<std::string, unsigned int>(linkID, 30);
114 }
115
116 void TreeSocket::CleanNegotiationInfo()
117 {
118         // connect is good, reset the autoconnect block (if used)
119         if (capab->ac)
120                 capab->ac->position = -1;
121         delete capab;
122         capab = NULL;
123 }
124
125 CullResult TreeSocket::cull()
126 {
127         Utils->timeoutlist.erase(this);
128         if (capab && capab->ac)
129                 Utils->Creator->ConnectServer(capab->ac, false);
130         return this->BufferedSocket::cull();
131 }
132
133 TreeSocket::~TreeSocket()
134 {
135         delete capab;
136 }
137
138 /** When an outbound connection finishes connecting, we receive
139  * this event, and must do CAPAB negotiation with the other
140  * side. If the other side is happy, as outlined in the server
141  * to server docs on the inspircd.org site, the other side
142  * will then send back its own SERVER string eventually.
143  */
144 void TreeSocket::OnConnected()
145 {
146         if (this->LinkState == CONNECTING)
147         {
148                 if (!capab->link->Hook.empty())
149                 {
150                         ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_IOHOOK, capab->link->Hook);
151                         if (!prov)
152                         {
153                                 SetError("Could not find hook '" + capab->link->Hook + "' for connection to " + linkID);
154                                 return;
155                         }
156                         static_cast<IOHookProvider*>(prov)->OnConnect(this);
157                 }
158
159                 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \002%s\002[%s] started.", linkID.c_str(),
160                         (capab->link->HiddenFromStats ? "<hidden>" : capab->link->IPAddr.c_str()));
161                 this->SendCapabilities(1);
162         }
163 }
164
165 void TreeSocket::OnError(BufferedSocketError e)
166 {
167         ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\002%s\002' failed with error: %s",
168                 linkID.c_str(), getError().c_str());
169         LinkState = DYING;
170         Close();
171 }
172
173 void TreeSocket::SendError(const std::string &errormessage)
174 {
175         WriteLine("ERROR :"+errormessage);
176         DoWrite();
177         LinkState = DYING;
178         SetError(errormessage);
179 }
180
181 CmdResult CommandSQuit::HandleServer(TreeServer* server, CommandBase::Params& params)
182 {
183         TreeServer* quitting = Utils->FindServer(params[0]);
184         if (!quitting)
185         {
186                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Squit from unknown server");
187                 return CMD_FAILURE;
188         }
189
190         CmdResult ret = CMD_SUCCESS;
191         if (quitting == server)
192         {
193                 ret = CMD_FAILURE;
194                 server = server->GetParent();
195         }
196         else if (quitting->GetParent() != server)
197                 throw ProtocolException("Attempted to SQUIT a non-directly connected server or the parent");
198
199         server->SQuitChild(quitting, params[1]);
200
201         // XXX: Return CMD_FAILURE when servers SQUIT themselves (i.e. :00S SQUIT 00S :Shutting down)
202         // to stop this message from being forwarded.
203         // The squit logic generates a SQUIT message with our sid as the source and sends it to the
204         // remaining servers.
205         return ret;
206 }
207
208 /** This function is called when we receive data from a remote
209  * server.
210  */
211 void TreeSocket::OnDataReady()
212 {
213         Utils->Creator->loopCall = true;
214         std::string line;
215         while (GetNextLine(line))
216         {
217                 std::string::size_type rline = line.find('\r');
218                 if (rline != std::string::npos)
219                         line.erase(rline);
220                 if (line.find('\0') != std::string::npos)
221                 {
222                         SendError("Read null character from socket");
223                         break;
224                 }
225
226                 try
227                 {
228                         ProcessLine(line);
229                 }
230                 catch (CoreException& ex)
231                 {
232                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error while processing: " + line);
233                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, ex.GetReason());
234                         SendError(ex.GetReason() + " - check the log file for details");
235                 }
236
237                 if (!getError().empty())
238                         break;
239         }
240         if (LinkState != CONNECTED && recvq.length() > 4096)
241                 SendError("RecvQ overrun (line too long)");
242         Utils->Creator->loopCall = false;
243 }