File indexing completed on 2024-06-16 04:56:07

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     selftest/gpgagentcheck.cpp
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2009 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include <config-kleopatra.h>
0011 
0012 #include "gpgagentcheck.h"
0013 
0014 #include "implementation_p.h"
0015 
0016 #include <Libkleo/Formatting>
0017 
0018 #include <gpgme++/context.h>
0019 
0020 #include <QTextDocument> // for Qt::escape
0021 
0022 #include <KLocalizedString>
0023 
0024 using namespace Kleo;
0025 using namespace Kleo::_detail;
0026 using namespace GpgME;
0027 
0028 namespace
0029 {
0030 
0031 class GpgAgentCheck : public SelfTestImplementation
0032 {
0033 public:
0034     explicit GpgAgentCheck()
0035         : SelfTestImplementation(i18nc("@title", "Gpg-Agent Connectivity"))
0036     {
0037         runTest();
0038     }
0039 
0040     void runTest()
0041     {
0042         m_skipped = true;
0043 
0044         if (!hasFeature(AssuanEngineFeature, 0)) {
0045             m_error = i18n("GpgME library too old");
0046             m_explanation = i18nc("@info",
0047                                   "Either the GpgME library itself is too old, "
0048                                   "or the GpgME++ library was compiled against "
0049                                   "an older GpgME that did not support connecting to gpg-agent.");
0050             m_proposedFix = xi18nc("@info",
0051                                    "Upgrade to <application>gpgme</application> 1.2.0 or higher, "
0052                                    "and ensure that gpgme++ was compiled against it.");
0053         } else if (ensureEngineVersion(GpgME::GpgConfEngine, 2, 1, 0)) {
0054             // 2.1 starts the agent on demand and requires it. So for 2.1.0 we can assume
0055             // autostart works and we don't need to care about the agent.
0056             m_skipped = false;
0057             m_passed = true;
0058             return;
0059         } else {
0060             Error error;
0061             const std::unique_ptr<Context> ctx = Context::createForEngine(AssuanEngine, &error);
0062             if (!ctx.get()) {
0063                 m_error = i18n("GpgME does not support gpg-agent");
0064                 m_explanation = xi18nc("@info",
0065                                        "<para>The <application>GpgME</application> library is new "
0066                                        "enough to support <application>gpg-agent</application>, "
0067                                        "but does not seem to do so in this installation.</para>"
0068                                        "<para>The error returned was: <message>%1</message>.</para>",
0069                                        Formatting::errorAsString(error).toHtmlEscaped());
0070                 // PENDING(marc) proposed fix?
0071             } else {
0072                 m_skipped = false;
0073 
0074                 const Error error = ctx->assuanTransact("GETINFO version");
0075                 if (error) {
0076                     m_passed = false;
0077                     m_error = i18n("unexpected error");
0078                     m_explanation = xi18nc("@info",
0079                                            "<para>Unexpected error while asking <application>gpg-agent</application> "
0080                                            "for its version.</para>"
0081                                            "<para>The error returned was: <message>%1</message>.</para>",
0082                                            Formatting::errorAsString(error).toHtmlEscaped());
0083                     // PENDING(marc) proposed fix?
0084                 } else {
0085                     m_passed = true;
0086                 }
0087             }
0088         }
0089     }
0090 };
0091 }
0092 
0093 std::shared_ptr<SelfTest> Kleo::makeGpgAgentConnectivitySelfTest()
0094 {
0095     return std::shared_ptr<SelfTest>(new GpgAgentCheck);
0096 }