From 16983dbe8e61fb79f38155b1da215ffbcd6cb63f Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Boric Date: Thu, 8 Jul 2021 22:00:23 +0200 Subject: [PATCH] LibCore: Add ability to not read shadow data for Account This stops spamming the kernel logs with unveil violations if the program didn't unveil /etc/shadow. --- Userland/Libraries/LibCore/Account.cpp | 21 +++++++++++++++------ Userland/Libraries/LibCore/Account.h | 11 ++++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibCore/Account.cpp b/Userland/Libraries/LibCore/Account.cpp index ff33f1baca..451add1c8a 100644 --- a/Userland/Libraries/LibCore/Account.cpp +++ b/Userland/Libraries/LibCore/Account.cpp @@ -66,7 +66,7 @@ Result Account::from_passwd(const passwd& pwd, const spwd& spwd return account; } -Account Account::self() +Account Account::self(Read options) { struct passwd fallback; fallback.pw_name = const_cast("(unknown)"); @@ -95,17 +95,20 @@ Account Account::self() spwd_dummy.sp_namp = pwd->pw_name; spwd_dummy.sp_pwdp = const_cast(""); #ifndef AK_OS_BSD_GENERIC - auto* spwd = getspnam(pwd->pw_name); + spwd* spwd = nullptr; + if (options != Read::PasswdOnly) + spwd = getspnam(pwd->pw_name); if (!spwd) spwd = &spwd_dummy; #else + (void)options; auto* spwd = &spwd_dummy; #endif return Account(*pwd, *spwd, extra_gids); } -Result Account::from_name(const char* username) +Result Account::from_name(const char* username, Read options) { errno = 0; auto* pwd = getpwnam(username); @@ -119,16 +122,19 @@ Result Account::from_name(const char* username) spwd_dummy.sp_namp = const_cast(username); spwd_dummy.sp_pwdp = const_cast(""); #ifndef AK_OS_BSD_GENERIC - auto* spwd = getspnam(username); + spwd* spwd = nullptr; + if (options != Read::PasswdOnly) + spwd = getspnam(pwd->pw_name); if (!spwd) spwd = &spwd_dummy; #else + (void)options; auto* spwd = &spwd_dummy; #endif return from_passwd(*pwd, *spwd); } -Result Account::from_uid(uid_t uid) +Result Account::from_uid(uid_t uid, Read options) { errno = 0; auto* pwd = getpwuid(uid); @@ -142,10 +148,13 @@ Result Account::from_uid(uid_t uid) spwd_dummy.sp_namp = pwd->pw_name; spwd_dummy.sp_pwdp = const_cast(""); #ifndef AK_OS_BSD_GENERIC - auto* spwd = getspnam(pwd->pw_name); + spwd* spwd = nullptr; + if (options != Read::PasswdOnly) + spwd = getspnam(pwd->pw_name); if (!spwd) spwd = &spwd_dummy; #else + (void)options; auto* spwd = &spwd_dummy; #endif return from_passwd(*pwd, *spwd); diff --git a/Userland/Libraries/LibCore/Account.h b/Userland/Libraries/LibCore/Account.h index 3b4967ef5e..c103468dbc 100644 --- a/Userland/Libraries/LibCore/Account.h +++ b/Userland/Libraries/LibCore/Account.h @@ -27,9 +27,14 @@ struct spwd { class Account { public: - static Account self(); - static Result from_name(const char* username); - static Result from_uid(uid_t uid); + enum class Read { + All, + PasswdOnly + }; + + static Account self(Read options = Read::All); + static Result from_name(const char* username, Read options = Read::All); + static Result from_uid(uid_t uid, Read options = Read::All); bool authenticate(const char* password) const; bool login() const;