From 1035b91a3dfae936144631d846652a7914acaeb5 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 11 May 2023 20:11:09 +0200 Subject: [PATCH] su: Do not change directory/set variables by default, instead add a --login option --- apps/login.cpp | 2 +- apps/su.cpp | 12 ++++++++++-- initrd/etc/init/99-login | 1 - 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/apps/login.cpp b/apps/login.cpp index 9b8e14fd..055e6532 100644 --- a/apps/login.cpp +++ b/apps/login.cpp @@ -38,7 +38,7 @@ Result luna_main(int argc, char** argv) username = name.view(); } - execl("/bin/su", "login", "-p", "--", username.chars(), nullptr); + execl("/bin/su", "login", "-lp", "--", username.chars(), nullptr); perror("su"); return 1; diff --git a/apps/su.cpp b/apps/su.cpp index 0f6d8486..0696311b 100644 --- a/apps/su.cpp +++ b/apps/su.cpp @@ -55,6 +55,7 @@ Result luna_main(int argc, char** argv) { StringView name; bool prompt_password; + bool login; if (geteuid() != 0) { @@ -67,6 +68,7 @@ Result luna_main(int argc, char** argv) parser.add_system_program_info("su"_sv); parser.add_positional_argument(name, "name"_sv, "root"_sv); parser.add_switch_argument(prompt_password, 'p', "prompt", "prompt for a password even if running as root"); + parser.add_switch_argument(login, 'l', "login"_sv, "change directory to the user's home and start a login shell"); parser.parse(argc, argv); struct passwd* entry = getpwnam(name.chars()); @@ -93,10 +95,16 @@ Result luna_main(int argc, char** argv) setgid(entry->pw_gid); setuid(entry->pw_uid); - chdir(entry->pw_dir); + if (login) + { + chdir(entry->pw_dir); + clearenv(); + setenv("PATH", "/bin:/sbin", 1); + } + + if (login || entry->pw_uid != 0) setenv("USER", entry->pw_name, 1); setenv("HOME", entry->pw_dir, 1); - setenv("USER", entry->pw_name, 1); setenv("SHELL", entry->pw_shell, 1); execl(entry->pw_shell, entry->pw_shell, NULL); diff --git a/initrd/etc/init/99-login b/initrd/etc/init/99-login index 631fd293..60edcd26 100644 --- a/initrd/etc/init/99-login +++ b/initrd/etc/init/99-login @@ -1,4 +1,3 @@ Name=login Command=/bin/login Restart=true -Environment=PATH=/bin:/sbin