Make the auto-updater aware of portable mode
pull/2761/merge
tux3 2016-03-11 23:27:32 +01:00
parent 1eb68542df
commit 318de322bf
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
6 changed files with 185 additions and 123 deletions

View File

@ -19,6 +19,7 @@
#include "widget.h"
#include "settings.h"
#include <QApplication>
#include <QDebug>
#include <QFile>
@ -77,10 +78,10 @@ int main(int argc, char *argv[])
{
qInstallMessageHandler(logMessageHandler);
QApplication a(argc, argv);
Settings s;
logFileStream.reset(new QTextStream);
logFileFile.reset(new QFile(QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QDir::separator()
+ "AppData" + QDir::separator() + "Roaming" + QDir::separator() + "tox")+QDir::separator()+"qtox.log"));
logFileFile.reset(new QFile(s.getSettingsDirPath()+"qtox.log"));
if (logFileFile->open(QIODevice::Append))
{
logFileStream->setDevice(logFileFile.get());
@ -97,7 +98,7 @@ int main(int argc, char *argv[])
GetUserNameA(buf, &bufsize);
qDebug() << "Updater running as user" << buf;
Widget w;
Widget w(s);
w.show();
return a.exec();

135
updater/settings.cpp Executable file
View File

@ -0,0 +1,135 @@
#include "settings.h"
#include <QFile>
#include <QSettings>
#include <QDir>
#include <QDebug>
#include <QStandardPaths>
#ifdef Q_OS_WIN
#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>
#endif
Settings::Settings()
{
portable = false;
QFile portableSettings(SETTINGS_FILE);
if (portableSettings.exists())
{
QSettings ps(SETTINGS_FILE, QSettings::IniFormat);
ps.beginGroup("General");
portable = ps.value("makeToxPortable", false).toBool();
}
qDebug() << "Portable: "<<portable;
#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
}
Settings::~Settings()
{
#ifdef Q_OS_WIN
CloseHandle(hPrimaryToken);
#endif
}
QString Settings::getSettingsDirPath() const
{
if (portable)
return QString(".")+QDir::separator();
// workaround for https://bugreports.qt-project.org/browse/QTBUG-38845
#ifdef Q_OS_WIN
wchar_t* path;
bool isOld = false; // If true, we can't unelevate and just return the path for our current home
auto shell32H = LoadLibrary(TEXT("shell32.dll"));
if (!(isOld = (shell32H == nullptr)))
{
auto SHGetKnownFolderPathH = (decltype(&SHGetKnownFolderPath))
GetProcAddress(shell32H, "SHGetKnownFolderPath");
if (!(isOld = (SHGetKnownFolderPathH == nullptr)))
SHGetKnownFolderPathH(FOLDERID_RoamingAppData, 0, hPrimaryToken, &path);
}
if (isOld)
{
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QDir::separator()
+ "AppData" + QDir::separator() + "Roaming" + QDir::separator() + "tox" + QDir::separator());
}
else
{
QString pathStr = QString::fromStdWString(path);
pathStr.replace("\\", "/");
return pathStr + "/tox";
}
#elif defined(Q_OS_OSX)
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QDir::separator()
+ "Library" + QDir::separator() + "Application Support" + QDir::separator() + "Tox")+QDir::separator();
#else
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
+ QDir::separator() + "tox")+QDir::separator();
#endif
}
#ifdef Q_OS_WIN
HANDLE Settings::getPrimaryToken() const
{
return hPrimaryToken;
}
#endif

29
updater/settings.h Executable file
View File

@ -0,0 +1,29 @@
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QString>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
class Settings
{
public:
Settings();
~Settings();
QString getSettingsDirPath() const; ///< The returned path ends with a directory separator
#ifdef Q_OS_WIN
HANDLE getPrimaryToken() const; ///< Used to impersonnate the unelevated user
#endif
private:
bool portable;
static constexpr const char* SETTINGS_FILE = "qtox.ini";
#ifdef Q_OS_WIN
HANDLE hPrimaryToken;
#endif
};
#endif // SETTINGS_H

View File

