diff --git a/lang/qt/src/qgpgmewkdlookupjob.cpp b/lang/qt/src/qgpgmewkdlookupjob.cpp index fcb757e1..b8622167 100644 --- a/lang/qt/src/qgpgmewkdlookupjob.cpp +++ b/lang/qt/src/qgpgmewkdlookupjob.cpp @@ -1,182 +1,183 @@ /* qgpgmewkdlookupjob.cpp This file is part of qgpgme, the Qt API binding for gpgme Copyright (c) 2021 g10 Code GmbH Software engineering by Ingo Klöcker QGpgME is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. QGpgME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "qgpgmewkdlookupjob.h" #include "qgpgme_debug.h" #include #include #include #include using namespace QGpgME; using namespace GpgME; QGpgMEWKDLookupJob::QGpgMEWKDLookupJob(Context *context) : mixin_type{context} { lateInitialization(); } QGpgMEWKDLookupJob::~QGpgMEWKDLookupJob() = default; static GpgME::Error startDirmngr(Context *assuanCtx) { Error err; auto spawnCtx = std::unique_ptr{Context::createForEngine(SpawnEngine, &err)}; if (err) { qCDebug(QGPGME_LOG) << "Error: Failed to get context for spawn engine (" << err.asString() << ")"; } const auto dirmngrProgram = GpgME::dirInfo("dirmngr-name"); const auto homedir = GpgME::dirInfo("homedir"); const char *argv[] = { dirmngrProgram, "--homedir", homedir, "--daemon", NULL }; auto ignoreIO = Data{Data::null}; if (!err) { qCDebug(QGPGME_LOG) << "Starting dirmngr ..."; err = spawnCtx->spawnAsync(dirmngrProgram, argv, ignoreIO, ignoreIO, ignoreIO, Context::SpawnDetached); } if (!err) { // wait for socket to become available int cnt = 0; do { ++cnt; qCDebug(QGPGME_LOG) << "Waiting for dirmngr to start ..."; QThread::msleep(250 * cnt); err = assuanCtx->assuanTransact("GETINFO version"); } while (err.code() == GPG_ERR_ASS_CONNECT_FAILED && cnt < 5); } return err; } static GpgME::Error setUpDirmngrAssuanConnection(Context *ctx) { Error err; const std::string dirmngrSocket = GpgME::dirInfo("dirmngr-socket"); err = ctx->setEngineFileName(dirmngrSocket.c_str()); if (!err) { err = ctx->setEngineHomeDirectory(""); } if (!err) { // try do connect to dirmngr err = ctx->assuanTransact("GETINFO version"); if (err.code() == GPG_ERR_ASS_CONNECT_FAILED) { err = startDirmngr(ctx); } } return err; } -static GpgME::Error run_wkd_get(Context *ctx, const QString &email) +static GpgME::Error run_wkd_get(Context *ctx, const std::string &email) { Error err; - const auto cmd = std::string{"WKD_GET "} + email.toUtf8().toStdString(); + const auto cmd = std::string{"WKD_GET "} + email; err = ctx->assuanTransact(cmd.c_str()); if (err.code() == GPG_ERR_NO_NAME || err.code() == GPG_ERR_NO_DATA) { // ignore those benign errors; GPG_ERR_NO_NAME indicates that the domain // doesn't exist (on first request); GPG_ERR_NO_DATA indicates that // no key for email is available via WKD or that the domain doesn't // support WKD or that the domain doesn't exist (on subsequent requests // using dirmngr's internal cache) qCDebug(QGPGME_LOG) << "WKD_GET returned" << err.asString() << "; ignoring..."; err = {}; } if (err) { qCDebug(QGPGME_LOG) << "WKD_GET failed with" << err.asString(); } return err; } static QGpgMEWKDLookupJob::result_type lookup_keys(Context *ctx, const QString &email) { WKDLookupResult result; Error err = setUpDirmngrAssuanConnection(ctx); + const auto pattern = email.toUtf8().toStdString(); if (!err) { - err = run_wkd_get(ctx, email); + err = run_wkd_get(ctx, pattern); } if (!err) { const auto transaction = std::unique_ptr(dynamic_cast(ctx->takeLastAssuanTransaction().release())); const auto source = transaction->firstStatusLine("SOURCE"); const auto rawData = transaction->data(); if (rawData.size() == 0) { qCDebug(QGPGME_LOG) << "No key found for" << email; - result = WKDLookupResult{GpgME::Data::null, {}, {}}; + result = WKDLookupResult{pattern, GpgME::Data::null, {}, {}}; } else { qCDebug(QGPGME_LOG) << "Found key for" << email << "at" << source.c_str(); - result = WKDLookupResult{GpgME::Data{rawData.c_str(), rawData.size()}, source, {}}; + result = WKDLookupResult{pattern, GpgME::Data{rawData.c_str(), rawData.size()}, source, {}}; } } - return std::make_tuple(err ? WKDLookupResult{err} : result, QString{}, Error{}); + return std::make_tuple(err ? WKDLookupResult{pattern, err} : result, QString{}, Error{}); } Error QGpgMEWKDLookupJob::start(const QString &email) { run(std::bind(&lookup_keys, std::placeholders::_1, email)); return Error(); } WKDLookupResult QGpgMEWKDLookupJob::exec(const QString &email) { const result_type r = lookup_keys(context(), email); resultHook(r); return std::get<0>(r); } #include "qgpgmewkdlookupjob.moc" diff --git a/lang/qt/src/wkdlookupresult.cpp b/lang/qt/src/wkdlookupresult.cpp index 71aa75cf..ac1a89e9 100644 --- a/lang/qt/src/wkdlookupresult.cpp +++ b/lang/qt/src/wkdlookupresult.cpp @@ -1,111 +1,117 @@ /* wkdlookupresult.cpp - wraps the result of a WKDLookupJob This file is part of qgpgme, the Qt API binding for gpgme Copyright (c) 2021 g10 Code GmbH Software engineering by Ingo Klöcker QGpgME is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. QGpgME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "wkdlookupresult.h" #include using namespace QGpgME; using namespace GpgME; class WKDLookupResult::Private { public: + std::string pattern; GpgME::Data keyData; std::string source; }; WKDLookupResult::WKDLookupResult() = default; WKDLookupResult::~WKDLookupResult() = default; -WKDLookupResult::WKDLookupResult(const Error &error) +WKDLookupResult::WKDLookupResult(const std::string &pattern, const Error &error) : Result{error} - , d{} + , d{new Private{pattern, {}, {}}} { } -WKDLookupResult::WKDLookupResult(const Data &keyData, const std::string &source, const Error &error) +WKDLookupResult::WKDLookupResult(const std::string &pattern, const Data &keyData, const std::string &source, const Error &error) : Result{error} - , d{new Private{keyData, source}} + , d{new Private{pattern, keyData, source}} { } WKDLookupResult::WKDLookupResult(const WKDLookupResult &other) : Result{other} { if (other.d) { d.reset(new Private{*other.d}); } } WKDLookupResult &WKDLookupResult::operator=(const WKDLookupResult &other) { auto tmp = other; swap(tmp); return *this; } WKDLookupResult::WKDLookupResult(WKDLookupResult &&other) = default; WKDLookupResult &WKDLookupResult::operator=(WKDLookupResult &&other) = default; void WKDLookupResult::swap(WKDLookupResult &other) noexcept { Result::swap(other); std::swap(this->d, other.d); } bool WKDLookupResult::isNull() const { return !d && !bool(error()); } +std::string WKDLookupResult::pattern() const +{ + return d ? d->pattern : std::string{}; +} + Data WKDLookupResult::keyData() const { return d ? d->keyData : Data{}; } std::string WKDLookupResult::source() const { return d ? d->source : std::string{}; } void QGpgME::swap(WKDLookupResult &a, WKDLookupResult &b) { a.swap(b); } diff --git a/lang/qt/src/wkdlookupresult.h b/lang/qt/src/wkdlookupresult.h index c40220a6..5696fb6d 100644 --- a/lang/qt/src/wkdlookupresult.h +++ b/lang/qt/src/wkdlookupresult.h @@ -1,83 +1,84 @@ /* wkdlookupresult.h - wraps the result of a WKDLookupJob This file is part of qgpgme, the Qt API binding for gpgme Copyright (c) 2021 g10 Code GmbH Software engineering by Ingo Klöcker QGpgME is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. QGpgME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #ifndef __QGPGME_WKDLOOKUPRESULT_H__ #define __QGPGME_WKDLOOKUPRESULT_H__ #include "qgpgme_export.h" #include #include namespace GpgME { class Data; class Error; } namespace QGpgME { class QGPGME_EXPORT WKDLookupResult : public GpgME::Result { public: WKDLookupResult(); ~WKDLookupResult(); - explicit WKDLookupResult(const GpgME::Error &err); - explicit WKDLookupResult(const GpgME::Data &keyData, const std::string &source, const GpgME::Error &err); + explicit WKDLookupResult(const std::string &pattern, const GpgME::Error &err); + explicit WKDLookupResult(const std::string &pattern, const GpgME::Data &keyData, const std::string &source, const GpgME::Error &err); WKDLookupResult(const WKDLookupResult &other); WKDLookupResult &operator=(const WKDLookupResult &other); WKDLookupResult(WKDLookupResult &&other); WKDLookupResult &operator=(WKDLookupResult &&other); void swap(WKDLookupResult &other) noexcept; bool isNull() const; + std::string pattern() const; GpgME::Data keyData() const; std::string source() const; private: class Private; std::unique_ptr d; }; QGPGME_EXPORT void swap(WKDLookupResult &a, WKDLookupResult &b); } #endif // __QGPGME_WKDLOOKUPRESULT_H__ diff --git a/lang/qt/tests/t-wkdlookup.cpp b/lang/qt/tests/t-wkdlookup.cpp index 0fbe67a2..a09591cb 100644 --- a/lang/qt/tests/t-wkdlookup.cpp +++ b/lang/qt/tests/t-wkdlookup.cpp @@ -1,155 +1,158 @@ /* t-wkdlookup.cpp This file is part of qgpgme, the Qt API binding for gpgme Copyright (c) 2021 g10 Code GmbH Software engineering by Ingo Klöcker QGpgME is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. QGpgME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "t-support.h" #include "data.h" #include "engineinfo.h" #include "protocol.h" #include "wkdlookupjob.h" #include "wkdlookupresult.h" #include #include #include #include using namespace QGpgME; using namespace GpgME; static const char *requiredVersion = "2.1.12"; namespace { bool keyHasUserIDWithMatchingEmailAddress(const Key &key, const QString &expectedEmailAddress) { const auto email = expectedEmailAddress.toLower(); const auto userIds = key.userIDs(); return std::any_of( std::begin(userIds), std::end(userIds), [email](const UserID &uid) { return email == QString::fromUtf8(uid.email()).toLower(); }); } } class WKDLookupTest : public QGpgMETest { Q_OBJECT Q_SIGNALS: void asyncDone(); private Q_SLOTS: void testWKDLookupAsync() { if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < requiredVersion) { QSKIP("dirmngr does not yet support WKD lookup"); } if (!doOnlineTests()) { QSKIP("Set DO_ONLINE_TESTS environment variable to run this test."); } const QString email = QLatin1String{"wk@gnupg.org"}; WKDLookupResult result; auto *job = openpgp()->wkdLookupJob(); connect(job, &WKDLookupJob::result, job, [this, &result](const WKDLookupResult &result_, const QString &, const Error &) { result = result_; Q_EMIT asyncDone(); }); job->start(email); QSignalSpy spy (this, SIGNAL(asyncDone())); QVERIFY(spy.wait(QSIGNALSPY_TIMEOUT)); QVERIFY(result.error().code() == GPG_ERR_NO_ERROR); + QCOMPARE(result.pattern(), "wk@gnupg.org"); QCOMPARE(result.source(), "https://openpgpkey.gnupg.org"); const auto keys = result.keyData().toKeys(GpgME::OpenPGP); QVERIFY(keys.size() == 1); QVERIFY(keyHasUserIDWithMatchingEmailAddress(keys.front(), email)); } void testWKDLookupSync() { if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < requiredVersion) { QSKIP("dirmngr does not yet support WKD lookup"); } if (!doOnlineTests()) { QSKIP("Set DO_ONLINE_TESTS environment variable to run this test."); } const QString email = QLatin1String{"wk@gnupg.org"}; auto *job = openpgp()->wkdLookupJob(); const auto result = job->exec(email); QVERIFY(result.error().code() == GPG_ERR_NO_ERROR); + QCOMPARE(result.pattern(), "wk@gnupg.org"); QCOMPARE(result.source(), "https://openpgpkey.gnupg.org"); const auto keys = result.keyData().toKeys(GpgME::OpenPGP); QVERIFY(keys.size() == 1); QVERIFY(keyHasUserIDWithMatchingEmailAddress(keys.front(), email)); } void testLookupWithNoResultAsync() { if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < requiredVersion) { QSKIP("dirmngr does not yet support WKD lookup"); } if (!doOnlineTests()) { QSKIP("Set DO_ONLINE_TESTS environment variable to run this test."); } const QString email = QLatin1String{"alfa@example.net"}; WKDLookupResult result; auto *job = openpgp()->wkdLookupJob(); connect(job, &WKDLookupJob::result, job, [this, &result](const WKDLookupResult &result_, const QString &, const Error &) { result = result_; Q_EMIT asyncDone(); }); job->start(email); QSignalSpy spy (this, SIGNAL(asyncDone())); QVERIFY(spy.wait(QSIGNALSPY_TIMEOUT)); QVERIFY(result.error().code() == GPG_ERR_NO_ERROR); + QCOMPARE(result.pattern(), "alfa@example.net"); QCOMPARE(result.source(), ""); QVERIFY(result.keyData().isNull()); } }; QTEST_MAIN(WKDLookupTest) #include "t-wkdlookup.moc"