diff options
author | Karel Zak | 2016-10-14 15:23:44 +0200 |
---|---|---|
committer | Karel Zak | 2017-09-18 11:48:56 +0200 |
commit | 13396b1046333a459c6bee2201c66f8fea42043c (patch) | |
tree | 3e257fce6e54ddbb733a355118ef21da6fd8fa15 /login-utils/su-common.c | |
parent | su: cleanup shell related code (diff) | |
download | kernel-qcow2-util-linux-13396b1046333a459c6bee2201c66f8fea42043c.tar.gz kernel-qcow2-util-linux-13396b1046333a459c6bee2201c66f8fea42043c.tar.xz kernel-qcow2-util-linux-13396b1046333a459c6bee2201c66f8fea42043c.zip |
su: cleanup setenv related code
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'login-utils/su-common.c')
-rw-r--r-- | login-utils/su-common.c | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/login-utils/su-common.c b/login-utils/su-common.c index 22411d227..59360e4af 100644 --- a/login-utils/su-common.c +++ b/login-utils/su-common.c @@ -409,10 +409,10 @@ create_watching_parent(struct su_context *su) } -static void -set_path(const struct passwd * const pw) +static void setenv_path(const struct passwd *pw) { int r; + if (pw->pw_uid) r = logindefs_setenv("PATH", "ENV_PATH", _PATH_DEFPATH); @@ -424,46 +424,45 @@ set_path(const struct passwd * const pw) _("failed to set the %s environment variable"), "PATH"); } -/* Update `environ' for the new shell based on PW, with SHELL being - the value for the SHELL environment variable. */ - -static void -modify_environment(struct su_context *su, const char *shell) +static void modify_environment(struct su_context *su, const char *shell) { const struct passwd *pw = su->pwd; + /* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH. + * Unset all other environment variables. + */ if (su->simulate_login) { - /* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH. - Unset all other environment variables. */ char *term = getenv("TERM"); if (term) term = xstrdup(term); + environ = xmalloc((6 + ! !term) * sizeof(char *)); environ[0] = NULL; if (term) { xsetenv("TERM", term, 1); free(term); } + xsetenv("HOME", pw->pw_dir, 1); if (shell) xsetenv("SHELL", shell, 1); xsetenv("USER", pw->pw_name, 1); xsetenv("LOGNAME", pw->pw_name, 1); - set_path(pw); - } else { - /* Set HOME, SHELL, and (if not becoming a superuser) - USER and LOGNAME. */ - if (su->change_environment) { - xsetenv("HOME", pw->pw_dir, 1); - if (shell) - xsetenv("SHELL", shell, 1); - if (getlogindefs_bool("ALWAYS_SET_PATH", 0)) - set_path(pw); - - if (pw->pw_uid) { - xsetenv("USER", pw->pw_name, 1); - xsetenv("LOGNAME", pw->pw_name, 1); - } + setenv_path(pw); + + /* Set HOME, SHELL, and (if not becoming a superuser) USER and LOGNAME. + */ + } else if (su->change_environment) { + xsetenv("HOME", pw->pw_dir, 1); + if (shell) + xsetenv("SHELL", shell, 1); + + if (getlogindefs_bool("ALWAYS_SET_PATH", 0)) + setenv_path(pw); + + if (pw->pw_uid) { + xsetenv("USER", pw->pw_name, 1); + xsetenv("LOGNAME", pw->pw_name, 1); } } |