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
|
|
|
*/
|
|
|
|
|
2021-06-01 06:46:57 -04:00
|
|
|
#include <LibCore/Account.h>
|
2020-02-19 19:59:09 +08:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-11-23 17:18:47 +01:00
|
|
|
#include <LibCore/System.h>
|
2023-03-22 02:35:30 +11:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2021-11-23 17:18:47 +01:00
|
|
|
#include <LibMain/Main.h>
|
2020-02-19 19:59:09 +08:00
|
|
|
#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
|
|
|
{
|
2022-12-07 00:14:01 +01:00
|
|
|
TRY(Core::System::pledge("stdio wpath rpath cpath fattr"));
|
2021-11-23 17:18:47 +01:00
|
|
|
TRY(Core::System::unveil("/etc/", "rwc"));
|
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();
|
|
|
|
|
2022-12-07 00:14:01 +01:00
|
|
|
if (remove_home)
|
2023-05-20 10:55:25 +02:00
|
|
|
TRY(Core::System::unveil(target_account.home_directory(), "r"sv));
|
2022-12-07 00:14:01 +01:00
|
|
|
|
2021-11-23 17:18:47 +01:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-06-01 06:46:57 -04:00
|
|
|
|
2022-12-07 00:51:11 +01:00
|
|
|
target_account.set_deleted();
|
|
|
|
TRY(target_account.sync());
|
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;
|
|
|
|
|
2023-05-20 10:35:42 +02:00
|
|
|
auto const real_path = TRY(FileSystem::real_path(target_account.home_directory()));
|
2020-12-21 10:41:08 +00:00
|
|
|
|
2023-05-20 10:35:42 +02:00
|
|
|
if (real_path == "/"sv) {
|
2021-05-30 14:05:34 +01:00
|
|
|
warnln("home directory is /, not deleted!");
|
2020-02-19 19:59:09 +08:00
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
2023-03-22 02:35:30 +11:00
|
|
|
if (auto result = FileSystem::remove(real_path, FileSystem::RecursionMode::Allowed); result.is_error()) {
|
2022-12-07 00:14:01 +01:00
|
|
|
warnln("{}", result.release_error());
|
2020-12-21 10:41:08 +00:00
|
|
|
return 12;
|
2020-02-19 19:59:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|