Created
April 18, 2012 22:30
-
-
Save saurik/2417089 to your computer and use it in GitHub Desktop.
setuid login wrapper that let's you only be you
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pwd.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <stdio.h> | |
static void check(const char **name, const char *user) { | |
if (*name != NULL || user == NULL) | |
return; | |
struct passwd *pw = getpwnam(user); | |
if (pw != NULL && pw->pw_uid == getuid()) | |
*name = user; | |
} | |
int main() { | |
const char *name = NULL; | |
check(&name, getenv("LOGNAME")); | |
check(&name, getlogin()); | |
struct passwd *pw = getpwuid(getuid()); | |
if (pw != NULL) | |
check(&name, pw->pw_name); | |
if (name == NULL) | |
return 1; | |
const char *login = "/usr/bin/login"; | |
if (access("/bin/login", X_OK) == 0) | |
login = "/bin/login"; | |
char host[64] = ""; | |
snprintf(host, sizeof(host), "local:pid=%d", getppid()); | |
if (setuid(0) != 0 || setgid(0) != 0) | |
return 1; | |
execl(login, "login", "-f", "-h", host, "--", name, NULL); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment