File indexing completed on 2024-06-23 05:14:10

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     selftest/gpgconfcheck.cpp
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include <config-kleopatra.h>
0011 
0012 #include "gpgconfcheck.h"
0013 
0014 #include "implementation_p.h"
0015 
0016 #include <Libkleo/GnuPG>
0017 #include <Libkleo/Hex>
0018 
0019 #include "kleopatra_debug.h"
0020 #include <KLocalizedString>
0021 
0022 #include <QGpgME/CryptoConfig>
0023 #include <QGpgME/Protocol>
0024 
0025 using namespace Kleo;
0026 using namespace Kleo::_detail;
0027 
0028 namespace
0029 {
0030 
0031 class GpgConfCheck : public SelfTestImplementation
0032 {
0033     QString m_component;
0034 
0035 public:
0036     explicit GpgConfCheck(const char *component)
0037         : SelfTestImplementation(i18nc("@title", "%1 Configuration Check", component && *component ? QLatin1StringView(component) : QLatin1String("gpgconf")))
0038         , m_component(QLatin1StringView(component))
0039     {
0040         runTest();
0041     }
0042 
0043     void runTest()
0044     {
0045         const auto conf = QGpgME::cryptoConfig();
0046         QString message;
0047         m_passed = true;
0048 
0049         if (!conf) {
0050             message = QStringLiteral("Could not be started.");
0051             m_passed = false;
0052         } else if (m_component.isEmpty() && conf->componentList().empty()) {
0053             message = QStringLiteral("Could not list components.");
0054             m_passed = false;
0055         } else if (!m_component.isEmpty()) {
0056             const auto comp = conf->component(m_component);
0057             if (!comp) {
0058                 message = QStringLiteral("Binary could not be found.");
0059                 m_passed = false;
0060             } else if (comp->groupList().empty()) {
0061                 // If we don't have any group it means that list-options
0062                 // for this component failed.
0063                 message = QStringLiteral("The configuration file is invalid.");
0064                 m_passed = false;
0065             }
0066         }
0067 
0068         if (!m_passed) {
0069             m_error = i18nc("self-test did not pass", "Failed");
0070             m_explanation = i18n(
0071                 "There was an error executing the GnuPG configuration self-check for %2:\n"
0072                 "  %1\n"
0073                 "You might want to execute \"gpgconf %3\" on the command line.\n",
0074                 message,
0075                 m_component.isEmpty() ? QStringLiteral("GnuPG") : m_component,
0076                 QStringLiteral("--check-options ") + (m_component.isEmpty() ? QString() : m_component));
0077 
0078             // To avoid modifying the l10n
0079             m_explanation.replace(QLatin1Char('\n'), QStringLiteral("<br/>"));
0080         }
0081     }
0082 };
0083 }
0084 
0085 std::shared_ptr<SelfTest> Kleo::makeGpgConfCheckConfigurationSelfTest(const char *component)
0086 {
0087     return std::shared_ptr<SelfTest>(new GpgConfCheck(component));
0088 }