forked from AdrianTM/mx-user
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhelper.cpp
More file actions
196 lines (172 loc) · 6.22 KB
/
helper.cpp
File metadata and controls
196 lines (172 loc) · 6.22 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
/**********************************************************************
* helper.cpp
**********************************************************************
* Copyright (C) 2026 MX Authors
*
* Authors: Adrian
* MX Linux <http://mxlinux.org>
* OpenAI Codex
*
* This program 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.
**********************************************************************/
#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]] QByteArray readHelperInput()
{
QFile input;
if (!input.open(stdin, QIODevice::ReadOnly)) {
return {};
}
return input.readAll();
}
[[nodiscard]] const QHash<QString, QStringList> &allowedCommands()
{
static const QHash<QString, QStringList> commands {
{"addgroup", {"/usr/sbin/addgroup", "/sbin/addgroup", "/usr/bin/addgroup"}},
{"adduser", {"/usr/sbin/adduser", "/sbin/adduser", "/usr/bin/adduser"}},
{"chown", {"/usr/bin/chown", "/bin/chown"}},
{"delgroup", {"/usr/sbin/delgroup", "/sbin/delgroup", "/usr/bin/delgroup"}},
{"deluser", {"/usr/sbin/deluser", "/sbin/deluser", "/usr/bin/deluser"}},
{"find", {"/usr/bin/find", "/bin/find"}},
{"gpasswd", {"/usr/bin/gpasswd", "/sbin/gpasswd", "/bin/gpasswd"}},
{"groupadd", {"/usr/sbin/groupadd", "/sbin/groupadd", "/usr/bin/groupadd"}},
{"groupdel", {"/usr/sbin/groupdel", "/sbin/groupdel", "/usr/bin/groupdel"}},
{"groupmod", {"/usr/sbin/groupmod", "/sbin/groupmod", "/usr/bin/groupmod"}},
{"killall", {"/usr/bin/killall", "/bin/killall"}},
{"passwd", {"/usr/bin/passwd", "/sbin/passwd", "/bin/passwd"}},
{"rm", {"/usr/bin/rm", "/bin/rm"}},
{"rsync", {"/usr/bin/rsync"}},
{"sed", {"/usr/bin/sed", "/bin/sed"}},
{"timeout", {"/usr/bin/timeout", "/bin/timeout"}},
{"useradd", {"/usr/sbin/useradd", "/sbin/useradd", "/usr/bin/useradd"}},
{"userdel", {"/usr/sbin/userdel", "/sbin/userdel", "/usr/bin/userdel"}},
{"usermod", {"/usr/sbin/usermod", "/sbin/usermod", "/usr/bin/usermod"}},
};
return commands;
}
[[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, const QByteArray &input = {})
{
ProcessResult result;
QProcess process;
process.start(program, args, QIODevice::ReadWrite);
if (!process.waitForStarted()) {
result.standardError = QString("Failed to start %1").arg(program).toUtf8();
result.exitCode = 127;
return result;
}
result.started = true;
if (!input.isEmpty()) {
process.write(input);
}
process.closeWriteChannel();
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 QByteArray &input = {})
{
const auto commandIt = allowedCommands().constFind(command);
if (commandIt == allowedCommands().constEnd()) {
printError(QString("Command is not allowed: %1").arg(command));
return 127;
}
const QString program = resolveBinary(commandIt.value());
if (program.isEmpty()) {
printError(QString("Command is not available: %1").arg(command));
return 127;
}
return relayResult(runProcess(program, args, input));
}
[[nodiscard]] int handleExec(const QStringList &args)
{
if (args.isEmpty()) {
printError(QStringLiteral("exec requires a command name"));
return 1;
}
return runAllowedCommand(args.constFirst(), args.mid(1), readHelperInput());
}
[[nodiscard]] int handlePasswd(const QStringList &args)
{
if (args.size() != 1) {
printError(QStringLiteral("passwd requires exactly one username"));
return 1;
}
static const QRegularExpression userNameRx(
QRegularExpression::anchoredPattern(QStringLiteral(R"([A-Za-z_][A-Za-z0-9_-]*[$]?)")));
const QString userName = args.constFirst();
if (!userNameRx.match(userName).hasMatch()) {
printError(QString("Invalid username: %1").arg(userName));
return 1;
}
return runAllowedCommand(QStringLiteral("passwd"), {userName}, readHelperInput());
}
} // namespace
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
const QStringList args = app.arguments().mid(1);
if (args.isEmpty()) {
printError(QStringLiteral("Missing helper action"));
return 1;
}
const QString action = args.constFirst();
const QStringList remainingArgs = args.mid(1);
if (action == QLatin1String("exec")) {
return handleExec(remainingArgs);
}
if (action == QLatin1String("passwd")) {
return handlePasswd(remainingArgs);
}
printError(QString("Unsupported helper action: %1").arg(action));
return 1;
}