2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
9 * Copyright (c) 1988, 1990, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static char sccsid
[] = "@(#)telnet.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/types.h>
51 * By the way, we need to include curses.h before telnet.h since,
52 * among other things, telnet.h #defines 'DO', which is a variable
53 * declared in curses.h.
56 #include <arpa/telnet.h>
70 #define strip(x) ((x)&0x7f)
72 /* Buffer for sub-options */
73 static unsigned char subbuffer
[SUBBUFSIZE
];
74 static unsigned char *subpointer
;
75 static unsigned char *subend
;
77 #define SB_CLEAR() subpointer = subbuffer;
78 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
79 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof (subbuffer))) { \
80 *subpointer++ = (c); \
83 #define SB_GET() ((*subpointer++)&0xff)
84 #define SB_PEEK() ((*subpointer)&0xff)
85 #define SB_EOF() (subpointer >= subend)
86 #define SB_LEN() (subend - subpointer)
88 char options
[SUBBUFSIZE
]; /* The combined options */
89 char do_dont_resp
[SUBBUFSIZE
];
90 char will_wont_resp
[SUBBUFSIZE
];
93 int autologin
= 0; /* Autologin anyone? */
97 static int ISend
; /* trying to send network data in */
100 int netdata
; /* Print out network data flow */
101 int crlf
; /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
103 int SYNCHing
; /* we are in TELNET SYNCH mode */
104 int flushout
; /* flush output */
105 int autoflush
= 0; /* flush output when interrupting? */
106 int autosynch
; /* send interrupt characters with SYNCH? */
107 int localflow
; /* we handle flow control locally */
108 int restartany
; /* if flow control enabled, restart on any character */
109 int localchars
; /* we recognize interrupt/quit */
110 int donelclchars
; /* the user has set "localchars" */
111 int donebinarytoggle
; /* the user has put us in binary */
112 int dontlecho
; /* do we suppress local echoing right now? */
113 int eof_pending
= 0; /* we received a genuine EOF on input, send IAC-EOF */
116 /* spin while waiting for authentication */
117 boolean_t scheduler_lockout_tty
= B_FALSE
;
118 int encrypt_flag
= 0;
119 int forwardable_flag
= 0;
120 int forward_flag
= 0;
121 boolean_t wantencryption
= B_FALSE
;
127 boolean_t escape_valid
= B_TRUE
;
128 #ifdef KLUDGELINEMODE
133 * Telnet receiver states for fsm
142 #define TS_SB 7 /* sub-option collection */
143 #define TS_SE 8 /* looking for sub-option end */
145 static int telrcv_state
;
147 static unsigned char telopt_environ
= TELOPT_NEW_ENVIRON
;
149 #define telopt_environ TELOPT_NEW_ENVIRON
152 jmp_buf toplevel
= { 0 };
155 static int flushline
;
158 int reqd_linemode
= 0; /* Set if either new or old line mode in */
159 /* effect since before initial negotiations */
161 #ifdef KLUDGELINEMODE
162 int kludgelinemode
= 1;
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
},
184 static void willoption(int);
185 static void wontoption(int);
186 static void lm_will(unsigned char *, int);
187 static void lm_wont(unsigned char *, int);
188 static void lm_do(unsigned char *, int);
189 static void lm_dont(unsigned char *, int);
190 static void slc_init(void);
191 static void slc_import(int);
192 static void slc_export(void);
193 static void slc_start_reply(size_t);
194 static void slc_add_reply(unsigned char, unsigned char, cc_t
);
195 static void slc_end_reply(void);
196 static void slc(unsigned char *, int);
197 static int slc_update(void);
198 static void env_opt(unsigned char *, int);
199 static void env_opt_start(void);
200 static void sendeof(void);
201 static int is_unique(register char *, register char **, register char **);
204 * Initialize telnet environment.
216 connected
= ISend
= localflow
= donebinarytoggle
= 0;
221 /* Don't change NetTrace */
223 escape
= CONTROL(']');
224 rlogin
= _POSIX_VDISABLE
;
225 #ifdef KLUDGELINEMODE
226 echoc
= CONTROL('E');
230 telrcv_state
= TS_DATA
;
245 char buffer
[100]; /* where things go */
254 ring
= va_arg(ap
, Ring
*);
255 format
= va_arg(ap
, char *);
258 while ((i
= *format
++) != 0) {
263 *ptr
++ = va_arg(ap
, int);
266 string
= va_arg(ap
, char *);
267 ring_supply_data(ring
, buffer
, ptr
-buffer
);
268 ring_supply_data(ring
, string
, strlen(string
));
272 ExitString("printring: trailing %%.\n",
276 ExitString("printring: unknown format "
277 "character.\n", EXIT_FAILURE
);
284 ring_supply_data(ring
, buffer
, ptr
-buffer
);
289 * These routines are in charge of sending option negotiations
292 * The basic idea is that we send the negotiation if either side
293 * is in disagreement as to what the current state should be.
298 register int c
, init
;
301 if (((do_dont_resp
[c
] == 0) && my_state_is_do(c
)) ||
302 my_want_state_is_do(c
))
304 set_my_want_state_do(c
);
309 printoption("SENT", DO
, c
);
314 register int c
, init
;
317 if (((do_dont_resp
[c
] == 0) && my_state_is_dont(c
)) ||
318 my_want_state_is_dont(c
))
320 set_my_want_state_dont(c
);
325 printoption("SENT", DONT
, c
);
330 register int c
, init
;
333 if (((will_wont_resp
[c
] == 0) && my_state_is_will(c
)) ||
334 my_want_state_is_will(c
))
336 set_my_want_state_will(c
);
341 printoption("SENT", WILL
, c
);
346 register int c
, init
;
349 if (((will_wont_resp
[c
] == 0) && my_state_is_wont(c
)) ||
350 my_want_state_is_wont(c
))
352 set_my_want_state_wont(c
);
357 printoption("SENT", WONT
, c
);
365 int new_state_ok
= 0;
367 if (do_dont_resp
[option
]) {
368 --do_dont_resp
[option
];
369 if (do_dont_resp
[option
] && my_state_is_do(option
))
370 --do_dont_resp
[option
];
373 if ((do_dont_resp
[option
] == 0) && my_want_state_is_dont(option
)) {
379 if (reqd_linemode
&& my_state_is_dont(option
)) {
384 settimer(modenegotiated
);
387 case TELOPT_AUTHENTICATION
:
397 * Special case for TM. If we get back a WILL,
398 * pretend we got back a WONT.
400 set_my_want_state_dont(option
);
401 set_my_state_dont(option
);
402 return; /* Never reply to TM will's/wont's */
404 case TELOPT_LINEMODE
:
410 set_my_want_state_do(option
);
412 setconnmode(0); /* possibly set new tty mode */
414 do_dont_resp
[option
]++;
415 send_dont(option
, 0);
418 set_my_state_do(option
);
419 if (option
== TELOPT_ENCRYPT
)
420 encrypt_send_support();
427 if (do_dont_resp
[option
]) {
428 --do_dont_resp
[option
];
429 if (do_dont_resp
[option
] && my_state_is_dont(option
))
430 --do_dont_resp
[option
];
433 if ((do_dont_resp
[option
] == 0) && my_want_state_is_do(option
)) {
437 #ifdef KLUDGELINEMODE
444 settimer(modenegotiated
);
450 set_my_want_state_dont(option
);
451 set_my_state_dont(option
);
452 return; /* Never reply to TM will's/wont's */
457 set_my_want_state_dont(option
);
458 if (my_state_is_do(option
))
459 send_dont(option
, 0);
460 setconnmode(0); /* Set new tty mode */
461 } else if (option
== TELOPT_TM
) {
463 * Special case for TM.
467 set_my_want_state_dont(option
);
469 set_my_state_dont(option
);
476 int new_state_ok
= 0;
478 if (will_wont_resp
[option
]) {
479 --will_wont_resp
[option
];
480 if (will_wont_resp
[option
] && my_state_is_will(option
))
481 --will_wont_resp
[option
];
484 if (will_wont_resp
[option
] == 0) {
485 if (my_want_state_is_wont(option
)) {
491 * Special case for TM. We send a WILL,
492 * but pretend we sent WONT.
494 send_will(option
, 0);
495 set_my_want_state_wont(TELOPT_TM
);
496 set_my_state_wont(TELOPT_TM
);
499 case TELOPT_BINARY
: /* binary mode */
500 case TELOPT_NAWS
: /* window size */
501 case TELOPT_TSPEED
: /* terminal speed */
502 case TELOPT_LFLOW
: /* local flow control */
503 case TELOPT_TTYPE
: /* terminal type option */
504 case TELOPT_SGA
: /* no big deal */
505 case TELOPT_ENCRYPT
: /* encryption variable option */
509 case TELOPT_NEW_ENVIRON
:
510 /* New environment variable option */
512 if (my_state_is_will(TELOPT_OLD_ENVIRON
))
513 /* turn off the old */
514 send_wont(TELOPT_OLD_ENVIRON
, 1);
516 case TELOPT_OLD_ENVIRON
:
517 /* Old environment variable option */
518 if (my_state_is_will(TELOPT_NEW_ENVIRON
))
519 /* Don't enable if new one is in use! */
522 telopt_environ
= option
;
527 case TELOPT_AUTHENTICATION
:
532 case TELOPT_XDISPLOC
: /* X Display location */
533 if (env_getvalue((unsigned char *)"DISPLAY"))
537 case TELOPT_LINEMODE
:
538 #ifdef KLUDGELINEMODE
540 send_do(TELOPT_SGA
, 1);
542 set_my_want_state_will(TELOPT_LINEMODE
);
543 send_will(option
, 0);
544 set_my_state_will(TELOPT_LINEMODE
);
548 case TELOPT_ECHO
: /* We're never going to echo... */
554 set_my_want_state_will(option
);
555 send_will(option
, 0);
556 setconnmode(0); /* Set new tty mode */
558 will_wont_resp
[option
]++;
559 send_wont(option
, 0);
563 * Handle options that need more things done after the
564 * other side has acknowledged the option.
567 case TELOPT_LINEMODE
:
568 #ifdef KLUDGELINEMODE
570 send_do(TELOPT_SGA
, 1);
572 set_my_state_will(option
);
574 send_do(TELOPT_SGA
, 0);
579 set_my_state_will(option
);
587 if (will_wont_resp
[option
]) {
588 --will_wont_resp
[option
];
589 if (will_wont_resp
[option
] && my_state_is_wont(option
))
590 --will_wont_resp
[option
];
593 if ((will_wont_resp
[option
] == 0) && my_want_state_is_will(option
)) {
595 case TELOPT_LINEMODE
:
596 linemode
= 0; /* put us back to the default state */
599 case TELOPT_NEW_ENVIRON
:
601 * The new environ option wasn't recognized, try
604 send_will(TELOPT_OLD_ENVIRON
, 1);
605 telopt_environ
= TELOPT_OLD_ENVIRON
;
609 /* we always accept a DONT */
610 set_my_want_state_wont(option
);
611 if (my_state_is_will(option
))
612 send_wont(option
, 0);
613 setconnmode(0); /* Set new tty mode */
615 set_my_state_wont(option
);
619 * Given a buffer returned by tgetent(), this routine will turn
620 * the pipe seperated list of names in the buffer into an array
621 * of pointers to null terminated names. We toss out any bad,
622 * duplicate, or verbose names (names with spaces).
625 static char *name_unknown
= "UNKNOWN";
626 static char *unknown
[] = { 0, 0 };
633 register char c
, *cp
, **argvp
, *cp2
, **argv
, **avt
;
636 if (strlen(name
) > 40u) {
638 unknown
[0] = name_unknown
;
644 unknown
[0] = name_unknown
;
646 * Count up the number of names.
648 for (n
= 1, cp
= buf
; *cp
&& *cp
!= ':'; cp
++) {
653 * Allocate an array to put the name pointers into
655 argv
= malloc((n
+3)*sizeof (char *));
660 * Fill up the array of pointers to names.
665 for (cp
= cp2
= buf
; (c
= *cp
) != NULL
; cp
++) {
666 if (c
== '|' || c
== ':') {
669 * Skip entries that have spaces or are over 40
670 * characters long. If this is our environment
671 * name, then put it up front. Otherwise, as
672 * long as this is not a duplicate name (case
673 * insensitive) add it to the list.
675 if (n
|| (cp
- cp2
> 41))
677 else if (name
&& (strncasecmp(name
, cp2
, cp
-cp2
) == 0))
679 else if (is_unique(cp2
, argv
+1, argvp
))
684 * Skip multiple delimiters. Reset cp2 to
685 * the beginning of the next name. Reset n,
686 * the flag for names with spaces.
688 while ((c
= *cp
) == '|')
694 * Skip entries with spaces or non-ascii values.
695 * Convert lower case letters to upper case.
697 if ((c
== ' ') || !isascii(c
))
704 * Check for an old V6 2 character name. If the second
705 * name points to the beginning of the buffer, and is
706 * only 2 characters long, move it to the end of the array.
708 if ((argv
[1] == buf
) && (strlen(argv
[1]) == 2)) {
710 for (avt
= &argv
[1]; avt
< argvp
; avt
++)
716 * Duplicate last name, for TTYPE option, and null
717 * terminate the array. If we didn't find a match on
718 * our terminal name, put that name at the beginning.
729 for (avt
= argv
; avt
< argvp
; avt
++)
740 is_unique(name
, as
, ae
)
741 register char *name
, **as
, **ae
;
746 n
= strlen(name
) + 1;
747 for (ap
= as
; ap
< ae
; ap
++)
748 if (strncasecmp(*ap
, name
, n
) == 0)
753 #define termbuf ttytype
754 extern char ttytype
[];
756 int resettermname
= 1;
762 static char **tnamep
= 0;
768 if (tnamep
&& tnamep
!= unknown
)
770 tname
= (char *)env_getvalue((unsigned char *)"TERM");
771 if ((tname
!= NULL
) && (setupterm(tname
, 1, &err
) == 0)) {
772 tnamep
= mklist(termbuf
, tname
);
774 if (tname
&& (strlen(tname
) <= 40u)) {
778 unknown
[0] = name_unknown
;
790 * Look at the sub-option buffer, and try to be helpful to the other
793 * Currently we recognize:
795 * Terminal type, send request.
796 * Terminal speed (send request).
797 * Local flow control (is request).
804 unsigned char subchar
;
806 printsub('<', subbuffer
, SB_LEN()+2);
807 switch (subchar
= SB_GET()) {
809 if (my_want_state_is_wont(TELOPT_TTYPE
))
811 if (SB_EOF() || SB_GET() != TELQUAL_SEND
) {
815 unsigned char temp
[50];
818 name
= gettermname();
819 len
= strlen(name
) + 4 + 2;
820 bytes
= snprintf((char *)temp
, sizeof (temp
),
821 "%c%c%c%c%s%c%c", IAC
, SB
,
822 TELOPT_TTYPE
, TELQUAL_IS
, name
, IAC
, SE
);
823 if ((len
< NETROOM()) && (bytes
< sizeof (temp
))) {
824 ring_supply_data(&netoring
, temp
, len
);
825 printsub('>', &temp
[2], len
-2);
827 ExitString("No room in buffer for "
828 "terminal type.\n", EXIT_FAILURE
);
834 if (my_want_state_is_wont(TELOPT_TSPEED
))
838 if (SB_GET() == TELQUAL_SEND
) {
840 unsigned char temp
[50];
843 TerminalSpeeds(&ispeed
, &ospeed
);
845 bytes
= snprintf((char *)temp
, sizeof (temp
),
846 "%c%c%c%c%d,%d%c%c", IAC
, SB
,
847 TELOPT_TSPEED
, TELQUAL_IS
, ospeed
, ispeed
, IAC
, SE
);
848 len
= strlen((char *)temp
+4) + 4; /* temp[3] is 0 ... */
850 if ((len
< NETROOM()) && (bytes
< sizeof (temp
))) {
851 ring_supply_data(&netoring
, temp
, len
);
852 printsub('>', temp
+2, len
- 2);
856 "telnet: not enough room in buffer "
857 "for terminal speed option reply\n");
861 if (my_want_state_is_wont(TELOPT_LFLOW
))
866 case LFLOW_RESTART_ANY
:
869 case LFLOW_RESTART_XON
:
885 case TELOPT_LINEMODE
:
886 if (my_want_state_is_wont(TELOPT_LINEMODE
))
892 lm_will(subpointer
, SB_LEN());
895 lm_wont(subpointer
, SB_LEN());
898 lm_do(subpointer
, SB_LEN());
901 lm_dont(subpointer
, SB_LEN());
904 slc(subpointer
, SB_LEN());
907 lm_mode(subpointer
, SB_LEN(), 0);
915 case TELOPT_OLD_ENVIRON
:
917 case TELOPT_NEW_ENVIRON
:
923 if (my_want_state_is_dont(subchar
))
927 if (my_want_state_is_wont(subchar
)) {
934 env_opt(subpointer
, SB_LEN());
937 case TELOPT_XDISPLOC
:
938 if (my_want_state_is_wont(TELOPT_XDISPLOC
))
942 if (SB_GET() == TELQUAL_SEND
) {
943 unsigned char temp
[50], *dp
;
946 if ((dp
= env_getvalue((unsigned char *)"DISPLAY")) ==
949 * Something happened, we no longer have a
950 * DISPLAY variable. So, turn off the option.
952 send_wont(TELOPT_XDISPLOC
, 1);
955 bytes
= snprintf((char *)temp
, sizeof (temp
),
956 "%c%c%c%c%s%c%c", IAC
, SB
,
957 TELOPT_XDISPLOC
, TELQUAL_IS
, dp
, IAC
, SE
);
958 len
= strlen((char *)temp
+4) + 4; /* temp[3] is 0 ... */
960 if ((len
< NETROOM()) && (bytes
< sizeof (temp
))) {
961 ring_supply_data(&netoring
, temp
, len
);
962 printsub('>', temp
+2, len
- 2);
966 "telnet: not enough room in buffer"
967 " for display location option reply\n");
971 case TELOPT_AUTHENTICATION
: {
978 if (my_want_state_is_wont(TELOPT_AUTHENTICATION
))
980 auth_send(subpointer
, SB_LEN());
983 if (my_want_state_is_wont(TELOPT_AUTHENTICATION
))
985 auth_reply(subpointer
, SB_LEN());
996 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
998 encrypt_start(subpointer
, SB_LEN());
1001 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1005 case ENCRYPT_SUPPORT
:
1006 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1008 encrypt_support(subpointer
, SB_LEN());
1010 case ENCRYPT_REQSTART
:
1011 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1013 encrypt_request_start(subpointer
, SB_LEN());
1015 case ENCRYPT_REQEND
:
1016 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1019 * We can always send an REQEND so that we cannot
1020 * get stuck encrypting. We should only get this
1021 * if we have been able to get in the correct mode
1024 encrypt_request_end();
1027 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1029 encrypt_is(subpointer
, SB_LEN());
1032 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1034 encrypt_reply(subpointer
, SB_LEN());
1036 case ENCRYPT_ENC_KEYID
:
1037 if (my_want_state_is_dont(TELOPT_ENCRYPT
))
1039 encrypt_enc_keyid(subpointer
, SB_LEN());
1041 case ENCRYPT_DEC_KEYID
:
1042 if (my_want_state_is_wont(TELOPT_ENCRYPT
))
1044 encrypt_dec_keyid(subpointer
, SB_LEN());
1055 static unsigned char str_lm
[] = { IAC
, SB
, TELOPT_LINEMODE
, 0, 0, IAC
, SE
};
1063 /* Should not happen... */
1065 "telnet: command missing from linemode WILL request\n");
1069 case LM_FORWARDMASK
: /* We shouldn't ever get this... */
1073 if (NETROOM() > sizeof (str_lm
)) {
1074 ring_supply_data(&netoring
, str_lm
, sizeof (str_lm
));
1075 printsub('>', &str_lm
[2], sizeof (str_lm
)-2);
1078 (void) printf("telnet: not enough room in buffer for"
1079 "reply to linemode WILL request\n");
1090 /* Should not happen... */
1092 "telnet: command missing from linemode WONT request\n");
1096 case LM_FORWARDMASK
: /* We shouldn't ever get this... */
1098 /* We are always DONT, so don't respond */
1109 /* Should not happen... */
1111 "telnet: command missing from linemode DO request\n");
1115 case LM_FORWARDMASK
:
1119 if (NETROOM() > sizeof (str_lm
)) {
1120 ring_supply_data(&netoring
, str_lm
, sizeof (str_lm
));
1121 printsub('>', &str_lm
[2], sizeof (str_lm
)-2);
1124 (void) printf("telnet: not enough room in buffer for"
1125 "reply to linemode DO request\n");
1136 /* Should not happen... */
1138 "telnet: command missing from linemode DONT request\n");
1142 case LM_FORWARDMASK
:
1144 /* we are always WONT, so don't respond */
1149 static unsigned char str_lm_mode
[] = {
1150 IAC
, SB
, TELOPT_LINEMODE
, LM_MODE
, 0, IAC
, SE
1154 lm_mode(cmd
, len
, init
)
1160 if ((linemode
&MODE_MASK
&~MODE_ACK
) == *cmd
)
1162 linemode
= *cmd
&(MODE_MASK
&~MODE_ACK
);
1163 str_lm_mode
[4] = linemode
;
1165 str_lm_mode
[4] |= MODE_ACK
;
1166 if (NETROOM() > sizeof (str_lm_mode
)) {
1167 ring_supply_data(&netoring
, str_lm_mode
, sizeof (str_lm_mode
));
1168 printsub('>', &str_lm_mode
[2], sizeof (str_lm_mode
)-2);
1171 (void) printf("telnet: not enough room in buffer for"
1172 "reply to linemode request\n");
1173 setconnmode(0); /* set changed mode */
1180 * Handle special character suboption of LINEMODE.
1186 char flags
; /* Current flags & level */
1187 char mylevel
; /* Maximum level & flags */
1190 #define SLC_IMPORT 0
1191 #define SLC_EXPORT 1
1192 #define SLC_RVALUE 2
1193 static int slc_mode
= SLC_EXPORT
;
1198 register struct spc
*spcp
;
1201 for (spcp
= spc_data
; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1204 spcp
->flags
= spcp
->mylevel
= SLC_NOSUPPORT
;
1207 #define initfunc(func, flags) { \
1208 spcp = &spc_data[func]; \
1209 if (spcp->valp = tcval(func)) { \
1210 spcp->val = *spcp->valp; \
1211 spcp->mylevel = SLC_VARIABLE|(flags);\
1214 spcp->mylevel = SLC_DEFAULT; \
1218 initfunc(SLC_SYNCH
, 0);
1220 initfunc(SLC_AO
, 0);
1221 initfunc(SLC_AYT
, 0);
1223 initfunc(SLC_ABORT
, SLC_FLUSHIN
|SLC_FLUSHOUT
);
1224 initfunc(SLC_EOF
, 0);
1225 initfunc(SLC_SUSP
, SLC_FLUSHIN
);
1226 initfunc(SLC_EC
, 0);
1227 initfunc(SLC_EL
, 0);
1228 initfunc(SLC_EW
, 0);
1229 initfunc(SLC_RP
, 0);
1230 initfunc(SLC_LNEXT
, 0);
1231 initfunc(SLC_XON
, 0);
1232 initfunc(SLC_XOFF
, 0);
1233 initfunc(SLC_FORW1
, 0);
1235 initfunc(SLC_FORW2
, 0);
1239 initfunc(SLC_IP
, SLC_FLUSHIN
|SLC_FLUSHOUT
);
1242 if (slc_mode
== SLC_EXPORT
)
1252 (void) printf("Special characters are %s values\n",
1253 slc_mode
== SLC_IMPORT
? "remote default" :
1254 slc_mode
== SLC_EXPORT
? "local" :
1261 slc_mode
= SLC_EXPORT
;
1262 if (my_state_is_will(TELOPT_LINEMODE
))
1267 slc_mode_import(def
)
1270 slc_mode
= def
? SLC_IMPORT
: SLC_RVALUE
;
1271 if (my_state_is_will(TELOPT_LINEMODE
))
1275 static unsigned char slc_import_val
[] = {
1276 IAC
, SB
, TELOPT_LINEMODE
, LM_SLC
, 0, SLC_VARIABLE
, 0, IAC
, SE
1278 static unsigned char slc_import_def
[] = {
1279 IAC
, SB
, TELOPT_LINEMODE
, LM_SLC
, 0, SLC_DEFAULT
, 0, IAC
, SE
1286 if (NETROOM() > sizeof (slc_import_val
)) {
1288 ring_supply_data(&netoring
, slc_import_def
,
1289 sizeof (slc_import_def
));
1290 printsub('>', &slc_import_def
[2],
1291 sizeof (slc_import_def
)-2);
1293 ring_supply_data(&netoring
, slc_import_val
,
1294 sizeof (slc_import_val
));
1295 printsub('>', &slc_import_val
[2],
1296 sizeof (slc_import_val
)-2);
1301 "telnet: not enough room in buffer for slc import"
1305 static uchar_t
*slc_reply
= NULL
;
1306 static uchar_t
*slc_replyp
= NULL
;
1308 * The SLC reply consists of: IAC, SB, TELOPT_LINEMODE, LM_SLC,
1309 * SLC triplets[], IAC, SE. i.e. it has a 'wrapper' of 6 control characters.
1311 #define SLC_WRAPPER_SIZE 6
1316 register struct spc
*spcp
;
1318 TerminalDefaultChars();
1320 slc_start_reply(NSLC
* 3); /* 3 bytes needed per triplet */
1321 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1322 if (spcp
->mylevel
!= SLC_NOSUPPORT
) {
1323 if (spcp
->val
== (cc_t
)(_POSIX_VDISABLE
))
1324 spcp
->flags
= SLC_NOSUPPORT
;
1326 spcp
->flags
= spcp
->mylevel
;
1328 spcp
->val
= *spcp
->valp
;
1329 slc_add_reply(spcp
- spc_data
, spcp
->flags
, spcp
->val
);
1333 (void) slc_update();
1334 setconnmode(1); /* Make sure the character values are set */
1339 register unsigned char *cp
;
1342 register struct spc
*spcp
;
1343 register int func
, level
;
1345 slc_start_reply(len
);
1347 for (; len
>= 3; len
-= 3, cp
+= 3) {
1349 func
= cp
[SLC_FUNC
];
1353 * Client side: always ignore 0 function.
1358 if ((cp
[SLC_FLAGS
] & SLC_LEVELBITS
) != SLC_NOSUPPORT
)
1359 slc_add_reply(func
, SLC_NOSUPPORT
, 0);
1363 spcp
= &spc_data
[func
];
1365 level
= cp
[SLC_FLAGS
]&(SLC_LEVELBITS
|SLC_ACK
);
1367 if ((cp
[SLC_VALUE
] == (unsigned char)spcp
->val
) &&
1368 ((level
&SLC_LEVELBITS
) == (spcp
->flags
&SLC_LEVELBITS
))) {
1372 if (level
== (SLC_DEFAULT
|SLC_ACK
)) {
1374 * This is an error condition, the SLC_ACK
1375 * bit should never be set for the SLC_DEFAULT
1376 * level. Our best guess to recover is to
1377 * ignore the SLC_ACK bit.
1379 cp
[SLC_FLAGS
] &= ~SLC_ACK
;
1382 if (level
== ((spcp
->flags
&SLC_LEVELBITS
)|SLC_ACK
)) {
1383 spcp
->val
= (cc_t
)cp
[SLC_VALUE
];
1384 spcp
->flags
= cp
[SLC_FLAGS
]; /* include SLC_ACK */
1390 if (level
<= (spcp
->mylevel
&SLC_LEVELBITS
)) {
1391 spcp
->flags
= cp
[SLC_FLAGS
]|SLC_ACK
;
1392 spcp
->val
= (cc_t
)cp
[SLC_VALUE
];
1394 if (level
== SLC_DEFAULT
) {
1395 if ((spcp
->mylevel
&SLC_LEVELBITS
) != SLC_DEFAULT
)
1396 spcp
->flags
= spcp
->mylevel
;
1398 spcp
->flags
= SLC_NOSUPPORT
;
1400 slc_add_reply(func
, spcp
->flags
, spcp
->val
);
1404 setconnmode(1); /* set the new character values */
1410 register struct spc
*spcp
;
1412 slc_start_reply(NSLC
* 3); /* 3 bytes needed per triplet */
1413 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1414 if (spcp
->valp
&& spcp
->val
!= *spcp
->valp
) {
1415 spcp
->val
= *spcp
->valp
;
1416 if (spcp
->val
== (cc_t
)(_POSIX_VDISABLE
))
1417 spcp
->flags
= SLC_NOSUPPORT
;
1419 spcp
->flags
= spcp
->mylevel
;
1420 slc_add_reply(spcp
- spc_data
, spcp
->flags
, spcp
->val
);
1428 slc_start_reply(size_t len
)
1431 * SLC triplets may contain escaped characters, allow for
1432 * worst case by allocating 2 bytes for every character.
1434 slc_reply
= realloc(slc_reply
, (len
* 2) + SLC_WRAPPER_SIZE
);
1435 if (slc_reply
== NULL
) {
1436 fprintf(stderr
, "telnet: error allocating SLC reply memory\n");
1439 slc_replyp
= slc_reply
;
1440 *slc_replyp
++ = IAC
;
1442 *slc_replyp
++ = TELOPT_LINEMODE
;
1443 *slc_replyp
++ = LM_SLC
;
1447 slc_add_reply(unsigned char func
, unsigned char flags
, cc_t value
)
1449 if ((*slc_replyp
++ = func
) == IAC
)
1450 *slc_replyp
++ = IAC
;
1451 if ((*slc_replyp
++ = flags
) == IAC
)
1452 *slc_replyp
++ = IAC
;
1453 if ((*slc_replyp
++ = (unsigned char)value
) == IAC
)
1454 *slc_replyp
++ = IAC
;
1462 *slc_replyp
++ = IAC
;
1464 len
= slc_replyp
- slc_reply
;
1465 if (len
<= SLC_WRAPPER_SIZE
)
1467 if (NETROOM() > len
) {
1468 ring_supply_data(&netoring
, slc_reply
, slc_replyp
- slc_reply
);
1469 printsub('>', &slc_reply
[2], slc_replyp
- slc_reply
- 2);
1472 (void) printf("telnet: not enough room in buffer for slc end "
1479 register struct spc
*spcp
;
1480 int need_update
= 0;
1482 for (spcp
= &spc_data
[1]; spcp
< &spc_data
[NSLC
+1]; spcp
++) {
1483 if (!(spcp
->flags
&SLC_ACK
))
1485 spcp
->flags
&= ~SLC_ACK
;
1486 if (spcp
->valp
&& (*spcp
->valp
!= spcp
->val
)) {
1487 *spcp
->valp
= spcp
->val
;
1491 return (need_update
);
1497 * Earlier version of telnet/telnetd from the BSD code had
1498 * the definitions of VALUE and VAR reversed. To ensure
1499 * maximum interoperability, we assume that the server is
1500 * an older BSD server, until proven otherwise. The newer
1501 * BSD servers should be able to handle either definition,
1502 * so it is better to use the wrong values if we don't
1503 * know what type of server it is.
1506 int old_env_var
= OLD_ENV_VAR
;
1507 int old_env_value
= OLD_ENV_VALUE
;
1509 #define old_env_var OLD_ENV_VAR
1510 #define old_env_value OLD_ENV_VALUE
1516 register unsigned char *buf
;
1519 register unsigned char *ep
= 0, *epc
= 0;
1522 switch (buf
[0]&0xff) {
1527 } else for (i
= 1; i
< len
; i
++) {
1528 switch (buf
[i
]&0xff) {
1532 if (telopt_environ
== TELOPT_OLD_ENVIRON
&&
1534 /* Server has the same definitions */
1535 old_env_var
= OLD_ENV_VAR
;
1536 old_env_value
= OLD_ENV_VALUE
;
1542 * Although OLD_ENV_VALUE is not legal, we will
1543 * still recognize it, just in case it is an
1544 * old server that has VAR & VALUE mixed up...
1555 ep
= epc
= &buf
[i
+1];
1575 /* Ignore for now. We shouldn't get it anyway. */
1583 static unsigned char *opt_reply
;
1584 static unsigned char *opt_replyp
;
1585 static unsigned char *opt_replyend
;
1586 #define OPT_REPLY_INITIAL_SIZE 256
1588 * The opt reply consists of: IAC, SB, telopt_environ, TELQUAL_IS,
1589 * value, IAC, SE. i.e. it has a 'wrapper' of 6 control characters.
1591 #define OPT_WRAPPER_SIZE 6
1596 opt_reply
= realloc(opt_reply
, OPT_REPLY_INITIAL_SIZE
);
1597 if (opt_reply
== NULL
) {
1599 "telnet: error allocating environment option memory\n");
1600 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1603 opt_replyp
= opt_reply
;
1604 opt_replyend
= opt_reply
+ OPT_REPLY_INITIAL_SIZE
;
1605 *opt_replyp
++ = IAC
;
1607 *opt_replyp
++ = telopt_environ
;
1608 *opt_replyp
++ = TELQUAL_IS
;
1612 env_opt_start_info()
1616 opt_replyp
[-1] = TELQUAL_INFO
;
1621 register unsigned char *ep
;
1623 register unsigned char *vp
, c
;
1627 if (opt_reply
== NULL
) /* XXX */
1630 if (ep
== NULL
|| *ep
== '\0') {
1631 /* Send user defined variables first. */
1632 (void) env_default(1, 0);
1633 while (ep
= env_default(0, 0))
1636 /* Now add the list of well know variables. */
1637 (void) env_default(1, 1);
1638 while (ep
= env_default(0, 1))
1642 vp
= env_getvalue(ep
);
1645 * Calculate space required for opt_reply and allocate more if required.
1646 * Assume worst case that every character is escaped, so needs 2 bytes.
1648 opt_reply_used
= opt_replyp
- opt_reply
; /* existing contents */
1649 opt_reply_size
= opt_reply_used
+ OPT_WRAPPER_SIZE
+
1650 (2 * (strlen((char *)ep
))) +
1651 (vp
== NULL
? 0 : (2 * strlen((char *)vp
)));
1653 if (opt_reply_size
> (opt_replyend
- opt_reply
)) {
1654 opt_reply
= realloc(opt_reply
, opt_reply_size
);
1655 if (opt_reply
== NULL
) {
1657 "telnet: can't allocate environment option "
1659 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1662 opt_replyp
= opt_reply
+ opt_reply_used
;
1663 opt_replyend
= opt_reply
+ opt_reply_size
;
1666 if (opt_welldefined((char *)ep
))
1668 if (telopt_environ
== TELOPT_OLD_ENVIRON
)
1669 *opt_replyp
++ = old_env_var
;
1672 *opt_replyp
++ = NEW_ENV_VAR
;
1674 *opt_replyp
++ = ENV_USERVAR
;
1676 while ((c
= *ep
++) != NULL
) {
1679 *opt_replyp
++ = IAC
;
1685 *opt_replyp
++ = ENV_ESC
;
1690 if ((ep
= vp
) != NULL
) {
1692 if (telopt_environ
== TELOPT_OLD_ENVIRON
)
1693 *opt_replyp
++ = old_env_value
;
1696 *opt_replyp
++ = NEW_ENV_VALUE
;
1707 if ((strcmp(ep
, "USER") == 0) ||
1708 (strcmp(ep
, "DISPLAY") == 0) ||
1709 (strcmp(ep
, "PRINTER") == 0) ||
1710 (strcmp(ep
, "SYSTEMTYPE") == 0) ||
1711 (strcmp(ep
, "JOB") == 0) ||
1712 (strcmp(ep
, "ACCT") == 0))
1717 env_opt_end(emptyok
)
1718 register int emptyok
;
1722 len
= opt_replyp
- opt_reply
+ 2;
1723 if (emptyok
|| len
> OPT_WRAPPER_SIZE
) {
1724 *opt_replyp
++ = IAC
;
1726 if (NETROOM() > len
) {
1727 ring_supply_data(&netoring
, opt_reply
, len
);
1728 printsub('>', &opt_reply
[2], len
- 2);
1731 (void) printf("telnet: not enough room in buffer for "
1732 "environment option end reply\n");
1736 opt_reply
= opt_replyp
= opt_replyend
= NULL
;
1747 register unsigned char *sbp
;
1749 int returnValue
= 0;
1754 while (--min_room
> 2 || (min_room
= TTYROOM()) > 2) {
1757 ring_consumed(&netiring
, count
);
1761 sbp
= netiring
.consume
;
1762 scc
= ring_full_consecutive(&netiring
);
1764 /* No more data coming in */
1769 c
= *sbp
++ & 0xff, scc
--; count
++;
1772 c
= (*decrypt_input
)(c
);
1774 switch (telrcv_state
) {
1777 telrcv_state
= TS_DATA
;
1779 break; /* Ignore \0 after CR */
1780 } else if ((c
== '\n') &&
1781 my_want_state_is_dont(TELOPT_ECHO
) && !crmod
) {
1789 telrcv_state
= TS_IAC
;
1793 * The 'crmod' hack (see following) is needed
1794 * since we can't * set CRMOD on output only.
1795 * Machines like MULTICS like to send \r without
1796 * \n; since we must turn off CRMOD to get proper
1797 * input, the mapping is done here (sigh).
1800 my_want_state_is_dont(TELOPT_BINARY
)) {
1805 c
= (*decrypt_input
)(c
);
1808 sbp
++, scc
--; count
++;
1811 } else if (my_want_state_is_dont(
1812 TELOPT_ECHO
) && (c
== '\n')) {
1813 sbp
++, scc
--; count
++;
1818 (*decrypt_input
)(-1);
1826 telrcv_state
= TS_CR
;
1842 telrcv_state
= TS_WILL
;
1846 telrcv_state
= TS_WONT
;
1850 telrcv_state
= TS_DO
;
1854 telrcv_state
= TS_DONT
;
1859 * We may have missed an urgent notification,
1860 * so make sure we flush whatever is in the
1863 printoption("RCVD", IAC
, DM
);
1865 if (ttyflush(1) == -2) {
1866 /* This will not return. */
1867 fatal_tty_error("write");
1869 SYNCHing
= stilloob();
1875 telrcv_state
= TS_SB
;
1885 printoption("RCVD", IAC
, c
);
1888 telrcv_state
= TS_DATA
;
1892 printoption("RCVD", WILL
, c
);
1894 telrcv_state
= TS_DATA
;
1898 printoption("RCVD", WONT
, c
);
1900 telrcv_state
= TS_DATA
;
1904 printoption("RCVD", DO
, c
);
1906 if (c
== TELOPT_NAWS
) {
1908 } else if (c
== TELOPT_LFLOW
) {
1913 telrcv_state
= TS_DATA
;
1917 printoption("RCVD", DONT
, c
);
1920 setconnmode(0); /* set new tty mode (maybe) */
1921 telrcv_state
= TS_DATA
;
1926 telrcv_state
= TS_SE
;
1936 * This is an error. We only expect to get
1937 * "IAC IAC" or "IAC SE". Several things may
1938 * have happend. An IAC was not doubled, the
1939 * IAC SE was left off, or another option got
1940 * inserted into the suboption are all possibilities.
1941 * If we assume that the IAC was not doubled,
1942 * and really the IAC SE was left off, we could
1943 * get into an infinate loop here. So, instead,
1944 * we terminate the suboption, and process the
1945 * partial suboption if we can.
1952 printoption("In SUBOPTION processing, "
1954 suboption(); /* handle sub-option */
1955 telrcv_state
= TS_IAC
;
1959 telrcv_state
= TS_SB
;
1965 suboption(); /* handle sub-option */
1966 telrcv_state
= TS_DATA
;
1971 ring_consumed(&netiring
, count
);
1972 return (returnValue
||count
);
1975 static int bol
= 1, local
= 0;
1983 command(0, "z\n", 2);
1994 int returnValue
= 0;
1999 while (NETROOM() > 2) {
2005 ring_consumed(&ttyiring
, count
);
2009 tbp
= ttyiring
.consume
;
2010 tcc
= ring_full_consecutive(&ttyiring
);
2015 c
= *tbp
++ & 0xff, sc
= strip(c
), tcc
--; count
++;
2016 if (rlogin
!= _POSIX_VDISABLE
) {
2025 if (sc
== '.' || c
== termEofChar
) {
2027 command(0, "close\n", 6);
2030 if (sc
== termSuspChar
) {
2032 command(0, "z\n", 2);
2036 command(0, (char *)tbp
, tcc
);
2050 if ((sc
== '\n') || (sc
== '\r'))
2052 } else if (sc
== escape
&& escape_valid
) {
2054 * Double escape is a pass through of a single
2057 if (tcc
&& strip(*tbp
) == escape
) {
2063 command(0, (char *)tbp
, tcc
);
2072 #ifdef KLUDGELINEMODE
2073 if (kludgelinemode
&& (globalmode
&MODE_EDIT
) && (sc
== echoc
)) {
2074 if (tcc
> 0 && strip(*tbp
) == echoc
) {
2075 tcc
--; tbp
++; count
++;
2077 dontlecho
= !dontlecho
;
2078 settimer(echotoggle
);
2085 if (MODE_LOCAL_CHARS(globalmode
)) {
2086 if (TerminalSpecialChars(sc
) == 0) {
2091 if (my_want_state_is_wont(TELOPT_BINARY
)) {
2095 * If we are in CRMOD mode (\r ==> \n)
2096 * on our local machine, then probably
2097 * a newline (unix) is CRLF (TELNET).
2099 if (MODE_LOCAL_CHARS(globalmode
)) {
2103 bol
= flushline
= 1;
2107 NET2ADD('\r', '\0');
2109 NET2ADD('\r', '\n');
2111 bol
= flushline
= 1;
2120 } else if (c
== IAC
) {
2127 ring_consumed(&ttyiring
, count
);
2128 return (returnValue
||count
); /* Non-zero if we did anything */
2134 * Try to do something.
2136 * If we do something useful, return 1; else return 0.
2143 int block
; /* should we block in the select ? */
2146 * One wants to be a bit careful about setting returnValue
2147 * to one, since a one implies we did some useful work,
2148 * and therefore probably won't be called to block next
2149 * time (TN3270 mode only).
2152 int netin
, netout
, netex
, ttyin
, ttyout
;
2154 /* Decide which rings should be processed */
2156 netout
= ring_full_count(&netoring
) &&
2158 (my_want_state_is_wont(TELOPT_LINEMODE
)
2159 #ifdef KLUDGELINEMODE
2160 /* X */ && (!kludgelinemode
|| my_want_state_is_do(TELOPT_SGA
))
2163 my_want_state_is_will(TELOPT_BINARY
));
2164 ttyout
= ring_full_count(&ttyoring
);
2166 ttyin
= (ring_empty_count(&ttyiring
) && !eof_pending
);
2168 netin
= !ISend
&& ring_empty_count(&netiring
);
2172 if (scheduler_lockout_tty
) {
2176 /* Call to system code to process rings */
2178 returnValue
= process_rings(netin
, netout
, netex
, ttyin
, ttyout
,
2181 /* Now, look at the input rings, looking for work to do. */
2183 if (ring_full_count(&ttyiring
)) {
2184 returnValue
|= telsnd();
2187 * If ttyiring is empty, check to see if there is a real EOF
2188 * pending. If so, we can maybe do the EOF write now.
2196 if (ring_full_count(&netiring
)) {
2197 returnValue
|= telrcv();
2199 return (returnValue
);
2203 * Select from tty and network...
2212 static char local_host
[MAXHOSTNAMELEN
] = { 0 };
2214 if (!local_host
[0]) {
2215 (void) gethostname(local_host
, sizeof (local_host
));
2216 local_host
[sizeof (local_host
)-1] = 0;
2218 auth_encrypt_init(local_host
, hostname
, "TELNET");
2219 auth_encrypt_user(user
);
2223 send_will(TELOPT_AUTHENTICATION
, 1);
2225 if (telnetport
|| wantencryption
) {
2226 send_do(TELOPT_ENCRYPT
, 1);
2227 send_will(TELOPT_ENCRYPT
, 1);
2232 send_do(TELOPT_SGA
, 1);
2233 send_will(TELOPT_TTYPE
, 1);
2234 send_will(TELOPT_NAWS
, 1);
2235 send_will(TELOPT_TSPEED
, 1);
2236 send_will(TELOPT_LFLOW
, 1);
2238 send_will(TELOPT_LINEMODE
, 1);
2239 send_will(TELOPT_NEW_ENVIRON
, 1);
2240 send_do(TELOPT_STATUS
, 1);
2241 if (env_getvalue((unsigned char *)"DISPLAY"))
2242 send_will(TELOPT_XDISPLOC
, 1);
2244 tel_enter_binary(eight
);
2248 * Note: we assume a tie to the authentication option here. This
2249 * is necessary so that authentication fails, we don't spin
2252 if (wantencryption
) {
2253 boolean_t printed_encrypt
= B_FALSE
;
2254 extern boolean_t auth_has_failed
;
2255 time_t timeout
= time(0) + 60;
2257 send_do(TELOPT_ENCRYPT
, 1);
2258 send_will(TELOPT_ENCRYPT
, 1);
2260 if (my_want_state_is_wont(TELOPT_AUTHENTICATION
)) {
2261 (void) printf(gettext(
2262 "\nServer refused to negotiate "
2263 "authentication, which is required\n"
2264 "for encryption. Good-bye.\n\r"));
2267 if (auth_has_failed
) {
2268 (void) printf(gettext(
2269 "\nAuthentication negotation has failed, "
2270 "which is required for\n"
2271 "encryption. Good-bye.\n\r"));
2274 if (my_want_state_is_dont(TELOPT_ENCRYPT
) ||
2275 my_want_state_is_wont(TELOPT_ENCRYPT
)) {
2276 (void) printf(gettext(
2277 "\nServer refused to negotiate encryption. "
2281 if (encrypt_is_encrypting())
2284 if (time(0) > timeout
) {
2285 (void) printf(gettext(
2286 "\nEncryption could not be enabled. "
2290 if (printed_encrypt
== B_FALSE
) {
2291 printed_encrypt
= B_TRUE
;
2292 (void) printf(gettext(
2293 "Waiting for encryption to be negotiated...\n"));
2295 * Turn on MODE_TRAPSIG and then turn off localchars
2296 * so that ^C will cause telnet to exit.
2298 TerminalNewMode(getconnmode()|MODE_TRAPSIG
);
2301 if (intr_happened
) {
2302 (void) printf(gettext(
2303 "\nUser requested an interrupt. Good-bye.\n\r"));
2308 if (printed_encrypt
) {
2309 (void) printf(gettext("done.\n"));
2318 while ((schedValue
= Scheduler(0)) != 0) {
2319 if (schedValue
== -1) {
2325 if (Scheduler(1) == -1) {
2332 #if 0 /* XXX - this not being in is a bug */
2336 * Return the address of the next "item" in the TELNET data
2337 * stream. This will be the address of the next character if
2338 * the current address is a user data character, or it will
2339 * be the address of the character following the TELNET command
2340 * if the current address is a TELNET IAC ("I Am a Command")
2348 if ((*current
&0xff) != IAC
) {
2351 switch (*(current
+1)&0xff) {
2357 case SB
: /* loop forever looking for the SE */
2359 register char *look
= current
+2;
2362 if ((*look
++&0xff) == IAC
) {
2363 if ((*look
++&0xff) == SE
) {
2378 * We are about to do a TELNET SYNCH operation. Clear
2379 * the path to the network.
2381 * Things are a bit tricky since we may have sent the first
2382 * byte or so of a previous TELNET command into the network.
2383 * So, we have to scan the network buffer from the beginning
2384 * until we are up to where we want to be.
2386 * A side effect of what we do, just to keep things
2387 * simple, is to clear the urgent data pointer. The principal
2388 * caller should be setting the urgent data pointer AFTER calling
2396 register char *thisitem
, *next
;
2398 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
2399 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
2403 while ((next
= nextitem(thisitem
)) <= netobuf
.send
) {
2407 /* Now, thisitem is first before/at boundary. */
2409 good
= netobuf
; /* where the good bytes go */
2411 while (netoring
.add
> thisitem
) {
2412 if (wewant(thisitem
)) {
2417 next
= nextitem(next
);
2418 } while (wewant(next
) && (nfrontp
> next
));
2419 length
= next
-thisitem
;
2420 memcpy(good
, thisitem
, length
);
2424 thisitem
= nextitem(thisitem
);
2432 * These routines add various telnet commands to the data stream.
2436 * doflush - Send do timing mark (for network connection flush) & then
2437 * get rid of anything in the output buffer. Return -1 if there was a
2438 * non-EWOULDBLOCK error on the tty flush, and otherwise return 0.
2448 /* Drop pending tty output */
2449 if (ttyflush(1) == -2)
2452 /* do printoption AFTER flush, otherwise the output gets tossed... */
2453 printoption("SENT", DO
, TELOPT_TM
);
2461 printoption("SENT", IAC
, AO
);
2463 if (doflush() == -1)
2474 printoption("SENT", IAC
, EL
);
2481 printoption("SENT", IAC
, EC
);
2488 netclear(); /* clear the path to the network */
2492 printoption("SENT", IAC
, DM
);
2496 int want_status_response
= 0;
2501 unsigned char tmp
[16];
2502 register unsigned char *cp
;
2504 if (my_want_state_is_dont(TELOPT_STATUS
)) {
2505 (void) printf("Remote side does not support STATUS option\n");
2512 *cp
++ = TELOPT_STATUS
;
2513 *cp
++ = TELQUAL_SEND
;
2516 if (NETROOM() >= cp
- tmp
) {
2517 ring_supply_data(&netoring
, tmp
, cp
-tmp
);
2518 printsub('>', tmp
+2, cp
- tmp
- 2);
2520 ++want_status_response
;
2528 printoption("SENT", IAC
, IP
);
2531 /* Ignore return as we're ending off anyway. */
2542 NET2ADD(IAC
, BREAK
);
2543 printoption("SENT", IAC
, BREAK
);
2546 if (doflush() == -1)
2558 NET2ADD(IAC
, ABORT
);
2559 printoption("SENT", IAC
, ABORT
);
2563 * Since sendabort() gets called while aborting,
2564 * ignore the doflush() return
2577 printoption("SENT", IAC
, SUSP
);
2580 if (doflush() == -1) {
2581 /* The following will not return. */
2582 fatal_tty_error("write");
2594 printoption("SENT", IAC
, xEOF
);
2598 * Send a window size update to the remote system.
2604 unsigned short rows
, cols
;
2605 unsigned char tmp
[16];
2606 register unsigned char *cp
;
2608 if (my_state_is_wont(TELOPT_NAWS
))
2611 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2612 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2614 if (TerminalWindowSize(&rows
, &cols
) == 0) { /* Failed */
2622 *cp
++ = TELOPT_NAWS
;
2627 if (NETROOM() >= cp
- tmp
) {
2628 ring_supply_data(&netoring
, tmp
, cp
-tmp
);
2629 printsub('>', tmp
+2, cp
- tmp
- 2);
2634 tel_enter_binary(rw
)
2638 send_do(TELOPT_BINARY
, 1);
2640 send_will(TELOPT_BINARY
, 1);
2644 tel_leave_binary(rw
)
2648 send_dont(TELOPT_BINARY
, 1);
2650 send_wont(TELOPT_BINARY
, 1);