OSDN Git Service

Make signature validation work, when keyring file is not located in the same director...
[mutilities/MUtilities.git] / src / UpdateChecker.cpp
index fecfce2..b963887 100644 (file)
@@ -53,11 +53,9 @@ static const char *update_mirrors_prim[] =
        "http://mulder.bplaced.net/",
        "http://mulder.cwsurf.de/",
        "http://mulder.6te.net/",
-       "http://mulder.webuda.com/",
-       "http://mulder.byethost13.com/",
-       "http://muldersoft.kilu.de/",
-       "http://mulder.pe.hu/",
-       "http://muldersoft.square7.ch/", //"http://muldersoft.zxq.net/",
+       "http://mulder.webuda.com/",                    //"http://mulder.byethost13.com/",
+       "http://mulder.pe.hu/",                                 //"http://muldersoft.kilu.de/",
+       "http://muldersoft.square7.ch/",                //"http://muldersoft.zxq.net/",
        "http://muldersoft.co.nf/",
        "http://muldersoft.eu.pn/",
        "http://lamexp.sourceforge.net/",
@@ -78,14 +76,17 @@ static const char *known_hosts[] =          //Taken form: http://www.alexa.com/topsites
        "http://www.163.com/",
        "http://www.7-zip.org/",
        "http://www.ac3filter.net/",
-       "http://www.amazon.com/",
+       "http://clbianco.altervista.org/",
+       "http://status.aws.amazon.com/",
        "http://antergos.com/",
        "http://www.aol.com/",
        "http://www.apache.org/",
        "http://www.apple.com/",
        "http://www.adobe.com/",
+       "http://archive.org/web/",
+       "http://www.artlebedev.ru/",
        "http://web.audacityteam.org/",
-       "http://automattic.com/",
+       "http://status.automattic.com/",
        "http://www.avidemux.org/",
        "http://www.babylon.com/",
        "http://www.baidu.com/",
@@ -93,21 +94,27 @@ static const char *known_hosts[] =          //Taken form: http://www.alexa.com/topsites
        "http://www.bbc.co.uk/",
        "http://www.berlios.de/",
        "http://www.bing.com/",
+       "http://www.bingeandgrab.com/",
        "http://www.bucketheadpikes.com/",
+       "http://www.buckethead-coop.com/",
        "http://www.ccc.de/",
+       "http://www.citizeninsomniac.com/WMV/",
        "http://www.cnet.com/",
        "http://cnzz.com/",
        "http://www.codeplex.com/",
+       "http://www.codeproject.com/",
+       "http://www.der-postillon.com/",
        "http://www.ebay.com/",
        "http://www.equation.com/",
+       "http://www.farbrausch.de/",
        "http://fc2.com/",
        "http://fedoraproject.org/wiki/Fedora_Project_Wiki",
        "http://blog.fefe.de/",
        "http://www.ffmpeg.org/",
        "http://blog.flickr.net/en",
        "http://free-codecs.com/",
-       "http://blog.gitorious.org/",
        "http://git-scm.com/",
+       "http://doc.gitlab.com/",
        "http://www.gmx.net/",
        "http://news.gnome.org/",
        "http://www.gnu.org/",
@@ -120,7 +127,8 @@ static const char *known_hosts[] =          //Taken form: http://www.alexa.com/topsites
        "http://www.imdb.com/",
        "http://www.imgburn.com/",
        "http://imgur.com/",
-       "http://en.jd.com/",
+       "http://www.jd.com/contact/",
+       "http://www.jiscdigitalmedia.ac.uk/",
        "http://kannmanumdieuhrzeitschonnbierchentrinken.de/",
        "http://mirrors.kernel.org/",
        "http://komisar.gin.by/",
@@ -130,28 +138,37 @@ static const char *known_hosts[] =                //Taken form: http://www.alexa.com/topsites
        "http://www.linuxmint.com/",
        "http://www.livedoor.com/",
        "http://www.livejournal.com/",
+       "http://longplayer.org/",
        "http://go.mail.ru/",
+       "http://marknelson.us/",
        "http://www.mediafire.com/about/",
+       "http://www.mod-technologies.com/",
        "http://ftp.mozilla.org/",
        "http://mplayerhq.hu/",
        "http://www.msn.com/en-us/",
        "http://wiki.multimedia.cx/",
        "http://www.nch.com.au/",
        "http://oss.netfarm.it/",
