2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)telnet.c 8.4 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnet/telnet.c,v 1.4.2.5 2002/04/13 10:59:08 markm Exp $
33 #include <sys/types.h>
35 /* By the way, we need to include curses.h before telnet.h since,
36 * among other things, telnet.h #defines 'DO', which is a variable
37 * declared in curses.h.
46 #include <arpa/telnet.h>
56 #include <libtelnet/auth.h>
59 #include <libtelnet/encrypt.h>
61 #include <libtelnet/misc.h>
63 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
65 static unsigned char subbuffer[SUBBUFSIZE],
66 *subpointer, *subend; /* buffer for sub-options */
67 #define SB_CLEAR() subpointer = subbuffer;
68 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
69 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
70 *subpointer++ = (c); \
73 #define SB_GET() ((*subpointer++)&0xff)
74 #define SB_PEEK() ((*subpointer)&0xff)
75 #define SB_EOF() (subpointer >= subend)
76 #define SB_LEN() (subend - subpointer)
78 char options[256]; /* The combined options */
79 char do_dont_resp[256];
80 char will_wont_resp[256];
84 autologin = 0, /* Autologin anyone? */
88 ISend, /* trying to send network data in */
91 netdata, /* Print out network data flow */
92 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
94 SYNCHing, /* we are in TELNET SYNCH mode */
95 flushout, /* flush output */
96 autoflush = 0, /* flush output when interrupting? */
97 autosynch, /* send interrupt characters with SYNCH? */
98 localflow, /* we handle flow control locally */
99 restartany, /* if flow control enabled, restart on any character */
100 localchars, /* we recognize interrupt/quit */
101 donelclchars, /* the user has set "localchars" */
102 donebinarytoggle, /* the user has put us in binary */
103 dontlecho, /* do we suppress local echoing right now? */
105 doaddrlookup = 1, /* do a reverse address lookup? */
110 char *line; /* hack around breakage in sra.c :-( !! */
115 #ifdef KLUDGELINEMODE
120 * Telnet receiver states for fsm
129 #define TS_SB 7 /* sub-option collection */
130 #define TS_SE 8 /* looking for sub-option end */
132 static int telrcv_state;
134 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
136 # define telopt_environ TELOPT_NEW_ENVIRON
145 #ifdef KLUDGELINEMODE
146 int kludgelinemode = 1;
149 static int is_unique(char *, char **, char **);
152 * The following are some clocks used to decide how to interpret
153 * the relationship between various variables.
159 * Initialize telnet environment.
170 connected = ISend = localflow = donebinarytoggle = 0;
171 #ifdef AUTHENTICATION
173 auth_encrypt_connect(connected);
180 /* Don't change NetTrace */
182 escape = CONTROL(']');
183 rlogin = _POSIX_VDISABLE;
184 #ifdef KLUDGELINEMODE
185 echoc = CONTROL('E');
189 telrcv_state = TS_DATA;
194 * These routines are in charge of sending option negotiations
197 * The basic idea is that we send the negotiation if either side
198 * is in disagreement as to what the current state should be.
202 send_do(int c, int init)
205 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
206 my_want_state_is_do(c))
208 set_my_want_state_do(c);
213 printoption("SENT", DO, c);
217 send_dont(int c, int init)
220 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
221 my_want_state_is_dont(c))
223 set_my_want_state_dont(c);
228 printoption("SENT", DONT, c);
232 send_will(int c, int init)
235 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
236 my_want_state_is_will(c))
238 set_my_want_state_will(c);
243 printoption("SENT", WILL, c);
247 send_wont(int c, int init)
250 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
251 my_want_state_is_wont(c))
253 set_my_want_state_wont(c);
258 printoption("SENT", WONT, c);
262 willoption(int option)
264 int new_state_ok = 0;
266 if (do_dont_resp[option]) {
267 --do_dont_resp[option];
268 if (do_dont_resp[option] && my_state_is_do(option))
269 --do_dont_resp[option];
272 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
279 settimer(modenegotiated);
282 #ifdef AUTHENTICATION
283 case TELOPT_AUTHENTICATION:
287 #endif /* ENCRYPTION */
295 * Special case for TM. If we get back a WILL,
296 * pretend we got back a WONT.
298 set_my_want_state_dont(option);
299 set_my_state_dont(option);
300 return; /* Never reply to TM will's/wont's */
302 case TELOPT_LINEMODE:
308 set_my_want_state_do(option);
310 setconnmode(0); /* possibly set new tty mode */
312 do_dont_resp[option]++;
313 send_dont(option, 0);
316 set_my_state_do(option);
318 if (option == TELOPT_ENCRYPT)
319 encrypt_send_support();
320 #endif /* ENCRYPTION */
324 wontoption(int option)
326 if (do_dont_resp[option]) {
327 --do_dont_resp[option];
328 if (do_dont_resp[option] && my_state_is_dont(option))
329 --do_dont_resp[option];
332 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
336 #ifdef KLUDGELINEMODE
343 settimer(modenegotiated);
349 set_my_want_state_dont(option);
350 set_my_state_dont(option);
351 return; /* Never reply to TM will's/wont's */
356 set_my_want_state_dont(option);
357 if (my_state_is_do(option))
358 send_dont(option, 0);
359 setconnmode(0); /* Set new tty mode */
360 } else if (option == TELOPT_TM) {
362 * Special case for TM.
366 set_my_want_state_dont(option);
368 set_my_state_dont(option);
374 int new_state_ok = 0;
376 if (will_wont_resp[option]) {
377 --will_wont_resp[option];
378 if (will_wont_resp[option] && my_state_is_will(option))
379 --will_wont_resp[option];
382 if (will_wont_resp[option] == 0) {
383 if (my_want_state_is_wont(option)) {
389 * Special case for TM. We send a WILL, but pretend
392 send_will(option, 0);
393 set_my_want_state_wont(TELOPT_TM);
394 set_my_state_wont(TELOPT_TM);
397 case TELOPT_BINARY: /* binary mode */
398 case TELOPT_NAWS: /* window size */
399 case TELOPT_TSPEED: /* terminal speed */
400 case TELOPT_LFLOW: /* local flow control */
401 case TELOPT_TTYPE: /* terminal type option */
402 case TELOPT_SGA: /* no big deal */
404 case TELOPT_ENCRYPT: /* encryption variable option */
405 #endif /* ENCRYPTION */
409 case TELOPT_NEW_ENVIRON: /* New environment variable option */
411 if (my_state_is_will(TELOPT_OLD_ENVIRON))
412 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
414 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
415 if (my_state_is_will(TELOPT_NEW_ENVIRON))
416 break; /* Don't enable if new one is in use! */
418 telopt_environ = option;
423 #ifdef AUTHENTICATION
424 case TELOPT_AUTHENTICATION:
430 case TELOPT_XDISPLOC: /* X Display location */
431 if (env_getvalue("DISPLAY"))
435 case TELOPT_LINEMODE:
436 #ifdef KLUDGELINEMODE
438 send_do(TELOPT_SGA, 1);
440 set_my_want_state_will(TELOPT_LINEMODE);
441 send_will(option, 0);
442 set_my_state_will(TELOPT_LINEMODE);
446 case TELOPT_ECHO: /* We're never going to echo... */
452 set_my_want_state_will(option);
453 send_will(option, 0);
454 setconnmode(0); /* Set new tty mode */
456 will_wont_resp[option]++;
457 send_wont(option, 0);
461 * Handle options that need more things done after the
462 * other side has acknowledged the option.
465 case TELOPT_LINEMODE:
466 #ifdef KLUDGELINEMODE
468 send_do(TELOPT_SGA, 1);
470 set_my_state_will(option);
472 send_do(TELOPT_SGA, 0);
477 set_my_state_will(option);
481 dontoption(int option)
484 if (will_wont_resp[option]) {
485 --will_wont_resp[option];
486 if (will_wont_resp[option] && my_state_is_wont(option))
487 --will_wont_resp[option];
490 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
492 case TELOPT_LINEMODE:
493 linemode = 0; /* put us back to the default state */
496 case TELOPT_NEW_ENVIRON:
498 * The new environ option wasn't recognized, try
501 send_will(TELOPT_OLD_ENVIRON, 1);
502 telopt_environ = TELOPT_OLD_ENVIRON;
506 /* we always accept a DONT */
507 set_my_want_state_wont(option);
508 if (my_state_is_will(option))
509 send_wont(option, 0);
510 setconnmode(0); /* Set new tty mode */
512 set_my_state_wont(option);
516 * Given a buffer returned by tgetent(), this routine will turn
517 * the pipe separated list of names in the buffer into an array
518 * of pointers to null terminated names. We toss out any bad,
519 * duplicate, or verbose names (names with spaces).
522 static const char *name_unknown = "UNKNOWN";
523 static const char *unknown[] = { NULL, NULL };
526 mklist(char *buf, char *name)
529 char c, *cp, **argvp, *cp2, **argv, **avt;
532 if (strlen(name) > 40) {
534 unknown[0] = name_unknown;
540 unknown[0] = name_unknown;
542 * Count up the number of names.
544 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
549 * Allocate an array to put the name pointers into
551 argv = (char **)malloc((n+3)*sizeof(char *));
556 * Fill up the array of pointers to names.
561 for (cp = cp2 = buf; (c = *cp); cp++) {
562 if (c == '|' || c == ':') {
565 * Skip entries that have spaces or are over 40
566 * characters long. If this is our environment
567 * name, then put it up front. Otherwise, as
568 * long as this is not a duplicate name (case
569 * insensitive) add it to the list.
571 if (n || (cp - cp2 > 41))
573 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
575 else if (is_unique(cp2, argv+1, argvp))
580 * Skip multiple delimiters. Reset cp2 to
581 * the beginning of the next name. Reset n,
582 * the flag for names with spaces.
584 while ((c = *cp) == '|')
590 * Skip entries with spaces or non-ascii values.
591 * Convert lower case letters to upper case.
593 if ((c == ' ') || !isascii(c))
600 * Check for an old V6 2 character name. If the second
601 * name points to the beginning of the buffer, and is
602 * only 2 characters long, move it to the end of the array.
604 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
606 for (avt = &argv[1]; avt < argvp; avt++)
612 * Duplicate last name, for TTYPE option, and null
613 * terminate the array. If we didn't find a match on
614 * our terminal name, put that name at the beginning.
625 for (avt = argv; avt < argvp; avt++)
630 return((const char **)argv);
636 is_unique(char *name, char **as, char **ae)
641 n = strlen(name) + 1;
642 for (ap = as; ap < ae; ap++)
643 if (strncasecmp(*ap, name, n) == 0)
653 setupterm(char *tname, int fd, int *errp)
655 if (tgetent(termbuf, tname) == 1) {
656 termbuf[1023] = '\0';
666 #define termbuf ttytype
667 extern char ttytype[];
670 int resettermname = 1;
676 static const char **tnamep = NULL;
677 static const char **next;
682 if (tnamep && tnamep != unknown)
684 if ((tname = env_getvalue("TERM")) &&
685 (setupterm(tname, 1, &err) == 0)) {
686 tnamep = mklist(termbuf, tname);
688 if (tname && (strlen(tname) <= 40)) {
692 unknown[0] = name_unknown;
704 * Look at the sub-option buffer, and try to be helpful to the other
707 * Currently we recognize:
709 * Terminal type, send request.
710 * Terminal speed (send request).
711 * Local flow control (is request).
718 unsigned char subchar;
720 printsub('<', subbuffer, SB_LEN()+2);
721 switch (subchar = SB_GET()) {
723 if (my_want_state_is_wont(TELOPT_TTYPE))
725 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
729 unsigned char temp[50];
732 name = gettermname();
733 len = strlen(name) + 4 + 2;
734 if (len < NETROOM()) {
735 sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
736 TELQUAL_IS, name, IAC, SE);
737 ring_supply_data(&netoring, temp, len);
738 printsub('>', &temp[2], len-2);
740 ExitString("No room in buffer for terminal type.\n", 1);
746 if (my_want_state_is_wont(TELOPT_TSPEED))
750 if (SB_GET() == TELQUAL_SEND) {
752 unsigned char temp[50];
755 TerminalSpeeds(&ispeed, &ospeed);
757 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
758 TELQUAL_IS, ospeed, ispeed, IAC, SE);
759 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
761 if (len < NETROOM()) {
762 ring_supply_data(&netoring, temp, len);
763 printsub('>', temp+2, len - 2);
765 /*@*/ else printf("lm_will: not enough room in buffer\n");
769 if (my_want_state_is_wont(TELOPT_LFLOW))
774 case LFLOW_RESTART_ANY:
777 case LFLOW_RESTART_XON:
793 case TELOPT_LINEMODE:
794 if (my_want_state_is_wont(TELOPT_LINEMODE))
800 lm_will(subpointer, SB_LEN());
803 lm_wont(subpointer, SB_LEN());
806 lm_do(subpointer, SB_LEN());
809 lm_dont(subpointer, SB_LEN());
812 slc(subpointer, SB_LEN());
815 lm_mode(subpointer, SB_LEN(), 0);
823 case TELOPT_OLD_ENVIRON:
825 case TELOPT_NEW_ENVIRON:
831 if (my_want_state_is_dont(subchar))
835 if (my_want_state_is_wont(subchar)) {
842 env_opt(subpointer, SB_LEN());
845 case TELOPT_XDISPLOC:
846 if (my_want_state_is_wont(TELOPT_XDISPLOC))
850 if (SB_GET() == TELQUAL_SEND) {
851 unsigned char temp[50], *dp;
854 if ((dp = env_getvalue("DISPLAY")) == NULL ||
855 strlen(dp) > sizeof(temp) - 7) {
857 * Something happened, we no longer have a DISPLAY
858 * variable. Or it is too long. So, turn off the option.
860 send_wont(TELOPT_XDISPLOC, 1);
863 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
864 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
865 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
867 if (len < NETROOM()) {
868 ring_supply_data(&netoring, temp, len);
869 printsub('>', temp+2, len - 2);
871 /*@*/ else printf("lm_will: not enough room in buffer\n");
875 #ifdef AUTHENTICATION
876 case TELOPT_AUTHENTICATION: {
883 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
885 auth_is(subpointer, SB_LEN());
888 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
890 auth_send(subpointer, SB_LEN());
893 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
895 auth_reply(subpointer, SB_LEN());
898 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
900 auth_name(subpointer, SB_LEN());
912 if (my_want_state_is_dont(TELOPT_ENCRYPT))
914 encrypt_start(subpointer, SB_LEN());
917 if (my_want_state_is_dont(TELOPT_ENCRYPT))
921 case ENCRYPT_SUPPORT:
922 if (my_want_state_is_wont(TELOPT_ENCRYPT))
924 encrypt_support(subpointer, SB_LEN());
926 case ENCRYPT_REQSTART:
927 if (my_want_state_is_wont(TELOPT_ENCRYPT))
929 encrypt_request_start(subpointer, SB_LEN());
932 if (my_want_state_is_wont(TELOPT_ENCRYPT))
935 * We can always send an REQEND so that we cannot
936 * get stuck encrypting. We should only get this
937 * if we have been able to get in the correct mode
940 encrypt_request_end();
943 if (my_want_state_is_dont(TELOPT_ENCRYPT))
945 encrypt_is(subpointer, SB_LEN());
948 if (my_want_state_is_wont(TELOPT_ENCRYPT))
950 encrypt_reply(subpointer, SB_LEN());
952 case ENCRYPT_ENC_KEYID:
953 if (my_want_state_is_dont(TELOPT_ENCRYPT))
955 encrypt_enc_keyid(subpointer, SB_LEN());
957 case ENCRYPT_DEC_KEYID:
958 if (my_want_state_is_wont(TELOPT_ENCRYPT))
960 encrypt_dec_keyid(subpointer, SB_LEN());
966 #endif /* ENCRYPTION */
972 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
975 lm_will(unsigned char *cmd, int len)
978 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
982 case LM_FORWARDMASK: /* We shouldn't ever get this... */
986 if (NETROOM() > (int)sizeof(str_lm)) {
987 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
988 printsub('>', &str_lm[2], sizeof(str_lm)-2);
990 /*@*/ else printf("lm_will: not enough room in buffer\n");
996 lm_wont(unsigned char *cmd, int len)
999 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1003 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1005 /* We are always DONT, so don't respond */
1011 lm_do(unsigned char *cmd, int len)
1014 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1018 case LM_FORWARDMASK:
1022 if (NETROOM() > (int)sizeof(str_lm)) {
1023 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1024 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1026 /*@*/ else printf("lm_do: not enough room in buffer\n");
1032 lm_dont(unsigned char *cmd, int len)
1035 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1039 case LM_FORWARDMASK:
1041 /* we are always WONT, so don't respond */
1046 static unsigned char str_lm_mode[] = {
1047 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1051 lm_mode(unsigned char *cmd, int len, int init)
1055 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1059 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1060 str_lm_mode[4] = linemode;
1062 str_lm_mode[4] |= MODE_ACK;
1063 if (NETROOM() > (int)sizeof(str_lm_mode)) {
1064 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1065 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1067 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1068 setconnmode(0); /* set changed mode */
1075 * Handle special character suboption of LINEMODE.
1081 char flags; /* Current flags & level */
1082 char mylevel; /* Maximum level & flags */
1085 #define SLC_IMPORT 0
1086 #define SLC_EXPORT 1
1087 #define SLC_RVALUE 2
1088 static int slc_mode = SLC_EXPORT;
1096 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1099 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1102 #define initfunc(func, flags) { \
1103 spcp = &spc_data[func]; \
1104 if ((spcp->valp = tcval(func))) { \
1105 spcp->val = *spcp->valp; \
1106 spcp->mylevel = SLC_VARIABLE|flags; \
1109 spcp->mylevel = SLC_DEFAULT; \
1113 initfunc(SLC_SYNCH, 0);
1115 initfunc(SLC_AO, 0);
1116 initfunc(SLC_AYT, 0);
1118 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1119 initfunc(SLC_EOF, 0);
1121 initfunc(SLC_SUSP, SLC_FLUSHIN);
1123 initfunc(SLC_EC, 0);
1124 initfunc(SLC_EL, 0);
1126 initfunc(SLC_EW, 0);
1127 initfunc(SLC_RP, 0);
1128 initfunc(SLC_LNEXT, 0);
1130 initfunc(SLC_XON, 0);
1131 initfunc(SLC_XOFF, 0);
1133 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1134 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1136 initfunc(SLC_FORW1, 0);
1138 initfunc(SLC_FORW2, 0);
1142 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1145 if (slc_mode == SLC_EXPORT)
1155 printf("Special characters are %s values\n",
1156 slc_mode == SLC_IMPORT ? "remote default" :
1157 slc_mode == SLC_EXPORT ? "local" :
1162 slc_mode_export(void)
1164 slc_mode = SLC_EXPORT;
1165 if (my_state_is_will(TELOPT_LINEMODE))
1170 slc_mode_import(int def)
1172 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1173 if (my_state_is_will(TELOPT_LINEMODE))
1177 unsigned char slc_import_val[] = {
1178 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1180 unsigned char slc_import_def[] = {
1181 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1187 if (NETROOM() > (int)sizeof(slc_import_val)) {
1189 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1190 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1192 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1193 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1196 /*@*/ else printf("slc_import: not enough room\n");
1204 TerminalDefaultChars();
1207 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1208 if (spcp->mylevel != SLC_NOSUPPORT) {
1209 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1210 spcp->flags = SLC_NOSUPPORT;
1212 spcp->flags = spcp->mylevel;
1214 spcp->val = *spcp->valp;
1215 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1220 setconnmode(1); /* Make sure the character values are set */
1224 slc(unsigned char *cp, int len)
1231 for (; len >= 3; len -=3, cp +=3) {
1233 func = cp[SLC_FUNC];
1237 * Client side: always ignore 0 function.
1242 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1243 slc_add_reply(func, SLC_NOSUPPORT, 0);
1247 spcp = &spc_data[func];
1249 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1251 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1252 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1256 if (level == (SLC_DEFAULT|SLC_ACK)) {
1258 * This is an error condition, the SLC_ACK
1259 * bit should never be set for the SLC_DEFAULT
1260 * level. Our best guess to recover is to
1261 * ignore the SLC_ACK bit.
1263 cp[SLC_FLAGS] &= ~SLC_ACK;
1266 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1267 spcp->val = (cc_t)cp[SLC_VALUE];
1268 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1274 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1275 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1276 spcp->val = (cc_t)cp[SLC_VALUE];
1278 if (level == SLC_DEFAULT) {
1279 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1280 spcp->flags = spcp->mylevel;
1282 spcp->flags = SLC_NOSUPPORT;
1284 slc_add_reply(func, spcp->flags, spcp->val);
1288 setconnmode(1); /* set the new character values */
1297 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1298 if (spcp->valp && spcp->val != *spcp->valp) {
1299 spcp->val = *spcp->valp;
1300 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1301 spcp->flags = SLC_NOSUPPORT;
1303 spcp->flags = spcp->mylevel;
1304 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1311 unsigned char slc_reply[128];
1312 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1313 unsigned char *slc_replyp;
1316 slc_start_reply(void)
1318 slc_replyp = slc_reply;
1319 *slc_replyp++ = IAC;
1321 *slc_replyp++ = TELOPT_LINEMODE;
1322 *slc_replyp++ = LM_SLC;
1326 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1328 /* A sequence of up to 6 bytes my be written for this member of the SLC
1329 * suboption list by this function. The end of negotiation command,
1330 * which is written by slc_end_reply(), will require 2 additional
1331 * bytes. Do not proceed unless there is sufficient space for these
1334 if (&slc_replyp[6+2] > slc_reply_eom)
1336 if ((*slc_replyp++ = func) == IAC)
1337 *slc_replyp++ = IAC;
1338 if ((*slc_replyp++ = flags) == IAC)
1339 *slc_replyp++ = IAC;
1340 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1341 *slc_replyp++ = IAC;
1348 /* The end of negotiation command requires 2 bytes. */
1349 if (&slc_replyp[2] > slc_reply_eom)
1351 *slc_replyp++ = IAC;
1353 len = slc_replyp - slc_reply;
1356 if (NETROOM() > len) {
1357 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1358 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1360 /*@*/else printf("slc_end_reply: not enough room\n");
1367 int need_update = 0;
1369 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1370 if (!(spcp->flags&SLC_ACK))
1372 spcp->flags &= ~SLC_ACK;
1373 if (spcp->valp && (*spcp->valp != spcp->val)) {
1374 *spcp->valp = spcp->val;
1378 return(need_update);
1384 * Earlier version of telnet/telnetd from the BSD code had
1385 * the definitions of VALUE and VAR reversed. To ensure
1386 * maximum interoperability, we assume that the server is
1387 * an older BSD server, until proven otherwise. The newer
1388 * BSD servers should be able to handle either definition,
1389 * so it is better to use the wrong values if we don't
1390 * know what type of server it is.
1393 int old_env_var = OLD_ENV_VAR;
1394 int old_env_value = OLD_ENV_VALUE;
1396 # define old_env_var OLD_ENV_VAR
1397 # define old_env_value OLD_ENV_VALUE
1402 env_opt(unsigned char *buf, int len)
1404 unsigned char *ep = NULL, *epc = NULL;
1407 switch(buf[0]&0xff) {
1412 } else for (i = 1; i < len; i++) {
1413 switch (buf[i]&0xff) {
1417 if (telopt_environ == TELOPT_OLD_ENVIRON
1419 /* Server has the same definitions */
1420 old_env_var = OLD_ENV_VAR;
1421 old_env_value = OLD_ENV_VALUE;
1427 * Although OLD_ENV_VALUE is not legal, we will
1428 * still recognize it, just in case it is an
1429 * old server that has VAR & VALUE mixed up...
1440 ep = epc = &buf[i+1];
1460 /* Ignore for now. We shouldn't get it anyway. */
1468 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1469 unsigned char *opt_reply = NULL;
1470 unsigned char *opt_replyp;
1471 unsigned char *opt_replyend;
1477 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1479 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1480 if (opt_reply == NULL) {
1481 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1482 opt_reply = opt_replyp = opt_replyend = NULL;
1485 opt_replyp = opt_reply;
1486 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1487 *opt_replyp++ = IAC;
1489 *opt_replyp++ = telopt_environ;
1490 *opt_replyp++ = TELQUAL_IS;
1494 env_opt_start_info(void)
1498 opt_replyp[-1] = TELQUAL_INFO;
1502 env_opt_add(unsigned char *ep)
1504 unsigned char *vp, c;
1506 if (opt_reply == NULL) /*XXX*/
1509 if (ep == NULL || *ep == '\0') {
1510 /* Send user defined variables first. */
1512 while ((ep = env_default(0, 0)))
1515 /* Now add the list of well know variables. */
1517 while ((ep = env_default(0, 1)))
1521 vp = env_getvalue(ep);
1522 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1523 2 * strlen((char *)ep) + 6 > opt_replyend)
1527 opt_replyend += OPT_REPLY_SIZE;
1528 len = opt_replyend - opt_reply;
1529 opt_reply = (unsigned char *)realloc(opt_reply, len);
1530 if (opt_reply == NULL) {
1531 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1532 opt_reply = opt_replyp = opt_replyend = NULL;
1535 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1536 opt_replyend = opt_reply + len;
1538 if (opt_welldefined(ep))
1540 if (telopt_environ == TELOPT_OLD_ENVIRON)
1541 *opt_replyp++ = old_env_var;
1544 *opt_replyp++ = NEW_ENV_VAR;
1546 *opt_replyp++ = ENV_USERVAR;
1548 while ((c = *ep++)) {
1549 if (opt_replyp + (2 + 2) > opt_replyend)
1553 *opt_replyp++ = IAC;
1559 *opt_replyp++ = ENV_ESC;
1565 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1568 if (telopt_environ == TELOPT_OLD_ENVIRON)
1569 *opt_replyp++ = old_env_value;
1572 *opt_replyp++ = NEW_ENV_VALUE;
1580 opt_welldefined(const char *ep)
1582 if ((strcmp(ep, "USER") == 0) ||
1583 (strcmp(ep, "DISPLAY") == 0) ||
1584 (strcmp(ep, "PRINTER") == 0) ||
1585 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1586 (strcmp(ep, "JOB") == 0) ||
1587 (strcmp(ep, "ACCT") == 0))
1593 env_opt_end(int emptyok)
1597 if (opt_replyp + 2 > opt_replyend)
1599 len = opt_replyp + 2 - opt_reply;
1600 if (emptyok || len > 6) {
1601 *opt_replyp++ = IAC;
1603 if (NETROOM() > len) {
1604 ring_supply_data(&netoring, opt_reply, len);
1605 printsub('>', &opt_reply[2], len - 2);
1607 /*@*/ else printf("slc_end_reply: not enough room\n");
1611 opt_reply = opt_replyp = opt_replyend = NULL;
1624 int returnValue = 0;
1628 while (TTYROOM() > 2) {
1631 ring_consumed(&netiring, count);
1635 sbp = netiring.consume;
1636 scc = ring_full_consecutive(&netiring);
1638 /* No more data coming in */
1643 c = *sbp++ & 0xff, scc--; count++;
1646 c = (*decrypt_input)(c);
1647 #endif /* ENCRYPTION */
1649 switch (telrcv_state) {
1652 telrcv_state = TS_DATA;
1654 break; /* Ignore \0 after CR */
1656 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1660 /* Else, fall through */
1664 telrcv_state = TS_IAC;
1668 * The 'crmod' hack (see following) is needed
1669 * since we can't * set CRMOD on output only.
1670 * Machines like MULTICS like to send \r without
1671 * \n; since we must turn off CRMOD to get proper
1672 * input, the mapping is done here (sigh).
1674 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1679 c = (*decrypt_input)(c);
1680 #endif /* ENCRYPTION */
1682 sbp++, scc--; count++;
1685 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1687 sbp++, scc--; count++;
1692 (*decrypt_input)(-1);
1693 #endif /* ENCRYPTION */
1701 telrcv_state = TS_CR;
1717 telrcv_state = TS_WILL;
1721 telrcv_state = TS_WONT;
1725 telrcv_state = TS_DO;
1729 telrcv_state = TS_DONT;
1734 * We may have missed an urgent notification,
1735 * so make sure we flush whatever is in the
1738 printoption("RCVD", IAC, DM);
1741 SYNCHing = stilloob();
1747 telrcv_state = TS_SB;
1757 printoption("RCVD", IAC, c);
1760 telrcv_state = TS_DATA;
1764 printoption("RCVD", WILL, c);
1766 telrcv_state = TS_DATA;
1770 printoption("RCVD", WONT, c);
1772 telrcv_state = TS_DATA;
1776 printoption("RCVD", DO, c);
1778 if (c == TELOPT_NAWS) {
1780 } else if (c == TELOPT_LFLOW) {
1785 telrcv_state = TS_DATA;
1789 printoption("RCVD", DONT, c);
1792 setconnmode(0); /* set new tty mode (maybe) */
1793 telrcv_state = TS_DATA;
1798 telrcv_state = TS_SE;
1808 * This is an error. We only expect to get
1809 * "IAC IAC" or "IAC SE". Several things may
1810 * have happend. An IAC was not doubled, the
1811 * IAC SE was left off, or another option got
1812 * inserted into the suboption are all possibilities.
1813 * If we assume that the IAC was not doubled,
1814 * and really the IAC SE was left off, we could
1815 * get into an infinate loop here. So, instead,
1816 * we terminate the suboption, and process the
1817 * partial suboption if we can.
1824 printoption("In SUBOPTION processing, RCVD", IAC, c);
1825 suboption(); /* handle sub-option */
1826 telrcv_state = TS_IAC;
1830 telrcv_state = TS_SB;
1836 suboption(); /* handle sub-option */
1837 telrcv_state = TS_DATA;
1842 ring_consumed(&netiring, count);
1843 return returnValue||count;
1846 static int bol = 1, local = 0;
1854 command(0, "z\n", 2);
1865 int returnValue = 0;
1870 while (NETROOM() > 2) {
1876 ring_consumed(&ttyiring, count);
1880 tbp = ttyiring.consume;
1881 tcc = ring_full_consecutive(&ttyiring);
1886 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1887 if (rlogin != _POSIX_VDISABLE) {
1896 if (sc == '.' || c == termEofChar) {
1898 command(0, "close\n", 6);
1901 if (sc == termSuspChar) {
1903 command(0, "z\n", 2);
1907 command(0, tbp, tcc);
1921 if ((sc == '\n') || (sc == '\r'))
1923 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1925 * Double escape is a pass through of a single escape character.
1927 if (tcc && strip(*tbp) == escape) {
1933 command(0, (char *)tbp, tcc);
1942 #ifdef KLUDGELINEMODE
1943 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1944 if (tcc > 0 && strip(*tbp) == echoc) {
1945 tcc--; tbp++; count++;
1947 dontlecho = !dontlecho;
1948 settimer(echotoggle);
1955 if (MODE_LOCAL_CHARS(globalmode)) {
1956 if (TerminalSpecialChars(sc) == 0) {
1961 if (my_want_state_is_wont(TELOPT_BINARY)) {
1965 * If we are in CRMOD mode (\r ==> \n)
1966 * on our local machine, then probably
1967 * a newline (unix) is CRLF (TELNET).
1969 if (MODE_LOCAL_CHARS(globalmode)) {
1973 bol = flushline = 1;
1977 NET2ADD('\r', '\0');
1979 NET2ADD('\r', '\n');
1981 bol = flushline = 1;
1990 } else if (c == IAC) {
1997 ring_consumed(&ttyiring, count);
1998 return returnValue||count; /* Non-zero if we did anything */
2004 * Try to do something.
2006 * If we do something useful, return 1; else return 0.
2011 Scheduler(int block)
2013 /* One wants to be a bit careful about setting returnValue
2014 * to one, since a one implies we did some useful work,
2015 * and therefore probably won't be called to block next
2018 int netin, netout, netex, ttyin, ttyout;
2020 /* Decide which rings should be processed */
2022 netout = ring_full_count(&netoring) &&
2024 (my_want_state_is_wont(TELOPT_LINEMODE)
2025 #ifdef KLUDGELINEMODE
2026 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2029 my_want_state_is_will(TELOPT_BINARY));
2030 ttyout = ring_full_count(&ttyoring);
2032 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2034 netin = !ISend && ring_empty_count(&netiring);
2038 /* Call to system code to process rings */
2040 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2042 /* Now, look at the input rings, looking for work to do. */
2044 if (ring_full_count(&ttyiring)) {
2045 returnValue |= telsnd();
2048 if (ring_full_count(&netiring)) {
2049 returnValue |= telrcv();
2054 #ifdef AUTHENTICATION
2055 #define __unusedhere
2057 #define __unusedhere __unused
2060 * Select from tty and network...
2063 telnet(char *user __unusedhere)
2067 #ifdef AUTHENTICATION
2070 static char local_host[256] = { 0 };
2072 if (!local_host[0]) {
2073 gethostname(local_host, sizeof(local_host));
2074 local_host[sizeof(local_host)-1] = 0;
2076 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2077 auth_encrypt_user(user);
2082 #ifdef AUTHENTICATION
2084 send_will(TELOPT_AUTHENTICATION, 1);
2087 send_do(TELOPT_ENCRYPT, 1);
2088 send_will(TELOPT_ENCRYPT, 1);
2089 #endif /* ENCRYPTION */
2090 send_do(TELOPT_SGA, 1);
2091 send_will(TELOPT_TTYPE, 1);
2092 send_will(TELOPT_NAWS, 1);
2093 send_will(TELOPT_TSPEED, 1);
2094 send_will(TELOPT_LFLOW, 1);
2095 send_will(TELOPT_LINEMODE, 1);
2096 send_will(TELOPT_NEW_ENVIRON, 1);
2097 send_do(TELOPT_STATUS, 1);
2098 if (env_getvalue("DISPLAY"))
2099 send_will(TELOPT_XDISPLOC, 1);
2101 tel_enter_binary(eight);
2107 while ((schedValue = Scheduler(0)) != 0) {
2108 if (schedValue == -1) {
2114 if (Scheduler(1) == -1) {
2121 #if 0 /* XXX - this not being in is a bug */
2125 * Return the address of the next "item" in the TELNET data
2126 * stream. This will be the address of the next character if
2127 * the current address is a user data character, or it will
2128 * be the address of the character following the TELNET command
2129 * if the current address is a TELNET IAC ("I Am a Command")
2134 nextitem(char *current)
2136 if ((*current&0xff) != IAC) {
2139 switch (*(current+1)&0xff) {
2145 case SB: /* loop forever looking for the SE */
2147 char *look = current+2;
2150 if ((*look++&0xff) == IAC) {
2151 if ((*look++&0xff) == SE) {
2166 * We are about to do a TELNET SYNCH operation. Clear
2167 * the path to the network.
2169 * Things are a bit tricky since we may have sent the first
2170 * byte or so of a previous TELNET command into the network.
2171 * So, we have to scan the network buffer from the beginning
2172 * until we are up to where we want to be.
2174 * A side effect of what we do, just to keep things
2175 * simple, is to clear the urgent data pointer. The principal
2176 * caller should be setting the urgent data pointer AFTER calling
2187 * These routines add various telnet commands to the data stream.
2197 (void) ttyflush(1); /* Flush/drop output */
2198 /* do printoption AFTER flush, otherwise the output gets tossed... */
2199 printoption("SENT", DO, TELOPT_TM);
2206 printoption("SENT", IAC, AO);
2216 printoption("SENT", IAC, EL);
2223 printoption("SENT", IAC, EC);
2227 dosynch(char *ch __unused)
2229 netclear(); /* clear the path to the network */
2233 printoption("SENT", IAC, DM);
2237 int want_status_response = 0;
2240 get_status(char *ch __unused)
2242 unsigned char tmp[16];
2245 if (my_want_state_is_dont(TELOPT_STATUS)) {
2246 printf("Remote side does not support STATUS option\n");
2253 *cp++ = TELOPT_STATUS;
2254 *cp++ = TELQUAL_SEND;
2257 if (NETROOM() >= cp - tmp) {
2258 ring_supply_data(&netoring, tmp, cp-tmp);
2259 printsub('>', tmp+2, cp - tmp - 2);
2261 ++want_status_response;
2269 printoption("SENT", IAC, IP);
2282 NET2ADD(IAC, BREAK);
2283 printoption("SENT", IAC, BREAK);
2296 NET2ADD(IAC, ABORT);
2297 printoption("SENT", IAC, ABORT);
2311 printoption("SENT", IAC, SUSP);
2325 printoption("SENT", IAC, xEOF);
2332 printoption("SENT", IAC, AYT);
2336 * Send a window size update to the remote system.
2343 unsigned char tmp[16];
2346 if (my_state_is_wont(TELOPT_NAWS))
2349 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2350 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2352 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2360 *cp++ = TELOPT_NAWS;
2365 if (NETROOM() >= cp - tmp) {
2366 ring_supply_data(&netoring, tmp, cp-tmp);
2367 printsub('>', tmp+2, cp - tmp - 2);
2372 tel_enter_binary(int rw)
2375 send_do(TELOPT_BINARY, 1);
2377 send_will(TELOPT_BINARY, 1);
2381 tel_leave_binary(int rw)
2384 send_dont(TELOPT_BINARY, 1);
2386 send_wont(TELOPT_BINARY, 1);