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
33 * @(#)telnet.c 8.4 (Berkeley) 5/30/95
34 * $FreeBSD: src/crypto/telnet/telnet/telnet.c,v 1.4.2.5 2002/04/13 10:59:08 markm Exp $
37 #include <sys/types.h>
39 /* By the way, we need to include curses.h before telnet.h since,
40 * among other things, telnet.h #defines 'DO', which is a variable
41 * declared in curses.h.
50 #include <arpa/telnet.h>
60 #include <libtelnet/auth.h>
63 #include <libtelnet/encrypt.h>
65 #include <libtelnet/misc.h>
67 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
69 static unsigned char subbuffer[SUBBUFSIZE],
70 *subpointer, *subend; /* buffer for sub-options */
71 #define SB_CLEAR() subpointer = subbuffer;
72 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
73 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
74 *subpointer++ = (c); \
77 #define SB_GET() ((*subpointer++)&0xff)
78 #define SB_PEEK() ((*subpointer)&0xff)
79 #define SB_EOF() (subpointer >= subend)
80 #define SB_LEN() (subend - subpointer)
82 char options[256]; /* The combined options */
83 char do_dont_resp[256];
84 char will_wont_resp[256];
88 autologin = 0, /* Autologin anyone? */
92 ISend, /* trying to send network data in */
95 netdata, /* Print out network data flow */
96 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
98 SYNCHing, /* we are in TELNET SYNCH mode */
99 flushout, /* flush output */
100 autoflush = 0, /* flush output when interrupting? */
101 autosynch, /* send interrupt characters with SYNCH? */
102 localflow, /* we handle flow control locally */
103 restartany, /* if flow control enabled, restart on any character */
104 localchars, /* we recognize interrupt/quit */
105 donelclchars, /* the user has set "localchars" */
106 donebinarytoggle, /* the user has put us in binary */
107 dontlecho, /* do we suppress local echoing right now? */
109 doaddrlookup = 1, /* do a reverse address lookup? */
114 char *line; /* hack around breakage in sra.c :-( !! */
119 #ifdef KLUDGELINEMODE
124 * Telnet receiver states for fsm
133 #define TS_SB 7 /* sub-option collection */
134 #define TS_SE 8 /* looking for sub-option end */
136 static int telrcv_state;
138 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
140 # define telopt_environ TELOPT_NEW_ENVIRON
149 #ifdef KLUDGELINEMODE
150 int kludgelinemode = 1;
153 static int is_unique(char *, char **, char **);
156 * The following are some clocks used to decide how to interpret
157 * the relationship between various variables.
163 * Initialize telnet environment.
174 connected = ISend = localflow = donebinarytoggle = 0;
175 #ifdef AUTHENTICATION
177 auth_encrypt_connect(connected);
184 /* Don't change NetTrace */
186 escape = CONTROL(']');
187 rlogin = _POSIX_VDISABLE;
188 #ifdef KLUDGELINEMODE
189 echoc = CONTROL('E');
193 telrcv_state = TS_DATA;
198 * These routines are in charge of sending option negotiations
201 * The basic idea is that we send the negotiation if either side
202 * is in disagreement as to what the current state should be.
206 send_do(int c, int init)
209 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
210 my_want_state_is_do(c))
212 set_my_want_state_do(c);
217 printoption("SENT", DO, c);
221 send_dont(int c, int init)
224 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
225 my_want_state_is_dont(c))
227 set_my_want_state_dont(c);
232 printoption("SENT", DONT, c);
236 send_will(int c, int init)
239 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
240 my_want_state_is_will(c))
242 set_my_want_state_will(c);
247 printoption("SENT", WILL, c);
251 send_wont(int c, int init)
254 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
255 my_want_state_is_wont(c))
257 set_my_want_state_wont(c);
262 printoption("SENT", WONT, c);
266 willoption(int option)
268 int new_state_ok = 0;
270 if (do_dont_resp[option]) {
271 --do_dont_resp[option];
272 if (do_dont_resp[option] && my_state_is_do(option))
273 --do_dont_resp[option];
276 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
283 settimer(modenegotiated);
286 #ifdef AUTHENTICATION
287 case TELOPT_AUTHENTICATION:
291 #endif /* ENCRYPTION */
299 * Special case for TM. If we get back a WILL,
300 * pretend we got back a WONT.
302 set_my_want_state_dont(option);
303 set_my_state_dont(option);
304 return; /* Never reply to TM will's/wont's */
306 case TELOPT_LINEMODE:
312 set_my_want_state_do(option);
314 setconnmode(0); /* possibly set new tty mode */
316 do_dont_resp[option]++;
317 send_dont(option, 0);
320 set_my_state_do(option);
322 if (option == TELOPT_ENCRYPT)
323 encrypt_send_support();
324 #endif /* ENCRYPTION */
328 wontoption(int option)
330 if (do_dont_resp[option]) {
331 --do_dont_resp[option];
332 if (do_dont_resp[option] && my_state_is_dont(option))
333 --do_dont_resp[option];
336 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
340 #ifdef KLUDGELINEMODE
347 settimer(modenegotiated);
353 set_my_want_state_dont(option);
354 set_my_state_dont(option);
355 return; /* Never reply to TM will's/wont's */
360 set_my_want_state_dont(option);
361 if (my_state_is_do(option))
362 send_dont(option, 0);
363 setconnmode(0); /* Set new tty mode */
364 } else if (option == TELOPT_TM) {
366 * Special case for TM.
370 set_my_want_state_dont(option);
372 set_my_state_dont(option);
378 int new_state_ok = 0;
380 if (will_wont_resp[option]) {
381 --will_wont_resp[option];
382 if (will_wont_resp[option] && my_state_is_will(option))
383 --will_wont_resp[option];
386 if (will_wont_resp[option] == 0) {
387 if (my_want_state_is_wont(option)) {
393 * Special case for TM. We send a WILL, but pretend
396 send_will(option, 0);
397 set_my_want_state_wont(TELOPT_TM);
398 set_my_state_wont(TELOPT_TM);
401 case TELOPT_BINARY: /* binary mode */
402 case TELOPT_NAWS: /* window size */
403 case TELOPT_TSPEED: /* terminal speed */
404 case TELOPT_LFLOW: /* local flow control */
405 case TELOPT_TTYPE: /* terminal type option */
406 case TELOPT_SGA: /* no big deal */
408 case TELOPT_ENCRYPT: /* encryption variable option */
409 #endif /* ENCRYPTION */
413 case TELOPT_NEW_ENVIRON: /* New environment variable option */
415 if (my_state_is_will(TELOPT_OLD_ENVIRON))
416 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
418 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
419 if (my_state_is_will(TELOPT_NEW_ENVIRON))
420 break; /* Don't enable if new one is in use! */
422 telopt_environ = option;
427 #ifdef AUTHENTICATION
428 case TELOPT_AUTHENTICATION:
434 case TELOPT_XDISPLOC: /* X Display location */
435 if (env_getvalue("DISPLAY"))
439 case TELOPT_LINEMODE:
440 #ifdef KLUDGELINEMODE
442 send_do(TELOPT_SGA, 1);
444 set_my_want_state_will(TELOPT_LINEMODE);
445 send_will(option, 0);
446 set_my_state_will(TELOPT_LINEMODE);
450 case TELOPT_ECHO: /* We're never going to echo... */
456 set_my_want_state_will(option);
457 send_will(option, 0);
458 setconnmode(0); /* Set new tty mode */
460 will_wont_resp[option]++;
461 send_wont(option, 0);
465 * Handle options that need more things done after the
466 * other side has acknowledged the option.
469 case TELOPT_LINEMODE:
470 #ifdef KLUDGELINEMODE
472 send_do(TELOPT_SGA, 1);
474 set_my_state_will(option);
476 send_do(TELOPT_SGA, 0);
481 set_my_state_will(option);
485 dontoption(int option)
488 if (will_wont_resp[option]) {
489 --will_wont_resp[option];
490 if (will_wont_resp[option] && my_state_is_wont(option))
491 --will_wont_resp[option];
494 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
496 case TELOPT_LINEMODE:
497 linemode = 0; /* put us back to the default state */
500 case TELOPT_NEW_ENVIRON:
502 * The new environ option wasn't recognized, try
505 send_will(TELOPT_OLD_ENVIRON, 1);
506 telopt_environ = TELOPT_OLD_ENVIRON;
510 /* we always accept a DONT */
511 set_my_want_state_wont(option);
512 if (my_state_is_will(option))
513 send_wont(option, 0);
514 setconnmode(0); /* Set new tty mode */
516 set_my_state_wont(option);
520 * Given a buffer returned by tgetent(), this routine will turn
521 * the pipe separated list of names in the buffer into an array
522 * of pointers to null terminated names. We toss out any bad,
523 * duplicate, or verbose names (names with spaces).
526 static const char *name_unknown = "UNKNOWN";
527 static const char *unknown[] = { NULL, NULL };
530 mklist(char *buf, char *name)
533 char c, *cp, **argvp, *cp2, **argv, **avt;
536 if (strlen(name) > 40) {
538 unknown[0] = name_unknown;
544 unknown[0] = name_unknown;
546 * Count up the number of names.
548 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
553 * Allocate an array to put the name pointers into
555 argv = (char **)malloc((n+3)*sizeof(char *));
560 * Fill up the array of pointers to names.
565 for (cp = cp2 = buf; (c = *cp); cp++) {
566 if (c == '|' || c == ':') {
569 * Skip entries that have spaces or are over 40
570 * characters long. If this is our environment
571 * name, then put it up front. Otherwise, as
572 * long as this is not a duplicate name (case
573 * insensitive) add it to the list.
575 if (n || (cp - cp2 > 41))
577 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
579 else if (is_unique(cp2, argv+1, argvp))
584 * Skip multiple delimiters. Reset cp2 to
585 * the beginning of the next name. Reset n,
586 * the flag for names with spaces.
588 while ((c = *cp) == '|')
594 * Skip entries with spaces or non-ascii values.
595 * Convert lower case letters to upper case.
597 if ((c == ' ') || !isascii(c))
604 * Check for an old V6 2 character name. If the second
605 * name points to the beginning of the buffer, and is
606 * only 2 characters long, move it to the end of the array.
608 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
610 for (avt = &argv[1]; avt < argvp; avt++)
616 * Duplicate last name, for TTYPE option, and null
617 * terminate the array. If we didn't find a match on
618 * our terminal name, put that name at the beginning.
629 for (avt = argv; avt < argvp; avt++)
634 return((const char **)argv);
640 is_unique(char *name, char **as, char **ae)
645 n = strlen(name) + 1;
646 for (ap = as; ap < ae; ap++)
647 if (strncasecmp(*ap, name, n) == 0)
657 setupterm(char *tname, int fd, int *errp)
659 if (tgetent(termbuf, tname) == 1) {
660 termbuf[1023] = '\0';
670 #define termbuf ttytype
671 extern char ttytype[];
674 int resettermname = 1;
680 static const char **tnamep = NULL;
681 static const char **next;
686 if (tnamep && tnamep != unknown)
688 if ((tname = env_getvalue("TERM")) &&
689 (setupterm(tname, 1, &err) == 0)) {
690 tnamep = mklist(termbuf, tname);
692 if (tname && (strlen(tname) <= 40)) {
696 unknown[0] = name_unknown;
708 * Look at the sub-option buffer, and try to be helpful to the other
711 * Currently we recognize:
713 * Terminal type, send request.
714 * Terminal speed (send request).
715 * Local flow control (is request).
722 unsigned char subchar;
724 printsub('<', subbuffer, SB_LEN()+2);
725 switch (subchar = SB_GET()) {
727 if (my_want_state_is_wont(TELOPT_TTYPE))
729 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
733 unsigned char temp[50];
736 name = gettermname();
737 len = strlen(name) + 4 + 2;
738 if (len < NETROOM()) {
739 sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
740 TELQUAL_IS, name, IAC, SE);
741 ring_supply_data(&netoring, temp, len);
742 printsub('>', &temp[2], len-2);
744 ExitString("No room in buffer for terminal type.\n", 1);
750 if (my_want_state_is_wont(TELOPT_TSPEED))
754 if (SB_GET() == TELQUAL_SEND) {
756 unsigned char temp[50];
759 TerminalSpeeds(&ispeed, &ospeed);
761 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
762 TELQUAL_IS, ospeed, ispeed, IAC, SE);
763 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
765 if (len < NETROOM()) {
766 ring_supply_data(&netoring, temp, len);
767 printsub('>', temp+2, len - 2);
769 /*@*/ else printf("lm_will: not enough room in buffer\n");
773 if (my_want_state_is_wont(TELOPT_LFLOW))
778 case LFLOW_RESTART_ANY:
781 case LFLOW_RESTART_XON:
797 case TELOPT_LINEMODE:
798 if (my_want_state_is_wont(TELOPT_LINEMODE))
804 lm_will(subpointer, SB_LEN());
807 lm_wont(subpointer, SB_LEN());
810 lm_do(subpointer, SB_LEN());
813 lm_dont(subpointer, SB_LEN());
816 slc(subpointer, SB_LEN());
819 lm_mode(subpointer, SB_LEN(), 0);
827 case TELOPT_OLD_ENVIRON:
829 case TELOPT_NEW_ENVIRON:
835 if (my_want_state_is_dont(subchar))
839 if (my_want_state_is_wont(subchar)) {
846 env_opt(subpointer, SB_LEN());
849 case TELOPT_XDISPLOC:
850 if (my_want_state_is_wont(TELOPT_XDISPLOC))
854 if (SB_GET() == TELQUAL_SEND) {
855 unsigned char temp[50], *dp;
858 if ((dp = env_getvalue("DISPLAY")) == NULL ||
859 strlen(dp) > sizeof(temp) - 7) {
861 * Something happened, we no longer have a DISPLAY
862 * variable. Or it is too long. So, turn off the option.
864 send_wont(TELOPT_XDISPLOC, 1);
867 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
868 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
869 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
871 if (len < NETROOM()) {
872 ring_supply_data(&netoring, temp, len);
873 printsub('>', temp+2, len - 2);
875 /*@*/ else printf("lm_will: not enough room in buffer\n");
879 #ifdef AUTHENTICATION
880 case TELOPT_AUTHENTICATION: {
887 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
889 auth_is(subpointer, SB_LEN());
892 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
894 auth_send(subpointer, SB_LEN());
897 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
899 auth_reply(subpointer, SB_LEN());
902 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
904 auth_name(subpointer, SB_LEN());
916 if (my_want_state_is_dont(TELOPT_ENCRYPT))
918 encrypt_start(subpointer, SB_LEN());
921 if (my_want_state_is_dont(TELOPT_ENCRYPT))
925 case ENCRYPT_SUPPORT:
926 if (my_want_state_is_wont(TELOPT_ENCRYPT))
928 encrypt_support(subpointer, SB_LEN());
930 case ENCRYPT_REQSTART:
931 if (my_want_state_is_wont(TELOPT_ENCRYPT))
933 encrypt_request_start(subpointer, SB_LEN());
936 if (my_want_state_is_wont(TELOPT_ENCRYPT))
939 * We can always send an REQEND so that we cannot
940 * get stuck encrypting. We should only get this
941 * if we have been able to get in the correct mode
944 encrypt_request_end();
947 if (my_want_state_is_dont(TELOPT_ENCRYPT))
949 encrypt_is(subpointer, SB_LEN());
952 if (my_want_state_is_wont(TELOPT_ENCRYPT))
954 encrypt_reply(subpointer, SB_LEN());
956 case ENCRYPT_ENC_KEYID:
957 if (my_want_state_is_dont(TELOPT_ENCRYPT))
959 encrypt_enc_keyid(subpointer, SB_LEN());
961 case ENCRYPT_DEC_KEYID:
962 if (my_want_state_is_wont(TELOPT_ENCRYPT))
964 encrypt_dec_keyid(subpointer, SB_LEN());
970 #endif /* ENCRYPTION */
976 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
979 lm_will(unsigned char *cmd, int len)
982 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
986 case LM_FORWARDMASK: /* We shouldn't ever get this... */
990 if (NETROOM() > (int)sizeof(str_lm)) {
991 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
992 printsub('>', &str_lm[2], sizeof(str_lm)-2);
994 /*@*/ else printf("lm_will: not enough room in buffer\n");
1000 lm_wont(unsigned char *cmd, int len)
1003 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1007 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1009 /* We are always DONT, so don't respond */
1015 lm_do(unsigned char *cmd, int len)
1018 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1022 case LM_FORWARDMASK:
1026 if (NETROOM() > (int)sizeof(str_lm)) {
1027 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1028 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1030 /*@*/ else printf("lm_do: not enough room in buffer\n");
1036 lm_dont(unsigned char *cmd, int len)
1039 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1043 case LM_FORWARDMASK:
1045 /* we are always WONT, so don't respond */
1050 static unsigned char str_lm_mode[] = {
1051 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1055 lm_mode(unsigned char *cmd, int len, int init)
1059 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1063 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1064 str_lm_mode[4] = linemode;
1066 str_lm_mode[4] |= MODE_ACK;
1067 if (NETROOM() > (int)sizeof(str_lm_mode)) {
1068 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1069 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1071 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1072 setconnmode(0); /* set changed mode */
1079 * Handle special character suboption of LINEMODE.
1085 char flags; /* Current flags & level */
1086 char mylevel; /* Maximum level & flags */
1089 #define SLC_IMPORT 0
1090 #define SLC_EXPORT 1
1091 #define SLC_RVALUE 2
1092 static int slc_mode = SLC_EXPORT;
1100 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1103 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1106 #define initfunc(func, flags) { \
1107 spcp = &spc_data[func]; \
1108 if ((spcp->valp = tcval(func))) { \
1109 spcp->val = *spcp->valp; \
1110 spcp->mylevel = SLC_VARIABLE|flags; \
1113 spcp->mylevel = SLC_DEFAULT; \
1117 initfunc(SLC_SYNCH, 0);
1119 initfunc(SLC_AO, 0);
1120 initfunc(SLC_AYT, 0);
1122 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1123 initfunc(SLC_EOF, 0);
1125 initfunc(SLC_SUSP, SLC_FLUSHIN);
1127 initfunc(SLC_EC, 0);
1128 initfunc(SLC_EL, 0);
1130 initfunc(SLC_EW, 0);
1131 initfunc(SLC_RP, 0);
1132 initfunc(SLC_LNEXT, 0);
1134 initfunc(SLC_XON, 0);
1135 initfunc(SLC_XOFF, 0);
1137 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1138 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1140 initfunc(SLC_FORW1, 0);
1142 initfunc(SLC_FORW2, 0);
1146 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1149 if (slc_mode == SLC_EXPORT)
1159 printf("Special characters are %s values\n",
1160 slc_mode == SLC_IMPORT ? "remote default" :
1161 slc_mode == SLC_EXPORT ? "local" :
1166 slc_mode_export(void)
1168 slc_mode = SLC_EXPORT;
1169 if (my_state_is_will(TELOPT_LINEMODE))
1174 slc_mode_import(int def)
1176 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1177 if (my_state_is_will(TELOPT_LINEMODE))
1181 unsigned char slc_import_val[] = {
1182 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1184 unsigned char slc_import_def[] = {
1185 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1191 if (NETROOM() > (int)sizeof(slc_import_val)) {
1193 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1194 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1196 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1197 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1200 /*@*/ else printf("slc_import: not enough room\n");
1208 TerminalDefaultChars();
1211 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1212 if (spcp->mylevel != SLC_NOSUPPORT) {
1213 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1214 spcp->flags = SLC_NOSUPPORT;
1216 spcp->flags = spcp->mylevel;
1218 spcp->val = *spcp->valp;
1219 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1224 setconnmode(1); /* Make sure the character values are set */
1228 slc(unsigned char *cp, int len)
1235 for (; len >= 3; len -=3, cp +=3) {
1237 func = cp[SLC_FUNC];
1241 * Client side: always ignore 0 function.
1246 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1247 slc_add_reply(func, SLC_NOSUPPORT, 0);
1251 spcp = &spc_data[func];
1253 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1255 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1256 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1260 if (level == (SLC_DEFAULT|SLC_ACK)) {
1262 * This is an error condition, the SLC_ACK
1263 * bit should never be set for the SLC_DEFAULT
1264 * level. Our best guess to recover is to
1265 * ignore the SLC_ACK bit.
1267 cp[SLC_FLAGS] &= ~SLC_ACK;
1270 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1271 spcp->val = (cc_t)cp[SLC_VALUE];
1272 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1278 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1279 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1280 spcp->val = (cc_t)cp[SLC_VALUE];
1282 if (level == SLC_DEFAULT) {
1283 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1284 spcp->flags = spcp->mylevel;
1286 spcp->flags = SLC_NOSUPPORT;
1288 slc_add_reply(func, spcp->flags, spcp->val);
1292 setconnmode(1); /* set the new character values */
1301 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1302 if (spcp->valp && spcp->val != *spcp->valp) {
1303 spcp->val = *spcp->valp;
1304 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1305 spcp->flags = SLC_NOSUPPORT;
1307 spcp->flags = spcp->mylevel;
1308 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1315 unsigned char slc_reply[128];
1316 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1317 unsigned char *slc_replyp;
1320 slc_start_reply(void)
1322 slc_replyp = slc_reply;
1323 *slc_replyp++ = IAC;
1325 *slc_replyp++ = TELOPT_LINEMODE;
1326 *slc_replyp++ = LM_SLC;
1330 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1332 /* A sequence of up to 6 bytes my be written for this member of the SLC
1333 * suboption list by this function. The end of negotiation command,
1334 * which is written by slc_end_reply(), will require 2 additional
1335 * bytes. Do not proceed unless there is sufficient space for these
1338 if (&slc_replyp[6+2] > slc_reply_eom)
1340 if ((*slc_replyp++ = func) == IAC)
1341 *slc_replyp++ = IAC;
1342 if ((*slc_replyp++ = flags) == IAC)
1343 *slc_replyp++ = IAC;
1344 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1345 *slc_replyp++ = IAC;
1352 /* The end of negotiation command requires 2 bytes. */
1353 if (&slc_replyp[2] > slc_reply_eom)
1355 *slc_replyp++ = IAC;
1357 len = slc_replyp - slc_reply;
1360 if (NETROOM() > len) {
1361 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1362 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1364 /*@*/else printf("slc_end_reply: not enough room\n");
1371 int need_update = 0;
1373 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1374 if (!(spcp->flags&SLC_ACK))
1376 spcp->flags &= ~SLC_ACK;
1377 if (spcp->valp && (*spcp->valp != spcp->val)) {
1378 *spcp->valp = spcp->val;
1382 return(need_update);
1388 * Earlier version of telnet/telnetd from the BSD code had
1389 * the definitions of VALUE and VAR reversed. To ensure
1390 * maximum interoperability, we assume that the server is
1391 * an older BSD server, until proven otherwise. The newer
1392 * BSD servers should be able to handle either definition,
1393 * so it is better to use the wrong values if we don't
1394 * know what type of server it is.
1397 int old_env_var = OLD_ENV_VAR;
1398 int old_env_value = OLD_ENV_VALUE;
1400 # define old_env_var OLD_ENV_VAR
1401 # define old_env_value OLD_ENV_VALUE
1406 env_opt(unsigned char *buf, int len)
1408 unsigned char *ep = NULL, *epc = NULL;
1411 switch(buf[0]&0xff) {
1416 } else for (i = 1; i < len; i++) {
1417 switch (buf[i]&0xff) {
1421 if (telopt_environ == TELOPT_OLD_ENVIRON
1423 /* Server has the same definitions */
1424 old_env_var = OLD_ENV_VAR;
1425 old_env_value = OLD_ENV_VALUE;
1431 * Although OLD_ENV_VALUE is not legal, we will
1432 * still recognize it, just in case it is an
1433 * old server that has VAR & VALUE mixed up...
1444 ep = epc = &buf[i+1];
1464 /* Ignore for now. We shouldn't get it anyway. */
1472 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1473 unsigned char *opt_reply = NULL;
1474 unsigned char *opt_replyp;
1475 unsigned char *opt_replyend;
1481 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1483 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1484 if (opt_reply == NULL) {
1485 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1486 opt_reply = opt_replyp = opt_replyend = NULL;
1489 opt_replyp = opt_reply;
1490 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1491 *opt_replyp++ = IAC;
1493 *opt_replyp++ = telopt_environ;
1494 *opt_replyp++ = TELQUAL_IS;
1498 env_opt_start_info(void)
1502 opt_replyp[-1] = TELQUAL_INFO;
1506 env_opt_add(unsigned char *ep)
1508 unsigned char *vp, c;
1510 if (opt_reply == NULL) /*XXX*/
1513 if (ep == NULL || *ep == '\0') {
1514 /* Send user defined variables first. */
1516 while ((ep = env_default(0, 0)))
1519 /* Now add the list of well know variables. */
1521 while ((ep = env_default(0, 1)))
1525 vp = env_getvalue(ep);
1526 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1527 2 * strlen((char *)ep) + 6 > opt_replyend)
1531 opt_replyend += OPT_REPLY_SIZE;
1532 len = opt_replyend - opt_reply;
1533 opt_reply = (unsigned char *)realloc(opt_reply, len);
1534 if (opt_reply == NULL) {
1535 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1536 opt_reply = opt_replyp = opt_replyend = NULL;
1539 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1540 opt_replyend = opt_reply + len;
1542 if (opt_welldefined(ep))
1544 if (telopt_environ == TELOPT_OLD_ENVIRON)
1545 *opt_replyp++ = old_env_var;
1548 *opt_replyp++ = NEW_ENV_VAR;
1550 *opt_replyp++ = ENV_USERVAR;
1552 while ((c = *ep++)) {
1553 if (opt_replyp + (2 + 2) > opt_replyend)
1557 *opt_replyp++ = IAC;
1563 *opt_replyp++ = ENV_ESC;
1569 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1572 if (telopt_environ == TELOPT_OLD_ENVIRON)
1573 *opt_replyp++ = old_env_value;
1576 *opt_replyp++ = NEW_ENV_VALUE;
1584 opt_welldefined(const char *ep)
1586 if ((strcmp(ep, "USER") == 0) ||
1587 (strcmp(ep, "DISPLAY") == 0) ||
1588 (strcmp(ep, "PRINTER") == 0) ||
1589 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1590 (strcmp(ep, "JOB") == 0) ||
1591 (strcmp(ep, "ACCT") == 0))
1597 env_opt_end(int emptyok)
1601 if (opt_replyp + 2 > opt_replyend)
1603 len = opt_replyp + 2 - opt_reply;
1604 if (emptyok || len > 6) {
1605 *opt_replyp++ = IAC;
1607 if (NETROOM() > len) {
1608 ring_supply_data(&netoring, opt_reply, len);
1609 printsub('>', &opt_reply[2], len - 2);
1611 /*@*/ else printf("slc_end_reply: not enough room\n");
1615 opt_reply = opt_replyp = opt_replyend = NULL;
1628 int returnValue = 0;
1632 while (TTYROOM() > 2) {
1635 ring_consumed(&netiring, count);
1639 sbp = netiring.consume;
1640 scc = ring_full_consecutive(&netiring);
1642 /* No more data coming in */
1647 c = *sbp++ & 0xff, scc--; count++;
1650 c = (*decrypt_input)(c);
1651 #endif /* ENCRYPTION */
1653 switch (telrcv_state) {
1656 telrcv_state = TS_DATA;
1658 break; /* Ignore \0 after CR */
1660 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1664 /* Else, fall through */
1668 telrcv_state = TS_IAC;
1672 * The 'crmod' hack (see following) is needed
1673 * since we can't * set CRMOD on output only.
1674 * Machines like MULTICS like to send \r without
1675 * \n; since we must turn off CRMOD to get proper
1676 * input, the mapping is done here (sigh).
1678 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1683 c = (*decrypt_input)(c);
1684 #endif /* ENCRYPTION */
1686 sbp++, scc--; count++;
1689 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1691 sbp++, scc--; count++;
1696 (*decrypt_input)(-1);
1697 #endif /* ENCRYPTION */
1705 telrcv_state = TS_CR;
1721 telrcv_state = TS_WILL;
1725 telrcv_state = TS_WONT;
1729 telrcv_state = TS_DO;
1733 telrcv_state = TS_DONT;
1738 * We may have missed an urgent notification,
1739 * so make sure we flush whatever is in the
1742 printoption("RCVD", IAC, DM);
1745 SYNCHing = stilloob();
1751 telrcv_state = TS_SB;
1761 printoption("RCVD", IAC, c);
1764 telrcv_state = TS_DATA;
1768 printoption("RCVD", WILL, c);
1770 telrcv_state = TS_DATA;
1774 printoption("RCVD", WONT, c);
1776 telrcv_state = TS_DATA;
1780 printoption("RCVD", DO, c);
1782 if (c == TELOPT_NAWS) {
1784 } else if (c == TELOPT_LFLOW) {
1789 telrcv_state = TS_DATA;
1793 printoption("RCVD", DONT, c);
1796 setconnmode(0); /* set new tty mode (maybe) */
1797 telrcv_state = TS_DATA;
1802 telrcv_state = TS_SE;
1812 * This is an error. We only expect to get
1813 * "IAC IAC" or "IAC SE". Several things may
1814 * have happend. An IAC was not doubled, the
1815 * IAC SE was left off, or another option got
1816 * inserted into the suboption are all possibilities.
1817 * If we assume that the IAC was not doubled,
1818 * and really the IAC SE was left off, we could
1819 * get into an infinate loop here. So, instead,
1820 * we terminate the suboption, and process the
1821 * partial suboption if we can.
1828 printoption("In SUBOPTION processing, RCVD", IAC, c);
1829 suboption(); /* handle sub-option */
1830 telrcv_state = TS_IAC;
1834 telrcv_state = TS_SB;
1840 suboption(); /* handle sub-option */
1841 telrcv_state = TS_DATA;
1846 ring_consumed(&netiring, count);
1847 return returnValue||count;
1850 static int bol = 1, local = 0;
1858 command(0, "z\n", 2);
1869 int returnValue = 0;
1874 while (NETROOM() > 2) {
1880 ring_consumed(&ttyiring, count);
1884 tbp = ttyiring.consume;
1885 tcc = ring_full_consecutive(&ttyiring);
1890 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1891 if (rlogin != _POSIX_VDISABLE) {
1900 if (sc == '.' || c == termEofChar) {
1902 command(0, "close\n", 6);
1905 if (sc == termSuspChar) {
1907 command(0, "z\n", 2);
1911 command(0, tbp, tcc);
1925 if ((sc == '\n') || (sc == '\r'))
1927 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1929 * Double escape is a pass through of a single escape character.
1931 if (tcc && strip(*tbp) == escape) {
1937 command(0, (char *)tbp, tcc);
1946 #ifdef KLUDGELINEMODE
1947 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1948 if (tcc > 0 && strip(*tbp) == echoc) {
1949 tcc--; tbp++; count++;
1951 dontlecho = !dontlecho;
1952 settimer(echotoggle);
1959 if (MODE_LOCAL_CHARS(globalmode)) {
1960 if (TerminalSpecialChars(sc) == 0) {
1965 if (my_want_state_is_wont(TELOPT_BINARY)) {
1969 * If we are in CRMOD mode (\r ==> \n)
1970 * on our local machine, then probably
1971 * a newline (unix) is CRLF (TELNET).
1973 if (MODE_LOCAL_CHARS(globalmode)) {
1977 bol = flushline = 1;
1981 NET2ADD('\r', '\0');
1983 NET2ADD('\r', '\n');
1985 bol = flushline = 1;
1994 } else if (c == IAC) {
2001 ring_consumed(&ttyiring, count);
2002 return returnValue||count; /* Non-zero if we did anything */
2008 * Try to do something.
2010 * If we do something useful, return 1; else return 0.
2015 Scheduler(int block)
2017 /* One wants to be a bit careful about setting returnValue
2018 * to one, since a one implies we did some useful work,
2019 * and therefore probably won't be called to block next
2022 int netin, netout, netex, ttyin, ttyout;
2024 /* Decide which rings should be processed */
2026 netout = ring_full_count(&netoring) &&
2028 (my_want_state_is_wont(TELOPT_LINEMODE)
2029 #ifdef KLUDGELINEMODE
2030 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2033 my_want_state_is_will(TELOPT_BINARY));
2034 ttyout = ring_full_count(&ttyoring);
2036 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2038 netin = !ISend && ring_empty_count(&netiring);
2042 /* Call to system code to process rings */
2044 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2046 /* Now, look at the input rings, looking for work to do. */
2048 if (ring_full_count(&ttyiring)) {
2049 returnValue |= telsnd();
2052 if (ring_full_count(&netiring)) {
2053 returnValue |= telrcv();
2058 #ifdef AUTHENTICATION
2059 #define __unusedhere
2061 #define __unusedhere __unused
2064 * Select from tty and network...
2067 telnet(char *user __unusedhere)
2071 #ifdef AUTHENTICATION
2074 static char local_host[256] = { 0 };
2076 if (!local_host[0]) {
2077 gethostname(local_host, sizeof(local_host));
2078 local_host[sizeof(local_host)-1] = 0;
2080 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2081 auth_encrypt_user(user);
2086 #ifdef AUTHENTICATION
2088 send_will(TELOPT_AUTHENTICATION, 1);
2091 send_do(TELOPT_ENCRYPT, 1);
2092 send_will(TELOPT_ENCRYPT, 1);
2093 #endif /* ENCRYPTION */
2094 send_do(TELOPT_SGA, 1);
2095 send_will(TELOPT_TTYPE, 1);
2096 send_will(TELOPT_NAWS, 1);
2097 send_will(TELOPT_TSPEED, 1);
2098 send_will(TELOPT_LFLOW, 1);
2099 send_will(TELOPT_LINEMODE, 1);
2100 send_will(TELOPT_NEW_ENVIRON, 1);
2101 send_do(TELOPT_STATUS, 1);
2102 if (env_getvalue("DISPLAY"))
2103 send_will(TELOPT_XDISPLOC, 1);
2105 tel_enter_binary(eight);
2111 while ((schedValue = Scheduler(0)) != 0) {
2112 if (schedValue == -1) {
2118 if (Scheduler(1) == -1) {
2125 #if 0 /* XXX - this not being in is a bug */
2129 * Return the address of the next "item" in the TELNET data
2130 * stream. This will be the address of the next character if
2131 * the current address is a user data character, or it will
2132 * be the address of the character following the TELNET command
2133 * if the current address is a TELNET IAC ("I Am a Command")
2138 nextitem(char *current)
2140 if ((*current&0xff) != IAC) {
2143 switch (*(current+1)&0xff) {
2149 case SB: /* loop forever looking for the SE */
2151 char *look = current+2;
2154 if ((*look++&0xff) == IAC) {
2155 if ((*look++&0xff) == SE) {
2170 * We are about to do a TELNET SYNCH operation. Clear
2171 * the path to the network.
2173 * Things are a bit tricky since we may have sent the first
2174 * byte or so of a previous TELNET command into the network.
2175 * So, we have to scan the network buffer from the beginning
2176 * until we are up to where we want to be.
2178 * A side effect of what we do, just to keep things
2179 * simple, is to clear the urgent data pointer. The principal
2180 * caller should be setting the urgent data pointer AFTER calling
2191 * These routines add various telnet commands to the data stream.
2201 (void) ttyflush(1); /* Flush/drop output */
2202 /* do printoption AFTER flush, otherwise the output gets tossed... */
2203 printoption("SENT", DO, TELOPT_TM);
2210 printoption("SENT", IAC, AO);
2220 printoption("SENT", IAC, EL);
2227 printoption("SENT", IAC, EC);
2231 dosynch(char *ch __unused)
2233 netclear(); /* clear the path to the network */
2237 printoption("SENT", IAC, DM);
2241 int want_status_response = 0;
2244 get_status(char *ch __unused)
2246 unsigned char tmp[16];
2249 if (my_want_state_is_dont(TELOPT_STATUS)) {
2250 printf("Remote side does not support STATUS option\n");
2257 *cp++ = TELOPT_STATUS;
2258 *cp++ = TELQUAL_SEND;
2261 if (NETROOM() >= cp - tmp) {
2262 ring_supply_data(&netoring, tmp, cp-tmp);
2263 printsub('>', tmp+2, cp - tmp - 2);
2265 ++want_status_response;
2273 printoption("SENT", IAC, IP);
2286 NET2ADD(IAC, BREAK);
2287 printoption("SENT", IAC, BREAK);
2300 NET2ADD(IAC, ABORT);
2301 printoption("SENT", IAC, ABORT);
2315 printoption("SENT", IAC, SUSP);
2329 printoption("SENT", IAC, xEOF);
2336 printoption("SENT", IAC, AYT);
2340 * Send a window size update to the remote system.
2347 unsigned char tmp[16];
2350 if (my_state_is_wont(TELOPT_NAWS))
2353 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2354 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2356 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2364 *cp++ = TELOPT_NAWS;
2369 if (NETROOM() >= cp - tmp) {
2370 ring_supply_data(&netoring, tmp, cp-tmp);
2371 printsub('>', tmp+2, cp - tmp - 2);
2376 tel_enter_binary(int rw)
2379 send_do(TELOPT_BINARY, 1);
2381 send_will(TELOPT_BINARY, 1);
2385 tel_leave_binary(int rw)
2388 send_dont(TELOPT_BINARY, 1);
2390 send_wont(TELOPT_BINARY, 1);