This commit was manufactured by cvs2svn to create tag
[heimdal.git] / appl / telnet / telnet / telnet.c
blobc427fbd13d66f6acdf854200e59ff11b514c14b6
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "telnet_locl.h"
35 #ifdef HAVE_TERMCAP_H
36 #include <termcap.h>
37 #endif
39 RCSID("$Id$");
41 #define strip(x) (eight ? (x) : ((x) & 0x7f))
43 static unsigned char subbuffer[SUBBUFSIZE],
44 *subpointer, *subend; /* buffer for sub-options */
45 #define SB_CLEAR() subpointer = subbuffer;
46 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
47 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
48 *subpointer++ = (c); \
51 #define SB_GET() ((*subpointer++)&0xff)
52 #define SB_PEEK() ((*subpointer)&0xff)
53 #define SB_EOF() (subpointer >= subend)
54 #define SB_LEN() (subend - subpointer)
56 char options[256]; /* The combined options */
57 char do_dont_resp[256];
58 char will_wont_resp[256];
60 int
61 eight = 3,
62 binary = 0,
63 autologin = 0, /* Autologin anyone? */
64 skiprc = 0,
65 connected,
66 showoptions,
67 ISend, /* trying to send network data in */
68 debug = 0,
69 crmod,
70 netdata, /* Print out network data flow */
71 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
72 telnetport,
73 wantencryption = 0,
74 SYNCHing, /* we are in TELNET SYNCH mode */
75 flushout, /* flush output */
76 autoflush = 0, /* flush output when interrupting? */
77 autosynch, /* send interrupt characters with SYNCH? */
78 localflow, /* we handle flow control locally */
79 restartany, /* if flow control enabled, restart on any character */
80 localchars, /* we recognize interrupt/quit */
81 donelclchars, /* the user has set "localchars" */
82 donebinarytoggle, /* the user has put us in binary */
83 dontlecho, /* do we suppress local echoing right now? */
84 globalmode;
86 char *prompt = 0;
88 int scheduler_lockout_tty = 0;
90 cc_t escape;
91 cc_t rlogin;
92 #ifdef KLUDGELINEMODE
93 cc_t echoc;
94 #endif
97 * Telnet receiver states for fsm
99 #define TS_DATA 0
100 #define TS_IAC 1
101 #define TS_WILL 2
102 #define TS_WONT 3
103 #define TS_DO 4
104 #define TS_DONT 5
105 #define TS_CR 6
106 #define TS_SB 7 /* sub-option collection */
107 #define TS_SE 8 /* looking for sub-option end */
109 static int telrcv_state;
110 #ifdef OLD_ENVIRON
111 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
112 #else
113 # define telopt_environ TELOPT_NEW_ENVIRON
114 #endif
116 jmp_buf toplevel;
117 jmp_buf peerdied;
119 int flushline;
120 int linemode;
122 #ifdef KLUDGELINEMODE
123 int kludgelinemode = 1;
124 #endif
127 * The following are some clocks used to decide how to interpret
128 * the relationship between various variables.
131 Clocks clocks;
133 static int is_unique(char *name, char **as, char **ae);
137 * Initialize telnet environment.
140 void
141 init_telnet(void)
143 env_init();
145 SB_CLEAR();
146 memset(options, 0, sizeof options);
148 connected = ISend = localflow = donebinarytoggle = 0;
149 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
150 auth_encrypt_connect(connected);
151 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
152 restartany = -1;
154 SYNCHing = 0;
156 /* Don't change NetTrace */
158 escape = CONTROL(']');
159 rlogin = _POSIX_VDISABLE;
160 #ifdef KLUDGELINEMODE
161 echoc = CONTROL('E');
162 #endif
164 flushline = 1;
165 telrcv_state = TS_DATA;
170 * These routines are in charge of sending option negotiations
171 * to the other side.
173 * The basic idea is that we send the negotiation if either side
174 * is in disagreement as to what the current state should be.
177 void
178 send_do(int c, int init)
180 if (init) {
181 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
182 my_want_state_is_do(c))
183 return;
184 set_my_want_state_do(c);
185 do_dont_resp[c]++;
187 NET2ADD(IAC, DO);
188 NETADD(c);
189 printoption("SENT", DO, c);
192 void
193 send_dont(int c, int init)
195 if (init) {
196 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
197 my_want_state_is_dont(c))
198 return;
199 set_my_want_state_dont(c);
200 do_dont_resp[c]++;
202 NET2ADD(IAC, DONT);
203 NETADD(c);
204 printoption("SENT", DONT, c);
207 void
208 send_will(int c, int init)
210 if (init) {
211 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
212 my_want_state_is_will(c))
213 return;
214 set_my_want_state_will(c);
215 will_wont_resp[c]++;
217 NET2ADD(IAC, WILL);
218 NETADD(c);
219 printoption("SENT", WILL, c);
222 void
223 send_wont(int c, int init)
225 if (init) {
226 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
227 my_want_state_is_wont(c))
228 return;
229 set_my_want_state_wont(c);
230 will_wont_resp[c]++;
232 NET2ADD(IAC, WONT);
233 NETADD(c);
234 printoption("SENT", WONT, c);
238 void
239 willoption(int option)
241 int new_state_ok = 0;
243 if (do_dont_resp[option]) {
244 --do_dont_resp[option];
245 if (do_dont_resp[option] && my_state_is_do(option))
246 --do_dont_resp[option];
249 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
251 switch (option) {
253 case TELOPT_ECHO:
254 case TELOPT_BINARY:
255 case TELOPT_SGA:
256 settimer(modenegotiated);
257 /* FALL THROUGH */
258 case TELOPT_STATUS:
259 #if defined(AUTHENTICATION)
260 case TELOPT_AUTHENTICATION:
261 #endif
262 #if defined(ENCRYPTION)
263 case TELOPT_ENCRYPT:
264 #endif
265 new_state_ok = 1;
266 break;
268 case TELOPT_TM:
269 if (flushout)
270 flushout = 0;
272 * Special case for TM. If we get back a WILL,
273 * pretend we got back a WONT.
275 set_my_want_state_dont(option);
276 set_my_state_dont(option);
277 return; /* Never reply to TM will's/wont's */
279 case TELOPT_LINEMODE:
280 default:
281 break;
284 if (new_state_ok) {
285 set_my_want_state_do(option);
286 send_do(option, 0);
287 setconnmode(0); /* possibly set new tty mode */
288 } else {
289 do_dont_resp[option]++;
290 send_dont(option, 0);
293 set_my_state_do(option);
294 #if defined(ENCRYPTION)
295 if (option == TELOPT_ENCRYPT)
296 encrypt_send_support();
297 #endif
300 void
301 wontoption(int option)
303 if (do_dont_resp[option]) {
304 --do_dont_resp[option];
305 if (do_dont_resp[option] && my_state_is_dont(option))
306 --do_dont_resp[option];
309 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
311 switch (option) {
313 #ifdef KLUDGELINEMODE
314 case TELOPT_SGA:
315 if (!kludgelinemode)
316 break;
317 /* FALL THROUGH */
318 #endif
319 case TELOPT_ECHO:
320 settimer(modenegotiated);
321 break;
323 case TELOPT_TM:
324 if (flushout)
325 flushout = 0;
326 set_my_want_state_dont(option);
327 set_my_state_dont(option);
328 return; /* Never reply to TM will's/wont's */
330 #ifdef ENCRYPTION
331 case TELOPT_ENCRYPT:
332 encrypt_not();
333 break;
334 #endif
335 default:
336 break;
338 set_my_want_state_dont(option);
339 if (my_state_is_do(option))
340 send_dont(option, 0);
341 setconnmode(0); /* Set new tty mode */
342 } else if (option == TELOPT_TM) {
344 * Special case for TM.
346 if (flushout)
347 flushout = 0;
348 set_my_want_state_dont(option);
350 set_my_state_dont(option);
353 static void
354 dooption(int option)
356 int new_state_ok = 0;
358 if (will_wont_resp[option]) {
359 --will_wont_resp[option];
360 if (will_wont_resp[option] && my_state_is_will(option))
361 --will_wont_resp[option];
364 if (will_wont_resp[option] == 0) {
365 if (my_want_state_is_wont(option)) {
367 switch (option) {
369 case TELOPT_TM:
371 * Special case for TM. We send a WILL, but pretend
372 * we sent WONT.
374 send_will(option, 0);
375 set_my_want_state_wont(TELOPT_TM);
376 set_my_state_wont(TELOPT_TM);
377 return;
379 case TELOPT_BINARY: /* binary mode */
380 case TELOPT_NAWS: /* window size */
381 case TELOPT_TSPEED: /* terminal speed */
382 case TELOPT_LFLOW: /* local flow control */
383 case TELOPT_TTYPE: /* terminal type option */
384 case TELOPT_SGA: /* no big deal */
385 #if defined(ENCRYPTION)
386 case TELOPT_ENCRYPT: /* encryption variable option */
387 #endif
388 new_state_ok = 1;
389 break;
391 case TELOPT_NEW_ENVIRON: /* New environment variable option */
392 #ifdef OLD_ENVIRON
393 if (my_state_is_will(TELOPT_OLD_ENVIRON))
394 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
395 goto env_common;
396 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
397 if (my_state_is_will(TELOPT_NEW_ENVIRON))
398 break; /* Don't enable if new one is in use! */
399 env_common:
400 telopt_environ = option;
401 #endif
402 new_state_ok = 1;
403 break;
405 #if defined(AUTHENTICATION)
406 case TELOPT_AUTHENTICATION:
407 if (autologin)
408 new_state_ok = 1;
409 break;
410 #endif
412 case TELOPT_XDISPLOC: /* X Display location */
413 if (env_getvalue((unsigned char *)"DISPLAY"))
414 new_state_ok = 1;
415 break;
417 case TELOPT_LINEMODE:
418 #ifdef KLUDGELINEMODE
419 kludgelinemode = 0;
420 send_do(TELOPT_SGA, 1);
421 #endif
422 set_my_want_state_will(TELOPT_LINEMODE);
423 send_will(option, 0);
424 set_my_state_will(TELOPT_LINEMODE);
425 slc_init();
426 return;
428 case TELOPT_ECHO: /* We're never going to echo... */
429 default:
430 break;
433 if (new_state_ok) {
434 set_my_want_state_will(option);
435 send_will(option, 0);
436 setconnmode(0); /* Set new tty mode */
437 } else {
438 will_wont_resp[option]++;
439 send_wont(option, 0);
441 } else {
443 * Handle options that need more things done after the
444 * other side has acknowledged the option.
446 switch (option) {
447 case TELOPT_LINEMODE:
448 #ifdef KLUDGELINEMODE
449 kludgelinemode = 0;
450 send_do(TELOPT_SGA, 1);
451 #endif
452 set_my_state_will(option);
453 slc_init();
454 send_do(TELOPT_SGA, 0);
455 return;
459 set_my_state_will(option);
462 static void
463 dontoption(int option)
466 if (will_wont_resp[option]) {
467 --will_wont_resp[option];
468 if (will_wont_resp[option] && my_state_is_wont(option))
469 --will_wont_resp[option];
472 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
473 switch (option) {
474 case TELOPT_LINEMODE:
475 linemode = 0; /* put us back to the default state */
476 break;
477 #ifdef OLD_ENVIRON
478 case TELOPT_NEW_ENVIRON:
480 * The new environ option wasn't recognized, try
481 * the old one.
483 send_will(TELOPT_OLD_ENVIRON, 1);
484 telopt_environ = TELOPT_OLD_ENVIRON;
485 break;
486 #endif
487 #if 0
488 #ifdef ENCRYPTION
489 case TELOPT_ENCRYPT:
490 encrypt_not();
491 break;
492 #endif
493 #endif
495 /* we always accept a DONT */
496 set_my_want_state_wont(option);
497 if (my_state_is_will(option))
498 send_wont(option, 0);
499 setconnmode(0); /* Set new tty mode */
501 set_my_state_wont(option);
505 * Given a buffer returned by tgetent(), this routine will turn
506 * the pipe seperated list of names in the buffer into an array
507 * of pointers to null terminated names. We toss out any bad,
508 * duplicate, or verbose names (names with spaces).
511 static char *name_unknown = "UNKNOWN";
512 static char *unknown[] = { 0, 0 };
514 static char **
515 mklist(char *buf, char *name)
517 int n;
518 char c, *cp, **argvp, *cp2, **argv, **avt;
520 if (name) {
521 if ((int)strlen(name) > 40) {
522 name = 0;
523 unknown[0] = name_unknown;
524 } else {
525 unknown[0] = name;
526 strupr(name);
528 } else
529 unknown[0] = name_unknown;
531 * Count up the number of names.
533 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
534 if (*cp == '|')
535 n++;
538 * Allocate an array to put the name pointers into
540 argv = (char **)malloc((n+3)*sizeof(char *));
541 if (argv == 0)
542 return(unknown);
545 * Fill up the array of pointers to names.
547 *argv = 0;
548 argvp = argv+1;
549 n = 0;
550 for (cp = cp2 = buf; (c = *cp); cp++) {
551 if (c == '|' || c == ':') {
552 *cp++ = '\0';
554 * Skip entries that have spaces or are over 40
555 * characters long. If this is our environment
556 * name, then put it up front. Otherwise, as
557 * long as this is not a duplicate name (case
558 * insensitive) add it to the list.
560 if (n || (cp - cp2 > 41))
562 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
563 *argv = cp2;
564 else if (is_unique(cp2, argv+1, argvp))
565 *argvp++ = cp2;
566 if (c == ':')
567 break;
569 * Skip multiple delimiters. Reset cp2 to
570 * the beginning of the next name. Reset n,
571 * the flag for names with spaces.
573 while ((c = *cp) == '|')
574 cp++;
575 cp2 = cp;
576 n = 0;
579 * Skip entries with spaces or non-ascii values.
580 * Convert lower case letters to upper case.
582 #define ISASCII(c) (!((c)&0x80))
583 if ((c == ' ') || !ISASCII(c))
584 n = 1;
585 else if (islower((unsigned char)c))
586 *cp = toupper(c);
590 * Check for an old V6 2 character name. If the second
591 * name points to the beginning of the buffer, and is
592 * only 2 characters long, move it to the end of the array.
594 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
595 --argvp;
596 for (avt = &argv[1]; avt < argvp; avt++)
597 *avt = *(avt+1);
598 *argvp++ = buf;
602 * Duplicate last name, for TTYPE option, and null
603 * terminate the array. If we didn't find a match on
604 * our terminal name, put that name at the beginning.
606 cp = *(argvp-1);
607 *argvp++ = cp;
608 *argvp = 0;
610 if (*argv == 0) {
611 if (name)
612 *argv = name;
613 else {
614 --argvp;
615 for (avt = argv; avt < argvp; avt++)
616 *avt = *(avt+1);
619 if (*argv)
620 return(argv);
621 else
622 return(unknown);
625 static int
626 is_unique(char *name, char **as, char **ae)
628 char **ap;
629 int n;
631 n = strlen(name) + 1;
632 for (ap = as; ap < ae; ap++)
633 if (strncasecmp(*ap, name, n) == 0)
634 return(0);
635 return (1);
638 static char termbuf[1024];
640 static int
641 telnet_setupterm(const char *tname, int fd, int *errp)
643 #ifdef HAVE_TGETENT
644 if (tgetent(termbuf, tname) == 1) {
645 termbuf[1023] = '\0';
646 if (errp)
647 *errp = 1;
648 return(0);
650 if (errp)
651 *errp = 0;
652 return(-1);
653 #else
654 strlcpy(termbuf, tname, sizeof(termbuf));
655 if(errp) *errp = 1;
656 return 0;
657 #endif
660 int resettermname = 1;
662 static char *
663 gettermname()
665 char *tname;
666 static char **tnamep = 0;
667 static char **next;
668 int err;
670 if (resettermname) {
671 resettermname = 0;
672 if (tnamep && tnamep != unknown)
673 free(tnamep);
674 if ((tname = (char *)env_getvalue((unsigned char *)"TERM")) &&
675 telnet_setupterm(tname, 1, &err) == 0) {
676 tnamep = mklist(termbuf, tname);
677 } else {
678 if (tname && ((int)strlen(tname) <= 40)) {
679 unknown[0] = tname;
680 strupr(tname);
681 } else
682 unknown[0] = name_unknown;
683 tnamep = unknown;
685 next = tnamep;
687 if (*next == 0)
688 next = tnamep;
689 return(*next++);
692 * suboption()
694 * Look at the sub-option buffer, and try to be helpful to the other
695 * side.
697 * Currently we recognize:
699 * Terminal type, send request.
700 * Terminal speed (send request).
701 * Local flow control (is request).
702 * Linemode
705 static void
706 suboption()
708 unsigned char subchar;
710 printsub('<', subbuffer, SB_LEN()+2);
711 switch (subchar = SB_GET()) {
712 case TELOPT_TTYPE:
713 if (my_want_state_is_wont(TELOPT_TTYPE))
714 return;
715 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
716 return;
717 } else {
718 char *name;
719 unsigned char temp[50];
720 int len;
722 name = gettermname();
723 len = strlen(name) + 4 + 2;
724 if (len < NETROOM()) {
725 snprintf((char *)temp, sizeof(temp),
726 "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
727 TELQUAL_IS, name, IAC, SE);
728 ring_supply_data(&netoring, temp, len);
729 printsub('>', &temp[2], len-2);
730 } else {
731 ExitString("No room in buffer for terminal type.\n", 1);
732 /*NOTREACHED*/
735 break;
736 case TELOPT_TSPEED:
737 if (my_want_state_is_wont(TELOPT_TSPEED))
738 return;
739 if (SB_EOF())
740 return;
741 if (SB_GET() == TELQUAL_SEND) {
742 long output_speed, input_speed;
743 unsigned char temp[50];
744 int len;
746 TerminalSpeeds(&input_speed, &output_speed);
748 snprintf((char *)temp, sizeof(temp),
749 "%c%c%c%c%u,%u%c%c", IAC, SB, TELOPT_TSPEED,
750 TELQUAL_IS,
751 (unsigned)output_speed,
752 (unsigned)input_speed, IAC, SE);
753 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
755 if (len < NETROOM()) {
756 ring_supply_data(&netoring, temp, len);
757 printsub('>', temp+2, len - 2);
759 /*@*/ else printf("lm_will: not enough room in buffer\n");
761 break;
762 case TELOPT_LFLOW:
763 if (my_want_state_is_wont(TELOPT_LFLOW))
764 return;
765 if (SB_EOF())
766 return;
767 switch(SB_GET()) {
768 case LFLOW_RESTART_ANY:
769 restartany = 1;
770 break;
771 case LFLOW_RESTART_XON:
772 restartany = 0;
773 break;
774 case LFLOW_ON:
775 localflow = 1;
776 break;
777 case LFLOW_OFF:
778 localflow = 0;
779 break;
780 default:
781 return;
783 setcommandmode();
784 setconnmode(0);
785 break;
787 case TELOPT_LINEMODE:
788 if (my_want_state_is_wont(TELOPT_LINEMODE))
789 return;
790 if (SB_EOF())
791 return;
792 switch (SB_GET()) {
793 case WILL:
794 lm_will(subpointer, SB_LEN());
795 break;
796 case WONT:
797 lm_wont(subpointer, SB_LEN());
798 break;
799 case DO:
800 lm_do(subpointer, SB_LEN());
801 break;
802 case DONT:
803 lm_dont(subpointer, SB_LEN());
804 break;
805 case LM_SLC:
806 slc(subpointer, SB_LEN());
807 break;
808 case LM_MODE:
809 lm_mode(subpointer, SB_LEN(), 0);
810 break;
811 default:
812 break;
814 break;
816 #ifdef OLD_ENVIRON
817 case TELOPT_OLD_ENVIRON:
818 #endif
819 case TELOPT_NEW_ENVIRON:
820 if (SB_EOF())
821 return;
822 switch(SB_PEEK()) {
823 case TELQUAL_IS:
824 case TELQUAL_INFO:
825 if (my_want_state_is_dont(subchar))
826 return;
827 break;
828 case TELQUAL_SEND:
829 if (my_want_state_is_wont(subchar)) {
830 return;
832 break;
833 default:
834 return;
836 env_opt(subpointer, SB_LEN());
837 break;
839 case TELOPT_XDISPLOC:
840 if (my_want_state_is_wont(TELOPT_XDISPLOC))
841 return;
842 if (SB_EOF())
843 return;
844 if (SB_GET() == TELQUAL_SEND) {
845 unsigned char temp[50], *dp;
846 int len;
848 if ((dp = env_getvalue((unsigned char *)"DISPLAY")) == NULL) {
850 * Something happened, we no longer have a DISPLAY
851 * variable. So, turn off the option.
853 send_wont(TELOPT_XDISPLOC, 1);
854 break;
856 snprintf((char *)temp, sizeof(temp),
857 "%c%c%c%c%s%c%c", IAC, SB, TELOPT_XDISPLOC,
858 TELQUAL_IS, dp, IAC, SE);
859 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
861 if (len < NETROOM()) {
862 ring_supply_data(&netoring, temp, len);
863 printsub('>', temp+2, len - 2);
865 /*@*/ else printf("lm_will: not enough room in buffer\n");
867 break;
869 #if defined(AUTHENTICATION)
870 case TELOPT_AUTHENTICATION: {
871 if (!autologin)
872 break;
873 if (SB_EOF())
874 return;
875 switch(SB_GET()) {
876 case TELQUAL_IS:
877 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
878 return;
879 auth_is(subpointer, SB_LEN());
880 break;
881 case TELQUAL_SEND:
882 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
883 return;
884 auth_send(subpointer, SB_LEN());
885 break;
886 case TELQUAL_REPLY:
887 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
888 return;
889 auth_reply(subpointer, SB_LEN());
890 break;
891 case TELQUAL_NAME:
892 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
893 return;
894 auth_name(subpointer, SB_LEN());
895 break;
898 break;
899 #endif
900 #if defined(ENCRYPTION)
901 case TELOPT_ENCRYPT:
902 if (SB_EOF())
903 return;
904 switch(SB_GET()) {
905 case ENCRYPT_START:
906 if (my_want_state_is_dont(TELOPT_ENCRYPT))
907 return;
908 encrypt_start(subpointer, SB_LEN());
909 break;
910 case ENCRYPT_END:
911 if (my_want_state_is_dont(TELOPT_ENCRYPT))
912 return;
913 encrypt_end();
914 break;
915 case ENCRYPT_SUPPORT:
916 if (my_want_state_is_wont(TELOPT_ENCRYPT))
917 return;
918 encrypt_support(subpointer, SB_LEN());
919 break;
920 case ENCRYPT_REQSTART:
921 if (my_want_state_is_wont(TELOPT_ENCRYPT))
922 return;
923 encrypt_request_start(subpointer, SB_LEN());
924 break;
925 case ENCRYPT_REQEND:
926 if (my_want_state_is_wont(TELOPT_ENCRYPT))
927 return;
929 * We can always send an REQEND so that we cannot
930 * get stuck encrypting. We should only get this
931 * if we have been able to get in the correct mode
932 * anyhow.
934 encrypt_request_end();
935 break;
936 case ENCRYPT_IS:
937 if (my_want_state_is_dont(TELOPT_ENCRYPT))
938 return;
939 encrypt_is(subpointer, SB_LEN());
940 break;
941 case ENCRYPT_REPLY:
942 if (my_want_state_is_wont(TELOPT_ENCRYPT))
943 return;
944 encrypt_reply(subpointer, SB_LEN());
945 break;
946 case ENCRYPT_ENC_KEYID:
947 if (my_want_state_is_dont(TELOPT_ENCRYPT))
948 return;
949 encrypt_enc_keyid(subpointer, SB_LEN());
950 break;
951 case ENCRYPT_DEC_KEYID:
952 if (my_want_state_is_wont(TELOPT_ENCRYPT))
953 return;
954 encrypt_dec_keyid(subpointer, SB_LEN());
955 break;
956 default:
957 break;
959 break;
960 #endif
961 default:
962 break;
966 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
968 void
969 lm_will(unsigned char *cmd, int len)
971 if (len < 1) {
972 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
973 return;
975 switch(cmd[0]) {
976 case LM_FORWARDMASK: /* We shouldn't ever get this... */
977 default:
978 str_lm[3] = DONT;
979 str_lm[4] = cmd[0];
980 if (NETROOM() > sizeof(str_lm)) {
981 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
982 printsub('>', &str_lm[2], sizeof(str_lm)-2);
984 /*@*/ else printf("lm_will: not enough room in buffer\n");
985 break;
989 void
990 lm_wont(unsigned char *cmd, int len)
992 if (len < 1) {
993 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
994 return;
996 switch(cmd[0]) {
997 case LM_FORWARDMASK: /* We shouldn't ever get this... */
998 default:
999 /* We are always DONT, so don't respond */
1000 return;
1004 void
1005 lm_do(unsigned char *cmd, int len)
1007 if (len < 1) {
1008 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1009 return;
1011 switch(cmd[0]) {
1012 case LM_FORWARDMASK:
1013 default:
1014 str_lm[3] = WONT;
1015 str_lm[4] = cmd[0];
1016 if (NETROOM() > sizeof(str_lm)) {
1017 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1018 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1020 /*@*/ else printf("lm_do: not enough room in buffer\n");
1021 break;
1025 void
1026 lm_dont(unsigned char *cmd, int len)
1028 if (len < 1) {
1029 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1030 return;
1032 switch(cmd[0]) {
1033 case LM_FORWARDMASK:
1034 default:
1035 /* we are always WONT, so don't respond */
1036 break;
1040 static unsigned char str_lm_mode[] = {
1041 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1044 void
1045 lm_mode(unsigned char *cmd, int len, int init)
1047 if (len != 1)
1048 return;
1049 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1050 return;
1051 if (*cmd&MODE_ACK)
1052 return;
1053 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1054 str_lm_mode[4] = linemode;
1055 if (!init)
1056 str_lm_mode[4] |= MODE_ACK;
1057 if (NETROOM() > sizeof(str_lm_mode)) {
1058 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1059 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1061 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1062 setconnmode(0); /* set changed mode */
1068 * slc()
1069 * Handle special character suboption of LINEMODE.
1072 struct spc {
1073 cc_t val;
1074 cc_t *valp;
1075 char flags; /* Current flags & level */
1076 char mylevel; /* Maximum level & flags */
1077 } spc_data[NSLC+1];
1079 #define SLC_IMPORT 0
1080 #define SLC_EXPORT 1
1081 #define SLC_RVALUE 2
1082 static int slc_mode = SLC_EXPORT;
1084 void
1085 slc_init()
1087 struct spc *spcp;
1089 localchars = 1;
1090 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1091 spcp->val = 0;
1092 spcp->valp = 0;
1093 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1096 #define initfunc(func, flags) { \
1097 spcp = &spc_data[func]; \
1098 if ((spcp->valp = tcval(func))) { \
1099 spcp->val = *spcp->valp; \
1100 spcp->mylevel = SLC_VARIABLE|flags; \
1101 } else { \
1102 spcp->val = 0; \
1103 spcp->mylevel = SLC_DEFAULT; \
1107 initfunc(SLC_SYNCH, 0);
1108 /* No BRK */
1109 initfunc(SLC_AO, 0);
1110 initfunc(SLC_AYT, 0);
1111 /* No EOR */
1112 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1113 initfunc(SLC_EOF, 0);
1114 initfunc(SLC_SUSP, SLC_FLUSHIN);
1115 initfunc(SLC_EC, 0);
1116 initfunc(SLC_EL, 0);
1117 initfunc(SLC_EW, 0);
1118 initfunc(SLC_RP, 0);
1119 initfunc(SLC_LNEXT, 0);
1120 initfunc(SLC_XON, 0);
1121 initfunc(SLC_XOFF, 0);
1122 initfunc(SLC_FORW1, 0);
1123 initfunc(SLC_FORW2, 0);
1124 /* No FORW2 */
1126 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1127 #undef initfunc
1129 if (slc_mode == SLC_EXPORT)
1130 slc_export();
1131 else
1132 slc_import(1);
1136 void
1137 slcstate()
1139 printf("Special characters are %s values\n",
1140 slc_mode == SLC_IMPORT ? "remote default" :
1141 slc_mode == SLC_EXPORT ? "local" :
1142 "remote");
1145 void
1146 slc_mode_export()
1148 slc_mode = SLC_EXPORT;
1149 if (my_state_is_will(TELOPT_LINEMODE))
1150 slc_export();
1153 void
1154 slc_mode_import(int def)
1156 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1157 if (my_state_is_will(TELOPT_LINEMODE))
1158 slc_import(def);
1161 unsigned char slc_import_val[] = {
1162 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1164 unsigned char slc_import_def[] = {
1165 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1168 void
1169 slc_import(int def)
1171 if (NETROOM() > sizeof(slc_import_val)) {
1172 if (def) {
1173 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1174 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1175 } else {
1176 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1177 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1180 /*@*/ else printf("slc_import: not enough room\n");
1183 void
1184 slc_export()
1186 struct spc *spcp;
1188 TerminalDefaultChars();
1190 slc_start_reply();
1191 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1192 if (spcp->mylevel != SLC_NOSUPPORT) {
1193 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1194 spcp->flags = SLC_NOSUPPORT;
1195 else
1196 spcp->flags = spcp->mylevel;
1197 if (spcp->valp)
1198 spcp->val = *spcp->valp;
1199 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1202 slc_end_reply();
1203 slc_update();
1204 setconnmode(1); /* Make sure the character values are set */
1207 void
1208 slc(unsigned char *cp, int len)
1210 struct spc *spcp;
1211 int func,level;
1213 slc_start_reply();
1215 for (; len >= 3; len -=3, cp +=3) {
1217 func = cp[SLC_FUNC];
1219 if (func == 0) {
1221 * Client side: always ignore 0 function.
1223 continue;
1225 if (func > NSLC) {
1226 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1227 slc_add_reply(func, SLC_NOSUPPORT, 0);
1228 continue;
1231 spcp = &spc_data[func];
1233 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1235 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1236 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1237 continue;
1240 if (level == (SLC_DEFAULT|SLC_ACK)) {
1242 * This is an error condition, the SLC_ACK
1243 * bit should never be set for the SLC_DEFAULT
1244 * level. Our best guess to recover is to
1245 * ignore the SLC_ACK bit.
1247 cp[SLC_FLAGS] &= ~SLC_ACK;
1250 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1251 spcp->val = (cc_t)cp[SLC_VALUE];
1252 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1253 continue;
1256 level &= ~SLC_ACK;
1258 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1259 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1260 spcp->val = (cc_t)cp[SLC_VALUE];
1262 if (level == SLC_DEFAULT) {
1263 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1264 spcp->flags = spcp->mylevel;
1265 else
1266 spcp->flags = SLC_NOSUPPORT;
1268 slc_add_reply(func, spcp->flags, spcp->val);
1270 slc_end_reply();
1271 if (slc_update())
1272 setconnmode(1); /* set the new character values */
1275 void
1276 slc_check()
1278 struct spc *spcp;
1280 slc_start_reply();
1281 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1282 if (spcp->valp && spcp->val != *spcp->valp) {
1283 spcp->val = *spcp->valp;
1284 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1285 spcp->flags = SLC_NOSUPPORT;
1286 else
1287 spcp->flags = spcp->mylevel;
1288 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1291 slc_end_reply();
1292 setconnmode(1);
1296 unsigned char slc_reply[128];
1297 unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
1298 unsigned char *slc_replyp;
1300 void
1301 slc_start_reply()
1303 slc_replyp = slc_reply;
1304 *slc_replyp++ = IAC;
1305 *slc_replyp++ = SB;
1306 *slc_replyp++ = TELOPT_LINEMODE;
1307 *slc_replyp++ = LM_SLC;
1310 void
1311 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1313 /* A sequence of up to 6 bytes my be written for this member of the SLC
1314 * suboption list by this function. The end of negotiation command,
1315 * which is written by slc_end_reply(), will require 2 additional
1316 * bytes. Do not proceed unless there is sufficient space for these
1317 * items.
1319 if (&slc_replyp[6+2] > slc_reply_eom)
1320 return;
1321 if ((*slc_replyp++ = func) == IAC)
1322 *slc_replyp++ = IAC;
1323 if ((*slc_replyp++ = flags) == IAC)
1324 *slc_replyp++ = IAC;
1325 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1326 *slc_replyp++ = IAC;
1329 void
1330 slc_end_reply()
1332 int len;
1334 /* The end of negotiation command requires 2 bytes. */
1335 if (&slc_replyp[2] > slc_reply_eom)
1336 return;
1337 *slc_replyp++ = IAC;
1338 *slc_replyp++ = SE;
1339 len = slc_replyp - slc_reply;
1340 if (len <= 6)
1341 return;
1342 if (NETROOM() > len) {
1343 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1344 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1346 /*@*/else printf("slc_end_reply: not enough room\n");
1350 slc_update()
1352 struct spc *spcp;
1353 int need_update = 0;
1355 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1356 if (!(spcp->flags&SLC_ACK))
1357 continue;
1358 spcp->flags &= ~SLC_ACK;
1359 if (spcp->valp && (*spcp->valp != spcp->val)) {
1360 *spcp->valp = spcp->val;
1361 need_update = 1;
1364 return(need_update);
1367 #ifdef OLD_ENVIRON
1368 # define old_env_var OLD_ENV_VAR
1369 # define old_env_value OLD_ENV_VALUE
1370 #endif
1372 void
1373 env_opt(unsigned char *buf, int len)
1375 unsigned char *ep = 0, *epc = 0;
1376 int i;
1378 switch(buf[0]&0xff) {
1379 case TELQUAL_SEND:
1380 env_opt_start();
1381 if (len == 1) {
1382 env_opt_add(NULL);
1383 } else for (i = 1; i < len; i++) {
1384 switch (buf[i]&0xff) {
1385 #ifdef OLD_ENVIRON
1386 case OLD_ENV_VAR:
1387 case OLD_ENV_VALUE:
1389 * Although OLD_ENV_VALUE is not legal, we will
1390 * still recognize it, just in case it is an
1391 * old server that has VAR & VALUE mixed up...
1393 /* FALL THROUGH */
1394 #else
1395 case NEW_ENV_VAR:
1396 #endif
1397 case ENV_USERVAR:
1398 if (ep) {
1399 *epc = 0;
1400 env_opt_add(ep);
1402 ep = epc = &buf[i+1];
1403 break;
1404 case ENV_ESC:
1405 i++;
1406 /*FALL THROUGH*/
1407 default:
1408 if (epc)
1409 *epc++ = buf[i];
1410 break;
1413 if (ep) {
1414 *epc = 0;
1415 env_opt_add(ep);
1417 env_opt_end(1);
1418 break;
1420 case TELQUAL_IS:
1421 case TELQUAL_INFO:
1422 /* Ignore for now. We shouldn't get it anyway. */
1423 break;
1425 default:
1426 break;
1430 #define OPT_REPLY_SIZE (2 * SUBBUFSIZE)
1431 unsigned char *opt_reply;
1432 unsigned char *opt_replyp;
1433 unsigned char *opt_replyend;
1435 void
1436 env_opt_start()
1438 if (opt_reply) {
1439 void *tmp = realloc (opt_reply, OPT_REPLY_SIZE);
1440 if (tmp != NULL) {
1441 opt_reply = tmp;
1442 } else {
1443 free (opt_reply);
1444 opt_reply = NULL;
1446 } else
1447 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1448 if (opt_reply == NULL) {
1449 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1450 opt_reply = opt_replyp = opt_replyend = NULL;
1451 return;
1453 opt_replyp = opt_reply;
1454 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1455 *opt_replyp++ = IAC;
1456 *opt_replyp++ = SB;
1457 *opt_replyp++ = telopt_environ;
1458 *opt_replyp++ = TELQUAL_IS;
1461 void
1462 env_opt_start_info()
1464 env_opt_start();
1465 if (opt_replyp)
1466 opt_replyp[-1] = TELQUAL_INFO;
1469 void
1470 env_opt_add(unsigned char *ep)
1472 unsigned char *vp, c;
1474 if (opt_reply == NULL) /*XXX*/
1475 return; /*XXX*/
1477 if (ep == NULL || *ep == '\0') {
1478 /* Send user defined variables first. */
1479 env_default(1, 0);
1480 while ((ep = env_default(0, 0)))
1481 env_opt_add(ep);
1483 /* Now add the list of well know variables. */
1484 env_default(1, 1);
1485 while ((ep = env_default(0, 1)))
1486 env_opt_add(ep);
1487 return;
1489 vp = env_getvalue(ep);
1490 if (opt_replyp + (vp ? 2 * strlen((char *)vp) : 0) +
1491 2 * strlen((char *)ep) + 6 > opt_replyend)
1493 int len;
1494 void *tmp;
1495 opt_replyend += OPT_REPLY_SIZE;
1496 len = opt_replyend - opt_reply;
1497 tmp = realloc(opt_reply, len);
1498 if (tmp == NULL) {
1499 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1500 opt_reply = opt_replyp = opt_replyend = NULL;
1501 return;
1503 opt_reply = tmp;
1504 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1505 opt_replyend = opt_reply + len;
1507 if (opt_welldefined((char *)ep)) {
1508 #ifdef OLD_ENVIRON
1509 if (telopt_environ == TELOPT_OLD_ENVIRON)
1510 *opt_replyp++ = old_env_var;
1511 else
1512 #endif
1513 *opt_replyp++ = NEW_ENV_VAR;
1514 } else
1515 *opt_replyp++ = ENV_USERVAR;
1516 for (;;) {
1517 while ((c = *ep++)) {
1518 if (opt_replyp + (2 + 2) > opt_replyend)
1519 return;
1520 switch(c&0xff) {
1521 case IAC:
1522 *opt_replyp++ = IAC;
1523 break;
1524 case NEW_ENV_VAR:
1525 case NEW_ENV_VALUE:
1526 case ENV_ESC:
1527 case ENV_USERVAR:
1528 *opt_replyp++ = ENV_ESC;
1529 break;
1531 *opt_replyp++ = c;
1533 if ((ep = vp)) {
1534 if (opt_replyp + (1 + 2 + 2) > opt_replyend)
1535 return;
1536 #ifdef OLD_ENVIRON
1537 if (telopt_environ == TELOPT_OLD_ENVIRON)
1538 *opt_replyp++ = old_env_value;
1539 else
1540 #endif
1541 *opt_replyp++ = NEW_ENV_VALUE;
1542 vp = NULL;
1543 } else
1544 break;
1549 opt_welldefined(char *ep)
1551 if ((strcmp(ep, "USER") == 0) ||
1552 (strcmp(ep, "DISPLAY") == 0) ||
1553 (strcmp(ep, "PRINTER") == 0) ||
1554 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1555 (strcmp(ep, "JOB") == 0) ||
1556 (strcmp(ep, "ACCT") == 0))
1557 return(1);
1558 return(0);
1561 void
1562 env_opt_end(int emptyok)
1564 int len;
1566 if (opt_replyp + 2 > opt_replyend)
1567 return;
1568 len = opt_replyp + 2 - opt_reply;
1569 if (emptyok || len > 6) {
1570 *opt_replyp++ = IAC;
1571 *opt_replyp++ = SE;
1572 if (NETROOM() > len) {
1573 ring_supply_data(&netoring, opt_reply, len);
1574 printsub('>', &opt_reply[2], len - 2);
1576 /*@*/ else printf("slc_end_reply: not enough room\n");
1578 if (opt_reply) {
1579 free(opt_reply);
1580 opt_reply = opt_replyp = opt_replyend = NULL;
1587 telrcv(void)
1589 int c;
1590 int scc;
1591 unsigned char *sbp = NULL;
1592 int count;
1593 int returnValue = 0;
1595 scc = 0;
1596 count = 0;
1597 while (TTYROOM() > 2) {
1598 if (scc == 0) {
1599 if (count) {
1600 ring_consumed(&netiring, count);
1601 returnValue = 1;
1602 count = 0;
1604 sbp = netiring.consume;
1605 scc = ring_full_consecutive(&netiring);
1606 if (scc == 0) {
1607 /* No more data coming in */
1608 break;
1612 c = *sbp++ & 0xff, scc--; count++;
1613 #if defined(ENCRYPTION)
1614 if (decrypt_input)
1615 c = (*decrypt_input)(c);
1616 #endif
1618 switch (telrcv_state) {
1620 case TS_CR:
1621 telrcv_state = TS_DATA;
1622 if (c == '\0') {
1623 break; /* Ignore \0 after CR */
1625 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1626 TTYADD(c);
1627 break;
1629 /* Else, fall through */
1631 case TS_DATA:
1632 if (c == IAC) {
1633 telrcv_state = TS_IAC;
1634 break;
1637 * The 'crmod' hack (see following) is needed
1638 * since we can't set CRMOD on output only.
1639 * Machines like MULTICS like to send \r without
1640 * \n; since we must turn off CRMOD to get proper
1641 * input, the mapping is done here (sigh).
1643 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1644 if (scc > 0) {
1645 c = *sbp&0xff;
1646 #if defined(ENCRYPTION)
1647 if (decrypt_input)
1648 c = (*decrypt_input)(c);
1649 #endif
1650 if (c == 0) {
1651 sbp++, scc--; count++;
1652 /* a "true" CR */
1653 TTYADD('\r');
1654 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1655 (c == '\n')) {
1656 sbp++, scc--; count++;
1657 TTYADD('\n');
1658 } else {
1659 #if defined(ENCRYPTION)
1660 if (decrypt_input)
1661 (*decrypt_input)(-1);
1662 #endif
1664 TTYADD('\r');
1665 if (crmod) {
1666 TTYADD('\n');
1669 } else {
1670 telrcv_state = TS_CR;
1671 TTYADD('\r');
1672 if (crmod) {
1673 TTYADD('\n');
1676 } else {
1677 TTYADD(c);
1679 continue;
1681 case TS_IAC:
1682 process_iac:
1683 switch (c) {
1685 case WILL:
1686 telrcv_state = TS_WILL;
1687 continue;
1689 case WONT:
1690 telrcv_state = TS_WONT;
1691 continue;
1693 case DO:
1694 telrcv_state = TS_DO;
1695 continue;
1697 case DONT:
1698 telrcv_state = TS_DONT;
1699 continue;
1701 case DM:
1703 * We may have missed an urgent notification,
1704 * so make sure we flush whatever is in the
1705 * buffer currently.
1707 printoption("RCVD", IAC, DM);
1708 SYNCHing = 1;
1709 ttyflush(1);
1710 SYNCHing = stilloob();
1711 settimer(gotDM);
1712 break;
1714 case SB:
1715 SB_CLEAR();
1716 telrcv_state = TS_SB;
1717 continue;
1720 case IAC:
1721 TTYADD(IAC);
1722 break;
1724 case NOP:
1725 case GA:
1726 default:
1727 printoption("RCVD", IAC, c);
1728 break;
1730 telrcv_state = TS_DATA;
1731 continue;
1733 case TS_WILL:
1734 printoption("RCVD", WILL, c);
1735 willoption(c);
1736 telrcv_state = TS_DATA;
1737 continue;
1739 case TS_WONT:
1740 printoption("RCVD", WONT, c);
1741 wontoption(c);
1742 telrcv_state = TS_DATA;
1743 continue;
1745 case TS_DO:
1746 printoption("RCVD", DO, c);
1747 dooption(c);
1748 if (c == TELOPT_NAWS) {
1749 sendnaws();
1750 } else if (c == TELOPT_LFLOW) {
1751 localflow = 1;
1752 setcommandmode();
1753 setconnmode(0);
1755 telrcv_state = TS_DATA;
1756 continue;
1758 case TS_DONT:
1759 printoption("RCVD", DONT, c);
1760 dontoption(c);
1761 flushline = 1;
1762 setconnmode(0); /* set new tty mode (maybe) */
1763 telrcv_state = TS_DATA;
1764 continue;
1766 case TS_SB:
1767 if (c == IAC) {
1768 telrcv_state = TS_SE;
1769 } else {
1770 SB_ACCUM(c);
1772 continue;
1774 case TS_SE:
1775 if (c != SE) {
1776 if (c != IAC) {
1778 * This is an error. We only expect to get
1779 * "IAC IAC" or "IAC SE". Several things may
1780 * have happend. An IAC was not doubled, the
1781 * IAC SE was left off, or another option got
1782 * inserted into the suboption are all possibilities.
1783 * If we assume that the IAC was not doubled,
1784 * and really the IAC SE was left off, we could
1785 * get into an infinate loop here. So, instead,
1786 * we terminate the suboption, and process the
1787 * partial suboption if we can.
1789 SB_ACCUM(IAC);
1790 SB_ACCUM(c);
1791 subpointer -= 2;
1792 SB_TERM();
1794 printoption("In SUBOPTION processing, RCVD", IAC, c);
1795 suboption(); /* handle sub-option */
1796 telrcv_state = TS_IAC;
1797 goto process_iac;
1799 SB_ACCUM(c);
1800 telrcv_state = TS_SB;
1801 } else {
1802 SB_ACCUM(IAC);
1803 SB_ACCUM(SE);
1804 subpointer -= 2;
1805 SB_TERM();
1806 suboption(); /* handle sub-option */
1807 telrcv_state = TS_DATA;
1811 if (count)
1812 ring_consumed(&netiring, count);
1813 return returnValue||count;
1816 static int bol = 1, local = 0;
1819 rlogin_susp(void)
1821 if (local) {
1822 local = 0;
1823 bol = 1;
1824 command(0, "z\n", 2);
1825 return(1);
1827 return(0);
1830 static int
1831 telsnd()
1833 int tcc;
1834 int count;
1835 int returnValue = 0;
1836 unsigned char *tbp = NULL;
1838 tcc = 0;
1839 count = 0;
1840 while (NETROOM() > 2) {
1841 int sc;
1842 int c;
1844 if (tcc == 0) {
1845 if (count) {
1846 ring_consumed(&ttyiring, count);
1847 returnValue = 1;
1848 count = 0;
1850 tbp = ttyiring.consume;
1851 tcc = ring_full_consecutive(&ttyiring);
1852 if (tcc == 0) {
1853 break;
1856 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1857 if (rlogin != _POSIX_VDISABLE) {
1858 if (bol) {
1859 bol = 0;
1860 if (sc == rlogin) {
1861 local = 1;
1862 continue;
1864 } else if (local) {
1865 local = 0;
1866 if (sc == '.' || c == termEofChar) {
1867 bol = 1;
1868 command(0, "close\n", 6);
1869 continue;
1871 if (sc == termSuspChar) {
1872 bol = 1;
1873 command(0, "z\n", 2);
1874 continue;
1876 if (sc == escape) {
1877 command(0, (char *)tbp, tcc);
1878 bol = 1;
1879 count += tcc;
1880 tcc = 0;
1881 flushline = 1;
1882 break;
1884 if (sc != rlogin) {
1885 ++tcc;
1886 --tbp;
1887 --count;
1888 c = sc = rlogin;
1891 if ((sc == '\n') || (sc == '\r'))
1892 bol = 1;
1893 } else if (sc == escape) {
1895 * Double escape is a pass through of a single escape character.
1897 if (tcc && strip(*tbp) == escape) {
1898 tbp++;
1899 tcc--;
1900 count++;
1901 bol = 0;
1902 } else {
1903 command(0, (char *)tbp, tcc);
1904 bol = 1;
1905 count += tcc;
1906 tcc = 0;
1907 flushline = 1;
1908 break;
1910 } else
1911 bol = 0;
1912 #ifdef KLUDGELINEMODE
1913 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1914 if (tcc > 0 && strip(*tbp) == echoc) {
1915 tcc--; tbp++; count++;
1916 } else {
1917 dontlecho = !dontlecho;
1918 settimer(echotoggle);
1919 setconnmode(0);
1920 flushline = 1;
1921 break;
1924 #endif
1925 if (MODE_LOCAL_CHARS(globalmode)) {
1926 if (TerminalSpecialChars(sc) == 0) {
1927 bol = 1;
1928 break;
1931 if (my_want_state_is_wont(TELOPT_BINARY)) {
1932 switch (c) {
1933 case '\n':
1935 * If we are in CRMOD mode (\r ==> \n)
1936 * on our local machine, then probably
1937 * a newline (unix) is CRLF (TELNET).
1939 if (MODE_LOCAL_CHARS(globalmode)) {
1940 NETADD('\r');
1942 NETADD('\n');
1943 bol = flushline = 1;
1944 break;
1945 case '\r':
1946 if (!crlf) {
1947 NET2ADD('\r', '\0');
1948 } else {
1949 NET2ADD('\r', '\n');
1951 bol = flushline = 1;
1952 break;
1953 case IAC:
1954 NET2ADD(IAC, IAC);
1955 break;
1956 default:
1957 NETADD(c);
1958 break;
1960 } else if (c == IAC) {
1961 NET2ADD(IAC, IAC);
1962 } else {
1963 NETADD(c);
1966 if (count)
1967 ring_consumed(&ttyiring, count);
1968 return returnValue||count; /* Non-zero if we did anything */
1972 * Scheduler()
1974 * Try to do something.
1976 * If we do something useful, return 1; else return 0.
1982 Scheduler(int block) /* should we block in the select ? */
1984 /* One wants to be a bit careful about setting returnValue
1985 * to one, since a one implies we did some useful work,
1986 * and therefore probably won't be called to block next
1987 * time (TN3270 mode only).
1989 int returnValue;
1990 int netin, netout, netex, ttyin, ttyout;
1992 /* Decide which rings should be processed */
1994 netout = ring_full_count(&netoring) &&
1995 (flushline ||
1996 (my_want_state_is_wont(TELOPT_LINEMODE)
1997 #ifdef KLUDGELINEMODE
1998 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
1999 #endif
2000 ) ||
2001 my_want_state_is_will(TELOPT_BINARY));
2002 ttyout = ring_full_count(&ttyoring);
2004 ttyin = ring_empty_count(&ttyiring);
2006 netin = !ISend && ring_empty_count(&netiring);
2008 netex = !SYNCHing;
2010 /* If we have seen a signal recently, reset things */
2012 if (scheduler_lockout_tty) {
2013 ttyin = ttyout = 0;
2016 /* Call to system code to process rings */
2018 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2020 /* Now, look at the input rings, looking for work to do. */
2022 if (ring_full_count(&ttyiring)) {
2023 returnValue |= telsnd();
2026 if (ring_full_count(&netiring)) {
2027 returnValue |= telrcv();
2029 return returnValue;
2033 * Select from tty and network...
2035 void
2036 my_telnet(char *user)
2038 int printed_encrypt = 0;
2040 sys_telnet_init();
2042 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2044 static char local_host[256] = { 0 };
2046 if (!local_host[0]) {
2047 /* XXX - should be k_gethostname? */
2048 gethostname(local_host, sizeof(local_host));
2049 local_host[sizeof(local_host)-1] = 0;
2051 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2052 auth_encrypt_user(user);
2054 #endif
2055 if (telnetport) {
2056 #if defined(AUTHENTICATION)
2057 if (autologin)
2058 send_will(TELOPT_AUTHENTICATION, 1);
2059 #endif
2060 #if defined(ENCRYPTION)
2061 send_do(TELOPT_ENCRYPT, 1);
2062 send_will(TELOPT_ENCRYPT, 1);
2063 #endif
2064 send_do(TELOPT_SGA, 1);
2065 send_will(TELOPT_TTYPE, 1);
2066 send_will(TELOPT_NAWS, 1);
2067 send_will(TELOPT_TSPEED, 1);
2068 send_will(TELOPT_LFLOW, 1);
2069 send_will(TELOPT_LINEMODE, 1);
2070 send_will(TELOPT_NEW_ENVIRON, 1);
2071 send_do(TELOPT_STATUS, 1);
2072 if (env_getvalue((unsigned char *)"DISPLAY"))
2073 send_will(TELOPT_XDISPLOC, 1);
2074 if (binary)
2075 tel_enter_binary(binary);
2078 #ifdef ENCRYPTION
2080 * Note: we assume a tie to the authentication option here. This
2081 * is necessary so that authentication fails, we don't spin
2082 * forever.
2084 if (telnetport && wantencryption) {
2085 extern int auth_has_failed;
2086 time_t timeout = time(0) + 60;
2088 send_do(TELOPT_ENCRYPT, 1);
2089 send_will(TELOPT_ENCRYPT, 1);
2090 while (1) {
2091 if (my_want_state_is_wont(TELOPT_AUTHENTICATION)) {
2092 if (wantencryption == -1) {
2093 break;
2094 } else {
2095 printf("\nServer refused to negotiate authentication,\n");
2096 printf("which is required for encryption.\n");
2097 Exit(1);
2100 if (auth_has_failed) {
2101 printf("\nAuthentication negotation has failed,\n");
2102 printf("which is required for encryption.\n");
2103 Exit(1);
2105 if (my_want_state_is_dont(TELOPT_ENCRYPT) ||
2106 my_want_state_is_wont(TELOPT_ENCRYPT)) {
2107 printf("\nServer refused to negotiate encryption.\n");
2108 Exit(1);
2110 if (encrypt_is_encrypting())
2111 break;
2112 if (time(0) > timeout) {
2113 printf("\nEncryption could not be enabled.\n");
2114 Exit(1);
2116 if (printed_encrypt == 0) {
2117 printed_encrypt = 1;
2118 printf("Waiting for encryption to be negotiated...\n");
2120 * Turn on MODE_TRAPSIG and then turn off localchars
2121 * so that ^C will cause telnet to exit.
2123 TerminalNewMode(getconnmode()|MODE_TRAPSIG);
2124 intr_waiting = 1;
2126 if (intr_happened) {
2127 printf("\nUser interrupt.\n");
2128 Exit(1);
2130 telnet_spin();
2132 if (printed_encrypt) {
2133 printf("Encryption negotiated.\n");
2134 intr_waiting = 0;
2135 setconnmode(0);
2138 #endif
2140 for (;;) {
2141 int schedValue;
2143 while ((schedValue = Scheduler(0)) != 0) {
2144 if (schedValue == -1) {
2145 setcommandmode();
2146 return;
2150 if (Scheduler(1) == -1) {
2151 setcommandmode();
2152 return;
2158 * netclear()
2160 * We are about to do a TELNET SYNCH operation. Clear
2161 * the path to the network.
2163 * Things are a bit tricky since we may have sent the first
2164 * byte or so of a previous TELNET command into the network.
2165 * So, we have to scan the network buffer from the beginning
2166 * until we are up to where we want to be.
2168 * A side effect of what we do, just to keep things
2169 * simple, is to clear the urgent data pointer. The principal
2170 * caller should be setting the urgent data pointer AFTER calling
2171 * us in any case.
2174 static void
2175 netclear()
2177 #if 0 /* XXX */
2178 char *thisitem, *next;
2179 char *good;
2180 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
2181 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
2183 thisitem = netobuf;
2185 while ((next = nextitem(thisitem)) <= netobuf.send) {
2186 thisitem = next;
2189 /* Now, thisitem is first before/at boundary. */
2191 good = netobuf; /* where the good bytes go */
2193 while (netoring.add > thisitem) {
2194 if (wewant(thisitem)) {
2195 int length;
2197 next = thisitem;
2198 do {
2199 next = nextitem(next);
2200 } while (wewant(next) && (nfrontp > next));
2201 length = next-thisitem;
2202 memmove(good, thisitem, length);
2203 good += length;
2204 thisitem = next;
2205 } else {
2206 thisitem = nextitem(thisitem);
2210 #endif /* 0 */
2214 * These routines add various telnet commands to the data stream.
2217 static void
2218 doflush()
2220 NET2ADD(IAC, DO);
2221 NETADD(TELOPT_TM);
2222 flushline = 1;
2223 flushout = 1;
2224 ttyflush(1); /* Flush/drop output */
2225 /* do printoption AFTER flush, otherwise the output gets tossed... */
2226 printoption("SENT", DO, TELOPT_TM);
2229 void
2230 xmitAO(void)
2232 NET2ADD(IAC, AO);
2233 printoption("SENT", IAC, AO);
2234 if (autoflush) {
2235 doflush();
2240 void
2241 xmitEL(void)
2243 NET2ADD(IAC, EL);
2244 printoption("SENT", IAC, EL);
2247 void
2248 xmitEC(void)
2250 NET2ADD(IAC, EC);
2251 printoption("SENT", IAC, EC);
2256 dosynch()
2258 netclear(); /* clear the path to the network */
2259 NETADD(IAC);
2260 setneturg();
2261 NETADD(DM);
2262 printoption("SENT", IAC, DM);
2263 return 1;
2266 int want_status_response = 0;
2269 get_status()
2271 unsigned char tmp[16];
2272 unsigned char *cp;
2274 if (my_want_state_is_dont(TELOPT_STATUS)) {
2275 printf("Remote side does not support STATUS option\n");
2276 return 0;
2278 cp = tmp;
2280 *cp++ = IAC;
2281 *cp++ = SB;
2282 *cp++ = TELOPT_STATUS;
2283 *cp++ = TELQUAL_SEND;
2284 *cp++ = IAC;
2285 *cp++ = SE;
2286 if (NETROOM() >= cp - tmp) {
2287 ring_supply_data(&netoring, tmp, cp-tmp);
2288 printsub('>', tmp+2, cp - tmp - 2);
2290 ++want_status_response;
2291 return 1;
2294 void
2295 intp(void)
2297 NET2ADD(IAC, IP);
2298 printoption("SENT", IAC, IP);
2299 flushline = 1;
2300 if (autoflush) {
2301 doflush();
2303 if (autosynch) {
2304 dosynch();
2308 void
2309 sendbrk(void)
2311 NET2ADD(IAC, BREAK);
2312 printoption("SENT", IAC, BREAK);
2313 flushline = 1;
2314 if (autoflush) {
2315 doflush();
2317 if (autosynch) {
2318 dosynch();
2322 void
2323 sendabort(void)
2325 NET2ADD(IAC, ABORT);
2326 printoption("SENT", IAC, ABORT);
2327 flushline = 1;
2328 if (autoflush) {
2329 doflush();
2331 if (autosynch) {
2332 dosynch();
2336 void
2337 sendsusp(void)
2339 NET2ADD(IAC, SUSP);
2340 printoption("SENT", IAC, SUSP);
2341 flushline = 1;
2342 if (autoflush) {
2343 doflush();
2345 if (autosynch) {
2346 dosynch();
2350 void
2351 sendeof(void)
2353 NET2ADD(IAC, xEOF);
2354 printoption("SENT", IAC, xEOF);
2357 void
2358 sendayt(void)
2360 NET2ADD(IAC, AYT);
2361 printoption("SENT", IAC, AYT);
2365 * Send a window size update to the remote system.
2368 void
2369 sendnaws()
2371 long rows, cols;
2372 unsigned char tmp[16];
2373 unsigned char *cp;
2375 if (my_state_is_wont(TELOPT_NAWS))
2376 return;
2378 #undef PUTSHORT
2379 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2380 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2382 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2383 return;
2386 cp = tmp;
2388 *cp++ = IAC;
2389 *cp++ = SB;
2390 *cp++ = TELOPT_NAWS;
2391 PUTSHORT(cp, cols);
2392 PUTSHORT(cp, rows);
2393 *cp++ = IAC;
2394 *cp++ = SE;
2395 if (NETROOM() >= cp - tmp) {
2396 ring_supply_data(&netoring, tmp, cp-tmp);
2397 printsub('>', tmp+2, cp - tmp - 2);
2401 void
2402 tel_enter_binary(int rw)
2404 if (rw&1)
2405 send_do(TELOPT_BINARY, 1);
2406 if (rw&2)
2407 send_will(TELOPT_BINARY, 1);
2410 void
2411 tel_leave_binary(int rw)
2413 if (rw&1)
2414 send_dont(TELOPT_BINARY, 1);
2415 if (rw&2)
2416 send_wont(TELOPT_BINARY, 1);