24 #include <QtCore/QFile>
25 #include <QtCore/QFileInfo>
26 #include <QtCore/QPointer>
27 #include <QtCore/QStringList>
28 #include <QtCore/QTextIStream>
29 #include <QtCore/QTimer>
58 QTimer::singleShot(5,
this, SLOT(
readKeys()));
65 <<
"--no-secmem-warning"
69 connect(m_process, SIGNAL(finished(
int,QProcess::ExitStatus)),
70 this, SLOT(slotFinished(
int,QProcess::ExitStatus)));
71 connect(m_process, SIGNAL(readyReadStandardOutput()),
72 this, SLOT(slotReadyReadStandardOutput()));
74 if (!m_process->waitForStarted()) {
75 KMessageBox::error(0L,
i18n(
"<qt>Cannot start <i>gpg</i> and retrieve the available keys. Make sure that <i>gpg</i> is installed, otherwise verification of downloaded resources will not be possible.</qt>"));
88 m_runMode = ListSecret;
91 <<
"--no-secmem-warning"
94 <<
"--list-secret-keys";
95 connect(m_process, SIGNAL(finished(
int,QProcess::ExitStatus)),
96 this, SLOT(slotFinished(
int,QProcess::ExitStatus)));
97 connect(m_process, SIGNAL(readyReadStandardOutput()),
98 this, SLOT(slotReadyReadStandardOutput()));
100 if (!m_process->waitForStarted()) {
107 void Security::slotFinished(
int exitCode, QProcess::ExitStatus exitStatus)
109 if (exitStatus != QProcess::NormalExit) {
110 m_gpgRunning =
false;
125 m_gpgRunning =
false;
132 void Security::slotReadyReadStandardOutput()
135 while (m_process->canReadLine()) {
136 data = QString::fromLocal8Bit(m_process->readLine());
140 if (data.startsWith(QLatin1String(
"pub")) || data.startsWith(QLatin1String(
"sec"))) {
142 if (data.startsWith(QLatin1String(
"pub")))
146 QStringList line = data.split(
':', QString::KeepEmptyParts);
151 if (trustStr ==
"u" || trustStr ==
"f")
154 key.
mail = data.section(
'<', -1, -1);
155 key.
mail.truncate(key.
mail.length() - 1);
156 key.
name = data.section(
'<', 0, 0);
157 if (key.
name.contains(
"("))
158 key.
name = key.
name.section(
'(', 0, 0);
159 m_keys[shortId] = key;
163 data = data.section(
']', 1, -1).trimmed();
164 if (data.startsWith(QLatin1String(
"GOODSIG"))) {
167 QString id = data.section(
' ', 1 , 1).right(8);
168 if (!m_keys.contains(
id)) {
171 m_signatureKey = m_keys[id];
174 if (data.startsWith(QLatin1String(
"NO_PUBKEY"))) {
178 if (data.startsWith(QLatin1String(
"BADSIG"))) {
180 QString id = data.section(
' ', 1 , 1).right(8);
181 if (!m_keys.contains(
id)) {
184 m_signatureKey = m_keys[id];
187 if (data.startsWith(QLatin1String(
"TRUST_ULTIMATE"))) {
194 if (data.contains(
"passphrase.enter")) {
197 dlg->setPrompt(
i18n(
"<qt>Enter passphrase for key <b>0x%1</b>, belonging to<br /><i>%2<%3></i><br />:</qt>", m_secretKey, key.
name, key.
mail));
199 m_process->write(dlg->password().toLocal8Bit() +
'\n');
208 if (data.contains(
"BAD_PASSPHRASE")) {
218 m_fileName = filename;
224 if (!m_keysRead || m_gpgRunning) {
228 if (m_keys.count() == 0) {
235 QFileInfo f(m_fileName);
240 QFile file(m_fileName);
241 if (file.open(QIODevice::ReadOnly)) {
247 file.setFileName(f.path() +
"/md5sum");
248 if (file.open(QIODevice::ReadOnly)) {
249 QByteArray md5sum_file;
250 file.readLine(md5sum_file.data(), 50);
251 if (!md5sum_file.isEmpty() &&
QString(md5sum_file).startsWith(md5sum))
256 m_signatureKey.
id =
"";
257 m_signatureKey.
name =
"";
258 m_signatureKey.
mail =
"";
259 m_signatureKey.
trusted =
false;
264 <<
"--no-secmem-warning"
268 << f.path() +
"/signature"
270 connect(m_process, SIGNAL(finished(
int,QProcess::ExitStatus)),
271 this, SLOT(slotFinished(
int,QProcess::ExitStatus)));
272 connect(m_process, SIGNAL(readyReadStandardOutput()),
273 this, SLOT(slotReadyReadStandardOutput()));
275 if (m_process->waitForStarted())
278 KMessageBox::error(0L,
i18n(
"<qt>Cannot start <i>gpg</i> and check the validity of the file. Make sure that <i>gpg</i> is installed, otherwise verification of downloaded resources will not be possible.</qt>"));
287 m_fileName = fileName;
293 if (!m_keysRead || m_gpgRunning) {
300 if (it.value().secret)
301 secretKeys.append(it.key());
304 if (secretKeys.count() == 0) {
310 QFileInfo f(m_fileName);
316 QFile file(m_fileName);
317 if (file.open(QIODevice::ReadOnly)) {
323 file.setFileName(f.path() +
"/md5sum");
324 if (file.open(QIODevice::WriteOnly)) {
325 QTextStream stream(&file);
331 if (secretKeys.count() > 1) {
335 m_secretKey = secretKeys[0];
341 m_secretKey = secretKeys[0];
346 <<
"--no-secmem-warning"
354 << f.path() +
"/signature"
356 connect(m_process, SIGNAL(finished(
int,QProcess::ExitStatus)),
357 this, SLOT(slotFinished(
int,QProcess::ExitStatus)));
358 connect(m_process, SIGNAL(readyReadStandardOutput()),
359 this, SLOT(slotReadyReadStandardOutput()));
362 if (m_process->waitForStarted())
365 KMessageBox::error(0L,
i18n(
"<qt>Cannot start <i>gpg</i> and sign the file. Make sure that <i>gpg</i> is installed, otherwise signing of the resources will not be possible.</qt>"));
372 #include "security.moc"