File: caff.cpp

package info (click to toggle)
kgpg 4:18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 12,168 kB
  • sloc: cpp: 18,911; xml: 159; makefile: 8; sh: 4
file content (387 lines) | stat: -rw-r--r-- 11,168 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (C) 2009,2010,2012,2013,2014,2015,2016 Rolf Eike Beer <kde@opensource.sf-tec.de>
 */

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "caff.h"
#include "caff_p.h"

#include "kgpg_debug.h"
#include "kgpginterface.h"
#include "kgpgsettings.h"
#include "core/KGpgKeyNode.h"
#include "core/KGpgSignableNode.h"
#include "transactions/kgpgdeluid.h"
#include "transactions/kgpgencrypt.h"
#include "transactions/kgpgexport.h"
#include "transactions/kgpgimport.h"
#include "transactions/kgpgsignuid.h"
#include "gpgproc.h"

#include <KLocalizedString>
#include <KMessageBox>

#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QFileInfo>
#include <QTemporaryDir>

KGpgCaffPrivate::KGpgCaffPrivate(KGpgCaff *parent, const KGpgSignableNode::List &ids, const QStringList &signers,
		const KGpgCaff::OperationFlags flags, const KGpgSignTransactionHelper::carefulCheck checklevel)
	: QObject(parent),
	q_ptr(parent),
	m_signers(signers),
	m_flags(flags),
	m_checklevel(checklevel),
	m_gpgVersion(GPGProc::gpgVersion(GPGProc::gpgVersionString(QString()))),
	m_allids(ids)
{
	const QString gpgCfg = KGpgSettings::gpgConfigPath();
	const QString secring = KgpgInterface::getGpgSetting(QLatin1String( "secret-keyring" ), gpgCfg);

	QFileInfo fn(gpgCfg);
	if (!secring.isEmpty()) {
		m_secringfile = secring;
	} else {
		fn.setFile(fn.dir(), QLatin1String("secring.gpg"));
		m_secringfile = QDir::toNativeSeparators(fn.absoluteFilePath());
	}

	fn.setFile(fn.dir(), QLatin1String("private-keys-v1.d"));
	m_secringdir = QDir::toNativeSeparators(fn.absoluteFilePath());
}

KGpgCaffPrivate::~KGpgCaffPrivate()
{
}

void
KGpgCaffPrivate::reexportKey(const KGpgSignableNode *key)
{
	Q_Q(KGpgCaff);

	Q_ASSERT(m_tempdir.isNull());

	// find out if the given id can be used for encryption
	const KGpgKeyNode *k;
	if (key->getType() & KgpgCore::ITYPE_PAIR)
		k = key->toKeyNode();
	else
		k = key->getParentKeyNode()->toKeyNode();

	// skip if not
	if (!k->canEncrypt()) {
		m_noEncIds << key;
		m_allids.removeFirst();
		checkNextLoop();
		return;
	}

	m_tempdir.reset(new QTemporaryDir());

	if (m_gpgVersion >= 0x20100) {
		/* see http://lists.gnupg.org/pipermail/gnupg-devel/2014-December/029296.html */
		QFile seclink(m_secringdir);

		if (!seclink.link(m_tempdir->path() + QLatin1String("private-keys-v1.d"))) {
			KMessageBox::sorry(qobject_cast<QWidget *>(q->parent()),
					i18n("This function is not available on this system. The symbolic link to the private GnuPG keys cannot be created."));
			return;
		}
	}

	// export all keys necessary for signing
	QStringList exportkeys(m_signers);
	exportkeys << key->getKeyNode()->getId();

	KGpgImport *imp = new KGpgImport(this);

	QStringList expOptions(QLatin1String( "--export-options" ));
	expOptions << QLatin1String( "export-clean,export-attribute" );
	KGpgExport *exp = new KGpgExport(this, exportkeys, expOptions);
	exp->setOutputTransaction(imp);

	imp->setGnuPGHome(m_tempdir->path());

	connect(imp, &KGpgImport::done, this, &KGpgCaffPrivate::slotReimportDone);
	imp->start();
}

