From a3fdf5148bb8f7760eea863eb13cb9264da054a6 Mon Sep 17 00:00:00 2001 From: Brendan Coles Date: Mon, 21 Dec 2020 10:41:08 +0000 Subject: Userland: userdel: Resolve home directory realpath before removal --- Userland/userdel.cpp | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/Userland/userdel.cpp b/Userland/userdel.cpp index e310fc14a8..a6385258d3 100644 --- a/Userland/userdel.cpp +++ b/Userland/userdel.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -116,27 +117,30 @@ int main(int argc, char** argv) } if (remove_home) { - if (home_directory == "/") { + if (access(home_directory.characters(), F_OK) == -1) + return 0; + + String real_path = Core::File::real_path_for(home_directory); + + if (real_path == "/") { fprintf(stderr, "home directory is /, not deleted!\n"); return 12; } - if (access(home_directory.characters(), F_OK) != -1) { - pid_t child; - const char* argv[] = { "rm", "-r", 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)) { - fprintf(stderr, "failed to remove the home directory\n"); - return 12; - } + pid_t child; + const char* argv[] = { "rm", "-r", 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)) { + fprintf(stderr, "failed to remove the home directory\n"); + return 12; } } -- cgit v1.2.3