sailfish-safe

Sailfish frontend for safe(1)
git clone git://git.z3bra.org/sailfish-safe.git
Log | Files | Refs | README | LICENSE

commit c02f4df7ac7118468fe4bfddfe4001515e39efa1
parent 1e7504c400a7984017f8f4d31ecaae4d0ea03795
Author: Daniel Vrátil <dvratil@kde.org>
Date:   Sat, 14 Dec 2019 01:16:11 +0100

Fix support for gpg 2.0

Diffstat:
Msrc/passwordprovider.cpp | 33+++++++++++++++++++++++----------
Msrc/passwordprovider.h | 7++++---
2 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/src/passwordprovider.cpp b/src/passwordprovider.cpp @@ -24,6 +24,7 @@ #include <QStandardPaths> #include <QClipboard> #include <QGuiApplication> +#include <QRegularExpression> namespace { @@ -90,9 +91,16 @@ PasswordProvider::GpgExecutable PasswordProvider::findGpgExecutable() auto gpgExe = QStandardPaths::findExecutable(QStringLiteral("gpg2")); if (gpgExe.isEmpty()) { gpgExe = QStandardPaths::findExecutable(QStringLiteral("gpg")); - return {gpgExe, false}; } - return {gpgExe, true}; + + QProcess process; + process.start(gpgExe, {QStringLiteral("--version")}, QIODevice::ReadOnly); + process.waitForFinished(); + const auto line = process.readLine(); + static const QRegularExpression rex(QStringLiteral("([0-9]+).([0-9]+).([0-9]+)")); + const auto match = rex.match(QString::fromUtf8(line)); + + return {gpgExe, match.captured(1).toInt(), match.captured(2).toInt()}; } void PasswordProvider::requestPassword() @@ -120,20 +128,25 @@ void PasswordProvider::requestPassword() return; } - QStringList args = { QStringLiteral("-d"), + qDebug("Detected gpg version: %d.%d", gpgExe.major_version, gpgExe.minor_version); + + QStringList args = { QStringLiteral("--decrypt"), QStringLiteral("--quiet"), QStringLiteral("--yes"), QStringLiteral("--compress-algo=none"), QStringLiteral("--no-encrypt-to"), - QStringLiteral("--passphrase-fd=0"), - mPath }; - if (gpgExe.isGpg2) { - args = QStringList{ QStringLiteral("--pinentry-mode=loopback"), - QStringLiteral("--batch"), - QStringLiteral("--use-agent") } - + args; + QStringLiteral("--passphrase-fd=0") }; + if (gpgExe.major_version >= 2) { + args += QStringList{ QStringLiteral("--batch"), + QStringLiteral("--no-use-agent") }; + + if (gpgExe.minor_version >= 1) { + args.push_back(QStringLiteral("--pinentry-mode=loopback")); + } } + args.push_back(mPath); + mGpg = new QProcess; connect(mGpg, &QProcess::errorOccurred, this, [this, gpgExe](QProcess::ProcessError state) { diff --git a/src/passwordprovider.h b/src/passwordprovider.h @@ -48,11 +48,12 @@ public: QString error() const; struct GpgExecutable { - GpgExecutable(const QString &path, bool isGpg2) - : path(path), isGpg2(isGpg2) + GpgExecutable(const QString &path, int major, int minor) + : path(path), major_version(major), minor_version(minor) {} QString path = {}; - bool isGpg2 = false; + int major_version = 0; + int minor_version = 0; }; static GpgExecutable findGpgExecutable();