Home | History | Annotate | Line # | Download | only in telnet
telnet.c revision 1.26
      1 /*	$NetBSD: telnet.c,v 1.26 2003/08/07 11:16:11 agc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)telnet.c	8.4 (Berkeley) 5/30/95";
     36 #else
     37 __RCSID("$NetBSD: telnet.c,v 1.26 2003/08/07 11:16:11 agc Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 
     43 #include <signal.h>
     44 #include <termcap.h>
     45 #include <unistd.h>
     46 /* By the way, we need to include curses.h before telnet.h since,
     47  * among other things, telnet.h #defines 'DO', which is a variable
     48  * declared in curses.h.
     49  */
     50 
     51 #include <arpa/telnet.h>
     52 
     53 #include <ctype.h>
     54 
     55 #include "ring.h"
     56 #include "defines.h"
     57 #include "externs.h"
     58 #include "types.h"
     59 #include "general.h"
     60 
     61 #include <libtelnet/misc.h>
     62 #ifdef AUTHENTICATION
     63 #include <libtelnet/auth.h>
     64 #endif
     65 #ifdef ENCRYPTION
     66 #include <libtelnet/encrypt.h>
     67 #endif
     68 
     69 
     70 #define	strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
     72 
     73 static unsigned char	subbuffer[SUBBUFSIZE],
     74 			*subpointer, *subend;	 /* buffer for sub-options */
     75 #define	SB_CLEAR()	subpointer = subbuffer;
     76 #define	SB_TERM()	{ subend = subpointer; SB_CLEAR(); }
     77 #define	SB_ACCUM(c)	if (subpointer < (subbuffer+sizeof subbuffer)) { \
     78 				*subpointer++ = (c); \
     79 			}
     80 
     81 #define	SB_GET()	((*subpointer++)&0xff)
     82 #define	SB_PEEK()	((*subpointer)&0xff)
     83 #define	SB_EOF()	(subpointer >= subend)
     84 #define	SB_LEN()	(subend - subpointer)
     85 
     86 char	options[256];		/* The combined options */
     87 char	do_dont_resp[256];
     88 char	will_wont_resp[256];
     89 
     90 int
     91 	eight = 0,
     92 	autologin = 0,	/* Autologin anyone? */
     93 	skiprc = 0,
     94 	connected,
     95 	showoptions,
     96 	In3270,		/* Are we in 3270 mode? */
     97 	ISend,		/* trying to send network data in */
     98 	debug = 0,
     99 	crmod,
    100 	netdata,	/* Print out network data flow */
    101 	crlf,		/* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
    102 #ifdef TN3270
    103 	noasynchtty = 0,/* User specified "-noasynch" on command line */
    104 	noasynchnet = 0,/* User specified "-noasynch" on command line */
    105 	askedSGA = 0,	/* We have talked about suppress go ahead */
    106 #endif	/* defined(TN3270) */
    107 	telnetport,
    108 	SYNCHing,	/* we are in TELNET SYNCH mode */
    109 	flushout,	/* flush output */
    110 	autoflush = 0,	/* flush output when interrupting? */
    111 	autosynch,	/* send interrupt characters with SYNCH? */
    112 	localflow,	/* we handle flow control locally */
    113 	restartany,	/* if flow control enabled, restart on any character */
    114 	localchars,	/* we recognize interrupt/quit */
    115 	donelclchars,	/* the user has set "localchars" */
    116 	donebinarytoggle,	/* the user has put us in binary */
    117 	dontlecho,	/* do we suppress local echoing right now? */
    118 	globalmode,
    119 	doaddrlookup = 1, /* do a reverse address lookup? */
    120 	clienteof = 0;
    121 
    122 char *prompt = 0;
    123 
    124 cc_t escape;
    125 cc_t rlogin;
    126 #ifdef	KLUDGELINEMODE
    127 cc_t echoc;
    128 #endif
    129 
    130 /*
    131  * Telnet receiver states for fsm
    132  */
    133 #define	TS_DATA		0
    134 #define	TS_IAC		1
    135 #define	TS_WILL		2
    136 #define	TS_WONT		3
    137 #define	TS_DO		4
    138 #define	TS_DONT		5
    139 #define	TS_CR		6
    140 #define	TS_SB		7		/* sub-option collection */
    141 #define	TS_SE		8		/* looking for sub-option end */
    142 
    143 static int	telrcv_state;
    144 #ifdef	OLD_ENVIRON
    145 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
    146 #else
    147 # define telopt_environ TELOPT_NEW_ENVIRON
    148 #endif
    149 
    150 jmp_buf	toplevel = { 0 };
    151 jmp_buf	peerdied;
    152 
    153 int	flushline;
    154 int	linemode;
    155 
    156 #ifdef	KLUDGELINEMODE
    157 int	kludgelinemode = 1;
    158 #endif
    159 
    160 static void dooption(int);
    161 static void dontoption(int);
    162 static void suboption(void);
    163 static int telsnd(void);
    164 static void netclear(void);
    165 static void doflush(void);
    166 
    167 /*
    168  * The following are some clocks used to decide how to interpret
    169  * the relationship between various variables.
    170  */
    171 
    172 Clocks clocks;
    173 
    174 #ifdef	notdef
    176 Modelist modelist[] = {
    177 	{ "telnet command mode", COMMAND_LINE },
    178 	{ "character-at-a-time mode", 0 },
    179 	{ "character-at-a-time mode (local echo)", LOCAL_ECHO|LOCAL_CHARS },
    180 	{ "line-by-line mode (remote echo)", LINE | LOCAL_CHARS },
    181 	{ "line-by-line mode", LINE | LOCAL_ECHO | LOCAL_CHARS },
    182 	{ "line-by-line mode (local echoing suppressed)", LINE | LOCAL_CHARS },
    183 	{ "3270 mode", 0 },
    184 };
    185 #endif
    186 
    187 
    188 /*
    190  * Initialize telnet environment.
    191  */
    192 
    193 void
    194 init_telnet(void)
    195 {
    196     env_init();
    197 
    198     SB_CLEAR();
    199     ClearArray(options);
    200 
    201     connected = In3270 = ISend = localflow = donebinarytoggle = 0;
    202 #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
    203     auth_encrypt_connect(connected);
    204 #endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
    205     restartany = -1;
    206 
    207     SYNCHing = 0;
    208 
    209     /* Don't change NetTrace */
    210 
    211     escape = CONTROL(']');
    212     rlogin = _POSIX_VDISABLE;
    213 #ifdef	KLUDGELINEMODE
    214     echoc = CONTROL('E');
    215 #endif
    216 
    217     flushline = 1;
    218     telrcv_state = TS_DATA;
    219 }
    220 
    221 
    223 #ifdef	notdef
    224 #include <stdarg.h>
    225 
    226 /*VARARGS*/
    227 static void
    228 printring(Ring *ring, char *format, ...)
    229     va_dcl
    230 {
    231     va_list ap;
    232     char buffer[100];		/* where things go */
    233     char *ptr;
    234     char *string;
    235     int i;
    236 
    237     va_start(ap, format);
    238 
    239     ptr = buffer;
    240 
    241     while ((i = *format++) != 0) {
    242 	if (i == '%') {
    243 	    i = *format++;
    244 	    switch (i) {
    245 	    case 'c':
    246 		*ptr++ = va_arg(ap, int);
    247 		break;
    248 	    case 's':
    249 		string = va_arg(ap, char *);
    250 		ring_supply_data(ring, buffer, ptr-buffer);
    251 		ring_supply_data(ring, string, strlen(string));
    252 		ptr = buffer;
    253 		break;
    254 	    case 0:
    255 		ExitString("printring: trailing %%.\n", 1);
    256 		/*NOTREACHED*/
    257 	    default:
    258 		ExitString("printring: unknown format character.\n", 1);
    259 		/*NOTREACHED*/
    260 	    }
    261 	} else {
    262 	    *ptr++ = i;
    263 	}
    264     }
    265     va_end(ap);
    266     ring_supply_data(ring, buffer, ptr-buffer);
    267 }
    268 #endif
    269 
    270 /*
    271  * These routines are in charge of sending option negotiations
    272  * to the other side.
    273  *
    274  * The basic idea is that we send the negotiation if either side
    275  * is in disagreement as to what the current state should be.
    276  */
    277 
    278 void
    279 send_do(int c, int init)
    280 {
    281     if (init) {
    282 	if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
    283 				my_want_state_is_do(c))
    284 	    return;
    285 	set_my_want_state_do(c);
    286 	do_dont_resp[c]++;
    287     }
    288     NET2ADD(IAC, DO);
    289     NETADD(c);
    290     printoption("SENT", DO, c);
    291 }
    292 
    293 void
    294 send_dont(int c, int init)
    295 {
    296     if (init) {
    297 	if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
    298 				my_want_state_is_dont(c))
    299 	    return;
    300 	set_my_want_state_dont(c);
    301 	do_dont_resp[c]++;
    302     }
    303     NET2ADD(IAC, DONT);
    304     NETADD(c);
    305     printoption("SENT", DONT, c);
    306 }
    307 
    308 void
    309 send_will(int c, int init)
    310 {
    311     if (init) {
    312 	if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
    313 				my_want_state_is_will(c))
    314 	    return;
    315 	set_my_want_state_will(c);
    316 	will_wont_resp[c]++;
    317     }
    318     NET2ADD(IAC, WILL);
    319     NETADD(c);
    320     printoption("SENT", WILL, c);
    321 }
    322 
    323 void
    324 send_wont(int c, int init)
    325 {
    326     if (init) {
    327 	if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
    328 				my_want_state_is_wont(c))
    329 	    return;
    330 	set_my_want_state_wont(c);
    331 	will_wont_resp[c]++;
    332     }
    333     NET2ADD(IAC, WONT);
    334     NETADD(c);
    335     printoption("SENT", WONT, c);
    336 }
    337 
    338 
    339 void
    340 willoption(int option)
    341 {
    342 	int new_state_ok = 0;
    343 
    344 	if (do_dont_resp[option]) {
    345 	    --do_dont_resp[option];
    346 	    if (do_dont_resp[option] && my_state_is_do(option))
    347 		--do_dont_resp[option];
    348 	}
    349 
    350 	if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
    351 
    352 	    switch (option) {
    353 
    354 	    case TELOPT_ECHO:
    355 #	    if defined(TN3270)
    356 		/*
    357 		 * The following is a pain in the rear-end.
    358 		 * Various IBM servers (some versions of Wiscnet,
    359 		 * possibly Fibronics/Spartacus, and who knows who
    360 		 * else) will NOT allow us to send "DO SGA" too early
    361 		 * in the setup proceedings.  On the other hand,
    362 		 * 4.2 servers (telnetd) won't set SGA correctly.
    363 		 * So, we are stuck.  Empirically (but, based on
    364 		 * a VERY small sample), the IBM servers don't send
    365 		 * out anything about ECHO, so we postpone our sending
    366 		 * "DO SGA" until we see "WILL ECHO" (which 4.2 servers
    367 		 * DO send).
    368 		  */
    369 		{
    370 		    if (askedSGA == 0) {
    371 			askedSGA = 1;
    372 			if (my_want_state_is_dont(TELOPT_SGA))
    373 			    send_do(TELOPT_SGA, 1);
    374 		    }
    375 		}
    376 		    /* Fall through */
    377 	    case TELOPT_EOR:
    378 #endif	    /* defined(TN3270) */
    379 	    case TELOPT_BINARY:
    380 	    case TELOPT_SGA:
    381 		settimer(modenegotiated);
    382 		/* FALL THROUGH */
    383 	    case TELOPT_STATUS:
    384 #ifdef AUTHENTICATION
    385 	    case TELOPT_AUTHENTICATION:
    386 #ifdef	ENCRYPTION
    387 	    case TELOPT_ENCRYPT:
    388 #endif	/* ENCRYPTION */
    389 #endif
    390 		new_state_ok = 1;
    391 		break;
    392 
    393 	    case TELOPT_TM:
    394 		if (flushout)
    395 		    flushout = 0;
    396 		/*
    397 		 * Special case for TM.  If we get back a WILL,
    398 		 * pretend we got back a WONT.
    399 		 */
    400 		set_my_want_state_dont(option);
    401 		set_my_state_dont(option);
    402 		return;			/* Never reply to TM will's/wont's */
    403 
    404 	    case TELOPT_LINEMODE:
    405 	    default:
    406 		break;
    407 	    }
    408 
    409 	    if (new_state_ok) {
    410 		set_my_want_state_do(option);
    411 		send_do(option, 0);
    412 		setconnmode(0);		/* possibly set new tty mode */
    413 	    } else {
    414 		do_dont_resp[option]++;
    415 		send_dont(option, 0);
    416 	    }
    417 	}
    418 	set_my_state_do(option);
    419 #ifdef	ENCRYPTION
    420 	if (option == TELOPT_ENCRYPT)
    421 		encrypt_send_support();
    422 #endif	/* ENCRYPTION */
    423 }
    424 
    425 void
    426 wontoption(int option)
    427 {
    428 	if (do_dont_resp[option]) {
    429 	    --do_dont_resp[option];
    430 	    if (do_dont_resp[option] && my_state_is_dont(option))
    431 		--do_dont_resp[option];
    432 	}
    433 
    434 	if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
    435 
    436 	    switch (option) {
    437 
    438 #ifdef	KLUDGELINEMODE
    439 	    case TELOPT_SGA:
    440 		if (!kludgelinemode)
    441 		    break;
    442 		/* FALL THROUGH */
    443 #endif
    444 	    case TELOPT_ECHO:
    445 		settimer(modenegotiated);
    446 		break;
    447 
    448 	    case TELOPT_TM:
    449 		if (flushout)
    450 		    flushout = 0;
    451 		set_my_want_state_dont(option);
    452 		set_my_state_dont(option);
    453 		return;		/* Never reply to TM will's/wont's */
    454 
    455 	    default:
    456 		break;
    457 	    }
    458 	    set_my_want_state_dont(option);
    459 	    if (my_state_is_do(option))
    460 		send_dont(option, 0);
    461 	    setconnmode(0);			/* Set new tty mode */
    462 	} else if (option == TELOPT_TM) {
    463 	    /*
    464 	     * Special case for TM.
    465 	     */
    466 	    if (flushout)
    467 		flushout = 0;
    468 	    set_my_want_state_dont(option);
    469 	}
    470 	set_my_state_dont(option);
    471 }
    472 
    473 static void
    474 dooption(int option)
    475 {
    476 	int new_state_ok = 0;
    477 
    478 	if (will_wont_resp[option]) {
    479 	    --will_wont_resp[option];
    480 	    if (will_wont_resp[option] && my_state_is_will(option))
    481 		--will_wont_resp[option];
    482 	}
    483 
    484 	if (will_wont_resp[option] == 0) {
    485 	  if (my_want_state_is_wont(option)) {
    486 
    487 	    switch (option) {
    488 
    489 	    case TELOPT_TM:
    490 		/*
    491 		 * Special case for TM.  We send a WILL, but pretend
    492 		 * we sent WONT.
    493 		 */
    494 		send_will(option, 0);
    495 		set_my_want_state_wont(TELOPT_TM);
    496 		set_my_state_wont(TELOPT_TM);
    497 		return;
    498 
    499 #	if defined(TN3270)
    500 	    case TELOPT_EOR:		/* end of record */
    501 #	endif	/* defined(TN3270) */
    502 	    case TELOPT_BINARY:		/* binary mode */
    503 	    case TELOPT_NAWS:		/* window size */
    504 	    case TELOPT_TSPEED:		/* terminal speed */
    505 	    case TELOPT_LFLOW:		/* local flow control */
    506 	    case TELOPT_TTYPE:		/* terminal type option */
    507 	    case TELOPT_SGA:		/* no big deal */
    508 #ifdef	ENCRYPTION
    509 	    case TELOPT_ENCRYPT:	/* encryption variable option */
    510 #endif	/* ENCRYPTION */
    511 		new_state_ok = 1;
    512 		break;
    513 
    514 	    case TELOPT_NEW_ENVIRON:	/* New environment variable option */
    515 #ifdef	OLD_ENVIRON
    516 		if (my_state_is_will(TELOPT_OLD_ENVIRON))
    517 			send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
    518 		goto env_common;
    519 	    case TELOPT_OLD_ENVIRON:	/* Old environment variable option */
    520 		if (my_state_is_will(TELOPT_NEW_ENVIRON))
    521 			break;		/* Don't enable if new one is in use! */
    522 	    env_common:
    523 		telopt_environ = option;
    524 #endif
    525 		new_state_ok = 1;
    526 		break;
    527 
    528 #ifdef AUTHENTICATION
    529 	    case TELOPT_AUTHENTICATION:
    530 		if (autologin)
    531 			new_state_ok = 1;
    532 		break;
    533 #endif
    534 
    535 	    case TELOPT_XDISPLOC:	/* X Display location */
    536 		if (env_getvalue((unsigned char *)"DISPLAY"))
    537 		    new_state_ok = 1;
    538 		break;
    539 
    540 	    case TELOPT_LINEMODE:
    541 #ifdef	KLUDGELINEMODE
    542 		kludgelinemode = 0;
    543 		send_do(TELOPT_SGA, 1);
    544 #endif
    545 		set_my_want_state_will(TELOPT_LINEMODE);
    546 		send_will(option, 0);
    547 		set_my_state_will(TELOPT_LINEMODE);
    548 		slc_init();
    549 		return;
    550 
    551 	    case TELOPT_ECHO:		/* We're never going to echo... */
    552 	    default:
    553 		break;
    554 	    }
    555 
    556 	    if (new_state_ok) {
    557 		set_my_want_state_will(option);
    558 		send_will(option, 0);
    559 		setconnmode(0);			/* Set new tty mode */
    560 	    } else {
    561 		will_wont_resp[option]++;
    562 		send_wont(option, 0);
    563 	    }
    564 	  } else {
    565 	    /*
    566 	     * Handle options that need more things done after the
    567 	     * other side has acknowledged the option.
    568 	     */
    569 	    switch (option) {
    570 	    case TELOPT_LINEMODE:
    571 #ifdef	KLUDGELINEMODE
    572 		kludgelinemode = 0;
    573 		send_do(TELOPT_SGA, 1);
    574 #endif
    575 		set_my_state_will(option);
    576 		slc_init();
    577 		send_do(TELOPT_SGA, 0);
    578 		return;
    579 	    }
    580 	  }
    581 	}
    582 	set_my_state_will(option);
    583 }
    584 
    585 static void
    586 dontoption(int option)
    587 {
    588 
    589 	if (will_wont_resp[option]) {
    590 	    --will_wont_resp[option];
    591 	    if (will_wont_resp[option] && my_state_is_wont(option))
    592 		--will_wont_resp[option];
    593 	}
    594 
    595 	if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
    596 	    switch (option) {
    597 	    case TELOPT_LINEMODE:
    598 		linemode = 0;	/* put us back to the default state */
    599 		break;
    600 #ifdef	OLD_ENVIRON
    601 	    case TELOPT_NEW_ENVIRON:
    602 		/*
    603 		 * The new environ option wasn't recognized, try
    604 		 * the old one.
    605 		 */
    606 		send_will(TELOPT_OLD_ENVIRON, 1);
    607 		telopt_environ = TELOPT_OLD_ENVIRON;
    608 		break;
    609 #endif
    610 	    }
    611 	    /* we always accept a DONT */
    612 	    set_my_want_state_wont(option);
    613 	    if (my_state_is_will(option))
    614 		send_wont(option, 0);
    615 	    setconnmode(0);			/* Set new tty mode */
    616 	}
    617 	set_my_state_wont(option);
    618 }
    619 
    620 /*
    621  * Given a buffer returned by tgetent(), this routine will turn
    622  * the pipe separated list of names in the buffer into an array
    623  * of pointers to null terminated names.  We toss out any bad,
    624  * duplicate, or verbose names (names with spaces).
    625  */
    626 
    627 static char *name_unknown = "UNKNOWN";
    628 static char *unknown[] = { 0, 0 };
    629 
    630 char **
    631 mklist(char *buf, char *name)
    632 {
    633 	int n;
    634 	char c, *cp, **argvp, *cp2, **argv, **avt;
    635 
    636 	if (name) {
    637 		if ((int)strlen(name) > 40) {
    638 			name = 0;
    639 			unknown[0] = name_unknown;
    640 		} else {
    641 			unknown[0] = name;
    642 			upcase(name);
    643 		}
    644 	} else
    645 		unknown[0] = name_unknown;
    646 	/*
    647 	 * Count up the number of names.
    648 	 */
    649 	for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
    650 		if (*cp == '|')
    651 			n++;
    652 	}
    653 	/*
    654 	 * Allocate an array to put the name pointers into
    655 	 */
    656 	argv = (char **)malloc((n+3)*sizeof(char *));
    657 	if (argv == 0)
    658 		return(unknown);
    659 
    660 	/*
    661 	 * Fill up the array of pointers to names.
    662 	 */
    663 	*argv = 0;
    664 	argvp = argv+1;
    665 	n = 0;
    666 	for (cp = cp2 = buf; (c = *cp);  cp++) {
    667 		if (c == '|' || c == ':') {
    668 			*cp++ = '\0';
    669 			/*
    670 			 * Skip entries that have spaces or are over 40
    671 			 * characters long.  If this is our environment
    672 			 * name, then put it up front.  Otherwise, as
    673 			 * long as this is not a duplicate name (case
    674 			 * insensitive) add it to the list.
    675 			 */
    676 			if (n || (cp - cp2 > 41))
    677 				;
    678 			else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
    679 				*argv = cp2;
    680 			else if (is_unique(cp2, argv+1, argvp))
    681 				*argvp++ = cp2;
    682 			if (c == ':')
    683 				break;
    684 			/*
    685 			 * Skip multiple delimiters. Reset cp2 to
    686 			 * the beginning of the next name. Reset n,
    687 			 * the flag for names with spaces.
    688 			 */
    689 			while ((c = *cp) == '|')
    690 				cp++;
    691 			cp2 = cp;
    692 			n = 0;
    693 		}
    694 		/*
    695 		 * Skip entries with spaces or non-ascii values.
    696 		 * Convert lower case letters to upper case.
    697 		 */
    698 		if ((c == ' ') || !isascii(c))
    699 			n = 1;
    700 		else if (islower((unsigned char)c))
    701 			*cp = toupper(c);
    702 	}
    703 
    704 	/*
    705 	 * Check for an old V6 2 character name.  If the second
    706 	 * name points to the beginning of the buffer, and is
    707 	 * only 2 characters long, move it to the end of the array.
    708 	 */
    709 	if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
    710 		--argvp;
    711 		for (avt = &argv[1]; avt < argvp; avt++)
    712 			*avt = *(avt+1);
    713 		*argvp++ = buf;
    714 	}
    715 
    716 	/*
    717 	 * Duplicate last name, for TTYPE option, and null
    718 	 * terminate the array.  If we didn't find a match on
    719 	 * our terminal name, put that name at the beginning.
    720 	 */
    721 	cp = *(argvp-1);
    722 	*argvp++ = cp;
    723 	*argvp = 0;
    724 
    725 	if (*argv == 0) {
    726 		if (name)
    727 			*argv = name;
    728 		else {
    729 			--argvp;
    730 			for (avt = argv; avt < argvp; avt++)
    731 				*avt = *(avt+1);
    732 		}
    733 	}
    734 	if (*argv)
    735 		return(argv);
    736 	else
    737 		return(unknown);
    738 }
    739 
    740 int
    741 is_unique(char *name, char **as, char **ae)
    742 {
    743 	char **ap;
    744 	int n;
    745 
    746 	n = strlen(name) + 1;
    747 	for (ap = as; ap < ae; ap++)
    748 		if (strncasecmp(*ap, name, n) == 0)
    749 			return(0);
    750 	return (1);
    751 }
    752 
    753 #ifdef	TERMCAP
    754 char *termbuf;
    755 
    756 /*ARGSUSED*/
    757 int
    758 setup_term(char *tname, int fd, int *errp)
    759 {
    760 	char zz[1024], *zz_ptr;
    761 	char *ext_tc, *newptr;
    762 
    763 	if ((termbuf = (char *) malloc(1024)) == NULL)
    764 		goto error;
    765 
    766 	if (tgetent(termbuf, tname) == 1) {
    767 		  /* check for ZZ capability, which indicates termcap truncated */
    768 		zz_ptr = zz;
    769 		if (tgetstr("ZZ", &zz_ptr) != NULL) {
    770 			  /* it was, fish back the full termcap */
    771 			sscanf(zz, "%p", &ext_tc);
    772 			if ((newptr = (char *) realloc(termbuf,
    773 						       strlen(ext_tc) + 1))
    774 			    == NULL) {
    775 				goto error;
    776 			}
    777 
    778 			strlcpy(newptr, ext_tc, strlen(ext_tc) + 1);
    779 			termbuf = newptr;
    780 		}
    781 
    782 		if (errp)
    783 			*errp = 1;
    784 		return(0);
    785 	}
    786   error:
    787 	if (errp)
    788 		*errp = 0;
    789 	return(-1);
    790 }
    791 #else
    792 #define	termbuf	ttytype
    793 extern char ttytype[];
    794 #endif
    795 
    796 int resettermname = 1;
    797 
    798 char *
    799 gettermname(void)
    800 {
    801 	char *tname;
    802 	static char **tnamep = 0;
    803 	static char **next;
    804 	int err;
    805 
    806 	if (resettermname) {
    807 		resettermname = 0;
    808 		if (tnamep && tnamep != unknown)
    809 			free(tnamep);
    810 		if ((tname = (char *)env_getvalue((unsigned char *)"TERM")) &&
    811 				(setup_term(tname, 1, &err) == 0)) {
    812 			tnamep = mklist(termbuf, tname);
    813 		} else {
    814 			if (tname && ((int)strlen(tname) <= 40)) {
    815 				unknown[0] = tname;
    816 				upcase(tname);
    817 			} else
    818 				unknown[0] = name_unknown;
    819 			tnamep = unknown;
    820 		}
    821 		next = tnamep;
    822 	}
    823 	if (*next == 0)
    824 		next = tnamep;
    825 	return(*next++);
    826 }
    827 /*
    828  * suboption()
    829  *
    830  *	Look at the sub-option buffer, and try to be helpful to the other
    831  * side.
    832  *
    833  *	Currently we recognize:
    834  *
    835  *		Terminal type, send request.
    836  *		Terminal speed (send request).
    837  *		Local flow control (is request).
    838  *		Linemode
    839  */
    840 
    841 static void
    842 suboption(void)
    843 {
    844     unsigned char subchar;
    845 
    846     printsub('<', subbuffer, SB_LEN()+2);
    847     switch (subchar = SB_GET()) {
    848     case TELOPT_TTYPE:
    849 	if (my_want_state_is_wont(TELOPT_TTYPE))
    850 	    return;
    851 	if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
    852 	    return;
    853 	} else {
    854 	    char *name;
    855 	    unsigned char temp[50];
    856 	    int len;
    857 
    858 #ifdef TN3270
    859 	    if (tn3270_ttype()) {
    860 		return;
    861 	    }
    862 #endif	/* defined(TN3270) */
    863 	    name = gettermname();
    864 	    len = strlen(name) + 4 + 2;
    865 	    if (len < NETROOM()) {
    866 		sprintf((char *)temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
    867 				TELQUAL_IS, name, IAC, SE);
    868 		ring_supply_data(&netoring, temp, len);
    869 		printsub('>', &temp[2], len-2);
    870 	    } else {
    871 		ExitString("No room in buffer for terminal type.\n", 1);
    872 		/*NOTREACHED*/
    873 	    }
    874 	}
    875 	break;
    876     case TELOPT_TSPEED:
    877 	if (my_want_state_is_wont(TELOPT_TSPEED))
    878 	    return;
    879 	if (SB_EOF())
    880 	    return;
    881 	if (SB_GET() == TELQUAL_SEND) {
    882 	    long ospeed, ispeed;
    883 	    unsigned char temp[50];
    884 	    int len;
    885 
    886 	    TerminalSpeeds(&ispeed, &ospeed);
    887 
    888 	    sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
    889 		    TELQUAL_IS, (long)ospeed, (long)ispeed, IAC, SE);
    890 	    len = strlen((char *)temp+4) + 4;	/* temp[3] is 0 ... */
    891 
    892 	    if (len < NETROOM()) {
    893 		ring_supply_data(&netoring, temp, len);
    894 		printsub('>', temp+2, len - 2);
    895 	    }
    896 /*@*/	    else printf("lm_will: not enough room in buffer\n");
    897 	}
    898 	break;
    899     case TELOPT_LFLOW:
    900 	if (my_want_state_is_wont(TELOPT_LFLOW))
    901 	    return;
    902 	if (SB_EOF())
    903 	    return;
    904 	switch(SB_GET()) {
    905 	case LFLOW_RESTART_ANY:
    906 	    restartany = 1;
    907 	    break;
    908 	case LFLOW_RESTART_XON:
    909 	    restartany = 0;
    910 	    break;
    911 	case LFLOW_ON:
    912 	    localflow = 1;
    913 	    break;
    914 	case LFLOW_OFF:
    915 	    localflow = 0;
    916 	    break;
    917 	default:
    918 	    return;
    919 	}
    920 	setcommandmode();
    921 	setconnmode(0);
    922 	break;
    923 
    924     case TELOPT_LINEMODE:
    925 	if (my_want_state_is_wont(TELOPT_LINEMODE))
    926 	    return;
    927 	if (SB_EOF())
    928 	    return;
    929 	switch (SB_GET()) {
    930 	case WILL:
    931 	    lm_will(subpointer, SB_LEN());
    932 	    break;
    933 	case WONT:
    934 	    lm_wont(subpointer, SB_LEN());
    935 	    break;
    936 	case DO:
    937 	    lm_do(subpointer, SB_LEN());
    938 	    break;
    939 	case DONT:
    940 	    lm_dont(subpointer, SB_LEN());
    941 	    break;
    942 	case LM_SLC:
    943 	    slc(subpointer, SB_LEN());
    944 	    break;
    945 	case LM_MODE:
    946 	    lm_mode(subpointer, SB_LEN(), 0);
    947 	    break;
    948 	default:
    949 	    break;
    950 	}
    951 	break;
    952 
    953 #ifdef	OLD_ENVIRON
    954     case TELOPT_OLD_ENVIRON:
    955 #endif
    956     case TELOPT_NEW_ENVIRON:
    957 	if (SB_EOF())
    958 	    return;
    959 	switch(SB_PEEK()) {
    960 	case TELQUAL_IS:
    961 	case TELQUAL_INFO:
    962 	    if (my_want_state_is_dont(subchar))
    963 		return;
    964 	    break;
    965 	case TELQUAL_SEND:
    966 	    if (my_want_state_is_wont(subchar)) {
    967 		return;
    968 	    }
    969 	    break;
    970 	default:
    971 	    return;
    972 	}
    973 	env_opt(subpointer, SB_LEN());
    974 	break;
    975 
    976     case TELOPT_XDISPLOC:
    977 	if (my_want_state_is_wont(TELOPT_XDISPLOC))
    978 	    return;
    979 	if (SB_EOF())
    980 	    return;
    981 	if (SB_GET() == TELQUAL_SEND) {
    982 	    unsigned char temp[50], *dp;
    983 	    int len;
    984 
    985 	    if ((dp = env_getvalue((unsigned char *)"DISPLAY")) == NULL) {
    986 		/*
    987 		 * Something happened, we no longer have a DISPLAY
    988 		 * variable.  So, turn off the option.
    989 		 */
    990 		send_wont(TELOPT_XDISPLOC, 1);
    991 		break;
    992 	    }
    993 	    sprintf((char *)temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_XDISPLOC,
    994 		    TELQUAL_IS, dp, IAC, SE);
    995 	    len = strlen((char *)temp+4) + 4;	/* temp[3] is 0 ... */
    996 
    997 	    if (len < NETROOM()) {
    998 		ring_supply_data(&netoring, temp, len);
    999 		printsub('>', temp+2, len - 2);
   1000 	    }
   1001 /*@*/	    else printf("lm_will: not enough room in buffer\n");
   1002 	}
   1003 	break;
   1004 
   1005 #ifdef AUTHENTICATION
   1006 	case TELOPT_AUTHENTICATION: {
   1007 		if (!autologin)
   1008 			break;
   1009 		if (SB_EOF())
   1010 			return;
   1011 		switch(SB_GET()) {
   1012 		case TELQUAL_IS:
   1013 			if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
   1014 				return;
   1015 			auth_is(subpointer, SB_LEN());
   1016 			break;
   1017 		case TELQUAL_SEND:
   1018 			if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
   1019 				return;
   1020 			auth_send(subpointer, SB_LEN());
   1021 			break;
   1022 		case TELQUAL_REPLY:
   1023 			if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
   1024 				return;
   1025 			auth_reply(subpointer, SB_LEN());
   1026 			break;
   1027 		case TELQUAL_NAME:
   1028 			if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
   1029 				return;
   1030 			auth_name(subpointer, SB_LEN());
   1031 			break;
   1032 		}
   1033 	}
   1034 	break;
   1035 #endif
   1036 #ifdef	ENCRYPTION
   1037     case TELOPT_ENCRYPT:
   1038 	if (SB_EOF())
   1039 		return;
   1040 	switch(SB_GET()) {
   1041 	case ENCRYPT_START:
   1042 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
   1043 			return;
   1044 		encrypt_start(subpointer, SB_LEN());
   1045 		break;
   1046 	case ENCRYPT_END:
   1047 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
   1048 			return;
   1049 		encrypt_end();
   1050 		break;
   1051 	case ENCRYPT_SUPPORT:
   1052 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1053 			return;
   1054 		encrypt_support(subpointer, SB_LEN());
   1055 		break;
   1056 	case ENCRYPT_REQSTART:
   1057 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1058 			return;
   1059 		encrypt_request_start(subpointer, SB_LEN());
   1060 		break;
   1061 	case ENCRYPT_REQEND:
   1062 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1063 			return;
   1064 		/*
   1065 		 * We can always send an REQEND so that we cannot
   1066 		 * get stuck encrypting.  We should only get this
   1067 		 * if we have been able to get in the correct mode
   1068 		 * anyhow.
   1069 		 */
   1070 		encrypt_request_end();
   1071 		break;
   1072 	case ENCRYPT_IS:
   1073 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
   1074 			return;
   1075 		encrypt_is(subpointer, SB_LEN());
   1076 		break;
   1077 	case ENCRYPT_REPLY:
   1078 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1079 			return;
   1080 		encrypt_reply(subpointer, SB_LEN());
   1081 		break;
   1082 	case ENCRYPT_ENC_KEYID:
   1083 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
   1084 			return;
   1085 		encrypt_enc_keyid(subpointer, SB_LEN());
   1086 		break;
   1087 	case ENCRYPT_DEC_KEYID:
   1088 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1089 			return;
   1090 		encrypt_dec_keyid(subpointer, SB_LEN());
   1091 		break;
   1092 	default:
   1093 		break;
   1094 	}
   1095 	break;
   1096 #endif	/* ENCRYPTION */
   1097     default:
   1098 	break;
   1099     }
   1100 }
   1101 
   1102 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
   1103 
   1104 void
   1105 lm_will(unsigned char *cmd, int len)
   1106 {
   1107     if (len < 1) {
   1108 /*@*/	printf("lm_will: no command!!!\n");	/* Should not happen... */
   1109 	return;
   1110     }
   1111     switch(cmd[0]) {
   1112     case LM_FORWARDMASK:	/* We shouldn't ever get this... */
   1113     default:
   1114 	str_lm[3] = DONT;
   1115 	str_lm[4] = cmd[0];
   1116 	if (NETROOM() > sizeof(str_lm)) {
   1117 	    ring_supply_data(&netoring, str_lm, sizeof(str_lm));
   1118 	    printsub('>', &str_lm[2], sizeof(str_lm)-2);
   1119 	}
   1120 /*@*/	else printf("lm_will: not enough room in buffer\n");
   1121 	break;
   1122     }
   1123 }
   1124 
   1125 void
   1126 lm_wont(unsigned char *cmd, int len)
   1127 {
   1128     if (len < 1) {
   1129 /*@*/	printf("lm_wont: no command!!!\n");	/* Should not happen... */
   1130 	return;
   1131     }
   1132     switch(cmd[0]) {
   1133     case LM_FORWARDMASK:	/* We shouldn't ever get this... */
   1134     default:
   1135 	/* We are always DONT, so don't respond */
   1136 	return;
   1137     }
   1138 }
   1139 
   1140 void
   1141 lm_do(unsigned char *cmd, int len)
   1142 {
   1143     if (len < 1) {
   1144 /*@*/	printf("lm_do: no command!!!\n");	/* Should not happen... */
   1145 	return;
   1146     }
   1147     switch(cmd[0]) {
   1148     case LM_FORWARDMASK:
   1149     default:
   1150 	str_lm[3] = WONT;
   1151 	str_lm[4] = cmd[0];
   1152 	if (NETROOM() > sizeof(str_lm)) {
   1153 	    ring_supply_data(&netoring, str_lm, sizeof(str_lm));
   1154 	    printsub('>', &str_lm[2], sizeof(str_lm)-2);
   1155 	}
   1156 /*@*/	else printf("lm_do: not enough room in buffer\n");
   1157 	break;
   1158     }
   1159 }
   1160 
   1161 void
   1162 lm_dont(unsigned char *cmd, int len)
   1163 {
   1164     if (len < 1) {
   1165 /*@*/	printf("lm_dont: no command!!!\n");	/* Should not happen... */
   1166 	return;
   1167     }
   1168     switch(cmd[0]) {
   1169     case LM_FORWARDMASK:
   1170     default:
   1171 	/* we are always WONT, so don't respond */
   1172 	break;
   1173     }
   1174 }
   1175 
   1176 static unsigned char str_lm_mode[] = {
   1177 	IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
   1178 };
   1179 
   1180 void
   1181 lm_mode(unsigned char *cmd, int len, int init)
   1182 {
   1183 	if (len != 1)
   1184 		return;
   1185 	if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
   1186 		return;
   1187 	if (*cmd&MODE_ACK)
   1188 		return;
   1189 	linemode = *cmd&(MODE_MASK&~MODE_ACK);
   1190 	str_lm_mode[4] = linemode;
   1191 	if (!init)
   1192 	    str_lm_mode[4] |= MODE_ACK;
   1193 	if (NETROOM() > sizeof(str_lm_mode)) {
   1194 	    ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
   1195 	    printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
   1196 	}
   1197 /*@*/	else printf("lm_mode: not enough room in buffer\n");
   1198 	setconnmode(0);	/* set changed mode */
   1199 }
   1200 
   1201 
   1202 
   1204 /*
   1205  * slc()
   1206  * Handle special character suboption of LINEMODE.
   1207  */
   1208 
   1209 struct spc {
   1210 	cc_t val;
   1211 	cc_t *valp;
   1212 	char flags;	/* Current flags & level */
   1213 	char mylevel;	/* Maximum level & flags */
   1214 } spc_data[NSLC+1];
   1215 
   1216 #define SLC_IMPORT	0
   1217 #define	SLC_EXPORT	1
   1218 #define SLC_RVALUE	2
   1219 static int slc_mode = SLC_EXPORT;
   1220 
   1221 void
   1222 slc_init(void)
   1223 {
   1224 	struct spc *spcp;
   1225 
   1226 	localchars = 1;
   1227 	for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
   1228 		spcp->val = 0;
   1229 		spcp->valp = 0;
   1230 		spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
   1231 	}
   1232 
   1233 #define	initfunc(func, flags) { \
   1234 					spcp = &spc_data[func]; \
   1235 					if ((spcp->valp = tcval(func)) != NULL){ \
   1236 					    spcp->val = *spcp->valp; \
   1237 					    spcp->mylevel = SLC_VARIABLE|flags; \
   1238 					} else { \
   1239 					    spcp->val = 0; \
   1240 					    spcp->mylevel = SLC_DEFAULT; \
   1241 					} \
   1242 				    }
   1243 
   1244 	initfunc(SLC_SYNCH, 0);
   1245 	/* No BRK */
   1246 	initfunc(SLC_AO, 0);
   1247 	initfunc(SLC_AYT, 0);
   1248 	/* No EOR */
   1249 	initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
   1250 	initfunc(SLC_EOF, 0);
   1251 	initfunc(SLC_SUSP, SLC_FLUSHIN);
   1252 	initfunc(SLC_EC, 0);
   1253 	initfunc(SLC_EL, 0);
   1254 	initfunc(SLC_EW, 0);
   1255 	initfunc(SLC_RP, 0);
   1256 	initfunc(SLC_LNEXT, 0);
   1257 	initfunc(SLC_XON, 0);
   1258 	initfunc(SLC_XOFF, 0);
   1259 	initfunc(SLC_FORW1, 0);
   1260 	initfunc(SLC_FORW2, 0);
   1261 	/* No FORW2 */
   1262 
   1263 	initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
   1264 #undef	initfunc
   1265 
   1266 	if (slc_mode == SLC_EXPORT)
   1267 		slc_export();
   1268 	else
   1269 		slc_import(1);
   1270 
   1271 }
   1272 
   1273 void
   1274 slcstate(void)
   1275 {
   1276     printf("Special characters are %s values\n",
   1277 		slc_mode == SLC_IMPORT ? "remote default" :
   1278 		slc_mode == SLC_EXPORT ? "local" :
   1279 					 "remote");
   1280 }
   1281 
   1282 void
   1283 slc_mode_export(int n)
   1284 {
   1285     slc_mode = SLC_EXPORT;
   1286     if (my_state_is_will(TELOPT_LINEMODE))
   1287 	slc_export();
   1288 }
   1289 
   1290 void
   1291 slc_mode_import(int def)
   1292 {
   1293     slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
   1294     if (my_state_is_will(TELOPT_LINEMODE))
   1295 	slc_import(def);
   1296 }
   1297 
   1298 unsigned char slc_import_val[] = {
   1299 	IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
   1300 };
   1301 unsigned char slc_import_def[] = {
   1302 	IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
   1303 };
   1304 
   1305 void
   1306 slc_import(int def)
   1307 {
   1308     if (NETROOM() > sizeof(slc_import_val)) {
   1309 	if (def) {
   1310 	    ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
   1311 	    printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
   1312 	} else {
   1313 	    ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
   1314 	    printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
   1315 	}
   1316     }
   1317 /*@*/ else printf("slc_import: not enough room\n");
   1318 }
   1319 
   1320 void
   1321 slc_export(void)
   1322 {
   1323     struct spc *spcp;
   1324 
   1325     TerminalDefaultChars();
   1326 
   1327     slc_start_reply();
   1328     for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
   1329 	if (spcp->mylevel != SLC_NOSUPPORT) {
   1330 	    if (spcp->val == (cc_t)(_POSIX_VDISABLE))
   1331 		spcp->flags = SLC_NOSUPPORT;
   1332 	    else
   1333 		spcp->flags = spcp->mylevel;
   1334 	    if (spcp->valp)
   1335 		spcp->val = *spcp->valp;
   1336 	    slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
   1337 	}
   1338     }
   1339     slc_end_reply();
   1340     (void)slc_update();
   1341     setconnmode(1);	/* Make sure the character values are set */
   1342 }
   1343 
   1344 void
   1345 slc(unsigned char *cp, int len)
   1346 {
   1347 	struct spc *spcp;
   1348 	int func,level;
   1349 
   1350 	slc_start_reply();
   1351 
   1352 	for (; len >= 3; len -=3, cp +=3) {
   1353 
   1354 		func = cp[SLC_FUNC];
   1355 
   1356 		if (func == 0) {
   1357 			/*
   1358 			 * Client side: always ignore 0 function.
   1359 			 */
   1360 			continue;
   1361 		}
   1362 		if (func > NSLC) {
   1363 			if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
   1364 				slc_add_reply(func, SLC_NOSUPPORT, 0);
   1365 			continue;
   1366 		}
   1367 
   1368 		spcp = &spc_data[func];
   1369 
   1370 		level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
   1371 
   1372 		if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
   1373 		    ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
   1374 			continue;
   1375 		}
   1376 
   1377 		if (level == (SLC_DEFAULT|SLC_ACK)) {
   1378 			/*
   1379 			 * This is an error condition, the SLC_ACK
   1380 			 * bit should never be set for the SLC_DEFAULT
   1381 			 * level.  Our best guess to recover is to
   1382 			 * ignore the SLC_ACK bit.
   1383 			 */
   1384 			cp[SLC_FLAGS] &= ~SLC_ACK;
   1385 		}
   1386 
   1387 		if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
   1388 			spcp->val = (cc_t)cp[SLC_VALUE];
   1389 			spcp->flags = cp[SLC_FLAGS];	/* include SLC_ACK */
   1390 			continue;
   1391 		}
   1392 
   1393 		level &= ~SLC_ACK;
   1394 
   1395 		if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
   1396 			spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
   1397 			spcp->val = (cc_t)cp[SLC_VALUE];
   1398 		}
   1399 		if (level == SLC_DEFAULT) {
   1400 			if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
   1401 				spcp->flags = spcp->mylevel;
   1402 			else
   1403 				spcp->flags = SLC_NOSUPPORT;
   1404 		}
   1405 		slc_add_reply(func, spcp->flags, spcp->val);
   1406 	}
   1407 	slc_end_reply();
   1408 	if (slc_update())
   1409 		setconnmode(1);	/* set the  new character values */
   1410 }
   1411 
   1412 void
   1413 slc_check(void)
   1414 {
   1415     struct spc *spcp;
   1416 
   1417     slc_start_reply();
   1418     for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
   1419 	if (spcp->valp && spcp->val != *spcp->valp) {
   1420 	    spcp->val = *spcp->valp;
   1421 	    if (spcp->val == (cc_t)(_POSIX_VDISABLE))
   1422 		spcp->flags = SLC_NOSUPPORT;
   1423 	    else
   1424 		spcp->flags = spcp->mylevel;
   1425 	    slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
   1426 	}
   1427     }
   1428     slc_end_reply();
   1429     setconnmode(1);
   1430 }
   1431 
   1432 
   1433 unsigned char slc_reply[128];
   1434 unsigned char *slc_replyp;
   1435 
   1436 void
   1437 slc_start_reply(void)
   1438 {
   1439 	slc_replyp = slc_reply;
   1440 	*slc_replyp++ = IAC;
   1441 	*slc_replyp++ = SB;
   1442 	*slc_replyp++ = TELOPT_LINEMODE;
   1443 	*slc_replyp++ = LM_SLC;
   1444 }
   1445 
   1446 void
   1447 slc_add_reply(unsigned int func, unsigned int flags, cc_t value)
   1448 {
   1449 	if ((*slc_replyp++ = func) == IAC)
   1450 		*slc_replyp++ = IAC;
   1451 	if ((*slc_replyp++ = flags) == IAC)
   1452 		*slc_replyp++ = IAC;
   1453 	if ((*slc_replyp++ = (unsigned char)value) == IAC)
   1454 		*slc_replyp++ = IAC;
   1455 }
   1456 
   1457 void
   1458 slc_end_reply(void)
   1459 {
   1460     int len;
   1461 
   1462     *slc_replyp++ = IAC;
   1463     *slc_replyp++ = SE;
   1464     len = slc_replyp - slc_reply;
   1465     if (len <= 6)
   1466 	return;
   1467     if (NETROOM() > len) {
   1468 	ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
   1469 	printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
   1470     }
   1471 /*@*/else printf("slc_end_reply: not enough room\n");
   1472 }
   1473 
   1474 int
   1475 slc_update(void)
   1476 {
   1477 	struct spc *spcp;
   1478 	int need_update = 0;
   1479 
   1480 	for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
   1481 		if (!(spcp->flags&SLC_ACK))
   1482 			continue;
   1483 		spcp->flags &= ~SLC_ACK;
   1484 		if (spcp->valp && (*spcp->valp != spcp->val)) {
   1485 			*spcp->valp = spcp->val;
   1486 			need_update = 1;
   1487 		}
   1488 	}
   1489 	return(need_update);
   1490 }
   1491 
   1492 #ifdef	OLD_ENVIRON
   1493 # ifdef	ENV_HACK
   1494 /*
   1495  * Earlier version of telnet/telnetd from the BSD code had
   1496  * the definitions of VALUE and VAR reversed.  To ensure
   1497  * maximum interoperability, we assume that the server is
   1498  * an older BSD server, until proven otherwise.  The newer
   1499  * BSD servers should be able to handle either definition,
   1500  * so it is better to use the wrong values if we don't
   1501  * know what type of server it is.
   1502  */
   1503 int env_auto = 1;
   1504 int old_env_var = OLD_ENV_VAR;
   1505 int old_env_value = OLD_ENV_VALUE;
   1506 # else
   1507 #  define old_env_var OLD_ENV_VAR
   1508 #  define old_env_value OLD_ENV_VALUE
   1509 # endif
   1510 #endif
   1511 
   1512 void
   1513 env_opt(unsigned char *buf, int len)
   1514 {
   1515 	unsigned char *ep = 0, *epc = 0;
   1516 	int i;
   1517 
   1518 	switch(buf[0]&0xff) {
   1519 	case TELQUAL_SEND:
   1520 		env_opt_start();
   1521 		if (len == 1) {
   1522 			env_opt_add(NULL);
   1523 		} else for (i = 1; i < len; i++) {
   1524 			switch (buf[i]&0xff) {
   1525 #ifdef	OLD_ENVIRON
   1526 			case OLD_ENV_VAR:
   1527 # ifdef	ENV_HACK
   1528 				if (telopt_environ == TELOPT_OLD_ENVIRON
   1529 				    && env_auto) {
   1530 					/* Server has the same definitions */
   1531 					old_env_var = OLD_ENV_VAR;
   1532 					old_env_value = OLD_ENV_VALUE;
   1533 				}
   1534 				/* FALL THROUGH */
   1535 # endif
   1536 			case OLD_ENV_VALUE:
   1537 				/*
   1538 				 * Although OLD_ENV_VALUE is not legal, we will
   1539 				 * still recognize it, just in case it is an
   1540 				 * old server that has VAR & VALUE mixed up...
   1541 				 */
   1542 				/* FALL THROUGH */
   1543 #else
   1544 			case NEW_ENV_VAR:
   1545 #endif
   1546 			case ENV_USERVAR:
   1547 				if (ep) {
   1548 					*epc = 0;
   1549 					env_opt_add(ep);
   1550 				}
   1551 				ep = epc = &buf[i+1];
   1552 				break;
   1553 			case ENV_ESC:
   1554 				i++;
   1555 				/*FALL THROUGH*/
   1556 			default:
   1557 				if (epc)
   1558 					*epc++ = buf[i];
   1559 				break;
   1560 			}
   1561 		}
   1562 		if (ep) {
   1563 			*epc = 0;
   1564 			env_opt_add(ep);
   1565 		}
   1566 		env_opt_end(1);
   1567 		break;
   1568 
   1569 	case TELQUAL_IS:
   1570 	case TELQUAL_INFO:
   1571 		/* Ignore for now.  We shouldn't get it anyway. */
   1572 		break;
   1573 
   1574 	default:
   1575 		break;
   1576 	}
   1577 }
   1578 
   1579 #define	OPT_REPLY_SIZE	256
   1580 unsigned char *opt_reply;
   1581 unsigned char *opt_replyp;
   1582 unsigned char *opt_replyend;
   1583 
   1584 void
   1585 env_opt_start(void)
   1586 {
   1587 	unsigned char *p;
   1588 
   1589 	if (opt_reply) {
   1590 		p = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
   1591 		if (p == NULL)
   1592 			free(opt_reply);
   1593 	} else
   1594 		p = (unsigned char *)malloc(OPT_REPLY_SIZE);
   1595 	opt_reply = p;
   1596 	if (opt_reply == NULL) {
   1597 /*@*/		printf("env_opt_start: malloc()/realloc() failed!!!\n");
   1598 		opt_reply = opt_replyp = opt_replyend = NULL;
   1599 		return;
   1600 	}
   1601 	opt_replyp = opt_reply;
   1602 	opt_replyend = opt_reply + OPT_REPLY_SIZE;
   1603 	*opt_replyp++ = IAC;
   1604 	*opt_replyp++ = SB;
   1605 	*opt_replyp++ = telopt_environ;
   1606 	*opt_replyp++ = TELQUAL_IS;
   1607 }
   1608 
   1609 void
   1610 env_opt_start_info(void)
   1611 {
   1612 	env_opt_start();
   1613 	if (opt_replyp)
   1614 	    opt_replyp[-1] = TELQUAL_INFO;
   1615 }
   1616 
   1617 void
   1618 env_opt_add(unsigned char *ep)
   1619 {
   1620 	unsigned char *vp, c;
   1621 
   1622 	if (opt_reply == NULL)		/*XXX*/
   1623 		return;			/*XXX*/
   1624 
   1625 	if (ep == NULL || *ep == '\0') {
   1626 		/* Send user defined variables first. */
   1627 		env_default(1, 0);
   1628 		while ((ep = env_default(0, 0)) != NULL)
   1629 			env_opt_add(ep);
   1630 
   1631 		/* Now add the list of well know variables.  */
   1632 		env_default(1, 1);
   1633 		while ((ep = env_default(0, 1)) != NULL)
   1634 			env_opt_add(ep);
   1635 		return;
   1636 	}
   1637 	vp = env_getvalue(ep);
   1638 	if (opt_replyp + (vp ? strlen((char *)vp) : 0) +
   1639 				strlen((char *)ep) + 6 > opt_replyend)
   1640 	{
   1641 		int len;
   1642 		unsigned char *p;
   1643 		opt_replyend += OPT_REPLY_SIZE;
   1644 		len = opt_replyend - opt_reply;
   1645 		p = (unsigned char *)realloc(opt_reply, len);
   1646 		if (p == NULL)
   1647 			free(opt_reply);
   1648 		opt_reply = p;
   1649 		if (opt_reply == NULL) {
   1650 /*@*/			printf("env_opt_add: realloc() failed!!!\n");
   1651 			opt_reply = opt_replyp = opt_replyend = NULL;
   1652 			return;
   1653 		}
   1654 		opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
   1655 		opt_replyend = opt_reply + len;
   1656 	}
   1657 	if (opt_welldefined(ep))
   1658 #ifdef	OLD_ENVIRON
   1659 		if (telopt_environ == TELOPT_OLD_ENVIRON)
   1660 			*opt_replyp++ = old_env_var;
   1661 		else
   1662 #endif
   1663 			*opt_replyp++ = NEW_ENV_VAR;
   1664 	else
   1665 		*opt_replyp++ = ENV_USERVAR;
   1666 	for (;;) {
   1667 		while ((c = *ep++) != '\0') {
   1668 			switch(c&0xff) {
   1669 			case IAC:
   1670 				*opt_replyp++ = IAC;
   1671 				break;
   1672 			case NEW_ENV_VAR:
   1673 			case NEW_ENV_VALUE:
   1674 			case ENV_ESC:
   1675 			case ENV_USERVAR:
   1676 				*opt_replyp++ = ENV_ESC;
   1677 				break;
   1678 			}
   1679 			*opt_replyp++ = c;
   1680 		}
   1681 		if ((ep = vp) != NULL) {
   1682 #ifdef	OLD_ENVIRON
   1683 			if (telopt_environ == TELOPT_OLD_ENVIRON)
   1684 				*opt_replyp++ = old_env_value;
   1685 			else
   1686 #endif
   1687 				*opt_replyp++ = NEW_ENV_VALUE;
   1688 			vp = NULL;
   1689 		} else
   1690 			break;
   1691 	}
   1692 }
   1693 
   1694 int
   1695 opt_welldefined(char *ep)
   1696 {
   1697 	if ((strcmp(ep, "USER") == 0) ||
   1698 	    (strcmp(ep, "DISPLAY") == 0) ||
   1699 	    (strcmp(ep, "PRINTER") == 0) ||
   1700 	    (strcmp(ep, "SYSTEMTYPE") == 0) ||
   1701 	    (strcmp(ep, "JOB") == 0) ||
   1702 	    (strcmp(ep, "ACCT") == 0))
   1703 		return(1);
   1704 	return(0);
   1705 }
   1706 void
   1707 env_opt_end(int emptyok)
   1708 {
   1709 	int len;
   1710 
   1711 	len = opt_replyp - opt_reply + 2;
   1712 	if (emptyok || len > 6) {
   1713 		*opt_replyp++ = IAC;
   1714 		*opt_replyp++ = SE;
   1715 		if (NETROOM() > len) {
   1716 			ring_supply_data(&netoring, opt_reply, len);
   1717 			printsub('>', &opt_reply[2], len - 2);
   1718 		}
   1719 /*@*/		else printf("slc_end_reply: not enough room\n");
   1720 	}
   1721 	if (opt_reply) {
   1722 		free(opt_reply);
   1723 		opt_reply = opt_replyp = opt_replyend = NULL;
   1724 	}
   1725 }
   1726 
   1727 
   1728 
   1730 int
   1731 telrcv(void)
   1732 {
   1733     int c;
   1734     int scc;
   1735     unsigned char *sbp = NULL;
   1736     int count;
   1737     int returnValue = 0;
   1738 
   1739     scc = 0;
   1740     count = 0;
   1741     while (TTYROOM() > 2) {
   1742 	if (scc == 0) {
   1743 	    if (count) {
   1744 		ring_consumed(&netiring, count);
   1745 		returnValue = 1;
   1746 		count = 0;
   1747 	    }
   1748 	    sbp = netiring.consume;
   1749 	    scc = ring_full_consecutive(&netiring);
   1750 	    if (scc == 0) {
   1751 		/* No more data coming in */
   1752 		break;
   1753 	    }
   1754 	}
   1755 
   1756 	c = *sbp++ & 0xff, scc--; count++;
   1757 #ifdef	ENCRYPTION
   1758 	if (decrypt_input)
   1759 		c = (*decrypt_input)(c);
   1760 #endif	/* ENCRYPTION */
   1761 
   1762 	switch (telrcv_state) {
   1763 
   1764 	case TS_CR:
   1765 	    telrcv_state = TS_DATA;
   1766 	    if (c == '\0') {
   1767 		break;	/* Ignore \0 after CR */
   1768 	    }
   1769 	    else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
   1770 		TTYADD(c);
   1771 		break;
   1772 	    }
   1773 	    /* Else, fall through */
   1774 
   1775 	case TS_DATA:
   1776 	    if (c == IAC) {
   1777 		telrcv_state = TS_IAC;
   1778 		break;
   1779 	    }
   1780 #	    if defined(TN3270)
   1781 	    if (In3270) {
   1782 		*Ifrontp++ = c;
   1783 		while (scc > 0) {
   1784 		    c = *sbp++ & 0377, scc--; count++;
   1785 #ifdef	ENCRYPTION
   1786 		    if (decrypt_input)
   1787 			c = (*decrypt_input)(c);
   1788 #endif	/* ENCRYPTION */
   1789 		    if (c == IAC) {
   1790 			telrcv_state = TS_IAC;
   1791 			break;
   1792 		    }
   1793 		    *Ifrontp++ = c;
   1794 		}
   1795 	    } else
   1796 #	    endif /* defined(TN3270) */
   1797 		    /*
   1798 		     * The 'crmod' hack (see following) is needed
   1799 		     * since we can't * set CRMOD on output only.
   1800 		     * Machines like MULTICS like to send \r without
   1801 		     * \n; since we must turn off CRMOD to get proper
   1802 		     * input, the mapping is done here (sigh).
   1803 		     */
   1804 	    if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
   1805 		if (scc > 0) {
   1806 		    c = *sbp&0xff;
   1807 #ifdef	ENCRYPTION
   1808 		    if (decrypt_input)
   1809 			c = (*decrypt_input)(c);
   1810 #endif	/* ENCRYPTION */
   1811 		    if (c == 0) {
   1812 			sbp++, scc--; count++;
   1813 			/* a "true" CR */
   1814 			TTYADD('\r');
   1815 		    } else if (my_want_state_is_dont(TELOPT_ECHO) &&
   1816 					(c == '\n')) {
   1817 			sbp++, scc--; count++;
   1818 			TTYADD('\n');
   1819 		    } else {
   1820 #ifdef	ENCRYPTION
   1821 			if (decrypt_input)
   1822 			    (*decrypt_input)(-1);
   1823 #endif	/* ENCRYPTION */
   1824 
   1825 			TTYADD('\r');
   1826 			if (crmod) {
   1827 				TTYADD('\n');
   1828 			}
   1829 		    }
   1830 		} else {
   1831 		    telrcv_state = TS_CR;
   1832 		    TTYADD('\r');
   1833 		    if (crmod) {
   1834 			    TTYADD('\n');
   1835 		    }
   1836 		}
   1837 	    } else {
   1838 		TTYADD(c);
   1839 	    }
   1840 	    continue;
   1841 
   1842 	case TS_IAC:
   1843 process_iac:
   1844 	    switch (c) {
   1845 
   1846 	    case WILL:
   1847 		telrcv_state = TS_WILL;
   1848 		continue;
   1849 
   1850 	    case WONT:
   1851 		telrcv_state = TS_WONT;
   1852 		continue;
   1853 
   1854 	    case DO:
   1855 		telrcv_state = TS_DO;
   1856 		continue;
   1857 
   1858 	    case DONT:
   1859 		telrcv_state = TS_DONT;
   1860 		continue;
   1861 
   1862 	    case DM:
   1863 		    /*
   1864 		     * We may have missed an urgent notification,
   1865 		     * so make sure we flush whatever is in the
   1866 		     * buffer currently.
   1867 		     */
   1868 		printoption("RCVD", IAC, DM);
   1869 		SYNCHing = 1;
   1870 		(void) ttyflush(1);
   1871 		SYNCHing = stilloob();
   1872 		settimer(gotDM);
   1873 		break;
   1874 
   1875 	    case SB:
   1876 		SB_CLEAR();
   1877 		telrcv_state = TS_SB;
   1878 		continue;
   1879 
   1880 #	    if defined(TN3270)
   1881 	    case EOR:
   1882 		if (In3270) {
   1883 		    if (Ibackp == Ifrontp) {
   1884 			Ibackp = Ifrontp = Ibuf;
   1885 			ISend = 0;	/* should have been! */
   1886 		    } else {
   1887 			Ibackp += DataFromNetwork(Ibackp, Ifrontp-Ibackp, 1);
   1888 			ISend = 1;
   1889 		    }
   1890 		}
   1891 		printoption("RCVD", IAC, EOR);
   1892 		break;
   1893 #	    endif /* defined(TN3270) */
   1894 
   1895 	    case IAC:
   1896 #	    if !defined(TN3270)
   1897 		TTYADD(IAC);
   1898 #	    else /* !defined(TN3270) */
   1899 		if (In3270) {
   1900 		    *Ifrontp++ = IAC;
   1901 		} else {
   1902 		    TTYADD(IAC);
   1903 		}
   1904 #	    endif /* !defined(TN3270) */
   1905 		break;
   1906 
   1907 	    case NOP:
   1908 	    case GA:
   1909 	    default:
   1910 		printoption("RCVD", IAC, c);
   1911 		break;
   1912 	    }
   1913 	    telrcv_state = TS_DATA;
   1914 	    continue;
   1915 
   1916 	case TS_WILL:
   1917 	    printoption("RCVD", WILL, c);
   1918 	    willoption(c);
   1919 	    SetIn3270();
   1920 	    telrcv_state = TS_DATA;
   1921 	    continue;
   1922 
   1923 	case TS_WONT:
   1924 	    printoption("RCVD", WONT, c);
   1925 	    wontoption(c);
   1926 	    SetIn3270();
   1927 	    telrcv_state = TS_DATA;
   1928 	    continue;
   1929 
   1930 	case TS_DO:
   1931 	    printoption("RCVD", DO, c);
   1932 	    dooption(c);
   1933 	    SetIn3270();
   1934 	    if (c == TELOPT_NAWS) {
   1935 		sendnaws();
   1936 	    } else if (c == TELOPT_LFLOW) {
   1937 		localflow = 1;
   1938 		setcommandmode();
   1939 		setconnmode(0);
   1940 	    }
   1941 	    telrcv_state = TS_DATA;
   1942 	    continue;
   1943 
   1944 	case TS_DONT:
   1945 	    printoption("RCVD", DONT, c);
   1946 	    dontoption(c);
   1947 	    flushline = 1;
   1948 	    setconnmode(0);	/* set new tty mode (maybe) */
   1949 	    SetIn3270();
   1950 	    telrcv_state = TS_DATA;
   1951 	    continue;
   1952 
   1953 	case TS_SB:
   1954 	    if (c == IAC) {
   1955 		telrcv_state = TS_SE;
   1956 	    } else {
   1957 		SB_ACCUM(c);
   1958 	    }
   1959 	    continue;
   1960 
   1961 	case TS_SE:
   1962 	    if (c != SE) {
   1963 		if (c != IAC) {
   1964 		    /*
   1965 		     * This is an error.  We only expect to get
   1966 		     * "IAC IAC" or "IAC SE".  Several things may
   1967 		     * have happend.  An IAC was not doubled, the
   1968 		     * IAC SE was left off, or another option got
   1969 		     * inserted into the suboption are all possibilities.
   1970 		     * If we assume that the IAC was not doubled,
   1971 		     * and really the IAC SE was left off, we could
   1972 		     * get into an infinite loop here.  So, instead,
   1973 		     * we terminate the suboption, and process the
   1974 		     * partial suboption if we can.
   1975 		     */
   1976 		    SB_ACCUM(IAC);
   1977 		    SB_ACCUM(c);
   1978 		    subpointer -= 2;
   1979 		    SB_TERM();
   1980 
   1981 		    printoption("In SUBOPTION processing, RCVD", IAC, c);
   1982 		    suboption();	/* handle sub-option */
   1983 		    SetIn3270();
   1984 		    telrcv_state = TS_IAC;
   1985 		    goto process_iac;
   1986 		}
   1987 		SB_ACCUM(c);
   1988 		telrcv_state = TS_SB;
   1989 	    } else {
   1990 		SB_ACCUM(IAC);
   1991 		SB_ACCUM(SE);
   1992 		subpointer -= 2;
   1993 		SB_TERM();
   1994 		suboption();	/* handle sub-option */
   1995 		SetIn3270();
   1996 		telrcv_state = TS_DATA;
   1997 	    }
   1998 	}
   1999     }
   2000     if (count)
   2001 	ring_consumed(&netiring, count);
   2002     return returnValue||count;
   2003 }
   2004 
   2005 static int bol = 1, local = 0;
   2006 
   2007 int
   2008 rlogin_susp(void)
   2009 {
   2010     if (local) {
   2011 	local = 0;
   2012 	bol = 1;
   2013 	command(0, "z\n", 2);
   2014 	return(1);
   2015     }
   2016     return(0);
   2017 }
   2018 
   2019 static int
   2020 telsnd(void)
   2021 {
   2022     int tcc;
   2023     int count;
   2024     int returnValue = 0;
   2025     unsigned char *tbp = NULL;
   2026 
   2027     tcc = 0;
   2028     count = 0;
   2029     while (NETROOM() > 2) {
   2030 	int sc;
   2031 	int c;
   2032 
   2033 	if (tcc == 0) {
   2034 	    if (count) {
   2035 		ring_consumed(&ttyiring, count);
   2036 		returnValue = 1;
   2037 		count = 0;
   2038 	    }
   2039 	    tbp = ttyiring.consume;
   2040 	    tcc = ring_full_consecutive(&ttyiring);
   2041 	    if (tcc == 0) {
   2042 		break;
   2043 	    }
   2044 	}
   2045 	c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
   2046 	if (rlogin != _POSIX_VDISABLE) {
   2047 		if (bol) {
   2048 			bol = 0;
   2049 			if (sc == rlogin) {
   2050 				local = 1;
   2051 				continue;
   2052 			}
   2053 		} else if (local) {
   2054 			local = 0;
   2055 			if (sc == '.' || c == termEofChar) {
   2056 				bol = 1;
   2057 				command(0, "close\n", 6);
   2058 				continue;
   2059 			}
   2060 			if (sc == termSuspChar) {
   2061 				bol = 1;
   2062 				command(0, "z\n", 2);
   2063 				continue;
   2064 			}
   2065 			if (sc == escape) {
   2066 				command(0, (char *)tbp, tcc);
   2067 				bol = 1;
   2068 				count += tcc;
   2069 				tcc = 0;
   2070 				flushline = 1;
   2071 				break;
   2072 			}
   2073 			if (sc != rlogin) {
   2074 				++tcc;
   2075 				--tbp;
   2076 				--count;
   2077 				c = sc = rlogin;
   2078 			}
   2079 		}
   2080 		if ((sc == '\n') || (sc == '\r'))
   2081 			bol = 1;
   2082 	} else if (sc == escape && escape != _POSIX_VDISABLE) {
   2083 	    /*
   2084 	     * Double escape is a pass through of a single escape character.
   2085 	     */
   2086 	    if (tcc && strip(*tbp) == escape) {
   2087 		tbp++;
   2088 		tcc--;
   2089 		count++;
   2090 		bol = 0;
   2091 	    } else {
   2092 		command(0, (char *)tbp, tcc);
   2093 		bol = 1;
   2094 		count += tcc;
   2095 		tcc = 0;
   2096 		flushline = 1;
   2097 		break;
   2098 	    }
   2099 	} else
   2100 	    bol = 0;
   2101 #ifdef	KLUDGELINEMODE
   2102 	if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
   2103 	    if (tcc > 0 && strip(*tbp) == echoc) {
   2104 		tcc--; tbp++; count++;
   2105 	    } else {
   2106 		dontlecho = !dontlecho;
   2107 		settimer(echotoggle);
   2108 		setconnmode(0);
   2109 		flushline = 1;
   2110 		break;
   2111 	    }
   2112 	}
   2113 #endif
   2114 	if (sc != _POSIX_VDISABLE && MODE_LOCAL_CHARS(globalmode)) {
   2115 	    if (TerminalSpecialChars(sc) == 0) {
   2116 		bol = 1;
   2117 		break;
   2118 	    }
   2119 	}
   2120 	if (my_want_state_is_wont(TELOPT_BINARY)) {
   2121 	    switch (c) {
   2122 	    case '\n':
   2123 		    /*
   2124 		     * If we are in CRMOD mode (\r ==> \n)
   2125 		     * on our local machine, then probably
   2126 		     * a newline (unix) is CRLF (TELNET).
   2127 		     */
   2128 		if (MODE_LOCAL_CHARS(globalmode)) {
   2129 		    NETADD('\r');
   2130 		}
   2131 		NETADD('\n');
   2132 		bol = flushline = 1;
   2133 		break;
   2134 	    case '\r':
   2135 		if (!crlf) {
   2136 		    NET2ADD('\r', '\0');
   2137 		} else {
   2138 		    NET2ADD('\r', '\n');
   2139 		}
   2140 		bol = flushline = 1;
   2141 		break;
   2142 	    case IAC:
   2143 		NET2ADD(IAC, IAC);
   2144 		break;
   2145 	    default:
   2146 		NETADD(c);
   2147 		break;
   2148 	    }
   2149 	} else if (c == IAC) {
   2150 	    NET2ADD(IAC, IAC);
   2151 	} else {
   2152 	    NETADD(c);
   2153 	}
   2154     }
   2155     if (count)
   2156 	ring_consumed(&ttyiring, count);
   2157     return returnValue||count;		/* Non-zero if we did anything */
   2158 }
   2159 
   2160 /*
   2162  * Scheduler()
   2163  *
   2164  * Try to do something.
   2165  *
   2166  * If we do something useful, return 1; else return 0.
   2167  *
   2168  */
   2169 
   2170 
   2171 int
   2172 Scheduler(int block)			/* should we block in the select ? */
   2173 {
   2174 		/* One wants to be a bit careful about setting returnValue
   2175 		 * to one, since a one implies we did some useful work,
   2176 		 * and therefore probably won't be called to block next
   2177 		 * time (TN3270 mode only).
   2178 		 */
   2179     int returnValue;
   2180     int netin, netout, netex, ttyin, ttyout;
   2181 
   2182     /* Decide which rings should be processed */
   2183 
   2184     netout = ring_full_count(&netoring) &&
   2185 	    (flushline ||
   2186 		(my_want_state_is_wont(TELOPT_LINEMODE)
   2187 #ifdef	KLUDGELINEMODE
   2188 			&& (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
   2189 #endif
   2190 		) ||
   2191 			my_want_state_is_will(TELOPT_BINARY));
   2192     ttyout = ring_full_count(&ttyoring);
   2193 
   2194 #ifdef TN3270
   2195     ttyin = ring_empty_count(&ttyiring) && (clienteof == 0) && (shell_active == 0);
   2196 #else	/* defined(TN3270) */
   2197     ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
   2198 #endif	/* defined(TN3270) */
   2199 
   2200 #ifdef TN3270
   2201     netin = ring_empty_count(&netiring);
   2202 #   else /* !defined(TN3270) */
   2203     netin = !ISend && ring_empty_count(&netiring);
   2204 #   endif /* !defined(TN3270) */
   2205 
   2206     netex = !SYNCHing;
   2207 
   2208     /* If we have seen a signal recently, reset things */
   2209 #   ifdef TN3270
   2210     if (HaveInput) {
   2211 	HaveInput = 0;
   2212 	(void) signal(SIGIO, inputAvailable);
   2213     }
   2214 #endif	/* defined(TN3270) */
   2215 
   2216     /* Call to system code to process rings */
   2217 
   2218     returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
   2219 
   2220     /* Now, look at the input rings, looking for work to do. */
   2221 
   2222     if (ring_full_count(&ttyiring)) {
   2223 #   if defined(TN3270)
   2224 	if (In3270) {
   2225 	    int c;
   2226 
   2227 	    c = DataFromTerminal(ttyiring.consume,
   2228 					ring_full_consecutive(&ttyiring));
   2229 	    if (c) {
   2230 		returnValue = 1;
   2231 		ring_consumed(&ttyiring, c);
   2232 	    }
   2233 	} else {
   2234 #   endif /* defined(TN3270) */
   2235 	    returnValue |= telsnd();
   2236 #   if defined(TN3270)
   2237 	}
   2238 #   endif /* defined(TN3270) */
   2239     }
   2240 
   2241     if (ring_full_count(&netiring)) {
   2242 #	if !defined(TN3270)
   2243 	returnValue |= telrcv();
   2244 #	else /* !defined(TN3270) */
   2245 	returnValue = Push3270();
   2246 #	endif /* !defined(TN3270) */
   2247     }
   2248     return returnValue;
   2249 }
   2250 
   2251 /*
   2253  * Select from tty and network...
   2254  */
   2255 void
   2256 telnet(const char *user)
   2257 {
   2258     sys_telnet_init();
   2259 
   2260 #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
   2261     {
   2262 	static char local_host[MAXHOSTNAMELEN + 1] = { 0 };
   2263 
   2264 	if (!local_host[0]) {
   2265 		gethostname(local_host, sizeof(local_host));
   2266 		local_host[sizeof(local_host)-1] = 0;
   2267 	}
   2268 	auth_encrypt_init(local_host, hostname, "TELNET", 0);
   2269 	auth_encrypt_user(user);
   2270     }
   2271 #endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
   2272 #   if !defined(TN3270)
   2273     if (telnetport) {
   2274 #ifdef AUTHENTICATION
   2275 	if (autologin)
   2276 		send_will(TELOPT_AUTHENTICATION, 1);
   2277 #endif
   2278 #ifdef	ENCRYPTION
   2279 	send_do(TELOPT_ENCRYPT, 1);
   2280 	send_will(TELOPT_ENCRYPT, 1);
   2281 #endif	/* ENCRYPTION */
   2282 	send_do(TELOPT_SGA, 1);
   2283 	send_will(TELOPT_TTYPE, 1);
   2284 	send_will(TELOPT_NAWS, 1);
   2285 	send_will(TELOPT_TSPEED, 1);
   2286 	send_will(TELOPT_LFLOW, 1);
   2287 	send_will(TELOPT_LINEMODE, 1);
   2288 	send_will(TELOPT_NEW_ENVIRON, 1);
   2289 	send_do(TELOPT_STATUS, 1);
   2290 	if (env_getvalue((unsigned char *)"DISPLAY"))
   2291 	    send_will(TELOPT_XDISPLOC, 1);
   2292 	if (eight)
   2293 	    tel_enter_binary(eight);
   2294     }
   2295 #   endif /* !defined(TN3270) */
   2296 
   2297 #   if !defined(TN3270)
   2298     for (;;) {
   2299 	int schedValue;
   2300 
   2301 	while ((schedValue = Scheduler(0)) != 0) {
   2302 	    if (schedValue == -1) {
   2303 		setcommandmode();
   2304 		return;
   2305 	    }
   2306 	}
   2307 
   2308 	if (Scheduler(1) == -1) {
   2309 	    setcommandmode();
   2310 	    return;
   2311 	}
   2312     }
   2313 #   else /* !defined(TN3270) */
   2314     for (;;) {
   2315 	int schedValue;
   2316 
   2317 	while (!In3270 && !shell_active) {
   2318 	    if (Scheduler(1) == -1) {
   2319 		setcommandmode();
   2320 		return;
   2321 	    }
   2322 	}
   2323 
   2324 	while ((schedValue = Scheduler(0)) != 0) {
   2325 	    if (schedValue == -1) {
   2326 		setcommandmode();
   2327 		return;
   2328 	    }
   2329 	}
   2330 		/* If there is data waiting to go out to terminal, don't
   2331 		 * schedule any more data for the terminal.
   2332 		 */
   2333 	if (ring_full_count(&ttyoring)) {
   2334 	    schedValue = 1;
   2335 	} else {
   2336 	    if (shell_active) {
   2337 		if (shell_continue() == 0) {
   2338 		    ConnectScreen();
   2339 		}
   2340 	    } else if (In3270) {
   2341 		schedValue = DoTerminalOutput();
   2342 	    }
   2343 	}
   2344 	if (schedValue && (shell_active == 0)) {
   2345 	    if (Scheduler(1) == -1) {
   2346 		setcommandmode();
   2347 		return;
   2348 	    }
   2349 	}
   2350     }
   2351 #   endif /* !defined(TN3270) */
   2352 }
   2353 
   2354 #if	0	/* XXX - this not being in is a bug */
   2356 /*
   2357  * nextitem()
   2358  *
   2359  *	Return the address of the next "item" in the TELNET data
   2360  * stream.  This will be the address of the next character if
   2361  * the current address is a user data character, or it will
   2362  * be the address of the character following the TELNET command
   2363  * if the current address is a TELNET IAC ("I Am a Command")
   2364  * character.
   2365  */
   2366 
   2367 static char *
   2368 nextitem(char *current)
   2369 {
   2370     if ((*current&0xff) != IAC) {
   2371 	return current+1;
   2372     }
   2373     switch (*(current+1)&0xff) {
   2374     case DO:
   2375     case DONT:
   2376     case WILL:
   2377     case WONT:
   2378 	return current+3;
   2379     case SB:		/* loop forever looking for the SE */
   2380 	{
   2381 	    char *look = current+2;
   2382 
   2383 	    for (;;) {
   2384 		if ((*look++&0xff) == IAC) {
   2385 		    if ((*look++&0xff) == SE) {
   2386 			return look;
   2387 		    }
   2388 		}
   2389 	    }
   2390 	}
   2391     default:
   2392 	return current+2;
   2393     }
   2394 }
   2395 #endif	/* 0 */
   2396 
   2397 /*
   2398  * netclear()
   2399  *
   2400  *	We are about to do a TELNET SYNCH operation.  Clear
   2401  * the path to the network.
   2402  *
   2403  *	Things are a bit tricky since we may have sent the first
   2404  * byte or so of a previous TELNET command into the network.
   2405  * So, we have to scan the network buffer from the beginning
   2406  * until we are up to where we want to be.
   2407  *
   2408  *	A side effect of what we do, just to keep things
   2409  * simple, is to clear the urgent data pointer.  The principal
   2410  * caller should be setting the urgent data pointer AFTER calling
   2411  * us in any case.
   2412  */
   2413 
   2414 static void
   2415 netclear(void)
   2416 {
   2417 #if	0	/* XXX */
   2418     char *thisitem, *next;
   2419     char *good;
   2420 #define	wewant(p)	((nfrontp > p) && ((*p&0xff) == IAC) && \
   2421 				((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
   2422 
   2423     thisitem = netobuf;
   2424 
   2425     while ((next = nextitem(thisitem)) <= netobuf.send) {
   2426 	thisitem = next;
   2427     }
   2428 
   2429     /* Now, thisitem is first before/at boundary. */
   2430 
   2431     good = netobuf;	/* where the good bytes go */
   2432 
   2433     while (netoring.add > thisitem) {
   2434 	if (wewant(thisitem)) {
   2435 	    int length;
   2436 
   2437 	    next = thisitem;
   2438 	    do {
   2439 		next = nextitem(next);
   2440 	    } while (wewant(next) && (nfrontp > next));
   2441 	    length = next-thisitem;
   2442 	    memmove(good, thisitem, length);
   2443 	    good += length;
   2444 	    thisitem = next;
   2445 	} else {
   2446 	    thisitem = nextitem(thisitem);
   2447 	}
   2448     }
   2449 
   2450 #endif	/* 0 */
   2451 }
   2452 
   2453 /*
   2455  * These routines add various telnet commands to the data stream.
   2456  */
   2457 
   2458 static void
   2459 doflush(void)
   2460 {
   2461     NET2ADD(IAC, DO);
   2462     NETADD(TELOPT_TM);
   2463     flushline = 1;
   2464     flushout = 1;
   2465     (void) ttyflush(1);			/* Flush/drop output */
   2466     /* do printoption AFTER flush, otherwise the output gets tossed... */
   2467     printoption("SENT", DO, TELOPT_TM);
   2468 }
   2469 
   2470 void
   2471 xmitAO(void)
   2472 {
   2473     NET2ADD(IAC, AO);
   2474     printoption("SENT", IAC, AO);
   2475     if (autoflush) {
   2476 	doflush();
   2477     }
   2478 }
   2479 
   2480 
   2481 void
   2482 xmitEL(void)
   2483 {
   2484     NET2ADD(IAC, EL);
   2485     printoption("SENT", IAC, EL);
   2486 }
   2487 
   2488 void
   2489 xmitEC(void)
   2490 {
   2491     NET2ADD(IAC, EC);
   2492     printoption("SENT", IAC, EC);
   2493 }
   2494 
   2495 
   2496 int
   2497 dosynch(char *s)
   2498 {
   2499     netclear();			/* clear the path to the network */
   2500     NETADD(IAC);
   2501     setneturg();
   2502     NETADD(DM);
   2503     printoption("SENT", IAC, DM);
   2504     return 1;
   2505 }
   2506 
   2507 int want_status_response = 0;
   2508 
   2509 int
   2510 get_status(char *s)
   2511 {
   2512     unsigned char tmp[16];
   2513     unsigned char *cp;
   2514 
   2515     if (my_want_state_is_dont(TELOPT_STATUS)) {
   2516 	printf("Remote side does not support STATUS option\n");
   2517 	return 0;
   2518     }
   2519     cp = tmp;
   2520 
   2521     *cp++ = IAC;
   2522     *cp++ = SB;
   2523     *cp++ = TELOPT_STATUS;
   2524     *cp++ = TELQUAL_SEND;
   2525     *cp++ = IAC;
   2526     *cp++ = SE;
   2527     if (NETROOM() >= cp - tmp) {
   2528 	ring_supply_data(&netoring, tmp, cp-tmp);
   2529 	printsub('>', tmp+2, cp - tmp - 2);
   2530     }
   2531     ++want_status_response;
   2532     return 1;
   2533 }
   2534 
   2535 void
   2536 intp(void)
   2537 {
   2538     NET2ADD(IAC, IP);
   2539     printoption("SENT", IAC, IP);
   2540     flushline = 1;
   2541     if (autoflush) {
   2542 	doflush();
   2543     }
   2544     if (autosynch) {
   2545 	dosynch(NULL);
   2546     }
   2547 }
   2548 
   2549 void
   2550 sendbrk(void)
   2551 {
   2552     NET2ADD(IAC, BREAK);
   2553     printoption("SENT", IAC, BREAK);
   2554     flushline = 1;
   2555     if (autoflush) {
   2556 	doflush();
   2557     }
   2558     if (autosynch) {
   2559 	dosynch(NULL);
   2560     }
   2561 }
   2562 
   2563 void
   2564 sendabort(void)
   2565 {
   2566     NET2ADD(IAC, ABORT);
   2567     printoption("SENT", IAC, ABORT);
   2568     flushline = 1;
   2569     if (autoflush) {
   2570 	doflush();
   2571     }
   2572     if (autosynch) {
   2573 	dosynch(NULL);
   2574     }
   2575 }
   2576 
   2577 void
   2578 sendsusp(void)
   2579 {
   2580     NET2ADD(IAC, SUSP);
   2581     printoption("SENT", IAC, SUSP);
   2582     flushline = 1;
   2583     if (autoflush) {
   2584 	doflush();
   2585     }
   2586     if (autosynch) {
   2587 	dosynch(NULL);
   2588     }
   2589 }
   2590 
   2591 void
   2592 sendeof(void)
   2593 {
   2594     NET2ADD(IAC, xEOF);
   2595     printoption("SENT", IAC, xEOF);
   2596 }
   2597 
   2598 void
   2599 sendayt(void)
   2600 {
   2601     NET2ADD(IAC, AYT);
   2602     printoption("SENT", IAC, AYT);
   2603 }
   2604 
   2605 /*
   2606  * Send a window size update to the remote system.
   2607  */
   2608 
   2609 void
   2610 sendnaws(void)
   2611 {
   2612     long rows, cols;
   2613     unsigned char tmp[16];
   2614     unsigned char *cp;
   2615 
   2616     if (my_state_is_wont(TELOPT_NAWS))
   2617 	return;
   2618 
   2619 #define	PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
   2620 			    if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
   2621 
   2622     if (TerminalWindowSize(&rows, &cols) == 0) {	/* Failed */
   2623 	return;
   2624     }
   2625 
   2626     cp = tmp;
   2627 
   2628     *cp++ = IAC;
   2629     *cp++ = SB;
   2630     *cp++ = TELOPT_NAWS;
   2631     PUTSHORT(cp, cols);
   2632     PUTSHORT(cp, rows);
   2633     *cp++ = IAC;
   2634     *cp++ = SE;
   2635     if (NETROOM() >= cp - tmp) {
   2636 	ring_supply_data(&netoring, tmp, cp-tmp);
   2637 	printsub('>', tmp+2, cp - tmp - 2);
   2638     }
   2639 }
   2640 
   2641 void
   2642 tel_enter_binary(int rw)
   2643 {
   2644     if (rw&1)
   2645 	send_do(TELOPT_BINARY, 1);
   2646     if (rw&2)
   2647 	send_will(TELOPT_BINARY, 1);
   2648 }
   2649 
   2650 void
   2651 tel_leave_binary(int rw)
   2652 {
   2653     if (rw&1)
   2654 	send_dont(TELOPT_BINARY, 1);
   2655     if (rw&2)
   2656 	send_wont(TELOPT_BINARY, 1);
   2657 }
   2658