LibCore+id: Make more use of Core::System wrappers in Core::Account

This commit is contained in:
Andreas Kling 2022-01-01 18:48:09 +01:00
parent 431bd069f0
commit c6ce606e47
Notes: sideshowbarker 2024-07-17 21:47:22 +09:00
3 changed files with 39 additions and 74 deletions

View file

@ -64,96 +64,61 @@ ErrorOr<Account> Account::from_passwd(const passwd& pwd, const spwd& spwd)
return account;
}
Account Account::self(Read options)
ErrorOr<Account> Account::self(Read options)
{
struct passwd fallback;
fallback.pw_name = const_cast<char*>("(unknown)");
fallback.pw_uid = getuid();
fallback.pw_gid = getgid();
fallback.pw_gecos = const_cast<char*>("");
fallback.pw_dir = const_cast<char*>("(unknown)");
fallback.pw_shell = const_cast<char*>("(unknown)");
Vector<gid_t> extra_gids = TRY(Core::System::getgroups());
Vector<gid_t> extra_gids;
int extra_gid_count = getgroups(0, nullptr);
if (extra_gid_count) {
extra_gids.resize(extra_gid_count);
int rc = getgroups(extra_gid_count, extra_gids.data());
if (rc < 0)
extra_gids.resize(0);
}
auto pwd = TRY(Core::System::getpwuid(getuid()));
if (!pwd.has_value())
return Error::from_string_literal("No such user"sv);
struct passwd* pwd = getpwuid(fallback.pw_uid);
if (!pwd)
pwd = &fallback;
else
pwd->pw_gid = fallback.pw_gid;
spwd spwd_dummy = {};
spwd_dummy.sp_namp = pwd->pw_name;
spwd_dummy.sp_pwdp = const_cast<char*>("");
spwd spwd = {};
#ifndef AK_OS_BSD_GENERIC
spwd* spwd = nullptr;
if (options != Read::PasswdOnly)
spwd = getspnam(pwd->pw_name);
if (!spwd)
spwd = &spwd_dummy;
#else
(void)options;
auto* spwd = &spwd_dummy;
if (options != Read::PasswdOnly) {
auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name));
if (!maybe_spwd.has_value())
return Error::from_string_literal("No shadow entry for user"sv);
spwd = maybe_spwd.release_value();
}
#endif
return Account(*pwd, *spwd, extra_gids);
return Account(*pwd, spwd, extra_gids);
}
ErrorOr<Account> Account::from_name(const char* username, Read options)
{
errno = 0;
auto* pwd = getpwnam(username);
if (!pwd) {
if (errno == 0)
return Error::from_string_literal("No such user"sv);
return Error::from_errno(errno);
}
spwd spwd_dummy = {};
spwd_dummy.sp_namp = const_cast<char*>(username);
spwd_dummy.sp_pwdp = const_cast<char*>("");
auto pwd = TRY(Core::System::getpwnam(username));
if (!pwd.has_value())
return Error::from_string_literal("No such user"sv);
spwd spwd = {};
#ifndef AK_OS_BSD_GENERIC
spwd* spwd = nullptr;
if (options != Read::PasswdOnly)
spwd = getspnam(pwd->pw_name);
if (!spwd)
spwd = &spwd_dummy;
#else
(void)options;
auto* spwd = &spwd_dummy;
if (options != Read::PasswdOnly) {
auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name));
if (!maybe_spwd.has_value())
return Error::from_string_literal("No shadow entry for user"sv);
spwd = maybe_spwd.release_value();
}
#endif
return from_passwd(*pwd, *spwd);
return from_passwd(*pwd, spwd);
}
ErrorOr<Account> Account::from_uid(uid_t uid, Read options)
{
errno = 0;
auto* pwd = getpwuid(uid);
if (!pwd) {
if (errno == 0)
return Error::from_string_literal("No such user"sv);
return Error::from_errno(errno);
}
spwd spwd_dummy = {};
spwd_dummy.sp_namp = pwd->pw_name;
spwd_dummy.sp_pwdp = const_cast<char*>("");
auto pwd = TRY(Core::System::getpwuid(uid));
if (!pwd.has_value())
return Error::from_string_literal("No such user"sv);
spwd spwd = {};
#ifndef AK_OS_BSD_GENERIC
spwd* spwd = nullptr;
if (options != Read::PasswdOnly)
spwd = getspnam(pwd->pw_name);
if (!spwd)
spwd = &spwd_dummy;
#else
(void)options;
auto* spwd = &spwd_dummy;
if (options != Read::PasswdOnly) {
auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name));
if (!maybe_spwd.has_value())
return Error::from_string_literal("No shadow entry for user"sv);
spwd = maybe_spwd.release_value();
}
#endif
return from_passwd(*pwd, *spwd);
return from_passwd(*pwd, spwd);
}
bool Account::authenticate(SecretString const& password) const

View file

@ -32,7 +32,7 @@ public:
PasswdOnly
};
static Account self(Read options = Read::All);
static ErrorOr<Account> self(Read options = Read::All);
static ErrorOr<Account> from_name(char const* username, Read options = Read::All);
static ErrorOr<Account> from_uid(uid_t uid, Read options = Read::All);

View file

@ -55,7 +55,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
else
account = TRY(Core::Account::from_name(user_str.characters(), Core::Account::Read::PasswdOnly));
} else {
account = Core::Account::self(Core::Account::Read::PasswdOnly);
account = TRY(Core::Account::self(Core::Account::Read::PasswdOnly));
}
return print_id_objects(account.value());