diff --git a/src/commands/dumpcertificatecommand.cpp b/src/commands/dumpcertificatecommand.cpp index a989febf6..27578a681 100644 --- a/src/commands/dumpcertificatecommand.cpp +++ b/src/commands/dumpcertificatecommand.cpp @@ -1,366 +1,366 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/dumpcertificatecommand.cpp This file is part of Kleopatra, the KDE keymanager Copyright (c) 2008 Klarälvdalens Datakonsult AB Kleopatra 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. Kleopatra is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include #include "dumpcertificatecommand.h" #include "command_p.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static const int PROCESS_TERMINATE_TIMEOUT = 5000; // milliseconds namespace { class DumpCertificateDialog : public QDialog { Q_OBJECT public: explicit DumpCertificateDialog(QWidget *parent = nullptr) : QDialog(parent), ui(this) { resize(600, 500); } Q_SIGNALS: void updateRequested(); public Q_SLOTS: void append(const QString &line) { ui.logTextWidget.append(line); ui.logTextWidget.ensureCursorVisible(); } void clear() { ui.logTextWidget.clear(); } private: struct Ui { QTextEdit logTextWidget; QPushButton updateButton, closeButton; QVBoxLayout vlay; QHBoxLayout hlay; explicit Ui(DumpCertificateDialog *q) : logTextWidget(q), updateButton(i18nc("@action:button Update the log text widget", "&Update"), q), closeButton(q), vlay(q), hlay() { KGuiItem::assign(&closeButton, KStandardGuiItem::close()); KDAB_SET_OBJECT_NAME(logTextWidget); KDAB_SET_OBJECT_NAME(updateButton); KDAB_SET_OBJECT_NAME(closeButton); KDAB_SET_OBJECT_NAME(vlay); KDAB_SET_OBJECT_NAME(hlay); logTextWidget.setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); logTextWidget.setReadOnly(true); vlay.addWidget(&logTextWidget, 1); vlay.addLayout(&hlay); hlay.addWidget(&updateButton); hlay.addStretch(1); hlay.addWidget(&closeButton); connect(&updateButton, &QAbstractButton::clicked, q, &DumpCertificateDialog::updateRequested); connect(&closeButton, &QAbstractButton::clicked, q, &QWidget::close); } } ui; }; } using namespace Kleo; using namespace Kleo::Commands; static QByteArray chomped(QByteArray ba) { while (ba.endsWith('\n') || ba.endsWith('\r')) { ba.chop(1); } return ba; } class DumpCertificateCommand::Private : Command::Private { friend class ::Kleo::Commands::DumpCertificateCommand; DumpCertificateCommand *q_func() const { return static_cast(q); } public: explicit Private(DumpCertificateCommand *qq, KeyListController *c); ~Private(); QString errorString() const { return QString::fromLocal8Bit(errorBuffer); } private: void init(); void refreshView(); private: void slotProcessFinished(int, QProcess::ExitStatus); void slotProcessReadyReadStandardOutput() { while (process.canReadLine()) { - const QString line = QString::fromUtf8(chomped(process.readLine())); + const QString line = Kleo::stringFromGpgOutput(chomped(process.readLine())); if (dialog) { dialog->append(line); } outputBuffer.push_back(line); } } void slotProcessReadyReadStandardError() { errorBuffer += process.readAllStandardError(); } void slotUpdateRequested() { if (process.state() == QProcess::NotRunning) { refreshView(); } } void slotDialogDestroyed() { dialog = nullptr; if (process.state() != QProcess::NotRunning) { q->cancel(); } else { finished(); } } private: QPointer dialog; KProcess process; QByteArray errorBuffer; QStringList outputBuffer; bool useDialog; bool canceled; }; DumpCertificateCommand::Private *DumpCertificateCommand::d_func() { return static_cast(d.get()); } const DumpCertificateCommand::Private *DumpCertificateCommand::d_func() const { return static_cast(d.get()); } #define d d_func() #define q q_func() DumpCertificateCommand::Private::Private(DumpCertificateCommand *qq, KeyListController *c) : Command::Private(qq, c), process(), errorBuffer(), outputBuffer(), useDialog(true), canceled(false) { process.setOutputChannelMode(KProcess::SeparateChannels); process.setReadChannel(KProcess::StandardOutput); } DumpCertificateCommand::Private::~Private() { if (dialog && !dialog->isVisible()) { delete dialog; } } DumpCertificateCommand::DumpCertificateCommand(KeyListController *c) : Command(new Private(this, c)) { d->init(); } DumpCertificateCommand::DumpCertificateCommand(QAbstractItemView *v, KeyListController *c) : Command(v, new Private(this, c)) { d->init(); } DumpCertificateCommand::DumpCertificateCommand(const GpgME::Key &k) : Command(k, new Private(this, nullptr)) { d->init(); } void DumpCertificateCommand::Private::init() { connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), q, SLOT(slotProcessFinished(int,QProcess::ExitStatus))); connect(&process, SIGNAL(readyReadStandardError()), q, SLOT(slotProcessReadyReadStandardError())); connect(&process, SIGNAL(readyReadStandardOutput()), q, SLOT(slotProcessReadyReadStandardOutput())); if (!key().isNull()) { process << gpgSmPath() << QStringLiteral("--dump-cert") << QLatin1String(key().primaryFingerprint()); } } DumpCertificateCommand::~DumpCertificateCommand() {} void DumpCertificateCommand::setUseDialog(bool use) { d->useDialog = use; } bool DumpCertificateCommand::useDialog() const { return d->useDialog; } QStringList DumpCertificateCommand::output() const { return d->outputBuffer; } void DumpCertificateCommand::doStart() { const std::vector keys = d->keys(); if (keys.size() != 1 || keys.front().protocol() != GpgME::CMS) { d->finished(); return; } if (d->useDialog) { d->dialog = new DumpCertificateDialog; d->dialog->setAttribute(Qt::WA_DeleteOnClose); d->dialog->setWindowTitle(i18n("Certificate Dump")); connect(d->dialog, SIGNAL(updateRequested()), this, SLOT(slotUpdateRequested())); connect(d->dialog, SIGNAL(destroyed()), this, SLOT(slotDialogDestroyed())); } d->refreshView(); } void DumpCertificateCommand::Private::refreshView() { if (dialog) { dialog->clear(); } errorBuffer.clear(); outputBuffer.clear(); process.start(); if (process.waitForStarted()) { if (dialog) { dialog->show(); } } else { KMessageBox::error(dialog ? static_cast(dialog) : parentWidgetOrView(), i18n("Unable to start process gpgsm. " "Please check your installation."), i18n("Dump Certificate Error")); finished(); } } void DumpCertificateCommand::doCancel() { d->canceled = true; if (d->process.state() != QProcess::NotRunning) { d->process.terminate(); QTimer::singleShot(PROCESS_TERMINATE_TIMEOUT, &d->process, &QProcess::kill); } if (d->dialog) { d->dialog->close(); } d->dialog = nullptr; } void DumpCertificateCommand::Private::slotProcessFinished(int code, QProcess::ExitStatus status) { if (!canceled) { if (status == QProcess::CrashExit) KMessageBox::error(dialog, i18n("The GpgSM process that tried to dump the certificate " "ended prematurely because of an unexpected error. " "Please check the output of gpgsm --dump-cert %1 for details.", QLatin1String(key().primaryFingerprint())), i18n("Dump Certificate Error")); else if (code) KMessageBox::error(dialog, i18n("An error occurred while trying to dump the certificate. " "The output from GpgSM was:\n%1", errorString()), i18n("Dump Certificate Error")); } if (!useDialog) { slotDialogDestroyed(); } } #undef d #undef q #include "moc_dumpcertificatecommand.cpp" #include "dumpcertificatecommand.moc" diff --git a/src/commands/gnupgprocesscommand.cpp b/src/commands/gnupgprocesscommand.cpp index 347595ebf..0c1bd108c 100644 --- a/src/commands/gnupgprocesscommand.cpp +++ b/src/commands/gnupgprocesscommand.cpp @@ -1,404 +1,405 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/gnupgprocesscommand.cpp This file is part of Kleopatra, the KDE keymanager Copyright (c) 2008 Klarälvdalens Datakonsult AB Kleopatra 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. Kleopatra is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include #include "gnupgprocesscommand.h" #include "command_p.h" +#include "utils/gnupg-helper.h" + #include "kleopatra_debug.h" #include #include #include #include #include #include #include #include #include #include #include #include #include static const int PROCESS_TERMINATE_TIMEOUT = 5000; // milliseconds using namespace Kleo; using namespace Kleo::Commands; namespace { class OutputDialog : public QDialog { Q_OBJECT public: explicit OutputDialog(QWidget *parent = nullptr) : QDialog(parent), vlay(this), logTextWidget(this), buttonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Close, Qt::Horizontal, this) { KDAB_SET_OBJECT_NAME(vlay); KDAB_SET_OBJECT_NAME(logTextWidget); KDAB_SET_OBJECT_NAME(buttonBox); logTextWidget.setReadOnly(true); vlay.addWidget(&logTextWidget, 1); vlay.addWidget(&buttonBox); connect(closeButton(), &QAbstractButton::clicked, this, &QWidget::close); connect(cancelButton(), &QAbstractButton::clicked, this, &OutputDialog::slotCancelClicked); resize(600, 500); } Q_SIGNALS: void cancelRequested(); public Q_SLOTS: void message(const QString &s) { logTextWidget.append(s); logTextWidget.ensureCursorVisible(); } void setComplete(bool complete) { cancelButton()->setVisible(!complete); } private Q_SLOTS: void slotCancelClicked() { cancelButton()->hide(); Q_EMIT cancelRequested(); } private: QAbstractButton *closeButton() const { return buttonBox.button(QDialogButtonBox::Close); } QAbstractButton *cancelButton() const { return buttonBox.button(QDialogButtonBox::Cancel); } private: QVBoxLayout vlay; QTextEdit logTextWidget; QDialogButtonBox buttonBox; }; } class GnuPGProcessCommand::Private : Command::Private { friend class ::Kleo::Commands::GnuPGProcessCommand; GnuPGProcessCommand *q_func() const { return static_cast(q); } public: explicit Private(GnuPGProcessCommand *qq, KeyListController *c); ~Private(); private: void init(); void ensureDialogCreated() { if (!showsOutputWindow) { return; } if (!dialog) { dialog = new OutputDialog; dialog->setAttribute(Qt::WA_DeleteOnClose); applyWindowID(dialog); connect(dialog.data(), &OutputDialog::cancelRequested, q, &Command::cancel); dialog->setWindowTitle(i18n("Subprocess Diagnostics")); } } void ensureDialogVisible() { if (!showsOutputWindow) { return; } ensureDialogCreated(); if (dialog->isVisible()) { dialog->raise(); } else { dialog->show(); } #ifdef Q_OS_WIN KWindowSystem::forceActiveWindow(dialog->winId()); #endif } void message(const QString &msg) { if (dialog) { dialog->message(msg); } else { qCDebug(KLEOPATRA_LOG) << msg; } } private: void slotProcessFinished(int, QProcess::ExitStatus); void slotProcessReadyReadStandardError(); private: QProcess process; QPointer dialog; QStringList arguments; QByteArray errorBuffer; bool ignoresSuccessOrFailure; bool showsOutputWindow; bool canceled; }; GnuPGProcessCommand::Private *GnuPGProcessCommand::d_func() { return static_cast(d.get()); } const GnuPGProcessCommand::Private *GnuPGProcessCommand::d_func() const { return static_cast(d.get()); } #define d d_func() #define q q_func() GnuPGProcessCommand::Private::Private(GnuPGProcessCommand *qq, KeyListController *c) : Command::Private(qq, c), process(), dialog(), errorBuffer(), ignoresSuccessOrFailure(false), showsOutputWindow(false), canceled(false) { process.setReadChannel(QProcess::StandardError); } GnuPGProcessCommand::Private::~Private() {} GnuPGProcessCommand::GnuPGProcessCommand(KeyListController *c) : Command(new Private(this, c)) { d->init(); } GnuPGProcessCommand::GnuPGProcessCommand(QAbstractItemView *v, KeyListController *c) : Command(v, new Private(this, c)) { d->init(); } GnuPGProcessCommand::GnuPGProcessCommand(const GpgME::Key &key) : Command(key, new Private(this, nullptr)) { d->init(); } void GnuPGProcessCommand::Private::init() { connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), q, SLOT(slotProcessFinished(int,QProcess::ExitStatus))); connect(&process, SIGNAL(readyReadStandardError()), q, SLOT(slotProcessReadyReadStandardError())); } GnuPGProcessCommand::~GnuPGProcessCommand() {} QDialog *GnuPGProcessCommand::dialog() const { return d->dialog; } bool GnuPGProcessCommand::preStartHook(QWidget *) const { return true; } void GnuPGProcessCommand::postSuccessHook(QWidget *) { } void GnuPGProcessCommand::doStart() { if (!preStartHook(d->parentWidgetOrView())) { d->finished(); return; } d->arguments = arguments(); d->process.setProgram(d->arguments.takeFirst()); + d->process.setArguments(d->arguments); // Historically code using this expects arguments first to be the program. - d->arguments.prepend(QLatin1String("utf-8")); - d->arguments.prepend(QLatin1String("--display-charset")); d->arguments.prepend(d->process.program()); d->process.start(); if (!d->process.waitForStarted()) { d->error(i18n("Unable to start process %1. " "Please check your installation.", d->arguments[0]), errorCaption()); d->finished(); } else { d->ensureDialogVisible(); d->message(i18n("Starting %1...", d->arguments.join(QLatin1Char(' ')))); } } void GnuPGProcessCommand::doCancel() { d->canceled = true; if (d->process.state() != QProcess::NotRunning) { d->process.terminate(); QTimer::singleShot(PROCESS_TERMINATE_TIMEOUT, &d->process, &QProcess::kill); } } void GnuPGProcessCommand::Private::slotProcessFinished(int code, QProcess::ExitStatus status) { if (!canceled) { if (status == QProcess::CrashExit) { const QString msg = q->crashExitMessage(arguments); if (!msg.isEmpty()) { error(msg, q->errorCaption()); } } else if (ignoresSuccessOrFailure) { if (dialog) { message(i18n("Process finished")); } else { ; } } else if (code) { const QString msg = q->errorExitMessage(arguments); if (!msg.isEmpty()) { error(q->errorExitMessage(arguments), q->errorCaption()); } } else { q->postSuccessHook(parentWidgetOrView()); const QString successMessage = q->successMessage(arguments); if (!successMessage.isNull()) { if (dialog) { message(successMessage); } else { information(successMessage, q->successCaption()); } } } } if (dialog) { dialog->setComplete(true); } finished(); } void GnuPGProcessCommand::Private::slotProcessReadyReadStandardError() { auto ba = process.readAllStandardError(); errorBuffer += ba; while (ba.endsWith('\n') || ba.endsWith('\r')) { ba.chop(1); } - message(QString::fromUtf8(ba.constData(), ba.size())); + message(Kleo::stringFromGpgOutput(ba)); } QString GnuPGProcessCommand::errorString() const { - return QString::fromUtf8(d->errorBuffer); + return Kleo::stringFromGpgOutput(d->errorBuffer); } void GnuPGProcessCommand::setIgnoresSuccessOrFailure(bool ignores) { d->ignoresSuccessOrFailure = ignores; } bool GnuPGProcessCommand::ignoresSuccessOrFailure() const { return d->ignoresSuccessOrFailure; } void GnuPGProcessCommand::setShowsOutputWindow(bool show) { if (show == d->showsOutputWindow) { return; } d->showsOutputWindow = show; if (show) { d->ensureDialogCreated(); } else { if (d->dialog) { d->dialog->deleteLater(); } d->dialog = nullptr; } } bool GnuPGProcessCommand::showsOutputWindow() const { return d->showsOutputWindow; } QProcess *GnuPGProcessCommand::process() { return &d->process; } QString GnuPGProcessCommand::successCaption() const { return QString(); } QString GnuPGProcessCommand::successMessage(const QStringList &args) const { Q_UNUSED(args); return QString(); } #undef d #undef q #include "moc_gnupgprocesscommand.cpp" #include "gnupgprocesscommand.moc" diff --git a/src/utils/gnupg-helper.cpp b/src/utils/gnupg-helper.cpp index f4ad4c19d..b724a4d87 100644 --- a/src/utils/gnupg-helper.cpp +++ b/src/utils/gnupg-helper.cpp @@ -1,301 +1,352 @@ /* -*- mode: c++; c-basic-offset:4 -*- utils/gnupg-helper.cpp This file is part of Kleopatra, the KDE keymanager Copyright (c) 2008 Klarälvdalens Datakonsult AB 2016 by Bundesamt für Sicherheit in der Informationstechnik Software engineering by Intevation GmbH Kleopatra 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. Kleopatra is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include #include "gnupg-helper.h" #include "utils/hex.h" #include #include #include #include #include #include "kleopatra_debug.h" #include #include #include #include #include #include #include #include #ifdef Q_OS_WIN #include "gnupg-registry.h" #endif // Q_OS_WIN #include #include using namespace GpgME; QString Kleo::gnupgHomeDirectory() { #ifdef Q_OS_WIN return QFile::decodeName(default_homedir()); #else const QByteArray gnupgHome = qgetenv("GNUPGHOME"); if (!gnupgHome.isEmpty()) { return QFile::decodeName(gnupgHome); } else { return QDir::homePath() + QLatin1String("/.gnupg"); } #endif } int Kleo::makeGnuPGError(int code) { return gpg_error(static_cast(code)); } static QString findGpgExe(GpgME::Engine engine, const QString &exe) { const GpgME::EngineInfo info = GpgME::engineInfo(engine); return info.fileName() ? QFile::decodeName(info.fileName()) : QStandardPaths::findExecutable(exe); } QString Kleo::gpgConfPath() { return findGpgExe(GpgME::GpgConfEngine, QStringLiteral("gpgconf")); } QString Kleo::gpgSmPath() { return findGpgExe(GpgME::GpgSMEngine, QStringLiteral("gpgsm")); } QString Kleo::gpgPath() { return findGpgExe(GpgME::GpgEngine, QStringLiteral("gpg")); } QStringList Kleo::gnupgFileWhitelist() { return QStringList() // The obvious pubring << QStringLiteral("pubring.gpg") // GnuPG 2.1 pubring << QStringLiteral("pubring.kbx") // Trust in X509 Certificates << QStringLiteral("trustlist.txt") // Trustdb controls ownertrust and thus WOT validity << QStringLiteral("trustdb.gpg") // We want to update when smartcard status changes << QStringLiteral("reader*.status") // No longer used in 2.1 but for 2.0 we want this << QStringLiteral("secring.gpg") // Changes to the trustmodel / compliance mode might // affect validity so we check this, too. // Globbing for gpg.conf* here will trigger too often // as gpgconf creates files like gpg.conf.bak or // gpg.conf.tmp12312.gpgconf that should not trigger // a change. << QStringLiteral("gpg.conf") << QStringLiteral("gpg.conf-?") << QStringLiteral("gpg.conf-?.?") ; } QString Kleo::gpg4winVersion() { QFile versionFile(gpg4winInstallPath() + QStringLiteral("/../VERSION")); if (!versionFile.open(QIODevice::ReadOnly)) { // No need to translate this should only be the case in development // builds. return QStringLiteral("Unknown (no VERSION file found)"); } const QString g4wTag = QString::fromUtf8(versionFile.readLine()); if (!g4wTag.startsWith(QLatin1String("gpg4win"))) { // Hu? Something unknown return QStringLiteral("Unknown (invalid VERSION file found)"); } // Next line is version. return QString::fromUtf8(versionFile.readLine()).trimmed(); } QString Kleo::gpg4winInstallPath() { #ifdef Q_OS_WIN // QApplication::applicationDirPath is only used as a fallback // to support the case where Kleopatra is not installed from // Gpg4win but Gpg4win is also installed. char *instDir = read_w32_registry_string("HKEY_LOCAL_MACHINE", "Software/GPG4Win", "Install Directory"); if (!instDir) { // Fallback to HKCU instDir = read_w32_registry_string("HKEY_CURRENT_USER", "Software/GPG4Win", "Install Directory"); } if (instDir) { QString ret = QString::fromLocal8Bit(instDir) + QStringLiteral("/bin"); free(instDir); return ret; } qCDebug(KLEOPATRA_LOG) << "Gpg4win not found. Falling back to Kleopatra instdir."; #endif return QCoreApplication::applicationDirPath(); } QString Kleo::gnupgInstallPath() { return gpgConfListDir("bindir"); } QString Kleo::gpgConfListDir(const char *which) { if (!which || !*which) { return QString(); } const QString gpgConfPath = Kleo::gpgConfPath(); if (gpgConfPath.isEmpty()) { return QString(); } QProcess gpgConf; qCDebug(KLEOPATRA_LOG) << "gpgConfListDir: starting " << qPrintable(gpgConfPath) << " --list-dirs"; gpgConf.start(gpgConfPath, QStringList() << QStringLiteral("--list-dirs")); if (!gpgConf.waitForFinished()) { qCDebug(KLEOPATRA_LOG) << "gpgConfListDir(): failed to execute gpgconf: " << qPrintable(gpgConf.errorString()); qCDebug(KLEOPATRA_LOG) << "output was:" << endl << gpgConf.readAllStandardError().constData(); return QString(); } const QList lines = gpgConf.readAllStandardOutput().split('\n'); for (const QByteArray &line : lines) if (line.startsWith(which) && line[qstrlen(which)] == ':') { const int begin = qstrlen(which) + 1; int end = line.size(); while (end && (line[end - 1] == '\n' || line[end - 1] == '\r')) { --end; } const QString result = QDir::fromNativeSeparators(QFile::decodeName(hexdecode(line.mid(begin, end - begin)))); qCDebug(KLEOPATRA_LOG) << "gpgConfListDir: found " << qPrintable(result) << " for '" << which << "'entry"; return result; } qCDebug(KLEOPATRA_LOG) << "gpgConfListDir(): didn't find '" << which << "'" << "entry in output:" << endl << gpgConf.readAllStandardError().constData(); return QString(); } bool Kleo::engineIsVersion(int major, int minor, int patch, Engine engine) { static QMap > cachedVersions; const int required_version[] = {major, minor, patch}; // Gpgconf means spawning processes which is expensive on windows. std::array actual_version; if (!cachedVersions.contains(engine)) { const Error err = checkEngine(engine); if (err.code() == GPG_ERR_INV_ENGINE) { qCDebug(KLEOPATRA_LOG) << "isVersion: invalid engine. '"; return false; } const char *actual = GpgME::engineInfo(engine).version(); QRegExp rx(QLatin1String("(\\d+)\\.(\\d+)\\.(\\d+)(?:-svn\\d+)?.*")); if (!rx.exactMatch(QString::fromUtf8(actual))) { qCDebug(KLEOPATRA_LOG) << "Can't parse version " << actual; return false; } bool ok; for (int i = 0; i < 3; ++i) { ok = false; actual_version[i] = rx.cap(i + 1).toUInt(&ok); Q_ASSERT(ok); } qCDebug(KLEOPATRA_LOG) << "Parsed" << actual << "as: " << actual_version[0] << '.' << actual_version[1] << '.' << actual_version[2] << '.'; cachedVersions.insert(engine, actual_version); } else { actual_version = cachedVersions.value(engine); } // return ! ( actual_version < required_version ) return !std::lexicographical_compare(std::begin(actual_version), std::end(actual_version), std::begin(required_version), std::end(required_version)); } const QString& Kleo::paperKeyInstallPath() { static const QString pkPath = QStandardPaths::findExecutable(QStringLiteral("paperkey"), QStringList() << QCoreApplication::applicationDirPath()).isEmpty() ? QStandardPaths::findExecutable(QStringLiteral("paperkey")) : QStandardPaths::findExecutable(QStringLiteral("paperkey"), QStringList() << QCoreApplication::applicationDirPath()); return pkPath; } bool Kleo::haveKeyserverConfigured() { if (engineIsVersion(2, 1, 19)) { // since 2.1.19 there is a builtin keyserver return true; } const QGpgME::CryptoConfig *const config = QGpgME::cryptoConfig(); if (!config) { return false; } const QGpgME::CryptoConfigEntry *const entry = config->entry(QStringLiteral("gpg"), QStringLiteral("Keyserver"), QStringLiteral("keyserver")); return entry && !entry->stringValue().isEmpty(); } bool Kleo::gpgComplianceP(const char *mode) { const auto conf = QGpgME::cryptoConfig(); const auto entry = conf->entry(QStringLiteral("gpg"), QStringLiteral("Configuration"), QStringLiteral("compliance")); return entry && entry->stringValue() == QString(mode); } enum GpgME::UserID::Validity Kleo::keyValidity(const GpgME::Key &key) { enum UserID::Validity validity = UserID::Validity::Unknown; for (const auto &uid: key.userIDs()) { if (validity == UserID::Validity::Unknown || validity > uid.validity()) { validity = uid.validity(); } } return validity; } + +#ifdef Q_OS_WIN +static QString fromEncoding (unsigned int src_encoding, const char *data) +{ + int n = MultiByteToWideChar(src_encoding, 0, data, -1, NULL, 0); + if (n < 0) { + return QString(); + } + + wchar_t *result = (wchar_t *) malloc ((n+1) * sizeof *result); + + n = MultiByteToWideChar(src_encoding, 0, data, -1, result, n); + if (n < 0) { + free(result); + return QString(); + } + const auto ret = QString::fromWCharArray(result, n); + free(result); + return ret; +} +#endif + +QString Kleo::stringFromGpgOutput(const QByteArray &ba) +{ +#ifdef Q_OS_WIN + /* Qt on Windows uses GetACP while GnuPG prefers + * GetConsoleOutputCP. + * + * As we are not a console application GetConsoleOutputCP + * usually returns 0. + * From experience the closest thing that let's us guess + * what GetConsoleOutputCP returns for a console application + * it appears to be the OEMCP. + */ + unsigned int cpno = GetConsoleOutputCP (); + if (!cpno) { + cpno = GetOEMCP(); + } + if (!cpno) { + cpno = GetACP(); + } + if (!cpno) { + qCDebug(KLEOPATRA_LOG) << "Failed to find native codepage"; + return QString(); + } + + return fromEncoding(cpno, ba.constData()); +#else + return QString::fromLocal8Bit(ba); +#endif +} diff --git a/src/utils/gnupg-helper.h b/src/utils/gnupg-helper.h index fbb116c92..412e6b0c8 100644 --- a/src/utils/gnupg-helper.h +++ b/src/utils/gnupg-helper.h @@ -1,82 +1,87 @@ /* -*- mode: c++; c-basic-offset:4 -*- utils/gnupg-helper.h This file is part of Kleopatra, the KDE keymanager Copyright (c) 2008 Klarälvdalens Datakonsult AB Kleopatra 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. Kleopatra is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #ifndef __KLEOPATRA_GNUPGHELPER_H__ #define __KLEOPATRA_GNUPGHELPER_H__ #include #include /* Support compilation with GPGME older than 1.9. */ #include #if GPGMEPP_VERSION > 0x10900 # define GPGME_HAS_KEY_IS_DEVS #endif /* Does the given object comply with DE_VS? This macro can be used to ensure that we can still build against older versions of GPGME without cluttering the code with preprocessor conditionals. */ #ifdef GPGME_HAS_KEY_IS_DEVS # define IS_DE_VS(x) (x).isDeVs() #else # define IS_DE_VS(x) false #endif class QString; class QStringList; +class QByteArray; namespace Kleo { QString gnupgHomeDirectory(); QString gpgConfPath(); QString gpgSmPath(); QString gpgPath(); QString gpgConfListDir(const char *which); QString gpg4winInstallPath(); QString gpg4winVersion(); QString gnupgInstallPath(); const QString& paperKeyInstallPath(); QStringList gnupgFileWhitelist(); - int makeGnuPGError(int code); bool engineIsVersion(int major, int minor, int patch, GpgME::Engine = GpgME::GpgConfEngine); bool haveKeyserverConfigured(); bool gpgComplianceP(const char *mode); enum GpgME::UserID::Validity keyValidity(const GpgME::Key &key); + +/* Convert GnuPG output to a QString with proper encoding. + * Takes Gpg Quirks into account and might handle future + * changes in GnuPG Output. */ +QString stringFromGpgOutput(const QByteArray &ba); } #endif // __KLEOPATRA_GNUPGHELPER_H__