+ }
+}
+
+DeferredSharedPtr<Authenticator> Core::authenticator(const QString& backendId) const
+{
+ auto it = std::find_if(_registeredAuthenticators.begin(),
+ _registeredAuthenticators.end(),
+ [backendId](const DeferredSharedPtr<Authenticator>& authenticator) {
+ return authenticator->backendId() == backendId;
+ });
+ return it != _registeredAuthenticators.end() ? *it : nullptr;
+}
+
+// FIXME: Apparently, this is the legacy way of initting storage backends?
+// If there's a not-legacy way, it should be used here
+bool Core::initAuthenticator(
+ const QString& backend, const QVariantMap& settings, const QProcessEnvironment& environment, bool loadFromEnvironment, bool setup)
+{
+ if (backend.isEmpty()) {
+ qWarning() << "No authenticator selected!";
+ return false;
+ }
+
+ auto auth = authenticator(backend);
+ if (!auth) {
+ qCritical() << "Selected auth backend is not available:" << backend;
+ return false;
+ }
+
+ Authenticator::State authState = auth->init(settings, environment, loadFromEnvironment);
+ switch (authState) {
+ case Authenticator::NeedsSetup:
+ if (!setup)
+ return false; // trigger setup process
+ if (auth->setup(settings, environment, loadFromEnvironment))
+ return initAuthenticator(backend, settings, environment, loadFromEnvironment, false);
+ return false;
+
+ case Authenticator::NotAvailable:
+ if (!setup) {
+ // If initialization wasn't successful, we quit to keep from coming up unconfigured
+ throw ExitException{EXIT_FAILURE, tr("Selected auth backend %1 is not available.").arg(backend)};
+ }
+ qCritical() << "Selected auth backend is not available:" << backend;
+ return false;
+
+ case Authenticator::IsReady:
+ // delete all other backends
+ _registeredAuthenticators.clear();
+ break;
+ }
+ _authenticator = std::move(auth);
+ return true;
+}
+
+/*** Network Management ***/
+
+bool Core::sslSupported()
+{
+ return instance()->_server.isCertValid() && instance()->_v6server.isCertValid();
+}
+
+bool Core::reloadCerts()
+{
+ bool retv4 = _server.reloadCerts();
+ bool retv6 = _v6server.reloadCerts();
+
+ return retv4 && retv6;
+}
+
+void Core::cacheSysIdent()
+{
+ if (isConfigured()) {
+ _authUserNames = _storage->getAllAuthUserNames();
+ }
+}
+
+QString Core::strictSysIdent(UserId user) const
+{
+ if (_authUserNames.contains(user)) {
+ return _authUserNames[user];
+ }
+
+ // A new user got added since we last pulled our cache from the database.
+ // There's no way to avoid a database hit - we don't even know the authname!
+ instance()->cacheSysIdent();
+
+ if (_authUserNames.contains(user)) {
+ return _authUserNames[user];
+ }
+
+ // ...something very weird is going on if we ended up here (an active CoreSession without a corresponding database entry?)
+ qWarning().nospace() << "Unable to find authusername for UserId " << user << ", this should never happen!";
+ return "unknown"; // Should we just terminate the program instead?
+}
+
+bool Core::startListening()
+{
+ // in mono mode we only start a local port if a port is specified in the cli call
+ if (Quassel::runMode() == Quassel::Monolithic && !Quassel::isOptionSet("port"))
+ return true;
+
+ bool success = false;
+ uint port = Quassel::optionValue("port").toUInt();
+
+ const QString listen = Quassel::optionValue("listen");
+ const QStringList listen_list = listen.split(",", QString::SkipEmptyParts);
+ if (listen_list.size() > 0) {
+ foreach (const QString listen_term, listen_list) { // TODO: handle multiple interfaces for same TCP version gracefully
+ QHostAddress addr;
+ if (!addr.setAddress(listen_term)) {
+ qCritical() << qPrintable(tr("Invalid listen address %1").arg(listen_term));
+ }
+ else {
+ switch (addr.protocol()) {
+ case QAbstractSocket::IPv6Protocol:
+ if (_v6server.listen(addr, port)) {
+ qInfo() << qPrintable(tr("Listening for GUI clients on IPv6 %1 port %2 using protocol version %3")
+ .arg(addr.toString())
+ .arg(_v6server.serverPort())
+ .arg(Quassel::buildInfo().protocolVersion));
+ success = true;
+ }
+ else
+ qWarning() << qPrintable(tr("Could not open IPv6 interface %1:%2: %3").arg(addr.toString()).arg(port).arg(_v6server.errorString()));
+ break;
+ case QAbstractSocket::IPv4Protocol:
+ if (_server.listen(addr, port)) {
+ qInfo() << qPrintable(tr("Listening for GUI clients on IPv4 %1 port %2 using protocol version %3")
+ .arg(addr.toString())
+ .arg(_server.serverPort())
+ .arg(Quassel::buildInfo().protocolVersion));
+ success = true;
+ }
+ else {
+ // if v6 succeeded on Any, the port will be already in use - don't display the error then
+ if (!success || _server.serverError() != QAbstractSocket::AddressInUseError)
+ qWarning() << qPrintable(tr("Could not open IPv4 interface %1:%2: %3").arg(addr.toString()).arg(port).arg(_server.errorString()));
+ }
+ break;
+ default:
+ qCritical() << qPrintable(tr("Invalid listen address %1, unknown network protocol").arg(listen_term));
+ break;
+ }
+ }
+ }
+ }
+ if (!success)
+ qCritical() << qPrintable(tr("Could not open any network interfaces to listen on!"));
+
+ if (_identServer) {
+ _identServer->startListening();
+ }
+
+ if (_metricsServer) {
+ _metricsServer->startListening();
+ }
+
+ return success;
+}
+
+void Core::stopListening(const QString& reason)
+{
+ if (_identServer) {
+ _identServer->stopListening(reason);
+ }