2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * Copyright (c) 1988, 1990, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid
[] = "@(#)telnet.c 8.1 (Berkeley) 6/6/93";
42 #include <sys/types.h>
47 * By the way, we need to include curses.h before telnet.h since,
48 * among other things, telnet.h #defines 'DO', which is a variable
49 * declared in curses.h.
52 #include <arpa/telnet.h>
66 #define strip(x) ((x)&0x7f)
68 /* Buffer for sub-options */
69 static unsigned char subbuffer
[SUBBUFSIZE
];
70 static unsigned char *subpointer
;
71 static unsigned char *subend
;
73 #define SB_CLEAR() subpointer = subbuffer;
74 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
75 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof (subbuffer))) { \
76 *subpointer++ = (c); \
79 #define SB_GET() ((*subpointer++)&0xff)
80 #define SB_PEEK() ((*subpointer)&0xff)
81 #define SB_EOF() (subpointer >= subend)
82 #define SB_LEN() (subend - subpointer)
84 char options
[SUBBUFSIZE
]; /* The combined options */
85 char do_dont_resp
[SUBBUFSIZE
];
86 char will_wont_resp
[SUBBUFSIZE
];
89 int autologin
= 0; /* Autologin anyone? */
93 static int ISend
; /* trying to send network data in */
96 int netdata
; /* Print out network data flow */
97 int crlf
; /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
99 int SYNCHing
; /* we are in TELNET SYNCH mode */
100 int flushout
; /* flush output */
101 int autoflush
= 0; /* flush output when interrupting? */
102 int autosynch
; /* send interrupt characters with SYNCH? */
103 int localflow
; /* we handle flow control locally */
104 int restartany
; /* if flow control enabled, restart on any character */
105 int localchars
; /* we recognize interrupt/quit */
106 int donelclchars
; /* the user has set "localchars" */
107 int donebinarytoggle
; /* the user has put us in binary */
108 int dontlecho
; /* do we suppress local echoing right now? */
109 int eof_pending
= 0; /* we received a genuine EOF on input, send IAC-EOF */
112 /* spin while waiting for authentication */
113 boolean_t scheduler_lockout_tty
= B_FALSE
;
114 int encrypt_flag
= 0;
115 int forwardable_flag
= 0;
116 int forward_flag
= 0;
117 boolean_t wantencryption
= B_FALSE
;
123 boolean_t escape_valid
= B_TRUE
;
124 #ifdef KLUDGELINEMODE
129 * Telnet receiver states for fsm
138 #define TS_SB 7 /* sub-option collection */
139 #define TS_SE 8 /* looking for sub-option end */
141 static int telrcv_state
;
143 static unsigned char telopt_environ
= TELOPT_NEW_ENVIRON
;
145 #define telopt_environ TELOPT_NEW_ENVIRON
148 jmp_buf toplevel
= { 0 };
151 static int flushline
;
154 int reqd_linemode
= 0; /* Set if either new or old line mode in */
155 /* effect since before initial negotiations */
157 #ifdef KLUDGELINEMODE
158 int kludgelinemode
= 1;
162 * The following are some clocks used to decide how to interpret
163 * the relationship between various variables.
169 Modelist modelist
[] = {
170 { "telnet command mode", COMMAND_LINE
},
171 { "character-at-a-time mode", 0 },
172 { "character-at-a-time mode (local echo)", LOCAL_ECHO
|LOCAL_CHARS
},
173 { "line-by-line mode (remote echo)", LINE
| LOCAL_CHARS
},
174 { "line-by-line mode", LINE
| LOCAL_ECHO
| LOCAL_CHARS
},
175 { "line-by-line mode (local echoing suppressed)", LINE
| LOCAL_CHARS
},
180 static void willoption(int);
181 static void wontoption(int);
182 static void lm_will(unsigned char *, int);
183 static void lm_wont(unsigned char *, int);
184 static void lm_do(unsigned char *, int);
185 static void lm_dont(unsigned char *, int);
186 static void slc_init(void);
187 static void slc_import(int);
188 static void slc_export(void);
189 static void slc_start_reply(size_t);
190 static void slc_add_reply(unsigned char, unsigned char, cc_t
);
191 static void slc_end_reply(void);
192 static void slc(unsigned char *, int);
193 static int slc_update(void);
194 static void env_opt(unsigned char *, int);
195 static void env_opt_start(void);
196 static void sendeof(void);
197 static int is_unique(register char *, register char **, register char **);
200 * Initialize telnet environment.
212 connected
= ISend
= localflow
= donebinarytoggle
= 0;
217 /* Don't change NetTrace */
219 escape
= CONTROL(']');
220 rlogin
= _POSIX_VDISABLE
;
221 #ifdef KLUDGELINEMODE
222 echoc
= CONTROL('E');
226 telrcv_state
= TS_DATA
;
241 char buffer
[100]; /* where things go */
250 ring
= va_arg(ap
, Ring
*);
251 format
= va_arg(ap
, char *);
254 while ((i
= *format
++) != 0) {
259 *ptr
++ = va_arg(ap
, int);
262 string
= va_arg(ap
, char *);
263 ring_supply_data(ring
, buffer
, ptr
-buffer
);
264 ring_supply_data(ring
, string
, strlen(string
));
268 ExitString("printring: trailing %%.\n",
272 ExitString("printring: unknown format "
273 "character.\n", EXIT_FAILURE
);
280 ring_supply_data(ring
, buffer
, ptr
-buffer
);
285 * These routines are in charge of sending option negotiations
288 * The basic idea is that we send the negotiation if either side
289 * is in disagreement as to what the current state should be.
294 register int c
, init
;
297 if (((do_dont_resp
[c
] == 0) && my_state_is_do(c
)) ||
298 my_want_state_is_do(c
))
300 set_my_want_state_do(c
);
305 printoption("SENT", DO
, c
);
310 register int c
, init
;
313 if (((do_dont_resp
[c
] == 0) && my_state_is_dont(c
)) ||
314 my_want_state_is_dont(c
))
316 set_my_want_state_dont(c
);
321 printoption("SENT", DONT
, c
);
326 register int c
, init
;
329 if (((will_wont_resp
[c
] == 0) && my_state_is_will(c
)) ||
330 my_want_state_is_will(c
))
332 set_my_want_state_will(c
);
337 printoption("SENT", WILL
, c
);
342 register int c
, init
;
345 if (((will_wont_resp
[c
] == 0) && my_state_is_wont(c
)) ||
346 my_want_state_is_wont(c
))
348 set_my_want_state_wont(c
);
353 printoption("SENT", WONT
, c
);
361 int new_state_ok
= 0;
363 if (do_dont_resp
[option
]) {
364 --do_dont_resp
[option
];
365 if (do_dont_resp
[option
] && my_state_is_do(option
))
366 --do_dont_resp
[option
];
369 if ((do_dont_resp
[option
] == 0) && my_want_state_is_dont(option
)) {
375 if (reqd_linemode
&& my_state_is_dont(option
)) {
380 settimer(modenegotiated
);
383 case TELOPT_AUTHENTICATION
:
393 * Special case for TM. If we get back a WILL,
394 * pretend we got back a WONT.
396 set_my_want_state_dont(option
);
397 set_my_state_dont(option
);
398 return; /* Never reply to TM will's/wont's */
400 case TELOPT_LINEMODE
:
406 set_my_want_state_do(option
);
408 setconnmode(0); /* possibly set new tty mode */
410 do_dont_resp
[option
]++;
411 send_dont(option
, 0);
414 set_my_state_do(option
);
415 if (option
== TELOPT_ENCRYPT
)
416 encrypt_send_support();
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
);
472 int new_state_ok
= 0;
474 if (will_wont_resp
[option
]) {
475 --will_wont_resp
[option
];
476 if (will_wont_resp
[option
] && my_state_is_will(option
))
477 --will_wont_resp
[option
];
480 if (will_wont_resp
[option
] == 0) {
481 if (my_want_state_is_wont(option
)) {
487 * Special case for TM. We send a WILL,
488 * but pretend we sent WONT.
490 send_will(option
, 0);
491 set_my_want_state_wont(TELOPT_TM
);
492 set_my_state_wont(TELOPT_TM
);
495 case TELOPT_BINARY
: /* binary mode */
496 case TELOPT_NAWS
: /* window size */
497 case TELOPT_TSPEED
: /* terminal speed */
498 case TELOPT_LFLOW
: /* local flow control */
499 case TELOPT_TTYPE
: /* terminal type option */
500 case TELOPT_SGA
: /* no big deal */
501 case TELOPT_ENCRYPT
: /* encryption variable option */
505 case TELOPT_NEW_ENVIRON
:
506 /* New environment variable option */
508 if (my_state_is_will(TELOPT_OLD_ENVIRON
))
509 /* turn off the old */
510 send_wont(TELOPT_OLD_ENVIRON
, 1);
512 case TELOPT_OLD_ENVIRON
:
513 /* Old environment variable option */
514 if (my_state_is_will(TELOPT_NEW_ENVIRON
))
515 /* Don't enable if new one is in use! */
518 telopt_environ
= option
;
523 case TELOPT_AUTHENTICATION
:
528 case TELOPT_XDISPLOC
: /* X Display location */
529 if (env_getvalue((unsigned char *)"DISPLAY"))
533 case TELOPT_LINEMODE
:
534 #ifdef KLUDGELINEMODE
536 send_do(TELOPT_SGA
, 1);
538 set_my_want_state_will(TELOPT_LINEMODE
);
539 send_will(option
, 0);
540 set_my_state_will(TELOPT_LINEMODE
);
544 case TELOPT_ECHO
: /* We're never going to echo... */
550 set_my_want_state_will(option
);
551 send_will(option
, 0);
552 setconnmode(0); /* Set new tty mode */
554 will_wont_resp
[option
]++;
555 send_wont(option
, 0);
559 * Handle options that need more things done after the
560 * other side has acknowledged the option.
563 case TELOPT_LINEMODE
:
564 #ifdef KLUDGELINEMODE
566 send_do(TELOPT_SGA
, 1);
568 set_my_state_will(option
);
570 send_do(TELOPT_SGA
, 0);
575 set_my_state_will(option
);
583 if (will_wont_resp
[option
]) {
584 --will_wont_resp
[option
];
585 if (will_wont_resp
[option
] && my_state_is_wont(option
))
586 --will_wont_resp
[option
];
589 if ((will_wont_resp
[option
] == 0) && my_want_state_is_will(option
)) {
591 case TELOPT_LINEMODE
:
592 linemode
= 0; /* put us back to the default state */
595 case TELOPT_NEW_ENVIRON
:
597 * The new environ option wasn't recognized, try
600 send_will(TELOPT_OLD_ENVIRON
, 1);
601 telopt_environ
= TELOPT_OLD_ENVIRON
;
605 /* we always accept a DONT */
606 set_my_want_state_wont(option
);
607 if (my_state_is_will(option
))
608 send_wont(option
, 0);
609 setconnmode(0); /* Set new tty mode */
611 set_my_state_wont(option
);
615 * Given a buffer returned by tgetent(), this routine will turn
616 * the pipe seperated list of names in the buffer into an array
617 * of pointers to null terminated names. We toss out any bad,
618 * duplicate, or verbose names (names with spaces).
621 static char *name_unknown
= "UNKNOWN";
622 static char *unknown
[] = { 0, 0 };
629 register char c
, *cp
, **argvp
, *cp2
, **argv
, **avt
;
632 if (strlen(name
) > 40u) {
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
= malloc((n
+3)*sizeof (char *));
656 * Fill up the array of pointers to names.
661 for (cp
= cp2
= buf
; (c
= *cp
) != '\0'; 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
))
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(name
, as
, ae
)
737 register char *name
, **as
, **ae
;
742 n
= strlen(name
) + 1;
743 for (ap
= as
; ap
< ae
; ap
++)
744 if (strncasecmp(*ap
, name
, n
) == 0)
749 #define termbuf ttytype
750 extern char ttytype
[];
752 int resettermname
= 1;
758 static char **tnamep
= 0;
764 if (tnamep
&& tnamep
!= unknown
)
766 tname
= (char *)env_getvalue((unsigned char *)"TERM");
767 if ((tname
!= NULL
) && (setupterm(tname
, 1, &err
) == 0)) {
768 tnamep
= mklist(termbuf
, tname
);
770 if (tname
&& (strlen(tname
) <= 40u)) {
774 unknown
[0] = name_unknown
;
786 * Look at the sub-option buffer, and try to be helpful to the other
789 * Currently we recognize:
791 * Terminal type, send request.
792 * Terminal speed (send request).
793 * Local flow control (is request).
800 unsigned char subchar
;
802 printsub('<', subbuffer
, SB_LEN()+2);
803 switch (subchar
= SB_GET()) {
805 if (my_want_state_is_wont(TELOPT_TTYPE
))
807 if (SB_EOF() || SB_GET() != TELQUAL_SEND
) {
811 unsigned char temp
[50];
814 name
= gettermname();
815 len
= strlen(name
) + 4 + 2;
816 bytes
= snprintf((char *)temp
, sizeof (temp
),
817 "%c%c%c%c%s%c%c", IAC
, SB
,
818 TELOPT_TTYPE
, TELQUAL_IS
, name
, IAC
, SE
);
819 if ((len
< NETROOM()) && (bytes
< sizeof (temp
))) {
820 ring_supply_data(&netoring
, temp
, len
);
821 printsub('>', &temp
[2], len
-2);
823 ExitString("No room in buffer for "
824 "terminal type.\n", EXIT_FAILURE
);
830 if (my_want_state_is_wont(TELOPT_TSPEED
))
834 if (SB_GET() == TELQUAL_SEND
) {
836 unsigned char temp
[50];
839 TerminalSpeeds(&ispeed
, &ospeed
);
841 bytes
= snprintf((char *)temp
, sizeof (temp
),
842 "%c%c%c%c%d,%d%c%c", IAC
, SB
,
843 TELOPT_TSPEED
, TELQUAL_IS
, ospeed
, ispeed
, IAC
, SE
);
844 len
= strlen((char *)temp
+4) + 4; /* temp[3] is 0 ... */
846 if ((len
< NETROOM()) && (bytes
< sizeof (temp
))) {
847 ring_supply_data(&netoring
, temp
, len
);
848 printsub('>', temp
+2, len
- 2);
852 "telnet: not enough room in buffer "
853 "for terminal speed option reply\n");
857 if (my_want_state_is_wont(TELOPT_LFLOW
))
862 case LFLOW_RESTART_ANY
:
865 case LFLOW_RESTART_XON
:
881 case TELOPT_LINEMODE
:
882 if (my_want_state_is_wont(TELOPT_LINEMODE
))
888 lm_will(subpointer
, SB_LEN());
891 lm_wont(subpointer
, SB_LEN());
894 lm_do(subpointer
, SB_LEN());
897 lm_dont(subpointer
, SB_LEN());
900 slc(subpointer
, SB_LEN());
903 lm_mode(subpointer
, SB_LEN(), 0);
911 case TELOPT_OLD_ENVIRON
:
913 case TELOPT_NEW_ENVIRON
:
919 if (my_want_state_is_dont(subchar
))
923 if (my_want_state_is_wont(subchar
)) {
930 env_opt(subpointer
, SB_LEN());
933 case TELOPT_XDISPLOC
:
934 if (my_want_state_is_wont(TELOPT_XDISPLOC
))
938 if (SB_GET() == TELQUAL_SEND
) {
939 unsigned char temp
[50], *dp
;
942 if ((dp
= env_getvalue((unsigned char *)"DISPLAY")) ==
945 * Something happened, we no longer have a
946 * DISPLAY variable. So, turn off the option.
948 send_wont(TELOPT_XDISPLOC
, 1);
951 bytes
= snprintf((char *)temp
, sizeof (temp
),
952 "%c%c%c%c%s%c%c", IAC
, SB
,
953 TELOPT_XDISPLOC
, TELQUAL_IS
, dp
, IAC
, SE
);
954 len
= strlen((char *)temp
+4) + 4; /* temp[3] is 0 ... */
956 if ((len
< NETROOM()) && (bytes
< sizeof (temp
))) {
957 ring_supply_data(&netoring
, temp
, len
);
958 printsub('>', temp
+2, len
- 2);
962 "telnet: not enough room in buffer"
963 " for display location option reply\n");
967 case TELOPT_AUTHENTICATION
: {
974 if (my_want_state_is_wont(TELOPT_AUTHENTICATION
))
976 auth_send(subpointer
, SB_LEN());
979 if (my_want_state_is_wont(TELOPT_AUTHENTICATION
))
981 auth_reply(subpointer
, SB_LEN());
992 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
994 encrypt_start(subpointer
, SB_LEN());
997 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1001 case ENCRYPT_SUPPORT
:
1002 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1004 encrypt_support(subpointer
, SB_LEN());
1006 case ENCRYPT_REQSTART
:
1007 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1009 encrypt_request_start(subpointer
, SB_LEN());
1011 case ENCRYPT_REQEND
:
1012 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1015 * We can always send an REQEND so that we cannot
1016 * get stuck encrypting. We should only get this
1017 * if we have been able to get in the correct mode
1020 encrypt_request_end();
1023 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1025 encrypt_is(subpointer
, SB_LEN());
1028 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1030 encrypt_reply(subpointer
, SB_LEN());
1032 case ENCRYPT_ENC_KEYID
:
1033 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1035 encrypt_enc_keyid(subpointer
, SB_LEN());
1037 case ENCRYPT_DEC_KEYID
:
1038 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1040 encrypt_dec_keyid(subpointer
, SB_LEN());
1051 static unsigned char str_lm
[] = { IAC
, SB
, TELOPT_LINEMODE
, 0, 0, IAC
, SE
};
1059 /* Should not happen... */
1061 "telnet: command missing from linemode WILL request\n");
1065 case LM_FORWARDMASK
: /* We shouldn't ever get this... */
1069 if (NETROOM() > sizeof (str_lm
)) {
1070 ring_supply_data(&netoring
, str_lm
, sizeof (str_lm
));
1071 printsub('>', &str_lm
[2], sizeof (str_lm
)-2);
1074 (void) printf("telnet: not enough room in buffer for"
1075 "reply to linemode WILL request\n");
1086 /* Should not happen... */
1088 "telnet: command missing from linemode WONT request\n");
1092 case LM_FORWARDMASK
: /* We shouldn't ever get this... */
1094 /* We are always DONT, so don't respond */
1105 /* Should not happen... */
1107 "telnet: command missing from linemode DO request\n");
1111 case LM_FORWARDMASK
:
1115 if (NETROOM() > sizeof (str_lm
)) {
1116 ring_supply_data(&netoring
, str_lm
, sizeof (str_lm
));
1117 printsub('>', &str_lm
[2], sizeof (str_lm
)-2);
1120 (void) printf("telnet: not enough room in buffer for"
1121 "reply to linemode DO request\n");
1132 /* Should not happen... */
1134 "telnet: command missing from linemode DONT request\n");
1138 case LM_FORWARDMASK
:
1140 /* we are always WONT, so don't respond */
1145 static unsigned char str_lm_mode
[] = {
1146 IAC
, SB
, TELOPT_LINEMODE
, LM_MODE
, 0, IAC
, SE
1150 lm_mode(cmd
, len
, init
)
1156 if ((linemode
&MODE_MASK
&~MODE_ACK
) == *cmd
)
1158 linemode
= *cmd
&(MODE_MASK
&~MODE_ACK
);
1159 str_lm_mode
[4] = linemode
;
1161 str_lm_mode
[4] |= MODE_ACK
;
1162 if (NETROOM() > sizeof (str_lm_mode
)) {
1163 ring_supply_data(&netoring
, str_lm_mode
, sizeof (str_lm_mode
));
1164 printsub('>', &str_lm_mode
[2], sizeof (str_lm_mode
)-2);
1167 (void) printf("telnet: not enough room in buffer for"
1168 "reply to linemode request\n");
1169 setconnmode(0); /* set changed mode */
1176 * Handle special character suboption of LINEMODE.
1182 char flags
; /* Current flags & level */
1183 char mylevel
; /* Maximum level & flags */
1186 #define SLC_IMPORT 0
1187 #define SLC_EXPORT 1
1188 #define SLC_RVALUE 2
1189 static int slc_mode
= SLC_EXPORT
;
1194 register struct spc
*spcp
;
1197 for (spcp
= spc_data
; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1200 spcp
->flags
= spcp
->mylevel
= SLC_NOSUPPORT
;
1203 #define initfunc(func, flags) { \
1204 spcp = &spc_data[func]; \
1205 if (spcp->valp = tcval(func)) { \
1206 spcp->val = *spcp->valp; \
1207 spcp->mylevel = SLC_VARIABLE|(flags);\
1210 spcp->mylevel = SLC_DEFAULT; \
1214 initfunc(SLC_SYNCH
, 0);
1216 initfunc(SLC_AO
, 0);
1217 initfunc(SLC_AYT
, 0);
1219 initfunc(SLC_ABORT
, SLC_FLUSHIN
|SLC_FLUSHOUT
);
1220 initfunc(SLC_EOF
, 0);
1221 initfunc(SLC_SUSP
, SLC_FLUSHIN
);
1222 initfunc(SLC_EC
, 0);
1223 initfunc(SLC_EL
, 0);
1224 initfunc(SLC_EW
, 0);
1225 initfunc(SLC_RP
, 0);
1226 initfunc(SLC_LNEXT
, 0);
1227 initfunc(SLC_XON
, 0);
1228 initfunc(SLC_XOFF
, 0);
1229 initfunc(SLC_FORW1
, 0);
1231 initfunc(SLC_FORW2
, 0);
1235 initfunc(SLC_IP
, SLC_FLUSHIN
|SLC_FLUSHOUT
);
1238 if (slc_mode
== SLC_EXPORT
)
1248 (void) printf("Special characters are %s values\n",
1249 slc_mode
== SLC_IMPORT
? "remote default" :
1250 slc_mode
== SLC_EXPORT
? "local" :
1257 slc_mode
= SLC_EXPORT
;
1258 if (my_state_is_will(TELOPT_LINEMODE
))
1263 slc_mode_import(def
)
1266 slc_mode
= def
? SLC_IMPORT
: SLC_RVALUE
;
1267 if (my_state_is_will(TELOPT_LINEMODE
))
1271 static unsigned char slc_import_val
[] = {
1272 IAC
, SB
, TELOPT_LINEMODE
, LM_SLC
, 0, SLC_VARIABLE
, 0, IAC
, SE
1274 static unsigned char slc_import_def
[] = {
1275 IAC
, SB
, TELOPT_LINEMODE
, LM_SLC
, 0, SLC_DEFAULT
, 0, IAC
, SE
1282 if (NETROOM() > sizeof (slc_import_val
)) {
1284 ring_supply_data(&netoring
, slc_import_def
,
1285 sizeof (slc_import_def
));
1286 printsub('>', &slc_import_def
[2],
1287 sizeof (slc_import_def
)-2);
1289 ring_supply_data(&netoring
, slc_import_val
,
1290 sizeof (slc_import_val
));
1291 printsub('>', &slc_import_val
[2],
1292 sizeof (slc_import_val
)-2);
1297 "telnet: not enough room in buffer for slc import"
1301 static uchar_t
*slc_reply
= NULL
;
1302 static uchar_t
*slc_replyp
= NULL
;
1304 * The SLC reply consists of: IAC, SB, TELOPT_LINEMODE, LM_SLC,
1305 * SLC triplets[], IAC, SE. i.e. it has a 'wrapper' of 6 control characters.
1307 #define SLC_WRAPPER_SIZE 6
1312 register struct spc
*spcp
;
1314 TerminalDefaultChars();
1316 slc_start_reply(NSLC
* 3); /* 3 bytes needed per triplet */
1317 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1318 if (spcp
->mylevel
!= SLC_NOSUPPORT
) {
1319 if (spcp
->val
== (cc_t
)(_POSIX_VDISABLE
))
1320 spcp
->flags
= SLC_NOSUPPORT
;
1322 spcp
->flags
= spcp
->mylevel
;
1324 spcp
->val
= *spcp
->valp
;
1325 slc_add_reply(spcp
- spc_data
, spcp
->flags
, spcp
->val
);
1329 (void) slc_update();
1330 setconnmode(1); /* Make sure the character values are set */
1335 register unsigned char *cp
;
1338 register struct spc
*spcp
;
1339 register int func
, level
;
1341 slc_start_reply(len
);
1343 for (; len
>= 3; len
-= 3, cp
+= 3) {
1345 func
= cp
[SLC_FUNC
];
1349 * Client side: always ignore 0 function.
1354 if ((cp
[SLC_FLAGS
] & SLC_LEVELBITS
) != SLC_NOSUPPORT
)
1355 slc_add_reply(func
, SLC_NOSUPPORT
, 0);
1359 spcp
= &spc_data
[func
];
1361 level
= cp
[SLC_FLAGS
]&(SLC_LEVELBITS
|SLC_ACK
);
1363 if ((cp
[SLC_VALUE
] == (unsigned char)spcp
->val
) &&
1364 ((level
&SLC_LEVELBITS
) == (spcp
->flags
&SLC_LEVELBITS
))) {
1368 if (level
== (SLC_DEFAULT
|SLC_ACK
)) {
1370 * This is an error condition, the SLC_ACK
1371 * bit should never be set for the SLC_DEFAULT
1372 * level. Our best guess to recover is to
1373 * ignore the SLC_ACK bit.
1375 cp
[SLC_FLAGS
] &= ~SLC_ACK
;
1378 if (level
== ((spcp
->flags
&SLC_LEVELBITS
)|SLC_ACK
)) {
1379 spcp
->val
= (cc_t
)cp
[SLC_VALUE
];
1380 spcp
->flags
= cp
[SLC_FLAGS
]; /* include SLC_ACK */
1386 if (level
<= (spcp
->mylevel
&SLC_LEVELBITS
)) {
1387 spcp
->flags
= cp
[SLC_FLAGS
]|SLC_ACK
;
1388 spcp
->val
= (cc_t
)cp
[SLC_VALUE
];
1390 if (level
== SLC_DEFAULT
) {
1391 if ((spcp
->mylevel
&SLC_LEVELBITS
) != SLC_DEFAULT
)
1392 spcp
->flags
= spcp
->mylevel
;
1394 spcp
->flags
= SLC_NOSUPPORT
;
1396 slc_add_reply(func
, spcp
->flags
, spcp
->val
);
1400 setconnmode(1); /* set the new character values */
1406 register struct spc
*spcp
;
1408 slc_start_reply(NSLC
* 3); /* 3 bytes needed per triplet */
1409 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1410 if (spcp
->valp
&& spcp
->val
!= *spcp
->valp
) {
1411 spcp
->val
= *spcp
->valp
;
1412 if (spcp
->val
== (cc_t
)(_POSIX_VDISABLE
))
1413 spcp
->flags
= SLC_NOSUPPORT
;
1415 spcp
->flags
= spcp
->mylevel
;
1416 slc_add_reply(spcp
- spc_data
, spcp
->flags
, spcp
->val
);
1424 slc_start_reply(size_t len
)
1427 * SLC triplets may contain escaped characters, allow for
1428 * worst case by allocating 2 bytes for every character.
1430 slc_reply
= realloc(slc_reply
, (len
* 2) + SLC_WRAPPER_SIZE
);
1431 if (slc_reply
== NULL
) {
1432 fprintf(stderr
, "telnet: error allocating SLC reply memory\n");
1435 slc_replyp
= slc_reply
;
1436 *slc_replyp
++ = IAC
;
1438 *slc_replyp
++ = TELOPT_LINEMODE
;
1439 *slc_replyp
++ = LM_SLC
;
1443 slc_add_reply(unsigned char func
, unsigned char flags
, cc_t value
)
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 *slc_replyp
++ = IAC
;
1460 len
= slc_replyp
- slc_reply
;
1461 if (len
<= SLC_WRAPPER_SIZE
)
1463 if (NETROOM() > len
) {
1464 ring_supply_data(&netoring
, slc_reply
, slc_replyp
- slc_reply
);
1465 printsub('>', &slc_reply
[2], slc_replyp
- slc_reply
- 2);
1468 (void) printf("telnet: not enough room in buffer for slc end "
1475 register struct spc
*spcp
;
1476 int need_update
= 0;
1478 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1479 if (!(spcp
->flags
&SLC_ACK
))
1481 spcp
->flags
&= ~SLC_ACK
;
1482 if (spcp
->valp
&& (*spcp
->valp
!= spcp
->val
)) {
1483 *spcp
->valp
= spcp
->val
;
1487 return (need_update
);
1493 * Earlier version of telnet/telnetd from the BSD code had
1494 * the definitions of VALUE and VAR reversed. To ensure
1495 * maximum interoperability, we assume that the server is
1496 * an older BSD server, until proven otherwise. The newer
1497 * BSD servers should be able to handle either definition,
1498 * so it is better to use the wrong values if we don't
1499 * know what type of server it is.
1502 int old_env_var
= OLD_ENV_VAR
;
1503 int old_env_value
= OLD_ENV_VALUE
;
1505 #define old_env_var OLD_ENV_VAR
1506 #define old_env_value OLD_ENV_VALUE
1512 register unsigned char *buf
;
1515 register unsigned char *ep
= 0, *epc
= 0;
1518 switch (buf
[0]&0xff) {
1523 } else for (i
= 1; i
< len
; i
++) {
1524 switch (buf
[i
]&0xff) {
1528 if (telopt_environ
== TELOPT_OLD_ENVIRON
&&
1530 /* Server has the same definitions */
1531 old_env_var
= OLD_ENV_VAR
;
1532 old_env_value
= OLD_ENV_VALUE
;
1538 * Although OLD_ENV_VALUE is not legal, we will
1539 * still recognize it, just in case it is an
1540 * old server that has VAR & VALUE mixed up...
1551 ep
= epc
= &buf
[i
+1];
1571 /* Ignore for now. We shouldn't get it anyway. */
1579 static unsigned char *opt_reply
;
1580 static unsigned char *opt_replyp
;
1581 static unsigned char *opt_replyend
;
1582 #define OPT_REPLY_INITIAL_SIZE 256
1584 * The opt reply consists of: IAC, SB, telopt_environ, TELQUAL_IS,
1585 * value, IAC, SE. i.e. it has a 'wrapper' of 6 control characters.
1587 #define OPT_WRAPPER_SIZE 6
1592 opt_reply
= realloc(opt_reply
, OPT_REPLY_INITIAL_SIZE
);
1593 if (opt_reply
== NULL
) {
1595 "telnet: error allocating environment option memory\n");
1596 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1599 opt_replyp
= opt_reply
;
1600 opt_replyend
= opt_reply
+ OPT_REPLY_INITIAL_SIZE
;
1601 *opt_replyp
++ = IAC
;
1603 *opt_replyp
++ = telopt_environ
;
1604 *opt_replyp
++ = TELQUAL_IS
;
1608 env_opt_start_info()
1612 opt_replyp
[-1] = TELQUAL_INFO
;
1617 register unsigned char *ep
;
1619 register unsigned char *vp
, c
;
1623 if (opt_reply
== NULL
) /* XXX */
1626 if (ep
== NULL
|| *ep
== '\0') {
1627 /* Send user defined variables first. */
1628 (void) env_default(1, 0);
1629 while (ep
= env_default(0, 0))
1632 /* Now add the list of well know variables. */
1633 (void) env_default(1, 1);
1634 while (ep
= env_default(0, 1))
1638 vp
= env_getvalue(ep
);
1641 * Calculate space required for opt_reply and allocate more if required.
1642 * Assume worst case that every character is escaped, so needs 2 bytes.
1644 opt_reply_used
= opt_replyp
- opt_reply
; /* existing contents */
1645 opt_reply_size
= opt_reply_used
+ OPT_WRAPPER_SIZE
+
1646 (2 * (strlen((char *)ep
))) +
1647 (vp
== NULL
? 0 : (2 * strlen((char *)vp
)));
1649 if (opt_reply_size
> (opt_replyend
- opt_reply
)) {
1650 opt_reply
= realloc(opt_reply
, opt_reply_size
);
1651 if (opt_reply
== NULL
) {
1653 "telnet: can't allocate environment option "
1655 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1658 opt_replyp
= opt_reply
+ opt_reply_used
;
1659 opt_replyend
= opt_reply
+ opt_reply_size
;
1662 if (opt_welldefined((char *)ep
))
1664 if (telopt_environ
== TELOPT_OLD_ENVIRON
)
1665 *opt_replyp
++ = old_env_var
;
1668 *opt_replyp
++ = NEW_ENV_VAR
;
1670 *opt_replyp
++ = ENV_USERVAR
;
1672 while ((c
= *ep
++) != '\0') {
1675 *opt_replyp
++ = IAC
;
1681 *opt_replyp
++ = ENV_ESC
;
1686 if ((ep
= vp
) != NULL
) {
1688 if (telopt_environ
== TELOPT_OLD_ENVIRON
)
1689 *opt_replyp
++ = old_env_value
;
1692 *opt_replyp
++ = NEW_ENV_VALUE
;
1703 if ((strcmp(ep
, "USER") == 0) ||
1704 (strcmp(ep
, "DISPLAY") == 0) ||
1705 (strcmp(ep
, "PRINTER") == 0) ||
1706 (strcmp(ep
, "SYSTEMTYPE") == 0) ||
1707 (strcmp(ep
, "JOB") == 0) ||
1708 (strcmp(ep
, "ACCT") == 0))
1713 env_opt_end(emptyok
)
1714 register int emptyok
;
1718 len
= opt_replyp
- opt_reply
+ 2;
1719 if (emptyok
|| len
> OPT_WRAPPER_SIZE
) {
1720 *opt_replyp
++ = IAC
;
1722 if (NETROOM() > len
) {
1723 ring_supply_data(&netoring
, opt_reply
, len
);
1724 printsub('>', &opt_reply
[2], len
- 2);
1727 (void) printf("telnet: not enough room in buffer for "
1728 "environment option end reply\n");
1732 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1743 register unsigned char *sbp
;
1745 int returnValue
= 0;
1750 while (--min_room
> 2 || (min_room
= TTYROOM()) > 2) {
1753 ring_consumed(&netiring
, count
);
1757 sbp
= netiring
.consume
;
1758 scc
= ring_full_consecutive(&netiring
);
1760 /* No more data coming in */
1765 c
= *sbp
++ & 0xff, scc
--; count
++;
1768 c
= (*decrypt_input
)(c
);
1770 switch (telrcv_state
) {
1773 telrcv_state
= TS_DATA
;
1775 break; /* Ignore \0 after CR */
1776 } else if ((c
== '\n') &&
1777 my_want_state_is_dont(TELOPT_ECHO
) && !crmod
) {
1785 telrcv_state
= TS_IAC
;
1789 * The 'crmod' hack (see following) is needed
1790 * since we can't * set CRMOD on output only.
1791 * Machines like MULTICS like to send \r without
1792 * \n; since we must turn off CRMOD to get proper
1793 * input, the mapping is done here (sigh).
1796 my_want_state_is_dont(TELOPT_BINARY
)) {
1801 c
= (*decrypt_input
)(c
);
1804 sbp
++, scc
--; count
++;
1807 } else if (my_want_state_is_dont(
1808 TELOPT_ECHO
) && (c
== '\n')) {
1809 sbp
++, scc
--; count
++;
1814 (*decrypt_input
)(-1);
1822 telrcv_state
= TS_CR
;
1838 telrcv_state
= TS_WILL
;
1842 telrcv_state
= TS_WONT
;
1846 telrcv_state
= TS_DO
;
1850 telrcv_state
= TS_DONT
;
1855 * We may have missed an urgent notification,
1856 * so make sure we flush whatever is in the
1859 printoption("RCVD", IAC
, DM
);
1861 if (ttyflush(1) == -2) {
1862 /* This will not return. */
1863 fatal_tty_error("write");
1865 SYNCHing
= stilloob();
1871 telrcv_state
= TS_SB
;
1881 printoption("RCVD", IAC
, c
);
1884 telrcv_state
= TS_DATA
;
1888 printoption("RCVD", WILL
, c
);
1890 telrcv_state
= TS_DATA
;
1894 printoption("RCVD", WONT
, c
);
1896 telrcv_state
= TS_DATA
;
1900 printoption("RCVD", DO
, c
);
1902 if (c
== TELOPT_NAWS
) {
1904 } else if (c
== TELOPT_LFLOW
) {
1909 telrcv_state
= TS_DATA
;
1913 printoption("RCVD", DONT
, c
);
1916 setconnmode(0); /* set new tty mode (maybe) */
1917 telrcv_state
= TS_DATA
;
1922 telrcv_state
= TS_SE
;
1932 * This is an error. We only expect to get
1933 * "IAC IAC" or "IAC SE". Several things may
1934 * have happend. An IAC was not doubled, the
1935 * IAC SE was left off, or another option got
1936 * inserted into the suboption are all possibilities.
1937 * If we assume that the IAC was not doubled,
1938 * and really the IAC SE was left off, we could
1939 * get into an infinate loop here. So, instead,
1940 * we terminate the suboption, and process the
1941 * partial suboption if we can.
1948 printoption("In SUBOPTION processing, "
1950 suboption(); /* handle sub-option */
1951 telrcv_state
= TS_IAC
;
1955 telrcv_state
= TS_SB
;
1961 suboption(); /* handle sub-option */
1962 telrcv_state
= TS_DATA
;
1967 ring_consumed(&netiring
, count
);
1968 return (returnValue
||count
);
1971 static int bol
= 1, local
= 0;
1979 command(0, "z\n", 2);
1990 int returnValue
= 0;
1995 while (NETROOM() > 2) {
2001 ring_consumed(&ttyiring
, count
);
2005 tbp
= ttyiring
.consume
;
2006 tcc
= ring_full_consecutive(&ttyiring
);
2011 c
= *tbp
++ & 0xff, sc
= strip(c
), tcc
--; count
++;
2012 if (rlogin
!= _POSIX_VDISABLE
) {
2021 if (sc
== '.' || c
== termEofChar
) {
2023 command(0, "close\n", 6);
2026 if (sc
== termSuspChar
) {
2028 command(0, "z\n", 2);
2032 command(0, (char *)tbp
, tcc
);
2046 if ((sc
== '\n') || (sc
== '\r'))
2048 } else if (sc
== escape
&& escape_valid
) {
2050 * Double escape is a pass through of a single
2053 if (tcc
&& strip(*tbp
) == escape
) {
2059 command(0, (char *)tbp
, tcc
);
2068 #ifdef KLUDGELINEMODE
2069 if (kludgelinemode
&& (globalmode
&MODE_EDIT
) && (sc
== echoc
)) {
2070 if (tcc
> 0 && strip(*tbp
) == echoc
) {
2071 tcc
--; tbp
++; count
++;
2073 dontlecho
= !dontlecho
;
2074 settimer(echotoggle
);
2081 if (MODE_LOCAL_CHARS(globalmode
)) {
2082 if (TerminalSpecialChars(sc
) == 0) {
2087 if (my_want_state_is_wont(TELOPT_BINARY
)) {
2091 * If we are in CRMOD mode (\r ==> \n)
2092 * on our local machine, then probably
2093 * a newline (unix) is CRLF (TELNET).
2095 if (MODE_LOCAL_CHARS(globalmode
)) {
2099 bol
= flushline
= 1;
2103 NET2ADD('\r', '\0');
2105 NET2ADD('\r', '\n');
2107 bol
= flushline
= 1;
2116 } else if (c
== IAC
) {
2123 ring_consumed(&ttyiring
, count
);
2124 return (returnValue
||count
); /* Non-zero if we did anything */
2130 * Try to do something.
2132 * If we do something useful, return 1; else return 0.
2139 int block
; /* should we block in the select ? */
2142 * One wants to be a bit careful about setting returnValue
2143 * to one, since a one implies we did some useful work,
2144 * and therefore probably won't be called to block next
2145 * time (TN3270 mode only).
2148 int netin
, netout
, netex
, ttyin
, ttyout
;
2150 /* Decide which rings should be processed */
2152 netout
= ring_full_count(&netoring
) &&
2154 (my_want_state_is_wont(TELOPT_LINEMODE
)
2155 #ifdef KLUDGELINEMODE
2156 /* X */ && (!kludgelinemode
|| my_want_state_is_do(TELOPT_SGA
))
2159 my_want_state_is_will(TELOPT_BINARY
));
2160 ttyout
= ring_full_count(&ttyoring
);
2162 ttyin
= (ring_empty_count(&ttyiring
) && !eof_pending
);
2164 netin
= !ISend
&& ring_empty_count(&netiring
);
2168 if (scheduler_lockout_tty
) {
2172 /* Call to system code to process rings */
2174 returnValue
= process_rings(netin
, netout
, netex
, ttyin
, ttyout
,
2177 /* Now, look at the input rings, looking for work to do. */
2179 if (ring_full_count(&ttyiring
)) {
2180 returnValue
|= telsnd();
2183 * If ttyiring is empty, check to see if there is a real EOF
2184 * pending. If so, we can maybe do the EOF write now.
2192 if (ring_full_count(&netiring
)) {
2193 returnValue
|= telrcv();
2195 return (returnValue
);
2199 * Select from tty and network...
2208 static char local_host
[MAXHOSTNAMELEN
] = { 0 };
2210 if (!local_host
[0]) {
2211 (void) gethostname(local_host
, sizeof (local_host
));
2212 local_host
[sizeof (local_host
)-1] = 0;
2214 auth_encrypt_init(local_host
, hostname
, "TELNET");
2215 auth_encrypt_user(user
);
2219 send_will(TELOPT_AUTHENTICATION
, 1);
2221 if (telnetport
|| wantencryption
) {
2222 send_do(TELOPT_ENCRYPT
, 1);
2223 send_will(TELOPT_ENCRYPT
, 1);
2228 send_do(TELOPT_SGA
, 1);
2229 send_will(TELOPT_TTYPE
, 1);
2230 send_will(TELOPT_NAWS
, 1);
2231 send_will(TELOPT_TSPEED
, 1);
2232 send_will(TELOPT_LFLOW
, 1);
2234 send_will(TELOPT_LINEMODE
, 1);
2235 send_will(TELOPT_NEW_ENVIRON
, 1);
2236 send_do(TELOPT_STATUS
, 1);
2237 if (env_getvalue((unsigned char *)"DISPLAY"))
2238 send_will(TELOPT_XDISPLOC
, 1);
2240 tel_enter_binary(eight
);
2244 * Note: we assume a tie to the authentication option here. This
2245 * is necessary so that authentication fails, we don't spin
2248 if (wantencryption
) {
2249 boolean_t printed_encrypt
= B_FALSE
;
2250 extern boolean_t auth_has_failed
;
2251 time_t timeout
= time(0) + 60;
2253 send_do(TELOPT_ENCRYPT
, 1);
2254 send_will(TELOPT_ENCRYPT
, 1);
2256 if (my_want_state_is_wont(TELOPT_AUTHENTICATION
)) {
2257 (void) printf(gettext(
2258 "\nServer refused to negotiate "
2259 "authentication, which is required\n"
2260 "for encryption. Good-bye.\n\r"));
2263 if (auth_has_failed
) {
2264 (void) printf(gettext(
2265 "\nAuthentication negotation has failed, "
2266 "which is required for\n"
2267 "encryption. Good-bye.\n\r"));
2270 if (my_want_state_is_dont(TELOPT_ENCRYPT
) ||
2271 my_want_state_is_wont(TELOPT_ENCRYPT
)) {
2272 (void) printf(gettext(
2273 "\nServer refused to negotiate encryption. "
2277 if (encrypt_is_encrypting())
2280 if (time(0) > timeout
) {
2281 (void) printf(gettext(
2282 "\nEncryption could not be enabled. "
2286 if (printed_encrypt
== B_FALSE
) {
2287 printed_encrypt
= B_TRUE
;
2288 (void) printf(gettext(
2289 "Waiting for encryption to be negotiated...\n"));
2291 * Turn on MODE_TRAPSIG and then turn off localchars
2292 * so that ^C will cause telnet to exit.
2294 TerminalNewMode(getconnmode()|MODE_TRAPSIG
);
2297 if (intr_happened
) {
2298 (void) printf(gettext(
2299 "\nUser requested an interrupt. Good-bye.\n\r"));
2304 if (printed_encrypt
) {
2305 (void) printf(gettext("done.\n"));
2314 while ((schedValue
= Scheduler(0)) != 0) {
2315 if (schedValue
== -1) {
2321 if (Scheduler(1) == -1) {
2328 #if 0 /* XXX - this not being in is a bug */
2332 * Return the address of the next "item" in the TELNET data
2333 * stream. This will be the address of the next character if
2334 * the current address is a user data character, or it will
2335 * be the address of the character following the TELNET command
2336 * if the current address is a TELNET IAC ("I Am a Command")
2344 if ((*current
&0xff) != IAC
) {
2347 switch (*(current
+1)&0xff) {
2353 case SB
: /* loop forever looking for the SE */
2355 register char *look
= current
+2;
2358 if ((*look
++&0xff) == IAC
) {
2359 if ((*look
++&0xff) == SE
) {
2374 * We are about to do a TELNET SYNCH operation. Clear
2375 * the path to the network.
2377 * Things are a bit tricky since we may have sent the first
2378 * byte or so of a previous TELNET command into the network.
2379 * So, we have to scan the network buffer from the beginning
2380 * until we are up to where we want to be.
2382 * A side effect of what we do, just to keep things
2383 * simple, is to clear the urgent data pointer. The principal
2384 * caller should be setting the urgent data pointer AFTER calling
2392 register char *thisitem
, *next
;
2394 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
2395 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
2399 while ((next
= nextitem(thisitem
)) <= netobuf
.send
) {
2403 /* Now, thisitem is first before/at boundary. */
2405 good
= netobuf
; /* where the good bytes go */
2407 while (netoring
.add
> thisitem
) {
2408 if (wewant(thisitem
)) {
2413 next
= nextitem(next
);
2414 } while (wewant(next
) && (nfrontp
> next
));
2415 length
= next
-thisitem
;
2416 memcpy(good
, thisitem
, length
);
2420 thisitem
= nextitem(thisitem
);
2428 * These routines add various telnet commands to the data stream.
2432 * doflush - Send do timing mark (for network connection flush) & then
2433 * get rid of anything in the output buffer. Return -1 if there was a
2434 * non-EWOULDBLOCK error on the tty flush, and otherwise return 0.
2444 /* Drop pending tty output */
2445 if (ttyflush(1) == -2)
2448 /* do printoption AFTER flush, otherwise the output gets tossed... */
2449 printoption("SENT", DO
, TELOPT_TM
);
2457 printoption("SENT", IAC
, AO
);
2459 if (doflush() == -1)
2470 printoption("SENT", IAC
, EL
);
2477 printoption("SENT", IAC
, EC
);
2484 netclear(); /* clear the path to the network */
2488 printoption("SENT", IAC
, DM
);
2492 int want_status_response
= 0;
2497 unsigned char tmp
[16];
2498 register unsigned char *cp
;
2500 if (my_want_state_is_dont(TELOPT_STATUS
)) {
2501 (void) printf("Remote side does not support STATUS option\n");
2508 *cp
++ = TELOPT_STATUS
;
2509 *cp
++ = TELQUAL_SEND
;
2512 if (NETROOM() >= cp
- tmp
) {
2513 ring_supply_data(&netoring
, tmp
, cp
-tmp
);
2514 printsub('>', tmp
+2, cp
- tmp
- 2);
2516 ++want_status_response
;
2524 printoption("SENT", IAC
, IP
);
2527 /* Ignore return as we're ending off anyway. */
2538 NET2ADD(IAC
, BREAK
);
2539 printoption("SENT", IAC
, BREAK
);
2542 if (doflush() == -1)
2554 NET2ADD(IAC
, ABORT
);
2555 printoption("SENT", IAC
, ABORT
);
2559 * Since sendabort() gets called while aborting,
2560 * ignore the doflush() return
2573 printoption("SENT", IAC
, SUSP
);
2576 if (doflush() == -1) {
2577 /* The following will not return. */
2578 fatal_tty_error("write");
2590 printoption("SENT", IAC
, xEOF
);
2594 * Send a window size update to the remote system.
2600 unsigned short rows
, cols
;
2601 unsigned char tmp
[16];
2602 register unsigned char *cp
;
2604 if (my_state_is_wont(TELOPT_NAWS
))
2607 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2608 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2610 if (TerminalWindowSize(&rows
, &cols
) == 0) { /* Failed */
2618 *cp
++ = TELOPT_NAWS
;
2623 if (NETROOM() >= cp
- tmp
) {
2624 ring_supply_data(&netoring
, tmp
, cp
-tmp
);
2625 printsub('>', tmp
+2, cp
- tmp
- 2);
2630 tel_enter_binary(rw
)
2634 send_do(TELOPT_BINARY
, 1);
2636 send_will(TELOPT_BINARY
, 1);
2640 tel_leave_binary(rw
)
2644 send_dont(TELOPT_BINARY
, 1);
2646 send_wont(TELOPT_BINARY
, 1);