2014-06-02 09:00:55 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-06-30 14:03:41 -04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2014-06-02 09:00:55 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-07-01 10:42:31 -04:00
|
|
|
#include <time.h>
|
2014-06-02 09:00:55 -04:00
|
|
|
#include <unistd.h>
|
2014-07-01 10:42:31 -04:00
|
|
|
#include <utmp.h>
|
2014-06-30 14:03:41 -04:00
|
|
|
|
2014-06-02 09:00:55 -04:00
|
|
|
#include "config.h"
|
2014-06-30 14:03:41 -04:00
|
|
|
#include "passwd.h"
|
2014-06-02 09:00:55 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int dologin(struct passwd *, int);
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-p] username\n", argv0);
|
|
|
|
}
|
|
|
|
|
2014-07-09 11:04:45 -04:00
|
|
|
/* Write utmp entry */
|
|
|
|
static void
|
2014-07-09 11:39:32 -04:00
|
|
|
writeutmp(const char *user, const char *tty)
|
|
|
|
{
|
2014-07-09 11:04:45 -04:00
|
|
|
struct utmp usr;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
memset(&usr, 0, sizeof(usr));
|
|
|
|
|
|
|
|
usr.ut_type = USER_PROCESS;
|
|
|
|
usr.ut_pid = getpid();
|
|
|
|
strlcpy(usr.ut_user, user, sizeof(usr.ut_user));
|
|
|
|
strlcpy(usr.ut_line, tty, sizeof(usr.ut_line));
|
|
|
|
usr.ut_tv.tv_sec = time(NULL);
|
|
|
|
|
|
|
|
fp = fopen(UTMP_PATH, "a");
|
|
|
|
if (fp) {
|
|
|
|
if (fwrite(&usr, sizeof(usr), 1, fp) != 1)
|
|
|
|
if (ferror(fp))
|
|
|
|
weprintf("%s: write error:", UTMP_PATH);
|
|
|
|
fclose(fp);
|
|
|
|
} else {
|
|
|
|
weprintf("fopen %s:", UTMP_PATH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 09:00:55 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
2014-07-09 11:04:45 -04:00
|
|
|
char *pass, *user;
|
2014-07-01 10:42:31 -04:00
|
|
|
char *tty;
|
2014-06-03 05:55:12 -04:00
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
2014-06-02 09:00:55 -04:00
|
|
|
int pflag = 0;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
2014-06-03 05:45:27 -04:00
|
|
|
if (isatty(STDIN_FILENO) == 0)
|
|
|
|
eprintf("stdin is not a tty\n");
|
|
|
|
|
2014-07-09 11:04:45 -04:00
|
|
|
user = argv[0];
|
2014-08-18 16:49:22 -04:00
|
|
|
errno = 0;
|
2014-07-09 11:04:45 -04:00
|
|
|
pw = getpwnam(user);
|
2014-08-18 16:49:22 -04:00
|
|
|
if (!pw) {
|
|
|
|
if (errno)
|
2014-08-18 16:55:38 -04:00
|
|
|
eprintf("getpwnam %s:", user);
|
2014-08-18 16:49:22 -04:00
|
|
|
else
|
|
|
|
eprintf("who are you?\n");
|
|
|
|
}
|
2014-06-02 09:00:55 -04:00
|
|
|
|
2014-06-03 05:55:12 -04:00
|
|
|
uid = pw->pw_uid;
|
|
|
|
gid = pw->pw_gid;
|
2014-06-02 09:00:55 -04:00
|
|
|
|
|
|
|
/* Flush pending input */
|
2014-06-03 05:45:27 -04:00
|
|
|
ioctl(STDIN_FILENO, TCFLSH, (void *)0);
|
2014-06-02 09:00:55 -04:00
|
|
|
|
2014-09-04 06:51:25 -04:00
|
|
|
pass = getpass("Password: ");
|
2014-06-02 09:00:55 -04:00
|
|
|
if (!pass)
|
|
|
|
eprintf("getpass:");
|
2014-06-09 07:58:40 -04:00
|
|
|
if (pw_check(pw, pass) <= 0)
|
2014-10-02 18:45:25 -04:00
|
|
|
exit(1);
|
2014-06-03 05:55:12 -04:00
|
|
|
|
2014-07-09 11:04:45 -04:00
|
|
|
tty = ttyname(STDIN_FILENO);
|
|
|
|
if (!tty)
|
|
|
|
eprintf("ttyname:");
|
|
|
|
|
|
|
|
writeutmp(user, tty);
|
|
|
|
|
|
|
|
if (initgroups(user, gid) < 0)
|
2014-06-02 09:00:55 -04:00
|
|
|
eprintf("initgroups:");
|
2014-06-03 05:55:12 -04:00
|
|
|
if (setgid(gid) < 0)
|
2014-06-02 09:00:55 -04:00
|
|
|
eprintf("setgid:");
|
2014-06-03 05:55:12 -04:00
|
|
|
if (setuid(uid) < 0)
|
2014-06-02 09:00:55 -04:00
|
|
|
eprintf("setuid:");
|
|
|
|
|
|
|
|
return dologin(pw, pflag);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dologin(struct passwd *pw, int preserve)
|
|
|
|
{
|
2014-06-05 07:12:18 -04:00
|
|
|
char *shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell;
|
|
|
|
|
2014-06-02 09:00:55 -04:00
|
|
|
if (preserve == 0)
|
|
|
|
clearenv();
|
2014-06-03 13:56:46 -04:00
|
|
|
setenv("HOME", pw->pw_dir, 1);
|
2014-06-05 07:12:18 -04:00
|
|
|
setenv("SHELL", shell, 1);
|
2014-06-03 13:56:46 -04:00
|
|
|
setenv("USER", pw->pw_name, 1);
|
|
|
|
setenv("LOGNAME", pw->pw_name, 1);
|
|
|
|
setenv("PATH", ENV_PATH, 1);
|
2014-06-02 09:00:55 -04:00
|
|
|
if (chdir(pw->pw_dir) < 0)
|
|
|
|
eprintf("chdir %s:", pw->pw_dir);
|
2014-06-05 07:12:18 -04:00
|
|
|
execlp(shell, shell, "-l", NULL);
|
|
|
|
weprintf("execlp %s:", shell);
|
2014-06-02 09:00:55 -04:00
|
|
|
return (errno == ENOENT) ? 127 : 126;
|
|
|
|
}
|