summaryrefslogtreecommitdiff
path: root/Userland/Utilities
diff options
context:
space:
mode:
authorbrapru <brapru@pm.me>2021-05-30 10:51:12 -0400
committerLinus Groh <mail@linusgroh.de>2021-05-30 17:42:03 +0100
commitd24dd7a3dfdfda3ceb120c2a7172324edea6444f (patch)
tree272535675a6a3a28af1979fc826d718386fe26bb /Userland/Utilities
parentaf8b7248c877663879e3e114a7150c78e8456fa4 (diff)
downloadserenity-d24dd7a3dfdfda3ceb120c2a7172324edea6444f.zip
Utilities: Implement ArmedScopeGuard in userdel to unlink temp files
Diffstat (limited to 'Userland/Utilities')
-rw-r--r--Userland/Utilities/userdel.cpp65
1 files changed, 27 insertions, 38 deletions
diff --git a/Userland/Utilities/userdel.cpp b/Userland/Utilities/userdel.cpp
index 252f6fd1d1..3009aa226c 100644
--- a/Userland/Utilities/userdel.cpp
+++ b/Userland/Utilities/userdel.cpp
@@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/ScopeGuard.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h>
@@ -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)