Skip to content

Instantly share code, notes, and snippets.

@saurik
Created April 18, 2012 22:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurik/2417089 to your computer and use it in GitHub Desktop.
Save saurik/2417089 to your computer and use it in GitHub Desktop.
setuid login wrapper that let's you only be you
#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