@ -18,11 +18,13 @@ QMAKE_CXXFLAGS += -fno-exceptions
SOURCES += main.cpp\
widget.cpp \
update.cpp \
serialize.cpp
serialize.cpp \
settings.cpp
HEADERS += widget.h \
update.h \
serialize.h
serialize.h \
settings.h
FORMS += widget.ui

View File

@ -50,9 +50,10 @@ const QString QTOX_PATH;
#endif
const QString SETTINGS_FILE = "settings.ini";
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
Widget::Widget(const Settings &s) :
QWidget(nullptr),
ui(new Ui::Widget),
settings{s}
{
ui->setupUi(this);
@ -60,66 +61,11 @@ Widget::Widget(QWidget *parent) :
if (!supported)
fatalError(tr("The qTox updater is not supported on this platform."));
#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
QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection);
}
Widget::~Widget()
{
#ifdef Q_OS_WIN
CloseHandle(hPrimaryToken);
#endif
delete ui;
}
@ -141,7 +87,7 @@ void Widget::fatalError(QString message)
void Widget::deleteUpdate()
{
QDir updateDir(getSettingsDirPath()+"/update/");
QDir updateDir(settings.getSettingsDirPath()+"/update/");
updateDir.removeRecursively();
}
@ -164,7 +110,9 @@ void Widget::startQToxAndExit()
GetProcAddress(advapi32H, "CreateProcessWithTokenW");
if ((unelevateOk = (CreateProcessWithTokenWH != nullptr)))
{
if (!CreateProcessWithTokenWH(hPrimaryToken, 0, QTOX_PATH.toStdWString().c_str(), 0, 0, 0, 0, &si, &pi))
if (!CreateProcessWithTokenWH(settings.getPrimaryToken(), 0,
QTOX_PATH.toStdWString().c_str(), 0, 0, 0,
QApplication::applicationDirPath().toStdWString().c_str(), &si, &pi))
unelevateOk = false;
}
}
@ -196,60 +144,11 @@ void Widget::restoreBackups()
QFile(file+".bak").rename(file);
}
QString Widget::getSettingsDirPath()
{
if (isToxPortableEnabled())
return ".";
#ifdef Q_OS_WIN
wchar_t* path;
bool isOld = false; // If true, we can't unelevate and just return the path for our current home
auto shell32H = LoadLibrary(TEXT("shell32.dll"));
if (!(isOld = (shell32H == nullptr)))
{
auto SHGetKnownFolderPathH = (decltype(&SHGetKnownFolderPath))
GetProcAddress(shell32H, "SHGetKnownFolderPath");
if (!(isOld = (SHGetKnownFolderPathH == nullptr)))
SHGetKnownFolderPathH(FOLDERID_RoamingAppData, 0, hPrimaryToken, &path);
}
if (isOld)
{
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QDir::separator()
+ "AppData" + QDir::separator() + "Roaming" + QDir::separator() + "tox" + QDir::separator());
}
else
{
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;
}
}
void Widget::update()
{
/// 1. Find and parse the update (0-5%)
// Check that the dir exists
QString updateDirStr = getSettingsDirPath()+"/update/";
QString updateDirStr = settings.getSettingsDirPath()+"/update/";
QDir updateDir(updateDirStr);
if (!updateDir.exists())
fatalError(tr("No update found."));
@ -259,7 +158,7 @@ void Widget::update()
// 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."));
fatalError(tr("The update is incomplete!"));
QByteArray updateFlistData = updateFlistFile.readAll();
updateFlistFile.close();

View File

@ -21,6 +21,7 @@
#ifndef WIDGET_H
#define WIDGET_H
#include "settings.h"
#include <QWidget>
#ifdef Q_OS_WIN
@ -36,15 +37,13 @@ class Widget : public QWidget
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
explicit Widget(const Settings& s);
~Widget();
// Utilities
void deleteBackups();
void restoreBackups();
void setProgress(int value);
QString getSettingsDirPath();
bool isToxPortableEnabled();
// Noreturn
void fatalError(QString message); ///< Calls deleteUpdate and startQToxAndExit
@ -58,10 +57,7 @@ public slots:
private:
Ui::Widget *ui;
QStringList backups;
#ifdef Q_OS_WIN
HANDLE hPrimaryToken;
#endif
const Settings& settings;
};
#endif // WIDGET_H