diff options
author | pyunbiwi <adamwisniowski2006@gmail.com> | 2021-08-16 03:58:34 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-16 21:16:30 +0200 |
commit | 68d07320cf0df9603fba26e1c19e533d29772bd2 (patch) | |
tree | 2be86debaa3fcfa5a0627d90dd12710b11038bc3 /Userland/Utilities | |
parent | 64bc5f668dad954f1dd65ec6a1b98d312af16e96 (diff) | |
download | serenity-68d07320cf0df9603fba26e1c19e533d29772bd2.zip |
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.
Diffstat (limited to 'Userland/Utilities')
-rw-r--r-- | Userland/Utilities/pls.cpp | 11 |
1 files changed, 10 insertions, 1 deletions
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<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; } |