File indexing completed on 2024-05-05 05:53:24

0001 /*
0002     SPDX-FileCopyrightText: 2010-2022 Rolf Eike Beer <kde@opensource.sf-tec.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "kgpggeneraterevoke.h"
0007 #include "kgpg_general_debug.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 
0012 #include <QFile>
0013 
0014 KGpgGenerateRevoke::KGpgGenerateRevoke(QObject *parent, const QString &keyID, const QUrl &revokeUrl, const int reason, const QString &description)
0015     : KGpgTransaction(parent),
0016     m_keyid(keyID),
0017     m_revUrl(revokeUrl),
0018     m_reason(reason),
0019     m_description(description)
0020 {
0021     QStringList args = { QLatin1String("--status-fd=1"),
0022             QLatin1String("--command-fd=0"),
0023             QLatin1String("--no-verbose")
0024             };
0025 
0026     if (!revokeUrl.isEmpty())
0027         args << QLatin1String("-o") << revokeUrl.toLocalFile();
0028 
0029     args << QLatin1String("--gen-revoke") << keyID;
0030     addArguments(args);
0031 }
0032 
0033 bool
0034 KGpgGenerateRevoke::preStart()
0035 {
0036     setSuccess(TS_OK);
0037 
0038     setDescription(i18n("Generating Revocation Certificate for key %1", m_keyid));
0039 
0040     return true;
0041 }
0042 
0043 bool
0044 KGpgGenerateRevoke::nextLine(const QString &line)
0045 {
0046     if (!line.startsWith(QLatin1String("[GNUPG:] "))) {
0047         m_output.append(line + QLatin1Char( '\n' ));
0048         return false;
0049     }
0050 
0051     if (line.contains(QLatin1String( "NEED_PASSPHRASE" ))) {
0052         setSuccess(TS_USER_ABORTED);
0053     } else if (line.contains(QLatin1String( "ask_revocation_reason.code" ))) {
0054         write(QByteArray::number(m_reason));
0055     } else if (line.contains(QLatin1String( "ask_revocation_reason.text" ))) {
0056         write(m_description.toUtf8());
0057         // GnuPG stops asking if we pass an empty line
0058         m_description.clear();
0059     } else if (line.contains(QLatin1String( "GET_" ))) {
0060         setSuccess(TS_MSG_SEQUENCE);
0061         qCDebug(KGPG_LOG_GENERAL) << line;
0062         return true;
0063     }
0064 
0065     return false;
0066 }
0067 
0068 KGpgTransaction::ts_boolanswer
0069 KGpgGenerateRevoke::boolQuestion(const QString& line)
0070 {
0071     if (line == QLatin1String("gen_revoke.okay")) {
0072         return BA_YES;
0073     } else if (line == QLatin1String("ask_revocation_reason.okay")) {
0074         return BA_YES;
0075     } else {
0076         return KGpgTransaction::boolQuestion(line);
0077     }
0078 }
0079 
0080 void
0081 KGpgGenerateRevoke::finish()
0082 {
0083     if (getSuccess() == TS_OK) {
0084         if (!m_revUrl.isEmpty()) {
0085             QFile of(m_revUrl.toLocalFile());
0086             if (of.open(QIODevice::ReadOnly)) {
0087                 m_output = QLatin1String( of.readAll() );
0088                 of.close();
0089             }
0090         }
0091         Q_EMIT revokeCertificate(m_output);
0092     }
0093 }
0094 
0095 bool
0096 KGpgGenerateRevoke::passphraseReceived()
0097 {
0098     return false;
0099 }
0100 
0101 KGpgTransaction::ts_boolanswer
0102 KGpgGenerateRevoke::confirmOverwrite(QUrl &currentFile)
0103 {
0104     currentFile = m_revUrl;
0105     return BA_UNKNOWN;
0106 }
0107 
0108 const QString &
0109 KGpgGenerateRevoke::getOutput() const
0110 {
0111     return m_output;
0112 }