void
KGpgCaffPrivate::slotReimportDone(int result)
{
	KGpgImport *imp = qobject_cast<KGpgImport *>(sender());

	if (result != KGpgTransaction::TS_OK) {
		abortOperation(result);
	} else {
		if (imp->getImportedIds(0x1).count() != 1 + m_signers.count()) {
			abortOperation(-1);
		} else {
			KGpgSignUid *signuid = new KGpgSignUid(this, m_signers.first(), m_allids.first(), false, m_checklevel);
			signuid->setGnuPGHome(m_tempdir->path());
			if (m_gpgVersion < 0x20100)
				signuid->setSecringFile(m_secringfile);
			connect(signuid, &KGpgSignUid::done, this, &KGpgCaffPrivate::slotSigningFinished);

			signuid->start();
		}
	}

	sender()->deleteLater();
}

void
KGpgCaffPrivate::abortOperation(int result)
{
	Q_Q(KGpgCaff);

	qCDebug(KGPG_LOG_GENERAL) << "transaction" << sender() << "failed, result" << result;
	m_tempdir.reset();

	emit q->aborted();
}

void
KGpgCaffPrivate::checkNextLoop()
{
	Q_Q(KGpgCaff);

	m_tempdir.reset();

	if (m_allids.isEmpty()) {
		if (!m_noEncIds.isEmpty()) {
			QStringList ids;

			for (const KGpgSignableNode *nd : qAsConst(m_noEncIds))
				if (nd->getEmail().isEmpty())
					ids << i18nc("%1 is the key id, %2 is the name and comment of the key or uid",
							"%1: %2", nd->getId(), nd->getNameComment());
				else
					ids << i18nc("%1 is the key id, %2 is the name and comment of the key or uid, %3 is the email address of the uid",
							"%1: %2 &lt;%3&gt;", nd->getId(), nd->getNameComment(), nd->getEmail());

			KMessageBox::detailedSorry(qobject_cast<QWidget *>(q->parent()),
					i18np("No mail was sent for the following user id because it belongs to a key without encryption capability:",
							"No mail was sent for the following user ids because they belong to keys without encryption capability:",
							m_noEncIds.count()),
					ids.join(QLatin1String("\n")));
		}

		if (!m_alreadyIds.isEmpty()) {
			QStringList ids;

			for (const KGpgSignableNode *nd : qAsConst(m_alreadyIds))
				if (nd->getEmail().isEmpty())
					ids << i18nc("%1 is the key id, %2 is the name and comment of the key or uid",
							"%1: %2", nd->getId(), nd->getNameComment());
				else
					ids << i18nc("%1 is the key id, %2 is the name and comment of the key or uid, %3 is the email address of the uid",
							"%1: %2 &lt;%3&gt;", nd->getId(), nd->getNameComment(), nd->getEmail());

			KMessageBox::detailedSorry(qobject_cast<QWidget *>(q->parent()),
					i18np("No mail was sent for the following user id because it was already signed:",
							"No mail was sent for the following user ids because they were already signed:",
							m_alreadyIds.count()),
					ids.join(QLatin1String("\n")));
		}

		emit q->done();
	} else {
		reexportKey(m_allids.first());
	}
}

void
KGpgCaffPrivate::slotSigningFinished(int result)
{
	sender()->deleteLater();

	if (result != KGpgTransaction::TS_OK) {
		if ((result == KGpgSignTransactionHelper::TS_ALREADY_SIGNED) && (m_flags & KGpgCaff::IgnoreAlreadySigned)) {
			m_alreadyIds << m_allids.takeFirst();
			checkNextLoop();
		} else {
			abortOperation(result);
		}
		return;
	}

	const KGpgSignableNode *uid = m_allids.first();

	// if there is no email address we can't send this out anyway, so don't bother.
	// could be improved: if this is the only selected uid from this key go and select
	// a proper mail address to send this to
	if (uid->getEmail().isEmpty()) {
		m_allids.removeFirst();
		checkNextLoop();
	}

	const KGpgKeyNode *key = uid->getKeyNode();

	int uidnum;

	if (uid == key) {
		uidnum = -1;
	} else {
		uidnum = -uid->getId().toInt();
	}

	KGpgDelUid::RemoveMode removeMode;
	switch (KGpgSettings::mailUats()) {
	case 0:
		removeMode = KGpgDelUid::RemoveWithEmail;
		break;
	case 1:
		if (uid == key) {
			removeMode = KGpgDelUid::RemoveWithEmail;
		} else {
			// check if this is the first uid with email address
			const KGpgSignableNode *otherUid;
			int index = 1;
			removeMode = KGpgDelUid::RemoveAllOther;

			while ( (otherUid = key->getUid(index++)) != nullptr) {
				if (otherUid == uid) {
					removeMode = KGpgDelUid::RemoveWithEmail;
					break;
				}
				if (!otherUid->getEmail().isEmpty())
					break;
			}
		}
		break;
	case 2:
		removeMode = KGpgDelUid::RemoveAllOther;
		break;
	default:
		Q_ASSERT(0);
		return;
	}

	KGpgDelUid *deluid = new KGpgDelUid(this, key, uidnum, removeMode);

	deluid->setGnuPGHome(m_tempdir->path());

	connect(deluid, &KGpgDelUid::done, this, &KGpgCaffPrivate::slotDelUidFinished);

	deluid->start();
}

