File indexing completed on 2024-04-28 09:46:03

0001 /*
0002     SPDX-FileCopyrightText: 2008-2022 Rolf Eike Beer <kde@opensource.sf-tec.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "kgpgchangepass.h"
0007 
0008 #include <KLocalizedString>
0009 
0010 KGpgChangePass::KGpgChangePass(QObject *parent, const QString &keyid)
0011     : KGpgTransaction(parent),
0012     m_seenold(false)
0013 {
0014     addArguments( { QLatin1String("--status-fd=1"),
0015             QLatin1String("--command-fd=0"),
0016             QLatin1String("--edit-key"),
0017             keyid,
0018             QLatin1String("passwd")
0019             } );
0020 }
0021 
0022 bool
0023 KGpgChangePass::preStart()
0024 {
0025     setSuccess(TS_MSG_SEQUENCE);
0026 
0027     return true;
0028 }
0029 
0030 bool
0031 KGpgChangePass::nextLine(const QString &line)
0032 {
0033     if (!line.startsWith(QLatin1String("[GNUPG:] ")))
0034         return false;
0035 
0036     if (line.contains(QLatin1String( "keyedit.prompt" ))) {
0037         if (m_seenold && (getSuccess() != TS_USER_ABORTED))
0038             setSuccess(TS_OK);
0039         // no need to save, change is automatically saved by GnuPG
0040         return true;
0041     } else if (line.contains(QLatin1String( "GET_" ))) {
0042         setSuccess(TS_MSG_SEQUENCE);
0043         return true;
0044     }
0045 
0046     return false;
0047 }
0048 
0049 bool
0050 KGpgChangePass::passphraseRequested()
0051 {
0052     const QString userIDs = getIdHints();
0053 
0054     if (!m_seenold) {
0055         return askPassphrase(i18n("Enter old passphrase for <b>%1</b>", userIDs));
0056     } else {
0057         askNewPassphrase(i18n("<qt>Enter new passphrase for <b>%1</b><br />If you forget this passphrase all your encrypted files and messages will be inaccessible.</qt>", userIDs));
0058     }
0059 
0060     return true;
0061 }
0062 
0063 bool
0064 KGpgChangePass::passphraseReceived()
0065 {
0066     m_seenold = true;
0067     setSuccess(TS_MSG_SEQUENCE);
0068     return false;
0069 }
0070 
0071 bool
0072 KGpgChangePass::hintLine(const KGpgTransaction::ts_hintType hint, const QString &args)
0073 {
0074     switch (hint) {
0075     case HT_PINENTRY_LAUNCHED:
0076         // If pinentry message appear 2 should be seen: the one asking for the old password,
0077         // and one asking for the new. So the old password has successfully been given if the
0078         // second one appears. BUT: if the user cancels one of these boxes they will reappear
0079         // up to 3 times, so even the third one could be the one asking for the old password.
0080         // Simply assume that everything is fine when pinentry is used, which will make the
0081         // result being set to TS_OK once keyedit.prompt is received.
0082         m_seenold = true;
0083         return true;
0084     default:
0085         return KGpgTransaction::hintLine(hint, args);
0086     }
0087 }