ckdist(1): Sync with FreeBSD.
[dragonfly.git] / usr.bin / telnet / telnet.c
blob085676fd93409d7dcbbbb8d2e2395c4aa5dcc58f
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
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.
40 #include <ctype.h>
41 #include <curses.h>
42 #include <signal.h>
43 #include <stdlib.h>
44 #include <term.h>
45 #include <unistd.h>
46 #include <arpa/telnet.h>
48 #include "ring.h"
50 #include "defines.h"
51 #include "externs.h"
52 #include "types.h"
53 #include "general.h"
55 #ifdef AUTHENTICATION
56 #include <libtelnet/auth.h>
57 #endif
58 #ifdef ENCRYPTION
59 #include <libtelnet/encrypt.h>
60 #endif
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];
82 int
83 eight = 0,
84 autologin = 0, /* Autologin anyone? */
85 skiprc = 0,
86 connected,
87 showoptions,
88 ISend, /* trying to send network data in */
89 telnet_debug = 0,
90 crmod,
91 netdata, /* Print out network data flow */
92 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
93 telnetport,
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? */
104 globalmode,
105 doaddrlookup = 1, /* do a reverse address lookup? */
106 clienteof = 0;
108 char *prompt = NULL;
109 #ifdef ENCRYPTION
110 char line[16]; /* hack around breakage in sra.c :-( !! */
111 #endif
113 cc_t escape;
114 cc_t rlogin;
115 #ifdef KLUDGELINEMODE
116 cc_t echoc;
117 #endif
120 * Telnet receiver states for fsm
122 #define TS_DATA 0
123 #define TS_IAC 1
124 #define TS_WILL 2
125 #define TS_WONT 3
126 #define TS_DO 4
127 #define TS_DONT 5
128 #define TS_CR 6
129 #define TS_SB 7 /* sub-option collection */
130 #define TS_SE 8 /* looking for sub-option end */
132 static int telrcv_state;
133 #ifdef OLD_ENVIRON
134 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
135 #else
136 # define telopt_environ TELOPT_NEW_ENVIRON
137 #endif
139 jmp_buf toplevel;
140 jmp_buf peerdied;
142 int flushline;
143 int linemode;
145 #ifdef KLUDGELINEMODE
146 int kludgelinemode = 1;
147 #endif
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.
156 Clocks clocks;
159 * Initialize telnet environment.
162 void
163 init_telnet(void)
165 env_init();
167 SB_CLEAR();
168 ClearArray(options);
170 connected = ISend = localflow = donebinarytoggle = 0;
171 #ifdef AUTHENTICATION
172 #ifdef ENCRYPTION
173 auth_encrypt_connect(connected);
174 #endif
175 #endif
176 restartany = -1;
178 SYNCHing = 0;
180 /* Don't change NetTrace */
182 escape = CONTROL(']');
183 rlogin = _POSIX_VDISABLE;
184 #ifdef KLUDGELINEMODE
185 echoc = CONTROL('E');
186 #endif
188 flushline = 1;
189 telrcv_state = TS_DATA;
194 * These routines are in charge of sending option negotiations
195 * to the other side.
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.
201 void
202 send_do(int c, int init)
204 if (init) {
205 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
206 my_want_state_is_do(c))
207 return;
208 set_my_want_state_do(c);
209 do_dont_resp[c]++;
211 NET2ADD(IAC, DO);
212 NETADD(c);
213 printoption("SENT", DO, c);
216 void
217 send_dont(int c, int init)
219 if (init) {
220 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
221 my_want_state_is_dont(c))
222 return;
223 set_my_want_state_dont(c);
224 do_dont_resp[c]++;
226 NET2ADD(IAC, DONT);
227 NETADD(c);
228 printoption("SENT", DONT, c);
231 void
232 send_will(int c, int init)
234 if (init) {
235 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
236 my_want_state_is_will(c))
237 return;
238 set_my_want_state_will(c);
239 will_wont_resp[c]++;
241 NET2ADD(IAC, WILL);
242 NETADD(c);
243 printoption("SENT", WILL, c);
246 void
247 send_wont(int c, int init)
249 if (init) {
250 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
251 my_want_state_is_wont(c))
252 return;
253 set_my_want_state_wont(c);
254 will_wont_resp[c]++;
256 NET2ADD(IAC, WONT);
257 NETADD(c);
258 printoption("SENT", WONT, c);
261 void
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)) {
274 switch (option) {
276 case TELOPT_ECHO:
277 case TELOPT_BINARY:
278 case TELOPT_SGA:
279 settimer(modenegotiated);
280 /* FALL THROUGH */
281 case TELOPT_STATUS:
282 #ifdef AUTHENTICATION
283 case TELOPT_AUTHENTICATION:
284 #endif
285 #ifdef ENCRYPTION
286 case TELOPT_ENCRYPT:
287 #endif /* ENCRYPTION */
288 new_state_ok = 1;
289 break;
291 case TELOPT_TM:
292 if (flushout)
293 flushout = 0;
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:
303 default:
304 break;
307 if (new_state_ok) {
308 set_my_want_state_do(option);
309 send_do(option, 0);
310 setconnmode(0); /* possibly set new tty mode */
311 } else {
312 do_dont_resp[option]++;
313 send_dont(option, 0);
316 set_my_state_do(option);
317 #ifdef ENCRYPTION
318 if (option == TELOPT_ENCRYPT)
319 encrypt_send_support();
320 #endif /* ENCRYPTION */
323 void
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)) {
334 switch (option) {
336 #ifdef KLUDGELINEMODE
337 case TELOPT_SGA:
338 if (!kludgelinemode)
339 break;
340 /* FALL THROUGH */
341 #endif
342 case TELOPT_ECHO:
343 settimer(modenegotiated);
344 break;
346 case TELOPT_TM:
347 if (flushout)
348 flushout = 0;
349 set_my_want_state_dont(option);
350 set_my_state_dont(option);
351 return; /* Never reply to TM will's/wont's */
353 default:
354 break;
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.
364 if (flushout)
365 flushout = 0;
366 set_my_want_state_dont(option);
368 set_my_state_dont(option);
371 static void
372 dooption(int 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)) {
385 switch (option) {
387 case TELOPT_TM:
389 * Special case for TM. We send a WILL, but pretend
390 * we sent WONT.
392 send_will(option, 0);
393 set_my_want_state_wont(TELOPT_TM);
394 set_my_state_wont(TELOPT_TM);
395 return;
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 */
403 #ifdef ENCRYPTION
404 case TELOPT_ENCRYPT: /* encryption variable option */
405 #endif /* ENCRYPTION */
406 new_state_ok = 1;
407 break;
409 case TELOPT_NEW_ENVIRON: /* New environment variable option */
410 #ifdef OLD_ENVIRON
411 if (my_state_is_will(TELOPT_OLD_ENVIRON))
412 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
413 goto env_common;
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! */
417 env_common:
418 telopt_environ = option;
419 #endif
420 new_state_ok = 1;
421 break;
423 #ifdef AUTHENTICATION
424 case TELOPT_AUTHENTICATION:
425 if (autologin)
426 new_state_ok = 1;
427 break;
428 #endif
430 case TELOPT_XDISPLOC: /* X Display location */
431 if (env_getvalue("DISPLAY"))
432 new_state_ok = 1;
433 break;
435 case TELOPT_LINEMODE:
436 #ifdef KLUDGELINEMODE
437 kludgelinemode = 0;
438 send_do(TELOPT_SGA, 1);
439 #endif
440 set_my_want_state_will(TELOPT_LINEMODE);
441 send_will(option, 0);
442 set_my_state_will(TELOPT_LINEMODE);
443 slc_init();
444 return;
446 case TELOPT_ECHO: /* We're never going to echo... */
447 default:
448 break;
451 if (new_state_ok) {
452 set_my_want_state_will(option);
453 send_will(option, 0);
454 setconnmode(0); /* Set new tty mode */
455 } else {
456 will_wont_resp[option]++;
457 send_wont(option, 0);
459 } else {
461 * Handle options that need more things done after the
462 * other side has acknowledged the option.
464 switch (option) {
465 case TELOPT_LINEMODE:
466 #ifdef KLUDGELINEMODE
467 kludgelinemode = 0;
468 send_do(TELOPT_SGA, 1);
469 #endif
470 set_my_state_will(option);
471 slc_init();
472 send_do(TELOPT_SGA, 0);
473 return;
477 set_my_state_will(option);
480 static void
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)) {
491 switch (option) {
492 case TELOPT_LINEMODE:
493 linemode = 0; /* put us back to the default state */
494 break;
495 #ifdef OLD_ENVIRON
496 case TELOPT_NEW_ENVIRON:
498 * The new environ option wasn't recognized, try
499 * the old one.
501 send_will(TELOPT_OLD_ENVIRON, 1);
502 telopt_environ = TELOPT_OLD_ENVIRON;
503 break;
504 #endif
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 };
525 static const char **
526 mklist(char *buf, char *name)
528 int n;
529 char c, *cp, **argvp, *cp2, **argv, **avt;
531 if (name) {
532 if (strlen(name) > 40) {
533 name = NULL;
534 unknown[0] = name_unknown;
535 } else {
536 unknown[0] = name;
537 upcase(name);
539 } else
540 unknown[0] = name_unknown;
542 * Count up the number of names.
544 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
545 if (*cp == '|')
546 n++;
549 * Allocate an array to put the name pointers into
551 argv = (char **)malloc((n+3)*sizeof(char *));
552 if (argv == NULL)
553 return(unknown);
556 * Fill up the array of pointers to names.
558 *argv = NULL;
559 argvp = argv+1;
560 n = 0;
561 for (cp = cp2 = buf; (c = *cp); cp++) {
562 if (c == '|' || c == ':') {
563 *cp++ = '\0';
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))
574 *argv = cp2;
575 else if (is_unique(cp2, argv+1, argvp))
576 *argvp++ = cp2;
577 if (c == ':')
578 break;
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) == '|')
585 cp++;
586 cp2 = cp;
587 n = 0;
590 * Skip entries with spaces or non-ascii values.
591 * Convert lower case letters to upper case.
593 if ((c == ' ') || !isascii(c))
594 n = 1;
595 else if (islower(c))
596 *cp = toupper(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)) {
605 --argvp;
606 for (avt = &argv[1]; avt < argvp; avt++)
607 *avt = *(avt+1);
608 *argvp++ = buf;
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.
616 cp = *(argvp-1);
617 *argvp++ = cp;
618 *argvp = NULL;
620 if (*argv == NULL) {
621 if (name)
622 *argv = name;
623 else {
624 --argvp;
625 for (avt = argv; avt < argvp; avt++)
626 *avt = *(avt+1);
629 if (*argv)
630 return((const char **)argv);
631 else
632 return(unknown);
635 static int
636 is_unique(char *name, char **as, char **ae)
638 char **ap;
639 int n;
641 n = strlen(name) + 1;
642 for (ap = as; ap < ae; ap++)
643 if (strncasecmp(*ap, name, n) == 0)
644 return(0);
645 return (1);
648 #ifdef TERMCAP
649 char termbuf[1024];
651 /*ARGSUSED*/
652 static int
653 setupterm(char *tname, int fd, int *errp)
655 if (tgetent(termbuf, tname) == 1) {
656 termbuf[1023] = '\0';
657 if (errp)
658 *errp = 1;
659 return(0);
661 if (errp)
662 *errp = 0;
663 return(-1);
665 #else
666 #define termbuf ttytype
667 extern char ttytype[];
668 #endif
670 int resettermname = 1;
672 static const char *
673 gettermname(void)
675 char *tname;
676 static const char **tnamep = NULL;
677 static const char **next;
678 int err;
680 if (resettermname) {
681 resettermname = 0;
682 if (tnamep && tnamep != unknown)
683 free(tnamep);
684 if ((tname = env_getvalue("TERM")) &&
685 (setupterm(tname, 1, &err) == 0)) {
686 tnamep = mklist(termbuf, tname);
687 } else {
688 if (tname && (strlen(tname) <= 40)) {
689 unknown[0] = tname;
690 upcase(tname);
691 } else
692 unknown[0] = name_unknown;
693 tnamep = unknown;
695 next = tnamep;
697 if (*next == NULL)
698 next = tnamep;
699 return(*next++);
702 * suboption()
704 * Look at the sub-option buffer, and try to be helpful to the other
705 * side.
707 * Currently we recognize:
709 * Terminal type, send request.
710 * Terminal speed (send request).
711 * Local flow control (is request).
712 * Linemode
715 static void
716 suboption(void)
718 unsigned char subchar;
720 printsub('<', subbuffer, SB_LEN()+2);
721 switch (subchar = SB_GET()) {
722 case TELOPT_TTYPE:
723 if (my_want_state_is_wont(TELOPT_TTYPE))
724 return;
725 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
726 return;
727 } else {
728 const char *name;
729 unsigned char temp[50];
730 int len;
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);
739 } else {
740 ExitString("No room in buffer for terminal type.\n", 1);
741 /*NOTREACHED*/
744 break;
745 case TELOPT_TSPEED:
746 if (my_want_state_is_wont(TELOPT_TSPEED))
747 return;
748 if (SB_EOF())
749 return;
750 if (SB_GET() == TELQUAL_SEND) {
751 long ospeed, ispeed;
752 unsigned char temp[50];
753 int len;
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");
767 break;
768 case TELOPT_LFLOW:
769 if (my_want_state_is_wont(TELOPT_LFLOW))
770 return;
771 if (SB_EOF())
772 return;
773 switch(SB_GET()) {
774 case LFLOW_RESTART_ANY:
775 restartany = 1;
776 break;
777 case LFLOW_RESTART_XON:
778 restartany = 0;
779 break;
780 case LFLOW_ON:
781 localflow = 1;
782 break;
783 case LFLOW_OFF:
784 localflow = 0;
785 break;
786 default:
787 return;
789 setcommandmode();
790 setconnmode(0);
791 break;
793 case TELOPT_LINEMODE:
794 if (my_want_state_is_wont(TELOPT_LINEMODE))
795 return;
796 if (SB_EOF())
797 return;
798 switch (SB_GET()) {
799 case WILL:
800 lm_will(subpointer, SB_LEN());
801 break;
802 case WONT:
803 lm_wont(subpointer, SB_LEN());
804 break;
805 case DO:
806 lm_do(subpointer, SB_LEN());
807 break;
808 case DONT:
809 lm_dont(subpointer, SB_LEN());
810 break;
811 case LM_SLC:
812 slc(subpointer, SB_LEN());
813 break;
814 case LM_MODE:
815 lm_mode(subpointer, SB_LEN(), 0);
816 break;
817 default:
818 break;
820 break;
822 #ifdef OLD_ENVIRON
823 case TELOPT_OLD_ENVIRON:
824 #endif
825 case TELOPT_NEW_ENVIRON:
826 if (SB_EOF())
827 return;
828 switch(SB_PEEK()) {
829 case TELQUAL_IS:
830 case TELQUAL_INFO:
831 if (my_want_state_is_dont(subchar))
832 return;
833 break;
834 case TELQUAL_SEND:
835 if (my_want_state_is_wont(subchar)) {
836 return;
838 break;
839 default:
840 return;
842 env_opt(subpointer, SB_LEN());
843 break;
845 case TELOPT_XDISPLOC:
846 if (my_want_state_is_wont(TELOPT_XDISPLOC))
847 return;
848 if (SB_EOF())
849 return;
850 if (SB_GET() == TELQUAL_SEND) {
851 unsigned char temp[50], *dp;
852 int len;
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);
861 break;
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");
873 break;
875 #ifdef AUTHENTICATION
876 case TELOPT_AUTHENTICATION: {
877 if (!autologin)
878 break;
879 if (SB_EOF())
880 return;
881 switch(SB_GET()) {
882 case TELQUAL_IS:
883 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
884 return;
885 auth_is(subpointer, SB_LEN());
886 break;
887 case TELQUAL_SEND:
888 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
889 return;
890 auth_send(subpointer, SB_LEN());
891 break;
892 case TELQUAL_REPLY:
893 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
894 return;
895 auth_reply(subpointer, SB_LEN());
896 break;
897 case TELQUAL_NAME:
898 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
899 return;
900 auth_name(subpointer, SB_LEN());
901 break;
904 break;
905 #endif
906 #ifdef ENCRYPTION
907 case TELOPT_ENCRYPT:
908 if (SB_EOF())
909 return;
910 switch(SB_GET()) {
911 case ENCRYPT_START:
912 if (my_want_state_is_dont(TELOPT_ENCRYPT))
913 return;
914 encrypt_start(subpointer, SB_LEN());
915 break;
916 case ENCRYPT_END:
917 if (my_want_state_is_dont(TELOPT_ENCRYPT))
918 return;
919 encrypt_end();
920 break;
921 case ENCRYPT_SUPPORT:
922 if (my_want_state_is_wont(TELOPT_ENCRYPT))
923 return;
924 encrypt_support(subpointer, SB_LEN());
925 break;
926 case ENCRYPT_REQSTART:
927 if (my_want_state_is_wont(TELOPT_ENCRYPT))
928 return;
929 encrypt_request_start(subpointer, SB_LEN());
930 break;
931 case ENCRYPT_REQEND:
932 if (my_want_state_is_wont(TELOPT_ENCRYPT))
933 return;
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
938 * anyhow.
940 encrypt_request_end();
941 break;
942 case ENCRYPT_IS:
943 if (my_want_state_is_dont(TELOPT_ENCRYPT))
944 return;
945 encrypt_is(subpointer, SB_LEN());
946 break;
947 case ENCRYPT_REPLY:
948 if (my_want_state_is_wont(TELOPT_ENCRYPT))
949 return;
950 encrypt_reply(subpointer, SB_LEN());
951 break;
952 case ENCRYPT_ENC_KEYID:
953 if (my_want_state_is_dont(TELOPT_ENCRYPT))
954 return;
955 encrypt_enc_keyid(subpointer, SB_LEN());
956 break;
957 case ENCRYPT_DEC_KEYID:
958 if (my_want_state_is_wont(TELOPT_ENCRYPT))
959 return;
960 encrypt_dec_keyid(subpointer, SB_LEN());
961 break;
962 default:
963 break;
965 break;
966 #endif /* ENCRYPTION */
967 default:
968 break;
972 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
974 void
975 lm_will(unsigned char *cmd, int len)
977 if (len < 1) {
978 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
979 return;
981 switch(cmd[0]) {
982 case LM_FORWARDMASK: /* We shouldn't ever get this... */
983 default:
984 str_lm[3] = DONT;
985 str_lm[4] = cmd[0];
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");
991 break;
995 void
996 lm_wont(unsigned char *cmd, int len)
998 if (len < 1) {
999 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1000 return;
1002 switch(cmd[0]) {
1003 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1004 default:
1005 /* We are always DONT, so don't respond */
1006 return;
1010 void
1011 lm_do(unsigned char *cmd, int len)
1013 if (len < 1) {
1014 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1015 return;
1017 switch(cmd[0]) {
1018 case LM_FORWARDMASK:
1019 default:
1020 str_lm[3] = WONT;
1021 str_lm[4] = cmd[0];
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");
1027 break;
1031 void
1032 lm_dont(unsigned char *cmd, int len)
1034 if (len < 1) {
1035 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1036 return;
1038 switch(cmd[0]) {
1039 case LM_FORWARDMASK:
1040 default:
1041 /* we are always WONT, so don't respond */
1042 break;
1046 static unsigned char str_lm_mode[] = {
1047 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1050 void
1051 lm_mode(unsigned char *cmd, int len, int init)
1053 if (len != 1)
1054 return;
1055 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1056 return;
1057 if (*cmd&MODE_ACK)
1058 return;
1059 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1060 str_lm_mode[4] = linemode;
1061 if (!init)
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 */
1074 * slc()
1075 * Handle special character suboption of LINEMODE.
1078 struct spc {
1079 cc_t val;
1080 cc_t *valp;
1081 char flags; /* Current flags & level */
1082 char mylevel; /* Maximum level & flags */
1083 } spc_data[NSLC+1];
1085 #define SLC_IMPORT 0
1086 #define SLC_EXPORT 1
1087 #define SLC_RVALUE 2
1088 static int slc_mode = SLC_EXPORT;
1090 void
1091 slc_init(void)
1093 struct spc *spcp;
1095 localchars = 1;
1096 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1097 spcp->val = 0;
1098 spcp->valp = NULL;
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; \
1107 } else { \
1108 spcp->val = 0; \
1109 spcp->mylevel = SLC_DEFAULT; \
1113 initfunc(SLC_SYNCH, 0);
1114 /* No BRK */
1115 initfunc(SLC_AO, 0);
1116 initfunc(SLC_AYT, 0);
1117 /* No EOR */
1118 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1119 initfunc(SLC_EOF, 0);
1120 #ifndef SYSV_TERMIO
1121 initfunc(SLC_SUSP, SLC_FLUSHIN);
1122 #endif
1123 initfunc(SLC_EC, 0);
1124 initfunc(SLC_EL, 0);
1125 #ifndef SYSV_TERMIO
1126 initfunc(SLC_EW, 0);
1127 initfunc(SLC_RP, 0);
1128 initfunc(SLC_LNEXT, 0);
1129 #endif
1130 initfunc(SLC_XON, 0);
1131 initfunc(SLC_XOFF, 0);
1132 #ifdef SYSV_TERMIO
1133 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1134 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1135 #endif
1136 initfunc(SLC_FORW1, 0);
1137 #ifdef USE_TERMIO
1138 initfunc(SLC_FORW2, 0);
1139 /* No FORW2 */
1140 #endif
1142 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1143 #undef initfunc
1145 if (slc_mode == SLC_EXPORT)
1146 slc_export();
1147 else
1148 slc_import(1);
1152 void
1153 slcstate(void)
1155 printf("Special characters are %s values\n",
1156 slc_mode == SLC_IMPORT ? "remote default" :
1157 slc_mode == SLC_EXPORT ? "local" :
1158 "remote");
1161 void
1162 slc_mode_export(void)
1164 slc_mode = SLC_EXPORT;
1165 if (my_state_is_will(TELOPT_LINEMODE))
1166 slc_export();
1169 void
1170 slc_mode_import(int def)
1172 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1173 if (my_state_is_will(TELOPT_LINEMODE))
1174 slc_import(def);
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
1184 void
1185 slc_import(int def)
1187 if (NETROOM() > (int)sizeof(slc_import_val)) {
1188 if (def) {
1189 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1190 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1191 } else {
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");
1199 void
1200 slc_export(void)
1202 struct spc *spcp;
1204 TerminalDefaultChars();
1206 slc_start_reply();
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;
1211 else
1212 spcp->flags = spcp->mylevel;
1213 if (spcp->valp)
1214 spcp->val = *spcp->valp;
1215 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1218 slc_end_reply();
1219 (void)slc_update();
1220 setconnmode(1); /* Make sure the character values are set */
1223 void
1224 slc(unsigned char *cp, int len)
1226 struct spc *spcp;
1227 int func,level;
1229 slc_start_reply();
1231 for (; len >= 3; len -=3, cp +=3) {
1233 func = cp[SLC_FUNC];
1235 if (func == 0) {
1237 * Client side: always ignore 0 function.
1239 continue;
1241 if (func > NSLC) {
1242 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1243 slc_add_reply(func, SLC_NOSUPPORT, 0);
1244 continue;
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))) {
1253 continue;
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 */
1269 continue;
1272 level &= ~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;
1281 else
1282 spcp->flags = SLC_NOSUPPORT;
1284 slc_add_reply(func, spcp->flags, spcp->val);
1286 slc_end_reply();
1287 if (slc_update())
1288 setconnmode(1); /* set the new character values */
1291 void
1292 slc_check(void)
1294 struct spc *spcp;
1296 slc_start_reply();
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;
1302 else
1303 spcp->flags = spcp->mylevel;
1304 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1307 slc_end_reply();
1308 setconnmode(1);
1311 unsigned char slc_reply[128];
1312 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1313 unsigned char *slc_replyp;
1315 void
1316 slc_start_reply(void)
1318 slc_replyp = slc_reply;
1319 *slc_replyp++ = IAC;
1320 *slc_replyp++ = SB;
1321 *slc_replyp++ = TELOPT_LINEMODE;
1322 *slc_replyp++ = LM_SLC;
1325 void
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
1332 * items.
1334 if (&slc_replyp[6+2] > slc_reply_eom)
1335 return;
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;
1344 void
1345 slc_end_reply(void)
1347 int len;
1348 /* The end of negotiation command requires 2 bytes. */
1349 if (&slc_replyp[2] > slc_reply_eom)
1350 return;
1351 *slc_replyp++ = IAC;
1352 *slc_replyp++ = SE;
1353 len = slc_replyp - slc_reply;
1354 if (len <= 6)
1355 return;
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");
1364 slc_update(void)
1366 struct spc *spcp;
1367 int need_update = 0;
1369 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1370 if (!(spcp->flags&SLC_ACK))
1371 continue;
1372 spcp->flags &= ~SLC_ACK;
1373 if (spcp->valp && (*spcp->valp != spcp->val)) {
1374 *spcp->valp = spcp->val;
1375 need_update = 1;
1378 return(need_update);
1381 #ifdef OLD_ENVIRON
1382 # ifdef ENV_HACK
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.
1392 int env_auto = 1;
1393 int old_env_var = OLD_ENV_VAR;
1394 int old_env_value = OLD_ENV_VALUE;
1395 # else
1396 # define old_env_var OLD_ENV_VAR
1397 # define old_env_value OLD_ENV_VALUE
1398 # endif
1399 #endif
1401 void
1402 env_opt(unsigned char *buf, int len)
1404 unsigned char *ep = NULL, *epc = NULL;
1405 int i;
1407 switch(buf[0]&0xff) {
1408 case TELQUAL_SEND:
1409 env_opt_start();
1410 if (len == 1) {
1411 env_opt_add(NULL);
1412 } else for (i = 1; i < len; i++) {
1413 switch (buf[i]&0xff) {
1414 #ifdef OLD_ENVIRON
1415 case OLD_ENV_VAR:
1416 # ifdef ENV_HACK
1417 if (telopt_environ == TELOPT_OLD_ENVIRON
1418 && env_auto) {
1419 /* Server has the same definitions */
1420 old_env_var = OLD_ENV_VAR;
1421 old_env_value = OLD_ENV_VALUE;
1423 /* FALL THROUGH */
1424 # endif
1425 case 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...
1431 /* FALL THROUGH */
1432 #else
1433 case NEW_ENV_VAR:
1434 #endif
1435 case ENV_USERVAR:
1436 if (ep) {
1437 *epc = 0;
1438 env_opt_add(ep);
1440 ep = epc = &buf[i+1];
1441 break;
1442 case ENV_ESC:
1443 i++;
1444 /*FALL THROUGH*/
1445 default:
1446 if (epc)
1447 *epc++ = buf[i];
1448 break;
1451 if (ep) {
1452 *epc = 0;
1453 env_opt_add(ep);
1455 env_opt_end(1);
1456 break;
1458 case TELQUAL_IS:
1459 case TELQUAL_INFO:
1460 /* Ignore for now. We shouldn't get it anyway. */
1461 break;
1463 default:
1464 break;
1468 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1469 unsigned char *opt_reply = NULL;
1470 unsigned char *opt_replyp;
1471 unsigned char *opt_replyend;
1473 void
1474 env_opt_start(void)
1476 if (opt_reply)
1477 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1478 else
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;
1483 return;
1485 opt_replyp = opt_reply;
1486 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1487 *opt_replyp++ = IAC;
1488 *opt_replyp++ = SB;
1489 *opt_replyp++ = telopt_environ;
1490 *opt_replyp++ = TELQUAL_IS;
1493 void
1494 env_opt_start_info(void)
1496 env_opt_start();
1497 if (opt_replyp)
1498 opt_replyp[-1] = TELQUAL_INFO;
1501 void
1502 env_opt_add(unsigned char *ep)
1504 unsigned char *vp, c;
1506 if (opt_reply == NULL) /*XXX*/
1507 return; /*XXX*/
1509 if (ep == NULL || *ep == '\0') {
1510 /* Send user defined variables first. */
1511 env_default(1, 0);
1512 while ((ep = env_default(0, 0)))
1513 env_opt_add(ep);
1515 /* Now add the list of well know variables. */
1516 env_default(1, 1);
1517 while ((ep = env_default(0, 1)))
1518 env_opt_add(ep);
1519 return;
1521 vp = env_getvalue(ep);
1522 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1523 2 * strlen((char *)ep) + 6 > opt_replyend)
1526 int len;
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;
1533 return;
1535 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1536 opt_replyend = opt_reply + len;
1538 if (opt_welldefined(ep))
1539 #ifdef OLD_ENVIRON
1540 if (telopt_environ == TELOPT_OLD_ENVIRON)
1541 *opt_replyp++ = old_env_var;
1542 else
1543 #endif
1544 *opt_replyp++ = NEW_ENV_VAR;
1545 else
1546 *opt_replyp++ = ENV_USERVAR;
1547 for (;;) {
1548 while ((c = *ep++)) {
1549 if (opt_replyp + (2 + 2) > opt_replyend)
1550 return;
1551 switch(c&0xff) {
1552 case IAC:
1553 *opt_replyp++ = IAC;
1554 break;
1555 case NEW_ENV_VAR:
1556 case NEW_ENV_VALUE:
1557 case ENV_ESC:
1558 case ENV_USERVAR:
1559 *opt_replyp++ = ENV_ESC;
1560 break;
1562 *opt_replyp++ = c;
1564 if ((ep = vp)) {
1565 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1566 return;
1567 #ifdef OLD_ENVIRON
1568 if (telopt_environ == TELOPT_OLD_ENVIRON)
1569 *opt_replyp++ = old_env_value;
1570 else
1571 #endif
1572 *opt_replyp++ = NEW_ENV_VALUE;
1573 vp = NULL;
1574 } else
1575 break;
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))
1588 return(1);
1589 return(0);
1592 void
1593 env_opt_end(int emptyok)
1595 int len;
1597 if (opt_replyp + 2 > opt_replyend)
1598 return;
1599 len = opt_replyp + 2 - opt_reply;
1600 if (emptyok || len > 6) {
1601 *opt_replyp++ = IAC;
1602 *opt_replyp++ = SE;
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");
1609 if (opt_reply) {
1610 free(opt_reply);
1611 opt_reply = opt_replyp = opt_replyend = NULL;
1618 telrcv(void)
1620 int c;
1621 int scc;
1622 unsigned char *sbp;
1623 int count;
1624 int returnValue = 0;
1626 scc = 0;
1627 count = 0;
1628 while (TTYROOM() > 2) {
1629 if (scc == 0) {
1630 if (count) {
1631 ring_consumed(&netiring, count);
1632 returnValue = 1;
1633 count = 0;
1635 sbp = netiring.consume;
1636 scc = ring_full_consecutive(&netiring);
1637 if (scc == 0) {
1638 /* No more data coming in */
1639 break;
1643 c = *sbp++ & 0xff, scc--; count++;
1644 #ifdef ENCRYPTION
1645 if (decrypt_input)
1646 c = (*decrypt_input)(c);
1647 #endif /* ENCRYPTION */
1649 switch (telrcv_state) {
1651 case TS_CR:
1652 telrcv_state = TS_DATA;
1653 if (c == '\0') {
1654 break; /* Ignore \0 after CR */
1656 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1657 TTYADD(c);
1658 break;
1660 /* Else, fall through */
1662 case TS_DATA:
1663 if (c == IAC) {
1664 telrcv_state = TS_IAC;
1665 break;
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)) {
1675 if (scc > 0) {
1676 c = *sbp&0xff;
1677 #ifdef ENCRYPTION
1678 if (decrypt_input)
1679 c = (*decrypt_input)(c);
1680 #endif /* ENCRYPTION */
1681 if (c == 0) {
1682 sbp++, scc--; count++;
1683 /* a "true" CR */
1684 TTYADD('\r');
1685 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1686 (c == '\n')) {
1687 sbp++, scc--; count++;
1688 TTYADD('\n');
1689 } else {
1690 #ifdef ENCRYPTION
1691 if (decrypt_input)
1692 (*decrypt_input)(-1);
1693 #endif /* ENCRYPTION */
1695 TTYADD('\r');
1696 if (crmod) {
1697 TTYADD('\n');
1700 } else {
1701 telrcv_state = TS_CR;
1702 TTYADD('\r');
1703 if (crmod) {
1704 TTYADD('\n');
1707 } else {
1708 TTYADD(c);
1710 continue;
1712 case TS_IAC:
1713 process_iac:
1714 switch (c) {
1716 case WILL:
1717 telrcv_state = TS_WILL;
1718 continue;
1720 case WONT:
1721 telrcv_state = TS_WONT;
1722 continue;
1724 case DO:
1725 telrcv_state = TS_DO;
1726 continue;
1728 case DONT:
1729 telrcv_state = TS_DONT;
1730 continue;
1732 case DM:
1734 * We may have missed an urgent notification,
1735 * so make sure we flush whatever is in the
1736 * buffer currently.
1738 printoption("RCVD", IAC, DM);
1739 SYNCHing = 1;
1740 (void) ttyflush(1);
1741 SYNCHing = stilloob();
1742 settimer(gotDM);
1743 break;
1745 case SB:
1746 SB_CLEAR();
1747 telrcv_state = TS_SB;
1748 continue;
1750 case IAC:
1751 TTYADD(IAC);
1752 break;
1754 case NOP:
1755 case GA:
1756 default:
1757 printoption("RCVD", IAC, c);
1758 break;
1760 telrcv_state = TS_DATA;
1761 continue;
1763 case TS_WILL:
1764 printoption("RCVD", WILL, c);
1765 willoption(c);
1766 telrcv_state = TS_DATA;
1767 continue;
1769 case TS_WONT:
1770 printoption("RCVD", WONT, c);
1771 wontoption(c);
1772 telrcv_state = TS_DATA;
1773 continue;
1775 case TS_DO:
1776 printoption("RCVD", DO, c);
1777 dooption(c);
1778 if (c == TELOPT_NAWS) {
1779 sendnaws();
1780 } else if (c == TELOPT_LFLOW) {
1781 localflow = 1;
1782 setcommandmode();
1783 setconnmode(0);
1785 telrcv_state = TS_DATA;
1786 continue;
1788 case TS_DONT:
1789 printoption("RCVD", DONT, c);
1790 dontoption(c);
1791 flushline = 1;
1792 setconnmode(0); /* set new tty mode (maybe) */
1793 telrcv_state = TS_DATA;
1794 continue;
1796 case TS_SB:
1797 if (c == IAC) {
1798 telrcv_state = TS_SE;
1799 } else {
1800 SB_ACCUM(c);
1802 continue;
1804 case TS_SE:
1805 if (c != SE) {
1806 if (c != IAC) {
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.
1819 SB_ACCUM(IAC);
1820 SB_ACCUM(c);
1821 subpointer -= 2;
1822 SB_TERM();
1824 printoption("In SUBOPTION processing, RCVD", IAC, c);
1825 suboption(); /* handle sub-option */
1826 telrcv_state = TS_IAC;
1827 goto process_iac;
1829 SB_ACCUM(c);
1830 telrcv_state = TS_SB;
1831 } else {
1832 SB_ACCUM(IAC);
1833 SB_ACCUM(SE);
1834 subpointer -= 2;
1835 SB_TERM();
1836 suboption(); /* handle sub-option */
1837 telrcv_state = TS_DATA;
1841 if (count)
1842 ring_consumed(&netiring, count);
1843 return returnValue||count;
1846 static int bol = 1, local = 0;
1849 rlogin_susp(void)
1851 if (local) {
1852 local = 0;
1853 bol = 1;
1854 command(0, "z\n", 2);
1855 return(1);
1857 return(0);
1860 static int
1861 telsnd(void)
1863 int tcc;
1864 int count;
1865 int returnValue = 0;
1866 unsigned char *tbp;
1868 tcc = 0;
1869 count = 0;
1870 while (NETROOM() > 2) {
1871 int sc;
1872 int c;
1874 if (tcc == 0) {
1875 if (count) {
1876 ring_consumed(&ttyiring, count);
1877 returnValue = 1;
1878 count = 0;
1880 tbp = ttyiring.consume;
1881 tcc = ring_full_consecutive(&ttyiring);
1882 if (tcc == 0) {
1883 break;
1886 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1887 if (rlogin != _POSIX_VDISABLE) {
1888 if (bol) {
1889 bol = 0;
1890 if (sc == rlogin) {
1891 local = 1;
1892 continue;
1894 } else if (local) {
1895 local = 0;
1896 if (sc == '.' || c == termEofChar) {
1897 bol = 1;
1898 command(0, "close\n", 6);
1899 continue;
1901 if (sc == termSuspChar) {
1902 bol = 1;
1903 command(0, "z\n", 2);
1904 continue;
1906 if (sc == escape) {
1907 command(0, tbp, tcc);
1908 bol = 1;
1909 count += tcc;
1910 tcc = 0;
1911 flushline = 1;
1912 break;
1914 if (sc != rlogin) {
1915 ++tcc;
1916 --tbp;
1917 --count;
1918 c = sc = rlogin;
1921 if ((sc == '\n') || (sc == '\r'))
1922 bol = 1;
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) {
1928 tbp++;
1929 tcc--;
1930 count++;
1931 bol = 0;
1932 } else {
1933 command(0, (char *)tbp, tcc);
1934 bol = 1;
1935 count += tcc;
1936 tcc = 0;
1937 flushline = 1;
1938 break;
1940 } else
1941 bol = 0;
1942 #ifdef KLUDGELINEMODE
1943 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1944 if (tcc > 0 && strip(*tbp) == echoc) {
1945 tcc--; tbp++; count++;
1946 } else {
1947 dontlecho = !dontlecho;
1948 settimer(echotoggle);
1949 setconnmode(0);
1950 flushline = 1;
1951 break;
1954 #endif
1955 if (MODE_LOCAL_CHARS(globalmode)) {
1956 if (TerminalSpecialChars(sc) == 0) {
1957 bol = 1;
1958 break;
1961 if (my_want_state_is_wont(TELOPT_BINARY)) {
1962 switch (c) {
1963 case '\n':
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)) {
1970 NETADD('\r');
1972 NETADD('\n');
1973 bol = flushline = 1;
1974 break;
1975 case '\r':
1976 if (!crlf) {
1977 NET2ADD('\r', '\0');
1978 } else {
1979 NET2ADD('\r', '\n');
1981 bol = flushline = 1;
1982 break;
1983 case IAC:
1984 NET2ADD(IAC, IAC);
1985 break;
1986 default:
1987 NETADD(c);
1988 break;
1990 } else if (c == IAC) {
1991 NET2ADD(IAC, IAC);
1992 } else {
1993 NETADD(c);
1996 if (count)
1997 ring_consumed(&ttyiring, count);
1998 return returnValue||count; /* Non-zero if we did anything */
2002 * Scheduler()
2004 * Try to do something.
2006 * If we do something useful, return 1; else return 0.
2010 static int
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
2017 int returnValue;
2018 int netin, netout, netex, ttyin, ttyout;
2020 /* Decide which rings should be processed */
2022 netout = ring_full_count(&netoring) &&
2023 (flushline ||
2024 (my_want_state_is_wont(TELOPT_LINEMODE)
2025 #ifdef KLUDGELINEMODE
2026 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2027 #endif
2028 ) ||
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);
2036 netex = !SYNCHing;
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();
2051 return returnValue;
2054 #ifdef AUTHENTICATION
2055 #define __unusedhere
2056 #else
2057 #define __unusedhere __unused
2058 #endif
2060 * Select from tty and network...
2062 void
2063 telnet(char *user __unusedhere)
2065 sys_telnet_init();
2067 #ifdef AUTHENTICATION
2068 #ifdef ENCRYPTION
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);
2079 #endif
2080 #endif
2081 if (telnetport) {
2082 #ifdef AUTHENTICATION
2083 if (autologin)
2084 send_will(TELOPT_AUTHENTICATION, 1);
2085 #endif
2086 #ifdef ENCRYPTION
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);
2100 if (eight)
2101 tel_enter_binary(eight);
2104 for (;;) {
2105 int schedValue;
2107 while ((schedValue = Scheduler(0)) != 0) {
2108 if (schedValue == -1) {
2109 setcommandmode();
2110 return;
2114 if (Scheduler(1) == -1) {
2115 setcommandmode();
2116 return;
2121 #if 0 /* XXX - this not being in is a bug */
2123 * nextitem()
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")
2130 * character.
2133 static char *
2134 nextitem(char *current)
2136 if ((*current&0xff) != IAC) {
2137 return current+1;
2139 switch (*(current+1)&0xff) {
2140 case DO:
2141 case DONT:
2142 case WILL:
2143 case WONT:
2144 return current+3;
2145 case SB: /* loop forever looking for the SE */
2147 char *look = current+2;
2149 for (;;) {
2150 if ((*look++&0xff) == IAC) {
2151 if ((*look++&0xff) == SE) {
2152 return look;
2157 default:
2158 return current+2;
2161 #endif /* 0 */
2164 * netclear()
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
2177 * us in any case.
2180 static void
2181 netclear(void)
2183 /* Deleted */
2187 * These routines add various telnet commands to the data stream.
2190 static void
2191 doflush(void)
2193 NET2ADD(IAC, DO);
2194 NETADD(TELOPT_TM);
2195 flushline = 1;
2196 flushout = 1;
2197 (void) ttyflush(1); /* Flush/drop output */
2198 /* do printoption AFTER flush, otherwise the output gets tossed... */
2199 printoption("SENT", DO, TELOPT_TM);
2202 void
2203 xmitAO(void)
2205 NET2ADD(IAC, AO);
2206 printoption("SENT", IAC, AO);
2207 if (autoflush) {
2208 doflush();
2212 void
2213 xmitEL(void)
2215 NET2ADD(IAC, EL);
2216 printoption("SENT", IAC, EL);
2219 void
2220 xmitEC(void)
2222 NET2ADD(IAC, EC);
2223 printoption("SENT", IAC, EC);
2227 dosynch(char *ch __unused)
2229 netclear(); /* clear the path to the network */
2230 NETADD(IAC);
2231 setneturg();
2232 NETADD(DM);
2233 printoption("SENT", IAC, DM);
2234 return 1;
2237 int want_status_response = 0;
2240 get_status(char *ch __unused)
2242 unsigned char tmp[16];
2243 unsigned char *cp;
2245 if (my_want_state_is_dont(TELOPT_STATUS)) {
2246 printf("Remote side does not support STATUS option\n");
2247 return 0;
2249 cp = tmp;
2251 *cp++ = IAC;
2252 *cp++ = SB;
2253 *cp++ = TELOPT_STATUS;
2254 *cp++ = TELQUAL_SEND;
2255 *cp++ = IAC;
2256 *cp++ = SE;
2257 if (NETROOM() >= cp - tmp) {
2258 ring_supply_data(&netoring, tmp, cp-tmp);
2259 printsub('>', tmp+2, cp - tmp - 2);
2261 ++want_status_response;
2262 return 1;
2265 void
2266 intp(void)
2268 NET2ADD(IAC, IP);
2269 printoption("SENT", IAC, IP);
2270 flushline = 1;
2271 if (autoflush) {
2272 doflush();
2274 if (autosynch) {
2275 dosynch(NULL);
2279 void
2280 sendbrk(void)
2282 NET2ADD(IAC, BREAK);
2283 printoption("SENT", IAC, BREAK);
2284 flushline = 1;
2285 if (autoflush) {
2286 doflush();
2288 if (autosynch) {
2289 dosynch(NULL);
2293 void
2294 sendabort(void)
2296 NET2ADD(IAC, ABORT);
2297 printoption("SENT", IAC, ABORT);
2298 flushline = 1;
2299 if (autoflush) {
2300 doflush();
2302 if (autosynch) {
2303 dosynch(NULL);
2307 void
2308 sendsusp(void)
2310 NET2ADD(IAC, SUSP);
2311 printoption("SENT", IAC, SUSP);
2312 flushline = 1;
2313 if (autoflush) {
2314 doflush();
2316 if (autosynch) {
2317 dosynch(NULL);
2321 void
2322 sendeof(void)
2324 NET2ADD(IAC, xEOF);
2325 printoption("SENT", IAC, xEOF);
2328 void
2329 sendayt(void)
2331 NET2ADD(IAC, AYT);
2332 printoption("SENT", IAC, AYT);
2336 * Send a window size update to the remote system.
2339 void
2340 sendnaws(void)
2342 long rows, cols;
2343 unsigned char tmp[16];
2344 unsigned char *cp;
2346 if (my_state_is_wont(TELOPT_NAWS))
2347 return;
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 */
2353 return;
2356 cp = tmp;
2358 *cp++ = IAC;
2359 *cp++ = SB;
2360 *cp++ = TELOPT_NAWS;
2361 PUTSHORT(cp, cols);
2362 PUTSHORT(cp, rows);
2363 *cp++ = IAC;
2364 *cp++ = SE;
2365 if (NETROOM() >= cp - tmp) {
2366 ring_supply_data(&netoring, tmp, cp-tmp);
2367 printsub('>', tmp+2, cp - tmp - 2);
2371 void
2372 tel_enter_binary(int rw)
2374 if (rw&1)
2375 send_do(TELOPT_BINARY, 1);
2376 if (rw&2)
2377 send_will(TELOPT_BINARY, 1);
2380 void
2381 tel_leave_binary(int rw)
2383 if (rw&1)
2384 send_dont(TELOPT_BINARY, 1);
2385 if (rw&2)
2386 send_wont(TELOPT_BINARY, 1);