mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 16:35:08 +00:00
Utilities: Implement ArmedScopeGuard in userdel to unlink temp files
This commit is contained in:
parent
af8b7248c8
commit
d24dd7a3df
1 changed files with 27 additions and 38 deletions
|
@ -5,6 +5,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/ScopeGuard.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
|
@ -60,6 +61,17 @@ int main(int argc, char** argv)
|
||||||
char temp_passwd[] = "/etc/passwd.XXXXXX";
|
char temp_passwd[] = "/etc/passwd.XXXXXX";
|
||||||
char temp_shadow[] = "/etc/shadow.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);
|
auto temp_passwd_fd = mkstemp(temp_passwd);
|
||||||
if (temp_passwd_fd == -1) {
|
if (temp_passwd_fd == -1) {
|
||||||
perror("failed to create temporary passwd file");
|
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");
|
FILE* temp_passwd_file = fdopen(temp_passwd_fd, "w");
|
||||||
if (!temp_passwd_file) {
|
if (!temp_passwd_file) {
|
||||||
perror("fdopen");
|
perror("fdopen");
|
||||||
if (unlink(temp_passwd) < 0) {
|
|
||||||
perror("unlink");
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* temp_shadow_file = fdopen(temp_shadow_fd, "w");
|
FILE* temp_shadow_file = fdopen(temp_shadow_fd, "w");
|
||||||
if (!temp_shadow_file) {
|
if (!temp_shadow_file) {
|
||||||
perror("fdopen");
|
perror("fdopen");
|
||||||
if (unlink(temp_shadow) < 0) {
|
|
||||||
perror("unlink");
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool user_exists = false;
|
bool user_exists = false;
|
||||||
String home_directory;
|
String home_directory;
|
||||||
|
|
||||||
int rc = 0;
|
|
||||||
setpwent();
|
setpwent();
|
||||||
for (auto* pw = getpwent(); pw; pw = getpwent()) {
|
for (auto* pw = getpwent(); pw; pw = getpwent()) {
|
||||||
if (strcmp(pw->pw_name, username)) {
|
if (strcmp(pw->pw_name, username)) {
|
||||||
if (putpwent(pw, temp_passwd_file) != 0) {
|
if (putpwent(pw, temp_passwd_file) != 0) {
|
||||||
perror("failed to put an entry in the temporary passwd file");
|
perror("failed to put an entry in the temporary passwd file");
|
||||||
rc = 1;
|
return 1;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
user_exists = true;
|
user_exists = true;
|
||||||
|
@ -117,8 +119,7 @@ int main(int argc, char** argv)
|
||||||
if (strcmp(spwd->sp_namp, username)) {
|
if (strcmp(spwd->sp_namp, username)) {
|
||||||
if (putspent(spwd, temp_shadow_file) != 0) {
|
if (putspent(spwd, temp_shadow_file) != 0) {
|
||||||
perror("failed to put an entry in the temporary shadow file");
|
perror("failed to put an entry in the temporary shadow file");
|
||||||
rc = 1;
|
return 1;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,52 +127,40 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
if (fclose(temp_passwd_file)) {
|
if (fclose(temp_passwd_file)) {
|
||||||
perror("fclose");
|
perror("fclose");
|
||||||
if (!rc)
|
return 1;
|
||||||
rc = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fclose(temp_shadow_file)) {
|
if (fclose(temp_shadow_file)) {
|
||||||
perror("fclose");
|
perror("fclose");
|
||||||
if (!rc)
|
return 1;
|
||||||
rc = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rc == 0 && !user_exists) {
|
if (!user_exists) {
|
||||||
warnln("specified user doesn't exist");
|
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");
|
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");
|
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");
|
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");
|
perror("failed to rename the temporary shadow file");
|
||||||
rc = 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rc) {
|
unlink_temp_files_guard.disarm();
|
||||||
if (unlink(temp_passwd) < 0) {
|
|
||||||
perror("unlink");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (unlink(temp_shadow) < 0) {
|
|
||||||
perror("unlink");
|
|
||||||
}
|
|
||||||
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (remove_home) {
|
if (remove_home) {
|
||||||
if (access(home_directory.characters(), F_OK) == -1)
|
if (access(home_directory.characters(), F_OK) == -1)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue