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
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);
224 printoption("SENT", DO, c);
228 send_dont(int c, int init)
231 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
232 my_want_state_is_dont(c))
234 set_my_want_state_dont(c);
241 printoption("SENT", DONT, c);
245 send_will(int c, int init)
248 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
249 my_want_state_is_will(c))
251 set_my_want_state_will(c);
258 printoption("SENT", WILL, c);
262 send_wont(int c, int init)
265 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
266 my_want_state_is_wont(c))
268 set_my_want_state_wont(c);
275 printoption("SENT", WONT, c);
279 willoption(int option)
281 int new_state_ok = 0;
283 if (do_dont_resp[option]) {
284 --do_dont_resp[option];
285 if (do_dont_resp[option] && my_state_is_do(option))
286 --do_dont_resp[option];
289 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
296 settimer(modenegotiated);
299 #ifdef AUTHENTICATION
300 case TELOPT_AUTHENTICATION:
304 #endif /* ENCRYPTION */
312 * Special case for TM. If we get back a WILL,
313 * pretend we got back a WONT.
315 set_my_want_state_dont(option);
316 set_my_state_dont(option);
317 return; /* Never reply to TM will's/wont's */
319 case TELOPT_LINEMODE:
325 set_my_want_state_do(option);
327 setconnmode(0); /* possibly set new tty mode */
329 do_dont_resp[option]++;
330 send_dont(option, 0);
333 set_my_state_do(option);
335 if (option == TELOPT_ENCRYPT)
336 encrypt_send_support();
337 #endif /* ENCRYPTION */
341 wontoption(int option)
343 if (do_dont_resp[option]) {
344 --do_dont_resp[option];
345 if (do_dont_resp[option] && my_state_is_dont(option))
346 --do_dont_resp[option];
349 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
353 #ifdef KLUDGELINEMODE
360 settimer(modenegotiated);
366 set_my_want_state_dont(option);
367 set_my_state_dont(option);
368 return; /* Never reply to TM will's/wont's */
373 set_my_want_state_dont(option);
374 if (my_state_is_do(option))
375 send_dont(option, 0);
376 setconnmode(0); /* Set new tty mode */
377 } else if (option == TELOPT_TM) {
379 * Special case for TM.
383 set_my_want_state_dont(option);
385 set_my_state_dont(option);
391 int new_state_ok = 0;
393 if (will_wont_resp[option]) {
394 --will_wont_resp[option];
395 if (will_wont_resp[option] && my_state_is_will(option))
396 --will_wont_resp[option];
399 if (will_wont_resp[option] == 0) {
400 if (my_want_state_is_wont(option)) {
406 * Special case for TM. We send a WILL, but pretend
409 send_will(option, 0);
410 set_my_want_state_wont(TELOPT_TM);
411 set_my_state_wont(TELOPT_TM);
414 case TELOPT_BINARY: /* binary mode */
415 case TELOPT_NAWS: /* window size */
416 case TELOPT_TSPEED: /* terminal speed */
417 case TELOPT_LFLOW: /* local flow control */
418 case TELOPT_TTYPE: /* terminal type option */
419 case TELOPT_SGA: /* no big deal */
421 case TELOPT_ENCRYPT: /* encryption variable option */
422 #endif /* ENCRYPTION */
426 case TELOPT_NEW_ENVIRON: /* New environment variable option */
428 if (my_state_is_will(TELOPT_OLD_ENVIRON))
429 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
431 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
432 if (my_state_is_will(TELOPT_NEW_ENVIRON))
433 break; /* Don't enable if new one is in use! */
435 telopt_environ = option;
440 #ifdef AUTHENTICATION
441 case TELOPT_AUTHENTICATION:
447 case TELOPT_XDISPLOC: /* X Display location */
448 if (env_getvalue("DISPLAY"))
452 case TELOPT_LINEMODE:
453 #ifdef KLUDGELINEMODE
455 send_do(TELOPT_SGA, 1);
457 set_my_want_state_will(TELOPT_LINEMODE);
458 send_will(option, 0);
459 set_my_state_will(TELOPT_LINEMODE);
463 case TELOPT_ECHO: /* We're never going to echo... */
469 set_my_want_state_will(option);
470 send_will(option, 0);
471 setconnmode(0); /* Set new tty mode */
473 will_wont_resp[option]++;
474 send_wont(option, 0);
478 * Handle options that need more things done after the
479 * other side has acknowledged the option.
482 case TELOPT_LINEMODE:
483 #ifdef KLUDGELINEMODE
485 send_do(TELOPT_SGA, 1);
487 set_my_state_will(option);
489 send_do(TELOPT_SGA, 0);
494 set_my_state_will(option);
498 dontoption(int option)
501 if (will_wont_resp[option]) {
502 --will_wont_resp[option];
503 if (will_wont_resp[option] && my_state_is_wont(option))
504 --will_wont_resp[option];
507 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
509 case TELOPT_LINEMODE:
510 linemode = 0; /* put us back to the default state */
513 case TELOPT_NEW_ENVIRON:
515 * The new environ option wasn't recognized, try
518 send_will(TELOPT_OLD_ENVIRON, 1);
519 telopt_environ = TELOPT_OLD_ENVIRON;
523 /* we always accept a DONT */
524 set_my_want_state_wont(option);
525 if (my_state_is_will(option))
526 send_wont(option, 0);
527 setconnmode(0); /* Set new tty mode */
529 set_my_state_wont(option);
533 * Given a buffer returned by tgetent(), this routine will turn
534 * the pipe separated list of names in the buffer into an array
535 * of pointers to null terminated names. We toss out any bad,
536 * duplicate, or verbose names (names with spaces).
539 static const char *name_unknown = "UNKNOWN";
540 static const char *unknown[] = { NULL, NULL };
543 mklist(char *buf, char *name)
546 char c, *cp, **argvp, *cp2, **argv, **avt;
549 if (strlen(name) > 40) {
551 unknown[0] = name_unknown;
557 unknown[0] = name_unknown;
559 * Count up the number of names.
561 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
566 * Allocate an array to put the name pointers into
568 argv = (char **)malloc((n+3)*sizeof(char *));
573 * Fill up the array of pointers to names.
578 for (cp = cp2 = buf; (c = *cp); cp++) {
579 if (c == '|' || c == ':') {
582 * Skip entries that have spaces or are over 40
583 * characters long. If this is our environment
584 * name, then put it up front. Otherwise, as
585 * long as this is not a duplicate name (case
586 * insensitive) add it to the list.
588 if (n || (cp - cp2 > 41))
590 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
592 else if (is_unique(cp2, argv+1, argvp))
597 * Skip multiple delimiters. Reset cp2 to
598 * the beginning of the next name. Reset n,
599 * the flag for names with spaces.
601 while ((c = *cp) == '|')
607 * Skip entries with spaces or non-ascii values.
608 * Convert lower case letters to upper case.
610 if ((c == ' ') || !isascii(c))
617 * Check for an old V6 2 character name. If the second
618 * name points to the beginning of the buffer, and is
619 * only 2 characters long, move it to the end of the array.
621 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
623 for (avt = &argv[1]; avt < argvp; avt++)
629 * Duplicate last name, for TTYPE option, and null
630 * terminate the array. If we didn't find a match on
631 * our terminal name, put that name at the beginning.
642 for (avt = argv; avt < argvp; avt++)
647 return((const char **)argv);
653 is_unique(char *name, char **as, char **ae)
658 n = strlen(name) + 1;
659 for (ap = as; ap < ae; ap++)
660 if (strncasecmp(*ap, name, n) == 0)
670 setupterm(char *tname, int fd, int *errp)
672 if (tgetent(termbuf, tname) == 1) {
673 termbuf[1023] = '\0';
683 #define termbuf ttytype
684 extern char ttytype[];
687 int resettermname = 1;
693 static const char **tnamep = 0;
694 static const char **next;
699 if (tnamep && tnamep != unknown)
701 if ((tname = env_getvalue("TERM")) &&
702 (setupterm(tname, 1, &err) == 0)) {
703 tnamep = mklist(termbuf, tname);
705 if (tname && (strlen(tname) <= 40)) {
709 unknown[0] = name_unknown;
721 * Look at the sub-option buffer, and try to be helpful to the other
724 * Currently we recognize:
726 * Terminal type, send request.
727 * Terminal speed (send request).
728 * Local flow control (is request).
735 unsigned char subchar;
737 printsub('<', subbuffer, SB_LEN()+2);
738 switch (subchar = SB_GET()) {
740 if (my_want_state_is_wont(TELOPT_TTYPE))
742 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
746 unsigned char temp[50];
749 name = gettermname();
750 len = strlen(name) + 4 + 2;
751 if (len < NETROOM()) {
752 sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
753 TELQUAL_IS, name, IAC, SE);
754 ring_supply_data(&netoring, temp, len);
755 printsub('>', &temp[2], len-2);
757 ExitString("No room in buffer for terminal type.\n", 1);
763 if (my_want_state_is_wont(TELOPT_TSPEED))
767 if (SB_GET() == TELQUAL_SEND) {
769 unsigned char temp[50];
772 TerminalSpeeds(&ispeed, &ospeed);
774 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
775 TELQUAL_IS, ospeed, ispeed, IAC, SE);
776 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
778 if (len < NETROOM()) {
779 ring_supply_data(&netoring, temp, len);
780 printsub('>', temp+2, len - 2);
782 /*@*/ else printf("lm_will: not enough room in buffer\n");
786 if (my_want_state_is_wont(TELOPT_LFLOW))
791 case LFLOW_RESTART_ANY:
794 case LFLOW_RESTART_XON:
810 case TELOPT_LINEMODE:
811 if (my_want_state_is_wont(TELOPT_LINEMODE))
817 lm_will(subpointer, SB_LEN());
820 lm_wont(subpointer, SB_LEN());
823 lm_do(subpointer, SB_LEN());
826 lm_dont(subpointer, SB_LEN());
829 slc(subpointer, SB_LEN());
832 lm_mode(subpointer, SB_LEN(), 0);
840 case TELOPT_OLD_ENVIRON:
842 case TELOPT_NEW_ENVIRON:
848 if (my_want_state_is_dont(subchar))
852 if (my_want_state_is_wont(subchar)) {
859 env_opt(subpointer, SB_LEN());
862 case TELOPT_XDISPLOC:
863 if (my_want_state_is_wont(TELOPT_XDISPLOC))
867 if (SB_GET() == TELQUAL_SEND) {
868 unsigned char temp[50], *dp;
871 if ((dp = env_getvalue("DISPLAY")) == NULL ||
872 strlen(dp) > sizeof(temp) - 7) {
874 * Something happened, we no longer have a DISPLAY
875 * variable. Or it is too long. So, turn off the option.
877 send_wont(TELOPT_XDISPLOC, 1);
880 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
881 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
882 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
884 if (len < NETROOM()) {
885 ring_supply_data(&netoring, temp, len);
886 printsub('>', temp+2, len - 2);
888 /*@*/ else printf("lm_will: not enough room in buffer\n");
892 #ifdef AUTHENTICATION
893 case TELOPT_AUTHENTICATION: {
900 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
902 auth_is(subpointer, SB_LEN());
905 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
907 auth_send(subpointer, SB_LEN());
910 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
912 auth_reply(subpointer, SB_LEN());
915 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
917 auth_name(subpointer, SB_LEN());
929 if (my_want_state_is_dont(TELOPT_ENCRYPT))
931 encrypt_start(subpointer, SB_LEN());
934 if (my_want_state_is_dont(TELOPT_ENCRYPT))
938 case ENCRYPT_SUPPORT:
939 if (my_want_state_is_wont(TELOPT_ENCRYPT))
941 encrypt_support(subpointer, SB_LEN());
943 case ENCRYPT_REQSTART:
944 if (my_want_state_is_wont(TELOPT_ENCRYPT))
946 encrypt_request_start(subpointer, SB_LEN());
949 if (my_want_state_is_wont(TELOPT_ENCRYPT))
952 * We can always send an REQEND so that we cannot
953 * get stuck encrypting. We should only get this
954 * if we have been able to get in the correct mode
957 encrypt_request_end();
960 if (my_want_state_is_dont(TELOPT_ENCRYPT))
962 encrypt_is(subpointer, SB_LEN());
965 if (my_want_state_is_wont(TELOPT_ENCRYPT))
967 encrypt_reply(subpointer, SB_LEN());
969 case ENCRYPT_ENC_KEYID:
970 if (my_want_state_is_dont(TELOPT_ENCRYPT))
972 encrypt_enc_keyid(subpointer, SB_LEN());
974 case ENCRYPT_DEC_KEYID:
975 if (my_want_state_is_wont(TELOPT_ENCRYPT))
977 encrypt_dec_keyid(subpointer, SB_LEN());
983 #endif /* ENCRYPTION */
989 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
992 lm_will(unsigned char *cmd, int len)
995 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
999 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1003 if (NETROOM() > (int)sizeof(str_lm)) {
1004 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1005 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1007 /*@*/ else printf("lm_will: not enough room in buffer\n");
1013 lm_wont(unsigned char *cmd, int len)
1016 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1020 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1022 /* We are always DONT, so don't respond */
1028 lm_do(unsigned char *cmd, int len)
1031 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1035 case LM_FORWARDMASK:
1039 if (NETROOM() > (int)sizeof(str_lm)) {
1040 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1041 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1043 /*@*/ else printf("lm_do: not enough room in buffer\n");
1049 lm_dont(unsigned char *cmd, int len)
1052 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1056 case LM_FORWARDMASK:
1058 /* we are always WONT, so don't respond */
1063 static unsigned char str_lm_mode[] = {
1064 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1068 lm_mode(unsigned char *cmd, int len, int init)
1072 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1076 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1077 str_lm_mode[4] = linemode;
1079 str_lm_mode[4] |= MODE_ACK;
1080 if (NETROOM() > (int)sizeof(str_lm_mode)) {
1081 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1082 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1084 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1085 setconnmode(0); /* set changed mode */
1092 * Handle special character suboption of LINEMODE.
1098 char flags; /* Current flags & level */
1099 char mylevel; /* Maximum level & flags */
1102 #define SLC_IMPORT 0
1103 #define SLC_EXPORT 1
1104 #define SLC_RVALUE 2
1105 static int slc_mode = SLC_EXPORT;
1113 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1116 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1119 #define initfunc(func, flags) { \
1120 spcp = &spc_data[func]; \
1121 if ((spcp->valp = tcval(func))) { \
1122 spcp->val = *spcp->valp; \
1123 spcp->mylevel = SLC_VARIABLE|flags; \
1126 spcp->mylevel = SLC_DEFAULT; \
1130 initfunc(SLC_SYNCH, 0);
1132 initfunc(SLC_AO, 0);
1133 initfunc(SLC_AYT, 0);
1135 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1136 initfunc(SLC_EOF, 0);
1138 initfunc(SLC_SUSP, SLC_FLUSHIN);
1140 initfunc(SLC_EC, 0);
1141 initfunc(SLC_EL, 0);
1143 initfunc(SLC_EW, 0);
1144 initfunc(SLC_RP, 0);
1145 initfunc(SLC_LNEXT, 0);
1147 initfunc(SLC_XON, 0);
1148 initfunc(SLC_XOFF, 0);
1150 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1151 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1153 initfunc(SLC_FORW1, 0);
1155 initfunc(SLC_FORW2, 0);
1159 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1162 if (slc_mode == SLC_EXPORT)
1172 printf("Special characters are %s values\n",
1173 slc_mode == SLC_IMPORT ? "remote default" :
1174 slc_mode == SLC_EXPORT ? "local" :
1179 slc_mode_export(void)
1181 slc_mode = SLC_EXPORT;
1182 if (my_state_is_will(TELOPT_LINEMODE))
1187 slc_mode_import(int def)
1189 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1190 if (my_state_is_will(TELOPT_LINEMODE))
1194 unsigned char slc_import_val[] = {
1195 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1197 unsigned char slc_import_def[] = {
1198 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1204 if (NETROOM() > (int)sizeof(slc_import_val)) {
1206 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1207 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1209 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1210 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1213 /*@*/ else printf("slc_import: not enough room\n");
1221 TerminalDefaultChars();
1224 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1225 if (spcp->mylevel != SLC_NOSUPPORT) {
1226 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1227 spcp->flags = SLC_NOSUPPORT;
1229 spcp->flags = spcp->mylevel;
1231 spcp->val = *spcp->valp;
1232 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1237 setconnmode(1); /* Make sure the character values are set */
1241 slc(unsigned char *cp, int len)
1248 for (; len >= 3; len -=3, cp +=3) {
1250 func = cp[SLC_FUNC];
1254 * Client side: always ignore 0 function.
1259 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1260 slc_add_reply(func, SLC_NOSUPPORT, 0);
1264 spcp = &spc_data[func];
1266 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1268 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1269 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1273 if (level == (SLC_DEFAULT|SLC_ACK)) {
1275 * This is an error condition, the SLC_ACK
1276 * bit should never be set for the SLC_DEFAULT
1277 * level. Our best guess to recover is to
1278 * ignore the SLC_ACK bit.
1280 cp[SLC_FLAGS] &= ~SLC_ACK;
1283 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1284 spcp->val = (cc_t)cp[SLC_VALUE];
1285 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1291 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1292 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1293 spcp->val = (cc_t)cp[SLC_VALUE];
1295 if (level == SLC_DEFAULT) {
1296 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1297 spcp->flags = spcp->mylevel;
1299 spcp->flags = SLC_NOSUPPORT;
1301 slc_add_reply(func, spcp->flags, spcp->val);
1305 setconnmode(1); /* set the new character values */
1314 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1315 if (spcp->valp && spcp->val != *spcp->valp) {
1316 spcp->val = *spcp->valp;
1317 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1318 spcp->flags = SLC_NOSUPPORT;
1320 spcp->flags = spcp->mylevel;
1321 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1328 unsigned char slc_reply[128];
1329 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1330 unsigned char *slc_replyp;
1333 slc_start_reply(void)
1335 slc_replyp = slc_reply;
1336 *slc_replyp++ = IAC;
1338 *slc_replyp++ = TELOPT_LINEMODE;
1339 *slc_replyp++ = LM_SLC;
1343 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1345 /* A sequence of up to 6 bytes my be written for this member of the SLC
1346 * suboption list by this function. The end of negotiation command,
1347 * which is written by slc_end_reply(), will require 2 additional
1348 * bytes. Do not proceed unless there is sufficient space for these
1351 if (&slc_replyp[6+2] > slc_reply_eom)
1353 if ((*slc_replyp++ = func) == IAC)
1354 *slc_replyp++ = IAC;
1355 if ((*slc_replyp++ = flags) == IAC)
1356 *slc_replyp++ = IAC;
1357 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1358 *slc_replyp++ = IAC;
1366 /* The end of negotiation command requires 2 bytes. */
1367 if (&slc_replyp[2] > slc_reply_eom)
1369 *slc_replyp++ = IAC;
1371 len = slc_replyp - slc_reply;
1374 if (NETROOM() > len) {
1375 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1376 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1378 /*@*/else printf("slc_end_reply: not enough room\n");
1385 int need_update = 0;
1387 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1388 if (!(spcp->flags&SLC_ACK))
1390 spcp->flags &= ~SLC_ACK;
1391 if (spcp->valp && (*spcp->valp != spcp->val)) {
1392 *spcp->valp = spcp->val;
1396 return(need_update);
1402 * Earlier version of telnet/telnetd from the BSD code had
1403 * the definitions of VALUE and VAR reversed. To ensure
1404 * maximum interoperability, we assume that the server is
1405 * an older BSD server, until proven otherwise. The newer
1406 * BSD servers should be able to handle either definition,
1407 * so it is better to use the wrong values if we don't
1408 * know what type of server it is.
1411 int old_env_var = OLD_ENV_VAR;
1412 int old_env_value = OLD_ENV_VALUE;
1414 # define old_env_var OLD_ENV_VAR
1415 # define old_env_value OLD_ENV_VALUE
1420 env_opt(unsigned char *buf, int len)
1422 unsigned char *ep = 0, *epc = 0;
1425 switch(buf[0]&0xff) {
1430 } else for (i = 1; i < len; i++) {
1431 switch (buf[i]&0xff) {
1435 if (telopt_environ == TELOPT_OLD_ENVIRON
1437 /* Server has the same definitions */
1438 old_env_var = OLD_ENV_VAR;
1439 old_env_value = OLD_ENV_VALUE;
1445 * Although OLD_ENV_VALUE is not legal, we will
1446 * still recognize it, just in case it is an
1447 * old server that has VAR & VALUE mixed up...
1458 ep = epc = &buf[i+1];
1478 /* Ignore for now. We shouldn't get it anyway. */
1486 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1487 unsigned char *opt_reply = NULL;
1488 unsigned char *opt_replyp;
1489 unsigned char *opt_replyend;
1495 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1497 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1498 if (opt_reply == NULL) {
1499 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1500 opt_reply = opt_replyp = opt_replyend = NULL;
1503 opt_replyp = opt_reply;
1504 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1505 *opt_replyp++ = IAC;
1507 *opt_replyp++ = telopt_environ;
1508 *opt_replyp++ = TELQUAL_IS;
1512 env_opt_start_info(void)
1516 opt_replyp[-1] = TELQUAL_INFO;
1520 env_opt_add(unsigned char *ep)
1522 unsigned char *vp, c;
1524 if (opt_reply == NULL) /*XXX*/
1527 if (ep == NULL || *ep == '\0') {
1528 /* Send user defined variables first. */
1530 while ((ep = env_default(0, 0)))
1533 /* Now add the list of well know variables. */
1535 while ((ep = env_default(0, 1)))
1539 vp = env_getvalue(ep);
1540 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1541 2 * strlen((char *)ep) + 6 > opt_replyend)
1544 opt_replyend += OPT_REPLY_SIZE;
1545 len = opt_replyend - opt_reply;
1546 opt_reply = (unsigned char *)realloc(opt_reply, len);
1547 if (opt_reply == NULL) {
1548 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1549 opt_reply = opt_replyp = opt_replyend = NULL;
1552 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1553 opt_replyend = opt_reply + len;
1555 if (opt_welldefined(ep))
1557 if (telopt_environ == TELOPT_OLD_ENVIRON)
1558 *opt_replyp++ = old_env_var;
1561 *opt_replyp++ = NEW_ENV_VAR;
1563 *opt_replyp++ = ENV_USERVAR;
1565 while ((c = *ep++)) {
1566 if (opt_replyp + (2 + 2) > opt_replyend)
1570 *opt_replyp++ = IAC;
1576 *opt_replyp++ = ENV_ESC;
1582 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1585 if (telopt_environ == TELOPT_OLD_ENVIRON)
1586 *opt_replyp++ = old_env_value;
1589 *opt_replyp++ = NEW_ENV_VALUE;
1597 opt_welldefined(const char *ep)
1599 if ((strcmp(ep, "USER") == 0) ||
1600 (strcmp(ep, "DISPLAY") == 0) ||
1601 (strcmp(ep, "PRINTER") == 0) ||
1602 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1603 (strcmp(ep, "JOB") == 0) ||
1604 (strcmp(ep, "ACCT") == 0))
1610 env_opt_end(int emptyok)
1614 if (opt_replyp + 2 > opt_replyend)
1616 len = opt_replyp + 2 - opt_reply;
1617 if (emptyok || len > 6) {
1618 *opt_replyp++ = IAC;
1620 if (NETROOM() > len) {
1621 ring_supply_data(&netoring, opt_reply, len);
1622 printsub('>', &opt_reply[2], len - 2);
1624 /*@*/ else printf("slc_end_reply: not enough room\n");
1628 opt_reply = opt_replyp = opt_replyend = NULL;
1641 int returnValue = 0;
1645 while (TTYROOM() > 2) {
1648 ring_consumed(&netiring, count);
1652 sbp = netiring.consume;
1653 scc = ring_full_consecutive(&netiring);
1655 /* No more data coming in */
1660 c = *sbp++ & 0xff, scc--; count++;
1663 c = (*decrypt_input)(c);
1664 #endif /* ENCRYPTION */
1666 switch (telrcv_state) {
1669 telrcv_state = TS_DATA;
1671 break; /* Ignore \0 after CR */
1673 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1680 if (c == IAC && telnetport >= 0) {
1681 telrcv_state = TS_IAC;
1685 * The 'crmod' hack (see following) is needed
1686 * since we can't * set CRMOD on output only.
1687 * Machines like MULTICS like to send \r without
1688 * \n; since we must turn off CRMOD to get proper
1689 * input, the mapping is done here (sigh).
1691 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1696 c = (*decrypt_input)(c);
1697 #endif /* ENCRYPTION */
1699 sbp++, scc--; count++;
1702 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1704 sbp++, scc--; count++;
1709 (*decrypt_input)(-1);
1710 #endif /* ENCRYPTION */
1718 telrcv_state = TS_CR;
1734 telrcv_state = TS_WILL;
1738 telrcv_state = TS_WONT;
1742 telrcv_state = TS_DO;
1746 telrcv_state = TS_DONT;
1751 * We may have missed an urgent notification,
1752 * so make sure we flush whatever is in the
1755 printoption("RCVD", IAC, DM);
1758 SYNCHing = stilloob();
1764 telrcv_state = TS_SB;
1774 printoption("RCVD", IAC, c);
1777 telrcv_state = TS_DATA;
1781 printoption("RCVD", WILL, c);
1783 telrcv_state = TS_DATA;
1787 printoption("RCVD", WONT, c);
1789 telrcv_state = TS_DATA;
1793 printoption("RCVD", DO, c);
1795 if (c == TELOPT_NAWS) {
1797 } else if (c == TELOPT_LFLOW) {
1802 telrcv_state = TS_DATA;
1806 printoption("RCVD", DONT, c);
1809 setconnmode(0); /* set new tty mode (maybe) */
1810 telrcv_state = TS_DATA;
1815 telrcv_state = TS_SE;
1825 * This is an error. We only expect to get
1826 * "IAC IAC" or "IAC SE". Several things may
1827 * have happend. An IAC was not doubled, the
1828 * IAC SE was left off, or another option got
1829 * inserted into the suboption are all possibilities.
1830 * If we assume that the IAC was not doubled,
1831 * and really the IAC SE was left off, we could
1832 * get into an infinate loop here. So, instead,
1833 * we terminate the suboption, and process the
1834 * partial suboption if we can.
1841 printoption("In SUBOPTION processing, RCVD", IAC, c);
1842 suboption(); /* handle sub-option */
1843 telrcv_state = TS_IAC;
1847 telrcv_state = TS_SB;
1853 suboption(); /* handle sub-option */
1854 telrcv_state = TS_DATA;
1859 ring_consumed(&netiring, count);
1860 return returnValue||count;
1863 static int bol = 1, local = 0;
1871 command(0, "z\n", 2);
1882 int returnValue = 0;
1887 while (NETROOM() > 2) {
1893 ring_consumed(&ttyiring, count);
1897 tbp = ttyiring.consume;
1898 tcc = ring_full_consecutive(&ttyiring);
1903 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1904 if (rlogin != _POSIX_VDISABLE) {
1913 if (sc == '.' || c == termEofChar) {
1915 command(0, "close\n", 6);
1918 if (sc == termSuspChar) {
1920 command(0, "z\n", 2);
1924 command(0, tbp, tcc);
1938 if ((sc == '\n') || (sc == '\r'))
1940 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1942 * Double escape is a pass through of a single escape character.
1944 if (tcc && strip(*tbp) == escape) {
1950 command(0, (char *)tbp, tcc);
1959 #ifdef KLUDGELINEMODE
1960 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1961 if (tcc > 0 && strip(*tbp) == echoc) {
1962 tcc--; tbp++; count++;
1964 dontlecho = !dontlecho;
1965 settimer(echotoggle);
1972 if (MODE_LOCAL_CHARS(globalmode)) {
1973 if (TerminalSpecialChars(sc) == 0) {
1978 if (my_want_state_is_wont(TELOPT_BINARY)) {
1982 * If we are in CRMOD mode (\r ==> \n)
1983 * on our local machine, then probably
1984 * a newline (unix) is CRLF (TELNET).
1986 if (MODE_LOCAL_CHARS(globalmode)) {
1990 bol = flushline = 1;
1994 NET2ADD('\r', '\0');
1996 NET2ADD('\r', '\n');
1998 bol = flushline = 1;
2007 } else if (c == IAC) {
2014 ring_consumed(&ttyiring, count);
2015 return returnValue||count; /* Non-zero if we did anything */
2021 * Try to do something.
2023 * If we do something useful, return 1; else return 0.
2028 Scheduler(int block)
2030 /* One wants to be a bit careful about setting returnValue
2031 * to one, since a one implies we did some useful work,
2032 * and therefore probably won't be called to block next
2035 int netin, netout, netex, ttyin, ttyout;
2037 /* Decide which rings should be processed */
2039 netout = ring_full_count(&netoring) &&
2041 (my_want_state_is_wont(TELOPT_LINEMODE)
2042 #ifdef KLUDGELINEMODE
2043 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2046 my_want_state_is_will(TELOPT_BINARY));
2047 ttyout = ring_full_count(&ttyoring);
2049 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2051 netin = !ISend && ring_empty_count(&netiring);
2055 /* Call to system code to process rings */
2057 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2059 /* Now, look at the input rings, looking for work to do. */
2061 if (ring_full_count(&ttyiring)) {
2062 returnValue |= telsnd();
2065 if (ring_full_count(&netiring)) {
2066 returnValue |= telrcv();
2071 #ifdef AUTHENTICATION
2072 #define __unusedhere
2074 #define __unusedhere __unused
2077 * Select from tty and network...
2080 telnet(char *user __unusedhere)
2084 #ifdef AUTHENTICATION
2087 static char local_host[256] = { 0 };
2089 if (!local_host[0]) {
2090 gethostname(local_host, sizeof(local_host));
2091 local_host[sizeof(local_host)-1] = 0;
2093 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2094 auth_encrypt_user(user);
2098 if (telnetport > 0) {
2099 #ifdef AUTHENTICATION
2101 send_will(TELOPT_AUTHENTICATION, 1);
2104 send_do(TELOPT_ENCRYPT, 1);
2105 send_will(TELOPT_ENCRYPT, 1);
2106 #endif /* ENCRYPTION */
2107 send_do(TELOPT_SGA, 1);
2108 send_will(TELOPT_TTYPE, 1);
2109 send_will(TELOPT_NAWS, 1);
2110 send_will(TELOPT_TSPEED, 1);
2111 send_will(TELOPT_LFLOW, 1);
2112 send_will(TELOPT_LINEMODE, 1);
2113 send_will(TELOPT_NEW_ENVIRON, 1);
2114 send_do(TELOPT_STATUS, 1);
2115 if (env_getvalue("DISPLAY"))
2116 send_will(TELOPT_XDISPLOC, 1);
2118 tel_enter_binary(eight);
2124 while ((schedValue = Scheduler(0)) != 0) {
2125 if (schedValue == -1) {
2131 if (Scheduler(1) == -1) {
2138 #if 0 /* XXX - this not being in is a bug */
2142 * Return the address of the next "item" in the TELNET data
2143 * stream. This will be the address of the next character if
2144 * the current address is a user data character, or it will
2145 * be the address of the character following the TELNET command
2146 * if the current address is a TELNET IAC ("I Am a Command")
2151 nextitem(char *current)
2153 if ((*current&0xff) != IAC) {
2156 switch (*(current+1)&0xff) {
2162 case SB: /* loop forever looking for the SE */
2164 char *look = current+2;
2167 if ((*look++&0xff) == IAC) {
2168 if ((*look++&0xff) == SE) {
2183 * We are about to do a TELNET SYNCH operation. Clear
2184 * the path to the network.
2186 * Things are a bit tricky since we may have sent the first
2187 * byte or so of a previous TELNET command into the network.
2188 * So, we have to scan the network buffer from the beginning
2189 * until we are up to where we want to be.
2191 * A side effect of what we do, just to keep things
2192 * simple, is to clear the urgent data pointer. The principal
2193 * caller should be setting the urgent data pointer AFTER calling
2204 * These routines add various telnet commands to the data stream.
2214 (void) ttyflush(1); /* Flush/drop output */
2215 /* do printoption AFTER flush, otherwise the output gets tossed... */
2216 printoption("SENT", DO, TELOPT_TM);
2223 printoption("SENT", IAC, AO);
2233 printoption("SENT", IAC, EL);
2240 printoption("SENT", IAC, EC);
2244 dosynch(char *ch __unused)
2246 netclear(); /* clear the path to the network */
2250 printoption("SENT", IAC, DM);
2254 int want_status_response = 0;
2257 get_status(char *ch __unused)
2259 unsigned char tmp[16];
2262 if (my_want_state_is_dont(TELOPT_STATUS)) {
2263 printf("Remote side does not support STATUS option\n");
2270 *cp++ = TELOPT_STATUS;
2271 *cp++ = TELQUAL_SEND;
2274 if (NETROOM() >= cp - tmp) {
2275 ring_supply_data(&netoring, tmp, cp-tmp);
2276 printsub('>', tmp+2, cp - tmp - 2);
2278 ++want_status_response;
2286 printoption("SENT", IAC, IP);
2299 NET2ADD(IAC, BREAK);
2300 printoption("SENT", IAC, BREAK);
2313 NET2ADD(IAC, ABORT);
2314 printoption("SENT", IAC, ABORT);
2328 printoption("SENT", IAC, SUSP);
2342 printoption("SENT", IAC, xEOF);
2349 printoption("SENT", IAC, AYT);
2353 * Send a window size update to the remote system.
2360 unsigned char tmp[16];
2363 if (my_state_is_wont(TELOPT_NAWS))
2366 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2367 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2369 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2377 *cp++ = TELOPT_NAWS;
2382 if (NETROOM() >= cp - tmp) {
2383 ring_supply_data(&netoring, tmp, cp-tmp);
2384 printsub('>', tmp+2, cp - tmp - 2);
2389 tel_enter_binary(int rw)
2392 send_do(TELOPT_BINARY, 1);
2394 send_will(TELOPT_BINARY, 1);
2398 tel_leave_binary(int rw)
2401 send_dont(TELOPT_BINARY, 1);
2403 send_wont(TELOPT_BINARY, 1);