2014-11-10 06:24:23 +08:00
|
|
|
/*
|
2015-09-19 16:15:04 +08:00
|
|
|
Copyright © 2014 by The qTox Project
|
2014-11-10 06:24:23 +08:00
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-09-19 16:15:04 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-11-10 06:24:23 +08:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2015-09-19 16:15:04 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-11-10 06:24:23 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-09-19 16:15:04 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-11-10 06:24:23 +08:00
|
|
|
|
2015-09-19 16:15:04 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>
|
2014-11-10 06:24:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "widget.h"
|
|
|
|
#include "ui_widget.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QMetaObject>
|
2015-12-11 01:44:45 +08:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QSettings>
|
2014-11-10 06:24:23 +08:00
|
|
|
|
|
|
|
#include "update.h"
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
2015-12-11 01:44:45 +08:00
|
|
|
#ifdef _WIN32_WINNT
|
|
|
|
#undef _WIN32_WINNT
|
|
|
|
#endif
|
|
|
|
#define _WIN32_WINNT 0x0600 // Vista for SHGetKnownFolderPath
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shldisp.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
#include <exdisp.h>
|
|
|
|
|
2014-11-10 06:24:23 +08:00
|
|
|
const bool supported = true;
|
|
|
|
const QString QTOX_PATH = "qtox.exe";
|
|
|
|
#else
|
|
|
|
const bool supported = false;
|
|
|
|
const QString QTOX_PATH;
|
|
|
|
#endif
|
2015-12-11 01:44:45 +08:00
|
|
|
const QString SETTINGS_FILE = "settings.ini";
|
2014-11-10 06:24:23 +08:00
|
|
|
|
|
|
|
Widget::Widget(QWidget *parent) :
|
|
|
|
QWidget(parent),
|
|
|
|
ui(new Ui::Widget)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
// Updates only for supported platforms
|
|
|
|
if (!supported)
|
|
|
|
fatalError(tr("The qTox updater is not supported on this platform."));
|
|
|
|
|
2015-12-11 01:44:45 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// Get a primary unelevated token of the actual user
|
|
|
|
hPrimaryToken = nullptr;
|
|
|
|
HANDLE hShellProcess = nullptr, hShellProcessToken = nullptr;
|
|
|
|
const DWORD dwTokenRights = TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_ASSIGN_PRIMARY
|
|
|
|
| TOKEN_DUPLICATE | TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_SESSIONID;
|
|
|
|
DWORD dwPID = 0;
|
|
|
|
HWND hwnd = nullptr;
|
|
|
|
DWORD dwLastErr = 0;
|
|
|
|
|
|
|
|
// Enable SeIncreaseQuotaPrivilege
|
|
|
|
HANDLE hProcessToken = NULL;
|
|
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hProcessToken))
|
|
|
|
goto unelevateFail;
|
|
|
|
TOKEN_PRIVILEGES tkp;
|
|
|
|
tkp.PrivilegeCount = 1;
|
|
|
|
LookupPrivilegeValueW(NULL, SE_INCREASE_QUOTA_NAME, &tkp.Privileges[0].Luid);
|
|
|
|
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
|
|
|
AdjustTokenPrivileges(hProcessToken, FALSE, &tkp, 0, NULL, NULL);
|
|
|
|
dwLastErr = GetLastError();
|
|
|
|
CloseHandle(hProcessToken);
|
|
|
|
if (ERROR_SUCCESS != dwLastErr)
|
|
|
|
goto unelevateFail;
|
|
|
|
|
|
|
|
// Get a primary copy of the desktop shell's token,
|
|
|
|
// we're assuming the shell is running as the actual user
|
|
|
|
hwnd = GetShellWindow();
|
|
|
|
if (!hwnd)
|
|
|
|
goto unelevateFail;
|
|
|
|
GetWindowThreadProcessId(hwnd, &dwPID);
|
|
|
|
if (!dwPID)
|
|
|
|
goto unelevateFail;
|
|
|
|
hShellProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPID);
|
|
|
|
if (!hShellProcess)
|
|
|
|
goto unelevateFail;
|
|
|
|
if (!OpenProcessToken(hShellProcess, TOKEN_DUPLICATE, &hShellProcessToken))
|
|
|
|
goto unelevateFail;
|
|
|
|
|
|
|
|
// Duplicate the shell's process token to get a primary token.
|
|
|
|
// Based on experimentation, this is the minimal set of rights required for CreateProcessWithTokenW (contrary to current documentation).
|
|
|
|
if (!DuplicateTokenEx(hShellProcessToken, dwTokenRights, NULL, SecurityImpersonation, TokenPrimary, &hPrimaryToken))
|
|
|
|
goto unelevateFail;
|
|
|
|
|
|
|
|
qDebug() << "Unelevated primary access token acquired";
|
|
|
|
goto unelevateCleanup;
|
|
|
|
unelevateFail:
|
|
|
|
qWarning() << "Unelevate failed, couldn't get access token";
|
|
|
|
unelevateCleanup:
|
|
|
|
CloseHandle(hShellProcessToken);
|
|
|
|
CloseHandle(hShellProcess);
|
|
|
|
#endif
|
|
|
|
|
2014-11-10 06:24:23 +08:00
|
|
|
QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget::~Widget()
|
|
|
|
{
|
2015-12-11 01:44:45 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
CloseHandle(hPrimaryToken);
|
|
|
|
#endif
|
2014-11-10 06:24:23 +08:00
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setProgress(int value)
|
|
|
|
{
|
|
|
|
ui->progress->setValue(value);
|
2015-01-27 07:23:27 +08:00
|
|
|
ui->progress->repaint();
|
2014-11-10 06:24:23 +08:00
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::fatalError(QString message)
|
|
|
|
{
|
2015-12-11 01:44:45 +08:00
|
|
|
qCritical() << "Update aborted with error:"<<message;
|
2014-11-10 06:24:23 +08:00
|
|
|
QMessageBox::critical(this,tr("Error"), message+'\n'+tr("qTox will restart now."));
|
|
|
|
deleteUpdate();
|
2014-11-16 18:24:15 +08:00
|
|
|
restoreBackups();
|
2014-11-10 06:24:23 +08:00
|
|
|
startQToxAndExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::deleteUpdate()
|
|
|
|
{
|
|
|
|
QDir updateDir(getSettingsDirPath()+"/update/");
|
|
|
|
updateDir.removeRecursively();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::startQToxAndExit()
|
|
|
|
{
|
2015-12-11 01:44:45 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// Try to restart qTox as the actual user with our unelevated token
|
|
|
|
STARTUPINFOW si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
SecureZeroMemory(&si, sizeof(si));
|
|
|
|
SecureZeroMemory(&pi, sizeof(pi));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
|
|
|
|
if (!CreateProcessWithTokenW(hPrimaryToken, 0, QTOX_PATH.toStdWString().c_str(), 0, 0, 0, 0, &si, &pi))
|
|
|
|
{
|
|
|
|
qWarning() << "Failed to start unelevated qTox";
|
|
|
|
QProcess::startDetached(QTOX_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
#else
|
2014-11-10 06:24:23 +08:00
|
|
|
QProcess::startDetached(QTOX_PATH);
|
2015-12-11 01:44:45 +08:00
|
|
|
#endif
|
2014-11-10 06:24:23 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2014-11-16 18:24:15 +08:00
|
|
|
void Widget::deleteBackups()
|
|
|
|
{
|
|
|
|
for (QString file : backups)
|
|
|
|
QFile(file+".bak").remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::restoreBackups()
|
|
|
|
{
|
|
|
|
for (QString file : backups)
|
|
|
|
QFile(file+".bak").rename(file);
|
|
|
|
}
|
|
|
|
|
2015-12-11 01:44:45 +08:00
|
|
|
QString Widget::getSettingsDirPath()
|
|
|
|
{
|
|
|
|
if (isToxPortableEnabled())
|
|
|
|
return ".";
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
wchar_t* path;
|
|
|
|
SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, hPrimaryToken, &path);
|
|
|
|
QString pathStr = QString::fromStdWString(path);
|
|
|
|
pathStr.replace("\\", "/");
|
|
|
|
return pathStr + "/tox";
|
|
|
|
#else
|
|
|
|
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QDir::separator() + "tox");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isToxPortableEnabled()
|
|
|
|
{
|
|
|
|
QFile portableSettings(SETTINGS_FILE);
|
|
|
|
if (portableSettings.exists())
|
|
|
|
{
|
|
|
|
QSettings ps(SETTINGS_FILE, QSettings::IniFormat);
|
|
|
|
ps.beginGroup("General");
|
|
|
|
return ps.value("makeToxPortable", false).toBool();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 06:24:23 +08:00
|
|
|
void Widget::update()
|
|
|
|
{
|
|
|
|
/// 1. Find and parse the update (0-5%)
|
|
|
|
// Check that the dir exists
|
|
|
|
QString updateDirStr = getSettingsDirPath()+"/update/";
|
|
|
|
QDir updateDir(updateDirStr);
|
|
|
|
if (!updateDir.exists())
|
|
|
|
fatalError(tr("No update found."));
|
|
|
|
|
2015-12-11 01:44:45 +08:00
|
|
|
setProgress(2);
|
|
|
|
|
2014-11-10 06:24:23 +08:00
|
|
|
// Check that we have a flist and that every file on the diff exists
|
|
|
|
QFile updateFlistFile(updateDirStr+"flist");
|
|
|
|
if (!updateFlistFile.open(QIODevice::ReadOnly))
|
|
|
|
fatalError(tr("The update is incomplete."));
|
2015-03-21 16:59:28 +08:00
|
|
|
|
2014-11-10 06:24:23 +08:00
|
|
|
QByteArray updateFlistData = updateFlistFile.readAll();
|
|
|
|
updateFlistFile.close();
|
|
|
|
|
|
|
|
QList<UpdateFileMeta> updateFlist = parseFlist(updateFlistData);
|
2015-12-11 01:44:45 +08:00
|
|
|
setProgress(5);
|
2015-12-10 11:37:07 +08:00
|
|
|
|
2015-12-11 01:44:45 +08:00
|
|
|
/// 2. Generate a diff (5-50%)
|
|
|
|
QList<UpdateFileMeta> diff = genUpdateDiff(updateFlist, this);
|
2014-11-10 06:24:23 +08:00
|
|
|
for (UpdateFileMeta fileMeta : diff)
|
|
|
|
if (!QFile::exists(updateDirStr+fileMeta.installpath))
|
|
|
|
fatalError(tr("The update is incomplete."));
|
2014-11-23 21:44:12 +08:00
|
|
|
|
2015-03-21 16:59:28 +08:00
|
|
|
if (diff.size() == 0)
|
2015-12-11 01:44:45 +08:00
|
|
|
fatalError(tr("The update is empty!"));
|
|
|
|
setProgress(50);
|
|
|
|
qDebug() << "Diff generated,"<<diff.size()<<"files to update";
|
2014-11-10 06:24:23 +08:00
|
|
|
|
2015-12-11 01:44:45 +08:00
|
|
|
/// 2. Check the update (50-75%)
|
|
|
|
float checkProgressStep = 25.0/(float)diff.size();
|
|
|
|
float checkProgress = 50;
|
2014-11-10 06:24:23 +08:00
|
|
|
for (UpdateFileMeta fileMeta : diff)
|
|
|
|
{
|
|
|
|
UpdateFile file;
|
|
|
|
file.metadata = fileMeta;
|
|
|
|
|
|
|
|
QFile fileFile(updateDirStr+fileMeta.installpath);
|
|
|
|
if (!fileFile.open(QIODevice::ReadOnly))
|
|
|
|
fatalError(tr("Update files are unreadable."));
|
2015-03-21 16:59:28 +08:00
|
|
|
|
2014-11-10 06:24:23 +08:00
|
|
|
file.data = fileFile.readAll();
|
|
|
|
fileFile.close();
|
|
|
|
|
|
|
|
if (file.data.size() != (int)fileMeta.size)
|
|
|
|
fatalError(tr("Update files are corrupted."));
|
|
|
|
|
|
|
|
if (crypto_sign_verify_detached(file.metadata.sig, (unsigned char*)file.data.data(),
|
|
|
|
file.data.size(), key) != 0)
|
|
|
|
fatalError(tr("Update files are corrupted."));
|
|
|
|
|
|
|
|
checkProgress += checkProgressStep;
|
|
|
|
setProgress(checkProgress);
|
|
|
|
}
|
2015-12-11 01:44:45 +08:00
|
|
|
setProgress(75);
|
|
|
|
qDebug() << "Update files signature verified, installing";
|
2014-11-10 06:24:23 +08:00
|
|
|
|
2015-12-11 01:44:45 +08:00
|
|
|
/// 3. Install the update (75-95%)
|
|
|
|
float installProgressStep = 20.0/(float)diff.size();
|
|
|
|
float installProgress = 75;
|
2014-11-10 06:24:23 +08:00
|
|
|
for (UpdateFileMeta fileMeta : diff)
|
|
|
|
{
|
2014-11-16 18:24:15 +08:00
|
|
|
// Backup old files
|
|
|
|
if (QFile(fileMeta.installpath).exists())
|
|
|
|
{
|
2015-01-27 07:23:27 +08:00
|
|
|
QFile(fileMeta.installpath+".bak").remove();
|
2014-11-16 18:24:15 +08:00
|
|
|
QFile(fileMeta.installpath).rename(fileMeta.installpath+".bak");
|
|
|
|
backups.append(fileMeta.installpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install new ones
|
2015-01-27 07:23:27 +08:00
|
|
|
QDir().mkpath(QFileInfo(fileMeta.installpath).absolutePath());
|
2014-11-16 18:24:15 +08:00
|
|
|
QFile fileFile(updateDirStr+fileMeta.installpath);
|
|
|
|
if (!fileFile.copy(fileMeta.installpath))
|
2015-01-27 07:23:27 +08:00
|
|
|
fatalError(tr("Unable to copy the update's files from ")+(updateDirStr+fileMeta.installpath)+" to "+fileMeta.installpath);
|
2014-11-16 18:24:15 +08:00
|
|
|
installProgress += installProgressStep;
|
|
|
|
setProgress(installProgress);
|
2014-11-10 06:24:23 +08:00
|
|
|
}
|
|
|
|
setProgress(95);
|
|
|
|
|
2014-11-16 18:24:15 +08:00
|
|
|
/// 4. Delete the update and backups (95-100%)
|
2014-11-10 06:24:23 +08:00
|
|
|
deleteUpdate();
|
2014-11-16 18:24:15 +08:00
|
|
|
setProgress(97);
|
|
|
|
deleteBackups();
|
2014-11-10 06:24:23 +08:00
|
|
|
setProgress(100);
|
|
|
|
|
|
|
|
/// 5. Start qTox and exit
|
2015-12-11 01:44:45 +08:00
|
|
|
qDebug() << "Update applied, restarting qTox!";
|
2014-11-10 06:24:23 +08:00
|
|
|
startQToxAndExit();
|
|
|
|
}
|