1 /*
2     SPDX-FileCopyrightText: 2008, 2009, 2010, 2011, 2012, 2013, 2017, 2018 Rolf Eike Beer <kde@opensource.sf-tec.de>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "kgpgchangepass.h"
7 
8 #include <KLocalizedString>
9 
KGpgChangePass(QObject * parent,const QString & keyid)10 KGpgChangePass::KGpgChangePass(QObject *parent, const QString &keyid)
11 	: KGpgTransaction(parent),
12 	m_seenold(false)
13 {
14 	addArguments( { QLatin1String("--status-fd=1"),
15 			QLatin1String("--command-fd=0"),
16 			QLatin1String("--edit-key"),
17 			keyid,
18 			QLatin1String("passwd")
19 			} );
20 }
21 
~KGpgChangePass()22 KGpgChangePass::~KGpgChangePass()
23 {
24 }
25 
26 bool
preStart()27 KGpgChangePass::preStart()
28 {
29 	setSuccess(TS_MSG_SEQUENCE);
30 
31 	return true;
32 }
33 
34 bool
nextLine(const QString & line)35 KGpgChangePass::nextLine(const QString &line)
36 {
37 	if (!line.startsWith(QLatin1String("[GNUPG:] ")))
38 		return false;
39 
40 	if (line.contains(QLatin1String( "keyedit.prompt" ))) {
41 		if (m_seenold && (getSuccess() != TS_USER_ABORTED))
42 			setSuccess(TS_OK);
43 		// no need to save, change is automatically saved by GnuPG
44 		return true;
45 	} else if (line.contains(QLatin1String( "GET_" ))) {
46 		setSuccess(TS_MSG_SEQUENCE);
47 		return true;
48 	}
49 
50 	return false;
51 }
52 
53 bool
passphraseRequested()54 KGpgChangePass::passphraseRequested()
55 {
56 	const QString userIDs = getIdHints();
57 
58 	if (!m_seenold) {
59 		return askPassphrase(i18n("Enter old passphrase for <b>%1</b>", userIDs));
60 	} else {
61 		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));
62 	}
63 
64 	return true;
65 }
66 
67 bool
passphraseReceived()68 KGpgChangePass::passphraseReceived()
69 {
70 	m_seenold = true;
71 	setSuccess(TS_MSG_SEQUENCE);
72 	return false;
73 }
74 
75 bool
hintLine(const KGpgTransaction::ts_hintType hint,const QString & args)76 KGpgChangePass::hintLine(const KGpgTransaction::ts_hintType hint, const QString &args)
77 {
78 	switch (hint) {
79 	case HT_PINENTRY_LAUNCHED:
80 		// If pinentry message appear 2 should be seen: the one asking for the old password,
81 		// and one asking for the new. So the old password has successfully been given if the
82 		// second one appears. BUT: if the user cancels one of these boxes they will reappear
83 		// up to 3 times, so even the third one could be the one asking for the old password.
84 		// Simply assume that everything is fine when pinentry is used, which will make the
85 		// result being set to TS_OK once keyedit.prompt is received.
86 		m_seenold = true;
87 		return true;
88 	default:
89 		return KGpgTransaction::hintLine(hint, args);
90 	}
91 }
92