ladybird/Userland/Utilities/userdel.cpp

65 lines
1.8 KiB
C++
Raw Normal View History

2020-02-19 11:59:09 +00:00
/*
* Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
* Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
2020-02-19 11:59:09 +00:00
*
* SPDX-License-Identifier: BSD-2-Clause
2020-02-19 11:59:09 +00:00
*/
#include <AK/DeprecatedString.h>
#include <LibCore/Account.h>
2020-02-19 11:59:09 +00:00
#include <LibCore/ArgsParser.h>
#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
{
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
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
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)
TRY(Core::System::unveil(target_account.home_directory(), "c"sv));
2021-11-23 16:18:47 +00:00
TRY(Core::System::unveil(nullptr, nullptr));
target_account.set_deleted();
TRY(target_account.sync());
2020-02-19 11:59:09 +00:00
if (remove_home) {
if (access(target_account.home_directory().characters(), F_OK) == -1)
return 0;
auto const real_path = Core::DeprecatedFile::real_path_for(target_account.home_directory());
if (real_path == "/") {
warnln("home directory is /, not deleted!");
2020-02-19 11:59:09 +00:00
return 12;
}
if (auto result = Core::DeprecatedFile::remove(real_path, Core::DeprecatedFile::RecursionMode::Allowed); result.is_error()) {
warnln("{}", result.release_error());
return 12;
2020-02-19 11:59:09 +00:00
}
}
return 0;
}