1 /* getty - get tty speed Author: Fred van Kempen */
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
11 * Version: 3.4 02/17/90
13 * Author: F. van Kempen, MicroWalt Corporation
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)
20 * Uname(), termios. More nonsense removed. (The result has
21 * only 10% of the original functionality, but a 10x chance of
23 * 12/12/92 Kees J. Bot
25 * Customizable login banner.
26 * 11/13/95 Kees J. Bot
28 * Suspend/resume signals removed.
29 * 2001-04-04 Kees J. Bot
31 * Removed unused custom banner and returned speed option
32 * functionality (by simply calling stty).
33 * 2012-09-24 T. Veerman
36 #include <sys/types.h>
44 #include <sys/utsname.h>
47 char LOGIN
[] = "/usr/bin/login";
48 char SHELL
[] = "/bin/sh";
50 char *tty_name
; /* name of the line */
52 /* Crude indication of a tty being physically secure: */
53 #define securetty(dev) ((unsigned) ((dev) - 0x0400) < (unsigned) 8)
55 void std_out(const char *s
)
57 write(1, s
, strlen(s
));
60 /* Read one character from stdin.
67 /* read character from TTY */
68 st
= read(0, &ch1
, 1);
76 std_out(": read error\n");
83 /* Handle the process of a GETTY.
85 void do_getty(char *name
, size_t len
, char **args
, const char *ttyname
)
87 register char *np
, *s
, *s0
;
89 struct utsname utsname
;
91 static char *def_banner
[] = { "%s Release %r Version %v (%t)\n\n%n login: ", 0 };
93 /* Clean up tty name. */
94 if((t
= strrchr(ttyname
, '/'))) ttyname
= t
+ 1;
96 if (args
[0] != NULL
) {
97 char cmd
[5+6+1]; /* "stty " + "115200" + '\0' */
99 speed
= atoi(args
[0]);
100 snprintf(cmd
, sizeof(cmd
), "stty %d\n", speed
);
104 /* Display prompt. */
108 /* Get data about this machine. */
111 /* Print the banner. */
112 for (banner
= def_banner
; *banner
!= NULL
; banner
++) {
113 std_out(banner
== args
? "\n" : " ");
115 for (s
= *banner
; *s
!= 0; s
++) {
120 case 'n': std_out("\n"); break;
121 case 's': std_out(" "); break;
122 case 't': std_out("\t"); break;
131 case 's': std_out(utsname
.sysname
); break;
132 case 'n': std_out(utsname
.nodename
); break;
133 case 'r': std_out(utsname
.release
); break;
134 case 'v': std_out(utsname
.version
); break;
135 case 'm': std_out(utsname
.machine
); break;
136 case 'p': std_out(utsname
.arch
); break;
137 case 't': std_out(ttyname
); break;
139 case 'k': std_out(utsname
.kernel
); break;
140 case 'h': std_out(utsname
.hostname
); break;
141 case 'b': std_out(utsname
.bus
); break;
153 while ((ch
= readch()) != '\n') {
154 if (np
< name
+ len
) *np
++ = ch
;
157 if (*name
== '\0') ch
= ' '; /* blank line typed! */
162 /* Execute the login(1) command with the current
163 * username as its argument. It will reply to the
164 * calling user by typing "Password: "...
166 void do_login(char *name
)
170 execl(LOGIN
, LOGIN
, name
, (char *) NULL
);
171 /* Failed to exec login. Impossible, but true. Try a shell, but only if
172 * the terminal is more or less secure, because it will be a root shell.
174 std_out("getty: can't exec ");
177 if (fstat(0, &st
) == 0 && S_ISCHR(st
.st_mode
) && securetty(st
.st_rdev
)) {
178 execl(SHELL
, SHELL
, (char *) NULL
);
183 int main(int argc
, char **argv
)
188 /* Don't let QUIT dump core. */
189 sigemptyset(&sa
.sa_mask
);
191 sa
.sa_handler
= exit
;
192 sigaction(SIGQUIT
, &sa
, NULL
);
194 tty_name
= ttyname(0);
195 if (tty_name
== NULL
) {
196 std_out("getty: tty name unknown\n");
201 chown(tty_name
, 0, 0); /* set owner of TTY to root */
202 chmod(tty_name
, 0600); /* mode to max secure */
204 do_getty(name
, sizeof(name
), argv
+1, tty_name
); /* handle getty() */
205 name
[29] = '\0'; /* make sure the name fits! */
207 do_login(name
); /* and call login(1) if OK */
209 return(1); /* never executed */