Make signature validation work, when keyring file is not located in the same directory as the file to be validated.

This commit is contained in:
LoRd_MuldeR 2015-08-31 22:56:39 +02:00
parent da2833d4a2
commit 4c8df65031
3 changed files with 55 additions and 4 deletions

View File

@ -84,6 +84,9 @@ namespace MUtils
MUTILS_API quint32 next_rand32(void); MUTILS_API quint32 next_rand32(void);
MUTILS_API quint64 next_rand64(void); MUTILS_API quint64 next_rand64(void);
//Temp File Name
MUTILS_API QString make_temp_file(const QString &basePath, const QString &extension, const bool placeholder = false);
//Parity //Parity
MUTILS_API bool parity(quint32 value); MUTILS_API bool parity(quint32 value);

View File

@ -109,6 +109,37 @@ QString MUtils::rand_str(const bool &bLong)
return QString("%1%2").arg(rand_str(false), rand_str(false)); return QString("%1%2").arg(rand_str(false), rand_str(false));
} }
///////////////////////////////////////////////////////////////////////////////
// GET TEMP FILE NAME
///////////////////////////////////////////////////////////////////////////////
QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
{
for(int i = 0; i < 4096; i++)
{
const QString tempFileName = QString("%1/%2.%3").arg(basePath, rand_str(), extension);
if(!QFileInfo(tempFileName).exists())
{
if(placeholder)
{
QFile file(tempFileName);
if(file.open(QFile::ReadWrite))
{
file.close();
return tempFileName;
}
}
else
{
return tempFileName;
}
}
}
qWarning("Failed to generate unique temp file name!");
return QString();
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// COMPUTE PARITY // COMPUTE PARITY
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -195,6 +226,7 @@ static bool temp_folder_cleanup_helper(const QString &tempPath)
static void temp_folder_cleaup(void) static void temp_folder_cleaup(void)
{ {
QWriteLocker writeLock(&g_temp_folder_lock); QWriteLocker writeLock(&g_temp_folder_lock);
qWarning("------------ temp_folder_cleaup ------------");
//Clean the directory //Clean the directory
while(!g_temp_folder_file.isNull()) while(!g_temp_folder_file.isNull())

View File

@ -648,11 +648,18 @@ bool UpdateChecker::checkSignature(const QString &file, const QString &signature
return false; return false;
} }
QString keyRingPath(m_binaryKeys);
bool removeKeyring = false;
if(QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0) if(QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
{ {
qWarning("CheckSignature: File and keyring should be in same folder!"); keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg");
removeKeyring = true;
if(!QFile::copy(m_binaryKeys, keyRingPath))
{
qWarning("CheckSignature: Failed to copy the key-ring file!");
return false; return false;
} }
}
QProcess process; QProcess process;
init_process(process, QFileInfo(file).absolutePath()); init_process(process, QFileInfo(file).absolutePath());
@ -662,10 +669,14 @@ bool UpdateChecker::checkSignature(const QString &file, const QString &signature
connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit())); connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit())); connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
process.start(m_binaryGnuPG, QStringList() << "--homedir" << "." << "--keyring" << QFileInfo(m_binaryKeys).fileName() << QFileInfo(signature).fileName() << QFileInfo(file).fileName()); process.start(m_binaryGnuPG, QStringList() << "--homedir" << "." << "--keyring" << QFileInfo(keyRingPath).fileName() << QFileInfo(signature).fileName() << QFileInfo(file).fileName());
if(!process.waitForStarted()) if(!process.waitForStarted())
{ {
if(removeKeyring)
{
remove_file(keyRingPath);
}
return false; return false;
} }
@ -678,6 +689,11 @@ bool UpdateChecker::checkSignature(const QString &file, const QString &signature
} }
} }
if(removeKeyring)
{
remove_file(keyRingPath);
}
log(QString().sprintf("Exited with code %d", process.exitCode())); log(QString().sprintf("Exited with code %d", process.exitCode()));
return (process.exitCode() == 0); return (process.exitCode() == 0);
} }