su: Do not change directory/set variables by default, instead add a --login option
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-05-11 20:11:09 +02:00
parent a935039e78
commit 1035b91a3d
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 11 additions and 4 deletions

View File

@ -38,7 +38,7 @@ Result<int> luna_main(int argc, char** argv)
username = name.view(); username = name.view();
} }
execl("/bin/su", "login", "-p", "--", username.chars(), nullptr); execl("/bin/su", "login", "-lp", "--", username.chars(), nullptr);
perror("su"); perror("su");
return 1; return 1;

View File

@ -55,6 +55,7 @@ Result<int> luna_main(int argc, char** argv)
{ {
StringView name; StringView name;
bool prompt_password; bool prompt_password;
bool login;
if (geteuid() != 0) if (geteuid() != 0)
{ {
@ -67,6 +68,7 @@ Result<int> luna_main(int argc, char** argv)
parser.add_system_program_info("su"_sv); parser.add_system_program_info("su"_sv);
parser.add_positional_argument(name, "name"_sv, "root"_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(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); parser.parse(argc, argv);
struct passwd* entry = getpwnam(name.chars()); struct passwd* entry = getpwnam(name.chars());
@ -93,10 +95,16 @@ Result<int> luna_main(int argc, char** argv)
setgid(entry->pw_gid); setgid(entry->pw_gid);
setuid(entry->pw_uid); 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("HOME", entry->pw_dir, 1);
setenv("USER", entry->pw_name, 1);
setenv("SHELL", entry->pw_shell, 1); setenv("SHELL", entry->pw_shell, 1);
execl(entry->pw_shell, entry->pw_shell, NULL); execl(entry->pw_shell, entry->pw_shell, NULL);

View File

@ -1,4 +1,3 @@
Name=login Name=login
Command=/bin/login Command=/bin/login
Restart=true Restart=true
Environment=PATH=/bin:/sbin