From 68d07320cf0df9603fba26e1c19e533d29772bd2 Mon Sep 17 00:00:00 2001 From: pyunbiwi Date: Mon, 16 Aug 2021 03:58:34 +0100 Subject: [PATCH] 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. --- Userland/Utilities/pls.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Userland/Utilities/pls.cpp b/Userland/Utilities/pls.cpp index 4eb2ab59f6..a54e76711a 100644 --- a/Userland/Utilities/pls.cpp +++ b/Userland/Utilities/pls.cpp @@ -15,6 +15,8 @@ int main(int argc, char** argv) { Vector 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; }