[9] | 1 | /* getty - get tty speed Author: Fred van Kempen */
|
---|
| 2 |
|
---|
| 3 | /*
|
---|
| 4 | * GETTY - Initialize and serve a login-terminal for INIT.
|
---|
| 5 | * Also, select the correct speed. The STTY() code
|
---|
| 6 | * was taken from stty(1).c; which was written by
|
---|
| 7 | * Andrew S. Tanenbaum.
|
---|
| 8 | *
|
---|
| 9 | * Usage: getty [-c filename] [-h] [-k] [-t] line [speed]
|
---|
| 10 | *
|
---|
| 11 | * Version: 3.4 02/17/90
|
---|
| 12 | *
|
---|
| 13 | * Author: F. van Kempen, MicroWalt Corporation
|
---|
| 14 | *
|
---|
| 15 | * Modifications:
|
---|
| 16 | * All the good stuff removed to get a minimal getty, because
|
---|
| 17 | * many modems don't like all that fancy speed detection stuff.
|
---|
| 18 | * 03/03/91 Kees J. Bot (kjb@cs.vu.nl)
|
---|
| 19 | *
|
---|
| 20 | * Uname(), termios. More nonsense removed. (The result has
|
---|
| 21 | * only 10% of the original functionality, but a 10x chance of
|
---|
| 22 | * working.)
|
---|
| 23 | * 12/12/92 Kees J. Bot
|
---|
| 24 | *
|
---|
| 25 | * Customizable login banner.
|
---|
| 26 | * 11/13/95 Kees J. Bot
|
---|
| 27 | *
|
---|
| 28 | * Suspend/resume signals removed.
|
---|
| 29 | * 2001-04-04 Kees J. Bot
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | #include <sys/types.h>
|
---|
| 33 | #include <sys/stat.h>
|
---|
| 34 | #include <unistd.h>
|
---|
| 35 | #include <signal.h>
|
---|
| 36 | #include <stdlib.h>
|
---|
| 37 | #include <string.h>
|
---|
| 38 | #include <fcntl.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <sys/utsname.h>
|
---|
| 41 |
|
---|
| 42 | char LOGIN[] = "/usr/bin/login";
|
---|
| 43 | char SHELL[] = "/bin/sh";
|
---|
| 44 |
|
---|
| 45 | char *tty_name; /* name of the line */
|
---|
| 46 |
|
---|
| 47 | /* Crude indication of a tty being physically secure: */
|
---|
| 48 | #define securetty(dev) ((unsigned) ((dev) - 0x0400) < (unsigned) 8)
|
---|
| 49 |
|
---|
| 50 | void std_out(char *s)
|
---|
| 51 | {
|
---|
| 52 | write(1, s, strlen(s));
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /* Read one character from stdin.
|
---|
| 56 | */
|
---|
| 57 | int readch(void)
|
---|
| 58 | {
|
---|
| 59 | int st;
|
---|
| 60 | char ch1;
|
---|
| 61 |
|
---|
| 62 | /* read character from TTY */
|
---|
| 63 | st = read(0, &ch1, 1);
|
---|
| 64 | if (st == 0) {
|
---|
| 65 | std_out("\n");
|
---|
| 66 | exit(0);
|
---|
| 67 | }
|
---|
| 68 | if (st < 0) {
|
---|
| 69 | std_out("getty: ");
|
---|
| 70 | std_out(tty_name);
|
---|
| 71 | std_out(": read error\n");
|
---|
| 72 | exit(1);
|
---|
| 73 | }
|
---|
| 74 | return(ch1 & 0xFF);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | /* Handle the process of a GETTY.
|
---|
| 79 | */
|
---|
| 80 | void do_getty(char *name, size_t len, char **args, char *ttyname)
|
---|
| 81 | {
|
---|
| 82 | register char *np, *s, *s0;
|
---|
| 83 | int ch;
|
---|
| 84 | struct utsname utsname;
|
---|
| 85 | char **banner, *t;
|
---|
| 86 | static char *def_banner[] = { "%s Release %r Version %v (%t)\n\n%n login: ", 0 };
|
---|
| 87 |
|
---|
| 88 | /* Clean up tty name. */
|
---|
| 89 | if((t = strrchr(ttyname, '/'))) ttyname = t + 1;
|
---|
| 90 |
|
---|
| 91 | /* Default banner? */
|
---|
| 92 | if (args[0] == NULL) args = def_banner;
|
---|
| 93 |
|
---|
| 94 | /* Display prompt. */
|
---|
| 95 | ch = ' ';
|
---|
| 96 | *name = '\0';
|
---|
| 97 | while (ch != '\n') {
|
---|
| 98 | /* Get data about this machine. */
|
---|
| 99 | uname(&utsname);
|
---|
| 100 |
|
---|
| 101 | /* Print the banner. */
|
---|
| 102 | for (banner = args; *banner != NULL; banner++) {
|
---|
| 103 | std_out(banner == args ? "\n" : " ");
|
---|
| 104 | s0 = *banner;
|
---|
| 105 | for (s = *banner; *s != 0; s++) {
|
---|
| 106 | if (*s == '\\') {
|
---|
| 107 | write(1, s0, s-s0);
|
---|
| 108 | s0 = s+2;
|
---|
| 109 | switch (*++s) {
|
---|
| 110 | case 'n': std_out("\n"); break;
|
---|
| 111 | case 's': std_out(" "); break;
|
---|
| 112 | case 't': std_out("\t"); break;
|
---|
| 113 | case 0: goto leave;
|
---|
| 114 | default: s0 = s;
|
---|
| 115 | }
|
---|
| 116 | } else
|
---|
| 117 | if (*s == '%') {
|
---|
| 118 | write(1, s0, s-s0);
|
---|
| 119 | s0 = s+2;
|
---|
| 120 | switch (*++s) {
|
---|
| 121 | case 's': std_out(utsname.sysname); break;
|
---|
| 122 | case 'n': std_out(utsname.nodename); break;
|
---|
| 123 | case 'r': std_out(utsname.release); break;
|
---|
| 124 | case 'v': std_out(utsname.version); break;
|
---|
| 125 | case 'm': std_out(utsname.machine); break;
|
---|
| 126 | case 'p': std_out(utsname.arch); break;
|
---|
| 127 | case 't': std_out(ttyname); break;
|
---|
| 128 | #if __minix_vmd
|
---|
| 129 | case 'k': std_out(utsname.kernel); break;
|
---|
| 130 | case 'h': std_out(utsname.hostname); break;
|
---|
| 131 | case 'b': std_out(utsname.bus); break;
|
---|
| 132 | #endif
|
---|
| 133 | case 0: goto leave;
|
---|
| 134 | default: s0 = s-1;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | leave:
|
---|
| 139 | write(1, s0, s-s0);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | np = name;
|
---|
| 143 | while ((ch = readch()) != '\n') {
|
---|
| 144 | if (np < name + len) *np++ = ch;
|
---|
| 145 | }
|
---|
| 146 | *np = '\0';
|
---|
| 147 | if (*name == '\0') ch = ' '; /* blank line typed! */
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | /* Execute the login(1) command with the current
|
---|
| 153 | * username as its argument. It will reply to the
|
---|
| 154 | * calling user by typing "Password: "...
|
---|
| 155 | */
|
---|
| 156 | void do_login(char *name)
|
---|
| 157 | {
|
---|
| 158 | struct stat st;
|
---|
| 159 |
|
---|
| 160 | execl(LOGIN, LOGIN, name, (char *) NULL);
|
---|
| 161 | /* Failed to exec login. Impossible, but true. Try a shell, but only if
|
---|
| 162 | * the terminal is more or less secure, because it will be a root shell.
|
---|
| 163 | */
|
---|
| 164 | std_out("getty: can't exec ");
|
---|
| 165 | std_out(LOGIN);
|
---|
| 166 | std_out("\n");
|
---|
| 167 | if (fstat(0, &st) == 0 && S_ISCHR(st.st_mode) && securetty(st.st_rdev)) {
|
---|
| 168 | execl(SHELL, SHELL, (char *) NULL);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | int main(int argc, char **argv)
|
---|
| 174 | {
|
---|
| 175 | register char *s;
|
---|
| 176 | char name[30];
|
---|
| 177 | struct sigaction sa;
|
---|
| 178 |
|
---|
| 179 | /* Don't let QUIT dump core. */
|
---|
| 180 | sigemptyset(&sa.sa_mask);
|
---|
| 181 | sa.sa_flags = 0;
|
---|
| 182 | sa.sa_handler = exit;
|
---|
| 183 | sigaction(SIGQUIT, &sa, NULL);
|
---|
| 184 |
|
---|
| 185 | tty_name = ttyname(0);
|
---|
| 186 | if (tty_name == NULL) {
|
---|
| 187 | std_out("getty: tty name unknown\n");
|
---|
| 188 | pause();
|
---|
| 189 | return(1);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | chown(tty_name, 0, 0); /* set owner of TTY to root */
|
---|
| 193 | chmod(tty_name, 0600); /* mode to max secure */
|
---|
| 194 |
|
---|
| 195 | do_getty(name, sizeof(name), argv+1, tty_name); /* handle getty() */
|
---|
| 196 | name[29] = '\0'; /* make sure the name fits! */
|
---|
| 197 |
|
---|
| 198 | do_login(name); /* and call login(1) if OK */
|
---|
| 199 |
|
---|
| 200 | return(1); /* never executed */
|
---|
| 201 | }
|
---|