diff options
author | saturneric <[email protected]> | 2025-04-18 17:11:40 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2025-04-18 17:11:40 +0000 |
commit | 502a43488d51c88be33d95be11ba8f160c2a3fd4 (patch) | |
tree | 87ee50bc5fdb1ca89d71ccfc988b1e6f67550f83 /src/core | |
parent | feat: add more info check (diff) | |
download | GpgFrontend-502a43488d51c88be33d95be11ba8f160c2a3fd4.tar.gz GpgFrontend-502a43488d51c88be33d95be11ba8f160c2a3fd4.zip |
feat: add more basic env checks at init
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/GpgCoreInit.cpp | 63 | ||||
-rw-r--r-- | src/core/function/gpg/GpgAutomatonHandler.h | 3 | ||||
-rw-r--r-- | src/core/model/GpgKeyGenerateInfo.cpp | 26 | ||||
-rw-r--r-- | src/core/model/GpgKeyGenerateInfo.h | 6 | ||||
-rw-r--r-- | src/core/utils/GpgUtils.cpp | 19 | ||||
-rw-r--r-- | src/core/utils/GpgUtils.h | 6 |
6 files changed, 103 insertions, 20 deletions
diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index ff9bb231..1735c316 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -387,6 +387,60 @@ auto DecideGnuPGPath(const QString& default_gnupg_path) -> QString { return default_gnupg_path; } +void EnsureGpgAgentConfHasPinentry(GpgContext& ctx) { + auto pinentry_path = DecidePinentry(); + if (pinentry_path.isEmpty()) { + LOG_W() << "no suitable pinentry found."; + return; + } + + QDir gnupg_dir(ctx.HomeDirectory()); + if (!gnupg_dir.exists()) { + gnupg_dir.mkpath("."); + } + + QString config_path = gnupg_dir.filePath("gpg-agent.conf"); + QFile config_file(config_path); + QStringList lines; + + LOG_D() << "checking pinentry config at:" << gnupg_dir; + + bool has_pinentry_line = false; + if (config_file.exists()) { + if (config_file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QTextStream in(&config_file); + while (!in.atEnd()) { + QString line = in.readLine(); + if (line.trimmed().startsWith("pinentry-program")) { + has_pinentry_line = true; + } + lines.append(line); + } + config_file.close(); + } + } + + if (!has_pinentry_line) { + lines.append(QString("pinentry-program %1").arg(pinentry_path)); + if (config_file.open(QIODevice::WriteOnly | QIODevice::Text)) { + QTextStream out(&config_file); + for (const QString& line : lines) { + out << line << '\n'; + } + config_file.close(); + LOG_D() << "updated gpg-agent.conf with pinentry:" << pinentry_path; + + // reload configure + GpgAdvancedOperator::GetInstance(ctx.GetChannel()) + .ReloadAllGpgComponents(); + } else { + LOG_W() << "failed to write to gpg-agent.conf"; + } + } else { + LOG_D() << "gpg-agent.conf already contains pinentry-program"; + } +} + auto InitBasicPath() -> bool { auto default_gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>( "core", "gpgme.ctx.gpgconf_path", QString{}); @@ -548,6 +602,7 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { return ConvertToChannelObjectPtr<>(SecureCreateUniqueObject<GpgContext>( args, kGpgFrontendDefaultChannel)); }); + if (!default_ctx.Good()) { LOG_E() << "Init GpgME Default Context failed!" << "GpgFrontend cannot start under this situation!"; @@ -557,6 +612,10 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { return -1; } +#if defined(__linux__) + EnsureGpgAgentConfHasPinentry(default_ctx); +#endif + Module::UpsertRTValue("core", "env.state.ctx", 1); if (!GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).FlushKeyCache()) { @@ -606,6 +665,10 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { continue; } +#if defined(__linux__) + EnsureGpgAgentConfHasPinentry(ctx); +#endif + if (!GpgKeyGetter::GetInstance(ctx.GetChannel()).FlushKeyCache()) { LOG_E() << "gpgme context init key cache failed, index:" << channel_index; diff --git a/src/core/function/gpg/GpgAutomatonHandler.h b/src/core/function/gpg/GpgAutomatonHandler.h index 7ab2bf44..34c23455 100644 --- a/src/core/function/gpg/GpgAutomatonHandler.h +++ b/src/core/function/gpg/GpgAutomatonHandler.h @@ -28,14 +28,13 @@ #pragma once -#include "core/GpgFrontendCore.h" #include "core/function/basic/GpgFunctionObject.h" #include "core/function/gpg/GpgContext.h" #include "core/typedef/GpgTypedef.h" namespace GpgFrontend { -class GpgAutomatonHandler +class GPGFRONTEND_CORE_EXPORT GpgAutomatonHandler : public SingletonFunctionObject<GpgAutomatonHandler> { public: using Command = QString; diff --git a/src/core/model/GpgKeyGenerateInfo.cpp b/src/core/model/GpgKeyGenerateInfo.cpp index b1dcaa7a..0d8aa23c 100644 --- a/src/core/model/GpgKeyGenerateInfo.cpp +++ b/src/core/model/GpgKeyGenerateInfo.cpp @@ -30,8 +30,9 @@ #include <cassert> -#include "module/ModuleManager.h" -#include "utils/CommonUtils.h" +#include "core/module/ModuleManager.h" +#include "core/utils/CommonUtils.h" +#include "core/utils/GpgUtils.h" namespace GpgFrontend { @@ -98,14 +99,12 @@ const QContainer<KeyAlgo> KeyGenerateInfo::kSubKeyAlgos = { {"elg4096", "ELG-E", "ELG-E", 4096, kENCRYPT, "2.2.0"}, }; -auto KeyGenerateInfo::GetSupportedKeyAlgo() -> QContainer<KeyAlgo> { - const auto gnupg_version = Module::RetrieveRTValueTypedOrDefault<>( - "core", "gpgme.ctx.gnupg_version", QString{"2.0.0"}); - +auto KeyGenerateInfo::GetSupportedKeyAlgo(int channel) -> QContainer<KeyAlgo> { QContainer<KeyAlgo> algos; for (const auto &algo : kPrimaryKeyAlgos) { - if (!algo.IsSupported(gnupg_version)) continue; + if (!CheckGpgVersion(channel, algo.SupportedVersion())) continue; + algos.append(algo); } @@ -116,14 +115,13 @@ auto KeyGenerateInfo::GetSupportedKeyAlgo() -> QContainer<KeyAlgo> { return algos; } -auto KeyGenerateInfo::GetSupportedSubkeyAlgo() -> QContainer<KeyAlgo> { - const auto gnupg_version = Module::RetrieveRTValueTypedOrDefault<>( - "core", "gpgme.ctx.gnupg_version", QString{"2.0.0"}); - +auto KeyGenerateInfo::GetSupportedSubkeyAlgo(int channel) + -> QContainer<KeyAlgo> { QContainer<KeyAlgo> algos; for (const auto &algo : kSubKeyAlgos) { - if (!algo.IsSupported(gnupg_version)) continue; + if (!CheckGpgVersion(channel, algo.SupportedVersion())) continue; + algos.append(algo); } @@ -490,9 +488,7 @@ auto KeyAlgo::CanAuth() const -> bool { return auth_; } auto KeyAlgo::CanCert() const -> bool { return cert_; } -auto KeyAlgo::IsSupported(const QString &version) const -> bool { - return GFCompareSoftwareVersion(version, supported_version_) >= 0; -} +auto KeyAlgo::SupportedVersion() const -> QString { return supported_version_; } auto KeyAlgo::operator==(const KeyAlgo &o) const -> bool { return this->id_ == o.id_; diff --git a/src/core/model/GpgKeyGenerateInfo.h b/src/core/model/GpgKeyGenerateInfo.h index 2956a00d..b3f3b2c7 100644 --- a/src/core/model/GpgKeyGenerateInfo.h +++ b/src/core/model/GpgKeyGenerateInfo.h @@ -62,7 +62,7 @@ class GPGFRONTEND_CORE_EXPORT KeyAlgo { [[nodiscard]] auto CanCert() const -> bool; - [[nodiscard]] auto IsSupported(const QString &version) const -> bool; + [[nodiscard]] auto SupportedVersion() const -> QString; private: QString id_; @@ -96,14 +96,14 @@ class GPGFRONTEND_CORE_EXPORT KeyGenerateInfo : public QObject { * * @return const QContainer<KeyGenAlgo>& */ - static auto GetSupportedKeyAlgo() -> QContainer<KeyAlgo>; + static auto GetSupportedKeyAlgo(int channel) -> QContainer<KeyAlgo>; /** * @brief Get the Supported Subkey Algo object * * @return const QContainer<KeyGenAlgo>& */ - static auto GetSupportedSubkeyAlgo() -> QContainer<KeyAlgo>; + static auto GetSupportedSubkeyAlgo(int channel) -> QContainer<KeyAlgo>; /** * @brief diff --git a/src/core/utils/GpgUtils.cpp b/src/core/utils/GpgUtils.cpp index 13f6ef05..8b2abeb7 100644 --- a/src/core/utils/GpgUtils.cpp +++ b/src/core/utils/GpgUtils.cpp @@ -456,4 +456,23 @@ auto GPGFRONTEND_CORE_EXPORT CheckGpgVersion(int channel, return true; } + +auto GPGFRONTEND_CORE_EXPORT DecidePinentry() -> QString { +#ifdef __linux__ + QStringList preferred_list = {"pinentry-gnome3", + "pinentry-qt" + "pinentry-gtk2"}; +#else + QStringList preferred_list = {"pinentry-qt"}; +#endif + + for (const QString& name : preferred_list) { + QString path = QStandardPaths::findExecutable(name); + if (!path.isEmpty()) { + return path; + } + } + + return {}; +} } // namespace GpgFrontend diff --git a/src/core/utils/GpgUtils.h b/src/core/utils/GpgUtils.h index d0fe1701..f005bbfc 100644 --- a/src/core/utils/GpgUtils.h +++ b/src/core/utils/GpgUtils.h @@ -221,4 +221,10 @@ auto GPGFRONTEND_CORE_EXPORT GpgAgentVersionGreaterThan(int channel, auto GPGFRONTEND_CORE_EXPORT CheckGpgVersion(int channel, const QString&) -> bool; +/** + * @brief + * + * @return QString + */ +auto GPGFRONTEND_CORE_EXPORT DecidePinentry() -> QString; } // namespace GpgFrontend
\ No newline at end of file |