From 2b901be5102a283c72ca2fd094842156a1d0d2df Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 18 Apr 2025 22:42:28 +0200 Subject: fix: gpgconf path should firstly be initialized at gpg context --- src/core/GpgCoreInit.cpp | 67 ------------------------------------ src/core/function/gpg/GpgContext.cpp | 24 ++++++------- 2 files changed, 12 insertions(+), 79 deletions(-) diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index e556cad4..b15a6800 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -387,56 +387,6 @@ 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; - } 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{}); @@ -553,10 +503,6 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { .value("gnupg/use_pinentry_as_password_input_dialog", !IsFlatpakENV()) .toBool(); - // try to restart all components - auto restart_all_gnupg_components_on_start = - settings.value("gnupg/restart_gpg_agent_on_start", false).toBool(); - // unit test mode if (args.unit_test_mode) { Module::UpsertRTValue("core", "env.state.basic", 1); @@ -608,11 +554,6 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { return -1; } -#if !(defined(_WIN32) || defined(WIN32)) - // auto config pinentry-program - EnsureGpgAgentConfHasPinentry(default_ctx); -#endif - Module::UpsertRTValue("core", "env.state.ctx", 1); if (!GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).FlushKeyCache()) { @@ -662,14 +603,6 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int { continue; } -#if defined(__linux__) - EnsureGpgAgentConfHasPinentry(ctx); -#endif - -#if defined(__APPLE__) && defined(__MACH__) - 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/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index 76e3df9f..b5b8b9e9 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -32,7 +32,6 @@ #include #include -#include #include #include "core/function/CoreSignalStation.h" @@ -58,7 +57,10 @@ class GpgContext::Impl { * @param args */ Impl(GpgContext *parent, const GpgContextInitArgs &args) - : parent_(parent), args_(args) { + : parent_(parent), + args_(args), + gpgconf_path_(Module::RetrieveRTValueTypedOrDefault<>( + "core", "gpgme.ctx.gpgconf_path", QString{})) { init(args); } @@ -207,6 +209,8 @@ class GpgContext::Impl { QMap component_dirs_; void init(const GpgContextInitArgs &args) { + assert(!gpgconf_path_.isEmpty()); + // init get_gpg_conf_dirs(); kill_gpg_agent(); @@ -282,10 +286,6 @@ class GpgContext::Impl { const GpgContextInitArgs &args) -> bool { assert(ctx != nullptr); - this->gpgconf_path_ = Module::RetrieveRTValueTypedOrDefault<>( - "core", "gpgme.ctx.gpgconf_path", QString{}); - assert(!gpgconf_path_.isEmpty()); - if (!gpgconf_path_.isEmpty()) { LOG_D() << "set gpgconf path: " << gpgconf_path_; auto err = gpgme_ctx_set_engine_info(ctx, GPGME_PROTOCOL_GPGCONF, @@ -427,15 +427,14 @@ class GpgContext::Impl { } auto kill_gpg_agent() -> bool { - const auto gpgconf = Module::RetrieveRTValueTypedOrDefault<>( - "core", "gpgme.ctx.gpgconf_path", QString{}); + auto gpgconf_path = QFileInfo(this->gpgconf_path_).absoluteFilePath(); - if (gpgconf.trimmed().isEmpty()) { + if (gpgconf_path.trimmed().isEmpty()) { LOG_E() << "gpgconf path is empty!"; return false; } - LOG_D() << "get gpgconf path: " << gpgconf; + LOG_D() << "get gpgconf path: " << gpgconf_path; auto args = QStringList{"--homedir", QDir::toNativeSeparators(HomeDirectory()), @@ -445,12 +444,13 @@ class GpgContext::Impl { << "channel:" << parent_->GetChannel(); QProcess process; - process.setProgram(gpgconf); + process.setProgram(gpgconf_path); process.setArguments(args); process.setProcessChannelMode(QProcess::MergedChannels); process.start(); if (!process.waitForFinished(3000)) { - LOG_W() << "timeout executing gpgconf: " << gpgconf << "ags: " << args; + LOG_W() << "timeout executing gpgconf: " << gpgconf_path + << "ags: " << args; return false; } -- cgit v1.2.3