+       "http://netrenderer.de/",
        "http://www.nytimes.com/",
        "http://www.opera.com/",
        "http://www.partha.com/",
+       "http://pastebin.com/",
        "http://pastie.org/",
+       "http://portableapps.com/about",
        "http://www.portablefreeware.com/",
        "http://www.qt.io/",
        "http://www.quakelive.com/",
+       "http://rationalqm.us/mine.html",
        "http://www.seamonkey-project.org/",
+       "http://selfhtml.org/",
        "http://www.shspvr.com/",
        "http://www.sina.com.cn/",
        "http://www.sohu.com/",
        "http://www.sogou.com/",
        "http://sourceforge.net/",
        "http://www.spiegel.de/",
+       "http://www.sputnikmusic.com/",
        "http://stackoverflow.com/",
        "http://tdm-gcc.tdragon.net/",
        "http://www.tdrsmusic.com/",
@@ -161,15 +178,17 @@ static const char *known_hosts[] =                //Taken form: http://www.alexa.com/topsites
        "http://www.videolan.org/",
        "http://virtualdub.org/",
        "http://blog.virustotal.com/",
-       "http://www.warr.org/buckethead.html",
+       "http://www.vkgoeswild.com/",
+       "http://www.warr.org/WAhere.html",
        "http://www.weibo.com/login.php",
-       "http://www.wikipedia.org/",
+       "http://status.wikimedia.org/",
        "http://www.winamp.com/",
+       "http://www.winhoros.de/",
        "http://wpde.org/",
+       "http://x265.org/",
        "http://xhmikosr.1f0.de/",
        "http://xiph.org/",
        "http://us.mail.yahoo.com/",
-       "http://www.yandex.ru/",
        "http://www.youtube.com/yt/about/",
        "http://www.zedo.com/",
        "http://ffmpeg.zeranoe.com/",
@@ -206,6 +225,20 @@ void UpdateCheckerInfo::resetInfo(void)
        m_downloadAddress.clear();
        m_downloadFilename.clear();
        m_downloadFilecode.clear();
+       m_downloadChecksum.clear();
+}
+
+bool UpdateCheckerInfo::isComplete(void)
+{
+       if(this->m_buildNo < 1)                return false;
+       if(this->m_buildDate.year() < 2010)    return false;
+       if(this->m_downloadSite.isEmpty())     return false;
+       if(this->m_downloadAddress.isEmpty())  return false;
+       if(this->m_downloadFilename.isEmpty()) return false;
+       if(this->m_downloadFilecode.isEmpty()) return false;
+       if(this->m_downloadChecksum.isEmpty()) return false;
+
+       return true;
 }
 
 ////////////////////////////////////////////////////////////
