From 3c76790d512e01a1eef5cd5e38afea66ecec7052 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Wed, 7 Dec 2022 00:14:01 +0100 Subject: [PATCH] userdel: Use `Core::File::remove()` instead of spawning `/bin/rm` --- Userland/Utilities/userdel.cpp | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/Userland/Utilities/userdel.cpp b/Userland/Utilities/userdel.cpp index 9b8e91df0a..0a341b75d1 100644 --- a/Userland/Utilities/userdel.cpp +++ b/Userland/Utilities/userdel.cpp @@ -7,31 +7,23 @@ #include #include -#include #include #include #include #include #include -#include -#include -#include #include #include -#include #include #include #include #include -#include -#include #include ErrorOr serenity_main(Main::Arguments arguments) { - TRY(Core::System::pledge("stdio wpath rpath cpath fattr proc exec")); + TRY(Core::System::pledge("stdio wpath rpath cpath fattr")); TRY(Core::System::unveil("/etc/", "rwc")); - TRY(Core::System::unveil("/bin/rm", "x")); StringView username; bool remove_home = false; @@ -50,11 +42,9 @@ ErrorOr serenity_main(Main::Arguments arguments) auto& target_account = account_or_error.value(); - if (remove_home) { + if (remove_home) TRY(Core::System::unveil(target_account.home_directory(), "c"sv)); - } else { - TRY(Core::System::pledge("stdio wpath rpath cpath fattr")); - } + TRY(Core::System::unveil(nullptr, nullptr)); char temp_passwd[] = "/etc/passwd.XXXXXX"; @@ -153,26 +143,15 @@ ErrorOr serenity_main(Main::Arguments arguments) if (access(target_account.home_directory().characters(), F_OK) == -1) return 0; - DeprecatedString real_path = Core::File::real_path_for(target_account.home_directory()); + auto const real_path = Core::File::real_path_for(target_account.home_directory()); if (real_path == "/") { warnln("home directory is /, not deleted!"); return 12; } - pid_t child; - char const* argv[] = { "rm", "-r", target_account.home_directory().characters(), nullptr }; - if ((errno = posix_spawn(&child, "/bin/rm", nullptr, nullptr, const_cast(argv), environ))) { - perror("posix_spawn"); - return 12; - } - int wstatus; - if (waitpid(child, &wstatus, 0) < 0) { - perror("waitpid"); - return 12; - } - if (WEXITSTATUS(wstatus)) { - warnln("failed to remove the home directory"); + if (auto result = Core::File::remove(real_path, Core::File::RecursionMode::Allowed); result.is_error()) { + warnln("{}", result.release_error()); return 12; } }