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/usr.bin/telnet/telnet.c,v 1.8.2.3 2002/04/13 11:07:13 markm Exp $
35 * $DragonFly: src/usr.bin/telnet/telnet.c,v 1.4 2005/03/31 16:11:13 corecode Exp $
38 #include <sys/types.h>
40 /* By the way, we need to include curses.h before telnet.h since,
41 * among other things, telnet.h #defines 'DO', which is a variable
42 * declared in curses.h.
51 #include <arpa/telnet.h>
60 #include <libtelnet/misc.h>
62 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
64 static unsigned char subbuffer[SUBBUFSIZE],
65 *subpointer, *subend; /* buffer for sub-options */
66 #define SB_CLEAR() subpointer = subbuffer;
67 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
68 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
69 *subpointer++ = (c); \
72 #define SB_GET() ((*subpointer++)&0xff)
73 #define SB_PEEK() ((*subpointer)&0xff)
74 #define SB_EOF() (subpointer >= subend)
75 #define SB_LEN() (subend - subpointer)
77 char options[256]; /* The combined options */
78 char do_dont_resp[256];
79 char will_wont_resp[256];
83 autologin = 0, /* Autologin anyone? */
87 ISend, /* trying to send network data in */
90 netdata, /* Print out network data flow */
91 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
93 SYNCHing, /* we are in TELNET SYNCH mode */
94 flushout, /* flush output */
95 autoflush = 0, /* flush output when interrupting? */
96 autosynch, /* send interrupt characters with SYNCH? */
97 localflow, /* we handle flow control locally */
98 restartany, /* if flow control enabled, restart on any character */
99 localchars, /* we recognize interrupt/quit */
100 donelclchars, /* the user has set "localchars" */
101 donebinarytoggle, /* the user has put us in binary */
102 dontlecho, /* do we suppress local echoing right now? */
104 doaddrlookup = 1, /* do a reverse address lookup? */
111 #ifdef KLUDGELINEMODE
116 * Telnet receiver states for fsm
125 #define TS_SB 7 /* sub-option collection */
126 #define TS_SE 8 /* looking for sub-option end */
128 static int telrcv_state;
130 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
132 # define telopt_environ TELOPT_NEW_ENVIRON
141 #ifdef KLUDGELINEMODE
142 int kludgelinemode = 1;
145 static int is_unique(char *, char **, char **);
148 * The following are some clocks used to decide how to interpret
149 * the relationship between various variables.
155 * Initialize telnet environment.
166 connected = ISend = localflow = donebinarytoggle = 0;
171 /* Don't change NetTrace */
173 escape = CONTROL(']');
174 rlogin = _POSIX_VDISABLE;
175 #ifdef KLUDGELINEMODE
176 echoc = CONTROL('E');
180 telrcv_state = TS_DATA;
185 * These routines are in charge of sending option negotiations
188 * The basic idea is that we send the negotiation if either side
189 * is in disagreement as to what the current state should be.
193 send_do(int c, int init)
196 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
197 my_want_state_is_do(c))
199 set_my_want_state_do(c);
204 printoption("SENT", DO, c);
208 send_dont(int c, int init)
211 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
212 my_want_state_is_dont(c))
214 set_my_want_state_dont(c);
219 printoption("SENT", DONT, c);
223 send_will(int c, int init)
226 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
227 my_want_state_is_will(c))
229 set_my_want_state_will(c);
234 printoption("SENT", WILL, c);
238 send_wont(int c, int init)
241 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
242 my_want_state_is_wont(c))
244 set_my_want_state_wont(c);
249 printoption("SENT", WONT, c);
253 willoption(int option)
255 int new_state_ok = 0;
257 if (do_dont_resp[option]) {
258 --do_dont_resp[option];
259 if (do_dont_resp[option] && my_state_is_do(option))
260 --do_dont_resp[option];
263 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
270 settimer(modenegotiated);
280 * Special case for TM. If we get back a WILL,
281 * pretend we got back a WONT.
283 set_my_want_state_dont(option);
284 set_my_state_dont(option);
285 return; /* Never reply to TM will's/wont's */
287 case TELOPT_LINEMODE:
293 set_my_want_state_do(option);
295 setconnmode(0); /* possibly set new tty mode */
297 do_dont_resp[option]++;
298 send_dont(option, 0);
301 set_my_state_do(option);
305 wontoption(int option)
307 if (do_dont_resp[option]) {
308 --do_dont_resp[option];
309 if (do_dont_resp[option] && my_state_is_dont(option))
310 --do_dont_resp[option];
313 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
317 #ifdef KLUDGELINEMODE
324 settimer(modenegotiated);
330 set_my_want_state_dont(option);
331 set_my_state_dont(option);
332 return; /* Never reply to TM will's/wont's */
337 set_my_want_state_dont(option);
338 if (my_state_is_do(option))
339 send_dont(option, 0);
340 setconnmode(0); /* Set new tty mode */
341 } else if (option == TELOPT_TM) {
343 * Special case for TM.
347 set_my_want_state_dont(option);
349 set_my_state_dont(option);
355 int new_state_ok = 0;
357 if (will_wont_resp[option]) {
358 --will_wont_resp[option];
359 if (will_wont_resp[option] && my_state_is_will(option))
360 --will_wont_resp[option];
363 if (will_wont_resp[option] == 0) {
364 if (my_want_state_is_wont(option)) {
370 * Special case for TM. We send a WILL, but pretend
373 send_will(option, 0);
374 set_my_want_state_wont(TELOPT_TM);
375 set_my_state_wont(TELOPT_TM);
378 case TELOPT_BINARY: /* binary mode */
379 case TELOPT_NAWS: /* window size */
380 case TELOPT_TSPEED: /* terminal speed */
381 case TELOPT_LFLOW: /* local flow control */
382 case TELOPT_TTYPE: /* terminal type option */
383 case TELOPT_SGA: /* no big deal */
387 case TELOPT_NEW_ENVIRON: /* New environment variable option */
389 if (my_state_is_will(TELOPT_OLD_ENVIRON))
390 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
392 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
393 if (my_state_is_will(TELOPT_NEW_ENVIRON))
394 break; /* Don't enable if new one is in use! */
396 telopt_environ = option;
402 case TELOPT_XDISPLOC: /* X Display location */
403 if (env_getvalue("DISPLAY"))
407 case TELOPT_LINEMODE:
408 #ifdef KLUDGELINEMODE
410 send_do(TELOPT_SGA, 1);
412 set_my_want_state_will(TELOPT_LINEMODE);
413 send_will(option, 0);
414 set_my_state_will(TELOPT_LINEMODE);
418 case TELOPT_ECHO: /* We're never going to echo... */
424 set_my_want_state_will(option);
425 send_will(option, 0);
426 setconnmode(0); /* Set new tty mode */
428 will_wont_resp[option]++;
429 send_wont(option, 0);
433 * Handle options that need more things done after the
434 * other side has acknowledged the option.
437 case TELOPT_LINEMODE:
438 #ifdef KLUDGELINEMODE
440 send_do(TELOPT_SGA, 1);
442 set_my_state_will(option);
444 send_do(TELOPT_SGA, 0);
449 set_my_state_will(option);
453 dontoption(int option)
456 if (will_wont_resp[option]) {
457 --will_wont_resp[option];
458 if (will_wont_resp[option] && my_state_is_wont(option))
459 --will_wont_resp[option];
462 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
464 case TELOPT_LINEMODE:
465 linemode = 0; /* put us back to the default state */
468 case TELOPT_NEW_ENVIRON:
470 * The new environ option wasn't recognized, try
473 send_will(TELOPT_OLD_ENVIRON, 1);
474 telopt_environ = TELOPT_OLD_ENVIRON;
478 /* we always accept a DONT */
479 set_my_want_state_wont(option);
480 if (my_state_is_will(option))
481 send_wont(option, 0);
482 setconnmode(0); /* Set new tty mode */
484 set_my_state_wont(option);
488 * Given a buffer returned by tgetent(), this routine will turn
489 * the pipe separated list of names in the buffer into an array
490 * of pointers to null terminated names. We toss out any bad,
491 * duplicate, or verbose names (names with spaces).
494 static const char *name_unknown = "UNKNOWN";
495 static const char *unknown[] = { NULL, NULL };
498 mklist(char *buf, char *name)
501 char c, *cp, **argvp, *cp2, **argv, **avt;
504 if (strlen(name) > 40) {
506 unknown[0] = name_unknown;
512 unknown[0] = name_unknown;
514 * Count up the number of names.
516 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
521 * Allocate an array to put the name pointers into
523 argv = (char **)malloc((n+3)*sizeof(char *));
528 * Fill up the array of pointers to names.
533 for (cp = cp2 = buf; (c = *cp); cp++) {
534 if (c == '|' || c == ':') {
537 * Skip entries that have spaces or are over 40
538 * characters long. If this is our environment
539 * name, then put it up front. Otherwise, as
540 * long as this is not a duplicate name (case
541 * insensitive) add it to the list.
543 if (n || (cp - cp2 > 41))
545 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
547 else if (is_unique(cp2, argv+1, argvp))
552 * Skip multiple delimiters. Reset cp2 to
553 * the beginning of the next name. Reset n,
554 * the flag for names with spaces.
556 while ((c = *cp) == '|')
562 * Skip entries with spaces or non-ascii values.
563 * Convert lower case letters to upper case.
565 if ((c == ' ') || !isascii(c))
572 * Check for an old V6 2 character name. If the second
573 * name points to the beginning of the buffer, and is
574 * only 2 characters long, move it to the end of the array.
576 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
578 for (avt = &argv[1]; avt < argvp; avt++)
584 * Duplicate last name, for TTYPE option, and null
585 * terminate the array. If we didn't find a match on
586 * our terminal name, put that name at the beginning.
597 for (avt = argv; avt < argvp; avt++)
602 return((const char **)argv);
608 is_unique(char *name, char **as, char **ae)
613 n = strlen(name) + 1;
614 for (ap = as; ap < ae; ap++)
615 if (strncasecmp(*ap, name, n) == 0)
625 setupterm(char *tname, int fd, int *errp)
627 if (tgetent(termbuf, tname) == 1) {
628 termbuf[1023] = '\0';
638 #define termbuf ttytype
639 extern char ttytype[];
642 int resettermname = 1;
648 static const char **tnamep = 0;
649 static const char **next;
654 if (tnamep && tnamep != unknown)
656 if ((tname = env_getvalue("TERM")) &&
657 (setupterm(tname, 1, &err) == 0)) {
658 tnamep = mklist(termbuf, tname);
660 if (tname && (strlen(tname) <= 40)) {
664 unknown[0] = name_unknown;
676 * Look at the sub-option buffer, and try to be helpful to the other
679 * Currently we recognize:
681 * Terminal type, send request.
682 * Terminal speed (send request).
683 * Local flow control (is request).
690 unsigned char subchar;
692 printsub('<', subbuffer, SB_LEN()+2);
693 switch (subchar = SB_GET()) {
695 if (my_want_state_is_wont(TELOPT_TTYPE))
697 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
701 unsigned char temp[50];
704 name = gettermname();
705 len = strlen(name) + 4 + 2;
706 if (len < NETROOM()) {
707 sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
708 TELQUAL_IS, name, IAC, SE);
709 ring_supply_data(&netoring, temp, len);
710 printsub('>', &temp[2], len-2);
712 ExitString("No room in buffer for terminal type.\n", 1);
718 if (my_want_state_is_wont(TELOPT_TSPEED))
722 if (SB_GET() == TELQUAL_SEND) {
724 unsigned char temp[50];
727 TerminalSpeeds(&ispeed, &ospeed);
729 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
730 TELQUAL_IS, ospeed, ispeed, IAC, SE);
731 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
733 if (len < NETROOM()) {
734 ring_supply_data(&netoring, temp, len);
735 printsub('>', temp+2, len - 2);
737 /*@*/ else printf("lm_will: not enough room in buffer\n");
741 if (my_want_state_is_wont(TELOPT_LFLOW))
746 case LFLOW_RESTART_ANY:
749 case LFLOW_RESTART_XON:
765 case TELOPT_LINEMODE:
766 if (my_want_state_is_wont(TELOPT_LINEMODE))
772 lm_will(subpointer, SB_LEN());
775 lm_wont(subpointer, SB_LEN());
778 lm_do(subpointer, SB_LEN());
781 lm_dont(subpointer, SB_LEN());
784 slc(subpointer, SB_LEN());
787 lm_mode(subpointer, SB_LEN(), 0);
795 case TELOPT_OLD_ENVIRON:
797 case TELOPT_NEW_ENVIRON:
803 if (my_want_state_is_dont(subchar))
807 if (my_want_state_is_wont(subchar)) {
814 env_opt(subpointer, SB_LEN());
817 case TELOPT_XDISPLOC:
818 if (my_want_state_is_wont(TELOPT_XDISPLOC))
822 if (SB_GET() == TELQUAL_SEND) {
823 unsigned char temp[50], *dp;
826 if ((dp = env_getvalue("DISPLAY")) == NULL ||
827 strlen(dp) > sizeof(temp) - 7) {
829 * Something happened, we no longer have a DISPLAY
830 * variable. Or it is too long. So, turn off the option.
832 send_wont(TELOPT_XDISPLOC, 1);
835 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
836 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
837 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
839 if (len < NETROOM()) {
840 ring_supply_data(&netoring, temp, len);
841 printsub('>', temp+2, len - 2);
843 /*@*/ else printf("lm_will: not enough room in buffer\n");
852 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
855 lm_will(unsigned char *cmd, int len)
858 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
862 case LM_FORWARDMASK: /* We shouldn't ever get this... */
866 if (NETROOM() > (int)sizeof(str_lm)) {
867 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
868 printsub('>', &str_lm[2], sizeof(str_lm)-2);
870 /*@*/ else printf("lm_will: not enough room in buffer\n");
876 lm_wont(unsigned char *cmd, int len)
879 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
883 case LM_FORWARDMASK: /* We shouldn't ever get this... */
885 /* We are always DONT, so don't respond */
891 lm_do(unsigned char *cmd, int len)
894 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
902 if (NETROOM() > (int)sizeof(str_lm)) {
903 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
904 printsub('>', &str_lm[2], sizeof(str_lm)-2);
906 /*@*/ else printf("lm_do: not enough room in buffer\n");
912 lm_dont(unsigned char *cmd, int len)
915 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
921 /* we are always WONT, so don't respond */
926 static unsigned char str_lm_mode[] = {
927 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
931 lm_mode(unsigned char *cmd, int len, int init)
935 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
939 linemode = *cmd&(MODE_MASK&~MODE_ACK);
940 str_lm_mode[4] = linemode;
942 str_lm_mode[4] |= MODE_ACK;
943 if (NETROOM() > (int)sizeof(str_lm_mode)) {
944 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
945 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
947 /*@*/ else printf("lm_mode: not enough room in buffer\n");
948 setconnmode(0); /* set changed mode */
955 * Handle special character suboption of LINEMODE.
961 char flags; /* Current flags & level */
962 char mylevel; /* Maximum level & flags */
968 static int slc_mode = SLC_EXPORT;
976 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
979 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
982 #define initfunc(func, flags) { \
983 spcp = &spc_data[func]; \
984 if ((spcp->valp = tcval(func))) { \
985 spcp->val = *spcp->valp; \
986 spcp->mylevel = SLC_VARIABLE|flags; \
989 spcp->mylevel = SLC_DEFAULT; \
993 initfunc(SLC_SYNCH, 0);
996 initfunc(SLC_AYT, 0);
998 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
999 initfunc(SLC_EOF, 0);
1001 initfunc(SLC_SUSP, SLC_FLUSHIN);
1003 initfunc(SLC_EC, 0);
1004 initfunc(SLC_EL, 0);
1006 initfunc(SLC_EW, 0);
1007 initfunc(SLC_RP, 0);
1008 initfunc(SLC_LNEXT, 0);
1010 initfunc(SLC_XON, 0);
1011 initfunc(SLC_XOFF, 0);
1013 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1014 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1016 initfunc(SLC_FORW1, 0);
1018 initfunc(SLC_FORW2, 0);
1022 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1025 if (slc_mode == SLC_EXPORT)
1035 printf("Special characters are %s values\n",
1036 slc_mode == SLC_IMPORT ? "remote default" :
1037 slc_mode == SLC_EXPORT ? "local" :
1042 slc_mode_export(void)
1044 slc_mode = SLC_EXPORT;
1045 if (my_state_is_will(TELOPT_LINEMODE))
1050 slc_mode_import(int def)
1052 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1053 if (my_state_is_will(TELOPT_LINEMODE))
1057 unsigned char slc_import_val[] = {
1058 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1060 unsigned char slc_import_def[] = {
1061 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1067 if (NETROOM() > (int)sizeof(slc_import_val)) {
1069 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1070 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1072 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1073 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1076 /*@*/ else printf("slc_import: not enough room\n");
1084 TerminalDefaultChars();
1087 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1088 if (spcp->mylevel != SLC_NOSUPPORT) {
1089 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1090 spcp->flags = SLC_NOSUPPORT;
1092 spcp->flags = spcp->mylevel;
1094 spcp->val = *spcp->valp;
1095 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1100 setconnmode(1); /* Make sure the character values are set */
1104 slc(unsigned char *cp, int len)
1111 for (; len >= 3; len -=3, cp +=3) {
1113 func = cp[SLC_FUNC];
1117 * Client side: always ignore 0 function.
1122 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1123 slc_add_reply(func, SLC_NOSUPPORT, 0);
1127 spcp = &spc_data[func];
1129 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1131 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1132 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1136 if (level == (SLC_DEFAULT|SLC_ACK)) {
1138 * This is an error condition, the SLC_ACK
1139 * bit should never be set for the SLC_DEFAULT
1140 * level. Our best guess to recover is to
1141 * ignore the SLC_ACK bit.
1143 cp[SLC_FLAGS] &= ~SLC_ACK;
1146 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1147 spcp->val = (cc_t)cp[SLC_VALUE];
1148 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1154 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1155 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1156 spcp->val = (cc_t)cp[SLC_VALUE];
1158 if (level == SLC_DEFAULT) {
1159 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1160 spcp->flags = spcp->mylevel;
1162 spcp->flags = SLC_NOSUPPORT;
1164 slc_add_reply(func, spcp->flags, spcp->val);
1168 setconnmode(1); /* set the new character values */
1177 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1178 if (spcp->valp && spcp->val != *spcp->valp) {
1179 spcp->val = *spcp->valp;
1180 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1181 spcp->flags = SLC_NOSUPPORT;
1183 spcp->flags = spcp->mylevel;
1184 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1191 unsigned char slc_reply[128];
1192 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1193 unsigned char *slc_replyp;
1196 slc_start_reply(void)
1198 slc_replyp = slc_reply;
1199 *slc_replyp++ = IAC;
1201 *slc_replyp++ = TELOPT_LINEMODE;
1202 *slc_replyp++ = LM_SLC;
1206 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1208 /* A sequence of up to 6 bytes my be written for this member of the SLC
1209 * suboption list by this function. The end of negotiation command,
1210 * which is written by slc_end_reply(), will require 2 additional
1211 * bytes. Do not proceed unless there is sufficient space for these
1214 if (&slc_replyp[6+2] > slc_reply_eom)
1216 if ((*slc_replyp++ = func) == IAC)
1217 *slc_replyp++ = IAC;
1218 if ((*slc_replyp++ = flags) == IAC)
1219 *slc_replyp++ = IAC;
1220 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1221 *slc_replyp++ = IAC;
1229 /* The end of negotiation command requires 2 bytes. */
1230 if (&slc_replyp[2] > slc_reply_eom)
1232 *slc_replyp++ = IAC;
1234 len = slc_replyp - slc_reply;
1237 if (NETROOM() > len) {
1238 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1239 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1241 /*@*/else printf("slc_end_reply: not enough room\n");
1248 int need_update = 0;
1250 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1251 if (!(spcp->flags&SLC_ACK))
1253 spcp->flags &= ~SLC_ACK;
1254 if (spcp->valp && (*spcp->valp != spcp->val)) {
1255 *spcp->valp = spcp->val;
1259 return(need_update);
1265 * Earlier version of telnet/telnetd from the BSD code had
1266 * the definitions of VALUE and VAR reversed. To ensure
1267 * maximum interoperability, we assume that the server is
1268 * an older BSD server, until proven otherwise. The newer
1269 * BSD servers should be able to handle either definition,
1270 * so it is better to use the wrong values if we don't
1271 * know what type of server it is.
1274 int old_env_var = OLD_ENV_VAR;
1275 int old_env_value = OLD_ENV_VALUE;
1277 # define old_env_var OLD_ENV_VAR
1278 # define old_env_value OLD_ENV_VALUE
1283 env_opt(unsigned char *buf, int len)
1285 unsigned char *ep = 0, *epc = 0;
1288 switch(buf[0]&0xff) {
1293 } else for (i = 1; i < len; i++) {
1294 switch (buf[i]&0xff) {
1298 if (telopt_environ == TELOPT_OLD_ENVIRON
1300 /* Server has the same definitions */
1301 old_env_var = OLD_ENV_VAR;
1302 old_env_value = OLD_ENV_VALUE;
1308 * Although OLD_ENV_VALUE is not legal, we will
1309 * still recognize it, just in case it is an
1310 * old server that has VAR & VALUE mixed up...
1321 ep = epc = &buf[i+1];
1341 /* Ignore for now. We shouldn't get it anyway. */
1349 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1350 unsigned char *opt_reply = NULL;
1351 unsigned char *opt_replyp;
1352 unsigned char *opt_replyend;
1358 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1360 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1361 if (opt_reply == NULL) {
1362 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1363 opt_reply = opt_replyp = opt_replyend = NULL;
1366 opt_replyp = opt_reply;
1367 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1368 *opt_replyp++ = IAC;
1370 *opt_replyp++ = telopt_environ;
1371 *opt_replyp++ = TELQUAL_IS;
1375 env_opt_start_info(void)
1379 opt_replyp[-1] = TELQUAL_INFO;
1383 env_opt_add(unsigned char *ep)
1385 unsigned char *vp, c;
1387 if (opt_reply == NULL) /*XXX*/
1390 if (ep == NULL || *ep == '\0') {
1391 /* Send user defined variables first. */
1393 while ((ep = env_default(0, 0)))
1396 /* Now add the list of well know variables. */
1398 while ((ep = env_default(0, 1)))
1402 vp = env_getvalue(ep);
1403 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1404 2 * strlen((char *)ep) + 6 > opt_replyend)
1408 opt_replyend += OPT_REPLY_SIZE;
1409 len = opt_replyend - opt_reply;
1410 opt_reply = (unsigned char *)realloc(opt_reply, len);
1411 if (opt_reply == NULL) {
1412 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1413 opt_reply = opt_replyp = opt_replyend = NULL;
1416 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1417 opt_replyend = opt_reply + len;
1419 if (opt_welldefined(ep))
1421 if (telopt_environ == TELOPT_OLD_ENVIRON)
1422 *opt_replyp++ = old_env_var;
1425 *opt_replyp++ = NEW_ENV_VAR;
1427 *opt_replyp++ = ENV_USERVAR;
1429 while ((c = *ep++)) {
1430 if (opt_replyp + (2 + 2) > opt_replyend)
1434 *opt_replyp++ = IAC;
1440 *opt_replyp++ = ENV_ESC;
1446 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1449 if (telopt_environ == TELOPT_OLD_ENVIRON)
1450 *opt_replyp++ = old_env_value;
1453 *opt_replyp++ = NEW_ENV_VALUE;
1461 opt_welldefined(const char *ep)
1463 if ((strcmp(ep, "USER") == 0) ||
1464 (strcmp(ep, "DISPLAY") == 0) ||
1465 (strcmp(ep, "PRINTER") == 0) ||
1466 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1467 (strcmp(ep, "JOB") == 0) ||
1468 (strcmp(ep, "ACCT") == 0))
1474 env_opt_end(int emptyok)
1478 if (opt_replyp + 2 > opt_replyend)
1480 len = opt_replyp + 2 - opt_reply;
1481 if (emptyok || len > 6) {
1482 *opt_replyp++ = IAC;
1484 if (NETROOM() > len) {
1485 ring_supply_data(&netoring, opt_reply, len);
1486 printsub('>', &opt_reply[2], len - 2);
1488 /*@*/ else printf("slc_end_reply: not enough room\n");
1492 opt_reply = opt_replyp = opt_replyend = NULL;
1505 int returnValue = 0;
1509 while (TTYROOM() > 2) {
1512 ring_consumed(&netiring, count);
1516 sbp = netiring.consume;
1517 scc = ring_full_consecutive(&netiring);
1519 /* No more data coming in */
1524 c = *sbp++ & 0xff, scc--; count++;
1526 switch (telrcv_state) {
1529 telrcv_state = TS_DATA;
1531 break; /* Ignore \0 after CR */
1533 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1537 /* Else, fall through */
1541 telrcv_state = TS_IAC;
1545 * The 'crmod' hack (see following) is needed
1546 * since we can't * set CRMOD on output only.
1547 * Machines like MULTICS like to send \r without
1548 * \n; since we must turn off CRMOD to get proper
1549 * input, the mapping is done here (sigh).
1551 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1555 sbp++, scc--; count++;
1558 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1560 sbp++, scc--; count++;
1570 telrcv_state = TS_CR;
1586 telrcv_state = TS_WILL;
1590 telrcv_state = TS_WONT;
1594 telrcv_state = TS_DO;
1598 telrcv_state = TS_DONT;
1603 * We may have missed an urgent notification,
1604 * so make sure we flush whatever is in the
1607 printoption("RCVD", IAC, DM);
1610 SYNCHing = stilloob();
1616 telrcv_state = TS_SB;
1626 printoption("RCVD", IAC, c);
1629 telrcv_state = TS_DATA;
1633 printoption("RCVD", WILL, c);
1635 telrcv_state = TS_DATA;
1639 printoption("RCVD", WONT, c);
1641 telrcv_state = TS_DATA;
1645 printoption("RCVD", DO, c);
1647 if (c == TELOPT_NAWS) {
1649 } else if (c == TELOPT_LFLOW) {
1654 telrcv_state = TS_DATA;
1658 printoption("RCVD", DONT, c);
1661 setconnmode(0); /* set new tty mode (maybe) */
1662 telrcv_state = TS_DATA;
1667 telrcv_state = TS_SE;
1677 * This is an error. We only expect to get
1678 * "IAC IAC" or "IAC SE". Several things may
1679 * have happend. An IAC was not doubled, the
1680 * IAC SE was left off, or another option got
1681 * inserted into the suboption are all possibilities.
1682 * If we assume that the IAC was not doubled,
1683 * and really the IAC SE was left off, we could
1684 * get into an infinate loop here. So, instead,
1685 * we terminate the suboption, and process the
1686 * partial suboption if we can.
1693 printoption("In SUBOPTION processing, RCVD", IAC, c);
1694 suboption(); /* handle sub-option */
1695 telrcv_state = TS_IAC;
1699 telrcv_state = TS_SB;
1705 suboption(); /* handle sub-option */
1706 telrcv_state = TS_DATA;
1711 ring_consumed(&netiring, count);
1712 return returnValue||count;
1715 static int bol = 1, local = 0;
1723 command(0, "z\n", 2);
1734 int returnValue = 0;
1739 while (NETROOM() > 2) {
1745 ring_consumed(&ttyiring, count);
1749 tbp = ttyiring.consume;
1750 tcc = ring_full_consecutive(&ttyiring);
1755 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1756 if (rlogin != _POSIX_VDISABLE) {
1765 if (sc == '.' || c == termEofChar) {
1767 command(0, "close\n", 6);
1770 if (sc == termSuspChar) {
1772 command(0, "z\n", 2);
1776 command(0, tbp, tcc);
1790 if ((sc == '\n') || (sc == '\r'))
1792 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1794 * Double escape is a pass through of a single escape character.
1796 if (tcc && strip(*tbp) == escape) {
1802 command(0, (char *)tbp, tcc);
1811 #ifdef KLUDGELINEMODE
1812 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1813 if (tcc > 0 && strip(*tbp) == echoc) {
1814 tcc--; tbp++; count++;
1816 dontlecho = !dontlecho;
1817 settimer(echotoggle);
1824 if (MODE_LOCAL_CHARS(globalmode)) {
1825 if (TerminalSpecialChars(sc) == 0) {
1830 if (my_want_state_is_wont(TELOPT_BINARY)) {
1834 * If we are in CRMOD mode (\r ==> \n)
1835 * on our local machine, then probably
1836 * a newline (unix) is CRLF (TELNET).
1838 if (MODE_LOCAL_CHARS(globalmode)) {
1842 bol = flushline = 1;
1846 NET2ADD('\r', '\0');
1848 NET2ADD('\r', '\n');
1850 bol = flushline = 1;
1859 } else if (c == IAC) {
1866 ring_consumed(&ttyiring, count);
1867 return returnValue||count; /* Non-zero if we did anything */
1873 * Try to do something.
1875 * If we do something useful, return 1; else return 0.
1880 Scheduler(int block)
1882 /* One wants to be a bit careful about setting returnValue
1883 * to one, since a one implies we did some useful work,
1884 * and therefore probably won't be called to block next
1887 int netin, netout, netex, ttyin, ttyout;
1889 /* Decide which rings should be processed */
1891 netout = ring_full_count(&netoring) &&
1893 (my_want_state_is_wont(TELOPT_LINEMODE)
1894 #ifdef KLUDGELINEMODE
1895 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
1898 my_want_state_is_will(TELOPT_BINARY));
1899 ttyout = ring_full_count(&ttyoring);
1901 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
1903 netin = !ISend && ring_empty_count(&netiring);
1907 /* Call to system code to process rings */
1909 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
1911 /* Now, look at the input rings, looking for work to do. */
1913 if (ring_full_count(&ttyiring)) {
1914 returnValue |= telsnd();
1917 if (ring_full_count(&netiring)) {
1918 returnValue |= telrcv();
1923 #define __unusedhere __unused
1925 * Select from tty and network...
1928 telnet(char *user __unusedhere)
1933 send_do(TELOPT_SGA, 1);
1934 send_will(TELOPT_TTYPE, 1);
1935 send_will(TELOPT_NAWS, 1);
1936 send_will(TELOPT_TSPEED, 1);
1937 send_will(TELOPT_LFLOW, 1);
1938 send_will(TELOPT_LINEMODE, 1);
1939 send_will(TELOPT_NEW_ENVIRON, 1);
1940 send_do(TELOPT_STATUS, 1);
1941 if (env_getvalue("DISPLAY"))
1942 send_will(TELOPT_XDISPLOC, 1);
1944 tel_enter_binary(eight);
1950 while ((schedValue = Scheduler(0)) != 0) {
1951 if (schedValue == -1) {
1957 if (Scheduler(1) == -1) {
1964 #if 0 /* XXX - this not being in is a bug */
1968 * Return the address of the next "item" in the TELNET data
1969 * stream. This will be the address of the next character if
1970 * the current address is a user data character, or it will
1971 * be the address of the character following the TELNET command
1972 * if the current address is a TELNET IAC ("I Am a Command")
1977 nextitem(char *current)
1979 if ((*current&0xff) != IAC) {
1982 switch (*(current+1)&0xff) {
1988 case SB: /* loop forever looking for the SE */
1990 char *look = current+2;
1993 if ((*look++&0xff) == IAC) {
1994 if ((*look++&0xff) == SE) {
2009 * We are about to do a TELNET SYNCH operation. Clear
2010 * the path to the network.
2012 * Things are a bit tricky since we may have sent the first
2013 * byte or so of a previous TELNET command into the network.
2014 * So, we have to scan the network buffer from the beginning
2015 * until we are up to where we want to be.
2017 * A side effect of what we do, just to keep things
2018 * simple, is to clear the urgent data pointer. The principal
2019 * caller should be setting the urgent data pointer AFTER calling
2030 * These routines add various telnet commands to the data stream.
2040 (void) ttyflush(1); /* Flush/drop output */
2041 /* do printoption AFTER flush, otherwise the output gets tossed... */
2042 printoption("SENT", DO, TELOPT_TM);
2049 printoption("SENT", IAC, AO);
2059 printoption("SENT", IAC, EL);
2066 printoption("SENT", IAC, EC);
2070 dosynch(char *ch __unused)
2072 netclear(); /* clear the path to the network */
2076 printoption("SENT", IAC, DM);
2080 int want_status_response = 0;
2083 get_status(char *ch __unused)
2085 unsigned char tmp[16];
2088 if (my_want_state_is_dont(TELOPT_STATUS)) {
2089 printf("Remote side does not support STATUS option\n");
2096 *cp++ = TELOPT_STATUS;
2097 *cp++ = TELQUAL_SEND;
2100 if (NETROOM() >= cp - tmp) {
2101 ring_supply_data(&netoring, tmp, cp-tmp);
2102 printsub('>', tmp+2, cp - tmp - 2);
2104 ++want_status_response;
2112 printoption("SENT", IAC, IP);
2125 NET2ADD(IAC, BREAK);
2126 printoption("SENT", IAC, BREAK);
2139 NET2ADD(IAC, ABORT);
2140 printoption("SENT", IAC, ABORT);
2154 printoption("SENT", IAC, SUSP);
2168 printoption("SENT", IAC, xEOF);
2175 printoption("SENT", IAC, AYT);
2179 * Send a window size update to the remote system.
2186 unsigned char tmp[16];
2189 if (my_state_is_wont(TELOPT_NAWS))
2192 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2193 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2195 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2203 *cp++ = TELOPT_NAWS;
2208 if (NETROOM() >= cp - tmp) {
2209 ring_supply_data(&netoring, tmp, cp-tmp);
2210 printsub('>', tmp+2, cp - tmp - 2);
2215 tel_enter_binary(int rw)
2218 send_do(TELOPT_BINARY, 1);
2220 send_will(TELOPT_BINARY, 1);
2224 tel_leave_binary(int rw)
2227 send_dont(TELOPT_BINARY, 1);
2229 send_wont(TELOPT_BINARY, 1);