2014-06-02 13:00:55 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2014-06-09 09:59:48 +00:00
|
|
|
#include "passwd.h"
|
2014-06-02 13:00:55 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int dologin(struct passwd *, int);
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-p] username\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
2014-06-09 09:59:48 +00:00
|
|
|
char *pass;
|
2014-06-03 09:55:12 +00:00
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
2014-06-02 13:00:55 +00:00
|
|
|
int pflag = 0;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
2014-06-03 09:45:27 +00:00
|
|
|
if (isatty(STDIN_FILENO) == 0)
|
|
|
|
eprintf("stdin is not a tty\n");
|
|
|
|
|
2014-06-02 13:00:55 +00:00
|
|
|
errno = 0;
|
|
|
|
pw = getpwnam(argv[0]);
|
|
|
|
if (errno)
|
|
|
|
eprintf("getpwnam: %s:", argv[0]);
|
|
|
|
else if (!pw)
|
|
|
|
eprintf("who are you?\n");
|
|
|
|
|
2014-06-03 09:55:12 +00:00
|
|
|
uid = pw->pw_uid;
|
|
|
|
gid = pw->pw_gid;
|
2014-06-02 13:00:55 +00:00
|
|
|
|
|
|
|
/* Flush pending input */
|
2014-06-03 09:45:27 +00:00
|
|
|
ioctl(STDIN_FILENO, TCFLSH, (void *)0);
|
2014-06-02 13:00:55 +00:00
|
|
|
|
2014-06-09 09:59:48 +00:00
|
|
|
pass = getpass("Password: "); putchar('\n');
|
2014-06-02 13:00:55 +00:00
|
|
|
if (!pass)
|
|
|
|
eprintf("getpass:");
|
2014-06-09 09:59:48 +00:00
|
|
|
if (pw_check(pw, pass) == 0)
|
|
|
|
eprintf("incorrect password\n");
|
2014-06-03 09:55:12 +00:00
|
|
|
|
|
|
|
if (initgroups(argv[0], gid) < 0)
|
2014-06-02 13:00:55 +00:00
|
|
|
eprintf("initgroups:");
|
2014-06-03 09:55:12 +00:00
|
|
|
if (setgid(gid) < 0)
|
2014-06-02 13:00:55 +00:00
|
|
|
eprintf("setgid:");
|
2014-06-03 09:55:12 +00:00
|
|
|
if (setuid(uid) < 0)
|
2014-06-02 13:00:55 +00:00
|
|
|
eprintf("setuid:");
|
|
|
|
|
|
|
|
return dologin(pw, pflag);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dologin(struct passwd *pw, int preserve)
|
|
|
|
{
|
2014-06-05 11:12:18 +00:00
|
|
|
char *shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell;
|
|
|
|
|
2014-06-02 13:00:55 +00:00
|
|
|
if (preserve == 0)
|
|
|
|
clearenv();
|
2014-06-03 17:56:46 +00:00
|
|
|
setenv("HOME", pw->pw_dir, 1);
|
2014-06-05 11:12:18 +00:00
|
|
|
setenv("SHELL", shell, 1);
|
2014-06-03 17:56:46 +00:00
|
|
|
setenv("USER", pw->pw_name, 1);
|
|
|
|
setenv("LOGNAME", pw->pw_name, 1);
|
|
|
|
setenv("PATH", ENV_PATH, 1);
|
2014-06-02 13:00:55 +00:00
|
|
|
if (chdir(pw->pw_dir) < 0)
|
|
|
|
eprintf("chdir %s:", pw->pw_dir);
|
2014-06-05 11:12:18 +00:00
|
|
|
execlp(shell, shell, "-l", NULL);
|
|
|
|
weprintf("execlp %s:", shell);
|
2014-06-02 13:00:55 +00:00
|
|
|
return (errno == ENOENT) ? 127 : 126;
|
|
|
|
}
|