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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
|
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#define PRINTOPTIONS
#include "telnetd.h"
/* RCSID("$KTH: utility.c,v 1.25 2001/05/17 00:34:42 assar Exp $"); */
/*
* utility functions performing io related tasks
*/
/*
* ttloop
*
* A small subroutine to flush the network output buffer, get some
* data from the network, and pass it through the telnet state
* machine. We also flush the pty input buffer (by dropping its data)
* if it becomes too full.
*
* return 0 if OK or 1 if interrupted by a signal.
*/
int
ttloop(void)
{
void netflush(void);
DIAG(TD_REPORT, {
output_data("td: ttloop\r\n");
});
if (nfrontp-nbackp)
netflush();
ncc = read(net, netibuf, sizeof netibuf);
if (ncc < 0) {
if (errno == EINTR)
return 1;
syslog(LOG_INFO, "ttloop: read: %m");
exit(1);
} else if (ncc == 0) {
syslog(LOG_INFO, "ttloop: peer died");
exit(1);
}
DIAG(TD_REPORT, {
output_data("td: ttloop read %d chars\r\n", ncc);
});
netip = netibuf;
telrcv(); /* state machine */
if (ncc > 0) {
pfrontp = pbackp = ptyobuf;
telrcv();
}
return 0;
} /* end of ttloop */
/*
* Check a descriptor to see if out of band data exists on it.
*/
int
stilloob(int s)
{
static struct timeval timeout = { 0 };
fd_set excepts;
int value;
if (s >= FD_SETSIZE)
fatal(ourpty, "fd too large");
do {
FD_ZERO(&excepts);
FD_SET(s, &excepts);
value = select(s+1, 0, 0, &excepts, &timeout);
} while ((value == -1) && (errno == EINTR));
if (value < 0) {
fatalperror(ourpty, "select");
}
if (FD_ISSET(s, &excepts)) {
return 1;
} else {
return 0;
}
}
void
ptyflush(void)
{
int n;
if ((n = pfrontp - pbackp) > 0) {
DIAG((TD_REPORT | TD_PTYDATA), {
output_data("td: ptyflush %d chars\r\n", n);
});
DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
n = write(ourpty, pbackp, n);
}
if (n < 0) {
if (errno == EWOULDBLOCK || errno == EINTR)
return;
cleanup(0);
}
pbackp += n;
if (pbackp == pfrontp)
pbackp = pfrontp = ptyobuf;
}
/*
* nextitem()
*
* Return the address of the next "item" in the TELNET data
* stream. This will be the address of the next character if
* the current address is a user data character, or it will
* be the address of the character following the TELNET command
* if the current address is a TELNET IAC ("I Am a Command")
* character.
*/
char *
nextitem(char *current)
{
if ((*current&0xff) != IAC) {
return current+1;
}
switch (*(current+1)&0xff) {
case DO:
case DONT:
case WILL:
case WONT:
return current+3;
case SB:{
/* loop forever looking for the SE */
char *look = current+2;
for (;;) {
if ((*look++&0xff) == IAC) {
if ((*look++&0xff) == SE) {
return look;
}
}
}
}
default:
return current+2;
}
}
/*
* netclear()
*
* We are about to do a TELNET SYNCH operation. Clear
* the path to the network.
*
* Things are a bit tricky since we may have sent the first
* byte or so of a previous TELNET command into the network.
* So, we have to scan the network buffer from the beginning
* until we are up to where we want to be.
*
* A side effect of what we do, just to keep things
* simple, is to clear the urgent data pointer. The principal
* caller should be setting the urgent data pointer AFTER calling
* us in any case.
*/
void
netclear(void)
{
char *thisitem, *next;
char *good;
#define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
#ifdef ENCRYPTION
thisitem = nclearto > netobuf ? nclearto : netobuf;
#else
thisitem = netobuf;
#endif
while ((next = nextitem(thisitem)) <= nbackp) {
thisitem = next;
}
/* Now, thisitem is first before/at boundary. */
#ifdef ENCRYPTION
good = nclearto > netobuf ? nclearto : netobuf;
#else
good = netobuf; /* where the good bytes go */
#endif
while (nfrontp > thisitem) {
if (wewant(thisitem)) {
int length;
next = thisitem;
do {
next = nextitem(next);
} while (wewant(next) && (nfrontp > next));
length = next-thisitem;
memmove(good, thisitem, length);
good += length;
thisitem = next;
} else {
thisitem = nextitem(thisitem);
}
}
nbackp = netobuf;
nfrontp = good; /* next byte to be sent */
neturg = 0;
} /* end of netclear */
/*
* netflush
* Send as much data as possible to the network,
* handling requests for urgent data.
*/
void
netflush(void)
{
int n;
extern int not42;
if ((n = nfrontp - nbackp) > 0) {
DIAG(TD_REPORT,
{ n += output_data("td: netflush %d chars\r\n", n);
});
#ifdef ENCRYPTION
if (encrypt_output) {
char *s = nclearto ? nclearto : nbackp;
if (nfrontp - s > 0) {
(*encrypt_output)((unsigned char *)s, nfrontp-s);
nclearto = nfrontp;
}
}
#endif
/*
* if no urgent data, or if the other side appears to be an
* old 4.2 client (and thus unable to survive TCP urgent data),
* write the entire buffer in non-OOB mode.
*/
#if 1 /* remove this to make it work between solaris 2.6 and linux */
if ((neturg == 0) || (not42 == 0)) {
#endif
n = write(net, nbackp, n); /* normal write */
#if 1 /* remove this to make it work between solaris 2.6 and linux */
} else {
n = neturg - nbackp;
/*
* In 4.2 (and 4.3) systems, there is some question about
* what byte in a sendOOB operation is the "OOB" data.
* To make ourselves compatible, we only send ONE byte
* out of band, the one WE THINK should be OOB (though
* we really have more the TCP philosophy of urgent data
* rather than the Unix philosophy of OOB data).
*/
if (n > 1) {
n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */
} else {
n = send(net, nbackp, n, MSG_OOB); /* URGENT data */
}
}
#endif
}
if (n < 0) {
if (errno == EWOULDBLOCK || errno == EINTR)
return;
cleanup(0);
}
nbackp += n;
#ifdef ENCRYPTION
if (nbackp > nclearto)
nclearto = 0;
#endif
if (nbackp >= neturg) {
neturg = 0;
}
if (nbackp == nfrontp) {
nbackp = nfrontp = netobuf;
#ifdef ENCRYPTION
nclearto = 0;
#endif
}
return;
}
/*
* writenet
*
* Just a handy little function to write a bit of raw data to the net.
* It will force a transmit of the buffer if necessary
*
* arguments
* ptr - A pointer to a character string to write
* len - How many bytes to write
*/
void
writenet(unsigned char *ptr, int len)
{
/* flush buffer if no room for new data) */
while ((&netobuf[BUFSIZ] - nfrontp) < len) {
/* if this fails, don't worry, buffer is a little big */
netflush();
}
memmove(nfrontp, ptr, len);
nfrontp += len;
}
/*
* miscellaneous functions doing a variety of little jobs follow ...
*/
void fatal(int f, char *msg)
{
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "telnetd: %s.\r\n", msg);
#ifdef ENCRYPTION
if (encrypt_output) {
/*
* Better turn off encryption first....
* Hope it flushes...
*/
encrypt_send_end();
netflush();
}
#endif
write(f, buf, (int)strlen(buf));
sleep(1); /*XXX*/
exit(1);
}
void
fatalperror_errno(int f, const char *msg, int error)
{
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(error));
fatal(f, buf);
}
void
fatalperror(int f, const char *msg)
{
fatalperror_errno(f, msg, errno);
}
char editedhost[32];
void edithost(char *pat, char *host)
{
char *res = editedhost;
if (!pat)
pat = "";
while (*pat) {
switch (*pat) {
case '#':
if (*host)
host++;
break;
case '@':
if (*host)
*res++ = *host++;
break;
default:
*res++ = *pat;
break;
}
if (res == &editedhost[sizeof editedhost - 1]) {
*res = '\0';
return;
}
pat++;
}
if (*host)
strlcpy (res, host,
sizeof editedhost - (res - editedhost));
else
*res = '\0';
editedhost[sizeof editedhost - 1] = '\0';
}
static char *putlocation;
void
putstr(char *s)
{
while (*s)
putchr(*s++);
}
void
putchr(int cc)
{
*putlocation++ = cc;
}
/*
* This is split on two lines so that SCCS will not see the M
* between two % signs and expand it...
*/
static char fmtstr[] = { "%l:%M" "%P on %A, %d %B %Y" };
void putf(char *cp, char *where)
{
#ifdef HAVE_UNAME
struct utsname name;
#endif
char *slash;
time_t t;
char db[100];
/* if we don't have uname, set these to sensible values */
char *sysname = "Unix",
*machine = "",
*release = "",
*version = "";
#ifdef HAVE_UNAME
uname(&name);
sysname=name.sysname;
machine=name.machine;
release=name.release;
version=name.version;
#endif
putlocation = where;
while (*cp) {
if (*cp != '%') {
putchr(*cp++);
continue;
}
switch (*++cp) {
case 't':
#ifdef STREAMSPTY
/* names are like /dev/pts/2 -- we want pts/2 */
slash = strchr(line+1, '/');
#else
slash = strrchr(line, '/');
#endif
if (slash == (char *) 0)
putstr(line);
else
putstr(&slash[1]);
break;
case 'h':
putstr(editedhost);
break;
case 's':
putstr(sysname);
break;
case 'm':
putstr(machine);
break;
case 'r':
putstr(release);
break;
case 'v':
putstr(version);
break;
case 'd':
time(&t);
strftime(db, sizeof(db), fmtstr, localtime(&t));
putstr(db);
break;
case '%':
putchr('%');
break;
}
cp++;
}
}
#ifdef DIAGNOSTICS
/*
* Print telnet options and commands in plain text, if possible.
*/
void
printoption(char *fmt, int option)
{
if (TELOPT_OK(option))
output_data("%s %s\r\n",
fmt,
TELOPT(option));
else if (TELCMD_OK(option))
output_data("%s %s\r\n",
fmt,
TELCMD(option));
else
output_data("%s %d\r\n",
fmt,
option);
return;
}
void
printsub(int direction, unsigned char *pointer, int length)
/* '<' or '>' */
/* where suboption data sits */
/* length of suboption data */
{
int i = 0;
#ifdef AUTHENTICATION
unsigned char buf[512];
#endif
if (!(diagnostic & TD_OPTIONS))
return;
if (direction) {
output_data("td: %s suboption ",
direction == '<' ? "recv" : "send");
if (length >= 3) {
int j;
i = pointer[length-2];
j = pointer[length-1];
if (i != IAC || j != SE) {
output_data("(terminated by ");
if (TELOPT_OK(i))
output_data("%s ",
TELOPT(i));
else if (TELCMD_OK(i))
output_data("%s ",
TELCMD(i));
else
output_data("%d ",
i);
if (TELOPT_OK(j))
output_data("%s",
TELOPT(j));
else if (TELCMD_OK(j))
output_data("%s",
TELCMD(j));
else
output_data("%d",
j);
output_data(", not IAC SE!) ");
}
}
length -= 2;
}
if (length < 1) {
output_data("(Empty suboption??\?)");
return;
}
switch (pointer[0]) {
case TELOPT_TTYPE:
output_data("TERMINAL-TYPE ");
switch (pointer[1]) {
case TELQUAL_IS:
output_data("IS \"%.*s\"",
length-2,
(char *)pointer+2);
break;
case TELQUAL_SEND:
output_data("SEND");
break;
default:
output_data("- unknown qualifier %d (0x%x).",
pointer[1], pointer[1]);
}
break;
case TELOPT_TSPEED:
output_data("TERMINAL-SPEED");
if (length < 2) {
output_data(" (empty suboption??\?)");
break;
}
switch (pointer[1]) {
case TELQUAL_IS:
output_data(" IS %.*s", length-2, (char *)pointer+2);
break;
default:
if (pointer[1] == 1)
output_data(" SEND");
else
output_data(" %d (unknown)", pointer[1]);
for (i = 2; i < length; i++) {
output_data(" ?%d?", pointer[i]);
}
break;
}
break;
case TELOPT_LFLOW:
output_data("TOGGLE-FLOW-CONTROL");
if (length < 2) {
output_data(" (empty suboption??\?)");
break;
}
switch (pointer[1]) {
case LFLOW_OFF:
output_data(" OFF");
break;
case LFLOW_ON:
output_data(" ON");
break;
case LFLOW_RESTART_ANY:
output_data(" RESTART-ANY");
break;
case LFLOW_RESTART_XON:
output_data(" RESTART-XON");
break;
default:
output_data(" %d (unknown)",
pointer[1]);
}
for (i = 2; i < length; i++) {
output_data(" ?%d?",
pointer[i]);
}
break;
case TELOPT_NAWS:
output_data("NAWS");
if (length < 2) {
output_data(" (empty suboption??\?)");
break;
}
if (length == 2) {
output_data(" ?%d?",
pointer[1]);
break;
}
output_data(" %u %u(%u)",
pointer[1],
pointer[2],
(((unsigned int)pointer[1])<<8) + pointer[2]);
if (length == 4) {
output_data(" ?%d?",
pointer[3]);
break;
}
output_data(" %u %u(%u)",
pointer[3],
pointer[4],
(((unsigned int)pointer[3])<<8) + pointer[4]);
for (i = 5; i < length; i++) {
output_data(" ?%d?",
pointer[i]);
}
break;
case TELOPT_LINEMODE:
output_data("LINEMODE ");
if (length < 2) {
output_data(" (empty suboption??\?)");
break;
}
switch (pointer[1]) {
case WILL:
output_data("WILL ");
goto common;
case WONT:
output_data("WONT ");
goto common;
case DO:
output_data("DO ");
goto common;
case DONT:
output_data("DONT ");
common:
if (length < 3) {
output_data("(no option??\?)");
break;
}
switch (pointer[2]) {
case LM_FORWARDMASK:
output_data("Forward Mask");
for (i = 3; i < length; i++) {
output_data(" %x", pointer[i]);
}
break;
default:
output_data("%d (unknown)",
pointer[2]);
for (i = 3; i < length; i++) {
output_data(" %d",
pointer[i]);
}
break;
}
break;
case LM_SLC:
output_data("SLC");
for (i = 2; i < length - 2; i += 3) {
if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
output_data(" %s",
SLC_NAME(pointer[i+SLC_FUNC]));
else
output_data(" %d",
pointer[i+SLC_FUNC]);
switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
case SLC_NOSUPPORT:
output_data(" NOSUPPORT");
break;
case SLC_CANTCHANGE:
output_data(" CANTCHANGE");
break;
case SLC_VARIABLE:
output_data(" VARIABLE");
break;
case SLC_DEFAULT:
output_data(" DEFAULT");
break;
}
output_data("%s%s%s",
pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
SLC_FLUSHOUT| SLC_LEVELBITS)) {
output_data("(0x%x)",
pointer[i+SLC_FLAGS]);
}
output_data(" %d;",
pointer[i+SLC_VALUE]);
if ((pointer[i+SLC_VALUE] == IAC) &&
(pointer[i+SLC_VALUE+1] == IAC))
i++;
}
for (; i < length; i++) {
output_data(" ?%d?",
pointer[i]);
}
break;
case LM_MODE:
output_data("MODE ");
if (length < 3) {
output_data("(no mode??\?)");
break;
}
{
char tbuf[32];
snprintf(tbuf,
sizeof(tbuf),
"%s%s%s%s%s",
pointer[2]&MODE_EDIT ? "|EDIT" : "",
pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
pointer[2]&MODE_ACK ? "|ACK" : "");
output_data("%s",
tbuf[1] ? &tbuf[1] : "0");
}
if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
output_data(" (0x%x)",
pointer[2]);
}
for (i = 3; i < length; i++) {
output_data(" ?0x%x?",
pointer[i]);
}
break;
default:
output_data("%d (unknown)",
pointer[1]);
for (i = 2; i < length; i++) {
output_data(" %d", pointer[i]);
}
}
break;
case TELOPT_STATUS: {
char *cp;
int j, k;
output_data("STATUS");
switch (pointer[1]) {
default:
if (pointer[1] == TELQUAL_SEND)
output_data(" SEND");
else
output_data(" %d (unknown)",
pointer[1]);
for (i = 2; i < length; i++) {
output_data(" ?%d?",
pointer[i]);
}
break;
case TELQUAL_IS:
output_data(" IS\r\n");
for (i = 2; i < length; i++) {
switch(pointer[i]) {
case DO: cp = "DO"; goto common2;
case DONT: cp = "DONT"; goto common2;
case WILL: cp = "WILL"; goto common2;
case WONT: cp = "WONT"; goto common2;
common2:
i++;
if (TELOPT_OK(pointer[i]))
output_data(" %s %s",
cp,
TELOPT(pointer[i]));
else
output_data(" %s %d",
cp,
pointer[i]);
output_data("\r\n");
break;
case SB:
output_data(" SB ");
i++;
j = k = i;
while (j < length) {
if (pointer[j] == SE) {
if (j+1 == length)
break;
if (pointer[j+1] == SE)
j++;
else
break;
}
pointer[k++] = pointer[j++];
}
printsub(0, &pointer[i], k - i);
if (i < length) {
output_data(" SE");
i = j;
} else
i = j - 1;
output_data("\r\n");
break;
default:
output_data(" %d",
pointer[i]);
break;
}
}
break;
}
break;
}
case TELOPT_XDISPLOC:
output_data("X-DISPLAY-LOCATION ");
switch (pointer[1]) {
case TELQUAL_IS:
output_data("IS \"%.*s\"",
length-2,
(char *)pointer+2);
break;
case TELQUAL_SEND:
output_data("SEND");
break;
default:
output_data("- unknown qualifier %d (0x%x).",
pointer[1], pointer[1]);
}
break;
case TELOPT_NEW_ENVIRON:
output_data("NEW-ENVIRON ");
goto env_common1;
case TELOPT_OLD_ENVIRON:
output_data("OLD-ENVIRON");
env_common1:
switch (pointer[1]) {
case TELQUAL_IS:
output_data("IS ");
goto env_common;
case TELQUAL_SEND:
output_data("SEND ");
goto env_common;
case TELQUAL_INFO:
output_data("INFO ");
env_common:
{
int noquote = 2;
for (i = 2; i < length; i++ ) {
switch (pointer[i]) {
case NEW_ENV_VAR:
output_data("\" VAR " + noquote);
noquote = 2;
break;
case NEW_ENV_VALUE:
output_data("\" VALUE " + noquote);
noquote = 2;
break;
case ENV_ESC:
output_data("\" ESC " + noquote);
noquote = 2;
break;
case ENV_USERVAR:
output_data("\" USERVAR " + noquote);
noquote = 2;
break;
default:
if (isprint(pointer[i]) && pointer[i] != '"') {
if (noquote) {
output_data ("\"");
noquote = 0;
}
output_data ("%c", pointer[i]);
} else {
output_data("\" %03o " + noquote,
pointer[i]);
noquote = 2;
}
break;
}
}
if (!noquote)
output_data ("\"");
break;
}
}
break;
#ifdef AUTHENTICATION
case TELOPT_AUTHENTICATION:
output_data("AUTHENTICATION");
if (length < 2) {
output_data(" (empty suboption??\?)");
break;
}
switch (pointer[1]) {
case TELQUAL_REPLY:
case TELQUAL_IS:
output_data(" %s ",
(pointer[1] == TELQUAL_IS) ?
"IS" : "REPLY");
if (AUTHTYPE_NAME_OK(pointer[2]))
output_data("%s ",
AUTHTYPE_NAME(pointer[2]));
else
output_data("%d ",
pointer[2]);
if (length < 3) {
output_data("(partial suboption??\?)");
break;
}
output_data("%s|%s",
((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
"CLIENT" : "SERVER",
((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
"MUTUAL" : "ONE-WAY");
auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
output_data("%s",
buf);
break;
case TELQUAL_SEND:
i = 2;
output_data(" SEND ");
while (i < length) {
if (AUTHTYPE_NAME_OK(pointer[i]))
output_data("%s ",
AUTHTYPE_NAME(pointer[i]));
else
output_data("%d ",
pointer[i]);
if (++i >= length) {
output_data("(partial suboption??\?)");
break;
}
output_data("%s|%s ",
((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
"CLIENT" : "SERVER",
((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
"MUTUAL" : "ONE-WAY");
++i;
}
break;
case TELQUAL_NAME:
i = 2;
output_data(" NAME \"%.*s\"",
length - 2,
pointer);
break;
default:
for (i = 2; i < length; i++) {
output_data(" ?%d?",
pointer[i]);
}
break;
}
break;
#endif
#ifdef ENCRYPTION
case TELOPT_ENCRYPT:
output_data("ENCRYPT");
if (length < 2) {
output_data(" (empty suboption?)");
break;
}
switch (pointer[1]) {
case ENCRYPT_START:
output_data(" START");
break;
case ENCRYPT_END:
output_data(" END");
break;
case ENCRYPT_REQSTART:
output_data(" REQUEST-START");
break;
case ENCRYPT_REQEND:
output_data(" REQUEST-END");
break;
case ENCRYPT_IS:
case ENCRYPT_REPLY:
output_data(" %s ",
(pointer[1] == ENCRYPT_IS) ?
"IS" : "REPLY");
if (length < 3) {
output_data(" (partial suboption?)");
break;
}
if (ENCTYPE_NAME_OK(pointer[2]))
output_data("%s ",
ENCTYPE_NAME(pointer[2]));
else
output_data(" %d (unknown)",
pointer[2]);
encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
output_data("%s",
buf);
break;
case ENCRYPT_SUPPORT:
i = 2;
output_data(" SUPPORT ");
while (i < length) {
if (ENCTYPE_NAME_OK(pointer[i]))
output_data("%s ",
ENCTYPE_NAME(pointer[i]));
else
output_data("%d ",
pointer[i]);
i++;
}
break;
case ENCRYPT_ENC_KEYID:
output_data(" ENC_KEYID %d", pointer[1]);
goto encommon;
case ENCRYPT_DEC_KEYID:
output_data(" DEC_KEYID %d", pointer[1]);
goto encommon;
default:
output_data(" %d (unknown)", pointer[1]);
encommon:
for (i = 2; i < length; i++) {
output_data(" %d", pointer[i]);
}
break;
}
break;
#endif
default:
if (TELOPT_OK(pointer[0]))
output_data("%s (unknown)",
TELOPT(pointer[0]));
else
output_data("%d (unknown)",
pointer[i]);
for (i = 1; i < length; i++) {
output_data(" %d", pointer[i]);
}
break;
}
output_data("\r\n");
}
/*
* Dump a data buffer in hex and ascii to the output data stream.
*/
void
printdata(char *tag, char *ptr, int cnt)
{
int i;
char xbuf[30];
while (cnt) {
/* flush net output buffer if no room for new data) */
if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
netflush();
}
/* add a line of output */
output_data("%s: ", tag);
for (i = 0; i < 20 && cnt; i++) {
output_data("%02x", *ptr);
if (isprint(*ptr)) {
xbuf[i] = *ptr;
} else {
xbuf[i] = '.';
}
if (i % 2) {
output_data(" ");
}
cnt--;
ptr++;
}
xbuf[i] = '\0';
output_data(" %s\r\n", xbuf);
}
}
#endif /* DIAGNOSTICS */
|