-
Notifications
You must be signed in to change notification settings - Fork 2
/
ckctel.c
9015 lines (8348 loc) · 289 KB
/
ckctel.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
char *cktelv = "Telnet support, 9.0.274, 16 Mar 2010";
#define CKCTEL_C
int sstelnet = 0; /* Do server-side Telnet negotiation */
/* C K C T E L -- Telnet support */
/*
Authors:
Telnet protocol by Frank da Cruz and Jeffrey Altman.
Telnet Forward X by Jeffrey Altman
Telnet START_TLS support by Jeffrey Altman
Telnet AUTH and ENCRYPT support by Jeffrey Altman
Telnet COMPORT support by Jeffrey Altman
Telnet NEW-ENVIRONMENT support by Jeffrey Altman
Telnet NAWS support by Frank da Cruz and Jeffrey Altman
Telnet TERMTYPE support by Jeffrey Altman
Telnet KERMIT support by Jeffrey Altman
Other contributions as indicated in the code.
Copyright (C) 1985, 2010,
Trustees of Columbia University in the City of New York.
All rights reserved. See the C-Kermit COPYING.TXT file or the
copyright text in the ckcmai.c module for disclaimer and permissions.
*/
/*
NOTE TO CONTRIBUTORS: This file, and all the other shared (ckc and cku)
C-Kermit source files, must be compatible with C preprocessors that support
only #ifdef, #else, #endif, #define, and #undef. Please do not use #if,
logical operators, or other preprocessor features in this module. Also,
don't use any ANSI C constructs except within #ifdef CK_ANSIC..#endif.
*/
#include "ckcsym.h"
#include "ckcdeb.h"
#ifdef TNCODE
#include "ckcker.h"
#define TELCMDS /* to define name array */
#define TELOPTS /* to define name array */
#define SLC_NAMES /* to define name array */
#define ENCRYPT_NAMES
#define AUTH_NAMES
#define TELOPT_STATES
#define TELOPT_MODES
#define TNC_NAMES
#include "ckcnet.h"
#include "ckctel.h"
#ifdef CK_AUTHENTICATION
#include "ckuath.h"
#endif /* CK_AUTHENTICATION */
#ifdef CK_SSL
#include "ck_ssl.h"
#endif /* CK_SSL */
#ifndef NOTERM
#ifdef OS2 /* For terminal type name string */
#include "ckuusr.h"
#ifndef NT
#include <os2.h>
#undef COMMENT
#else
#define isascii __isascii
#endif /* NT */
#include "ckocon.h"
extern int tt_type, max_tt;
extern struct tt_info_rec tt_info[];
#endif /* OS2 */
#endif /* NOTERM */
#ifdef OS2
#include <assert.h>
#ifdef NT
#include <setjmpex.h>
#else /* NT */
#include <setjmp.h>
#endif /* NT */
#include <signal.h>
#include "ckcsig.h"
#include "ckosyn.h"
#endif /* OS2 */
#ifdef CK_NAWS /* Negotiate About Window Size */
#ifdef RLOGCODE
_PROTOTYP( int rlog_naws, (void) );
#endif /* RLOGCODE */
#endif /* CK_NAWS */
int tn_init = 0; /* Telnet protocol initialized flag */
int tn_begun = 0; /* Telnet protocol started flag */
static int tn_first = 1; /* First time init flag */
extern int tn_exit; /* Exit on disconnect */
extern int nettype;
extern int inserver; /* Running as IKSD */
char *tn_term = NULL; /* Terminal type override */
#ifdef CK_SNDLOC
char *tn_loc = NULL; /* Location override */
#endif /* CK_SNDLOC */
int tn_nlm = TNL_CRLF; /* Telnet CR -> CR LF mode */
int tn_b_nlm = TNL_CR; /* Telnet Binary CR RAW mode */
int tn_b_meu = 0; /* Telnet Binary ME means U too */
int tn_b_ume = 0; /* Telnet Binary U means ME too */
int tn_wait_flg = 1; /* Telnet Wait for Negotiations */
int tn_infinite = 0; /* Telnet Bug Infinite-Loop-Check */
int tn_rem_echo = 1; /* We will echo if WILL ECHO */
int tn_b_xfer = 0; /* Telnet Binary for Xfers? */
int tn_sb_bug = 1; /* Telnet BUG - SB w/o WILL or DO */
int tn_auth_krb5_des_bug = 1; /* Telnet BUG - AUTH KRB5 DES */
/* truncates long keys */
int tn_no_encrypt_xfer = 0; /* Turn off Telnet Encrypt? */
int tn_delay_sb = 1; /* Delay SBs until safe */
int tn_auth_how = TN_AUTH_HOW_ANY;
int tn_auth_enc = TN_AUTH_ENC_ANY;
int tn_deb = 0; /* Telnet Debug mode */
int tn_sfu = 0; /* Microsoft SFU compatibility */
#ifdef CK_FORWARD_X
char * tn_fwdx_xauthority = NULL; /* Xauthority File */
int fwdx_no_encrypt = 0; /* Forward-X requires encryption */
#endif /* CK_FORWARD_X */
#ifdef OS2
int ttnum = -1; /* Last Telnet Terminal Type sent */
int ttnumend = 0; /* Has end of list been found */
#endif /* OS2 */
char tn_msg[TN_MSG_LEN]; /* Telnet data can be rather long */
char hexbuf[TN_MSG_LEN];
char tn_msg_out[TN_MSG_LEN];
#ifdef CK_FORWARD_X
CHAR fwdx_msg_out[TN_MSG_LEN];
#endif /* CK_FORWARD_X */
/*
In order to prevent an infinite telnet negotiation loop we maintain a
count of the number of times the same telnet negotiation message is
sent. When this count hits MAXTNCNT, we do not send any more of the
message. The count is stored in the tncnts[][] array.
The tncnts[][] array is indexed by negotiation option (SUPPRESS GO AHEAD,
TERMINAL TYPE, NAWS, etc. - see the tnopts[] array) and the four
negotiation message types (WILL, WONT, DO, DONT). All telnet negotiations
are kept track of in this way.
The count for a message is zeroed when the "opposite" message is sent.
WILL is the opposite of WONT, and DO is the opposite of DONT.
For example sending "WILL SGA" increments tncnts[TELOPT_SGA][0]
and zeroes tncnts[TELOPT_SGA][1].
The code that does this is in tn_sopt().
[email protected], 18/3/1995
8/16/1998 - with the recent rewrite of the telnet state machine I don't
think this code is necessary anymore. However, it can't do any harm so
I am leaving it in. - Jeff
12/28/1998 - all references to tncnts[] must be done with TELOPT_INDEX(opt)
because the Telnet option list is no longer contiguous. We also must
allocate NTELOPTS + 1 because the TELOPT_INDEX() macro returns NTELOPTS
for an invalid option number.
*/
#define MAXTNCNT 4 /* Permits 4 intermediate telnet firewalls/gateways */
char tncnts[NTELOPTS+1][4]; /* Counts */
char tnopps[4] = { 1,0,3,2 }; /* Opposites */
#ifdef CK_ENVIRONMENT
#ifdef CK_FORWARD_X
#define TSBUFSIZ 2056
#else /* CK_FORWARD_X */
#define TSBUFSIZ 1024
#endif /* CK_FORWARD_X */
char tn_env_acct[64];
char tn_env_disp[64];
char tn_env_job[64];
char tn_env_prnt[64];
char tn_env_sys[64];
char * tn_env_uservar[8][2];
int tn_env_flg = 1;
#else /* CK_ENVIRONMENT */
#define TSBUFSIZ 41
int tn_env_flg = 0;
#endif /* CK_ENVIRONMENT */
#ifdef COMMENT
/* SIGWINCH handler moved to ckuusx.c */
#ifndef NOSIGWINCH
#ifdef CK_NAWS /* Window size business */
#ifdef UNIX
#include <signal.h>
#endif /* UNIX */
#endif /* CK_NAWS */
#endif /* NOSIGWINCH */
#endif /* COMMENT */
CHAR sb[TSBUFSIZ]; /* Buffer - incoming subnegotiations */
CHAR sb_out[TSBUFSIZ]; /* Buffer - outgoing subnegotiations */
int tn_duplex = 1; /* Local echo */
extern char uidbuf[]; /* User ID buffer */
extern int quiet, ttnet, ttnproto, debses, what, duplex, oldplex, local;
extern int seslog, sessft, whyclosed;
#ifdef OS2
#ifndef NOTERM
extern int tt_rows[], tt_cols[];
extern int tt_status[VNUM];
extern int scrninitialized[];
#endif /* NOTERM */
#else /* OS2 */
extern int tt_rows, tt_cols; /* Everybody has this */
#endif /* OS2 */
extern int cmd_cols, cmd_rows;
extern char namecopy[];
extern char myipaddr[]; /* Global copy of my IP address */
#ifndef TELOPT_MACRO
int
telopt_index(opt) int opt; {
if (opt >= 0 && opt <= TELOPT_STDERR)
return(opt);
else if (opt >= TELOPT_PRAGMA_LOGON && opt <= TELOPT_PRAGMA_HEARTBEAT)
return(opt-88);
else if (opt == TELOPT_IBM_SAK)
return(opt-147);
else
return(NTELOPTS);
}
int
telopt_ok(opt) int opt; {
return((opt >= TELOPT_BINARY && opt <= TELOPT_STDERR) ||
(opt >= TELOPT_PRAGMA_LOGON && opt <= TELOPT_PRAGMA_HEARTBEAT) ||
(opt == TELOPT_IBM_SAK));
}
CHAR *
telopt(opt) int opt; {
if (telopt_ok(opt))
return((CHAR *)telopts[telopt_index(opt)]);
else
return((CHAR *)"UNKNOWN");
}
int
telopt_mode_ok(opt) int opt; {
return((unsigned int)(opt) <= TN_NG_MU);
}
CHAR *
telopt_mode(opt) int opt; {
if (telopt_mode_ok(opt))
return((CHAR *)telopt_modes[opt-TN_NG_RF]);
else
return((CHAR *)"UNKNOWN");
}
#endif /* TELOPT_MACRO */
static int
tn_outst(notquiet) int notquiet; {
int outstanding = 0;
int x = 0;
#ifdef CK_ENCRYPTION
int e = 0;
int d = 0;
#endif /* CK_ENCRYPTION */
if (tn_wait_flg) {
for (x = TELOPT_FIRST; x <= TELOPT_LAST; x++) {
if (TELOPT_OK(x)) {
if (TELOPT_UNANSWERED_WILL(x)) {
if ( notquiet )
printf("?Telnet waiting for response to WILL %s\r\n",
TELOPT(x));
debug(F111,"tn_outst","unanswered WILL",x);
outstanding = 1;
if ( !notquiet )
break;
}
if (TELOPT_UNANSWERED_DO(x)) {
if ( notquiet )
printf("?Telnet waiting for response to DO %s\r\n",
TELOPT(x));
debug(F111,"tn_outst","unanswered DO",x);
outstanding = 1;
if ( !notquiet )
break;
}
if (TELOPT_UNANSWERED_WONT(x)) {
if ( notquiet )
printf("?Telnet waiting for response to WONT %s\r\n",
TELOPT(x));
debug(F111,"tn_outst","unanswered WONT",x);
outstanding = 1;
if ( !notquiet )
break;
}
if (TELOPT_UNANSWERED_DONT(x)) {
if ( notquiet )
printf("?Telnet waiting for response to DONT %s\r\n",
TELOPT(x));
debug(F111,"tn_outst","unanswered DONT",x);
outstanding = 1;
if ( !notquiet )
break;
}
if (TELOPT_UNANSWERED_SB(x)) {
if ( notquiet )
printf("?Telnet waiting for response to SB %s\r\n",
TELOPT(x));
debug(F111,"tn_outst","unanswered SB",x);
outstanding = 1;
if ( !notquiet )
break;
}
}
}
#ifdef CK_AUTHENTICATION
if (ck_tn_auth_in_progress()) {
if (TELOPT_ME(TELOPT_AUTHENTICATION)) {
if (notquiet)
printf("?Telnet waiting for WILL %s subnegotiation\r\n",
TELOPT(TELOPT_AUTHENTICATION));
debug(F111,
"tn_outst",
"ME authentication in progress",
TELOPT_AUTHENTICATION
);
outstanding = 1;
} else if (TELOPT_U(TELOPT_AUTHENTICATION)) {
if (notquiet)
printf("?Telnet waiting for DO %s subnegotiation\r\n",
TELOPT(TELOPT_AUTHENTICATION));
debug(F111,
"tn_outst",
"U authentication in progress",
TELOPT_AUTHENTICATION
);
outstanding = 1;
}
}
#endif /* CK_AUTHENTICATION */
#ifdef CK_ENCRYPTION
if (!outstanding) {
e = ck_tn_encrypting();
d = ck_tn_decrypting();
if (TELOPT_ME(TELOPT_ENCRYPTION)) {
if (TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop && e ||
!TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop && !e
) {
if ( notquiet )
printf("?Telnet waiting for WILL %s subnegotiation\r\n",
TELOPT(TELOPT_ENCRYPTION));
debug(F111,
"tn_outst",
"encryption mode switch",
TELOPT_ENCRYPTION
);
outstanding = 1;
}
}
if (TELOPT_U(TELOPT_ENCRYPTION)) {
if (TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop && d ||
!TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop && !d
) {
if ( notquiet )
printf("?Telnet waiting for DO %s subnegotiation\r\n",
TELOPT(TELOPT_ENCRYPTION));
debug(F111,
"tn_outst",
"decryption mode switch",
TELOPT_ENCRYPTION
);
outstanding = 1;
}
}
}
#endif /* CK_ENCRYPTION */
} /* if (tn_wait_flg) */
#ifdef IKS_OPTION
/* Even if we are not waiting for Telnet options we must wait for */
/* Kermit Telnet Subnegotiations if we have sent a request to the */
/* other guy. Otherwise we will get out of sync. */
if (!outstanding) {
if (TELOPT_U(TELOPT_KERMIT) &&
(TELOPT_SB(TELOPT_KERMIT).kermit.me_req_start ||
TELOPT_SB(TELOPT_KERMIT).kermit.me_req_stop ||
!TELOPT_SB(TELOPT_KERMIT).kermit.sop)
) {
if ( notquiet )
printf("?Telnet waiting for SB %s negotiation\r\n",
TELOPT(TELOPT_KERMIT));
debug(F111,"tn_outst","U kermit in progress",TELOPT_KERMIT);
outstanding = 1;
}
}
#endif /* IKS_OPTION */
#ifdef TN_COMPORT
if (!outstanding) {
if (TELOPT_ME(TELOPT_COMPORT)) {
if (TELOPT_SB(TELOPT_COMPORT).comport.wait_for_sb) {
if (notquiet)
printf("?Telnet waiting for SB %s negotiation\r\n",
TELOPT(TELOPT_COMPORT));
debug(F111,"tn_outst","ComPort SB in progress",TELOPT_COMPORT);
outstanding = 1;
}
if (TELOPT_SB(TELOPT_COMPORT).comport.wait_for_ms) {
if (notquiet)
printf("?Telnet waiting for SB %s MODEM_STATUS negotiation\r\n",
TELOPT(TELOPT_COMPORT));
debug(F111,"tn_outst","ComPort SB MS in progress",TELOPT_COMPORT);
outstanding = 1;
}
}
}
#endif /* TN_COMPORT */
return(outstanding);
}
int
istncomport() {
#ifdef TN_COMPORT
if (!local) return(0);
if (ttnet != NET_TCPB) return(0);
if (ttnproto != NP_TELNET) return(0);
if (TELOPT_ME(TELOPT_COMPORT))
return(1);
else
#endif /* TN_COMPORT */
return(0);
}
/* tn_wait() -- Wait for response to Telnet negotiation. */
/*
Wait for up to <timeout> seconds for the response to arrive.
Place all non-telnet data into Telnet Wait Buffer.
If response does arrive return 1, else return 0.
*/
#ifndef TN_WAIT_BUF_SZ
#define TN_WAIT_BUF_SZ 4096
#endif /* TN_WAIT_BUF_SZ */
static char tn_wait_buf[TN_WAIT_BUF_SZ];
static int tn_wait_idx = 0;
#ifndef TN_TIMEOUT
#define TN_TIMEOUT 120
#endif /* TN_TIMEOUT */
static int tn_wait_tmo = TN_TIMEOUT;
#ifdef CKSPINNER
VOID
prtwait(state) int state; {
switch (state % 4) {
case 0:
printf("/");
break;
case 1:
printf("-");
break;
case 2:
printf("\\");
break;
case 3:
printf("|");
break;
}
}
#endif /* CKSPINNER */
static int nflag = 0;
int
#ifdef CK_ANSIC
tn_wait(char * where)
#else
tn_wait(where) char * where;
#endif /* CK_ANSIC */
/* tn_wait */ {
extern int ckxech, local;
int ch = 0, count = 0;
#ifndef NOHINTS
int nohintgiven = 1;
extern int hints;
#endif /* NOHINTS */
int outstanding;
#ifdef TN_COMPORT
int savcarr;
extern int ttcarr;
#endif /* TN_COMPORT */
/* if (!IS_TELNET()) return(1); */
rtimer();
debug(F110,"tn_wait waiting for",where,0);
tn_wait_tmo = TN_TIMEOUT;
debug(F111,"tn_wait","timeout",tn_wait_tmo);
outstanding = tn_outst(0);
debug(F111,"tn_wait","outstanding",outstanding);
debug(F111,"tn_wait","tn_wait_flg",tn_wait_flg);
/* The following is meant to be !(||). We only want to return */
/* immediately if both the tn_wait_flg && tn_outst() are false */
if (!(outstanding || tn_wait_flg)) /* If no need to wait */
return(1); /* Don't. */
if (tn_deb || debses) tn_debug("<wait for outstanding negotiations>");
#ifdef CKSPINNER
if (!sstelnet && !quiet)
prtwait(0);
#endif /* CKSPINNER */
/* Wait up to TN_TIMEOUT sec for responses to outstanding telnet negs */
do {
#ifdef NTSIG
ck_ih();
#endif /* NTSIG */
ch = ttinc(1);
if (ch == -1) { /* Timed out */
if (!sstelnet && !quiet) { /* Let user know... */
#ifdef CKSPINNER
printf("\b");
prtwait(gtimer());
#else
if (nflag == 0) {
printf(" Negotiations.");
nflag++;
}
if (nflag > 0) {
printf(".");
nflag++;
fflush(stdout);
}
#endif /* CKSPINNER */
}
} else if (ch < -1) {
printf("\r\n?Connection closed by peer.\r\n");
if (tn_deb || debses) tn_debug("<connection closed by peer>");
return(-1);
} else
switch (ch) {
case IAC:
#ifdef CKSPINNER
if (!sstelnet && !quiet)
printf("\b");
#endif /* CKSPINNER */
ch = tn_doop((CHAR)(ch & 0xff),inserver?ckxech:duplex,ttinc);
#ifdef CKSPINNER
if (!sstelnet && !quiet) {
prtwait(gtimer());
}
#endif /* CKSPINNER */
debug(F101,"tn_wait tn_doop","",ch);
switch (ch) {
case 1:
duplex = 1; /* Turn on echoing */
if (inserver)
ckxech = 1;
break;
case 2:
duplex = 0; /* Turn off echoing */
if (inserver)
ckxech = 0;
break;
case 3:
tn_wait_buf[tn_wait_idx++] = IAC;
break;
case 4: /* IKS event */
case 6: /* Logout */
break;
case -1:
if (!quiet)
printf("?Telnet Option negotiation error.\n");
if (tn_deb || debses)
tn_debug("<Telnet Option negotiation error>");
return(-1);
case -2:
printf("?Connection closed by peer.\n");
if (tn_deb || debses) tn_debug("<Connection closed by peer>");
ttclos(0);
return(-2);
default:
if (ch < 0) {
if (tn_deb || debses) tn_debug("<Unknown connection error>");
return(ch);
}
} /* switch */
break;
default:
#ifdef CKSPINNER
if (!sstelnet && !quiet) {
printf("\b");
prtwait(gtimer());
}
#endif /* CKSPINNER */
tn_wait_buf[tn_wait_idx++] = (CHAR)(ch & 0xff);
} /* switch */
outstanding = tn_outst(0);
if ( outstanding && ch != IAC ) {
int timer = gtimer();
if ( timer > tn_wait_tmo ) {
if (!sstelnet) {
printf(
"\r\n?Telnet Protocol Timeout - connection closed\r\n");
if (tn_deb || debses)
tn_debug(
"<telnet protocol timeout - connection closed>");
tn_outst(1);
}
/* if we do not close the connection, then we will block */
/* the next time we hit a wait. and if we don't we will */
/* do the wrong thing if the host sends 0xFF and does */
/* not intend it to be an IAC. */
ttclos(0);
whyclosed = WC_TELOPT;
return(-1);
}
#ifndef NOHINTS
else if ( hints && timer > 30 && nohintgiven && !inserver ) {
#ifdef CKSPINNER
printf("\b");
#else /* CKSPINNER */
printf("\r\n");
#endif /* CKSPINNER */
printf("*************************\r\n");
printf("The Telnet %s is not sending required responses.\r\n\r\n",
sstelnet?"client":"server");
tn_outst(1);
printf("\nYou can continue to wait or you can cancel with Ctrl-C.\r\n");
printf("In case the Telnet server never responds as required,\r\n");
printf("you can try connecting to this host with TELNET /NOWAIT.\r\n");
printf("Use SET HINTS OFF to suppress further hints.\r\n");
printf("*************************\r\n");
nohintgiven = 0;
}
#endif /* NOHINTS */
}
#ifdef TN_COMPORT
/* Must disable carrier detect check if we are using Telnet Comport */
savcarr = ttcarr;
ttscarr(CAR_OFF);
count = ttchk();
ttscarr(savcarr);
#else /* TN_COMPORT */
count = ttchk();
#endif /* TN_COMPORT */
} while ((tn_wait_idx < TN_WAIT_BUF_SZ) &&
(outstanding && count >= 0));
if (tn_wait_idx == TN_WAIT_BUF_SZ) {
if (tn_deb || debses) tn_debug("<Telnet Wait Buffer filled>");
return(0);
}
if (!sstelnet && !quiet) {
#ifdef CKSPINNER
printf("\b \b");
#else
if (nflag > 0) {
printf(" (OK)\n");
nflag = -1;
}
#endif /* CKSPINNER */
}
if (tn_deb || debses) tn_debug("<no outstanding negotiations>");
return(0);
}
/* Push data from the Telnet Wait Buffer into the I/O Queue */
/* Return 1 on success */
int
tn_push() {
#ifdef NETLEBUF
extern int tt_push_inited;
#endif /* NETLEBUF */
/* if (!IS_TELNET()) return(1); */
if (tn_wait_idx) {
ckhexdump((CHAR *)"tn_push",tn_wait_buf,tn_wait_idx);
#ifdef NETLEBUF
if (!tt_push_inited) /* Local handling */
le_init();
le_puts((CHAR *)tn_wait_buf,tn_wait_idx);
#else /* External handling... */
#ifdef OS2 /* K95 has its own way */
le_puts((CHAR *)tn_wait_buf,tn_wait_idx);
#else
#ifdef TTLEBUF /* UNIX, etc */
le_puts((CHAR *)tn_wait_buf,tn_wait_idx);
#else
/*
If you see this message in AOS/VS, OS-9, VOS, etc, you need to copy
the #ifdef TTLEBUF..#endif code from ckutio.c to the corresponding
places in your ck?tio.c module.
*/
printf("tn_push called but not implemented - data lost.\n");
#endif /* TTLEBUF */
#endif /* OS2 */
#endif /* NETLEBUF */
tn_wait_idx = 0;
}
tn_wait_tmo = TN_TIMEOUT; /* Reset wait timer stats */
return(1);
}
/* T N _ S O P T */
/*
Sends a telnet option, avoids loops.
Returns 1 if command was sent, 0 if not, -1 on error.
*/
int
tn_sopt(cmd,opt) int cmd, opt; { /* TELNET SEND OPTION */
CHAR buf[5];
char msg[128];
int rc;
if (ttnet != NET_TCPB) return(-1); /* Must be TCP/IP */
if (ttnproto != NP_TELNET) return(-1); /* Must be telnet protocol */
if (!TELCMD_OK(cmd)) return(-1);
if (TELOPT_OK(opt)) {
if (cmd == DO && TELOPT_UNANSWERED_DO(opt)) return(0);
if (cmd == WILL && TELOPT_UNANSWERED_WILL(opt)) return(0);
if (cmd == DONT && TELOPT_UNANSWERED_DONT(opt)) return(0);
if (cmd == WONT && TELOPT_UNANSWERED_WONT(opt)) return(0);
}
#ifdef CK_SSL
if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
return(0);
}
#endif /* CK_SSL */
if (cmd == DO && opt == TELOPT_AUTHENTICATION)
buf[0] = 0;
if (tn_infinite && TELOPT_OK(opt)) { /* See comment above about */
int index = TELOPT_INDEX(opt); /* preventing infinite loops */
int m = cmd - WILL;
if (tncnts[index][m] > MAXTNCNT) {
#ifdef DEBUG
if (tn_deb || debses || deblog) {
ckmakmsg(msg,sizeof(msg),
"TELNET negotiation loop ",
TELCMD(cmd), " ",
TELOPT(opt));
debug(F101,msg,"",opt);
if (tn_deb || debses) tn_debug(msg);
}
#endif /* DEBUG */
return(0);
}
tncnts[index][m]++;
tncnts[index][tnopps[m]] = 0;
}
buf[0] = (CHAR) IAC;
buf[1] = (CHAR) (cmd & 0xff);
buf[2] = (CHAR) (opt & 0xff);
buf[3] = (CHAR) 0;
#ifdef DEBUG
if ((tn_deb || debses || deblog) && cmd != SB)
ckmakmsg(msg,sizeof(msg),"TELNET SENT ",TELCMD(cmd)," ",
TELOPT(opt));
#endif /* DEBUG */
#ifdef OS2
RequestTelnetMutex( SEM_INDEFINITE_WAIT );
#endif
debug(F101,msg,"",opt);
if ((tn_deb || debses) && cmd != SB)
tn_debug(msg);
rc = (ttol(buf,3) < 3);
#ifdef OS2
ReleaseTelnetMutex();
#endif
if (rc)
return(-1);
if (TELOPT_OK(opt)) {
if (cmd == DONT && TELOPT_UNANSWERED_DO(opt))
TELOPT_UNANSWERED_DO(opt) = 0;
if (cmd == WONT && TELOPT_UNANSWERED_WILL(opt))
TELOPT_UNANSWERED_WILL(opt) = 0;
if (cmd == DO && TELOPT_UNANSWERED_DONT(opt))
TELOPT_UNANSWERED_DONT(opt) = 0;
if (cmd == WILL && TELOPT_UNANSWERED_WONT(opt))
TELOPT_UNANSWERED_WONT(opt) = 0;
}
return(1);
}
/* Send a telnet sub-option */
/* Returns 1 if command was sent, 0 if not, -1 on error */
int
tn_ssbopt(opt,sub,data,len) int opt, sub; CHAR * data; int len; {
CHAR buf[256];
int n,m,rc;
if (ttnet != NET_TCPB) return(0); /* Must be TCP/IP */
if (ttnproto != NP_TELNET) return(0); /* Must be telnet protocol */
if (!TELOPT_OK(opt)) return(-1);
if (len < 0 || len > 250) {
debug(F111,"Unable to Send TELNET SB - data too long","len",len);
return(-1); /* Data too long */
}
#ifdef CK_SSL
if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
if (ttchk() < 0)
return(-1);
else
return(1);
}
#endif /* CK_SSL */
if (!data) len = 0;
buf[0] = (CHAR) IAC;
buf[1] = (CHAR) (SB & 0xff);
buf[2] = (CHAR) (opt & 0xff);
buf[3] = (CHAR) (sub & 0xff);
if (data && len > 0) {
memcpy(&buf[4],data,len);
}
buf[4+len] = (CHAR) IAC;
buf[5+len] = (CHAR) SE;
#ifdef DEBUG
if (tn_deb || debses || deblog) {
if (opt == TELOPT_START_TLS && sub == 1)
ckmakmsg(tn_msg_out,TN_MSG_LEN,"TELNET SENT SB ",
TELOPT(opt)," FOLLOWS IAC SE",NULL);
else if (opt == TELOPT_TTYPE && sub == 1)
ckmakmsg(tn_msg_out,TN_MSG_LEN,"TELNET SENT SB ", TELOPT(opt),
" SEND IAC SE",NULL);
else if (opt == TELOPT_TTYPE && sub == 0)
ckmakxmsg(tn_msg_out,TN_MSG_LEN,"TELNET SENT SB ",TELOPT(opt)," IS ",
(char *)data," IAC SE",NULL,NULL,NULL,NULL,NULL,NULL,NULL);
else if (opt == TELOPT_NEWENVIRON) {
int i, quote;
ckmakmsg(tn_msg_out,TN_MSG_LEN,"TELNET SENT SB ",
TELOPT(TELOPT_NEWENVIRON)," ",
sub == TELQUAL_SEND ? "SEND" :
sub == TELQUAL_IS ? "IS" :
sub == TELQUAL_INFO ?"INFO" : "UNKNOWN" );
for (i = 0, quote = 0; i < len; i++) {
if (quote) {
sprintf(hexbuf,"%02x",data[i]); /* safe but ugly */
ckstrncat(tn_msg_out,hexbuf,TN_MSG_LEN);
quote = 0;
} else {
switch (data[i]) {
case TEL_ENV_USERVAR:
ckstrncat(tn_msg_out," USERVAR ",TN_MSG_LEN);
break;
case TEL_ENV_VAR:
ckstrncat(tn_msg_out," VAR ",TN_MSG_LEN);
break;
case TEL_ENV_VALUE:
ckstrncat(tn_msg_out," VALUE ",TN_MSG_LEN);
break;
case TEL_ENV_ESC:
ckstrncat(tn_msg_out," ESC ",TN_MSG_LEN);
quote = 1;
break;
case IAC:
ckstrncat(tn_msg_out," IAC ",TN_MSG_LEN);
break;
default:
sprintf(hexbuf,"%c",data[i]); /* safe but ugly */
ckstrncat(tn_msg_out,hexbuf,TN_MSG_LEN);
}
}
}
ckstrncat(tn_msg_out," IAC SE",TN_MSG_LEN);
} else {
sprintf(hexbuf,"%02x",sub); /* safe but ugly */
ckmakxmsg(tn_msg_out,TN_MSG_LEN,
"TELNET SENT SB ",TELOPT(opt),
" ",
hexbuf,
" <data> IAC SE",
NULL,NULL,NULL,NULL,NULL,NULL,NULL
);
}
}
#endif /* DEBUG */
#ifdef OS2
RequestTelnetMutex( SEM_INDEFINITE_WAIT );
#endif /* OS2 */
#ifdef DEBUG
debug(F101,tn_msg_out,"",opt);
if (tn_deb || debses)
tn_debug(tn_msg_out);
#endif /* DEBUG */
rc = (ttol(buf,6+len) < 6+len);
#ifdef OS2
ReleaseTelnetMutex();
#endif
if (rc)
return(-1);
return(1);
}
/*
tn_flui() -- Processes all waiting data for Telnet commands.
All non-Telnet data is to be stored into the Telnet Wait Buffer.
Returns 1 on success.
*/
int
tn_flui() {
extern int ckxech;
int x = 0;
/* if (!IS_TELNET()) return(0); */
/* Wait up to 5 sec for responses to outstanding telnet negotiations */
while (x >= 0 && ttchk() > 0 && tn_wait_idx < TN_WAIT_BUF_SZ) {
x = ttinc(1);
switch (x) {
case IAC:
x = tn_doop((CHAR)(x & 0xff),inserver?ckxech:duplex,ttinc);
debug(F101,"tn_flui tn_doop","",x);
switch (x) {
case 1: /* Turn on echoing */
duplex = 1;
if (inserver)
ckxech = 1;
break;
case 2: /* Turn off echoing */
duplex = 0;
if (inserver)
ckxech = 0;
break;
case 3:
tn_wait_buf[tn_wait_idx++] = IAC;
break;
case 4: /* IKS event */
case 6: /* Logout */
break;
}
break;
default:
if (x >= 0)
tn_wait_buf[tn_wait_idx++] = x;
}
}
return(1);
}
unsigned char *
tn_get_display()
{
char * disp = NULL;
static char tmploc[256];
/* Must compute the DISPLAY string we are going to send to the host */
/* If one is not assigned, do not send a string unless the user has */
/* explicitedly requested we try to send one via X-Display Location */
/* But do not send a string at all if FORWARD_X is in use. */
/* if (!IS_TELNET()) return(0); */
debug(F110,"tn_get_display() myipaddr",myipaddr,0);
#ifdef CK_ENVIRONMENT
debug(F110,"tn_get_display() tn_env_disp",tn_env_disp,0);
if (tn_env_disp[0]) {
int colon = ckindex(":",tn_env_disp,0,0,1);
if ( !colon ) {
ckmakmsg(tmploc,256,myipaddr,":",tn_env_disp,NULL);
disp = tmploc;
} else if ( ckindex("localhost:",tn_env_disp,0,0,0) ||
ckindex("unix:",tn_env_disp,0,0,0) ||
ckindex("127.0.0.1:",tn_env_disp,0,0,0) ||
!ckstrcmp("0:",tn_env_disp,2,1) ||
tn_env_disp[0] == ':' ) {
ckmakmsg(tmploc,256,myipaddr,":",&tn_env_disp[colon],NULL);
disp = tmploc;
} else
disp = tn_env_disp;
}
else
#endif /* CK_ENVIRONMENT */
if (TELOPT_ME(TELOPT_XDISPLOC) ||
TELOPT_U(TELOPT_FORWARD_X)) {
ckmakmsg(tmploc,256,myipaddr,":0.0",NULL,NULL);
disp = tmploc;
}
debug(F110,"tn_get_display() returns",disp,0);
return((CHAR *)disp);
}