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 5d85792178.

Closes https://github.com/MusicPlayerDaemon/MPD/issues/1687
This commit is contained in:
Max Kellermann 2023-01-17 18:47:50 +01:00
parent 15a1973e28
commit 3cacb56bb7
2 changed files with 1 additions and 14 deletions

View File

@ -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('ENABLE_DAEMON', enable_daemon)
conf.set('HAVE_GETPWNAM_R', compiler.has_function('getpwnam_r')) 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_INITGROUPS', compiler.has_function('initgroups'))
conf.set('HAVE_FNMATCH', compiler.has_function('fnmatch')) conf.set('HAVE_FNMATCH', compiler.has_function('fnmatch'))

View File

@ -34,7 +34,6 @@
#include <shlobj.h> #include <shlobj.h>
#else #else
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <pwd.h> #include <pwd.h>
#endif #endif
@ -80,15 +79,6 @@ public:
return result != nullptr; 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->() { const passwd *operator->() {
assert(result != nullptr); assert(result != nullptr);
return result; return result;
@ -375,10 +365,8 @@ GetHomeDir() noexcept
if (const auto home = getenv("HOME"); if (const auto home = getenv("HOME");
IsValidPathString(home) && IsValidDir(home)) IsValidPathString(home) && IsValidDir(home))
return AllocatedPath::FromFS(home); return AllocatedPath::FromFS(home);
if (PasswdEntry pw; pw.ReadByUid(getuid()))
return SafePathFromFS(pw->pw_dir);
#endif #endif
return nullptr; return nullptr;
} }