1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 "clientauthhandler.h"
32 #include "clientsettings.h"
33 #include "logmessage.h"
34 #include "peerfactory.h"
36 using namespace Protocol;
38 ClientAuthHandler::ClientAuthHandler(CoreAccount account, QObject *parent)
39 : AuthHandler(parent),
44 _connectionFeatures(0)
50 Peer *ClientAuthHandler::peer() const
56 void ClientAuthHandler::connectToCore()
58 CoreAccountSettings s;
61 QSslSocket *socket = new QSslSocket(this);
62 // make sure the warning is shown if we happen to connect without SSL support later
63 s.setAccountValue("ShowNoClientSslWarning", true);
65 if (_account.useSsl()) {
66 if (s.accountValue("ShowNoClientSslWarning", true).toBool()) {
67 bool accepted = false;
68 emit handleNoSslInClient(&accepted);
70 emit errorMessage(tr("Unencrypted connection canceled"));
73 s.setAccountValue("ShowNoClientSslWarning", false);
76 QTcpSocket *socket = new QTcpSocket(this);
79 #ifndef QT_NO_NETWORKPROXY
81 proxy.setType(_account.proxyType());
82 if (_account.proxyType() == QNetworkProxy::Socks5Proxy ||
83 _account.proxyType() == QNetworkProxy::HttpProxy) {
84 proxy.setHostName(_account.proxyHostName());
85 proxy.setPort(_account.proxyPort());
86 proxy.setUser(_account.proxyUser());
87 proxy.setPassword(_account.proxyPassword());
90 if (_account.proxyType() == QNetworkProxy::DefaultProxy) {
91 QNetworkProxyFactory::setUseSystemConfiguration(true);
93 QNetworkProxyFactory::setUseSystemConfiguration(false);
94 socket->setProxy(proxy);
99 connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));
100 connect(socket, SIGNAL(readyRead()), SLOT(onReadyRead()));
101 connect(socket, SIGNAL(connected()), SLOT(onSocketConnected()));
103 emit statusMessage(tr("Connecting to %1...").arg(_account.accountName()));
104 socket->connectToHost(_account.hostName(), _account.port());
108 void ClientAuthHandler::onSocketStateChanged(QAbstractSocket::SocketState socketState)
112 switch(socketState) {
113 case QAbstractSocket::HostLookupState:
115 text = tr("Looking up %1...").arg(_account.hostName());
117 case QAbstractSocket::ConnectingState:
119 text = tr("Connecting to %1...").arg(_account.hostName());
121 case QAbstractSocket::ConnectedState:
122 text = tr("Connected to %1").arg(_account.hostName());
124 case QAbstractSocket::ClosingState:
126 text = tr("Disconnecting from %1...").arg(_account.hostName());
128 case QAbstractSocket::UnconnectedState:
130 text = tr("Disconnected");
131 // Ensure the disconnected() signal is sent even if we haven't reached the Connected state yet.
132 // The baseclass implementation will make sure to only send the signal once.
133 // However, we do want to prefer a potential socket error signal that may be on route already, so
134 // give this a chance to overtake us by spinning the loop...
135 QTimer::singleShot(0, this, SLOT(onSocketDisconnected()));
142 if (!text.isEmpty()) {
143 emit statusMessage(text);
147 void ClientAuthHandler::onSocketError(QAbstractSocket::SocketError error)
149 if (_probing && error == QAbstractSocket::RemoteHostClosedError) {
154 _probing = false; // all other errors are unrelated to probing and should be handled
155 AuthHandler::onSocketError(error);
159 void ClientAuthHandler::onSocketDisconnected()
161 if (_probing && _legacy) {
162 // Remote host has closed the connection while probing
164 disconnect(socket(), SIGNAL(readyRead()), this, SLOT(onReadyRead()));
165 emit statusMessage(tr("Reconnecting in compatibility mode..."));
166 socket()->connectToHost(_account.hostName(), _account.port());
170 AuthHandler::onSocketDisconnected();
174 void ClientAuthHandler::onSocketConnected()
177 qWarning() << Q_FUNC_INFO << "Peer already exists!";
181 socket()->setSocketOption(QAbstractSocket::KeepAliveOption, true);
184 // First connection attempt, try probing for a capable core
187 QDataStream stream(socket()); // stream handles the endianness for us
188 stream.setVersion(QDataStream::Qt_4_2);
190 quint32 magic = Protocol::magic;
192 if (_account.useSsl())
193 magic |= Protocol::Encryption;
195 magic |= Protocol::Compression;
199 // here goes the list of protocols we support, in order of preference
200 PeerFactory::ProtoList protos = PeerFactory::supportedProtocols();
201 for (int i = 0; i < protos.count(); ++i) {
202 quint32 reply = protos[i].first;
203 reply |= protos[i].second<<8;
204 if (i == protos.count() - 1)
205 reply |= 0x80000000; // end list
209 socket()->flush(); // make sure the probing data is sent immediately
213 // If we arrive here, it's the second connection attempt, meaning probing was not successful -> enable legacy support
215 qDebug() << "Legacy core detected, switching to compatibility mode";
217 RemotePeer *peer = PeerFactory::createPeer(PeerFactory::ProtoDescriptor(Protocol::LegacyProtocol, 0), this, socket(), Compressor::NoCompression, this);
218 // Only needed for the legacy peer, as all others check the protocol version before instantiation
219 connect(peer, SIGNAL(protocolVersionMismatch(int,int)), SLOT(onProtocolVersionMismatch(int,int)));
225 void ClientAuthHandler::onReadyRead()
227 if (socket()->bytesAvailable() < 4)
231 return; // make sure to not read more data than needed
234 disconnect(socket(), SIGNAL(readyRead()), this, SLOT(onReadyRead()));
237 socket()->read((char *)&reply, 4);
238 reply = qFromBigEndian<quint32>(reply);
240 Protocol::Type type = static_cast<Protocol::Type>(reply & 0xff);
241 quint16 protoFeatures = static_cast<quint16>(reply>>8 & 0xffff);
242 _connectionFeatures = static_cast<quint8>(reply>>24);
244 Compressor::CompressionLevel level;
245 if (_connectionFeatures & Protocol::Compression)
246 level = Compressor::BestCompression;
248 level = Compressor::NoCompression;
250 RemotePeer *peer = PeerFactory::createPeer(PeerFactory::ProtoDescriptor(type, protoFeatures), this, socket(), level, this);
252 qWarning() << "No valid protocol supported for this core!";
253 emit errorPopup(tr("<b>Incompatible Quassel Core!</b><br>"
254 "None of the protocols this client speaks are supported by the core you are trying to connect to."));
256 requestDisconnect(tr("Core speaks none of the protocols we support"));
260 if (peer->protocol() == Protocol::LegacyProtocol) {
261 connect(peer, SIGNAL(protocolVersionMismatch(int,int)), SLOT(onProtocolVersionMismatch(int,int)));
269 void ClientAuthHandler::onProtocolVersionMismatch(int actual, int expected)
271 emit errorPopup(tr("<b>The Quassel Core you are trying to connect to is too old!</b><br>"
272 "We need at least protocol v%1, but the core speaks v%2 only.").arg(expected, actual));
273 requestDisconnect(tr("Incompatible protocol version, connection to core refused"));
277 void ClientAuthHandler::setPeer(RemotePeer *peer)
279 qDebug().nospace() << "Using " << qPrintable(peer->protocolName()) << "...";
282 connect(_peer, SIGNAL(transferProgress(int,int)), SIGNAL(transferProgress(int,int)));
284 // The legacy protocol enables SSL later, after registration
285 if (!_account.useSsl() || _legacy)
287 // otherwise, do it now
289 checkAndEnableSsl(_connectionFeatures & Protocol::Encryption);
293 void ClientAuthHandler::startRegistration()
295 emit statusMessage(tr("Synchronizing to core..."));
297 // useSsl will be ignored by non-legacy peers
300 useSsl = _account.useSsl();
303 _peer->dispatch(RegisterClient(Quassel::Features{}, Quassel::buildInfo().fancyVersionString, Quassel::buildInfo().commitDate, useSsl));
307 void ClientAuthHandler::handle(const ClientDenied &msg)
309 emit errorPopup(msg.errorString);
310 requestDisconnect(tr("The core refused connection from this client"));
314 void ClientAuthHandler::handle(const ClientRegistered &msg)
316 _coreConfigured = msg.coreConfigured;
317 _backendInfo = msg.backendInfo;
318 _authenticatorInfo = msg.authenticatorInfo;
320 _peer->setFeatures(std::move(msg.features));
322 // The legacy protocol enables SSL at this point
323 if(_legacy && _account.useSsl())
324 checkAndEnableSsl(msg.sslSupported);
330 void ClientAuthHandler::onConnectionReady()
332 const auto &coreFeatures = _peer->features();
333 auto unsupported = coreFeatures.toStringList(false);
334 if (!unsupported.isEmpty()) {
335 quInfo() << qPrintable(tr("Core does not support the following features: %1").arg(unsupported.join(", ")));
337 if (!coreFeatures.unknownFeatures().isEmpty()) {
338 quInfo() << qPrintable(tr("Core supports unknown features: %1").arg(coreFeatures.unknownFeatures().join(", ")));
341 emit connectionReady();
342 emit statusMessage(tr("Connected to %1").arg(_account.accountName()));
344 if (!_coreConfigured) {
346 emit startCoreSetup(_backendInfo, _authenticatorInfo);
348 else // TODO: check if we need LoginEnabled
353 void ClientAuthHandler::setupCore(const SetupData &setupData)
355 _peer->dispatch(setupData);
359 void ClientAuthHandler::handle(const SetupFailed &msg)
361 emit coreSetupFailed(msg.errorString);
365 void ClientAuthHandler::handle(const SetupDone &msg)
369 emit coreSetupSuccessful();
373 void ClientAuthHandler::login(const QString &user, const QString &password, bool remember)
375 _account.setUser(user);
376 _account.setPassword(password);
377 _account.setStorePassword(remember);
382 void ClientAuthHandler::login(const QString &previousError)
384 emit statusMessage(tr("Logging in..."));
385 if (_account.user().isEmpty() || _account.password().isEmpty() || !previousError.isEmpty()) {
387 emit userAuthenticationRequired(&_account, &valid, previousError); // *must* be a synchronous call
388 if (!valid || _account.user().isEmpty() || _account.password().isEmpty()) {
389 requestDisconnect(tr("Login canceled"));
394 _peer->dispatch(Login(_account.user(), _account.password()));
398 void ClientAuthHandler::handle(const LoginFailed &msg)
400 login(msg.errorString);
404 void ClientAuthHandler::handle(const LoginSuccess &msg)
408 emit loginSuccessful(_account);
412 void ClientAuthHandler::handle(const SessionState &msg)
414 disconnect(socket(), 0, this, 0); // this is the last message we shall ever get
416 // give up ownership of the peer; CoreSession takes responsibility now
418 emit handshakeComplete(_peer, msg);
424 void ClientAuthHandler::checkAndEnableSsl(bool coreSupportsSsl)
427 Q_UNUSED(coreSupportsSsl);
429 CoreAccountSettings s;
430 if (coreSupportsSsl && _account.useSsl()) {
431 // Make sure the warning is shown next time we don't have SSL in the core
432 s.setAccountValue("ShowNoCoreSslWarning", true);
434 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
436 connect(sslSocket, SIGNAL(encrypted()), SLOT(onSslSocketEncrypted()));
437 connect(sslSocket, SIGNAL(sslErrors(QList<QSslError>)), SLOT(onSslErrors()));
438 qDebug() << "Starting encryption...";
440 sslSocket->startClientEncryption();
443 if (s.accountValue("ShowNoCoreSslWarning", true).toBool()) {
444 bool accepted = false;
445 emit handleNoSslInCore(&accepted);
447 requestDisconnect(tr("Unencrypted connection cancelled"));
450 s.setAccountValue("ShowNoCoreSslWarning", false);
451 s.setAccountValue("SslCert", QString());
452 s.setAccountValue("SslCertDigestVersion", QVariant(QVariant::Int));
464 void ClientAuthHandler::onSslSocketEncrypted()
466 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
469 if (!socket->sslErrors().count()) {
470 // Cert is valid, so we don't want to store it as known
471 // That way, a warning will appear in case it becomes invalid at some point
472 CoreAccountSettings s;
473 s.setAccountValue("SSLCert", QString());
474 s.setAccountValue("SslCertDigestVersion", QVariant(QVariant::Int));
477 emit encrypted(true);
486 void ClientAuthHandler::onSslErrors()
488 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
491 CoreAccountSettings s;
492 QByteArray knownDigest = s.accountValue("SslCert").toByteArray();
493 ClientAuthHandler::DigestVersion knownDigestVersion = static_cast<ClientAuthHandler::DigestVersion>(s.accountValue("SslCertDigestVersion").toInt());
495 QByteArray calculatedDigest;
496 switch (knownDigestVersion) {
497 case ClientAuthHandler::DigestVersion::Md5:
498 calculatedDigest = socket->peerCertificate().digest(QCryptographicHash::Md5);
501 case ClientAuthHandler::DigestVersion::Sha2_512:
502 calculatedDigest = socket->peerCertificate().digest(QCryptographicHash::Sha512);
506 qWarning() << "Certificate digest version" << QString(knownDigestVersion) << "is not supported";
509 if (knownDigest != calculatedDigest) {
510 bool accepted = false;
511 bool permanently = false;
512 emit handleSslErrors(socket, &accepted, &permanently);
515 requestDisconnect(tr("Unencrypted connection canceled"));
520 s.setAccountValue("SslCert", socket->peerCertificate().digest(QCryptographicHash::Sha512));
521 s.setAccountValue("SslCertDigestVersion", ClientAuthHandler::DigestVersion::Latest);
524 s.setAccountValue("SslCert", QString());
525 s.setAccountValue("SslCertDigestVersion", QVariant(QVariant::Int));
528 else if (knownDigestVersion != ClientAuthHandler::DigestVersion::Latest) {
529 s.setAccountValue("SslCert", socket->peerCertificate().digest(QCryptographicHash::Sha512));
530 s.setAccountValue("SslCertDigestVersion", ClientAuthHandler::DigestVersion::Latest);
533 socket->ignoreSslErrors();
536 #endif /* HAVE_SSL */