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: src/crypto/telnet/telnet/telnet.c,v 1.4.2.5 2002/04/13 10:59:08 markm Exp $");
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>
65 #include <libtelnet/auth.h>
68 #include <libtelnet/encrypt.h>
70 #include <libtelnet/misc.h>
72 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
74 static unsigned char subbuffer[SUBBUFSIZE],
75 *subpointer, *subend; /* buffer for sub-options */
76 #define SB_CLEAR() subpointer = subbuffer;
77 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
78 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
79 *subpointer++ = (c); \
82 #define SB_GET() ((*subpointer++)&0xff)
83 #define SB_PEEK() ((*subpointer)&0xff)
84 #define SB_EOF() (subpointer >= subend)
85 #define SB_LEN() (subend - subpointer)
87 char options[256]; /* The combined options */
88 char do_dont_resp[256];
89 char will_wont_resp[256];
93 autologin = 0, /* Autologin anyone? */
97 ISend, /* trying to send network data in */
100 netdata, /* Print out network data flow */
101 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
103 SYNCHing, /* we are in TELNET SYNCH mode */
104 flushout, /* flush output */
105 autoflush = 0, /* flush output when interrupting? */
106 autosynch, /* send interrupt characters with SYNCH? */
107 localflow, /* we handle flow control locally */
108 restartany, /* if flow control enabled, restart on any character */
109 localchars, /* we recognize interrupt/quit */
110 donelclchars, /* the user has set "localchars" */
111 donebinarytoggle, /* the user has put us in binary */
112 dontlecho, /* do we suppress local echoing right now? */
114 doaddrlookup = 1, /* do a reverse address lookup? */
119 char *line; /* hack around breakage in sra.c :-( !! */
124 #ifdef KLUDGELINEMODE
129 * Telnet receiver states for fsm
138 #define TS_SB 7 /* sub-option collection */
139 #define TS_SE 8 /* looking for sub-option end */
141 static int telrcv_state;
143 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
145 # define telopt_environ TELOPT_NEW_ENVIRON
154 #ifdef KLUDGELINEMODE
155 int kludgelinemode = 1;
158 static int is_unique(char *, char **, char **);
161 * The following are some clocks used to decide how to interpret
162 * the relationship between various variables.
168 * Initialize telnet environment.
179 connected = ISend = localflow = donebinarytoggle = 0;
180 #ifdef AUTHENTICATION
182 auth_encrypt_connect(connected);
189 /* Don't change NetTrace */
191 escape = CONTROL(']');
192 rlogin = _POSIX_VDISABLE;
193 #ifdef KLUDGELINEMODE
194 echoc = CONTROL('E');
198 telrcv_state = TS_DATA;
203 * These routines are in charge of sending option negotiations
206 * The basic idea is that we send the negotiation if either side
207 * is in disagreement as to what the current state should be.
211 send_do(int c, int init)
214 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
215 my_want_state_is_do(c))
217 set_my_want_state_do(c);
222 printoption("SENT", DO, c);
226 send_dont(int c, int init)
229 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
230 my_want_state_is_dont(c))
232 set_my_want_state_dont(c);
237 printoption("SENT", DONT, c);
241 send_will(int c, int init)
244 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
245 my_want_state_is_will(c))
247 set_my_want_state_will(c);
252 printoption("SENT", WILL, c);
256 send_wont(int c, int init)
259 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
260 my_want_state_is_wont(c))
262 set_my_want_state_wont(c);
267 printoption("SENT", WONT, c);
271 willoption(int option)
273 int new_state_ok = 0;
275 if (do_dont_resp[option]) {
276 --do_dont_resp[option];
277 if (do_dont_resp[option] && my_state_is_do(option))
278 --do_dont_resp[option];
281 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
288 settimer(modenegotiated);
291 #ifdef AUTHENTICATION
292 case TELOPT_AUTHENTICATION:
296 #endif /* ENCRYPTION */
304 * Special case for TM. If we get back a WILL,
305 * pretend we got back a WONT.
307 set_my_want_state_dont(option);
308 set_my_state_dont(option);
309 return; /* Never reply to TM will's/wont's */
311 case TELOPT_LINEMODE:
317 set_my_want_state_do(option);
319 setconnmode(0); /* possibly set new tty mode */
321 do_dont_resp[option]++;
322 send_dont(option, 0);
325 set_my_state_do(option);
327 if (option == TELOPT_ENCRYPT)
328 encrypt_send_support();
329 #endif /* ENCRYPTION */
333 wontoption(int option)
335 if (do_dont_resp[option]) {
336 --do_dont_resp[option];
337 if (do_dont_resp[option] && my_state_is_dont(option))
338 --do_dont_resp[option];
341 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
345 #ifdef KLUDGELINEMODE
352 settimer(modenegotiated);
358 set_my_want_state_dont(option);
359 set_my_state_dont(option);
360 return; /* Never reply to TM will's/wont's */
365 set_my_want_state_dont(option);
366 if (my_state_is_do(option))
367 send_dont(option, 0);
368 setconnmode(0); /* Set new tty mode */
369 } else if (option == TELOPT_TM) {
371 * Special case for TM.
375 set_my_want_state_dont(option);
377 set_my_state_dont(option);
383 int new_state_ok = 0;
385 if (will_wont_resp[option]) {
386 --will_wont_resp[option];
387 if (will_wont_resp[option] && my_state_is_will(option))
388 --will_wont_resp[option];
391 if (will_wont_resp[option] == 0) {
392 if (my_want_state_is_wont(option)) {
398 * Special case for TM. We send a WILL, but pretend
401 send_will(option, 0);
402 set_my_want_state_wont(TELOPT_TM);
403 set_my_state_wont(TELOPT_TM);
406 case TELOPT_BINARY: /* binary mode */
407 case TELOPT_NAWS: /* window size */
408 case TELOPT_TSPEED: /* terminal speed */
409 case TELOPT_LFLOW: /* local flow control */
410 case TELOPT_TTYPE: /* terminal type option */
411 case TELOPT_SGA: /* no big deal */
413 case TELOPT_ENCRYPT: /* encryption variable option */
414 #endif /* ENCRYPTION */
418 case TELOPT_NEW_ENVIRON: /* New environment variable option */
420 if (my_state_is_will(TELOPT_OLD_ENVIRON))
421 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
423 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
424 if (my_state_is_will(TELOPT_NEW_ENVIRON))
425 break; /* Don't enable if new one is in use! */
427 telopt_environ = option;
432 #ifdef AUTHENTICATION
433 case TELOPT_AUTHENTICATION:
439 case TELOPT_XDISPLOC: /* X Display location */
440 if (env_getvalue("DISPLAY"))
444 case TELOPT_LINEMODE:
445 #ifdef KLUDGELINEMODE
447 send_do(TELOPT_SGA, 1);
449 set_my_want_state_will(TELOPT_LINEMODE);
450 send_will(option, 0);
451 set_my_state_will(TELOPT_LINEMODE);
455 case TELOPT_ECHO: /* We're never going to echo... */
461 set_my_want_state_will(option);
462 send_will(option, 0);
463 setconnmode(0); /* Set new tty mode */
465 will_wont_resp[option]++;
466 send_wont(option, 0);
470 * Handle options that need more things done after the
471 * other side has acknowledged the option.
474 case TELOPT_LINEMODE:
475 #ifdef KLUDGELINEMODE
477 send_do(TELOPT_SGA, 1);
479 set_my_state_will(option);
481 send_do(TELOPT_SGA, 0);
486 set_my_state_will(option);
490 dontoption(int option)
493 if (will_wont_resp[option]) {
494 --will_wont_resp[option];
495 if (will_wont_resp[option] && my_state_is_wont(option))
496 --will_wont_resp[option];
499 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
501 case TELOPT_LINEMODE:
502 linemode = 0; /* put us back to the default state */
505 case TELOPT_NEW_ENVIRON:
507 * The new environ option wasn't recognized, try
510 send_will(TELOPT_OLD_ENVIRON, 1);
511 telopt_environ = TELOPT_OLD_ENVIRON;
515 /* we always accept a DONT */
516 set_my_want_state_wont(option);
517 if (my_state_is_will(option))
518 send_wont(option, 0);
519 setconnmode(0); /* Set new tty mode */
521 set_my_state_wont(option);
525 * Given a buffer returned by tgetent(), this routine will turn
526 * the pipe separated list of names in the buffer into an array
527 * of pointers to null terminated names. We toss out any bad,
528 * duplicate, or verbose names (names with spaces).
531 static const char *name_unknown = "UNKNOWN";
532 static const char *unknown[] = { NULL, NULL };
535 mklist(char *buf, char *name)
538 char c, *cp, **argvp, *cp2, **argv, **avt;
541 if (strlen(name) > 40) {
543 unknown[0] = name_unknown;
549 unknown[0] = name_unknown;
551 * Count up the number of names.
553 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
558 * Allocate an array to put the name pointers into
560 argv = (char **)malloc((n+3)*sizeof(char *));
565 * Fill up the array of pointers to names.
570 for (cp = cp2 = buf; (c = *cp); cp++) {
571 if (c == '|' || c == ':') {
574 * Skip entries that have spaces or are over 40
575 * characters long. If this is our environment
576 * name, then put it up front. Otherwise, as
577 * long as this is not a duplicate name (case
578 * insensitive) add it to the list.
580 if (n || (cp - cp2 > 41))
582 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
584 else if (is_unique(cp2, argv+1, argvp))
589 * Skip multiple delimiters. Reset cp2 to
590 * the beginning of the next name. Reset n,
591 * the flag for names with spaces.
593 while ((c = *cp) == '|')
599 * Skip entries with spaces or non-ascii values.
600 * Convert lower case letters to upper case.
602 if ((c == ' ') || !isascii(c))
609 * Check for an old V6 2 character name. If the second
610 * name points to the beginning of the buffer, and is
611 * only 2 characters long, move it to the end of the array.
613 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
615 for (avt = &argv[1]; avt < argvp; avt++)
621 * Duplicate last name, for TTYPE option, and null
622 * terminate the array. If we didn't find a match on
623 * our terminal name, put that name at the beginning.
634 for (avt = argv; avt < argvp; avt++)
639 return((const char **)argv);
645 is_unique(char *name, char **as, char **ae)
650 n = strlen(name) + 1;
651 for (ap = as; ap < ae; ap++)
652 if (strncasecmp(*ap, name, n) == 0)
662 setupterm(char *tname, int fd, int *errp)
664 if (tgetent(termbuf, tname) == 1) {
665 termbuf[1023] = '\0';
675 #define termbuf ttytype
676 extern char ttytype[];
679 int resettermname = 1;
685 static const char **tnamep = 0;
686 static const char **next;
691 if (tnamep && tnamep != unknown)
693 if ((tname = env_getvalue("TERM")) &&
694 (setupterm(tname, 1, &err) == 0)) {
695 tnamep = mklist(termbuf, tname);
697 if (tname && (strlen(tname) <= 40)) {
701 unknown[0] = name_unknown;
713 * Look at the sub-option buffer, and try to be helpful to the other
716 * Currently we recognize:
718 * Terminal type, send request.
719 * Terminal speed (send request).
720 * Local flow control (is request).
727 unsigned char subchar;
729 printsub('<', subbuffer, SB_LEN()+2);
730 switch (subchar = SB_GET()) {
732 if (my_want_state_is_wont(TELOPT_TTYPE))
734 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
738 unsigned char temp[50];
741 name = gettermname();
742 len = strlen(name) + 4 + 2;
743 if (len < NETROOM()) {
744 sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
745 TELQUAL_IS, name, IAC, SE);
746 ring_supply_data(&netoring, temp, len);
747 printsub('>', &temp[2], len-2);
749 ExitString("No room in buffer for terminal type.\n", 1);
755 if (my_want_state_is_wont(TELOPT_TSPEED))
759 if (SB_GET() == TELQUAL_SEND) {
761 unsigned char temp[50];
764 TerminalSpeeds(&ispeed, &ospeed);
766 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
767 TELQUAL_IS, ospeed, ispeed, IAC, SE);
768 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
770 if (len < NETROOM()) {
771 ring_supply_data(&netoring, temp, len);
772 printsub('>', temp+2, len - 2);
774 /*@*/ else printf("lm_will: not enough room in buffer\n");
778 if (my_want_state_is_wont(TELOPT_LFLOW))
783 case LFLOW_RESTART_ANY:
786 case LFLOW_RESTART_XON:
802 case TELOPT_LINEMODE:
803 if (my_want_state_is_wont(TELOPT_LINEMODE))
809 lm_will(subpointer, SB_LEN());
812 lm_wont(subpointer, SB_LEN());
815 lm_do(subpointer, SB_LEN());
818 lm_dont(subpointer, SB_LEN());
821 slc(subpointer, SB_LEN());
824 lm_mode(subpointer, SB_LEN(), 0);
832 case TELOPT_OLD_ENVIRON:
834 case TELOPT_NEW_ENVIRON:
840 if (my_want_state_is_dont(subchar))
844 if (my_want_state_is_wont(subchar)) {
851 env_opt(subpointer, SB_LEN());
854 case TELOPT_XDISPLOC:
855 if (my_want_state_is_wont(TELOPT_XDISPLOC))
859 if (SB_GET() == TELQUAL_SEND) {
860 unsigned char temp[50], *dp;
863 if ((dp = env_getvalue("DISPLAY")) == NULL ||
864 strlen(dp) > sizeof(temp) - 7) {
866 * Something happened, we no longer have a DISPLAY
867 * variable. Or it is too long. So, turn off the option.
869 send_wont(TELOPT_XDISPLOC, 1);
872 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
873 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
874 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
876 if (len < NETROOM()) {
877 ring_supply_data(&netoring, temp, len);
878 printsub('>', temp+2, len - 2);
880 /*@*/ else printf("lm_will: not enough room in buffer\n");
884 #ifdef AUTHENTICATION
885 case TELOPT_AUTHENTICATION: {
892 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
894 auth_is(subpointer, SB_LEN());
897 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
899 auth_send(subpointer, SB_LEN());
902 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
904 auth_reply(subpointer, SB_LEN());
907 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
909 auth_name(subpointer, SB_LEN());
921 if (my_want_state_is_dont(TELOPT_ENCRYPT))
923 encrypt_start(subpointer, SB_LEN());
926 if (my_want_state_is_dont(TELOPT_ENCRYPT))
930 case ENCRYPT_SUPPORT:
931 if (my_want_state_is_wont(TELOPT_ENCRYPT))
933 encrypt_support(subpointer, SB_LEN());
935 case ENCRYPT_REQSTART:
936 if (my_want_state_is_wont(TELOPT_ENCRYPT))
938 encrypt_request_start(subpointer, SB_LEN());
941 if (my_want_state_is_wont(TELOPT_ENCRYPT))
944 * We can always send an REQEND so that we cannot
945 * get stuck encrypting. We should only get this
946 * if we have been able to get in the correct mode
949 encrypt_request_end();
952 if (my_want_state_is_dont(TELOPT_ENCRYPT))
954 encrypt_is(subpointer, SB_LEN());
957 if (my_want_state_is_wont(TELOPT_ENCRYPT))
959 encrypt_reply(subpointer, SB_LEN());
961 case ENCRYPT_ENC_KEYID:
962 if (my_want_state_is_dont(TELOPT_ENCRYPT))
964 encrypt_enc_keyid(subpointer, SB_LEN());
966 case ENCRYPT_DEC_KEYID:
967 if (my_want_state_is_wont(TELOPT_ENCRYPT))
969 encrypt_dec_keyid(subpointer, SB_LEN());
975 #endif /* ENCRYPTION */
981 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
984 lm_will(unsigned char *cmd, int len)
987 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
991 case LM_FORWARDMASK: /* We shouldn't ever get this... */
995 if (NETROOM() > (int)sizeof(str_lm)) {
996 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
997 printsub('>', &str_lm[2], sizeof(str_lm)-2);
999 /*@*/ else printf("lm_will: not enough room in buffer\n");
1005 lm_wont(unsigned char *cmd, int len)
1008 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1012 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1014 /* We are always DONT, so don't respond */
1020 lm_do(unsigned char *cmd, int len)
1023 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1027 case LM_FORWARDMASK:
1031 if (NETROOM() > (int)sizeof(str_lm)) {
1032 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1033 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1035 /*@*/ else printf("lm_do: not enough room in buffer\n");
1041 lm_dont(unsigned char *cmd, int len)
1044 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1048 case LM_FORWARDMASK:
1050 /* we are always WONT, so don't respond */
1055 static unsigned char str_lm_mode[] = {
1056 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1060 lm_mode(unsigned char *cmd, int len, int init)
1064 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1068 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1069 str_lm_mode[4] = linemode;
1071 str_lm_mode[4] |= MODE_ACK;
1072 if (NETROOM() > (int)sizeof(str_lm_mode)) {
1073 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1074 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1076 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1077 setconnmode(0); /* set changed mode */
1084 * Handle special character suboption of LINEMODE.
1090 char flags; /* Current flags & level */
1091 char mylevel; /* Maximum level & flags */
1094 #define SLC_IMPORT 0
1095 #define SLC_EXPORT 1
1096 #define SLC_RVALUE 2
1097 static int slc_mode = SLC_EXPORT;
1105 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1108 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1111 #define initfunc(func, flags) { \
1112 spcp = &spc_data[func]; \
1113 if ((spcp->valp = tcval(func))) { \
1114 spcp->val = *spcp->valp; \
1115 spcp->mylevel = SLC_VARIABLE|flags; \
1118 spcp->mylevel = SLC_DEFAULT; \
1122 initfunc(SLC_SYNCH, 0);
1124 initfunc(SLC_AO, 0);
1125 initfunc(SLC_AYT, 0);
1127 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1128 initfunc(SLC_EOF, 0);
1130 initfunc(SLC_SUSP, SLC_FLUSHIN);
1132 initfunc(SLC_EC, 0);
1133 initfunc(SLC_EL, 0);
1135 initfunc(SLC_EW, 0);
1136 initfunc(SLC_RP, 0);
1137 initfunc(SLC_LNEXT, 0);
1139 initfunc(SLC_XON, 0);
1140 initfunc(SLC_XOFF, 0);
1142 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1143 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1145 initfunc(SLC_FORW1, 0);
1147 initfunc(SLC_FORW2, 0);
1151 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1154 if (slc_mode == SLC_EXPORT)
1164 printf("Special characters are %s values\n",
1165 slc_mode == SLC_IMPORT ? "remote default" :
1166 slc_mode == SLC_EXPORT ? "local" :
1171 slc_mode_export(void)
1173 slc_mode = SLC_EXPORT;
1174 if (my_state_is_will(TELOPT_LINEMODE))
1179 slc_mode_import(int def)
1181 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1182 if (my_state_is_will(TELOPT_LINEMODE))
1186 unsigned char slc_import_val[] = {
1187 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1189 unsigned char slc_import_def[] = {
1190 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1196 if (NETROOM() > (int)sizeof(slc_import_val)) {
1198 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1199 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1201 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1202 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1205 /*@*/ else printf("slc_import: not enough room\n");
1213 TerminalDefaultChars();
1216 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1217 if (spcp->mylevel != SLC_NOSUPPORT) {
1218 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1219 spcp->flags = SLC_NOSUPPORT;
1221 spcp->flags = spcp->mylevel;
1223 spcp->val = *spcp->valp;
1224 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1229 setconnmode(1); /* Make sure the character values are set */
1233 slc(unsigned char *cp, int len)
1240 for (; len >= 3; len -=3, cp +=3) {
1242 func = cp[SLC_FUNC];
1246 * Client side: always ignore 0 function.
1251 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1252 slc_add_reply(func, SLC_NOSUPPORT, 0);
1256 spcp = &spc_data[func];
1258 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1260 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1261 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1265 if (level == (SLC_DEFAULT|SLC_ACK)) {
1267 * This is an error condition, the SLC_ACK
1268 * bit should never be set for the SLC_DEFAULT
1269 * level. Our best guess to recover is to
1270 * ignore the SLC_ACK bit.
1272 cp[SLC_FLAGS] &= ~SLC_ACK;
1275 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1276 spcp->val = (cc_t)cp[SLC_VALUE];
1277 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1283 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1284 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1285 spcp->val = (cc_t)cp[SLC_VALUE];
1287 if (level == SLC_DEFAULT) {
1288 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1289 spcp->flags = spcp->mylevel;
1291 spcp->flags = SLC_NOSUPPORT;
1293 slc_add_reply(func, spcp->flags, spcp->val);
1297 setconnmode(1); /* set the new character values */
1306 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1307 if (spcp->valp && spcp->val != *spcp->valp) {
1308 spcp->val = *spcp->valp;
1309 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1310 spcp->flags = SLC_NOSUPPORT;
1312 spcp->flags = spcp->mylevel;
1313 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1320 unsigned char slc_reply[128];
1321 unsigned char *slc_replyp;
1324 slc_start_reply(void)
1326 slc_replyp = slc_reply;
1327 *slc_replyp++ = IAC;
1329 *slc_replyp++ = TELOPT_LINEMODE;
1330 *slc_replyp++ = LM_SLC;
1334 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1336 if ((*slc_replyp++ = func) == IAC)
1337 *slc_replyp++ = IAC;
1338 if ((*slc_replyp++ = flags) == IAC)
1339 *slc_replyp++ = IAC;
1340 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1341 *slc_replyp++ = IAC;
1349 *slc_replyp++ = IAC;
1351 len = slc_replyp - slc_reply;
1354 if (NETROOM() > len) {
1355 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1356 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1358 /*@*/else printf("slc_end_reply: not enough room\n");
1365 int need_update = 0;
1367 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1368 if (!(spcp->flags&SLC_ACK))
1370 spcp->flags &= ~SLC_ACK;
1371 if (spcp->valp && (*spcp->valp != spcp->val)) {
1372 *spcp->valp = spcp->val;
1376 return(need_update);
1382 * Earlier version of telnet/telnetd from the BSD code had
1383 * the definitions of VALUE and VAR reversed. To ensure
1384 * maximum interoperability, we assume that the server is
1385 * an older BSD server, until proven otherwise. The newer
1386 * BSD servers should be able to handle either definition,
1387 * so it is better to use the wrong values if we don't
1388 * know what type of server it is.
1391 int old_env_var = OLD_ENV_VAR;
1392 int old_env_value = OLD_ENV_VALUE;
1394 # define old_env_var OLD_ENV_VAR
1395 # define old_env_value OLD_ENV_VALUE
1400 env_opt(unsigned char *buf, int len)
1402 unsigned char *ep = 0, *epc = 0;
1405 switch(buf[0]&0xff) {
1410 } else for (i = 1; i < len; i++) {
1411 switch (buf[i]&0xff) {
1415 if (telopt_environ == TELOPT_OLD_ENVIRON
1417 /* Server has the same definitions */
1418 old_env_var = OLD_ENV_VAR;
1419 old_env_value = OLD_ENV_VALUE;
1425 * Although OLD_ENV_VALUE is not legal, we will
1426 * still recognize it, just in case it is an
1427 * old server that has VAR & VALUE mixed up...
1438 ep = epc = &buf[i+1];
1458 /* Ignore for now. We shouldn't get it anyway. */
1466 #define OPT_REPLY_SIZE 256
1467 unsigned char *opt_reply;
1468 unsigned char *opt_replyp;
1469 unsigned char *opt_replyend;
1475 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1477 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1478 if (opt_reply == NULL) {
1479 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1480 opt_reply = opt_replyp = opt_replyend = NULL;
1483 opt_replyp = opt_reply;
1484 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1485 *opt_replyp++ = IAC;
1487 *opt_replyp++ = telopt_environ;
1488 *opt_replyp++ = TELQUAL_IS;
1492 env_opt_start_info(void)
1496 opt_replyp[-1] = TELQUAL_INFO;
1500 env_opt_add(unsigned char *ep)
1502 unsigned char *vp, c;
1504 if (opt_reply == NULL) /*XXX*/
1507 if (ep == NULL || *ep == '\0') {
1508 /* Send user defined variables first. */
1510 while ((ep = env_default(0, 0)))
1513 /* Now add the list of well know variables. */
1515 while ((ep = env_default(0, 1)))
1519 vp = env_getvalue(ep);
1520 if (opt_replyp + (vp ? strlen((char *)vp) : 0) +
1521 strlen((char *)ep) + 6 > opt_replyend)
1524 opt_replyend += OPT_REPLY_SIZE;
1525 len = opt_replyend - opt_reply;
1526 opt_reply = (unsigned char *)realloc(opt_reply, len);
1527 if (opt_reply == NULL) {
1528 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1529 opt_reply = opt_replyp = opt_replyend = NULL;
1532 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1533 opt_replyend = opt_reply + len;
1535 if (opt_welldefined(ep))
1537 if (telopt_environ == TELOPT_OLD_ENVIRON)
1538 *opt_replyp++ = old_env_var;
1541 *opt_replyp++ = NEW_ENV_VAR;
1543 *opt_replyp++ = ENV_USERVAR;
1545 while ((c = *ep++)) {
1548 *opt_replyp++ = IAC;
1554 *opt_replyp++ = ENV_ESC;
1561 if (telopt_environ == TELOPT_OLD_ENVIRON)
1562 *opt_replyp++ = old_env_value;
1565 *opt_replyp++ = NEW_ENV_VALUE;
1573 opt_welldefined(const char *ep)
1575 if ((strcmp(ep, "USER") == 0) ||
1576 (strcmp(ep, "DISPLAY") == 0) ||
1577 (strcmp(ep, "PRINTER") == 0) ||
1578 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1579 (strcmp(ep, "JOB") == 0) ||
1580 (strcmp(ep, "ACCT") == 0))
1586 env_opt_end(int emptyok)
1590 len = opt_replyp - opt_reply + 2;
1591 if (emptyok || len > 6) {
1592 *opt_replyp++ = IAC;
1594 if (NETROOM() > len) {
1595 ring_supply_data(&netoring, opt_reply, len);
1596 printsub('>', &opt_reply[2], len - 2);
1598 /*@*/ else printf("slc_end_reply: not enough room\n");
1602 opt_reply = opt_replyp = opt_replyend = NULL;
1615 int returnValue = 0;
1619 while (TTYROOM() > 2) {
1622 ring_consumed(&netiring, count);
1626 sbp = netiring.consume;
1627 scc = ring_full_consecutive(&netiring);
1629 /* No more data coming in */
1634 c = *sbp++ & 0xff, scc--; count++;
1637 c = (*decrypt_input)(c);
1638 #endif /* ENCRYPTION */
1640 switch (telrcv_state) {
1643 telrcv_state = TS_DATA;
1645 break; /* Ignore \0 after CR */
1647 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1651 /* Else, fall through */
1655 telrcv_state = TS_IAC;
1659 * The 'crmod' hack (see following) is needed
1660 * since we can't * set CRMOD on output only.
1661 * Machines like MULTICS like to send \r without
1662 * \n; since we must turn off CRMOD to get proper
1663 * input, the mapping is done here (sigh).
1665 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1670 c = (*decrypt_input)(c);
1671 #endif /* ENCRYPTION */
1673 sbp++, scc--; count++;
1676 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1678 sbp++, scc--; count++;
1683 (*decrypt_input)(-1);
1684 #endif /* ENCRYPTION */
1692 telrcv_state = TS_CR;
1708 telrcv_state = TS_WILL;
1712 telrcv_state = TS_WONT;
1716 telrcv_state = TS_DO;
1720 telrcv_state = TS_DONT;
1725 * We may have missed an urgent notification,
1726 * so make sure we flush whatever is in the
1729 printoption("RCVD", IAC, DM);
1732 SYNCHing = stilloob();
1738 telrcv_state = TS_SB;
1748 printoption("RCVD", IAC, c);
1751 telrcv_state = TS_DATA;
1755 printoption("RCVD", WILL, c);
1757 telrcv_state = TS_DATA;
1761 printoption("RCVD", WONT, c);
1763 telrcv_state = TS_DATA;
1767 printoption("RCVD", DO, c);
1769 if (c == TELOPT_NAWS) {
1771 } else if (c == TELOPT_LFLOW) {
1776 telrcv_state = TS_DATA;
1780 printoption("RCVD", DONT, c);
1783 setconnmode(0); /* set new tty mode (maybe) */
1784 telrcv_state = TS_DATA;
1789 telrcv_state = TS_SE;
1799 * This is an error. We only expect to get
1800 * "IAC IAC" or "IAC SE". Several things may
1801 * have happend. An IAC was not doubled, the
1802 * IAC SE was left off, or another option got
1803 * inserted into the suboption are all possibilities.
1804 * If we assume that the IAC was not doubled,
1805 * and really the IAC SE was left off, we could
1806 * get into an infinate loop here. So, instead,
1807 * we terminate the suboption, and process the
1808 * partial suboption if we can.
1815 printoption("In SUBOPTION processing, RCVD", IAC, c);
1816 suboption(); /* handle sub-option */
1817 telrcv_state = TS_IAC;
1821 telrcv_state = TS_SB;
1827 suboption(); /* handle sub-option */
1828 telrcv_state = TS_DATA;
1833 ring_consumed(&netiring, count);
1834 return returnValue||count;
1837 static int bol = 1, local = 0;
1845 command(0, "z\n", 2);
1856 int returnValue = 0;
1861 while (NETROOM() > 2) {
1867 ring_consumed(&ttyiring, count);
1871 tbp = ttyiring.consume;
1872 tcc = ring_full_consecutive(&ttyiring);
1877 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1878 if (rlogin != _POSIX_VDISABLE) {
1887 if (sc == '.' || c == termEofChar) {
1889 command(0, "close\n", 6);
1892 if (sc == termSuspChar) {
1894 command(0, "z\n", 2);
1898 command(0, tbp, tcc);
1912 if ((sc == '\n') || (sc == '\r'))
1914 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1916 * Double escape is a pass through of a single escape character.
1918 if (tcc && strip(*tbp) == escape) {
1924 command(0, (char *)tbp, tcc);
1933 #ifdef KLUDGELINEMODE
1934 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1935 if (tcc > 0 && strip(*tbp) == echoc) {
1936 tcc--; tbp++; count++;
1938 dontlecho = !dontlecho;
1939 settimer(echotoggle);
1946 if (MODE_LOCAL_CHARS(globalmode)) {
1947 if (TerminalSpecialChars(sc) == 0) {
1952 if (my_want_state_is_wont(TELOPT_BINARY)) {
1956 * If we are in CRMOD mode (\r ==> \n)
1957 * on our local machine, then probably
1958 * a newline (unix) is CRLF (TELNET).
1960 if (MODE_LOCAL_CHARS(globalmode)) {
1964 bol = flushline = 1;
1968 NET2ADD('\r', '\0');
1970 NET2ADD('\r', '\n');
1972 bol = flushline = 1;
1981 } else if (c == IAC) {
1988 ring_consumed(&ttyiring, count);
1989 return returnValue||count; /* Non-zero if we did anything */
1995 * Try to do something.
1997 * If we do something useful, return 1; else return 0.
2002 Scheduler(int block)
2004 /* One wants to be a bit careful about setting returnValue
2005 * to one, since a one implies we did some useful work,
2006 * and therefore probably won't be called to block next
2009 int netin, netout, netex, ttyin, ttyout;
2011 /* Decide which rings should be processed */
2013 netout = ring_full_count(&netoring) &&
2015 (my_want_state_is_wont(TELOPT_LINEMODE)
2016 #ifdef KLUDGELINEMODE
2017 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2020 my_want_state_is_will(TELOPT_BINARY));
2021 ttyout = ring_full_count(&ttyoring);
2023 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2025 netin = !ISend && ring_empty_count(&netiring);
2029 /* Call to system code to process rings */
2031 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2033 /* Now, look at the input rings, looking for work to do. */
2035 if (ring_full_count(&ttyiring)) {
2036 returnValue |= telsnd();
2039 if (ring_full_count(&netiring)) {
2040 returnValue |= telrcv();
2045 #ifdef AUTHENTICATION
2046 #define __unusedhere
2048 #define __unusedhere __unused
2051 * Select from tty and network...
2054 telnet(char *user __unusedhere)
2058 #ifdef AUTHENTICATION
2061 static char local_host[256] = { 0 };
2063 if (!local_host[0]) {
2064 gethostname(local_host, sizeof(local_host));
2065 local_host[sizeof(local_host)-1] = 0;
2067 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2068 auth_encrypt_user(user);
2073 #ifdef AUTHENTICATION
2075 send_will(TELOPT_AUTHENTICATION, 1);
2078 send_do(TELOPT_ENCRYPT, 1);
2079 send_will(TELOPT_ENCRYPT, 1);
2080 #endif /* ENCRYPTION */
2081 send_do(TELOPT_SGA, 1);
2082 send_will(TELOPT_TTYPE, 1);
2083 send_will(TELOPT_NAWS, 1);
2084 send_will(TELOPT_TSPEED, 1);
2085 send_will(TELOPT_LFLOW, 1);
2086 send_will(TELOPT_LINEMODE, 1);
2087 send_will(TELOPT_NEW_ENVIRON, 1);
2088 send_do(TELOPT_STATUS, 1);
2089 if (env_getvalue("DISPLAY"))
2090 send_will(TELOPT_XDISPLOC, 1);
2092 tel_enter_binary(eight);
2098 while ((schedValue = Scheduler(0)) != 0) {
2099 if (schedValue == -1) {
2105 if (Scheduler(1) == -1) {
2112 #if 0 /* XXX - this not being in is a bug */
2116 * Return the address of the next "item" in the TELNET data
2117 * stream. This will be the address of the next character if
2118 * the current address is a user data character, or it will
2119 * be the address of the character following the TELNET command
2120 * if the current address is a TELNET IAC ("I Am a Command")
2125 nextitem(char *current)
2127 if ((*current&0xff) != IAC) {
2130 switch (*(current+1)&0xff) {
2136 case SB: /* loop forever looking for the SE */
2138 char *look = current+2;
2141 if ((*look++&0xff) == IAC) {
2142 if ((*look++&0xff) == SE) {
2157 * We are about to do a TELNET SYNCH operation. Clear
2158 * the path to the network.
2160 * Things are a bit tricky since we may have sent the first
2161 * byte or so of a previous TELNET command into the network.
2162 * So, we have to scan the network buffer from the beginning
2163 * until we are up to where we want to be.
2165 * A side effect of what we do, just to keep things
2166 * simple, is to clear the urgent data pointer. The principal
2167 * caller should be setting the urgent data pointer AFTER calling
2178 * These routines add various telnet commands to the data stream.
2188 (void) ttyflush(1); /* Flush/drop output */
2189 /* do printoption AFTER flush, otherwise the output gets tossed... */
2190 printoption("SENT", DO, TELOPT_TM);
2197 printoption("SENT", IAC, AO);
2207 printoption("SENT", IAC, EL);
2214 printoption("SENT", IAC, EC);
2218 dosynch(char *ch __unused)
2220 netclear(); /* clear the path to the network */
2224 printoption("SENT", IAC, DM);
2228 int want_status_response = 0;
2231 get_status(char *ch __unused)
2233 unsigned char tmp[16];
2236 if (my_want_state_is_dont(TELOPT_STATUS)) {
2237 printf("Remote side does not support STATUS option\n");
2244 *cp++ = TELOPT_STATUS;
2245 *cp++ = TELQUAL_SEND;
2248 if (NETROOM() >= cp - tmp) {
2249 ring_supply_data(&netoring, tmp, cp-tmp);
2250 printsub('>', tmp+2, cp - tmp - 2);
2252 ++want_status_response;
2260 printoption("SENT", IAC, IP);
2273 NET2ADD(IAC, BREAK);
2274 printoption("SENT", IAC, BREAK);
2287 NET2ADD(IAC, ABORT);
2288 printoption("SENT", IAC, ABORT);
2302 printoption("SENT", IAC, SUSP);
2316 printoption("SENT", IAC, xEOF);
2323 printoption("SENT", IAC, AYT);
2327 * Send a window size update to the remote system.
2334 unsigned char tmp[16];
2337 if (my_state_is_wont(TELOPT_NAWS))
2340 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2341 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2343 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2351 *cp++ = TELOPT_NAWS;
2356 if (NETROOM() >= cp - tmp) {
2357 ring_supply_data(&netoring, tmp, cp-tmp);
2358 printsub('>', tmp+2, cp - tmp - 2);
2363 tel_enter_binary(int rw)
2366 send_do(TELOPT_BINARY, 1);
2368 send_will(TELOPT_BINARY, 1);
2372 tel_leave_binary(int rw)
2375 send_dont(TELOPT_BINARY, 1);
2377 send_wont(TELOPT_BINARY, 1);