X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fcore.cpp;h=ad9eab8586ea239bb7e2f6c0c63c10589f762de7;hp=a86424f43ffa0b5b5c6ce336ea9cd6aad245a431;hb=10e6f4629e39c66cfb8db6ab2806bf8f13ec700b;hpb=921e54680da16fcf2adb7a90506875aceb6633a4 diff --git a/src/core/core.cpp b/src/core/core.cpp index a86424f4..ad9eab85 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-2015 by the Quassel Project * + * Copyright (C) 2005-2016 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -194,17 +194,24 @@ void Core::init() "to work.")); exit(1); // TODO make this less brutal (especially for mono client -> popup) } + qWarning() << "Core is currently not configured! Please connect with a Quassel Client for basic setup."; + + if (!cs.isWritable()) { + qWarning() << "Cannot write quasselcore configuration; probably a permission problem."; + exit(EXIT_FAILURE); + } + } if (Quassel::isOptionSet("add-user")) { - createUser(); - exit(0); + exit(createUser() ? EXIT_SUCCESS : EXIT_FAILURE); + } if (Quassel::isOptionSet("change-userpass")) { - changeUserPass(Quassel::optionValue("change-userpass")); - exit(0); + exit(changeUserPass(Quassel::optionValue("change-userpass")) ? + EXIT_SUCCESS : EXIT_FAILURE); } connect(&_server, SIGNAL(newConnection()), this, SLOT(incomingConnection())); @@ -222,7 +229,7 @@ Core::~Core() foreach(CoreAuthHandler *handler, _connectingClients) { handler->deleteLater(); // disconnect non authed clients } - qDeleteAll(sessions); + qDeleteAll(_sessions); qDeleteAll(_storageBackends); } @@ -234,7 +241,8 @@ void Core::saveState() CoreSettings s; QVariantMap state; QVariantList activeSessions; - foreach(UserId user, instance()->sessions.keys()) activeSessions << QVariant::fromValue(user); + foreach(UserId user, instance()->_sessions.keys()) + activeSessions << QVariant::fromValue(user); state["CoreStateVersion"] = 1; state["ActiveSessions"] = activeSessions; s.setCoreState(state); @@ -247,7 +255,7 @@ void Core::restoreState() // qWarning() << qPrintable(tr("Cannot restore a state for an unconfigured core!")); return; } - if (instance()->sessions.count()) { + if (instance()->_sessions.count()) { qWarning() << qPrintable(tr("Calling restoreState() even though active sessions exist!")); return; } @@ -265,7 +273,7 @@ void Core::restoreState() quInfo() << "Restoring previous core state..."; foreach(QVariant v, activeSessions) { UserId user = v.value(); - instance()->createSession(user, true); + instance()->sessionForUser(user, true); } } } @@ -291,7 +299,9 @@ QString Core::setupCore(const QString &adminUser, const QString &adminPassword, return tr("Could not setup storage!"); } - saveBackendSettings(backend, setupData); + if (!saveBackendSettings(backend, setupData)) { + return tr("Could not save backend settings, probably a permission problem."); + } quInfo() << qPrintable(tr("Creating admin user...")); _storage->addUser(adminUser, adminPassword); @@ -427,6 +437,23 @@ bool Core::sslSupported() } +bool Core::reloadCerts() +{ +#ifdef HAVE_SSL + SslServer *sslServerv4 = qobject_cast(&instance()->_server); + bool retv4 = sslServerv4->reloadCerts(); + + SslServer *sslServerv6 = qobject_cast(&instance()->_v6server); + bool retv6 = sslServerv6->reloadCerts(); + + return retv4 && retv6; +#else + // SSL not supported, don't mark configuration reload as failed + return true; +#endif +} + + bool Core::startListening() { // in mono mode we only start a local port if a port is specified in the cli call @@ -577,19 +604,7 @@ void Core::setupClientSession(RemotePeer *peer, UserId uid) handler->deleteLater(); // Find or create session for validated user - SessionThread *session; - if (sessions.contains(uid)) { - session = sessions[uid]; - } - else { - session = createSession(uid); - if (!session) { - qWarning() << qPrintable(tr("Could not initialize session for client:")) << qPrintable(peer->description()); - peer->close(); - peer->deleteLater(); - return; - } - } + sessionForUser(uid); // as we are currently handling an event triggered by incoming data on this socket // it is unsafe to directly move the socket to the client thread. @@ -610,14 +625,7 @@ void Core::customEvent(QEvent *event) void Core::addClientHelper(RemotePeer *peer, UserId uid) { // Find or create session for validated user - if (!sessions.contains(uid)) { - qWarning() << qPrintable(tr("Could not find a session for client:")) << qPrintable(peer->description()); - peer->close(); - peer->deleteLater(); - return; - } - - SessionThread *session = sessions[uid]; + SessionThread *session = sessionForUser(uid); session->addClient(peer); } @@ -643,26 +651,20 @@ void Core::setupInternalClientSession(InternalPeer *clientPeer) clientPeer->setPeer(corePeer); // Find or create session for validated user - SessionThread *sessionThread; - if (sessions.contains(uid)) - sessionThread = sessions[uid]; - else - sessionThread = createSession(uid); - + SessionThread *sessionThread = sessionForUser(uid); sessionThread->addClient(corePeer); } -SessionThread *Core::createSession(UserId uid, bool restore) +SessionThread *Core::sessionForUser(UserId uid, bool restore) { - if (sessions.contains(uid)) { - qWarning() << "Calling createSession() when a session for the user already exists!"; - return 0; - } - SessionThread *sess = new SessionThread(uid, restore, this); - sessions[uid] = sess; - sess->start(); - return sess; + if (_sessions.contains(uid)) + return _sessions[uid]; + + SessionThread *session = new SessionThread(uid, restore, this); + _sessions[uid] = session; + session->start(); + return session; } @@ -681,6 +683,7 @@ QVariantList Core::backendInfo() v["Description"] = backend->description(); v["SetupKeys"] = backend->setupKeys(); v["SetupDefaults"] = backend->setupDefaults(); + v["IsDefault"] = isStorageBackendDefault(backend); backends.append(v); } return backends; @@ -704,7 +707,9 @@ bool Core::selectBackend(const QString &backend) Storage::State storageState = storage->init(settings); switch (storageState) { case Storage::IsReady: - saveBackendSettings(backend, settings); + if (!saveBackendSettings(backend, settings)) { + qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem.")); + } qWarning() << "Switched backend to:" << qPrintable(backend); qWarning() << "Backend already initialized. Skipping Migration"; return true; @@ -722,7 +727,9 @@ bool Core::selectBackend(const QString &backend) return false; } - saveBackendSettings(backend, settings); + if (!saveBackendSettings(backend, settings)) { + qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem.")); + } qWarning() << "Switched backend to:" << qPrintable(backend); break; } @@ -738,7 +745,10 @@ bool Core::selectBackend(const QString &backend) storage = 0; if (reader->migrateTo(writer)) { qDebug() << "Migration finished!"; - saveBackendSettings(backend, settings); + if (!saveBackendSettings(backend, settings)) { + qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem.")); + return false; + } return true; } return false; @@ -763,7 +773,7 @@ bool Core::selectBackend(const QString &backend) } -void Core::createUser() +bool Core::createUser() { QTextStream out(stdout); QTextStream in(stdin); @@ -785,30 +795,32 @@ void Core::createUser() if (password != password2) { qWarning() << "Passwords don't match!"; - return; + return false; } if (password.isEmpty()) { qWarning() << "Password is empty!"; - return; + return false; } if (_configured && _storage->addUser(username, password).isValid()) { out << "Added user " << username << " successfully!" << endl; + return true; } else { qWarning() << "Unable to add user:" << qPrintable(username); + return false; } } -void Core::changeUserPass(const QString &username) +bool Core::changeUserPass(const QString &username) { QTextStream out(stdout); QTextStream in(stdin); UserId userId = _storage->getUserId(username); if (!userId.isValid()) { out << "User " << username << " does not exist." << endl; - return; + return false; } out << "Change password for user: " << username << endl; @@ -826,22 +838,33 @@ void Core::changeUserPass(const QString &username) if (password != password2) { qWarning() << "Passwords don't match!"; - return; + return false; } if (password.isEmpty()) { qWarning() << "Password is empty!"; - return; + return false; } if (_configured && _storage->updateUser(userId, password)) { out << "Password changed successfully!" << endl; + return true; } else { qWarning() << "Failed to change password!"; + return false; } } +bool Core::changeUserPassword(UserId userId, const QString &password) +{ + if (!isConfigured() || !userId.isValid()) + return false; + + return instance()->_storage->updateUser(userId, password); +} + + AbstractSqlMigrationReader *Core::getMigrationReader(Storage *storage) { if (!storage) @@ -872,12 +895,14 @@ AbstractSqlMigrationWriter *Core::getMigrationWriter(Storage *storage) } -void Core::saveBackendSettings(const QString &backend, const QVariantMap &settings) +bool Core::saveBackendSettings(const QString &backend, const QVariantMap &settings) { QVariantMap dbsettings; dbsettings["Backend"] = backend; dbsettings["ConnectionProperties"] = settings; - CoreSettings().setStorageSettings(dbsettings); + CoreSettings s = CoreSettings(); + s.setStorageSettings(dbsettings); + return s.sync(); }