272
274
/*** Core Setup ***/
276
QString Core::setup(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData)
278
return instance()->setupCore(adminUser, adminPassword, backend, setupData);
282
QString Core::setupCore(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData)
285
return tr("Core is already configured! Not configuring again...");
287
if (adminUser.isEmpty() || adminPassword.isEmpty()) {
288
return tr("Admin user or password not set.");
290
if (!(_configured = initStorage(backend, setupData, true))) {
291
return tr("Could not setup storage!");
294
saveBackendSettings(backend, setupData);
296
quInfo() << qPrintable(tr("Creating admin user..."));
297
_storage->addUser(adminUser, adminPassword);
298
startListening(); // TODO check when we need this
273
303
QString Core::setupCoreForInternalUsage()
275
305
Q_ASSERT(!_storageBackends.isEmpty());
276
QVariantMap setupData;
277
307
qsrand(QDateTime::currentDateTime().toTime_t());
279
309
for (int i = 0; i < 10; i++) {
281
311
pass += qrand() % 10;
283
setupData["AdminUser"] = "AdminUser";
284
setupData["AdminPasswd"] = QString::number(pass);
285
setupData["Backend"] = QString("SQLite"); // mono client currently needs sqlite
286
return setupCore(setupData);
290
QString Core::setupCore(QVariantMap setupData)
292
QString user = setupData.take("AdminUser").toString();
293
QString password = setupData.take("AdminPasswd").toString();
294
if (user.isEmpty() || password.isEmpty()) {
295
return tr("Admin user or password not set.");
297
if (_configured || !(_configured = initStorage(setupData, true))) {
298
return tr("Could not setup storage!");
301
s.setStorageSettings(setupData);
302
quInfo() << qPrintable(tr("Creating admin user..."));
303
_storage->addUser(user, password);
304
startListening(); // TODO check when we need this
314
// mono client currently needs sqlite
315
return setupCore("AdminUser", QString::number(pass), "SQLite", QVariantMap());
517
532
Q_ASSERT(server);
518
533
while (server->hasPendingConnections()) {
519
534
QTcpSocket *socket = server->nextPendingConnection();
520
RemotePeer *peer = new LegacyPeer(socket, this);
522
connect(peer, SIGNAL(disconnected()), SLOT(clientDisconnected()));
523
connect(peer, SIGNAL(dataReceived(QVariant)), SLOT(processClientMessage(QVariant)));
524
connect(peer, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socketError(QAbstractSocket::SocketError)));
526
clientInfo.insert(peer, QVariantMap());
536
CoreAuthHandler *handler = new CoreAuthHandler(socket, this);
537
_connectingClients.insert(handler);
539
connect(handler, SIGNAL(disconnected()), SLOT(clientDisconnected()));
540
connect(handler, SIGNAL(socketError(QAbstractSocket::SocketError,QString)), SLOT(socketError(QAbstractSocket::SocketError,QString)));
541
connect(handler, SIGNAL(handshakeComplete(RemotePeer*,UserId)), SLOT(setupClientSession(RemotePeer*,UserId)));
527
543
quInfo() << qPrintable(tr("Client connected from")) << qPrintable(socket->peerAddress().toString());
529
545
if (!_configured) {
536
void Core::processClientMessage(const QVariant &data)
538
RemotePeer *peer = qobject_cast<RemotePeer *>(sender());
540
qWarning() << Q_FUNC_INFO << "Message not sent by RemoteConnection!";
544
QVariantMap msg = data.toMap();
545
if (!msg.contains("MsgType")) {
546
// Client is way too old, does not even use the current init format
547
qWarning() << qPrintable(tr("Antique client trying to connect... refusing."));
552
// OK, so we have at least an init message format we can understand
553
if (msg["MsgType"] == "ClientInit") {
556
// Just version information -- check it!
557
uint ver = msg["ProtocolVersion"].toUInt();
558
if (ver < Quassel::buildInfo().coreNeedsProtocol) {
559
reply["MsgType"] = "ClientInitReject";
560
reply["Error"] = tr("<b>Your Quassel Client is too old!</b><br>"
561
"This core needs at least client/core protocol version %1.<br>"
562
"Please consider upgrading your client.").arg(Quassel::buildInfo().coreNeedsProtocol);
563
peer->writeSocketData(reply);
564
qWarning() << qPrintable(tr("Client")) << peer->description() << qPrintable(tr("too old, rejecting."));
569
reply["ProtocolVersion"] = Quassel::buildInfo().protocolVersion;
570
reply["CoreVersion"] = Quassel::buildInfo().fancyVersionString;
571
reply["CoreDate"] = Quassel::buildInfo().buildDate;
572
reply["CoreStartTime"] = startTime(); // v10 clients don't necessarily parse this, see below
574
// FIXME: newer clients no longer use the hardcoded CoreInfo (for now), since it gets the
575
// time zone wrong. With the next protocol bump (10 -> 11), we should remove this
576
// or make it properly configurable.
578
int uptime = startTime().secsTo(QDateTime::currentDateTime().toUTC());
579
int updays = uptime / 86400; uptime %= 86400;
580
int uphours = uptime / 3600; uptime %= 3600;
581
int upmins = uptime / 60;
582
reply["CoreInfo"] = tr("<b>Quassel Core Version %1</b><br>"
584
"Up %3d%4h%5m (since %6)").arg(Quassel::buildInfo().fancyVersionString)
585
.arg(Quassel::buildInfo().buildDate)
586
.arg(updays).arg(uphours, 2, 10, QChar('0')).arg(upmins, 2, 10, QChar('0')).arg(startTime().toString(Qt::TextDate));
588
reply["CoreFeatures"] = (int)Quassel::features();
591
SslServer *sslServer = qobject_cast<SslServer *>(&_server);
592
QSslSocket *sslSocket = qobject_cast<QSslSocket *>(peer->socket());
593
bool supportSsl = sslServer && sslSocket && sslServer->isCertValid();
595
bool supportSsl = false;
598
#ifndef QT_NO_COMPRESS
599
bool supportsCompression = true;
601
bool supportsCompression = false;
604
reply["SupportSsl"] = supportSsl;
605
reply["SupportsCompression"] = supportsCompression;
606
// switch to ssl/compression after client has been informed about our capabilities (see below)
608
reply["LoginEnabled"] = true;
610
// check if we are configured, start wizard otherwise
612
reply["Configured"] = false;
613
QList<QVariant> backends;
614
foreach(Storage *backend, _storageBackends.values()) {
616
v["DisplayName"] = backend->displayName();
617
v["Description"] = backend->description();
618
v["SetupKeys"] = backend->setupKeys();
619
v["SetupDefaults"] = backend->setupDefaults();
622
reply["StorageBackends"] = backends;
623
reply["LoginEnabled"] = false;
626
reply["Configured"] = true;
628
clientInfo[peer] = msg; // store for future reference
629
reply["MsgType"] = "ClientInitAck";
630
peer->writeSocketData(reply);
631
peer->socket()->flush(); // ensure that the write cache is flushed before we switch to ssl
634
// after we told the client that we are ssl capable we switch to ssl mode
635
if (supportSsl && msg["UseSsl"].toBool()) {
636
qDebug() << qPrintable(tr("Starting TLS for Client:")) << peer->description();
637
connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError> &)), SLOT(sslErrors(const QList<QSslError> &)));
638
sslSocket->startServerEncryption();
642
#ifndef QT_NO_COMPRESS
643
if (supportsCompression && msg["UseCompression"].toBool()) {
644
peer->socket()->setProperty("UseCompression", true);
645
qDebug() << "Using compression for Client:" << qPrintable(peer->socket()->peerAddress().toString());
650
// for the rest, we need an initialized connection
651
if (!clientInfo.contains(peer)) {
653
reply["MsgType"] = "ClientLoginReject";
654
reply["Error"] = tr("<b>Client not initialized!</b><br>You need to send an init message before trying to login.");
655
peer->writeSocketData(reply);
656
qWarning() << qPrintable(tr("Client")) << qPrintable(peer->socket()->peerAddress().toString()) << qPrintable(tr("did not send an init message before trying to login, rejecting."));
657
peer->close(); return;
659
if (msg["MsgType"] == "CoreSetupData") {
661
QString result = setupCore(msg["SetupData"].toMap());
662
if (!result.isEmpty()) {
663
reply["MsgType"] = "CoreSetupReject";
664
reply["Error"] = result;
667
reply["MsgType"] = "CoreSetupAck";
669
peer->writeSocketData(reply);
671
else if (msg["MsgType"] == "ClientLogin") {
673
UserId uid = _storage->validateUser(msg["User"].toString(), msg["Password"].toString());
675
reply["MsgType"] = "ClientLoginReject";
676
reply["Error"] = tr("<b>Invalid username or password!</b><br>The username/password combination you supplied could not be found in the database.");
677
peer->writeSocketData(reply);
680
reply["MsgType"] = "ClientLoginAck";
681
peer->writeSocketData(reply);
682
quInfo() << qPrintable(tr("Client")) << qPrintable(peer->socket()->peerAddress().toString()) << qPrintable(tr("initialized and authenticated successfully as \"%1\" (UserId: %2).").arg(msg["User"].toString()).arg(uid.toInt()));
683
setupClientSession(peer, uid);
689
552
// Potentially called during the initialization phase (before handing the connection off to the session)
690
553
void Core::clientDisconnected()
692
RemotePeer *peer = qobject_cast<RemotePeer *>(sender());
555
CoreAuthHandler *handler = qobject_cast<CoreAuthHandler *>(sender());
695
quInfo() << qPrintable(tr("Non-authed client disconnected.")) << qPrintable(peer->socket()->peerAddress().toString());
696
clientInfo.remove(peer);
558
quInfo() << qPrintable(tr("Non-authed client disconnected:")) << qPrintable(handler->socket()->peerAddress().toString());
559
_connectingClients.remove(handler);
560
handler->deleteLater();
699
562
// make server listen again if still not configured
700
563
if (!_configured) {