diff --git a/Userland/Utilities/userdel.cpp b/Userland/Utilities/userdel.cpp index 252f6fd1d11..3009aa226ca 100644 --- a/Userland/Utilities/userdel.cpp +++ b/Userland/Utilities/userdel.cpp @@ -5,6 +5,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -60,6 +61,17 @@ int main(int argc, char** argv) char temp_passwd[] = "/etc/passwd.XXXXXX"; char temp_shadow[] = "/etc/shadow.XXXXXX"; + auto unlink_temp_files = [&] { + if (unlink(temp_passwd) < 0) + perror("unlink"); + if (unlink(temp_shadow) < 0) + perror("unlink"); + }; + + ArmedScopeGuard unlink_temp_files_guard = [&] { + unlink_temp_files(); + }; + auto temp_passwd_fd = mkstemp(temp_passwd); if (temp_passwd_fd == -1) { perror("failed to create temporary passwd file"); @@ -75,34 +87,24 @@ int main(int argc, char** argv) FILE* temp_passwd_file = fdopen(temp_passwd_fd, "w"); if (!temp_passwd_file) { perror("fdopen"); - if (unlink(temp_passwd) < 0) { - perror("unlink"); - } - return 1; } FILE* temp_shadow_file = fdopen(temp_shadow_fd, "w"); if (!temp_shadow_file) { perror("fdopen"); - if (unlink(temp_shadow) < 0) { - perror("unlink"); - } - return 1; } bool user_exists = false; String home_directory; - int rc = 0; setpwent(); for (auto* pw = getpwent(); pw; pw = getpwent()) { if (strcmp(pw->pw_name, username)) { if (putpwent(pw, temp_passwd_file) != 0) { perror("failed to put an entry in the temporary passwd file"); - rc = 1; - break; + return 1; } } else { user_exists = true; @@ -117,8 +119,7 @@ int main(int argc, char** argv) if (strcmp(spwd->sp_namp, username)) { if (putspent(spwd, temp_shadow_file) != 0) { perror("failed to put an entry in the temporary shadow file"); - rc = 1; - break; + return 1; } } } @@ -126,52 +127,40 @@ int main(int argc, char** argv) if (fclose(temp_passwd_file)) { perror("fclose"); - if (!rc) - rc = 1; + return 1; } if (fclose(temp_shadow_file)) { perror("fclose"); - if (!rc) - rc = 1; + return 1; } - if (rc == 0 && !user_exists) { + if (!user_exists) { warnln("specified user doesn't exist"); - rc = 6; + return 1; } - if (rc == 0 && chmod(temp_passwd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) { + if (chmod(temp_passwd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) { perror("chmod"); - rc = 1; + return 1; } - if (rc == 0 && chmod(temp_shadow, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) { + if (chmod(temp_shadow, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) { perror("chmod"); - rc = 1; + return 1; } - if (rc == 0 && rename(temp_passwd, "/etc/passwd") < 0) { + if (rename(temp_passwd, "/etc/passwd") < 0) { perror("failed to rename the temporary passwd file"); - rc = 1; + return 1; } - if (rc == 0 && rename(temp_shadow, "/etc/shadow") < 0) { + if (rename(temp_shadow, "/etc/shadow") < 0) { perror("failed to rename the temporary shadow file"); - rc = 1; + return 1; } - if (rc) { - if (unlink(temp_passwd) < 0) { - perror("unlink"); - } - - if (unlink(temp_shadow) < 0) { - perror("unlink"); - } - - return rc; - } + unlink_temp_files_guard.disarm(); if (remove_home) { if (access(home_directory.characters(), F_OK) == -1)