Delete authenticators in core destructor too
[quassel.git] / src / core / core.cpp
index b5bea2c..04dd3bb 100644 (file)
 #include "network.h"
 #include "postgresqlstorage.h"
 #include "quassel.h"
+#include "sqlauthenticator.h"
 #include "sqlitestorage.h"
 #include "util.h"
 
+// Currently building with LDAP bindings is optional.
+#ifdef HAVE_LDAP
+#include "ldapauthenticator.h"
+#endif
+
 // migration related
 #include <QFile>
 #ifdef Q_OS_WIN
@@ -83,7 +89,8 @@ void Core::destroy()
 
 Core::Core()
     : QObject(),
-      _storage(0)
+      _storage(0),
+      _authenticator(0)
 {
 #ifdef HAVE_UMASK
     umask(S_IRWXG | S_IRWXO);
@@ -168,6 +175,7 @@ Core::Core()
     }
 
     registerStorageBackends();
+    registerAuthenticatorBackends();
 
     connect(&_storageSyncTimer, SIGNAL(timeout()), this, SLOT(syncStorage()));
     _storageSyncTimer.start(10 * 60 * 1000); // 10 minutes
@@ -181,11 +189,17 @@ void Core::init()
     QVariantMap dbsettings = cs.storageSettings().toMap();
     _configured = initStorage(dbsettings.value("Backend").toString(), dbsettings.value("ConnectionProperties").toMap());
 
+    // Not entirely sure what is 'legacy' about the above, but it seems to be the way things work!
+    QVariantMap authSettings = cs.authSettings().toMap();
+    initAuthenticator(authSettings.value("AuthBackend").toString(), authSettings.value("ConnectionProperties").toMap());
+
     if (Quassel::isOptionSet("select-backend")) {
         selectBackend(Quassel::optionValue("select-backend"));
         exit(0);
     }
 
+    // TODO: add --select-authenticator command line option and code.
+
     if (!_configured) {
         if (!_storageBackends.count()) {
             qWarning() << qPrintable(tr("Could not initialize any storage backend! Exiting..."));
@@ -194,7 +208,14 @@ 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")) {
@@ -224,6 +245,7 @@ Core::~Core()
     }
     qDeleteAll(_sessions);
     qDeleteAll(_storageBackends);
+    qDeleteAll(_authenticatorBackends);
 }
 
 
@@ -274,13 +296,13 @@ void Core::restoreState()
 
 /*** Core Setup ***/
 
-QString Core::setup(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData)
+QString Core::setup(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData, const QString &authBackend, const QVariantMap &authSetupData)
 {
-    return instance()->setupCore(adminUser, adminPassword, backend, setupData);
+    return instance()->setupCore(adminUser, adminPassword, backend, setupData, authBackend, authSetupData);
 }
 
 
-QString Core::setupCore(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData)
+QString Core::setupCore(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData, const QString &authBackend, const QVariantMap &authSetupData)
 {
     if (_configured)
         return tr("Core is already configured! Not configuring again...");
@@ -292,7 +314,16 @@ QString Core::setupCore(const QString &adminUser, const QString &adminPassword,
         return tr("Could not setup storage!");
     }
 
-    saveBackendSettings(backend, setupData);
+    quInfo() << "Selected authenticator: " << authBackend;
+    if (!(_configured = initAuthenticator(authBackend, authSetupData, true)))
+    {
+        return tr("Could not setup authenticator!");
+    }
+
+    if (!saveBackendSettings(backend, setupData)) {
+        return tr("Could not save backend settings, probably a permission problem.");
+    }
+       saveAuthBackendSettings(authBackend, authSetupData);
 
     quInfo() << qPrintable(tr("Creating admin user..."));
     _storage->addUser(adminUser, adminPassword);
@@ -313,7 +344,7 @@ QString Core::setupCoreForInternalUsage()
     }
 
     // mono client currently needs sqlite
-    return setupCore("AdminUser", QString::number(pass), "SQLite", QVariantMap());
+    return setupCore("AdminUser", QString::number(pass), "SQLite", QVariantMap(), "StorageAuth", QVariantMap());
 }
 
 
