mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
LibCore+id: Make more use of Core::System wrappers in Core::Account
This commit is contained in:
parent
431bd069f0
commit
c6ce606e47
3 changed files with 39 additions and 74 deletions
|
@ -64,96 +64,61 @@ ErrorOr<Account> Account::from_passwd(const passwd& pwd, const spwd& spwd)
|
||||||
return account;
|
return account;
|
||||||
}
|
}
|
||||||
|
|
||||||
Account Account::self(Read options)
|
ErrorOr<Account> Account::self(Read options)
|
||||||
{
|
{
|
||||||
struct passwd fallback;
|
Vector<gid_t> extra_gids = TRY(Core::System::getgroups());
|
||||||
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;
|
auto pwd = TRY(Core::System::getpwuid(getuid()));
|
||||||
int extra_gid_count = getgroups(0, nullptr);
|
if (!pwd.has_value())
|
||||||
if (extra_gid_count) {
|
return Error::from_string_literal("No such user"sv);
|
||||||
extra_gids.resize(extra_gid_count);
|
|
||||||
int rc = getgroups(extra_gid_count, extra_gids.data());
|
|
||||||
if (rc < 0)
|
|
||||||
extra_gids.resize(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct passwd* pwd = getpwuid(fallback.pw_uid);
|
spwd spwd = {};
|
||||||
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*>("");
|
|
||||||
#ifndef AK_OS_BSD_GENERIC
|
#ifndef AK_OS_BSD_GENERIC
|
||||||
spwd* spwd = nullptr;
|
if (options != Read::PasswdOnly) {
|
||||||
if (options != Read::PasswdOnly)
|
auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name));
|
||||||
spwd = getspnam(pwd->pw_name);
|
if (!maybe_spwd.has_value())
|
||||||
if (!spwd)
|
return Error::from_string_literal("No shadow entry for user"sv);
|
||||||
spwd = &spwd_dummy;
|
spwd = maybe_spwd.release_value();
|
||||||
#else
|
}
|
||||||
(void)options;
|
|
||||||
auto* spwd = &spwd_dummy;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return Account(*pwd, *spwd, extra_gids);
|
return Account(*pwd, spwd, extra_gids);
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<Account> Account::from_name(const char* username, Read options)
|
ErrorOr<Account> Account::from_name(const char* username, Read options)
|
||||||
{
|
{
|
||||||
errno = 0;
|
auto pwd = TRY(Core::System::getpwnam(username));
|
||||||
auto* pwd = getpwnam(username);
|
if (!pwd.has_value())
|
||||||
if (!pwd) {
|
return Error::from_string_literal("No such user"sv);
|
||||||
if (errno == 0)
|
|
||||||
return Error::from_string_literal("No such user"sv);
|
spwd spwd = {};
|
||||||
return Error::from_errno(errno);
|
|
||||||
}
|
|
||||||
spwd spwd_dummy = {};
|
|
||||||
spwd_dummy.sp_namp = const_cast<char*>(username);
|
|
||||||
spwd_dummy.sp_pwdp = const_cast<char*>("");
|
|
||||||
#ifndef AK_OS_BSD_GENERIC
|
#ifndef AK_OS_BSD_GENERIC
|
||||||
spwd* spwd = nullptr;
|
if (options != Read::PasswdOnly) {
|
||||||
if (options != Read::PasswdOnly)
|
auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name));
|
||||||
spwd = getspnam(pwd->pw_name);
|
if (!maybe_spwd.has_value())
|
||||||
if (!spwd)
|
return Error::from_string_literal("No shadow entry for user"sv);
|
||||||
spwd = &spwd_dummy;
|
spwd = maybe_spwd.release_value();
|
||||||
#else
|
}
|
||||||
(void)options;
|
|
||||||
auto* spwd = &spwd_dummy;
|
|
||||||
#endif
|
#endif
|
||||||
return from_passwd(*pwd, *spwd);
|
return from_passwd(*pwd, spwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<Account> Account::from_uid(uid_t uid, Read options)
|
ErrorOr<Account> Account::from_uid(uid_t uid, Read options)
|
||||||
{
|
{
|
||||||
errno = 0;
|
auto pwd = TRY(Core::System::getpwuid(uid));
|
||||||
auto* pwd = getpwuid(uid);
|
if (!pwd.has_value())
|
||||||
if (!pwd) {
|
return Error::from_string_literal("No such user"sv);
|
||||||
if (errno == 0)
|
|
||||||
return Error::from_string_literal("No such user"sv);
|
spwd spwd = {};
|
||||||
return Error::from_errno(errno);
|
|
||||||
}
|
|
||||||
spwd spwd_dummy = {};
|
|
||||||
spwd_dummy.sp_namp = pwd->pw_name;
|
|
||||||
spwd_dummy.sp_pwdp = const_cast<char*>("");
|
|
||||||
#ifndef AK_OS_BSD_GENERIC
|
#ifndef AK_OS_BSD_GENERIC
|
||||||
spwd* spwd = nullptr;
|
if (options != Read::PasswdOnly) {
|
||||||
if (options != Read::PasswdOnly)
|
auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name));
|
||||||
spwd = getspnam(pwd->pw_name);
|
if (!maybe_spwd.has_value())
|
||||||
if (!spwd)
|
return Error::from_string_literal("No shadow entry for user"sv);
|
||||||
spwd = &spwd_dummy;
|
spwd = maybe_spwd.release_value();
|
||||||
#else
|
}
|
||||||
(void)options;
|
|
||||||
auto* spwd = &spwd_dummy;
|
|
||||||
#endif
|
#endif
|
||||||
return from_passwd(*pwd, *spwd);
|
return from_passwd(*pwd, spwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Account::authenticate(SecretString const& password) const
|
bool Account::authenticate(SecretString const& password) const
|
||||||
|
|
|
@ -32,7 +32,7 @@ public:
|
||||||
PasswdOnly
|
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_name(char const* username, Read options = Read::All);
|
||||||
static ErrorOr<Account> from_uid(uid_t uid, Read options = Read::All);
|
static ErrorOr<Account> from_uid(uid_t uid, Read options = Read::All);
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
else
|
else
|
||||||
account = TRY(Core::Account::from_name(user_str.characters(), Core::Account::Read::PasswdOnly));
|
account = TRY(Core::Account::from_name(user_str.characters(), Core::Account::Read::PasswdOnly));
|
||||||
} else {
|
} else {
|
||||||
account = Core::Account::self(Core::Account::Read::PasswdOnly);
|
account = TRY(Core::Account::self(Core::Account::Read::PasswdOnly));
|
||||||
}
|
}
|
||||||
|
|
||||||
return print_id_objects(account.value());
|
return print_id_objects(account.value());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue