2021-05-05 11:33:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/Account.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-12-02 23:31:50 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-05-05 11:33:00 +00:00
|
|
|
#include <grp.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static void print_account_gids(Core::Account const& account)
|
2021-05-05 11:33:00 +00:00
|
|
|
{
|
|
|
|
auto* gr = getgrgid(account.gid());
|
|
|
|
if (!gr) {
|
|
|
|
outln();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
out("{}", gr->gr_name);
|
|
|
|
for (auto& gid : account.extra_gids()) {
|
|
|
|
gr = getgrgid(gid);
|
|
|
|
out(" {}", gr->gr_name);
|
|
|
|
}
|
|
|
|
outln();
|
|
|
|
}
|
|
|
|
|
2021-12-02 23:31:50 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-05-05 11:33:00 +00:00
|
|
|
{
|
2021-12-02 23:31:50 +00:00
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
|
|
|
TRY(Core::System::unveil("/etc/group", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2022-04-03 23:16:06 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2021-05-05 11:33:00 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
Vector<ByteString> usernames;
|
2021-05-05 11:33:00 +00:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("Print group memberships for each username or, if no username is specified, for the current process.");
|
|
|
|
args_parser.add_positional_argument(usernames, "Usernames to list group memberships for", "usernames", Core::ArgsParser::Required::No);
|
2021-12-02 23:31:50 +00:00
|
|
|
args_parser.parse(arguments);
|
2021-05-05 11:33:00 +00:00
|
|
|
|
|
|
|
if (usernames.is_empty()) {
|
2022-01-21 18:31:43 +00:00
|
|
|
auto account = TRY(Core::Account::from_uid(geteuid(), Core::Account::Read::PasswdOnly));
|
2021-12-02 23:31:50 +00:00
|
|
|
print_account_gids(account);
|
2021-05-05 11:33:00 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 18:06:06 +00:00
|
|
|
for (auto const& username : usernames) {
|
|
|
|
auto result = Core::Account::from_name(username, Core::Account::Read::PasswdOnly);
|
2021-05-05 11:33:00 +00:00
|
|
|
if (result.is_error()) {
|
|
|
|
warnln("{} '{}'", result.error(), username);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
out("{} : ", username);
|
|
|
|
print_account_gids(result.value());
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|