-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tc.c
745 lines (581 loc) · 15.4 KB
/
tc.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/* program must be invoked from /etc/inittab
*/
#include <errno.h>
#include <ctype.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <dirent.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <netinet/tcp.h>
#include <sys/klog.h>
#include <poll.h>
#include <sys/sysinfo.h>
#include "error.h"
#define DEFAULT_LISTEN_PORT 4095
#define min(x, y) ((x) < (y) ? (x) : (y))
typedef struct
{
int sysrq_fd;
int vcsa0_fd; /* virtual console 0 */
char *dmesg_buffer;
int dmesg_buffer_size;
} parameters_t;
int WRITE(int sock, const char *s, int len)
{
while(len > 0)
{
int rc = write(sock, s, len);
if (rc <= 0)
return -1;
len -= rc;
s += rc;
}
return 0;
}
int sockprint(int fd, char *format, ...)
{
static char buffer[4096];
int len;
va_list ap;
va_start(ap, format);
len = vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
return WRITE(fd, buffer, len);
}
int readchar(int fd)
{
for(;;)
{
char key;
int rc = read(fd, &key, 1);
if (rc <= 0)
break;
return key;
}
return -1;
}
int flush_socket(int fd)
{
int rc = 0;
struct pollfd fds[1] = { { fd, POLLIN, 0 } };
for(;;) {
static char buffer[128];
rc = poll(fds, 1, 0);
if (rc <= 0)
break;
rc = read(fd, buffer, sizeof(buffer));
if (rc <= 0) {
rc = -1;
break;
}
}
return rc;
}
char *get_string(int fd)
{
static char buffer[4096];
size_t len = 0;
if (flush_socket(fd) == -1)
return NULL;
do
{
int key = readchar(fd);
if (key == -1)
return NULL;
if (key == 0xff) { // flush telnet prot
readchar(fd);
readchar(fd);
continue;
}
if (key == 10 || key == 13)
break;
buffer[len++] = key;
}
while(len < (sizeof(buffer) - 1));
buffer[len] = 0x00;
return buffer;
}
int ec_help(int fd)
{
int rc = 0;
rc |= sockprint(fd, "tcpconsole v " VERSION ", (C) 2009-2022 by [email protected]\r\n");
rc |= sockprint(fd, "h: this help\r\n");
rc |= sockprint(fd, "d: dump virtual console 0\r\n");
rc |= sockprint(fd, "j: 'kill -9' for a given pid\r\n");
rc |= sockprint(fd, "k: 'killall -9' for a given name\r\n");
rc |= sockprint(fd, "l: dump dmesg\r\n");
rc |= sockprint(fd, "m: dump dmesg & clear dmesg buffer\r\n");
rc |= sockprint(fd, "p: process list\r\n");
rc |= sockprint(fd, "i: show system (e.g. load)\r\n");
rc |= sockprint(fd, "1-8: set dmesg loglevel\r\n");
rc |= sockprint(fd, "q: log off\r\n");
rc |= sockprint(fd, "\r\nSysreq:\r\n");
rc |= sockprint(fd, "B - boot\r\n");
rc |= sockprint(fd, "C - kexec\r\n");
rc |= sockprint(fd, "D - list all locks\r\n");
rc |= sockprint(fd, "E - SIGTERM to all but init, I - SIGKILL to all but init\r\n");
rc |= sockprint(fd, "F - call oom_kill\r\n");
rc |= sockprint(fd, "L - backtrace\r\n");
rc |= sockprint(fd, "M - memory info dump, P - register/flags dump\r\n");
rc |= sockprint(fd, "O - switch off\r\n");
rc |= sockprint(fd, "Q - list hrtimers\r\n");
rc |= sockprint(fd, "S - SYNC, U - umount\r\n");
rc |= sockprint(fd, "T - tasklist dump, W - unint. tasks dump\r\n");
rc |= sockprint(fd, "See https://docs.kernel.org/admin-guide/sysrq.html for a complete list.\r\n");
return rc;
}
int sockerror(int fd, char *what)
{
return sockprint(fd, "error on %s: %s (%d)\r\n", what, strerror(errno), errno);
}
int dump_virtual_console(int fd_out, int fd_in)
{
struct{ char lines, cols, x, y; } scrn;
int x, y;
if (lseek(fd_in, 0, SEEK_SET) == -1)
return sockerror(fd_out, "lseek");
if (read(fd_in, &scrn, 4) == -1)
return sockerror(fd_out, "read on vcs");
for(y=0; y<scrn.lines; y++)
{
int nspaces = 0;
for(x=0; x<scrn.cols; x++)
{
int loop;
char ca[2];
if (read(fd_in, ca, 2) == -1)
return sockerror(fd_out, "read on vcs (data)");
if (ca[0] != ' ')
{
for(loop=0; loop<nspaces; loop++)
sockprint(fd_out, " ");
nspaces = 0;
sockprint(fd_out, "%c", ca[0]);
}
else
{
nspaces++;
}
}
if (sockprint(fd_out, "\r\n") == -1)
return -1;
}
return 0;
}
int dump_dmesg(int fd, char *dmesg_buffer, int dmesg_buffer_size, char clear)
{
int loop, rc = 0;
int nread = klogctl(clear?4:3, dmesg_buffer, dmesg_buffer_size);
if (nread <= 0)
return sockerror(fd, "klogctl(3)");
for(loop=0; loop<nread; loop++) {
if (dmesg_buffer[loop] == 10) {
char cr = 13;
rc = WRITE(fd, &cr, 1);
}
if (dmesg_buffer[loop] == 10 || (dmesg_buffer[loop] >= 32 && dmesg_buffer[loop] < 127))
rc = WRITE(fd, &dmesg_buffer[loop], 1);
if (rc)
break;
}
return rc;
}
int set_dmesg_loglevel(int fd, int level)
{
if (klogctl(8, NULL, level) == -1)
return sockerror(fd, "klogctl(8)");
return sockprint(fd, "dmesg loglevel set to %d\r\n", level);
}
int dump_loadavg_uptime(int fd)
{
struct sysinfo si;
double avg[3] = { 0, 0, 0 };
if (getloadavg(avg, 3) == -1)
return sockerror(fd, "getloadavg(3)");
if (sysinfo(&si) == -1)
return sockerror(fd, "sysinfo(2)");
return sockprint(fd, "load: 1min: %f, 5min: %f, 15min: %f, uptime: %d seconds\r\n", avg[0], avg[1], avg[2], si.uptime);
}
int dump_ps(int fd)
{
int rc = 0, tnprocs = 0, tnthreads = 0;
struct dirent *de;
DIR *dirp = opendir("/proc");
while((de = readdir(dirp)) != NULL)
{
if (isdigit(de -> d_name[0]))
{
FILE *fh;
static char path[300];
snprintf(path, sizeof(path), "/proc/%s/stat", de -> d_name);
fh = fopen(path, "r");
if (fh)
{
static char fname[4096], dummystr[2];
int dummy, nthreads, ppid, rss;
fscanf(fh, "%d %s %c %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", &dummy, fname, &dummystr[0], &ppid, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &nthreads, &dummy, &dummy, &dummy, &rss);
rc |= sockprint(fd, "%5s, ppid %5d, # threads: %2d, rss: %5d ", de -> d_name, ppid, nthreads, rss);
tnprocs++;
tnthreads += nthreads;
}
if (fh)
{
fclose(fh);
snprintf(path, sizeof(path), "/proc/%s/cmdline", de -> d_name);
fh = fopen(path, "r");
}
if (fh)
{
int len, loop;
static char non_ret_buffer[4096];
len = fread(non_ret_buffer, 1, sizeof(non_ret_buffer) - 1, fh);
if (len < 0)
rc |= sockerror(fd, "fread");
else
{
non_ret_buffer[len] = 0x00;
for(loop=0; loop<len; loop++)
{
if (non_ret_buffer[loop] == 0x00)
non_ret_buffer[loop] = ' ';
}
rc |= sockprint(fd, "%d %s\r\n", len, non_ret_buffer);
}
fclose(fh);
}
else
{
rc |= sockprint(fd, "Error opening %s\r\n", path);
}
if (rc) break;
}
}
closedir(dirp);
rc |= sockprint(fd, "# procs: %d, # threads: %d\r\n", tnprocs, tnthreads);
return rc;
}
int do_sysreq(int fd, char key, int sysreq_fd)
{
int yn;
if (key < 'a' || key > 'z')
return sockprint(fd, "key out of range\r\n");
if (sockprint(fd, "Send %c to sysreq? (y/n)\r\n", key) == -1)
return -1;
do
{
yn = readchar(fd);
}
while(yn != 'y' && yn != 'n' && yn != -1);
if (yn == 'y')
{
if (WRITE(sysreq_fd, &key, 1) == -1)
return sockerror(fd, "WRITE(sysreq_fd)");
}
return yn == -1 ? -1 : 0;
}
int kill_one_proc(int client_fd)
{
int rc = 0;
pid_t pid;
char *entered;
if (sockprint(client_fd, "Process id (PID, q to abort): ") == -1)
return -1;
entered = get_string(client_fd);
if (!entered)
return -1;
if (strcmp(entered, "q") == 0)
return 0;
pid = atoi(entered);
rc = sockprint(client_fd, "Killing pid %d\r\n", pid);
if (kill(pid, SIGTERM) == -1)
rc |= sockerror(client_fd, "kill(-9)");
return rc;
}
int kill_procs(int client_fd)
{
int nprocs = 0;
struct dirent *de;
DIR *dirp = opendir("/proc");
char *entered;
if (sockprint(client_fd, "Process name (q to abort): ") == -1)
return -1;
entered = get_string(client_fd);
if (!entered)
return -1;
if (strcmp(entered, "q") == 0)
return 0;
if (sockprint(client_fd, "\r\nKilling proces %s\r\n", entered) == -1)
return -1;
while((de = readdir(dirp)) != NULL)
{
if (isdigit(de -> d_name[0]))
{
FILE *fh;
static char path[300];
snprintf(path, sizeof(path), "/proc/%s/stat", de -> d_name);
fh = fopen(path, "r");
if (fh)
{
static char fname[4096], dummystr[2], *pdummy;
int dummy;
fscanf(fh, "%d %s %c %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", &dummy, fname, &dummystr[0], &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy);
pdummy = strrchr(fname, ')');
if (pdummy) *pdummy = 0x00;
pdummy = fname + 1;
if (strcmp(pdummy, entered) == 0)
{
pid_t pid = atoi(de -> d_name);
if (sockprint(client_fd, "Killing pid %d\r\n", pid) == -1)
break;
if (kill(pid, SIGTERM) == -1)
{
if (sockerror(client_fd, "kill(-9)") == -1)
break;
}
nprocs++;
}
fclose(fh);
}
}
}
closedir(dirp);
return sockprint(client_fd, "Terminated %d processes\r\n", nprocs);
}
void serve_client(int fd, parameters_t *pars)
{
sockprint(fd, "Enter 'h' for help\r\n");
for(;;)
{
int key;
if (sockprint(fd, "emergency console > ") == -1)
break;
if ((key = readchar(fd)) == -1)
break;
if (key < 32 || key > 126)
{
if (sockprint(fd, "\r") == -1)
break;
continue;
}
if (sockprint(fd, "%c\r\n", key) == -1)
break;
switch(key)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (set_dmesg_loglevel(fd, key - '0') == -1)
return;
break;
case 'd':
if (dump_virtual_console(fd, pars -> vcsa0_fd) == -1)
return;
break;
case '?':
case 'h':
if (ec_help(fd) == -1)
return;
break;
case 'i':
if (dump_loadavg_uptime(fd) == -1)
return;
break;
case 'j':
if (kill_one_proc(fd) == -1)
return;
break;
case 'k':
if (kill_procs(fd) == -1)
return;
break;
case 'l':
if (dump_dmesg(fd, pars -> dmesg_buffer, pars -> dmesg_buffer_size, 0) == -1)
return;
break;
case 'm':
if (dump_dmesg(fd, pars -> dmesg_buffer, pars -> dmesg_buffer_size, 1) == -1)
return;
break;
case 'p':
if (dump_ps(fd) == -1)
return;
break;
case 'q':
return;
case 10:
case 13:
break;
default:
if (isupper(key))
do_sysreq(fd, tolower(key), pars -> sysrq_fd);
else
sockprint(fd, "'%c' is not understood\r\n", key);
break;
}
}
}
int verify_password(int client_fd, char *password)
{
char *entered = NULL;
const char suppress_goahead[] = { 0xff, 0xfb, 0x03 };
const char dont_linemode[] = { 0xff, 0xfe, 0x22 };
const char dont_new_env[] = { 0xff, 0xfe, 0x27 };
const char will_echo[] = { 0xff, 0xfb, 0x01 };
const char dont_echo[] = { 0xff, 0xfe, 0x01 };
WRITE(client_fd, suppress_goahead, sizeof suppress_goahead );
WRITE(client_fd, dont_linemode, sizeof dont_linemode );
WRITE(client_fd, dont_new_env, sizeof dont_new_env);
WRITE(client_fd, will_echo, sizeof will_echo);
WRITE(client_fd, dont_echo, sizeof dont_echo);
if (sockprint(client_fd, "Password: ") == -1)
return -1;
entered = get_string(client_fd);
if (!entered)
return -1;
if (strcmp(password, entered) == 0)
return 0;
sockprint(client_fd, "Invalid password");
return -1;
}
void listen_on_socket(int port, parameters_t *pars, char *password)
{
int server_fd;
struct sockaddr_in server_addr;
int on = 1, optlen, sec60 = 60;
memset(&server_addr, 0x00, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = INADDR_ANY;
server_fd = socket(PF_INET, SOCK_STREAM, 0);
if (server_fd == -1)
error_exit("error creating socket");
if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1)
error_exit("bind() failed");
if (listen(server_fd, SOMAXCONN))
error_exit("listen(%d) failed", SOMAXCONN);
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
error_exit("setsockopt(SO_REUSEADDR) failed");
if (setsockopt(server_fd, IPPROTO_TCP, TCP_KEEPIDLE, &sec60, sizeof(sec60)) == -1)
error_exit("setsockopt(TCP_KEEPIDLE) failed");
if (setsockopt(server_fd, IPPROTO_TCP, TCP_KEEPINTVL, &sec60, sizeof(sec60)) == -1)
error_exit("setsockopt(TCP_KEEPINTVL) failed");
syslog(LOG_PID|LOG_DAEMON, "Listening on %d", port);
for(;;)
{
struct sockaddr_in client_addr;
socklen_t client_addr_size = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_size);
if (client_fd == -1)
{
if (errno == EINTR)
continue;
sleep(1);
continue;
}
optlen = sizeof(on);
if(setsockopt(client_fd, SOL_SOCKET, SO_KEEPALIVE, &on, optlen) == -1)
{
if (sockerror(client_fd, "setsockopt(SO_KEEPALIVE)") == -1)
{
close(client_fd);
continue;
}
}
if (verify_password(client_fd, password) == 0)
serve_client(client_fd, pars);
close(client_fd);
}
}
void write_pidfile(char *file)
{
FILE *pidf = fopen(file, "w");
if (pidf == NULL)
error_exit("Error creating pid-file %s\n", file);
fprintf(pidf, "%d\n", getpid());
fclose(pidf);
}
int open_file(char *path, int mode)
{
int fd = open(path, mode);
if (fd == -1)
error_exit("Open_file(%s) failed", path);
return fd;
}
char * read_password(char *file)
{
char buffer[128], *pw, *lf;
struct stat buf;
int fd = open_file(file, O_RDONLY), rc;
if (fstat(fd, &buf) == -1)
error_exit("fstat(%s) failed", file);
rc = read(fd, buffer, sizeof(buffer) - 1);
if (rc == -1)
error_exit("error reading password");
buffer[rc] = 0x00;
lf = strchr(buffer, '\n');
if (lf) *lf = 0x00;
close(fd);
pw = strdup(buffer);
if (!pw)
error_exit("strdup() failed");
return pw;
}
int main(int argc, char *argv[])
{
char *password;
int port = DEFAULT_LISTEN_PORT;
parameters_t pars;
struct sched_param sched_par;
openlog("tcpconsole", LOG_CONS|LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_DAEMON);
if (getuid())
error_exit("This program must be invoked with root-rights.");
password = read_password("/etc/tcpconsole.pw");
if (signal(SIGTERM, SIG_IGN) == SIG_ERR)
error_exit("signal(SIGTERM) failed");
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
error_exit("signal(SIGHUP) failed");
pars.sysrq_fd = open_file("/proc/sysrq-trigger", O_WRONLY);
pars.vcsa0_fd = open_file("/dev/vcsa", O_RDONLY);
if (setpriority(PRIO_PROCESS, 0, -10) == -1)
error_exit("Setpriority failed");
if (nice(-20) == -1)
error_exit("Failed to set nice-value to -20");
if (mlockall(MCL_CURRENT) == -1 || mlockall(MCL_FUTURE) == -1)
error_exit("Failed to lock program in core");
memset(&sched_par, 0x00, sizeof(sched_par));
sched_par.sched_priority = sched_get_priority_max(SCHED_RR);
if (sched_setscheduler(0, SCHED_RR, &sched_par) == -1)
error_exit("Failed to set scheduler properties for this process");
syslog(LOG_INFO, "tcpconsole started");
write_pidfile("/var/run/tcpconsole.pid");
if ((pars.dmesg_buffer_size = klogctl(10, NULL, 0)) == -1)
error_exit("klogctl(10) failed");
pars.dmesg_buffer = (char *)malloc(pars.dmesg_buffer_size + 1);
if (!pars.dmesg_buffer)
error_exit("malloc failure");
listen_on_socket(port, &pars, password);
return 1;
}