2023-04-08 11:50:18 +00:00
|
|
|
#include <os/ArgumentParser.h>
|
2023-04-08 14:31:33 +00:00
|
|
|
#include <pwd.h>
|
2023-04-08 11:50:18 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2023-04-08 14:31:33 +00:00
|
|
|
StringView name;
|
2023-04-08 11:50:18 +00:00
|
|
|
|
|
|
|
if (geteuid() != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "su must be run as root!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
os::ArgumentParser parser;
|
2023-04-08 14:31:33 +00:00
|
|
|
parser.add_positional_argument(name, "name"_sv, true);
|
2023-04-08 11:50:18 +00:00
|
|
|
parser.parse(argc, argv);
|
|
|
|
|
2023-04-08 14:31:33 +00:00
|
|
|
struct passwd* entry = getpwnam(name.chars());
|
|
|
|
if (!entry)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "su: user %s not found!\n", name.chars());
|
|
|
|
return 1;
|
|
|
|
}
|
2023-04-08 11:50:18 +00:00
|
|
|
|
2023-04-08 14:31:33 +00:00
|
|
|
if (getuid() != geteuid())
|
2023-04-08 11:50:18 +00:00
|
|
|
{
|
2023-04-08 14:31:33 +00:00
|
|
|
fprintf(stderr, "FIXME: you have to enter %s's password first!\n", name.chars());
|
2023-04-08 11:50:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-04-08 14:31:33 +00:00
|
|
|
setgid(entry->pw_gid);
|
|
|
|
setuid(entry->pw_uid);
|
2023-04-08 11:50:18 +00:00
|
|
|
|
2023-04-08 14:31:33 +00:00
|
|
|
execl(entry->pw_shell, entry->pw_shell, NULL);
|
2023-04-08 11:50:18 +00:00
|
|
|
}
|