/
consent.c
1544 lines (1415 loc) · 36.7 KB
/
consent.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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright conserver.com, 2000
*
* Maintainer/Enhancer: Bryan Stansell (bryan@conserver.com)
*
* Copyright GNAC, Inc., 1998
*/
/*
* Copyright 1992 Purdue Research Foundation, West Lafayette, Indiana
* 47907. All rights reserved.
*
* Written by Kevin S Braunsdorf, ksb@cc.purdue.edu, purdue!ksb
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company or the Regents of the University of California.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. Neither the authors nor Purdue University are responsible for any
* consequences of the use of this software.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Credit to the authors and Purdue
* University must appear in documentation and sources.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 4. This notice may not be removed or altered.
*/
/*
* Network console modifications by Robert Olson, olson@mcs.anl.gov.
*/
#include <compat.h>
#include <pwd.h>
#include <cutil.h>
#include <consent.h>
#include <client.h>
#include <group.h>
#include <access.h>
#include <readcfg.h>
#include <main.h>
BAUD baud[] = {
#if defined(FOR_CYCLADES_TS)
{"0", 0},
{"50", 1},
{"75", 2},
{"110", 3},
{"134", 4},
{"150", 5},
{"200", 6},
{"300", 7},
{"600", 8},
{"1200", 9},
{"1800", 10},
{"2400", 11},
{"4800", 12},
{"9600", 13},
{"14400", 14},
{"19200", 15},
{"28800", 16},
{"38400", 17},
{"57600", 18},
{"76800", 19},
{"115200", 20},
{"230400", 21},
{"460800", 22},
{"500000", 23},
{"576000", 24},
{"921600", 25},
{"1000000", 26},
{"1152000", 27},
{"1500000", 28},
{"2000000", 29},
{"2500000", 30},
{"3000000", 31},
{"3500000", 32},
{"4000000", 33},
#else /* FOR_CYCLADES_TS */
# if defined(B4000000)
{"4000000", B4000000},
# endif
# if defined(B3500000)
{"3500000", B3500000},
# endif
# if defined(B3000000)
{"3000000", B3000000},
# endif
# if defined(B2500000)
{"2500000", B2500000},
# endif
# if defined(B2000000)
{"2000000", B2000000},
# endif
# if defined(B1500000)
{"1500000", B1500000},
# endif
# if defined(B1152000)
{"1152000", B1152000},
# endif
# if defined(B1000000)
{"1000000", B1000000},
# endif
# if defined(B921600)
{"921600", B921600},
# endif
# if defined(B576000)
{"576000", B576000},
# endif
# if defined(B500000)
{"500000", B500000},
# endif
# if defined(B460800)
{"460800", B460800},
# endif
# if defined(B230400)
{"230400", B230400},
# endif
# if defined(B115200)
{"115200", B115200},
# endif
# if defined(B57600)
{"57600", B57600},
# endif
# if defined(B38400)
{"38400", B38400},
# endif
# if defined(B19200)
{"19200", B19200},
# endif
# if defined(B9600)
{"9600", B9600},
# endif
# if defined(B4800)
{"4800", B4800},
# endif
# if defined(B2400)
{"2400", B2400},
# endif
# if defined(B1800)
{"1800", B1800},
# endif
{"1200", B1200},
# if defined(B600)
{"600", B600},
# endif
# if defined(B300)
{"300", B300},
# endif
#endif /* FOR_CYCLADES_TS */
};
/* find a baud rate for the string "9600x" -> B9600 (ksb)
*/
BAUD *
FindBaud(char *pcMode)
{
int i;
for (i = 0; i < sizeof(baud) / sizeof(struct baud); ++i) {
if (strcmp(pcMode, baud[i].acrate) == 0)
return baud + i;
}
return (BAUD *)0;
}
#if !defined(PAREXT)
# define PAREXT 0
#endif
struct parity parity[] = {
{"even", PARENB | CS7, 0},
{"mark", PARENB | CS7 | PARODD | PAREXT, 0},
{"none", CS8, 0},
{"odd", PARENB | CS7 | PARODD, 0},
{"space", PARENB | CS7 | PAREXT, 0},
};
/* find a parity "even" or "E" or "ev" -> EVEN
*/
PARITY *
FindParity(char *pcMode)
{
int i;
for (i = 0; i < sizeof(parity) / sizeof(struct parity); ++i) {
if (strcasecmp(pcMode, parity[i].key) == 0)
return parity + i;
}
return (PARITY *)0;
}
/* setup a tty device (ksb)
*/
static int
TtyDev(CONSENT *pCE)
{
struct termios termp;
struct stat stPerm;
int cofile;
cofile = FileFDNum(pCE->cofile);
/* here we should fstat for `read-only' checks
*/
if (-1 == fstat(cofile, &stPerm)) {
Error("[%s] fstat(%s(%d)): %s: forcing down", pCE->server,
pCE->device, cofile, strerror(errno));
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return -1;
} else if (0 == (stPerm.st_mode & 0222)) {
/* any device that is read-only we won't write to
*/
pCE->fronly = 1;
}
/*
* Get terminal attributes
*/
if (-1 == tcgetattr(cofile, &termp)) {
Error("[%s] tcgetattr(%s(%d)): %s: forcing down", pCE->server,
pCE->device, cofile, strerror(errno));
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return -1;
}
/*
* Turn off: echo
* icrnl
* opost No post processing
* icanon No line editing
* isig No signal generation
* Turn on: ixoff
*/
termp.c_iflag = BRKINT;
if (pCE->ixon == FLAGTRUE)
termp.c_iflag |= IXON;
if (pCE->ixany == FLAGTRUE)
termp.c_iflag |= IXANY;
if (pCE->ixoff == FLAGTRUE)
termp.c_iflag |= IXOFF;
termp.c_oflag = 0;
/* CLOCAL suggested by egan@us.ibm.com
* carrier transitions result in dropped consoles otherwise
*/
termp.c_cflag = CREAD | CLOCAL;
if (pCE->hupcl == FLAGTRUE)
termp.c_cflag |= HUPCL;
if (pCE->cstopb == FLAGTRUE)
termp.c_cflag |= CSTOPB;
#if defined(CRTSCTS)
if (pCE->crtscts == FLAGTRUE)
termp.c_cflag |= CRTSCTS;
#endif
termp.c_cflag |= pCE->parity->iset;
termp.c_lflag = 0;
/*
* Set the VMIN == 1
* Set the VTIME == 1 (0.1 sec)
* Don't bother with the control characters as they are not used
*/
termp.c_cc[VMIN] = 1;
termp.c_cc[VTIME] = 1;
if (-1 == cfsetospeed(&termp, pCE->baud->irate)) {
Error("[%s] cfsetospeed(%s(%d)): %s: forcing down", pCE->server,
pCE->device, cofile, strerror(errno));
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return -1;
}
if (-1 == cfsetispeed(&termp, pCE->baud->irate)) {
Error("[%s] cfsetispeed(%s(%d)): %s: forcing down", pCE->server,
pCE->device, cofile, strerror(errno));
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return -1;
}
/*
* Set terminal attributes
*/
if (-1 == tcsetattr(cofile, TCSADRAIN, &termp)) {
Error("[%s] tcsetattr(%s(%d),TCSADRAIN): %s: forcing down",
pCE->server, pCE->device, cofile, strerror(errno));
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return -1;
}
if (fDebug >= 2) {
int i;
Debug(2, "TtyDev(): [%s] termp.c_iflag=%lu", pCE->server,
(unsigned long)termp.c_iflag);
Debug(2, "TtyDev(): [%s] termp.c_oflag=%lu", pCE->server,
(unsigned long)termp.c_oflag);
Debug(2, "TtyDev(): [%s] termp.c_cflag=%lu", pCE->server,
(unsigned long)termp.c_cflag);
Debug(2, "TtyDev(): [%s] termp.c_lflag=%lu", pCE->server,
(unsigned long)termp.c_lflag);
#if defined(NCCS)
for (i = 0; i < NCCS; i++) {
Debug(2, "TtyDev(): [%s] termp.c_cc[%d]=%lu", pCE->server, i,
(unsigned long)termp.c_cc[i]);
}
#endif
}
#if HAVE_STROPTS_H
/*
* eat all the streams modules upto and including ttcompat
*/
while (ioctl(cofile, I_FIND, "ttcompat") == 1) {
ioctl(cofile, I_POP, 0);
}
#endif
pCE->fup = 1;
return 0;
}
void
StopInit(CONSENT *pCE)
{
if (pCE->initcmd == (char *)0)
return;
if (pCE->initpid != 0 || pCE->initfile != (CONSFILE *)0)
SendIWaitClientsMsg(pCE,
(pCE->fup &&
pCE->ioState ==
ISNORMAL) ? "up]\r\n" : "down]\r\n");
if (pCE->initpid != 0) {
kill(pCE->initpid, SIGHUP);
CONDDEBUG((1, "StopInit(): sending initcmd pid %lu signal %d",
(unsigned long)pCE->initpid, SIGHUP));
Msg("[%s] initcmd terminated: pid %lu", pCE->server,
(unsigned long)pCE->initpid);
TagLogfileAct(pCE, "initcmd terminated");
pCE->initpid = 0;
}
if (pCE->initfile != (CONSFILE *)0) {
int initfile = FileFDNum(pCE->initfile);
FD_CLR(initfile, &rinit);
initfile = FileFDOutNum(pCE->initfile);
FD_CLR(initfile, &winit);
FileClose(&pCE->initfile);
pCE->initfile = (CONSFILE *)0;
}
}
#if HAVE_FREEIPMI
ipmiconsole_ctx_t
IpmiSOLCreate(CONSENT *pCE)
{
ipmiconsole_ctx_t ctx;
struct ipmiconsole_ipmi_config ipmi;
struct ipmiconsole_protocol_config protocol;
struct ipmiconsole_engine_config engine;
if (ipmiconsole_engine_init(1, 0) < 0)
return 0;
ipmi.username = pCE->username;
ipmi.password = pCE->password;
if (pCE->ipmikg->used <= 1) { /* 1 == NULL only */
ipmi.k_g = NULL;
ipmi.k_g_len = 0;
} else {
ipmi.k_g = (unsigned char *)pCE->ipmikg->string;
ipmi.k_g_len = pCE->ipmikg->used - 1;
}
ipmi.privilege_level = pCE->ipmiprivlevel;
ipmi.cipher_suite_id = pCE->ipmiciphersuite;
ipmi.workaround_flags = pCE->ipmiworkaround;
protocol.session_timeout_len = -1;
protocol.retransmission_timeout_len = -1;
protocol.retransmission_backoff_count = -1;
protocol.keepalive_timeout_len = -1;
protocol.retransmission_keepalive_timeout_len = -1;
protocol.acceptable_packet_errors_count = -1;
protocol.maximum_retransmission_count = -1;
engine.engine_flags = IPMICONSOLE_ENGINE_OUTPUT_ON_SOL_ESTABLISHED;
engine.behavior_flags = 0;
engine.debug_flags = 0;
ctx = ipmiconsole_ctx_create(pCE->host, &ipmi, &protocol, &engine);
return ctx;
}
#endif
/* invoke the initcmd command */
void
StartInit(CONSENT *pCE)
{
int i;
pid_t iNewGrp;
extern char **environ;
int pin[2];
int pout[2];
static char *apcArgv[] = {
"/bin/sh", "-ce", (char *)0, (char *)0
};
if (pCE->initcmd == (char *)0)
return;
/* this should never happen, but hey, just in case */
if (pCE->initfile != (CONSFILE *)0 || pCE->initpid != 0) {
Error("[%s] StartInit(): initpid/initfile sync error",
pCE->server);
StopInit(pCE);
}
/* pin[0] = parent read, pin[1] = child write */
if (pipe(pin) != 0) {
Error("[%s] StartInit(): pipe(): %s", pCE->server,
strerror(errno));
return;
}
/* pout[0] = child read, pout[l] = parent write */
if (pipe(pout) != 0) {
close(pin[0]);
close(pin[1]);
Error("[%s] StartInit(): pipe(): %s", pCE->server,
strerror(errno));
return;
}
fflush(stdout);
fflush(stderr);
switch (pCE->initpid = fork()) {
case -1:
pCE->initpid = 0;
return;
case 0:
thepid = getpid();
break;
default:
close(pout[0]);
close(pin[1]);
if ((pCE->initfile =
FileOpenPipe(pin[0], pout[1])) == (CONSFILE *)0) {
Error("[%s] FileOpenPipe(%d,%d) failed: forcing down",
pCE->server, pin[0], pout[1]);
close(pin[0]);
close(pout[1]);
kill(pCE->initpid, SIGHUP);
pCE->initpid = 0;
return;
}
Msg("[%s] initcmd started: pid %lu", pCE->server,
(unsigned long)pCE->initpid);
TagLogfileAct(pCE, "initcmd started");
FD_SET(pin[0], &rinit);
if (maxfd < pin[0] + 1)
maxfd = pin[0] + 1;
fflush(stderr);
return;
}
close(pin[0]);
close(pout[1]);
/* put the signals back that we ignore (trapped auto-reset to default)
*/
SimpleSignal(SIGQUIT, SIG_DFL);
SimpleSignal(SIGINT, SIG_DFL);
SimpleSignal(SIGPIPE, SIG_DFL);
#if defined(SIGTTOU)
SimpleSignal(SIGTTOU, SIG_DFL);
#endif
#if defined(SIGTTIN)
SimpleSignal(SIGTTIN, SIG_DFL);
#endif
#if defined(SIGTSTP)
SimpleSignal(SIGTSTP, SIG_DFL);
#endif
#if defined(SIGPOLL)
SimpleSignal(SIGPOLL, SIG_DFL);
#endif
/* setup new process with clean file descriptors
*/
#if HAVE_CLOSEFROM
for (i = 3; i <= pout[0] || i <= pin[1]; i++) {
if (i != pout[0] && i != pin[1])
close(i);
}
closefrom(i);
#else
i = GetMaxFiles();
for ( /* i above */ ; --i > 2;) {
if (i != pout[0] && i != pin[1])
close(i);
}
#endif
/* leave 2 until we have to close it */
close(1);
close(0);
#if HAVE_SETSID
iNewGrp = setsid();
if (-1 == iNewGrp) {
Error("[%s] setsid(): %s", pCE->server, strerror(errno));
iNewGrp = getpid();
}
#else
iNewGrp = getpid();
#endif
if (dup(pout[0]) != 0 || dup(pin[1]) != 1) {
Error("[%s] StartInit(): fd sync error", pCE->server);
exit(EX_OSERR);
}
close(pout[0]);
close(pin[1]);
if (geteuid() == 0) {
if (pCE->initgid != 0)
setgid(pCE->initgid);
if (pCE->inituid != 0)
setuid(pCE->inituid);
}
tcsetpgrp(0, iNewGrp);
apcArgv[2] = pCE->initcmd;
close(2);
dup(1); /* better be 2, but it is too late now */
execve(apcArgv[0], apcArgv, environ);
Error("[%s] execve(%s): %s", pCE->server, apcArgv[2], strerror(errno));
exit(EX_OSERR);
return;
}
/* We exit() here, so only call this in a child process before an exec() */
void
SetupTty(CONSENT *pCE, int fd)
{
struct termios n_tio;
#if HAVE_STROPTS_H && !defined(_AIX)
/* SYSVr4 semantics for opening stream ptys (gregf)
* under PTX (others?) we have to push the compatibility
* streams modules `ptem', `ld', and `ttcompat'
*/
ioctl(1, I_PUSH, "ptem");
ioctl(1, I_PUSH, "ldterm");
ioctl(1, I_PUSH, "ttcompat");
#endif
if (0 != tcgetattr(1, &n_tio)) {
exit(EX_OSERR);
}
n_tio.c_iflag &= ~(IGNCR | IUCLC);
n_tio.c_iflag |= ICRNL;
if (pCE->ixon == FLAGTRUE)
n_tio.c_iflag |= IXON;
if (pCE->ixany == FLAGTRUE)
n_tio.c_iflag |= IXANY;
if (pCE->ixoff == FLAGTRUE)
n_tio.c_iflag |= IXOFF;
n_tio.c_oflag &=
~(OLCUC | ONOCR | ONLRET | OFILL | NLDLY | CRDLY | TABDLY | BSDLY);
n_tio.c_oflag |= OPOST | ONLCR;
n_tio.c_lflag &= ~(XCASE | NOFLSH | ECHOK | ECHONL);
n_tio.c_lflag |= ISIG | ICANON | ECHO;
n_tio.c_cc[VEOF] = '\004';
n_tio.c_cc[VEOL] = '\000';
n_tio.c_cc[VERASE] = '\010';
n_tio.c_cc[VINTR] = '\003';
n_tio.c_cc[VKILL] = '@';
/* MIN */
n_tio.c_cc[VQUIT] = '\034';
n_tio.c_cc[VSTART] = '\021';
n_tio.c_cc[VSTOP] = '\023';
n_tio.c_cc[VSUSP] = '\032';
if (0 != tcsetattr(1, TCSANOW, &n_tio))
exit(EX_OSERR);
}
/* setup a virtual device (ksb)
*/
static int
VirtDev(CONSENT *pCE)
{
int i;
pid_t iNewGrp;
extern char **environ;
char *pcShell, **ppcArgv;
fflush(stdout);
fflush(stderr);
switch (pCE->ipid = fork()) {
case -1:
pCE->ipid = 0;
return -1;
case 0:
thepid = getpid();
break;
default:
fflush(stderr);
pCE->fup = 1;
return 0;
}
/* put the signals back that we ignore (trapped auto-reset to default)
*/
SimpleSignal(SIGQUIT, SIG_DFL);
SimpleSignal(SIGINT, SIG_DFL);
SimpleSignal(SIGPIPE, SIG_DFL);
#if defined(SIGTTOU)
SimpleSignal(SIGTTOU, SIG_DFL);
#endif
#if defined(SIGTTIN)
SimpleSignal(SIGTTIN, SIG_DFL);
#endif
#if defined(SIGTSTP)
SimpleSignal(SIGTSTP, SIG_DFL);
#endif
#if defined(SIGPOLL)
SimpleSignal(SIGPOLL, SIG_DFL);
#endif
/* setup new process with clean filew descriptors
*/
#if HAVE_CLOSEFROM
for (i = 3; i < pCE->execSlaveFD; i++)
close(i);
i++;
closefrom(i);
#else
i = GetMaxFiles();
for ( /* i above */ ; --i > 2;) {
if (i != pCE->execSlaveFD)
close(i);
}
#endif
/* leave 2 until we *have to close it*
*/
close(1);
close(0);
#if HAVE_SETSID
iNewGrp = setsid();
if (-1 == iNewGrp) {
Error("[%s] setsid(): %s", pCE->server, strerror(errno));
iNewGrp = getpid();
}
#else
iNewGrp = getpid();
#endif
if (dup(pCE->execSlaveFD) != 0 || dup(pCE->execSlaveFD) != 1) {
Error("[%s] fd sync error", pCE->server);
exit(EX_OSERR);
}
if (geteuid() == 0) {
if (pCE->execgid != 0)
setgid(pCE->execgid);
if (pCE->execuid != 0) {
fchown(0, pCE->execuid, -1);
setuid(pCE->execuid);
}
}
SetupTty(pCE, 0);
tcsetpgrp(0, iNewGrp);
/* if the command is null we should run root's shell, directly
* if we can't find root's shell run /bin/sh
*/
pcShell = "/bin/sh";
if (pCE->exec == (char *)0) {
static char *apcArgv[] = {
"-shell", "-i", (char *)0
};
struct passwd *pwd;
if ((struct passwd *)0 != (pwd = getpwuid(0)) &&
'\000' != pwd->pw_shell[0]) {
pcShell = pwd->pw_shell;
}
ppcArgv = apcArgv;
} else {
static char *apcArgv[] = {
"/bin/sh", "-ce", (char *)0, (char *)0
};
apcArgv[2] = pCE->exec;
ppcArgv = apcArgv;
}
close(2);
dup(1); /* better be 2, but it is too late now */
execve(pcShell, ppcArgv, environ);
Error("[%s] execve(): %s", pCE->server, strerror(errno));
exit(EX_OSERR);
return -1;
}
char *
ConsState(CONSENT *pCE)
{
if (!pCE->fup)
return "down";
if (pCE->initfile != (CONSFILE *)0)
return "initializing";
switch (pCE->ioState) {
case ISNORMAL:
return "up";
case INCONNECT:
return "connecting";
case ISDISCONNECTED:
return "disconnected";
#if HAVE_OPENSSL
case INSSLACCEPT:
return "SSL_accept";
case INSSLSHUTDOWN:
return "SSL_shutdown";
#endif
#if HAVE_GSSAPI
case INGSSACCEPT:
return "GSSAPI_accept";
#endif
case ISFLUSHING:
return "flushing";
}
return "in unknown state";
}
/* down a console, virtual or real (ksb)
*
* this should be kept pretty simple, 'cause the config file reading code
* can come along and reconfigure a console out from under this - and it
* expects to be able to call ConsDown() to shut it down. so, only mess
* with the "runtime" members of the structure here.
*/
void
ConsDown(CONSENT *pCE, FLAG downHard, FLAG force)
{
if (force != FLAGTRUE &&
!(FileBufEmpty(pCE->fdlog) && FileBufEmpty(pCE->cofile) &&
FileBufEmpty(pCE->initfile))) {
pCE->ioState = ISFLUSHING;
return;
}
StopInit(pCE);
if (pCE->ipid != 0) {
CONDDEBUG((1, "ConsDown(): sending pid %lu signal %d",
(unsigned long)pCE->ipid, SIGHUP));
kill(pCE->ipid, SIGHUP);
pCE->ipid = 0;
}
if (pCE->cofile != (CONSFILE *)0) {
int cofile = FileFDNum(pCE->cofile);
FD_CLR(cofile, &rinit);
FD_CLR(cofile, &winit);
FileClose(&pCE->cofile);
}
#if HAVE_FREEIPMI
/* need to do this after cofile close above as
* ipmiconsole_ctx_destroy will close the fd */
if (pCE->ipmictx != (ipmiconsole_ctx_t) 0) {
ipmiconsole_ctx_destroy(pCE->ipmictx);
pCE->ipmictx = (ipmiconsole_ctx_t) 0;
}
#endif
if (pCE->fdlog != (CONSFILE *)0) {
if (pCE->nolog) {
TagLogfile(pCE, "Console logging restored");
}
TagLogfile(pCE, "Console down");
FD_CLR(FileFDNum(pCE->fdlog), &winit);
FileClose(&pCE->fdlog);
}
if (pCE->type == EXEC && pCE->execSlaveFD != 0) {
close(pCE->execSlaveFD);
pCE->execSlaveFD = 0;
}
pCE->fup = 0;
pCE->nolog = 0;
pCE->autoReUp = 0;
pCE->downHard = downHard;
pCE->ioState = ISDISCONNECTED;
pCE->telnetState = 0;
pCE->sentDoEcho = FLAGFALSE;
pCE->sentDoSGA = FLAGFALSE;
}
/* set up a console the way it should be for use to work with it (ksb)
* also, recover from silo over runs by dropping the line and re-opening
* We also maintian the select set for the caller.
*/
void
ConsInit(CONSENT *pCE)
{
time_t tyme;
extern int FallBack(char **, int *);
int cofile = -1;
int ret;
#if HAVE_GETTIMEOFDAY
struct timeval tv;
#else
time_t tv;
#endif
if (pCE->spintimer > 0 && pCE->spinmax > 0) {
#if HAVE_GETTIMEOFDAY
if (gettimeofday(&tv, (void *)0) == 0) {
/* less than pCE->spintimer seconds gone by? */
if ((tv.tv_sec <= pCE->lastInit.tv_sec + pCE->spintimer - 1)
|| (tv.tv_sec == pCE->lastInit.tv_sec + 1 &&
tv.tv_usec <= pCE->lastInit.tv_usec)) {
#else
if ((tv = time((time_t *)0)) != (time_t)-1) {
/* less than pCE->spintimer seconds gone by? (approx) */
if (tv <= pCE->lastInit + pCE->spintimer) {
#endif
pCE->spincount++;
if (pCE->spincount >= pCE->spinmax) {
pCE->spincount = 0;
pCE->lastInit = tv;
Error
("[%s] initialization rate exceeded: forcing down",
pCE->server);
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return;
}
} else
pCE->spincount = 0;
pCE->lastInit = tv;
} else
pCE->spincount = 0;
}
/* clean up old stuff
*/
if (pCE->fup) {
ConsDown(pCE, FLAGFALSE, FLAGTRUE);
Sleep(250000);
}
pCE->autoReUp = 0;
pCE->fronly = 0;
pCE->nolog = 0;
pCE->iend = 0;
/* try to open them again
*/
if (pCE->logfile != (char *)0) {
if ((CONSFILE *)0 ==
(pCE->fdlog =
FileOpen(pCE->logfile, O_RDWR | O_CREAT | O_APPEND, 0644))) {
Error("[%s] FileOpen(%s): %s: forcing down", pCE->server,
pCE->logfile, strerror(errno));
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return;
}
}
TagLogfile(pCE, "Console up");
switch (pCE->type) {
case UNKNOWNTYPE: /* shut up gcc */
break;
case NOOP:
pCE->fup = 1;
pCE->ioState = ISNORMAL;
break;
case EXEC:
if ((cofile =
FallBack(&pCE->execSlave, &pCE->execSlaveFD)) == -1) {
Error
("[%s] failed to allocate pseudo-tty: %s: forcing down",
pCE->server, strerror(errno));
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return;
}
if ((pCE->cofile =
FileOpenFD(cofile, simpleFile)) == (CONSFILE *)0) {
Error
("[%s] FileOpenFD(%d,simpleFile) failed: forcing down",
pCE->server, cofile);
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return;
}
VirtDev(pCE);
pCE->ioState = ISNORMAL;
break;
case HOST:
{
#if USE_IPV6
int error;
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
struct addrinfo *ai, *rp, hints;
#else
struct sockaddr_in port;
struct hostent *hp;
#endif /* USE_IPV6 */
#if HAVE_SETSOCKOPT
int one = 1;
#endif
Sleep(100000); /* Not all terminal servers can keep up */
#if USE_IPV6
# if HAVE_MEMSET
memset(&hints, 0, sizeof(hints));
# else
bzero(&hints, sizeof(hints));
# endif
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_socktype = SOCK_STREAM;
snprintf(serv, sizeof(serv), "%hu", pCE->netport);
error = getaddrinfo(pCE->host, serv, &hints, &ai);
if (error) {
Error("[%s] getaddrinfo(%s): %s: forcing down",
pCE->server, pCE->host, gai_strerror(error));
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return;
}
rp = ai;
while (rp) {
error =
getnameinfo(rp->ai_addr, rp->ai_addrlen, host,
sizeof(host), serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV);
if (error)
continue;
CONDDEBUG((1,
"[%s]: trying hostname=%s, ip=%s, port=%s",
pCE->server, pCE->host, host, serv));
cofile =
socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (cofile != -1) {
# if HAVE_SETSOCKOPT
if (setsockopt
(cofile, SOL_SOCKET, SO_KEEPALIVE,
(char *)&one, sizeof(one)) < 0)
goto fail;
# endif
if (!SetFlags(cofile, O_NONBLOCK, 0))
goto fail;
ret = connect(cofile, rp->ai_addr, rp->ai_addrlen);
if (ret == 0 || errno == EINPROGRESS)
goto success;
fail:
close(cofile);
}
rp = rp->ai_next;
}
Error("[%s]: Unable to connect to %s:%s", pCE->server,
host, serv);
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return;
success:
freeaddrinfo(ai);
#else
# if HAVE_MEMSET
memset((void *)&port, 0, sizeof(port));
# else
bzero((char *)&port, sizeof(port));
# endif
if ((hp = gethostbyname(pCE->host)) == NULL) {
Error("[%s] gethostbyname(%s): %s: forcing down",
pCE->server, pCE->host, hstrerror(h_errno));
ConsDown(pCE, FLAGTRUE, FLAGTRUE);
return;