From 8b45766aaa2c8f5682be4f630d4e73ba32a9f191 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 8 Apr 2023 16:31:33 +0200 Subject: [PATCH] su: Use user names and read from the password file instead of using raw user IDs --- apps/su.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/apps/su.cpp b/apps/su.cpp index 2141be81..bc2a84f3 100644 --- a/apps/su.cpp +++ b/apps/su.cpp @@ -1,11 +1,12 @@ #include +#include #include #include #include int main(int argc, char** argv) { - StringView id; + StringView name; if (geteuid() != 0) { @@ -14,19 +15,24 @@ int main(int argc, char** argv) } os::ArgumentParser parser; - parser.add_positional_argument(id, "id"_sv, true); + parser.add_positional_argument(name, "name"_sv, true); parser.parse(argc, argv); - int uid = atoi(id.chars()); - - if (uid == 0) + struct passwd* entry = getpwnam(name.chars()); + if (!entry) { - fprintf(stderr, "Already root!\n"); + fprintf(stderr, "su: user %s not found!\n", name.chars()); return 1; } - setgid(uid); - setuid(uid); + if (getuid() != geteuid()) + { + fprintf(stderr, "FIXME: you have to enter %s's password first!\n", name.chars()); + return 1; + } - execl("/bin/sh", "sh", NULL); + setgid(entry->pw_gid); + setuid(entry->pw_uid); + + execl(entry->pw_shell, entry->pw_shell, NULL); }