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.3 2005/03/28 18:03:32 drhodus 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 const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1318 unsigned char *slc_replyp;
1321 slc_start_reply(void)
1323 slc_replyp = slc_reply;
1324 *slc_replyp++ = IAC;
1326 *slc_replyp++ = TELOPT_LINEMODE;
1327 *slc_replyp++ = LM_SLC;
1331 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1333 /* A sequence of up to 6 bytes my be written for this member of the SLC
1334 * suboption list by this function. The end of negotiation command,
1335 * which is written by slc_end_reply(), will require 2 additional
1336 * bytes. Do not proceed unless there is sufficient space for these
1339 if (&slc_replyp[6+2] > slc_reply_eom)
1341 if ((*slc_replyp++ = func) == IAC)
1342 *slc_replyp++ = IAC;
1343 if ((*slc_replyp++ = flags) == IAC)
1344 *slc_replyp++ = IAC;
1345 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1346 *slc_replyp++ = IAC;
1353 /* The end of negotiation command requires 2 bytes. */
1354 if (&slc_replyp[2] > slc_reply_eom)
1356 *slc_replyp++ = IAC;
1358 len = slc_replyp - slc_reply;
1361 if (NETROOM() > len) {
1362 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1363 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1365 /*@*/else printf("slc_end_reply: not enough room\n");
1372 int need_update = 0;
1374 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1375 if (!(spcp->flags&SLC_ACK))
1377 spcp->flags &= ~SLC_ACK;
1378 if (spcp->valp && (*spcp->valp != spcp->val)) {
1379 *spcp->valp = spcp->val;
1383 return(need_update);
1389 * Earlier version of telnet/telnetd from the BSD code had
1390 * the definitions of VALUE and VAR reversed. To ensure
1391 * maximum interoperability, we assume that the server is
1392 * an older BSD server, until proven otherwise. The newer
1393 * BSD servers should be able to handle either definition,
1394 * so it is better to use the wrong values if we don't
1395 * know what type of server it is.
1398 int old_env_var = OLD_ENV_VAR;
1399 int old_env_value = OLD_ENV_VALUE;
1401 # define old_env_var OLD_ENV_VAR
1402 # define old_env_value OLD_ENV_VALUE
1407 env_opt(unsigned char *buf, int len)
1409 unsigned char *ep = 0, *epc = 0;
1412 switch(buf[0]&0xff) {
1417 } else for (i = 1; i < len; i++) {
1418 switch (buf[i]&0xff) {
1422 if (telopt_environ == TELOPT_OLD_ENVIRON
1424 /* Server has the same definitions */
1425 old_env_var = OLD_ENV_VAR;
1426 old_env_value = OLD_ENV_VALUE;
1432 * Although OLD_ENV_VALUE is not legal, we will
1433 * still recognize it, just in case it is an
1434 * old server that has VAR & VALUE mixed up...
1445 ep = epc = &buf[i+1];
1465 /* Ignore for now. We shouldn't get it anyway. */
1473 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1474 unsigned char *opt_reply = NULL;
1475 unsigned char *opt_replyp;
1476 unsigned char *opt_replyend;
1482 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1484 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1485 if (opt_reply == NULL) {
1486 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1487 opt_reply = opt_replyp = opt_replyend = NULL;
1490 opt_replyp = opt_reply;
1491 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1492 *opt_replyp++ = IAC;
1494 *opt_replyp++ = telopt_environ;
1495 *opt_replyp++ = TELQUAL_IS;
1499 env_opt_start_info(void)
1503 opt_replyp[-1] = TELQUAL_INFO;
1507 env_opt_add(unsigned char *ep)
1509 unsigned char *vp, c;
1511 if (opt_reply == NULL) /*XXX*/
1514 if (ep == NULL || *ep == '\0') {
1515 /* Send user defined variables first. */
1517 while ((ep = env_default(0, 0)))
1520 /* Now add the list of well know variables. */
1522 while ((ep = env_default(0, 1)))
1526 vp = env_getvalue(ep);
1527 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1528 2 * strlen((char *)ep) + 6 > opt_replyend)
1532 opt_replyend += OPT_REPLY_SIZE;
1533 len = opt_replyend - opt_reply;
1534 opt_reply = (unsigned char *)realloc(opt_reply, len);
1535 if (opt_reply == NULL) {
1536 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1537 opt_reply = opt_replyp = opt_replyend = NULL;
1540 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1541 opt_replyend = opt_reply + len;
1543 if (opt_welldefined(ep))
1545 if (telopt_environ == TELOPT_OLD_ENVIRON)
1546 *opt_replyp++ = old_env_var;
1549 *opt_replyp++ = NEW_ENV_VAR;
1551 *opt_replyp++ = ENV_USERVAR;
1553 while ((c = *ep++)) {
1554 if (opt_replyp + (2 + 2) > opt_replyend)
1558 *opt_replyp++ = IAC;
1564 *opt_replyp++ = ENV_ESC;
1570 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1573 if (telopt_environ == TELOPT_OLD_ENVIRON)
1574 *opt_replyp++ = old_env_value;
1577 *opt_replyp++ = NEW_ENV_VALUE;
1585 opt_welldefined(const char *ep)
1587 if ((strcmp(ep, "USER") == 0) ||
1588 (strcmp(ep, "DISPLAY") == 0) ||
1589 (strcmp(ep, "PRINTER") == 0) ||
1590 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1591 (strcmp(ep, "JOB") == 0) ||
1592 (strcmp(ep, "ACCT") == 0))
1598 env_opt_end(int emptyok)
1602 if (opt_replyp + 2 > opt_replyend)
1604 len = opt_replyp + 2 - opt_reply;
1605 if (emptyok || len > 6) {
1606 *opt_replyp++ = IAC;
1608 if (NETROOM() > len) {
1609 ring_supply_data(&netoring, opt_reply, len);
1610 printsub('>', &opt_reply[2], len - 2);
1612 /*@*/ else printf("slc_end_reply: not enough room\n");
1616 opt_reply = opt_replyp = opt_replyend = NULL;
1629 int returnValue = 0;
1633 while (TTYROOM() > 2) {
1636 ring_consumed(&netiring, count);
1640 sbp = netiring.consume;
1641 scc = ring_full_consecutive(&netiring);
1643 /* No more data coming in */
1648 c = *sbp++ & 0xff, scc--; count++;
1651 c = (*decrypt_input)(c);
1652 #endif /* ENCRYPTION */
1654 switch (telrcv_state) {
1657 telrcv_state = TS_DATA;
1659 break; /* Ignore \0 after CR */
1661 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1665 /* Else, fall through */
1669 telrcv_state = TS_IAC;
1673 * The 'crmod' hack (see following) is needed
1674 * since we can't * set CRMOD on output only.
1675 * Machines like MULTICS like to send \r without
1676 * \n; since we must turn off CRMOD to get proper
1677 * input, the mapping is done here (sigh).
1679 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1684 c = (*decrypt_input)(c);
1685 #endif /* ENCRYPTION */
1687 sbp++, scc--; count++;
1690 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1692 sbp++, scc--; count++;
1697 (*decrypt_input)(-1);
1698 #endif /* ENCRYPTION */
1706 telrcv_state = TS_CR;
1722 telrcv_state = TS_WILL;
1726 telrcv_state = TS_WONT;
1730 telrcv_state = TS_DO;
1734 telrcv_state = TS_DONT;
1739 * We may have missed an urgent notification,
1740 * so make sure we flush whatever is in the
1743 printoption("RCVD", IAC, DM);
1746 SYNCHing = stilloob();
1752 telrcv_state = TS_SB;
1762 printoption("RCVD", IAC, c);
1765 telrcv_state = TS_DATA;
1769 printoption("RCVD", WILL, c);
1771 telrcv_state = TS_DATA;
1775 printoption("RCVD", WONT, c);
1777 telrcv_state = TS_DATA;
1781 printoption("RCVD", DO, c);
1783 if (c == TELOPT_NAWS) {
1785 } else if (c == TELOPT_LFLOW) {
1790 telrcv_state = TS_DATA;
1794 printoption("RCVD", DONT, c);
1797 setconnmode(0); /* set new tty mode (maybe) */
1798 telrcv_state = TS_DATA;
1803 telrcv_state = TS_SE;
1813 * This is an error. We only expect to get
1814 * "IAC IAC" or "IAC SE". Several things may
1815 * have happend. An IAC was not doubled, the
1816 * IAC SE was left off, or another option got
1817 * inserted into the suboption are all possibilities.
1818 * If we assume that the IAC was not doubled,
1819 * and really the IAC SE was left off, we could
1820 * get into an infinate loop here. So, instead,
1821 * we terminate the suboption, and process the
1822 * partial suboption if we can.
1829 printoption("In SUBOPTION processing, RCVD", IAC, c);
1830 suboption(); /* handle sub-option */
1831 telrcv_state = TS_IAC;
1835 telrcv_state = TS_SB;
1841 suboption(); /* handle sub-option */
1842 telrcv_state = TS_DATA;
1847 ring_consumed(&netiring, count);
1848 return returnValue||count;
1851 static int bol = 1, local = 0;
1859 command(0, "z\n", 2);
1870 int returnValue = 0;
1875 while (NETROOM() > 2) {
1881 ring_consumed(&ttyiring, count);
1885 tbp = ttyiring.consume;
1886 tcc = ring_full_consecutive(&ttyiring);
1891 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1892 if (rlogin != _POSIX_VDISABLE) {
1901 if (sc == '.' || c == termEofChar) {
1903 command(0, "close\n", 6);
1906 if (sc == termSuspChar) {
1908 command(0, "z\n", 2);
1912 command(0, tbp, tcc);
1926 if ((sc == '\n') || (sc == '\r'))
1928 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1930 * Double escape is a pass through of a single escape character.
1932 if (tcc && strip(*tbp) == escape) {
1938 command(0, (char *)tbp, tcc);
1947 #ifdef KLUDGELINEMODE
1948 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1949 if (tcc > 0 && strip(*tbp) == echoc) {
1950 tcc--; tbp++; count++;
1952 dontlecho = !dontlecho;
1953 settimer(echotoggle);
1960 if (MODE_LOCAL_CHARS(globalmode)) {
1961 if (TerminalSpecialChars(sc) == 0) {
1966 if (my_want_state_is_wont(TELOPT_BINARY)) {
1970 * If we are in CRMOD mode (\r ==> \n)
1971 * on our local machine, then probably
1972 * a newline (unix) is CRLF (TELNET).
1974 if (MODE_LOCAL_CHARS(globalmode)) {
1978 bol = flushline = 1;
1982 NET2ADD('\r', '\0');
1984 NET2ADD('\r', '\n');
1986 bol = flushline = 1;
1995 } else if (c == IAC) {
2002 ring_consumed(&ttyiring, count);
2003 return returnValue||count; /* Non-zero if we did anything */
2009 * Try to do something.
2011 * If we do something useful, return 1; else return 0.
2016 Scheduler(int block)
2018 /* One wants to be a bit careful about setting returnValue
2019 * to one, since a one implies we did some useful work,
2020 * and therefore probably won't be called to block next
2023 int netin, netout, netex, ttyin, ttyout;
2025 /* Decide which rings should be processed */
2027 netout = ring_full_count(&netoring) &&
2029 (my_want_state_is_wont(TELOPT_LINEMODE)
2030 #ifdef KLUDGELINEMODE
2031 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2034 my_want_state_is_will(TELOPT_BINARY));
2035 ttyout = ring_full_count(&ttyoring);
2037 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2039 netin = !ISend && ring_empty_count(&netiring);
2043 /* Call to system code to process rings */
2045 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2047 /* Now, look at the input rings, looking for work to do. */
2049 if (ring_full_count(&ttyiring)) {
2050 returnValue |= telsnd();
2053 if (ring_full_count(&netiring)) {
2054 returnValue |= telrcv();
2059 #ifdef AUTHENTICATION
2060 #define __unusedhere
2062 #define __unusedhere __unused
2065 * Select from tty and network...
2068 telnet(char *user __unusedhere)
2072 #ifdef AUTHENTICATION
2075 static char local_host[256] = { 0 };
2077 if (!local_host[0]) {
2078 gethostname(local_host, sizeof(local_host));
2079 local_host[sizeof(local_host)-1] = 0;
2081 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2082 auth_encrypt_user(user);
2087 #ifdef AUTHENTICATION
2089 send_will(TELOPT_AUTHENTICATION, 1);
2092 send_do(TELOPT_ENCRYPT, 1);
2093 send_will(TELOPT_ENCRYPT, 1);
2094 #endif /* ENCRYPTION */
2095 send_do(TELOPT_SGA, 1);
2096 send_will(TELOPT_TTYPE, 1);
2097 send_will(TELOPT_NAWS, 1);
2098 send_will(TELOPT_TSPEED, 1);
2099 send_will(TELOPT_LFLOW, 1);
2100 send_will(TELOPT_LINEMODE, 1);
2101 send_will(TELOPT_NEW_ENVIRON, 1);
2102 send_do(TELOPT_STATUS, 1);
2103 if (env_getvalue("DISPLAY"))
2104 send_will(TELOPT_XDISPLOC, 1);
2106 tel_enter_binary(eight);
2112 while ((schedValue = Scheduler(0)) != 0) {
2113 if (schedValue == -1) {
2119 if (Scheduler(1) == -1) {
2126 #if 0 /* XXX - this not being in is a bug */
2130 * Return the address of the next "item" in the TELNET data
2131 * stream. This will be the address of the next character if
2132 * the current address is a user data character, or it will
2133 * be the address of the character following the TELNET command
2134 * if the current address is a TELNET IAC ("I Am a Command")
2139 nextitem(char *current)
2141 if ((*current&0xff) != IAC) {
2144 switch (*(current+1)&0xff) {
2150 case SB: /* loop forever looking for the SE */
2152 char *look = current+2;
2155 if ((*look++&0xff) == IAC) {
2156 if ((*look++&0xff) == SE) {
2171 * We are about to do a TELNET SYNCH operation. Clear
2172 * the path to the network.
2174 * Things are a bit tricky since we may have sent the first
2175 * byte or so of a previous TELNET command into the network.
2176 * So, we have to scan the network buffer from the beginning
2177 * until we are up to where we want to be.
2179 * A side effect of what we do, just to keep things
2180 * simple, is to clear the urgent data pointer. The principal
2181 * caller should be setting the urgent data pointer AFTER calling
2192 * These routines add various telnet commands to the data stream.
2202 (void) ttyflush(1); /* Flush/drop output */
2203 /* do printoption AFTER flush, otherwise the output gets tossed... */
2204 printoption("SENT", DO, TELOPT_TM);
2211 printoption("SENT", IAC, AO);
2221 printoption("SENT", IAC, EL);
2228 printoption("SENT", IAC, EC);
2232 dosynch(char *ch __unused)
2234 netclear(); /* clear the path to the network */
2238 printoption("SENT", IAC, DM);
2242 int want_status_response = 0;
2245 get_status(char *ch __unused)
2247 unsigned char tmp[16];
2250 if (my_want_state_is_dont(TELOPT_STATUS)) {
2251 printf("Remote side does not support STATUS option\n");
2258 *cp++ = TELOPT_STATUS;
2259 *cp++ = TELQUAL_SEND;
2262 if (NETROOM() >= cp - tmp) {
2263 ring_supply_data(&netoring, tmp, cp-tmp);
2264 printsub('>', tmp+2, cp - tmp - 2);
2266 ++want_status_response;
2274 printoption("SENT", IAC, IP);
2287 NET2ADD(IAC, BREAK);
2288 printoption("SENT", IAC, BREAK);
2301 NET2ADD(IAC, ABORT);
2302 printoption("SENT", IAC, ABORT);
2316 printoption("SENT", IAC, SUSP);
2330 printoption("SENT", IAC, xEOF);
2337 printoption("SENT", IAC, AYT);
2341 * Send a window size update to the remote system.
2348 unsigned char tmp[16];
2351 if (my_state_is_wont(TELOPT_NAWS))
2354 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2355 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2357 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2365 *cp++ = TELOPT_NAWS;
2370 if (NETROOM() >= cp - tmp) {
2371 ring_supply_data(&netoring, tmp, cp-tmp);
2372 printsub('>', tmp+2, cp - tmp - 2);
2377 tel_enter_binary(int rw)
2380 send_do(TELOPT_BINARY, 1);
2382 send_will(TELOPT_BINARY, 1);
2386 tel_leave_binary(int rw)
2389 send_dont(TELOPT_BINARY, 1);
2391 send_wont(TELOPT_BINARY, 1);