fix: Fix Linux permission handling

This commit is contained in:
Nova 2024-10-09 20:35:41 +03:30
parent e8c69e3dba
commit 98ebd19119
5 changed files with 22 additions and 34 deletions

View File

@ -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;

View File

@ -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() {

View File

@ -4,24 +4,9 @@
#include <QProcess>
#include <QStandardPaths>
#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) {

View File

@ -2,10 +2,8 @@
#include <QString>
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);

View File

@ -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