diff --git a/Userland/Libraries/LibCore/Account.cpp b/Userland/Libraries/LibCore/Account.cpp index deee1bff030..ff33f1baca6 100644 --- a/Userland/Libraries/LibCore/Account.cpp +++ b/Userland/Libraries/LibCore/Account.cpp @@ -66,6 +66,45 @@ Result Account::from_passwd(const passwd& pwd, const spwd& spwd return account; } +Account Account::self() +{ + struct passwd fallback; + fallback.pw_name = const_cast("(unknown)"); + fallback.pw_uid = getuid(); + fallback.pw_gid = getgid(); + fallback.pw_gecos = const_cast(""); + fallback.pw_dir = const_cast("(unknown)"); + fallback.pw_shell = const_cast("(unknown)"); + + Vector 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); + } + + 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(""); +#ifndef AK_OS_BSD_GENERIC + auto* spwd = getspnam(pwd->pw_name); + if (!spwd) + spwd = &spwd_dummy; +#else + auto* spwd = &spwd_dummy; +#endif + + return Account(*pwd, *spwd, extra_gids); +} + Result Account::from_name(const char* username) { errno = 0; diff --git a/Userland/Libraries/LibCore/Account.h b/Userland/Libraries/LibCore/Account.h index 695e75d4baa..3b4967ef5ee 100644 --- a/Userland/Libraries/LibCore/Account.h +++ b/Userland/Libraries/LibCore/Account.h @@ -27,6 +27,7 @@ struct spwd { class Account { public: + static Account self(); static Result from_name(const char* username); static Result from_uid(uid_t uid);