2014-02-10 12:12:50 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2014-02-10 12:12:50 +00:00
|
|
|
#include <fcntl.h>
|
2014-02-11 10:55:38 +00:00
|
|
|
#include <limits.h>
|
2014-02-10 12:12:50 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-02-10 15:04:24 +00:00
|
|
|
#include <string.h>
|
2014-02-10 12:12:50 +00:00
|
|
|
#include <unistd.h>
|
2014-07-02 10:45:10 +00:00
|
|
|
#include <utmp.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
2014-07-01 14:48:35 +00:00
|
|
|
#include "config.h"
|
2014-02-10 12:12:50 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-06 14:19:33 +00:00
|
|
|
static char *tty = "/dev/tty1";
|
|
|
|
static char *defaultterm = "linux";
|
|
|
|
|
2014-02-10 12:12:50 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-02-11 10:36:23 +00:00
|
|
|
eprintf("usage: %s [tty] [term] [cmd] [args...]\n", argv0);
|
2014-02-10 12:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-02-11 10:55:38 +00:00
|
|
|
char term[128], logname[LOGIN_NAME_MAX], c;
|
2014-02-11 11:09:00 +00:00
|
|
|
char hostname[HOST_NAME_MAX + 1];
|
2014-07-02 10:45:10 +00:00
|
|
|
struct utmp usr;
|
|
|
|
struct sigaction sa;
|
|
|
|
FILE *fp;
|
|
|
|
int fd;
|
2014-02-14 13:52:04 +00:00
|
|
|
unsigned int i = 0;
|
2014-02-10 15:04:24 +00:00
|
|
|
ssize_t n;
|
2014-07-02 10:45:10 +00:00
|
|
|
long pos;
|
2014-02-10 12:12:50 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
strlcpy(term, defaultterm, sizeof(term));
|
|
|
|
if (argc > 0) {
|
|
|
|
tty = argv[0];
|
2014-02-24 12:26:40 +00:00
|
|
|
if (argc > 1)
|
|
|
|
strlcpy(term, argv[1], sizeof(term));
|
2014-02-10 12:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
|
2014-02-24 12:26:40 +00:00
|
|
|
setenv("TERM", term, 1);
|
2014-02-10 12:12:50 +00: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 10:14:09 +00:00
|
|
|
|
2014-11-12 11:49:44 +00:00
|
|
|
/* steal the controlling terminal if necessary */
|
|
|
|
if (ioctl(fd, TIOCSCTTY, (void *)1) != 0)
|
|
|
|
weprintf("TIOCSCTTY: could not set controlling tty\n");
|
|
|
|
vhangup();
|
2014-02-10 12:12:50 +00:00
|
|
|
close(fd);
|
2014-11-12 11:49:44 +00:00
|
|
|
|
2014-02-10 12:12:50 +00:00
|
|
|
fd = open(tty, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
eprintf("open %s:", tty);
|
2014-11-30 13:12:15 +00:00
|
|
|
dup2(fd, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
2014-11-12 11:49:44 +00:00
|
|
|
if (fchown(fd, 0, 0) < 0)
|
|
|
|
weprintf("fchown %s:", tty);
|
|
|
|
if (fchmod(fd, 0600) < 0)
|
|
|
|
weprintf("fchmod %s:", tty);
|
|
|
|
if (fd > 2)
|
|
|
|
close(fd);
|
2014-02-10 12:12:50 +00:00
|
|
|
|
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
|
2014-07-02 10:45:10 +00:00
|
|
|
/* Clear all utmp entries for this tty */
|
2014-07-01 14:48:35 +00:00
|
|
|
fp = fopen(UTMP_PATH, "r+");
|
2014-07-02 10:45:10 +00:00
|
|
|
if (fp) {
|
|
|
|
do {
|
|
|
|
pos = ftell(fp);
|
|
|
|
if (fread(&usr, sizeof(usr), 1, fp) != 1)
|
|
|
|
break;
|
|
|
|
if (usr.ut_line[0] == '\0')
|
|
|
|
continue;
|
|
|
|
if (strcmp(usr.ut_line, tty) != 0)
|
|
|
|
continue;
|
|
|
|
memset(&usr, 0, sizeof(usr));
|
|
|
|
fseek(fp, pos, SEEK_SET);
|
|
|
|
if (fwrite(&usr, sizeof(usr), 1, fp) != 1)
|
|
|
|
break;
|
|
|
|
} while (1);
|
|
|
|
if (ferror(fp))
|
2014-07-01 14:48:35 +00:00
|
|
|
weprintf("%s: I/O error:", UTMP_PATH);
|
2014-07-02 10:45:10 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2014-02-11 10:36:23 +00:00
|
|
|
if (argc > 2)
|
|
|
|
return execvp(argv[2], argv + 2);
|
|
|
|
|
2014-02-11 11:09:00 +00:00
|
|
|
if (gethostname(hostname, sizeof(hostname)) == 0)
|
|
|
|
printf("%s ", hostname);
|
|
|
|
printf("login: ");
|
2014-02-10 15:04:24 +00:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/* Flush pending input */
|
2014-11-30 13:12:15 +00:00
|
|
|
ioctl(0, TCFLSH, (void *)0);
|
2014-02-10 15:04:24 +00:00
|
|
|
memset(logname, 0, sizeof(logname));
|
|
|
|
while (1) {
|
2014-11-30 13:12:15 +00:00
|
|
|
n = read(0, &c, 1);
|
2014-02-10 15:04:24 +00:00
|
|
|
if (n < 0)
|
|
|
|
eprintf("read:");
|
|
|
|
if (n == 0)
|
2014-10-02 22:45:25 +00:00
|
|
|
return 1;
|
2014-02-10 15:04:24 +00:00
|
|
|
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')
|
2014-10-02 22:45:25 +00:00
|
|
|
return 1;
|
2014-06-05 10:28:08 +00:00
|
|
|
return execlp("/bin/login", "login", "-p", logname, NULL);
|
2014-02-10 12:12:50 +00:00
|
|
|
}
|