-// Potentially called during the initialization phase (before handing the connection off to the session)
-void Core::clientDisconnected() {
- QTcpSocket *socket = dynamic_cast<QTcpSocket*>(sender()); // Note: This might be a QObject* already (if called by ~Core())!
- Q_ASSERT(socket);
- blocksizes.remove(socket);
- clientInfo.remove(socket);
- qDebug() << qPrintable(tr("Non-authed client disconnected."));
- socket->deleteLater();
- socket = 0;
-
- // make server listen again if still not configured
- if (!configured) {
- startListening();
- }
-
- // TODO remove unneeded sessions - if necessary/possible...
- // Suggestion: kill sessions if they are not connected to any network and client.
-}
-
-void Core::setupClientSession(QTcpSocket *socket, UserId uid) {
- // Find or create session for validated user
- SessionThread *sess;
- if(sessions.contains(uid)) sess = sessions[uid];
- else sess = createSession(uid);
- // Hand over socket, session then sends state itself
- disconnect(socket, 0, this, 0);
- blocksizes.remove(socket);
- clientInfo.remove(socket);
- if(!sess) {
- qWarning() << qPrintable(tr("Could not initialize session for client %1!").arg(socket->peerAddress().toString()));
- socket->close();
- }
- sess->addClient(socket);
-}
-
-SessionThread *Core::createSession(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;
+// migration / backend selection
+bool Core::selectBackend(const QString &backend)
+{
+ // reregister all storage backends
+ registerStorageBackends();
+ auto storage = storageBackend(backend);
+ if (!storage) {
+ QStringList backends;
+ std::transform(_registeredStorageBackends.begin(), _registeredStorageBackends.end(),
+ std::back_inserter(backends), [](const DeferredSharedPtr<Storage>& backend) {
+ return backend->displayName();
+ });
+ quWarning() << qPrintable(tr("Unsupported storage backend: %1").arg(backend));
+ quWarning() << qPrintable(tr("Supported backends are:")) << qPrintable(backends.join(", "));
+ return false;
+ }
+
+ QVariantMap settings = promptForSettings(storage.get());
+
+ Storage::State storageState = storage->init(settings);
+ switch (storageState) {
+ case Storage::IsReady:
+ if (!saveBackendSettings(backend, settings)) {
+ qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem."));
+ }
+ quWarning() << qPrintable(tr("Switched storage backend to: %1").arg(backend));
+ quWarning() << qPrintable(tr("Backend already initialized. Skipping Migration..."));
+ return true;
+ case Storage::NotAvailable:
+ qCritical() << qPrintable(tr("Storage backend is not available: %1").arg(backend));
+ return false;
+ case Storage::NeedsSetup:
+ if (!storage->setup(settings)) {
+ quWarning() << qPrintable(tr("Unable to setup storage backend: %1").arg(backend));
+ return false;
+ }
+
+ if (storage->init(settings) != Storage::IsReady) {
+ quWarning() << qPrintable(tr("Unable to initialize storage backend: %1").arg(backend));
+ return false;
+ }
+
+ if (!saveBackendSettings(backend, settings)) {
+ qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem."));
+ }
+ quWarning() << qPrintable(tr("Switched storage backend to: %1").arg(backend));
+ break;
+ }
+
+ // let's see if we have a current storage object we can migrate from
+ auto reader = getMigrationReader(_storage.get());
+ auto writer = getMigrationWriter(storage.get());
+ if (reader && writer) {
+ qDebug() << qPrintable(tr("Migrating storage backend %1 to %2...").arg(_storage->displayName(), storage->displayName()));
+ _storage.reset();
+ storage.reset();
+ if (reader->migrateTo(writer.get())) {
+ qDebug() << "Migration finished!";
+ qDebug() << qPrintable(tr("Migration finished!"));
+ if (!saveBackendSettings(backend, settings)) {
+ qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem."));
+ return false;
+ }
+ return true;
+ }
+ quWarning() << qPrintable(tr("Unable to migrate storage backend! (No migration writer for %1)").arg(backend));
+ return false;
+ }
+
+ // inform the user why we cannot merge
+ if (!_storage) {
+ quWarning() << qPrintable(tr("No currently active storage backend. Skipping migration..."));
+ }
+ else if (!reader) {
+ quWarning() << qPrintable(tr("Currently active storage backend does not support migration: %1").arg(_storage->displayName()));
+ }
+ if (writer) {
+ quWarning() << qPrintable(tr("New storage backend does not support migration: %1").arg(backend));
+ }
+
+ // so we were unable to merge, but let's create a user \o/
+ _storage = std::move(storage);
+ createUser();
+ return true;
+}
+
+// TODO: I am not sure if this function is implemented correctly.
+// There is currently no concept of migraiton between auth backends.
+bool Core::selectAuthenticator(const QString &backend)
+{
+ // Register all authentication backends.
+ registerAuthenticators();
+ auto auther = authenticator(backend);
+ if (!auther) {
+ QStringList authenticators;
+ std::transform(_registeredAuthenticators.begin(), _registeredAuthenticators.end(),
+ std::back_inserter(authenticators), [](const DeferredSharedPtr<Authenticator>& authenticator) {
+ return authenticator->displayName();
+ });
+ quWarning() << qPrintable(tr("Unsupported authenticator: %1").arg(backend));
+ quWarning() << qPrintable(tr("Supported authenticators are:")) << qPrintable(authenticators.join(", "));
+ return false;
+ }
+
+ QVariantMap settings = promptForSettings(auther.get());
+
+ Authenticator::State state = auther->init(settings);
+ switch (state) {
+ case Authenticator::IsReady:
+ saveAuthenticatorSettings(backend, settings);
+ quWarning() << qPrintable(tr("Switched authenticator to: %1").arg(backend));
+ return true;
+ case Authenticator::NotAvailable:
+ qCritical() << qPrintable(tr("Authenticator is not available: %1").arg(backend));
+ return false;
+ case Authenticator::NeedsSetup:
+ if (!auther->setup(settings)) {
+ quWarning() << qPrintable(tr("Unable to setup authenticator: %1").arg(backend));
+ return false;
+ }
+
+ if (auther->init(settings) != Authenticator::IsReady) {
+ quWarning() << qPrintable(tr("Unable to initialize authenticator: %1").arg(backend));
+ return false;
+ }
+
+ saveAuthenticatorSettings(backend, settings);
+ quWarning() << qPrintable(tr("Switched authenticator to: %1").arg(backend));
+ }
+
+ _authenticator = std::move(auther);
+ return true;
+}
+
+
+bool Core::createUser()
+{
+ QTextStream out(stdout);
+ QTextStream in(stdin);
+ out << "Add a new user:" << endl;
+ out << "Username: ";
+ out.flush();
+ QString username = in.readLine().trimmed();
+
+ disableStdInEcho();
+ out << "Password: ";
+ out.flush();
+ QString password = in.readLine().trimmed();
+ out << endl;
+ out << "Repeat Password: ";
+ out.flush();
+ QString password2 = in.readLine().trimmed();
+ out << endl;
+ enableStdInEcho();
+
+ if (password != password2) {
+ quWarning() << "Passwords don't match!";
+ return false;
+ }
+ if (password.isEmpty()) {
+ quWarning() << "Password is empty!";
+ return false;
+ }
+
+ if (_configured && _storage->addUser(username, password).isValid()) {
+ out << "Added user " << username << " successfully!" << endl;
+ return true;
+ }
+ else {
+ quWarning() << "Unable to add user:" << qPrintable(username);
+ return false;
+ }
+}
+
+
+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 false;
+ }
+
+ if (!canChangeUserPassword(userId)) {
+ out << "User " << username << " is configured through an auth provider that has forbidden manual password changing." << endl;
+ return false;
+ }
+
+ out << "Change password for user: " << username << endl;
+
+ disableStdInEcho();
+ out << "New Password: ";
+ out.flush();
+ QString password = in.readLine().trimmed();
+ out << endl;
+ out << "Repeat Password: ";
+ out.flush();
+ QString password2 = in.readLine().trimmed();
+ out << endl;
+ enableStdInEcho();
+
+ if (password != password2) {
+ quWarning() << "Passwords don't match!";
+ return false;
+ }
+ if (password.isEmpty()) {
+ quWarning() << "Password is empty!";
+ return false;
+ }
+
+ if (_configured && _storage->updateUser(userId, password)) {
+ out << "Password changed successfully!" << endl;
+ return true;
+ }
+ else {
+ quWarning() << "Failed to change password!";
+ return false;
+ }
+}
+
+
+bool Core::changeUserPassword(UserId userId, const QString &password)
+{
+ if (!isConfigured() || !userId.isValid())
+ return false;
+
+ if (!canChangeUserPassword(userId))
+ return false;
+
+ return instance()->_storage->updateUser(userId, password);