1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:57:35 +00:00

Userland: userdel: Resolve home directory realpath before removal

This commit is contained in:
Brendan Coles 2020-12-21 10:41:08 +00:00 committed by Andreas Kling
parent 58c52b155a
commit a3fdf5148b

View file

@ -27,6 +27,7 @@
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <ctype.h> #include <ctype.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
@ -116,27 +117,30 @@ int main(int argc, char** argv)
} }
if (remove_home) { 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"); fprintf(stderr, "home directory is /, not deleted!\n");
return 12; return 12;
} }
if (access(home_directory.characters(), F_OK) != -1) { pid_t child;
pid_t child; const char* argv[] = { "rm", "-r", home_directory.characters(), nullptr };
const char* argv[] = { "rm", "-r", home_directory.characters(), nullptr }; if ((errno = posix_spawn(&child, "/bin/rm", nullptr, nullptr, const_cast<char**>(argv), environ))) {
if ((errno = posix_spawn(&child, "/bin/rm", nullptr, nullptr, const_cast<char**>(argv), environ))) { perror("posix_spawn");
perror("posix_spawn"); return 12;
return 12; }
} int wstatus;
int wstatus; if (waitpid(child, &wstatus, 0) < 0) {
if (waitpid(child, &wstatus, 0) < 0) { perror("waitpid");
perror("waitpid"); return 12;
return 12; }
} if (WEXITSTATUS(wstatus)) {
if (WEXITSTATUS(wstatus)) { fprintf(stderr, "failed to remove the home directory\n");
fprintf(stderr, "failed to remove the home directory\n"); return 12;
return 12;
}
} }
} }