2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char sccsid
[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/contrib/telnet/telnet/telnet.c,v 1.16 2005/03/28 14:45:12 nectar Exp $");
42 #include <sys/types.h>
44 /* By the way, we need to include curses.h before telnet.h since,
45 * among other things, telnet.h #defines 'DO', which is a variable
46 * declared in curses.h.
55 #include <arpa/telnet.h>
70 #include <libtelnet/auth.h>
73 #include <libtelnet/encrypt.h>
75 #include <libtelnet/misc.h>
77 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
79 static unsigned char subbuffer
[SUBBUFSIZE
],
80 *subpointer
, *subend
; /* buffer for sub-options */
81 #define SB_CLEAR() subpointer = subbuffer;
82 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
83 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
84 *subpointer++ = (c); \
87 #define SB_GET() ((*subpointer++)&0xff)
88 #define SB_PEEK() ((*subpointer)&0xff)
89 #define SB_EOF() (subpointer >= subend)
90 #define SB_LEN() (subend - subpointer)
92 char options
[256]; /* The combined options */
93 char do_dont_resp
[256];
94 char will_wont_resp
[256];
98 autologin
= 0, /* Autologin anyone? */
102 ISend
, /* trying to send network data in */
105 netdata
, /* Print out network data flow */
106 crlf
, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
108 SYNCHing
, /* we are in TELNET SYNCH mode */
109 flushout
, /* flush output */
110 autoflush
= 0, /* flush output when interrupting? */
111 autosynch
, /* send interrupt characters with SYNCH? */
112 localflow
, /* we handle flow control locally */
113 restartany
, /* if flow control enabled, restart on any character */
114 localchars
, /* we recognize interrupt/quit */
115 donelclchars
, /* the user has set "localchars" */
116 donebinarytoggle
, /* the user has put us in binary */
117 dontlecho
, /* do we suppress local echoing right now? */
119 doaddrlookup
= 1, /* do a reverse address lookup? */
124 char *line
; /* hack around breakage in sra.c :-( !! */
129 #ifdef KLUDGELINEMODE
134 * Telnet receiver states for fsm
143 #define TS_SB 7 /* sub-option collection */
144 #define TS_SE 8 /* looking for sub-option end */
146 static int telrcv_state
;
148 unsigned char telopt_environ
= TELOPT_NEW_ENVIRON
;
150 # define telopt_environ TELOPT_NEW_ENVIRON
159 #ifdef KLUDGELINEMODE
160 int kludgelinemode
= 1;
163 static int is_unique(char *, char **, char **);
166 * The following are some clocks used to decide how to interpret
167 * the relationship between various variables.
173 * Initialize telnet environment.
184 connected
= ISend
= localflow
= donebinarytoggle
= 0;
185 #ifdef AUTHENTICATION
187 auth_encrypt_connect(connected
);
194 /* Don't change NetTrace */
196 escape
= CONTROL(']');
197 rlogin
= _POSIX_VDISABLE
;
198 #ifdef KLUDGELINEMODE
199 echoc
= CONTROL('E');
203 telrcv_state
= TS_DATA
;
208 * These routines are in charge of sending option negotiations
211 * The basic idea is that we send the negotiation if either side
212 * is in disagreement as to what the current state should be.
216 send_do(int c
, int init
)
219 if (((do_dont_resp
[c
] == 0) && my_state_is_do(c
)) ||
220 my_want_state_is_do(c
))
222 set_my_want_state_do(c
);
229 printoption("SENT", DO
, c
);
233 send_dont(int c
, int init
)
236 if (((do_dont_resp
[c
] == 0) && my_state_is_dont(c
)) ||
237 my_want_state_is_dont(c
))
239 set_my_want_state_dont(c
);
246 printoption("SENT", DONT
, c
);
250 send_will(int c
, int init
)
253 if (((will_wont_resp
[c
] == 0) && my_state_is_will(c
)) ||
254 my_want_state_is_will(c
))
256 set_my_want_state_will(c
);
263 printoption("SENT", WILL
, c
);
267 send_wont(int c
, int init
)
270 if (((will_wont_resp
[c
] == 0) && my_state_is_wont(c
)) ||
271 my_want_state_is_wont(c
))
273 set_my_want_state_wont(c
);
280 printoption("SENT", WONT
, c
);
284 willoption(int option
)
286 int new_state_ok
= 0;
288 if (do_dont_resp
[option
]) {
289 --do_dont_resp
[option
];
290 if (do_dont_resp
[option
] && my_state_is_do(option
))
291 --do_dont_resp
[option
];
294 if ((do_dont_resp
[option
] == 0) && my_want_state_is_dont(option
)) {
301 settimer(modenegotiated
);
304 #ifdef AUTHENTICATION
305 case TELOPT_AUTHENTICATION
:
309 #endif /* ENCRYPTION */
317 * Special case for TM. If we get back a WILL,
318 * pretend we got back a WONT.
320 set_my_want_state_dont(option
);
321 set_my_state_dont(option
);
322 return; /* Never reply to TM will's/wont's */
324 case TELOPT_LINEMODE
:
330 set_my_want_state_do(option
);
332 setconnmode(0); /* possibly set new tty mode */
334 do_dont_resp
[option
]++;
335 send_dont(option
, 0);
338 set_my_state_do(option
);
340 if (option
== TELOPT_ENCRYPT
)
341 encrypt_send_support();
342 #endif /* ENCRYPTION */
346 wontoption(int option
)
348 if (do_dont_resp
[option
]) {
349 --do_dont_resp
[option
];
350 if (do_dont_resp
[option
] && my_state_is_dont(option
))
351 --do_dont_resp
[option
];
354 if ((do_dont_resp
[option
] == 0) && my_want_state_is_do(option
)) {
358 #ifdef KLUDGELINEMODE
365 settimer(modenegotiated
);
371 set_my_want_state_dont(option
);
372 set_my_state_dont(option
);
373 return; /* Never reply to TM will's/wont's */
378 set_my_want_state_dont(option
);
379 if (my_state_is_do(option
))
380 send_dont(option
, 0);
381 setconnmode(0); /* Set new tty mode */
382 } else if (option
== TELOPT_TM
) {
384 * Special case for TM.
388 set_my_want_state_dont(option
);
390 set_my_state_dont(option
);
396 int new_state_ok
= 0;
398 if (will_wont_resp
[option
]) {
399 --will_wont_resp
[option
];
400 if (will_wont_resp
[option
] && my_state_is_will(option
))
401 --will_wont_resp
[option
];
404 if (will_wont_resp
[option
] == 0) {
405 if (my_want_state_is_wont(option
)) {
411 * Special case for TM. We send a WILL, but pretend
414 send_will(option
, 0);
415 set_my_want_state_wont(TELOPT_TM
);
416 set_my_state_wont(TELOPT_TM
);
419 case TELOPT_BINARY
: /* binary mode */
420 case TELOPT_NAWS
: /* window size */
421 case TELOPT_TSPEED
: /* terminal speed */
422 case TELOPT_LFLOW
: /* local flow control */
423 case TELOPT_TTYPE
: /* terminal type option */
424 case TELOPT_SGA
: /* no big deal */
426 case TELOPT_ENCRYPT
: /* encryption variable option */
427 #endif /* ENCRYPTION */
431 case TELOPT_NEW_ENVIRON
: /* New environment variable option */
433 if (my_state_is_will(TELOPT_OLD_ENVIRON
))
434 send_wont(TELOPT_OLD_ENVIRON
, 1); /* turn off the old */
436 case TELOPT_OLD_ENVIRON
: /* Old environment variable option */
437 if (my_state_is_will(TELOPT_NEW_ENVIRON
))
438 break; /* Don't enable if new one is in use! */
440 telopt_environ
= option
;
445 #ifdef AUTHENTICATION
446 case TELOPT_AUTHENTICATION
:
452 case TELOPT_XDISPLOC
: /* X Display location */
453 if (env_getvalue((const unsigned char *)"DISPLAY
"))
457 case TELOPT_LINEMODE:
458 #ifdef KLUDGELINEMODE
460 send_do(TELOPT_SGA, 1);
462 set_my_want_state_will(TELOPT_LINEMODE);
463 send_will(option, 0);
464 set_my_state_will(TELOPT_LINEMODE);
468 case TELOPT_ECHO: /* We're never going to echo... */
474 set_my_want_state_will(option);
475 send_will(option, 0);
476 setconnmode(0); /* Set new tty mode */
478 will_wont_resp[option]++;
479 send_wont(option, 0);
483 * Handle options that need more things done after the
484 * other side has acknowledged the option.
487 case TELOPT_LINEMODE:
488 #ifdef KLUDGELINEMODE
490 send_do(TELOPT_SGA, 1);
492 set_my_state_will(option);
494 send_do(TELOPT_SGA, 0);
499 set_my_state_will(option);
503 dontoption(int option)
506 if (will_wont_resp[option]) {
507 --will_wont_resp[option];
508 if (will_wont_resp[option] && my_state_is_wont(option))
509 --will_wont_resp[option];
512 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
514 case TELOPT_LINEMODE:
515 linemode = 0; /* put us back to the default state */
518 case TELOPT_NEW_ENVIRON:
520 * The new environ option wasn't recognized, try
523 send_will(TELOPT_OLD_ENVIRON, 1);
524 telopt_environ = TELOPT_OLD_ENVIRON;
528 /* we always accept a DONT */
529 set_my_want_state_wont(option);
530 if (my_state_is_will(option))
531 send_wont(option, 0);
532 setconnmode(0); /* Set new tty mode */
534 set_my_state_wont(option);
538 * Given a buffer returned by tgetent(), this routine will turn
539 * the pipe separated list of names in the buffer into an array
540 * of pointers to null terminated names. We toss out any bad,
541 * duplicate, or verbose names (names with spaces).
544 static const char *name_unknown = "UNKNOWN
";
545 static const char *unknown[] = { NULL, NULL };
548 mklist(char *buf, char *name)
551 char c, *cp, **argvp, *cp2, **argv, **avt;
554 if (strlen(name) > 40) {
556 unknown[0] = name_unknown;
562 unknown[0] = name_unknown;
564 * Count up the number of names.
566 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
571 * Allocate an array to put the name pointers into
573 argv = (char **)malloc((n+3)*sizeof(char *));
578 * Fill up the array of pointers to names.
583 for (cp = cp2 = buf; (c = *cp); cp++) {
584 if (c == '|' || c == ':') {
587 * Skip entries that have spaces or are over 40
588 * characters long. If this is our environment
589 * name, then put it up front. Otherwise, as
590 * long as this is not a duplicate name (case
591 * insensitive) add it to the list.
593 if (n || (cp - cp2 > 41))
595 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
597 else if (is_unique(cp2, argv+1, argvp))
602 * Skip multiple delimiters. Reset cp2 to
603 * the beginning of the next name. Reset n,
604 * the flag for names with spaces.
606 while ((c = *cp) == '|')
612 * Skip entries with spaces or non-ascii values.
613 * Convert lower case letters to upper case.
615 if ((c == ' ') || !isascii(c))
622 * Check for an old V6 2 character name. If the second
623 * name points to the beginning of the buffer, and is
624 * only 2 characters long, move it to the end of the array.
626 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
628 for (avt = &argv[1]; avt < argvp; avt++)
634 * Duplicate last name, for TTYPE option, and null
635 * terminate the array. If we didn't find a match on
636 * our terminal name, put that name at the beginning.
647 for (avt = argv; avt < argvp; avt++)
652 return((const char **)argv);
658 is_unique(char *name, char **as, char **ae)
663 n = strlen(name) + 1;
664 for (ap = as; ap < ae; ap++)
665 if (strncasecmp(*ap, name, n) == 0)
675 __private_extern__ int
679 setupterm(char *tname, int fd, int *errp)
681 if (tgetent(termbuf, tname) == 1) {
682 termbuf[1023] = '\0';
692 #define termbuf ttytype
693 extern char ttytype[];
696 int resettermname = 1;
702 static const char **tnamep = 0;
703 static const char **next;
708 if (tnamep && tnamep != unknown)
710 if ((tname = (char *)env_getvalue((const unsigned char *)"TERM
")) &&
711 (setupterm(tname, 1, &err) == 0)) {
712 tnamep = mklist(termbuf, tname);
714 if (tname && (strlen(tname) <= 40)) {
718 unknown[0] = name_unknown;
730 * Look at the sub-option buffer, and try to be helpful to the other
733 * Currently we recognize:
735 * Terminal type, send request.
736 * Terminal speed (send request).
737 * Local flow control (is request).
744 unsigned char subchar;
746 printsub('<', subbuffer, SB_LEN()+2);
747 switch (subchar = SB_GET()) {
749 if (my_want_state_is_wont(TELOPT_TTYPE))
751 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
755 unsigned char temp[50];
758 name = gettermname();
759 len = strlen(name) + 4 + 2;
760 if (len < NETROOM()) {
761 sprintf((char *)temp, "%c%c%c%c%s%c%c
", IAC, SB, TELOPT_TTYPE,
762 TELQUAL_IS, name, IAC, SE);
763 ring_supply_data(&netoring, temp, len);
764 printsub('>', &temp[2], len-2);
766 ExitString("No room in buffer
for terminal type
.\n", 1);
772 if (my_want_state_is_wont(TELOPT_TSPEED))
776 if (SB_GET() == TELQUAL_SEND) {
778 unsigned char temp[50];
781 TerminalSpeeds(&ispeed, &ospeed);
783 sprintf((char *)temp, "%c%c%c%c%ld
,%ld%c%c
", IAC, SB, TELOPT_TSPEED,
784 TELQUAL_IS, ospeed, ispeed, IAC, SE);
785 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
787 if (len < NETROOM()) {
788 ring_supply_data(&netoring, temp, len);
789 printsub('>', temp+2, len - 2);
791 /*@*/ else printf("lm_will
: not enough room in buffer
\n");
795 if (my_want_state_is_wont(TELOPT_LFLOW))
800 case LFLOW_RESTART_ANY:
803 case LFLOW_RESTART_XON:
819 case TELOPT_LINEMODE:
820 if (my_want_state_is_wont(TELOPT_LINEMODE))
826 lm_will(subpointer, SB_LEN());
829 lm_wont(subpointer, SB_LEN());
832 lm_do(subpointer, SB_LEN());
835 lm_dont(subpointer, SB_LEN());
838 slc(subpointer, SB_LEN());
841 lm_mode(subpointer, SB_LEN(), 0);
849 case TELOPT_OLD_ENVIRON:
851 case TELOPT_NEW_ENVIRON:
857 if (my_want_state_is_dont(subchar))
861 if (my_want_state_is_wont(subchar)) {
868 env_opt(subpointer, SB_LEN());
871 case TELOPT_XDISPLOC:
872 if (my_want_state_is_wont(TELOPT_XDISPLOC))
876 if (SB_GET() == TELQUAL_SEND) {
877 unsigned char temp[50], *dp;
880 if ((dp = env_getvalue((const unsigned char *)"DISPLAY
")) == NULL ||
881 strlen((const char *)dp) > sizeof(temp) - 7) {
883 * Something happened, we no longer have a DISPLAY
884 * variable. Or it is too long. So, turn off the option.
886 send_wont(TELOPT_XDISPLOC, 1);
889 snprintf((char *)temp, sizeof(temp), "%c%c%c%c%s%c%c
", IAC, SB,
890 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
891 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
893 if (len < NETROOM()) {
894 ring_supply_data(&netoring, temp, len);
895 printsub('>', temp+2, len - 2);
897 /*@*/ else printf("lm_will
: not enough room in buffer
\n");
901 #ifdef AUTHENTICATION
902 case TELOPT_AUTHENTICATION: {
909 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
911 auth_is(subpointer, SB_LEN());
914 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
916 auth_send(subpointer, SB_LEN());
919 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
921 auth_reply(subpointer, SB_LEN());
924 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
926 auth_name(subpointer, SB_LEN());
938 if (my_want_state_is_dont(TELOPT_ENCRYPT))
940 encrypt_start(subpointer, SB_LEN());
943 if (my_want_state_is_dont(TELOPT_ENCRYPT))
947 case ENCRYPT_SUPPORT:
948 if (my_want_state_is_wont(TELOPT_ENCRYPT))
950 encrypt_support(subpointer, SB_LEN());
952 case ENCRYPT_REQSTART:
953 if (my_want_state_is_wont(TELOPT_ENCRYPT))
955 encrypt_request_start(subpointer, SB_LEN());
958 if (my_want_state_is_wont(TELOPT_ENCRYPT))
961 * We can always send an REQEND so that we cannot
962 * get stuck encrypting. We should only get this
963 * if we have been able to get in the correct mode
966 encrypt_request_end();
969 if (my_want_state_is_dont(TELOPT_ENCRYPT))
971 encrypt_is(subpointer, SB_LEN());
974 if (my_want_state_is_wont(TELOPT_ENCRYPT))
976 encrypt_reply(subpointer, SB_LEN());
978 case ENCRYPT_ENC_KEYID:
979 if (my_want_state_is_dont(TELOPT_ENCRYPT))
981 encrypt_enc_keyid(subpointer, SB_LEN());
983 case ENCRYPT_DEC_KEYID:
984 if (my_want_state_is_wont(TELOPT_ENCRYPT))
986 encrypt_dec_keyid(subpointer, SB_LEN());
992 #endif /* ENCRYPTION */
998 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
1001 lm_will(unsigned char *cmd, int len)
1004 /*@*/ printf("lm_will
: no command
!!!\n"); /* Should not happen... */
1008 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1012 if (NETROOM() > (int)sizeof(str_lm)) {
1013 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1014 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1016 /*@*/ else printf("lm_will
: not enough room in buffer
\n");
1022 lm_wont(unsigned char *cmd, int len)
1025 /*@*/ printf("lm_wont
: no command
!!!\n"); /* Should not happen... */
1029 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1031 /* We are always DONT, so don't respond */
1037 lm_do(unsigned char *cmd, int len)
1040 /*@*/ printf("lm_do
: no command
!!!\n"); /* Should not happen... */
1044 case LM_FORWARDMASK:
1048 if (NETROOM() > (int)sizeof(str_lm)) {
1049 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1050 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1052 /*@*/ else printf("lm_do
: not enough room in buffer
\n");
1058 lm_dont(unsigned char *cmd, int len)
1061 /*@*/ printf("lm_dont
: no command
!!!\n"); /* Should not happen... */
1065 case LM_FORWARDMASK:
1067 /* we are always WONT, so don't respond */
1072 static unsigned char str_lm_mode[] = {
1073 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1077 lm_mode(unsigned char *cmd, int len, int init)
1081 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1085 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1086 str_lm_mode[4] = linemode;
1088 str_lm_mode[4] |= MODE_ACK;
1089 if (NETROOM() > (int)sizeof(str_lm_mode)) {
1090 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1091 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1093 /*@*/ else printf("lm_mode
: not enough room in buffer
\n");
1094 setconnmode(0); /* set changed mode */
1101 * Handle special character suboption of LINEMODE.
1107 char flags; /* Current flags & level */
1108 char mylevel; /* Maximum level & flags */
1111 #define SLC_IMPORT 0
1112 #define SLC_EXPORT 1
1113 #define SLC_RVALUE 2
1114 static int slc_mode = SLC_EXPORT;
1122 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1125 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1128 #define initfunc(func, flags) { \
1129 spcp = &spc_data[func]; \
1130 if ((spcp->valp = tcval(func))) { \
1131 spcp->val = *spcp->valp; \
1132 spcp->mylevel = SLC_VARIABLE|flags; \
1135 spcp->mylevel = SLC_DEFAULT; \
1139 initfunc(SLC_SYNCH, 0);
1141 initfunc(SLC_AO, 0);
1142 initfunc(SLC_AYT, 0);
1144 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1145 initfunc(SLC_EOF, 0);
1147 initfunc(SLC_SUSP, SLC_FLUSHIN);
1149 initfunc(SLC_EC, 0);
1150 initfunc(SLC_EL, 0);
1152 initfunc(SLC_EW, 0);
1153 initfunc(SLC_RP, 0);
1154 initfunc(SLC_LNEXT, 0);
1156 initfunc(SLC_XON, 0);
1157 initfunc(SLC_XOFF, 0);
1159 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1160 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1162 initfunc(SLC_FORW1, 0);
1164 initfunc(SLC_FORW2, 0);
1168 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1171 if (slc_mode == SLC_EXPORT)
1181 printf("Special characters are %s values
\n",
1182 slc_mode == SLC_IMPORT ? "remote
default" :
1183 slc_mode == SLC_EXPORT ? "local
" :
1188 slc_mode_export(void)
1190 slc_mode = SLC_EXPORT;
1191 if (my_state_is_will(TELOPT_LINEMODE))
1196 slc_mode_import(int def)
1198 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1199 if (my_state_is_will(TELOPT_LINEMODE))
1203 unsigned char slc_import_val[] = {
1204 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1206 unsigned char slc_import_def[] = {
1207 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1213 if (NETROOM() > (int)sizeof(slc_import_val)) {
1215 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1216 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1218 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1219 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1222 /*@*/ else printf("slc_import
: not enough room
\n");
1230 TerminalDefaultChars();
1233 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1234 if (spcp->mylevel != SLC_NOSUPPORT) {
1235 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1236 spcp->flags = SLC_NOSUPPORT;
1238 spcp->flags = spcp->mylevel;
1240 spcp->val = *spcp->valp;
1241 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1246 setconnmode(1); /* Make sure the character values are set */
1250 slc(unsigned char *cp, int len)
1257 for (; len >= 3; len -=3, cp +=3) {
1259 func = cp[SLC_FUNC];
1263 * Client side: always ignore 0 function.
1268 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1269 slc_add_reply(func, SLC_NOSUPPORT, 0);
1273 spcp = &spc_data[func];
1275 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1277 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1278 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1282 if (level == (SLC_DEFAULT|SLC_ACK)) {
1284 * This is an error condition, the SLC_ACK
1285 * bit should never be set for the SLC_DEFAULT
1286 * level. Our best guess to recover is to
1287 * ignore the SLC_ACK bit.
1289 cp[SLC_FLAGS] &= ~SLC_ACK;
1292 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1293 spcp->val = (cc_t)cp[SLC_VALUE];
1294 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1300 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1301 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1302 spcp->val = (cc_t)cp[SLC_VALUE];
1304 if (level == SLC_DEFAULT) {
1305 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1306 spcp->flags = spcp->mylevel;
1308 spcp->flags = SLC_NOSUPPORT;
1310 slc_add_reply(func, spcp->flags, spcp->val);
1314 setconnmode(1); /* set the new character values */
1323 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1324 if (spcp->valp && spcp->val != *spcp->valp) {
1325 spcp->val = *spcp->valp;
1326 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1327 spcp->flags = SLC_NOSUPPORT;
1329 spcp->flags = spcp->mylevel;
1330 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1337 unsigned char slc_reply[128];
1338 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1339 unsigned char *slc_replyp;
1342 slc_start_reply(void)
1344 slc_replyp = slc_reply;
1345 *slc_replyp++ = IAC;
1347 *slc_replyp++ = TELOPT_LINEMODE;
1348 *slc_replyp++ = LM_SLC;
1352 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1354 /* A sequence of up to 6 bytes my be written for this member of the SLC
1355 * suboption list by this function. The end of negotiation command,
1356 * which is written by slc_end_reply(), will require 2 additional
1357 * bytes. Do not proceed unless there is sufficient space for these
1360 if (&slc_replyp[6+2] > slc_reply_eom)
1362 if ((*slc_replyp++ = func) == IAC)
1363 *slc_replyp++ = IAC;
1364 if ((*slc_replyp++ = flags) == IAC)
1365 *slc_replyp++ = IAC;
1366 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1367 *slc_replyp++ = IAC;
1375 /* The end of negotiation command requires 2 bytes. */
1376 if (&slc_replyp[2] > slc_reply_eom)
1378 *slc_replyp++ = IAC;
1380 len = slc_replyp - slc_reply;
1383 if (NETROOM() > len) {
1384 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1385 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1387 /*@*/else printf("slc_end_reply
: not enough room
\n");
1394 int need_update = 0;
1396 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1397 if (!(spcp->flags&SLC_ACK))
1399 spcp->flags &= ~SLC_ACK;
1400 if (spcp->valp && (*spcp->valp != spcp->val)) {
1401 *spcp->valp = spcp->val;
1405 return(need_update);
1411 * Earlier version of telnet/telnetd from the BSD code had
1412 * the definitions of VALUE and VAR reversed. To ensure
1413 * maximum interoperability, we assume that the server is
1414 * an older BSD server, until proven otherwise. The newer
1415 * BSD servers should be able to handle either definition,
1416 * so it is better to use the wrong values if we don't
1417 * know what type of server it is.
1420 int old_env_var = OLD_ENV_VAR;
1421 int old_env_value = OLD_ENV_VALUE;
1423 # define old_env_var OLD_ENV_VAR
1424 # define old_env_value OLD_ENV_VALUE
1429 env_opt(unsigned char *buf, int len)
1431 unsigned char *ep = 0, *epc = 0;
1434 switch(buf[0]&0xff) {
1439 } else for (i = 1; i < len; i++) {
1440 switch (buf[i]&0xff) {
1444 if (telopt_environ == TELOPT_OLD_ENVIRON
1446 /* Server has the same definitions */
1447 old_env_var = OLD_ENV_VAR;
1448 old_env_value = OLD_ENV_VALUE;
1454 * Although OLD_ENV_VALUE is not legal, we will
1455 * still recognize it, just in case it is an
1456 * old server that has VAR & VALUE mixed up...
1467 ep = epc = &buf[i+1];
1487 /* Ignore for now. We shouldn't get it anyway. */
1495 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1496 unsigned char *opt_reply = NULL;
1497 unsigned char *opt_replyp;
1498 unsigned char *opt_replyend;
1504 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1506 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1507 if (opt_reply == NULL) {
1508 /*@*/ printf("env_opt_start
: malloc()/realloc() failed
!!!\n");
1509 opt_reply = opt_replyp = opt_replyend = NULL;
1512 opt_replyp = opt_reply;
1513 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1514 *opt_replyp++ = IAC;
1516 *opt_replyp++ = telopt_environ;
1517 *opt_replyp++ = TELQUAL_IS;
1521 env_opt_start_info(void)
1525 opt_replyp[-1] = TELQUAL_INFO;
1529 env_opt_add(unsigned char *ep)
1531 unsigned char *vp, c;
1533 if (opt_reply == NULL) /*XXX*/
1536 if (ep == NULL || *ep == '\0') {
1537 /* Send user defined variables first. */
1539 while ((ep = env_default(0, 0)))
1542 /* Now add the list of well know variables. */
1544 while ((ep = env_default(0, 1)))
1548 vp = env_getvalue(ep);
1549 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1550 2 * strlen((char *)ep) + 6 > opt_replyend)
1554 // rdar://problem/4022837
1555 opt_replyend += OPT_REPLY_SIZE + 2*strlen((char *)ep) + 2*(vp ? strlen((char *)vp) : 0);
1557 opt_replyend += OPT_REPLY_SIZE;
1559 len = opt_replyend - opt_reply;
1560 opt_reply = (unsigned char *)realloc(opt_reply, len);
1561 if (opt_reply == NULL) {
1562 /*@*/ printf("env_opt_add
: realloc() failed
!!!\n");
1563 opt_reply = opt_replyp = opt_replyend = NULL;
1566 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1567 opt_replyend = opt_reply + len;
1569 if (opt_welldefined((const char *)ep))
1571 if (telopt_environ == TELOPT_OLD_ENVIRON)
1572 *opt_replyp++ = old_env_var;
1575 *opt_replyp++ = NEW_ENV_VAR;
1577 *opt_replyp++ = ENV_USERVAR;
1579 while ((c = *ep++)) {
1580 if (opt_replyp + (2 + 2) > opt_replyend)
1584 *opt_replyp++ = IAC;
1590 *opt_replyp++ = ENV_ESC;
1596 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1599 if (telopt_environ == TELOPT_OLD_ENVIRON)
1600 *opt_replyp++ = old_env_value;
1603 *opt_replyp++ = NEW_ENV_VALUE;
1611 opt_welldefined(const char *ep)
1613 if ((strcmp(ep, "USER
") == 0) ||
1614 (strcmp(ep, "DISPLAY
") == 0) ||
1615 (strcmp(ep, "PRINTER
") == 0) ||
1616 (strcmp(ep, "SYSTEMTYPE
") == 0) ||
1617 (strcmp(ep, "JOB
") == 0) ||
1618 (strcmp(ep, "ACCT
") == 0))
1624 env_opt_end(int emptyok)
1628 if (opt_replyp + 2 > opt_replyend)
1630 len = opt_replyp + 2 - opt_reply;
1631 if (emptyok || len > 6) {
1632 *opt_replyp++ = IAC;
1634 if (NETROOM() > len) {
1635 ring_supply_data(&netoring, opt_reply, len);
1636 printsub('>', &opt_reply[2], len - 2);
1638 /*@*/ else printf("slc_end_reply
: not enough room
\n");
1642 opt_reply = opt_replyp = opt_replyend = NULL;
1653 unsigned char *sbp = NULL;
1655 int returnValue = 0;
1659 while (TTYROOM() > 2) {
1662 ring_consumed(&netiring, count);
1666 sbp = netiring.consume;
1667 scc = ring_full_consecutive(&netiring);
1669 /* No more data coming in */
1674 c = *sbp++ & 0xff, scc--; count++;
1677 c = (*decrypt_input)(c);
1678 #endif /* ENCRYPTION */
1680 switch (telrcv_state) {
1683 telrcv_state = TS_DATA;
1685 break; /* Ignore \0 after CR */
1687 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1694 if (c == IAC && telnetport >= 0) {
1695 telrcv_state = TS_IAC;
1699 * The 'crmod' hack (see following) is needed
1700 * since we can't * set CRMOD on output only.
1701 * Machines like MULTICS like to send \r without
1702 * \n; since we must turn off CRMOD to get proper
1703 * input, the mapping is done here (sigh).
1705 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1710 c = (*decrypt_input)(c);
1711 #endif /* ENCRYPTION */
1713 sbp++, scc--; count++;
1716 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1718 sbp++, scc--; count++;
1723 (*decrypt_input)(-1);
1724 #endif /* ENCRYPTION */
1732 telrcv_state = TS_CR;
1748 telrcv_state = TS_WILL;
1752 telrcv_state = TS_WONT;
1756 telrcv_state = TS_DO;
1760 telrcv_state = TS_DONT;
1765 * We may have missed an urgent notification,
1766 * so make sure we flush whatever is in the
1769 printoption("RCVD
", IAC, DM);
1772 SYNCHing = stilloob();
1778 telrcv_state = TS_SB;
1788 printoption("RCVD
", IAC, c);
1791 telrcv_state = TS_DATA;
1795 printoption("RCVD
", WILL, c);
1797 telrcv_state = TS_DATA;
1801 printoption("RCVD
", WONT, c);
1803 telrcv_state = TS_DATA;
1807 printoption("RCVD
", DO, c);
1809 if (c == TELOPT_NAWS) {
1811 } else if (c == TELOPT_LFLOW) {
1816 telrcv_state = TS_DATA;
1820 printoption("RCVD
", DONT, c);
1823 setconnmode(0); /* set new tty mode (maybe) */
1824 telrcv_state = TS_DATA;
1829 telrcv_state = TS_SE;
1839 * This is an error. We only expect to get
1840 * "IAC IAC
" or "IAC SE
". Several things may
1841 * have happend. An IAC was not doubled, the
1842 * IAC SE was left off, or another option got
1843 * inserted into the suboption are all possibilities.
1844 * If we assume that the IAC was not doubled,
1845 * and really the IAC SE was left off, we could
1846 * get into an infinate loop here. So, instead,
1847 * we terminate the suboption, and process the
1848 * partial suboption if we can.
1855 printoption("In SUBOPTION processing
, RCVD
", IAC, c);
1856 suboption(); /* handle sub-option */
1857 telrcv_state = TS_IAC;
1861 telrcv_state = TS_SB;
1867 suboption(); /* handle sub-option */
1868 telrcv_state = TS_DATA;
1873 ring_consumed(&netiring, count);
1874 return returnValue||count;
1877 static int bol = 1, local = 0;
1885 command(0, "z
\n", 2);
1896 int returnValue = 0;
1897 unsigned char *tbp = NULL;
1901 while (NETROOM() > 2) {
1907 ring_consumed(&ttyiring, count);
1911 tbp = ttyiring.consume;
1912 tcc = ring_full_consecutive(&ttyiring);
1917 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1918 if (rlogin != _POSIX_VDISABLE) {
1927 if (sc == '.' || c == termEofChar) {
1929 command(0, "close
\n", 6);
1932 if (sc == termSuspChar) {
1934 command(0, "z
\n", 2);
1938 command(0, (const char *)tbp, tcc);
1952 if ((sc == '\n') || (sc == '\r'))
1954 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1956 * Double escape is a pass through of a single escape character.
1958 if (tcc && strip(*tbp) == escape) {
1964 command(0, (char *)tbp, tcc);
1973 #ifdef KLUDGELINEMODE
1974 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1975 if (tcc > 0 && strip(*tbp) == echoc) {
1976 tcc--; tbp++; count++;
1978 dontlecho = !dontlecho;
1979 settimer(echotoggle);
1986 if (MODE_LOCAL_CHARS(globalmode)) {
1987 if (TerminalSpecialChars(sc) == 0) {
1992 if (my_want_state_is_wont(TELOPT_BINARY)) {
1996 * If we are in CRMOD mode (\r ==> \n)
1997 * on our local machine, then probably
1998 * a newline (unix) is CRLF (TELNET).
2000 if (MODE_LOCAL_CHARS(globalmode)) {
2004 bol = flushline = 1;
2008 NET2ADD('\r', '\0');
2010 NET2ADD('\r', '\n');
2012 bol = flushline = 1;
2021 } else if (c == IAC) {
2028 ring_consumed(&ttyiring, count);
2029 return returnValue||count; /* Non-zero if we did anything */
2035 * Try to do something.
2037 * If we do something useful, return 1; else return 0.
2042 Scheduler(int block)
2044 /* One wants to be a bit careful about setting returnValue
2045 * to one, since a one implies we did some useful work,
2046 * and therefore probably won't be called to block next
2049 int netin, netout, netex, ttyin, ttyout;
2051 /* Decide which rings should be processed */
2053 netout = ring_full_count(&netoring) &&
2055 (my_want_state_is_wont(TELOPT_LINEMODE)
2056 #ifdef KLUDGELINEMODE
2057 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2060 my_want_state_is_will(TELOPT_BINARY));
2061 ttyout = ring_full_count(&ttyoring);
2063 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2065 netin = !ISend && ring_empty_count(&netiring);
2069 /* Call to system code to process rings */
2071 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2073 /* Now, look at the input rings, looking for work to do. */
2075 if (ring_full_count(&ttyiring)) {
2076 returnValue |= telsnd();
2079 if (ring_full_count(&netiring)) {
2080 returnValue |= telrcv();
2085 #ifdef AUTHENTICATION
2086 #define __unusedhere
2088 #define __unusedhere __unused
2091 * Select from tty and network...
2094 telnet(char *user __unusedhere)
2098 #ifdef AUTHENTICATION
2101 static char local_host[256] = { 0 };
2103 if (!local_host[0]) {
2104 gethostname(local_host, sizeof(local_host));
2105 local_host[sizeof(local_host)-1] = 0;
2107 auth_encrypt_init(local_host, hostname, "TELNET
", 0);
2108 auth_encrypt_user(user);
2112 if (telnetport > 0) {
2113 #ifdef AUTHENTICATION
2115 send_will(TELOPT_AUTHENTICATION, 1);
2118 send_do(TELOPT_ENCRYPT, 1);
2119 send_will(TELOPT_ENCRYPT, 1);
2120 #endif /* ENCRYPTION */
2121 send_do(TELOPT_SGA, 1);
2122 send_will(TELOPT_TTYPE, 1);
2123 send_will(TELOPT_NAWS, 1);
2124 send_will(TELOPT_TSPEED, 1);
2125 send_will(TELOPT_LFLOW, 1);
2126 send_will(TELOPT_LINEMODE, 1);
2127 send_will(TELOPT_NEW_ENVIRON, 1);
2128 send_do(TELOPT_STATUS, 1);
2129 if (env_getvalue((const unsigned char *)"DISPLAY
"))
2130 send_will(TELOPT_XDISPLOC, 1);
2132 tel_enter_binary(eight);
2138 while ((schedValue = Scheduler(0)) != 0) {
2139 if (schedValue == -1) {
2145 if (Scheduler(1) == -1) {
2152 #if 0 /* XXX - this not being in is a bug */
2156 * Return the address of the next "item
" in the TELNET data
2157 * stream. This will be the address of the next character if
2158 * the current address is a user data character, or it will
2159 * be the address of the character following the TELNET command
2160 * if the current address is a TELNET IAC ("I Am a Command
")
2165 nextitem(char *current)
2167 if ((*current&0xff) != IAC) {
2170 switch (*(current+1)&0xff) {
2176 case SB: /* loop forever looking for the SE */
2178 char *look = current+2;
2181 if ((*look++&0xff) == IAC) {
2182 if ((*look++&0xff) == SE) {
2197 * We are about to do a TELNET SYNCH operation. Clear
2198 * the path to the network.
2200 * Things are a bit tricky since we may have sent the first
2201 * byte or so of a previous TELNET command into the network.
2202 * So, we have to scan the network buffer from the beginning
2203 * until we are up to where we want to be.
2205 * A side effect of what we do, just to keep things
2206 * simple, is to clear the urgent data pointer. The principal
2207 * caller should be setting the urgent data pointer AFTER calling
2218 * These routines add various telnet commands to the data stream.
2228 (void) ttyflush(1); /* Flush/drop output */
2229 /* do printoption AFTER flush, otherwise the output gets tossed... */
2230 printoption("SENT
", DO, TELOPT_TM);
2237 printoption("SENT
", IAC, AO);
2247 printoption("SENT
", IAC, EL);
2254 printoption("SENT
", IAC, EC);
2258 dosynch(char *ch __unused)
2260 netclear(); /* clear the path to the network */
2264 printoption("SENT
", IAC, DM);
2268 int want_status_response = 0;
2271 get_status(char *ch __unused)
2273 unsigned char tmp[16];
2276 if (my_want_state_is_dont(TELOPT_STATUS)) {
2277 printf("Remote side does
not support STATUS option
\n");
2284 *cp++ = TELOPT_STATUS;
2285 *cp++ = TELQUAL_SEND;
2288 if (NETROOM() >= cp - tmp) {
2289 ring_supply_data(&netoring, tmp, cp-tmp);
2290 printsub('>', tmp+2, cp - tmp - 2);
2292 ++want_status_response;
2300 printoption("SENT
", IAC, IP);
2313 NET2ADD(IAC, BREAK);
2314 printoption("SENT
", IAC, BREAK);
2327 NET2ADD(IAC, ABORT);
2328 printoption("SENT
", IAC, ABORT);
2342 printoption("SENT
", IAC, SUSP);
2356 printoption("SENT
", IAC, xEOF);
2363 printoption("SENT
", IAC, AYT);
2367 * Send a window size update to the remote system.
2374 unsigned char tmp[16];
2377 if (my_state_is_wont(TELOPT_NAWS))
2380 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2381 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2383 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2391 *cp++ = TELOPT_NAWS;
2396 if (NETROOM() >= cp - tmp) {
2397 ring_supply_data(&netoring, tmp, cp-tmp);
2398 printsub('>', tmp+2, cp - tmp - 2);
2403 tel_enter_binary(int rw)
2406 send_do(TELOPT_BINARY, 1);
2408 send_will(TELOPT_BINARY, 1);
2412 tel_leave_binary(int rw)
2415 send_dont(TELOPT_BINARY, 1);
2417 send_wont(TELOPT_BINARY, 1);