su: fix setting argv0

argv0 was being set to the user's login shell even when -p was
specified. Only populate newargv once the shell is properly determined.
This commit is contained in:
neeshy 2024-02-12 16:25:50 -05:00 committed by Roberto E. Vargas Caballero
parent 8f5a0c3236
commit cf9e26d7fa
1 changed files with 5 additions and 5 deletions

10
su.c
View File

@ -75,9 +75,6 @@ main(int argc, char *argv[])
shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell; shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell;
if (lflag) { if (lflag) {
newargv[0] = shell;
newargv[1] = "-l";
newargv[2] = NULL;
term = getenv("TERM"); term = getenv("TERM");
clearenv(); clearenv();
setenv("HOME", pw->pw_dir, 1); setenv("HOME", pw->pw_dir, 1);
@ -87,9 +84,10 @@ main(int argc, char *argv[])
setenv("TERM", term ? term : "linux", 1); setenv("TERM", term ? term : "linux", 1);
if (chdir(pw->pw_dir) < 0) if (chdir(pw->pw_dir) < 0)
eprintf("chdir %s:", pw->pw_dir); eprintf("chdir %s:", pw->pw_dir);
} else {
newargv[0] = shell; newargv[0] = shell;
newargv[1] = NULL; newargv[1] = "-l";
newargv[2] = NULL;
} else {
if (pflag) { if (pflag) {
shell = getenv("SHELL"); shell = getenv("SHELL");
} else { } else {
@ -100,6 +98,8 @@ main(int argc, char *argv[])
setenv("LOGNAME", pw->pw_name, 1); setenv("LOGNAME", pw->pw_name, 1);
} }
} }
newargv[0] = shell;
newargv[1] = NULL;
} }
if (strcmp(pw->pw_name, "root") == 0) if (strcmp(pw->pw_name, "root") == 0)
setenv("PATH", ENV_SUPATH, 1); setenv("PATH", ENV_SUPATH, 1);