From 98ebd19119e400ef5cedf826ef6ffdf5468f2b34 Mon Sep 17 00:00:00 2001 From: Nova Date: Wed, 9 Oct 2024 20:35:41 +0330 Subject: [PATCH] fix: Fix Linux permission handling --- main/NekoGui.cpp | 7 +++---- sys/ExternalProcess.cpp | 6 +----- sys/linux/LinuxCap.cpp | 23 ++++------------------- sys/linux/LinuxCap.h | 6 ++---- ui/mainwindow.cpp | 14 ++++++++++++-- 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/main/NekoGui.cpp b/main/NekoGui.cpp index a08979d..07c78c8 100644 --- a/main/NekoGui.cpp +++ b/main/NekoGui.cpp @@ -421,11 +421,10 @@ namespace NekoGui { bool admin = false; #ifdef Q_OS_WIN admin = Windows_IsInAdmin(); +#elifdef Q_OS_LINUX + admin = QFileInfo(FindNekoBoxCoreRealPath()).groupId() == 0; #else -#ifdef Q_OS_LINUX - admin |= Linux_GetCapString(FindNekoBoxCoreRealPath()).contains("cap_sys_admin"); -#endif - admin |= geteuid() == 0; + admin = geteuid() == 0; #endif isAdminCache = admin; diff --git a/sys/ExternalProcess.cpp b/sys/ExternalProcess.cpp index 66abf95..ca6bcfb 100644 --- a/sys/ExternalProcess.cpp +++ b/sys/ExternalProcess.cpp @@ -53,12 +53,8 @@ namespace NekoGui_sys { } QProcess::setEnvironment(env); -#ifdef Q_OS_LINUX - if (NekoGui::IsAdmin()) QProcess::startCommand("sudo " + program + " " + arguments.join(" ")); - else QProcess::start(program, arguments); -#else + QProcess::start(program, arguments); -#endif } void ExternalProcess::Kill() { diff --git a/sys/linux/LinuxCap.cpp b/sys/linux/LinuxCap.cpp index 5e65311..59fb2cb 100644 --- a/sys/linux/LinuxCap.cpp +++ b/sys/linux/LinuxCap.cpp @@ -4,24 +4,9 @@ #include #include -#define EXIT_CODE(p) (p.exitStatus() == QProcess::NormalExit ? p.exitCode() : -1) - -QString Linux_GetCapString(const QString &path) { - QProcess p; - p.setProgram(Linux_FindCapProgsExec("getcap")); - p.setArguments({path}); - p.start(); - p.waitForFinished(500); - return p.readAllStandardOutput(); -} - -int Linux_Pkexec_SetCapString(const QString &path, const QString &cap) { - QProcess p; - p.setProgram("pkexec"); - p.setArguments({Linux_FindCapProgsExec("setcap"), cap, path}); - p.start(); - p.waitForFinished(-1); - return EXIT_CODE(p); +int Linux_Run_Command(const QString &commandName, const QString &args) { + auto command = QString("pkexec %1 %2").arg(Linux_FindCapProgsExec(commandName)).arg(args); + return system(command.toStdString().c_str()); } bool Linux_HavePkexec() { @@ -31,7 +16,7 @@ bool Linux_HavePkexec() { p.setProcessChannelMode(QProcess::SeparateChannels); p.start(); p.waitForFinished(500); - return EXIT_CODE(p) == 0; + return (p.exitStatus() == QProcess::NormalExit ? p.exitCode() : -1) == 0; } QString Linux_FindCapProgsExec(const QString &name) { diff --git a/sys/linux/LinuxCap.h b/sys/linux/LinuxCap.h index 64750e4..75a2346 100644 --- a/sys/linux/LinuxCap.h +++ b/sys/linux/LinuxCap.h @@ -2,10 +2,8 @@ #include -QString Linux_GetCapString(const QString &path); - -int Linux_Pkexec_SetCapString(const QString &path, const QString &cap); - bool Linux_HavePkexec(); QString Linux_FindCapProgsExec(const QString &name); + +int Linux_Run_Command(const QString &commandName, const QString &args); \ No newline at end of file diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index 18a75b3..d0283b1 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -732,12 +732,22 @@ bool MainWindow::get_elevated_permissions(int reason) { MessageBoxWarning(software_name, "Please install \"pkexec\" first."); return false; } - auto n = QMessageBox::warning(GetMessageBoxParent(), software_name, tr("Please run Nekoray as admin"), QMessageBox::Yes | QMessageBox::No); + auto n = QMessageBox::warning(GetMessageBoxParent(), software_name, tr("Please give the core root privileges"), QMessageBox::Yes | QMessageBox::No); if (n == QMessageBox::Yes) { - auto ret = Linux_Pkexec_SetCapString(NekoGui::FindNekoBoxCoreRealPath(), "cap_sys_admin=ep"); + auto chownArgs = QString("root:root " + NekoGui::FindNekoBoxCoreRealPath()); + auto ret = Linux_Run_Command("chown", chownArgs); + if (ret != 0) { + MW_show_log(QString("Failed to run chown %1 code is %2").arg(chownArgs).arg(ret)); + return false; + } + auto chmodArgs = QString("u+s " + NekoGui::FindNekoBoxCoreRealPath()); + ret = Linux_Run_Command("chmod", chmodArgs); if (ret == 0) { this->exit_reason = reason; on_menu_exit_triggered(); + } else { + MW_show_log(QString("Failed to run chmod %1").arg(chmodArgs)); + return false; } } #endif