@@ -236,7 +269,6 @@ UpdateChecker::UpdateChecker(const QString &binWGet, const QString &binGnuPG, co
 
 UpdateChecker::~UpdateChecker(void)
 {
-       delete m_updateInfo;
 }
 
 ////////////////////////////////////////////////////////////
@@ -339,7 +371,7 @@ void UpdateChecker::checkForUpdates(void)
                setProgress(m_progress + 1);
                if(!m_success)
                {
-                       if(tryUpdateMirror(m_updateInfo, currentMirror))
+                       if(tryUpdateMirror(m_updateInfo.data(), currentMirror))
                        {
                                m_success = true;
                        }
@@ -503,13 +535,13 @@ bool UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString
 bool UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
 {
        log("", "Downloading update info:");
-       if(!getFile(QString("%1%2"    ).arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileVers))
+       if(!getFile(QString("%1%2"     ).arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileVers))
        {
                return false;
        }
 
        log("", "Downloading signature:");
-       if(!getFile(QString("%1%2.sig").arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileSign))
+       if(!getFile(QString("%1%2.sig2").arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileSign))
        {
                return false;
        }
@@ -517,7 +549,19 @@ bool UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers
        return true;
 }
 
-bool UpdateChecker::getFile(const QString &url, const QString &outFile, unsigned int maxRedir, bool *httpOk)
+bool UpdateChecker::getFile(const QString &url, const QString &outFile, const unsigned int maxRedir, bool *httpOk)
+{
+       for(int i = 0; i < 2; i++)
+       {
+               if(getFile(url, (i > 0), outFile, maxRedir, httpOk))
+               {
+                       return true;
+               }
+       }
+       return false;
+}
+
+bool UpdateChecker::getFile(const QString &url, const bool forceIp4, const QString &outFile, const unsigned int maxRedir, bool *httpOk)
 {
        QFileInfo output(outFile);
        output.setCaching(false);
@@ -536,6 +580,11 @@ bool UpdateChecker::getFile(const QString &url, const QString &outFile, unsigned
        init_process(process, output.absolutePath());
 
        QStringList args;
+       if(forceIp4)
+       {
+               args << "-4";
+       }
+
        args << "-T" << "15" << "--no-cache" << "--no-dns-cache" << QString().sprintf("--max-redirect=%u", maxRedir);
        args << QString("--referer=%1://%2/").arg(QUrl(url).scheme(), QUrl(url).host()) << "-U" << USER_AGENT_STR;
        args << "-O" << output.fileName() << url;
@@ -547,7 +596,6 @@ bool UpdateChecker::getFile(const QString &url, const QString &outFile, unsigned
 
        QTimer timer;
        timer.setSingleShot(true);
-       timer.setInterval(25000);
        connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
 
        const QRegExp httpResponseOK("200 OK$");
@@ -559,7 +607,7 @@ bool UpdateChecker::getFile(const QString &url, const QString &outFile, unsigned
                return false;
        }
 
-       timer.start();
+       timer.start(25000);
 
        while(process.state() != QProcess::NotRunning)
        {
@@ -600,10 +648,17 @@ bool UpdateChecker::checkSignature(const QString &file, const QString &signature
                return false;
        }
 
+       QString keyRingPath(m_binaryKeys);
+       bool removeKeyring = false;
        if(QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
        {
-               qWarning("CheckSignature: File and keyring should be in same folder!");
-               return false;
+               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;
+               }
        }
 
        QProcess process;
@@ -614,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(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(removeKeyring)
+               {
+                       remove_file(keyRingPath);
+               }
                return false;
        }
 
@@ -630,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()));
        return (process.exitCode() == 0);
 }
@@ -668,12 +732,12 @@ bool UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *upd
                        if(value.cap(1).compare("BuildNo", Qt::CaseInsensitive) == 0)
                        {
                                bool ok = false;
-                               unsigned int temp = value.cap(2).toUInt(&ok);
+                               const unsigned int temp = value.cap(2).toUInt(&ok);
                                if(ok) updateInfo->m_buildNo = temp;
                        }
                        else if(value.cap(1).compare("BuildDate", Qt::CaseInsensitive) == 0)
                        {
-                               QDate temp = QDate::fromString(value.cap(2).trimmed(), Qt::ISODate);
+                               const QDate temp = QDate::fromString(value.cap(2).trimmed(), Qt::ISODate);
                                if(temp.isValid()) updateInfo->m_buildDate = temp;
                        }
                        else if(value.cap(1).compare("DownloadSite", Qt::CaseInsensitive) == 0)
@@ -692,6 +756,10 @@ bool UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *upd
                        {
                                updateInfo->m_downloadFilecode = value.cap(2).trimmed();
                        }
+                       else if(value.cap(1).compare("DownloadChecksum", Qt::CaseInsensitive) == 0)
+                       {
+                               updateInfo->m_downloadChecksum = value.cap(2).trimmed();
+                       }
                }
                if(inHdr && (value.indexIn(line) >= 0))
                {
@@ -723,22 +791,14 @@ bool UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *upd
                log("Version info is from the future, take care!");
                qWarning("Version info is from the future, take care!");
        }
-
-       bool complete = true;
-
-       if(!(updateInfo->m_buildNo > 0)) complete = false;
-       if(!(updateInfo->m_buildDate.year() >= 2010)) complete = false;
-       if(updateInfo->m_downloadSite.isEmpty()) complete = false;
-       if(updateInfo->m_downloadAddress.isEmpty()) complete = false;
-       if(updateInfo->m_downloadFilename.isEmpty()) complete = false;
-       if(updateInfo->m_downloadFilecode.isEmpty()) complete = false;
        
-       if(!complete)
+       if(!updateInfo->isComplete())
        {
                log("WARNING: Version info is incomplete!");
+               return false;
        }
 
-       return complete;
+       return true;
 }
 
 ////////////////////////////////////////////////////////////