@@ -338,7 +369,6 @@ bool Core::registerStorageBackend(Storage *backend)
     }
 }
 
-
 void Core::unregisterStorageBackends()
 {
     foreach(Storage *s, _storageBackends.values()) {
@@ -354,6 +384,45 @@ void Core::unregisterStorageBackend(Storage *backend)
     backend->deleteLater();
 }
 
+// Authentication handling, now independent from storage.
+// Register and unregister authenticators.
+
+void Core::registerAuthenticatorBackends()
+{
+    // Register new authentication backends here!
+    registerAuthenticatorBackend(new SqlAuthenticator(this));
+#ifdef HAVE_LDAP
+    registerAuthenticatorBackend(new LdapAuthenticator(this));
+#endif
+
+}
+
+bool Core::registerAuthenticatorBackend(Authenticator *authenticator)
+{
+    if (authenticator->isAvailable())
+    {
+        _authenticatorBackends[authenticator->displayName()] = authenticator;
+        return true;
+    } else {
+        authenticator->deleteLater();
+        return false;
+    }
+}
+
+void Core::unregisterAuthenticatorBackends()
+{
+    foreach(Authenticator* a, _authenticatorBackends.values())
+    {
+        a->deleteLater();
+    }
+    _authenticatorBackends.clear();
+}
+
+void Core::unregisterAuthenticatorBackend(Authenticator *backend)
+{
+    _authenticatorBackends.remove(backend->displayName());
+    backend->deleteLater();
+}
 
 // old db settings:
 // "Type" => "sqlite"
@@ -395,6 +464,43 @@ bool Core::initStorage(const QString &backend, const QVariantMap &settings, bool
     return true;
 }
 
+// XXX: TODO: Apparently, this is legacy?
+bool Core::initAuthenticator(const QString &backend, const QVariantMap &settings, bool setup)
+{
+    _authenticator = 0;
+
+    if (backend.isEmpty()) {
+            return false;
+    }
+
+    Authenticator *authenticator = 0;
+    if (_authenticatorBackends.contains(backend)) {
+        authenticator = _authenticatorBackends[backend];
+    }
+    else {
+        qCritical() << "Selected auth backend is not available:" << backend;
+        return false;
+    }
+
+    Authenticator::State authState = authenticator->init(settings);
+    switch (authState) {
+    case Authenticator::NeedsSetup:
+        if (!setup)
+            return false;  // trigger setup process
+        if (authenticator->setup(settings))
+            return initAuthenticator(backend, settings, false);
+    // if initialization wasn't successful, we quit to keep from coming up unconfigured
+    case Authenticator::NotAvailable:
+        qCritical() << "FATAL: Selected auth backend is not available:" << backend;
+        exit(EXIT_FAILURE);
+    case Authenticator::IsReady:
+        // delete all other backends
+        _authenticatorBackends.remove(backend);
+        unregisterAuthenticatorBackends();
+    }
+    _authenticator = authenticator;
+    return true;
+}
 
 void Core::syncStorage()
 {
@@ -680,6 +786,19 @@ QVariantList Core::backendInfo()
     return backends;
 }
 
+QVariantList Core::authenticatorInfo()
+{
+    QVariantList backends;
+    foreach(const Authenticator *backend, instance()->_authenticatorBackends.values()) {
+        QVariantMap v;
+        v["DisplayName"] = backend->displayName();
+        v["Description"] = backend->description();
+        v["SetupKeys"] = backend->setupKeys();
+        v["SetupDefaults"] = backend->setupDefaults();
+        backends.append(v);
+    }
+    return backends;
+}
 
 // migration / backend selection
 bool Core::selectBackend(const QString &backend)
@@ -698,7 +817,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;
@@ -716,7 +837,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;
     }
@@ -732,7 +855,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;
@@ -879,12 +1005,22 @@ 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();
+}
+
+void Core::saveAuthBackendSettings(const QString &backend, const QVariantMap &settings)
+{
+    QVariantMap dbsettings;
+    dbsettings["AuthBackend"] = backend;
+    dbsettings["ConnectionProperties"] = settings;
+    CoreSettings().setAuthSettings(dbsettings);
 }