From: Mike Lowis Date: Wed, 27 Apr 2016 20:11:02 +0000 (-0400) Subject: Remove login prompt from getty in preparation to move it to login X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=66ae34fee68af097ab9707657875698d75350e65;p=proto%2Falbase.git Remove login prompt from getty in preparation to move it to login --- diff --git a/source/getty.c b/source/getty.c index 1e1d2957..7af8da1d 100644 --- a/source/getty.c +++ b/source/getty.c @@ -15,8 +15,6 @@ char* ARGV0; static char* TTY = "/dev/tty1"; static char* TERM = "linux"; static char* UTMP = "/var/run/utmp"; -static char Hostname[256] = {0}; -static char Username[256] = {0}; static void ignoresig(int signal, int ignore) { struct sigaction sigact; @@ -30,16 +28,16 @@ static int opentty(void) { int fd; /* First reset the tty and take control if necessary */ if ((fd = open(TTY, O_RDWR)) < 0) - die("Failed to open %s\n", TTY); + die("Failed to open %s", TTY); if (isatty(fd) == 0) - die("%s is not a tty\n", TTY); + die("%s is not a tty", TTY); if (ioctl(fd, TIOCSCTTY, (void *)1) != 0) - warn("TIOCSCTTY: could not set controlling tty\n"); + warn("TIOCSCTTY: could not set controlling tty"); vhangup(); close(fd); /* Now open it for real */ if ((fd = open(TTY, O_RDWR)) < 0) - die("Failed to open %s\n", TTY); + die("Failed to open %s", TTY); return fd; } @@ -60,31 +58,6 @@ static void clearutmp(void) { fclose(fp); } -static int dologin(void) { - int i = 0, c = 0; - /* Print the hostname */ - if (gethostname(Hostname, sizeof(Hostname)) == 0) - printf("%s ", Hostname); - printf("login: "); - fflush(stdout); - /* Read in the username */ - ioctl(0, TCFLSH, (void *)0); - while (1) { - if (0 == efread(&c, 1, 1, stdin)) - return 1; - if (i >= (sizeof(Username) - 1)) - die("login name too long\n"); - if (c == '\n' || c == '\r') - break; - Username[i++] = c; - } - /* Execute the login command */ - if ((Username[0] == '\0') || (Username[0] == '-')) - return 1; - else - return execlp("/bin/login", "login", "-p", Username, NULL); -} - int main(int argc, char *argv[]) { /* Print usage and exit if we receive any flags */ OPTBEGIN{ @@ -98,15 +71,14 @@ int main(int argc, char *argv[]) { ignoresig(SIGHUP, 1); // ignore SIGHUP setenv("TERM", TERM, 1); setsid(); - /* Open the TTY for normal usage */ int fd = opentty(); dup2(fd, 0); // Make stdin the TTY dup2(fd, 1); // Make stdout the TTY dup2(fd, 2); // Make stderr the TTY if (fchown(fd, 0, 0) < 0) - warn("fchown %s:", TTY); + warn("chown of %s failed", TTY); if (fchmod(fd, 0600) < 0) - warn("fchmod %s:", TTY); + warn("chmod of %s failed", TTY); ignoresig(SIGHUP, 0); // stop ignoring SIGHUP /* clear all utmp entries for this TTY */ clearutmp(); @@ -114,5 +86,5 @@ int main(int argc, char *argv[]) { if (argc > 2) return execvp(argv[2], &(argv[2])); else - return dologin(); + return execlp("/bin/login", "login", "-p", NULL); } diff --git a/source/login.c b/source/login.c index 25a59e48..ec9b8008 100644 --- a/source/login.c +++ b/source/login.c @@ -20,111 +20,111 @@ static void writeutmp(const char *user, const char *tty) { - 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); - } + 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); + } } static int dologin(struct passwd *pw, int preserve) { - char *shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell; - - if (preserve == 0) - clearenv(); - setenv("HOME", pw->pw_dir, 1); - setenv("SHELL", shell, 1); - setenv("USER", pw->pw_name, 1); - setenv("LOGNAME", pw->pw_name, 1); - setenv("PATH", ENV_PATH, 1); - if (chdir(pw->pw_dir) < 0) - eprintf("chdir %s:", pw->pw_dir); - execlp(shell, shell, "-l", NULL); - weprintf("execlp %s:", shell); - return (errno == ENOENT) ? 127 : 126; + char *shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell; + + if (preserve == 0) + clearenv(); + setenv("HOME", pw->pw_dir, 1); + setenv("SHELL", shell, 1); + setenv("USER", pw->pw_name, 1); + setenv("LOGNAME", pw->pw_name, 1); + setenv("PATH", ENV_PATH, 1); + if (chdir(pw->pw_dir) < 0) + eprintf("chdir %s:", pw->pw_dir); + execlp(shell, shell, "-l", NULL); + weprintf("execlp %s:", shell); + return (errno == ENOENT) ? 127 : 126; } static void usage(void) { - eprintf("usage: %s [-p] username\n", argv0); + eprintf("usage: %s [-p] username\n", argv0); } int main(int argc, char *argv[]) { - struct passwd *pw; - char *pass, *user; - char *tty; - uid_t uid; - gid_t gid; - int pflag = 0; - - ARGBEGIN { - case 'p': - pflag = 1; - break; - default: - usage(); - } ARGEND; - - if (argc < 1) - usage(); - - if (isatty(0) == 0 || isatty(1) == 0 || isatty(2) == 0) - eprintf("no tty"); - - user = argv[0]; - errno = 0; - pw = getpwnam(user); - if (!pw) { - if (errno) - eprintf("getpwnam %s:", user); - else - eprintf("who are you?\n"); - } - - uid = pw->pw_uid; - gid = pw->pw_gid; - - /* Flush pending input */ - ioctl(0, TCFLSH, (void *)0); - - pass = getpass("Password: "); - if (!pass) - eprintf("getpass:"); - if (pw_check(pw, pass) <= 0) - exit(1); - - tty = ttyname(0); - if (!tty) - eprintf("ttyname:"); - - writeutmp(user, tty); - - if (initgroups(user, gid) < 0) - eprintf("initgroups:"); - if (setgid(gid) < 0) - eprintf("setgid:"); - if (setuid(uid) < 0) - eprintf("setuid:"); - - return dologin(pw, pflag); + struct passwd *pw; + char *pass, *user; + char *tty; + uid_t uid; + gid_t gid; + int pflag = 0; + + ARGBEGIN { + case 'p': + pflag = 1; + break; + default: + usage(); + } ARGEND; + + if (argc < 1) + usage(); + + if (isatty(0) == 0 || isatty(1) == 0 || isatty(2) == 0) + eprintf("no tty"); + + user = argv[0]; + errno = 0; + pw = getpwnam(user); + if (!pw) { + if (errno) + eprintf("getpwnam %s:", user); + else + eprintf("who are you?\n"); + } + + uid = pw->pw_uid; + gid = pw->pw_gid; + + /* Flush pending input */ + ioctl(0, TCFLSH, (void *)0); + + pass = getpass("Password: "); + if (!pass) + eprintf("getpass:"); + if (pw_check(pw, pass) <= 0) + exit(1); + + tty = ttyname(0); + if (!tty) + eprintf("ttyname:"); + + writeutmp(user, tty); + + if (initgroups(user, gid) < 0) + eprintf("initgroups:"); + if (setgid(gid) < 0) + eprintf("setgid:"); + if (setuid(uid) < 0) + eprintf("setuid:"); + + return dologin(pw, pflag); }