From 3cacb56bb7dd10dede9f6786c21203d662e99e57 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@gmail.com>
Date: Tue, 17 Jan 2023 18:47:50 +0100
Subject: [PATCH] fs/StandardDirectory: don't fall back to getpwuid() without
 $HOME

If the environment variable $HOME does not exist, don't attempt to
obtain it from /etc/passwd; without $HOME, the calling process
indicates that it does not wish MPD to access the home directory.

This also prevents MPD from attempting to load
`/root/.config/mpd/mpd.conf` if MPD got started as global systemd
service.  Reading from there makes no sense, only /etc/mpd.conf shall
be used then.

This piece of code was initially added by commit 5d857921783996.

Closes https://github.com/MusicPlayerDaemon/MPD/issues/1687
---
 meson.build                  |  1 -
 src/fs/StandardDirectory.cxx | 14 +-------------
 2 files changed, 1 insertion(+), 14 deletions(-)

diff --git a/meson.build b/meson.build
index 467297e0f..ae8a7a14b 100644
--- a/meson.build
+++ b/meson.build
@@ -205,7 +205,6 @@ enable_daemon = not is_windows and not is_android and get_option('daemon')
 conf.set('ENABLE_DAEMON', enable_daemon)
 
 conf.set('HAVE_GETPWNAM_R', compiler.has_function('getpwnam_r'))
-conf.set('HAVE_GETPWUID_R', compiler.has_function('getpwuid_r'))
 conf.set('HAVE_INITGROUPS', compiler.has_function('initgroups'))
 conf.set('HAVE_FNMATCH', compiler.has_function('fnmatch'))
 
diff --git a/src/fs/StandardDirectory.cxx b/src/fs/StandardDirectory.cxx
index ad0ae2118..1ff5de098 100644
--- a/src/fs/StandardDirectory.cxx
+++ b/src/fs/StandardDirectory.cxx
@@ -34,7 +34,6 @@
 #include <shlobj.h>
 #else
 #include <stdlib.h>
-#include <unistd.h>
 #include <pwd.h>
 #endif
 
@@ -80,15 +79,6 @@ public:
 		return result != nullptr;
 	}
 
-	bool ReadByUid(uid_t uid) {
-#ifdef HAVE_GETPWUID_R
-		getpwuid_r(uid, &pw, buf.data(), buf.size(), &result);
-#else
-		result = getpwuid(uid);
-#endif
-		return result != nullptr;
-	}
-
 	const passwd *operator->() {
 		assert(result != nullptr);
 		return result;
@@ -375,10 +365,8 @@ GetHomeDir() noexcept
 	if (const auto home = getenv("HOME");
 	    IsValidPathString(home) && IsValidDir(home))
 		return AllocatedPath::FromFS(home);
-
-	if (PasswdEntry pw; pw.ReadByUid(getuid()))
-		return SafePathFromFS(pw->pw_dir);
 #endif
+
 	return nullptr;
 }