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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 static const char sccsid[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95";
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>
64 #include <libtelnet/misc.h>
66 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
68 static unsigned char subbuffer[SUBBUFSIZE],
69 *subpointer, *subend; /* buffer for sub-options */
70 #define SB_CLEAR() subpointer = subbuffer;
71 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
72 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
73 *subpointer++ = (c); \
76 #define SB_GET() ((*subpointer++)&0xff)
77 #define SB_PEEK() ((*subpointer)&0xff)
78 #define SB_EOF() (subpointer >= subend)
79 #define SB_LEN() (subend - subpointer)
81 char options[256]; /* The combined options */
82 char do_dont_resp[256];
83 char will_wont_resp[256];
87 autologin = 0, /* Autologin anyone? */
91 ISend, /* trying to send network data in */
94 netdata, /* Print out network data flow */
95 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
97 SYNCHing, /* we are in TELNET SYNCH mode */
98 flushout, /* flush output */
99 autoflush = 0, /* flush output when interrupting? */
100 autosynch, /* send interrupt characters with SYNCH? */
101 localflow, /* we handle flow control locally */
102 restartany, /* if flow control enabled, restart on any character */
103 localchars, /* we recognize interrupt/quit */
104 donelclchars, /* the user has set "localchars" */
105 donebinarytoggle, /* the user has put us in binary */
106 dontlecho, /* do we suppress local echoing right now? */
108 doaddrlookup = 1, /* do a reverse address lookup? */
115 #ifdef KLUDGELINEMODE
120 * Telnet receiver states for fsm
129 #define TS_SB 7 /* sub-option collection */
130 #define TS_SE 8 /* looking for sub-option end */
132 static int telrcv_state;
134 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
136 # define telopt_environ TELOPT_NEW_ENVIRON
145 #ifdef KLUDGELINEMODE
146 int kludgelinemode = 1;
149 static int is_unique(char *, char **, char **);
152 * The following are some clocks used to decide how to interpret
153 * the relationship between various variables.
159 * Initialize telnet environment.
170 connected = ISend = localflow = donebinarytoggle = 0;
175 /* Don't change NetTrace */
177 escape = CONTROL(']');
178 rlogin = _POSIX_VDISABLE;
179 #ifdef KLUDGELINEMODE
180 echoc = CONTROL('E');
184 telrcv_state = TS_DATA;
189 * These routines are in charge of sending option negotiations
192 * The basic idea is that we send the negotiation if either side
193 * is in disagreement as to what the current state should be.
197 send_do(int c, int init)
200 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
201 my_want_state_is_do(c))
203 set_my_want_state_do(c);
208 printoption("SENT", DO, c);
212 send_dont(int c, int init)
215 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
216 my_want_state_is_dont(c))
218 set_my_want_state_dont(c);
223 printoption("SENT", DONT, c);
227 send_will(int c, int init)
230 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
231 my_want_state_is_will(c))
233 set_my_want_state_will(c);
238 printoption("SENT", WILL, c);
242 send_wont(int c, int init)
245 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
246 my_want_state_is_wont(c))
248 set_my_want_state_wont(c);
253 printoption("SENT", WONT, c);
257 willoption(int option)
259 int new_state_ok = 0;
261 if (do_dont_resp[option]) {
262 --do_dont_resp[option];
263 if (do_dont_resp[option] && my_state_is_do(option))
264 --do_dont_resp[option];
267 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
274 settimer(modenegotiated);
284 * Special case for TM. If we get back a WILL,
285 * pretend we got back a WONT.
287 set_my_want_state_dont(option);
288 set_my_state_dont(option);
289 return; /* Never reply to TM will's/wont's */
291 case TELOPT_LINEMODE:
297 set_my_want_state_do(option);
299 setconnmode(0); /* possibly set new tty mode */
301 do_dont_resp[option]++;
302 send_dont(option, 0);
305 set_my_state_do(option);
309 wontoption(int option)
311 if (do_dont_resp[option]) {
312 --do_dont_resp[option];
313 if (do_dont_resp[option] && my_state_is_dont(option))
314 --do_dont_resp[option];
317 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
321 #ifdef KLUDGELINEMODE
328 settimer(modenegotiated);
334 set_my_want_state_dont(option);
335 set_my_state_dont(option);
336 return; /* Never reply to TM will's/wont's */
341 set_my_want_state_dont(option);
342 if (my_state_is_do(option))
343 send_dont(option, 0);
344 setconnmode(0); /* Set new tty mode */
345 } else if (option == TELOPT_TM) {
347 * Special case for TM.
351 set_my_want_state_dont(option);
353 set_my_state_dont(option);
359 int new_state_ok = 0;
361 if (will_wont_resp[option]) {
362 --will_wont_resp[option];
363 if (will_wont_resp[option] && my_state_is_will(option))
364 --will_wont_resp[option];
367 if (will_wont_resp[option] == 0) {
368 if (my_want_state_is_wont(option)) {
374 * Special case for TM. We send a WILL, but pretend
377 send_will(option, 0);
378 set_my_want_state_wont(TELOPT_TM);
379 set_my_state_wont(TELOPT_TM);
382 case TELOPT_BINARY: /* binary mode */
383 case TELOPT_NAWS: /* window size */
384 case TELOPT_TSPEED: /* terminal speed */
385 case TELOPT_LFLOW: /* local flow control */
386 case TELOPT_TTYPE: /* terminal type option */
387 case TELOPT_SGA: /* no big deal */
391 case TELOPT_NEW_ENVIRON: /* New environment variable option */
393 if (my_state_is_will(TELOPT_OLD_ENVIRON))
394 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
396 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
397 if (my_state_is_will(TELOPT_NEW_ENVIRON))
398 break; /* Don't enable if new one is in use! */
400 telopt_environ = option;
406 case TELOPT_XDISPLOC: /* X Display location */
407 if (env_getvalue("DISPLAY"))
411 case TELOPT_LINEMODE:
412 #ifdef KLUDGELINEMODE
414 send_do(TELOPT_SGA, 1);
416 set_my_want_state_will(TELOPT_LINEMODE);
417 send_will(option, 0);
418 set_my_state_will(TELOPT_LINEMODE);
422 case TELOPT_ECHO: /* We're never going to echo... */
428 set_my_want_state_will(option);
429 send_will(option, 0);
430 setconnmode(0); /* Set new tty mode */
432 will_wont_resp[option]++;
433 send_wont(option, 0);
437 * Handle options that need more things done after the
438 * other side has acknowledged the option.
441 case TELOPT_LINEMODE:
442 #ifdef KLUDGELINEMODE
444 send_do(TELOPT_SGA, 1);
446 set_my_state_will(option);
448 send_do(TELOPT_SGA, 0);
453 set_my_state_will(option);
457 dontoption(int option)
460 if (will_wont_resp[option]) {
461 --will_wont_resp[option];
462 if (will_wont_resp[option] && my_state_is_wont(option))
463 --will_wont_resp[option];
466 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
468 case TELOPT_LINEMODE:
469 linemode = 0; /* put us back to the default state */
472 case TELOPT_NEW_ENVIRON:
474 * The new environ option wasn't recognized, try
477 send_will(TELOPT_OLD_ENVIRON, 1);
478 telopt_environ = TELOPT_OLD_ENVIRON;
482 /* we always accept a DONT */
483 set_my_want_state_wont(option);
484 if (my_state_is_will(option))
485 send_wont(option, 0);
486 setconnmode(0); /* Set new tty mode */
488 set_my_state_wont(option);
492 * Given a buffer returned by tgetent(), this routine will turn
493 * the pipe separated list of names in the buffer into an array
494 * of pointers to null terminated names. We toss out any bad,
495 * duplicate, or verbose names (names with spaces).
498 static const char *name_unknown = "UNKNOWN";
499 static const char *unknown[] = { NULL, NULL };
502 mklist(char *buf, char *name)
505 char c, *cp, **argvp, *cp2, **argv, **avt;
508 if (strlen(name) > 40) {
510 unknown[0] = name_unknown;
516 unknown[0] = name_unknown;
518 * Count up the number of names.
520 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
525 * Allocate an array to put the name pointers into
527 argv = (char **)malloc((n+3)*sizeof(char *));
532 * Fill up the array of pointers to names.
537 for (cp = cp2 = buf; (c = *cp); cp++) {
538 if (c == '|' || c == ':') {
541 * Skip entries that have spaces or are over 40
542 * characters long. If this is our environment
543 * name, then put it up front. Otherwise, as
544 * long as this is not a duplicate name (case
545 * insensitive) add it to the list.
547 if (n || (cp - cp2 > 41))
549 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
551 else if (is_unique(cp2, argv+1, argvp))
556 * Skip multiple delimiters. Reset cp2 to
557 * the beginning of the next name. Reset n,
558 * the flag for names with spaces.
560 while ((c = *cp) == '|')
566 * Skip entries with spaces or non-ascii values.
567 * Convert lower case letters to upper case.
569 if ((c == ' ') || !isascii(c))
576 * Check for an old V6 2 character name. If the second
577 * name points to the beginning of the buffer, and is
578 * only 2 characters long, move it to the end of the array.
580 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
582 for (avt = &argv[1]; avt < argvp; avt++)
588 * Duplicate last name, for TTYPE option, and null
589 * terminate the array. If we didn't find a match on
590 * our terminal name, put that name at the beginning.
601 for (avt = argv; avt < argvp; avt++)
606 return((const char **)argv);
612 is_unique(char *name, char **as, char **ae)
617 n = strlen(name) + 1;
618 for (ap = as; ap < ae; ap++)
619 if (strncasecmp(*ap, name, n) == 0)
629 setupterm(char *tname, int fd, int *errp)
631 if (tgetent(termbuf, tname) == 1) {
632 termbuf[1023] = '\0';
642 #define termbuf ttytype
643 extern char ttytype[];
646 int resettermname = 1;
652 static const char **tnamep = 0;
653 static const char **next;
658 if (tnamep && tnamep != unknown)
660 if ((tname = env_getvalue("TERM")) &&
661 (setupterm(tname, 1, &err) == 0)) {
662 tnamep = mklist(termbuf, tname);
664 if (tname && (strlen(tname) <= 40)) {
668 unknown[0] = name_unknown;
680 * Look at the sub-option buffer, and try to be helpful to the other
683 * Currently we recognize:
685 * Terminal type, send request.
686 * Terminal speed (send request).
687 * Local flow control (is request).
694 unsigned char subchar;
696 printsub('<', subbuffer, SB_LEN()+2);
697 switch (subchar = SB_GET()) {
699 if (my_want_state_is_wont(TELOPT_TTYPE))
701 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
705 unsigned char temp[50];
708 name = gettermname();
709 len = strlen(name) + 4 + 2;
710 if (len < NETROOM()) {
711 sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
712 TELQUAL_IS, name, IAC, SE);
713 ring_supply_data(&netoring, temp, len);
714 printsub('>', &temp[2], len-2);
716 ExitString("No room in buffer for terminal type.\n", 1);
722 if (my_want_state_is_wont(TELOPT_TSPEED))
726 if (SB_GET() == TELQUAL_SEND) {
728 unsigned char temp[50];
731 TerminalSpeeds(&ispeed, &ospeed);
733 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
734 TELQUAL_IS, ospeed, ispeed, IAC, SE);
735 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
737 if (len < NETROOM()) {
738 ring_supply_data(&netoring, temp, len);
739 printsub('>', temp+2, len - 2);
741 /*@*/ else printf("lm_will: not enough room in buffer\n");
745 if (my_want_state_is_wont(TELOPT_LFLOW))
750 case LFLOW_RESTART_ANY:
753 case LFLOW_RESTART_XON:
769 case TELOPT_LINEMODE:
770 if (my_want_state_is_wont(TELOPT_LINEMODE))
776 lm_will(subpointer, SB_LEN());
779 lm_wont(subpointer, SB_LEN());
782 lm_do(subpointer, SB_LEN());
785 lm_dont(subpointer, SB_LEN());
788 slc(subpointer, SB_LEN());
791 lm_mode(subpointer, SB_LEN(), 0);
799 case TELOPT_OLD_ENVIRON:
801 case TELOPT_NEW_ENVIRON:
807 if (my_want_state_is_dont(subchar))
811 if (my_want_state_is_wont(subchar)) {
818 env_opt(subpointer, SB_LEN());
821 case TELOPT_XDISPLOC:
822 if (my_want_state_is_wont(TELOPT_XDISPLOC))
826 if (SB_GET() == TELQUAL_SEND) {
827 unsigned char temp[50], *dp;
830 if ((dp = env_getvalue("DISPLAY")) == NULL ||
831 strlen(dp) > sizeof(temp) - 7) {
833 * Something happened, we no longer have a DISPLAY
834 * variable. Or it is too long. So, turn off the option.
836 send_wont(TELOPT_XDISPLOC, 1);
839 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
840 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
841 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
843 if (len < NETROOM()) {
844 ring_supply_data(&netoring, temp, len);
845 printsub('>', temp+2, len - 2);
847 /*@*/ else printf("lm_will: not enough room in buffer\n");
856 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
859 lm_will(unsigned char *cmd, int len)
862 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
866 case LM_FORWARDMASK: /* We shouldn't ever get this... */
870 if (NETROOM() > (int)sizeof(str_lm)) {
871 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
872 printsub('>', &str_lm[2], sizeof(str_lm)-2);
874 /*@*/ else printf("lm_will: not enough room in buffer\n");
880 lm_wont(unsigned char *cmd, int len)
883 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
887 case LM_FORWARDMASK: /* We shouldn't ever get this... */
889 /* We are always DONT, so don't respond */
895 lm_do(unsigned char *cmd, int len)
898 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
906 if (NETROOM() > (int)sizeof(str_lm)) {
907 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
908 printsub('>', &str_lm[2], sizeof(str_lm)-2);
910 /*@*/ else printf("lm_do: not enough room in buffer\n");
916 lm_dont(unsigned char *cmd, int len)
919 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
925 /* we are always WONT, so don't respond */
930 static unsigned char str_lm_mode[] = {
931 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
935 lm_mode(unsigned char *cmd, int len, int init)
939 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
943 linemode = *cmd&(MODE_MASK&~MODE_ACK);
944 str_lm_mode[4] = linemode;
946 str_lm_mode[4] |= MODE_ACK;
947 if (NETROOM() > (int)sizeof(str_lm_mode)) {
948 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
949 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
951 /*@*/ else printf("lm_mode: not enough room in buffer\n");
952 setconnmode(0); /* set changed mode */
959 * Handle special character suboption of LINEMODE.
965 char flags; /* Current flags & level */
966 char mylevel; /* Maximum level & flags */
972 static int slc_mode = SLC_EXPORT;
980 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
983 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
986 #define initfunc(func, flags) { \
987 spcp = &spc_data[func]; \
988 if ((spcp->valp = tcval(func))) { \
989 spcp->val = *spcp->valp; \
990 spcp->mylevel = SLC_VARIABLE|flags; \
993 spcp->mylevel = SLC_DEFAULT; \
997 initfunc(SLC_SYNCH, 0);
1000 initfunc(SLC_AYT, 0);
1002 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1003 initfunc(SLC_EOF, 0);
1005 initfunc(SLC_SUSP, SLC_FLUSHIN);
1007 initfunc(SLC_EC, 0);
1008 initfunc(SLC_EL, 0);
1010 initfunc(SLC_EW, 0);
1011 initfunc(SLC_RP, 0);
1012 initfunc(SLC_LNEXT, 0);
1014 initfunc(SLC_XON, 0);
1015 initfunc(SLC_XOFF, 0);
1017 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1018 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1020 initfunc(SLC_FORW1, 0);
1022 initfunc(SLC_FORW2, 0);
1026 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1029 if (slc_mode == SLC_EXPORT)
1039 printf("Special characters are %s values\n",
1040 slc_mode == SLC_IMPORT ? "remote default" :
1041 slc_mode == SLC_EXPORT ? "local" :
1046 slc_mode_export(void)
1048 slc_mode = SLC_EXPORT;
1049 if (my_state_is_will(TELOPT_LINEMODE))
1054 slc_mode_import(int def)
1056 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1057 if (my_state_is_will(TELOPT_LINEMODE))
1061 unsigned char slc_import_val[] = {
1062 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1064 unsigned char slc_import_def[] = {
1065 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1071 if (NETROOM() > (int)sizeof(slc_import_val)) {
1073 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1074 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1076 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1077 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1080 /*@*/ else printf("slc_import: not enough room\n");
1088 TerminalDefaultChars();
1091 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1092 if (spcp->mylevel != SLC_NOSUPPORT) {
1093 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1094 spcp->flags = SLC_NOSUPPORT;
1096 spcp->flags = spcp->mylevel;
1098 spcp->val = *spcp->valp;
1099 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1104 setconnmode(1); /* Make sure the character values are set */
1108 slc(unsigned char *cp, int len)
1115 for (; len >= 3; len -=3, cp +=3) {
1117 func = cp[SLC_FUNC];
1121 * Client side: always ignore 0 function.
1126 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1127 slc_add_reply(func, SLC_NOSUPPORT, 0);
1131 spcp = &spc_data[func];
1133 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1135 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1136 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1140 if (level == (SLC_DEFAULT|SLC_ACK)) {
1142 * This is an error condition, the SLC_ACK
1143 * bit should never be set for the SLC_DEFAULT
1144 * level. Our best guess to recover is to
1145 * ignore the SLC_ACK bit.
1147 cp[SLC_FLAGS] &= ~SLC_ACK;
1150 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1151 spcp->val = (cc_t)cp[SLC_VALUE];
1152 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1158 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1159 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1160 spcp->val = (cc_t)cp[SLC_VALUE];
1162 if (level == SLC_DEFAULT) {
1163 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1164 spcp->flags = spcp->mylevel;
1166 spcp->flags = SLC_NOSUPPORT;
1168 slc_add_reply(func, spcp->flags, spcp->val);
1172 setconnmode(1); /* set the new character values */
1181 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1182 if (spcp->valp && spcp->val != *spcp->valp) {
1183 spcp->val = *spcp->valp;
1184 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1185 spcp->flags = SLC_NOSUPPORT;
1187 spcp->flags = spcp->mylevel;
1188 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1195 unsigned char slc_reply[128];
1196 unsigned char *slc_replyp;
1199 slc_start_reply(void)
1201 slc_replyp = slc_reply;
1202 *slc_replyp++ = IAC;
1204 *slc_replyp++ = TELOPT_LINEMODE;
1205 *slc_replyp++ = LM_SLC;
1209 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1211 if ((*slc_replyp++ = func) == IAC)
1212 *slc_replyp++ = IAC;
1213 if ((*slc_replyp++ = flags) == IAC)
1214 *slc_replyp++ = IAC;
1215 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1216 *slc_replyp++ = IAC;
1224 *slc_replyp++ = IAC;
1226 len = slc_replyp - slc_reply;
1229 if (NETROOM() > len) {
1230 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1231 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1233 /*@*/else printf("slc_end_reply: not enough room\n");
1240 int need_update = 0;
1242 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1243 if (!(spcp->flags&SLC_ACK))
1245 spcp->flags &= ~SLC_ACK;
1246 if (spcp->valp && (*spcp->valp != spcp->val)) {
1247 *spcp->valp = spcp->val;
1251 return(need_update);
1257 * Earlier version of telnet/telnetd from the BSD code had
1258 * the definitions of VALUE and VAR reversed. To ensure
1259 * maximum interoperability, we assume that the server is
1260 * an older BSD server, until proven otherwise. The newer
1261 * BSD servers should be able to handle either definition,
1262 * so it is better to use the wrong values if we don't
1263 * know what type of server it is.
1266 int old_env_var = OLD_ENV_VAR;
1267 int old_env_value = OLD_ENV_VALUE;
1269 # define old_env_var OLD_ENV_VAR
1270 # define old_env_value OLD_ENV_VALUE
1275 env_opt(unsigned char *buf, int len)
1277 unsigned char *ep = 0, *epc = 0;
1280 switch(buf[0]&0xff) {
1285 } else for (i = 1; i < len; i++) {
1286 switch (buf[i]&0xff) {
1290 if (telopt_environ == TELOPT_OLD_ENVIRON
1292 /* Server has the same definitions */
1293 old_env_var = OLD_ENV_VAR;
1294 old_env_value = OLD_ENV_VALUE;
1300 * Although OLD_ENV_VALUE is not legal, we will
1301 * still recognize it, just in case it is an
1302 * old server that has VAR & VALUE mixed up...
1313 ep = epc = &buf[i+1];
1333 /* Ignore for now. We shouldn't get it anyway. */
1341 #define OPT_REPLY_SIZE 256
1342 unsigned char *opt_reply;
1343 unsigned char *opt_replyp;
1344 unsigned char *opt_replyend;
1350 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1352 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1353 if (opt_reply == NULL) {
1354 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1355 opt_reply = opt_replyp = opt_replyend = NULL;
1358 opt_replyp = opt_reply;
1359 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1360 *opt_replyp++ = IAC;
1362 *opt_replyp++ = telopt_environ;
1363 *opt_replyp++ = TELQUAL_IS;
1367 env_opt_start_info(void)
1371 opt_replyp[-1] = TELQUAL_INFO;
1375 env_opt_add(unsigned char *ep)
1377 unsigned char *vp, c;
1379 if (opt_reply == NULL) /*XXX*/
1382 if (ep == NULL || *ep == '\0') {
1383 /* Send user defined variables first. */
1385 while ((ep = env_default(0, 0)))
1388 /* Now add the list of well know variables. */
1390 while ((ep = env_default(0, 1)))
1394 vp = env_getvalue(ep);
1395 if (opt_replyp + (vp ? strlen((char *)vp) : 0) +
1396 strlen((char *)ep) + 6 > opt_replyend)
1399 opt_replyend += OPT_REPLY_SIZE;
1400 len = opt_replyend - opt_reply;
1401 opt_reply = (unsigned char *)realloc(opt_reply, len);
1402 if (opt_reply == NULL) {
1403 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1404 opt_reply = opt_replyp = opt_replyend = NULL;
1407 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1408 opt_replyend = opt_reply + len;
1410 if (opt_welldefined(ep))
1412 if (telopt_environ == TELOPT_OLD_ENVIRON)
1413 *opt_replyp++ = old_env_var;
1416 *opt_replyp++ = NEW_ENV_VAR;
1418 *opt_replyp++ = ENV_USERVAR;
1420 while ((c = *ep++)) {
1423 *opt_replyp++ = IAC;
1429 *opt_replyp++ = ENV_ESC;
1436 if (telopt_environ == TELOPT_OLD_ENVIRON)
1437 *opt_replyp++ = old_env_value;
1440 *opt_replyp++ = NEW_ENV_VALUE;
1448 opt_welldefined(const char *ep)
1450 if ((strcmp(ep, "USER") == 0) ||
1451 (strcmp(ep, "DISPLAY") == 0) ||
1452 (strcmp(ep, "PRINTER") == 0) ||
1453 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1454 (strcmp(ep, "JOB") == 0) ||
1455 (strcmp(ep, "ACCT") == 0))
1461 env_opt_end(int emptyok)
1465 len = opt_replyp - opt_reply + 2;
1466 if (emptyok || len > 6) {
1467 *opt_replyp++ = IAC;
1469 if (NETROOM() > len) {
1470 ring_supply_data(&netoring, opt_reply, len);
1471 printsub('>', &opt_reply[2], len - 2);
1473 /*@*/ else printf("slc_end_reply: not enough room\n");
1477 opt_reply = opt_replyp = opt_replyend = NULL;
1490 int returnValue = 0;
1494 while (TTYROOM() > 2) {
1497 ring_consumed(&netiring, count);
1501 sbp = netiring.consume;
1502 scc = ring_full_consecutive(&netiring);
1504 /* No more data coming in */
1509 c = *sbp++ & 0xff, scc--; count++;
1511 switch (telrcv_state) {
1514 telrcv_state = TS_DATA;
1516 break; /* Ignore \0 after CR */
1518 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1522 /* Else, fall through */
1526 telrcv_state = TS_IAC;
1530 * The 'crmod' hack (see following) is needed
1531 * since we can't * set CRMOD on output only.
1532 * Machines like MULTICS like to send \r without
1533 * \n; since we must turn off CRMOD to get proper
1534 * input, the mapping is done here (sigh).
1536 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1540 sbp++, scc--; count++;
1543 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1545 sbp++, scc--; count++;
1555 telrcv_state = TS_CR;
1571 telrcv_state = TS_WILL;
1575 telrcv_state = TS_WONT;
1579 telrcv_state = TS_DO;
1583 telrcv_state = TS_DONT;
1588 * We may have missed an urgent notification,
1589 * so make sure we flush whatever is in the
1592 printoption("RCVD", IAC, DM);
1595 SYNCHing = stilloob();
1601 telrcv_state = TS_SB;
1611 printoption("RCVD", IAC, c);
1614 telrcv_state = TS_DATA;
1618 printoption("RCVD", WILL, c);
1620 telrcv_state = TS_DATA;
1624 printoption("RCVD", WONT, c);
1626 telrcv_state = TS_DATA;
1630 printoption("RCVD", DO, c);
1632 if (c == TELOPT_NAWS) {
1634 } else if (c == TELOPT_LFLOW) {
1639 telrcv_state = TS_DATA;
1643 printoption("RCVD", DONT, c);
1646 setconnmode(0); /* set new tty mode (maybe) */
1647 telrcv_state = TS_DATA;
1652 telrcv_state = TS_SE;
1662 * This is an error. We only expect to get
1663 * "IAC IAC" or "IAC SE". Several things may
1664 * have happend. An IAC was not doubled, the
1665 * IAC SE was left off, or another option got
1666 * inserted into the suboption are all possibilities.
1667 * If we assume that the IAC was not doubled,
1668 * and really the IAC SE was left off, we could
1669 * get into an infinate loop here. So, instead,
1670 * we terminate the suboption, and process the
1671 * partial suboption if we can.
1678 printoption("In SUBOPTION processing, RCVD", IAC, c);
1679 suboption(); /* handle sub-option */
1680 telrcv_state = TS_IAC;
1684 telrcv_state = TS_SB;
1690 suboption(); /* handle sub-option */
1691 telrcv_state = TS_DATA;
1696 ring_consumed(&netiring, count);
1697 return returnValue||count;
1700 static int bol = 1, local = 0;
1708 command(0, "z\n", 2);
1719 int returnValue = 0;
1724 while (NETROOM() > 2) {
1730 ring_consumed(&ttyiring, count);
1734 tbp = ttyiring.consume;
1735 tcc = ring_full_consecutive(&ttyiring);
1740 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1741 if (rlogin != _POSIX_VDISABLE) {
1750 if (sc == '.' || c == termEofChar) {
1752 command(0, "close\n", 6);
1755 if (sc == termSuspChar) {
1757 command(0, "z\n", 2);
1761 command(0, tbp, tcc);
1775 if ((sc == '\n') || (sc == '\r'))
1777 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1779 * Double escape is a pass through of a single escape character.
1781 if (tcc && strip(*tbp) == escape) {
1787 command(0, (char *)tbp, tcc);
1796 #ifdef KLUDGELINEMODE
1797 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1798 if (tcc > 0 && strip(*tbp) == echoc) {
1799 tcc--; tbp++; count++;
1801 dontlecho = !dontlecho;
1802 settimer(echotoggle);
1809 if (MODE_LOCAL_CHARS(globalmode)) {
1810 if (TerminalSpecialChars(sc) == 0) {
1815 if (my_want_state_is_wont(TELOPT_BINARY)) {
1819 * If we are in CRMOD mode (\r ==> \n)
1820 * on our local machine, then probably
1821 * a newline (unix) is CRLF (TELNET).
1823 if (MODE_LOCAL_CHARS(globalmode)) {
1827 bol = flushline = 1;
1831 NET2ADD('\r', '\0');
1833 NET2ADD('\r', '\n');
1835 bol = flushline = 1;
1844 } else if (c == IAC) {
1851 ring_consumed(&ttyiring, count);
1852 return returnValue||count; /* Non-zero if we did anything */
1858 * Try to do something.
1860 * If we do something useful, return 1; else return 0.
1865 Scheduler(int block)
1867 /* One wants to be a bit careful about setting returnValue
1868 * to one, since a one implies we did some useful work,
1869 * and therefore probably won't be called to block next
1872 int netin, netout, netex, ttyin, ttyout;
1874 /* Decide which rings should be processed */
1876 netout = ring_full_count(&netoring) &&
1878 (my_want_state_is_wont(TELOPT_LINEMODE)
1879 #ifdef KLUDGELINEMODE
1880 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
1883 my_want_state_is_will(TELOPT_BINARY));
1884 ttyout = ring_full_count(&ttyoring);
1886 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
1888 netin = !ISend && ring_empty_count(&netiring);
1892 /* Call to system code to process rings */
1894 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
1896 /* Now, look at the input rings, looking for work to do. */
1898 if (ring_full_count(&ttyiring)) {
1899 returnValue |= telsnd();
1902 if (ring_full_count(&netiring)) {
1903 returnValue |= telrcv();
1908 #define __unusedhere __unused
1910 * Select from tty and network...
1913 telnet(char *user __unusedhere)
1918 send_do(TELOPT_SGA, 1);
1919 send_will(TELOPT_TTYPE, 1);
1920 send_will(TELOPT_NAWS, 1);
1921 send_will(TELOPT_TSPEED, 1);
1922 send_will(TELOPT_LFLOW, 1);
1923 send_will(TELOPT_LINEMODE, 1);
1924 send_will(TELOPT_NEW_ENVIRON, 1);
1925 send_do(TELOPT_STATUS, 1);
1926 if (env_getvalue("DISPLAY"))
1927 send_will(TELOPT_XDISPLOC, 1);
1929 tel_enter_binary(eight);
1935 while ((schedValue = Scheduler(0)) != 0) {
1936 if (schedValue == -1) {
1942 if (Scheduler(1) == -1) {
1949 #if 0 /* XXX - this not being in is a bug */
1953 * Return the address of the next "item" in the TELNET data
1954 * stream. This will be the address of the next character if
1955 * the current address is a user data character, or it will
1956 * be the address of the character following the TELNET command
1957 * if the current address is a TELNET IAC ("I Am a Command")
1962 nextitem(char *current)
1964 if ((*current&0xff) != IAC) {
1967 switch (*(current+1)&0xff) {
1973 case SB: /* loop forever looking for the SE */
1975 char *look = current+2;
1978 if ((*look++&0xff) == IAC) {
1979 if ((*look++&0xff) == SE) {
1994 * We are about to do a TELNET SYNCH operation. Clear
1995 * the path to the network.
1997 * Things are a bit tricky since we may have sent the first
1998 * byte or so of a previous TELNET command into the network.
1999 * So, we have to scan the network buffer from the beginning
2000 * until we are up to where we want to be.
2002 * A side effect of what we do, just to keep things
2003 * simple, is to clear the urgent data pointer. The principal
2004 * caller should be setting the urgent data pointer AFTER calling
2015 * These routines add various telnet commands to the data stream.
2025 (void) ttyflush(1); /* Flush/drop output */
2026 /* do printoption AFTER flush, otherwise the output gets tossed... */
2027 printoption("SENT", DO, TELOPT_TM);
2034 printoption("SENT", IAC, AO);
2044 printoption("SENT", IAC, EL);
2051 printoption("SENT", IAC, EC);
2055 dosynch(char *ch __unused)
2057 netclear(); /* clear the path to the network */
2061 printoption("SENT", IAC, DM);
2065 int want_status_response = 0;
2068 get_status(char *ch __unused)
2070 unsigned char tmp[16];
2073 if (my_want_state_is_dont(TELOPT_STATUS)) {
2074 printf("Remote side does not support STATUS option\n");
2081 *cp++ = TELOPT_STATUS;
2082 *cp++ = TELQUAL_SEND;
2085 if (NETROOM() >= cp - tmp) {
2086 ring_supply_data(&netoring, tmp, cp-tmp);
2087 printsub('>', tmp+2, cp - tmp - 2);
2089 ++want_status_response;
2097 printoption("SENT", IAC, IP);
2110 NET2ADD(IAC, BREAK);
2111 printoption("SENT", IAC, BREAK);
2124 NET2ADD(IAC, ABORT);
2125 printoption("SENT", IAC, ABORT);
2139 printoption("SENT", IAC, SUSP);
2153 printoption("SENT", IAC, xEOF);
2160 printoption("SENT", IAC, AYT);
2164 * Send a window size update to the remote system.
2171 unsigned char tmp[16];
2174 if (my_state_is_wont(TELOPT_NAWS))
2177 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2178 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2180 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2188 *cp++ = TELOPT_NAWS;
2193 if (NETROOM() >= cp - tmp) {
2194 ring_supply_data(&netoring, tmp, cp-tmp);
2195 printsub('>', tmp+2, cp - tmp - 2);
2200 tel_enter_binary(int rw)
2203 send_do(TELOPT_BINARY, 1);
2205 send_will(TELOPT_BINARY, 1);
2209 tel_leave_binary(int rw)
2212 send_dont(TELOPT_BINARY, 1);
2214 send_wont(TELOPT_BINARY, 1);