-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhelper.cpp
More file actions
203 lines (177 loc) · 6.3 KB
/
helper.cpp
File metadata and controls
203 lines (177 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**********************************************************************
* helper.cpp
**********************************************************************
* Copyright (C) 2026 MX Authors
*
* Authors: Adrian
* MX Linux <http://mxlinux.org>
* OpenAI Codex
*
* This is free 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.
*
* This program 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 this package. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <QCoreApplication>
#include <QFileInfo>
#include <QHash>
#include <QProcess>
#include <QRegularExpression>
#include <cstdio>
namespace
{
struct ProcessResult
{
bool started = false;
int exitCode = 1;
QProcess::ExitStatus exitStatus = QProcess::NormalExit;
QByteArray standardOutput;
QByteArray standardError;
};
void writeAndFlush(FILE *stream, const QByteArray &data)
{
if (!data.isEmpty()) {
std::fwrite(data.constData(), 1, static_cast<size_t>(data.size()), stream);
std::fflush(stream);
}
}
void printError(const QString &message)
{
writeAndFlush(stderr, message.toUtf8() + '\n');
}
[[nodiscard]] const QHash<QString, QStringList> &allowedCommands()
{
static const QHash<QString, QStringList> commands {
{"apt-get", {"/usr/bin/apt-get"}},
{"chmod", {"/usr/bin/chmod", "/bin/chmod"}},
{"chown", {"/usr/bin/chown", "/bin/chown"}},
{"du", {"/usr/bin/du", "/bin/du"}},
{"find", {"/usr/bin/find", "/bin/find"}},
{"install", {"/usr/bin/install", "/bin/install"}},
{"mv", {"/usr/bin/mv", "/bin/mv"}},
{"pacman", {"/usr/bin/pacman"}},
{"pgrep", {"/usr/bin/pgrep", "/bin/pgrep"}},
{"rm", {"/usr/bin/rm", "/bin/rm"}},
{"true", {"/usr/bin/true", "/bin/true"}},
{"truncate", {"/usr/bin/truncate", "/bin/truncate"}},
};
return commands;
}
[[nodiscard]] QString resolveBinary(const QStringList &candidates);
[[nodiscard]] QString resolveRunuserBinary()
{
static const QStringList candidates {"/usr/sbin/runuser", "/sbin/runuser", "/usr/bin/runuser"};
return resolveBinary(candidates);
}
[[nodiscard]] QString resolveFlatpakBinary()
{
static const QStringList candidates {"/usr/bin/flatpak", "/bin/flatpak"};
return resolveBinary(candidates);
}
[[nodiscard]] QString resolveBinary(const QStringList &candidates)
{
for (const QString &candidate : candidates) {
const QFileInfo info(candidate);
if (info.exists() && info.isExecutable()) {
return candidate;
}
}
return {};
}
[[nodiscard]] ProcessResult runProcess(const QString &program, const QStringList &args)
{
ProcessResult result;
QProcess process;
process.start(program, args, QIODevice::ReadOnly);
if (!process.waitForStarted()) {
result.standardError = QString("Failed to start %1").arg(program).toUtf8();
result.exitCode = 127;
return result;
}
result.started = true;
process.waitForFinished(-1);
result.exitStatus = process.exitStatus();
result.exitCode = process.exitCode();
result.standardOutput = process.readAllStandardOutput();
result.standardError = process.readAllStandardError();
return result;
}
[[nodiscard]] int relayResult(const ProcessResult &result)
{
writeAndFlush(stdout, result.standardOutput);
writeAndFlush(stderr, result.standardError);
if (!result.started) {
return result.exitCode;
}
return result.exitStatus == QProcess::NormalExit ? result.exitCode : 1;
}
[[nodiscard]] int runAllowedCommand(const QString &command, const QStringList &args)
{
const auto commandIt = allowedCommands().constFind(command);
if (commandIt == allowedCommands().constEnd()) {
printError(QString("Command is not allowed: %1").arg(command));
return 127;
}
const QString resolvedCommand = resolveBinary(commandIt.value());
if (resolvedCommand.isEmpty()) {
printError(QString("Command is not available: %1").arg(command));
return 127;
}
return relayResult(runProcess(resolvedCommand, args));
}
[[nodiscard]] int runFlatpakCleanupForUser(const QString &user)
{
static const QRegularExpression safeUserPattern(QStringLiteral("^[A-Za-z0-9._-]+$"));
if (user.isEmpty() || !safeUserPattern.match(user).hasMatch()) {
printError(QString("Invalid username: %1").arg(user));
return 1;
}
const QString runuserBinary = resolveRunuserBinary();
if (runuserBinary.isEmpty()) {
printError(QStringLiteral("Command is not available: runuser"));
return 127;
}
const QString flatpakBinary = resolveFlatpakBinary();
if (flatpakBinary.isEmpty()) {
printError(QStringLiteral("Command is not available: flatpak"));
return 127;
}
return relayResult(runProcess(runuserBinary,
{"-u", user, "--", flatpakBinary, "uninstall", "--unused",
"--delete-data", "--noninteractive"}));
}
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
const QStringList arguments = app.arguments().mid(1);
if (arguments.isEmpty()) {
printError(QStringLiteral("Missing helper action"));
return 1;
}
const QString action = arguments.constFirst();
if (action == "exec") {
if (arguments.size() < 2) {
printError(QStringLiteral("Missing helper command"));
return 1;
}
return runAllowedCommand(arguments.at(1), arguments.mid(2));
}
if (action == "flatpak-cleanup-user") {
if (arguments.size() != 2) {
printError(QStringLiteral("flatpak-cleanup-user requires exactly one username"));
return 1;
}
return runFlatpakCleanupForUser(arguments.at(1));
}
printError(QString("Unsupported helper action: %1").arg(action));
return 1;
}