From 37a62a8d0b590710b902376ca3320cc05e954b63 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 26 Jul 2024 18:24:34 +0200 Subject: feat: remove spdlog and clean up log --- src/core/function/gpg/GpgContext.cpp | 68 ++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'src/core/function/gpg/GpgContext.cpp') diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index 7dec318e..16898375 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -77,8 +77,8 @@ class GpgContext::Impl { [[nodiscard]] auto Good() const -> bool { return good_; } - auto SetPassphraseCb(const gpgme_ctx_t &ctx, gpgme_passphrase_cb_t cb) - -> bool { + auto SetPassphraseCb(const gpgme_ctx_t &ctx, + gpgme_passphrase_cb_t cb) -> bool { if (gpgme_get_pinentry_mode(ctx) != GPGME_PINENTRY_MODE_LOOPBACK) { if (CheckGpgError(gpgme_set_pinentry_mode( ctx, GPGME_PINENTRY_MODE_LOOPBACK)) != GPG_ERR_NO_ERROR) { @@ -130,26 +130,26 @@ class GpgContext::Impl { passphrase_info != nullptr ? passphrase_info : "", prev_was_bad != 0, ask_for_new)); - GF_CORE_LOG_DEBUG( - "custom passphrase cb called, uid: {}, info: {}, last_was_bad: {}", - uid_hint == nullptr ? "" : QString{uid_hint}, - passphrase_info == nullptr ? "" : QString{passphrase_info}, - prev_was_bad); + qCDebug(core) << "custom passphrase cb called, uid: " + << (uid_hint == nullptr ? "" : QString{uid_hint}) + << ", info: " + << (passphrase_info == nullptr ? "" + : QString{passphrase_info}) + << ", last_was_bad: " << prev_was_bad; QEventLoop looper; QObject::connect(CoreSignalStation::GetInstance(), &CoreSignalStation::SignalUserInputPassphraseCallback, &looper, &QEventLoop::quit); - emit CoreSignalStation::GetInstance()->SignalNeedUserInputPassphrase( - context); + emit CoreSignalStation::GetInstance() -> SignalNeedUserInputPassphrase( + context); looper.exec(); ResetCacheValue("PinentryContext"); auto passphrase = context->GetPassphrase().toStdString(); auto passpahrase_size = passphrase.size(); - GF_CORE_LOG_DEBUG("get passphrase from pinentry size: {}", - passpahrase_size); + qCDebug(core, "get passphrase from pinentry size: %lu", passpahrase_size); size_t res = 0; if (passpahrase_size > 0) { @@ -162,15 +162,15 @@ class GpgContext::Impl { res += gpgme_io_write(fd, "\n", 1); - GF_CORE_LOG_DEBUG("custom passphrase cd is about to return, res: {}", res); + qCDebug(core, "custom passphrase cd is about to return, res: %ld", res); return res == passpahrase_size + 1 ? 0 : gpgme_error_from_errno(GPG_ERR_CANCELED); } - static auto TestStatusCb(void *hook, const char *keyword, const char *args) - -> gpgme_error_t { - GF_CORE_LOG_DEBUG("keyword {}", keyword); + static auto TestStatusCb(void *hook, const char *keyword, + const char *args) -> gpgme_error_t { + qCDebug(core, "keyword %s", keyword); return GPG_ERR_NO_ERROR; } @@ -188,10 +188,11 @@ class GpgContext::Impl { const auto gpgme_version = Module::RetrieveRTValueTypedOrDefault<>( "core", "gpgme.version", QString{"0.0.0"}); - GF_CORE_LOG_DEBUG("got gpgme version version from rt: {}", gpgme_version); + qCDebug(core) << "got gpgme version version from rt: " << gpgme_version; if (gpgme_get_keylist_mode(ctx) == 0) { - GF_CORE_LOG_ERROR( + qCWarning( + core, "ctx is not a valid pointer, reported by gpgme_get_keylist_mode"); return false; } @@ -210,8 +211,8 @@ class GpgContext::Impl { const auto database_path = Module::RetrieveRTValueTypedOrDefault<>( "core", "gpgme.ctx.database_path", QString{}); - GF_CORE_LOG_DEBUG("ctx set engine info, db path: {}, app path: {}", - database_path, app_path); + qCDebug(core) << "ctx set engine info, db path: " << database_path + << ", app path: " << app_path; auto app_path_buffer = app_path.toUtf8(); auto database_path_buffer = database_path.toUtf8(); @@ -232,23 +233,22 @@ class GpgContext::Impl { assert(ctx != nullptr); if (args.custom_gpgconf && !args.custom_gpgconf_path.isEmpty()) { - GF_CORE_LOG_DEBUG("set custom gpgconf path: {}", - args.custom_gpgconf_path); + qCDebug(core) << "set custom gpgconf path: " << args.custom_gpgconf_path; auto err = gpgme_ctx_set_engine_info(ctx, GPGME_PROTOCOL_GPGCONF, args.custom_gpgconf_path.toUtf8(), nullptr); if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { - GF_CORE_LOG_ERROR("set gpg context engine info error: {}", - DescribeGpgErrCode(err).second); + qCWarning(core) << "set gpg context engine info error: " + << DescribeGpgErrCode(err).second; return false; } } // set context offline mode - GF_CORE_LOG_DEBUG("gpg context: offline mode: {}", args_.offline_mode); - GF_CORE_LOG_DEBUG("gpg context: auto import missing key: {}", - args_.auto_import_missing_key); + qCDebug(core, "gpg context: offline mode: %d", args_.offline_mode); + qCDebug(core, "gpg context: auto import missing key: %d", + args_.auto_import_missing_key); gpgme_set_offline(ctx, args_.offline_mode ? 1 : 0); // set option auto import missing key @@ -260,19 +260,19 @@ class GpgContext::Impl { } if (!set_ctx_key_list_mode(ctx)) { - GF_CORE_LOG_DEBUG("set ctx key list mode failed"); + qCDebug(core, "set ctx key list mode failed"); return false; } // for unit test if (args_.test_mode) { if (!SetPassphraseCb(ctx, TestPassphraseCb)) { - GF_CORE_LOG_ERROR("set passphrase cb failed, test"); + qCWarning(core, "set passphrase cb failed, test"); return false; }; } else if (!args_.use_pinentry) { if (!SetPassphraseCb(ctx, CustomPassphraseCb)) { - GF_CORE_LOG_DEBUG("set passphrase cb failed, custom"); + qCDebug(core, "set passphrase cb failed, custom"); return false; } } @@ -283,7 +283,7 @@ class GpgContext::Impl { } if (!set_ctx_openpgp_engine_info(ctx)) { - GF_CORE_LOG_ERROR("set gpgme context openpgp engine info failed"); + qCWarning(core, "set gpgme context openpgp engine info failed"); return false; } @@ -293,15 +293,15 @@ class GpgContext::Impl { auto binary_ctx_initialize(const GpgContextInitArgs &args) -> bool { gpgme_ctx_t p_ctx; if (auto err = CheckGpgError(gpgme_new(&p_ctx)); err != GPG_ERR_NO_ERROR) { - GF_CORE_LOG_ERROR("get new gpg context error: {}", - DescribeGpgErrCode(err).second); + qCWarning(core) << "get new gpg context error: " + << DescribeGpgErrCode(err).second; return false; } assert(p_ctx != nullptr); binary_ctx_ref_ = p_ctx; if (!common_ctx_initialize(binary_ctx_ref_, args)) { - GF_CORE_LOG_ERROR("get new ctx failed, binary"); + qCWarning(core, "get new ctx failed, binary"); return false; } @@ -312,7 +312,7 @@ class GpgContext::Impl { auto default_ctx_initialize(const GpgContextInitArgs &args) -> bool { gpgme_ctx_t p_ctx; if (CheckGpgError(gpgme_new(&p_ctx)) != GPG_ERR_NO_ERROR) { - GF_CORE_LOG_ERROR("get new ctx failed, default"); + qCWarning(core, "get new ctx failed, default"); return false; } assert(p_ctx != nullptr); -- cgit v1.2.3 From 3d84beaf22caaf299a3004032402bd94ee4ab886 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 26 Jul 2024 18:33:09 +0200 Subject: fix: clean up warnings --- src/core/function/gpg/GpgContext.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/core/function/gpg/GpgContext.cpp') diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index 16898375..e0869188 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -32,6 +32,7 @@ #include #include +#include #include #include "core/function/CoreSignalStation.h" @@ -100,7 +101,7 @@ class GpgContext::Impl { QString pass = "abcdefg\n"; #endif - auto passpahrase_size = pass.size(); + auto passphrase_size = pass.size(); size_t off = 0; do { @@ -108,13 +109,13 @@ class GpgContext::Impl { const char *p_pass = pass.data(); res = gpgme_io_write(fd, &p_pass[off], passpahrase_size - off); #else - res = gpgme_io_write(fd, &pass[off], passpahrase_size - off); + res = gpgme_io_write(fd, &pass[off], passphrase_size - off); #endif if (res > 0) off += res; - } while (res > 0 && off != passpahrase_size); + } while (res > 0 && static_cast(off) != passphrase_size); res += gpgme_io_write(fd, "\n", 1); - return res == passpahrase_size + 1 + return static_cast(res) == (passphrase_size + 1) ? 0 : gpgme_error_from_errno(GPG_ERR_CANCELED); } -- cgit v1.2.3 From e8efa4d104a889f60b2096510a7c492d74ed6b66 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sat, 27 Jul 2024 14:18:56 +0200 Subject: feat: move pinentry out of source code --- src/core/function/gpg/GpgContext.cpp | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'src/core/function/gpg/GpgContext.cpp') diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index e0869188..d89ce92a 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -139,32 +139,41 @@ class GpgContext::Impl { << ", last_was_bad: " << prev_was_bad; QEventLoop looper; - QObject::connect(CoreSignalStation::GetInstance(), - &CoreSignalStation::SignalUserInputPassphraseCallback, - &looper, &QEventLoop::quit); + QString passphrase = ""; + + Module::TriggerEvent( + "REQUEST_PIN_ENTRY", + {{"uid_hint", uid_hint != nullptr ? uid_hint : ""}, + {"passphrase_info", passphrase_info != nullptr ? passphrase_info : ""}, + {"prev_was_bad", prev_was_bad ? "1" : "0"}, + {"ask_for_new", ask_for_new ? "1" : "0"}}, + [&passphrase, &looper](Module::EventIdentifier i, + Module::Event::ListenerIdentifier ei, + Module::Event::Params p) { + qCWarning(core) << "REQUEST_PIN_ENTRY callback: " << i << ei << p; + passphrase = p["passphrase"]; + looper.quit(); + }); - emit CoreSignalStation::GetInstance() -> SignalNeedUserInputPassphrase( - context); looper.exec(); - ResetCacheValue("PinentryContext"); - auto passphrase = context->GetPassphrase().toStdString(); - auto passpahrase_size = passphrase.size(); - qCDebug(core, "get passphrase from pinentry size: %lu", passpahrase_size); + + auto passphrase_size = passphrase.size(); + qCWarning(core, "get passphrase from pinentry size: %lld", passphrase_size); size_t res = 0; - if (passpahrase_size > 0) { + if (passphrase_size > 0) { size_t off = 0; do { - res = gpgme_io_write(fd, &passphrase[off], passpahrase_size - off); + res = gpgme_io_write(fd, &passphrase[off], passphrase_size - off); if (res > 0) off += res; - } while (res > 0 && off != passpahrase_size); + } while (res > 0 && off != passphrase_size); } res += gpgme_io_write(fd, "\n", 1); qCDebug(core, "custom passphrase cd is about to return, res: %ld", res); - return res == passpahrase_size + 1 + return res == passphrase_size + 1 ? 0 : gpgme_error_from_errno(GPG_ERR_CANCELED); } -- cgit v1.2.3 From 0a61ccd17aec7712e42a47da04f0af367609dadd Mon Sep 17 00:00:00 2001 From: saturneric Date: Sat, 27 Jul 2024 14:22:01 +0200 Subject: fix: do some clean up for removal of pinentry --- src/core/function/gpg/GpgContext.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/core/function/gpg/GpgContext.cpp') diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index d89ce92a..c6cb75da 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -145,12 +145,11 @@ class GpgContext::Impl { "REQUEST_PIN_ENTRY", {{"uid_hint", uid_hint != nullptr ? uid_hint : ""}, {"passphrase_info", passphrase_info != nullptr ? passphrase_info : ""}, - {"prev_was_bad", prev_was_bad ? "1" : "0"}, + {"prev_was_bad", (prev_was_bad != 0) ? "1" : "0"}, {"ask_for_new", ask_for_new ? "1" : "0"}}, [&passphrase, &looper](Module::EventIdentifier i, Module::Event::ListenerIdentifier ei, Module::Event::Params p) { - qCWarning(core) << "REQUEST_PIN_ENTRY callback: " << i << ei << p; passphrase = p["passphrase"]; looper.quit(); }); @@ -159,7 +158,7 @@ class GpgContext::Impl { ResetCacheValue("PinentryContext"); auto passphrase_size = passphrase.size(); - qCWarning(core, "get passphrase from pinentry size: %lld", passphrase_size); + qCDebug(core, "get passphrase from pinentry size: %lld", passphrase_size); size_t res = 0; if (passphrase_size > 0) { @@ -280,7 +279,8 @@ class GpgContext::Impl { qCWarning(core, "set passphrase cb failed, test"); return false; }; - } else if (!args_.use_pinentry) { + } else if (!args_.use_pinentry && + Module::IsModuleActivate(kPinentryModuleID)) { if (!SetPassphraseCb(ctx, CustomPassphraseCb)) { qCDebug(core, "set passphrase cb failed, custom"); return false; -- cgit v1.2.3 From 8bb3bf646e0f3ba7b5d9909fc773795d58f780f8 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 28 Jul 2024 14:21:13 +0200 Subject: fix: passphrase corrected is not correct as the real one --- src/core/function/gpg/GpgContext.cpp | 64 ++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 36 deletions(-) (limited to 'src/core/function/gpg/GpgContext.cpp') diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index c6cb75da..75e97e0c 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -93,31 +93,24 @@ class GpgContext::Impl { static auto TestPassphraseCb(void *opaque, const char *uid_hint, const char *passphrase_info, int last_was_bad, int fd) -> gpgme_error_t { - size_t res; -#ifdef QT5_BUILD - QString pass_qstr = "abcdefg\n"; - QByteArray pass = pass_qstr.toUtf8(); -#else - QString pass = "abcdefg\n"; -#endif - - auto passphrase_size = pass.size(); - size_t off = 0; - - do { -#ifdef QT5_BUILD - const char *p_pass = pass.data(); - res = gpgme_io_write(fd, &p_pass[off], passpahrase_size - off); -#else - res = gpgme_io_write(fd, &pass[off], passphrase_size - off); -#endif - if (res > 0) off += res; - } while (res > 0 && static_cast(off) != passphrase_size); + QString passphrase = "abcdefg"; + auto pass_bytes = passphrase.toLatin1(); + auto pass_size = pass_bytes.size(); + const auto *p_pass_bytes = pass_bytes.constData(); + + ssize_t res = 0; + if (pass_size > 0) { + ssize_t off = 0; + ssize_t ret = 0; + do { + ret = gpgme_io_write(fd, &p_pass_bytes[off], pass_size - off); + if (ret > 0) off += ret; + } while (ret > 0 && off != pass_size); + res = off; + } res += gpgme_io_write(fd, "\n", 1); - return static_cast(res) == (passphrase_size + 1) - ? 0 - : gpgme_error_from_errno(GPG_ERR_CANCELED); + return res == pass_size + 1 ? 0 : gpgme_error_from_errno(GPG_ERR_CANCELED); } static auto CustomPassphraseCb(void *hook, const char *uid_hint, @@ -157,24 +150,23 @@ class GpgContext::Impl { looper.exec(); ResetCacheValue("PinentryContext"); - auto passphrase_size = passphrase.size(); - qCDebug(core, "get passphrase from pinentry size: %lld", passphrase_size); + auto pass_bytes = passphrase.toLatin1(); + auto pass_size = pass_bytes.size(); + const auto *p_pass_bytes = pass_bytes.constData(); - size_t res = 0; - if (passphrase_size > 0) { - size_t off = 0; + ssize_t res = 0; + if (pass_size > 0) { + ssize_t off = 0; + ssize_t ret = 0; do { - res = gpgme_io_write(fd, &passphrase[off], passphrase_size - off); - if (res > 0) off += res; - } while (res > 0 && off != passphrase_size); + ret = gpgme_io_write(fd, &p_pass_bytes[off], pass_size - off); + if (ret > 0) off += ret; + } while (ret > 0 && off != pass_size); + res = off; } res += gpgme_io_write(fd, "\n", 1); - - qCDebug(core, "custom passphrase cd is about to return, res: %ld", res); - return res == passphrase_size + 1 - ? 0 - : gpgme_error_from_errno(GPG_ERR_CANCELED); + return res == pass_size + 1 ? 0 : gpgme_error_from_errno(GPG_ERR_CANCELED); } static auto TestStatusCb(void *hook, const char *keyword, -- cgit v1.2.3