/* Copyright © 2014 by The qTox Project This file is part of qTox, a Qt-based graphical interface for Tox. qTox is libre software: you can redistribute it and/or modify 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. qTox is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with qTox. If not, see */ #include "widget.h" #include "ui_widget.h" #include #include #include #include #include #include #include #include "update.h" #ifdef Q_OS_WIN #ifdef _WIN32_WINNT #undef _WIN32_WINNT #endif #define _WIN32_WINNT 0x0600 // Vista for SHGetKnownFolderPath #include #include #include #include const bool supported = true; const QString QTOX_PATH = "qtox.exe"; #else const bool supported = false; const QString QTOX_PATH; #endif const QString SETTINGS_FILE = "settings.ini"; 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.")); #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; } void Widget::setProgress(int value) { ui->progress->setValue(value); ui->progress->repaint(); qApp->processEvents(); } void Widget::fatalError(QString message) { qCritical() << "Update aborted with error:"< updateFlist = parseFlist(updateFlistData); setProgress(5); /// 2. Generate a diff (5-50%) QList diff = genUpdateDiff(updateFlist, this); for (UpdateFileMeta fileMeta : diff) if (!QFile::exists(updateDirStr+fileMeta.installpath)) fatalError(tr("The update is incomplete.")); if (diff.size() == 0) fatalError(tr("The update is empty!")); setProgress(50); qDebug() << "Diff generated,"<