// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include "SecretStore.h" #include "koutnet_crypto_debug.h" #include #include #include #include #include #if defined(KOUTNET_HAVE_KWALLET) #include #include #else // without KWallet the Wallet type never materialises: it is only ever a // forward-declared return type, and wallet() below always yields nullptr, so // the store degrades to the in-memory-only path with every write refused. namespace KWallet { class Wallet; } #endif namespace koutnet { namespace { // ── in-memory fallback (tests, CI, headless without kwalletd) ─────────────── QHash &memoryStore() { static QHash store; return store; } bool g_forceMemory = false; QString g_lastError; bool memoryForced() { return g_forceMemory || QStandardPaths::isTestModeEnabled(); } // Probed exactly once per process; shared by the query below and wallet(). bool g_walletProbeDone = false; bool g_walletUsable = false; #if defined(KOUTNET_HAVE_KWALLET) // ── KWallet backend ───────────────────────────────────────────────────────── constexpr char kFolder[] = "KOutNet"; KWallet::Wallet *g_wallet = nullptr; // Open the wallet once; a missing daemon is a permissive failure (no // persistence), never a hard crash. KWallet::Wallet *wallet() { if (g_walletProbeDone) return g_walletUsable ? g_wallet : nullptr; g_walletProbeDone = true; KWallet::Wallet *w = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), WId()); if (!w) { g_lastError = QStringLiteral("KWallet is not available - secrets will not be persisted"); qCWarning(KOUTNET_LOG_CRYPTO) << g_lastError; return nullptr; } const QString folder = QString::fromLatin1(kFolder); if (!w->hasFolder(folder)) { if (!w->createFolder(folder)) { g_lastError = QStringLiteral("could not create the %1 wallet folder").arg(folder); qCWarning(KOUTNET_LOG_CRYPTO) << g_lastError; return nullptr; } } if (!w->setFolder(folder)) { g_lastError = QStringLiteral("could not enter the %1 wallet folder").arg(folder); qCWarning(KOUTNET_LOG_CRYPTO) << g_lastError; return nullptr; } g_wallet = w; g_walletUsable = true; return w; } #else // KWallet was not found at configure time (Windows, non-KDE setups). There is // no second-best store: reads miss, writes are refused, and the callers // surface the degradation themselves. KWallet::Wallet *wallet() { return nullptr; } #endif } // namespace // ── public API ────────────────────────────────────────────────────────────── void SecretStore::setInMemoryOnly(bool enabled) { g_forceMemory = enabled; if (enabled) memoryStore().clear(); } bool SecretStore::isInMemoryOnly() { // this is a pure query; it deliberately does not open the wallet. return memoryForced() || (g_walletProbeDone && !g_walletUsable); } bool SecretStore::isAvailable() { if (memoryForced()) return true; return wallet() != nullptr; } bool SecretStore::read(const QString &key, QString *outValue) { if (memoryForced()) { const auto it = memoryStore().constFind(key); if (it == memoryStore().constEnd()) { g_lastError = QStringLiteral("no in-memory entry named %1").arg(key); return false; } if (outValue) *outValue = it.value(); return true; } KWallet::Wallet *w = wallet(); if (!w) { g_lastError = QStringLiteral("KWallet is unavailable - nothing to read"); return false; } QByteArray value; if (w->readEntry(key, value) != 0) { g_lastError = QStringLiteral("no wallet entry named %1").arg(key); return false; } if (outValue) *outValue = QString::fromUtf8(value); return true; } bool SecretStore::write(const QString &key, const QString &value) { if (memoryForced()) { memoryStore().insert(key, value); g_lastError.clear(); return true; } KWallet::Wallet *w = wallet(); if (!w) { g_lastError = QStringLiteral("KWallet is unavailable - the secret was not persisted"); return false; } if (w->writeEntry(key, value.toUtf8()) != 0) { g_lastError = QStringLiteral("could not write %1 to KWallet").arg(key); return false; } g_lastError.clear(); return true; } bool SecretStore::remove(const QString &key) { if (memoryForced()) { memoryStore().remove(key); g_lastError.clear(); return true; } KWallet::Wallet *w = wallet(); if (!w) { g_lastError = QStringLiteral("KWallet is unavailable - nothing to remove"); return false; } // Removing an entry that was never there is still success for our callers: // every removal path is an unconditional idempotent drop. w->removeEntry(key); g_lastError.clear(); return true; } bool SecretStore::purgePlaintextConfigKeys(const QStringList &keys, QString *outDetail) { QSettings settings; const QString path = settings.fileName(); bool removedAny = false; for (const QString &key : keys) { if (!settings.contains(key)) continue; settings.remove(key); removedAny = true; } QString syncFailure; if (removedAny) { settings.sync(); if (settings.status() != QSettings::NoError) { syncFailure = QStringLiteral( " (writing it failed - check its owner and " "permissions)"); } } QSettings verify; for (const QString &key : keys) { if (!verify.contains(key)) continue; if (outDetail) { *outDetail = QStringLiteral( "%1 is still readable through a config file that " "is not %2") .arg(key, path); } return false; } QFile file(path); if (!file.exists()) return true; if (!file.open(QIODevice::ReadOnly)) { if (outDetail) *outDetail = QStringLiteral("%1 could not be re-read to confirm the deletion").arg(path); return false; } const QByteArray contents = file.readAll(); file.close(); for (const QString &key : keys) { const QByteArray leaf = key.section(QLatin1Char('/'), -1).toUtf8(); if (!contents.contains(leaf)) continue; if (outDetail) { *outDetail = QStringLiteral("%1 still contains %2%3").arg(path, QString::fromUtf8(leaf), syncFailure); } return false; } return true; } QString SecretStore::lastError() { return g_lastError; } } // namespace koutnet