Commit d4916218 authored by Rolf Eike Beer's avatar Rolf Eike Beer
Browse files

use --list-config to request certain information from GnuPG

This is available since GnuPG 1.3.5, so it's safe to use. This makes parsing
certain output much more reliable.
parent f33f8c75
......@@ -253,28 +253,3 @@ KGpgGroupNode::remove()
conffile.resize(0);
t << lines.join(QLatin1String("\n")) + QLatin1Char('\n');
}
QStringList
KGpgGroupNode::readGroups()
{
QStringList groups;
QFile qfile(KGpgSettings::gpgConfigPath());
if (!qfile.exists() || !qfile.open(QIODevice::ReadOnly))
return groups;
QTextStream t(&qfile);
while (!t.atEnd()) {
QString line = t.readLine().simplified().section(QLatin1Char('#'), 0, 0);
if (!KGpgGroupNodePrivate::groupPattern().exactMatch(line))
continue;
// remove the "group " at the start
line.remove(0, 6);
// transform it in a simple space separated list
groups.append(line.replace(QLatin1Char('='), QLatin1Char(' ')).simplified());
}
return groups;
}
/* Copyright 2008,2009,2010,2012 Rolf Eike Beer <kde@opensource.sf-tec.de>
/* Copyright 2008,2009,2010,2012,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
......@@ -70,15 +70,6 @@ public:
* Remove this group from the GnuPG config file
*/
void remove();
/**
* @brief get all groups from GnuPG config file
* @return list of groups names and their keys
*
* The strings are themself space separated list. The first entry is the
* group name, the others are the keys inside
*/
static QStringList readGroups();
};
#endif /* KGPGGROUPNODE_H */
/* Copyright 2008,2009,2010,2012,2013 Rolf Eike Beer <kde@opensource.sf-tec.de>
/* Copyright 2008,2009,2010,2012,2013,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
......@@ -56,9 +56,11 @@ void
KGpgRootNode::addGroups(const QStringList &groups)
{
foreach (const QString &group, groups) {
QStringList members = group.split(QLatin1Char(' '));
const QString groupName = members.takeFirst();
new KGpgGroupNode(this, groupName, members);
const QStringList parts = group.split(QLatin1Char(':'));
if (parts.count() < 2)
continue;
const QString groupName = parts.first();
new KGpgGroupNode(this, groupName, parts.at(1).split(QLatin1Char(';')));
}
}
......
/*
* Copyright (C) 2007,2008,2009,2010,2011,2012,2013,2014 Rolf Eike Beer <kde@opensource.sf-tec.de>
* Copyright (C) 2007,2008,2009,2010,2011,2012,2013,2014,2016 Rolf Eike Beer <kde@opensource.sf-tec.de>
*/
/***************************************************************************
......@@ -316,35 +316,23 @@ int GPGProc::gpgVersion(const QString &vstr)
QString GPGProc::gpgVersionString(const QString &binary)
{
GPGProc process(0, binary);
process << QLatin1String( "--version" );
process.start();
process.waitForFinished(-1);
const QStringList vlist = getGgpParsedConfig(binary, "version");
if (process.exitCode() == 255)
if (vlist.empty())
return QString();
QString line;
if (process.readln(line) != -1)
return line.simplified().section(QLatin1Char( ' ' ), -1);
else
return QString();
return vlist.first().split(QLatin1Char(':')).first();
}
QStringList
GPGProc::getGpgPubkeyAlgorithms(const QString &binary)
{
QStringList ret;
QStringList ret = getGgpParsedConfig(binary, "pubkeyname");
const auto algorithms = getGpgStatusLine(binary, QLatin1String("Pubkey:")).split(QLatin1Char(','));
for (const QString &s : algorithms) {
QString t = s.trimmed();
if (t == QLatin1String("?"))
continue;
ret << t;
}
if (ret.isEmpty())
return ret;
return ret;
return ret.first().split(QLatin1Char(':')).first().split(QLatin1Char(';'));
}
QString GPGProc::getGpgStartupError(const QString &binary)
......@@ -366,6 +354,29 @@ QString GPGProc::getGpgStartupError(const QString &binary)
return result;
}
QStringList GPGProc::getGgpParsedConfig(const QString &binary, const QByteArray &key)
{
GPGProc process(0, binary);
process << QLatin1String("--list-config") << QLatin1String("--with-colons");
process.start();
process.waitForFinished(-1);
QStringList result;
QByteArray filter = "cfg:";
if (!key.isEmpty())
filter += key + ':';
while (process.hasLineStandardOutput()) {
QByteArray tmp;
process.readLineStandardOutput(&tmp);
if (tmp.startsWith(filter))
result << QString::fromUtf8(tmp.mid(filter.length()));
}
return result;
}
QString GPGProc::getGpgHome(const QString &binary)
{
// First try: if environment is set GnuPG will use that directory
......
/*
* Copyright (C) 2007,2008,2009,2010,2011,2012,2013,2014 Rolf Eike Beer <kde@opensource.sf-tec.de>
* Copyright (C) 2007,2008,2009,2010,2011,2012,2013,2014,2016 Rolf Eike Beer <kde@opensource.sf-tec.de>
*/
/***************************************************************************
......@@ -13,10 +13,11 @@
#ifndef GPGPROC_H
#define GPGPROC_H
#include <QString>
#include "klinebufferedprocess.h"
#include <QByteArray>
#include <QString>
class QStringList;
/**
......@@ -154,6 +155,17 @@ public:
* @return the error message GnuPG gave out (if any)
*/
static QString getGpgStartupError(const QString &binary);
/**
* @brief run GnuPG and let it return it's config output
* @param binary the GnuPG binary to run
* @param key if only fields of a given type should be returned
* @return all matching fields
*
* In case a key is given the key is already removed from the
* returned lines.
*/
static QStringList getGgpParsedConfig(const QString &binary, const QByteArray &key = QByteArray());
signals:
/**
* Emitted when the process is ready for reading.
......
/* Copyright 2008,2009,2010,2011,2012,2013 Rolf Eike Beer <kde@opensource.sf-tec.de>
/* Copyright 2008,2009,2010,2011,2012,2013,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
......@@ -18,6 +18,7 @@
*/
#include "kgpgitemmodel.h"
#include "gpgproc.h"
#include "kgpgsettings.h"
#include "core/convert.h"
#include "core/images.h"
......@@ -503,7 +504,7 @@ KGpgItemModel::refreshGroups()
endRemoveRows();
}
const QStringList groups = KGpgGroupNode::readGroups();
const QStringList groups = GPGProc::getGgpParsedConfig(KGpgSettings::gpgBinaryPath(), "group");
if (groups.isEmpty())
return;
......
Supports Markdown
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