1 /***************************************************************************
2 * Copyright (C) 2005-2014 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"
23 // TODO: support system application proxy (new in Qt 4.6)
32 #include "clientsettings.h"
34 #include "protocols/legacy/legacypeer.h"
36 using namespace Protocol;
38 ClientAuthHandler::ClientAuthHandler(CoreAccount account, QObject *parent)
39 : AuthHandler(parent),
47 void ClientAuthHandler::connectToCore()
49 CoreAccountSettings s;
52 QSslSocket *socket = new QSslSocket(this);
53 // make sure the warning is shown if we happen to connect without SSL support later
54 s.setAccountValue("ShowNoClientSslWarning", true);
56 if (_account.useSsl()) {
57 if (s.accountValue("ShowNoClientSslWarning", true).toBool()) {
58 bool accepted = false;
59 emit handleNoSslInClient(&accepted);
61 emit errorMessage(tr("Unencrypted connection canceled"));
64 s.setAccountValue("ShowNoClientSslWarning", false);
67 QTcpSocket *socket = new QTcpSocket(this);
70 // TODO: Handle system proxy
71 #ifndef QT_NO_NETWORKPROXY
72 if (_account.useProxy()) {
73 QNetworkProxy proxy(_account.proxyType(), _account.proxyHostName(), _account.proxyPort(), _account.proxyUser(), _account.proxyPassword());
74 socket->setProxy(proxy);
79 // handled by the base class for now; may need to rethink for protocol detection
80 //connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(onSocketError(QAbstractSocket::SocketError)));
81 connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));
82 connect(socket, SIGNAL(connected()), SLOT(onSocketConnected()));
84 emit statusMessage(tr("Connecting to %1...").arg(_account.accountName()));
85 socket->connectToHost(_account.hostName(), _account.port());
89 // TODO: handle protocol detection
90 void ClientAuthHandler::onSocketStateChanged(QAbstractSocket::SocketState socketState)
95 case QAbstractSocket::UnconnectedState:
96 text = tr("Disconnected");
97 // Ensure the disconnected() signal is sent even if we haven't reached the Connected state yet.
98 // The baseclass implementation will make sure to only send the signal once.
99 onSocketDisconnected();
101 case QAbstractSocket::HostLookupState:
102 text = tr("Looking up %1...").arg(_account.hostName());
104 case QAbstractSocket::ConnectingState:
105 text = tr("Connecting to %1...").arg(_account.hostName());
107 case QAbstractSocket::ConnectedState:
108 text = tr("Connected to %1").arg(_account.hostName());
110 case QAbstractSocket::ClosingState:
111 text = tr("Disconnecting from %1...").arg(_account.hostName());
117 if (!text.isEmpty()) {
118 emit statusMessage(text);
122 // TODO: handle protocol detection
124 void ClientAuthHandler::onSocketError(QAbstractSocket::SocketError error)
126 emit socketError(error, socket()->errorString());
130 void ClientAuthHandler::onSocketConnected()
132 // TODO: protocol detection
135 qWarning() << Q_FUNC_INFO << "Peer already exists!";
139 socket()->setSocketOption(QAbstractSocket::KeepAliveOption, true);
141 _peer = new LegacyPeer(this, socket(), this);
143 connect(_peer, SIGNAL(transferProgress(int,int)), SIGNAL(transferProgress(int,int)));
146 connect(_peer, SIGNAL(protocolVersionMismatch(int,int)), SLOT(onProtocolVersionMismatch(int,int)));
148 emit statusMessage(tr("Synchronizing to core..."));
152 useSsl = _account.useSsl();
155 _peer->dispatch(RegisterClient(Quassel::buildInfo().fancyVersionString, useSsl));
159 void ClientAuthHandler::onProtocolVersionMismatch(int actual, int expected)
161 emit errorPopup(tr("<b>The Quassel Core you are trying to connect to is too old!</b><br>"
162 "We need at least protocol v%1, but the core speaks v%2 only.").arg(expected, actual));
163 requestDisconnect(tr("Incompatible protocol version, connection to core refused"));
167 void ClientAuthHandler::handle(const ClientDenied &msg)
169 emit errorPopup(msg.errorString);
170 requestDisconnect(tr("The core refused connection from this client"));
174 void ClientAuthHandler::handle(const ClientRegistered &msg)
176 _coreConfigured = msg.coreConfigured;
177 _backendInfo = msg.backendInfo;
179 Client::setCoreFeatures(static_cast<Quassel::Features>(msg.coreFeatures));
182 CoreAccountSettings s;
183 if (_account.useSsl()) {
184 if (msg.sslSupported) {
185 // Make sure the warning is shown next time we don't have SSL in the core
186 s.setAccountValue("ShowNoCoreSslWarning", true);
188 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
190 connect(sslSocket, SIGNAL(encrypted()), SLOT(onSslSocketEncrypted()));
191 connect(sslSocket, SIGNAL(sslErrors(QList<QSslError>)), SLOT(onSslErrors()));
192 qDebug() << "Starting encryption...";
194 sslSocket->startClientEncryption();
197 if (s.accountValue("ShowNoCoreSslWarning", true).toBool()) {
198 bool accepted = false;
199 emit handleNoSslInCore(&accepted);
201 requestDisconnect(tr("Unencrypted connection cancelled"));
204 s.setAccountValue("ShowNoCoreSslWarning", false);
205 s.setAccountValue("SslCert", QString());
212 // if we use SSL we wait for the next step until every SSL warning has been cleared
219 void ClientAuthHandler::onSslSocketEncrypted()
221 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
224 if (!socket->sslErrors().count()) {
225 // Cert is valid, so we don't want to store it as known
226 // That way, a warning will appear in case it becomes invalid at some point
227 CoreAccountSettings s;
228 s.setAccountValue("SSLCert", QString());
231 emit encrypted(true);
236 void ClientAuthHandler::onSslErrors()
238 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
241 CoreAccountSettings s;
242 QByteArray knownDigest = s.accountValue("SslCert").toByteArray();
244 if (knownDigest != socket->peerCertificate().digest()) {
245 bool accepted = false;
246 bool permanently = false;
247 emit handleSslErrors(socket, &accepted, &permanently);
250 requestDisconnect(tr("Unencrypted connection canceled"));
255 s.setAccountValue("SslCert", socket->peerCertificate().digest());
257 s.setAccountValue("SslCert", QString());
260 socket->ignoreSslErrors();
263 #endif /* HAVE_SSL */
266 void ClientAuthHandler::onConnectionReady()
268 emit connectionReady();
269 emit statusMessage(tr("Connected to %1").arg(_account.accountName()));
271 if (!_coreConfigured) {
273 emit startCoreSetup(_backendInfo);
275 else // TODO: check if we need LoginEnabled
280 void ClientAuthHandler::setupCore(const SetupData &setupData)
282 _peer->dispatch(setupData);
286 void ClientAuthHandler::handle(const SetupFailed &msg)
288 emit coreSetupFailed(msg.errorString);
292 void ClientAuthHandler::handle(const SetupDone &msg)
296 emit coreSetupSuccessful();
300 void ClientAuthHandler::login(const QString &user, const QString &password, bool remember)
302 _account.setUser(user);
303 _account.setPassword(password);
304 _account.setStorePassword(remember);
309 void ClientAuthHandler::login(const QString &previousError)
311 emit statusMessage(tr("Logging in..."));
312 if (_account.user().isEmpty() || _account.password().isEmpty() || !previousError.isEmpty()) {
314 emit userAuthenticationRequired(&_account, &valid, previousError); // *must* be a synchronous call
315 if (!valid || _account.user().isEmpty() || _account.password().isEmpty()) {
316 requestDisconnect(tr("Login canceled"));
321 _peer->dispatch(Login(_account.user(), _account.password()));
325 void ClientAuthHandler::handle(const LoginFailed &msg)
327 login(msg.errorString);
331 void ClientAuthHandler::handle(const LoginSuccess &msg)
335 emit loginSuccessful(_account);
339 void ClientAuthHandler::handle(const SessionState &msg)
341 disconnect(socket(), 0, this, 0); // this is the last message we shall ever get
343 // give up ownership of the peer; CoreSession takes responsibility now
345 emit handshakeComplete(_peer, msg);