2019-06-24 22:01:18 +08:00
|
|
|
/*
|
|
|
|
Copyright © 2019 by The qTox Project Contributors
|
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-03-12 06:27:32 +08:00
|
|
|
#include "settings.h"
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
2016-03-12 06:27:32 +08:00
|
|
|
#include <QFile>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#ifdef _WIN32_WINNT
|
|
|
|
#undef _WIN32_WINNT
|
|
|
|
#endif
|
|
|
|
#define _WIN32_WINNT 0x0600 // Vista for SHGetKnownFolderPath
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <exdisp.h>
|
2016-03-12 06:27:32 +08:00
|
|
|
#include <shldisp.h>
|
|
|
|
#include <shlobj.h>
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <windows.h>
|
2016-03-12 06:27:32 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Settings::Settings()
|
|
|
|
{
|
|
|
|
portable = false;
|
|
|
|
QFile portableSettings(SETTINGS_FILE);
|
2017-02-26 19:52:45 +08:00
|
|
|
if (portableSettings.exists()) {
|
2016-03-12 06:27:32 +08:00
|
|
|
QSettings ps(SETTINGS_FILE, QSettings::IniFormat);
|
|
|
|
ps.beginGroup("General");
|
|
|
|
portable = ps.value("makeToxPortable", false).toBool();
|
|
|
|
}
|
2017-02-26 19:52:45 +08:00
|
|
|
qDebug() << "Portable: " << portable;
|
2016-03-12 06:27:32 +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
|
2018-09-07 17:56:52 +08:00
|
|
|
HANDLE hProcessToken = nullptr;
|
2016-03-12 06:27:32 +08:00
|
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hProcessToken))
|
|
|
|
goto unelevateFail;
|
|
|
|
TOKEN_PRIVILEGES tkp;
|
|
|
|
tkp.PrivilegeCount = 1;
|
2018-09-07 17:56:52 +08:00
|
|
|
LookupPrivilegeValueW(nullptr, SE_INCREASE_QUOTA_NAME, &tkp.Privileges[0].Luid);
|
2016-03-12 06:27:32 +08:00
|
|
|
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
2018-09-07 17:56:52 +08:00
|
|
|
AdjustTokenPrivileges(hProcessToken, FALSE, &tkp, 0, nullptr, nullptr);
|
2016-03-12 06:27:32 +08:00
|
|
|
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.
|
2017-02-26 19:52:45 +08:00
|
|
|
// Based on experimentation, this is the minimal set of rights required for
|
|
|
|
// CreateProcessWithTokenW (contrary to current documentation).
|
2018-09-07 17:56:52 +08:00
|
|
|
if (!DuplicateTokenEx(hShellProcessToken, dwTokenRights, nullptr, SecurityImpersonation,
|
2017-02-26 19:52:45 +08:00
|
|
|
TokenPrimary, &hPrimaryToken))
|
2016-03-12 06:27:32 +08:00
|
|
|
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)
|
2017-02-26 19:52:45 +08:00
|
|
|
return QString(".") + QDir::separator();
|
2016-03-12 06:27:32 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
// workaround for https://bugreports.qt-project.org/browse/QTBUG-38845
|
2016-03-12 06:27:32 +08:00
|
|
|
#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"));
|
2017-02-26 19:52:45 +08:00
|
|
|
if (!(isOld = (shell32H == nullptr))) {
|
|
|
|
auto SHGetKnownFolderPathH =
|
|
|
|
(decltype(&SHGetKnownFolderPath))GetProcAddress(shell32H, "SHGetKnownFolderPath");
|
2016-03-12 06:27:32 +08:00
|
|
|
if (!(isOld = (SHGetKnownFolderPathH == nullptr)))
|
|
|
|
SHGetKnownFolderPathH(FOLDERID_RoamingAppData, 0, hPrimaryToken, &path);
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (isOld) {
|
|
|
|
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)
|
|
|
|
+ QDir::separator() + "AppData" + QDir::separator() + "Roaming"
|
|
|
|
+ QDir::separator() + "tox" + QDir::separator());
|
|
|
|
} else {
|
2016-03-12 06:27:32 +08:00
|
|
|
QString pathStr = QString::fromStdWString(path);
|
|
|
|
pathStr.replace("\\", "/");
|
|
|
|
return pathStr + "/tox";
|
|
|
|
}
|
|
|
|
#elif defined(Q_OS_OSX)
|
2017-02-26 19:52:45 +08:00
|
|
|
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)
|
|
|
|
+ QDir::separator() + "Library" + QDir::separator()
|
|
|
|
+ "Application Support" + QDir::separator() + "Tox")
|
|
|
|
+ QDir::separator();
|
2016-03-12 06:27:32 +08:00
|
|
|
#else
|
|
|
|
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
|
2017-02-26 19:52:45 +08:00
|
|
|
+ QDir::separator() + "tox")
|
|
|
|
+ QDir::separator();
|
2016-03-12 06:27:32 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
HANDLE Settings::getPrimaryToken() const
|
|
|
|
{
|
|
|
|
return hPrimaryToken;
|
|
|
|
}
|
|
|
|
#endif
|