Commit 906a1301 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Import certificate groups when importing certificates from files

Existing groups are replaced. If several files containing groups with
identical IDs are imported, then it is undefined which version of those
groups will be imported.

GnuPG-bug-id: 5638
parent e62cb2d3
......@@ -134,6 +134,7 @@ void ImportCertificateFromFileCommand::doStart()
const auto data = in.readAll();
d->startImport(GpgME::OpenPGP, data, fn);
d->startImport(GpgME::CMS, data, fn);
d->importGroupsFromFile(fn);
}
d->setWaitForMoreJobs(false);
}
......
......@@ -5,6 +5,8 @@
SPDX-FileCopyrightText: 2007, 2008 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2016 Bundesamt für Sicherheit in der Informationstechnik
SPDX-FileContributor: Intevation GmbH
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -13,6 +15,7 @@
#include "importcertificatescommand.h"
#include "importcertificatescommand_p.h"
#include "certifycertificatecommand.h"
#include "kleopatra_debug.h"
......@@ -20,6 +23,7 @@
#include <Libkleo/KeyList>
#include <Libkleo/KeyListSortFilterProxyModel>
#include <Libkleo/KeyCache>
#include <Libkleo/KeyGroupConfig>
#include <Libkleo/Predicates>
#include <Libkleo/Formatting>
#include <Libkleo/Stl_Util>
......@@ -553,6 +557,9 @@ void ImportCertificatesCommand::Private::processResults()
handleExternalCMSImports(results);
handleOwnerTrust(results);
importGroups();
showDetails(results);
auto tv = dynamic_cast<QTreeView *> (view());
......@@ -602,6 +609,40 @@ void ImportCertificatesCommand::Private::keyCacheUpdated()
processResults();
}
static ImportedGroup storeGroup(const KeyGroup &group, const QString &id)
{
const auto status = KeyCache::instance()->group(group.id()).isNull() ?
ImportedGroup::Status::New :
ImportedGroup::Status::Updated;
if (status == ImportedGroup::Status::New) {
KeyCache::mutableInstance()->insert(group);
} else {
KeyCache::mutableInstance()->update(group);
}
return {id, group, status};
}
void ImportCertificatesCommand::Private::importGroups()
{
for (const auto &path : filesToImportGroupsFrom) {
const bool certificateImportSucceeded =
std::any_of(std::cbegin(results), std::cend(results),
[path](const auto &r) {
return r.id == path && !importFailed(r);
});
if (certificateImportSucceeded) {
qCDebug(KLEOPATRA_LOG) << __func__ << "Importing groups from file" << path;
const KeyGroupConfig config{path};
const auto groups = config.readGroups();
std::transform(std::begin(groups), std::end(groups),
std::back_inserter(importedGroups),
[path](const auto &group) {
return storeGroup(group, path);
});
}
}
}
static std::unique_ptr<ImportJob> get_import_job(GpgME::Protocol protocol)
{
Q_ASSERT(protocol != UnknownProtocol);
......@@ -686,6 +727,11 @@ void ImportCertificatesCommand::Private::startImport(GpgME::Protocol protocol, c
}
}
void ImportCertificatesCommand::Private::importGroupsFromFile(const QString &filename)
{
filesToImportGroupsFrom.push_back(filename);
}
void ImportCertificatesCommand::doCancel()
{
std::for_each(std::cbegin(d->jobs), std::cend(d->jobs),
......
......@@ -3,6 +3,8 @@
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2007, 2008 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -12,6 +14,8 @@
#include "command_p.h"
#include "importcertificatescommand.h"
#include <Libkleo/KeyGroup>
#include <gpgme++/global.h>
#include <gpgme++/importresult.h>
......@@ -61,6 +65,18 @@ struct ImportResultData
GpgME::ImportResult result;
};
struct ImportedGroup
{
enum class Status
{
New,
Updated,
};
QString id;
Kleo::KeyGroup group;
Status status;
};
class Kleo::ImportCertificatesCommand::Private : public Command::Private
{
friend class ::Kleo::ImportCertificatesCommand;
......@@ -80,6 +96,8 @@ public:
void importResult(const GpgME::ImportResult &);
void importResult(const ImportResultData &result);
void importGroupsFromFile(const QString &filename);
void showError(QWidget *parent, const GpgME::Error &error, const QString &id = QString());
void showError(const GpgME::Error &error, const QString &id = QString());
......@@ -97,12 +115,15 @@ private:
void processResults();
void tryToFinish();
void keyCacheUpdated();
void importGroups();
private:
bool waitForMoreJobs = false;
std::vector<GpgME::Protocol> nonWorkingProtocols;
std::vector<ImportJobData> jobs;
std::vector<QString> filesToImportGroupsFrom;
std::vector<ImportResultData> results;
std::vector<ImportedGroup> importedGroups;
std::shared_ptr<KeyCacheAutoRefreshSuspension> keyCacheAutoRefreshSuspension;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment