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.2 2003/06/17 04:29:32 dillon 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 *slc_replyp;
1195 slc_start_reply(void)
1197 slc_replyp = slc_reply;
1198 *slc_replyp++ = IAC;
1200 *slc_replyp++ = TELOPT_LINEMODE;
1201 *slc_replyp++ = LM_SLC;
1205 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1207 if ((*slc_replyp++ = func) == IAC)
1208 *slc_replyp++ = IAC;
1209 if ((*slc_replyp++ = flags) == IAC)
1210 *slc_replyp++ = IAC;
1211 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1212 *slc_replyp++ = IAC;
1220 *slc_replyp++ = IAC;
1222 len = slc_replyp - slc_reply;
1225 if (NETROOM() > len) {
1226 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1227 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1229 /*@*/else printf("slc_end_reply: not enough room\n");
1236 int need_update = 0;
1238 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1239 if (!(spcp->flags&SLC_ACK))
1241 spcp->flags &= ~SLC_ACK;
1242 if (spcp->valp && (*spcp->valp != spcp->val)) {
1243 *spcp->valp = spcp->val;
1247 return(need_update);
1253 * Earlier version of telnet/telnetd from the BSD code had
1254 * the definitions of VALUE and VAR reversed. To ensure
1255 * maximum interoperability, we assume that the server is
1256 * an older BSD server, until proven otherwise. The newer
1257 * BSD servers should be able to handle either definition,
1258 * so it is better to use the wrong values if we don't
1259 * know what type of server it is.
1262 int old_env_var = OLD_ENV_VAR;
1263 int old_env_value = OLD_ENV_VALUE;
1265 # define old_env_var OLD_ENV_VAR
1266 # define old_env_value OLD_ENV_VALUE
1271 env_opt(unsigned char *buf, int len)
1273 unsigned char *ep = 0, *epc = 0;
1276 switch(buf[0]&0xff) {
1281 } else for (i = 1; i < len; i++) {
1282 switch (buf[i]&0xff) {
1286 if (telopt_environ == TELOPT_OLD_ENVIRON
1288 /* Server has the same definitions */
1289 old_env_var = OLD_ENV_VAR;
1290 old_env_value = OLD_ENV_VALUE;
1296 * Although OLD_ENV_VALUE is not legal, we will
1297 * still recognize it, just in case it is an
1298 * old server that has VAR & VALUE mixed up...
1309 ep = epc = &buf[i+1];
1329 /* Ignore for now. We shouldn't get it anyway. */
1337 #define OPT_REPLY_SIZE 256
1338 unsigned char *opt_reply;
1339 unsigned char *opt_replyp;
1340 unsigned char *opt_replyend;
1346 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1348 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1349 if (opt_reply == NULL) {
1350 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1351 opt_reply = opt_replyp = opt_replyend = NULL;
1354 opt_replyp = opt_reply;
1355 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1356 *opt_replyp++ = IAC;
1358 *opt_replyp++ = telopt_environ;
1359 *opt_replyp++ = TELQUAL_IS;
1363 env_opt_start_info(void)
1367 opt_replyp[-1] = TELQUAL_INFO;
1371 env_opt_add(unsigned char *ep)
1373 unsigned char *vp, c;
1375 if (opt_reply == NULL) /*XXX*/
1378 if (ep == NULL || *ep == '\0') {
1379 /* Send user defined variables first. */
1381 while ((ep = env_default(0, 0)))
1384 /* Now add the list of well know variables. */
1386 while ((ep = env_default(0, 1)))
1390 vp = env_getvalue(ep);
1391 if (opt_replyp + (vp ? strlen((char *)vp) : 0) +
1392 strlen((char *)ep) + 6 > opt_replyend)
1395 opt_replyend += OPT_REPLY_SIZE;
1396 len = opt_replyend - opt_reply;
1397 opt_reply = (unsigned char *)realloc(opt_reply, len);
1398 if (opt_reply == NULL) {
1399 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1400 opt_reply = opt_replyp = opt_replyend = NULL;
1403 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1404 opt_replyend = opt_reply + len;
1406 if (opt_welldefined(ep))
1408 if (telopt_environ == TELOPT_OLD_ENVIRON)
1409 *opt_replyp++ = old_env_var;
1412 *opt_replyp++ = NEW_ENV_VAR;
1414 *opt_replyp++ = ENV_USERVAR;
1416 while ((c = *ep++)) {
1419 *opt_replyp++ = IAC;
1425 *opt_replyp++ = ENV_ESC;
1432 if (telopt_environ == TELOPT_OLD_ENVIRON)
1433 *opt_replyp++ = old_env_value;
1436 *opt_replyp++ = NEW_ENV_VALUE;
1444 opt_welldefined(const char *ep)
1446 if ((strcmp(ep, "USER") == 0) ||
1447 (strcmp(ep, "DISPLAY") == 0) ||
1448 (strcmp(ep, "PRINTER") == 0) ||
1449 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1450 (strcmp(ep, "JOB") == 0) ||
1451 (strcmp(ep, "ACCT") == 0))
1457 env_opt_end(int emptyok)
1461 len = opt_replyp - opt_reply + 2;
1462 if (emptyok || len > 6) {
1463 *opt_replyp++ = IAC;
1465 if (NETROOM() > len) {
1466 ring_supply_data(&netoring, opt_reply, len);
1467 printsub('>', &opt_reply[2], len - 2);
1469 /*@*/ else printf("slc_end_reply: not enough room\n");
1473 opt_reply = opt_replyp = opt_replyend = NULL;
1486 int returnValue = 0;
1490 while (TTYROOM() > 2) {
1493 ring_consumed(&netiring, count);
1497 sbp = netiring.consume;
1498 scc = ring_full_consecutive(&netiring);
1500 /* No more data coming in */
1505 c = *sbp++ & 0xff, scc--; count++;
1507 switch (telrcv_state) {
1510 telrcv_state = TS_DATA;
1512 break; /* Ignore \0 after CR */
1514 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1518 /* Else, fall through */
1522 telrcv_state = TS_IAC;
1526 * The 'crmod' hack (see following) is needed
1527 * since we can't * set CRMOD on output only.
1528 * Machines like MULTICS like to send \r without
1529 * \n; since we must turn off CRMOD to get proper
1530 * input, the mapping is done here (sigh).
1532 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1536 sbp++, scc--; count++;
1539 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1541 sbp++, scc--; count++;
1551 telrcv_state = TS_CR;
1567 telrcv_state = TS_WILL;
1571 telrcv_state = TS_WONT;
1575 telrcv_state = TS_DO;
1579 telrcv_state = TS_DONT;
1584 * We may have missed an urgent notification,
1585 * so make sure we flush whatever is in the
1588 printoption("RCVD", IAC, DM);
1591 SYNCHing = stilloob();
1597 telrcv_state = TS_SB;
1607 printoption("RCVD", IAC, c);
1610 telrcv_state = TS_DATA;
1614 printoption("RCVD", WILL, c);
1616 telrcv_state = TS_DATA;
1620 printoption("RCVD", WONT, c);
1622 telrcv_state = TS_DATA;
1626 printoption("RCVD", DO, c);
1628 if (c == TELOPT_NAWS) {
1630 } else if (c == TELOPT_LFLOW) {
1635 telrcv_state = TS_DATA;
1639 printoption("RCVD", DONT, c);
1642 setconnmode(0); /* set new tty mode (maybe) */
1643 telrcv_state = TS_DATA;
1648 telrcv_state = TS_SE;
1658 * This is an error. We only expect to get
1659 * "IAC IAC" or "IAC SE". Several things may
1660 * have happend. An IAC was not doubled, the
1661 * IAC SE was left off, or another option got
1662 * inserted into the suboption are all possibilities.
1663 * If we assume that the IAC was not doubled,
1664 * and really the IAC SE was left off, we could
1665 * get into an infinate loop here. So, instead,
1666 * we terminate the suboption, and process the
1667 * partial suboption if we can.
1674 printoption("In SUBOPTION processing, RCVD", IAC, c);
1675 suboption(); /* handle sub-option */
1676 telrcv_state = TS_IAC;
1680 telrcv_state = TS_SB;
1686 suboption(); /* handle sub-option */
1687 telrcv_state = TS_DATA;
1692 ring_consumed(&netiring, count);
1693 return returnValue||count;
1696 static int bol = 1, local = 0;
1704 command(0, "z\n", 2);
1715 int returnValue = 0;
1720 while (NETROOM() > 2) {
1726 ring_consumed(&ttyiring, count);
1730 tbp = ttyiring.consume;
1731 tcc = ring_full_consecutive(&ttyiring);
1736 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1737 if (rlogin != _POSIX_VDISABLE) {
1746 if (sc == '.' || c == termEofChar) {
1748 command(0, "close\n", 6);
1751 if (sc == termSuspChar) {
1753 command(0, "z\n", 2);
1757 command(0, tbp, tcc);
1771 if ((sc == '\n') || (sc == '\r'))
1773 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1775 * Double escape is a pass through of a single escape character.
1777 if (tcc && strip(*tbp) == escape) {
1783 command(0, (char *)tbp, tcc);
1792 #ifdef KLUDGELINEMODE
1793 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1794 if (tcc > 0 && strip(*tbp) == echoc) {
1795 tcc--; tbp++; count++;
1797 dontlecho = !dontlecho;
1798 settimer(echotoggle);
1805 if (MODE_LOCAL_CHARS(globalmode)) {
1806 if (TerminalSpecialChars(sc) == 0) {
1811 if (my_want_state_is_wont(TELOPT_BINARY)) {
1815 * If we are in CRMOD mode (\r ==> \n)
1816 * on our local machine, then probably
1817 * a newline (unix) is CRLF (TELNET).
1819 if (MODE_LOCAL_CHARS(globalmode)) {
1823 bol = flushline = 1;
1827 NET2ADD('\r', '\0');
1829 NET2ADD('\r', '\n');
1831 bol = flushline = 1;
1840 } else if (c == IAC) {
1847 ring_consumed(&ttyiring, count);
1848 return returnValue||count; /* Non-zero if we did anything */
1854 * Try to do something.
1856 * If we do something useful, return 1; else return 0.
1861 Scheduler(int block)
1863 /* One wants to be a bit careful about setting returnValue
1864 * to one, since a one implies we did some useful work,
1865 * and therefore probably won't be called to block next
1868 int netin, netout, netex, ttyin, ttyout;
1870 /* Decide which rings should be processed */
1872 netout = ring_full_count(&netoring) &&
1874 (my_want_state_is_wont(TELOPT_LINEMODE)
1875 #ifdef KLUDGELINEMODE
1876 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
1879 my_want_state_is_will(TELOPT_BINARY));
1880 ttyout = ring_full_count(&ttyoring);
1882 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
1884 netin = !ISend && ring_empty_count(&netiring);
1888 /* Call to system code to process rings */
1890 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
1892 /* Now, look at the input rings, looking for work to do. */
1894 if (ring_full_count(&ttyiring)) {
1895 returnValue |= telsnd();
1898 if (ring_full_count(&netiring)) {
1899 returnValue |= telrcv();
1904 #define __unusedhere __unused
1906 * Select from tty and network...
1909 telnet(char *user __unusedhere)
1914 send_do(TELOPT_SGA, 1);
1915 send_will(TELOPT_TTYPE, 1);
1916 send_will(TELOPT_NAWS, 1);
1917 send_will(TELOPT_TSPEED, 1);
1918 send_will(TELOPT_LFLOW, 1);
1919 send_will(TELOPT_LINEMODE, 1);
1920 send_will(TELOPT_NEW_ENVIRON, 1);
1921 send_do(TELOPT_STATUS, 1);
1922 if (env_getvalue("DISPLAY"))
1923 send_will(TELOPT_XDISPLOC, 1);
1925 tel_enter_binary(eight);
1931 while ((schedValue = Scheduler(0)) != 0) {
1932 if (schedValue == -1) {
1938 if (Scheduler(1) == -1) {
1945 #if 0 /* XXX - this not being in is a bug */
1949 * Return the address of the next "item" in the TELNET data
1950 * stream. This will be the address of the next character if
1951 * the current address is a user data character, or it will
1952 * be the address of the character following the TELNET command
1953 * if the current address is a TELNET IAC ("I Am a Command")
1958 nextitem(char *current)
1960 if ((*current&0xff) != IAC) {
1963 switch (*(current+1)&0xff) {
1969 case SB: /* loop forever looking for the SE */
1971 char *look = current+2;
1974 if ((*look++&0xff) == IAC) {
1975 if ((*look++&0xff) == SE) {
1990 * We are about to do a TELNET SYNCH operation. Clear
1991 * the path to the network.
1993 * Things are a bit tricky since we may have sent the first
1994 * byte or so of a previous TELNET command into the network.
1995 * So, we have to scan the network buffer from the beginning
1996 * until we are up to where we want to be.
1998 * A side effect of what we do, just to keep things
1999 * simple, is to clear the urgent data pointer. The principal
2000 * caller should be setting the urgent data pointer AFTER calling
2011 * These routines add various telnet commands to the data stream.
2021 (void) ttyflush(1); /* Flush/drop output */
2022 /* do printoption AFTER flush, otherwise the output gets tossed... */
2023 printoption("SENT", DO, TELOPT_TM);
2030 printoption("SENT", IAC, AO);
2040 printoption("SENT", IAC, EL);
2047 printoption("SENT", IAC, EC);
2051 dosynch(char *ch __unused)
2053 netclear(); /* clear the path to the network */
2057 printoption("SENT", IAC, DM);
2061 int want_status_response = 0;
2064 get_status(char *ch __unused)
2066 unsigned char tmp[16];
2069 if (my_want_state_is_dont(TELOPT_STATUS)) {
2070 printf("Remote side does not support STATUS option\n");
2077 *cp++ = TELOPT_STATUS;
2078 *cp++ = TELQUAL_SEND;
2081 if (NETROOM() >= cp - tmp) {
2082 ring_supply_data(&netoring, tmp, cp-tmp);
2083 printsub('>', tmp+2, cp - tmp - 2);
2085 ++want_status_response;
2093 printoption("SENT", IAC, IP);
2106 NET2ADD(IAC, BREAK);
2107 printoption("SENT", IAC, BREAK);
2120 NET2ADD(IAC, ABORT);
2121 printoption("SENT", IAC, ABORT);
2135 printoption("SENT", IAC, SUSP);
2149 printoption("SENT", IAC, xEOF);
2156 printoption("SENT", IAC, AYT);
2160 * Send a window size update to the remote system.
2167 unsigned char tmp[16];
2170 if (my_state_is_wont(TELOPT_NAWS))
2173 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2174 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2176 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2184 *cp++ = TELOPT_NAWS;
2189 if (NETROOM() >= cp - tmp) {
2190 ring_supply_data(&netoring, tmp, cp-tmp);
2191 printsub('>', tmp+2, cp - tmp - 2);
2196 tel_enter_binary(int rw)
2199 send_do(TELOPT_BINARY, 1);
2201 send_will(TELOPT_BINARY, 1);
2205 tel_leave_binary(int rw)
2208 send_dont(TELOPT_BINARY, 1);
2210 send_wont(TELOPT_BINARY, 1);