1 /* $NetBSD: telnet.c,v 1.30 2005/03/29 12:18:28 drochner Exp $ */
4 * Copyright (c) 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95";
37 __RCSID("$NetBSD: telnet.c,v 1.30 2005/03/29 12:18:28 drochner Exp $");
41 #include <sys/param.h>
46 /* By the way, we need to include curses.h before telnet.h since,
47 * among other things, telnet.h #defines 'DO', which is a variable
48 * declared in curses.h.
51 #include <arpa/telnet.h>
61 #include <libtelnet/misc.h>
63 #include <libtelnet/auth.h>
66 #include <libtelnet/encrypt.h>
70 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
72 static unsigned char subbuffer
[SUBBUFSIZE
],
73 *subpointer
, *subend
; /* buffer for sub-options */
74 #define SB_CLEAR() subpointer = subbuffer;
75 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
76 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
77 *subpointer++ = (c); \
80 #define SB_GET() ((*subpointer++)&0xff)
81 #define SB_PEEK() ((*subpointer)&0xff)
82 #define SB_EOF() (subpointer >= subend)
83 #define SB_LEN() (subend - subpointer)
85 char options
[256]; /* The combined options */
86 char do_dont_resp
[256];
87 char will_wont_resp
[256];
91 autologin
= 0, /* Autologin anyone? */
95 In3270
, /* Are we in 3270 mode? */
96 ISend
, /* trying to send network data in */
99 netdata
, /* Print out network data flow */
100 crlf
, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
102 noasynchtty
= 0,/* User specified "-noasynch" on command line */
103 noasynchnet
= 0,/* User specified "-noasynch" on command line */
104 askedSGA
= 0, /* We have talked about suppress go ahead */
105 #endif /* defined(TN3270) */
107 SYNCHing
, /* we are in TELNET SYNCH mode */
108 flushout
, /* flush output */
109 autoflush
= 0, /* flush output when interrupting? */
110 autosynch
, /* send interrupt characters with SYNCH? */
111 localflow
, /* we handle flow control locally */
112 restartany
, /* if flow control enabled, restart on any character */
113 localchars
, /* we recognize interrupt/quit */
114 donelclchars
, /* the user has set "localchars" */
115 donebinarytoggle
, /* the user has put us in binary */
116 dontlecho
, /* do we suppress local echoing right now? */
118 doaddrlookup
= 1, /* do a reverse address lookup? */
125 #ifdef KLUDGELINEMODE
130 * Telnet receiver states for fsm
139 #define TS_SB 7 /* sub-option collection */
140 #define TS_SE 8 /* looking for sub-option end */
142 static int telrcv_state
;
144 unsigned char telopt_environ
= TELOPT_NEW_ENVIRON
;
146 # define telopt_environ TELOPT_NEW_ENVIRON
149 jmp_buf toplevel
= { 0 };
154 #ifdef KLUDGELINEMODE
155 int kludgelinemode
= 1;
158 static void dooption(int);
159 static void dontoption(int);
160 static void suboption(void);
161 static int telsnd(void);
162 static void netclear(void);
163 static void doflush(void);
166 * The following are some clocks used to decide how to interpret
167 * the relationship between various variables.
173 Modelist modelist
[] = {
174 { "telnet command mode", COMMAND_LINE
},
175 { "character-at-a-time mode", 0 },
176 { "character-at-a-time mode (local echo)", LOCAL_ECHO
|LOCAL_CHARS
},
177 { "line-by-line mode (remote echo)", LINE
| LOCAL_CHARS
},
178 { "line-by-line mode", LINE
| LOCAL_ECHO
| LOCAL_CHARS
},
179 { "line-by-line mode (local echoing suppressed)", LINE
| LOCAL_CHARS
},
186 * Initialize telnet environment.
197 connected
= In3270
= ISend
= localflow
= donebinarytoggle
= 0;
198 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
199 auth_encrypt_connect(connected
);
200 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
205 /* Don't change NetTrace */
207 escape
= CONTROL(']');
208 rlogin
= _POSIX_VDISABLE
;
209 #ifdef KLUDGELINEMODE
210 echoc
= CONTROL('E');
214 telrcv_state
= TS_DATA
;
223 printring(Ring
*ring
, char *format
, ...)
227 char buffer
[100]; /* where things go */
232 va_start(ap
, format
);
236 while ((i
= *format
++) != 0) {
241 *ptr
++ = va_arg(ap
, int);
244 string
= va_arg(ap
, char *);
245 ring_supply_data(ring
, buffer
, ptr
-buffer
);
246 ring_supply_data(ring
, string
, strlen(string
));
250 ExitString("printring: trailing %%.\n", 1);
253 ExitString("printring: unknown format character.\n", 1);
261 ring_supply_data(ring
, buffer
, ptr
-buffer
);
266 * These routines are in charge of sending option negotiations
269 * The basic idea is that we send the negotiation if either side
270 * is in disagreement as to what the current state should be.
274 send_do(int c
, int init
)
277 if (((do_dont_resp
[c
] == 0) && my_state_is_do(c
)) ||
278 my_want_state_is_do(c
))
280 set_my_want_state_do(c
);
285 printoption("SENT", DO
, c
);
289 send_dont(int c
, int init
)
292 if (((do_dont_resp
[c
] == 0) && my_state_is_dont(c
)) ||
293 my_want_state_is_dont(c
))
295 set_my_want_state_dont(c
);
300 printoption("SENT", DONT
, c
);
304 send_will(int c
, int init
)
307 if (((will_wont_resp
[c
] == 0) && my_state_is_will(c
)) ||
308 my_want_state_is_will(c
))
310 set_my_want_state_will(c
);
315 printoption("SENT", WILL
, c
);
319 send_wont(int c
, int init
)
322 if (((will_wont_resp
[c
] == 0) && my_state_is_wont(c
)) ||
323 my_want_state_is_wont(c
))
325 set_my_want_state_wont(c
);
330 printoption("SENT", WONT
, c
);
335 willoption(int option
)
337 int new_state_ok
= 0;
339 if (do_dont_resp
[option
]) {
340 --do_dont_resp
[option
];
341 if (do_dont_resp
[option
] && my_state_is_do(option
))
342 --do_dont_resp
[option
];
345 if ((do_dont_resp
[option
] == 0) && my_want_state_is_dont(option
)) {
352 * The following is a pain in the rear-end.
353 * Various IBM servers (some versions of Wiscnet,
354 * possibly Fibronics/Spartacus, and who knows who
355 * else) will NOT allow us to send "DO SGA" too early
356 * in the setup proceedings. On the other hand,
357 * 4.2 servers (telnetd) won't set SGA correctly.
358 * So, we are stuck. Empirically (but, based on
359 * a VERY small sample), the IBM servers don't send
360 * out anything about ECHO, so we postpone our sending
361 * "DO SGA" until we see "WILL ECHO" (which 4.2 servers
367 if (my_want_state_is_dont(TELOPT_SGA
))
368 send_do(TELOPT_SGA
, 1);
373 #endif /* defined(TN3270) */
376 settimer(modenegotiated
);
379 #ifdef AUTHENTICATION
380 case TELOPT_AUTHENTICATION
:
383 #endif /* ENCRYPTION */
392 * Special case for TM. If we get back a WILL,
393 * pretend we got back a WONT.
395 set_my_want_state_dont(option
);
396 set_my_state_dont(option
);
397 return; /* Never reply to TM will's/wont's */
399 case TELOPT_LINEMODE
:
405 set_my_want_state_do(option
);
407 setconnmode(0); /* possibly set new tty mode */
409 do_dont_resp
[option
]++;
410 send_dont(option
, 0);
413 set_my_state_do(option
);
415 if (option
== TELOPT_ENCRYPT
)
416 encrypt_send_support();
417 #endif /* ENCRYPTION */
421 wontoption(int option
)
423 if (do_dont_resp
[option
]) {
424 --do_dont_resp
[option
];
425 if (do_dont_resp
[option
] && my_state_is_dont(option
))
426 --do_dont_resp
[option
];
429 if ((do_dont_resp
[option
] == 0) && my_want_state_is_do(option
)) {
433 #ifdef KLUDGELINEMODE
440 settimer(modenegotiated
);
446 set_my_want_state_dont(option
);
447 set_my_state_dont(option
);
448 return; /* Never reply to TM will's/wont's */
453 set_my_want_state_dont(option
);
454 if (my_state_is_do(option
))
455 send_dont(option
, 0);
456 setconnmode(0); /* Set new tty mode */
457 } else if (option
== TELOPT_TM
) {
459 * Special case for TM.
463 set_my_want_state_dont(option
);
465 set_my_state_dont(option
);
471 int new_state_ok
= 0;
473 if (will_wont_resp
[option
]) {
474 --will_wont_resp
[option
];
475 if (will_wont_resp
[option
] && my_state_is_will(option
))
476 --will_wont_resp
[option
];
479 if (will_wont_resp
[option
] == 0) {
480 if (my_want_state_is_wont(option
)) {
486 * Special case for TM. We send a WILL, but pretend
489 send_will(option
, 0);
490 set_my_want_state_wont(TELOPT_TM
);
491 set_my_state_wont(TELOPT_TM
);
495 case TELOPT_EOR
: /* end of record */
496 # endif /* defined(TN3270) */
497 case TELOPT_BINARY
: /* binary mode */
498 case TELOPT_NAWS
: /* window size */
499 case TELOPT_TSPEED
: /* terminal speed */
500 case TELOPT_LFLOW
: /* local flow control */
501 case TELOPT_TTYPE
: /* terminal type option */
502 case TELOPT_SGA
: /* no big deal */
504 case TELOPT_ENCRYPT
: /* encryption variable option */
505 #endif /* ENCRYPTION */
509 case TELOPT_NEW_ENVIRON
: /* New environment variable option */
511 if (my_state_is_will(TELOPT_OLD_ENVIRON
))
512 send_wont(TELOPT_OLD_ENVIRON
, 1); /* turn off the old */
514 case TELOPT_OLD_ENVIRON
: /* Old environment variable option */
515 if (my_state_is_will(TELOPT_NEW_ENVIRON
))
516 break; /* Don't enable if new one is in use! */
518 telopt_environ
= option
;
523 #ifdef AUTHENTICATION
524 case TELOPT_AUTHENTICATION
:
530 case TELOPT_XDISPLOC
: /* X Display location */
531 if (env_getvalue((unsigned char *)"DISPLAY"))
535 case TELOPT_LINEMODE
:
536 #ifdef KLUDGELINEMODE
538 send_do(TELOPT_SGA
, 1);
540 set_my_want_state_will(TELOPT_LINEMODE
);
541 send_will(option
, 0);
542 set_my_state_will(TELOPT_LINEMODE
);
546 case TELOPT_ECHO
: /* We're never going to echo... */
552 set_my_want_state_will(option
);
553 send_will(option
, 0);
554 setconnmode(0); /* Set new tty mode */
556 will_wont_resp
[option
]++;
557 send_wont(option
, 0);
561 * Handle options that need more things done after the
562 * other side has acknowledged the option.
565 case TELOPT_LINEMODE
:
566 #ifdef KLUDGELINEMODE
568 send_do(TELOPT_SGA
, 1);
570 set_my_state_will(option
);
572 send_do(TELOPT_SGA
, 0);
577 set_my_state_will(option
);
581 dontoption(int option
)
584 if (will_wont_resp
[option
]) {
585 --will_wont_resp
[option
];
586 if (will_wont_resp
[option
] && my_state_is_wont(option
))
587 --will_wont_resp
[option
];
590 if ((will_wont_resp
[option
] == 0) && my_want_state_is_will(option
)) {
592 case TELOPT_LINEMODE
:
593 linemode
= 0; /* put us back to the default state */
596 case TELOPT_NEW_ENVIRON
:
598 * The new environ option wasn't recognized, try
601 send_will(TELOPT_OLD_ENVIRON
, 1);
602 telopt_environ
= TELOPT_OLD_ENVIRON
;
606 /* we always accept a DONT */
607 set_my_want_state_wont(option
);
608 if (my_state_is_will(option
))
609 send_wont(option
, 0);
610 setconnmode(0); /* Set new tty mode */
612 set_my_state_wont(option
);
616 * Given a buffer returned by tgetent(), this routine will turn
617 * the pipe separated list of names in the buffer into an array
618 * of pointers to null terminated names. We toss out any bad,
619 * duplicate, or verbose names (names with spaces).
622 static char *name_unknown
= "UNKNOWN";
623 static char *unknown
[] = { 0, 0 };
626 mklist(char *buf
, char *name
)
629 char c
, *cp
, **argvp
, *cp2
, **argv
, **avt
;
632 if ((int)strlen(name
) > 40) {
634 unknown
[0] = name_unknown
;
640 unknown
[0] = name_unknown
;
642 * Count up the number of names.
644 for (n
= 1, cp
= buf
; *cp
&& *cp
!= ':'; cp
++) {
649 * Allocate an array to put the name pointers into
651 argv
= (char **)malloc((n
+3)*sizeof(char *));
656 * Fill up the array of pointers to names.
661 for (cp
= cp2
= buf
; (c
= *cp
); cp
++) {
662 if (c
== '|' || c
== ':') {
665 * Skip entries that have spaces or are over 40
666 * characters long. If this is our environment
667 * name, then put it up front. Otherwise, as
668 * long as this is not a duplicate name (case
669 * insensitive) add it to the list.
671 if (n
|| (cp
- cp2
> 41))
673 else if (name
&& (strncasecmp(name
, cp2
, cp
-cp2
) == 0))
675 else if (is_unique(cp2
, argv
+1, argvp
))
680 * Skip multiple delimiters. Reset cp2 to
681 * the beginning of the next name. Reset n,
682 * the flag for names with spaces.
684 while ((c
= *cp
) == '|')
690 * Skip entries with spaces or non-ascii values.
691 * Convert lower case letters to upper case.
693 if ((c
== ' ') || !isascii(c
))
695 else if (islower((unsigned char)c
))
696 *cp
= toupper((unsigned char)c
);
700 * Check for an old V6 2 character name. If the second
701 * name points to the beginning of the buffer, and is
702 * only 2 characters long, move it to the end of the array.
704 if ((argv
[1] == buf
) && (strlen(argv
[1]) == 2)) {
706 for (avt
= &argv
[1]; avt
< argvp
; avt
++)
712 * Duplicate last name, for TTYPE option, and null
713 * terminate the array. If we didn't find a match on
714 * our terminal name, put that name at the beginning.
725 for (avt
= argv
; avt
< argvp
; avt
++)
736 is_unique(char *name
, char **as
, char **ae
)
741 n
= strlen(name
) + 1;
742 for (ap
= as
; ap
< ae
; ap
++)
743 if (strncasecmp(*ap
, name
, n
) == 0)
753 setup_term(char *tname
, int fd
, int *errp
)
755 char zz
[1024], *zz_ptr
;
756 char *ext_tc
, *newptr
;
758 if ((termbuf
= (char *) malloc(1024)) == NULL
)
761 if (tgetent(termbuf
, tname
) == 1) {
762 /* check for ZZ capability, which indicates termcap truncated */
764 if (tgetstr("ZZ", &zz_ptr
) != NULL
) {
765 /* it was, fish back the full termcap */
766 sscanf(zz
, "%p", &ext_tc
);
767 if ((newptr
= (char *) realloc(termbuf
,
773 strlcpy(newptr
, ext_tc
, strlen(ext_tc
) + 1);
787 #define termbuf ttytype
788 extern char ttytype
[];
791 int resettermname
= 1;
797 static char **tnamep
= 0;
803 if (tnamep
&& tnamep
!= unknown
)
805 if ((tname
= (char *)env_getvalue((unsigned char *)"TERM")) &&
806 (setup_term(tname
, 1, &err
) == 0)) {
807 tnamep
= mklist(termbuf
, tname
);
809 if (tname
&& ((int)strlen(tname
) <= 40)) {
813 unknown
[0] = name_unknown
;
825 * Look at the sub-option buffer, and try to be helpful to the other
828 * Currently we recognize:
830 * Terminal type, send request.
831 * Terminal speed (send request).
832 * Local flow control (is request).
839 unsigned char subchar
;
841 printsub('<', subbuffer
, SB_LEN()+2);
842 switch (subchar
= SB_GET()) {
844 if (my_want_state_is_wont(TELOPT_TTYPE
))
846 if (SB_EOF() || SB_GET() != TELQUAL_SEND
) {
850 unsigned char temp
[50];
854 if (tn3270_ttype()) {
857 #endif /* defined(TN3270) */
858 name
= gettermname();
859 len
= strlen(name
) + 4 + 2;
860 if (len
< NETROOM()) {
861 sprintf((char *)temp
, "%c%c%c%c%s%c%c", IAC
, SB
, TELOPT_TTYPE
,
862 TELQUAL_IS
, name
, IAC
, SE
);
863 ring_supply_data(&netoring
, temp
, len
);
864 printsub('>', &temp
[2], len
-2);
866 ExitString("No room in buffer for terminal type.\n", 1);
872 if (my_want_state_is_wont(TELOPT_TSPEED
))
876 if (SB_GET() == TELQUAL_SEND
) {
878 unsigned char temp
[50];
881 TerminalSpeeds(&ispeed
, &ospeed
);
883 sprintf((char *)temp
, "%c%c%c%c%ld,%ld%c%c", IAC
, SB
, TELOPT_TSPEED
,
884 TELQUAL_IS
, (long)ospeed
, (long)ispeed
, IAC
, SE
);
885 len
= strlen((char *)temp
+4) + 4; /* temp[3] is 0 ... */
887 if (len
< NETROOM()) {
888 ring_supply_data(&netoring
, temp
, len
);
889 printsub('>', temp
+2, len
- 2);
891 /*@*/ else printf("lm_will: not enough room in buffer\n");
895 if (my_want_state_is_wont(TELOPT_LFLOW
))
900 case LFLOW_RESTART_ANY
:
903 case LFLOW_RESTART_XON
:
919 case TELOPT_LINEMODE
:
920 if (my_want_state_is_wont(TELOPT_LINEMODE
))
926 lm_will(subpointer
, SB_LEN());
929 lm_wont(subpointer
, SB_LEN());
932 lm_do(subpointer
, SB_LEN());
935 lm_dont(subpointer
, SB_LEN());
938 slc(subpointer
, SB_LEN());
941 lm_mode(subpointer
, SB_LEN(), 0);
949 case TELOPT_OLD_ENVIRON
:
951 case TELOPT_NEW_ENVIRON
:
957 if (my_want_state_is_dont(subchar
))
961 if (my_want_state_is_wont(subchar
)) {
968 env_opt(subpointer
, SB_LEN());
971 case TELOPT_XDISPLOC
:
972 if (my_want_state_is_wont(TELOPT_XDISPLOC
))
976 if (SB_GET() == TELQUAL_SEND
) {
977 unsigned char temp
[50], *dp
;
980 if ((dp
= env_getvalue((unsigned char *)"DISPLAY")) == NULL
) {
982 * Something happened, we no longer have a DISPLAY
983 * variable. So, turn off the option.
985 send_wont(TELOPT_XDISPLOC
, 1);
988 sprintf((char *)temp
, "%c%c%c%c%s%c%c", IAC
, SB
, TELOPT_XDISPLOC
,
989 TELQUAL_IS
, dp
, IAC
, SE
);
990 len
= strlen((char *)temp
+4) + 4; /* temp[3] is 0 ... */
992 if (len
< NETROOM()) {
993 ring_supply_data(&netoring
, temp
, len
);
994 printsub('>', temp
+2, len
- 2);
996 /*@*/ else printf("lm_will: not enough room in buffer\n");
1000 #ifdef AUTHENTICATION
1001 case TELOPT_AUTHENTICATION
: {
1008 if (my_want_state_is_dont(TELOPT_AUTHENTICATION
))
1010 auth_is(subpointer
, SB_LEN());
1013 if (my_want_state_is_wont(TELOPT_AUTHENTICATION
))
1015 auth_send(subpointer
, SB_LEN());
1018 if (my_want_state_is_wont(TELOPT_AUTHENTICATION
))
1020 auth_reply(subpointer
, SB_LEN());
1023 if (my_want_state_is_dont(TELOPT_AUTHENTICATION
))
1025 auth_name(subpointer
, SB_LEN());
1032 case TELOPT_ENCRYPT
:
1037 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1039 encrypt_start(subpointer
, SB_LEN());
1042 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1046 case ENCRYPT_SUPPORT
:
1047 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1049 encrypt_support(subpointer
, SB_LEN());
1051 case ENCRYPT_REQSTART
:
1052 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1054 encrypt_request_start(subpointer
, SB_LEN());
1056 case ENCRYPT_REQEND
:
1057 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1060 * We can always send an REQEND so that we cannot
1061 * get stuck encrypting. We should only get this
1062 * if we have been able to get in the correct mode
1065 encrypt_request_end();
1068 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1070 encrypt_is(subpointer
, SB_LEN());
1073 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1075 encrypt_reply(subpointer
, SB_LEN());
1077 case ENCRYPT_ENC_KEYID
:
1078 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1080 encrypt_enc_keyid(subpointer
, SB_LEN());
1082 case ENCRYPT_DEC_KEYID
:
1083 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1085 encrypt_dec_keyid(subpointer
, SB_LEN());
1091 #endif /* ENCRYPTION */
1097 static unsigned char str_lm
[] = { IAC
, SB
, TELOPT_LINEMODE
, 0, 0, IAC
, SE
};
1100 lm_will(unsigned char *cmd
, int len
)
1103 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
1107 case LM_FORWARDMASK
: /* We shouldn't ever get this... */
1111 if (NETROOM() > sizeof(str_lm
)) {
1112 ring_supply_data(&netoring
, str_lm
, sizeof(str_lm
));
1113 printsub('>', &str_lm
[2], sizeof(str_lm
)-2);
1115 /*@*/ else printf("lm_will: not enough room in buffer\n");
1121 lm_wont(unsigned char *cmd
, int len
)
1124 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1128 case LM_FORWARDMASK
: /* We shouldn't ever get this... */
1130 /* We are always DONT, so don't respond */
1136 lm_do(unsigned char *cmd
, int len
)
1139 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1143 case LM_FORWARDMASK
:
1147 if (NETROOM() > sizeof(str_lm
)) {
1148 ring_supply_data(&netoring
, str_lm
, sizeof(str_lm
));
1149 printsub('>', &str_lm
[2], sizeof(str_lm
)-2);
1151 /*@*/ else printf("lm_do: not enough room in buffer\n");
1157 lm_dont(unsigned char *cmd
, int len
)
1160 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1164 case LM_FORWARDMASK
:
1166 /* we are always WONT, so don't respond */
1171 static unsigned char str_lm_mode
[] = {
1172 IAC
, SB
, TELOPT_LINEMODE
, LM_MODE
, 0, IAC
, SE
1176 lm_mode(unsigned char *cmd
, int len
, int init
)
1180 if ((linemode
&MODE_MASK
&~MODE_ACK
) == *cmd
)
1184 linemode
= *cmd
&(MODE_MASK
&~MODE_ACK
);
1185 str_lm_mode
[4] = linemode
;
1187 str_lm_mode
[4] |= MODE_ACK
;
1188 if (NETROOM() > sizeof(str_lm_mode
)) {
1189 ring_supply_data(&netoring
, str_lm_mode
, sizeof(str_lm_mode
));
1190 printsub('>', &str_lm_mode
[2], sizeof(str_lm_mode
)-2);
1192 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1193 setconnmode(0); /* set changed mode */
1200 * Handle special character suboption of LINEMODE.
1206 char flags
; /* Current flags & level */
1207 char mylevel
; /* Maximum level & flags */
1210 #define SLC_IMPORT 0
1211 #define SLC_EXPORT 1
1212 #define SLC_RVALUE 2
1213 static int slc_mode
= SLC_EXPORT
;
1221 for (spcp
= spc_data
; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1224 spcp
->flags
= spcp
->mylevel
= SLC_NOSUPPORT
;
1227 #define initfunc(func, flags) { \
1228 spcp = &spc_data[func]; \
1229 if ((spcp->valp = tcval(func)) != NULL){ \
1230 spcp->val = *spcp->valp; \
1231 spcp->mylevel = SLC_VARIABLE|flags; \
1234 spcp->mylevel = SLC_DEFAULT; \
1238 initfunc(SLC_SYNCH
, 0);
1240 initfunc(SLC_AO
, 0);
1241 initfunc(SLC_AYT
, 0);
1243 initfunc(SLC_ABORT
, SLC_FLUSHIN
|SLC_FLUSHOUT
);
1244 initfunc(SLC_EOF
, 0);
1245 initfunc(SLC_SUSP
, SLC_FLUSHIN
);
1246 initfunc(SLC_EC
, 0);
1247 initfunc(SLC_EL
, 0);
1248 initfunc(SLC_EW
, 0);
1249 initfunc(SLC_RP
, 0);
1250 initfunc(SLC_LNEXT
, 0);
1251 initfunc(SLC_XON
, 0);
1252 initfunc(SLC_XOFF
, 0);
1253 initfunc(SLC_FORW1
, 0);
1254 initfunc(SLC_FORW2
, 0);
1257 initfunc(SLC_IP
, SLC_FLUSHIN
|SLC_FLUSHOUT
);
1260 if (slc_mode
== SLC_EXPORT
)
1270 printf("Special characters are %s values\n",
1271 slc_mode
== SLC_IMPORT
? "remote default" :
1272 slc_mode
== SLC_EXPORT
? "local" :
1277 slc_mode_export(int n
)
1279 slc_mode
= SLC_EXPORT
;
1280 if (my_state_is_will(TELOPT_LINEMODE
))
1285 slc_mode_import(int def
)
1287 slc_mode
= def
? SLC_IMPORT
: SLC_RVALUE
;
1288 if (my_state_is_will(TELOPT_LINEMODE
))
1292 unsigned char slc_import_val
[] = {
1293 IAC
, SB
, TELOPT_LINEMODE
, LM_SLC
, 0, SLC_VARIABLE
, 0, IAC
, SE
1295 unsigned char slc_import_def
[] = {
1296 IAC
, SB
, TELOPT_LINEMODE
, LM_SLC
, 0, SLC_DEFAULT
, 0, IAC
, SE
1302 if (NETROOM() > sizeof(slc_import_val
)) {
1304 ring_supply_data(&netoring
, slc_import_def
, sizeof(slc_import_def
));
1305 printsub('>', &slc_import_def
[2], sizeof(slc_import_def
)-2);
1307 ring_supply_data(&netoring
, slc_import_val
, sizeof(slc_import_val
));
1308 printsub('>', &slc_import_val
[2], sizeof(slc_import_val
)-2);
1311 /*@*/ else printf("slc_import: not enough room\n");
1319 TerminalDefaultChars();
1322 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1323 if (spcp
->mylevel
!= SLC_NOSUPPORT
) {
1324 if (spcp
->val
== (cc_t
)(_POSIX_VDISABLE
))
1325 spcp
->flags
= SLC_NOSUPPORT
;
1327 spcp
->flags
= spcp
->mylevel
;
1329 spcp
->val
= *spcp
->valp
;
1330 slc_add_reply(spcp
- spc_data
, spcp
->flags
, spcp
->val
);
1335 setconnmode(1); /* Make sure the character values are set */
1339 slc(unsigned char *cp
, int len
)
1346 for (; len
>= 3; len
-=3, cp
+=3) {
1348 func
= cp
[SLC_FUNC
];
1352 * Client side: always ignore 0 function.
1357 if ((cp
[SLC_FLAGS
] & SLC_LEVELBITS
) != SLC_NOSUPPORT
)
1358 slc_add_reply(func
, SLC_NOSUPPORT
, 0);
1362 spcp
= &spc_data
[func
];
1364 level
= cp
[SLC_FLAGS
]&(SLC_LEVELBITS
|SLC_ACK
);
1366 if ((cp
[SLC_VALUE
] == (unsigned char)spcp
->val
) &&
1367 ((level
&SLC_LEVELBITS
) == (spcp
->flags
&SLC_LEVELBITS
))) {
1371 if (level
== (SLC_DEFAULT
|SLC_ACK
)) {
1373 * This is an error condition, the SLC_ACK
1374 * bit should never be set for the SLC_DEFAULT
1375 * level. Our best guess to recover is to
1376 * ignore the SLC_ACK bit.
1378 cp
[SLC_FLAGS
] &= ~SLC_ACK
;
1381 if (level
== ((spcp
->flags
&SLC_LEVELBITS
)|SLC_ACK
)) {
1382 spcp
->val
= (cc_t
)cp
[SLC_VALUE
];
1383 spcp
->flags
= cp
[SLC_FLAGS
]; /* include SLC_ACK */
1389 if (level
<= (spcp
->mylevel
&SLC_LEVELBITS
)) {
1390 spcp
->flags
= cp
[SLC_FLAGS
]|SLC_ACK
;
1391 spcp
->val
= (cc_t
)cp
[SLC_VALUE
];
1393 if (level
== SLC_DEFAULT
) {
1394 if ((spcp
->mylevel
&SLC_LEVELBITS
) != SLC_DEFAULT
)
1395 spcp
->flags
= spcp
->mylevel
;
1397 spcp
->flags
= SLC_NOSUPPORT
;
1399 slc_add_reply(func
, spcp
->flags
, spcp
->val
);
1403 setconnmode(1); /* set the new character values */
1412 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1413 if (spcp
->valp
&& spcp
->val
!= *spcp
->valp
) {
1414 spcp
->val
= *spcp
->valp
;
1415 if (spcp
->val
== (cc_t
)(_POSIX_VDISABLE
))
1416 spcp
->flags
= SLC_NOSUPPORT
;
1418 spcp
->flags
= spcp
->mylevel
;
1419 slc_add_reply(spcp
- spc_data
, spcp
->flags
, spcp
->val
);
1427 unsigned char slc_reply
[128];
1428 unsigned char *slc_replyp
;
1431 slc_start_reply(void)
1433 slc_replyp
= slc_reply
;
1434 *slc_replyp
++ = IAC
;
1436 *slc_replyp
++ = TELOPT_LINEMODE
;
1437 *slc_replyp
++ = LM_SLC
;
1441 slc_add_reply(unsigned int func
, unsigned int flags
, cc_t value
)
1443 if ((slc_replyp
- slc_reply
) + 6 > sizeof(slc_reply
))
1445 if ((*slc_replyp
++ = func
) == IAC
)
1446 *slc_replyp
++ = IAC
;
1447 if ((*slc_replyp
++ = flags
) == IAC
)
1448 *slc_replyp
++ = IAC
;
1449 if ((*slc_replyp
++ = (unsigned char)value
) == IAC
)
1450 *slc_replyp
++ = IAC
;
1458 len
= slc_replyp
- slc_reply
;
1459 if (len
<= 4 || (len
+ 2 > sizeof(slc_reply
)))
1461 *slc_replyp
++ = IAC
;
1464 if (NETROOM() > len
) {
1465 ring_supply_data(&netoring
, slc_reply
, slc_replyp
- slc_reply
);
1466 printsub('>', &slc_reply
[2], slc_replyp
- slc_reply
- 2);
1468 /*@*/else printf("slc_end_reply: not enough room\n");
1475 int need_update
= 0;
1477 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1478 if (!(spcp
->flags
&SLC_ACK
))
1480 spcp
->flags
&= ~SLC_ACK
;
1481 if (spcp
->valp
&& (*spcp
->valp
!= spcp
->val
)) {
1482 *spcp
->valp
= spcp
->val
;
1486 return(need_update
);
1492 * Earlier version of telnet/telnetd from the BSD code had
1493 * the definitions of VALUE and VAR reversed. To ensure
1494 * maximum interoperability, we assume that the server is
1495 * an older BSD server, until proven otherwise. The newer
1496 * BSD servers should be able to handle either definition,
1497 * so it is better to use the wrong values if we don't
1498 * know what type of server it is.
1501 int old_env_var
= OLD_ENV_VAR
;
1502 int old_env_value
= OLD_ENV_VALUE
;
1504 # define old_env_var OLD_ENV_VAR
1505 # define old_env_value OLD_ENV_VALUE
1510 env_opt(unsigned char *buf
, int len
)
1512 unsigned char *ep
= 0, *epc
= 0;
1515 switch(buf
[0]&0xff) {
1520 } else for (i
= 1; i
< len
; i
++) {
1521 switch (buf
[i
]&0xff) {
1525 if (telopt_environ
== TELOPT_OLD_ENVIRON
1527 /* Server has the same definitions */
1528 old_env_var
= OLD_ENV_VAR
;
1529 old_env_value
= OLD_ENV_VALUE
;
1535 * Although OLD_ENV_VALUE is not legal, we will
1536 * still recognize it, just in case it is an
1537 * old server that has VAR & VALUE mixed up...
1548 ep
= epc
= &buf
[i
+1];
1568 /* Ignore for now. We shouldn't get it anyway. */
1576 #define OPT_REPLY_SIZE 256
1577 unsigned char *opt_reply
;
1578 unsigned char *opt_replyp
;
1579 unsigned char *opt_replyend
;
1587 p
= (unsigned char *)realloc(opt_reply
, OPT_REPLY_SIZE
);
1591 p
= (unsigned char *)malloc(OPT_REPLY_SIZE
);
1593 if (opt_reply
== NULL
) {
1594 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1595 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1598 opt_replyp
= opt_reply
;
1599 opt_replyend
= opt_reply
+ OPT_REPLY_SIZE
;
1600 *opt_replyp
++ = IAC
;
1602 *opt_replyp
++ = telopt_environ
;
1603 *opt_replyp
++ = TELQUAL_IS
;
1607 env_opt_start_info(void)
1611 opt_replyp
[-1] = TELQUAL_INFO
;
1615 env_opt_add(unsigned char *ep
)
1617 unsigned char *vp
, c
;
1618 unsigned int len
, olen
, elen
;
1620 if (opt_reply
== NULL
) /*XXX*/
1623 if (ep
== NULL
|| *ep
== '\0') {
1624 /* Send user defined variables first. */
1626 while ((ep
= env_default(0, 0)) != NULL
)
1629 /* Now add the list of well know variables. */
1631 while ((ep
= env_default(0, 1)) != NULL
)
1635 vp
= env_getvalue(ep
);
1636 elen
= 2 * (vp
? strlen((char *)vp
) : 0) +
1637 2 * strlen((char *)ep
) + 6;
1638 if ((opt_replyend
- opt_replyp
) < elen
)
1641 len
= opt_replyend
- opt_reply
+ elen
;
1642 olen
= opt_replyp
- opt_reply
;
1643 p
= (unsigned char *)realloc(opt_reply
, len
);
1647 if (opt_reply
== NULL
) {
1648 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1649 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1652 opt_replyp
= opt_reply
+ olen
;
1653 opt_replyend
= opt_reply
+ len
;
1655 if (opt_welldefined(ep
))
1657 if (telopt_environ
== TELOPT_OLD_ENVIRON
)
1658 *opt_replyp
++ = old_env_var
;
1661 *opt_replyp
++ = NEW_ENV_VAR
;
1663 *opt_replyp
++ = ENV_USERVAR
;
1665 while ((c
= *ep
++) != '\0') {
1668 *opt_replyp
++ = IAC
;
1674 *opt_replyp
++ = ENV_ESC
;
1679 if ((ep
= vp
) != NULL
) {
1681 if (telopt_environ
== TELOPT_OLD_ENVIRON
)
1682 *opt_replyp
++ = old_env_value
;
1685 *opt_replyp
++ = NEW_ENV_VALUE
;
1693 opt_welldefined(char *ep
)
1695 if ((strcmp(ep
, "USER") == 0) ||
1696 (strcmp(ep
, "DISPLAY") == 0) ||
1697 (strcmp(ep
, "PRINTER") == 0) ||
1698 (strcmp(ep
, "SYSTEMTYPE") == 0) ||
1699 (strcmp(ep
, "JOB") == 0) ||
1700 (strcmp(ep
, "ACCT") == 0))
1705 env_opt_end(int emptyok
)
1709 len
= opt_replyp
- opt_reply
+ 2;
1710 if (emptyok
|| len
> 6) {
1711 *opt_replyp
++ = IAC
;
1713 if (NETROOM() > len
) {
1714 ring_supply_data(&netoring
, opt_reply
, len
);
1715 printsub('>', &opt_reply
[2], len
- 2);
1717 /*@*/ else printf("slc_end_reply: not enough room\n");
1721 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1732 unsigned char *sbp
= NULL
;
1734 int returnValue
= 0;
1738 while (TTYROOM() > 2) {
1741 ring_consumed(&netiring
, count
);
1745 sbp
= netiring
.consume
;
1746 scc
= ring_full_consecutive(&netiring
);
1748 /* No more data coming in */
1753 c
= *sbp
++ & 0xff, scc
--; count
++;
1756 c
= (*decrypt_input
)(c
);
1757 #endif /* ENCRYPTION */
1759 switch (telrcv_state
) {
1762 telrcv_state
= TS_DATA
;
1764 break; /* Ignore \0 after CR */
1766 else if ((c
== '\n') && my_want_state_is_dont(TELOPT_ECHO
) && !crmod
) {
1770 /* Else, fall through */
1774 telrcv_state
= TS_IAC
;
1777 # if defined(TN3270)
1781 c
= *sbp
++ & 0377, scc
--; count
++;
1784 c
= (*decrypt_input
)(c
);
1785 #endif /* ENCRYPTION */
1787 telrcv_state
= TS_IAC
;
1793 # endif /* defined(TN3270) */
1795 * The 'crmod' hack (see following) is needed
1796 * since we can't * set CRMOD on output only.
1797 * Machines like MULTICS like to send \r without
1798 * \n; since we must turn off CRMOD to get proper
1799 * input, the mapping is done here (sigh).
1801 if ((c
== '\r') && my_want_state_is_dont(TELOPT_BINARY
)) {
1806 c
= (*decrypt_input
)(c
);
1807 #endif /* ENCRYPTION */
1809 sbp
++, scc
--; count
++;
1812 } else if (my_want_state_is_dont(TELOPT_ECHO
) &&
1814 sbp
++, scc
--; count
++;
1819 (*decrypt_input
)(-1);
1820 #endif /* ENCRYPTION */
1828 telrcv_state
= TS_CR
;
1844 telrcv_state
= TS_WILL
;
1848 telrcv_state
= TS_WONT
;
1852 telrcv_state
= TS_DO
;
1856 telrcv_state
= TS_DONT
;
1861 * We may have missed an urgent notification,
1862 * so make sure we flush whatever is in the
1865 printoption("RCVD", IAC
, DM
);
1868 SYNCHing
= stilloob();
1874 telrcv_state
= TS_SB
;
1877 # if defined(TN3270)
1880 if (Ibackp
== Ifrontp
) {
1881 Ibackp
= Ifrontp
= Ibuf
;
1882 ISend
= 0; /* should have been! */
1884 Ibackp
+= DataFromNetwork(Ibackp
, Ifrontp
-Ibackp
, 1);
1888 printoption("RCVD", IAC
, EOR
);
1890 # endif /* defined(TN3270) */
1893 # if !defined(TN3270)
1895 # else /* !defined(TN3270) */
1901 # endif /* !defined(TN3270) */
1907 printoption("RCVD", IAC
, c
);
1910 telrcv_state
= TS_DATA
;
1914 printoption("RCVD", WILL
, c
);
1917 telrcv_state
= TS_DATA
;
1921 printoption("RCVD", WONT
, c
);
1924 telrcv_state
= TS_DATA
;
1928 printoption("RCVD", DO
, c
);
1931 if (c
== TELOPT_NAWS
) {
1933 } else if (c
== TELOPT_LFLOW
) {
1938 telrcv_state
= TS_DATA
;
1942 printoption("RCVD", DONT
, c
);
1945 setconnmode(0); /* set new tty mode (maybe) */
1947 telrcv_state
= TS_DATA
;
1952 telrcv_state
= TS_SE
;
1962 * This is an error. We only expect to get
1963 * "IAC IAC" or "IAC SE". Several things may
1964 * have happened. An IAC was not doubled, the
1965 * IAC SE was left off, or another option got
1966 * inserted into the suboption are all possibilities.
1967 * If we assume that the IAC was not doubled,
1968 * and really the IAC SE was left off, we could
1969 * get into an infinite loop here. So, instead,
1970 * we terminate the suboption, and process the
1971 * partial suboption if we can.
1978 printoption("In SUBOPTION processing, RCVD", IAC
, c
);
1979 suboption(); /* handle sub-option */
1981 telrcv_state
= TS_IAC
;
1985 telrcv_state
= TS_SB
;
1991 suboption(); /* handle sub-option */
1993 telrcv_state
= TS_DATA
;
1998 ring_consumed(&netiring
, count
);
1999 return returnValue
||count
;
2002 static int bol
= 1, local
= 0;
2010 command(0, "z\n", 2);
2021 int returnValue
= 0;
2022 unsigned char *tbp
= NULL
;
2026 while (NETROOM() > 2) {
2032 ring_consumed(&ttyiring
, count
);
2036 tbp
= ttyiring
.consume
;
2037 tcc
= ring_full_consecutive(&ttyiring
);
2042 c
= *tbp
++ & 0xff, sc
= strip(c
), tcc
--; count
++;
2043 if (rlogin
!= _POSIX_VDISABLE
) {
2052 if (sc
== '.' || c
== termEofChar
) {
2054 command(0, "close\n", 6);
2057 if (sc
== termSuspChar
) {
2059 command(0, "z\n", 2);
2063 command(0, (char *)tbp
, tcc
);
2077 if ((sc
== '\n') || (sc
== '\r'))
2079 } else if (sc
== escape
&& escape
!= _POSIX_VDISABLE
) {
2081 * Double escape is a pass through of a single escape character.
2083 if (tcc
&& strip(*tbp
) == escape
) {
2089 command(0, (char *)tbp
, tcc
);
2098 #ifdef KLUDGELINEMODE
2099 if (kludgelinemode
&& (globalmode
&MODE_EDIT
) && (sc
== echoc
)) {
2100 if (tcc
> 0 && strip(*tbp
) == echoc
) {
2101 tcc
--; tbp
++; count
++;
2103 dontlecho
= !dontlecho
;
2104 settimer(echotoggle
);
2111 if (sc
!= _POSIX_VDISABLE
&& MODE_LOCAL_CHARS(globalmode
)) {
2112 if (TerminalSpecialChars(sc
) == 0) {
2117 if (my_want_state_is_wont(TELOPT_BINARY
)) {
2121 * If we are in CRMOD mode (\r ==> \n)
2122 * on our local machine, then probably
2123 * a newline (unix) is CRLF (TELNET).
2125 if (MODE_LOCAL_CHARS(globalmode
)) {
2129 bol
= flushline
= 1;
2133 NET2ADD('\r', '\0');
2135 NET2ADD('\r', '\n');
2137 bol
= flushline
= 1;
2146 } else if (c
== IAC
) {
2153 ring_consumed(&ttyiring
, count
);
2154 return returnValue
||count
; /* Non-zero if we did anything */
2160 * Try to do something.
2162 * If we do something useful, return 1; else return 0.
2168 Scheduler(int block
) /* should we block in the select ? */
2170 /* One wants to be a bit careful about setting returnValue
2171 * to one, since a one implies we did some useful work,
2172 * and therefore probably won't be called to block next
2173 * time (TN3270 mode only).
2176 int netin
, netout
, netex
, ttyin
, ttyout
;
2178 /* Decide which rings should be processed */
2180 netout
= ring_full_count(&netoring
) &&
2182 (my_want_state_is_wont(TELOPT_LINEMODE
)
2183 #ifdef KLUDGELINEMODE
2184 && (!kludgelinemode
|| my_want_state_is_do(TELOPT_SGA
))
2187 my_want_state_is_will(TELOPT_BINARY
));
2188 ttyout
= ring_full_count(&ttyoring
);
2191 ttyin
= ring_empty_count(&ttyiring
) && (clienteof
== 0) && (shell_active
== 0);
2192 #else /* defined(TN3270) */
2193 ttyin
= ring_empty_count(&ttyiring
) && (clienteof
== 0);
2194 #endif /* defined(TN3270) */
2197 netin
= ring_empty_count(&netiring
);
2198 # else /* !defined(TN3270) */
2199 netin
= !ISend
&& ring_empty_count(&netiring
);
2200 # endif /* !defined(TN3270) */
2204 /* If we have seen a signal recently, reset things */
2208 (void) signal(SIGIO
, inputAvailable
);
2210 #endif /* defined(TN3270) */
2212 /* Call to system code to process rings */
2214 returnValue
= process_rings(netin
, netout
, netex
, ttyin
, ttyout
, !block
);
2216 /* Now, look at the input rings, looking for work to do. */
2218 if (ring_full_count(&ttyiring
)) {
2219 # if defined(TN3270)
2223 c
= DataFromTerminal(ttyiring
.consume
,
2224 ring_full_consecutive(&ttyiring
));
2227 ring_consumed(&ttyiring
, c
);
2230 # endif /* defined(TN3270) */
2231 returnValue
|= telsnd();
2232 # if defined(TN3270)
2234 # endif /* defined(TN3270) */
2237 if (ring_full_count(&netiring
)) {
2238 # if !defined(TN3270)
2239 returnValue
|= telrcv();
2240 # else /* !defined(TN3270) */
2241 returnValue
= Push3270();
2242 # endif /* !defined(TN3270) */
2248 * Select from tty and network...
2251 telnet(const char *user
)
2255 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2257 static char local_host
[MAXHOSTNAMELEN
+ 1] = { 0 };
2259 if (!local_host
[0]) {
2260 gethostname(local_host
, sizeof(local_host
));
2261 local_host
[sizeof(local_host
)-1] = 0;
2263 auth_encrypt_init(local_host
, hostname
, "TELNET", 0);
2264 auth_encrypt_user(user
);
2266 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
2267 # if !defined(TN3270)
2269 #ifdef AUTHENTICATION
2271 send_will(TELOPT_AUTHENTICATION
, 1);
2274 send_do(TELOPT_ENCRYPT
, 1);
2275 send_will(TELOPT_ENCRYPT
, 1);
2276 #endif /* ENCRYPTION */
2277 send_do(TELOPT_SGA
, 1);
2278 send_will(TELOPT_TTYPE
, 1);
2279 send_will(TELOPT_NAWS
, 1);
2280 send_will(TELOPT_TSPEED
, 1);
2281 send_will(TELOPT_LFLOW
, 1);
2282 send_will(TELOPT_LINEMODE
, 1);
2283 send_will(TELOPT_NEW_ENVIRON
, 1);
2284 send_do(TELOPT_STATUS
, 1);
2285 if (env_getvalue((unsigned char *)"DISPLAY"))
2286 send_will(TELOPT_XDISPLOC
, 1);
2288 tel_enter_binary(eight
);
2290 # endif /* !defined(TN3270) */
2292 # if !defined(TN3270)
2296 while ((schedValue
= Scheduler(0)) != 0) {
2297 if (schedValue
== -1) {
2303 if (Scheduler(1) == -1) {
2308 # else /* !defined(TN3270) */
2312 while (!In3270
&& !shell_active
) {
2313 if (Scheduler(1) == -1) {
2319 while ((schedValue
= Scheduler(0)) != 0) {
2320 if (schedValue
== -1) {
2325 /* If there is data waiting to go out to terminal, don't
2326 * schedule any more data for the terminal.
2328 if (ring_full_count(&ttyoring
)) {
2332 if (shell_continue() == 0) {
2335 } else if (In3270
) {
2336 schedValue
= DoTerminalOutput();
2339 if (schedValue
&& (shell_active
== 0)) {
2340 if (Scheduler(1) == -1) {
2346 # endif /* !defined(TN3270) */
2349 #if 0 /* XXX - this not being in is a bug */
2353 * Return the address of the next "item" in the TELNET data
2354 * stream. This will be the address of the next character if
2355 * the current address is a user data character, or it will
2356 * be the address of the character following the TELNET command
2357 * if the current address is a TELNET IAC ("I Am a Command")
2362 nextitem(char *current
)
2364 if ((*current
&0xff) != IAC
) {
2367 switch (*(current
+1)&0xff) {
2373 case SB
: /* loop forever looking for the SE */
2375 char *look
= current
+2;
2378 if ((*look
++&0xff) == IAC
) {
2379 if ((*look
++&0xff) == SE
) {
2394 * We are about to do a TELNET SYNCH operation. Clear
2395 * the path to the network.
2397 * Things are a bit tricky since we may have sent the first
2398 * byte or so of a previous TELNET command into the network.
2399 * So, we have to scan the network buffer from the beginning
2400 * until we are up to where we want to be.
2402 * A side effect of what we do, just to keep things
2403 * simple, is to clear the urgent data pointer. The principal
2404 * caller should be setting the urgent data pointer AFTER calling
2412 char *thisitem
, *next
;
2414 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
2415 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
2419 while ((next
= nextitem(thisitem
)) <= netobuf
.send
) {
2423 /* Now, thisitem is first before/at boundary. */
2425 good
= netobuf
; /* where the good bytes go */
2427 while (netoring
.add
> thisitem
) {
2428 if (wewant(thisitem
)) {
2433 next
= nextitem(next
);
2434 } while (wewant(next
) && (nfrontp
> next
));
2435 length
= next
-thisitem
;
2436 memmove(good
, thisitem
, length
);
2440 thisitem
= nextitem(thisitem
);
2448 * These routines add various telnet commands to the data stream.
2458 (void) ttyflush(1); /* Flush/drop output */
2459 /* do printoption AFTER flush, otherwise the output gets tossed... */
2460 printoption("SENT", DO
, TELOPT_TM
);
2467 printoption("SENT", IAC
, AO
);
2478 printoption("SENT", IAC
, EL
);
2485 printoption("SENT", IAC
, EC
);
2492 netclear(); /* clear the path to the network */
2496 printoption("SENT", IAC
, DM
);
2500 int want_status_response
= 0;
2505 unsigned char tmp
[16];
2508 if (my_want_state_is_dont(TELOPT_STATUS
)) {
2509 printf("Remote side does not support STATUS option\n");
2516 *cp
++ = TELOPT_STATUS
;
2517 *cp
++ = TELQUAL_SEND
;
2520 if (NETROOM() >= cp
- tmp
) {
2521 ring_supply_data(&netoring
, tmp
, cp
-tmp
);
2522 printsub('>', tmp
+2, cp
- tmp
- 2);
2524 ++want_status_response
;
2532 printoption("SENT", IAC
, IP
);
2545 NET2ADD(IAC
, BREAK
);
2546 printoption("SENT", IAC
, BREAK
);
2559 NET2ADD(IAC
, ABORT
);
2560 printoption("SENT", IAC
, ABORT
);
2574 printoption("SENT", IAC
, SUSP
);
2588 printoption("SENT", IAC
, xEOF
);
2595 printoption("SENT", IAC
, AYT
);
2599 * Send a window size update to the remote system.
2606 unsigned char tmp
[16];
2609 if (my_state_is_wont(TELOPT_NAWS
))
2612 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2613 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2615 if (TerminalWindowSize(&rows
, &cols
) == 0) { /* Failed */
2623 *cp
++ = TELOPT_NAWS
;
2628 if (NETROOM() >= cp
- tmp
) {
2629 ring_supply_data(&netoring
, tmp
, cp
-tmp
);
2630 printsub('>', tmp
+2, cp
- tmp
- 2);
2635 tel_enter_binary(int rw
)
2638 send_do(TELOPT_BINARY
, 1);
2640 send_will(TELOPT_BINARY
, 1);
2644 tel_leave_binary(int rw
)
2647 send_dont(TELOPT_BINARY
, 1);
2649 send_wont(TELOPT_BINARY
, 1);