File indexing completed on 2024-06-23 05:13:40

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     commands/exportopenpgpcertstoservercommand.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 "exportopenpgpcertstoservercommand.h"
0013 
0014 #include "command_p.h"
0015 
0016 #include <Libkleo/Algorithm>
0017 #include <Libkleo/Formatting>
0018 #include <Libkleo/GnuPG>
0019 #include <Libkleo/KeyHelpers>
0020 
0021 #include <gpgme++/key.h>
0022 
0023 #include <KLocalizedString>
0024 #include <KMessageBox>
0025 
0026 using namespace Kleo;
0027 using namespace Kleo::Commands;
0028 using namespace GpgME;
0029 
0030 ExportOpenPGPCertsToServerCommand::ExportOpenPGPCertsToServerCommand(KeyListController *c)
0031     : GnuPGProcessCommand(c)
0032 {
0033 }
0034 
0035 ExportOpenPGPCertsToServerCommand::ExportOpenPGPCertsToServerCommand(QAbstractItemView *v, KeyListController *c)
0036     : GnuPGProcessCommand(v, c)
0037 {
0038 }
0039 
0040 ExportOpenPGPCertsToServerCommand::ExportOpenPGPCertsToServerCommand(const Key &key)
0041     : GnuPGProcessCommand(key)
0042 {
0043 }
0044 
0045 ExportOpenPGPCertsToServerCommand::ExportOpenPGPCertsToServerCommand(const std::vector<Key> &keys)
0046     : GnuPGProcessCommand(keys)
0047 {
0048 }
0049 
0050 ExportOpenPGPCertsToServerCommand::~ExportOpenPGPCertsToServerCommand() = default;
0051 
0052 static bool confirmExport(const std::vector<Key> &pgpKeys, QWidget *parentWidget)
0053 {
0054     auto notCertifiedKeys = std::accumulate(pgpKeys.cbegin(), pgpKeys.cend(), QStringList{}, [](auto keyNames, const auto &key) {
0055         const bool allValidUserIDsAreCertifiedByUser = Kleo::all_of(key.userIDs(), [](const UserID &userId) {
0056             return userId.isBad() || Kleo::userIDIsCertifiedByUser(userId);
0057         });
0058         if (!allValidUserIDsAreCertifiedByUser) {
0059             keyNames.push_back(Formatting::formatForComboBox(key));
0060         }
0061         return keyNames;
0062     });
0063     if (!notCertifiedKeys.empty()) {
0064         if (pgpKeys.size() == 1) {
0065             const auto answer = KMessageBox::warningContinueCancel( //
0066                 parentWidget,
0067                 xi18nc("@info",
0068                        "<para>You haven't certified all valid user IDs of this certificate "
0069                        "with an exportable certification. People relying on your certifications "
0070                        "may not be able to verify the certificate.</para>"
0071                        "<para>Do you want to continue the export?</para>"),
0072                 i18nc("@title:window", "Confirm Certificate Export"),
0073                 KGuiItem{i18ncp("@action:button", "Export Certificate", "Export Certificates", 1)},
0074                 KStandardGuiItem::cancel(),
0075                 QStringLiteral("confirm-upload-of-uncertified-keys"));
0076             return answer == KMessageBox::Continue;
0077         } else {
0078             std::sort(notCertifiedKeys.begin(), notCertifiedKeys.end());
0079             const auto answer = KMessageBox::warningContinueCancelList( //
0080                 parentWidget,
0081                 xi18nc("@info",
0082                        "<para>You haven't certified all valid user IDs of the certificates listed below "
0083                        "with exportable certifications. People relying on your certifications "
0084                        "may not be able to verify the certificates.</para>"
0085                        "<para>Do you want to continue the export?</para>"),
0086                 notCertifiedKeys,
0087                 i18nc("@title:window", "Confirm Certificate Export"),
0088                 KGuiItem{i18ncp("@action:button", "Export Certificate", "Export Certificates", pgpKeys.size())},
0089                 KStandardGuiItem::cancel(),
0090                 QStringLiteral("confirm-upload-of-uncertified-keys"));
0091             return answer == KMessageBox::Continue;
0092         }
0093     }
0094 
0095     return true;
0096 }
0097 
0098 bool ExportOpenPGPCertsToServerCommand::preStartHook(QWidget *parent) const
0099 {
0100     if (!haveKeyserverConfigured()) {
0101         d->error(i18ncp("@info",
0102                         "Exporting the certificate to a key server is not possible "
0103                         "because the usage of key servers has been disabled explicitly.",
0104                         "Exporting the certificates to a key server is not possible "
0105                         "because the usage of key servers has been disabled explicitly.",
0106                         d->keys().size()));
0107         return false;
0108     }
0109     if (!confirmExport(d->keys(), parent)) {
0110         return false;
0111     }
0112     return KMessageBox::warningContinueCancel(parent,
0113                                               xi18nc("@info",
0114                                                      "<para>When OpenPGP certificates have been exported to a public directory server, "
0115                                                      "it is nearly impossible to remove them again.</para>"
0116                                                      "<para>Before exporting your certificate to a public directory server, make sure that you "
0117                                                      "have created a revocation certificate so you can revoke the certificate if needed later.</para>"
0118                                                      "<para>Are you sure you want to continue?</para>"),
0119                                               i18nc("@title:window", "OpenPGP Certificate Export"),
0120                                               KGuiItem{i18ncp("@action:button", "Export Certificate", "Export Certificates", d->keys().size())},
0121                                               KStandardGuiItem::cancel(),
0122                                               QStringLiteral("warn-export-openpgp-nonrevocable"))
0123         == KMessageBox::Continue;
0124 }
0125 
0126 QStringList ExportOpenPGPCertsToServerCommand::arguments() const
0127 {
0128     QStringList result;
0129     result << gpgPath();
0130     result << QStringLiteral("--send-keys");
0131     const auto keys = d->keys();
0132     for (const Key &key : keys) {
0133         result << QLatin1StringView(key.primaryFingerprint());
0134     }
0135     return result;
0136 }
0137 
0138 QString ExportOpenPGPCertsToServerCommand::errorCaption() const
0139 {
0140     return i18nc("@title:window", "OpenPGP Certificate Export Error");
0141 }
0142 
0143 QString ExportOpenPGPCertsToServerCommand::successCaption() const
0144 {
0145     return i18nc("@title:window", "OpenPGP Certificate Export Finished");
0146 }
0147 
0148 QString ExportOpenPGPCertsToServerCommand::crashExitMessage(const QStringList &args) const
0149 {
0150     return xi18nc("@info",
0151                   "<para>The GPG process that tried to export OpenPGP certificates "
0152                   "ended prematurely because of an unexpected error.</para>"
0153                   "<para>Please check the output of <icode>%1</icode> for details.</para>",
0154                   args.join(QLatin1Char(' ')));
0155 }
0156 
0157 QString ExportOpenPGPCertsToServerCommand::errorExitMessage(const QStringList &args) const
0158 {
0159     // ki18n(" ") as initializer because initializing with empty string leads to
0160     // (I18N_EMPTY_MESSAGE)
0161     const auto errorLines = errorString().split(QLatin1Char{'\n'});
0162     const auto errorText = std::accumulate(errorLines.begin(), errorLines.end(), KLocalizedString{ki18n(" ")}, [](KLocalizedString temp, const auto &line) {
0163         return kxi18nc("@info used for concatenating multiple lines of text with line breaks; most likely this shouldn't be translated", "%1<nl />%2")
0164             .subs(temp)
0165             .subs(line);
0166     });
0167     return xi18nc("@info",
0168                   "<para>An error occurred while trying to export OpenPGP certificates.</para> "
0169                   "<para>The output of <command>%1</command> was:<nl /><message>%2</message></para>",
0170                   args[0],
0171                   errorText);
0172 }
0173 
0174 QString ExportOpenPGPCertsToServerCommand::successMessage(const QStringList &) const
0175 {
0176     return i18nc("@info", "OpenPGP certificates exported successfully.");
0177 }
0178 
0179 #include "moc_exportopenpgpcertstoservercommand.cpp"