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

kgpg

  • sources
  • kde-4.12
  • kdeutils
  • kgpg
kgpginterface.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002 Jean-Baptiste Mardelle <bj@altern.org>
3  * Copyright (C) 2007,2008,2009,2010,2011,2012,2013,2014
4  * Rolf Eike Beer <kde@opensource.sf-tec.de>
5  */
6 /***************************************************************************
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  ***************************************************************************/
14 
15 #include "kgpginterface.h"
16 
17 #include "gpgproc.h"
18 #include "core/convert.h"
19 #include "core/KGpgKeyNode.h"
20 #include "core/KGpgSignNode.h"
21 #include "core/KGpgSubkeyNode.h"
22 #include "core/KGpgUatNode.h"
23 #include "core/KGpgUidNode.h"
24 
25 #include <KConfig>
26 #include <KDebug>
27 #include <KGlobal>
28 #include <KLocale>
29 #include <KMessageBox>
30 #include <KProcess>
31 #include <QFile>
32 #include <QString>
33 #include <QTextStream>
34 
35 using namespace KgpgCore;
36 
37 QString KgpgInterface::getGpgSetting(const QString &name, const QString &configfile)
38 {
39  const QString tmp(name.simplified() + QLatin1Char( ' ' ));
40  QFile qfile(configfile);
41 
42  if (qfile.open(QIODevice::ReadOnly) && (qfile.exists())) {
43  QTextStream t(&qfile);
44  while (!t.atEnd()) {
45  QString result(t.readLine().simplified());
46  if (result.startsWith(tmp)) {
47  result = result.mid(tmp.length()).simplified();
48  return result.section(QLatin1Char( ' ' ), 0, 0);
49  }
50  }
51  qfile.close();
52  }
53 
54  return QString();
55 }
56 
57 void KgpgInterface::setGpgSetting(const QString &name, const QString &value, const QString &url)
58 {
59  QFile qfile(url);
60 
61  if (qfile.open(QIODevice::ReadOnly) && (qfile.exists())) {
62  const QString temp(name + QLatin1Char( ' ' ));
63  QString texttowrite;
64  bool found = false;
65  QTextStream t(&qfile);
66 
67  while (!t.atEnd()) {
68  QString result = t.readLine();
69  if (result.simplified().startsWith(temp)) {
70  if (!value.isEmpty())
71  result = temp + QLatin1Char( ' ' ) + value;
72  else
73  result.clear();
74  found = true;
75  }
76 
77  texttowrite += result + QLatin1Char( '\n' );
78  }
79 
80  qfile.close();
81  if ((!found) && (!value.isEmpty()))
82  texttowrite += QLatin1Char( '\n' ) + temp + QLatin1Char( ' ' ) + value;
83 
84  if (qfile.open(QIODevice::WriteOnly)) {
85  QTextStream t(&qfile);
86  t << texttowrite;
87  qfile.close();
88  }
89  }
90 }
91 
92 bool KgpgInterface::getGpgBoolSetting(const QString &name, const QString &configfile)
93 {
94  QFile qfile(configfile);
95  if (qfile.open(QIODevice::ReadOnly) && (qfile.exists())) {
96  QTextStream t(&qfile);
97  while (!t.atEnd()) {
98  if (t.readLine().simplified().startsWith(name))
99  return true;
100  }
101  qfile.close();
102  }
103  return false;
104 }
105 
106 void KgpgInterface::setGpgBoolSetting(const QString &name, const bool enable, const QString &url)
107 {
108  QFile qfile(url);
109 
110  if (qfile.open(QIODevice::ReadOnly) && (qfile.exists())) {
111  QString texttowrite;
112  bool found = false;
113  QTextStream t(&qfile);
114 
115  while (!t.atEnd()) {
116  QString result(t.readLine());
117 
118  if (result.simplified().startsWith(name)) {
119  if (enable)
120  result = name;
121  else
122  result.clear();
123 
124  found = true;
125  }
126 
127  texttowrite += result + QLatin1Char( '\n' );
128  }
129  qfile.close();
130 
131  if ((!found) && (enable))
132  texttowrite += name;
133 
134  if (qfile.open(QIODevice::WriteOnly)) {
135  QTextStream t(&qfile);
136  t << texttowrite;
137  qfile.close();
138  }
139  }
140 }
141 
146 static KgpgCore::KgpgKeyList
147 readPublicKeysProcess(GPGProc &p, KGpgKeyNode *readNode)
148 {
149  QStringList lsp;
150  int items;
151  KgpgCore::KgpgKeyList publiclistkeys;
152  KgpgCore::KgpgKey *publickey = NULL;
153  unsigned int idIndex = 0;
154  QString log;
155  KGpgSignableNode *currentSNode = NULL;
156 
157  while ((items = p.readln(lsp)) >= 0) {
158  if ((lsp.at(0) == QLatin1String( "pub" )) && (items >= 10)) {
159  KgpgSubKeyType subtype;
160  KgpgSubKeyType keytype;
161  bool enabled = true;
162  if (items > 11) {
163  const QString &caps = lsp.at(11);
164 
165  enabled = !caps.contains(QLatin1Char('D'), Qt::CaseSensitive);
166 
167  subtype = Convert::toSubType(caps, false);
168  keytype = Convert::toSubType(caps, true);
169  }
170 
171  publiclistkeys << KgpgKey(lsp.at(4), lsp.at(2).toUInt(), Convert::toTrust(lsp.at(1)),
172  Convert::toAlgo(lsp.at(3).toInt()), subtype, keytype,
173  QDateTime::fromTime_t(lsp.at(5).toUInt()));
174 
175  publickey = &publiclistkeys.last();
176 
177  publickey->setOwnerTrust(Convert::toOwnerTrust(lsp.at(8)));
178 
179  if (lsp.at(6).isEmpty())
180  publickey->setExpiration(QDateTime());
181  else
182  publickey->setExpiration(QDateTime::fromTime_t(lsp.at(6).toUInt()));
183 
184  publickey->setValid(enabled); // disabled key
185 
186  idIndex = 0;
187  } else if (publickey && (lsp.at(0) == QLatin1String( "fpr" )) && (items >= 10)) {
188  const QString fingervalue(lsp.at(9));
189 
190  publickey->setFingerprint(fingervalue);
191  } else if (publickey && (lsp.at(0) == QLatin1String( "sub" )) && (items >= 7)) {
192  KgpgSubKeyType subtype;
193 
194  if (items > 11)
195  subtype = Convert::toSubType(lsp.at(11), false);
196 
197  KgpgKeySub sub(lsp.at(4), lsp.at(2).toUInt(), Convert::toTrust(lsp.at(1)),
198  Convert::toAlgo(lsp.at(3).toInt()), subtype, QDateTime::fromTime_t(lsp.at(5).toUInt()));
199 
200  // FIXME: Please see kgpgkey.h, KgpgSubKey class
201  if (items <= 11)
202  sub.setValid(true);
203  else
204  sub.setValid(!lsp.at(11).contains(QLatin1Char( 'D' )));
205 
206  if (lsp.at(6).isEmpty())
207  sub.setExpiration(QDateTime());
208  else
209  sub.setExpiration(QDateTime::fromTime_t(lsp.at(6).toUInt()));
210 
211  publickey->subList()->append(sub);
212  if (readNode == NULL)
213  currentSNode = NULL;
214  else
215  currentSNode = new KGpgSubkeyNode(readNode, sub);
216  } else if (publickey && (lsp.at(0) == QLatin1String( "uat" ))) {
217  idIndex++;
218  if (readNode != NULL) {
219  currentSNode = new KGpgUatNode(readNode, idIndex, lsp);
220  }
221  } else if (publickey && (lsp.at(0) == QLatin1String( "uid" )) && (items >= 10)) {
222  if (idIndex == 0) {
223  QString fullname(lsp.at(9));
224  QString kmail;
225  if (fullname.contains(QLatin1Char( '<' )) ) {
226  kmail = fullname;
227 
228  if (fullname.contains(QLatin1Char( ')' )) )
229  kmail = kmail.section(QLatin1Char( ')' ), 1);
230 
231  kmail = kmail.section(QLatin1Char( '<' ), 1);
232  kmail.truncate(kmail.length() - 1);
233 
234  if (kmail.contains(QLatin1Char( '<' ))) {
235  // several email addresses in the same key
236  kmail = kmail.replace(QLatin1Char( '>' ), QLatin1Char( ';' ));
237  kmail.remove(QLatin1Char( '<' ));
238  }
239  }
240 
241  QString kname(fullname.section( QLatin1String( " <" ), 0, 0));
242  QString comment;
243  if (fullname.contains(QLatin1Char( '(' )) ) {
244  kname = kname.section( QLatin1String( " (" ), 0, 0);
245  comment = fullname.section(QLatin1Char( '(' ), 1, 1);
246  comment = comment.section(QLatin1Char( ')' ), 0, 0);
247  }
248 
249  idIndex++;
250  publickey->setEmail(kmail);
251  publickey->setComment(comment);
252  publickey->setName(kname);
253 
254  currentSNode = readNode;
255  } else {
256  idIndex++;
257  if (readNode != NULL) {
258  currentSNode = new KGpgUidNode(readNode, idIndex, lsp);
259  }
260  }
261  } else if (publickey && ((lsp.at(0) == QLatin1String( "sig" )) || (lsp.at(0) == QLatin1String( "rev" ))) && (items >= 11)) {
262  // there are no strings here that could have a recoded QLatin1Char( ':' ) in them
263  const QString signature = lsp.join(QLatin1String(":"));
264 
265  if (currentSNode != NULL)
266  (void) new KGpgSignNode(currentSNode, lsp);
267  } else {
268  log += lsp.join(QString(QLatin1Char( ':' ))) + QLatin1Char( '\n' );
269  }
270  }
271 
272  if (p.exitCode() != 0) {
273  KMessageBox::detailedError(NULL, i18n("An error occurred while scanning your keyring"), log);
274  log.clear();
275  }
276 
277  return publiclistkeys;
278 }
279 
280 KgpgKeyList KgpgInterface::readPublicKeys(const QStringList &ids)
281 {
282  GPGProc process;
283  process <<
284  QLatin1String("--with-colons") <<
285  QLatin1String("--with-fingerprint") <<
286  QLatin1String("--fixed-list-mode") <<
287  QLatin1String("--list-keys") <<
288  ids;
289 
290  process.setOutputChannelMode(KProcess::MergedChannels);
291 
292  process.start();
293  process.waitForFinished(-1);
294  return readPublicKeysProcess(process, NULL);
295 }
296 
297 void KgpgInterface::readSignatures(KGpgKeyNode *node)
298 {
299  GPGProc process;
300  process <<
301  QLatin1String("--with-colons") <<
302  QLatin1String("--with-fingerprint") <<
303  QLatin1String("--fixed-list-mode") <<
304  QLatin1String("--list-sigs") <<
305  node->getId();
306 
307  process.setOutputChannelMode(KProcess::MergedChannels);
308 
309  process.start();
310  process.waitForFinished(-1);
311 
312  readPublicKeysProcess(process, node);
313 }
314 
315 static KgpgCore::KgpgKeyList
316 readSecretKeysProcess(GPGProc &p)
317 {
318  QStringList lsp;
319  int items;
320  bool hasuid = false;
321  KgpgCore::KgpgKeyList result;
322  KgpgCore::KgpgKey *secretkey = NULL;
323 
324  while ( (items = p.readln(lsp)) >= 0 ) {
325  if ((lsp.at(0) == QLatin1String( "sec" )) && (items >= 10)) {
326  KgpgSubKeyType subtype;
327  KgpgSubKeyType keytype;
328 
329  if (items >= 11) {
330  const QString &caps = lsp.at(11);
331 
332  subtype = Convert::toSubType(caps, false);
333  keytype = Convert::toSubType(caps, true);
334  }
335 
336  result << KgpgKey(lsp.at(4), lsp.at(2).toUInt(), Convert::toTrust(lsp.at(1)),
337  Convert::toAlgo(lsp.at(3).toInt()), subtype, keytype,
338  QDateTime::fromTime_t(lsp.at(5).toUInt()));
339 
340  secretkey = &result.last();
341 
342  secretkey->setSecret(true);
343 
344  if (lsp.at(6).isEmpty())
345  secretkey->setExpiration(QDateTime());
346  else
347  secretkey->setExpiration(QDateTime::fromTime_t(lsp.at(6).toUInt()));
348  hasuid = true;
349  } else if ((lsp.at(0) == QLatin1String( "uid" )) && (items >= 10)) {
350  if (hasuid)
351  continue;
352 
353  hasuid = true;
354 
355  const QString fullname(lsp.at(9));
356  if (fullname.contains(QLatin1Char( '<' ) )) {
357  QString kmail(fullname);
358 
359  if (fullname.contains(QLatin1Char( ')' ) ))
360  kmail = kmail.section(QLatin1Char( ')' ), 1);
361 
362  kmail = kmail.section(QLatin1Char( '<' ), 1);
363  kmail.truncate(kmail.length() - 1);
364 
365  if (kmail.contains(QLatin1Char( '<' ) )) { // several email addresses in the same key
366  kmail = kmail.replace(QLatin1Char( '>' ), QLatin1Char( ';' ));
367  kmail.remove(QLatin1Char( '<' ));
368  }
369 
370  secretkey->setEmail(kmail);
371  } else {
372  secretkey->setEmail(QString());
373  }
374 
375  QString kname(fullname.section( QLatin1String( " <" ), 0, 0));
376  if (fullname.contains(QLatin1Char( '(' ) )) {
377  kname = kname.section( QLatin1String( " (" ), 0, 0);
378  QString comment = fullname.section(QLatin1Char( '(' ), 1, 1);
379  comment = comment.section(QLatin1Char( ')' ), 0, 0);
380 
381  secretkey->setComment(comment);
382  } else {
383  secretkey->setComment(QString());
384  }
385  secretkey->setName(kname);
386  } else if ((lsp.at(0) == QLatin1String( "fpr" )) && (items >= 10)) {
387  secretkey->setFingerprint(lsp.at(9));
388  }
389  }
390 
391  return result;
392 }
393 
394 KgpgKeyList KgpgInterface::readSecretKeys(const QStringList &ids)
395 {
396  GPGProc process;
397  process <<
398  QLatin1String("--with-colons") <<
399  QLatin1String("--list-secret-keys") <<
400  QLatin1String("--with-fingerprint") <<
401  QLatin1String("--fixed-list-mode") <<
402  ids;
403 
404  process.start();
405  process.waitForFinished(-1);
406  KgpgCore::KgpgKeyList result = readSecretKeysProcess(process);
407 
408  return result;
409 }
410 
411 #include "kgpginterface.moc"
KGpgUidNode.h
KGpgUatNode.h
KgpgCore::KgpgKey::setName
void setName(const QString &name)
Definition: kgpgkey.cpp:193
GPGProc
A interface to GnuPG handling UTF8 recoding correctly.
Definition: gpgproc.h:36
KgpgCore::KgpgKey::setFingerprint
void setFingerprint(const QString &fingerprint)
Definition: kgpgkey.cpp:208
KgpgInterface::setGpgBoolSetting
void setGpgBoolSetting(const QString &name, const bool enable, const QString &url)
Definition: kgpginterface.cpp:106
GPGProc::readln
int readln(QString &line, const bool colons=false)
Reads a line of text (excluding '\n').
Definition: gpgproc.cpp:203
KGpgSignNode.h
KGpgKeyNode::getId
virtual QString getId() const
Definition: KGpgKeyNode.cpp:113
KgpgInterface::getGpgBoolSetting
bool getGpgBoolSetting(const QString &name, const QString &configfile)
Definition: kgpginterface.cpp:92
KGpgUatNode
A user attribute (i.e.
Definition: KGpgUatNode.h:36
KgpgCore::Convert::toTrust
KgpgKeyTrust toTrust(const QChar &c)
Definition: convert.cpp:123
KgpgCore::KgpgKey::setEmail
void setEmail(const QString &email)
Definition: kgpgkey.cpp:198
KgpgCore::KgpgKey::setValid
void setValid(const bool valid)
Definition: kgpgkey.cpp:188
KgpgCore::KgpgKey
Definition: kgpgkey.h:254
KgpgInterface::setGpgSetting
void setGpgSetting(const QString &name, const QString &value, const QString &url)
Definition: kgpginterface.cpp:57
KgpgCore::KgpgKey::setComment
void setComment(const QString &comment)
Definition: kgpgkey.cpp:203
KGpgKeyNode
A public key with or without corresponding secret key.
Definition: KGpgKeyNode.h:33
KgpgCore::Images::signature
QPixmap signature()
Definition: images.cpp:64
KgpgCore::KgpgKey::setExpiration
void setExpiration(const QDateTime &date)
Definition: kgpgkey.cpp:218
GPGProc::start
void start()
Starts the process.
Definition: gpgproc.cpp:185
KgpgCore::KgpgKeySub
Definition: kgpgkey.h:150
KgpgCore::Convert::toOwnerTrust
KgpgKeyOwnerTrust toOwnerTrust(const QChar &c)
Definition: convert.cpp:146
KgpgInterface::readSignatures
void readSignatures(KGpgKeyNode *node)
Definition: kgpginterface.cpp:297
KgpgInterface::readPublicKeys
KgpgCore::KgpgKeyList readPublicKeys(const QStringList &ids=QStringList())
Definition: kgpginterface.cpp:280
gpgproc.h
KgpgCore::Convert::toSubType
KgpgSubKeyType toSubType(const QString &capString, bool upper)
parse the GnuPG capabilities field
Definition: convert.cpp:163
kgpginterface.h
KGpgSubkeyNode
a subkey of a public key or key pair
Definition: KGpgSubkeyNode.h:31
KgpgCore::KgpgKeyList
Definition: kgpgkey.h:312
KgpgInterface::readSecretKeys
KgpgCore::KgpgKeyList readSecretKeys(const QStringList &ids=QStringList())
Definition: kgpginterface.cpp:394
KGpgKeyNode.h
KGpgSubkeyNode.h
KgpgCore::KgpgKey::setOwnerTrust
void setOwnerTrust(const KgpgKeyOwnerTrust &owtrust)
Definition: kgpgkey.cpp:213
KGpgUidNode
A user id of a public key or key pair.
Definition: KGpgUidNode.h:33
KGpgSignNode
A signature to another key object.
Definition: KGpgSignNode.h:31
readSecretKeysProcess
static KgpgCore::KgpgKeyList readSecretKeysProcess(GPGProc &p)
Definition: kgpginterface.cpp:316
readPublicKeysProcess
static KgpgCore::KgpgKeyList readPublicKeysProcess(GPGProc &p, KGpgKeyNode *readNode)
Definition: kgpginterface.cpp:147
convert.h
KgpgCore::KgpgKey::setSecret
void setSecret(const bool secret)
Definition: kgpgkey.cpp:183
KgpgCore::Convert::toAlgo
KgpgKeyAlgo toAlgo(const uint v)
Definition: convert.cpp:104
KGpgSignableNode
An object that may have KGpgSignNode children.
Definition: KGpgSignableNode.h:31
KgpgInterface::getGpgSetting
QString getGpgSetting(const QString &name, const QString &configfile)
Definition: kgpginterface.cpp:37
KgpgCore::KgpgKey::subList
KgpgKeySubListPtr subList() const
Definition: kgpgkey.cpp:342
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgpg

Skip menu "kgpg"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

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