ssl: Remove fallback code for missing SSL support
[quassel.git] / src / core / coreauthhandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "coreauthhandler.h"
22
23 #include <QtEndian>
24
25 #include <QSslSocket>
26
27 #include "core.h"
28
29 CoreAuthHandler::CoreAuthHandler(QTcpSocket* socket, QObject* parent)
30     : AuthHandler(parent)
31     , _peer(nullptr)
32     , _metricsServer(Core::instance()->metricsServer())
33     , _proxyReceived(false)
34     , _proxyLine({})
35     , _useProxyLine(false)
36     , _magicReceived(false)
37     , _legacy(false)
38     , _clientRegistered(false)
39     , _connectionFeatures(0)
40 {
41     setSocket(socket);
42     connect(socket, &QIODevice::readyRead, this, &CoreAuthHandler::onReadyRead);
43
44     // TODO: Timeout for the handshake phase
45 }
46
47 void CoreAuthHandler::onReadyRead()
48 {
49     // once we have selected a peer, we certainly don't want to read more data!
50     if (_peer)
51         return;
52
53     if (!_proxyReceived) {
54         quint32 magic;
55         socket()->peek((char*) &magic, 4);
56         magic = qFromBigEndian<quint32>(magic);
57
58         if (magic == Protocol::proxyMagic) {
59             if (!socket()->canReadLine()) {
60                 return;
61             }
62             QByteArray line = socket()->readLine(108);
63             _proxyLine = ProxyLine::parseProxyLine(line);
64             if (_proxyLine.protocol != QAbstractSocket::UnknownNetworkLayerProtocol) {
65                 QList<QString> subnets = Quassel::optionValue("proxy-cidr").split(",");
66                 for (const QString& subnet : subnets) {
67                     if (socket()->peerAddress().isInSubnet(QHostAddress::parseSubnet(subnet))) {
68                         _useProxyLine = true;
69                         break;
70                     }
71                 }
72             }
73         }
74         _proxyReceived = true;
75     }
76
77     if (socket()->bytesAvailable() < 4)
78         return;
79
80     if (!_magicReceived) {
81         quint32 magic;
82         socket()->peek((char*)&magic, 4);
83         magic = qFromBigEndian<quint32>(magic);
84
85         if ((magic & 0xffffff00) != Protocol::magic) {
86             // no magic, assume legacy protocol
87             qDebug() << "Legacy client detected, switching to compatibility mode";
88             _legacy = true;
89             RemotePeer* peer = PeerFactory::createPeer(PeerFactory::ProtoDescriptor(Protocol::LegacyProtocol, 0),
90                                                        this,
91                                                        socket(),
92                                                        Compressor::NoCompression,
93                                                        this);
94             connect(peer, &RemotePeer::protocolVersionMismatch, this, &CoreAuthHandler::onProtocolVersionMismatch);
95             setPeer(peer);
96             return;
97         }
98
99         _magicReceived = true;
100         quint8 features = magic & 0xff;
101         // figure out which connection features we'll use based on the client's support
102         if (Core::sslSupported() && (features & Protocol::Encryption))
103             _connectionFeatures |= Protocol::Encryption;
104         if (features & Protocol::Compression)
105             _connectionFeatures |= Protocol::Compression;
106
107         socket()->read((char*)&magic, 4);  // read the 4 bytes we've just peeked at
108     }
109
110     // read the list of protocols supported by the client
111     while (socket()->bytesAvailable() >= 4 && _supportedProtos.size() < 16) {  // sanity check
112         quint32 data;
113         socket()->read((char*)&data, 4);
114         data = qFromBigEndian<quint32>(data);
115
116         auto type = static_cast<Protocol::Type>(data & 0xff);
117         auto protoFeatures = static_cast<quint16>(data >> 8 & 0xffff);
118         _supportedProtos.append(PeerFactory::ProtoDescriptor(type, protoFeatures));
119
120         if (data >= 0x80000000) {  // last protocol
121             Compressor::CompressionLevel level;
122             if (_connectionFeatures & Protocol::Compression)
123                 level = Compressor::BestCompression;
124             else
125                 level = Compressor::NoCompression;
126
127             RemotePeer* peer = PeerFactory::createPeer(_supportedProtos, this, socket(), level, this);
128             if (!peer) {
129                 qWarning() << "Received invalid handshake data from client" << hostAddress().toString();
130                 close();
131                 return;
132             }
133
134             if (peer->protocol() == Protocol::LegacyProtocol) {
135                 _legacy = true;
136                 connect(peer, &RemotePeer::protocolVersionMismatch, this, &CoreAuthHandler::onProtocolVersionMismatch);
137             }
138             setPeer(peer);
139
140             // inform the client
141             quint32 reply = peer->protocol() | peer->enabledFeatures() << 8 | _connectionFeatures << 24;
142             reply = qToBigEndian<quint32>(reply);
143             socket()->write((char*)&reply, 4);
144             socket()->flush();
145
146             if (!_legacy && (_connectionFeatures & Protocol::Encryption))
147                 startSsl();  // legacy peer enables it later
148             return;
149         }
150     }
151 }
152
153 void CoreAuthHandler::setPeer(RemotePeer* peer)
154 {
155     qDebug().nospace() << "Using " << qPrintable(peer->protocolName()) << "...";
156
157     _peer = peer;
158     if (_proxyLine.protocol != QAbstractSocket::UnknownNetworkLayerProtocol) {
159         _peer->setProxyLine(_proxyLine);
160     }
161     disconnect(socket(), &QIODevice::readyRead, this, &CoreAuthHandler::onReadyRead);
162 }
163
164 // only in compat mode
165 void CoreAuthHandler::onProtocolVersionMismatch(int actual, int expected)
166 {
167     qWarning() << qPrintable(tr("Client")) << _peer->description() << qPrintable(tr("too old, rejecting."));
168     QString errorString = tr("<b>Your Quassel Client is too old!</b><br>"
169                              "This core needs at least client/core protocol version %1 (got: %2).<br>"
170                              "Please consider upgrading your client.")
171                               .arg(expected, actual);
172     _peer->dispatch(Protocol::ClientDenied(errorString));
173     _peer->close();
174 }
175
176 bool CoreAuthHandler::checkClientRegistered()
177 {
178     if (!_clientRegistered) {
179         qWarning() << qPrintable(tr("Client")) << qPrintable(hostAddress().toString())
180                    << qPrintable(tr("did not send a registration message before trying to login, rejecting."));
181         _peer->dispatch(
182             Protocol::ClientDenied(tr("<b>Client not initialized!</b><br>You need to send a registration message before trying to login.")));
183         _peer->close();
184         return false;
185     }
186     return true;
187 }
188
189 void CoreAuthHandler::handle(const Protocol::RegisterClient& msg)
190 {
191     bool useSsl;
192     if (_legacy)
193         useSsl = Core::sslSupported() && msg.sslSupported;
194     else
195         useSsl = _connectionFeatures & Protocol::Encryption;
196
197     if (Quassel::isOptionSet("require-ssl") && !useSsl && !_peer->isLocal()) {
198         qInfo() << qPrintable(tr("SSL required but non-SSL connection attempt from %1").arg(hostAddress().toString()));
199         _peer->dispatch(Protocol::ClientDenied(tr("<b>SSL is required!</b><br>You need to use SSL in order to connect to this core.")));
200         _peer->close();
201         return;
202     }
203
204     _peer->setFeatures(std::move(msg.features));
205     _peer->setBuildDate(msg.buildDate);
206     _peer->setClientVersion(msg.clientVersion);
207
208     QVariantList backends;
209     QVariantList authenticators;
210     bool configured = Core::isConfigured();
211     if (!configured) {
212         backends = Core::backendInfo();
213         if (_peer->hasFeature(Quassel::Feature::Authenticators)) {
214             authenticators = Core::authenticatorInfo();
215         }
216     }
217
218     _peer->dispatch(Protocol::ClientRegistered(Quassel::Features{}, configured, backends, authenticators, useSsl));
219
220     // useSsl is only used for the legacy protocol
221     if (_legacy && useSsl)
222         startSsl();
223
224     _clientRegistered = true;
225 }
226
227 void CoreAuthHandler::handle(const Protocol::SetupData& msg)
228 {
229     if (!checkClientRegistered())
230         return;
231
232     // The default parameter to authenticator is Database.
233     // Maybe this should be hardcoded elsewhere, i.e. as a define.
234     QString authenticator = msg.authenticator;
235     qInfo() << "[" << authenticator << "]";
236     if (authenticator.trimmed().isEmpty()) {
237         authenticator = QString("Database");
238     }
239
240     QString result = Core::setup(msg.adminUser, msg.adminPassword, msg.backend, msg.setupData, authenticator, msg.authSetupData);
241     if (!result.isEmpty())
242         _peer->dispatch(Protocol::SetupFailed(result));
243     else
244         _peer->dispatch(Protocol::SetupDone());
245 }
246
247 void CoreAuthHandler::handle(const Protocol::Login& msg)
248 {
249     if (!checkClientRegistered())
250         return;
251
252     if (!Core::isConfigured()) {
253         qWarning() << qPrintable(tr("Client")) << qPrintable(hostAddress().toString())
254                    << qPrintable(tr("attempted to login before the core was configured, rejecting."));
255         _peer->dispatch(Protocol::ClientDenied(
256             tr("<b>Attempted to login before core was configured!</b><br>The core must be configured before attempting to login.")));
257         return;
258     }
259
260     // First attempt local auth using the real username and password.
261     // If that fails, move onto the auth provider.
262
263     // Check to see if the user has the "Database" authenticator configured.
264     UserId uid = 0;
265     if (Core::getUserAuthenticator(msg.user) == "Database") {
266         uid = Core::validateUser(msg.user, msg.password);
267     }
268
269     // If they did not, *or* if the database login fails, try to use a different authenticator.
270     // TODO: this logic should likely be moved into Core::authenticateUser in the future.
271     // Right now a core can only have one authenticator configured; this might be something
272     // to change in the future.
273     if (uid == 0) {
274         uid = Core::authenticateUser(msg.user, msg.password);
275     }
276
277     if (uid == 0) {
278         qInfo() << qPrintable(tr("Invalid login attempt from %1 as \"%2\"").arg(hostAddress().toString(), msg.user));
279         _peer->dispatch(Protocol::LoginFailed(tr(
280             "<b>Invalid username or password!</b><br>The username/password combination you supplied could not be found in the database.")));
281         if (_metricsServer) {
282             _metricsServer->addLoginAttempt(msg.user, false);
283         }
284         return;
285     }
286     _peer->dispatch(Protocol::LoginSuccess());
287     if (_metricsServer) {
288         _metricsServer->addLoginAttempt(uid, true);
289     }
290
291     qInfo() << qPrintable(tr("Client %1 initialized and authenticated successfully as \"%2\" (UserId: %3).")
292                               .arg(_peer->address(), msg.user, QString::number(uid.toInt())));
293
294     const auto& clientFeatures = _peer->features();
295     auto unsupported = clientFeatures.toStringList(false);
296     if (!unsupported.isEmpty()) {
297         if (unsupported.contains("NoFeatures"))
298             qInfo() << qPrintable(tr("Client does not support extended features."));
299         else
300             qInfo() << qPrintable(tr("Client does not support the following features: %1").arg(unsupported.join(", ")));
301     }
302
303     if (!clientFeatures.unknownFeatures().isEmpty()) {
304         qInfo() << qPrintable(tr("Client supports unknown features: %1").arg(clientFeatures.unknownFeatures().join(", ")));
305     }
306
307     disconnect(socket(), nullptr, this, nullptr);
308     disconnect(_peer, nullptr, this, nullptr);
309     _peer->setParent(nullptr);  // Core needs to take care of this one now!
310
311     socket()->flush();  // Make sure all data is sent before handing over the peer (and socket) to the session thread (bug 682)
312     emit handshakeComplete(_peer, uid);
313 }
314
315 QHostAddress CoreAuthHandler::hostAddress() const
316 {
317     if (_useProxyLine) {
318         return _proxyLine.sourceHost;
319     }
320     else if (socket()) {
321         return socket()->peerAddress();
322     }
323
324     return {};
325 }
326
327 bool CoreAuthHandler::isLocal() const
328 {
329     return hostAddress() == QHostAddress::LocalHost ||
330            hostAddress() == QHostAddress::LocalHostIPv6;
331 }
332
333 /*** SSL Stuff ***/
334
335 void CoreAuthHandler::startSsl()
336 {
337     auto* sslSocket = qobject_cast<QSslSocket*>(socket());
338     Q_ASSERT(sslSocket);
339
340     qDebug() << qPrintable(tr("Starting encryption for Client:")) << _peer->description();
341     connect(sslSocket, selectOverload<const QList<QSslError>&>(&QSslSocket::sslErrors), this, &CoreAuthHandler::onSslErrors);
342     sslSocket->flush();  // ensure that the write cache is flushed before we switch to ssl (bug 682)
343     sslSocket->startServerEncryption();
344 }
345
346 void CoreAuthHandler::onSslErrors()
347 {
348     auto* sslSocket = qobject_cast<QSslSocket*>(socket());
349     Q_ASSERT(sslSocket);
350     sslSocket->ignoreSslErrors();
351 }