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
33 * @(#)telnet.c 8.4 (Berkeley) 5/30/95
34 * $FreeBSD: src/crypto/telnet/telnet/telnet.c,v 1.4.2.5 2002/04/13 10:59:08 markm Exp $
35 * $DragonFly: src/crypto/telnet/telnet/telnet.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
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/telnet.h>
61 #include <libtelnet/auth.h>
64 #include <libtelnet/encrypt.h>
66 #include <libtelnet/misc.h>
68 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
70 static unsigned char subbuffer[SUBBUFSIZE],
71 *subpointer, *subend; /* buffer for sub-options */
72 #define SB_CLEAR() subpointer = subbuffer;
73 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
74 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
75 *subpointer++ = (c); \
78 #define SB_GET() ((*subpointer++)&0xff)
79 #define SB_PEEK() ((*subpointer)&0xff)
80 #define SB_EOF() (subpointer >= subend)
81 #define SB_LEN() (subend - subpointer)
83 char options[256]; /* The combined options */
84 char do_dont_resp[256];
85 char will_wont_resp[256];
89 autologin = 0, /* Autologin anyone? */
93 ISend, /* trying to send network data in */
96 netdata, /* Print out network data flow */
97 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
99 SYNCHing, /* we are in TELNET SYNCH mode */
100 flushout, /* flush output */
101 autoflush = 0, /* flush output when interrupting? */
102 autosynch, /* send interrupt characters with SYNCH? */
103 localflow, /* we handle flow control locally */
104 restartany, /* if flow control enabled, restart on any character */
105 localchars, /* we recognize interrupt/quit */
106 donelclchars, /* the user has set "localchars" */
107 donebinarytoggle, /* the user has put us in binary */
108 dontlecho, /* do we suppress local echoing right now? */
110 doaddrlookup = 1, /* do a reverse address lookup? */
115 char *line; /* hack around breakage in sra.c :-( !! */
120 #ifdef KLUDGELINEMODE
125 * Telnet receiver states for fsm
134 #define TS_SB 7 /* sub-option collection */
135 #define TS_SE 8 /* looking for sub-option end */
137 static int telrcv_state;
139 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
141 # 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.
207 send_do(int c, int init)
210 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
211 my_want_state_is_do(c))
213 set_my_want_state_do(c);
218 printoption("SENT", DO, c);
222 send_dont(int c, int init)
225 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
226 my_want_state_is_dont(c))
228 set_my_want_state_dont(c);
233 printoption("SENT", DONT, c);
237 send_will(int c, int init)
240 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
241 my_want_state_is_will(c))
243 set_my_want_state_will(c);
248 printoption("SENT", WILL, c);
252 send_wont(int c, int init)
255 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
256 my_want_state_is_wont(c))
258 set_my_want_state_wont(c);
263 printoption("SENT", WONT, c);
267 willoption(int option)
269 int new_state_ok = 0;
271 if (do_dont_resp[option]) {
272 --do_dont_resp[option];
273 if (do_dont_resp[option] && my_state_is_do(option))
274 --do_dont_resp[option];
277 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
284 settimer(modenegotiated);
287 #ifdef AUTHENTICATION
288 case TELOPT_AUTHENTICATION:
292 #endif /* ENCRYPTION */
300 * Special case for TM. If we get back a WILL,
301 * pretend we got back a WONT.
303 set_my_want_state_dont(option);
304 set_my_state_dont(option);
305 return; /* Never reply to TM will's/wont's */
307 case TELOPT_LINEMODE:
313 set_my_want_state_do(option);
315 setconnmode(0); /* possibly set new tty mode */
317 do_dont_resp[option]++;
318 send_dont(option, 0);
321 set_my_state_do(option);
323 if (option == TELOPT_ENCRYPT)
324 encrypt_send_support();
325 #endif /* ENCRYPTION */
329 wontoption(int option)
331 if (do_dont_resp[option]) {
332 --do_dont_resp[option];
333 if (do_dont_resp[option] && my_state_is_dont(option))
334 --do_dont_resp[option];
337 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
341 #ifdef KLUDGELINEMODE
348 settimer(modenegotiated);
354 set_my_want_state_dont(option);
355 set_my_state_dont(option);
356 return; /* Never reply to TM will's/wont's */
361 set_my_want_state_dont(option);
362 if (my_state_is_do(option))
363 send_dont(option, 0);
364 setconnmode(0); /* Set new tty mode */
365 } else if (option == TELOPT_TM) {
367 * Special case for TM.
371 set_my_want_state_dont(option);
373 set_my_state_dont(option);
379 int new_state_ok = 0;
381 if (will_wont_resp[option]) {
382 --will_wont_resp[option];
383 if (will_wont_resp[option] && my_state_is_will(option))
384 --will_wont_resp[option];
387 if (will_wont_resp[option] == 0) {
388 if (my_want_state_is_wont(option)) {
394 * Special case for TM. We send a WILL, but pretend
397 send_will(option, 0);
398 set_my_want_state_wont(TELOPT_TM);
399 set_my_state_wont(TELOPT_TM);
402 case TELOPT_BINARY: /* binary mode */
403 case TELOPT_NAWS: /* window size */
404 case TELOPT_TSPEED: /* terminal speed */
405 case TELOPT_LFLOW: /* local flow control */
406 case TELOPT_TTYPE: /* terminal type option */
407 case TELOPT_SGA: /* no big deal */
409 case TELOPT_ENCRYPT: /* encryption variable option */
410 #endif /* ENCRYPTION */
414 case TELOPT_NEW_ENVIRON: /* New environment variable option */
416 if (my_state_is_will(TELOPT_OLD_ENVIRON))
417 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
419 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
420 if (my_state_is_will(TELOPT_NEW_ENVIRON))
421 break; /* Don't enable if new one is in use! */
423 telopt_environ = option;
428 #ifdef AUTHENTICATION
429 case TELOPT_AUTHENTICATION:
435 case TELOPT_XDISPLOC: /* X Display location */
436 if (env_getvalue("DISPLAY"))
440 case TELOPT_LINEMODE:
441 #ifdef KLUDGELINEMODE
443 send_do(TELOPT_SGA, 1);
445 set_my_want_state_will(TELOPT_LINEMODE);
446 send_will(option, 0);
447 set_my_state_will(TELOPT_LINEMODE);
451 case TELOPT_ECHO: /* We're never going to echo... */
457 set_my_want_state_will(option);
458 send_will(option, 0);
459 setconnmode(0); /* Set new tty mode */
461 will_wont_resp[option]++;
462 send_wont(option, 0);
466 * Handle options that need more things done after the
467 * other side has acknowledged the option.
470 case TELOPT_LINEMODE:
471 #ifdef KLUDGELINEMODE
473 send_do(TELOPT_SGA, 1);
475 set_my_state_will(option);
477 send_do(TELOPT_SGA, 0);
482 set_my_state_will(option);
486 dontoption(int option)
489 if (will_wont_resp[option]) {
490 --will_wont_resp[option];
491 if (will_wont_resp[option] && my_state_is_wont(option))
492 --will_wont_resp[option];
495 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
497 case TELOPT_LINEMODE:
498 linemode = 0; /* put us back to the default state */
501 case TELOPT_NEW_ENVIRON:
503 * The new environ option wasn't recognized, try
506 send_will(TELOPT_OLD_ENVIRON, 1);
507 telopt_environ = TELOPT_OLD_ENVIRON;
511 /* we always accept a DONT */
512 set_my_want_state_wont(option);
513 if (my_state_is_will(option))
514 send_wont(option, 0);
515 setconnmode(0); /* Set new tty mode */
517 set_my_state_wont(option);
521 * Given a buffer returned by tgetent(), this routine will turn
522 * the pipe separated list of names in the buffer into an array
523 * of pointers to null terminated names. We toss out any bad,
524 * duplicate, or verbose names (names with spaces).
527 static const char *name_unknown = "UNKNOWN";
528 static const char *unknown[] = { NULL, NULL };
531 mklist(char *buf, char *name)
534 char c, *cp, **argvp, *cp2, **argv, **avt;
537 if (strlen(name) > 40) {
539 unknown[0] = name_unknown;
545 unknown[0] = name_unknown;
547 * Count up the number of names.
549 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
554 * Allocate an array to put the name pointers into
556 argv = (char **)malloc((n+3)*sizeof(char *));
561 * Fill up the array of pointers to names.
566 for (cp = cp2 = buf; (c = *cp); cp++) {
567 if (c == '|' || c == ':') {
570 * Skip entries that have spaces or are over 40
571 * characters long. If this is our environment
572 * name, then put it up front. Otherwise, as
573 * long as this is not a duplicate name (case
574 * insensitive) add it to the list.
576 if (n || (cp - cp2 > 41))
578 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
580 else if (is_unique(cp2, argv+1, argvp))
585 * Skip multiple delimiters. Reset cp2 to
586 * the beginning of the next name. Reset n,
587 * the flag for names with spaces.
589 while ((c = *cp) == '|')
595 * Skip entries with spaces or non-ascii values.
596 * Convert lower case letters to upper case.
598 if ((c == ' ') || !isascii(c))
605 * Check for an old V6 2 character name. If the second
606 * name points to the beginning of the buffer, and is
607 * only 2 characters long, move it to the end of the array.
609 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
611 for (avt = &argv[1]; avt < argvp; avt++)
617 * Duplicate last name, for TTYPE option, and null
618 * terminate the array. If we didn't find a match on
619 * our terminal name, put that name at the beginning.
630 for (avt = argv; avt < argvp; avt++)
635 return((const char **)argv);
641 is_unique(char *name, char **as, char **ae)
646 n = strlen(name) + 1;
647 for (ap = as; ap < ae; ap++)
648 if (strncasecmp(*ap, name, n) == 0)
658 setupterm(char *tname, int fd, int *errp)
660 if (tgetent(termbuf, tname) == 1) {
661 termbuf[1023] = '\0';
671 #define termbuf ttytype
672 extern char ttytype[];
675 int resettermname = 1;
681 static const char **tnamep = 0;
682 static const char **next;
687 if (tnamep && tnamep != unknown)
689 if ((tname = env_getvalue("TERM")) &&
690 (setupterm(tname, 1, &err) == 0)) {
691 tnamep = mklist(termbuf, tname);
693 if (tname && (strlen(tname) <= 40)) {
697 unknown[0] = name_unknown;
709 * Look at the sub-option buffer, and try to be helpful to the other
712 * Currently we recognize:
714 * Terminal type, send request.
715 * Terminal speed (send request).
716 * Local flow control (is request).
723 unsigned char subchar;
725 printsub('<', subbuffer, SB_LEN()+2);
726 switch (subchar = SB_GET()) {
728 if (my_want_state_is_wont(TELOPT_TTYPE))
730 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
734 unsigned char temp[50];
737 name = gettermname();
738 len = strlen(name) + 4 + 2;
739 if (len < NETROOM()) {
740 sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
741 TELQUAL_IS, name, IAC, SE);
742 ring_supply_data(&netoring, temp, len);
743 printsub('>', &temp[2], len-2);
745 ExitString("No room in buffer for terminal type.\n", 1);
751 if (my_want_state_is_wont(TELOPT_TSPEED))
755 if (SB_GET() == TELQUAL_SEND) {
757 unsigned char temp[50];
760 TerminalSpeeds(&ispeed, &ospeed);
762 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
763 TELQUAL_IS, ospeed, ispeed, IAC, SE);
764 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
766 if (len < NETROOM()) {
767 ring_supply_data(&netoring, temp, len);
768 printsub('>', temp+2, len - 2);
770 /*@*/ else printf("lm_will: not enough room in buffer\n");
774 if (my_want_state_is_wont(TELOPT_LFLOW))
779 case LFLOW_RESTART_ANY:
782 case LFLOW_RESTART_XON:
798 case TELOPT_LINEMODE:
799 if (my_want_state_is_wont(TELOPT_LINEMODE))
805 lm_will(subpointer, SB_LEN());
808 lm_wont(subpointer, SB_LEN());
811 lm_do(subpointer, SB_LEN());
814 lm_dont(subpointer, SB_LEN());
817 slc(subpointer, SB_LEN());
820 lm_mode(subpointer, SB_LEN(), 0);
828 case TELOPT_OLD_ENVIRON:
830 case TELOPT_NEW_ENVIRON:
836 if (my_want_state_is_dont(subchar))
840 if (my_want_state_is_wont(subchar)) {
847 env_opt(subpointer, SB_LEN());
850 case TELOPT_XDISPLOC:
851 if (my_want_state_is_wont(TELOPT_XDISPLOC))
855 if (SB_GET() == TELQUAL_SEND) {
856 unsigned char temp[50], *dp;
859 if ((dp = env_getvalue("DISPLAY")) == NULL ||
860 strlen(dp) > sizeof(temp) - 7) {
862 * Something happened, we no longer have a DISPLAY
863 * variable. Or it is too long. So, turn off the option.
865 send_wont(TELOPT_XDISPLOC, 1);
868 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
869 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
870 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
872 if (len < NETROOM()) {
873 ring_supply_data(&netoring, temp, len);
874 printsub('>', temp+2, len - 2);
876 /*@*/ else printf("lm_will: not enough room in buffer\n");
880 #ifdef AUTHENTICATION
881 case TELOPT_AUTHENTICATION: {
888 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
890 auth_is(subpointer, SB_LEN());
893 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
895 auth_send(subpointer, SB_LEN());
898 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
900 auth_reply(subpointer, SB_LEN());
903 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
905 auth_name(subpointer, SB_LEN());
917 if (my_want_state_is_dont(TELOPT_ENCRYPT))
919 encrypt_start(subpointer, SB_LEN());
922 if (my_want_state_is_dont(TELOPT_ENCRYPT))
926 case ENCRYPT_SUPPORT:
927 if (my_want_state_is_wont(TELOPT_ENCRYPT))
929 encrypt_support(subpointer, SB_LEN());
931 case ENCRYPT_REQSTART:
932 if (my_want_state_is_wont(TELOPT_ENCRYPT))
934 encrypt_request_start(subpointer, SB_LEN());
937 if (my_want_state_is_wont(TELOPT_ENCRYPT))
940 * We can always send an REQEND so that we cannot
941 * get stuck encrypting. We should only get this
942 * if we have been able to get in the correct mode
945 encrypt_request_end();
948 if (my_want_state_is_dont(TELOPT_ENCRYPT))
950 encrypt_is(subpointer, SB_LEN());
953 if (my_want_state_is_wont(TELOPT_ENCRYPT))
955 encrypt_reply(subpointer, SB_LEN());
957 case ENCRYPT_ENC_KEYID:
958 if (my_want_state_is_dont(TELOPT_ENCRYPT))
960 encrypt_enc_keyid(subpointer, SB_LEN());
962 case ENCRYPT_DEC_KEYID:
963 if (my_want_state_is_wont(TELOPT_ENCRYPT))
965 encrypt_dec_keyid(subpointer, SB_LEN());
971 #endif /* ENCRYPTION */
977 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
980 lm_will(unsigned char *cmd, int len)
983 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
987 case LM_FORWARDMASK: /* We shouldn't ever get this... */
991 if (NETROOM() > (int)sizeof(str_lm)) {
992 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
993 printsub('>', &str_lm[2], sizeof(str_lm)-2);
995 /*@*/ else printf("lm_will: not enough room in buffer\n");
1001 lm_wont(unsigned char *cmd, int len)
1004 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1008 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1010 /* We are always DONT, so don't respond */
1016 lm_do(unsigned char *cmd, int len)
1019 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1023 case LM_FORWARDMASK:
1027 if (NETROOM() > (int)sizeof(str_lm)) {
1028 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1029 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1031 /*@*/ else printf("lm_do: not enough room in buffer\n");
1037 lm_dont(unsigned char *cmd, int len)
1040 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1044 case LM_FORWARDMASK:
1046 /* we are always WONT, so don't respond */
1051 static unsigned char str_lm_mode[] = {
1052 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1056 lm_mode(unsigned char *cmd, int len, int init)
1060 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1064 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1065 str_lm_mode[4] = linemode;
1067 str_lm_mode[4] |= MODE_ACK;
1068 if (NETROOM() > (int)sizeof(str_lm_mode)) {
1069 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1070 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1072 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1073 setconnmode(0); /* set changed mode */
1080 * Handle special character suboption of LINEMODE.
1086 char flags; /* Current flags & level */
1087 char mylevel; /* Maximum level & flags */
1090 #define SLC_IMPORT 0
1091 #define SLC_EXPORT 1
1092 #define SLC_RVALUE 2
1093 static int slc_mode = SLC_EXPORT;
1101 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1104 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1107 #define initfunc(func, flags) { \
1108 spcp = &spc_data[func]; \
1109 if ((spcp->valp = tcval(func))) { \
1110 spcp->val = *spcp->valp; \
1111 spcp->mylevel = SLC_VARIABLE|flags; \
1114 spcp->mylevel = SLC_DEFAULT; \
1118 initfunc(SLC_SYNCH, 0);
1120 initfunc(SLC_AO, 0);
1121 initfunc(SLC_AYT, 0);
1123 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1124 initfunc(SLC_EOF, 0);
1126 initfunc(SLC_SUSP, SLC_FLUSHIN);
1128 initfunc(SLC_EC, 0);
1129 initfunc(SLC_EL, 0);
1131 initfunc(SLC_EW, 0);
1132 initfunc(SLC_RP, 0);
1133 initfunc(SLC_LNEXT, 0);
1135 initfunc(SLC_XON, 0);
1136 initfunc(SLC_XOFF, 0);
1138 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1139 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1141 initfunc(SLC_FORW1, 0);
1143 initfunc(SLC_FORW2, 0);
1147 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1150 if (slc_mode == SLC_EXPORT)
1160 printf("Special characters are %s values\n",
1161 slc_mode == SLC_IMPORT ? "remote default" :
1162 slc_mode == SLC_EXPORT ? "local" :
1167 slc_mode_export(void)
1169 slc_mode = SLC_EXPORT;
1170 if (my_state_is_will(TELOPT_LINEMODE))
1175 slc_mode_import(int def)
1177 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1178 if (my_state_is_will(TELOPT_LINEMODE))
1182 unsigned char slc_import_val[] = {
1183 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1185 unsigned char slc_import_def[] = {
1186 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1192 if (NETROOM() > (int)sizeof(slc_import_val)) {
1194 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1195 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1197 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1198 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1201 /*@*/ else printf("slc_import: not enough room\n");
1209 TerminalDefaultChars();
1212 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1213 if (spcp->mylevel != SLC_NOSUPPORT) {
1214 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1215 spcp->flags = SLC_NOSUPPORT;
1217 spcp->flags = spcp->mylevel;
1219 spcp->val = *spcp->valp;
1220 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1225 setconnmode(1); /* Make sure the character values are set */
1229 slc(unsigned char *cp, int len)
1236 for (; len >= 3; len -=3, cp +=3) {
1238 func = cp[SLC_FUNC];
1242 * Client side: always ignore 0 function.
1247 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1248 slc_add_reply(func, SLC_NOSUPPORT, 0);
1252 spcp = &spc_data[func];
1254 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1256 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1257 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1261 if (level == (SLC_DEFAULT|SLC_ACK)) {
1263 * This is an error condition, the SLC_ACK
1264 * bit should never be set for the SLC_DEFAULT
1265 * level. Our best guess to recover is to
1266 * ignore the SLC_ACK bit.
1268 cp[SLC_FLAGS] &= ~SLC_ACK;
1271 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1272 spcp->val = (cc_t)cp[SLC_VALUE];
1273 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1279 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1280 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1281 spcp->val = (cc_t)cp[SLC_VALUE];
1283 if (level == SLC_DEFAULT) {
1284 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1285 spcp->flags = spcp->mylevel;
1287 spcp->flags = SLC_NOSUPPORT;
1289 slc_add_reply(func, spcp->flags, spcp->val);
1293 setconnmode(1); /* set the new character values */
1302 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1303 if (spcp->valp && spcp->val != *spcp->valp) {
1304 spcp->val = *spcp->valp;
1305 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1306 spcp->flags = SLC_NOSUPPORT;
1308 spcp->flags = spcp->mylevel;
1309 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1316 unsigned char slc_reply[128];
1317 unsigned char *slc_replyp;
1320 slc_start_reply(void)
1322 slc_replyp = slc_reply;
1323 *slc_replyp++ = IAC;
1325 *slc_replyp++ = TELOPT_LINEMODE;
1326 *slc_replyp++ = LM_SLC;
1330 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1332 if ((*slc_replyp++ = func) == IAC)
1333 *slc_replyp++ = IAC;
1334 if ((*slc_replyp++ = flags) == IAC)
1335 *slc_replyp++ = IAC;
1336 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1337 *slc_replyp++ = IAC;
1345 *slc_replyp++ = IAC;
1347 len = slc_replyp - slc_reply;
1350 if (NETROOM() > len) {
1351 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1352 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1354 /*@*/else printf("slc_end_reply: not enough room\n");
1361 int need_update = 0;
1363 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1364 if (!(spcp->flags&SLC_ACK))
1366 spcp->flags &= ~SLC_ACK;
1367 if (spcp->valp && (*spcp->valp != spcp->val)) {
1368 *spcp->valp = spcp->val;
1372 return(need_update);
1378 * Earlier version of telnet/telnetd from the BSD code had
1379 * the definitions of VALUE and VAR reversed. To ensure
1380 * maximum interoperability, we assume that the server is
1381 * an older BSD server, until proven otherwise. The newer
1382 * BSD servers should be able to handle either definition,
1383 * so it is better to use the wrong values if we don't
1384 * know what type of server it is.
1387 int old_env_var = OLD_ENV_VAR;
1388 int old_env_value = OLD_ENV_VALUE;
1390 # define old_env_var OLD_ENV_VAR
1391 # define old_env_value OLD_ENV_VALUE
1396 env_opt(unsigned char *buf, int len)
1398 unsigned char *ep = 0, *epc = 0;
1401 switch(buf[0]&0xff) {
1406 } else for (i = 1; i < len; i++) {
1407 switch (buf[i]&0xff) {
1411 if (telopt_environ == TELOPT_OLD_ENVIRON
1413 /* Server has the same definitions */
1414 old_env_var = OLD_ENV_VAR;
1415 old_env_value = OLD_ENV_VALUE;
1421 * Although OLD_ENV_VALUE is not legal, we will
1422 * still recognize it, just in case it is an
1423 * old server that has VAR & VALUE mixed up...
1434 ep = epc = &buf[i+1];
1454 /* Ignore for now. We shouldn't get it anyway. */
1462 #define OPT_REPLY_SIZE 256
1463 unsigned char *opt_reply;
1464 unsigned char *opt_replyp;
1465 unsigned char *opt_replyend;
1471 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1473 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1474 if (opt_reply == NULL) {
1475 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1476 opt_reply = opt_replyp = opt_replyend = NULL;
1479 opt_replyp = opt_reply;
1480 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1481 *opt_replyp++ = IAC;
1483 *opt_replyp++ = telopt_environ;
1484 *opt_replyp++ = TELQUAL_IS;
1488 env_opt_start_info(void)
1492 opt_replyp[-1] = TELQUAL_INFO;
1496 env_opt_add(unsigned char *ep)
1498 unsigned char *vp, c;
1500 if (opt_reply == NULL) /*XXX*/
1503 if (ep == NULL || *ep == '\0') {
1504 /* Send user defined variables first. */
1506 while ((ep = env_default(0, 0)))
1509 /* Now add the list of well know variables. */
1511 while ((ep = env_default(0, 1)))
1515 vp = env_getvalue(ep);
1516 if (opt_replyp + (vp ? strlen((char *)vp) : 0) +
1517 strlen((char *)ep) + 6 > opt_replyend)
1520 opt_replyend += OPT_REPLY_SIZE;
1521 len = opt_replyend - opt_reply;
1522 opt_reply = (unsigned char *)realloc(opt_reply, len);
1523 if (opt_reply == NULL) {
1524 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1525 opt_reply = opt_replyp = opt_replyend = NULL;
1528 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1529 opt_replyend = opt_reply + len;
1531 if (opt_welldefined(ep))
1533 if (telopt_environ == TELOPT_OLD_ENVIRON)
1534 *opt_replyp++ = old_env_var;
1537 *opt_replyp++ = NEW_ENV_VAR;
1539 *opt_replyp++ = ENV_USERVAR;
1541 while ((c = *ep++)) {
1544 *opt_replyp++ = IAC;
1550 *opt_replyp++ = ENV_ESC;
1557 if (telopt_environ == TELOPT_OLD_ENVIRON)
1558 *opt_replyp++ = old_env_value;
1561 *opt_replyp++ = NEW_ENV_VALUE;
1569 opt_welldefined(const char *ep)
1571 if ((strcmp(ep, "USER") == 0) ||
1572 (strcmp(ep, "DISPLAY") == 0) ||
1573 (strcmp(ep, "PRINTER") == 0) ||
1574 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1575 (strcmp(ep, "JOB") == 0) ||
1576 (strcmp(ep, "ACCT") == 0))
1582 env_opt_end(int emptyok)
1586 len = opt_replyp - opt_reply + 2;
1587 if (emptyok || len > 6) {
1588 *opt_replyp++ = IAC;
1590 if (NETROOM() > len) {
1591 ring_supply_data(&netoring, opt_reply, len);
1592 printsub('>', &opt_reply[2], len - 2);
1594 /*@*/ else printf("slc_end_reply: not enough room\n");
1598 opt_reply = opt_replyp = opt_replyend = NULL;
1611 int returnValue = 0;
1615 while (TTYROOM() > 2) {
1618 ring_consumed(&netiring, count);
1622 sbp = netiring.consume;
1623 scc = ring_full_consecutive(&netiring);
1625 /* No more data coming in */
1630 c = *sbp++ & 0xff, scc--; count++;
1633 c = (*decrypt_input)(c);
1634 #endif /* ENCRYPTION */
1636 switch (telrcv_state) {
1639 telrcv_state = TS_DATA;
1641 break; /* Ignore \0 after CR */
1643 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1647 /* Else, fall through */
1651 telrcv_state = TS_IAC;
1655 * The 'crmod' hack (see following) is needed
1656 * since we can't * set CRMOD on output only.
1657 * Machines like MULTICS like to send \r without
1658 * \n; since we must turn off CRMOD to get proper
1659 * input, the mapping is done here (sigh).
1661 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1666 c = (*decrypt_input)(c);
1667 #endif /* ENCRYPTION */
1669 sbp++, scc--; count++;
1672 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1674 sbp++, scc--; count++;
1679 (*decrypt_input)(-1);
1680 #endif /* ENCRYPTION */
1688 telrcv_state = TS_CR;
1704 telrcv_state = TS_WILL;
1708 telrcv_state = TS_WONT;
1712 telrcv_state = TS_DO;
1716 telrcv_state = TS_DONT;
1721 * We may have missed an urgent notification,
1722 * so make sure we flush whatever is in the
1725 printoption("RCVD", IAC, DM);
1728 SYNCHing = stilloob();
1734 telrcv_state = TS_SB;
1744 printoption("RCVD", IAC, c);
1747 telrcv_state = TS_DATA;
1751 printoption("RCVD", WILL, c);
1753 telrcv_state = TS_DATA;
1757 printoption("RCVD", WONT, c);
1759 telrcv_state = TS_DATA;
1763 printoption("RCVD", DO, c);
1765 if (c == TELOPT_NAWS) {
1767 } else if (c == TELOPT_LFLOW) {
1772 telrcv_state = TS_DATA;
1776 printoption("RCVD", DONT, c);
1779 setconnmode(0); /* set new tty mode (maybe) */
1780 telrcv_state = TS_DATA;
1785 telrcv_state = TS_SE;
1795 * This is an error. We only expect to get
1796 * "IAC IAC" or "IAC SE". Several things may
1797 * have happend. An IAC was not doubled, the
1798 * IAC SE was left off, or another option got
1799 * inserted into the suboption are all possibilities.
1800 * If we assume that the IAC was not doubled,
1801 * and really the IAC SE was left off, we could
1802 * get into an infinate loop here. So, instead,
1803 * we terminate the suboption, and process the
1804 * partial suboption if we can.
1811 printoption("In SUBOPTION processing, RCVD", IAC, c);
1812 suboption(); /* handle sub-option */
1813 telrcv_state = TS_IAC;
1817 telrcv_state = TS_SB;
1823 suboption(); /* handle sub-option */
1824 telrcv_state = TS_DATA;
1829 ring_consumed(&netiring, count);
1830 return returnValue||count;
1833 static int bol = 1, local = 0;
1841 command(0, "z\n", 2);
1852 int returnValue = 0;
1857 while (NETROOM() > 2) {
1863 ring_consumed(&ttyiring, count);
1867 tbp = ttyiring.consume;
1868 tcc = ring_full_consecutive(&ttyiring);
1873 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1874 if (rlogin != _POSIX_VDISABLE) {
1883 if (sc == '.' || c == termEofChar) {
1885 command(0, "close\n", 6);
1888 if (sc == termSuspChar) {
1890 command(0, "z\n", 2);
1894 command(0, tbp, tcc);
1908 if ((sc == '\n') || (sc == '\r'))
1910 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1912 * Double escape is a pass through of a single escape character.
1914 if (tcc && strip(*tbp) == escape) {
1920 command(0, (char *)tbp, tcc);
1929 #ifdef KLUDGELINEMODE
1930 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1931 if (tcc > 0 && strip(*tbp) == echoc) {
1932 tcc--; tbp++; count++;
1934 dontlecho = !dontlecho;
1935 settimer(echotoggle);
1942 if (MODE_LOCAL_CHARS(globalmode)) {
1943 if (TerminalSpecialChars(sc) == 0) {
1948 if (my_want_state_is_wont(TELOPT_BINARY)) {
1952 * If we are in CRMOD mode (\r ==> \n)
1953 * on our local machine, then probably
1954 * a newline (unix) is CRLF (TELNET).
1956 if (MODE_LOCAL_CHARS(globalmode)) {
1960 bol = flushline = 1;
1964 NET2ADD('\r', '\0');
1966 NET2ADD('\r', '\n');
1968 bol = flushline = 1;
1977 } else if (c == IAC) {
1984 ring_consumed(&ttyiring, count);
1985 return returnValue||count; /* Non-zero if we did anything */
1991 * Try to do something.
1993 * If we do something useful, return 1; else return 0.
1998 Scheduler(int block)
2000 /* One wants to be a bit careful about setting returnValue
2001 * to one, since a one implies we did some useful work,
2002 * and therefore probably won't be called to block next
2005 int netin, netout, netex, ttyin, ttyout;
2007 /* Decide which rings should be processed */
2009 netout = ring_full_count(&netoring) &&
2011 (my_want_state_is_wont(TELOPT_LINEMODE)
2012 #ifdef KLUDGELINEMODE
2013 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2016 my_want_state_is_will(TELOPT_BINARY));
2017 ttyout = ring_full_count(&ttyoring);
2019 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2021 netin = !ISend && ring_empty_count(&netiring);
2025 /* Call to system code to process rings */
2027 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2029 /* Now, look at the input rings, looking for work to do. */
2031 if (ring_full_count(&ttyiring)) {
2032 returnValue |= telsnd();
2035 if (ring_full_count(&netiring)) {
2036 returnValue |= telrcv();
2041 #ifdef AUTHENTICATION
2042 #define __unusedhere
2044 #define __unusedhere __unused
2047 * Select from tty and network...
2050 telnet(char *user __unusedhere)
2054 #ifdef AUTHENTICATION
2057 static char local_host[256] = { 0 };
2059 if (!local_host[0]) {
2060 gethostname(local_host, sizeof(local_host));
2061 local_host[sizeof(local_host)-1] = 0;
2063 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2064 auth_encrypt_user(user);
2069 #ifdef AUTHENTICATION
2071 send_will(TELOPT_AUTHENTICATION, 1);
2074 send_do(TELOPT_ENCRYPT, 1);
2075 send_will(TELOPT_ENCRYPT, 1);
2076 #endif /* ENCRYPTION */
2077 send_do(TELOPT_SGA, 1);
2078 send_will(TELOPT_TTYPE, 1);
2079 send_will(TELOPT_NAWS, 1);
2080 send_will(TELOPT_TSPEED, 1);
2081 send_will(TELOPT_LFLOW, 1);
2082 send_will(TELOPT_LINEMODE, 1);
2083 send_will(TELOPT_NEW_ENVIRON, 1);
2084 send_do(TELOPT_STATUS, 1);
2085 if (env_getvalue("DISPLAY"))
2086 send_will(TELOPT_XDISPLOC, 1);
2088 tel_enter_binary(eight);
2094 while ((schedValue = Scheduler(0)) != 0) {
2095 if (schedValue == -1) {
2101 if (Scheduler(1) == -1) {
2108 #if 0 /* XXX - this not being in is a bug */
2112 * Return the address of the next "item" in the TELNET data
2113 * stream. This will be the address of the next character if
2114 * the current address is a user data character, or it will
2115 * be the address of the character following the TELNET command
2116 * if the current address is a TELNET IAC ("I Am a Command")
2121 nextitem(char *current)
2123 if ((*current&0xff) != IAC) {
2126 switch (*(current+1)&0xff) {
2132 case SB: /* loop forever looking for the SE */
2134 char *look = current+2;
2137 if ((*look++&0xff) == IAC) {
2138 if ((*look++&0xff) == SE) {
2153 * We are about to do a TELNET SYNCH operation. Clear
2154 * the path to the network.
2156 * Things are a bit tricky since we may have sent the first
2157 * byte or so of a previous TELNET command into the network.
2158 * So, we have to scan the network buffer from the beginning
2159 * until we are up to where we want to be.
2161 * A side effect of what we do, just to keep things
2162 * simple, is to clear the urgent data pointer. The principal
2163 * caller should be setting the urgent data pointer AFTER calling
2174 * These routines add various telnet commands to the data stream.
2184 (void) ttyflush(1); /* Flush/drop output */
2185 /* do printoption AFTER flush, otherwise the output gets tossed... */
2186 printoption("SENT", DO, TELOPT_TM);
2193 printoption("SENT", IAC, AO);
2203 printoption("SENT", IAC, EL);
2210 printoption("SENT", IAC, EC);
2214 dosynch(char *ch __unused)
2216 netclear(); /* clear the path to the network */
2220 printoption("SENT", IAC, DM);
2224 int want_status_response = 0;
2227 get_status(char *ch __unused)
2229 unsigned char tmp[16];
2232 if (my_want_state_is_dont(TELOPT_STATUS)) {
2233 printf("Remote side does not support STATUS option\n");
2240 *cp++ = TELOPT_STATUS;
2241 *cp++ = TELQUAL_SEND;
2244 if (NETROOM() >= cp - tmp) {
2245 ring_supply_data(&netoring, tmp, cp-tmp);
2246 printsub('>', tmp+2, cp - tmp - 2);
2248 ++want_status_response;
2256 printoption("SENT", IAC, IP);
2269 NET2ADD(IAC, BREAK);
2270 printoption("SENT", IAC, BREAK);
2283 NET2ADD(IAC, ABORT);
2284 printoption("SENT", IAC, ABORT);
2298 printoption("SENT", IAC, SUSP);
2312 printoption("SENT", IAC, xEOF);
2319 printoption("SENT", IAC, AYT);
2323 * Send a window size update to the remote system.
2330 unsigned char tmp[16];
2333 if (my_state_is_wont(TELOPT_NAWS))
2336 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2337 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2339 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2347 *cp++ = TELOPT_NAWS;
2352 if (NETROOM() >= cp - tmp) {
2353 ring_supply_data(&netoring, tmp, cp-tmp);
2354 printsub('>', tmp+2, cp - tmp - 2);
2359 tel_enter_binary(int rw)
2362 send_do(TELOPT_BINARY, 1);
2364 send_will(TELOPT_BINARY, 1);
2368 tel_leave_binary(int rw)
2371 send_dont(TELOPT_BINARY, 1);
2373 send_wont(TELOPT_BINARY, 1);