void
KGpgCaffPrivate::slotDelUidFinished(int result)
{
	sender()->deleteLater();

	const KGpgSignableNode *uid = m_allids.first();
	const KGpgKeyNode *key = uid->getKeyNode();

	if (result != KGpgTransaction::TS_OK) {
		// it's no error if we tried to delete all other ids but there is no other id
		if ((uid != key) || (result != KGpgDelUid::TS_NO_SUCH_UID)) {
			abortOperation(result);
			return;
		}
	}

	QStringList expOptions(QLatin1String( "--export-options" ));
	expOptions << QLatin1String( "export-attribute" );

	KGpgExport *exp = new KGpgExport(this, QStringList(key->getId()), expOptions);

	exp->setGnuPGHome(m_tempdir->path());

	connect(exp, &KGpgExport::done, this, &KGpgCaffPrivate::slotExportFinished);

	exp->start();
}

void
KGpgCaffPrivate::slotExportFinished(int result)
{
	sender()->deleteLater();

	if (result != KGpgTransaction::TS_OK) {
		abortOperation(result);
		return;
	}

	const KGpgSignableNode *uid = m_allids.first();
	const KGpgKeyNode *key = uid->getKeyNode();

	KGpgExport *exp = qobject_cast<KGpgExport *>(sender());
	Q_ASSERT(exp != nullptr);

	QString body = KGpgSettings::emailTemplate();
	body.replace(QLatin1Char( '%' ) + i18nc("Email template placeholder for key id", "KEYID") + QLatin1Char( '%' ), key->getId());
	body.replace(QLatin1Char( '%' ) + i18nc("Email template placeholder for key id", "UIDNAME") + QLatin1Char( '%' ), uid->getNameComment());

	body += QLatin1Char( '\n' ) + QLatin1String( exp->getOutputData() );

	KGpgEncrypt *enc = new KGpgEncrypt(this, QStringList(key->getId()), body, KGpgEncrypt::AsciiArmored | KGpgEncrypt::AllowUntrustedEncryption);

	// Set the home directory to make sure custom encrypt options
	// as well as the "always encrypt to" setting are not honored.
	enc->setGnuPGHome(m_tempdir->path());

	connect(enc, &KGpgEncrypt::done, this, &KGpgCaffPrivate::slotTextEncrypted);

	enc->start();
}

void
KGpgCaffPrivate::slotTextEncrypted(int result)
{
	sender()->deleteLater();

	switch (result) {
	case KGpgTransaction::TS_OK: {
		KGpgEncrypt *enc = qobject_cast<KGpgEncrypt *>(sender());
		Q_ASSERT(enc != nullptr);

		const QString text = enc->encryptedText().join(QLatin1String("\n"));

		const KGpgSignableNode *uid = m_allids.takeFirst();

		const QString email = uid->getEmail();
		const QString keyid = uid->getKeyNode()->getId();

		QDesktopServices::openUrl(QUrl(QLatin1String("mailto:") + email +
				QLatin1String("?subject=") + i18nc("%1 is 64 bit key id (in hex), text is used as email subject", "Your key %1", keyid) +
				QLatin1String("&body=") + text));
		break;
		}
	default:
		abortOperation(result);
		break;
	case KGpgTransaction::TS_USER_ABORTED:
		m_allids.clear();
		break;
	}

	checkNextLoop();
}

KGpgCaff::KGpgCaff(QObject *parent, const KGpgSignableNode::List &ids, const QStringList &signids,
		const int checklevel, const OperationFlags flags)
	: QObject(parent),
	d_ptr(new KGpgCaffPrivate(this, ids, signids, flags, static_cast<KGpgSignTransactionHelper::carefulCheck>(checklevel)))
{
}

void
KGpgCaff::run()
{
	Q_D(KGpgCaff);

	d->reexportKey(d->m_allids.first());
}