• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
  • selftest
gpgconfcheck.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  selftest/gpgconfcheck.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2008 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "gpgconfcheck.h"
36 
37 #include "implementation_p.h"
38 
39 #include <utils/gnupg-helper.h>
40 #include <utils/hex.h>
41 
42 #include <KDebug>
43 #include <KLocalizedString>
44 
45 #include <QProcess>
46 #include <QDir>
47 
48 #ifndef Q_MOC_RUN
49 #include <boost/shared_ptr.hpp>
50 #endif
51 
52 #include <cassert>
53 
54 using namespace Kleo;
55 using namespace Kleo::_detail;
56 using namespace boost;
57 
58 namespace {
59 
60  class GpgConfCheck : public SelfTestImplementation {
61  QString m_component;
62  public:
63  explicit GpgConfCheck( const char * component )
64  : SelfTestImplementation( i18nc("@title", "%1 Configuration Check", component && *component ? QLatin1String(component) : QLatin1String("gpgconf") ) ),
65  m_component( QLatin1String(component) )
66  {
67  runTest();
68  }
69 
70  QStringList arguments() const {
71  if ( m_component.isEmpty() )
72  return QStringList() << QLatin1String("--check-config");
73  else
74  return QStringList() << QLatin1String("--check-options") << m_component ;
75  }
76 
77  bool canRun() {
78  if ( !ensureEngineVersion( GpgME::GpgConfEngine, 2, 0, 10 ) )
79  return false;
80 
81  if ( !m_component.isEmpty() )
82  return true;
83 
84  QProcess gpgconf;
85  gpgconf.setReadChannel( QProcess::StandardOutput );
86  gpgconf.start( gpgConfPath(), QStringList() << QLatin1String("--list-dirs"), QIODevice::ReadOnly );
87  gpgconf.waitForFinished();
88  if ( gpgconf.exitStatus() != QProcess::NormalExit || gpgconf.exitCode() != 0 ) {
89  kDebug() << "GpgConfCheck: \"gpgconf --list-dirs\" gives error, disabling";
90  return false;
91  }
92  const QList<QByteArray> lines = gpgconf.readAll().split( '\n' );
93  Q_FOREACH( const QByteArray & line, lines )
94  if ( line.startsWith( "sysconfdir:" ) ) //krazy:exclude=strings
95  try {
96  return QDir( QFile::decodeName( hexdecode( line.mid( strlen( "sysconfdir:" ) ) ) ) ).exists( QLatin1String("gpgconf.conf") );
97  } catch ( ... ) { return false; }
98  kDebug() << "GpgConfCheck: \"gpgconf --list-dirs\" has no sysconfdir entry";
99  return false;
100  }
101 
102  void runTest() {
103 
104  if ( !canRun() ) {
105  if ( !m_skipped )
106  m_passed = true;
107  return;
108  }
109 
110  QProcess process;
111  process.setProcessChannelMode( QProcess::MergedChannels );
112 
113  process.start( gpgConfPath(), arguments(), QIODevice::ReadOnly );
114 
115  process.waitForFinished();
116 
117  const QString output = QString::fromUtf8( process.readAll() );
118  const QString message = process.exitStatus() == QProcess::CrashExit ? i18n( "The process terminated prematurely" ) : process.errorString() ;
119 
120  if ( process.exitStatus() != QProcess::NormalExit ||
121  process.error() != QProcess::UnknownError ) {
122  m_passed = false;
123  m_error = i18nc("self-test did not pass", "Failed");
124  m_explaination =
125  i18n( "There was an error executing the GnuPG configuration self-check for %2:\n"
126  " %1\n"
127  "You might want to execute \"gpgconf %3\" on the command line.\n",
128  message, m_component.isEmpty() ? QLatin1String("GnuPG") : m_component, arguments().join(QLatin1String(" ") ) );
129  if ( !output.trimmed().isEmpty() )
130  m_explaination += QLatin1Char('\n') + i18n("Diagnostics:") + QLatin1Char('\n') + output ;
131 
132  m_proposedFix.clear();
133  } else if ( process.exitCode() ) {
134  m_passed = false;
135  m_error = i18nc("self-check did not pass", "Failed");
136  m_explaination = !output.trimmed().isEmpty()
137  ? i18nc("Self-test did not pass",
138  "The GnuPG configuration self-check failed.\n"
139  "\n"
140  "Error code: %1\n"
141  "Diagnostics:", process.exitCode() ) + QLatin1Char('\n') + output
142  : i18nc("self-check did not pass",
143  "The GnuPG configuration self-check failed with error code %1.\n"
144  "No output was received.", process.exitCode() );
145  m_proposedFix.clear();
146  } else {
147  m_passed = true;
148  }
149  }
150 
151  };
152 }
153 
154 shared_ptr<SelfTest> Kleo::makeGpgConfCheckConfigurationSelfTest( const char * component ) {
155  return shared_ptr<SelfTest>( new GpgConfCheck( component ) );
156 }
QByteArray::split
QList< QByteArray > split(char sep) const
QByteArray
QProcess::error
QProcess::ProcessError error() const
QString::clear
void clear()
boost::shared_ptr
Definition: encryptemailcontroller.h:51
QString::fromUtf8
QString fromUtf8(const char *str, int size)
QProcess
process
static QString process(const Dir &dir, bool *fatal)
Definition: createchecksumscontroller.cpp:557
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
hex.h
QIODevice::readAll
QByteArray readAll()
QString
QList
Definition: commands/command.h:46
gpgconfcheck.h
QByteArray::mid
QByteArray mid(int pos, int len) const
QStringList
gnupg-helper.h
QLatin1Char
QDir
QProcess::setProcessChannelMode
void setProcessChannelMode(ProcessChannelMode mode)
Kleo::makeGpgConfCheckConfigurationSelfTest
boost::shared_ptr< SelfTest > makeGpgConfCheckConfigurationSelfTest(const char *component=0)
Definition: gpgconfcheck.cpp:154
QLatin1String
Kleo::gpgConfPath
QString gpgConfPath()
Definition: gnupg-helper.cpp:78
implementation_p.h
QProcess::setReadChannel
void setReadChannel(ProcessChannel channel)
Kleo::_detail::SelfTestImplementation
Definition: implementation_p.h:45
QProcess::exitStatus
QProcess::ExitStatus exitStatus() const
QProcess::exitCode
int exitCode() const
QProcess::start
void start(const QString &program, const QStringList &arguments, QFlags< QIODevice::OpenModeFlag > mode)
QFile::decodeName
QString decodeName(const QByteArray &localFileName)
Kleo::hexdecode
std::string hexdecode(const char *s)
Definition: hex.cpp:117
QProcess::waitForFinished
bool waitForFinished(int msecs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal