2020-02-19 19:59:09 +08:00
|
|
|
/*
|
2020-03-02 14:23:54 +01:00
|
|
|
* Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
|
2021-05-29 08:36:08 -04:00
|
|
|
* Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
|
2020-02-19 19:59:09 +08:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-19 19:59:09 +08:00
|
|
|
*/
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2021-05-30 10:51:12 -04:00
|
|
|
#include <AK/ScopeGuard.h>
|
2020-02-19 19:59:09 +08:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-06-01 06:46:57 -04:00
|
|
|
#include <LibCore/Account.h>
|
2020-02-19 19:59:09 +08:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-12-21 10:41:08 +00:00
|
|
|
#include <LibCore/File.h>
|
2021-11-23 17:18:47 +01:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
#include <LibMain/Main.h>
|
2020-02-19 19:59:09 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <pwd.h>
|
2021-05-29 08:36:08 -04:00
|
|
|
#include <shadow.h>
|
2020-06-28 13:40:10 -04:00
|
|
|
#include <spawn.h>
|
2020-02-19 19:59:09 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2021-11-23 17:18:47 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-02-19 19:59:09 +08:00
|
|
|
{
|
2021-11-27 14:26:34 -08:00
|
|
|
TRY(Core::System::pledge("stdio wpath rpath cpath fattr proc exec"));
|
2021-11-23 17:18:47 +01:00
|
|
|
TRY(Core::System::unveil("/etc/", "rwc"));
|
|
|
|
|
TRY(Core::System::unveil("/bin/rm", "x"));
|
2021-01-12 06:39:08 +00:00
|
|
|
|
2022-06-10 20:06:06 +02:00
|
|
|
StringView username;
|
2020-02-19 19:59:09 +08:00
|
|
|
bool remove_home = false;
|
|
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
|
args_parser.add_option(remove_home, "Remove home directory", "remove", 'r');
|
|
|
|
|
args_parser.add_positional_argument(username, "Login user identity (username)", "login");
|
2021-11-23 17:18:47 +01:00
|
|
|
args_parser.parse(arguments);
|
2020-02-19 19:59:09 +08:00
|
|
|
|
2021-06-01 06:46:57 -04:00
|
|
|
auto account_or_error = Core::Account::from_name(username);
|
|
|
|
|
|
|
|
|
|
if (account_or_error.is_error()) {
|
|
|
|
|
warnln("Core::Account::from_name: {}", account_or_error.error());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto& target_account = account_or_error.value();
|
|
|
|
|
|
|
|
|
|
if (remove_home) {
|
2022-07-11 17:32:29 +00:00
|
|
|
TRY(Core::System::unveil(target_account.home_directory(), "c"sv));
|
2021-06-01 06:46:57 -04:00
|
|
|
} else {
|
2021-11-27 14:26:34 -08:00
|
|
|
TRY(Core::System::pledge("stdio wpath rpath cpath fattr"));
|
2021-01-12 06:39:08 +00:00
|
|
|
}
|
2021-11-23 17:18:47 +01:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-06-01 06:46:57 -04:00
|
|
|
|
2021-05-29 08:36:08 -04:00
|
|
|
char temp_passwd[] = "/etc/passwd.XXXXXX";
|
|
|
|
|
char temp_shadow[] = "/etc/shadow.XXXXXX";
|
|
|
|
|
|
2021-05-30 10:51:12 -04:00
|
|
|
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();
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-29 08:36:08 -04:00
|
|
|
auto temp_passwd_fd = mkstemp(temp_passwd);
|
|
|
|
|
if (temp_passwd_fd == -1) {
|
|
|
|
|
perror("failed to create temporary passwd file");
|
2020-02-19 19:59:09 +08:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-29 08:36:08 -04:00
|
|
|
auto temp_shadow_fd = mkstemp(temp_shadow);
|
|
|
|
|
if (temp_shadow_fd == -1) {
|
|
|
|
|
perror("failed to create temporary shadow file");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE* temp_passwd_file = fdopen(temp_passwd_fd, "w");
|
|
|
|
|
if (!temp_passwd_file) {
|
2020-02-19 19:59:09 +08:00
|
|
|
perror("fdopen");
|
2021-05-29 08:36:08 -04:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE* temp_shadow_file = fdopen(temp_shadow_fd, "w");
|
|
|
|
|
if (!temp_shadow_file) {
|
|
|
|
|
perror("fdopen");
|
2020-02-19 19:59:09 +08:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setpwent();
|
|
|
|
|
for (auto* pw = getpwent(); pw; pw = getpwent()) {
|
2021-06-01 06:46:57 -04:00
|
|
|
if (strcmp(pw->pw_name, target_account.username().characters())) {
|
2021-05-29 08:36:08 -04:00
|
|
|
if (putpwent(pw, temp_passwd_file) != 0) {
|
2020-02-19 19:59:09 +08:00
|
|
|
perror("failed to put an entry in the temporary passwd file");
|
2021-05-30 10:51:12 -04:00
|
|
|
return 1;
|
2020-02-19 19:59:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
endpwent();
|
|
|
|
|
|
2021-05-29 08:36:08 -04:00
|
|
|
setspent();
|
|
|
|
|
for (auto* spwd = getspent(); spwd; spwd = getspent()) {
|
2021-06-01 06:46:57 -04:00
|
|
|
if (strcmp(spwd->sp_namp, target_account.username().characters())) {
|
2021-05-29 08:36:08 -04:00
|
|
|
if (putspent(spwd, temp_shadow_file) != 0) {
|
|
|
|
|
perror("failed to put an entry in the temporary shadow file");
|
2021-05-30 10:51:12 -04:00
|
|
|
return 1;
|
2021-05-29 08:36:08 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
endspent();
|
|
|
|
|
|
|
|
|
|
if (fclose(temp_passwd_file)) {
|
|
|
|
|
perror("fclose");
|
2021-05-30 10:51:12 -04:00
|
|
|
return 1;
|
2021-05-29 08:36:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fclose(temp_shadow_file)) {
|
2020-02-19 19:59:09 +08:00
|
|
|
perror("fclose");
|
2021-05-30 10:51:12 -04:00
|
|
|
return 1;
|
2020-02-19 19:59:09 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 10:51:12 -04:00
|
|
|
if (chmod(temp_passwd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) {
|
2021-05-29 08:36:08 -04:00
|
|
|
perror("chmod");
|
2021-05-30 10:51:12 -04:00
|
|
|
return 1;
|
2021-05-29 08:36:08 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 10:51:12 -04:00
|
|
|
if (chmod(temp_shadow, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) {
|
2020-02-19 19:59:09 +08:00
|
|
|
perror("chmod");
|
2021-05-30 10:51:12 -04:00
|
|
|
return 1;
|
2020-02-19 19:59:09 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 10:51:12 -04:00
|
|
|
if (rename(temp_passwd, "/etc/passwd") < 0) {
|
2020-02-19 19:59:09 +08:00
|
|
|
perror("failed to rename the temporary passwd file");
|
2021-05-30 10:51:12 -04:00
|
|
|
return 1;
|
2020-02-19 19:59:09 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 10:51:12 -04:00
|
|
|
if (rename(temp_shadow, "/etc/shadow") < 0) {
|
2021-05-29 08:36:08 -04:00
|
|
|
perror("failed to rename the temporary shadow file");
|
2021-05-30 10:51:12 -04:00
|
|
|
return 1;
|
2021-05-29 08:36:08 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 10:51:12 -04:00
|
|
|
unlink_temp_files_guard.disarm();
|
2020-02-19 19:59:09 +08:00
|
|
|
|
|
|
|
|
if (remove_home) {
|
2021-06-01 06:46:57 -04:00
|
|
|
if (access(target_account.home_directory().characters(), F_OK) == -1)
|
2020-12-21 10:41:08 +00:00
|
|
|
return 0;
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString real_path = Core::File::real_path_for(target_account.home_directory());
|
2020-12-21 10:41:08 +00:00
|
|
|
|
|
|
|
|
if (real_path == "/") {
|
2021-05-30 14:05:34 +01:00
|
|
|
warnln("home directory is /, not deleted!");
|
2020-02-19 19:59:09 +08:00
|
|
|
return 12;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 10:41:08 +00:00
|
|
|
pid_t child;
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* argv[] = { "rm", "-r", target_account.home_directory().characters(), nullptr };
|
2020-12-21 10:41:08 +00:00
|
|
|
if ((errno = posix_spawn(&child, "/bin/rm", nullptr, nullptr, const_cast<char**>(argv), environ))) {
|
|
|
|
|
perror("posix_spawn");
|
|
|
|
|
return 12;
|
|
|
|
|
}
|
|
|
|
|
int wstatus;
|
|
|
|
|
if (waitpid(child, &wstatus, 0) < 0) {
|
|
|
|
|
perror("waitpid");
|
|
|
|
|
return 12;
|
|
|
|
|
}
|
|
|
|
|
if (WEXITSTATUS(wstatus)) {
|
2021-05-30 14:05:34 +01:00
|
|
|
warnln("failed to remove the home directory");
|
2020-12-21 10:41:08 +00:00
|
|
|
return 12;
|
2020-02-19 19:59:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|