2020-02-19 11:59:09 +00:00
|
|
|
/*
|
2020-03-02 13:23:54 +00:00
|
|
|
* Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
|
2021-05-29 12:36:08 +00:00
|
|
|
* Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
|
2020-02-19 11:59:09 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-19 11:59:09 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2021-06-01 10:46:57 +00:00
|
|
|
#include <LibCore/Account.h>
|
2020-02-19 11:59:09 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-08 20:08:01 +00:00
|
|
|
#include <LibCore/DeprecatedFile.h>
|
2021-11-23 16:18:47 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-02-19 11:59:09 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-11-23 16:18:47 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-02-19 11:59:09 +00:00
|
|
|
{
|
2022-12-06 23:14:01 +00:00
|
|
|
TRY(Core::System::pledge("stdio wpath rpath cpath fattr"));
|
2021-11-23 16:18:47 +00:00
|
|
|
TRY(Core::System::unveil("/etc/", "rwc"));
|
2021-01-12 06:39:08 +00:00
|
|
|
|
2022-06-10 18:06:06 +00:00
|
|
|
StringView username;
|
2020-02-19 11:59:09 +00: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 16:18:47 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-02-19 11:59:09 +00:00
|
|
|
|
2021-06-01 10:46:57 +00: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-06 23:14:01 +00:00
|
|
|
if (remove_home)
|
2022-07-11 17:32:29 +00:00
|
|
|
TRY(Core::System::unveil(target_account.home_directory(), "c"sv));
|
2022-12-06 23:14:01 +00:00
|
|
|
|
2021-11-23 16:18:47 +00:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-06-01 10:46:57 +00:00
|
|
|
|
2022-12-06 23:51:11 +00:00
|
|
|
target_account.set_deleted();
|
|
|
|
TRY(target_account.sync());
|
2020-02-19 11:59:09 +00:00
|
|
|
|
|
|
|
if (remove_home) {
|
2021-06-01 10:46:57 +00:00
|
|
|
if (access(target_account.home_directory().characters(), F_OK) == -1)
|
2020-12-21 10:41:08 +00:00
|
|
|
return 0;
|
|
|
|
|
2023-02-08 20:08:01 +00:00
|
|
|
auto const real_path = Core::DeprecatedFile::real_path_for(target_account.home_directory());
|
2020-12-21 10:41:08 +00:00
|
|
|
|
|
|
|
if (real_path == "/") {
|
2021-05-30 13:05:34 +00:00
|
|
|
warnln("home directory is /, not deleted!");
|
2020-02-19 11:59:09 +00:00
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:08:01 +00:00
|
|
|
if (auto result = Core::DeprecatedFile::remove(real_path, Core::DeprecatedFile::RecursionMode::Allowed); result.is_error()) {
|
2022-12-06 23:14:01 +00:00
|
|
|
warnln("{}", result.release_error());
|
2020-12-21 10:41:08 +00:00
|
|
|
return 12;
|
2020-02-19 11:59:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|