2014-02-10 07:12:50 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <fcntl.h>
|
2014-02-11 05:55:38 -05:00
|
|
|
#include <limits.h>
|
2014-02-10 07:12:50 -05:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-02-10 10:04:24 -05:00
|
|
|
#include <string.h>
|
2014-02-10 07:12:50 -05:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-02-11 05:36:23 -05:00
|
|
|
eprintf("usage: %s [tty] [term] [cmd] [args...]\n", argv0);
|
2014-02-10 07:12:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *tty = "/dev/tty1";
|
2014-02-24 07:26:40 -05:00
|
|
|
static char *defaultterm = "linux";
|
2014-02-10 07:12:50 -05:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct sigaction sa;
|
2014-02-11 05:55:38 -05:00
|
|
|
char term[128], logname[LOGIN_NAME_MAX], c;
|
2014-02-11 06:09:00 -05:00
|
|
|
char hostname[HOST_NAME_MAX + 1];
|
2014-02-14 08:52:04 -05:00
|
|
|
unsigned int i = 0;
|
2014-02-10 10:04:24 -05:00
|
|
|
ssize_t n;
|
2014-02-10 07:12:50 -05:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
strlcpy(term, defaultterm, sizeof(term));
|
|
|
|
if (argc > 0) {
|
|
|
|
tty = argv[0];
|
2014-02-24 07:26:40 -05:00
|
|
|
if (argc > 1)
|
|
|
|
strlcpy(term, argv[1], sizeof(term));
|
2014-02-10 07:12:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
|
2014-02-24 07:26:40 -05:00
|
|
|
setenv("TERM", term, 1);
|
2014-02-10 07:12:50 -05:00
|
|
|
|
|
|
|
setsid();
|
|
|
|
|
|
|
|
fd = open(tty, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
eprintf("open %s:", tty);
|
|
|
|
if (isatty(fd) == 0)
|
|
|
|
eprintf("%s is not a tty\n", tty);
|
2014-02-11 05:14:09 -05:00
|
|
|
if (ioctl(fd, TIOCSCTTY, (void *)1) == 0)
|
|
|
|
vhangup();
|
|
|
|
else
|
2014-02-10 07:12:50 -05:00
|
|
|
fprintf(stderr, "could not set controlling tty\n");
|
2014-02-11 05:14:09 -05:00
|
|
|
|
2014-02-10 07:12:50 -05:00
|
|
|
close(fd);
|
|
|
|
fd = open(tty, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
eprintf("open %s:", tty);
|
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
dup2(fd, STDERR_FILENO);
|
|
|
|
|
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
|
2014-02-11 08:08:10 -05:00
|
|
|
if (fchown(fd, 0, 0) < 0)
|
|
|
|
eprintf("fchown %s:", tty);
|
|
|
|
if (fchmod(fd, 0600) < 0)
|
|
|
|
eprintf("chmod %s:", tty);
|
|
|
|
|
2014-02-11 05:36:23 -05:00
|
|
|
if (argc > 2)
|
|
|
|
return execvp(argv[2], argv + 2);
|
|
|
|
|
2014-02-11 06:09:00 -05:00
|
|
|
if (gethostname(hostname, sizeof(hostname)) == 0)
|
|
|
|
printf("%s ", hostname);
|
|
|
|
printf("login: ");
|
2014-02-10 10:04:24 -05:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/* Flush pending input */
|
|
|
|
ioctl(STDIN_FILENO, TCFLSH, (void *)0);
|
|
|
|
memset(logname, 0, sizeof(logname));
|
|
|
|
while (1) {
|
|
|
|
n = read(STDIN_FILENO, &c, 1);
|
|
|
|
if (n < 0)
|
|
|
|
eprintf("read:");
|
|
|
|
if (n == 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
if (i >= sizeof(logname) - 1)
|
|
|
|
eprintf("login name too long\n");
|
|
|
|
if (c == '\n' || c == '\r')
|
|
|
|
break;
|
|
|
|
logname[i++] = c;
|
|
|
|
}
|
|
|
|
if (logname[0] == '-')
|
|
|
|
eprintf("login name cannot start with '-'\n");
|
|
|
|
if (logname[0] == '\0')
|
|
|
|
return EXIT_FAILURE;
|
2014-02-17 06:30:56 -05:00
|
|
|
return execvp("/bin/login", (char *[]){ "login", "-p", logname, NULL });
|
2014-02-10 07:12:50 -05:00
|
|
|
}
|