Force setting $USER if UID is 0

Works around some weird su implementations.

Fixes #3916.
This commit is contained in:
Fabian Homborg 2017-04-07 15:06:54 +02:00
parent 3ade72266c
commit 3fa5d6c794

View file

@ -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());