From 255ebaf535c9f6d8a88e23d55d8be04b0ea73343 Mon Sep 17 00:00:00 2001
From: Ron Yorston <rmy@pobox.com>
Date: Mon, 27 Mar 2023 08:57:40 +0100
Subject: drop: adjust environment on privilege change

Some environment variables are subject to special treatment: USER,
LOGNAME, HOME and SHELL are initialised when the shell starts if
they don't already have a value.

Some adjustments are necessary when changing privilege level:

- USERNAME is added to the set of variables subject to special
  treatment.  Unlike the others this is normally set on Windows.

- The special variables are now also updated on shell start up if
  the current process is running with elevated privileges.  This is
  necessary so USER, USERNAME and LOGNAME have the correct value.

- USER, USERNAME and LOGNAME are set to the name of the unprivileged
  user when elevated privileges are dropped, though not if they've
  been changed from the expected value of "root".

Costs 160-208 bytes.

(GitHub issue #300)
---
 shell/ash.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

(limited to 'shell')

diff --git a/shell/ash.c b/shell/ash.c
index d78c6e828..1eddec4ea 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -15550,7 +15550,7 @@ exitshell(void)
 # undef getenv
 static void xsetenv_if_unset(const char *key, const char *value)
 {
-	if (!getenv(key))
+	if (!getenv(key) || getuid() == 0)
 		xsetenv(key, value);
 }
 #endif
@@ -15641,12 +15641,13 @@ init(void)
 			}
 
 			/* Initialise some variables normally set at login, but
-			 * only if someone hasn't already set them. */
+			 * only if someone hasn't already set them or we're root. */
 			pw = getpwuid(getuid());
 			if (pw) {
-				xsetenv_if_unset("USER",    pw->pw_name);
-				xsetenv_if_unset("LOGNAME", pw->pw_name);
-				xsetenv_if_unset("HOME",    pw->pw_dir);
+				xsetenv_if_unset("USER",     pw->pw_name);
+				xsetenv_if_unset("USERNAME", pw->pw_name);
+				xsetenv_if_unset("LOGNAME",  pw->pw_name);
+				xsetenv_if_unset("HOME",     pw->pw_dir);
 			}
 			xsetenv_if_unset("SHELL",   DEFAULT_SHELL);
 		}
-- 
cgit v1.2.3-55-g6feb