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
36 static const char sccsid[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
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
153 #ifdef KLUDGELINEMODE
154 int kludgelinemode = 1;
157 static int is_unique(char *, char **, char **);
160 * The following are some clocks used to decide how to interpret
161 * the relationship between various variables.
167 * Initialize telnet environment.
178 connected = ISend = localflow = donebinarytoggle = 0;
179 #ifdef AUTHENTICATION
181 auth_encrypt_connect(connected);
188 /* Don't change NetTrace */
190 escape = CONTROL(']');
191 rlogin = _POSIX_VDISABLE;
192 #ifdef KLUDGELINEMODE
193 echoc = CONTROL('E');
197 telrcv_state = TS_DATA;
202 * These routines are in charge of sending option negotiations
205 * The basic idea is that we send the negotiation if either side
206 * is in disagreement as to what the current state should be.
210 send_do(int c, int init)
213 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
214 my_want_state_is_do(c))
216 set_my_want_state_do(c);
223 printoption("SENT", DO, c);
227 send_dont(int c, int init)
230 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
231 my_want_state_is_dont(c))
233 set_my_want_state_dont(c);
240 printoption("SENT", DONT, c);
244 send_will(int c, int init)
247 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
248 my_want_state_is_will(c))
250 set_my_want_state_will(c);
257 printoption("SENT", WILL, c);
261 send_wont(int c, int init)
264 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
265 my_want_state_is_wont(c))
267 set_my_want_state_wont(c);
274 printoption("SENT", WONT, c);
278 willoption(int option)
280 int new_state_ok = 0;
282 if (do_dont_resp[option]) {
283 --do_dont_resp[option];
284 if (do_dont_resp[option] && my_state_is_do(option))
285 --do_dont_resp[option];
288 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
295 settimer(modenegotiated);
298 #ifdef AUTHENTICATION
299 case TELOPT_AUTHENTICATION:
303 #endif /* ENCRYPTION */
311 * Special case for TM. If we get back a WILL,
312 * pretend we got back a WONT.
314 set_my_want_state_dont(option);
315 set_my_state_dont(option);
316 return; /* Never reply to TM will's/wont's */
318 case TELOPT_LINEMODE:
324 set_my_want_state_do(option);
326 setconnmode(0); /* possibly set new tty mode */
328 do_dont_resp[option]++;
329 send_dont(option, 0);
332 set_my_state_do(option);
334 if (option == TELOPT_ENCRYPT)
335 encrypt_send_support();
336 #endif /* ENCRYPTION */
340 wontoption(int option)
342 if (do_dont_resp[option]) {
343 --do_dont_resp[option];
344 if (do_dont_resp[option] && my_state_is_dont(option))
345 --do_dont_resp[option];
348 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
352 #ifdef KLUDGELINEMODE
359 settimer(modenegotiated);
365 set_my_want_state_dont(option);
366 set_my_state_dont(option);
367 return; /* Never reply to TM will's/wont's */
372 set_my_want_state_dont(option);
373 if (my_state_is_do(option))
374 send_dont(option, 0);
375 setconnmode(0); /* Set new tty mode */
376 } else if (option == TELOPT_TM) {
378 * Special case for TM.
382 set_my_want_state_dont(option);
384 set_my_state_dont(option);
390 int new_state_ok = 0;
392 if (will_wont_resp[option]) {
393 --will_wont_resp[option];
394 if (will_wont_resp[option] && my_state_is_will(option))
395 --will_wont_resp[option];
398 if (will_wont_resp[option] == 0) {
399 if (my_want_state_is_wont(option)) {
405 * Special case for TM. We send a WILL, but pretend
408 send_will(option, 0);
409 set_my_want_state_wont(TELOPT_TM);
410 set_my_state_wont(TELOPT_TM);
413 case TELOPT_BINARY: /* binary mode */
414 case TELOPT_NAWS: /* window size */
415 case TELOPT_TSPEED: /* terminal speed */
416 case TELOPT_LFLOW: /* local flow control */
417 case TELOPT_TTYPE: /* terminal type option */
418 case TELOPT_SGA: /* no big deal */
420 case TELOPT_ENCRYPT: /* encryption variable option */
421 #endif /* ENCRYPTION */
425 case TELOPT_NEW_ENVIRON: /* New environment variable option */
427 if (my_state_is_will(TELOPT_OLD_ENVIRON))
428 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
430 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
431 if (my_state_is_will(TELOPT_NEW_ENVIRON))
432 break; /* Don't enable if new one is in use! */
434 telopt_environ = option;
439 #ifdef AUTHENTICATION
440 case TELOPT_AUTHENTICATION:
446 case TELOPT_XDISPLOC: /* X Display location */
447 if (env_getvalue("DISPLAY"))
451 case TELOPT_LINEMODE:
452 #ifdef KLUDGELINEMODE
454 send_do(TELOPT_SGA, 1);
456 set_my_want_state_will(TELOPT_LINEMODE);
457 send_will(option, 0);
458 set_my_state_will(TELOPT_LINEMODE);
462 case TELOPT_ECHO: /* We're never going to echo... */
468 set_my_want_state_will(option);
469 send_will(option, 0);
470 setconnmode(0); /* Set new tty mode */
472 will_wont_resp[option]++;
473 send_wont(option, 0);
477 * Handle options that need more things done after the
478 * other side has acknowledged the option.
481 case TELOPT_LINEMODE:
482 #ifdef KLUDGELINEMODE
484 send_do(TELOPT_SGA, 1);
486 set_my_state_will(option);
488 send_do(TELOPT_SGA, 0);
493 set_my_state_will(option);
497 dontoption(int option)
500 if (will_wont_resp[option]) {
501 --will_wont_resp[option];
502 if (will_wont_resp[option] && my_state_is_wont(option))
503 --will_wont_resp[option];
506 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
508 case TELOPT_LINEMODE:
509 linemode = 0; /* put us back to the default state */
512 case TELOPT_NEW_ENVIRON:
514 * The new environ option wasn't recognized, try
517 send_will(TELOPT_OLD_ENVIRON, 1);
518 telopt_environ = TELOPT_OLD_ENVIRON;
522 /* we always accept a DONT */
523 set_my_want_state_wont(option);
524 if (my_state_is_will(option))
525 send_wont(option, 0);
526 setconnmode(0); /* Set new tty mode */
528 set_my_state_wont(option);
532 * Given a buffer returned by tgetent(), this routine will turn
533 * the pipe separated list of names in the buffer into an array
534 * of pointers to null terminated names. We toss out any bad,
535 * duplicate, or verbose names (names with spaces).
538 static const char *name_unknown = "UNKNOWN";
539 static const char *unknown[] = { NULL, NULL };
542 mklist(char *buf, char *name)
545 char c, *cp, **argvp, *cp2, **argv, **avt;
548 if (strlen(name) > 40) {
550 unknown[0] = name_unknown;
556 unknown[0] = name_unknown;
558 * Count up the number of names.
560 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
565 * Allocate an array to put the name pointers into
567 argv = (char **)malloc((n+3)*sizeof(char *));
572 * Fill up the array of pointers to names.
577 for (cp = cp2 = buf; (c = *cp); cp++) {
578 if (c == '|' || c == ':') {
581 * Skip entries that have spaces or are over 40
582 * characters long. If this is our environment
583 * name, then put it up front. Otherwise, as
584 * long as this is not a duplicate name (case
585 * insensitive) add it to the list.
587 if (n || (cp - cp2 > 41))
589 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
591 else if (is_unique(cp2, argv+1, argvp))
596 * Skip multiple delimiters. Reset cp2 to
597 * the beginning of the next name. Reset n,
598 * the flag for names with spaces.
600 while ((c = *cp) == '|')
606 * Skip entries with spaces or non-ascii values.
607 * Convert lower case letters to upper case.
609 if ((c == ' ') || !isascii(c))
616 * Check for an old V6 2 character name. If the second
617 * name points to the beginning of the buffer, and is
618 * only 2 characters long, move it to the end of the array.
620 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
622 for (avt = &argv[1]; avt < argvp; avt++)
628 * Duplicate last name, for TTYPE option, and null
629 * terminate the array. If we didn't find a match on
630 * our terminal name, put that name at the beginning.
641 for (avt = argv; avt < argvp; avt++)
646 return((const char **)argv);
652 is_unique(char *name, char **as, char **ae)
657 n = strlen(name) + 1;
658 for (ap = as; ap < ae; ap++)
659 if (strncasecmp(*ap, name, n) == 0)
669 setupterm(char *tname, int fd, int *errp)
671 if (tgetent(termbuf, tname) == 1) {
672 termbuf[1023] = '\0';
682 #define termbuf ttytype
683 extern char ttytype[];
686 int resettermname = 1;
692 static const char **tnamep = 0;
693 static const char **next;
698 if (tnamep && tnamep != unknown)
700 if ((tname = env_getvalue("TERM")) &&
701 (setupterm(tname, 1, &err) == 0)) {
702 tnamep = mklist(termbuf, tname);
704 if (tname && (strlen(tname) <= 40)) {
708 unknown[0] = name_unknown;
720 * Look at the sub-option buffer, and try to be helpful to the other
723 * Currently we recognize:
725 * Terminal type, send request.
726 * Terminal speed (send request).
727 * Local flow control (is request).
734 unsigned char subchar;
736 printsub('<', subbuffer, SB_LEN()+2);
737 switch (subchar = SB_GET()) {
739 if (my_want_state_is_wont(TELOPT_TTYPE))
741 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
745 unsigned char temp[50];
748 name = gettermname();
749 len = strlen(name) + 4 + 2;
750 if (len < NETROOM()) {
751 sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
752 TELQUAL_IS, name, IAC, SE);
753 ring_supply_data(&netoring, temp, len);
754 printsub('>', &temp[2], len-2);
756 ExitString("No room in buffer for terminal type.\n", 1);
762 if (my_want_state_is_wont(TELOPT_TSPEED))
766 if (SB_GET() == TELQUAL_SEND) {
768 unsigned char temp[50];
771 TerminalSpeeds(&ispeed, &ospeed);
773 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
774 TELQUAL_IS, ospeed, ispeed, IAC, SE);
775 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
777 if (len < NETROOM()) {
778 ring_supply_data(&netoring, temp, len);
779 printsub('>', temp+2, len - 2);
781 /*@*/ else printf("lm_will: not enough room in buffer\n");
785 if (my_want_state_is_wont(TELOPT_LFLOW))
790 case LFLOW_RESTART_ANY:
793 case LFLOW_RESTART_XON:
809 case TELOPT_LINEMODE:
810 if (my_want_state_is_wont(TELOPT_LINEMODE))
816 lm_will(subpointer, SB_LEN());
819 lm_wont(subpointer, SB_LEN());
822 lm_do(subpointer, SB_LEN());
825 lm_dont(subpointer, SB_LEN());
828 slc(subpointer, SB_LEN());
831 lm_mode(subpointer, SB_LEN(), 0);
839 case TELOPT_OLD_ENVIRON:
841 case TELOPT_NEW_ENVIRON:
847 if (my_want_state_is_dont(subchar))
851 if (my_want_state_is_wont(subchar)) {
858 env_opt(subpointer, SB_LEN());
861 case TELOPT_XDISPLOC:
862 if (my_want_state_is_wont(TELOPT_XDISPLOC))
866 if (SB_GET() == TELQUAL_SEND) {
867 unsigned char temp[50], *dp;
870 if ((dp = env_getvalue("DISPLAY")) == NULL ||
871 strlen(dp) > sizeof(temp) - 7) {
873 * Something happened, we no longer have a DISPLAY
874 * variable. Or it is too long. So, turn off the option.
876 send_wont(TELOPT_XDISPLOC, 1);
879 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
880 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
881 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
883 if (len < NETROOM()) {
884 ring_supply_data(&netoring, temp, len);
885 printsub('>', temp+2, len - 2);
887 /*@*/ else printf("lm_will: not enough room in buffer\n");
891 #ifdef AUTHENTICATION
892 case TELOPT_AUTHENTICATION: {
899 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
901 auth_is(subpointer, SB_LEN());
904 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
906 auth_send(subpointer, SB_LEN());
909 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
911 auth_reply(subpointer, SB_LEN());
914 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
916 auth_name(subpointer, SB_LEN());
928 if (my_want_state_is_dont(TELOPT_ENCRYPT))
930 encrypt_start(subpointer, SB_LEN());
933 if (my_want_state_is_dont(TELOPT_ENCRYPT))
937 case ENCRYPT_SUPPORT:
938 if (my_want_state_is_wont(TELOPT_ENCRYPT))
940 encrypt_support(subpointer, SB_LEN());
942 case ENCRYPT_REQSTART:
943 if (my_want_state_is_wont(TELOPT_ENCRYPT))
945 encrypt_request_start(subpointer, SB_LEN());
948 if (my_want_state_is_wont(TELOPT_ENCRYPT))
951 * We can always send an REQEND so that we cannot
952 * get stuck encrypting. We should only get this
953 * if we have been able to get in the correct mode
956 encrypt_request_end();
959 if (my_want_state_is_dont(TELOPT_ENCRYPT))
961 encrypt_is(subpointer, SB_LEN());
964 if (my_want_state_is_wont(TELOPT_ENCRYPT))
966 encrypt_reply(subpointer, SB_LEN());
968 case ENCRYPT_ENC_KEYID:
969 if (my_want_state_is_dont(TELOPT_ENCRYPT))
971 encrypt_enc_keyid(subpointer, SB_LEN());
973 case ENCRYPT_DEC_KEYID:
974 if (my_want_state_is_wont(TELOPT_ENCRYPT))
976 encrypt_dec_keyid(subpointer, SB_LEN());
982 #endif /* ENCRYPTION */
988 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
991 lm_will(unsigned char *cmd, int len)
994 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
998 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1002 if (NETROOM() > (int)sizeof(str_lm)) {
1003 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1004 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1006 /*@*/ else printf("lm_will: not enough room in buffer\n");
1012 lm_wont(unsigned char *cmd, int len)
1015 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1019 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1021 /* We are always DONT, so don't respond */
1027 lm_do(unsigned char *cmd, int len)
1030 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1034 case LM_FORWARDMASK:
1038 if (NETROOM() > (int)sizeof(str_lm)) {
1039 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1040 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1042 /*@*/ else printf("lm_do: not enough room in buffer\n");
1048 lm_dont(unsigned char *cmd, int len)
1051 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1055 case LM_FORWARDMASK:
1057 /* we are always WONT, so don't respond */
1062 static unsigned char str_lm_mode[] = {
1063 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1067 lm_mode(unsigned char *cmd, int len, int init)
1071 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1075 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1076 str_lm_mode[4] = linemode;
1078 str_lm_mode[4] |= MODE_ACK;
1079 if (NETROOM() > (int)sizeof(str_lm_mode)) {
1080 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1081 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1083 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1084 setconnmode(0); /* set changed mode */
1091 * Handle special character suboption of LINEMODE.
1097 char flags; /* Current flags & level */
1098 char mylevel; /* Maximum level & flags */
1101 #define SLC_IMPORT 0
1102 #define SLC_EXPORT 1
1103 #define SLC_RVALUE 2
1104 static int slc_mode = SLC_EXPORT;
1112 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1115 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1118 #define initfunc(func, flags) { \
1119 spcp = &spc_data[func]; \
1120 if ((spcp->valp = tcval(func))) { \
1121 spcp->val = *spcp->valp; \
1122 spcp->mylevel = SLC_VARIABLE|flags; \
1125 spcp->mylevel = SLC_DEFAULT; \
1129 initfunc(SLC_SYNCH, 0);
1131 initfunc(SLC_AO, 0);
1132 initfunc(SLC_AYT, 0);
1134 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1135 initfunc(SLC_EOF, 0);
1137 initfunc(SLC_SUSP, SLC_FLUSHIN);
1139 initfunc(SLC_EC, 0);
1140 initfunc(SLC_EL, 0);
1142 initfunc(SLC_EW, 0);
1143 initfunc(SLC_RP, 0);
1144 initfunc(SLC_LNEXT, 0);
1146 initfunc(SLC_XON, 0);
1147 initfunc(SLC_XOFF, 0);
1149 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1150 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1152 initfunc(SLC_FORW1, 0);
1154 initfunc(SLC_FORW2, 0);
1158 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1161 if (slc_mode == SLC_EXPORT)
1171 printf("Special characters are %s values\n",
1172 slc_mode == SLC_IMPORT ? "remote default" :
1173 slc_mode == SLC_EXPORT ? "local" :
1178 slc_mode_export(void)
1180 slc_mode = SLC_EXPORT;
1181 if (my_state_is_will(TELOPT_LINEMODE))
1186 slc_mode_import(int def)
1188 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1189 if (my_state_is_will(TELOPT_LINEMODE))
1193 unsigned char slc_import_val[] = {
1194 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1196 unsigned char slc_import_def[] = {
1197 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1203 if (NETROOM() > (int)sizeof(slc_import_val)) {
1205 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1206 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1208 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1209 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1212 /*@*/ else printf("slc_import: not enough room\n");
1220 TerminalDefaultChars();
1223 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1224 if (spcp->mylevel != SLC_NOSUPPORT) {
1225 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1226 spcp->flags = SLC_NOSUPPORT;
1228 spcp->flags = spcp->mylevel;
1230 spcp->val = *spcp->valp;
1231 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1236 setconnmode(1); /* Make sure the character values are set */
1240 slc(unsigned char *cp, int len)
1247 for (; len >= 3; len -=3, cp +=3) {
1249 func = cp[SLC_FUNC];
1253 * Client side: always ignore 0 function.
1258 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1259 slc_add_reply(func, SLC_NOSUPPORT, 0);
1263 spcp = &spc_data[func];
1265 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1267 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1268 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1272 if (level == (SLC_DEFAULT|SLC_ACK)) {
1274 * This is an error condition, the SLC_ACK
1275 * bit should never be set for the SLC_DEFAULT
1276 * level. Our best guess to recover is to
1277 * ignore the SLC_ACK bit.
1279 cp[SLC_FLAGS] &= ~SLC_ACK;
1282 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1283 spcp->val = (cc_t)cp[SLC_VALUE];
1284 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1290 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1291 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1292 spcp->val = (cc_t)cp[SLC_VALUE];
1294 if (level == SLC_DEFAULT) {
1295 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1296 spcp->flags = spcp->mylevel;
1298 spcp->flags = SLC_NOSUPPORT;
1300 slc_add_reply(func, spcp->flags, spcp->val);
1304 setconnmode(1); /* set the new character values */
1313 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1314 if (spcp->valp && spcp->val != *spcp->valp) {
1315 spcp->val = *spcp->valp;
1316 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1317 spcp->flags = SLC_NOSUPPORT;
1319 spcp->flags = spcp->mylevel;
1320 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1327 unsigned char slc_reply[128];
1328 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1329 unsigned char *slc_replyp;
1332 slc_start_reply(void)
1334 slc_replyp = slc_reply;
1335 *slc_replyp++ = IAC;
1337 *slc_replyp++ = TELOPT_LINEMODE;
1338 *slc_replyp++ = LM_SLC;
1342 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1344 /* A sequence of up to 6 bytes my be written for this member of the SLC
1345 * suboption list by this function. The end of negotiation command,
1346 * which is written by slc_end_reply(), will require 2 additional
1347 * bytes. Do not proceed unless there is sufficient space for these
1350 if (&slc_replyp[6+2] > slc_reply_eom)
1352 if ((*slc_replyp++ = func) == IAC)
1353 *slc_replyp++ = IAC;
1354 if ((*slc_replyp++ = flags) == IAC)
1355 *slc_replyp++ = IAC;
1356 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1357 *slc_replyp++ = IAC;
1365 /* The end of negotiation command requires 2 bytes. */
1366 if (&slc_replyp[2] > slc_reply_eom)
1368 *slc_replyp++ = IAC;
1370 len = slc_replyp - slc_reply;
1373 if (NETROOM() > len) {
1374 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1375 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1377 /*@*/else printf("slc_end_reply: not enough room\n");
1384 int need_update = 0;
1386 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1387 if (!(spcp->flags&SLC_ACK))
1389 spcp->flags &= ~SLC_ACK;
1390 if (spcp->valp && (*spcp->valp != spcp->val)) {
1391 *spcp->valp = spcp->val;
1395 return(need_update);
1401 * Earlier version of telnet/telnetd from the BSD code had
1402 * the definitions of VALUE and VAR reversed. To ensure
1403 * maximum interoperability, we assume that the server is
1404 * an older BSD server, until proven otherwise. The newer
1405 * BSD servers should be able to handle either definition,
1406 * so it is better to use the wrong values if we don't
1407 * know what type of server it is.
1410 int old_env_var = OLD_ENV_VAR;
1411 int old_env_value = OLD_ENV_VALUE;
1413 # define old_env_var OLD_ENV_VAR
1414 # define old_env_value OLD_ENV_VALUE
1419 env_opt(unsigned char *buf, int len)
1421 unsigned char *ep = 0, *epc = 0;
1424 switch(buf[0]&0xff) {
1429 } else for (i = 1; i < len; i++) {
1430 switch (buf[i]&0xff) {
1434 if (telopt_environ == TELOPT_OLD_ENVIRON
1436 /* Server has the same definitions */
1437 old_env_var = OLD_ENV_VAR;
1438 old_env_value = OLD_ENV_VALUE;
1444 * Although OLD_ENV_VALUE is not legal, we will
1445 * still recognize it, just in case it is an
1446 * old server that has VAR & VALUE mixed up...
1457 ep = epc = &buf[i+1];
1477 /* Ignore for now. We shouldn't get it anyway. */
1485 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1486 unsigned char *opt_reply = NULL;
1487 unsigned char *opt_replyp;
1488 unsigned char *opt_replyend;
1494 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1496 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1497 if (opt_reply == NULL) {
1498 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1499 opt_reply = opt_replyp = opt_replyend = NULL;
1502 opt_replyp = opt_reply;
1503 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1504 *opt_replyp++ = IAC;
1506 *opt_replyp++ = telopt_environ;
1507 *opt_replyp++ = TELQUAL_IS;
1511 env_opt_start_info(void)
1515 opt_replyp[-1] = TELQUAL_INFO;
1519 env_opt_add(unsigned char *ep)
1521 unsigned char *vp, c;
1523 if (opt_reply == NULL) /*XXX*/
1526 if (ep == NULL || *ep == '\0') {
1527 /* Send user defined variables first. */
1529 while ((ep = env_default(0, 0)))
1532 /* Now add the list of well know variables. */
1534 while ((ep = env_default(0, 1)))
1538 vp = env_getvalue(ep);
1539 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1540 2 * strlen((char *)ep) + 6 > opt_replyend)
1543 opt_replyend += OPT_REPLY_SIZE;
1544 len = opt_replyend - opt_reply;
1545 opt_reply = (unsigned char *)realloc(opt_reply, len);
1546 if (opt_reply == NULL) {
1547 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1548 opt_reply = opt_replyp = opt_replyend = NULL;
1551 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1552 opt_replyend = opt_reply + len;
1554 if (opt_welldefined(ep))
1556 if (telopt_environ == TELOPT_OLD_ENVIRON)
1557 *opt_replyp++ = old_env_var;
1560 *opt_replyp++ = NEW_ENV_VAR;
1562 *opt_replyp++ = ENV_USERVAR;
1564 while ((c = *ep++)) {
1565 if (opt_replyp + (2 + 2) > opt_replyend)
1569 *opt_replyp++ = IAC;
1575 *opt_replyp++ = ENV_ESC;
1581 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1584 if (telopt_environ == TELOPT_OLD_ENVIRON)
1585 *opt_replyp++ = old_env_value;
1588 *opt_replyp++ = NEW_ENV_VALUE;
1596 opt_welldefined(const char *ep)
1598 if ((strcmp(ep, "USER") == 0) ||
1599 (strcmp(ep, "DISPLAY") == 0) ||
1600 (strcmp(ep, "PRINTER") == 0) ||
1601 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1602 (strcmp(ep, "JOB") == 0) ||
1603 (strcmp(ep, "ACCT") == 0))
1609 env_opt_end(int emptyok)
1613 if (opt_replyp + 2 > opt_replyend)
1615 len = opt_replyp + 2 - opt_reply;
1616 if (emptyok || len > 6) {
1617 *opt_replyp++ = IAC;
1619 if (NETROOM() > len) {
1620 ring_supply_data(&netoring, opt_reply, len);
1621 printsub('>', &opt_reply[2], len - 2);
1623 /*@*/ else printf("slc_end_reply: not enough room\n");
1627 opt_reply = opt_replyp = opt_replyend = NULL;
1640 int returnValue = 0;
1644 while (TTYROOM() > 2) {
1647 ring_consumed(&netiring, count);
1651 sbp = netiring.consume;
1652 scc = ring_full_consecutive(&netiring);
1654 /* No more data coming in */
1659 c = *sbp++ & 0xff, scc--; count++;
1662 c = (*decrypt_input)(c);
1663 #endif /* ENCRYPTION */
1665 switch (telrcv_state) {
1668 telrcv_state = TS_DATA;
1670 break; /* Ignore \0 after CR */
1672 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1679 if (c == IAC && telnetport >= 0) {
1680 telrcv_state = TS_IAC;
1684 * The 'crmod' hack (see following) is needed
1685 * since we can't * set CRMOD on output only.
1686 * Machines like MULTICS like to send \r without
1687 * \n; since we must turn off CRMOD to get proper
1688 * input, the mapping is done here (sigh).
1690 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1695 c = (*decrypt_input)(c);
1696 #endif /* ENCRYPTION */
1698 sbp++, scc--; count++;
1701 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1703 sbp++, scc--; count++;
1708 (*decrypt_input)(-1);
1709 #endif /* ENCRYPTION */
1717 telrcv_state = TS_CR;
1733 telrcv_state = TS_WILL;
1737 telrcv_state = TS_WONT;
1741 telrcv_state = TS_DO;
1745 telrcv_state = TS_DONT;
1750 * We may have missed an urgent notification,
1751 * so make sure we flush whatever is in the
1754 printoption("RCVD", IAC, DM);
1757 SYNCHing = stilloob();
1763 telrcv_state = TS_SB;
1773 printoption("RCVD", IAC, c);
1776 telrcv_state = TS_DATA;
1780 printoption("RCVD", WILL, c);
1782 telrcv_state = TS_DATA;
1786 printoption("RCVD", WONT, c);
1788 telrcv_state = TS_DATA;
1792 printoption("RCVD", DO, c);
1794 if (c == TELOPT_NAWS) {
1796 } else if (c == TELOPT_LFLOW) {
1801 telrcv_state = TS_DATA;
1805 printoption("RCVD", DONT, c);
1808 setconnmode(0); /* set new tty mode (maybe) */
1809 telrcv_state = TS_DATA;
1814 telrcv_state = TS_SE;
1824 * This is an error. We only expect to get
1825 * "IAC IAC" or "IAC SE". Several things may
1826 * have happend. An IAC was not doubled, the
1827 * IAC SE was left off, or another option got
1828 * inserted into the suboption are all possibilities.
1829 * If we assume that the IAC was not doubled,
1830 * and really the IAC SE was left off, we could
1831 * get into an infinate loop here. So, instead,
1832 * we terminate the suboption, and process the
1833 * partial suboption if we can.
1840 printoption("In SUBOPTION processing, RCVD", IAC, c);
1841 suboption(); /* handle sub-option */
1842 telrcv_state = TS_IAC;
1846 telrcv_state = TS_SB;
1852 suboption(); /* handle sub-option */
1853 telrcv_state = TS_DATA;
1858 ring_consumed(&netiring, count);
1859 return returnValue||count;
1862 static int bol = 1, local = 0;
1870 command(0, "z\n", 2);
1881 int returnValue = 0;
1886 while (NETROOM() > 2) {
1892 ring_consumed(&ttyiring, count);
1896 tbp = ttyiring.consume;
1897 tcc = ring_full_consecutive(&ttyiring);
1902 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1903 if (rlogin != _POSIX_VDISABLE) {
1912 if (sc == '.' || c == termEofChar) {
1914 command(0, "close\n", 6);
1917 if (sc == termSuspChar) {
1919 command(0, "z\n", 2);
1923 command(0, tbp, tcc);
1937 if ((sc == '\n') || (sc == '\r'))
1939 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1941 * Double escape is a pass through of a single escape character.
1943 if (tcc && strip(*tbp) == escape) {
1949 command(0, (char *)tbp, tcc);
1958 #ifdef KLUDGELINEMODE
1959 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1960 if (tcc > 0 && strip(*tbp) == echoc) {
1961 tcc--; tbp++; count++;
1963 dontlecho = !dontlecho;
1964 settimer(echotoggle);
1971 if (MODE_LOCAL_CHARS(globalmode)) {
1972 if (TerminalSpecialChars(sc) == 0) {
1977 if (my_want_state_is_wont(TELOPT_BINARY)) {
1981 * If we are in CRMOD mode (\r ==> \n)
1982 * on our local machine, then probably
1983 * a newline (unix) is CRLF (TELNET).
1985 if (MODE_LOCAL_CHARS(globalmode)) {
1989 bol = flushline = 1;
1993 NET2ADD('\r', '\0');
1995 NET2ADD('\r', '\n');
1997 bol = flushline = 1;
2006 } else if (c == IAC) {
2013 ring_consumed(&ttyiring, count);
2014 return returnValue||count; /* Non-zero if we did anything */
2020 * Try to do something.
2022 * If we do something useful, return 1; else return 0.
2027 Scheduler(int block)
2029 /* One wants to be a bit careful about setting returnValue
2030 * to one, since a one implies we did some useful work,
2031 * and therefore probably won't be called to block next
2034 int netin, netout, netex, ttyin, ttyout;
2036 /* Decide which rings should be processed */
2038 netout = ring_full_count(&netoring) &&
2040 (my_want_state_is_wont(TELOPT_LINEMODE)
2041 #ifdef KLUDGELINEMODE
2042 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2045 my_want_state_is_will(TELOPT_BINARY));
2046 ttyout = ring_full_count(&ttyoring);
2048 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2050 netin = !ISend && ring_empty_count(&netiring);
2054 /* Call to system code to process rings */
2056 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2058 /* Now, look at the input rings, looking for work to do. */
2060 if (ring_full_count(&ttyiring)) {
2061 returnValue |= telsnd();
2064 if (ring_full_count(&netiring)) {
2065 returnValue |= telrcv();
2070 #ifdef AUTHENTICATION
2071 #define __unusedhere
2073 #define __unusedhere __unused
2076 * Select from tty and network...
2079 telnet(char *user __unusedhere)
2083 #ifdef AUTHENTICATION
2086 static char local_host[256] = { 0 };
2088 if (!local_host[0]) {
2089 gethostname(local_host, sizeof(local_host));
2090 local_host[sizeof(local_host)-1] = 0;
2092 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2093 auth_encrypt_user(user);
2097 if (telnetport > 0) {
2098 #ifdef AUTHENTICATION
2100 send_will(TELOPT_AUTHENTICATION, 1);
2103 send_do(TELOPT_ENCRYPT, 1);
2104 send_will(TELOPT_ENCRYPT, 1);
2105 #endif /* ENCRYPTION */
2106 send_do(TELOPT_SGA, 1);
2107 send_will(TELOPT_TTYPE, 1);
2108 send_will(TELOPT_NAWS, 1);
2109 send_will(TELOPT_TSPEED, 1);
2110 send_will(TELOPT_LFLOW, 1);
2111 send_will(TELOPT_LINEMODE, 1);
2112 send_will(TELOPT_NEW_ENVIRON, 1);
2113 send_do(TELOPT_STATUS, 1);
2114 if (env_getvalue("DISPLAY"))
2115 send_will(TELOPT_XDISPLOC, 1);
2117 tel_enter_binary(eight);
2123 while ((schedValue = Scheduler(0)) != 0) {
2124 if (schedValue == -1) {
2130 if (Scheduler(1) == -1) {
2137 #if 0 /* XXX - this not being in is a bug */
2141 * Return the address of the next "item" in the TELNET data
2142 * stream. This will be the address of the next character if
2143 * the current address is a user data character, or it will
2144 * be the address of the character following the TELNET command
2145 * if the current address is a TELNET IAC ("I Am a Command")
2150 nextitem(char *current)
2152 if ((*current&0xff) != IAC) {
2155 switch (*(current+1)&0xff) {
2161 case SB: /* loop forever looking for the SE */
2163 char *look = current+2;
2166 if ((*look++&0xff) == IAC) {
2167 if ((*look++&0xff) == SE) {
2182 * We are about to do a TELNET SYNCH operation. Clear
2183 * the path to the network.
2185 * Things are a bit tricky since we may have sent the first
2186 * byte or so of a previous TELNET command into the network.
2187 * So, we have to scan the network buffer from the beginning
2188 * until we are up to where we want to be.
2190 * A side effect of what we do, just to keep things
2191 * simple, is to clear the urgent data pointer. The principal
2192 * caller should be setting the urgent data pointer AFTER calling
2203 * These routines add various telnet commands to the data stream.
2213 (void) ttyflush(1); /* Flush/drop output */
2214 /* do printoption AFTER flush, otherwise the output gets tossed... */
2215 printoption("SENT", DO, TELOPT_TM);
2222 printoption("SENT", IAC, AO);
2232 printoption("SENT", IAC, EL);
2239 printoption("SENT", IAC, EC);
2243 dosynch(char *ch __unused)
2245 netclear(); /* clear the path to the network */
2249 printoption("SENT", IAC, DM);
2253 int want_status_response = 0;
2256 get_status(char *ch __unused)
2258 unsigned char tmp[16];
2261 if (my_want_state_is_dont(TELOPT_STATUS)) {
2262 printf("Remote side does not support STATUS option\n");
2269 *cp++ = TELOPT_STATUS;
2270 *cp++ = TELQUAL_SEND;
2273 if (NETROOM() >= cp - tmp) {
2274 ring_supply_data(&netoring, tmp, cp-tmp);
2275 printsub('>', tmp+2, cp - tmp - 2);
2277 ++want_status_response;
2285 printoption("SENT", IAC, IP);
2298 NET2ADD(IAC, BREAK);
2299 printoption("SENT", IAC, BREAK);
2312 NET2ADD(IAC, ABORT);
2313 printoption("SENT", IAC, ABORT);
2327 printoption("SENT", IAC, SUSP);
2341 printoption("SENT", IAC, xEOF);
2348 printoption("SENT", IAC, AYT);
2352 * Send a window size update to the remote system.
2359 unsigned char tmp[16];
2362 if (my_state_is_wont(TELOPT_NAWS))
2365 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2366 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2368 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2376 *cp++ = TELOPT_NAWS;
2381 if (NETROOM() >= cp - tmp) {
2382 ring_supply_data(&netoring, tmp, cp-tmp);
2383 printsub('>', tmp+2, cp - tmp - 2);
2388 tel_enter_binary(int rw)
2391 send_do(TELOPT_BINARY, 1);
2393 send_will(TELOPT_BINARY, 1);
2397 tel_leave_binary(int rw)
2400 send_dont(TELOPT_BINARY, 1);
2402 send_wont(TELOPT_BINARY, 1);