瀏覽代碼

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.
Jean-Baptiste Boric 4 年之前
父節點
當前提交
16983dbe8e
共有 2 個文件被更改,包括 23 次插入9 次删除
  1. 15 6
      Userland/Libraries/LibCore/Account.cpp
  2. 8 3
      Userland/Libraries/LibCore/Account.h

+ 15 - 6
Userland/Libraries/LibCore/Account.cpp

@@ -66,7 +66,7 @@ Result<Account, String> 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<char*>("(unknown)");
@@ -95,17 +95,20 @@ Account Account::self()
     spwd_dummy.sp_namp = pwd->pw_name;
     spwd_dummy.sp_pwdp = const_cast<char*>("");
 #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, String> Account::from_name(const char* username)
+Result<Account, String> Account::from_name(const char* username, Read options)
 {
     errno = 0;
     auto* pwd = getpwnam(username);
@@ -119,16 +122,19 @@ Result<Account, String> Account::from_name(const char* username)
     spwd_dummy.sp_namp = const_cast<char*>(username);
     spwd_dummy.sp_pwdp = const_cast<char*>("");
 #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, String> Account::from_uid(uid_t uid)
+Result<Account, String> Account::from_uid(uid_t uid, Read options)
 {
     errno = 0;
     auto* pwd = getpwuid(uid);
@@ -142,10 +148,13 @@ Result<Account, String> Account::from_uid(uid_t uid)
     spwd_dummy.sp_namp = pwd->pw_name;
     spwd_dummy.sp_pwdp = const_cast<char*>("");
 #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);

+ 8 - 3
Userland/Libraries/LibCore/Account.h

@@ -27,9 +27,14 @@ struct spwd {
 
 class Account {
 public:
-    static Account self();
-    static Result<Account, String> from_name(const char* username);
-    static Result<Account, String> from_uid(uid_t uid);
+    enum class Read {
+        All,
+        PasswdOnly
+    };
+
+    static Account self(Read options = Read::All);
+    static Result<Account, String> from_name(const char* username, Read options = Read::All);
+    static Result<Account, String> from_uid(uid_t uid, Read options = Read::All);
 
     bool authenticate(const char* password) const;
     bool login() const;