mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +00:00
Utilities: Implement ArmedScopeGuard in userdel to unlink temp files
This commit is contained in:
parent
af8b7248c8
commit
d24dd7a3df
Notes:
sideshowbarker
2024-07-18 17:09:21 +09:00
Author: https://github.com/brapru Commit: https://github.com/SerenityOS/serenity/commit/d24dd7a3dfd Pull-request: https://github.com/SerenityOS/serenity/pull/7556 Issue: https://github.com/SerenityOS/serenity/issues/4884 Reviewed-by: https://github.com/bcoles Reviewed-by: https://github.com/linusg
1 changed files with 27 additions and 38 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue