-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathegetty.c
463 lines (394 loc) · 8.79 KB
/
egetty.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* File: egetty.c
* Implements: ethernet getty
*
* Copyright: Jens Låås, 2011
* Copyright license: According to GPL, see file COPYING in this directory.
*
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <sys/types.h>
#include <fcntl.h>
#include <poll.h>
#include <pty.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h> /* the L2 protocols */
#include <net/if_arp.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <net/if.h>
#include "egetty.h"
#include "skbuff.h"
static char **envp;
struct {
int console;
char *device;
int kmsg;
int waitif;
int debug;
struct sockaddr_ll client;
int devsocket;
} conf;
char *indextoname(unsigned int ifindex)
{
static char ifname[IF_NAMESIZE+1];
if(if_indextoname(ifindex, ifname))
return ifname;
return "ENXIO";
}
static int devsocket(void)
{
/* we couldn't care less which one; just want to talk to the kernel! */
static int dumb[3] = { AF_INET, AF_PACKET, AF_INET6 };
int i, fd;
for(i=0; i<3; i++)
if((fd = socket(dumb[i], SOCK_DGRAM, 0)) >= 0)
return fd;
return -1;
}
/* Set a certain interface flag. */
static int set_flag(char *ifname, short flag)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ifname);
if (ioctl(conf.devsocket, SIOCGIFFLAGS, &ifr) < 0) {
return -1;
}
strcpy(ifr.ifr_name, ifname);
ifr.ifr_flags |= flag;
if (ioctl(conf.devsocket, SIOCSIFFLAGS, &ifr) < 0) {
return -1;
}
return 0;
}
/* Check interface flag. */
static int check_flag(char *ifname, short flag)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ifname);
if (ioctl(conf.devsocket, SIOCGIFFLAGS, &ifr) < 0) {
return -1;
}
return (ifr.ifr_flags & flag) != flag;
}
int putfd(int fd, char *s)
{
if(s)
write(fd, s, strlen(s));
return 0;
}
pid_t login(int *fd)
{
pid_t pid;
int amaster, tty, rc=0;
char name[256];
pid = forkpty(&amaster, name,
NULL, NULL);
if(pid == 0) {
/* child */
if(conf.kmsg) {
if ((rc=ioctl(0, TIOCCONS, 0))) {
if(conf.debug) {
putfd(1, "TIOCCONS: ");
putfd(1, strerror(errno));
}
if (errno == EBUSY) {
tty = open("/dev/tty0", O_WRONLY);
if (tty >= 0) {
if ((rc=ioctl(tty, TIOCCONS, 0))==0) {
rc=ioctl(0, TIOCCONS, 0);
if(conf.debug && rc) {
putfd(1, "TIOCCONS: ");
putfd(1, strerror(errno));
}
}
close(tty);
} else
rc = -1;
}
}
if(conf.debug) {
if(rc)
putfd(1, "failed to redirect console\n");
else
putfd(1, "redirected console\n");
}
}
char *argv[]={"/bin/login", "--", 0, 0};
(void) execve( argv[0], argv, envp );
printf("execve failed\n");
exit(1);
}
if(pid == -1) return -1;
*fd = amaster;
return pid;
}
int console_ucast(int s, int ifindex, struct sk_buff *skb)
{
struct sockaddr_ll dest;
socklen_t destlen = sizeof(dest);
memset(&dest, 0, sizeof(dest));
dest.sll_family = AF_PACKET;
dest.sll_halen = 6;
dest.sll_protocol = htons(ETH_P_EGETTY);
dest.sll_ifindex = ifindex;
memcpy(dest.sll_addr, conf.client.sll_addr, 6);
return sendto(s, skb->data, skb->len, 0, (const struct sockaddr *)&dest, destlen);
}
int console_bcast(int s, int ifindex, struct sk_buff *skb)
{
struct sockaddr_ll dest;
socklen_t destlen = sizeof(dest);
memset(&dest, 0, sizeof(dest));
dest.sll_family = AF_PACKET;
dest.sll_halen = 6;
dest.sll_protocol = htons(ETH_P_EGETTY);
dest.sll_ifindex = ifindex;
memset(dest.sll_addr, 255, 6);
return sendto(s, skb->data, skb->len, 0, (const struct sockaddr *)&dest, destlen);
}
int console_put(int s, int ifindex, struct sk_buff *skb)
{
int rc;
uint8_t *p;
p = skb_push(skb, 4);
*p++ = EGETTY_OUT;
*p++ = conf.console;
*p++ = skb->len >> 8;
*p = skb->len & 0xff;
rc = console_ucast(s, ifindex, skb);
if(rc == -1) {
printf("sendto failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
int console_hello(int s, int ifindex, struct sk_buff *skb)
{
int rc;
uint8_t *p;
p = skb_push(skb, 4);
*p++ = EGETTY_HELLO;
*p++ = conf.console;
*p++ = skb->len >> 8;
*p = skb->len & 0xff;
rc = console_bcast(s, ifindex, skb);
if(rc == -1) {
printf("sendto failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
int main(int argc, char **argv, char **arge)
{
int s;
struct sockaddr_ll from;
socklen_t fromlen = sizeof(from);
int ifindex=-1;
uint8_t *buf, *p;
ssize_t n;
unsigned int len;
int count=1;
int timeout = -1;
int loginfd = -1;
pid_t pid=-1;
struct sk_buff *skb;
envp = arge;
conf.debug = 0;
conf.device = "eth0";
conf.devsocket = -1;
while(--argc > 0) {
if(strcmp(argv[argc], "debug")==0) {
printf("Debug mode\n");
conf.debug = 1;
continue;
}
if(strcmp(argv[argc], "console")==0) {
conf.kmsg = 1;
continue;
}
if(strcmp(argv[argc], "waitif")==0) {
conf.waitif = 1;
continue;
}
if( (strlen(argv[argc]) < 3) && isdigit(*argv[argc])) {
conf.console = atoi(argv[argc]);
continue;
}
conf.device = argv[argc];
}
conf.devsocket = devsocket();
if(conf.waitif) {
/* wait for interface to become available */
while(check_flag(conf.device, (IFF_UP | IFF_RUNNING))) {
sleep(1);
}
} else {
/* active interface */
while(set_flag(conf.device, (IFF_UP | IFF_RUNNING))) {
sleep(1);
}
}
memset(conf.client.sll_addr, 255, 6);
s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_EGETTY));
if(s == -1)
{
fprintf(stderr, "socket(): %s\n", strerror(errno));
exit(1);
}
if(conf.device)
{
ifindex = if_nametoindex(conf.device);
if(!ifindex)
{
fprintf(stderr, "Not an interface\n");
exit(1);
}
}
if(ifindex >= 0)
{
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(addr));
addr.sll_family = AF_PACKET;
addr.sll_protocol = htons(ETH_P_EGETTY);
addr.sll_ifindex = ifindex;
if(bind(s, (const struct sockaddr *)&addr, sizeof(addr)))
{
fprintf(stderr, "bind() to interface failed\n");
exit(1);
}
}
skb = alloc_skb(1500);
skb_reset(skb);
skb_reserve(skb, 4);
console_hello(s, ifindex, skb);
while(count)
{
struct pollfd fds[2];
if(pid) {
int status;
if(waitpid(-1, &status, WNOHANG)>0) {
pid = -1;
close(loginfd);
}
}
if(pid == -1) {
pid = login(&loginfd);
if(pid == -1)
exit(1);
if(conf.debug) {
printf("child pid = %d\n", pid);
printf("loginfd = %d\n", loginfd);
}
}
fds[0].fd = s;
fds[0].events = POLLIN;
fds[0].revents = 0;
fds[1].fd = loginfd;
fds[1].events = POLLIN;
fds[1].revents = 0;
n = poll(fds, 2, timeout);
if(n == 0) {
printf("timeout\n");
exit(1);
}
if(fds[1].revents & POLLIN) {
if(conf.debug) printf("POLLIN child\n");
skb_reset(skb);
skb_reserve(skb, 4);
buf = skb_put(skb, 0);
n = read(loginfd, buf, skb_tailroom(skb));
if(n == -1) {
fprintf(stderr, "read() failed\n");
exit(1);
}
buf[n] = 0;
if(conf.debug)
printf("child: %d bytes\n", n);
skb_put(skb, n);
console_put(s, ifindex, skb);
}
if(fds[0].revents) {
skb_reset(skb);
buf = skb_put(skb, 0);
n = recvfrom(s, buf, skb_tailroom(skb), 0, (struct sockaddr *)&from, &fromlen);
if(n == -1) {
fprintf(stderr, "recvfrom() failed. ifconfig up?\n");
exit(1);
}
skb_put(skb, n);
if(conf.debug) printf("received packet %d bytes\n", skb->len);
if(ntohs(from.sll_protocol) == ETH_P_EGETTY) {
if(conf.debug)
printf("Received EGETTY\n");
p = skb->data;
if(*p == EGETTY_HUP) {
if(pid != -1) kill(pid, 9);
continue;
}
if(*p == EGETTY_SCAN) {
skb_reset(skb);
skb_reserve(skb, 4);
console_hello(s, ifindex, skb);
continue;
}
if(*p == EGETTY_WINCH) {
p++;
if(*p != conf.console) {
if(conf.debug)
printf("Wrong console %d not %d\n", *p, conf.console);
continue;
}
p++;
{
struct winsize winp;
winp.ws_row = *p++;
winp.ws_col = *p++;
winp.ws_xpixel = 0;
winp.ws_ypixel = 0;
ioctl(loginfd, TIOCSWINSZ, &winp);
if(conf.debug)
printf("WINCH to %d, %d\n", winp.ws_row, winp.ws_col);
}
continue;
}
if(*p != EGETTY_IN) {
if(conf.debug)
printf("Not EGETTY_IN: %d\n", *p);
continue;
}
p++;
if(*p != conf.console) {
if(conf.debug)
printf("Wrong console %d not %d\n", *p, conf.console);
continue;
}
memcpy(conf.client.sll_addr, from.sll_addr, 6);
p++;
len = *p++ << 8;
len += *p;
if(len > n) {
printf("Length field too long: %d\n", len);
continue;
}
skb_trim(skb, len);
skb_pull(skb, 4);
if(conf.debug) printf("Sent %d bytes to child\n", skb->len);
write(loginfd, skb->data, skb->len);
continue;
}
}
}
exit(0);
}