From 9e9662b695b8c38a57e84de5d9fe8495296c618e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 16 Dec 2021 19:18:37 +0100 Subject: [PATCH] pls: Port to LibMain :^) --- Userland/Utilities/CMakeLists.txt | 1 + Userland/Utilities/pls.cpp | 81 +++++++++---------------------- 2 files changed, 23 insertions(+), 59 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 999c54c7cf..139651a5c6 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -118,6 +118,7 @@ target_link_libraries(open LibDesktop) target_link_libraries(pape LibGUI) target_link_libraries(passwd LibCrypt LibMain) target_link_libraries(paste LibGUI) +target_link_libraries(pls LibMain) target_link_libraries(pgrep LibRegex) target_link_libraries(pls LibCrypt) target_link_libraries(pmap LibMain) diff --git a/Userland/Utilities/pls.cpp b/Userland/Utilities/pls.cpp index 5e88a4c633..e66758e072 100644 --- a/Userland/Utilities/pls.cpp +++ b/Userland/Utilities/pls.cpp @@ -5,98 +5,61 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include #include #include #include +#include +#include #include #include -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { 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); + args_parser.parse(arguments); - if (pledge("stdio rpath exec id tty", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio rpath exec id tty")); - if (seteuid(0) < 0) { - perror("seteuid"); - return 1; - } + TRY(Core::System::seteuid(0)); - // 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; - } + auto as_user = TRY(Core::Account::from_uid(as_user_uid)); // 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); - if (account_or_error.is_error()) { - warnln("{}", account_or_error.error()); - return 1; - } - - auto const& account = account_or_error.value(); + auto account = TRY(Core::Account::from_uid(uid)); if (account.has_password()) { - auto password_or_error = Core::get_password(); - if (password_or_error.is_error()) { - warnln("{}", password_or_error.error()); - return 1; - } - - auto const& password = password_or_error.value(); - if (!account.authenticate(password)) { - warnln("Incorrect or disabled password."); - return 1; - } + auto password = TRY(Core::get_password()); + if (!account.authenticate(password)) + return Error::from_string_literal("Incorrect or disabled password."sv); } } - if (pledge("stdio rpath exec id", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio rpath exec id")); - if (setgid(0) < 0) { - perror("setgid"); - return 1; - } + TRY(Core::System::setgid(0)); + TRY(Core::System::setuid(as_user_uid)); - if (setuid(as_user_uid) < 0) { - perror("setuid"); - return 1; - } + TRY(Core::System::pledge("stdio rpath exec")); - if (pledge("stdio rpath exec", nullptr) < 0) { - perror("pledge"); - return 1; - } - - Vector arguments; + Vector exec_arguments; for (auto const& arg : command) - arguments.append(arg); - arguments.append(nullptr); + exec_arguments.append(arg); + exec_arguments.append(nullptr); Vector environment_strings; if (auto* term = getenv("TERM")) environment_strings.append(String::formatted("TERM={}", term)); - Vector environment; + Vector exec_environment; for (auto& item : environment_strings) - environment.append(item.characters()); - environment.append(nullptr); + exec_environment.append(item.characters()); + exec_environment.append(nullptr); - if (execvpe(command.at(0), const_cast(arguments.data()), const_cast(environment.data())) < 0) { + if (execvpe(command.at(0), const_cast(exec_arguments.data()), const_cast(exec_environment.data())) < 0) { perror("execvpe"); exit(1); }