Launch new version directly in write-protected mode

This commit is contained in:
Ilya Fedin 2021-05-03 14:21:17 +04:00 committed by John Preston
parent 7444f17c4e
commit ee00f12131
2 changed files with 16 additions and 38 deletions

View File

@ -8,7 +8,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <cstdio> #include <cstdio>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <cstdlib> #include <cstdlib>
#include <unistd.h> #include <unistd.h>
@ -391,8 +390,6 @@ int main(int argc, char *argv[]) {
char *key = 0; char *key = 0;
char *workdir = 0; char *workdir = 0;
char *oldUsername = 0;
char *dbusAddress = 0;
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
if (equal(argv[i], "-noupdate")) { if (equal(argv[i], "-noupdate")) {
needupdate = false; needupdate = false;
@ -410,19 +407,16 @@ int main(int argc, char *argv[]) {
tosettings = true; tosettings = true;
} else if (equal(argv[i], "-workdir_custom")) { } else if (equal(argv[i], "-workdir_custom")) {
customWorkingDir = true; customWorkingDir = true;
} else if (equal(argv[i], "-writeprotected")) {
writeprotected = true;
} else if (equal(argv[i], "-key") && ++i < argc) { } else if (equal(argv[i], "-key") && ++i < argc) {
key = argv[i]; key = argv[i];
} else if (equal(argv[i], "-writeprotected") && ++i < argc) {
writeprotected = true;
oldUsername = argv[i];
} else if (equal(argv[i], "-workpath") && ++i < argc) { } else if (equal(argv[i], "-workpath") && ++i < argc) {
workDir = workdir = argv[i]; workDir = workdir = argv[i];
} else if (equal(argv[i], "-exename") && ++i < argc) { } else if (equal(argv[i], "-exename") && ++i < argc) {
exeName = argv[i]; exeName = argv[i];
} else if (equal(argv[i], "-exepath") && ++i < argc) { } else if (equal(argv[i], "-exepath") && ++i < argc) {
exePath = argv[i]; exePath = argv[i];
} else if (equal(argv[i], "-dbus") && ++i < argc) {
dbusAddress = argv[i];
} }
} }
if (exeName.empty() || exeName.find('/') != string::npos) { if (exeName.empty() || exeName.find('/') != string::npos) {
@ -504,15 +498,6 @@ int main(int argc, char *argv[]) {
// Force null-terminated .data() call result. // Force null-terminated .data() call result.
values.push_back(arg + char(0)); 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(path);
push("-noupdate"); push("-noupdate");
if (autostart) push("-autostart"); if (autostart) push("-autostart");
@ -536,19 +521,17 @@ int main(int argc, char *argv[]) {
} }
args.push_back(nullptr); args.push_back(nullptr);
// let the parent launch instead
if (!writeprotected) {
pid_t pid = fork(); pid_t pid = fork();
switch (pid) { switch (pid) {
case -1: case -1:
writeLog("fork() failed!"); writeLog("fork() failed!");
return 1; return 1;
case 0: case 0:
execvp(args[0], args.data()); execv(args[0], args.data());
return 1; return 1;
} }
// pkexec needs an alive parent
if (writeprotected) {
waitpid(pid, nullptr, 0);
} }
writeLog("Executed Telegram, closing log and quitting.."); writeLog("Executed Telegram, closing log and quitting..");

View File

@ -125,14 +125,7 @@ bool Launcher::launchUpdater(UpdaterLaunch action) {
argumentsList.push("-workdir_custom"); argumentsList.push("-workdir_custom");
} }
if (cWriteProtected()) { if (cWriteProtected()) {
const auto currentUid = geteuid();
const auto pw = getpwuid(currentUid);
if (pw) {
argumentsList.push("-writeprotected"); argumentsList.push("-writeprotected");
argumentsList.push(pw->pw_name);
argumentsList.push("-dbus");
argumentsList.push(qgetenv("DBUS_SESSION_BUS_ADDRESS"));
}
} }
} }
@ -150,6 +143,8 @@ bool Launcher::launchUpdater(UpdaterLaunch action) {
// pkexec needs an alive parent // pkexec needs an alive parent
if (cWriteProtected()) { if (cWriteProtected()) {
waitpid(pid, nullptr, 0); waitpid(pid, nullptr, 0);
// launch new version in the same environment
return launchUpdater(UpdaterLaunch::JustRelaunch);
} }
return true; return true;