1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "coreauthhandler.h"
29 CoreAuthHandler::CoreAuthHandler(QSslSocket* socket, QObject* parent)
32 , _metricsServer(Core::instance()->metricsServer())
33 , _proxyReceived(false)
35 , _useProxyLine(false)
36 , _magicReceived(false)
38 , _clientRegistered(false)
39 , _connectionFeatures(0)
42 connect(socket, &QIODevice::readyRead, this, &CoreAuthHandler::onReadyRead);
44 // TODO: Timeout for the handshake phase
47 void CoreAuthHandler::onReadyRead()
49 // once we have selected a peer, we certainly don't want to read more data!
53 if (!_proxyReceived) {
55 socket()->peek((char*) &magic, 4);
56 magic = qFromBigEndian<quint32>(magic);
58 if (magic == Protocol::proxyMagic) {
59 if (!socket()->canReadLine()) {
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))) {
74 _proxyReceived = true;
77 if (socket()->bytesAvailable() < 4)
80 if (!_magicReceived) {
82 socket()->peek((char*)&magic, 4);
83 magic = qFromBigEndian<quint32>(magic);
85 if ((magic & 0xffffff00) != Protocol::magic) {
86 // no magic, assume legacy protocol
87 qDebug() << "Legacy client detected, switching to compatibility mode";
89 RemotePeer* peer = PeerFactory::createPeer(PeerFactory::ProtoDescriptor(Protocol::LegacyProtocol, 0),
92 Compressor::NoCompression,
94 connect(peer, &RemotePeer::protocolVersionMismatch, this, &CoreAuthHandler::onProtocolVersionMismatch);
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;
107 socket()->read((char*)&magic, 4); // read the 4 bytes we've just peeked at
110 // read the list of protocols supported by the client
111 while (socket()->bytesAvailable() >= 4 && _supportedProtos.size() < 16) { // sanity check
113 socket()->read((char*)&data, 4);
114 data = qFromBigEndian<quint32>(data);
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));
120 if (data >= 0x80000000) { // last protocol
121 Compressor::CompressionLevel level;
122 if (_connectionFeatures & Protocol::Compression)
123 level = Compressor::BestCompression;
125 level = Compressor::NoCompression;
127 RemotePeer* peer = PeerFactory::createPeer(_supportedProtos, this, socket(), level, this);
129 qWarning() << "Received invalid handshake data from client" << hostAddress().toString();
134 if (peer->protocol() == Protocol::LegacyProtocol) {
136 connect(peer, &RemotePeer::protocolVersionMismatch, this, &CoreAuthHandler::onProtocolVersionMismatch);
141 quint32 reply = peer->protocol() | peer->enabledFeatures() << 8 | _connectionFeatures << 24;
142 reply = qToBigEndian<quint32>(reply);
143 socket()->write((char*)&reply, 4);
146 if (!_legacy && (_connectionFeatures & Protocol::Encryption))
147 startSsl(); // legacy peer enables it later
153 void CoreAuthHandler::setPeer(RemotePeer* peer)
155 qDebug().nospace() << "Using " << qPrintable(peer->protocolName()) << "...";
158 if (_proxyLine.protocol != QAbstractSocket::UnknownNetworkLayerProtocol) {
159 _peer->setProxyLine(_proxyLine);
161 disconnect(socket(), &QIODevice::readyRead, this, &CoreAuthHandler::onReadyRead);
164 // only in compat mode
165 void CoreAuthHandler::onProtocolVersionMismatch(int actual, int expected)
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));
176 bool CoreAuthHandler::checkClientRegistered()
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."));
182 Protocol::ClientDenied(tr("<b>Client not initialized!</b><br>You need to send a registration message before trying to login.")));
189 void CoreAuthHandler::handle(const Protocol::RegisterClient& msg)
193 useSsl = Core::sslSupported() && msg.sslSupported;
195 useSsl = _connectionFeatures & Protocol::Encryption;
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.")));
204 _peer->setFeatures(std::move(msg.features));
205 _peer->setBuildDate(msg.buildDate);
206 _peer->setClientVersion(msg.clientVersion);
208 QVariantList backends;
209 QVariantList authenticators;
210 bool configured = Core::isConfigured();
212 backends = Core::backendInfo();
213 if (_peer->hasFeature(Quassel::Feature::Authenticators)) {
214 authenticators = Core::authenticatorInfo();
218 _peer->dispatch(Protocol::ClientRegistered(Quassel::Features{}, configured, backends, authenticators, useSsl));
220 // useSsl is only used for the legacy protocol
221 if (_legacy && useSsl)
224 _clientRegistered = true;
227 void CoreAuthHandler::handle(const Protocol::SetupData& msg)
229 if (!checkClientRegistered())
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");
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));
244 _peer->dispatch(Protocol::SetupDone());
247 void CoreAuthHandler::handle(const Protocol::Login& msg)
249 if (!checkClientRegistered())
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.")));
260 // First attempt local auth using the real username and password.
261 // If that fails, move onto the auth provider.
263 // Check to see if the user has the "Database" authenticator configured.
265 if (Core::getUserAuthenticator(msg.user) == "Database") {
266 uid = Core::validateUser(msg.user, msg.password);
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.
274 uid = Core::authenticateUser(msg.user, msg.password);
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);
286 _peer->dispatch(Protocol::LoginSuccess());
287 if (_metricsServer) {
288 _metricsServer->addLoginAttempt(uid, true);
291 qInfo() << qPrintable(tr("Client %1 initialized and authenticated successfully as \"%2\" (UserId: %3).")
292 .arg(_peer->address(), msg.user, QString::number(uid.toInt())));
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."));
300 qInfo() << qPrintable(tr("Client does not support the following features: %1").arg(unsupported.join(", ")));
303 if (!clientFeatures.unknownFeatures().isEmpty()) {
304 qInfo() << qPrintable(tr("Client supports unknown features: %1").arg(clientFeatures.unknownFeatures().join(", ")));
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!
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);
315 QHostAddress CoreAuthHandler::hostAddress() const
318 return _proxyLine.sourceHost;
321 return socket()->peerAddress();
327 bool CoreAuthHandler::isLocal() const
329 return hostAddress() == QHostAddress::LocalHost ||
330 hostAddress() == QHostAddress::LocalHostIPv6;
335 void CoreAuthHandler::startSsl()
337 qDebug() << qPrintable(tr("Starting encryption for Client:")) << _peer->description();
338 connect(socket(), selectOverload<const QList<QSslError>&>(&QSslSocket::sslErrors), this, &CoreAuthHandler::onSslErrors);
339 socket()->flush(); // ensure that the write cache is flushed before we switch to ssl (bug 682)
340 socket()->startServerEncryption();
343 void CoreAuthHandler::onSslErrors()
345 socket()->ignoreSslErrors();