inet6: only mark autoconf addresses tentative if detached
[dragonfly.git] / usr.bin / telnet / telnet.c
blob78c36b9ce00260099d713b2995f3302d33d25f04
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 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c",
736 IAC, SB, TELOPT_TTYPE, 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 snprintf((char *)temp, sizeof(temp), "%c%c%c%c%ld,%ld%c%c",
758 IAC, SB, TELOPT_TSPEED, TELQUAL_IS,
759 ospeed, ispeed, IAC, SE);
760 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
762 if (len < NETROOM()) {
763 ring_supply_data(&netoring, temp, len);
764 printsub('>', temp+2, len - 2);
766 /*@*/ else printf("lm_will: not enough room in buffer\n");
768 break;
769 case TELOPT_LFLOW:
770 if (my_want_state_is_wont(TELOPT_LFLOW))
771 return;
772 if (SB_EOF())
773 return;
774 switch(SB_GET()) {
775 case LFLOW_RESTART_ANY:
776 restartany = 1;
777 break;
778 case LFLOW_RESTART_XON:
779 restartany = 0;
780 break;
781 case LFLOW_ON:
782 localflow = 1;
783 break;
784 case LFLOW_OFF:
785 localflow = 0;
786 break;
787 default:
788 return;
790 setcommandmode();
791 setconnmode(0);
792 break;
794 case TELOPT_LINEMODE:
795 if (my_want_state_is_wont(TELOPT_LINEMODE))
796 return;
797 if (SB_EOF())
798 return;
799 switch (SB_GET()) {
800 case WILL:
801 lm_will(subpointer, SB_LEN());
802 break;
803 case WONT:
804 lm_wont(subpointer, SB_LEN());
805 break;
806 case DO:
807 lm_do(subpointer, SB_LEN());
808 break;
809 case DONT:
810 lm_dont(subpointer, SB_LEN());
811 break;
812 case LM_SLC:
813 slc(subpointer, SB_LEN());
814 break;
815 case LM_MODE:
816 lm_mode(subpointer, SB_LEN(), 0);
817 break;
818 default:
819 break;
821 break;
823 #ifdef OLD_ENVIRON
824 case TELOPT_OLD_ENVIRON:
825 #endif
826 case TELOPT_NEW_ENVIRON:
827 if (SB_EOF())
828 return;
829 switch(SB_PEEK()) {
830 case TELQUAL_IS:
831 case TELQUAL_INFO:
832 if (my_want_state_is_dont(subchar))
833 return;
834 break;
835 case TELQUAL_SEND:
836 if (my_want_state_is_wont(subchar)) {
837 return;
839 break;
840 default:
841 return;
843 env_opt(subpointer, SB_LEN());
844 break;
846 case TELOPT_XDISPLOC:
847 if (my_want_state_is_wont(TELOPT_XDISPLOC))
848 return;
849 if (SB_EOF())
850 return;
851 if (SB_GET() == TELQUAL_SEND) {
852 unsigned char temp[50], *dp;
853 int len;
855 if ((dp = env_getvalue("DISPLAY")) == NULL ||
856 strlen(dp) > sizeof(temp) - 7) {
858 * Something happened, we no longer have a DISPLAY
859 * variable. Or it is too long. So, turn off the option.
861 send_wont(TELOPT_XDISPLOC, 1);
862 break;
864 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
865 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
866 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
868 if (len < NETROOM()) {
869 ring_supply_data(&netoring, temp, len);
870 printsub('>', temp+2, len - 2);
872 /*@*/ else printf("lm_will: not enough room in buffer\n");
874 break;
876 #ifdef AUTHENTICATION
877 case TELOPT_AUTHENTICATION: {
878 if (!autologin)
879 break;
880 if (SB_EOF())
881 return;
882 switch(SB_GET()) {
883 case TELQUAL_IS:
884 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
885 return;
886 auth_is(subpointer, SB_LEN());
887 break;
888 case TELQUAL_SEND:
889 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
890 return;
891 auth_send(subpointer, SB_LEN());
892 break;
893 case TELQUAL_REPLY:
894 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
895 return;
896 auth_reply(subpointer, SB_LEN());
897 break;
898 case TELQUAL_NAME:
899 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
900 return;
901 auth_name(subpointer, SB_LEN());
902 break;
905 break;
906 #endif
907 #ifdef ENCRYPTION
908 case TELOPT_ENCRYPT:
909 if (SB_EOF())
910 return;
911 switch(SB_GET()) {
912 case ENCRYPT_START:
913 if (my_want_state_is_dont(TELOPT_ENCRYPT))
914 return;
915 encrypt_start(subpointer, SB_LEN());
916 break;
917 case ENCRYPT_END:
918 if (my_want_state_is_dont(TELOPT_ENCRYPT))
919 return;
920 encrypt_end();
921 break;
922 case ENCRYPT_SUPPORT:
923 if (my_want_state_is_wont(TELOPT_ENCRYPT))
924 return;
925 encrypt_support(subpointer, SB_LEN());
926 break;
927 case ENCRYPT_REQSTART:
928 if (my_want_state_is_wont(TELOPT_ENCRYPT))
929 return;
930 encrypt_request_start(subpointer, SB_LEN());
931 break;
932 case ENCRYPT_REQEND:
933 if (my_want_state_is_wont(TELOPT_ENCRYPT))
934 return;
936 * We can always send an REQEND so that we cannot
937 * get stuck encrypting. We should only get this
938 * if we have been able to get in the correct mode
939 * anyhow.
941 encrypt_request_end();
942 break;
943 case ENCRYPT_IS:
944 if (my_want_state_is_dont(TELOPT_ENCRYPT))
945 return;
946 encrypt_is(subpointer, SB_LEN());
947 break;
948 case ENCRYPT_REPLY:
949 if (my_want_state_is_wont(TELOPT_ENCRYPT))
950 return;
951 encrypt_reply(subpointer, SB_LEN());
952 break;
953 case ENCRYPT_ENC_KEYID:
954 if (my_want_state_is_dont(TELOPT_ENCRYPT))
955 return;
956 encrypt_enc_keyid(subpointer, SB_LEN());
957 break;
958 case ENCRYPT_DEC_KEYID:
959 if (my_want_state_is_wont(TELOPT_ENCRYPT))
960 return;
961 encrypt_dec_keyid(subpointer, SB_LEN());
962 break;
963 default:
964 break;
966 break;
967 #endif /* ENCRYPTION */
968 default:
969 break;
973 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
975 void
976 lm_will(unsigned char *cmd, int len)
978 if (len < 1) {
979 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
980 return;
982 switch(cmd[0]) {
983 case LM_FORWARDMASK: /* We shouldn't ever get this... */
984 default:
985 str_lm[3] = DONT;
986 str_lm[4] = cmd[0];
987 if (NETROOM() > (int)sizeof(str_lm)) {
988 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
989 printsub('>', &str_lm[2], sizeof(str_lm)-2);
991 /*@*/ else printf("lm_will: not enough room in buffer\n");
992 break;
996 void
997 lm_wont(unsigned char *cmd, int len)
999 if (len < 1) {
1000 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1001 return;
1003 switch(cmd[0]) {
1004 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1005 default:
1006 /* We are always DONT, so don't respond */
1007 return;
1011 void
1012 lm_do(unsigned char *cmd, int len)
1014 if (len < 1) {
1015 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1016 return;
1018 switch(cmd[0]) {
1019 case LM_FORWARDMASK:
1020 default:
1021 str_lm[3] = WONT;
1022 str_lm[4] = cmd[0];
1023 if (NETROOM() > (int)sizeof(str_lm)) {
1024 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1025 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1027 /*@*/ else printf("lm_do: not enough room in buffer\n");
1028 break;
1032 void
1033 lm_dont(unsigned char *cmd, int len)
1035 if (len < 1) {
1036 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1037 return;
1039 switch(cmd[0]) {
1040 case LM_FORWARDMASK:
1041 default:
1042 /* we are always WONT, so don't respond */
1043 break;
1047 static unsigned char str_lm_mode[] = {
1048 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1051 void
1052 lm_mode(unsigned char *cmd, int len, int init)
1054 if (len != 1)
1055 return;
1056 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1057 return;
1058 if (*cmd&MODE_ACK)
1059 return;
1060 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1061 str_lm_mode[4] = linemode;
1062 if (!init)
1063 str_lm_mode[4] |= MODE_ACK;
1064 if (NETROOM() > (int)sizeof(str_lm_mode)) {
1065 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1066 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1068 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1069 setconnmode(0); /* set changed mode */
1075 * slc()
1076 * Handle special character suboption of LINEMODE.
1079 struct spc {
1080 cc_t val;
1081 cc_t *valp;
1082 char flags; /* Current flags & level */
1083 char mylevel; /* Maximum level & flags */
1084 } spc_data[NSLC+1];
1086 #define SLC_IMPORT 0
1087 #define SLC_EXPORT 1
1088 #define SLC_RVALUE 2
1089 static int slc_mode = SLC_EXPORT;
1091 void
1092 slc_init(void)
1094 struct spc *spcp;
1096 localchars = 1;
1097 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1098 spcp->val = 0;
1099 spcp->valp = NULL;
1100 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1103 #define initfunc(func, flags) { \
1104 spcp = &spc_data[func]; \
1105 if ((spcp->valp = tcval(func))) { \
1106 spcp->val = *spcp->valp; \
1107 spcp->mylevel = SLC_VARIABLE|flags; \
1108 } else { \
1109 spcp->val = 0; \
1110 spcp->mylevel = SLC_DEFAULT; \
1114 initfunc(SLC_SYNCH, 0);
1115 /* No BRK */
1116 initfunc(SLC_AO, 0);
1117 initfunc(SLC_AYT, 0);
1118 /* No EOR */
1119 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1120 initfunc(SLC_EOF, 0);
1121 #ifndef SYSV_TERMIO
1122 initfunc(SLC_SUSP, SLC_FLUSHIN);
1123 #endif
1124 initfunc(SLC_EC, 0);
1125 initfunc(SLC_EL, 0);
1126 #ifndef SYSV_TERMIO
1127 initfunc(SLC_EW, 0);
1128 initfunc(SLC_RP, 0);
1129 initfunc(SLC_LNEXT, 0);
1130 #endif
1131 initfunc(SLC_XON, 0);
1132 initfunc(SLC_XOFF, 0);
1133 #ifdef SYSV_TERMIO
1134 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1135 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1136 #endif
1137 initfunc(SLC_FORW1, 0);
1138 #ifdef USE_TERMIO
1139 initfunc(SLC_FORW2, 0);
1140 /* No FORW2 */
1141 #endif
1143 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1144 #undef initfunc
1146 if (slc_mode == SLC_EXPORT)
1147 slc_export();
1148 else
1149 slc_import(1);
1153 void
1154 slcstate(void)
1156 printf("Special characters are %s values\n",
1157 slc_mode == SLC_IMPORT ? "remote default" :
1158 slc_mode == SLC_EXPORT ? "local" :
1159 "remote");
1162 void
1163 slc_mode_export(void)
1165 slc_mode = SLC_EXPORT;
1166 if (my_state_is_will(TELOPT_LINEMODE))
1167 slc_export();
1170 void
1171 slc_mode_import(int def)
1173 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1174 if (my_state_is_will(TELOPT_LINEMODE))
1175 slc_import(def);
1178 unsigned char slc_import_val[] = {
1179 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1181 unsigned char slc_import_def[] = {
1182 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1185 void
1186 slc_import(int def)
1188 if (NETROOM() > (int)sizeof(slc_import_val)) {
1189 if (def) {
1190 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1191 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1192 } else {
1193 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1194 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1197 /*@*/ else printf("slc_import: not enough room\n");
1200 void
1201 slc_export(void)
1203 struct spc *spcp;
1205 TerminalDefaultChars();
1207 slc_start_reply();
1208 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1209 if (spcp->mylevel != SLC_NOSUPPORT) {
1210 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1211 spcp->flags = SLC_NOSUPPORT;
1212 else
1213 spcp->flags = spcp->mylevel;
1214 if (spcp->valp)
1215 spcp->val = *spcp->valp;
1216 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1219 slc_end_reply();
1220 (void)slc_update();
1221 setconnmode(1); /* Make sure the character values are set */
1224 void
1225 slc(unsigned char *cp, int len)
1227 struct spc *spcp;
1228 int func,level;
1230 slc_start_reply();
1232 for (; len >= 3; len -=3, cp +=3) {
1234 func = cp[SLC_FUNC];
1236 if (func == 0) {
1238 * Client side: always ignore 0 function.
1240 continue;
1242 if (func > NSLC) {
1243 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1244 slc_add_reply(func, SLC_NOSUPPORT, 0);
1245 continue;
1248 spcp = &spc_data[func];
1250 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1252 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1253 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1254 continue;
1257 if (level == (SLC_DEFAULT|SLC_ACK)) {
1259 * This is an error condition, the SLC_ACK
1260 * bit should never be set for the SLC_DEFAULT
1261 * level. Our best guess to recover is to
1262 * ignore the SLC_ACK bit.
1264 cp[SLC_FLAGS] &= ~SLC_ACK;
1267 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1268 spcp->val = (cc_t)cp[SLC_VALUE];
1269 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1270 continue;
1273 level &= ~SLC_ACK;
1275 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1276 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1277 spcp->val = (cc_t)cp[SLC_VALUE];
1279 if (level == SLC_DEFAULT) {
1280 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1281 spcp->flags = spcp->mylevel;
1282 else
1283 spcp->flags = SLC_NOSUPPORT;
1285 slc_add_reply(func, spcp->flags, spcp->val);
1287 slc_end_reply();
1288 if (slc_update())
1289 setconnmode(1); /* set the new character values */
1292 void
1293 slc_check(void)
1295 struct spc *spcp;
1297 slc_start_reply();
1298 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1299 if (spcp->valp && spcp->val != *spcp->valp) {
1300 spcp->val = *spcp->valp;
1301 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1302 spcp->flags = SLC_NOSUPPORT;
1303 else
1304 spcp->flags = spcp->mylevel;
1305 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1308 slc_end_reply();
1309 setconnmode(1);
1312 unsigned char slc_reply[128];
1313 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1314 unsigned char *slc_replyp;
1316 void
1317 slc_start_reply(void)
1319 slc_replyp = slc_reply;
1320 *slc_replyp++ = IAC;
1321 *slc_replyp++ = SB;
1322 *slc_replyp++ = TELOPT_LINEMODE;
1323 *slc_replyp++ = LM_SLC;
1326 void
1327 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1329 /* A sequence of up to 6 bytes my be written for this member of the SLC
1330 * suboption list by this function. The end of negotiation command,
1331 * which is written by slc_end_reply(), will require 2 additional
1332 * bytes. Do not proceed unless there is sufficient space for these
1333 * items.
1335 if (&slc_replyp[6+2] > slc_reply_eom)
1336 return;
1337 if ((*slc_replyp++ = func) == IAC)
1338 *slc_replyp++ = IAC;
1339 if ((*slc_replyp++ = flags) == IAC)
1340 *slc_replyp++ = IAC;
1341 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1342 *slc_replyp++ = IAC;
1345 void
1346 slc_end_reply(void)
1348 int len;
1349 /* The end of negotiation command requires 2 bytes. */
1350 if (&slc_replyp[2] > slc_reply_eom)
1351 return;
1352 *slc_replyp++ = IAC;
1353 *slc_replyp++ = SE;
1354 len = slc_replyp - slc_reply;
1355 if (len <= 6)
1356 return;
1357 if (NETROOM() > len) {
1358 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1359 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1361 /*@*/else printf("slc_end_reply: not enough room\n");
1365 slc_update(void)
1367 struct spc *spcp;
1368 int need_update = 0;
1370 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1371 if (!(spcp->flags&SLC_ACK))
1372 continue;
1373 spcp->flags &= ~SLC_ACK;
1374 if (spcp->valp && (*spcp->valp != spcp->val)) {
1375 *spcp->valp = spcp->val;
1376 need_update = 1;
1379 return(need_update);
1382 #ifdef OLD_ENVIRON
1383 # ifdef ENV_HACK
1385 * Earlier version of telnet/telnetd from the BSD code had
1386 * the definitions of VALUE and VAR reversed. To ensure
1387 * maximum interoperability, we assume that the server is
1388 * an older BSD server, until proven otherwise. The newer
1389 * BSD servers should be able to handle either definition,
1390 * so it is better to use the wrong values if we don't
1391 * know what type of server it is.
1393 int env_auto = 1;
1394 int old_env_var = OLD_ENV_VAR;
1395 int old_env_value = OLD_ENV_VALUE;
1396 # else
1397 # define old_env_var OLD_ENV_VAR
1398 # define old_env_value OLD_ENV_VALUE
1399 # endif
1400 #endif
1402 void
1403 env_opt(unsigned char *buf, int len)
1405 unsigned char *ep = NULL, *epc = NULL;
1406 int i;
1408 switch(buf[0]&0xff) {
1409 case TELQUAL_SEND:
1410 env_opt_start();
1411 if (len == 1) {
1412 env_opt_add(NULL);
1413 } else for (i = 1; i < len; i++) {
1414 switch (buf[i]&0xff) {
1415 #ifdef OLD_ENVIRON
1416 case OLD_ENV_VAR:
1417 # ifdef ENV_HACK
1418 if (telopt_environ == TELOPT_OLD_ENVIRON
1419 && env_auto) {
1420 /* Server has the same definitions */
1421 old_env_var = OLD_ENV_VAR;
1422 old_env_value = OLD_ENV_VALUE;
1424 /* FALL THROUGH */
1425 # endif
1426 case OLD_ENV_VALUE:
1428 * Although OLD_ENV_VALUE is not legal, we will
1429 * still recognize it, just in case it is an
1430 * old server that has VAR & VALUE mixed up...
1432 /* FALL THROUGH */
1433 #else
1434 case NEW_ENV_VAR:
1435 #endif
1436 case ENV_USERVAR:
1437 if (ep) {
1438 *epc = 0;
1439 env_opt_add(ep);
1441 ep = epc = &buf[i+1];
1442 break;
1443 case ENV_ESC:
1444 i++;
1445 /*FALL THROUGH*/
1446 default:
1447 if (epc)
1448 *epc++ = buf[i];
1449 break;
1452 if (ep) {
1453 *epc = 0;
1454 env_opt_add(ep);
1456 env_opt_end(1);
1457 break;
1459 case TELQUAL_IS:
1460 case TELQUAL_INFO:
1461 /* Ignore for now. We shouldn't get it anyway. */
1462 break;
1464 default:
1465 break;
1469 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1470 unsigned char *opt_reply = NULL;
1471 unsigned char *opt_replyp;
1472 unsigned char *opt_replyend;
1474 void
1475 env_opt_start(void)
1477 if (opt_reply)
1478 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1479 else
1480 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1481 if (opt_reply == NULL) {
1482 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1483 opt_reply = opt_replyp = opt_replyend = NULL;
1484 return;
1486 opt_replyp = opt_reply;
1487 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1488 *opt_replyp++ = IAC;
1489 *opt_replyp++ = SB;
1490 *opt_replyp++ = telopt_environ;
1491 *opt_replyp++ = TELQUAL_IS;
1494 void
1495 env_opt_start_info(void)
1497 env_opt_start();
1498 if (opt_replyp)
1499 opt_replyp[-1] = TELQUAL_INFO;
1502 void
1503 env_opt_add(unsigned char *ep)
1505 unsigned char *vp, c;
1507 if (opt_reply == NULL) /*XXX*/
1508 return; /*XXX*/
1510 if (ep == NULL || *ep == '\0') {
1511 /* Send user defined variables first. */
1512 env_default(1, 0);
1513 while ((ep = env_default(0, 0)))
1514 env_opt_add(ep);
1516 /* Now add the list of well know variables. */
1517 env_default(1, 1);
1518 while ((ep = env_default(0, 1)))
1519 env_opt_add(ep);
1520 return;
1522 vp = env_getvalue(ep);
1523 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1524 2 * strlen((char *)ep) + 6 > opt_replyend)
1527 int len;
1528 opt_replyend += OPT_REPLY_SIZE;
1529 len = opt_replyend - opt_reply;
1530 opt_reply = (unsigned char *)realloc(opt_reply, len);
1531 if (opt_reply == NULL) {
1532 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1533 opt_reply = opt_replyp = opt_replyend = NULL;
1534 return;
1536 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1537 opt_replyend = opt_reply + len;
1539 if (opt_welldefined(ep))
1540 #ifdef OLD_ENVIRON
1541 if (telopt_environ == TELOPT_OLD_ENVIRON)
1542 *opt_replyp++ = old_env_var;
1543 else
1544 #endif
1545 *opt_replyp++ = NEW_ENV_VAR;
1546 else
1547 *opt_replyp++ = ENV_USERVAR;
1548 for (;;) {
1549 while ((c = *ep++)) {
1550 if (opt_replyp + (2 + 2) > opt_replyend)
1551 return;
1552 switch(c&0xff) {
1553 case IAC:
1554 *opt_replyp++ = IAC;
1555 break;
1556 case NEW_ENV_VAR:
1557 case NEW_ENV_VALUE:
1558 case ENV_ESC:
1559 case ENV_USERVAR:
1560 *opt_replyp++ = ENV_ESC;
1561 break;
1563 *opt_replyp++ = c;
1565 if ((ep = vp)) {
1566 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1567 return;
1568 #ifdef OLD_ENVIRON
1569 if (telopt_environ == TELOPT_OLD_ENVIRON)
1570 *opt_replyp++ = old_env_value;
1571 else
1572 #endif
1573 *opt_replyp++ = NEW_ENV_VALUE;
1574 vp = NULL;
1575 } else
1576 break;
1581 opt_welldefined(const char *ep)
1583 if ((strcmp(ep, "USER") == 0) ||
1584 (strcmp(ep, "DISPLAY") == 0) ||
1585 (strcmp(ep, "PRINTER") == 0) ||
1586 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1587 (strcmp(ep, "JOB") == 0) ||
1588 (strcmp(ep, "ACCT") == 0))
1589 return(1);
1590 return(0);
1593 void
1594 env_opt_end(int emptyok)
1596 int len;
1598 if (opt_replyp + 2 > opt_replyend)
1599 return;
1600 len = opt_replyp + 2 - opt_reply;
1601 if (emptyok || len > 6) {
1602 *opt_replyp++ = IAC;
1603 *opt_replyp++ = SE;
1604 if (NETROOM() > len) {
1605 ring_supply_data(&netoring, opt_reply, len);
1606 printsub('>', &opt_reply[2], len - 2);
1608 /*@*/ else printf("slc_end_reply: not enough room\n");
1610 if (opt_reply) {
1611 free(opt_reply);
1612 opt_reply = opt_replyp = opt_replyend = NULL;
1619 telrcv(void)
1621 int c;
1622 int scc;
1623 unsigned char *sbp;
1624 int count;
1625 int returnValue = 0;
1627 scc = 0;
1628 count = 0;
1629 while (TTYROOM() > 2) {
1630 if (scc == 0) {
1631 if (count) {
1632 ring_consumed(&netiring, count);
1633 returnValue = 1;
1634 count = 0;
1636 sbp = netiring.consume;
1637 scc = ring_full_consecutive(&netiring);
1638 if (scc == 0) {
1639 /* No more data coming in */
1640 break;
1644 c = *sbp++ & 0xff, scc--; count++;
1645 #ifdef ENCRYPTION
1646 if (decrypt_input)
1647 c = (*decrypt_input)(c);
1648 #endif /* ENCRYPTION */
1650 switch (telrcv_state) {
1652 case TS_CR:
1653 telrcv_state = TS_DATA;
1654 if (c == '\0') {
1655 break; /* Ignore \0 after CR */
1657 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1658 TTYADD(c);
1659 break;
1661 /* Else, fall through */
1663 case TS_DATA:
1664 if (c == IAC) {
1665 telrcv_state = TS_IAC;
1666 break;
1669 * The 'crmod' hack (see following) is needed
1670 * since we can't * set CRMOD on output only.
1671 * Machines like MULTICS like to send \r without
1672 * \n; since we must turn off CRMOD to get proper
1673 * input, the mapping is done here (sigh).
1675 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1676 if (scc > 0) {
1677 c = *sbp&0xff;
1678 #ifdef ENCRYPTION
1679 if (decrypt_input)
1680 c = (*decrypt_input)(c);
1681 #endif /* ENCRYPTION */
1682 if (c == 0) {
1683 sbp++, scc--; count++;
1684 /* a "true" CR */
1685 TTYADD('\r');
1686 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1687 (c == '\n')) {
1688 sbp++, scc--; count++;
1689 TTYADD('\n');
1690 } else {
1691 #ifdef ENCRYPTION
1692 if (decrypt_input)
1693 (*decrypt_input)(-1);
1694 #endif /* ENCRYPTION */
1696 TTYADD('\r');
1697 if (crmod) {
1698 TTYADD('\n');
1701 } else {
1702 telrcv_state = TS_CR;
1703 TTYADD('\r');
1704 if (crmod) {
1705 TTYADD('\n');
1708 } else {
1709 TTYADD(c);
1711 continue;
1713 case TS_IAC:
1714 process_iac:
1715 switch (c) {
1717 case WILL:
1718 telrcv_state = TS_WILL;
1719 continue;
1721 case WONT:
1722 telrcv_state = TS_WONT;
1723 continue;
1725 case DO:
1726 telrcv_state = TS_DO;
1727 continue;
1729 case DONT:
1730 telrcv_state = TS_DONT;
1731 continue;
1733 case DM:
1735 * We may have missed an urgent notification,
1736 * so make sure we flush whatever is in the
1737 * buffer currently.
1739 printoption("RCVD", IAC, DM);
1740 SYNCHing = 1;
1741 (void) ttyflush(1);
1742 SYNCHing = stilloob();
1743 settimer(gotDM);
1744 break;
1746 case SB:
1747 SB_CLEAR();
1748 telrcv_state = TS_SB;
1749 continue;
1751 case IAC:
1752 TTYADD(IAC);
1753 break;
1755 case NOP:
1756 case GA:
1757 default:
1758 printoption("RCVD", IAC, c);
1759 break;
1761 telrcv_state = TS_DATA;
1762 continue;
1764 case TS_WILL:
1765 printoption("RCVD", WILL, c);
1766 willoption(c);
1767 telrcv_state = TS_DATA;
1768 continue;
1770 case TS_WONT:
1771 printoption("RCVD", WONT, c);
1772 wontoption(c);
1773 telrcv_state = TS_DATA;
1774 continue;
1776 case TS_DO:
1777 printoption("RCVD", DO, c);
1778 dooption(c);
1779 if (c == TELOPT_NAWS) {
1780 sendnaws();
1781 } else if (c == TELOPT_LFLOW) {
1782 localflow = 1;
1783 setcommandmode();
1784 setconnmode(0);
1786 telrcv_state = TS_DATA;
1787 continue;
1789 case TS_DONT:
1790 printoption("RCVD", DONT, c);
1791 dontoption(c);
1792 flushline = 1;
1793 setconnmode(0); /* set new tty mode (maybe) */
1794 telrcv_state = TS_DATA;
1795 continue;
1797 case TS_SB:
1798 if (c == IAC) {
1799 telrcv_state = TS_SE;
1800 } else {
1801 SB_ACCUM(c);
1803 continue;
1805 case TS_SE:
1806 if (c != SE) {
1807 if (c != IAC) {
1809 * This is an error. We only expect to get
1810 * "IAC IAC" or "IAC SE". Several things may
1811 * have happend. An IAC was not doubled, the
1812 * IAC SE was left off, or another option got
1813 * inserted into the suboption are all possibilities.
1814 * If we assume that the IAC was not doubled,
1815 * and really the IAC SE was left off, we could
1816 * get into an infinate loop here. So, instead,
1817 * we terminate the suboption, and process the
1818 * partial suboption if we can.
1820 SB_ACCUM(IAC);
1821 SB_ACCUM(c);
1822 subpointer -= 2;
1823 SB_TERM();
1825 printoption("In SUBOPTION processing, RCVD", IAC, c);
1826 suboption(); /* handle sub-option */
1827 telrcv_state = TS_IAC;
1828 goto process_iac;
1830 SB_ACCUM(c);
1831 telrcv_state = TS_SB;
1832 } else {
1833 SB_ACCUM(IAC);
1834 SB_ACCUM(SE);
1835 subpointer -= 2;
1836 SB_TERM();
1837 suboption(); /* handle sub-option */
1838 telrcv_state = TS_DATA;
1842 if (count)
1843 ring_consumed(&netiring, count);
1844 return returnValue||count;
1847 static int bol = 1, local = 0;
1850 rlogin_susp(void)
1852 if (local) {
1853 local = 0;
1854 bol = 1;
1855 command(0, "z\n", 2);
1856 return(1);
1858 return(0);
1861 static int
1862 telsnd(void)
1864 int tcc;
1865 int count;
1866 int returnValue = 0;
1867 unsigned char *tbp;
1869 tcc = 0;
1870 count = 0;
1871 while (NETROOM() > 2) {
1872 int sc;
1873 int c;
1875 if (tcc == 0) {
1876 if (count) {
1877 ring_consumed(&ttyiring, count);
1878 returnValue = 1;
1879 count = 0;
1881 tbp = ttyiring.consume;
1882 tcc = ring_full_consecutive(&ttyiring);
1883 if (tcc == 0) {
1884 break;
1887 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1888 if (rlogin != _POSIX_VDISABLE) {
1889 if (bol) {
1890 bol = 0;
1891 if (sc == rlogin) {
1892 local = 1;
1893 continue;
1895 } else if (local) {
1896 local = 0;
1897 if (sc == '.' || c == termEofChar) {
1898 bol = 1;
1899 command(0, "close\n", 6);
1900 continue;
1902 if (sc == termSuspChar) {
1903 bol = 1;
1904 command(0, "z\n", 2);
1905 continue;
1907 if (sc == escape) {
1908 command(0, tbp, tcc);
1909 bol = 1;
1910 count += tcc;
1911 tcc = 0;
1912 flushline = 1;
1913 break;
1915 if (sc != rlogin) {
1916 ++tcc;
1917 --tbp;
1918 --count;
1919 c = sc = rlogin;
1922 if ((sc == '\n') || (sc == '\r'))
1923 bol = 1;
1924 } else if (escape != _POSIX_VDISABLE && sc == escape) {
1926 * Double escape is a pass through of a single escape character.
1928 if (tcc && strip(*tbp) == escape) {
1929 tbp++;
1930 tcc--;
1931 count++;
1932 bol = 0;
1933 } else {
1934 command(0, (char *)tbp, tcc);
1935 bol = 1;
1936 count += tcc;
1937 tcc = 0;
1938 flushline = 1;
1939 break;
1941 } else
1942 bol = 0;
1943 #ifdef KLUDGELINEMODE
1944 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1945 if (tcc > 0 && strip(*tbp) == echoc) {
1946 tcc--; tbp++; count++;
1947 } else {
1948 dontlecho = !dontlecho;
1949 settimer(echotoggle);
1950 setconnmode(0);
1951 flushline = 1;
1952 break;
1955 #endif
1956 if (MODE_LOCAL_CHARS(globalmode)) {
1957 if (TerminalSpecialChars(sc) == 0) {
1958 bol = 1;
1959 break;
1962 if (my_want_state_is_wont(TELOPT_BINARY)) {
1963 switch (c) {
1964 case '\n':
1966 * If we are in CRMOD mode (\r ==> \n)
1967 * on our local machine, then probably
1968 * a newline (unix) is CRLF (TELNET).
1970 if (MODE_LOCAL_CHARS(globalmode)) {
1971 NETADD('\r');
1973 NETADD('\n');
1974 bol = flushline = 1;
1975 break;
1976 case '\r':
1977 if (!crlf) {
1978 NET2ADD('\r', '\0');
1979 } else {
1980 NET2ADD('\r', '\n');
1982 bol = flushline = 1;
1983 break;
1984 case IAC:
1985 NET2ADD(IAC, IAC);
1986 break;
1987 default:
1988 NETADD(c);
1989 break;
1991 } else if (c == IAC) {
1992 NET2ADD(IAC, IAC);
1993 } else {
1994 NETADD(c);
1997 if (count)
1998 ring_consumed(&ttyiring, count);
1999 return returnValue||count; /* Non-zero if we did anything */
2003 * Scheduler()
2005 * Try to do something.
2007 * If we do something useful, return 1; else return 0.
2011 static int
2012 Scheduler(int block)
2014 /* One wants to be a bit careful about setting returnValue
2015 * to one, since a one implies we did some useful work,
2016 * and therefore probably won't be called to block next
2018 int returnValue;
2019 int netin, netout, netex, ttyin, ttyout;
2021 /* Decide which rings should be processed */
2023 netout = ring_full_count(&netoring) &&
2024 (flushline ||
2025 (my_want_state_is_wont(TELOPT_LINEMODE)
2026 #ifdef KLUDGELINEMODE
2027 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2028 #endif
2029 ) ||
2030 my_want_state_is_will(TELOPT_BINARY));
2031 ttyout = ring_full_count(&ttyoring);
2033 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2035 netin = !ISend && ring_empty_count(&netiring);
2037 netex = !SYNCHing;
2039 /* Call to system code to process rings */
2041 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2043 /* Now, look at the input rings, looking for work to do. */
2045 if (ring_full_count(&ttyiring)) {
2046 returnValue |= telsnd();
2049 if (ring_full_count(&netiring)) {
2050 returnValue |= telrcv();
2052 return returnValue;
2055 #ifdef AUTHENTICATION
2056 #define __unusedhere
2057 #else
2058 #define __unusedhere __unused
2059 #endif
2061 * Select from tty and network...
2063 void
2064 telnet(char *user __unusedhere)
2066 sys_telnet_init();
2068 #ifdef AUTHENTICATION
2069 #ifdef ENCRYPTION
2071 static char local_host[256] = { 0 };
2073 if (!local_host[0]) {
2074 gethostname(local_host, sizeof(local_host));
2075 local_host[sizeof(local_host)-1] = 0;
2077 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2078 auth_encrypt_user(user);
2080 #endif
2081 #endif
2082 if (telnetport) {
2083 #ifdef AUTHENTICATION
2084 if (autologin)
2085 send_will(TELOPT_AUTHENTICATION, 1);
2086 #endif
2087 #ifdef ENCRYPTION
2088 send_do(TELOPT_ENCRYPT, 1);
2089 send_will(TELOPT_ENCRYPT, 1);
2090 #endif /* ENCRYPTION */
2091 send_do(TELOPT_SGA, 1);
2092 send_will(TELOPT_TTYPE, 1);
2093 send_will(TELOPT_NAWS, 1);
2094 send_will(TELOPT_TSPEED, 1);
2095 send_will(TELOPT_LFLOW, 1);
2096 send_will(TELOPT_LINEMODE, 1);
2097 send_will(TELOPT_NEW_ENVIRON, 1);
2098 send_do(TELOPT_STATUS, 1);
2099 if (env_getvalue("DISPLAY"))
2100 send_will(TELOPT_XDISPLOC, 1);
2101 if (eight)
2102 tel_enter_binary(eight);
2105 for (;;) {
2106 int schedValue;
2108 while ((schedValue = Scheduler(0)) != 0) {
2109 if (schedValue == -1) {
2110 setcommandmode();
2111 return;
2115 if (Scheduler(1) == -1) {
2116 setcommandmode();
2117 return;
2122 #if 0 /* XXX - this not being in is a bug */
2124 * nextitem()
2126 * Return the address of the next "item" in the TELNET data
2127 * stream. This will be the address of the next character if
2128 * the current address is a user data character, or it will
2129 * be the address of the character following the TELNET command
2130 * if the current address is a TELNET IAC ("I Am a Command")
2131 * character.
2134 static char *
2135 nextitem(char *current)
2137 if ((*current&0xff) != IAC) {
2138 return current+1;
2140 switch (*(current+1)&0xff) {
2141 case DO:
2142 case DONT:
2143 case WILL:
2144 case WONT:
2145 return current+3;
2146 case SB: /* loop forever looking for the SE */
2148 char *look = current+2;
2150 for (;;) {
2151 if ((*look++&0xff) == IAC) {
2152 if ((*look++&0xff) == SE) {
2153 return look;
2158 default:
2159 return current+2;
2162 #endif /* 0 */
2165 * netclear()
2167 * We are about to do a TELNET SYNCH operation. Clear
2168 * the path to the network.
2170 * Things are a bit tricky since we may have sent the first
2171 * byte or so of a previous TELNET command into the network.
2172 * So, we have to scan the network buffer from the beginning
2173 * until we are up to where we want to be.
2175 * A side effect of what we do, just to keep things
2176 * simple, is to clear the urgent data pointer. The principal
2177 * caller should be setting the urgent data pointer AFTER calling
2178 * us in any case.
2181 static void
2182 netclear(void)
2184 /* Deleted */
2188 * These routines add various telnet commands to the data stream.
2191 static void
2192 doflush(void)
2194 NET2ADD(IAC, DO);
2195 NETADD(TELOPT_TM);
2196 flushline = 1;
2197 flushout = 1;
2198 (void) ttyflush(1); /* Flush/drop output */
2199 /* do printoption AFTER flush, otherwise the output gets tossed... */
2200 printoption("SENT", DO, TELOPT_TM);
2203 void
2204 xmitAO(void)
2206 NET2ADD(IAC, AO);
2207 printoption("SENT", IAC, AO);
2208 if (autoflush) {
2209 doflush();
2213 void
2214 xmitEL(void)
2216 NET2ADD(IAC, EL);
2217 printoption("SENT", IAC, EL);
2220 void
2221 xmitEC(void)
2223 NET2ADD(IAC, EC);
2224 printoption("SENT", IAC, EC);
2228 dosynch(char *ch __unused)
2230 netclear(); /* clear the path to the network */
2231 NETADD(IAC);
2232 setneturg();
2233 NETADD(DM);
2234 printoption("SENT", IAC, DM);
2235 return 1;
2238 int want_status_response = 0;
2241 get_status(char *ch __unused)
2243 unsigned char tmp[16];
2244 unsigned char *cp;
2246 if (my_want_state_is_dont(TELOPT_STATUS)) {
2247 printf("Remote side does not support STATUS option\n");
2248 return 0;
2250 cp = tmp;
2252 *cp++ = IAC;
2253 *cp++ = SB;
2254 *cp++ = TELOPT_STATUS;
2255 *cp++ = TELQUAL_SEND;
2256 *cp++ = IAC;
2257 *cp++ = SE;
2258 if (NETROOM() >= cp - tmp) {
2259 ring_supply_data(&netoring, tmp, cp-tmp);
2260 printsub('>', tmp+2, cp - tmp - 2);
2262 ++want_status_response;
2263 return 1;
2266 void
2267 intp(void)
2269 NET2ADD(IAC, IP);
2270 printoption("SENT", IAC, IP);
2271 flushline = 1;
2272 if (autoflush) {
2273 doflush();
2275 if (autosynch) {
2276 dosynch(NULL);
2280 void
2281 sendbrk(void)
2283 NET2ADD(IAC, BREAK);
2284 printoption("SENT", IAC, BREAK);
2285 flushline = 1;
2286 if (autoflush) {
2287 doflush();
2289 if (autosynch) {
2290 dosynch(NULL);
2294 void
2295 sendabort(void)
2297 NET2ADD(IAC, ABORT);
2298 printoption("SENT", IAC, ABORT);
2299 flushline = 1;
2300 if (autoflush) {
2301 doflush();
2303 if (autosynch) {
2304 dosynch(NULL);
2308 void
2309 sendsusp(void)
2311 NET2ADD(IAC, SUSP);
2312 printoption("SENT", IAC, SUSP);
2313 flushline = 1;
2314 if (autoflush) {
2315 doflush();
2317 if (autosynch) {
2318 dosynch(NULL);
2322 void
2323 sendeof(void)
2325 NET2ADD(IAC, xEOF);
2326 printoption("SENT", IAC, xEOF);
2329 void
2330 sendayt(void)
2332 NET2ADD(IAC, AYT);
2333 printoption("SENT", IAC, AYT);
2337 * Send a window size update to the remote system.
2340 void
2341 sendnaws(void)
2343 long rows, cols;
2344 unsigned char tmp[16];
2345 unsigned char *cp;
2347 if (my_state_is_wont(TELOPT_NAWS))
2348 return;
2350 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2351 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2353 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2354 return;
2357 cp = tmp;
2359 *cp++ = IAC;
2360 *cp++ = SB;
2361 *cp++ = TELOPT_NAWS;
2362 PUTSHORT(cp, cols);
2363 PUTSHORT(cp, rows);
2364 *cp++ = IAC;
2365 *cp++ = SE;
2366 if (NETROOM() >= cp - tmp) {
2367 ring_supply_data(&netoring, tmp, cp-tmp);
2368 printsub('>', tmp+2, cp - tmp - 2);
2372 void
2373 tel_enter_binary(int rw)
2375 if (rw&1)
2376 send_do(TELOPT_BINARY, 1);
2377 if (rw&2)
2378 send_will(TELOPT_BINARY, 1);
2381 void
2382 tel_leave_binary(int rw)
2384 if (rw&1)
2385 send_dont(TELOPT_BINARY, 1);
2386 if (rw&2)
2387 send_wont(TELOPT_BINARY, 1);