Luna/apps/su.cpp

39 lines
819 B
C++
Raw Normal View History

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