2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char sccsid[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
44 /* By the way, we need to include curses.h before telnet.h since,
45 * among other things, telnet.h #defines 'DO', which is a variable
46 * declared in curses.h.
55 #include <arpa/inet.h>
56 #include <arpa/telnet.h>
66 #include <libtelnet/auth.h>
69 #include <libtelnet/encrypt.h>
71 #include <libtelnet/misc.h>
73 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
75 static unsigned char subbuffer[SUBBUFSIZE],
76 *subpointer, *subend; /* buffer for sub-options */
77 #define SB_CLEAR() subpointer = subbuffer;
78 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
79 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
80 *subpointer++ = (c); \
83 #define SB_GET() ((*subpointer++)&0xff)
84 #define SB_PEEK() ((*subpointer)&0xff)
85 #define SB_EOF() (subpointer >= subend)
86 #define SB_LEN() (subend - subpointer)
88 char options[256]; /* The combined options */
89 char do_dont_resp[256];
90 char will_wont_resp[256];
94 autologin = 0, /* Autologin anyone? */
98 ISend, /* trying to send network data in */
101 netdata, /* Print out network data flow */
102 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
104 SYNCHing, /* we are in TELNET SYNCH mode */
105 flushout, /* flush output */
106 autoflush = 0, /* flush output when interrupting? */
107 autosynch, /* send interrupt characters with SYNCH? */
108 localflow, /* we handle flow control locally */
109 restartany, /* if flow control enabled, restart on any character */
110 localchars, /* we recognize interrupt/quit */
111 donelclchars, /* the user has set "localchars" */
112 donebinarytoggle, /* the user has put us in binary */
113 dontlecho, /* do we suppress local echoing right now? */
115 doaddrlookup = 1, /* do a reverse address lookup? */
120 char *line; /* hack around breakage in sra.c :-( !! */
125 #ifdef KLUDGELINEMODE
130 * Telnet receiver states for fsm
139 #define TS_SB 7 /* sub-option collection */
140 #define TS_SE 8 /* looking for sub-option end */
142 static int telrcv_state;
144 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
146 # define telopt_environ TELOPT_NEW_ENVIRON
154 #ifdef KLUDGELINEMODE
155 int kludgelinemode = 1;
158 static int is_unique(char *, char **, char **);
161 * The following are some clocks used to decide how to interpret
162 * the relationship between various variables.
168 * Initialize telnet environment.
179 connected = ISend = localflow = donebinarytoggle = 0;
180 #ifdef AUTHENTICATION
182 auth_encrypt_connect(connected);
189 /* Don't change NetTrace */
191 escape = CONTROL(']');
192 rlogin = _POSIX_VDISABLE;
193 #ifdef KLUDGELINEMODE
194 echoc = CONTROL('E');
198 telrcv_state = TS_DATA;
203 * These routines are in charge of sending option negotiations
206 * The basic idea is that we send the negotiation if either side
207 * is in disagreement as to what the current state should be.
210 unsigned char ComPortBaudRate[256];
213 DoBaudRate(char *arg)
215 char *temp, temp2[10];
220 baudrate = (uint32_t)strtol(arg, &temp, 10);
221 if (temp[0] != '\0' || (baudrate == 0 && errno != 0))
222 ExitString("Invalid baud rate provided.\n", 1);
224 for (i = 1; termspeeds[i].speed != -1; i++)
225 if (baudrate == termspeeds[i].speed)
227 if (termspeeds[i].speed == -1)
228 ExitString("Invalid baud rate provided.\n", 1);
230 strlcpy(ComPortBaudRate, arg, sizeof(ComPortBaudRate));
232 if (NETROOM() < sizeof(temp2)) {
233 ExitString("No room in buffer for baud rate.\n", 1);
237 snprintf(temp2, sizeof(temp2), "%c%c%c%c....%c%c", IAC, SB, TELOPT_COMPORT,
238 COMPORT_SET_BAUDRATE, IAC, SE);
240 baudrate = htonl(baudrate);
241 memcpy(&temp2[4], &baudrate, sizeof(baudrate));
242 ring_supply_data(&netoring, temp2, sizeof(temp2));
243 printsub('>', &temp[2], sizeof(temp2) - 2);
247 send_do(int c, int init)
250 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
251 my_want_state_is_do(c))
253 set_my_want_state_do(c);
260 printoption("SENT", DO, c);
264 send_dont(int c, int init)
267 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
268 my_want_state_is_dont(c))
270 set_my_want_state_dont(c);
277 printoption("SENT", DONT, c);
281 send_will(int c, int init)
284 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
285 my_want_state_is_will(c))
287 set_my_want_state_will(c);
294 printoption("SENT", WILL, c);
298 send_wont(int c, int init)
301 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
302 my_want_state_is_wont(c))
304 set_my_want_state_wont(c);
311 printoption("SENT", WONT, c);
315 willoption(int option)
317 int new_state_ok = 0;
319 if (do_dont_resp[option]) {
320 --do_dont_resp[option];
321 if (do_dont_resp[option] && my_state_is_do(option))
322 --do_dont_resp[option];
325 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
332 settimer(modenegotiated);
335 #ifdef AUTHENTICATION
336 case TELOPT_AUTHENTICATION:
340 #endif /* ENCRYPTION */
348 * Special case for TM. If we get back a WILL,
349 * pretend we got back a WONT.
351 set_my_want_state_dont(option);
352 set_my_state_dont(option);
353 return; /* Never reply to TM will's/wont's */
355 case TELOPT_LINEMODE:
361 set_my_want_state_do(option);
363 setconnmode(0); /* possibly set new tty mode */
365 do_dont_resp[option]++;
366 send_dont(option, 0);
369 set_my_state_do(option);
371 if (option == TELOPT_ENCRYPT)
372 encrypt_send_support();
373 #endif /* ENCRYPTION */
377 wontoption(int option)
379 if (do_dont_resp[option]) {
380 --do_dont_resp[option];
381 if (do_dont_resp[option] && my_state_is_dont(option))
382 --do_dont_resp[option];
385 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
389 #ifdef KLUDGELINEMODE
396 settimer(modenegotiated);
402 set_my_want_state_dont(option);
403 set_my_state_dont(option);
404 return; /* Never reply to TM will's/wont's */
409 set_my_want_state_dont(option);
410 if (my_state_is_do(option))
411 send_dont(option, 0);
412 setconnmode(0); /* Set new tty mode */
413 } else if (option == TELOPT_TM) {
415 * Special case for TM.
419 set_my_want_state_dont(option);
421 set_my_state_dont(option);
427 int new_state_ok = 0;
429 if (will_wont_resp[option]) {
430 --will_wont_resp[option];
431 if (will_wont_resp[option] && my_state_is_will(option))
432 --will_wont_resp[option];
435 if (will_wont_resp[option] == 0) {
436 if (my_want_state_is_wont(option)) {
442 * Special case for TM. We send a WILL, but pretend
445 send_will(option, 0);
446 set_my_want_state_wont(TELOPT_TM);
447 set_my_state_wont(TELOPT_TM);
450 case TELOPT_BINARY: /* binary mode */
451 case TELOPT_NAWS: /* window size */
452 case TELOPT_TSPEED: /* terminal speed */
453 case TELOPT_LFLOW: /* local flow control */
454 case TELOPT_TTYPE: /* terminal type option */
455 case TELOPT_SGA: /* no big deal */
457 case TELOPT_ENCRYPT: /* encryption variable option */
458 #endif /* ENCRYPTION */
462 case TELOPT_NEW_ENVIRON: /* New environment variable option */
464 if (my_state_is_will(TELOPT_OLD_ENVIRON))
465 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
467 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
468 if (my_state_is_will(TELOPT_NEW_ENVIRON))
469 break; /* Don't enable if new one is in use! */
471 telopt_environ = option;
476 #ifdef AUTHENTICATION
477 case TELOPT_AUTHENTICATION:
483 case TELOPT_XDISPLOC: /* X Display location */
484 if (env_getvalue("DISPLAY"))
488 case TELOPT_LINEMODE:
489 #ifdef KLUDGELINEMODE
491 send_do(TELOPT_SGA, 1);
493 set_my_want_state_will(TELOPT_LINEMODE);
494 send_will(option, 0);
495 set_my_state_will(TELOPT_LINEMODE);
499 case TELOPT_ECHO: /* We're never going to echo... */
505 set_my_want_state_will(option);
506 send_will(option, 0);
507 setconnmode(0); /* Set new tty mode */
509 will_wont_resp[option]++;
510 send_wont(option, 0);
514 * Handle options that need more things done after the
515 * other side has acknowledged the option.
518 case TELOPT_LINEMODE:
519 #ifdef KLUDGELINEMODE
521 send_do(TELOPT_SGA, 1);
523 set_my_state_will(option);
525 send_do(TELOPT_SGA, 0);
530 set_my_state_will(option);
534 dontoption(int option)
537 if (will_wont_resp[option]) {
538 --will_wont_resp[option];
539 if (will_wont_resp[option] && my_state_is_wont(option))
540 --will_wont_resp[option];
543 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
545 case TELOPT_LINEMODE:
546 linemode = 0; /* put us back to the default state */
549 case TELOPT_NEW_ENVIRON:
551 * The new environ option wasn't recognized, try
554 send_will(TELOPT_OLD_ENVIRON, 1);
555 telopt_environ = TELOPT_OLD_ENVIRON;
559 /* we always accept a DONT */
560 set_my_want_state_wont(option);
561 if (my_state_is_will(option))
562 send_wont(option, 0);
563 setconnmode(0); /* Set new tty mode */
565 set_my_state_wont(option);
569 * Given a buffer returned by tgetent(), this routine will turn
570 * the pipe separated list of names in the buffer into an array
571 * of pointers to null terminated names. We toss out any bad,
572 * duplicate, or verbose names (names with spaces).
575 static const char *name_unknown = "UNKNOWN";
576 static const char *unknown[] = { NULL, NULL };
579 mklist(char *buf, char *name)
582 char c, *cp, **argvp, *cp2, **argv, **avt;
585 if (strlen(name) > 40) {
587 unknown[0] = name_unknown;
593 unknown[0] = name_unknown;
595 * Count up the number of names.
597 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
602 * Allocate an array to put the name pointers into
604 argv = (char **)malloc((n+3)*sizeof(char *));
609 * Fill up the array of pointers to names.
614 for (cp = cp2 = buf; (c = *cp); cp++) {
615 if (c == '|' || c == ':') {
618 * Skip entries that have spaces or are over 40
619 * characters long. If this is our environment
620 * name, then put it up front. Otherwise, as
621 * long as this is not a duplicate name (case
622 * insensitive) add it to the list.
624 if (n || (cp - cp2 > 41))
626 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
628 else if (is_unique(cp2, argv+1, argvp))
633 * Skip multiple delimiters. Reset cp2 to
634 * the beginning of the next name. Reset n,
635 * the flag for names with spaces.
637 while ((c = *cp) == '|')
643 * Skip entries with spaces or non-ascii values.
644 * Convert lower case letters to upper case.
646 if ((c == ' ') || !isascii(c))
653 * Check for an old V6 2 character name. If the second
654 * name points to the beginning of the buffer, and is
655 * only 2 characters long, move it to the end of the array.
657 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
659 for (avt = &argv[1]; avt < argvp; avt++)
665 * Duplicate last name, for TTYPE option, and null
666 * terminate the array. If we didn't find a match on
667 * our terminal name, put that name at the beginning.
678 for (avt = argv; avt < argvp; avt++)
683 return((const char **)argv);
689 is_unique(char *name, char **as, char **ae)
694 n = strlen(name) + 1;
695 for (ap = as; ap < ae; ap++)
696 if (strncasecmp(*ap, name, n) == 0)
706 setupterm(char *tname, int fd, int *errp)
708 if (tgetent(termbuf, tname) == 1) {
709 termbuf[1023] = '\0';
719 #define termbuf ttytype
720 extern char ttytype[];
723 int resettermname = 1;
729 static const char **tnamep = 0;
730 static const char **next;
735 if (tnamep && tnamep != unknown)
737 if ((tname = env_getvalue("TERM")) &&
738 (setupterm(tname, 1, &err) == 0)) {
739 tnamep = mklist(termbuf, tname);
741 if (tname && (strlen(tname) <= 40)) {
745 unknown[0] = name_unknown;
757 * Look at the sub-option buffer, and try to be helpful to the other
760 * Currently we recognize:
762 * Terminal type, send request.
763 * Terminal speed (send request).
764 * Local flow control (is request).
771 unsigned char subchar;
773 printsub('<', subbuffer, SB_LEN()+2);
774 switch (subchar = SB_GET()) {
776 if (my_want_state_is_wont(TELOPT_TTYPE))
778 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
782 unsigned char temp[50];
785 name = gettermname();
786 len = strlen(name) + 4 + 2;
787 if (len < NETROOM()) {
788 sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
789 TELQUAL_IS, name, IAC, SE);
790 ring_supply_data(&netoring, temp, len);
791 printsub('>', &temp[2], len-2);
793 ExitString("No room in buffer for terminal type.\n", 1);
799 if (my_want_state_is_wont(TELOPT_TSPEED))
803 if (SB_GET() == TELQUAL_SEND) {
805 unsigned char temp[50];
808 TerminalSpeeds(&ispeed, &ospeed);
810 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
811 TELQUAL_IS, ospeed, ispeed, IAC, SE);
812 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
814 if (len < NETROOM()) {
815 ring_supply_data(&netoring, temp, len);
816 printsub('>', temp+2, len - 2);
818 /*@*/ else printf("lm_will: not enough room in buffer\n");
822 if (my_want_state_is_wont(TELOPT_LFLOW))
827 case LFLOW_RESTART_ANY:
830 case LFLOW_RESTART_XON:
846 case TELOPT_LINEMODE:
847 if (my_want_state_is_wont(TELOPT_LINEMODE))
853 lm_will(subpointer, SB_LEN());
856 lm_wont(subpointer, SB_LEN());
859 lm_do(subpointer, SB_LEN());
862 lm_dont(subpointer, SB_LEN());
865 slc(subpointer, SB_LEN());
868 lm_mode(subpointer, SB_LEN(), 0);
876 case TELOPT_OLD_ENVIRON:
878 case TELOPT_NEW_ENVIRON:
884 if (my_want_state_is_dont(subchar))
888 if (my_want_state_is_wont(subchar)) {
895 env_opt(subpointer, SB_LEN());
898 case TELOPT_XDISPLOC:
899 if (my_want_state_is_wont(TELOPT_XDISPLOC))
903 if (SB_GET() == TELQUAL_SEND) {
904 unsigned char temp[50], *dp;
907 if ((dp = env_getvalue("DISPLAY")) == NULL ||
908 strlen(dp) > sizeof(temp) - 7) {
910 * Something happened, we no longer have a DISPLAY
911 * variable. Or it is too long. So, turn off the option.
913 send_wont(TELOPT_XDISPLOC, 1);
916 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
917 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
918 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
920 if (len < NETROOM()) {
921 ring_supply_data(&netoring, temp, len);
922 printsub('>', temp+2, len - 2);
924 /*@*/ else printf("lm_will: not enough room in buffer\n");
928 #ifdef AUTHENTICATION
929 case TELOPT_AUTHENTICATION: {
936 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
938 auth_is(subpointer, SB_LEN());
941 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
943 auth_send(subpointer, SB_LEN());
946 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
948 auth_reply(subpointer, SB_LEN());
951 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
953 auth_name(subpointer, SB_LEN());
965 if (my_want_state_is_dont(TELOPT_ENCRYPT))
967 encrypt_start(subpointer, SB_LEN());
970 if (my_want_state_is_dont(TELOPT_ENCRYPT))
974 case ENCRYPT_SUPPORT:
975 if (my_want_state_is_wont(TELOPT_ENCRYPT))
977 encrypt_support(subpointer, SB_LEN());
979 case ENCRYPT_REQSTART:
980 if (my_want_state_is_wont(TELOPT_ENCRYPT))
982 encrypt_request_start(subpointer, SB_LEN());
985 if (my_want_state_is_wont(TELOPT_ENCRYPT))
988 * We can always send an REQEND so that we cannot
989 * get stuck encrypting. We should only get this
990 * if we have been able to get in the correct mode
993 encrypt_request_end();
996 if (my_want_state_is_dont(TELOPT_ENCRYPT))
998 encrypt_is(subpointer, SB_LEN());
1001 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1003 encrypt_reply(subpointer, SB_LEN());
1005 case ENCRYPT_ENC_KEYID:
1006 if (my_want_state_is_dont(TELOPT_ENCRYPT))
1008 encrypt_enc_keyid(subpointer, SB_LEN());
1010 case ENCRYPT_DEC_KEYID:
1011 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1013 encrypt_dec_keyid(subpointer, SB_LEN());
1019 #endif /* ENCRYPTION */
1025 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
1028 lm_will(unsigned char *cmd, int len)
1031 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
1035 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1039 if (NETROOM() > (int)sizeof(str_lm)) {
1040 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1041 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1043 /*@*/ else printf("lm_will: not enough room in buffer\n");
1049 lm_wont(unsigned char *cmd, int len)
1052 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1056 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1058 /* We are always DONT, so don't respond */
1064 lm_do(unsigned char *cmd, int len)
1067 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1071 case LM_FORWARDMASK:
1075 if (NETROOM() > (int)sizeof(str_lm)) {
1076 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1077 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1079 /*@*/ else printf("lm_do: not enough room in buffer\n");
1085 lm_dont(unsigned char *cmd, int len)
1088 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1092 case LM_FORWARDMASK:
1094 /* we are always WONT, so don't respond */
1099 static unsigned char str_lm_mode[] = {
1100 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1104 lm_mode(unsigned char *cmd, int len, int init)
1108 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1112 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1113 str_lm_mode[4] = linemode;
1115 str_lm_mode[4] |= MODE_ACK;
1116 if (NETROOM() > (int)sizeof(str_lm_mode)) {
1117 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1118 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1120 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1121 setconnmode(0); /* set changed mode */
1128 * Handle special character suboption of LINEMODE.
1134 char flags; /* Current flags & level */
1135 char mylevel; /* Maximum level & flags */
1138 #define SLC_IMPORT 0
1139 #define SLC_EXPORT 1
1140 #define SLC_RVALUE 2
1141 static int slc_mode = SLC_EXPORT;
1149 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1152 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1155 #define initfunc(func, flags) { \
1156 spcp = &spc_data[func]; \
1157 if ((spcp->valp = tcval(func))) { \
1158 spcp->val = *spcp->valp; \
1159 spcp->mylevel = SLC_VARIABLE|flags; \
1162 spcp->mylevel = SLC_DEFAULT; \
1166 initfunc(SLC_SYNCH, 0);
1168 initfunc(SLC_AO, 0);
1169 initfunc(SLC_AYT, 0);
1171 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1172 initfunc(SLC_EOF, 0);
1174 initfunc(SLC_SUSP, SLC_FLUSHIN);
1176 initfunc(SLC_EC, 0);
1177 initfunc(SLC_EL, 0);
1179 initfunc(SLC_EW, 0);
1180 initfunc(SLC_RP, 0);
1181 initfunc(SLC_LNEXT, 0);
1183 initfunc(SLC_XON, 0);
1184 initfunc(SLC_XOFF, 0);
1186 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1187 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1189 initfunc(SLC_FORW1, 0);
1191 initfunc(SLC_FORW2, 0);
1195 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1198 if (slc_mode == SLC_EXPORT)
1208 printf("Special characters are %s values\n",
1209 slc_mode == SLC_IMPORT ? "remote default" :
1210 slc_mode == SLC_EXPORT ? "local" :
1215 slc_mode_export(void)
1217 slc_mode = SLC_EXPORT;
1218 if (my_state_is_will(TELOPT_LINEMODE))
1223 slc_mode_import(int def)
1225 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1226 if (my_state_is_will(TELOPT_LINEMODE))
1230 unsigned char slc_import_val[] = {
1231 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1233 unsigned char slc_import_def[] = {
1234 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1240 if (NETROOM() > (int)sizeof(slc_import_val)) {
1242 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1243 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1245 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1246 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1249 /*@*/ else printf("slc_import: not enough room\n");
1257 TerminalDefaultChars();
1260 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1261 if (spcp->mylevel != SLC_NOSUPPORT) {
1262 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1263 spcp->flags = SLC_NOSUPPORT;
1265 spcp->flags = spcp->mylevel;
1267 spcp->val = *spcp->valp;
1268 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1273 setconnmode(1); /* Make sure the character values are set */
1277 slc(unsigned char *cp, int len)
1284 for (; len >= 3; len -=3, cp +=3) {
1286 func = cp[SLC_FUNC];
1290 * Client side: always ignore 0 function.
1295 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1296 slc_add_reply(func, SLC_NOSUPPORT, 0);
1300 spcp = &spc_data[func];
1302 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1304 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1305 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1309 if (level == (SLC_DEFAULT|SLC_ACK)) {
1311 * This is an error condition, the SLC_ACK
1312 * bit should never be set for the SLC_DEFAULT
1313 * level. Our best guess to recover is to
1314 * ignore the SLC_ACK bit.
1316 cp[SLC_FLAGS] &= ~SLC_ACK;
1319 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1320 spcp->val = (cc_t)cp[SLC_VALUE];
1321 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1327 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1328 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1329 spcp->val = (cc_t)cp[SLC_VALUE];
1331 if (level == SLC_DEFAULT) {
1332 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1333 spcp->flags = spcp->mylevel;
1335 spcp->flags = SLC_NOSUPPORT;
1337 slc_add_reply(func, spcp->flags, spcp->val);
1341 setconnmode(1); /* set the new character values */
1350 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1351 if (spcp->valp && spcp->val != *spcp->valp) {
1352 spcp->val = *spcp->valp;
1353 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1354 spcp->flags = SLC_NOSUPPORT;
1356 spcp->flags = spcp->mylevel;
1357 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1364 unsigned char slc_reply[128];
1365 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1366 unsigned char *slc_replyp;
1369 slc_start_reply(void)
1371 slc_replyp = slc_reply;
1372 *slc_replyp++ = IAC;
1374 *slc_replyp++ = TELOPT_LINEMODE;
1375 *slc_replyp++ = LM_SLC;
1379 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1381 /* A sequence of up to 6 bytes my be written for this member of the SLC
1382 * suboption list by this function. The end of negotiation command,
1383 * which is written by slc_end_reply(), will require 2 additional
1384 * bytes. Do not proceed unless there is sufficient space for these
1387 if (&slc_replyp[6+2] > slc_reply_eom)
1389 if ((*slc_replyp++ = func) == IAC)
1390 *slc_replyp++ = IAC;
1391 if ((*slc_replyp++ = flags) == IAC)
1392 *slc_replyp++ = IAC;
1393 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1394 *slc_replyp++ = IAC;
1402 /* The end of negotiation command requires 2 bytes. */
1403 if (&slc_replyp[2] > slc_reply_eom)
1405 *slc_replyp++ = IAC;
1407 len = slc_replyp - slc_reply;
1410 if (NETROOM() > len) {
1411 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1412 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1414 /*@*/else printf("slc_end_reply: not enough room\n");
1421 int need_update = 0;
1423 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1424 if (!(spcp->flags&SLC_ACK))
1426 spcp->flags &= ~SLC_ACK;
1427 if (spcp->valp && (*spcp->valp != spcp->val)) {
1428 *spcp->valp = spcp->val;
1432 return(need_update);
1438 * Earlier version of telnet/telnetd from the BSD code had
1439 * the definitions of VALUE and VAR reversed. To ensure
1440 * maximum interoperability, we assume that the server is
1441 * an older BSD server, until proven otherwise. The newer
1442 * BSD servers should be able to handle either definition,
1443 * so it is better to use the wrong values if we don't
1444 * know what type of server it is.
1447 int old_env_var = OLD_ENV_VAR;
1448 int old_env_value = OLD_ENV_VALUE;
1450 # define old_env_var OLD_ENV_VAR
1451 # define old_env_value OLD_ENV_VALUE
1456 env_opt(unsigned char *buf, int len)
1458 unsigned char *ep = 0, *epc = 0;
1461 switch(buf[0]&0xff) {
1466 } else for (i = 1; i < len; i++) {
1467 switch (buf[i]&0xff) {
1471 if (telopt_environ == TELOPT_OLD_ENVIRON
1473 /* Server has the same definitions */
1474 old_env_var = OLD_ENV_VAR;
1475 old_env_value = OLD_ENV_VALUE;
1481 * Although OLD_ENV_VALUE is not legal, we will
1482 * still recognize it, just in case it is an
1483 * old server that has VAR & VALUE mixed up...
1494 ep = epc = &buf[i+1];
1514 /* Ignore for now. We shouldn't get it anyway. */
1522 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1523 unsigned char *opt_reply = NULL;
1524 unsigned char *opt_replyp;
1525 unsigned char *opt_replyend;
1531 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1533 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1534 if (opt_reply == NULL) {
1535 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1536 opt_reply = opt_replyp = opt_replyend = NULL;
1539 opt_replyp = opt_reply;
1540 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1541 *opt_replyp++ = IAC;
1543 *opt_replyp++ = telopt_environ;
1544 *opt_replyp++ = TELQUAL_IS;
1548 env_opt_start_info(void)
1552 opt_replyp[-1] = TELQUAL_INFO;
1556 env_opt_add(unsigned char *ep)
1558 unsigned char *vp, c;
1560 if (opt_reply == NULL) /*XXX*/
1563 if (ep == NULL || *ep == '\0') {
1564 /* Send user defined variables first. */
1566 while ((ep = env_default(0, 0)))
1569 /* Now add the list of well know variables. */
1571 while ((ep = env_default(0, 1)))
1575 vp = env_getvalue(ep);
1576 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1577 2 * strlen((char *)ep) + 6 > opt_replyend)
1580 opt_replyend += OPT_REPLY_SIZE;
1581 len = opt_replyend - opt_reply;
1582 opt_reply = (unsigned char *)realloc(opt_reply, len);
1583 if (opt_reply == NULL) {
1584 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1585 opt_reply = opt_replyp = opt_replyend = NULL;
1588 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1589 opt_replyend = opt_reply + len;
1591 if (opt_welldefined(ep))
1593 if (telopt_environ == TELOPT_OLD_ENVIRON)
1594 *opt_replyp++ = old_env_var;
1597 *opt_replyp++ = NEW_ENV_VAR;
1599 *opt_replyp++ = ENV_USERVAR;
1601 while ((c = *ep++)) {
1602 if (opt_replyp + (2 + 2) > opt_replyend)
1606 *opt_replyp++ = IAC;
1612 *opt_replyp++ = ENV_ESC;
1618 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1621 if (telopt_environ == TELOPT_OLD_ENVIRON)
1622 *opt_replyp++ = old_env_value;
1625 *opt_replyp++ = NEW_ENV_VALUE;
1633 opt_welldefined(const char *ep)
1635 if ((strcmp(ep, "USER") == 0) ||
1636 (strcmp(ep, "DISPLAY") == 0) ||
1637 (strcmp(ep, "PRINTER") == 0) ||
1638 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1639 (strcmp(ep, "JOB") == 0) ||
1640 (strcmp(ep, "ACCT") == 0))
1646 env_opt_end(int emptyok)
1650 if (opt_replyp + 2 > opt_replyend)
1652 len = opt_replyp + 2 - opt_reply;
1653 if (emptyok || len > 6) {
1654 *opt_replyp++ = IAC;
1656 if (NETROOM() > len) {
1657 ring_supply_data(&netoring, opt_reply, len);
1658 printsub('>', &opt_reply[2], len - 2);
1660 /*@*/ else printf("slc_end_reply: not enough room\n");
1664 opt_reply = opt_replyp = opt_replyend = NULL;
1677 int returnValue = 0;
1681 while (TTYROOM() > 2) {
1684 ring_consumed(&netiring, count);
1688 sbp = netiring.consume;
1689 scc = ring_full_consecutive(&netiring);
1691 /* No more data coming in */
1696 c = *sbp++ & 0xff, scc--; count++;
1699 c = (*decrypt_input)(c);
1700 #endif /* ENCRYPTION */
1702 switch (telrcv_state) {
1705 telrcv_state = TS_DATA;
1707 break; /* Ignore \0 after CR */
1709 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1716 if (c == IAC && telnetport >= 0) {
1717 telrcv_state = TS_IAC;
1721 * The 'crmod' hack (see following) is needed
1722 * since we can't * set CRMOD on output only.
1723 * Machines like MULTICS like to send \r without
1724 * \n; since we must turn off CRMOD to get proper
1725 * input, the mapping is done here (sigh).
1727 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1732 c = (*decrypt_input)(c);
1733 #endif /* ENCRYPTION */
1735 sbp++, scc--; count++;
1738 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1740 sbp++, scc--; count++;
1745 (*decrypt_input)(-1);
1746 #endif /* ENCRYPTION */
1754 telrcv_state = TS_CR;
1770 telrcv_state = TS_WILL;
1774 telrcv_state = TS_WONT;
1778 telrcv_state = TS_DO;
1782 telrcv_state = TS_DONT;
1787 * We may have missed an urgent notification,
1788 * so make sure we flush whatever is in the
1791 printoption("RCVD", IAC, DM);
1794 SYNCHing = stilloob();
1800 telrcv_state = TS_SB;
1810 printoption("RCVD", IAC, c);
1813 telrcv_state = TS_DATA;
1817 printoption("RCVD", WILL, c);
1819 telrcv_state = TS_DATA;
1823 printoption("RCVD", WONT, c);
1825 telrcv_state = TS_DATA;
1829 printoption("RCVD", DO, c);
1831 if (c == TELOPT_NAWS) {
1833 } else if (c == TELOPT_LFLOW) {
1838 telrcv_state = TS_DATA;
1842 printoption("RCVD", DONT, c);
1845 setconnmode(0); /* set new tty mode (maybe) */
1846 telrcv_state = TS_DATA;
1851 telrcv_state = TS_SE;
1861 * This is an error. We only expect to get
1862 * "IAC IAC" or "IAC SE". Several things may
1863 * have happend. An IAC was not doubled, the
1864 * IAC SE was left off, or another option got
1865 * inserted into the suboption are all possibilities.
1866 * If we assume that the IAC was not doubled,
1867 * and really the IAC SE was left off, we could
1868 * get into an infinate loop here. So, instead,
1869 * we terminate the suboption, and process the
1870 * partial suboption if we can.
1877 printoption("In SUBOPTION processing, RCVD", IAC, c);
1878 suboption(); /* handle sub-option */
1879 telrcv_state = TS_IAC;
1883 telrcv_state = TS_SB;
1889 suboption(); /* handle sub-option */
1890 telrcv_state = TS_DATA;
1895 ring_consumed(&netiring, count);
1896 return returnValue||count;
1899 static int bol = 1, local = 0;
1907 command(0, "z\n", 2);
1918 int returnValue = 0;
1923 while (NETROOM() > 2) {
1929 ring_consumed(&ttyiring, count);
1933 tbp = ttyiring.consume;
1934 tcc = ring_full_consecutive(&ttyiring);
1939 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1940 if (rlogin != _POSIX_VDISABLE) {
1949 if (sc == '.' || c == termEofChar) {
1951 command(0, "close\n", 6);
1954 if (sc == termSuspChar) {
1956 command(0, "z\n", 2);
1960 command(0, tbp, tcc);
1974 if ((sc == '\n') || (sc == '\r'))
1976 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1978 * Double escape is a pass through of a single escape character.
1980 if (tcc && strip(*tbp) == escape) {
1986 command(0, (char *)tbp, tcc);
1995 #ifdef KLUDGELINEMODE
1996 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1997 if (tcc > 0 && strip(*tbp) == echoc) {
1998 tcc--; tbp++; count++;
2000 dontlecho = !dontlecho;
2001 settimer(echotoggle);
2008 if (MODE_LOCAL_CHARS(globalmode)) {
2009 if (TerminalSpecialChars(sc) == 0) {
2014 if (my_want_state_is_wont(TELOPT_BINARY)) {
2018 * If we are in CRMOD mode (\r ==> \n)
2019 * on our local machine, then probably
2020 * a newline (unix) is CRLF (TELNET).
2022 if (MODE_LOCAL_CHARS(globalmode)) {
2026 bol = flushline = 1;
2030 NET2ADD('\r', '\0');
2032 NET2ADD('\r', '\n');
2034 bol = flushline = 1;
2043 } else if (c == IAC) {
2050 ring_consumed(&ttyiring, count);
2051 return returnValue||count; /* Non-zero if we did anything */
2057 * Try to do something.
2059 * If we do something useful, return 1; else return 0.
2064 Scheduler(int block)
2066 /* One wants to be a bit careful about setting returnValue
2067 * to one, since a one implies we did some useful work,
2068 * and therefore probably won't be called to block next
2071 int netin, netout, netex, ttyin, ttyout;
2073 /* Decide which rings should be processed */
2075 netout = ring_full_count(&netoring) &&
2077 (my_want_state_is_wont(TELOPT_LINEMODE)
2078 #ifdef KLUDGELINEMODE
2079 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2082 my_want_state_is_will(TELOPT_BINARY));
2083 ttyout = ring_full_count(&ttyoring);
2085 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2087 netin = !ISend && ring_empty_count(&netiring);
2091 /* Call to system code to process rings */
2093 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2095 /* Now, look at the input rings, looking for work to do. */
2097 if (ring_full_count(&ttyiring)) {
2098 returnValue |= telsnd();
2101 if (ring_full_count(&netiring)) {
2102 returnValue |= telrcv();
2107 #ifdef AUTHENTICATION
2108 #define __unusedhere
2110 #define __unusedhere __unused
2113 * Select from tty and network...
2116 telnet(char *user __unusedhere)
2120 #ifdef AUTHENTICATION
2123 static char local_host[256] = { 0 };
2125 if (!local_host[0]) {
2126 gethostname(local_host, sizeof(local_host));
2127 local_host[sizeof(local_host)-1] = 0;
2129 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2130 auth_encrypt_user(user);
2134 if (telnetport > 0) {
2135 #ifdef AUTHENTICATION
2137 send_will(TELOPT_AUTHENTICATION, 1);
2140 send_do(TELOPT_ENCRYPT, 1);
2141 send_will(TELOPT_ENCRYPT, 1);
2142 #endif /* ENCRYPTION */
2143 send_do(TELOPT_SGA, 1);
2144 send_will(TELOPT_TTYPE, 1);
2145 send_will(TELOPT_NAWS, 1);
2146 send_will(TELOPT_TSPEED, 1);
2147 send_will(TELOPT_LFLOW, 1);
2148 send_will(TELOPT_LINEMODE, 1);
2149 send_will(TELOPT_NEW_ENVIRON, 1);
2150 send_do(TELOPT_STATUS, 1);
2151 if (env_getvalue("DISPLAY"))
2152 send_will(TELOPT_XDISPLOC, 1);
2154 tel_enter_binary(eight);
2160 while ((schedValue = Scheduler(0)) != 0) {
2161 if (schedValue == -1) {
2167 if (Scheduler(1) == -1) {
2174 #if 0 /* XXX - this not being in is a bug */
2178 * Return the address of the next "item" in the TELNET data
2179 * stream. This will be the address of the next character if
2180 * the current address is a user data character, or it will
2181 * be the address of the character following the TELNET command
2182 * if the current address is a TELNET IAC ("I Am a Command")
2187 nextitem(char *current)
2189 if ((*current&0xff) != IAC) {
2192 switch (*(current+1)&0xff) {
2198 case SB: /* loop forever looking for the SE */
2200 char *look = current+2;
2203 if ((*look++&0xff) == IAC) {
2204 if ((*look++&0xff) == SE) {
2219 * We are about to do a TELNET SYNCH operation. Clear
2220 * the path to the network.
2222 * Things are a bit tricky since we may have sent the first
2223 * byte or so of a previous TELNET command into the network.
2224 * So, we have to scan the network buffer from the beginning
2225 * until we are up to where we want to be.
2227 * A side effect of what we do, just to keep things
2228 * simple, is to clear the urgent data pointer. The principal
2229 * caller should be setting the urgent data pointer AFTER calling
2240 * These routines add various telnet commands to the data stream.
2250 (void) ttyflush(1); /* Flush/drop output */
2251 /* do printoption AFTER flush, otherwise the output gets tossed... */
2252 printoption("SENT", DO, TELOPT_TM);
2259 printoption("SENT", IAC, AO);
2269 printoption("SENT", IAC, EL);
2276 printoption("SENT", IAC, EC);
2280 dosynch(char *ch __unused)
2282 netclear(); /* clear the path to the network */
2286 printoption("SENT", IAC, DM);
2290 int want_status_response = 0;
2293 get_status(char *ch __unused)
2295 unsigned char tmp[16];
2298 if (my_want_state_is_dont(TELOPT_STATUS)) {
2299 printf("Remote side does not support STATUS option\n");
2306 *cp++ = TELOPT_STATUS;
2307 *cp++ = TELQUAL_SEND;
2310 if (NETROOM() >= cp - tmp) {
2311 ring_supply_data(&netoring, tmp, cp-tmp);
2312 printsub('>', tmp+2, cp - tmp - 2);
2314 ++want_status_response;
2322 printoption("SENT", IAC, IP);
2335 NET2ADD(IAC, BREAK);
2336 printoption("SENT", IAC, BREAK);
2349 NET2ADD(IAC, ABORT);
2350 printoption("SENT", IAC, ABORT);
2364 printoption("SENT", IAC, SUSP);
2378 printoption("SENT", IAC, xEOF);
2385 printoption("SENT", IAC, AYT);
2389 * Send a window size update to the remote system.
2396 unsigned char tmp[16];
2399 if (my_state_is_wont(TELOPT_NAWS))
2402 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2403 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2405 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2413 *cp++ = TELOPT_NAWS;
2418 if (NETROOM() >= cp - tmp) {
2419 ring_supply_data(&netoring, tmp, cp-tmp);
2420 printsub('>', tmp+2, cp - tmp - 2);
2425 tel_enter_binary(int rw)
2428 send_do(TELOPT_BINARY, 1);
2430 send_will(TELOPT_BINARY, 1);
2434 tel_leave_binary(int rw)
2437 send_dont(TELOPT_BINARY, 1);
2439 send_wont(TELOPT_BINARY, 1);