From ee00f1213136375d9177f7aa2491e34142d9e9fc Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Mon, 3 May 2021 14:21:17 +0400 Subject: [PATCH] Launch new version directly in write-protected mode --- Telegram/SourceFiles/_other/updater_linux.cpp | 43 ++++++------------- .../platform/linux/launcher_linux.cpp | 11 ++--- 2 files changed, 16 insertions(+), 38 deletions(-) diff --git a/Telegram/SourceFiles/_other/updater_linux.cpp b/Telegram/SourceFiles/_other/updater_linux.cpp index 397f10056b..c5cfe12696 100644 --- a/Telegram/SourceFiles/_other/updater_linux.cpp +++ b/Telegram/SourceFiles/_other/updater_linux.cpp @@ -8,7 +8,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include #include #include -#include #include #include #include @@ -391,8 +390,6 @@ int main(int argc, char *argv[]) { char *key = 0; char *workdir = 0; - char *oldUsername = 0; - char *dbusAddress = 0; for (int i = 1; i < argc; ++i) { if (equal(argv[i], "-noupdate")) { needupdate = false; @@ -410,19 +407,16 @@ int main(int argc, char *argv[]) { tosettings = true; } else if (equal(argv[i], "-workdir_custom")) { customWorkingDir = true; + } else if (equal(argv[i], "-writeprotected")) { + writeprotected = true; } else if (equal(argv[i], "-key") && ++i < argc) { key = argv[i]; - } else if (equal(argv[i], "-writeprotected") && ++i < argc) { - writeprotected = true; - oldUsername = argv[i]; } else if (equal(argv[i], "-workpath") && ++i < argc) { workDir = workdir = argv[i]; } else if (equal(argv[i], "-exename") && ++i < argc) { exeName = argv[i]; } else if (equal(argv[i], "-exepath") && ++i < argc) { exePath = argv[i]; - } else if (equal(argv[i], "-dbus") && ++i < argc) { - dbusAddress = argv[i]; } } if (exeName.empty() || exeName.find('/') != string::npos) { @@ -504,15 +498,6 @@ int main(int argc, char *argv[]) { // Force null-terminated .data() call result. values.push_back(arg + char(0)); }; - if (writeprotected) { // run un-elevated - push("pkexec"); - push("--user"); - push(oldUsername); - push("env"); - push("DBUS_SESSION_BUS_ADDRESS=" + string(dbusAddress)); - push("systemd-run"); // restore environment - push("--user"); - } push(path); push("-noupdate"); if (autostart) push("-autostart"); @@ -536,19 +521,17 @@ int main(int argc, char *argv[]) { } args.push_back(nullptr); - pid_t pid = fork(); - switch (pid) { - case -1: - writeLog("fork() failed!"); - return 1; - case 0: - execvp(args[0], args.data()); - return 1; - } - - // pkexec needs an alive parent - if (writeprotected) { - waitpid(pid, nullptr, 0); + // let the parent launch instead + if (!writeprotected) { + pid_t pid = fork(); + switch (pid) { + case -1: + writeLog("fork() failed!"); + return 1; + case 0: + execv(args[0], args.data()); + return 1; + } } writeLog("Executed Telegram, closing log and quitting.."); diff --git a/Telegram/SourceFiles/platform/linux/launcher_linux.cpp b/Telegram/SourceFiles/platform/linux/launcher_linux.cpp index 6bb9bffd40..3f8489502b 100644 --- a/Telegram/SourceFiles/platform/linux/launcher_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/launcher_linux.cpp @@ -125,14 +125,7 @@ bool Launcher::launchUpdater(UpdaterLaunch action) { argumentsList.push("-workdir_custom"); } if (cWriteProtected()) { - const auto currentUid = geteuid(); - const auto pw = getpwuid(currentUid); - if (pw) { - argumentsList.push("-writeprotected"); - argumentsList.push(pw->pw_name); - argumentsList.push("-dbus"); - argumentsList.push(qgetenv("DBUS_SESSION_BUS_ADDRESS")); - } + argumentsList.push("-writeprotected"); } } @@ -150,6 +143,8 @@ bool Launcher::launchUpdater(UpdaterLaunch action) { // pkexec needs an alive parent if (cWriteProtected()) { waitpid(pid, nullptr, 0); + // launch new version in the same environment + return launchUpdater(UpdaterLaunch::JustRelaunch); } return true;