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