diff options
author | Brendan Coles <bcoles@gmail.com> | 2020-12-21 10:41:08 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-21 12:23:50 +0100 |
commit | a3fdf5148bb8f7760eea863eb13cb9264da054a6 (patch) | |
tree | 9cedec6e0d73fe12b30330d9681e06a0ff68ba0a | |
parent | 58c52b155a782c50684e2cbcf971fd879e0fbdb6 (diff) | |
download | serenity-a3fdf5148bb8f7760eea863eb13cb9264da054a6.zip |
Userland: userdel: Resolve home directory realpath before removal
-rw-r--r-- | Userland/userdel.cpp | 38 |
1 files 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 <AK/String.h> #include <AK/StringBuilder.h> #include <LibCore/ArgsParser.h> +#include <LibCore/File.h> #include <ctype.h> #include <dirent.h> #include <errno.h> @@ -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<char**>(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<char**>(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; } } |