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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char sccsid[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
40 /* By the way, we need to include curses.h before telnet.h since,
41 * among other things, telnet.h #defines 'DO', which is a variable
42 * declared in curses.h.
51 #include <arpa/inet.h>
52 #include <arpa/telnet.h>
62 #include <libtelnet/auth.h>
65 #include <libtelnet/encrypt.h>
67 #include <libtelnet/misc.h>
69 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
71 static unsigned char subbuffer[SUBBUFSIZE],
72 *subpointer, *subend; /* buffer for sub-options */
73 #define SB_CLEAR() subpointer = subbuffer;
74 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
75 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
76 *subpointer++ = (c); \
79 #define SB_GET() ((*subpointer++)&0xff)
80 #define SB_PEEK() ((*subpointer)&0xff)
81 #define SB_EOF() (subpointer >= subend)
82 #define SB_LEN() (subend - subpointer)
84 char options[256]; /* The combined options */
85 char do_dont_resp[256];
86 char will_wont_resp[256];
90 autologin = 0, /* Autologin anyone? */
94 ISend, /* trying to send network data in */
97 netdata, /* Print out network data flow */
98 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
100 SYNCHing, /* we are in TELNET SYNCH mode */
101 flushout, /* flush output */
102 autoflush = 0, /* flush output when interrupting? */
103 autosynch, /* send interrupt characters with SYNCH? */
104 localflow, /* we handle flow control locally */
105 restartany, /* if flow control enabled, restart on any character */
106 localchars, /* we recognize interrupt/quit */
107 donelclchars, /* the user has set "localchars" */
108 donebinarytoggle, /* the user has put us in binary */
109 dontlecho, /* do we suppress local echoing right now? */
111 doaddrlookup = 1, /* do a reverse address lookup? */
116 char *line; /* hack around breakage in sra.c :-( !! */
121 #ifdef KLUDGELINEMODE
126 * Telnet receiver states for fsm
135 #define TS_SB 7 /* sub-option collection */
136 #define TS_SE 8 /* looking for sub-option end */
138 static int telrcv_state;
140 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
142 # define telopt_environ TELOPT_NEW_ENVIRON
150 #ifdef KLUDGELINEMODE
151 int kludgelinemode = 1;
154 static int is_unique(char *, char **, char **);
157 * The following are some clocks used to decide how to interpret
158 * the relationship between various variables.
164 * Initialize telnet environment.
175 connected = ISend = localflow = donebinarytoggle = 0;
176 #ifdef AUTHENTICATION
178 auth_encrypt_connect(connected);
185 /* Don't change NetTrace */
187 escape = CONTROL(']');
188 rlogin = _POSIX_VDISABLE;
189 #ifdef KLUDGELINEMODE
190 echoc = CONTROL('E');
194 telrcv_state = TS_DATA;
199 * These routines are in charge of sending option negotiations
202 * The basic idea is that we send the negotiation if either side
203 * is in disagreement as to what the current state should be.
206 unsigned char ComPortBaudRate[256];
209 DoBaudRate(char *arg)
211 char *temp, temp2[10];
216 baudrate = (uint32_t)strtol(arg, &temp, 10);
217 if (temp[0] != '\0' || (baudrate == 0 && errno != 0))
218 ExitString("Invalid baud rate provided.\n", 1);
220 for (i = 1; termspeeds[i].speed != -1; i++)
221 if (baudrate == termspeeds[i].speed)
223 if (termspeeds[i].speed == -1)
224 ExitString("Invalid baud rate provided.\n", 1);
226 strlcpy(ComPortBaudRate, arg, sizeof(ComPortBaudRate));
228 if (NETROOM() < sizeof(temp2)) {
229 ExitString("No room in buffer for baud rate.\n", 1);
233 snprintf(temp2, sizeof(temp2), "%c%c%c%c....%c%c", IAC, SB, TELOPT_COMPORT,
234 COMPORT_SET_BAUDRATE, IAC, SE);
236 baudrate = htonl(baudrate);
237 memcpy(&temp2[4], &baudrate, sizeof(baudrate));
238 ring_supply_data(&netoring, temp2, sizeof(temp2));
239 printsub('>', &temp[2], sizeof(temp2) - 2);
243 send_do(int c, int init)
246 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
247 my_want_state_is_do(c))
249 set_my_want_state_do(c);
256 printoption("SENT", DO, c);
260 send_dont(int c, int init)
263 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
264 my_want_state_is_dont(c))
266 set_my_want_state_dont(c);
273 printoption("SENT", DONT, c);
277 send_will(int c, int init)
280 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
281 my_want_state_is_will(c))
283 set_my_want_state_will(c);
290 printoption("SENT", WILL, c);
294 send_wont(int c, int init)
297 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
298 my_want_state_is_wont(c))
300 set_my_want_state_wont(c);
307 printoption("SENT", WONT, c);
311 willoption(int option)
313 int new_state_ok = 0;
315 if (do_dont_resp[option]) {
316 --do_dont_resp[option];
317 if (do_dont_resp[option] && my_state_is_do(option))
318 --do_dont_resp[option];
321 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
328 settimer(modenegotiated);
331 #ifdef AUTHENTICATION
332 case TELOPT_AUTHENTICATION:
336 #endif /* ENCRYPTION */
344 * Special case for TM. If we get back a WILL,
345 * pretend we got back a WONT.
347 set_my_want_state_dont(option);
348 set_my_state_dont(option);
349 return; /* Never reply to TM will's/wont's */
351 case TELOPT_LINEMODE:
357 set_my_want_state_do(option);
359 setconnmode(0); /* possibly set new tty mode */
361 do_dont_resp[option]++;
362 send_dont(option, 0);
365 set_my_state_do(option);
367 if (option == TELOPT_ENCRYPT)
368 encrypt_send_support();
369 #endif /* ENCRYPTION */
373 wontoption(int option)
375 if (do_dont_resp[option]) {
376 --do_dont_resp[option];
377 if (do_dont_resp[option] && my_state_is_dont(option))
378 --do_dont_resp[option];
381 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
385 #ifdef KLUDGELINEMODE
392 settimer(modenegotiated);
398 set_my_want_state_dont(option);
399 set_my_state_dont(option);
400 return; /* Never reply to TM will's/wont's */
405 set_my_want_state_dont(option);
406 if (my_state_is_do(option))
407 send_dont(option, 0);
408 setconnmode(0); /* Set new tty mode */
409 } else if (option == TELOPT_TM) {
411 * Special case for TM.
415 set_my_want_state_dont(option);
417 set_my_state_dont(option);
423 int new_state_ok = 0;
425 if (will_wont_resp[option]) {
426 --will_wont_resp[option];
427 if (will_wont_resp[option] && my_state_is_will(option))
428 --will_wont_resp[option];
431 if (will_wont_resp[option] == 0) {
432 if (my_want_state_is_wont(option)) {
438 * Special case for TM. We send a WILL, but pretend
441 send_will(option, 0);
442 set_my_want_state_wont(TELOPT_TM);
443 set_my_state_wont(TELOPT_TM);
446 case TELOPT_BINARY: /* binary mode */
447 case TELOPT_NAWS: /* window size */
448 case TELOPT_TSPEED: /* terminal speed */
449 case TELOPT_LFLOW: /* local flow control */
450 case TELOPT_TTYPE: /* terminal type option */
451 case TELOPT_SGA: /* no big deal */
453 case TELOPT_ENCRYPT: /* encryption variable option */
454 #endif /* ENCRYPTION */
458 case TELOPT_NEW_ENVIRON: /* New environment variable option */
460 if (my_state_is_will(TELOPT_OLD_ENVIRON))
461 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
463 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
464 if (my_state_is_will(TELOPT_NEW_ENVIRON))
465 break; /* Don't enable if new one is in use! */
467 telopt_environ = option;
472 #ifdef AUTHENTICATION
473 case TELOPT_AUTHENTICATION:
479 case TELOPT_XDISPLOC: /* X Display location */
480 if (env_getvalue("DISPLAY"))
484 case TELOPT_LINEMODE:
485 #ifdef KLUDGELINEMODE
487 send_do(TELOPT_SGA, 1);
489 set_my_want_state_will(TELOPT_LINEMODE);
490 send_will(option, 0);
491 set_my_state_will(TELOPT_LINEMODE);
495 case TELOPT_ECHO: /* We're never going to echo... */
501 set_my_want_state_will(option);
502 send_will(option, 0);
503 setconnmode(0); /* Set new tty mode */
505 will_wont_resp[option]++;
506 send_wont(option, 0);
510 * Handle options that need more things done after the
511 * other side has acknowledged the option.
514 case TELOPT_LINEMODE:
515 #ifdef KLUDGELINEMODE
517 send_do(TELOPT_SGA, 1);
519 set_my_state_will(option);
521 send_do(TELOPT_SGA, 0);
526 set_my_state_will(option);
530 dontoption(int option)
533 if (will_wont_resp[option]) {
534 --will_wont_resp[option];
535 if (will_wont_resp[option] && my_state_is_wont(option))
536 --will_wont_resp[option];
539 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
541 case TELOPT_LINEMODE:
542 linemode = 0; /* put us back to the default state */
545 case TELOPT_NEW_ENVIRON:
547 * The new environ option wasn't recognized, try
550 send_will(TELOPT_OLD_ENVIRON, 1);
551 telopt_environ = TELOPT_OLD_ENVIRON;
555 /* we always accept a DONT */
556 set_my_want_state_wont(option);
557 if (my_state_is_will(option))
558 send_wont(option, 0);
559 setconnmode(0); /* Set new tty mode */
561 set_my_state_wont(option);
565 * Given a buffer returned by tgetent(), this routine will turn
566 * the pipe separated list of names in the buffer into an array
567 * of pointers to null terminated names. We toss out any bad,
568 * duplicate, or verbose names (names with spaces).
571 static const char *name_unknown = "UNKNOWN";
572 static const char *unknown[] = { NULL, NULL };
575 mklist(char *buf, char *name)
578 char c, *cp, **argvp, *cp2, **argv, **avt;
581 if (strlen(name) > 40) {
583 unknown[0] = name_unknown;
589 unknown[0] = name_unknown;
591 * Count up the number of names.
593 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
598 * Allocate an array to put the name pointers into
600 argv = (char **)malloc((n+3)*sizeof(char *));
605 * Fill up the array of pointers to names.
610 for (cp = cp2 = buf; (c = *cp); cp++) {
611 if (c == '|' || c == ':') {
614 * Skip entries that have spaces or are over 40
615 * characters long. If this is our environment
616 * name, then put it up front. Otherwise, as
617 * long as this is not a duplicate name (case
618 * insensitive) add it to the list.
620 if (n || (cp - cp2 > 41))
622 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
624 else if (is_unique(cp2, argv+1, argvp))
629 * Skip multiple delimiters. Reset cp2 to
630 * the beginning of the next name. Reset n,
631 * the flag for names with spaces.
633 while ((c = *cp) == '|')
639 * Skip entries with spaces or non-ascii values.
640 * Convert lower case letters to upper case.
642 if ((c == ' ') || !isascii(c))
649 * Check for an old V6 2 character name. If the second
650 * name points to the beginning of the buffer, and is
651 * only 2 characters long, move it to the end of the array.
653 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
655 for (avt = &argv[1]; avt < argvp; avt++)
661 * Duplicate last name, for TTYPE option, and null
662 * terminate the array. If we didn't find a match on
663 * our terminal name, put that name at the beginning.
674 for (avt = argv; avt < argvp; avt++)
679 return((const char **)argv);
685 is_unique(char *name, char **as, char **ae)
690 n = strlen(name) + 1;
691 for (ap = as; ap < ae; ap++)
692 if (strncasecmp(*ap, name, n) == 0)
702 setupterm(char *tname, int fd, int *errp)
704 if (tgetent(termbuf, tname) == 1) {
705 termbuf[1023] = '\0';
715 #define termbuf ttytype
716 extern char ttytype[];
719 int resettermname = 1;
725 static const char **tnamep = 0;
726 static const char **next;
731 if (tnamep && tnamep != unknown)
733 if ((tname = env_getvalue("TERM")) &&
734 (setupterm(tname, 1, &err) == 0)) {
735 tnamep = mklist(termbuf, tname);
737 if (tname && (strlen(tname) <= 40)) {
741 unknown[0] = name_unknown;
753 * Look at the sub-option buffer, and try to be helpful to the other
756 * Currently we recognize:
758 * Terminal type, send request.
759 * Terminal speed (send request).
760 * Local flow control (is request).
767 unsigned char subchar;
769 printsub('<', subbuffer, SB_LEN()+2);
770 switch (subchar = SB_GET()) {
772 if (my_want_state_is_wont(TELOPT_TTYPE))
774 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
778 unsigned char temp[50];
781 name = gettermname();
782 len = strlen(name) + 4 + 2;
783 if (len < NETROOM()) {
784 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
785 TELQUAL_IS, name, IAC, SE);
786 ring_supply_data(&netoring, temp, len);
787 printsub('>', &temp[2], len-2);
789 ExitString("No room in buffer for terminal type.\n", 1);
795 if (my_want_state_is_wont(TELOPT_TSPEED))
799 if (SB_GET() == TELQUAL_SEND) {
801 unsigned char temp[50];
804 TerminalSpeeds(&ispeed, &ospeed);
806 snprintf((char *)temp, sizeof(temp), "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
807 TELQUAL_IS, ospeed, ispeed, IAC, SE);
808 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
810 if (len < NETROOM()) {
811 ring_supply_data(&netoring, temp, len);
812 printsub('>', temp+2, len - 2);
814 /*@*/ else printf("lm_will: not enough room in buffer\n");
818 if (my_want_state_is_wont(TELOPT_LFLOW))
823 case LFLOW_RESTART_ANY:
826 case LFLOW_RESTART_XON:
842 case TELOPT_LINEMODE:
843 if (my_want_state_is_wont(TELOPT_LINEMODE))
849 lm_will(subpointer, SB_LEN());
852 lm_wont(subpointer, SB_LEN());
855 lm_do(subpointer, SB_LEN());
858 lm_dont(subpointer, SB_LEN());
861 slc(subpointer, SB_LEN());
864 lm_mode(subpointer, SB_LEN(), 0);
872 case TELOPT_OLD_ENVIRON:
874 case TELOPT_NEW_ENVIRON:
880 if (my_want_state_is_dont(subchar))
884 if (my_want_state_is_wont(subchar)) {
891 env_opt(subpointer, SB_LEN());
894 case TELOPT_XDISPLOC:
895 if (my_want_state_is_wont(TELOPT_XDISPLOC))
899 if (SB_GET() == TELQUAL_SEND) {
900 unsigned char temp[50], *dp;
903 if ((dp = env_getvalue("DISPLAY")) == NULL ||
904 strlen(dp) > sizeof(temp) - 7) {
906 * Something happened, we no longer have a DISPLAY
907 * variable. Or it is too long. So, turn off the option.
909 send_wont(TELOPT_XDISPLOC, 1);
912 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
913 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
914 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
916 if (len < NETROOM()) {
917 ring_supply_data(&netoring, temp, len);
918 printsub('>', temp+2, len - 2);
920 /*@*/ else printf("lm_will: not enough room in buffer\n");
924 #ifdef AUTHENTICATION
925 case TELOPT_AUTHENTICATION: {
932 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
934 auth_is(subpointer, SB_LEN());
937 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
939 auth_send(subpointer, SB_LEN());
942 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
944 auth_reply(subpointer, SB_LEN());
947 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
949 auth_name(subpointer, SB_LEN());
961 if (my_want_state_is_dont(TELOPT_ENCRYPT))
963 encrypt_start(subpointer, SB_LEN());
966 if (my_want_state_is_dont(TELOPT_ENCRYPT))
970 case ENCRYPT_SUPPORT:
971 if (my_want_state_is_wont(TELOPT_ENCRYPT))
973 encrypt_support(subpointer, SB_LEN());
975 case ENCRYPT_REQSTART:
976 if (my_want_state_is_wont(TELOPT_ENCRYPT))
978 encrypt_request_start(subpointer, SB_LEN());
981 if (my_want_state_is_wont(TELOPT_ENCRYPT))
984 * We can always send an REQEND so that we cannot
985 * get stuck encrypting. We should only get this
986 * if we have been able to get in the correct mode
989 encrypt_request_end();
992 if (my_want_state_is_dont(TELOPT_ENCRYPT))
994 encrypt_is(subpointer, SB_LEN());
997 if (my_want_state_is_wont(TELOPT_ENCRYPT))
999 encrypt_reply(subpointer, SB_LEN());
1001 case ENCRYPT_ENC_KEYID:
1002 if (my_want_state_is_dont(TELOPT_ENCRYPT))
1004 encrypt_enc_keyid(subpointer, SB_LEN());
1006 case ENCRYPT_DEC_KEYID:
1007 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1009 encrypt_dec_keyid(subpointer, SB_LEN());
1015 #endif /* ENCRYPTION */
1021 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
1024 lm_will(unsigned char *cmd, int len)
1027 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
1031 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1035 if (NETROOM() > (int)sizeof(str_lm)) {
1036 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1037 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1039 /*@*/ else printf("lm_will: not enough room in buffer\n");
1045 lm_wont(unsigned char *cmd, int len)
1048 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1052 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1054 /* We are always DONT, so don't respond */
1060 lm_do(unsigned char *cmd, int len)
1063 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1067 case LM_FORWARDMASK:
1071 if (NETROOM() > (int)sizeof(str_lm)) {
1072 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1073 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1075 /*@*/ else printf("lm_do: not enough room in buffer\n");
1081 lm_dont(unsigned char *cmd, int len)
1084 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1088 case LM_FORWARDMASK:
1090 /* we are always WONT, so don't respond */
1095 static unsigned char str_lm_mode[] = {
1096 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1100 lm_mode(unsigned char *cmd, int len, int init)
1104 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1108 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1109 str_lm_mode[4] = linemode;
1111 str_lm_mode[4] |= MODE_ACK;
1112 if (NETROOM() > (int)sizeof(str_lm_mode)) {
1113 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1114 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1116 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1117 setconnmode(0); /* set changed mode */
1124 * Handle special character suboption of LINEMODE.
1130 char flags; /* Current flags & level */
1131 char mylevel; /* Maximum level & flags */
1134 #define SLC_IMPORT 0
1135 #define SLC_EXPORT 1
1136 #define SLC_RVALUE 2
1137 static int slc_mode = SLC_EXPORT;
1145 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1148 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1151 #define initfunc(func, flags) { \
1152 spcp = &spc_data[func]; \
1153 if ((spcp->valp = tcval(func))) { \
1154 spcp->val = *spcp->valp; \
1155 spcp->mylevel = SLC_VARIABLE|flags; \
1158 spcp->mylevel = SLC_DEFAULT; \
1162 initfunc(SLC_SYNCH, 0);
1164 initfunc(SLC_AO, 0);
1165 initfunc(SLC_AYT, 0);
1167 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1168 initfunc(SLC_EOF, 0);
1170 initfunc(SLC_SUSP, SLC_FLUSHIN);
1172 initfunc(SLC_EC, 0);
1173 initfunc(SLC_EL, 0);
1175 initfunc(SLC_EW, 0);
1176 initfunc(SLC_RP, 0);
1177 initfunc(SLC_LNEXT, 0);
1179 initfunc(SLC_XON, 0);
1180 initfunc(SLC_XOFF, 0);
1182 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1183 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1185 initfunc(SLC_FORW1, 0);
1187 initfunc(SLC_FORW2, 0);
1191 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1194 if (slc_mode == SLC_EXPORT)
1204 printf("Special characters are %s values\n",
1205 slc_mode == SLC_IMPORT ? "remote default" :
1206 slc_mode == SLC_EXPORT ? "local" :
1211 slc_mode_export(void)
1213 slc_mode = SLC_EXPORT;
1214 if (my_state_is_will(TELOPT_LINEMODE))
1219 slc_mode_import(int def)
1221 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1222 if (my_state_is_will(TELOPT_LINEMODE))
1226 unsigned char slc_import_val[] = {
1227 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1229 unsigned char slc_import_def[] = {
1230 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1236 if (NETROOM() > (int)sizeof(slc_import_val)) {
1238 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1239 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1241 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1242 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1245 /*@*/ else printf("slc_import: not enough room\n");
1253 TerminalDefaultChars();
1256 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1257 if (spcp->mylevel != SLC_NOSUPPORT) {
1258 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1259 spcp->flags = SLC_NOSUPPORT;
1261 spcp->flags = spcp->mylevel;
1263 spcp->val = *spcp->valp;
1264 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1269 setconnmode(1); /* Make sure the character values are set */
1273 slc(unsigned char *cp, int len)
1280 for (; len >= 3; len -=3, cp +=3) {
1282 func = cp[SLC_FUNC];
1286 * Client side: always ignore 0 function.
1291 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1292 slc_add_reply(func, SLC_NOSUPPORT, 0);
1296 spcp = &spc_data[func];
1298 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1300 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1301 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1305 if (level == (SLC_DEFAULT|SLC_ACK)) {
1307 * This is an error condition, the SLC_ACK
1308 * bit should never be set for the SLC_DEFAULT
1309 * level. Our best guess to recover is to
1310 * ignore the SLC_ACK bit.
1312 cp[SLC_FLAGS] &= ~SLC_ACK;
1315 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1316 spcp->val = (cc_t)cp[SLC_VALUE];
1317 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1323 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1324 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1325 spcp->val = (cc_t)cp[SLC_VALUE];
1327 if (level == SLC_DEFAULT) {
1328 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1329 spcp->flags = spcp->mylevel;
1331 spcp->flags = SLC_NOSUPPORT;
1333 slc_add_reply(func, spcp->flags, spcp->val);
1337 setconnmode(1); /* set the new character values */
1346 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1347 if (spcp->valp && spcp->val != *spcp->valp) {
1348 spcp->val = *spcp->valp;
1349 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1350 spcp->flags = SLC_NOSUPPORT;
1352 spcp->flags = spcp->mylevel;
1353 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1360 unsigned char slc_reply[128];
1361 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1362 unsigned char *slc_replyp;
1365 slc_start_reply(void)
1367 slc_replyp = slc_reply;
1368 *slc_replyp++ = IAC;
1370 *slc_replyp++ = TELOPT_LINEMODE;
1371 *slc_replyp++ = LM_SLC;
1375 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1377 /* A sequence of up to 6 bytes my be written for this member of the SLC
1378 * suboption list by this function. The end of negotiation command,
1379 * which is written by slc_end_reply(), will require 2 additional
1380 * bytes. Do not proceed unless there is sufficient space for these
1383 if (&slc_replyp[6+2] > slc_reply_eom)
1385 if ((*slc_replyp++ = func) == IAC)
1386 *slc_replyp++ = IAC;
1387 if ((*slc_replyp++ = flags) == IAC)
1388 *slc_replyp++ = IAC;
1389 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1390 *slc_replyp++ = IAC;
1398 /* The end of negotiation command requires 2 bytes. */
1399 if (&slc_replyp[2] > slc_reply_eom)
1401 *slc_replyp++ = IAC;
1403 len = slc_replyp - slc_reply;
1406 if (NETROOM() > len) {
1407 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1408 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1410 /*@*/else printf("slc_end_reply: not enough room\n");
1417 int need_update = 0;
1419 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1420 if (!(spcp->flags&SLC_ACK))
1422 spcp->flags &= ~SLC_ACK;
1423 if (spcp->valp && (*spcp->valp != spcp->val)) {
1424 *spcp->valp = spcp->val;
1428 return(need_update);
1434 * Earlier version of telnet/telnetd from the BSD code had
1435 * the definitions of VALUE and VAR reversed. To ensure
1436 * maximum interoperability, we assume that the server is
1437 * an older BSD server, until proven otherwise. The newer
1438 * BSD servers should be able to handle either definition,
1439 * so it is better to use the wrong values if we don't
1440 * know what type of server it is.
1443 int old_env_var = OLD_ENV_VAR;
1444 int old_env_value = OLD_ENV_VALUE;
1446 # define old_env_var OLD_ENV_VAR
1447 # define old_env_value OLD_ENV_VALUE
1452 env_opt(unsigned char *buf, int len)
1454 unsigned char *ep = 0, *epc = 0;
1457 switch(buf[0]&0xff) {
1462 } else for (i = 1; i < len; i++) {
1463 switch (buf[i]&0xff) {
1467 if (telopt_environ == TELOPT_OLD_ENVIRON
1469 /* Server has the same definitions */
1470 old_env_var = OLD_ENV_VAR;
1471 old_env_value = OLD_ENV_VALUE;
1477 * Although OLD_ENV_VALUE is not legal, we will
1478 * still recognize it, just in case it is an
1479 * old server that has VAR & VALUE mixed up...
1490 ep = epc = &buf[i+1];
1510 /* Ignore for now. We shouldn't get it anyway. */
1518 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1519 unsigned char *opt_reply = NULL;
1520 unsigned char *opt_replyp;
1521 unsigned char *opt_replyend;
1527 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1529 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1530 if (opt_reply == NULL) {
1531 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1532 opt_reply = opt_replyp = opt_replyend = NULL;
1535 opt_replyp = opt_reply;
1536 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1537 *opt_replyp++ = IAC;
1539 *opt_replyp++ = telopt_environ;
1540 *opt_replyp++ = TELQUAL_IS;
1544 env_opt_start_info(void)
1548 opt_replyp[-1] = TELQUAL_INFO;
1552 env_opt_add(unsigned char *ep)
1554 unsigned char *vp, c;
1556 if (opt_reply == NULL) /*XXX*/
1559 if (ep == NULL || *ep == '\0') {
1560 /* Send user defined variables first. */
1562 while ((ep = env_default(0, 0)))
1565 /* Now add the list of well know variables. */
1567 while ((ep = env_default(0, 1)))
1571 vp = env_getvalue(ep);
1572 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1573 2 * strlen((char *)ep) + 6 > opt_replyend)
1576 opt_replyend += OPT_REPLY_SIZE;
1577 len = opt_replyend - opt_reply;
1578 opt_reply = (unsigned char *)realloc(opt_reply, len);
1579 if (opt_reply == NULL) {
1580 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1581 opt_reply = opt_replyp = opt_replyend = NULL;
1584 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1585 opt_replyend = opt_reply + len;
1587 if (opt_welldefined(ep))
1589 if (telopt_environ == TELOPT_OLD_ENVIRON)
1590 *opt_replyp++ = old_env_var;
1593 *opt_replyp++ = NEW_ENV_VAR;
1595 *opt_replyp++ = ENV_USERVAR;
1597 while ((c = *ep++)) {
1598 if (opt_replyp + (2 + 2) > opt_replyend)
1602 *opt_replyp++ = IAC;
1608 *opt_replyp++ = ENV_ESC;
1614 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1617 if (telopt_environ == TELOPT_OLD_ENVIRON)
1618 *opt_replyp++ = old_env_value;
1621 *opt_replyp++ = NEW_ENV_VALUE;
1629 opt_welldefined(const char *ep)
1631 if ((strcmp(ep, "USER") == 0) ||
1632 (strcmp(ep, "DISPLAY") == 0) ||
1633 (strcmp(ep, "PRINTER") == 0) ||
1634 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1635 (strcmp(ep, "JOB") == 0) ||
1636 (strcmp(ep, "ACCT") == 0))
1642 env_opt_end(int emptyok)
1646 if (opt_replyp + 2 > opt_replyend)
1648 len = opt_replyp + 2 - opt_reply;
1649 if (emptyok || len > 6) {
1650 *opt_replyp++ = IAC;
1652 if (NETROOM() > len) {
1653 ring_supply_data(&netoring, opt_reply, len);
1654 printsub('>', &opt_reply[2], len - 2);
1656 /*@*/ else printf("slc_end_reply: not enough room\n");
1660 opt_reply = opt_replyp = opt_replyend = NULL;
1673 int returnValue = 0;
1677 while (TTYROOM() > 2) {
1680 ring_consumed(&netiring, count);
1684 sbp = netiring.consume;
1685 scc = ring_full_consecutive(&netiring);
1687 /* No more data coming in */
1692 c = *sbp++ & 0xff, scc--; count++;
1695 c = (*decrypt_input)(c);
1696 #endif /* ENCRYPTION */
1698 switch (telrcv_state) {
1701 telrcv_state = TS_DATA;
1703 break; /* Ignore \0 after CR */
1705 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1712 if (c == IAC && telnetport >= 0) {
1713 telrcv_state = TS_IAC;
1717 * The 'crmod' hack (see following) is needed
1718 * since we can't * set CRMOD on output only.
1719 * Machines like MULTICS like to send \r without
1720 * \n; since we must turn off CRMOD to get proper
1721 * input, the mapping is done here (sigh).
1723 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1728 c = (*decrypt_input)(c);
1729 #endif /* ENCRYPTION */
1731 sbp++, scc--; count++;
1734 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1736 sbp++, scc--; count++;
1741 (*decrypt_input)(-1);
1742 #endif /* ENCRYPTION */
1750 telrcv_state = TS_CR;
1766 telrcv_state = TS_WILL;
1770 telrcv_state = TS_WONT;
1774 telrcv_state = TS_DO;
1778 telrcv_state = TS_DONT;
1783 * We may have missed an urgent notification,
1784 * so make sure we flush whatever is in the
1787 printoption("RCVD", IAC, DM);
1790 SYNCHing = stilloob();
1796 telrcv_state = TS_SB;
1806 printoption("RCVD", IAC, c);
1809 telrcv_state = TS_DATA;
1813 printoption("RCVD", WILL, c);
1815 telrcv_state = TS_DATA;
1819 printoption("RCVD", WONT, c);
1821 telrcv_state = TS_DATA;
1825 printoption("RCVD", DO, c);
1827 if (c == TELOPT_NAWS) {
1829 } else if (c == TELOPT_LFLOW) {
1834 telrcv_state = TS_DATA;
1838 printoption("RCVD", DONT, c);
1841 setconnmode(0); /* set new tty mode (maybe) */
1842 telrcv_state = TS_DATA;
1847 telrcv_state = TS_SE;
1857 * This is an error. We only expect to get
1858 * "IAC IAC" or "IAC SE". Several things may
1859 * have happend. An IAC was not doubled, the
1860 * IAC SE was left off, or another option got
1861 * inserted into the suboption are all possibilities.
1862 * If we assume that the IAC was not doubled,
1863 * and really the IAC SE was left off, we could
1864 * get into an infinate loop here. So, instead,
1865 * we terminate the suboption, and process the
1866 * partial suboption if we can.
1873 printoption("In SUBOPTION processing, RCVD", IAC, c);
1874 suboption(); /* handle sub-option */
1875 telrcv_state = TS_IAC;
1879 telrcv_state = TS_SB;
1885 suboption(); /* handle sub-option */
1886 telrcv_state = TS_DATA;
1891 ring_consumed(&netiring, count);
1892 return returnValue||count;
1895 static int bol = 1, local = 0;
1903 command(0, "z\n", 2);
1914 int returnValue = 0;
1919 while (NETROOM() > 2) {
1925 ring_consumed(&ttyiring, count);
1929 tbp = ttyiring.consume;
1930 tcc = ring_full_consecutive(&ttyiring);
1935 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1936 if (rlogin != _POSIX_VDISABLE) {
1945 if (sc == '.' || c == termEofChar) {
1947 command(0, "close\n", 6);
1950 if (sc == termSuspChar) {
1952 command(0, "z\n", 2);
1956 command(0, tbp, tcc);
1970 if ((sc == '\n') || (sc == '\r'))
1972 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1974 * Double escape is a pass through of a single escape character.
1976 if (tcc && strip(*tbp) == escape) {
1982 command(0, (char *)tbp, tcc);
1991 #ifdef KLUDGELINEMODE
1992 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1993 if (tcc > 0 && strip(*tbp) == echoc) {
1994 tcc--; tbp++; count++;
1996 dontlecho = !dontlecho;
1997 settimer(echotoggle);
2004 if (MODE_LOCAL_CHARS(globalmode)) {
2005 if (TerminalSpecialChars(sc) == 0) {
2010 if (my_want_state_is_wont(TELOPT_BINARY)) {
2014 * If we are in CRMOD mode (\r ==> \n)
2015 * on our local machine, then probably
2016 * a newline (unix) is CRLF (TELNET).
2018 if (MODE_LOCAL_CHARS(globalmode)) {
2022 bol = flushline = 1;
2026 NET2ADD('\r', '\0');
2028 NET2ADD('\r', '\n');
2030 bol = flushline = 1;
2039 } else if (c == IAC) {
2046 ring_consumed(&ttyiring, count);
2047 return returnValue||count; /* Non-zero if we did anything */
2053 * Try to do something.
2055 * If we do something useful, return 1; else return 0.
2060 Scheduler(int block)
2062 /* One wants to be a bit careful about setting returnValue
2063 * to one, since a one implies we did some useful work,
2064 * and therefore probably won't be called to block next
2067 int netin, netout, netex, ttyin, ttyout;
2069 /* Decide which rings should be processed */
2071 netout = ring_full_count(&netoring) &&
2073 (my_want_state_is_wont(TELOPT_LINEMODE)
2074 #ifdef KLUDGELINEMODE
2075 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2078 my_want_state_is_will(TELOPT_BINARY));
2079 ttyout = ring_full_count(&ttyoring);
2081 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2083 netin = !ISend && ring_empty_count(&netiring);
2087 /* Call to system code to process rings */
2089 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2091 /* Now, look at the input rings, looking for work to do. */
2093 if (ring_full_count(&ttyiring)) {
2094 returnValue |= telsnd();
2097 if (ring_full_count(&netiring)) {
2098 returnValue |= telrcv();
2103 #ifdef AUTHENTICATION
2104 #define __unusedhere
2106 #define __unusedhere __unused
2109 * Select from tty and network...
2112 telnet(char *user __unusedhere)
2116 #ifdef AUTHENTICATION
2119 static char local_host[256] = { 0 };
2121 if (!local_host[0]) {
2122 gethostname(local_host, sizeof(local_host));
2123 local_host[sizeof(local_host)-1] = 0;
2125 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2126 auth_encrypt_user(user);
2130 if (telnetport > 0) {
2131 #ifdef AUTHENTICATION
2133 send_will(TELOPT_AUTHENTICATION, 1);
2136 send_do(TELOPT_ENCRYPT, 1);
2137 send_will(TELOPT_ENCRYPT, 1);
2138 #endif /* ENCRYPTION */
2139 send_do(TELOPT_SGA, 1);
2140 send_will(TELOPT_TTYPE, 1);
2141 send_will(TELOPT_NAWS, 1);
2142 send_will(TELOPT_TSPEED, 1);
2143 send_will(TELOPT_LFLOW, 1);
2144 send_will(TELOPT_LINEMODE, 1);
2145 send_will(TELOPT_NEW_ENVIRON, 1);
2146 send_do(TELOPT_STATUS, 1);
2147 if (env_getvalue("DISPLAY"))
2148 send_will(TELOPT_XDISPLOC, 1);
2150 tel_enter_binary(eight);
2156 while ((schedValue = Scheduler(0)) != 0) {
2157 if (schedValue == -1) {
2163 if (Scheduler(1) == -1) {
2170 #if 0 /* XXX - this not being in is a bug */
2174 * Return the address of the next "item" in the TELNET data
2175 * stream. This will be the address of the next character if
2176 * the current address is a user data character, or it will
2177 * be the address of the character following the TELNET command
2178 * if the current address is a TELNET IAC ("I Am a Command")
2183 nextitem(char *current)
2185 if ((*current&0xff) != IAC) {
2188 switch (*(current+1)&0xff) {
2194 case SB: /* loop forever looking for the SE */
2196 char *look = current+2;
2199 if ((*look++&0xff) == IAC) {
2200 if ((*look++&0xff) == SE) {
2215 * We are about to do a TELNET SYNCH operation. Clear
2216 * the path to the network.
2218 * Things are a bit tricky since we may have sent the first
2219 * byte or so of a previous TELNET command into the network.
2220 * So, we have to scan the network buffer from the beginning
2221 * until we are up to where we want to be.
2223 * A side effect of what we do, just to keep things
2224 * simple, is to clear the urgent data pointer. The principal
2225 * caller should be setting the urgent data pointer AFTER calling
2236 * These routines add various telnet commands to the data stream.
2246 (void) ttyflush(1); /* Flush/drop output */
2247 /* do printoption AFTER flush, otherwise the output gets tossed... */
2248 printoption("SENT", DO, TELOPT_TM);
2255 printoption("SENT", IAC, AO);
2265 printoption("SENT", IAC, EL);
2272 printoption("SENT", IAC, EC);
2276 dosynch(char *ch __unused)
2278 netclear(); /* clear the path to the network */
2282 printoption("SENT", IAC, DM);
2286 int want_status_response = 0;
2289 get_status(char *ch __unused)
2291 unsigned char tmp[16];
2294 if (my_want_state_is_dont(TELOPT_STATUS)) {
2295 printf("Remote side does not support STATUS option\n");
2302 *cp++ = TELOPT_STATUS;
2303 *cp++ = TELQUAL_SEND;
2306 if (NETROOM() >= cp - tmp) {
2307 ring_supply_data(&netoring, tmp, cp-tmp);
2308 printsub('>', tmp+2, cp - tmp - 2);
2310 ++want_status_response;
2318 printoption("SENT", IAC, IP);
2331 NET2ADD(IAC, BREAK);
2332 printoption("SENT", IAC, BREAK);
2345 NET2ADD(IAC, ABORT);
2346 printoption("SENT", IAC, ABORT);
2360 printoption("SENT", IAC, SUSP);
2374 printoption("SENT", IAC, xEOF);
2381 printoption("SENT", IAC, AYT);
2385 * Send a window size update to the remote system.
2392 unsigned char tmp[16];
2395 if (my_state_is_wont(TELOPT_NAWS))
2398 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2399 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2401 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2409 *cp++ = TELOPT_NAWS;
2414 if (NETROOM() >= cp - tmp) {
2415 ring_supply_data(&netoring, tmp, cp-tmp);
2416 printsub('>', tmp+2, cp - tmp - 2);
2421 tel_enter_binary(int rw)
2424 send_do(TELOPT_BINARY, 1);
2426 send_will(TELOPT_BINARY, 1);
2430 tel_leave_binary(int rw)
2433 send_dont(TELOPT_BINARY, 1);
2435 send_wont(TELOPT_BINARY, 1);