2021-07-09 15:37:48 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020, Jesse Buhagiar <jooster669@gmail.com>
|
|
|
|
* Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
|
|
|
|
* Copyright (c) 2021, Maxime Friess <M4x1me@pm.me>
|
2022-06-30 17:19:03 +03:00
|
|
|
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2023-06-19 09:09:55 +03:00
|
|
|
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
|
2021-07-09 15:37:48 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-06-19 09:09:55 +03:00
|
|
|
#include <AK/HashTable.h>
|
|
|
|
#include <LibCore/Account.h>
|
2021-07-09 15:37:48 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-01-03 00:54:06 +03:00
|
|
|
#include <LibCore/Group.h>
|
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-07-09 15:37:48 +03:00
|
|
|
|
2022-01-03 00:54:06 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-07-09 15:37:48 +03:00
|
|
|
{
|
2022-01-03 00:54:06 +03:00
|
|
|
TRY(Core::System::pledge("stdio wpath rpath cpath chown"));
|
2021-07-09 15:37:48 +03:00
|
|
|
|
2022-01-03 00:54:06 +03:00
|
|
|
gid_t gid = 0;
|
|
|
|
StringView group_name;
|
2023-06-19 09:09:55 +03:00
|
|
|
Vector<DeprecatedString> group_members;
|
2021-07-09 15:37:48 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(gid, "Group ID (gid) for the new group", "gid", 'g', "gid");
|
2023-06-19 09:09:55 +03:00
|
|
|
args_parser.add_option(Core::ArgsParser::Option {
|
|
|
|
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
|
|
|
|
.help_string = "A comma-separated list of usernames to add as members of the new group",
|
|
|
|
.long_name = "users",
|
|
|
|
.short_name = 'U',
|
|
|
|
.value_name = "user-list",
|
|
|
|
.accept_value = [&group_members](StringView comma_separated_users) {
|
|
|
|
auto accounts_or_error = Core::Account::all(Core::Account::Read::PasswdOnly);
|
|
|
|
if (accounts_or_error.is_error())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
OrderedHashTable<DeprecatedString> unique_group_members;
|
|
|
|
auto accounts = accounts_or_error.release_value();
|
|
|
|
for (auto const& username : comma_separated_users.split_view(',')) {
|
|
|
|
auto matching_account = accounts.first_matching([&](auto const& account) { return username == account.username(); });
|
|
|
|
if (!matching_account.has_value()) {
|
|
|
|
warnln("Invalid member username: '{}'", username);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_group_members.set(matching_account->username());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& member : unique_group_members)
|
|
|
|
group_members.append(member);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
2022-01-03 00:54:06 +03:00
|
|
|
args_parser.add_positional_argument(group_name, "Name of the group (groupname)", "group");
|
|
|
|
args_parser.parse(arguments);
|
2021-07-09 15:37:48 +03:00
|
|
|
|
2023-06-19 09:09:55 +03:00
|
|
|
Core::Group group { group_name, gid, group_members };
|
2022-01-03 00:54:06 +03:00
|
|
|
TRY(Core::Group::add_group(group));
|
2021-07-09 15:37:48 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|