1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 08:18:12 +00:00

Userland: Add as-user execution to the pls utility

Commands may be executed as a specific user by passing the user's UID to
the '-u' flag in pls.
This commit is contained in:
pyunbiwi 2021-08-16 03:58:34 +01:00 committed by Andreas Kling
parent 64bc5f668d
commit 68d07320cf

View file

@ -15,6 +15,8 @@ int main(int argc, char** argv)
{
Vector<char const*> command;
Core::ArgsParser args_parser;
uid_t as_user_uid = 0;
args_parser.add_option(as_user_uid, "User to execute as", nullptr, 'u', "UID");
args_parser.add_positional_argument(command, "Command to run at elevated privilege level", "command");
args_parser.parse(argc, argv);
@ -28,6 +30,13 @@ int main(int argc, char** argv)
return 1;
}
// Fail gracefully if as_user_uid is invalid
auto as_user_or_error = Core::Account::from_uid(as_user_uid);
if (as_user_or_error.is_error()) {
warnln("{}", as_user_or_error.error());
return 1;
}
// If the current user is not a superuser, make them authenticate themselves.
if (auto uid = getuid()) {
auto account_or_error = Core::Account::from_uid(uid);
@ -62,7 +71,7 @@ int main(int argc, char** argv)
return 1;
}
if (setuid(0) < 0) {
if (setuid(as_user_uid) < 0) {
perror("setuid");
return 1;
}