From 3fa5d6c79405c2f5b1299af5d487a7bc95b3a10e Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 7 Apr 2017 15:06:54 +0200 Subject: [PATCH] Force setting $USER if UID is 0 Works around some weird su implementations. Fixes #3916. --- src/env.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/env.cpp b/src/env.cpp index 44fa1fef1..77a274249 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -803,7 +803,13 @@ void env_init(const struct config_paths_t *paths /* or NULL */) { // Set up the USER and PATH variables setup_path(); - setup_user(false); + + // Some `su`s keep $USER when changing to root. + // This leads to issues later on (and e.g. in prompts), + // so we work around it by resetting $USER. + // TODO: Figure out if that su actually checks if username == "root"(as the man page says) or UID == 0. + uid_t uid = getuid(); + setup_user(uid == 0); // Set up the version variable. wcstring version = str2wcstring(get_fish_version()); @@ -824,6 +830,10 @@ void env_init(const struct config_paths_t *paths /* or NULL */) { env_read_only.insert(L"SHLVL"); // Set up the HOME variable. + // Unlike $USER, it doesn't seem that `su`s pass this along + // if the target user is root, unless "--preserve-environment" is used. + // Since that is an explicit choice, we should allow it to enable e.g. + // env HOME=(mktemp -d) su --preserve-environment fish if (env_get_string(L"HOME").missing_or_empty()) { const env_var_t unam = env_get_string(L"USER"); char *unam_narrow = wcs2str(unam.c_str());