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 "telnet_locl.h"
39 RCSID("$Id: telnet.c,v 1.25 1999/03/11 13:49:34 joda Exp $");
41 #define strip(x) (eight ? (x) : ((x) & 0x7f))
43 static unsigned char subbuffer[SUBBUFSIZE],
44 *subpointer, *subend; /* buffer for sub-options */
45 #define SB_CLEAR() subpointer = subbuffer;
46 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
47 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
48 *subpointer++ = (c); \
51 #define SB_GET() ((*subpointer++)&0xff)
52 #define SB_PEEK() ((*subpointer)&0xff)
53 #define SB_EOF() (subpointer >= subend)
54 #define SB_LEN() (subend - subpointer)
56 char options[256]; /* The combined options */
57 char do_dont_resp[256];
58 char will_wont_resp[256];
63 autologin = 0, /* Autologin anyone? */
67 ISend, /* trying to send network data in */
70 netdata, /* Print out network data flow */
71 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
73 SYNCHing, /* we are in TELNET SYNCH mode */
74 flushout, /* flush output */
75 autoflush = 0, /* flush output when interrupting? */
76 autosynch, /* send interrupt characters with SYNCH? */
77 localflow, /* we handle flow control locally */
78 restartany, /* if flow control enabled, restart on any character */
79 localchars, /* we recognize interrupt/quit */
80 donelclchars, /* the user has set "localchars" */
81 donebinarytoggle, /* the user has put us in binary */
82 dontlecho, /* do we suppress local echoing right now? */
94 * Telnet receiver states for fsm
103 #define TS_SB 7 /* sub-option collection */
104 #define TS_SE 8 /* looking for sub-option end */
106 static int telrcv_state;
108 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
110 # define telopt_environ TELOPT_NEW_ENVIRON
119 #ifdef KLUDGELINEMODE
120 int kludgelinemode = 1;
124 * The following are some clocks used to decide how to interpret
125 * the relationship between various variables.
130 static int is_unique(char *name, char **as, char **ae);
134 * Initialize telnet environment.
143 memset(options, 0, sizeof options);
145 connected = ISend = localflow = donebinarytoggle = 0;
146 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
147 auth_encrypt_connect(connected);
148 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
153 /* Don't change NetTrace */
155 escape = CONTROL(']');
156 rlogin = _POSIX_VDISABLE;
157 #ifdef KLUDGELINEMODE
158 echoc = CONTROL('E');
162 telrcv_state = TS_DATA;
167 * These routines are in charge of sending option negotiations
170 * The basic idea is that we send the negotiation if either side
171 * is in disagreement as to what the current state should be.
175 send_do(int c, int init)
178 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
179 my_want_state_is_do(c))
181 set_my_want_state_do(c);
186 printoption("SENT", DO, c);
190 send_dont(int c, int init)
193 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
194 my_want_state_is_dont(c))
196 set_my_want_state_dont(c);
201 printoption("SENT", DONT, c);
205 send_will(int c, int init)
208 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
209 my_want_state_is_will(c))
211 set_my_want_state_will(c);
216 printoption("SENT", WILL, c);
220 send_wont(int c, int init)
223 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
224 my_want_state_is_wont(c))
226 set_my_want_state_wont(c);
231 printoption("SENT", WONT, c);
236 willoption(int option)
238 int new_state_ok = 0;
240 if (do_dont_resp[option]) {
241 --do_dont_resp[option];
242 if (do_dont_resp[option] && my_state_is_do(option))
243 --do_dont_resp[option];
246 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
253 settimer(modenegotiated);
256 #if defined(AUTHENTICATION)
257 case TELOPT_AUTHENTICATION:
259 #if defined(ENCRYPTION)
269 * Special case for TM. If we get back a WILL,
270 * pretend we got back a WONT.
272 set_my_want_state_dont(option);
273 set_my_state_dont(option);
274 return; /* Never reply to TM will's/wont's */
276 case TELOPT_LINEMODE:
282 set_my_want_state_do(option);
284 setconnmode(0); /* possibly set new tty mode */
286 do_dont_resp[option]++;
287 send_dont(option, 0);
290 set_my_state_do(option);
291 #if defined(ENCRYPTION)
292 if (option == TELOPT_ENCRYPT)
293 encrypt_send_support();
298 wontoption(int option)
300 if (do_dont_resp[option]) {
301 --do_dont_resp[option];
302 if (do_dont_resp[option] && my_state_is_dont(option))
303 --do_dont_resp[option];
306 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
310 #ifdef KLUDGELINEMODE
317 settimer(modenegotiated);
323 set_my_want_state_dont(option);
324 set_my_state_dont(option);
325 return; /* Never reply to TM will's/wont's */
335 set_my_want_state_dont(option);
336 if (my_state_is_do(option))
337 send_dont(option, 0);
338 setconnmode(0); /* Set new tty mode */
339 } else if (option == TELOPT_TM) {
341 * Special case for TM.
345 set_my_want_state_dont(option);
347 set_my_state_dont(option);
353 int new_state_ok = 0;
355 if (will_wont_resp[option]) {
356 --will_wont_resp[option];
357 if (will_wont_resp[option] && my_state_is_will(option))
358 --will_wont_resp[option];
361 if (will_wont_resp[option] == 0) {
362 if (my_want_state_is_wont(option)) {
368 * Special case for TM. We send a WILL, but pretend
371 send_will(option, 0);
372 set_my_want_state_wont(TELOPT_TM);
373 set_my_state_wont(TELOPT_TM);
376 case TELOPT_BINARY: /* binary mode */
377 case TELOPT_NAWS: /* window size */
378 case TELOPT_TSPEED: /* terminal speed */
379 case TELOPT_LFLOW: /* local flow control */
380 case TELOPT_TTYPE: /* terminal type option */
381 case TELOPT_SGA: /* no big deal */
382 #if defined(ENCRYPTION)
383 case TELOPT_ENCRYPT: /* encryption variable option */
388 case TELOPT_NEW_ENVIRON: /* New environment variable option */
390 if (my_state_is_will(TELOPT_OLD_ENVIRON))
391 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
393 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
394 if (my_state_is_will(TELOPT_NEW_ENVIRON))
395 break; /* Don't enable if new one is in use! */
397 telopt_environ = option;
402 #if defined(AUTHENTICATION)
403 case TELOPT_AUTHENTICATION:
409 case TELOPT_XDISPLOC: /* X Display location */
410 if (env_getvalue((unsigned char *)"DISPLAY"))
414 case TELOPT_LINEMODE:
415 #ifdef KLUDGELINEMODE
417 send_do(TELOPT_SGA, 1);
419 set_my_want_state_will(TELOPT_LINEMODE);
420 send_will(option, 0);
421 set_my_state_will(TELOPT_LINEMODE);
425 case TELOPT_ECHO: /* We're never going to echo... */
431 set_my_want_state_will(option);
432 send_will(option, 0);
433 setconnmode(0); /* Set new tty mode */
435 will_wont_resp[option]++;
436 send_wont(option, 0);
440 * Handle options that need more things done after the
441 * other side has acknowledged the option.
444 case TELOPT_LINEMODE:
445 #ifdef KLUDGELINEMODE
447 send_do(TELOPT_SGA, 1);
449 set_my_state_will(option);
451 send_do(TELOPT_SGA, 0);
456 set_my_state_will(option);
460 dontoption(int option)
463 if (will_wont_resp[option]) {
464 --will_wont_resp[option];
465 if (will_wont_resp[option] && my_state_is_wont(option))
466 --will_wont_resp[option];
469 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
471 case TELOPT_LINEMODE:
472 linemode = 0; /* put us back to the default state */
475 case TELOPT_NEW_ENVIRON:
477 * The new environ option wasn't recognized, try
480 send_will(TELOPT_OLD_ENVIRON, 1);
481 telopt_environ = TELOPT_OLD_ENVIRON;
492 /* we always accept a DONT */
493 set_my_want_state_wont(option);
494 if (my_state_is_will(option))
495 send_wont(option, 0);
496 setconnmode(0); /* Set new tty mode */
498 set_my_state_wont(option);
502 * Given a buffer returned by tgetent(), this routine will turn
503 * the pipe seperated list of names in the buffer into an array
504 * of pointers to null terminated names. We toss out any bad,
505 * duplicate, or verbose names (names with spaces).
508 static char *name_unknown = "UNKNOWN";
509 static char *unknown[] = { 0, 0 };
512 mklist(char *buf, char *name)
515 char c, *cp, **argvp, *cp2, **argv, **avt;
518 if ((int)strlen(name) > 40) {
520 unknown[0] = name_unknown;
526 unknown[0] = name_unknown;
528 * Count up the number of names.
530 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
535 * Allocate an array to put the name pointers into
537 argv = (char **)malloc((n+3)*sizeof(char *));
542 * Fill up the array of pointers to names.
547 for (cp = cp2 = buf; (c = *cp); cp++) {
548 if (c == '|' || c == ':') {
551 * Skip entries that have spaces or are over 40
552 * characters long. If this is our environment
553 * name, then put it up front. Otherwise, as
554 * long as this is not a duplicate name (case
555 * insensitive) add it to the list.
557 if (n || (cp - cp2 > 41))
559 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
561 else if (is_unique(cp2, argv+1, argvp))
566 * Skip multiple delimiters. Reset cp2 to
567 * the beginning of the next name. Reset n,
568 * the flag for names with spaces.
570 while ((c = *cp) == '|')
576 * Skip entries with spaces or non-ascii values.
577 * Convert lower case letters to upper case.
579 #define ISASCII(c) (!((c)&0x80))
580 if ((c == ' ') || !ISASCII(c))
587 * Check for an old V6 2 character name. If the second
588 * name points to the beginning of the buffer, and is
589 * only 2 characters long, move it to the end of the array.
591 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
593 for (avt = &argv[1]; avt < argvp; avt++)
599 * Duplicate last name, for TTYPE option, and null
600 * terminate the array. If we didn't find a match on
601 * our terminal name, put that name at the beginning.
612 for (avt = argv; avt < argvp; avt++)
623 is_unique(char *name, char **as, char **ae)
628 n = strlen(name) + 1;
629 for (ap = as; ap < ae; ap++)
630 if (strncasecmp(*ap, name, n) == 0)
635 static char termbuf[1024];
638 telnet_setupterm(const char *tname, int fd, int *errp)
640 if (tgetent(termbuf, tname) == 1) {
641 termbuf[1023] = '\0';
651 int resettermname = 1;
657 static char **tnamep = 0;
663 if (tnamep && tnamep != unknown)
665 if ((tname = (char *)env_getvalue((unsigned char *)"TERM")) &&
666 telnet_setupterm(tname, 1, &err) == 0) {
667 tnamep = mklist(termbuf, tname);
669 if (tname && ((int)strlen(tname) <= 40)) {
673 unknown[0] = name_unknown;
685 * Look at the sub-option buffer, and try to be helpful to the other
688 * Currently we recognize:
690 * Terminal type, send request.
691 * Terminal speed (send request).
692 * Local flow control (is request).
699 unsigned char subchar;
701 printsub('<', subbuffer, SB_LEN()+2);
702 switch (subchar = SB_GET()) {
704 if (my_want_state_is_wont(TELOPT_TTYPE))
706 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
710 unsigned char temp[50];
713 name = gettermname();
714 len = strlen(name) + 4 + 2;
715 if (len < NETROOM()) {
716 snprintf((char *)temp, sizeof(temp),
717 "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
718 TELQUAL_IS, name, IAC, SE);
719 ring_supply_data(&netoring, temp, len);
720 printsub('>', &temp[2], len-2);
722 ExitString("No room in buffer for terminal type.\n", 1);
728 if (my_want_state_is_wont(TELOPT_TSPEED))
732 if (SB_GET() == TELQUAL_SEND) {
733 long output_speed, input_speed;
734 unsigned char temp[50];
737 TerminalSpeeds(&input_speed, &output_speed);
739 snprintf((char *)temp, sizeof(temp),
740 "%c%c%c%c%u,%u%c%c", IAC, SB, TELOPT_TSPEED,
742 (unsigned)output_speed,
743 (unsigned)input_speed, IAC, SE);
744 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
746 if (len < NETROOM()) {
747 ring_supply_data(&netoring, temp, len);
748 printsub('>', temp+2, len - 2);
750 /*@*/ else printf("lm_will: not enough room in buffer\n");
754 if (my_want_state_is_wont(TELOPT_LFLOW))
759 case LFLOW_RESTART_ANY:
762 case LFLOW_RESTART_XON:
778 case TELOPT_LINEMODE:
779 if (my_want_state_is_wont(TELOPT_LINEMODE))
785 lm_will(subpointer, SB_LEN());
788 lm_wont(subpointer, SB_LEN());
791 lm_do(subpointer, SB_LEN());
794 lm_dont(subpointer, SB_LEN());
797 slc(subpointer, SB_LEN());
800 lm_mode(subpointer, SB_LEN(), 0);
808 case TELOPT_OLD_ENVIRON:
810 case TELOPT_NEW_ENVIRON:
816 if (my_want_state_is_dont(subchar))
820 if (my_want_state_is_wont(subchar)) {
827 env_opt(subpointer, SB_LEN());
830 case TELOPT_XDISPLOC:
831 if (my_want_state_is_wont(TELOPT_XDISPLOC))
835 if (SB_GET() == TELQUAL_SEND) {
836 unsigned char temp[50], *dp;
839 if ((dp = env_getvalue((unsigned char *)"DISPLAY")) == NULL) {
841 * Something happened, we no longer have a DISPLAY
842 * variable. So, turn off the option.
844 send_wont(TELOPT_XDISPLOC, 1);
847 snprintf((char *)temp, sizeof(temp),
848 "%c%c%c%c%s%c%c", IAC, SB, TELOPT_XDISPLOC,
849 TELQUAL_IS, dp, IAC, SE);
850 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
852 if (len < NETROOM()) {
853 ring_supply_data(&netoring, temp, len);
854 printsub('>', temp+2, len - 2);
856 /*@*/ else printf("lm_will: not enough room in buffer\n");
860 #if defined(AUTHENTICATION)
861 case TELOPT_AUTHENTICATION: {
868 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
870 auth_is(subpointer, SB_LEN());
873 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
875 auth_send(subpointer, SB_LEN());
878 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
880 auth_reply(subpointer, SB_LEN());
883 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
885 auth_name(subpointer, SB_LEN());
891 #if defined(ENCRYPTION)
897 if (my_want_state_is_dont(TELOPT_ENCRYPT))
899 encrypt_start(subpointer, SB_LEN());
902 if (my_want_state_is_dont(TELOPT_ENCRYPT))
906 case ENCRYPT_SUPPORT:
907 if (my_want_state_is_wont(TELOPT_ENCRYPT))
909 encrypt_support(subpointer, SB_LEN());
911 case ENCRYPT_REQSTART:
912 if (my_want_state_is_wont(TELOPT_ENCRYPT))
914 encrypt_request_start(subpointer, SB_LEN());
917 if (my_want_state_is_wont(TELOPT_ENCRYPT))
920 * We can always send an REQEND so that we cannot
921 * get stuck encrypting. We should only get this
922 * if we have been able to get in the correct mode
925 encrypt_request_end();
928 if (my_want_state_is_dont(TELOPT_ENCRYPT))
930 encrypt_is(subpointer, SB_LEN());
933 if (my_want_state_is_wont(TELOPT_ENCRYPT))
935 encrypt_reply(subpointer, SB_LEN());
937 case ENCRYPT_ENC_KEYID:
938 if (my_want_state_is_dont(TELOPT_ENCRYPT))
940 encrypt_enc_keyid(subpointer, SB_LEN());
942 case ENCRYPT_DEC_KEYID:
943 if (my_want_state_is_wont(TELOPT_ENCRYPT))
945 encrypt_dec_keyid(subpointer, SB_LEN());
957 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
960 lm_will(unsigned char *cmd, int len)
963 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
967 case LM_FORWARDMASK: /* We shouldn't ever get this... */
971 if (NETROOM() > sizeof(str_lm)) {
972 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
973 printsub('>', &str_lm[2], sizeof(str_lm)-2);
975 /*@*/ else printf("lm_will: not enough room in buffer\n");
981 lm_wont(unsigned char *cmd, int len)
984 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
988 case LM_FORWARDMASK: /* We shouldn't ever get this... */
990 /* We are always DONT, so don't respond */
996 lm_do(unsigned char *cmd, int len)
999 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1003 case LM_FORWARDMASK:
1007 if (NETROOM() > sizeof(str_lm)) {
1008 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1009 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1011 /*@*/ else printf("lm_do: not enough room in buffer\n");
1017 lm_dont(unsigned char *cmd, int len)
1020 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1024 case LM_FORWARDMASK:
1026 /* we are always WONT, so don't respond */
1031 static unsigned char str_lm_mode[] = {
1032 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1036 lm_mode(unsigned char *cmd, int len, int init)
1040 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1044 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1045 str_lm_mode[4] = linemode;
1047 str_lm_mode[4] |= MODE_ACK;
1048 if (NETROOM() > sizeof(str_lm_mode)) {
1049 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1050 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1052 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1053 setconnmode(0); /* set changed mode */
1060 * Handle special character suboption of LINEMODE.
1066 char flags; /* Current flags & level */
1067 char mylevel; /* Maximum level & flags */
1070 #define SLC_IMPORT 0
1071 #define SLC_EXPORT 1
1072 #define SLC_RVALUE 2
1073 static int slc_mode = SLC_EXPORT;
1081 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1084 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1087 #define initfunc(func, flags) { \
1088 spcp = &spc_data[func]; \
1089 if ((spcp->valp = tcval(func))) { \
1090 spcp->val = *spcp->valp; \
1091 spcp->mylevel = SLC_VARIABLE|flags; \
1094 spcp->mylevel = SLC_DEFAULT; \
1098 initfunc(SLC_SYNCH, 0);
1100 initfunc(SLC_AO, 0);
1101 initfunc(SLC_AYT, 0);
1103 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1104 initfunc(SLC_EOF, 0);
1105 initfunc(SLC_SUSP, SLC_FLUSHIN);
1106 initfunc(SLC_EC, 0);
1107 initfunc(SLC_EL, 0);
1108 initfunc(SLC_EW, 0);
1109 initfunc(SLC_RP, 0);
1110 initfunc(SLC_LNEXT, 0);
1111 initfunc(SLC_XON, 0);
1112 initfunc(SLC_XOFF, 0);
1113 initfunc(SLC_FORW1, 0);
1114 initfunc(SLC_FORW2, 0);
1117 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1120 if (slc_mode == SLC_EXPORT)
1130 printf("Special characters are %s values\n",
1131 slc_mode == SLC_IMPORT ? "remote default" :
1132 slc_mode == SLC_EXPORT ? "local" :
1139 slc_mode = SLC_EXPORT;
1140 if (my_state_is_will(TELOPT_LINEMODE))
1145 slc_mode_import(int def)
1147 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1148 if (my_state_is_will(TELOPT_LINEMODE))
1152 unsigned char slc_import_val[] = {
1153 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1155 unsigned char slc_import_def[] = {
1156 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1162 if (NETROOM() > sizeof(slc_import_val)) {
1164 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1165 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1167 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1168 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1171 /*@*/ else printf("slc_import: not enough room\n");
1179 TerminalDefaultChars();
1182 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1183 if (spcp->mylevel != SLC_NOSUPPORT) {
1184 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1185 spcp->flags = SLC_NOSUPPORT;
1187 spcp->flags = spcp->mylevel;
1189 spcp->val = *spcp->valp;
1190 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1195 setconnmode(1); /* Make sure the character values are set */
1199 slc(unsigned char *cp, int len)
1206 for (; len >= 3; len -=3, cp +=3) {
1208 func = cp[SLC_FUNC];
1212 * Client side: always ignore 0 function.
1217 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1218 slc_add_reply(func, SLC_NOSUPPORT, 0);
1222 spcp = &spc_data[func];
1224 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1226 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1227 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1231 if (level == (SLC_DEFAULT|SLC_ACK)) {
1233 * This is an error condition, the SLC_ACK
1234 * bit should never be set for the SLC_DEFAULT
1235 * level. Our best guess to recover is to
1236 * ignore the SLC_ACK bit.
1238 cp[SLC_FLAGS] &= ~SLC_ACK;
1241 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1242 spcp->val = (cc_t)cp[SLC_VALUE];
1243 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1249 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1250 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1251 spcp->val = (cc_t)cp[SLC_VALUE];
1253 if (level == SLC_DEFAULT) {
1254 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1255 spcp->flags = spcp->mylevel;
1257 spcp->flags = SLC_NOSUPPORT;
1259 slc_add_reply(func, spcp->flags, spcp->val);
1263 setconnmode(1); /* set the new character values */
1272 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1273 if (spcp->valp && spcp->val != *spcp->valp) {
1274 spcp->val = *spcp->valp;
1275 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1276 spcp->flags = SLC_NOSUPPORT;
1278 spcp->flags = spcp->mylevel;
1279 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1287 unsigned char slc_reply[128];
1288 unsigned char *slc_replyp;
1293 slc_replyp = slc_reply;
1294 *slc_replyp++ = IAC;
1296 *slc_replyp++ = TELOPT_LINEMODE;
1297 *slc_replyp++ = LM_SLC;
1301 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1303 if ((*slc_replyp++ = func) == IAC)
1304 *slc_replyp++ = IAC;
1305 if ((*slc_replyp++ = flags) == IAC)
1306 *slc_replyp++ = IAC;
1307 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1308 *slc_replyp++ = IAC;
1316 *slc_replyp++ = IAC;
1318 len = slc_replyp - slc_reply;
1321 if (NETROOM() > len) {
1322 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1323 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1325 /*@*/else printf("slc_end_reply: not enough room\n");
1332 int need_update = 0;
1334 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1335 if (!(spcp->flags&SLC_ACK))
1337 spcp->flags &= ~SLC_ACK;
1338 if (spcp->valp && (*spcp->valp != spcp->val)) {
1339 *spcp->valp = spcp->val;
1343 return(need_update);
1347 # define old_env_var OLD_ENV_VAR
1348 # define old_env_value OLD_ENV_VALUE
1352 env_opt(unsigned char *buf, int len)
1354 unsigned char *ep = 0, *epc = 0;
1357 switch(buf[0]&0xff) {
1362 } else for (i = 1; i < len; i++) {
1363 switch (buf[i]&0xff) {
1368 * Although OLD_ENV_VALUE is not legal, we will
1369 * still recognize it, just in case it is an
1370 * old server that has VAR & VALUE mixed up...
1381 ep = epc = &buf[i+1];
1401 /* Ignore for now. We shouldn't get it anyway. */
1409 #define OPT_REPLY_SIZE 256
1410 unsigned char *opt_reply;
1411 unsigned char *opt_replyp;
1412 unsigned char *opt_replyend;
1418 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1420 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1421 if (opt_reply == NULL) {
1422 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1423 opt_reply = opt_replyp = opt_replyend = NULL;
1426 opt_replyp = opt_reply;
1427 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1428 *opt_replyp++ = IAC;
1430 *opt_replyp++ = telopt_environ;
1431 *opt_replyp++ = TELQUAL_IS;
1435 env_opt_start_info()
1439 opt_replyp[-1] = TELQUAL_INFO;
1443 env_opt_add(unsigned char *ep)
1445 unsigned char *vp, c;
1447 if (opt_reply == NULL) /*XXX*/
1450 if (ep == NULL || *ep == '\0') {
1451 /* Send user defined variables first. */
1453 while ((ep = env_default(0, 0)))
1456 /* Now add the list of well know variables. */
1458 while ((ep = env_default(0, 1)))
1462 vp = env_getvalue(ep);
1463 if (opt_replyp + (vp ? strlen((char *)vp) : 0) +
1464 strlen((char *)ep) + 6 > opt_replyend)
1467 opt_replyend += OPT_REPLY_SIZE;
1468 len = opt_replyend - opt_reply;
1469 opt_reply = (unsigned char *)realloc(opt_reply, len);
1470 if (opt_reply == NULL) {
1471 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1472 opt_reply = opt_replyp = opt_replyend = NULL;
1475 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1476 opt_replyend = opt_reply + len;
1478 if (opt_welldefined((char *)ep)) {
1480 if (telopt_environ == TELOPT_OLD_ENVIRON)
1481 *opt_replyp++ = old_env_var;
1484 *opt_replyp++ = NEW_ENV_VAR;
1486 *opt_replyp++ = ENV_USERVAR;
1488 while ((c = *ep++)) {
1491 *opt_replyp++ = IAC;
1497 *opt_replyp++ = ENV_ESC;
1504 if (telopt_environ == TELOPT_OLD_ENVIRON)
1505 *opt_replyp++ = old_env_value;
1508 *opt_replyp++ = NEW_ENV_VALUE;
1516 opt_welldefined(char *ep)
1518 if ((strcmp(ep, "USER") == 0) ||
1519 (strcmp(ep, "DISPLAY") == 0) ||
1520 (strcmp(ep, "PRINTER") == 0) ||
1521 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1522 (strcmp(ep, "JOB") == 0) ||
1523 (strcmp(ep, "ACCT") == 0))
1529 env_opt_end(int emptyok)
1533 len = opt_replyp - opt_reply + 2;
1534 if (emptyok || len > 6) {
1535 *opt_replyp++ = IAC;
1537 if (NETROOM() > len) {
1538 ring_supply_data(&netoring, opt_reply, len);
1539 printsub('>', &opt_reply[2], len - 2);
1541 /*@*/ else printf("slc_end_reply: not enough room\n");
1545 opt_reply = opt_replyp = opt_replyend = NULL;
1556 unsigned char *sbp = NULL;
1558 int returnValue = 0;
1562 while (TTYROOM() > 2) {
1565 ring_consumed(&netiring, count);
1569 sbp = netiring.consume;
1570 scc = ring_full_consecutive(&netiring);
1572 /* No more data coming in */
1577 c = *sbp++ & 0xff, scc--; count++;
1578 #if defined(ENCRYPTION)
1580 c = (*decrypt_input)(c);
1583 switch (telrcv_state) {
1586 telrcv_state = TS_DATA;
1588 break; /* Ignore \0 after CR */
1590 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1594 /* Else, fall through */
1598 telrcv_state = TS_IAC;
1602 * The 'crmod' hack (see following) is needed
1603 * since we can't set CRMOD on output only.
1604 * Machines like MULTICS like to send \r without
1605 * \n; since we must turn off CRMOD to get proper
1606 * input, the mapping is done here (sigh).
1608 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1611 #if defined(ENCRYPTION)
1613 c = (*decrypt_input)(c);
1616 sbp++, scc--; count++;
1619 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1621 sbp++, scc--; count++;
1624 #if defined(ENCRYPTION)
1626 (*decrypt_input)(-1);
1635 telrcv_state = TS_CR;
1651 telrcv_state = TS_WILL;
1655 telrcv_state = TS_WONT;
1659 telrcv_state = TS_DO;
1663 telrcv_state = TS_DONT;
1668 * We may have missed an urgent notification,
1669 * so make sure we flush whatever is in the
1672 printoption("RCVD", IAC, DM);
1675 SYNCHing = stilloob();
1681 telrcv_state = TS_SB;
1692 printoption("RCVD", IAC, c);
1695 telrcv_state = TS_DATA;
1699 printoption("RCVD", WILL, c);
1701 telrcv_state = TS_DATA;
1705 printoption("RCVD", WONT, c);
1707 telrcv_state = TS_DATA;
1711 printoption("RCVD", DO, c);
1713 if (c == TELOPT_NAWS) {
1715 } else if (c == TELOPT_LFLOW) {
1720 telrcv_state = TS_DATA;
1724 printoption("RCVD", DONT, c);
1727 setconnmode(0); /* set new tty mode (maybe) */
1728 telrcv_state = TS_DATA;
1733 telrcv_state = TS_SE;
1743 * This is an error. We only expect to get
1744 * "IAC IAC" or "IAC SE". Several things may
1745 * have happend. An IAC was not doubled, the
1746 * IAC SE was left off, or another option got
1747 * inserted into the suboption are all possibilities.
1748 * If we assume that the IAC was not doubled,
1749 * and really the IAC SE was left off, we could
1750 * get into an infinate loop here. So, instead,
1751 * we terminate the suboption, and process the
1752 * partial suboption if we can.
1759 printoption("In SUBOPTION processing, RCVD", IAC, c);
1760 suboption(); /* handle sub-option */
1761 telrcv_state = TS_IAC;
1765 telrcv_state = TS_SB;
1771 suboption(); /* handle sub-option */
1772 telrcv_state = TS_DATA;
1777 ring_consumed(&netiring, count);
1778 return returnValue||count;
1781 static int bol = 1, local = 0;
1789 command(0, "z\n", 2);
1800 int returnValue = 0;
1801 unsigned char *tbp = NULL;
1805 while (NETROOM() > 2) {
1811 ring_consumed(&ttyiring, count);
1815 tbp = ttyiring.consume;
1816 tcc = ring_full_consecutive(&ttyiring);
1821 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1822 if (rlogin != _POSIX_VDISABLE) {
1831 if (sc == '.' || c == termEofChar) {
1833 command(0, "close\n", 6);
1836 if (sc == termSuspChar) {
1838 command(0, "z\n", 2);
1842 command(0, (char *)tbp, tcc);
1856 if ((sc == '\n') || (sc == '\r'))
1858 } else if (sc == escape) {
1860 * Double escape is a pass through of a single escape character.
1862 if (tcc && strip(*tbp) == escape) {
1868 command(0, (char *)tbp, tcc);
1877 #ifdef KLUDGELINEMODE
1878 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1879 if (tcc > 0 && strip(*tbp) == echoc) {
1880 tcc--; tbp++; count++;
1882 dontlecho = !dontlecho;
1883 settimer(echotoggle);
1890 if (MODE_LOCAL_CHARS(globalmode)) {
1891 if (TerminalSpecialChars(sc) == 0) {
1896 if (my_want_state_is_wont(TELOPT_BINARY)) {
1900 * If we are in CRMOD mode (\r ==> \n)
1901 * on our local machine, then probably
1902 * a newline (unix) is CRLF (TELNET).
1904 if (MODE_LOCAL_CHARS(globalmode)) {
1908 bol = flushline = 1;
1912 NET2ADD('\r', '\0');
1914 NET2ADD('\r', '\n');
1916 bol = flushline = 1;
1925 } else if (c == IAC) {
1932 ring_consumed(&ttyiring, count);
1933 return returnValue||count; /* Non-zero if we did anything */
1939 * Try to do something.
1941 * If we do something useful, return 1; else return 0.
1947 Scheduler(int block) /* should we block in the select ? */
1949 /* One wants to be a bit careful about setting returnValue
1950 * to one, since a one implies we did some useful work,
1951 * and therefore probably won't be called to block next
1952 * time (TN3270 mode only).
1955 int netin, netout, netex, ttyin, ttyout;
1957 /* Decide which rings should be processed */
1959 netout = ring_full_count(&netoring) &&
1961 (my_want_state_is_wont(TELOPT_LINEMODE)
1962 #ifdef KLUDGELINEMODE
1963 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
1966 my_want_state_is_will(TELOPT_BINARY));
1967 ttyout = ring_full_count(&ttyoring);
1969 ttyin = ring_empty_count(&ttyiring);
1971 netin = !ISend && ring_empty_count(&netiring);
1975 /* If we have seen a signal recently, reset things */
1977 /* Call to system code to process rings */
1979 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
1981 /* Now, look at the input rings, looking for work to do. */
1983 if (ring_full_count(&ttyiring)) {
1984 returnValue |= telsnd();
1987 if (ring_full_count(&netiring)) {
1988 returnValue |= telrcv();
1994 * Select from tty and network...
1997 my_telnet(char *user)
2001 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2003 static char local_host[256] = { 0 };
2005 if (!local_host[0]) {
2006 /* XXX - should be k_gethostname? */
2007 gethostname(local_host, sizeof(local_host));
2008 local_host[sizeof(local_host)-1] = 0;
2010 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2011 auth_encrypt_user(user);
2015 #if defined(AUTHENTICATION)
2017 send_will(TELOPT_AUTHENTICATION, 1);
2019 #if defined(ENCRYPTION)
2020 send_do(TELOPT_ENCRYPT, 1);
2021 send_will(TELOPT_ENCRYPT, 1);
2023 send_do(TELOPT_SGA, 1);
2024 send_will(TELOPT_TTYPE, 1);
2025 send_will(TELOPT_NAWS, 1);
2026 send_will(TELOPT_TSPEED, 1);
2027 send_will(TELOPT_LFLOW, 1);
2028 send_will(TELOPT_LINEMODE, 1);
2029 send_will(TELOPT_NEW_ENVIRON, 1);
2030 send_do(TELOPT_STATUS, 1);
2031 if (env_getvalue((unsigned char *)"DISPLAY"))
2032 send_will(TELOPT_XDISPLOC, 1);
2034 tel_enter_binary(binary);
2040 while ((schedValue = Scheduler(0)) != 0) {
2041 if (schedValue == -1) {
2047 if (Scheduler(1) == -1) {
2057 * We are about to do a TELNET SYNCH operation. Clear
2058 * the path to the network.
2060 * Things are a bit tricky since we may have sent the first
2061 * byte or so of a previous TELNET command into the network.
2062 * So, we have to scan the network buffer from the beginning
2063 * until we are up to where we want to be.
2065 * A side effect of what we do, just to keep things
2066 * simple, is to clear the urgent data pointer. The principal
2067 * caller should be setting the urgent data pointer AFTER calling
2075 char *thisitem, *next;
2077 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
2078 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
2082 while ((next = nextitem(thisitem)) <= netobuf.send) {
2086 /* Now, thisitem is first before/at boundary. */
2088 good = netobuf; /* where the good bytes go */
2090 while (netoring.add > thisitem) {
2091 if (wewant(thisitem)) {
2096 next = nextitem(next);
2097 } while (wewant(next) && (nfrontp > next));
2098 length = next-thisitem;
2099 memmove(good, thisitem, length);
2103 thisitem = nextitem(thisitem);
2111 * These routines add various telnet commands to the data stream.
2121 ttyflush(1); /* Flush/drop output */
2122 /* do printoption AFTER flush, otherwise the output gets tossed... */
2123 printoption("SENT", DO, TELOPT_TM);
2130 printoption("SENT", IAC, AO);
2141 printoption("SENT", IAC, EL);
2148 printoption("SENT", IAC, EC);
2155 netclear(); /* clear the path to the network */
2159 printoption("SENT", IAC, DM);
2163 int want_status_response = 0;
2168 unsigned char tmp[16];
2171 if (my_want_state_is_dont(TELOPT_STATUS)) {
2172 printf("Remote side does not support STATUS option\n");
2179 *cp++ = TELOPT_STATUS;
2180 *cp++ = TELQUAL_SEND;
2183 if (NETROOM() >= cp - tmp) {
2184 ring_supply_data(&netoring, tmp, cp-tmp);
2185 printsub('>', tmp+2, cp - tmp - 2);
2187 ++want_status_response;
2195 printoption("SENT", IAC, IP);
2208 NET2ADD(IAC, BREAK);
2209 printoption("SENT", IAC, BREAK);
2222 NET2ADD(IAC, ABORT);
2223 printoption("SENT", IAC, ABORT);
2237 printoption("SENT", IAC, SUSP);
2251 printoption("SENT", IAC, xEOF);
2258 printoption("SENT", IAC, AYT);
2262 * Send a window size update to the remote system.
2269 unsigned char tmp[16];
2272 if (my_state_is_wont(TELOPT_NAWS))
2275 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2276 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2278 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2286 *cp++ = TELOPT_NAWS;
2291 if (NETROOM() >= cp - tmp) {
2292 ring_supply_data(&netoring, tmp, cp-tmp);
2293 printsub('>', tmp+2, cp - tmp - 2);
2298 tel_enter_binary(int rw)
2301 send_do(TELOPT_BINARY, 1);
2303 send_will(TELOPT_BINARY, 1);
2307 tel_leave_binary(int rw)
2310 send_dont(TELOPT_BINARY, 1);
2312 send_wont(TELOPT_BINARY, 1);