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