|
Packit |
d7e8d0 |
/*
|
|
Packit |
d7e8d0 |
qgpgmekeylistjob.cpp
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
This file is part of qgpgme, the Qt API binding for gpgme
|
|
Packit |
d7e8d0 |
Copyright (c) 2004,2008 Klarälvdalens Datakonsult AB
|
|
Packit |
d7e8d0 |
Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
|
|
Packit |
d7e8d0 |
Software engineering by Intevation GmbH
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME is free software; you can redistribute it and/or
|
|
Packit |
d7e8d0 |
modify it under the terms of the GNU General Public License as
|
|
Packit |
d7e8d0 |
published by the Free Software Foundation; either version 2 of the
|
|
Packit |
d7e8d0 |
License, or (at your option) any later version.
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgME is distributed in the hope that it will be useful,
|
|
Packit |
d7e8d0 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
d7e8d0 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
d7e8d0 |
General Public License for more details.
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
You should have received a copy of the GNU General Public License
|
|
Packit |
d7e8d0 |
along with this program; if not, write to the Free Software
|
|
Packit |
d7e8d0 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
In addition, as a special exception, the copyright holders give
|
|
Packit |
d7e8d0 |
permission to link the code of this program with any edition of
|
|
Packit |
d7e8d0 |
the Qt library by Trolltech AS, Norway (or with modified versions
|
|
Packit |
d7e8d0 |
of Qt that use the same license as Qt), and distribute linked
|
|
Packit |
d7e8d0 |
combinations including the two. You must obey the GNU General
|
|
Packit |
d7e8d0 |
Public License in all respects for all of the code used other than
|
|
Packit |
d7e8d0 |
Qt. If you modify this file, you may extend this exception to
|
|
Packit |
d7e8d0 |
your version of the file, but you are not obligated to do so. If
|
|
Packit |
d7e8d0 |
you do not wish to do so, delete this exception statement from
|
|
Packit |
d7e8d0 |
your version.
|
|
Packit |
d7e8d0 |
*/
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
d7e8d0 |
#include "config.h"
|
|
Packit |
d7e8d0 |
#endif
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#include "qgpgmekeylistjob.h"
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#include "key.h"
|
|
Packit |
d7e8d0 |
#include "context.h"
|
|
Packit |
d7e8d0 |
#include "keylistresult.h"
|
|
Packit |
d7e8d0 |
#include <gpg-error.h>
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#include <QStringList>
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#include <algorithm>
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
#include <cstdlib>
|
|
Packit |
d7e8d0 |
#include <cstring>
|
|
Packit |
d7e8d0 |
#include <cassert>
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
using namespace QGpgME;
|
|
Packit |
d7e8d0 |
using namespace GpgME;
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgMEKeyListJob::QGpgMEKeyListJob(Context *context)
|
|
Packit |
d7e8d0 |
: mixin_type(context),
|
|
Packit |
d7e8d0 |
mResult(), mSecretOnly(false)
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
lateInitialization();
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
QGpgMEKeyListJob::~QGpgMEKeyListJob() {}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
static KeyListResult do_list_keys(Context *ctx, const QStringList &pats, std::vector<Key> &keys, bool secretOnly)
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
const _detail::PatternConverter pc(pats);
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
if (const Error err = ctx->startKeyListing(pc.patterns(), secretOnly)) {
|
|
Packit Service |
30b792 |
return KeyListResult(nullptr, err);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
Error err;
|
|
Packit |
d7e8d0 |
do {
|
|
Packit |
d7e8d0 |
keys.push_back(ctx->nextKey(err));
|
|
Packit |
d7e8d0 |
} while (!err);
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
keys.pop_back();
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
const KeyListResult result = ctx->endKeyListing();
|
|
Packit |
d7e8d0 |
ctx->cancelPendingOperation();
|
|
Packit |
d7e8d0 |
return result;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
static QGpgMEKeyListJob::result_type list_keys(Context *ctx, QStringList pats, bool secretOnly)
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (pats.size() < 2) {
|
|
Packit |
d7e8d0 |
std::vector<Key> keys;
|
|
Packit |
d7e8d0 |
const KeyListResult r = do_list_keys(ctx, pats, keys, secretOnly);
|
|
Packit |
d7e8d0 |
return std::make_tuple(r, keys, QString(), Error());
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
// The communication channel between gpgme and gpgsm is limited in
|
|
Packit |
d7e8d0 |
// the number of patterns that can be transported, but they won't
|
|
Packit |
d7e8d0 |
// say to how much, so we need to find out ourselves if we get a
|
|
Packit |
d7e8d0 |
// LINE_TOO_LONG error back...
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
// We could of course just feed them single patterns, and that would
|
|
Packit |
d7e8d0 |
// probably be easier, but the performance penalty would currently
|
|
Packit |
d7e8d0 |
// be noticeable.
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
unsigned int chunkSize = pats.size();
|
|
Packit |
d7e8d0 |
retry:
|
|
Packit |
d7e8d0 |
std::vector<Key> keys;
|
|
Packit |
d7e8d0 |
keys.reserve(pats.size());
|
|
Packit |
d7e8d0 |
KeyListResult result;
|
|
Packit |
d7e8d0 |
do {
|
|
Packit |
d7e8d0 |
const KeyListResult this_result = do_list_keys(ctx, pats.mid(0, chunkSize), keys, secretOnly);
|
|
Packit |
d7e8d0 |
if (this_result.error().code() == GPG_ERR_LINE_TOO_LONG) {
|
|
Packit |
d7e8d0 |
// got LINE_TOO_LONG, try a smaller chunksize:
|
|
Packit |
d7e8d0 |
chunkSize /= 2;
|
|
Packit |
d7e8d0 |
if (chunkSize < 1)
|
|
Packit |
d7e8d0 |
// chunks smaller than one can't be -> return the error.
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
return std::make_tuple(this_result, keys, QString(), Error());
|
|
Packit |
d7e8d0 |
} else {
|
|
Packit |
d7e8d0 |
goto retry;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
} else if (this_result.error().code() == GPG_ERR_EOF) {
|
|
Packit |
d7e8d0 |
// early end of keylisting (can happen when ~/.gnupg doesn't
|
|
Packit |
d7e8d0 |
// exist). Fakeing an empty result:
|
|
Packit |
d7e8d0 |
return std::make_tuple(KeyListResult(), std::vector<Key>(), QString(), Error());
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
// ok, that seemed to work...
|
|
Packit |
d7e8d0 |
result.mergeWith(this_result);
|
|
Packit |
d7e8d0 |
if (result.error().code()) {
|
|
Packit |
d7e8d0 |
break;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
pats = pats.mid(chunkSize);
|
|
Packit |
d7e8d0 |
} while (!pats.empty());
|
|
Packit |
d7e8d0 |
return std::make_tuple(result, keys, QString(), Error());
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
Error QGpgMEKeyListJob::start(const QStringList &patterns, bool secretOnly)
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
mSecretOnly = secretOnly;
|
|
Packit |
d7e8d0 |
run(std::bind(&list_keys, std::placeholders::_1, patterns, secretOnly));
|
|
Packit |
d7e8d0 |
return Error();
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
KeyListResult QGpgMEKeyListJob::exec(const QStringList &patterns, bool secretOnly, std::vector<Key> &keys)
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
mSecretOnly = secretOnly;
|
|
Packit |
d7e8d0 |
const result_type r = list_keys(context(), patterns, secretOnly);
|
|
Packit |
d7e8d0 |
resultHook(r);
|
|
Packit |
d7e8d0 |
keys = std::get<1>(r);
|
|
Packit |
d7e8d0 |
return std::get<0>(r);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
void QGpgMEKeyListJob::resultHook(const result_type &tuple)
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
mResult = std::get<0>(tuple);
|
|
Packit |
d7e8d0 |
Q_FOREACH (const Key &key, std::get<1>(tuple)) {
|
|
Packit |
d7e8d0 |
Q_EMIT nextKey(key);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
void QGpgMEKeyListJob::addMode(KeyListMode mode)
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
context()->addKeyListMode(mode);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
#if 0
|
|
Packit |
d7e8d0 |
void QGpgMEKeyListJob::showErrorDialog(QWidget *parent, const QString &caption) const
|
|
Packit |
d7e8d0 |
{
|
|
Packit |
d7e8d0 |
if (!mResult.error() || mResult.error().isCanceled()) {
|
|
Packit |
d7e8d0 |
return;
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
const QString msg = i18n("<qt>An error occurred while fetching "
|
|
Packit |
d7e8d0 |
"the keys from the backend:"
|
|
Packit |
d7e8d0 |
"%1 </qt>",
|
|
Packit |
d7e8d0 |
QString::fromLocal8Bit(mResult.error().asString()));
|
|
Packit |
d7e8d0 |
KMessageBox::error(parent, msg, caption);
|
|
Packit |
d7e8d0 |
}
|
|
Packit |
d7e8d0 |
#endif
|
|
Packit |
d7e8d0 |
#include "qgpgmekeylistjob.moc"
|