ladybird/Userland/su.cpp

36 lines
695 B
C++
Raw Normal View History

#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <alloca.h>
extern "C" int main(int, char**);
int main(int argc, char** argv)
{
2019-02-21 22:49:16 +00:00
uid_t uid = 0;
gid_t gid = 0;
if (argc > 1) {
auto* pwd = getpwnam(argv[1]);
if (!pwd) {
fprintf(stderr, "No such user: %s\n", argv[1]);
return 1;
}
uid = pwd->pw_uid;
gid = pwd->pw_gid;
}
int rc = setgid(uid);
if (rc < 0) {
perror("setgid");
return 1;
}
rc = setuid(gid);
if (rc < 0) {
perror("setuid");
return 1;
}
rc = execl("/bin/sh", "sh", nullptr);
2019-02-21 22:49:16 +00:00
perror("execl");
return 1;
}