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.34 2002/05/03 10:19:43 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>)? */
74 SYNCHing, /* we are in TELNET SYNCH mode */
75 flushout, /* flush output */
76 autoflush = 0, /* flush output when interrupting? */
77 autosynch, /* send interrupt characters with SYNCH? */
78 localflow, /* we handle flow control locally */
79 restartany, /* if flow control enabled, restart on any character */
80 localchars, /* we recognize interrupt/quit */
81 donelclchars, /* the user has set "localchars" */
82 donebinarytoggle, /* the user has put us in binary */
83 dontlecho, /* do we suppress local echoing right now? */
88 int scheduler_lockout_tty = 0;
97 * Telnet receiver states for fsm
106 #define TS_SB 7 /* sub-option collection */
107 #define TS_SE 8 /* looking for sub-option end */
109 static int telrcv_state;
111 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
113 # define telopt_environ TELOPT_NEW_ENVIRON
122 #ifdef KLUDGELINEMODE
123 int kludgelinemode = 1;
127 * The following are some clocks used to decide how to interpret
128 * the relationship between various variables.
133 static int is_unique(char *name, char **as, char **ae);
137 * Initialize telnet environment.
146 memset(options, 0, sizeof options);
148 connected = ISend = localflow = donebinarytoggle = 0;
149 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
150 auth_encrypt_connect(connected);
151 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
156 /* Don't change NetTrace */
158 escape = CONTROL(']');
159 rlogin = _POSIX_VDISABLE;
160 #ifdef KLUDGELINEMODE
161 echoc = CONTROL('E');
165 telrcv_state = TS_DATA;
170 * These routines are in charge of sending option negotiations
173 * The basic idea is that we send the negotiation if either side
174 * is in disagreement as to what the current state should be.
178 send_do(int c, int init)
181 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
182 my_want_state_is_do(c))
184 set_my_want_state_do(c);
189 printoption("SENT", DO, c);
193 send_dont(int c, int init)
196 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
197 my_want_state_is_dont(c))
199 set_my_want_state_dont(c);
204 printoption("SENT", DONT, c);
208 send_will(int c, int init)
211 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
212 my_want_state_is_will(c))
214 set_my_want_state_will(c);
219 printoption("SENT", WILL, c);
223 send_wont(int c, int init)
226 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
227 my_want_state_is_wont(c))
229 set_my_want_state_wont(c);
234 printoption("SENT", WONT, c);
239 willoption(int option)
241 int new_state_ok = 0;
243 if (do_dont_resp[option]) {
244 --do_dont_resp[option];
245 if (do_dont_resp[option] && my_state_is_do(option))
246 --do_dont_resp[option];
249 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
256 settimer(modenegotiated);
259 #if defined(AUTHENTICATION)
260 case TELOPT_AUTHENTICATION:
262 #if defined(ENCRYPTION)
272 * Special case for TM. If we get back a WILL,
273 * pretend we got back a WONT.
275 set_my_want_state_dont(option);
276 set_my_state_dont(option);
277 return; /* Never reply to TM will's/wont's */
279 case TELOPT_LINEMODE:
285 set_my_want_state_do(option);
287 setconnmode(0); /* possibly set new tty mode */
289 do_dont_resp[option]++;
290 send_dont(option, 0);
293 set_my_state_do(option);
294 #if defined(ENCRYPTION)
295 if (option == TELOPT_ENCRYPT)
296 encrypt_send_support();
301 wontoption(int option)
303 if (do_dont_resp[option]) {
304 --do_dont_resp[option];
305 if (do_dont_resp[option] && my_state_is_dont(option))
306 --do_dont_resp[option];
309 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
313 #ifdef KLUDGELINEMODE
320 settimer(modenegotiated);
326 set_my_want_state_dont(option);
327 set_my_state_dont(option);
328 return; /* Never reply to TM will's/wont's */
338 set_my_want_state_dont(option);
339 if (my_state_is_do(option))
340 send_dont(option, 0);
341 setconnmode(0); /* Set new tty mode */
342 } else if (option == TELOPT_TM) {
344 * Special case for TM.
348 set_my_want_state_dont(option);
350 set_my_state_dont(option);
356 int new_state_ok = 0;
358 if (will_wont_resp[option]) {
359 --will_wont_resp[option];
360 if (will_wont_resp[option] && my_state_is_will(option))
361 --will_wont_resp[option];
364 if (will_wont_resp[option] == 0) {
365 if (my_want_state_is_wont(option)) {
371 * Special case for TM. We send a WILL, but pretend
374 send_will(option, 0);
375 set_my_want_state_wont(TELOPT_TM);
376 set_my_state_wont(TELOPT_TM);
379 case TELOPT_BINARY: /* binary mode */
380 case TELOPT_NAWS: /* window size */
381 case TELOPT_TSPEED: /* terminal speed */
382 case TELOPT_LFLOW: /* local flow control */
383 case TELOPT_TTYPE: /* terminal type option */
384 case TELOPT_SGA: /* no big deal */
385 #if defined(ENCRYPTION)
386 case TELOPT_ENCRYPT: /* encryption variable option */
391 case TELOPT_NEW_ENVIRON: /* New environment variable option */
393 if (my_state_is_will(TELOPT_OLD_ENVIRON))
394 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
396 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
397 if (my_state_is_will(TELOPT_NEW_ENVIRON))
398 break; /* Don't enable if new one is in use! */
400 telopt_environ = option;
405 #if defined(AUTHENTICATION)
406 case TELOPT_AUTHENTICATION:
412 case TELOPT_XDISPLOC: /* X Display location */
413 if (env_getvalue((unsigned char *)"DISPLAY"))
417 case TELOPT_LINEMODE:
418 #ifdef KLUDGELINEMODE
420 send_do(TELOPT_SGA, 1);
422 set_my_want_state_will(TELOPT_LINEMODE);
423 send_will(option, 0);
424 set_my_state_will(TELOPT_LINEMODE);
428 case TELOPT_ECHO: /* We're never going to echo... */
434 set_my_want_state_will(option);
435 send_will(option, 0);
436 setconnmode(0); /* Set new tty mode */
438 will_wont_resp[option]++;
439 send_wont(option, 0);
443 * Handle options that need more things done after the
444 * other side has acknowledged the option.
447 case TELOPT_LINEMODE:
448 #ifdef KLUDGELINEMODE
450 send_do(TELOPT_SGA, 1);
452 set_my_state_will(option);
454 send_do(TELOPT_SGA, 0);
459 set_my_state_will(option);
463 dontoption(int option)
466 if (will_wont_resp[option]) {
467 --will_wont_resp[option];
468 if (will_wont_resp[option] && my_state_is_wont(option))
469 --will_wont_resp[option];
472 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
474 case TELOPT_LINEMODE:
475 linemode = 0; /* put us back to the default state */
478 case TELOPT_NEW_ENVIRON:
480 * The new environ option wasn't recognized, try
483 send_will(TELOPT_OLD_ENVIRON, 1);
484 telopt_environ = TELOPT_OLD_ENVIRON;
495 /* we always accept a DONT */
496 set_my_want_state_wont(option);
497 if (my_state_is_will(option))
498 send_wont(option, 0);
499 setconnmode(0); /* Set new tty mode */
501 set_my_state_wont(option);
505 * Given a buffer returned by tgetent(), this routine will turn
506 * the pipe seperated list of names in the buffer into an array
507 * of pointers to null terminated names. We toss out any bad,
508 * duplicate, or verbose names (names with spaces).
511 static char *name_unknown = "UNKNOWN";
512 static char *unknown[] = { 0, 0 };
515 mklist(char *buf, char *name)
518 char c, *cp, **argvp, *cp2, **argv, **avt;
521 if ((int)strlen(name) > 40) {
523 unknown[0] = name_unknown;
529 unknown[0] = name_unknown;
531 * Count up the number of names.
533 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
538 * Allocate an array to put the name pointers into
540 argv = (char **)malloc((n+3)*sizeof(char *));
545 * Fill up the array of pointers to names.
550 for (cp = cp2 = buf; (c = *cp); cp++) {
551 if (c == '|' || c == ':') {
554 * Skip entries that have spaces or are over 40
555 * characters long. If this is our environment
556 * name, then put it up front. Otherwise, as
557 * long as this is not a duplicate name (case
558 * insensitive) add it to the list.
560 if (n || (cp - cp2 > 41))
562 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
564 else if (is_unique(cp2, argv+1, argvp))
569 * Skip multiple delimiters. Reset cp2 to
570 * the beginning of the next name. Reset n,
571 * the flag for names with spaces.
573 while ((c = *cp) == '|')
579 * Skip entries with spaces or non-ascii values.
580 * Convert lower case letters to upper case.
582 #define ISASCII(c) (!((c)&0x80))
583 if ((c == ' ') || !ISASCII(c))
585 else if (islower((unsigned char)c))
590 * Check for an old V6 2 character name. If the second
591 * name points to the beginning of the buffer, and is
592 * only 2 characters long, move it to the end of the array.
594 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
596 for (avt = &argv[1]; avt < argvp; avt++)
602 * Duplicate last name, for TTYPE option, and null
603 * terminate the array. If we didn't find a match on
604 * our terminal name, put that name at the beginning.
615 for (avt = argv; avt < argvp; avt++)
626 is_unique(char *name, char **as, char **ae)
631 n = strlen(name) + 1;
632 for (ap = as; ap < ae; ap++)
633 if (strncasecmp(*ap, name, n) == 0)
638 static char termbuf[1024];
641 telnet_setupterm(const char *tname, int fd, int *errp)
644 if (tgetent(termbuf, tname) == 1) {
645 termbuf[1023] = '\0';
654 strlcpy(termbuf, tname, sizeof(termbuf));
660 int resettermname = 1;
666 static char **tnamep = 0;
672 if (tnamep && tnamep != unknown)
674 if ((tname = (char *)env_getvalue((unsigned char *)"TERM")) &&
675 telnet_setupterm(tname, 1, &err) == 0) {
676 tnamep = mklist(termbuf, tname);
678 if (tname && ((int)strlen(tname) <= 40)) {
682 unknown[0] = name_unknown;
694 * Look at the sub-option buffer, and try to be helpful to the other
697 * Currently we recognize:
699 * Terminal type, send request.
700 * Terminal speed (send request).
701 * Local flow control (is request).
708 unsigned char subchar;
710 printsub('<', subbuffer, SB_LEN()+2);
711 switch (subchar = SB_GET()) {
713 if (my_want_state_is_wont(TELOPT_TTYPE))
715 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
719 unsigned char temp[50];
722 name = gettermname();
723 len = strlen(name) + 4 + 2;
724 if (len < NETROOM()) {
725 snprintf((char *)temp, sizeof(temp),
726 "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
727 TELQUAL_IS, name, IAC, SE);
728 ring_supply_data(&netoring, temp, len);
729 printsub('>', &temp[2], len-2);
731 ExitString("No room in buffer for terminal type.\n", 1);
737 if (my_want_state_is_wont(TELOPT_TSPEED))
741 if (SB_GET() == TELQUAL_SEND) {
742 long output_speed, input_speed;
743 unsigned char temp[50];
746 TerminalSpeeds(&input_speed, &output_speed);
748 snprintf((char *)temp, sizeof(temp),
749 "%c%c%c%c%u,%u%c%c", IAC, SB, TELOPT_TSPEED,
751 (unsigned)output_speed,
752 (unsigned)input_speed, IAC, SE);
753 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
755 if (len < NETROOM()) {
756 ring_supply_data(&netoring, temp, len);
757 printsub('>', temp+2, len - 2);
759 /*@*/ else printf("lm_will: not enough room in buffer\n");
763 if (my_want_state_is_wont(TELOPT_LFLOW))
768 case LFLOW_RESTART_ANY:
771 case LFLOW_RESTART_XON:
787 case TELOPT_LINEMODE:
788 if (my_want_state_is_wont(TELOPT_LINEMODE))
794 lm_will(subpointer, SB_LEN());
797 lm_wont(subpointer, SB_LEN());
800 lm_do(subpointer, SB_LEN());
803 lm_dont(subpointer, SB_LEN());
806 slc(subpointer, SB_LEN());
809 lm_mode(subpointer, SB_LEN(), 0);
817 case TELOPT_OLD_ENVIRON:
819 case TELOPT_NEW_ENVIRON:
825 if (my_want_state_is_dont(subchar))
829 if (my_want_state_is_wont(subchar)) {
836 env_opt(subpointer, SB_LEN());
839 case TELOPT_XDISPLOC:
840 if (my_want_state_is_wont(TELOPT_XDISPLOC))
844 if (SB_GET() == TELQUAL_SEND) {
845 unsigned char temp[50], *dp;
848 if ((dp = env_getvalue((unsigned char *)"DISPLAY")) == NULL) {
850 * Something happened, we no longer have a DISPLAY
851 * variable. So, turn off the option.
853 send_wont(TELOPT_XDISPLOC, 1);
856 snprintf((char *)temp, sizeof(temp),
857 "%c%c%c%c%s%c%c", IAC, SB, TELOPT_XDISPLOC,
858 TELQUAL_IS, dp, IAC, SE);
859 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
861 if (len < NETROOM()) {
862 ring_supply_data(&netoring, temp, len);
863 printsub('>', temp+2, len - 2);
865 /*@*/ else printf("lm_will: not enough room in buffer\n");
869 #if defined(AUTHENTICATION)
870 case TELOPT_AUTHENTICATION: {
877 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
879 auth_is(subpointer, SB_LEN());
882 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
884 auth_send(subpointer, SB_LEN());
887 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
889 auth_reply(subpointer, SB_LEN());
892 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
894 auth_name(subpointer, SB_LEN());
900 #if defined(ENCRYPTION)
906 if (my_want_state_is_dont(TELOPT_ENCRYPT))
908 encrypt_start(subpointer, SB_LEN());
911 if (my_want_state_is_dont(TELOPT_ENCRYPT))
915 case ENCRYPT_SUPPORT:
916 if (my_want_state_is_wont(TELOPT_ENCRYPT))
918 encrypt_support(subpointer, SB_LEN());
920 case ENCRYPT_REQSTART:
921 if (my_want_state_is_wont(TELOPT_ENCRYPT))
923 encrypt_request_start(subpointer, SB_LEN());
926 if (my_want_state_is_wont(TELOPT_ENCRYPT))
929 * We can always send an REQEND so that we cannot
930 * get stuck encrypting. We should only get this
931 * if we have been able to get in the correct mode
934 encrypt_request_end();
937 if (my_want_state_is_dont(TELOPT_ENCRYPT))
939 encrypt_is(subpointer, SB_LEN());
942 if (my_want_state_is_wont(TELOPT_ENCRYPT))
944 encrypt_reply(subpointer, SB_LEN());
946 case ENCRYPT_ENC_KEYID:
947 if (my_want_state_is_dont(TELOPT_ENCRYPT))
949 encrypt_enc_keyid(subpointer, SB_LEN());
951 case ENCRYPT_DEC_KEYID:
952 if (my_want_state_is_wont(TELOPT_ENCRYPT))
954 encrypt_dec_keyid(subpointer, SB_LEN());
966 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
969 lm_will(unsigned char *cmd, int len)
972 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
976 case LM_FORWARDMASK: /* We shouldn't ever get this... */
980 if (NETROOM() > sizeof(str_lm)) {
981 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
982 printsub('>', &str_lm[2], sizeof(str_lm)-2);
984 /*@*/ else printf("lm_will: not enough room in buffer\n");
990 lm_wont(unsigned char *cmd, int len)
993 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
997 case LM_FORWARDMASK: /* We shouldn't ever get this... */
999 /* We are always DONT, so don't respond */
1005 lm_do(unsigned char *cmd, int len)
1008 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1012 case LM_FORWARDMASK:
1016 if (NETROOM() > sizeof(str_lm)) {
1017 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1018 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1020 /*@*/ else printf("lm_do: not enough room in buffer\n");
1026 lm_dont(unsigned char *cmd, int len)
1029 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1033 case LM_FORWARDMASK:
1035 /* we are always WONT, so don't respond */
1040 static unsigned char str_lm_mode[] = {
1041 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1045 lm_mode(unsigned char *cmd, int len, int init)
1049 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1053 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1054 str_lm_mode[4] = linemode;
1056 str_lm_mode[4] |= MODE_ACK;
1057 if (NETROOM() > sizeof(str_lm_mode)) {
1058 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1059 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1061 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1062 setconnmode(0); /* set changed mode */
1069 * Handle special character suboption of LINEMODE.
1075 char flags; /* Current flags & level */
1076 char mylevel; /* Maximum level & flags */
1079 #define SLC_IMPORT 0
1080 #define SLC_EXPORT 1
1081 #define SLC_RVALUE 2
1082 static int slc_mode = SLC_EXPORT;
1090 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1093 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1096 #define initfunc(func, flags) { \
1097 spcp = &spc_data[func]; \
1098 if ((spcp->valp = tcval(func))) { \
1099 spcp->val = *spcp->valp; \
1100 spcp->mylevel = SLC_VARIABLE|flags; \
1103 spcp->mylevel = SLC_DEFAULT; \
1107 initfunc(SLC_SYNCH, 0);
1109 initfunc(SLC_AO, 0);
1110 initfunc(SLC_AYT, 0);
1112 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1113 initfunc(SLC_EOF, 0);
1114 initfunc(SLC_SUSP, SLC_FLUSHIN);
1115 initfunc(SLC_EC, 0);
1116 initfunc(SLC_EL, 0);
1117 initfunc(SLC_EW, 0);
1118 initfunc(SLC_RP, 0);
1119 initfunc(SLC_LNEXT, 0);
1120 initfunc(SLC_XON, 0);
1121 initfunc(SLC_XOFF, 0);
1122 initfunc(SLC_FORW1, 0);
1123 initfunc(SLC_FORW2, 0);
1126 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1129 if (slc_mode == SLC_EXPORT)
1139 printf("Special characters are %s values\n",
1140 slc_mode == SLC_IMPORT ? "remote default" :
1141 slc_mode == SLC_EXPORT ? "local" :
1148 slc_mode = SLC_EXPORT;
1149 if (my_state_is_will(TELOPT_LINEMODE))
1154 slc_mode_import(int def)
1156 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1157 if (my_state_is_will(TELOPT_LINEMODE))
1161 unsigned char slc_import_val[] = {
1162 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1164 unsigned char slc_import_def[] = {
1165 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1171 if (NETROOM() > sizeof(slc_import_val)) {
1173 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1174 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1176 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1177 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1180 /*@*/ else printf("slc_import: not enough room\n");
1188 TerminalDefaultChars();
1191 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1192 if (spcp->mylevel != SLC_NOSUPPORT) {
1193 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1194 spcp->flags = SLC_NOSUPPORT;
1196 spcp->flags = spcp->mylevel;
1198 spcp->val = *spcp->valp;
1199 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1204 setconnmode(1); /* Make sure the character values are set */
1208 slc(unsigned char *cp, int len)
1215 for (; len >= 3; len -=3, cp +=3) {
1217 func = cp[SLC_FUNC];
1221 * Client side: always ignore 0 function.
1226 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1227 slc_add_reply(func, SLC_NOSUPPORT, 0);
1231 spcp = &spc_data[func];
1233 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1235 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1236 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1240 if (level == (SLC_DEFAULT|SLC_ACK)) {
1242 * This is an error condition, the SLC_ACK
1243 * bit should never be set for the SLC_DEFAULT
1244 * level. Our best guess to recover is to
1245 * ignore the SLC_ACK bit.
1247 cp[SLC_FLAGS] &= ~SLC_ACK;
1250 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1251 spcp->val = (cc_t)cp[SLC_VALUE];
1252 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1258 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1259 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1260 spcp->val = (cc_t)cp[SLC_VALUE];
1262 if (level == SLC_DEFAULT) {
1263 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1264 spcp->flags = spcp->mylevel;
1266 spcp->flags = SLC_NOSUPPORT;
1268 slc_add_reply(func, spcp->flags, spcp->val);
1272 setconnmode(1); /* set the new character values */
1281 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1282 if (spcp->valp && spcp->val != *spcp->valp) {
1283 spcp->val = *spcp->valp;
1284 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1285 spcp->flags = SLC_NOSUPPORT;
1287 spcp->flags = spcp->mylevel;
1288 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1296 unsigned char slc_reply[128];
1297 unsigned char *slc_replyp;
1302 slc_replyp = slc_reply;
1303 *slc_replyp++ = IAC;
1305 *slc_replyp++ = TELOPT_LINEMODE;
1306 *slc_replyp++ = LM_SLC;
1310 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1312 if ((*slc_replyp++ = func) == IAC)
1313 *slc_replyp++ = IAC;
1314 if ((*slc_replyp++ = flags) == IAC)
1315 *slc_replyp++ = IAC;
1316 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1317 *slc_replyp++ = IAC;
1325 *slc_replyp++ = IAC;
1327 len = slc_replyp - slc_reply;
1330 if (NETROOM() > len) {
1331 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1332 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1334 /*@*/else printf("slc_end_reply: not enough room\n");
1341 int need_update = 0;
1343 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1344 if (!(spcp->flags&SLC_ACK))
1346 spcp->flags &= ~SLC_ACK;
1347 if (spcp->valp && (*spcp->valp != spcp->val)) {
1348 *spcp->valp = spcp->val;
1352 return(need_update);
1356 # define old_env_var OLD_ENV_VAR
1357 # define old_env_value OLD_ENV_VALUE
1361 env_opt(unsigned char *buf, int len)
1363 unsigned char *ep = 0, *epc = 0;
1366 switch(buf[0]&0xff) {
1371 } else for (i = 1; i < len; i++) {
1372 switch (buf[i]&0xff) {
1377 * Although OLD_ENV_VALUE is not legal, we will
1378 * still recognize it, just in case it is an
1379 * old server that has VAR & VALUE mixed up...
1390 ep = epc = &buf[i+1];
1410 /* Ignore for now. We shouldn't get it anyway. */
1418 #define OPT_REPLY_SIZE 256
1419 unsigned char *opt_reply;
1420 unsigned char *opt_replyp;
1421 unsigned char *opt_replyend;
1427 void *tmp = realloc (opt_reply, OPT_REPLY_SIZE);
1435 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1436 if (opt_reply == NULL) {
1437 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1438 opt_reply = opt_replyp = opt_replyend = NULL;
1441 opt_replyp = opt_reply;
1442 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1443 *opt_replyp++ = IAC;
1445 *opt_replyp++ = telopt_environ;
1446 *opt_replyp++ = TELQUAL_IS;
1450 env_opt_start_info()
1454 opt_replyp[-1] = TELQUAL_INFO;
1458 env_opt_add(unsigned char *ep)
1460 unsigned char *vp, c;
1462 if (opt_reply == NULL) /*XXX*/
1465 if (ep == NULL || *ep == '\0') {
1466 /* Send user defined variables first. */
1468 while ((ep = env_default(0, 0)))
1471 /* Now add the list of well know variables. */
1473 while ((ep = env_default(0, 1)))
1477 vp = env_getvalue(ep);
1478 if (opt_replyp + (vp ? strlen((char *)vp) : 0) +
1479 strlen((char *)ep) + 6 > opt_replyend)
1483 opt_replyend += OPT_REPLY_SIZE;
1484 len = opt_replyend - opt_reply;
1485 tmp = realloc(opt_reply, len);
1487 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1488 opt_reply = opt_replyp = opt_replyend = NULL;
1492 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1493 opt_replyend = opt_reply + len;
1495 if (opt_welldefined((char *)ep)) {
1497 if (telopt_environ == TELOPT_OLD_ENVIRON)
1498 *opt_replyp++ = old_env_var;
1501 *opt_replyp++ = NEW_ENV_VAR;
1503 *opt_replyp++ = ENV_USERVAR;
1505 while ((c = *ep++)) {
1508 *opt_replyp++ = IAC;
1514 *opt_replyp++ = ENV_ESC;
1521 if (telopt_environ == TELOPT_OLD_ENVIRON)
1522 *opt_replyp++ = old_env_value;
1525 *opt_replyp++ = NEW_ENV_VALUE;
1533 opt_welldefined(char *ep)
1535 if ((strcmp(ep, "USER") == 0) ||
1536 (strcmp(ep, "DISPLAY") == 0) ||
1537 (strcmp(ep, "PRINTER") == 0) ||
1538 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1539 (strcmp(ep, "JOB") == 0) ||
1540 (strcmp(ep, "ACCT") == 0))
1546 env_opt_end(int emptyok)
1550 len = opt_replyp - opt_reply + 2;
1551 if (emptyok || len > 6) {
1552 *opt_replyp++ = IAC;
1554 if (NETROOM() > len) {
1555 ring_supply_data(&netoring, opt_reply, len);
1556 printsub('>', &opt_reply[2], len - 2);
1558 /*@*/ else printf("slc_end_reply: not enough room\n");
1562 opt_reply = opt_replyp = opt_replyend = NULL;
1573 unsigned char *sbp = NULL;
1575 int returnValue = 0;
1579 while (TTYROOM() > 2) {
1582 ring_consumed(&netiring, count);
1586 sbp = netiring.consume;
1587 scc = ring_full_consecutive(&netiring);
1589 /* No more data coming in */
1594 c = *sbp++ & 0xff, scc--; count++;
1595 #if defined(ENCRYPTION)
1597 c = (*decrypt_input)(c);
1600 switch (telrcv_state) {
1603 telrcv_state = TS_DATA;
1605 break; /* Ignore \0 after CR */
1607 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1611 /* Else, fall through */
1615 telrcv_state = TS_IAC;
1619 * The 'crmod' hack (see following) is needed
1620 * since we can't set CRMOD on output only.
1621 * Machines like MULTICS like to send \r without
1622 * \n; since we must turn off CRMOD to get proper
1623 * input, the mapping is done here (sigh).
1625 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1628 #if defined(ENCRYPTION)
1630 c = (*decrypt_input)(c);
1633 sbp++, scc--; count++;
1636 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1638 sbp++, scc--; count++;
1641 #if defined(ENCRYPTION)
1643 (*decrypt_input)(-1);
1652 telrcv_state = TS_CR;
1668 telrcv_state = TS_WILL;
1672 telrcv_state = TS_WONT;
1676 telrcv_state = TS_DO;
1680 telrcv_state = TS_DONT;
1685 * We may have missed an urgent notification,
1686 * so make sure we flush whatever is in the
1689 printoption("RCVD", IAC, DM);
1692 SYNCHing = stilloob();
1698 telrcv_state = TS_SB;
1709 printoption("RCVD", IAC, c);
1712 telrcv_state = TS_DATA;
1716 printoption("RCVD", WILL, c);
1718 telrcv_state = TS_DATA;
1722 printoption("RCVD", WONT, c);
1724 telrcv_state = TS_DATA;
1728 printoption("RCVD", DO, c);
1730 if (c == TELOPT_NAWS) {
1732 } else if (c == TELOPT_LFLOW) {
1737 telrcv_state = TS_DATA;
1741 printoption("RCVD", DONT, c);
1744 setconnmode(0); /* set new tty mode (maybe) */
1745 telrcv_state = TS_DATA;
1750 telrcv_state = TS_SE;
1760 * This is an error. We only expect to get
1761 * "IAC IAC" or "IAC SE". Several things may
1762 * have happend. An IAC was not doubled, the
1763 * IAC SE was left off, or another option got
1764 * inserted into the suboption are all possibilities.
1765 * If we assume that the IAC was not doubled,
1766 * and really the IAC SE was left off, we could
1767 * get into an infinate loop here. So, instead,
1768 * we terminate the suboption, and process the
1769 * partial suboption if we can.
1776 printoption("In SUBOPTION processing, RCVD", IAC, c);
1777 suboption(); /* handle sub-option */
1778 telrcv_state = TS_IAC;
1782 telrcv_state = TS_SB;
1788 suboption(); /* handle sub-option */
1789 telrcv_state = TS_DATA;
1794 ring_consumed(&netiring, count);
1795 return returnValue||count;
1798 static int bol = 1, local = 0;
1806 command(0, "z\n", 2);
1817 int returnValue = 0;
1818 unsigned char *tbp = NULL;
1822 while (NETROOM() > 2) {
1828 ring_consumed(&ttyiring, count);
1832 tbp = ttyiring.consume;
1833 tcc = ring_full_consecutive(&ttyiring);
1838 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1839 if (rlogin != _POSIX_VDISABLE) {
1848 if (sc == '.' || c == termEofChar) {
1850 command(0, "close\n", 6);
1853 if (sc == termSuspChar) {
1855 command(0, "z\n", 2);
1859 command(0, (char *)tbp, tcc);
1873 if ((sc == '\n') || (sc == '\r'))
1875 } else if (sc == escape) {
1877 * Double escape is a pass through of a single escape character.
1879 if (tcc && strip(*tbp) == escape) {
1885 command(0, (char *)tbp, tcc);
1894 #ifdef KLUDGELINEMODE
1895 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1896 if (tcc > 0 && strip(*tbp) == echoc) {
1897 tcc--; tbp++; count++;
1899 dontlecho = !dontlecho;
1900 settimer(echotoggle);
1907 if (MODE_LOCAL_CHARS(globalmode)) {
1908 if (TerminalSpecialChars(sc) == 0) {
1913 if (my_want_state_is_wont(TELOPT_BINARY)) {
1917 * If we are in CRMOD mode (\r ==> \n)
1918 * on our local machine, then probably
1919 * a newline (unix) is CRLF (TELNET).
1921 if (MODE_LOCAL_CHARS(globalmode)) {
1925 bol = flushline = 1;
1929 NET2ADD('\r', '\0');
1931 NET2ADD('\r', '\n');
1933 bol = flushline = 1;
1942 } else if (c == IAC) {
1949 ring_consumed(&ttyiring, count);
1950 return returnValue||count; /* Non-zero if we did anything */
1956 * Try to do something.
1958 * If we do something useful, return 1; else return 0.
1964 Scheduler(int block) /* should we block in the select ? */
1966 /* One wants to be a bit careful about setting returnValue
1967 * to one, since a one implies we did some useful work,
1968 * and therefore probably won't be called to block next
1969 * time (TN3270 mode only).
1972 int netin, netout, netex, ttyin, ttyout;
1974 /* Decide which rings should be processed */
1976 netout = ring_full_count(&netoring) &&
1978 (my_want_state_is_wont(TELOPT_LINEMODE)
1979 #ifdef KLUDGELINEMODE
1980 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
1983 my_want_state_is_will(TELOPT_BINARY));
1984 ttyout = ring_full_count(&ttyoring);
1986 ttyin = ring_empty_count(&ttyiring);
1988 netin = !ISend && ring_empty_count(&netiring);
1992 /* If we have seen a signal recently, reset things */
1994 if (scheduler_lockout_tty) {
1998 /* Call to system code to process rings */
2000 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2002 /* Now, look at the input rings, looking for work to do. */
2004 if (ring_full_count(&ttyiring)) {
2005 returnValue |= telsnd();
2008 if (ring_full_count(&netiring)) {
2009 returnValue |= telrcv();
2015 * Select from tty and network...
2018 my_telnet(char *user)
2020 int printed_encrypt = 0;
2024 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2026 static char local_host[256] = { 0 };
2028 if (!local_host[0]) {
2029 /* XXX - should be k_gethostname? */
2030 gethostname(local_host, sizeof(local_host));
2031 local_host[sizeof(local_host)-1] = 0;
2033 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2034 auth_encrypt_user(user);
2038 #if defined(AUTHENTICATION)
2040 send_will(TELOPT_AUTHENTICATION, 1);
2042 #if defined(ENCRYPTION)
2043 send_do(TELOPT_ENCRYPT, 1);
2044 send_will(TELOPT_ENCRYPT, 1);
2046 send_do(TELOPT_SGA, 1);
2047 send_will(TELOPT_TTYPE, 1);
2048 send_will(TELOPT_NAWS, 1);
2049 send_will(TELOPT_TSPEED, 1);
2050 send_will(TELOPT_LFLOW, 1);
2051 send_will(TELOPT_LINEMODE, 1);
2052 send_will(TELOPT_NEW_ENVIRON, 1);
2053 send_do(TELOPT_STATUS, 1);
2054 if (env_getvalue((unsigned char *)"DISPLAY"))
2055 send_will(TELOPT_XDISPLOC, 1);
2057 tel_enter_binary(binary);
2062 * Note: we assume a tie to the authentication option here. This
2063 * is necessary so that authentication fails, we don't spin
2066 if (telnetport && wantencryption) {
2067 extern int auth_has_failed;
2068 time_t timeout = time(0) + 60;
2070 send_do(TELOPT_ENCRYPT, 1);
2071 send_will(TELOPT_ENCRYPT, 1);
2073 if (my_want_state_is_wont(TELOPT_AUTHENTICATION)) {
2074 if (wantencryption == -1) {
2077 printf("\nServer refused to negotiate authentication,\n");
2078 printf("which is required for encryption.\n");
2082 if (auth_has_failed) {
2083 printf("\nAuthentication negotation has failed,\n");
2084 printf("which is required for encryption.\n");
2087 if (my_want_state_is_dont(TELOPT_ENCRYPT) ||
2088 my_want_state_is_wont(TELOPT_ENCRYPT)) {
2089 printf("\nServer refused to negotiate encryption.\n");
2092 if (encrypt_is_encrypting())
2094 if (time(0) > timeout) {
2095 printf("\nEncryption could not be enabled.\n");
2098 if (printed_encrypt == 0) {
2099 printed_encrypt = 1;
2100 printf("Waiting for encryption to be negotiated...\n");
2102 * Turn on MODE_TRAPSIG and then turn off localchars
2103 * so that ^C will cause telnet to exit.
2105 TerminalNewMode(getconnmode()|MODE_TRAPSIG);
2108 if (intr_happened) {
2109 printf("\nUser interrupt.\n");
2114 if (printed_encrypt) {
2115 printf("Encryption negotiated.\n");
2125 while ((schedValue = Scheduler(0)) != 0) {
2126 if (schedValue == -1) {
2132 if (Scheduler(1) == -1) {
2142 * We are about to do a TELNET SYNCH operation. Clear
2143 * the path to the network.
2145 * Things are a bit tricky since we may have sent the first
2146 * byte or so of a previous TELNET command into the network.
2147 * So, we have to scan the network buffer from the beginning
2148 * until we are up to where we want to be.
2150 * A side effect of what we do, just to keep things
2151 * simple, is to clear the urgent data pointer. The principal
2152 * caller should be setting the urgent data pointer AFTER calling
2160 char *thisitem, *next;
2162 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
2163 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
2167 while ((next = nextitem(thisitem)) <= netobuf.send) {
2171 /* Now, thisitem is first before/at boundary. */
2173 good = netobuf; /* where the good bytes go */
2175 while (netoring.add > thisitem) {
2176 if (wewant(thisitem)) {
2181 next = nextitem(next);
2182 } while (wewant(next) && (nfrontp > next));
2183 length = next-thisitem;
2184 memmove(good, thisitem, length);
2188 thisitem = nextitem(thisitem);
2196 * These routines add various telnet commands to the data stream.
2206 ttyflush(1); /* Flush/drop output */
2207 /* do printoption AFTER flush, otherwise the output gets tossed... */
2208 printoption("SENT", DO, TELOPT_TM);
2215 printoption("SENT", IAC, AO);
2226 printoption("SENT", IAC, EL);
2233 printoption("SENT", IAC, EC);
2240 netclear(); /* clear the path to the network */
2244 printoption("SENT", IAC, DM);
2248 int want_status_response = 0;
2253 unsigned char tmp[16];
2256 if (my_want_state_is_dont(TELOPT_STATUS)) {
2257 printf("Remote side does not support STATUS option\n");
2264 *cp++ = TELOPT_STATUS;
2265 *cp++ = TELQUAL_SEND;
2268 if (NETROOM() >= cp - tmp) {
2269 ring_supply_data(&netoring, tmp, cp-tmp);
2270 printsub('>', tmp+2, cp - tmp - 2);
2272 ++want_status_response;
2280 printoption("SENT", IAC, IP);
2293 NET2ADD(IAC, BREAK);
2294 printoption("SENT", IAC, BREAK);
2307 NET2ADD(IAC, ABORT);
2308 printoption("SENT", IAC, ABORT);
2322 printoption("SENT", IAC, SUSP);
2336 printoption("SENT", IAC, xEOF);
2343 printoption("SENT", IAC, AYT);
2347 * Send a window size update to the remote system.
2354 unsigned char tmp[16];
2357 if (my_state_is_wont(TELOPT_NAWS))
2361 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2362 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2364 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2372 *cp++ = TELOPT_NAWS;
2377 if (NETROOM() >= cp - tmp) {
2378 ring_supply_data(&netoring, tmp, cp-tmp);
2379 printsub('>', tmp+2, cp - tmp - 2);
2384 tel_enter_binary(int rw)
2387 send_do(TELOPT_BINARY, 1);
2389 send_will(TELOPT_BINARY, 1);
2393 tel_leave_binary(int rw)
2396 send_dont(TELOPT_BINARY, 1);
2398 send_wont(TELOPT_BINARY, 1);