1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:47:45 +00:00

Userland: Use Core::ArgsParser for 'su'

This commit is contained in:
Linus Groh 2020-08-05 20:26:32 +02:00 committed by Andreas Kling
parent d320f7b9d2
commit 3298b0318c

View file

@ -25,6 +25,7 @@
*/ */
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/GetPassword.h> #include <LibCore/GetPassword.h>
#include <alloca.h> #include <alloca.h>
#include <grp.h> #include <grp.h>
@ -37,17 +38,22 @@ extern "C" int main(int, char**);
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (geteuid() != 0) { const char* user = nullptr;
Core::ArgsParser args_parser;
args_parser.add_positional_argument(user, "User to switch to (defaults to user with UID 0)", "user", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
if (geteuid() != 0)
fprintf(stderr, "Not running as root :(\n"); fprintf(stderr, "Not running as root :(\n");
}
uid_t uid = 0; uid_t uid = 0;
gid_t gid = 0; gid_t gid = 0;
struct passwd* pwd = nullptr; struct passwd* pwd = nullptr;
if (argc > 1) { if (user) {
pwd = getpwnam(argv[1]); pwd = getpwnam(user);
if (!pwd) { if (!pwd) {
fprintf(stderr, "No such user: %s\n", argv[1]); fprintf(stderr, "No such user: %s\n", user);
return 1; return 1;
} }
uid = pwd->pw_uid; uid = pwd->pw_uid;