17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * Copyright (c) 1988, 1990, 1993
37c478bdstevel@tonic-gate *	The Regents of the University of California.  All rights reserved.
47c478bdstevel@tonic-gate *
57c478bdstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
67c478bdstevel@tonic-gate * modification, are permitted provided that the following conditions
77c478bdstevel@tonic-gate * are met:
87c478bdstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
97c478bdstevel@tonic-gate *    notice, this list of conditions and the following disclaimer.
107c478bdstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
117c478bdstevel@tonic-gate *    notice, this list of conditions and the following disclaimer in the
127c478bdstevel@tonic-gate *    documentation and/or other materials provided with the distribution.
137c478bdstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
147c478bdstevel@tonic-gate *    must display the following acknowledgement:
157c478bdstevel@tonic-gate *	This product includes software developed by the University of
167c478bdstevel@tonic-gate *	California, Berkeley and its contributors.
177c478bdstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
187c478bdstevel@tonic-gate *    may be used to endorse or promote products derived from this software
197c478bdstevel@tonic-gate *    without specific prior written permission.
207c478bdstevel@tonic-gate *
217c478bdstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227c478bdstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c478bdstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c478bdstevel@tonic-gate * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257c478bdstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267c478bdstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277c478bdstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287c478bdstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297c478bdstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307c478bdstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c478bdstevel@tonic-gate * SUCH DAMAGE.
327c478bdstevel@tonic-gate *
33740638cbw * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
347c478bdstevel@tonic-gate * Use is subject to license terms.
357c478bdstevel@tonic-gate */
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gate#include <sys/param.h>
387c478bdstevel@tonic-gate#include <sys/file.h>
397c478bdstevel@tonic-gate#include <sys/socket.h>
407c478bdstevel@tonic-gate#include <sys/sysmacros.h>
417c478bdstevel@tonic-gate#include <netinet/in.h>
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate#include <signal.h>
447c478bdstevel@tonic-gate#include <netdb.h>
457c478bdstevel@tonic-gate#include <ctype.h>
467c478bdstevel@tonic-gate#include <pwd.h>
477c478bdstevel@tonic-gate#include <errno.h>
487c478bdstevel@tonic-gate#include <strings.h>
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate#include <arpa/telnet.h>
517c478bdstevel@tonic-gate#include <arpa/inet.h>
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gate#include "general.h"
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate#include "ring.h"
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate#include "externs.h"
587c478bdstevel@tonic-gate#include "defines.h"
597c478bdstevel@tonic-gate#include "types.h"
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gateextern	char *telnet_krb5_realm;
627c478bdstevel@tonic-gateextern	void krb5_profile_get_options(char *, char *,
637c478bdstevel@tonic-gate		profile_options_boolean*);
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate#include <k5-int.h>
667c478bdstevel@tonic-gate#include <profile/prof_int.h>
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gateprofile_options_boolean config_file_options[] = {
697c478bdstevel@tonic-gate	{ "forwardable", &forwardable_flag, 0},
707c478bdstevel@tonic-gate	{ "forward", &forward_flag, 0},
717c478bdstevel@tonic-gate	{ "encrypt", &encrypt_flag, 0 },
727c478bdstevel@tonic-gate	{ "autologin", &autologin, 0 },
737c478bdstevel@tonic-gate	{ NULL, NULL, 0}
747c478bdstevel@tonic-gate};
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate#include <netinet/ip.h>
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate/*
797c478bdstevel@tonic-gate * Number of maximum IPv4 gateways user can specify. This number is limited by
807c478bdstevel@tonic-gate * the maximum size of the IPv4 options in the IPv4 header.
817c478bdstevel@tonic-gate */
827c478bdstevel@tonic-gate#define	MAX_GATEWAY	8
837c478bdstevel@tonic-gate/*
847c478bdstevel@tonic-gate * Number of maximum IPv6 gateways user can specify. This number is limited by
857c478bdstevel@tonic-gate * the maximum header extension length of the IPv6 routing header.
867c478bdstevel@tonic-gate */
877c478bdstevel@tonic-gate#define	MAX_GATEWAY6	127
887c478bdstevel@tonic-gate#define	MAXMAX_GATEWAY	MAX(MAX_GATEWAY, MAX_GATEWAY6)
897c478bdstevel@tonic-gate
907c478bdstevel@tonic-gate/*
917c478bdstevel@tonic-gate * Depending on the address resolutions of the target and gateways,
927c478bdstevel@tonic-gate * we determine which addresses of the target we'll try connecting to.
937c478bdstevel@tonic-gate */
947c478bdstevel@tonic-gate#define	ALL_ADDRS	0	/* try all addrs of target */
957c478bdstevel@tonic-gate#define	ONLY_V4		1	/* try only IPv4 addrs of target */
967c478bdstevel@tonic-gate#define	ONLY_V6		2	/* try only IPv6 addrs of target */
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate#if defined(USE_TOS)
997c478bdstevel@tonic-gateint tos = -1;
1007c478bdstevel@tonic-gate#endif
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gatechar	*hostname;
1037c478bdstevel@tonic-gatestatic char _hostname[MAXHOSTNAMELEN];
1047c478bdstevel@tonic-gate
1057c478bdstevel@tonic-gatestatic int send_tncmd(void (*func)(), char *, char *);
1067c478bdstevel@tonic-gatestatic void call(int n_ptrs, ...);
1077c478bdstevel@tonic-gatestatic int cmdrc(char *, char *);
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gatetypedef struct {
1107c478bdstevel@tonic-gate	char	*name;		/* command name */
1117c478bdstevel@tonic-gate	char	*help;		/* help string (NULL for no help) */
1127c478bdstevel@tonic-gate	int	(*handler)();	/* routine which executes command */
1137c478bdstevel@tonic-gate	int	needconnect;	/* Do we need to be connected to execute? */
1147c478bdstevel@tonic-gate} Command;
1157c478bdstevel@tonic-gate
1167c478bdstevel@tonic-gate/*
1177c478bdstevel@tonic-gate * storage for IPv6 and/or IPv4 addresses of gateways
1187c478bdstevel@tonic-gate */
1197c478bdstevel@tonic-gatestruct gateway {
1207c478bdstevel@tonic-gate	struct in6_addr	gw_addr6;
1217c478bdstevel@tonic-gate	struct in_addr	gw_addr;
1227c478bdstevel@tonic-gate};
1237c478bdstevel@tonic-gate
1247c478bdstevel@tonic-gate/*
1257c478bdstevel@tonic-gate * IPv4 source routing option.
1267c478bdstevel@tonic-gate * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs
1277c478bdstevel@tonic-gate * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr.
1287c478bdstevel@tonic-gate * If it were defined as "struct in_addr ipsr_addrs[1]", "ipsr_ptr" would be
1297c478bdstevel@tonic-gate * followed by one byte of padding to avoid misaligned struct in_addr.
1307c478bdstevel@tonic-gate */
1317c478bdstevel@tonic-gatestruct ip_sourceroute {
1327c478bdstevel@tonic-gate	uint8_t ipsr_code;
1337c478bdstevel@tonic-gate	uint8_t ipsr_len;
1347c478bdstevel@tonic-gate	uint8_t ipsr_ptr;
1357c478bdstevel@tonic-gate	/* up to 9 IPv4 addresses */
1367c478bdstevel@tonic-gate	uint8_t ipsr_addrs[1][sizeof (struct in_addr)];
1377c478bdstevel@tonic-gate};
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gatestatic char *line = NULL;
1407c478bdstevel@tonic-gatestatic unsigned linesize = 0;
1417c478bdstevel@tonic-gatestatic int margc;
1427c478bdstevel@tonic-gatestatic char **margv = NULL;
1437c478bdstevel@tonic-gatestatic unsigned margvlen = 0;
1447c478bdstevel@tonic-gatestatic int doing_rc = 0;   /* .telnetrc file is being read and processed */
1457c478bdstevel@tonic-gate
1467c478bdstevel@tonic-gatestatic void
1477c478bdstevel@tonic-gateClose(int *fd)
1487c478bdstevel@tonic-gate{
1497c478bdstevel@tonic-gate	if (*fd != -1) {
1507c478bdstevel@tonic-gate		(void) close(*fd);
1517c478bdstevel@tonic-gate		*fd = -1;
1527c478bdstevel@tonic-gate	}
1537c478bdstevel@tonic-gate}
1547c478bdstevel@tonic-gate
1557c478bdstevel@tonic-gatestatic void
1567c478bdstevel@tonic-gateFree(char **p)
1577c478bdstevel@tonic-gate{
1587c478bdstevel@tonic-gate	if (*p != NULL) {
1597c478bdstevel@tonic-gate		free(*p);
1607c478bdstevel@tonic-gate		*p = NULL;
1617c478bdstevel@tonic-gate	}
1627c478bdstevel@tonic-gate}
1637c478bdstevel@tonic-gate
1647c478bdstevel@tonic-gatestatic void
1657c478bdstevel@tonic-gateFreeHostnameList(char *list[])
1667c478bdstevel@tonic-gate{
1677c478bdstevel@tonic-gate	unsigned i;
1687c478bdstevel@tonic-gate	for (i = 0; i <= MAXMAX_GATEWAY && list[i] != NULL; i++)
1697c478bdstevel@tonic-gate		Free(&list[i]);
1707c478bdstevel@tonic-gate}
1717c478bdstevel@tonic-gate
1727c478bdstevel@tonic-gate#define	MARGV_CHUNK_SIZE 8
1737c478bdstevel@tonic-gate
1747c478bdstevel@tonic-gatestatic void
1757c478bdstevel@tonic-gateset_argv(str)
1767c478bdstevel@tonic-gatechar *str;
1777c478bdstevel@tonic-gate{
1787c478bdstevel@tonic-gate	if (margc == margvlen) {
1797c478bdstevel@tonic-gate		char **newmargv;
1807c478bdstevel@tonic-gate
1817c478bdstevel@tonic-gate		margvlen += MARGV_CHUNK_SIZE;
1827c478bdstevel@tonic-gate
1837c478bdstevel@tonic-gate		if ((newmargv = realloc(margv, margvlen * sizeof (char *)))
1847c478bdstevel@tonic-gate			== NULL)
1857c478bdstevel@tonic-gate			ExitString("telnet: no space for arguments",
1867c478bdstevel@tonic-gate				EXIT_FAILURE);
1877c478bdstevel@tonic-gate
1887c478bdstevel@tonic-gate		margv = newmargv;
1897c478bdstevel@tonic-gate	}
1907c478bdstevel@tonic-gate
1917c478bdstevel@tonic-gate	margv[margc] = str;
1927c478bdstevel@tonic-gate	if (str != NULL)
1937c478bdstevel@tonic-gate		margc++;
1947c478bdstevel@tonic-gate}
1957c478bdstevel@tonic-gate
1967c478bdstevel@tonic-gatestatic void
1977c478bdstevel@tonic-gatemakeargv()
1987c478bdstevel@tonic-gate{
1997c478bdstevel@tonic-gate	char *cp, *cp2, c;
2007c478bdstevel@tonic-gate	boolean_t shellcmd = B_FALSE;
2017c478bdstevel@tonic-gate
2027c478bdstevel@tonic-gate	margc = 0;
2037c478bdstevel@tonic-gate	cp = line;
2047c478bdstevel@tonic-gate	if (*cp == '!') {		/* Special case shell escape */
2057c478bdstevel@tonic-gate		set_argv("!");		/* No room in string to get this */
2067c478bdstevel@tonic-gate		cp++;
2077c478bdstevel@tonic-gate		shellcmd = B_TRUE;
2087c478bdstevel@tonic-gate	}
2097c478bdstevel@tonic-gate	while ((c = *cp) != '\0') {
2107c478bdstevel@tonic-gate		register int inquote = 0;
2117c478bdstevel@tonic-gate		while (isspace(c))
2127c478bdstevel@tonic-gate			c = *++cp;
2137c478bdstevel@tonic-gate		if (c == '\0')
2147c478bdstevel@tonic-gate			break;
2157c478bdstevel@tonic-gate		set_argv(cp);
2167c478bdstevel@tonic-gate		/*
2177c478bdstevel@tonic-gate		 * For the shell escape, put the rest of the line, less
2187c478bdstevel@tonic-gate		 * leading space, into a single argument, breaking out from
2197c478bdstevel@tonic-gate		 * the loop to prevent the rest of the line being split up
2207c478bdstevel@tonic-gate		 * into smaller arguments.
2217c478bdstevel@tonic-gate		 */
2227c478bdstevel@tonic-gate		if (shellcmd)
2237c478bdstevel@tonic-gate			break;
2247c478bdstevel@tonic-gate		for (cp2 = cp; c != '\0'; c = *++cp) {
2257c478bdstevel@tonic-gate			if (inquote) {
2267c478bdstevel@tonic-gate				if (c == inquote) {
2277c478bdstevel@tonic-gate					inquote = 0;
2287c478bdstevel@tonic-gate					continue;
2297c478bdstevel@tonic-gate				}
2307c478bdstevel@tonic-gate			} else {
2317c478bdstevel@tonic-gate				if (c == '\\') {
2327c478bdstevel@tonic-gate					if ((c = *++cp) == '\0')
2337c478bdstevel@tonic-gate						break;
2347c478bdstevel@tonic-gate				} else if (c == '"') {
2357c478bdstevel@tonic-gate					inquote = '"';
2367c478bdstevel@tonic-gate					continue;
2377c478bdstevel@tonic-gate				} else if (c == '\'') {
2387c478bdstevel@tonic-gate					inquote = '\'';
2397c478bdstevel@tonic-gate					continue;
2407c478bdstevel@tonic-gate				} else if (isspace(c))
2417c478bdstevel@tonic-gate					break;
2427c478bdstevel@tonic-gate			}
2437c478bdstevel@tonic-gate			*cp2++ = c;
2447c478bdstevel@tonic-gate		}
2457c478bdstevel@tonic-gate		*cp2 = '\0';
2467c478bdstevel@tonic-gate		if (c == '\0')
2477c478bdstevel@tonic-gate			break;
2487c478bdstevel@tonic-gate		cp++;
2497c478bdstevel@tonic-gate	}
2507c478bdstevel@tonic-gate	set_argv((char *)NULL);
2517c478bdstevel@tonic-gate}
2527c478bdstevel@tonic-gate
2537c478bdstevel@tonic-gate/*
2547c478bdstevel@tonic-gate * Make a character string into a number.
2557c478bdstevel@tonic-gate *
2567c478bdstevel@tonic-gate * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1).
2577c478bdstevel@tonic-gate */
2587c478bdstevel@tonic-gate
259740638cbw	static int
2607c478bdstevel@tonic-gatespecial(s)
2617c478bdstevel@tonic-gate	register char *s;
2627c478bdstevel@tonic-gate{
2637c478bdstevel@tonic-gate	register char c;
2647c478bdstevel@tonic-gate	char b;
2657c478bdstevel@tonic-gate
2667c478bdstevel@tonic-gate	switch (*s) {
2677c478bdstevel@tonic-gate	case '^':
2687c478bdstevel@tonic-gate		b = *++s;
2697c478bdstevel@tonic-gate		if (b == '?') {
2707c478bdstevel@tonic-gate		    c = b | 0x40;		/* DEL */
2717c478bdstevel@tonic-gate		} else {
2727c478bdstevel@tonic-gate		    c = b & 0x1f;
2737c478bdstevel@tonic-gate		}
2747c478bdstevel@tonic-gate		break;
2757c478bdstevel@tonic-gate	default:
2767c478bdstevel@tonic-gate		c = *s;
2777c478bdstevel@tonic-gate		break;
2787c478bdstevel@tonic-gate	}
2797c478bdstevel@tonic-gate	return (c);
2807c478bdstevel@tonic-gate}
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate/*
2837c478bdstevel@tonic-gate * Construct a control character sequence
2847c478bdstevel@tonic-gate * for a special character.
2857c478bdstevel@tonic-gate */
2867c478bdstevel@tonic-gate	static char *
2877c478bdstevel@tonic-gatecontrol(c)
2887c478bdstevel@tonic-gate	register cc_t c;
2897c478bdstevel@tonic-gate{
2907c478bdstevel@tonic-gate	static char buf[5];
2917c478bdstevel@tonic-gate	/*
2927c478bdstevel@tonic-gate	 * The only way I could get the Sun 3.5 compiler
2937c478bdstevel@tonic-gate	 * to shut up about
2947c478bdstevel@tonic-gate	 *	if ((unsigned int)c >= 0x80)
2957c478bdstevel@tonic-gate	 * was to assign "c" to an unsigned int variable...
2967c478bdstevel@tonic-gate	 * Arggg....
2977c478bdstevel@tonic-gate	 */
2987c478bdstevel@tonic-gate	register unsigned int uic = (unsigned int)c;
2997c478bdstevel@tonic-gate
3007c478bdstevel@tonic-gate	if (uic == 0x7f)
3017c478bdstevel@tonic-gate		return ("^?");
3027c478bdstevel@tonic-gate	if (c == (cc_t)_POSIX_VDISABLE) {
3037c478bdstevel@tonic-gate		return ("off");
3047c478bdstevel@tonic-gate	}
3057c478bdstevel@tonic-gate	if (uic >= 0x80) {
3067c478bdstevel@tonic-gate		buf[0] = '\\';
3077c478bdstevel@tonic-gate		buf[1] = ((c>>6)&07) + '0';
3087c478bdstevel@tonic-gate		buf[2] = ((c>>3)&07) + '0';
3097c478bdstevel@tonic-gate		buf[3] = (c&07) + '0';
3107c478bdstevel@tonic-gate		buf[4] = 0;
3117c478bdstevel@tonic-gate	} else if (uic >= 0x20) {
3127c478bdstevel@tonic-gate		buf[0] = c;
3137c478bdstevel@tonic-gate		buf[1] = 0;
3147c478bdstevel@tonic-gate	} else {
3157c478bdstevel@tonic-gate		buf[0] = '^';
3167c478bdstevel@tonic-gate		buf[1] = '@'+c;
3177c478bdstevel@tonic-gate		buf[2] = 0;
3187c478bdstevel@tonic-gate	}
3197c478bdstevel@tonic-gate	return (buf);
3207c478bdstevel@tonic-gate}
3217c478bdstevel@tonic-gate
3227c478bdstevel@tonic-gate/*
3237c478bdstevel@tonic-gate * Same as control() except that its only used for escape handling, which uses
3247c478bdstevel@tonic-gate * _POSIX_VDISABLE differently and is aided by the use of the state variable
3257c478bdstevel@tonic-gate * escape_valid.
3267c478bdstevel@tonic-gate */
3277c478bdstevel@tonic-gate	static char *
3287c478bdstevel@tonic-gateesc_control(c)
3297c478bdstevel@tonic-gate	register cc_t c;
3307c478bdstevel@tonic-gate{
3317c478bdstevel@tonic-gate	static char buf[5];
3327c478bdstevel@tonic-gate	/*
3337c478bdstevel@tonic-gate	 * The only way I could get the Sun 3.5 compiler
3347c478bdstevel@tonic-gate	 * to shut up about
3357c478bdstevel@tonic-gate	 *	if ((unsigned int)c >= 0x80)
3367c478bdstevel@tonic-gate	 * was to assign "c" to an unsigned int variable...
3377c478bdstevel@tonic-gate	 * Arggg....
3387c478bdstevel@tonic-gate	 */
3397c478bdstevel@tonic-gate	register unsigned int uic = (unsigned int)c;
3407c478bdstevel@tonic-gate
3417c478bdstevel@tonic-gate	if (escape_valid == B_FALSE)
3427c478bdstevel@tonic-gate		return ("off");
3437c478bdstevel@tonic-gate	if (uic == 0x7f)
3447c478bdstevel@tonic-gate		return ("^?");
3457c478bdstevel@tonic-gate	if (uic >= 0x80) {
3467c478bdstevel@tonic-gate		buf[0] = '\\';
3477c478bdstevel@tonic-gate		buf[1] = ((c>>6)&07) + '0';
3487c478bdstevel@tonic-gate		buf[2] = ((c>>3)&07) + '0';
3497c478bdstevel@tonic-gate		buf[3] = (c&07) + '0';
3507c478bdstevel@tonic-gate		buf[4] = 0;
3517c478bdstevel@tonic-gate	} else if (uic >= 0x20) {
3527c478bdstevel@tonic-gate		buf[0] = c;
3537c478bdstevel@tonic-gate		buf[1] = 0;
3547c478bdstevel@tonic-gate	} else {
3557c478bdstevel@tonic-gate		buf[0] = '^';
3567c478bdstevel@tonic-gate		buf[1] = '@'+c;
3577c478bdstevel@tonic-gate		buf[2] = 0;
3587c478bdstevel@tonic-gate	}
3597c478bdstevel@tonic-gate	return (buf);
3607c478bdstevel@tonic-gate}
3617c478bdstevel@tonic-gate
3627c478bdstevel@tonic-gate/*
3637c478bdstevel@tonic-gate *	The following are data structures and routines for
3647c478bdstevel@tonic-gate *	the "send" command.
3657c478bdstevel@tonic-gate *
3667c478bdstevel@tonic-gate */
3677c478bdstevel@tonic-gate
3687c478bdstevel@tonic-gatestruct sendlist {
3697c478bdstevel@tonic-gate	char	*name;		/* How user refers to it (case independent) */
3707c478bdstevel@tonic-gate	char	*help;		/* Help information (0 ==> no help) */
3717c478bdstevel@tonic-gate	int	needconnect;	/* Need to be connected */
3727c478bdstevel@tonic-gate	int	narg;		/* Number of arguments */
3737c478bdstevel@tonic-gate	int	(*handler)();	/* Routine to perform (for special ops) */
3747c478bdstevel@tonic-gate	int	nbyte;		/* Number of bytes to send this command */
3757c478bdstevel@tonic-gate	int	what;		/* Character to be sent (<0 ==> special) */
3767c478bdstevel@tonic-gate};
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate
3797c478bdstevel@tonic-gatestatic int send_esc(void);
3807c478bdstevel@tonic-gatestatic int send_help(void);
3817c478bdstevel@tonic-gatestatic int send_docmd(char *);
3827c478bdstevel@tonic-gatestatic int send_dontcmd(char *);
3837c478bdstevel@tonic-gatestatic int send_willcmd(char *);
3847c478bdstevel@tonic-gatestatic int send_wontcmd(char *);
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gatestatic struct sendlist Sendlist[] = {
3877c478bdstevel@tonic-gate	{ "ao",	"Send Telnet Abort output",		1, 0, 0, 2, AO },
3887c478bdstevel@tonic-gate	{ "ayt", "Send Telnet 'Are You There'",		1, 0, 0, 2, AYT },
3897c478bdstevel@tonic-gate	{ "b", 0,					1, 0, 0, 2, BREAK },
3907c478bdstevel@tonic-gate	{ "br", 0,					1, 0, 0, 2, BREAK },
3917c478bdstevel@tonic-gate	{ "break", 0,					1, 0, 0, 2, BREAK },
3927c478bdstevel@tonic-gate	{ "brk", "Send Telnet Break",			1, 0, 0, 2, BREAK },
3937c478bdstevel@tonic-gate	{ "ec",	"Send Telnet Erase Character",		1, 0, 0, 2, EC },
3947c478bdstevel@tonic-gate	{ "el",	"Send Telnet Erase Line",		1, 0, 0, 2, EL },
3957c478bdstevel@tonic-gate	{ "escape", "Send current escape character",	1, 0, send_esc, 1, 0 },
3967c478bdstevel@tonic-gate	{ "ga",	"Send Telnet 'Go Ahead' sequence",	1, 0, 0, 2, GA },
3977c478bdstevel@tonic-gate	{ "ip",	"Send Telnet Interrupt Process",	1, 0, 0, 2, IP },
3987c478bdstevel@tonic-gate	{ "intp", 0,					1, 0, 0, 2, IP },
3997c478bdstevel@tonic-gate	{ "interrupt", 0,				1, 0, 0, 2, IP },
4007c478bdstevel@tonic-gate	{ "intr",	0,				1, 0, 0, 2, IP },
4017c478bdstevel@tonic-gate	{ "nop", "Send Telnet 'No operation'",		1, 0, 0, 2, NOP },
4027c478bdstevel@tonic-gate	{ "eor", "Send Telnet 'End of Record'",		1, 0, 0, 2, EOR },
4037c478bdstevel@tonic-gate	{ "abort", "Send Telnet 'Abort Process'",	1, 0, 0, 2, ABORT },
4047c478bdstevel@tonic-gate	{ "susp", "Send Telnet 'Suspend Process'",	1, 0, 0, 2, SUSP },
4057c478bdstevel@tonic-gate	{ "eof", "Send Telnet End of File Character",	1, 0, 0, 2, xEOF },
4067c478bdstevel@tonic-gate	{ "synch", "Perform Telnet 'Synch operation'",	1, 0, dosynch, 2, 0 },
4077c478bdstevel@tonic-gate	{ "getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0 },
4087c478bdstevel@tonic-gate	{ "?",	"Display send options",			0, 0, send_help, 0, 0 },
4097c478bdstevel@tonic-gate	{ "help",	0,				0, 0, send_help, 0, 0 },
4107c478bdstevel@tonic-gate	{ "do",	0,				0, 1, send_docmd, 3, 0 },
4117c478bdstevel@tonic-gate	{ "dont", 0,				0, 1, send_dontcmd, 3, 0 },
4127c478bdstevel@tonic-gate	{ "will", 0,				0, 1, send_willcmd, 3, 0 },
4137c478bdstevel@tonic-gate	{ "wont", 0,				0, 1, send_wontcmd, 3, 0 },
4147c478bdstevel@tonic-gate	{ 0 }
4157c478bdstevel@tonic-gate};
4167c478bdstevel@tonic-gate
4177c478bdstevel@tonic-gate#define	GETSEND(name) ((struct sendlist *)genget(name, (char **)Sendlist, \
4187c478bdstevel@tonic-gate				sizeof (struct sendlist)))
4197c478bdstevel@tonic-gate
4207c478bdstevel@tonic-gatestatic int
4217c478bdstevel@tonic-gatesendcmd(argc, argv)
4227c478bdstevel@tonic-gate	int  argc;
4237c478bdstevel@tonic-gate	char **argv;
4247c478bdstevel@tonic-gate{
4257c478bdstevel@tonic-gate	int count;	/* how many bytes we are going to need to send */
4267c478bdstevel@tonic-gate	int i;
4277c478bdstevel@tonic-gate	struct sendlist *s;	/* pointer to current command */
4287c478bdstevel@tonic-gate	int success = 0;
4297c478bdstevel@tonic-gate	int needconnect = 0;
4307c478bdstevel@tonic-gate
4317c478bdstevel@tonic-gate	if (argc < 2) {
4327c478bdstevel@tonic-gate		(void) printf(
4337c478bdstevel@tonic-gate		    "need at least one argument for 'send' command\n");
4347c478bdstevel@tonic-gate		(void) printf("'send ?' for help\n");
4357c478bdstevel@tonic-gate		return (0);
4367c478bdstevel@tonic-gate	}
4377c478bdstevel@tonic-gate	/*
4387c478bdstevel@tonic-gate	 * First, validate all the send arguments.
4397c478bdstevel@tonic-gate	 * In addition, we see how much space we are going to need, and
4407c478bdstevel@tonic-gate	 * whether or not we will be doing a "SYNCH" operation (which
4417c478bdstevel@tonic-gate	 * flushes the network queue).
4427c478bdstevel@tonic-gate	 */
4437c478bdstevel@tonic-gate	count = 0;
4447c478bdstevel@tonic-gate	for (i = 1; i < argc; i++) {
4457c478bdstevel@tonic-gate		s = GETSEND(argv[i]);
4467c478bdstevel@tonic-gate		if (s == 0) {
4477c478bdstevel@tonic-gate			(void) printf("Unknown send argument '%s'\n'send ?' "
4487c478bdstevel@tonic-gate			    "for help.\n", argv[i]);
4497c478bdstevel@tonic-gate			return (0);
4507c478bdstevel@tonic-gate		} else if (Ambiguous(s)) {
4517c478bdstevel@tonic-gate			(void) printf("Ambiguous send argument '%s'\n'send ?' "
4527c478bdstevel@tonic-gate			    "for help.\n", argv[i]);
4537c478bdstevel@tonic-gate			return (0);
4547c478bdstevel@tonic-gate		}
4557c478bdstevel@tonic-gate		if (i + s->narg >= argc) {
4567c478bdstevel@tonic-gate			(void) fprintf(stderr,
4577c478bdstevel@tonic-gate			    "Need %d argument%s to 'send %s' "
4587c478bdstevel@tonic-gate			    "command.  'send %s ?' for help.\n",
4597c478bdstevel@tonic-gate			    s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
4607c478bdstevel@tonic-gate			return (0);
4617c478bdstevel@tonic-gate		}
4627c478bdstevel@tonic-gate		count += s->nbyte;
4637c478bdstevel@tonic-gate		if (s->handler == send_help) {
4647c478bdstevel@tonic-gate			(void) send_help();
4657c478bdstevel@tonic-gate			return (0);
4667c478bdstevel@tonic-gate		}
4677c478bdstevel@tonic-gate
4687c478bdstevel@tonic-gate		i += s->narg;
4697c478bdstevel@tonic-gate		needconnect += s->needconnect;
4707c478bdstevel@tonic-gate	}
4717c478bdstevel@tonic-gate	if (!connected && needconnect) {
4727c478bdstevel@tonic-gate		(void) printf("?Need to be connected first.\n");
4737c478bdstevel@tonic-gate		(void) printf("'send ?' for help\n");
4747c478bdstevel@tonic-gate		return (0);
4757c478bdstevel@tonic-gate	}
4767c478bdstevel@tonic-gate	/* Now, do we have enough room? */
4777c478bdstevel@tonic-gate	if (NETROOM() < count) {
4787c478bdstevel@tonic-gate		(void) printf("There is not enough room in the buffer "
4797c478bdstevel@tonic-gate		    "TO the network\n");
4807c478bdstevel@tonic-gate		(void) printf(
4817c478bdstevel@tonic-gate		    "to process your request.  Nothing will be done.\n");
4827c478bdstevel@tonic-gate		(void) printf("('send synch' will throw away most "
4837c478bdstevel@tonic-gate		    "data in the network\n");
4847c478bdstevel@tonic-gate		(void) printf("buffer, if this might help.)\n");
4857c478bdstevel@tonic-gate		return (0);
4867c478bdstevel@tonic-gate	}
4877c478bdstevel@tonic-gate	/* OK, they are all OK, now go through again and actually send */
4887c478bdstevel@tonic-gate	count = 0;
4897c478bdstevel@tonic-gate	for (i = 1; i < argc; i++) {
4907c478bdstevel@tonic-gate		if ((s = GETSEND(argv[i])) == 0) {
4917c478bdstevel@tonic-gate			(void) fprintf(stderr,
4927c478bdstevel@tonic-gate			    "Telnet 'send' error - argument disappeared!\n");
4937c478bdstevel@tonic-gate			(void) quit();
4947c478bdstevel@tonic-gate			/*NOTREACHED*/
4957c478bdstevel@tonic-gate		}
4967c478bdstevel@tonic-gate		if (s->handler) {
4977c478bdstevel@tonic-gate			count++;
4987c478bdstevel@tonic-gate			success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
4997c478bdstevel@tonic-gate			    (s->narg > 1) ? argv[i+2] : 0);
5007c478bdstevel@tonic-gate			i += s->narg;
5017c478bdstevel@tonic-gate		} else {
5027c478bdstevel@tonic-gate			NET2ADD(IAC, s->what);
5037c478bdstevel@tonic-gate			printoption("SENT", IAC, s->what);
5047c478bdstevel@tonic-gate		}
5057c478bdstevel@tonic-gate	}
5067c478bdstevel@tonic-gate	return (count == success);
5077c478bdstevel@tonic-gate}
5087c478bdstevel@tonic-gate
5097c478bdstevel@tonic-gatestatic int
5107c478bdstevel@tonic-gatesend_esc()
5117c478bdstevel@tonic-gate{
5127c478bdstevel@tonic-gate	NETADD(escape);
5137c478bdstevel@tonic-gate	return (1);
5147c478bdstevel@tonic-gate}
5157c478bdstevel@tonic-gate
5167c478bdstevel@tonic-gatestatic int
5177c478bdstevel@tonic-gatesend_docmd(name)
5187c478bdstevel@tonic-gate	char *name;
5197c478bdstevel@tonic-gate{
5207c478bdstevel@tonic-gate	return (send_tncmd(send_do, "do", name));
5217c478bdstevel@tonic-gate}
5227c478bdstevel@tonic-gate
5237c478bdstevel@tonic-gatestatic int
5247c478bdstevel@tonic-gatesend_dontcmd(name)
5257c478bdstevel@tonic-gate	char *name;
5267c478bdstevel@tonic-gate{
5277c478bdstevel@tonic-gate	return (send_tncmd(send_dont, "dont", name));
5287c478bdstevel@tonic-gate}
5297c478bdstevel@tonic-gate
5307c478bdstevel@tonic-gatestatic int
5317c478bdstevel@tonic-gatesend_willcmd(name)
5327c478bdstevel@tonic-gate	char *name;
5337c478bdstevel@tonic-gate{
5347c478bdstevel@tonic-gate	return (send_tncmd(send_will, "will", name));
5357c478bdstevel@tonic-gate}
5367c478bdstevel@tonic-gate
5377c478bdstevel@tonic-gatestatic int
5387c478bdstevel@tonic-gatesend_wontcmd(name)
5397c478bdstevel@tonic-gate	char *name;
5407c478bdstevel@tonic-gate{
5417c478bdstevel@tonic-gate	return (send_tncmd(send_wont, "wont", name));
5427c478bdstevel@tonic-gate}
5437c478bdstevel@tonic-gate
5447c478bdstevel@tonic-gateint
5457c478bdstevel@tonic-gatesend_tncmd(func, cmd, name)
5467c478bdstevel@tonic-gate	void	(*func)();
5477c478bdstevel@tonic-gate	char	*cmd, *name;
5487c478bdstevel@tonic-gate{
5497c478bdstevel@tonic-gate	char **cpp;
5507c478bdstevel@tonic-gate	extern char *telopts[];
5517c478bdstevel@tonic-gate	register int val = 0;
5527c478bdstevel@tonic-gate
5537c478bdstevel@tonic-gate	if (isprefix(name, "help") || isprefix(name, "?")) {
5547c478bdstevel@tonic-gate		register int col, len;
5557c478bdstevel@tonic-gate
5567c478bdstevel@tonic-gate		(void) printf("Usage: send %s <value|option>\n", cmd);
5577c478bdstevel@tonic-gate		(void) printf("\"value\" must be from 0 to 255\n");
5587c478bdstevel@tonic-gate		(void) printf("Valid options are:\n\t");
5597c478bdstevel@tonic-gate
5607c478bdstevel@tonic-gate		col = 8;
5617c478bdstevel@tonic-gate		for (cpp = telopts; *cpp; cpp++) {
5627c478bdstevel@tonic-gate			len = strlen(*cpp) + 3;
5637c478bdstevel@tonic-gate			if (col + len > 65) {
5647c478bdstevel@tonic-gate				(void) printf("\n\t");
5657c478bdstevel@tonic-gate				col = 8;
5667c478bdstevel@tonic-gate			}
5677c478bdstevel@tonic-gate			(void) printf(" \"%s\"", *cpp);
5687c478bdstevel@tonic-gate			col += len;
5697c478bdstevel@tonic-gate		}
5707c478bdstevel@tonic-gate		(void) printf("\n");
5717c478bdstevel@tonic-gate		return (0);
5727c478bdstevel@tonic-gate	}
5737c478bdstevel@tonic-gate	cpp = (char **)genget(name, telopts, sizeof (char *));
5747c478bdstevel@tonic-gate	if (Ambiguous(cpp)) {
5757c478bdstevel@tonic-gate		(void) fprintf(stderr,
5767c478bdstevel@tonic-gate		    "'%s': ambiguous argument ('send %s ?' for help).\n",
5777c478bdstevel@tonic-gate		    name, cmd);
5787c478bdstevel@tonic-gate		return (0);
5797c478bdstevel@tonic-gate	}
5807c478bdstevel@tonic-gate	if (cpp) {
5817c478bdstevel@tonic-gate		val = cpp - telopts;
5827c478bdstevel@tonic-gate	} else {
5837c478bdstevel@tonic-gate		register char *cp = name;
5847c478bdstevel@tonic-gate
5857c478bdstevel@tonic-gate		while (*cp >= '0' && *cp <= '9') {
5867c478bdstevel@tonic-gate			val *= 10;
5877c478bdstevel@tonic-gate			val += *cp - '0';
5887c478bdstevel@tonic-gate			cp++;
5897c478bdstevel@tonic-gate		}
5907c478bdstevel@tonic-gate		if (*cp != 0) {
5917c478bdstevel@tonic-gate			(void) fprintf(stderr,
5927c478bdstevel@tonic-gate			    "'%s': unknown argument ('send %s ?' for help).\n",
5937c478bdstevel@tonic-gate			    name, cmd);
5947c478bdstevel@tonic-gate			return (0);
5957c478bdstevel@tonic-gate		} else if (val < 0 || val > 255) {
5967c478bdstevel@tonic-gate			(void) fprintf(stderr,
5977c478bdstevel@tonic-gate			    "'%s': bad value ('send %s ?' for help).\n",
5987c478bdstevel@tonic-gate			    name, cmd);
5997c478bdstevel@tonic-gate			return (0);
6007c478bdstevel@tonic-gate		}
6017c478bdstevel@tonic-gate	}
6027c478bdstevel@tonic-gate	if (!connected) {
6037c478bdstevel@tonic-gate		(void) printf("?Need to be connected first.\n");
6047c478bdstevel@tonic-gate		return (0);
6057c478bdstevel@tonic-gate	}
6067c478bdstevel@tonic-gate	(*func)(val, 1);
6077c478bdstevel@tonic-gate	return (1);
6087c478bdstevel@tonic-gate}
6097c478bdstevel@tonic-gate
6107c478bdstevel@tonic-gatestatic int
6117c478bdstevel@tonic-gatesend_help()
6127c478bdstevel@tonic-gate{
6137c478bdstevel@tonic-gate	struct sendlist *s;	/* pointer to current command */
6147c478bdstevel@tonic-gate	for (s = Sendlist; s->name; s++) {
6157c478bdstevel@tonic-gate		if (s->help)
6167c478bdstevel@tonic-gate			(void) printf("%-15s %s\n", s->name, s->help);
6177c478bdstevel@tonic-gate	}
6187c478bdstevel@tonic-gate	return (0);
6197c478bdstevel@tonic-gate}
6207c478bdstevel@tonic-gate
6217c478bdstevel@tonic-gate/*
6227c478bdstevel@tonic-gate * The following are the routines and data structures referred
6237c478bdstevel@tonic-gate * to by the arguments to the "toggle" command.
6247c478bdstevel@tonic-gate */
6257c478bdstevel@tonic-gate
6267c478bdstevel@tonic-gatestatic int
6277c478bdstevel@tonic-gatelclchars()
6287c478bdstevel@tonic-gate{
6297c478bdstevel@tonic-gate	donelclchars = 1;
6307c478bdstevel@tonic-gate	return (1);
6317c478bdstevel@tonic-gate}
6327c478bdstevel@tonic-gate
6337c478bdstevel@tonic-gatestatic int
6347c478bdstevel@tonic-gatetogdebug()
6357c478bdstevel@tonic-gate{
6367c478bdstevel@tonic-gate	if (net > 0 &&
6377c478bdstevel@tonic-gate	    (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
6387c478bdstevel@tonic-gate		perror("setsockopt (SO_DEBUG)");
6397c478bdstevel@tonic-gate	}
6407c478bdstevel@tonic-gate	return (1);
6417c478bdstevel@tonic-gate}
6427c478bdstevel@tonic-gate
6437c478bdstevel@tonic-gate
6447c478bdstevel@tonic-gatestatic int
6457c478bdstevel@tonic-gatetogcrlf()
6467c478bdstevel@tonic-gate{
6477c478bdstevel@tonic-gate	if (crlf) {
6487c478bdstevel@tonic-gate		(void) printf(
6497c478bdstevel@tonic-gate		    "Will send carriage returns as telnet <CR><LF>.\n");
6507c478bdstevel@tonic-gate	} else {
6517c478bdstevel@tonic-gate		(void) printf(
6527c478bdstevel@tonic-gate		    "Will send carriage returns as telnet <CR><NUL>.\n");
6537c478bdstevel@tonic-gate	}
6547c478bdstevel@tonic-gate	return (1);
6557c478bdstevel@tonic-gate}
6567c478bdstevel@tonic-gate
6577c478bdstevel@tonic-gatestatic int binmode;
6587c478bdstevel@tonic-gate
6597c478bdstevel@tonic-gatestatic int
6607c478bdstevel@tonic-gatetogbinary(val)
6617c478bdstevel@tonic-gate	int val;
6627c478bdstevel@tonic-gate{
6637c478bdstevel@tonic-gate	donebinarytoggle = 1;
6647c478bdstevel@tonic-gate
6657c478bdstevel@tonic-gate	if (val >= 0) {
6667c478bdstevel@tonic-gate		binmode = val;
6677c478bdstevel@tonic-gate	} else {
6687c478bdstevel@tonic-gate		if (my_want_state_is_will(TELOPT_BINARY) &&
6697c478bdstevel@tonic-gate		    my_want_state_is_do(TELOPT_BINARY)) {
6707c478bdstevel@tonic-gate			binmode = 1;
6717c478bdstevel@tonic-gate		} else if (my_want_state_is_wont(TELOPT_BINARY) &&
6727c478bdstevel@tonic-gate		    my_want_state_is_dont(TELOPT_BINARY)) {
6737c478bdstevel@tonic-gate			binmode = 0;
6747c478bdstevel@tonic-gate		}
6757c478bdstevel@tonic-gate		val = binmode ? 0 : 1;
6767c478bdstevel@tonic-gate	}
6777c478bdstevel@tonic-gate
6787c478bdstevel@tonic-gate	if (val == 1) {
6797c478bdstevel@tonic-gate		if (my_want_state_is_will(TELOPT_BINARY) &&
6807c478bdstevel@tonic-gate		    my_want_state_is_do(TELOPT_BINARY)) {
6817c478bdstevel@tonic-gate			(void) printf("Already operating in binary mode "
6827c478bdstevel@tonic-gate			    "with remote host.\n");
6837c478bdstevel@tonic-gate		} else {
6847c478bdstevel@tonic-gate			(void) printf(
6857c478bdstevel@tonic-gate			    "Negotiating binary mode with remote host.\n");
6867c478bdstevel@tonic-gate			tel_enter_binary(3);
6877c478bdstevel@tonic-gate		}
6887c478bdstevel@tonic-gate	} else {
6897c478bdstevel@tonic-gate		if (my_want_state_is_wont(TELOPT_BINARY) &&
6907c478bdstevel@tonic-gate		    my_want_state_is_dont(TELOPT_BINARY)) {
6917c478bdstevel@tonic-gate			(void) printf("Already in network ascii mode "
6927c478bdstevel@tonic-gate			    "with remote host.\n");
6937c478bdstevel@tonic-gate		} else {
6947c478bdstevel@tonic-gate			(void) printf("Negotiating network ascii mode "
6957c478bdstevel@tonic-gate			    "with remote host.\n");
6967c478bdstevel@tonic-gate			tel_leave_binary(3);
6977c478bdstevel@tonic-gate		}
6987c478bdstevel@tonic-gate	}
6997c478bdstevel@tonic-gate	return (1);
7007c478bdstevel@tonic-gate}
7017c478bdstevel@tonic-gate
7027c478bdstevel@tonic-gatestatic int
7037c478bdstevel@tonic-gatetogrbinary(val)
7047c478bdstevel@tonic-gate	int val;
7057c478bdstevel@tonic-gate{
7067c478bdstevel@tonic-gate	donebinarytoggle = 1;
7077c478bdstevel@tonic-gate
7087c478bdstevel@tonic-gate	if (val == -1)
7097c478bdstevel@tonic-gate		val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
7107c478bdstevel@tonic-gate
7117c478bdstevel@tonic-gate	if (val == 1) {
7127c478bdstevel@tonic-gate		if (my_want_state_is_do(TELOPT_BINARY)) {
7137c478bdstevel@tonic-gate			(void) printf("Already receiving in binary mode.\n");
7147c478bdstevel@tonic-gate		} else {
7157c478bdstevel@tonic-gate			(void) printf("Negotiating binary mode on input.\n");
7167c478bdstevel@tonic-gate			tel_enter_binary(1);
7177c478bdstevel@tonic-gate		}
7187c478bdstevel@tonic-gate	} else {
7197c478bdstevel@tonic-gate		if (my_want_state_is_dont(TELOPT_BINARY)) {
7207c478bdstevel@tonic-gate			(void) printf(
7217c478bdstevel@tonic-gate			    "Already receiving in network ascii mode.\n");
7227c478bdstevel@tonic-gate		} else {
7237c478bdstevel@tonic-gate			(void) printf(
7247c478bdstevel@tonic-gate			    "Negotiating network ascii mode on input.\n");
7257c478bdstevel@tonic-gate			    tel_leave_binary(1);
7267c478bdstevel@tonic-gate		}
7277c478bdstevel@tonic-gate	}
7287c478bdstevel@tonic-gate	return (1);
7297c478bdstevel@tonic-gate}
7307c478bdstevel@tonic-gate
7317c478bdstevel@tonic-gatestatic int
7327c478bdstevel@tonic-gatetogxbinary(val)
7337c478bdstevel@tonic-gate	int val;
7347c478bdstevel@tonic-gate{
7357c478bdstevel@tonic-gate	donebinarytoggle = 1;
7367c478bdstevel@tonic-gate
7377c478bdstevel@tonic-gate	if (val == -1)
7387c478bdstevel@tonic-gate		val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
7397c478bdstevel@tonic-gate
7407c478bdstevel@tonic-gate	if (val == 1) {
7417c478bdstevel@tonic-gate		if (my_want_state_is_will(TELOPT_BINARY)) {
7427c478bdstevel@tonic-gate			(void) printf("Already transmitting in binary mode.\n");
7437c478bdstevel@tonic-gate		} else {
7447c478bdstevel@tonic-gate			(void) printf("Negotiating binary mode on output.\n");
7457c478bdstevel@tonic-gate			tel_enter_binary(2);
7467c478bdstevel@tonic-gate		}
7477c478bdstevel@tonic-gate	} else {
7487c478bdstevel@tonic-gate		if (my_want_state_is_wont(TELOPT_BINARY)) {
7497c478bdstevel@tonic-gate			(void) printf(
7507c478bdstevel@tonic-gate			    "Already transmitting in network ascii mode.\n");
7517c478bdstevel@tonic-gate		} else {
7527c478bdstevel@tonic-gate			(void) printf(
7537c478bdstevel@tonic-gate			    "Negotiating network ascii mode on output.\n");
7547c478bdstevel@tonic-gate			tel_leave_binary(2);
7557c478bdstevel@tonic-gate		}
7567c478bdstevel@tonic-gate	}
7577c478bdstevel@tonic-gate	return (1);
7587c478bdstevel@tonic-gate}
7597c478bdstevel@tonic-gate
7607c478bdstevel@tonic-gate
7617c478bdstevel@tonic-gatestatic int togglehelp(void);
7627c478bdstevel@tonic-gateextern int auth_togdebug(int);
7637c478bdstevel@tonic-gate
7647c478bdstevel@tonic-gatestruct togglelist {
7657c478bdstevel@tonic-gate	char	*name;		/* name of toggle */
7667c478bdstevel@tonic-gate	char	*help;		/* help message */
7677c478bdstevel@tonic-gate	int	(*handler)();	/* routine to do actual setting */
7687c478bdstevel@tonic-gate	int	*variable;
7697c478bdstevel@tonic-gate	char	*actionexplanation;
7707c478bdstevel@tonic-gate};
7717c478bdstevel@tonic-gate
7727c478bdstevel@tonic-gatestatic struct togglelist Togglelist[] = {
7737c478bdstevel@tonic-gate	{ "autoflush",
7747c478bdstevel@tonic-gate	"flushing of output when sending interrupt characters",
7757c478bdstevel@tonic-gate	    0,
7767c478bdstevel@tonic-gate		&autoflush,
7777c478bdstevel@tonic-gate		    "flush output when sending interrupt characters" },
7787c478bdstevel@tonic-gate	{ "autosynch",
7797c478bdstevel@tonic-gate	"automatic sending of interrupt characters in urgent mode",
7807c478bdstevel@tonic-gate	    0,
7817c478bdstevel@tonic-gate		&autosynch,
7827c478bdstevel@tonic-gate		    "send interrupt characters in urgent mode" },
7837c478bdstevel@tonic-gate	{ "autologin",
7847c478bdstevel@tonic-gate	"automatic sending of login and/or authentication info",
7857c478bdstevel@tonic-gate	    0,
7867c478bdstevel@tonic-gate		&autologin,
7877c478bdstevel@tonic-gate		    "send login name and/or authentication information" },
7887c478bdstevel@tonic-gate	{ "authdebug",
7897c478bdstevel@tonic-gate	"authentication debugging",
7907c478bdstevel@tonic-gate	    auth_togdebug,
7917c478bdstevel@tonic-gate		0,
7927c478bdstevel@tonic-gate		    "print authentication debugging information" },
7937c478bdstevel@tonic-gate	{ "autoencrypt",
7947c478bdstevel@tonic-gate	"automatic encryption of data stream",
7957c478bdstevel@tonic-gate	    EncryptAutoEnc,
7967c478bdstevel@tonic-gate		0,
7977c478bdstevel@tonic-gate		    "automatically encrypt output" },
7987c478bdstevel@tonic-gate	{ "autodecrypt",
7997c478bdstevel@tonic-gate	"automatic decryption of data stream",
8007c478bdstevel@tonic-gate	    EncryptAutoDec,
8017c478bdstevel@tonic-gate		0,
8027c478bdstevel@tonic-gate		    "automatically decrypt input" },
8037c478bdstevel@tonic-gate	{ "verbose_encrypt",
8047c478bdstevel@tonic-gate	"verbose encryption output",
8057c478bdstevel@tonic-gate	    EncryptVerbose,
8067c478bdstevel@tonic-gate		0,
8077c478bdstevel@tonic-gate		    "print verbose encryption output" },
8087c478bdstevel@tonic-gate	{ "encdebug",
8097c478bdstevel@tonic-gate	"encryption debugging",
8107c478bdstevel@tonic-gate	    EncryptDebug,
8117c478bdstevel@tonic-gate		0,
8127c478bdstevel@tonic-gate		    "print encryption debugging information" },
8137c478bdstevel@tonic-gate	{ "skiprc",
8147c478bdstevel@tonic-gate	"don't read ~/.telnetrc file",
8157c478bdstevel@tonic-gate	    0,
8167c478bdstevel@tonic-gate		&skiprc,
8177c478bdstevel@tonic-gate		    "skip reading of ~/.telnetrc file" },
8187c478bdstevel@tonic-gate	{ "binary",
8197c478bdstevel@tonic-gate	"sending and receiving of binary data",
8207c478bdstevel@tonic-gate	    togbinary,
8217c478bdstevel@tonic-gate		0,
8227c478bdstevel@tonic-gate		    0 },
8237c478bdstevel@tonic-gate	{ "inbinary",
8247c478bdstevel@tonic-gate	"receiving of binary data",
8257c478bdstevel@tonic-gate	    togrbinary,
8267c478bdstevel@tonic-gate		0,
8277c478bdstevel@tonic-gate		    0 },
8287c478bdstevel@tonic-gate	{ "outbinary",
8297c478bdstevel@tonic-gate	"sending of binary data",
8307c478bdstevel@tonic-gate	    togxbinary,
8317c478bdstevel@tonic-gate		0,
8327c478bdstevel@tonic-gate		    0 },
8337c478bdstevel@tonic-gate	{ "crlf",
8347c478bdstevel@tonic-gate	"sending carriage returns as telnet <CR><LF>",
8357c478bdstevel@tonic-gate	    togcrlf,
8367c478bdstevel@tonic-gate		&crlf,
8377c478bdstevel@tonic-gate		    0 },
8387c478bdstevel@tonic-gate	{ "crmod",
8397c478bdstevel@tonic-gate	"mapping of received carriage returns",
8407c478bdstevel@tonic-gate	    0,
8417c478bdstevel@tonic-gate		&crmod,
8427c478bdstevel@tonic-gate		    "map carriage return on output" },
8437c478bdstevel@tonic-gate	{ "localchars",
8447c478bdstevel@tonic-gate	"local recognition of certain control characters",
8457c478bdstevel@tonic-gate	    lclchars,
8467c478bdstevel@tonic-gate		&localchars,
8477c478bdstevel@tonic-gate		    "recognize certain control characters" },
8487c478bdstevel@tonic-gate	{ " ", "", 0 },		/* empty line */
8497c478bdstevel@tonic-gate	{ "debug",
8507c478bdstevel@tonic-gate	"debugging",
8517c478bdstevel@tonic-gate	    togdebug,
8527c478bdstevel@tonic-gate		&debug,
8537c478bdstevel@tonic-gate		    "turn on socket level debugging" },
8547c478bdstevel@tonic-gate	{ "netdata",
8557c478bdstevel@tonic-gate	"printing of hexadecimal network data (debugging)",
8567c478bdstevel@tonic-gate	    0,
8577c478bdstevel@tonic-gate		&netdata,
8587c478bdstevel@tonic-gate		    "print hexadecimal representation of network traffic" },
8597c478bdstevel@tonic-gate	{ "prettydump",
8607c478bdstevel@tonic-gate	"output of \"netdata\" to user readable format (debugging)",
8617c478bdstevel@tonic-gate	    0,
8627c478bdstevel@tonic-gate		&prettydump,
8637c478bdstevel@tonic-gate		    "print user readable output for \"netdata\"" },
8647c478bdstevel@tonic-gate	{ "options",
8657c478bdstevel@tonic-gate	"viewing of options processing (debugging)",
8667c478bdstevel@tonic-gate	    0,
8677c478bdstevel@tonic-gate		&showoptions,
8687c478bdstevel@tonic-gate		    "show option processing" },
8697c478bdstevel@tonic-gate	{ "termdata",
8707c478bdstevel@tonic-gate	"(debugging) toggle printing of hexadecimal terminal data",
8717c478bdstevel@tonic-gate	    0,
8727c478bdstevel@tonic-gate		&termdata,
8737c478bdstevel@tonic-gate		    "print hexadecimal representation of terminal traffic" },
8747c478bdstevel@tonic-gate	{ "?",
8757c478bdstevel@tonic-gate	0,
8767c478bdstevel@tonic-gate	    togglehelp },
8777c478bdstevel@tonic-gate	{ "help",
8787c478bdstevel@tonic-gate	0,
8797c478bdstevel@tonic-gate	    togglehelp },
8807c478bdstevel@tonic-gate	{ 0 }
8817c478bdstevel@tonic-gate};
8827c478bdstevel@tonic-gate
8837c478bdstevel@tonic-gatestatic int
8847c478bdstevel@tonic-gatetogglehelp()
8857c478bdstevel@tonic-gate{
8867c478bdstevel@tonic-gate	struct togglelist *c;
8877c478bdstevel@tonic-gate
8887c478bdstevel@tonic-gate	for (c = Togglelist; c->name; c++) {
8897c478bdstevel@tonic-gate		if (c->help) {
8907c478bdstevel@tonic-gate			if (*c->help)
8917c478bdstevel@tonic-gate				(void) printf(
8927c478bdstevel@tonic-gate					"%-15s toggle %s\n", c->name, c->help);
8937c478bdstevel@tonic-gate			else
8947c478bdstevel@tonic-gate				(void) printf("\n");
8957c478bdstevel@tonic-gate		}
8967c478bdstevel@tonic-gate	}
8977c478bdstevel@tonic-gate	(void) printf("\n");
8987c478bdstevel@tonic-gate	(void) printf("%-15s %s\n", "?", "display help information");
8997c478bdstevel@tonic-gate	return (0);
9007c478bdstevel@tonic-gate}
9017c478bdstevel@tonic-gate
9027c478bdstevel@tonic-gatestatic void
9037c478bdstevel@tonic-gatesettogglehelp(set)
9047c478bdstevel@tonic-gate	int set;
9057c478bdstevel@tonic-gate{
9067c478bdstevel@tonic-gate	struct togglelist *c;
9077c478bdstevel@tonic-gate
9087c478bdstevel@tonic-gate	for (c = Togglelist; c->name; c++) {
9097c478bdstevel@tonic-gate		if (c->help) {
9107c478bdstevel@tonic-gate			if (*c->help)
9117c478bdstevel@tonic-gate				(void) printf("%-15s %s %s\n", c->name,
9127c478bdstevel@tonic-gate				    set ? "enable" : "disable", c->help);
9137c478bdstevel@tonic-gate			else
9147c478bdstevel@tonic-gate				(void) printf("\n");
9157c478bdstevel@tonic-gate		}
9167c478bdstevel@tonic-gate	}
9177c478bdstevel@tonic-gate}
9187c478bdstevel@tonic-gate
9197c478bdstevel@tonic-gate#define	GETTOGGLE(name) (struct togglelist *) \
9207c478bdstevel@tonic-gate		genget(name, (char **)Togglelist, sizeof (struct togglelist))
9217c478bdstevel@tonic-gate
9227c478bdstevel@tonic-gatestatic int
9237c478bdstevel@tonic-gatetoggle(argc, argv)
9247c478bdstevel@tonic-gate	int  argc;
9257c478bdstevel@tonic-gate	char *argv[];
9267c478bdstevel@tonic-gate{
9277c478bdstevel@tonic-gate	int retval = 1;
9287c478bdstevel@tonic-gate	char *name;
9297c478bdstevel@tonic-gate	struct togglelist *c;
9307c478bdstevel@tonic-gate
9317c478bdstevel@tonic-gate	if (argc < 2) {
9327c478bdstevel@tonic-gate		(void) fprintf(stderr,
9337c478bdstevel@tonic-gate		    "Need an argument to 'toggle' command.  "
9347c478bdstevel@tonic-gate		    "'toggle ?' for help.\n");
9357c478bdstevel@tonic-gate		return (0);
9367c478bdstevel@tonic-gate	}
9377c478bdstevel@tonic-gate	argc--;
9387c478bdstevel@tonic-gate	argv++;
9397c478bdstevel@tonic-gate	while (argc--) {
9407c478bdstevel@tonic-gate		name = *argv++;
9417c478bdstevel@tonic-gate		c = GETTOGGLE(name);
9427c478bdstevel@tonic-gate		if (Ambiguous(c)) {
9437c478bdstevel@tonic-gate			(void) fprintf(stderr, "'%s': ambiguous argument "
9447c478bdstevel@tonic-gate			    "('toggle ?' for help).\n", name);
9457c478bdstevel@tonic-gate			return (0);
9467c478bdstevel@tonic-gate		} else if (c == 0) {
9477c478bdstevel@tonic-gate			(void) fprintf(stderr, "'%s': unknown argument "
9487c478bdstevel@tonic-gate			    "('toggle ?' for help).\n", name);
9497c478bdstevel@tonic-gate			return (0);
9507c478bdstevel@tonic-gate		} else {
9517c478bdstevel@tonic-gate			if (c->variable) {
9527c478bdstevel@tonic-gate				*c->variable = !*c->variable;	/* invert it */
9537c478bdstevel@tonic-gate				if (c->actionexplanation) {
9547c478bdstevel@tonic-gate			(void) printf("%s %s.\n",
9557c478bdstevel@tonic-gate				*c->variable ? "Will" : "Won't",
9567c478bdstevel@tonic-gate							c->actionexplanation);
9577c478bdstevel@tonic-gate				}
9587c478bdstevel@tonic-gate			}
9597c478bdstevel@tonic-gate			if (c->handler) {
9607c478bdstevel@tonic-gate				retval &= (*c->handler)(-1);
9617c478bdstevel@tonic-gate			}
9627c478bdstevel@tonic-gate		}
9637c478bdstevel@tonic-gate	}
9647c478bdstevel@tonic-gate	return (retval);
9657c478bdstevel@tonic-gate}
9667c478bdstevel@tonic-gate
9677c478bdstevel@tonic-gate/*
9687c478bdstevel@tonic-gate * The following perform the "set" command.
9697c478bdstevel@tonic-gate */
9707c478bdstevel@tonic-gate
9717c478bdstevel@tonic-gate#ifdef	USE_TERMIO
9727c478bdstevel@tonic-gatestruct termio new_tc = { 0 };
9737c478bdstevel@tonic-gate#endif
9747c478bdstevel@tonic-gate
9757c478bdstevel@tonic-gatestruct setlist {
9767c478bdstevel@tonic-gate	char *name;		/* name */
9777c478bdstevel@tonic-gate	char *help;		/* help information */
9787c478bdstevel@tonic-gate	void (*handler)();
9797c478bdstevel@tonic-gate	cc_t *charp;		/* where it is located at */
9807c478bdstevel@tonic-gate};
9817c478bdstevel@tonic-gate
9827c478bdstevel@tonic-gatestatic struct setlist Setlist[] = {
9837c478bdstevel@tonic-gate#ifdef	KLUDGELINEMODE
9847c478bdstevel@tonic-gate	{ "echo",  "character to toggle local echoing on/off", 0, &echoc },
9857c478bdstevel@tonic-gate#endif
9867c478bdstevel@tonic-gate	{ "escape", "character to escape back to telnet command mode", 0,
9877c478bdstevel@tonic-gate	    &escape },
9887c478bdstevel@tonic-gate	{ "rlogin", "rlogin escape character", 0, &rlogin },
9897c478bdstevel@tonic-gate	{ "tracefile", "file to write trace information to", SetNetTrace,
9907c478bdstevel@tonic-gate	    (cc_t *)NetTraceFile},
9917c478bdstevel@tonic-gate	{ " ", "" },
9927c478bdstevel@tonic-gate	{ " ", "The following need 'localchars' to be toggled true", 0, 0 },
9937c478bdstevel@tonic-gate	{ "flushoutput", "character to cause an Abort Output", 0,
9947c478bdstevel@tonic-gate	    termFlushCharp },
9957c478bdstevel@tonic-gate	{ "interrupt", "character to cause an Interrupt Process", 0,
9967c478bdstevel@tonic-gate	    termIntCharp },
9977c478bdstevel@tonic-gate	{ "quit", "character to cause an Abort process", 0, termQuitCharp },
9987c478bdstevel@tonic-gate	{ "eof", "character to cause an EOF ", 0, termEofCharp },
9997c478bdstevel@tonic-gate	{ " ", "" },
10007c478bdstevel@tonic-gate	{ " ", "The following are for local editing in linemode", 0, 0 },
10017c478bdstevel@tonic-gate	{ "erase", "character to use to erase a character", 0, termEraseCharp },
10027c478bdstevel@tonic-gate	{ "kill", "character to use to erase a line", 0, termKillCharp },
10037c478bdstevel@tonic-gate	{ "lnext", "character to use for literal next", 0,
10047c478bdstevel@tonic-gate	    termLiteralNextCharp },
10057c478bdstevel@tonic-gate	{ "susp", "character to cause a Suspend Process", 0, termSuspCharp },
10067c478bdstevel@tonic-gate	{ "reprint", "character to use for line reprint", 0, termRprntCharp },
10077c478bdstevel@tonic-gate	{ "worderase", "character to use to erase a word", 0, termWerasCharp },
10087c478bdstevel@tonic-gate	{ "start",	"character to use for XON", 0, termStartCharp },
10097c478bdstevel@tonic-gate	{ "stop",	"character to use for XOFF", 0, termStopCharp },
10107c478bdstevel@tonic-gate	{ "forw1",	"alternate end of line character", 0, termForw1Charp },
10117c478bdstevel@tonic-gate	{ "forw2",	"alternate end of line character", 0, termForw2Charp },
10127c478bdstevel@tonic-gate	{ "ayt",	"alternate AYT character", 0, termAytCharp },
10137c478bdstevel@tonic-gate	{ 0 }
10147c478bdstevel@tonic-gate};
10157c478bdstevel@tonic-gate
10167c478bdstevel@tonic-gatestatic struct setlist *
10177c478bdstevel@tonic-gategetset(name)
10187c478bdstevel@tonic-gate    char *name;
10197c478bdstevel@tonic-gate{
10207c478bdstevel@tonic-gate	return ((struct setlist *)
10217c478bdstevel@tonic-gate	    genget(name, (char **)Setlist, sizeof (struct setlist)));
10227c478bdstevel@tonic-gate}
10237c478bdstevel@tonic-gate
10247c478bdstevel@tonic-gate    void
10257c478bdstevel@tonic-gateset_escape_char(s)
10267c478bdstevel@tonic-gate    char *s;
10277c478bdstevel@tonic-gate{
10287c478bdstevel@tonic-gate	if (rlogin != _POSIX_VDISABLE) {
10297c478bdstevel@tonic-gate		rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
10307c478bdstevel@tonic-gate		(void) printf("Telnet rlogin escape character is '%s'.\n",
10317c478bdstevel@tonic-gate					control(rlogin));
10327c478bdstevel@tonic-gate	} else {
10337c478bdstevel@tonic-gate		escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
10347c478bdstevel@tonic-gate		(void) printf("Telnet escape character is '%s'.\n",
10357c478bdstevel@tonic-gate		    esc_control(escape));
10367c478bdstevel@tonic-gate	}
10377c478bdstevel@tonic-gate}
10387c478bdstevel@tonic-gate
10397c478bdstevel@tonic-gatestatic int
10407c478bdstevel@tonic-gatesetcmd(argc, argv)
10417c478bdstevel@tonic-gate	int  argc;
10427c478bdstevel@tonic-gate	char *argv[];
10437c478bdstevel@tonic-gate{
10447c478bdstevel@tonic-gate	int value;
10457c478bdstevel@tonic-gate	struct setlist *ct;
10467c478bdstevel@tonic-gate	struct togglelist *c;
10477c478bdstevel@tonic-gate
10487c478bdstevel@tonic-gate	if (argc < 2 || argc > 3) {
10497c478bdstevel@tonic-gate		(void) printf(
10507c478bdstevel@tonic-gate			"Format is 'set Name Value'\n'set ?' for help.\n");
10517c478bdstevel@tonic-gate		return (0);
10527c478bdstevel@tonic-gate	}
10537c478bdstevel@tonic-gate	if ((argc == 2) &&
10547c478bdstevel@tonic-gate	    (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
10557c478bdstevel@tonic-gate		for (ct = Setlist; ct->name; ct++)
10567c478bdstevel@tonic-gate			(void) printf("%-15s %s\n", ct->name, ct->help);
10577c478bdstevel@tonic-gate		(void) printf("\n");
10587c478bdstevel@tonic-gate		settogglehelp(1);
10597c478bdstevel@tonic-gate		(void) printf("%-15s %s\n", "?", "display help information");
10607c478bdstevel@tonic-gate		return (0);
10617c478bdstevel@tonic-gate	}
10627c478bdstevel@tonic-gate
10637c478bdstevel@tonic-gate	ct = getset(argv[1]);
10647c478bdstevel@tonic-gate	if (ct == 0) {
10657c478bdstevel@tonic-gate		c = GETTOGGLE(argv[1]);
10667c478bdstevel@tonic-gate		if (c == 0) {
10677c478bdstevel@tonic-gate			(void) fprintf(stderr, "'%s': unknown argument "
10687c478bdstevel@tonic-gate			    "('set ?' for help).\n", argv[1]);
10697c478bdstevel@tonic-gate			return (0);
10707c478bdstevel@tonic-gate		} else if (Ambiguous(c)) {
10717c478bdstevel@tonic-gate			(void) fprintf(stderr, "'%s': ambiguous argument "
10727c478bdstevel@tonic-gate			    "('set ?' for help).\n", argv[1]);
10737c478bdstevel@tonic-gate			return (0);
10747c478bdstevel@tonic-gate		}
10757c478bdstevel@tonic-gate		if (c->variable) {
10767c478bdstevel@tonic-gate			if ((argc == 2) || (strcmp("on", argv[2]) == 0))
10777c478bdstevel@tonic-gate				*c->variable = 1;
10787c478bdstevel@tonic-gate			else if (strcmp("off", argv[2]) == 0)
10797c478bdstevel@tonic-gate				*c->variable = 0;
10807c478bdstevel@tonic-gate			else {
10817c478bdstevel@tonic-gate				(void) printf(
10827c478bdstevel@tonic-gate				    "Format is 'set togglename [on|off]'\n"
10837c478bdstevel@tonic-gate				    "'set ?' for help.\n");
10847c478bdstevel@tonic-gate				return (0);
10857c478bdstevel@tonic-gate			}
10867c478bdstevel@tonic-gate			if (c->actionexplanation) {
10877c478bdstevel@tonic-gate				(void) printf("%s %s.\n",
10887c478bdstevel@tonic-gate				    *c->variable? "Will" : "Won't",
10897c478bdstevel@tonic-gate				    c->actionexplanation);
10907c478bdstevel@tonic-gate			}
10917c478bdstevel@tonic-gate		}
10927c478bdstevel@tonic-gate		if (c->handler)
10937c478bdstevel@tonic-gate			(*c->handler)(1);
10947c478bdstevel@tonic-gate	} else if (argc != 3) {
10957c478bdstevel@tonic-gate		(void) printf(
10967c478bdstevel@tonic-gate			"Format is 'set Name Value'\n'set ?' for help.\n");
10977c478bdstevel@tonic-gate		return (0);
10987c478bdstevel@tonic-gate	} else if (Ambiguous(ct)) {
10997c478bdstevel@tonic-gate		(void) fprintf(stderr,
11007c478bdstevel@tonic-gate		    "'%s': ambiguous argument ('set ?' for help).\n", argv[1]);
11017c478bdstevel@tonic-gate		return (0);
11027c478bdstevel@tonic-gate	} else if (ct->handler) {
11037c478bdstevel@tonic-gate		(*ct->handler)(argv[2]);
11047c478bdstevel@tonic-gate		(void) printf(
11057c478bdstevel@tonic-gate		    "%s set to \"%s\".\n", ct->name, (char *)ct->charp);
11067c478bdstevel@tonic-gate	} else {
11077c478bdstevel@tonic-gate		if (strcmp("off", argv[2])) {
11087c478bdstevel@tonic-gate			value = special(argv[2]);
11097c478bdstevel@tonic-gate		} else {
11107c478bdstevel@tonic-gate			value = _POSIX_VDISABLE;
11117c478bdstevel@tonic-gate		}
11127c478bdstevel@tonic-gate		*(ct->charp) = (cc_t)value;
11137c478bdstevel@tonic-gate		(void) printf("%s character is '%s'.\n", ct->name,
11147c478bdstevel@tonic-gate		    control(*(ct->charp)));
11157c478bdstevel@tonic-gate	}
11167c478bdstevel@tonic-gate	slc_check();
11177c478bdstevel@tonic-gate	return (1);
11187c478bdstevel@tonic-gate}
11197c478bdstevel@tonic-gate
11207c478bdstevel@tonic-gatestatic int
11217c478bdstevel@tonic-gateunsetcmd(argc, argv)
11227c478bdstevel@tonic-gate	int  argc;
11237c478bdstevel@tonic-gate	char *argv[];
11247c478bdstevel@tonic-gate{
11257c478bdstevel@tonic-gate	struct setlist *ct;
11267c478bdstevel@tonic-gate	struct togglelist *c;
11277c478bdstevel@tonic-gate	register char *name;
11287c478bdstevel@tonic-gate
11297c478bdstevel@tonic-gate	if (argc < 2) {
11307c478bdstevel@tonic-gate		(void) fprintf(stderr, "Need an argument to 'unset' command.  "
11317c478bdstevel@tonic-gate		    "'unset ?' for help.\n");
11327c478bdstevel@tonic-gate		return (0);
11337c478bdstevel@tonic-gate	}
11347c478bdstevel@tonic-gate	if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
11357c478bdstevel@tonic-gate		for (ct = Setlist; ct->name; ct++)
11367c478bdstevel@tonic-gate			(void) printf("%-15s %s\n", ct->name, ct->help);
11377c478bdstevel@tonic-gate		(void) printf("\n");
11387c478bdstevel@tonic-gate		settogglehelp(0);
11397c478bdstevel@tonic-gate		(void) printf("%-15s %s\n", "?", "display help information");
11407c478bdstevel@tonic-gate		return (0);
11417c478bdstevel@tonic-gate	}
11427c478bdstevel@tonic-gate
11437c478bdstevel@tonic-gate	argc--;
11447c478bdstevel@tonic-gate	argv++;
11457c478bdstevel@tonic-gate	while (argc--) {
11467c478bdstevel@tonic-gate		name = *argv++;
11477c478bdstevel@tonic-gate		ct = getset(name);
11487c478bdstevel@tonic-gate		if (ct == 0) {
11497c478bdstevel@tonic-gate			c = GETTOGGLE(name);
11507c478bdstevel@tonic-gate			if (c == 0) {
11517c478bdstevel@tonic-gate				(void) fprintf(stderr, "'%s': unknown argument "
11527c478bdstevel@tonic-gate				    "('unset ?' for help).\n", name);
11537c478bdstevel@tonic-gate				return (0);
11547c478bdstevel@tonic-gate			} else if (Ambiguous(c)) {
11557c478bdstevel@tonic-gate				(void) fprintf(stderr,
11567c478bdstevel@tonic-gate				    "'%s': ambiguous argument "
11577c478bdstevel@tonic-gate				    "('unset ?' for help).\n", name);
11587c478bdstevel@tonic-gate				return (0);
11597c478bdstevel@tonic-gate			}
11607c478bdstevel@tonic-gate			if (c->variable) {
11617c478bdstevel@tonic-gate				*c->variable = 0;
11627c478bdstevel@tonic-gate				if (c->actionexplanation) {
11637c478bdstevel@tonic-gate					(void) printf("%s %s.\n",
11647c478bdstevel@tonic-gate					    *c->variable? "Will" : "Won't",
11657c478bdstevel@tonic-gate					    c->actionexplanation);
11667c478bdstevel@tonic-gate				}
11677c478bdstevel@tonic-gate			}
11687c478bdstevel@tonic-gate			if (c->handler)
11697c478bdstevel@tonic-gate				(*c->handler)(0);
11707c478bdstevel@tonic-gate		} else if (Ambiguous(ct)) {
11717c478bdstevel@tonic-gate			(void) fprintf(stderr, "'%s': ambiguous argument "
11727c478bdstevel@tonic-gate			    "('unset ?' for help).\n", name);
11737c478bdstevel@tonic-gate			return (0);
11747c478bdstevel@tonic-gate		} else if (ct->handler) {
11757c478bdstevel@tonic-gate			(*ct->handler)(0);
11767c478bdstevel@tonic-gate			(void) printf("%s reset to \"%s\".\n", ct->name,
11777c478bdstevel@tonic-gate			    (char *)ct->charp);
11787c478bdstevel@tonic-gate		} else {
11797c478bdstevel@tonic-gate			*(ct->charp) = _POSIX_VDISABLE;
11807c478bdstevel@tonic-gate			(void) printf("%s character is '%s'.\n", ct->name,
11817c478bdstevel@tonic-gate			    control(*(ct->charp)));
11827c478bdstevel@tonic-gate		}
11837c478bdstevel@tonic-gate	}
11847c478bdstevel@tonic-gate	return (1);
11857c478bdstevel@tonic-gate}
11867c478bdstevel@tonic-gate
11877c478bdstevel@tonic-gate/*
11887c478bdstevel@tonic-gate * The following are the data structures and routines for the
11897c478bdstevel@tonic-gate * 'mode' command.
11907c478bdstevel@tonic-gate */
11917c478bdstevel@tonic-gateextern int reqd_linemode;
11927c478bdstevel@tonic-gate
11937c478bdstevel@tonic-gate#ifdef	KLUDGELINEMODE
11947c478bdstevel@tonic-gateextern int kludgelinemode;
11957c478bdstevel@tonic-gate
11967c478bdstevel@tonic-gatestatic int
11977c478bdstevel@tonic-gatedokludgemode()
11987c478bdstevel@tonic-gate{
11997c478bdstevel@tonic-gate	kludgelinemode = 1;
12007c478bdstevel@tonic-gate	send_wont(TELOPT_LINEMODE, 1);
12017c478bdstevel@tonic-gate	send_dont(TELOPT_SGA, 1);
12027c478bdstevel@tonic-gate	send_dont(TELOPT_ECHO, 1);
12037c478bdstevel@tonic-gate	/*
12047c478bdstevel@tonic-gate	 * If processing the .telnetrc file, keep track of linemode and/or
12057c478bdstevel@tonic-gate	 * kludgelinemode requests which are processed before initial option
12067c478bdstevel@tonic-gate	 * negotiations occur.
12077c478bdstevel@tonic-gate	 */
12087c478bdstevel@tonic-gate	if (doing_rc)
12097c478bdstevel@tonic-gate		reqd_linemode = 1;
12107c478bdstevel@tonic-gate	return (1);
12117c478bdstevel@tonic-gate}
12127c478bdstevel@tonic-gate#endif
12137c478bdstevel@tonic-gate
12147c478bdstevel@tonic-gatestatic int
12157c478bdstevel@tonic-gatedolinemode()
12167c478bdstevel@tonic-gate{
12177c478bdstevel@tonic-gate#ifdef	KLUDGELINEMODE
12187c478bdstevel@tonic-gate	if (kludgelinemode)
12197c478bdstevel@tonic-gate		send_dont(TELOPT_SGA, 1);
12207c478bdstevel@tonic-gate#endif
12217c478bdstevel@tonic-gate	send_will(TELOPT_LINEMODE, 1);
12227c478bdstevel@tonic-gate	send_dont(TELOPT_ECHO, 1);
12237c478bdstevel@tonic-gate
12247c478bdstevel@tonic-gate	/*
12257c478bdstevel@tonic-gate	 * If processing the .telnetrc file, keep track of linemode and/or
12267c478bdstevel@tonic-gate	 * kludgelinemode requests which are processed before initial option
12277c478bdstevel@tonic-gate	 * negotiations occur.
12287c478bdstevel@tonic-gate	 */
12297c478bdstevel@tonic-gate	if (doing_rc)
12307c478bdstevel@tonic-gate		reqd_linemode = 1;
12317c478bdstevel@tonic-gate	return (1);
12327c478bdstevel@tonic-gate}
12337c478bdstevel@tonic-gate
12347c478bdstevel@tonic-gatestatic int
12357c478bdstevel@tonic-gatedocharmode()
12367c478bdstevel@tonic-gate{
12377c478bdstevel@tonic-gate#ifdef	KLUDGELINEMODE
12387c478bdstevel@tonic-gate	if (kludgelinemode)
12397c478bdstevel@tonic-gate		send_do(TELOPT_SGA, 1);
12407c478bdstevel@tonic-gate	else
12417c478bdstevel@tonic-gate#endif
12427c478bdstevel@tonic-gate		send_wont(TELOPT_LINEMODE, 1);
12437c478bdstevel@tonic-gate	send_do(TELOPT_ECHO, 1);
12447c478bdstevel@tonic-gate	reqd_linemode = 0;
12457c478bdstevel@tonic-gate	return (1);
12467c478bdstevel@tonic-gate}
12477c478bdstevel@tonic-gate
12487c478bdstevel@tonic-gatestatic int
12497c478bdstevel@tonic-gatedolmmode(bit, on)
12507c478bdstevel@tonic-gate	int bit, on;
12517c478bdstevel@tonic-gate{
12527c478bdstevel@tonic-gate	unsigned char c;
12537c478bdstevel@tonic-gate	extern int linemode;
12547c478bdstevel@tonic-gate
12557c478bdstevel@tonic-gate	if (my_want_state_is_wont(TELOPT_LINEMODE)) {
12567c478bdstevel@tonic-gate		(void) printf("?Need to have LINEMODE option enabled first.\n");
12577c478bdstevel@tonic-gate		(void) printf("'mode ?' for help.\n");
12587c478bdstevel@tonic-gate		return (0);
12597c478bdstevel@tonic-gate	}
12607c478bdstevel@tonic-gate
12617c478bdstevel@tonic-gate	if (on)
12627c478bdstevel@tonic-gate		c = (linemode | bit);
12637c478bdstevel@tonic-gate	else
12647c478bdstevel@tonic-gate		c = (linemode & ~bit);
12657c478bdstevel@tonic-gate	lm_mode(&c, 1, 1);
12667c478bdstevel@tonic-gate	return (1);
12677c478bdstevel@tonic-gate}
12687c478bdstevel@tonic-gate
12697c478bdstevel@tonic-gatestatic int
12707c478bdstevel@tonic-gatesetmode(bit)
12717c478bdstevel@tonic-gate{
12727c478bdstevel@tonic-gate	return (dolmmode(bit, 1));
12737c478bdstevel@tonic-gate}
12747c478bdstevel@tonic-gate
12757c478bdstevel@tonic-gatestatic int
12767c478bdstevel@tonic-gateclearmode(bit)
12777c478bdstevel@tonic-gate{
12787c478bdstevel@tonic-gate	return (dolmmode(bit, 0));
12797c478bdstevel@tonic-gate}
12807c478bdstevel@tonic-gate
12817c478bdstevel@tonic-gatestruct modelist {
12827c478bdstevel@tonic-gate	char	*name;		/* command name */
12837c478bdstevel@tonic-gate	char	*help;		/* help string */
12847c478bdstevel@tonic-gate	int	(*handler)();	/* routine which executes command */
12857c478bdstevel@tonic-gate	int	needconnect;	/* Do we need to be connected to execute? */
12867c478bdstevel@tonic-gate	int	arg1;
12877c478bdstevel@tonic-gate};
12887c478bdstevel@tonic-gate
12897c478bdstevel@tonic-gatestatic int modehelp();
12907c478bdstevel@tonic-gate
12917c478bdstevel@tonic-gatestatic struct modelist ModeList[] = {
12927c478bdstevel@tonic-gate	{ "character", "Disable LINEMODE option",	docharmode, 1 },
12937c478bdstevel@tonic-gate#ifdef	KLUDGELINEMODE
12947c478bdstevel@tonic-gate	{ "",	"(or disable obsolete line-by-line mode)", 0 },
12957c478bdstevel@tonic-gate#endif
12967c478bdstevel@tonic-gate	{ "line",	"Enable LINEMODE option",	dolinemode, 1 },
12977c478bdstevel@tonic-gate#ifdef	KLUDGELINEMODE
12987c478bdstevel@tonic-gate	{ "",	"(or enable obsolete line-by-line mode)", 0 },
12997c478bdstevel@tonic-gate#endif
13007c478bdstevel@tonic-gate	{ "", "", 0 },
13017c478bdstevel@tonic-gate	{ "",	"These require the LINEMODE option to be enabled", 0 },
13027c478bdstevel@tonic-gate	{ "isig",	"Enable signal trapping", setmode, 1, MODE_TRAPSIG },
13037c478bdstevel@tonic-gate	{ "+isig",	0,			setmode, 1, MODE_TRAPSIG },
13047c478bdstevel@tonic-gate	{ "-isig",	"Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
13057c478bdstevel@tonic-gate	{ "edit",	"Enable character editing",	setmode, 1, MODE_EDIT },
13067c478bdstevel@tonic-gate	{ "+edit",	0,			setmode, 1, MODE_EDIT },
13077c478bdstevel@tonic-gate	{ "-edit",	"Disable character editing", clearmode, 1, MODE_EDIT },
13087c478bdstevel@tonic-gate	{ "softtabs",	"Enable tab expansion",	setmode, 1, MODE_SOFT_TAB },
13097c478bdstevel@tonic-gate	{ "+softtabs",	0,			setmode, 1, MODE_SOFT_TAB },
13107c478bdstevel@tonic-gate	{ "-softtabs",	"Disable tab expansion",
13117c478bdstevel@tonic-gate						clearmode, 1, MODE_SOFT_TAB },
13127c478bdstevel@tonic-gate	{ "litecho",	"Enable literal character echo",
13137c478bdstevel@tonic-gate						setmode, 1, MODE_LIT_ECHO },
13147c478bdstevel@tonic-gate	{ "+litecho",	0,			setmode, 1, MODE_LIT_ECHO },
13157c478bdstevel@tonic-gate	{ "-litecho",	"Disable literal character echo", clearmode, 1,
13167c478bdstevel@tonic-gate		MODE_LIT_ECHO },
13177c478bdstevel@tonic-gate	{ "help",	0,			modehelp, 0 },
13187c478bdstevel@tonic-gate#ifdef	KLUDGELINEMODE
13197c478bdstevel@tonic-gate	{ "kludgeline", 0,				dokludgemode, 1 },
13207c478bdstevel@tonic-gate#endif
13217c478bdstevel@tonic-gate	{ "", "", 0 },
13227c478bdstevel@tonic-gate	{ "?",	"Print help information",	modehelp, 0 },
13237c478bdstevel@tonic-gate	{ 0 },
13247c478bdstevel@tonic-gate};
13257c478bdstevel@tonic-gate
13267c478bdstevel@tonic-gate
13277c478bdstevel@tonic-gatestatic int
13287c478bdstevel@tonic-gatemodehelp()
13297c478bdstevel@tonic-gate{
13307c478bdstevel@tonic-gate	struct modelist *mt;
13317c478bdstevel@tonic-gate
13327c478bdstevel@tonic-gate	(void) printf("format is:  'mode Mode', where 'Mode' is one of:\n\n");
13337c478bdstevel@tonic-gate	for (mt = ModeList; mt->name; mt++) {
13347c478bdstevel@tonic-gate		if (mt->help) {
13357c478bdstevel@tonic-gate			if (*mt->help)
13367c478bdstevel@tonic-gate				(void) printf("%-15s %s\n", mt->name, mt->help);
13377c478bdstevel@tonic-gate			else
13387c478bdstevel@tonic-gate				(void) printf("\n");
13397c478bdstevel@tonic-gate		}
13407c478bdstevel@tonic-gate	}
13417c478bdstevel@tonic-gate	return (0);
13427c478bdstevel@tonic-gate}
13437c478bdstevel@tonic-gate
13447c478bdstevel@tonic-gate#define	GETMODECMD(name) (struct modelist *) \
13457c478bdstevel@tonic-gate		genget(name, (char **)ModeList, sizeof (struct modelist))
13467c478bdstevel@tonic-gate
13477c478bdstevel@tonic-gatestatic int
13487c478bdstevel@tonic-gatemodecmd(argc, argv)
13497c478bdstevel@tonic-gate	int  argc;
13507c478bdstevel@tonic-gate	char *argv[];
13517c478bdstevel@tonic-gate{
13527c478bdstevel@tonic-gate	struct modelist *mt;
13537c478bdstevel@tonic-gate
13547c478bdstevel@tonic-gate	if (argc != 2) {
13557c478bdstevel@tonic-gate		(void) printf("'mode' command requires an argument\n");
13567c478bdstevel@tonic-gate		(void) printf("'mode ?' for help.\n");
13577c478bdstevel@tonic-gate	} else if ((mt = GETMODECMD(argv[1])) == 0) {
13587c478bdstevel@tonic-gate		(void) fprintf(stderr,
13597c478bdstevel@tonic-gate		    "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
13607c478bdstevel@tonic-gate	} else if (Ambiguous(mt)) {
13617c478bdstevel@tonic-gate		(void) fprintf(stderr,
13627c478bdstevel@tonic-gate		    "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
13637c478bdstevel@tonic-gate	} else if (mt->needconnect && !connected) {
13647c478bdstevel@tonic-gate		(void) printf("?Need to be connected first.\n");
13657c478bdstevel@tonic-gate		(void) printf("'mode ?' for help.\n");
13667c478bdstevel@tonic-gate	} else if (mt->handler) {
13677c478bdstevel@tonic-gate		return (*mt->handler)(mt->arg1);
13687c478bdstevel@tonic-gate	}
13697c478bdstevel@tonic-gate	return (0);
13707c478bdstevel@tonic-gate}
13717c478bdstevel@tonic-gate
13727c478bdstevel@tonic-gate/*
13737c478bdstevel@tonic-gate * The following data structures and routines implement the
13747c478bdstevel@tonic-gate * "display" command.
13757c478bdstevel@tonic-gate */
13767c478bdstevel@tonic-gate
13777c478bdstevel@tonic-gatestatic int
13787c478bdstevel@tonic-gatedisplay(argc, argv)
13797c478bdstevel@tonic-gate	int  argc;
13807c478bdstevel@tonic-gate	char *argv[];
13817c478bdstevel@tonic-gate{
13827c478bdstevel@tonic-gate	struct togglelist *tl;
13837c478bdstevel@tonic-gate	struct setlist *sl;
13847c478bdstevel@tonic-gate
13857c478bdstevel@tonic-gate#define	dotog(tl)	if (tl->variable && tl->actionexplanation) { \
13867c478bdstevel@tonic-gate			    if (*tl->variable) { \
13877c478bdstevel@tonic-gate				(void) printf("will"); \
13887c478bdstevel@tonic-gate			    } else { \
13897c478bdstevel@tonic-gate				(void) printf("won't"); \
13907c478bdstevel@tonic-gate			    } \
13917c478bdstevel@tonic-gate			    (void) printf(" %s.\n", tl->actionexplanation); \
13927c478bdstevel@tonic-gate			}
13937c478bdstevel@tonic-gate
13947c478bdstevel@tonic-gate#define	doset(sl)   if (sl->name && *sl->name != ' ') { \
13957c478bdstevel@tonic-gate			if (sl->handler == 0) \
13967c478bdstevel@tonic-gate			    (void) printf("%-15s [%s]\n", sl->name, \
13977c478bdstevel@tonic-gate			    control(*sl->charp)); \
13987c478bdstevel@tonic-gate			else \
13997c478bdstevel@tonic-gate			    (void) printf("%-15s \"%s\"\n", sl->name, \
14007c478bdstevel@tonic-gate			    (char *)sl->charp); \
14017c478bdstevel@tonic-gate		    }
14027c478bdstevel@tonic-gate
14037c478bdstevel@tonic-gate	if (argc == 1) {
14047c478bdstevel@tonic-gate		for (tl = Togglelist; tl->name; tl++) {
14057c478bdstevel@tonic-gate			dotog(tl);
14067c478bdstevel@tonic-gate		}
14077c478bdstevel@tonic-gate		(void) printf("\n");
14087c478bdstevel@tonic-gate		for (sl = Setlist; sl->name; sl++) {
14097c478bdstevel@tonic-gate			doset(sl);
14107c478bdstevel@tonic-gate		}
14117c478bdstevel@tonic-gate	} else {
14127c478bdstevel@tonic-gate		int i;
14137c478bdstevel@tonic-gate
14147c478bdstevel@tonic-gate		for (i = 1; i < argc; i++) {
14157c478bdstevel@tonic-gate			sl = getset(argv[i]);
14167c478bdstevel@tonic-gate			tl = GETTOGGLE(argv[i]);
14177c478bdstevel@tonic-gate			if (Ambiguous(sl) || Ambiguous(tl)) {
14187c478bdstevel@tonic-gate				(void) printf(
14197c478bdstevel@tonic-gate				    "?Ambiguous argument '%s'.\n", argv[i]);
14207c478bdstevel@tonic-gate				return (0);
14217c478bdstevel@tonic-gate			} else if (!sl && !tl) {
14227c478bdstevel@tonic-gate				(void) printf(
14237c478bdstevel@tonic-gate				    "?Unknown argument '%s'.\n", argv[i]);
14247c478bdstevel@tonic-gate				return (0);
14257c478bdstevel@tonic-gate			} else {
14267c478bdstevel@tonic-gate				if (tl) {
14277c478bdstevel@tonic-gate					dotog(tl);
14287c478bdstevel@tonic-gate				}
14297c478bdstevel@tonic-gate				if (sl) {
14307c478bdstevel@tonic-gate				    doset(sl);
14317c478bdstevel@tonic-gate				}
14327c478bdstevel@tonic-gate			}
14337c478bdstevel@tonic-gate		}
14347c478bdstevel@tonic-gate	}
14357c478bdstevel@tonic-gate	optionstatus();
14367c478bdstevel@tonic-gate	(void) EncryptStatus();
14377c478bdstevel@tonic-gate	return (1);
14387c478bdstevel@tonic-gate#undef	doset
14397c478bdstevel@tonic-gate#undef	dotog
14407c478bdstevel@tonic-gate}
14417c478bdstevel@tonic-gate
14427c478bdstevel@tonic-gate/*
14437c478bdstevel@tonic-gate * The following are the data structures, and many of the routines,
14447c478bdstevel@tonic-gate * relating to command processing.
14457c478bdstevel@tonic-gate */
14467c478bdstevel@tonic-gate
14477c478bdstevel@tonic-gate/*
14487c478bdstevel@tonic-gate * Set the escape character.
14497c478bdstevel@tonic-gate */
14507c478bdstevel@tonic-gate	static int
14517c478bdstevel@tonic-gatesetescape(argc, argv)
14527c478bdstevel@tonic-gate	int argc;
14537c478bdstevel@tonic-gate	char *argv[];
14547c478bdstevel@tonic-gate{
14557c478bdstevel@tonic-gate	register char *arg;
14567c478bdstevel@tonic-gate	char *buf = NULL;
14577c478bdstevel@tonic-gate
14587c478bdstevel@tonic-gate	if (argc > 2)
14597c478bdstevel@tonic-gate		arg = argv[1];
14607c478bdstevel@tonic-gate	else {
14617c478bdstevel@tonic-gate		(void) printf("new escape character: ");
14627c478bdstevel@tonic-gate		if (GetString(&buf, NULL, stdin) == NULL) {
14637c478bdstevel@tonic-gate			if (!feof(stdin)) {
14647c478bdstevel@tonic-gate				perror("can't set escape character");
14657c478bdstevel@tonic-gate				goto setescape_exit;
14667c478bdstevel@tonic-gate			}
14677c478bdstevel@tonic-gate		}
14687c478bdstevel@tonic-gate		arg = buf;
14697c478bdstevel@tonic-gate	}
14707c478bdstevel@tonic-gate	/* we place no limitations on what escape can be. */
14717c478bdstevel@tonic-gate	escape = arg[0];
14727c478bdstevel@tonic-gate	(void) printf("Escape character is '%s'.\n", esc_control(escape));
14737c478bdstevel@tonic-gate	(void) fflush(stdout);
14747c478bdstevel@tonic-gatesetescape_exit:
14757c478bdstevel@tonic-gate	Free(&buf);
14767c478bdstevel@tonic-gate	return (1);
14777c478bdstevel@tonic-gate}
14787c478bdstevel@tonic-gate
14797c478bdstevel@tonic-gate/*ARGSUSED*/
14807c478bdstevel@tonic-gatestatic int
14817c478bdstevel@tonic-gatetogcrmod(argc, argv)
14827c478bdstevel@tonic-gate	int argc;
14837c478bdstevel@tonic-gate	char *argv[];
14847c478bdstevel@tonic-gate{
14857c478bdstevel@tonic-gate	crmod = !crmod;
14867c478bdstevel@tonic-gate	(void) printf(
14877c478bdstevel@tonic-gate	    "%s map carriage return on output.\n", crmod ? "Will" : "Won't");
14887c478bdstevel@tonic-gate	(void) fflush(stdout);
14897c478bdstevel@tonic-gate	return (1);
14907c478bdstevel@tonic-gate}
14917c478bdstevel@tonic-gate
14927c478bdstevel@tonic-gate/*ARGSUSED*/
14937c478bdstevel@tonic-gatestatic int
14947c478bdstevel@tonic-gatesuspend(argc, argv)
14957c478bdstevel@tonic-gate	int argc;
14967c478bdstevel@tonic-gate	char *argv[];
14977c478bdstevel@tonic-gate{
14987c478bdstevel@tonic-gate	setcommandmode();
14997c478bdstevel@tonic-gate	{
15007c478bdstevel@tonic-gate		unsigned short oldrows, oldcols, newrows, newcols;
15017c478bdstevel@tonic-gate		int err;
15027c478bdstevel@tonic-gate
15037c478bdstevel@tonic-gate		err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
15047c478bdstevel@tonic-gate		(void) kill(0, SIGTSTP);
15057c478bdstevel@tonic-gate		/*
15067c478bdstevel@tonic-gate		 * If we didn't get the window size before the SUSPEND, but we
15077c478bdstevel@tonic-gate		 * can get them now (?), then send the NAWS to make sure that
15087c478bdstevel@tonic-gate		 * we are set up for the right window size.
15097c478bdstevel@tonic-gate		 */
15107c478bdstevel@tonic-gate		if (TerminalWindowSize(&newrows, &newcols) && connected &&
15117c478bdstevel@tonic-gate		    (err || ((oldrows != newrows) || (oldcols != newcols)))) {
15127c478bdstevel@tonic-gate			sendnaws();
15137c478bdstevel@tonic-gate		}
15147c478bdstevel@tonic-gate	}
15157c478bdstevel@tonic-gate	/* reget parameters in case they were changed */
15167c478bdstevel@tonic-gate	TerminalSaveState();
15177c478bdstevel@tonic-gate	setconnmode(0);
15187c478bdstevel@tonic-gate	return (1);
15197c478bdstevel@tonic-gate}
15207c478bdstevel@tonic-gate
15217c478bdstevel@tonic-gate/*ARGSUSED*/
15227c478bdstevel@tonic-gatestatic int
15237c478bdstevel@tonic-gateshell(argc, argv)
15247c478bdstevel@tonic-gate	int argc;
15257c478bdstevel@tonic-gate	char *argv[];
15267c478bdstevel@tonic-gate{
15277c478bdstevel@tonic-gate	unsigned short oldrows, oldcols, newrows, newcols;
15287c478bdstevel@tonic-gate	int err;
15297c478bdstevel@tonic-gate
15307c478bdstevel@tonic-gate	setcommandmode();
15317c478bdstevel@tonic-gate
15327c478bdstevel@tonic-gate	err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
15337c478bdstevel@tonic-gate	switch (vfork()) {
15347c478bdstevel@tonic-gate	case -1:
15357c478bdstevel@tonic-gate		perror("Fork failed\n");
15367c478bdstevel@tonic-gate		break;
15377c478bdstevel@tonic-gate
15387c478bdstevel@tonic-gate	case 0:
15397c478bdstevel@tonic-gate		{
15407c478bdstevel@tonic-gate		/*
15417c478bdstevel@tonic-gate		 * Fire up the shell in the child.
15427c478bdstevel@tonic-gate		 */
15437c478bdstevel@tonic-gate		register char *shellp, *shellname;
15447c478bdstevel@tonic-gate
15457c478bdstevel@tonic-gate		shellp = getenv("SHELL");
15467c478bdstevel@tonic-gate		if (shellp == NULL)
15477c478bdstevel@tonic-gate			shellp = "/bin/sh";
15487c478bdstevel@tonic-gate		if ((shellname = strrchr(shellp, '/')) == 0)
15497c478bdstevel@tonic-gate			shellname = shellp;
15507c478bdstevel@tonic-gate		else
15517c478bdstevel@tonic-gate			shellname++;
15527c478bdstevel@tonic-gate		if (argc > 1)
15537c478bdstevel@tonic-gate			(void) execl(shellp, shellname, "-c", argv[1], 0);
15547c478bdstevel@tonic-gate		else
15557c478bdstevel@tonic-gate			(void) execl(shellp, shellname, 0);
15567c478bdstevel@tonic-gate		perror("Execl");
15577c478bdstevel@tonic-gate		_exit(EXIT_FAILURE);
15587c478bdstevel@tonic-gate		}
15597c478bdstevel@tonic-gate	default:
15607c478bdstevel@tonic-gate		(void) wait((int *)0);	/* Wait for the shell to complete */
15617c478bdstevel@tonic-gate
15627c478bdstevel@tonic-gate		if (TerminalWindowSize(&newrows, &newcols) && connected &&
15637c478bdstevel@tonic-gate		(err || ((oldrows != newrows) || (oldcols != newcols)))) {
15647c478bdstevel@tonic-gate			sendnaws();
15657c478bdstevel@tonic-gate		}
15667c478bdstevel@tonic-gate		break;
15677c478bdstevel@tonic-gate	}
15687c478bdstevel@tonic-gate	return (1);
15697c478bdstevel@tonic-gate}
15707c478bdstevel@tonic-gate
15717c478bdstevel@tonic-gatestatic int
15727c478bdstevel@tonic-gatebye(argc, argv)
15737c478bdstevel@tonic-gate	int  argc;	/* Number of arguments */
15747c478bdstevel@tonic-gate	char *argv[];	/* arguments */
15757c478bdstevel@tonic-gate{
15767c478bdstevel@tonic-gate	extern int resettermname;
15777c478bdstevel@tonic-gate
15787c478bdstevel@tonic-gate	if (connected) {
15797c478bdstevel@tonic-gate		(void) shutdown(net, 2);
15807c478bdstevel@tonic-gate		(void) printf("Connection to %.*s closed.\n", MAXHOSTNAMELEN,
15817c478bdstevel@tonic-gate		    hostname);
15827c478bdstevel@tonic-gate		Close(&net);
15837c478bdstevel@tonic-gate		connected = 0;
15847c478bdstevel@tonic-gate		resettermname = 1;
15857c478bdstevel@tonic-gate		/* reset options */
15867c478bdstevel@tonic-gate		(void) tninit();
15877c478bdstevel@tonic-gate	}
15887c478bdstevel@tonic-gate	if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
15897c478bdstevel@tonic-gate		longjmp(toplevel, 1);
15907c478bdstevel@tonic-gate		/* NOTREACHED */
15917c478bdstevel@tonic-gate	}
15927c478bdstevel@tonic-gate	return (1);			/* Keep lint, etc., happy */
15937c478bdstevel@tonic-gate}
15947c478bdstevel@tonic-gate
15957c478bdstevel@tonic-gate/*VARARGS*/
15967c478bdstevel@tonic-gateint
15977c478bdstevel@tonic-gatequit()
15987c478bdstevel@tonic-gate{
15997c478bdstevel@tonic-gate	(void) call(3, bye, "bye", "fromquit");
16007c478bdstevel@tonic-gate	Exit(EXIT_SUCCESS);
16017c478bdstevel@tonic-gate	/*NOTREACHED*/
16027c478bdstevel@tonic-gate	return (1);
16037c478bdstevel@tonic-gate}
16047c478bdstevel@tonic-gate
16057c478bdstevel@tonic-gate/*ARGSUSED*/
16067c478bdstevel@tonic-gatestatic int
16077c478bdstevel@tonic-gatelogout(argc, argv)
16087c478bdstevel@tonic-gate	int argc;
16097c478bdstevel@tonic-gate	char *argv[];
16107c478bdstevel@tonic-gate{
16117c478bdstevel@tonic-gate	send_do(TELOPT_LOGOUT, 1);
16127c478bdstevel@tonic-gate	(void) netflush();
16137c478bdstevel@tonic-gate	return (1);
16147c478bdstevel@tonic-gate}
16157c478bdstevel@tonic-gate
16167c478bdstevel@tonic-gate
16177c478bdstevel@tonic-gate/*
16187c478bdstevel@tonic-gate * The SLC command.
16197c478bdstevel@tonic-gate */
16207c478bdstevel@tonic-gate
16217c478bdstevel@tonic-gatestruct slclist {
16227c478bdstevel@tonic-gate	char	*name;
16237c478bdstevel@tonic-gate	char	*help;
16247c478bdstevel@tonic-gate	void	(*handler)();
16257c478bdstevel@tonic-gate	int	arg;
16267c478bdstevel@tonic-gate};
16277c478bdstevel@tonic-gate
16287c478bdstevel@tonic-gatestatic void slc_help();
16297c478bdstevel@tonic-gate
16307c478bdstevel@tonic-gatestatic struct slclist SlcList[] = {
16317c478bdstevel@tonic-gate	{ "export",	"Use local special character definitions",
16327c478bdstevel@tonic-gate						slc_mode_export,	0 },
16337c478bdstevel@tonic-gate	{ "import",	"Use remote special character definitions",
16347c478bdstevel@tonic-gate						slc_mode_import,	1 },
16357c478bdstevel@tonic-gate	{ "check",	"Verify remote special character definitions",
16367c478bdstevel@tonic-gate						slc_mode_import,	0 },
16377c478bdstevel@tonic-gate	{ "help",	0,			slc_help,		0 },
16387c478bdstevel@tonic-gate	{ "?",	"Print help information",	slc_help,		0 },
16397c478bdstevel@tonic-gate	{ 0 },
16407c478bdstevel@tonic-gate};
16417c478bdstevel@tonic-gate
16427c478bdstevel@tonic-gatestatic void
16437c478bdstevel@tonic-gateslc_help()
16447c478bdstevel@tonic-gate{
16457c478bdstevel@tonic-gate	struct slclist *c;
16467c478bdstevel@tonic-gate
16477c478bdstevel@tonic-gate	for (c = SlcList; c->name; c++) {
16487c478bdstevel@tonic-gate		if (c->help) {
16497c478bdstevel@tonic-gate			if (*c->help)
16507c478bdstevel@tonic-gate				(void) printf("%-15s %s\n", c->name, c->help);
16517c478bdstevel@tonic-gate			else
16527c478bdstevel@tonic-gate				(void) printf("\n");
16537c478bdstevel@tonic-gate		}
16547c478bdstevel@tonic-gate	}
16557c478bdstevel@tonic-gate}
16567c478bdstevel@tonic-gate
16577c478bdstevel@tonic-gatestatic struct slclist *
16587c478bdstevel@tonic-gategetslc(name)
16597c478bdstevel@tonic-gate	char *name;
16607c478bdstevel@tonic-gate{
16617c478bdstevel@tonic-gate	return ((struct slclist *)
16627c478bdstevel@tonic-gate	    genget(name, (char **)SlcList, sizeof (struct slclist)));
16637c478bdstevel@tonic-gate}
16647c478bdstevel@tonic-gate
1665740638cbwstatic int
16667c478bdstevel@tonic-gateslccmd(argc, argv)
16677c478bdstevel@tonic-gate	int  argc;
16687c478bdstevel@tonic-gate	char *argv[];
16697c478bdstevel@tonic-gate{
16707c478bdstevel@tonic-gate	struct slclist *c;
16717c478bdstevel@tonic-gate
16727c478bdstevel@tonic-gate	if (argc != 2) {
16737c478bdstevel@tonic-gate		(void) fprintf(stderr,
16747c478bdstevel@tonic-gate		    "Need an argument to 'slc' command.  'slc ?' for help.\n");
16757c478bdstevel@tonic-gate		return (0);
16767c478bdstevel@tonic-gate	}
16777c478bdstevel@tonic-gate	c = getslc(argv[1]);
16787c478bdstevel@tonic-gate	if (c == 0) {
16797c478bdstevel@tonic-gate		(void) fprintf(stderr,
16807c478bdstevel@tonic-gate		    "'%s': unknown argument ('slc ?' for help).\n",
16817c478bdstevel@tonic-gate		    argv[1]);
16827c478bdstevel@tonic-gate		return (0);
16837c478bdstevel@tonic-gate	}
16847c478bdstevel@tonic-gate	if (Ambiguous(c)) {
16857c478bdstevel@tonic-gate		(void) fprintf(stderr,
16867c478bdstevel@tonic-gate		    "'%s': ambiguous argument ('slc ?' for help).\n", argv[1]);
16877c478bdstevel@tonic-gate		return (0);
16887c478bdstevel@tonic-gate	}
16897c478bdstevel@tonic-gate	(*c->handler)(c->arg);
16907c478bdstevel@tonic-gate	slcstate();
16917c478bdstevel@tonic-gate	return (1);
16927c478bdstevel@tonic-gate}
16937c478bdstevel@tonic-gate
16947c478bdstevel@tonic-gate/*
16957c478bdstevel@tonic-gate * The ENVIRON command.
16967c478bdstevel@tonic-gate */
16977c478bdstevel@tonic-gate
16987c478bdstevel@tonic-gatestruct envlist {
16997c478bdstevel@tonic-gate	char	*name;
17007c478bdstevel@tonic-gate	char	*help;
17017c478bdstevel@tonic-gate	void	(*handler)();
17027c478bdstevel@tonic-gate	int	narg;
17037c478bdstevel@tonic-gate};
17047c478bdstevel@tonic-gate
17057c478bdstevel@tonic-gatestatic struct env_lst *env_define(unsigned char *, unsigned char *);
17067c478bdstevel@tonic-gatestatic void env_undefine(unsigned char *);
17077c478bdstevel@tonic-gatestatic void env_export(unsigned char *);
17087c478bdstevel@tonic-gatestatic void env_unexport(unsigned char *);
17097c478bdstevel@tonic-gatestatic void env_send(unsigned char *);
17107c478bdstevel@tonic-gate#if defined(OLD_ENVIRON) && defined(ENV_HACK)
17117c478bdstevel@tonic-gatestatic void env_varval(unsigned char *);
17127c478bdstevel@tonic-gate#endif
17137c478bdstevel@tonic-gatestatic void env_list(void);
17147c478bdstevel@tonic-gate
17157c478bdstevel@tonic-gatestatic void env_help(void);
17167c478bdstevel@tonic-gate
17177c478bdstevel@tonic-gatestatic struct envlist EnvList[] = {
17187c478bdstevel@tonic-gate	{ "define",	"Define an environment variable",
17197c478bdstevel@tonic-gate						(void (*)())env_define,	2 },
17207c478bdstevel@tonic-gate	{ "undefine", "Undefine an environment variable",
17217c478bdstevel@tonic-gate						env_undefine,	1 },
17227c478bdstevel@tonic-gate	{ "export",	"Mark an environment variable for automatic export",
17237c478bdstevel@tonic-gate						env_export,	1 },
17247c478bdstevel@tonic-gate	{ "unexport", "Don't mark an environment variable for automatic export",
17257c478bdstevel@tonic-gate						env_unexport,	1 },
17267c478bdstevel@tonic-gate	{ "send",	"Send an environment variable", env_send,	1 },
17277c478bdstevel@tonic-gate	{ "list",	"List the current environment variables",
17287c478bdstevel@tonic-gate						env_list,	0 },
17297c478bdstevel@tonic-gate#if defined(OLD_ENVIRON) && defined(ENV_HACK)
17307c478bdstevel@tonic-gate	{ "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
17317c478bdstevel@tonic-gate						env_varval,    1 },
17327c478bdstevel@tonic-gate#endif
17337c478bdstevel@tonic-gate	{ "help",	0,			env_help,		0 },
17347c478bdstevel@tonic-gate	{ "?",	"Print help information",	env_help,		0 },
17357c478bdstevel@tonic-gate	{ 0 },
17367c478bdstevel@tonic-gate};
17377c478bdstevel@tonic-gate
17387c478bdstevel@tonic-gatestatic void
17397c478bdstevel@tonic-gateenv_help()
17407c478bdstevel@tonic-gate{
17417c478bdstevel@tonic-gate	struct envlist *c;
17427c478bdstevel@tonic-gate
17437c478bdstevel@tonic-gate	for (c = EnvList; c->name; c++) {
17447c478bdstevel@tonic-gate		if (c->help) {
17457c478bdstevel@tonic-gate			if (*c->help)
17467c478bdstevel@tonic-gate				(void) printf("%-15s %s\n", c->name, c->help);
17477c478bdstevel@tonic-gate			else
17487c478bdstevel@tonic-gate				(void) printf("\n");
17497c478bdstevel@tonic-gate		}
17507c478bdstevel@tonic-gate	}
17517c478bdstevel@tonic-gate}
17527c478bdstevel@tonic-gate
17537c478bdstevel@tonic-gatestatic struct envlist *
17547c478bdstevel@tonic-gategetenvcmd(name)
17557c478bdstevel@tonic-gate    char *name;
17567c478bdstevel@tonic-gate{
17577c478bdstevel@tonic-gate	return ((struct envlist *)
17587c478bdstevel@tonic-gate	    genget(name, (char **)EnvList, sizeof (struct envlist)));
17597c478bdstevel@tonic-gate}
17607c478bdstevel@tonic-gate
17617c478bdstevel@tonic-gatestatic int
17627c478bdstevel@tonic-gateenv_cmd(argc, argv)
17637c478bdstevel@tonic-gate	int  argc;
17647c478bdstevel@tonic-gate	char *argv[];
17657c478bdstevel@tonic-gate{
17667c478bdstevel@tonic-gate	struct envlist *c;
17677c478bdstevel@tonic-gate
17687c478bdstevel@tonic-gate	if (argc < 2) {
17697c478bdstevel@tonic-gate		(void) fprintf(stderr,
17707c478bdstevel@tonic-gate		    "Need an argument to 'environ' command.  "
17717c478bdstevel@tonic-gate		    "'environ ?' for help.\n");
17727c478bdstevel@tonic-gate		return (0);
17737c478bdstevel@tonic-gate	}
17747c478bdstevel@tonic-gate	c = getenvcmd(argv[1]);
17757c478bdstevel@tonic-gate	if (c == 0) {
17767c478bdstevel@tonic-gate		(void) fprintf(stderr, "'%s': unknown argument "
17777c478bdstevel@tonic-gate		    "('environ ?' for help).\n", argv[1]);
17787c478bdstevel@tonic-gate		return (0);
17797c478bdstevel@tonic-gate	}
17807c478bdstevel@tonic-gate	if (Ambiguous(c)) {
17817c478bdstevel@tonic-gate		(void) fprintf(stderr, "'%s': ambiguous argument "
17827c478bdstevel@tonic-gate		    "('environ ?' for help).\n", argv[1]);
17837c478bdstevel@tonic-gate		return (0);
17847c478bdstevel@tonic-gate	}
17857c478bdstevel@tonic-gate	if (c->narg + 2 != argc) {
17867c478bdstevel@tonic-gate		(void) fprintf(stderr,
17877c478bdstevel@tonic-gate		    "Need %s%d argument%s to 'environ %s' command.  "
17887c478bdstevel@tonic-gate		    "'environ ?' for help.\n",
17897c478bdstevel@tonic-gate		    c->narg + 2 < argc ? "only " : "",
17907c478bdstevel@tonic-gate		    c->narg, c->narg == 1 ? "" : "s", c->name);
17917c478bdstevel@tonic-gate		return (0);
17927c478bdstevel@tonic-gate	}
17937c478bdstevel@tonic-gate	(*c->handler)(argv[2], argv[3]);
17947c478bdstevel@tonic-gate	return (1);
17957c478bdstevel@tonic-gate}
17967c478bdstevel@tonic-gate
17977c478bdstevel@tonic-gatestruct env_lst {
17987c478bdstevel@tonic-gate	struct env_lst *next;	/* pointer to next structure */
17997c478bdstevel@tonic-gate	struct env_lst *prev;	/* pointer to previous structure */
18007c478bdstevel@tonic-gate	unsigned char *var;	/* pointer to variable name */
18017c478bdstevel@tonic-gate	unsigned char *value;	/* pointer to variable value */
18027c478bdstevel@tonic-gate	int export;		/* 1 -> export with default list of variables */
18037c478bdstevel@tonic-gate	int welldefined;	/* A well defined variable */
18047c478bdstevel@tonic-gate};
18057c478bdstevel@tonic-gate
18067c478bdstevel@tonic-gatestatic struct env_lst envlisthead;
18077c478bdstevel@tonic-gate
18087c478bdstevel@tonic-gatestatic struct env_lst *
18097c478bdstevel@tonic-gateenv_find(var)
18107c478bdstevel@tonic-gate	unsigned char *var;
18117c478bdstevel@tonic-gate{
18127c478bdstevel@tonic-gate	register struct env_lst *ep;
18137c478bdstevel@tonic-gate
18147c478bdstevel@tonic-gate	for (ep = envlisthead.next; ep; ep = ep->next) {
18157c478bdstevel@tonic-gate		if (strcmp((char *)ep->var, (char *)var) == 0)
18167c478bdstevel@tonic-gate			return (ep);
18177c478bdstevel@tonic-gate	}
18187c478bdstevel@tonic-gate	return (NULL);
18197c478bdstevel@tonic-gate}
18207c478bdstevel@tonic-gate
18217c478bdstevel@tonic-gateint
18227c478bdstevel@tonic-gateenv_init()
18237c478bdstevel@tonic-gate{
18247c478bdstevel@tonic-gate#ifdef	lint
18257c478bdstevel@tonic-gate	char **environ = NULL;
18267c478bdstevel@tonic-gate#else	/* lint */
18277c478bdstevel@tonic-gate	extern char **environ;
18287c478bdstevel@tonic-gate#endif	/* lint */
18297c478bdstevel@tonic-gate	char **epp, *cp;
18307c478bdstevel@tonic-gate	struct env_lst *ep;
18317c478bdstevel@tonic-gate
18327c478bdstevel@tonic-gate	for (epp = environ; *epp; epp++) {
18337c478bdstevel@tonic-gate		if (cp = strchr(*epp, '=')) {
18347c478bdstevel@tonic-gate			*cp = '\0';
18357c478bdstevel@tonic-gate
18367c478bdstevel@tonic-gate			ep = env_define((unsigned char *)*epp,
18377c478bdstevel@tonic-gate					(unsigned char *)cp+1);
18387c478bdstevel@tonic-gate			if (ep == NULL)
18397c478bdstevel@tonic-gate				return (0);
18407c478bdstevel@tonic-gate			ep->export = 0;
18417c478bdstevel@tonic-gate			*cp = '=';
18427c478bdstevel@tonic-gate		}
18437c478bdstevel@tonic-gate	}
18447c478bdstevel@tonic-gate	/*
18457c478bdstevel@tonic-gate	 * Special case for DISPLAY variable.  If it is ":0.0" or
18467c478bdstevel@tonic-gate	 * "unix:0.0", we have to get rid of "unix" and insert our
18477c478bdstevel@tonic-gate	 * hostname.
18487c478bdstevel@tonic-gate	 */
18497c478bdstevel@tonic-gate	if (((ep = env_find((uchar_t *)"DISPLAY")) != NULL) &&
18507c478bdstevel@tonic-gate	    ((*ep->value == ':') ||
18517c478bdstevel@tonic-gate	    (strncmp((char *)ep->value, "unix:", 5) == 0))) {
18527c478bdstevel@tonic-gate		char hbuf[MAXHOSTNAMELEN];
18537c478bdstevel@tonic-gate		char *cp2 = strchr((char *)ep->value, ':');
18547c478bdstevel@tonic-gate
18557c478bdstevel@tonic-gate		if (gethostname(hbuf, MAXHOSTNAMELEN) == -1) {
18567c478bdstevel@tonic-gate			perror("telnet: cannot get hostname");
18577c478bdstevel@tonic-gate			return (0);
18587c478bdstevel@tonic-gate		}
18597c478bdstevel@tonic-gate		hbuf[MAXHOSTNAMELEN-1] = '\0';
18607c478bdstevel@tonic-gate		cp = malloc(strlen(hbuf) + strlen(cp2) + 1);
18617c478bdstevel@tonic-gate		if (cp == NULL) {
18627c478bdstevel@tonic-gate			perror("telnet: cannot define DISPLAY variable");
18637c478bdstevel@tonic-gate			return (0);
18647c478bdstevel@tonic-gate		}
18657c478bdstevel@tonic-gate		(void) sprintf((char *)cp, "%s%s", hbuf, cp2);
18667c478bdstevel@tonic-gate		free(ep->value);
18677c478bdstevel@tonic-gate		ep->value = (unsigned char *)cp;
18687c478bdstevel@tonic-gate	}
18697c478bdstevel@tonic-gate	/*
18707c478bdstevel@tonic-gate	 * If LOGNAME is defined, but USER is not, then add
18717c478bdstevel@tonic-gate	 * USER with the value from LOGNAME.  We do this because the "accepted
18727c478bdstevel@tonic-gate	 * practice" is to always pass USER on the wire, but SVR4 uses
18737c478bdstevel@tonic-gate	 * LOGNAME by default.
18747c478bdstevel@tonic-gate	 */
18757c478bdstevel@tonic-gate	if ((ep = env_find((uchar_t *)"LOGNAME")) != NULL &&
18767c478bdstevel@tonic-gate		env_find((uchar_t *)"USER") == NULL) {
18777c478bdstevel@tonic-gate		if (env_define((unsigned char *)"USER", ep->value) != NULL)
18787c478bdstevel@tonic-gate			env_unexport((unsigned char *)"USER");
18797c478bdstevel@tonic-gate	}
18807c478bdstevel@tonic-gate	env_export((unsigned char *)"DISPLAY");
18817c478bdstevel@tonic-gate	env_export((unsigned char *)"PRINTER");
18827c478bdstevel@tonic-gate
18837c478bdstevel@tonic-gate	return (1);
18847c478bdstevel@tonic-gate}
18857c478bdstevel@tonic-gate
18867c478bdstevel@tonic-gatestatic struct env_lst *
18877c478bdstevel@tonic-gateenv_define(var, value)
18887c478bdstevel@tonic-gate	unsigned char *var, *value;
18897c478bdstevel@tonic-gate{
18907c478bdstevel@tonic-gate	unsigned char *tmp_value;
18917c478bdstevel@tonic-gate	unsigned char *tmp_var;
18927c478bdstevel@tonic-gate	struct env_lst *ep;
18937c478bdstevel@tonic-gate
18947c478bdstevel@tonic-gate	/*
18957c478bdstevel@tonic-gate	 * Allocate copies of arguments first, to make cleanup easier
18967c478bdstevel@tonic-gate	 * in the case of allocation errors.
18977c478bdstevel@tonic-gate	 */
18987c478bdstevel@tonic-gate	tmp_var = (unsigned char *)strdup((char *)var);
18997c478bdstevel@tonic-gate	if (tmp_var == NULL) {
19007c478bdstevel@tonic-gate		perror("telnet: can't copy environment variable name");
19017c478bdstevel@tonic-gate		return (NULL);
19027c478bdstevel@tonic-gate	}
19037c478bdstevel@tonic-gate
19047c478bdstevel@tonic-gate	tmp_value = (unsigned char *)strdup((char *)value);
19057c478bdstevel@tonic-gate	if (tmp_value == NULL) {
19067c478bdstevel@tonic-gate		free(tmp_var);
19077c478bdstevel@tonic-gate		perror("telnet: can't copy environment variable value");
19087c478bdstevel@tonic-gate		return (NULL);
19097c478bdstevel@tonic-gate	}
19107c478bdstevel@tonic-gate
19117c478bdstevel@tonic-gate	if (ep = env_find(var)) {
19127c478bdstevel@tonic-gate		if (ep->var)
19137c478bdstevel@tonic-gate			free(ep->var);
19147c478bdstevel@tonic-gate		if (ep->value)
19157c478bdstevel@tonic-gate			free(ep->value);
19167c478bdstevel@tonic-gate	} else {
19177c478bdstevel@tonic-gate		ep = malloc(sizeof (struct env_lst));
19187c478bdstevel@tonic-gate		if (ep == NULL) {
19197c478bdstevel@tonic-gate			perror("telnet: can't define environment variable");
19207c478bdstevel@tonic-gate			free(tmp_var);
19217c478bdstevel@tonic-gate			free(tmp_value);
19227c478bdstevel@tonic-gate			return (NULL);
19237c478bdstevel@tonic-gate		}
19247c478bdstevel@tonic-gate
19257c478bdstevel@tonic-gate		ep->next = envlisthead.next;
19267c478bdstevel@tonic-gate		envlisthead.next = ep;
19277c478bdstevel@tonic-gate		ep->prev = &envlisthead;
19287c478bdstevel@tonic-gate		if (ep->next)
19297c478bdstevel@tonic-gate			ep->next->prev = ep;
19307c478bdstevel@tonic-gate	}
19317c478bdstevel@tonic-gate	ep->welldefined = opt_welldefined((char *)var);
19327c478bdstevel@tonic-gate	ep->export = 1;
19337c478bdstevel@tonic-gate	ep->var = tmp_var;
19347c478bdstevel@tonic-gate	ep->value = tmp_value;
19357c478bdstevel@tonic-gate
19367c478bdstevel@tonic-gate	return (ep);
19377c478bdstevel@tonic-gate}
19387c478bdstevel@tonic-gate
19397c478bdstevel@tonic-gatestatic void
19407c478bdstevel@tonic-gateenv_undefine(var)
19417c478bdstevel@tonic-gate	unsigned char *var;
19427c478bdstevel@tonic-gate{
19437c478bdstevel@tonic-gate	register struct env_lst *ep;
19447c478bdstevel@tonic-gate
19457c478bdstevel@tonic-gate	if (ep = env_find(var)) {
19467c478bdstevel@tonic-gate		ep->prev->next = ep->next;
19477c478bdstevel@tonic-gate		if (ep->next)
19487c478bdstevel@tonic-gate			ep->next->prev = ep->prev;
19497c478bdstevel@tonic-gate		if (ep->var)
19507c478bdstevel@tonic-gate			free(ep->var);
19517c478bdstevel@tonic-gate		if (ep->value)
19527c478bdstevel@tonic-gate			free(ep->value);
19537c478bdstevel@tonic-gate		free(ep);
19547c478bdstevel@tonic-gate	}
19557c478bdstevel@tonic-gate}
19567c478bdstevel@tonic-gate
19577c478bdstevel@tonic-gatestatic void
19587c478bdstevel@tonic-gateenv_export(var)
19597c478bdstevel@tonic-gate	unsigned char *var;
19607c478bdstevel@tonic-gate{
19617c478bdstevel@tonic-gate	register struct env_lst *ep;
19627c478bdstevel@tonic-gate
19637c478bdstevel@tonic-gate	if (ep = env_find(var))
19647c478bdstevel@tonic-gate		ep->export = 1;
19657c478bdstevel@tonic-gate}
19667c478bdstevel@tonic-gate
19677c478bdstevel@tonic-gatestatic void
19687c478bdstevel@tonic-gateenv_unexport(var)
19697c478bdstevel@tonic-gate	unsigned char *var;
19707c478bdstevel@tonic-gate{
19717c478bdstevel@tonic-gate	register struct env_lst *ep;
19727c478bdstevel@tonic-gate
19737c478bdstevel@tonic-gate	if (ep = env_find(var))
19747c478bdstevel@tonic-gate		ep->export = 0;
19757c478bdstevel@tonic-gate}
19767c478bdstevel@tonic-gate
19777c478bdstevel@tonic-gatestatic void
19787c478bdstevel@tonic-gateenv_send(var)
19797c478bdstevel@tonic-gate	unsigned char *var;
19807c478bdstevel@tonic-gate{
19817c478bdstevel@tonic-gate	register struct env_lst *ep;
19827c478bdstevel@tonic-gate
19837c478bdstevel@tonic-gate	if (my_state_is_wont(TELOPT_NEW_ENVIRON)
19847c478bdstevel@tonic-gate#ifdef	OLD_ENVIRON
19857c478bdstevel@tonic-gate	    /* old style */ && my_state_is_wont(TELOPT_OLD_ENVIRON)
19867c478bdstevel@tonic-gate#endif
19877c478bdstevel@tonic-gate		/* no environ */) {
19887c478bdstevel@tonic-gate		(void) fprintf(stderr,
19897c478bdstevel@tonic-gate		    "Cannot send '%s': Telnet ENVIRON option not enabled\n",
19907c478bdstevel@tonic-gate									var);
19917c478bdstevel@tonic-gate		return;
19927c478bdstevel@tonic-gate	}
19937c478bdstevel@tonic-gate	ep = env_find(var);
19947c478bdstevel@tonic-gate	if (ep == 0) {
19957c478bdstevel@tonic-gate		(void) fprintf(stderr,
19967c478bdstevel@tonic-gate		    "Cannot send '%s': variable not defined\n", var);
19977c478bdstevel@tonic-gate		return;
19987c478bdstevel@tonic-gate	}
19997c478bdstevel@tonic-gate	env_opt_start_info();
20007c478bdstevel@tonic-gate	env_opt_add(ep->var);
20017c478bdstevel@tonic-gate	env_opt_end(0);
20027c478bdstevel@tonic-gate}
20037c478bdstevel@tonic-gate
20047c478bdstevel@tonic-gatestatic void
20057c478bdstevel@tonic-gateenv_list()
20067c478bdstevel@tonic-gate{
20077c478bdstevel@tonic-gate	register struct env_lst *ep;
20087c478bdstevel@tonic-gate
20097c478bdstevel@tonic-gate	for (ep = envlisthead.next; ep; ep = ep->next) {
20107c478bdstevel@tonic-gate		(void) printf("%c %-20s %s\n", ep->export ? '*' : ' ',
20117c478bdstevel@tonic-gate					ep->var, ep->value);
20127c478bdstevel@tonic-gate	}
20137c478bdstevel@tonic-gate}
20147c478bdstevel@tonic-gate
20157c478bdstevel@tonic-gate	unsigned char *
20167c478bdstevel@tonic-gateenv_default(init, welldefined)
20177c478bdstevel@tonic-gate	int init;
20187c478bdstevel@tonic-gate{
20197c478bdstevel@tonic-gate	static struct env_lst *nep = NULL;
20207c478bdstevel@tonic-gate
20217c478bdstevel@tonic-gate	if (init) {
20227c478bdstevel@tonic-gate		/* return value is not used */
20237c478bdstevel@tonic-gate		nep = &envlisthead;
20247c478bdstevel@tonic-gate		return (NULL);
20257c478bdstevel@tonic-gate	}
20267c478bdstevel@tonic-gate	if (nep) {
20277c478bdstevel@tonic-gate		while ((nep = nep->next) != NULL) {
20287c478bdstevel@tonic-gate			if (nep->export && (nep->welldefined == welldefined))
20297c478bdstevel@tonic-gate				return (nep->var);
20307c478bdstevel@tonic-gate		}
20317c478bdstevel@tonic-gate	}
20327c478bdstevel@tonic-gate	return (NULL);
20337c478bdstevel@tonic-gate}
20347c478bdstevel@tonic-gate
20357c478bdstevel@tonic-gate	unsigned char *
20367c478bdstevel@tonic-gateenv_getvalue(var)
20377c478bdstevel@tonic-gate	unsigned char *var;
20387c478bdstevel@tonic-gate{
20397c478bdstevel@tonic-gate	register struct env_lst *ep;
20407c478bdstevel@tonic-gate
20417c478bdstevel@tonic-gate	if (ep = env_find(var))
20427c478bdstevel@tonic-gate		return (ep->value);
20437c478bdstevel@tonic-gate	return (NULL);
20447c478bdstevel@tonic-gate}
20457c478bdstevel@tonic-gate
20467c478bdstevel@tonic-gate#if defined(OLD_ENVIRON) && defined(ENV_HACK)
20477c478bdstevel@tonic-gatestatic void
20487c478bdstevel@tonic-gateenv_varval(what)
20497c478bdstevel@tonic-gate	unsigned char *what;
20507c478bdstevel@tonic-gate{
20517c478bdstevel@tonic-gate	extern int old_env_var, old_env_value, env_auto;
20527c478bdstevel@tonic-gate	int len = strlen((char *)what);
20537c478bdstevel@tonic-gate
20547c478bdstevel@tonic-gate	if (len == 0)
20557c478bdstevel@tonic-gate		goto unknown;
20567c478bdstevel@tonic-gate
20577c478bdstevel@tonic-gate	if (strncasecmp((char *)what, "status", len) == 0) {
20587c478bdstevel@tonic-gate		if (env_auto)
20597c478bdstevel@tonic-gate			(void) printf("%s%s", "VAR and VALUE are/will be ",
20607c478bdstevel@tonic-gate					"determined automatically\n");
20617c478bdstevel@tonic-gate		if (old_env_var == OLD_ENV_VAR)
20627c478bdstevel@tonic-gate			(void) printf(
20637c478bdstevel@tonic-gate			    "VAR and VALUE set to correct definitions\n");
20647c478bdstevel@tonic-gate		else
20657c478bdstevel@tonic-gate			(void) printf(
20667c478bdstevel@tonic-gate			    "VAR and VALUE definitions are reversed\n");
20677c478bdstevel@tonic-gate	} else if (strncasecmp((char *)what, "auto", len) == 0) {
20687c478bdstevel@tonic-gate		env_auto = 1;
20697c478bdstevel@tonic-gate		old_env_var = OLD_ENV_VALUE;
20707c478bdstevel@tonic-gate		old_env_value = OLD_ENV_VAR;
20717c478bdstevel@tonic-gate	} else if (strncasecmp((char *)what, "right", len) == 0) {
20727c478bdstevel@tonic-gate		env_auto = 0;
20737c478bdstevel@tonic-gate		old_env_var = OLD_ENV_VAR;
20747c478bdstevel@tonic-gate		old_env_value = OLD_ENV_VALUE;
20757c478bdstevel@tonic-gate	} else if (strncasecmp((char *)what, "wrong", len) == 0) {
20767c478bdstevel@tonic-gate		env_auto = 0;
20777c478bdstevel@tonic-gate		old_env_var = OLD_ENV_VALUE;
20787c478bdstevel@tonic-gate		old_env_value = OLD_ENV_VAR;
20797c478bdstevel@tonic-gate	} else {
20807c478bdstevel@tonic-gateunknown:
20817c478bdstevel@tonic-gate		(void) printf(
20827c478bdstevel@tonic-gate		    "Unknown \"varval\" command. (\"auto\", \"right\", "
20837c478bdstevel@tonic-gate		    "\"wrong\", \"status\")\n");
20847c478bdstevel@tonic-gate	}
20857c478bdstevel@tonic-gate}
20867c478bdstevel@tonic-gate#endif	/* OLD_ENVIRON && ENV_HACK */
20877c478bdstevel@tonic-gate
20887c478bdstevel@tonic-gate/*
20897c478bdstevel@tonic-gate * The AUTHENTICATE command.
20907c478bdstevel@tonic-gate */
20917c478bdstevel@tonic-gate
20927c478bdstevel@tonic-gatestruct authlist {
20937c478bdstevel@tonic-gate	char	*name;
20947c478bdstevel@tonic-gate	char	*help;
20957c478bdstevel@tonic-gate	int	(*handler)();
20967c478bdstevel@tonic-gate	int	narg;
20977c478bdstevel@tonic-gate};
20987c478bdstevel@tonic-gate
20997c478bdstevel@tonic-gateextern int auth_enable(char *);
21007c478bdstevel@tonic-gateextern int auth_disable(char *);
21017c478bdstevel@tonic-gateextern int auth_status(void);
21027c478bdstevel@tonic-gate
21037c478bdstevel@tonic-gatestatic int auth_help(void);
21047c478bdstevel@tonic-gate
21057c478bdstevel@tonic-gatestatic struct authlist AuthList[] = {
21067c478bdstevel@tonic-gate	{ "status",
21077c478bdstevel@tonic-gate	    "Display current status of authentication information",
21087c478bdstevel@tonic-gate	    auth_status,	0 },
21097c478bdstevel@tonic-gate	{ "disable",
21107c478bdstevel@tonic-gate	    "Disable an authentication type ('auth disable ?' for more)",
21117c478bdstevel@tonic-gate	    auth_disable,	1 },
21127c478bdstevel@tonic-gate	{ "enable",
21137c478bdstevel@tonic-gate	    "Enable an authentication type ('auth enable ?' for more)",
21147c478bdstevel@tonic-gate	    auth_enable,	1 },
21157c478bdstevel@tonic-gate	{ "help",	0,			auth_help,		0 },
21167c478bdstevel@tonic-gate	{ "?",	"Print help information",	auth_help,		0 },
21177c478bdstevel@tonic-gate	{ 0 },
21187c478bdstevel@tonic-gate};
21197c478bdstevel@tonic-gate
21207c478bdstevel@tonic-gatestatic int
21217c478bdstevel@tonic-gateauth_help(void)
21227c478bdstevel@tonic-gate{
21237c478bdstevel@tonic-gate	struct authlist *c;
21247c478bdstevel@tonic-gate
21257c478bdstevel@tonic-gate	for (c = AuthList; c->name; c++) {
21267c478bdstevel@tonic-gate		if (c->help) {
21277c478bdstevel@tonic-gate			if (*c->help)
21287c478bdstevel@tonic-gate				(void) printf("%-15s %s\n", c->name, c->help);
21297c478bdstevel@tonic-gate			else
21307c478bdstevel@tonic-gate				(void) printf("\n");
21317c478bdstevel@tonic-gate		}
21327c478bdstevel@tonic-gate	}
21337c478bdstevel@tonic-gate	return (0);
21347c478bdstevel@tonic-gate}
21357c478bdstevel@tonic-gate
21367c478bdstevel@tonic-gate
21377c478bdstevel@tonic-gatestatic int
21387c478bdstevel@tonic-gateauth_cmd(argc, argv)
21397c478bdstevel@tonic-gate	int  argc;
21407c478bdstevel@tonic-gate	char *argv[];
21417c478bdstevel@tonic-gate{
21427c478bdstevel@tonic-gate	struct authlist *c;
21437c478bdstevel@tonic-gate
21447c478bdstevel@tonic-gate	if (argc < 2) {
21457c478bdstevel@tonic-gate		(void) fprintf(stderr, "Need an argument to 'auth' "
21467c478bdstevel@tonic-gate			"command.  'auth ?' for help.\n");
21477c478bdstevel@tonic-gate		return (0);
21487c478bdstevel@tonic-gate	}
21497c478bdstevel@tonic-gate
21507c478bdstevel@tonic-gate	c = (struct authlist *)
21517c478bdstevel@tonic-gate		genget(argv[1], (char **)AuthList, sizeof (struct authlist));
21527c478bdstevel@tonic-gate	if (c == 0) {
21537c478bdstevel@tonic-gate		(void) fprintf(stderr,
21547c478bdstevel@tonic-gate		    "'%s': unknown argument ('auth ?' for help).\n",
21557c478bdstevel@tonic-gate		    argv[1]);
21567c478bdstevel@tonic-gate		return (0);
21577c478bdstevel@tonic-gate	}
21587c478bdstevel@tonic-gate	if (Ambiguous(c)) {
21597c478bdstevel@tonic-gate		(void) fprintf(stderr,
21607c478bdstevel@tonic-gate		    "'%s': ambiguous argument ('auth ?' for help).\n", argv[1]);
21617c478bdstevel@tonic-gate		return (0);
21627c478bdstevel@tonic-gate	}
21637c478bdstevel@tonic-gate	if (c->narg + 2 != argc) {
21647c478bdstevel@tonic-gate		(void) fprintf(stderr,
21657c478bdstevel@tonic-gate		    "Need %s%d argument%s to 'auth %s' command."
21667c478bdstevel@tonic-gate		    " 'auth ?' for help.\n",
21677c478bdstevel@tonic-gate		    c->narg + 2 < argc ? "only " : "",
21687c478bdstevel@tonic-gate		    c->narg, c->narg == 1 ? "" : "s", c->name);
21697c478bdstevel@tonic-gate		return (0);
21707c478bdstevel@tonic-gate	}
21717c478bdstevel@tonic-gate	return ((*c->handler)(argv[2], argv[3]));
21727c478bdstevel@tonic-gate}
21737c478bdstevel@tonic-gate
21747c478bdstevel@tonic-gate/*
21757c478bdstevel@tonic-gate * The FORWARD command.
21767c478bdstevel@tonic-gate */
21777c478bdstevel@tonic-gate
21787c478bdstevel@tonic-gateextern int forward_flags;
21797c478bdstevel@tonic-gate
21807c478bdstevel@tonic-gatestruct forwlist {
21817c478bdstevel@tonic-gate	char *name;
21827c478bdstevel@tonic-gate	char *help;
21837c478bdstevel@tonic-gate	int (*handler)();
21847c478bdstevel@tonic-gate	int f_flags;
21857c478bdstevel@tonic-gate};
21867c478bdstevel@tonic-gate
21877c478bdstevel@tonic-gatestatic int forw_status(void);
21887c478bdstevel@tonic-gatestatic int forw_set(int);
21897c478bdstevel@tonic-gatestatic int forw_help(void);
21907c478bdstevel@tonic-gate
21917c478bdstevel@tonic-gatestatic struct forwlist ForwList[] = {
21927c478bdstevel@tonic-gate	{"status",
21937c478bdstevel@tonic-gate		"Display current status of credential forwarding",
21947c478bdstevel@tonic-gate		forw_status, 0},
21957c478bdstevel@tonic-gate	{"disable",
21967c478bdstevel@tonic-gate		"Disable credential forwarding",
21977c478bdstevel@tonic-gate		forw_set, 0},
21987c478bdstevel@tonic-gate	{"enable",
21997c478bdstevel@tonic-gate		"Enable credential forwarding",
22007c478bdstevel@tonic-gate		forw_set, OPTS_FORWARD_CREDS},
22017c478bdstevel@tonic-gate	{"forwardable",
22027c478bdstevel@tonic-gate		"Enable credential forwarding of "
22037c478bdstevel@tonic-gate				"forwardable credentials",
22047c478bdstevel@tonic-gate		forw_set, OPTS_FORWARD_CREDS |	OPTS_FORWARDABLE_CREDS},
22057c478bdstevel@tonic-gate	{"help",
22067c478bdstevel@tonic-gate		0,
22077c478bdstevel@tonic-gate		forw_help, 0},
22087c478bdstevel@tonic-gate	{"?",
22097c478bdstevel@tonic-gate		"Print help information",
22107c478bdstevel@tonic-gate		forw_help, 0},
22117c478bdstevel@tonic-gate	{0},
22127c478bdstevel@tonic-gate};
22137c478bdstevel@tonic-gate
22147c478bdstevel@tonic-gatestatic int
22157c478bdstevel@tonic-gateforw_status(void)
22167c478bdstevel@tonic-gate{
22177c478bdstevel@tonic-gate	if (forward_flags & OPTS_FORWARD_CREDS) {
22187c478bdstevel@tonic-gate		if (forward_flags & OPTS_FORWARDABLE_CREDS)
22197c478bdstevel@tonic-gate			(void) printf(gettext(
22207c478bdstevel@tonic-gate				"Credential forwarding of "
22217c478bdstevel@tonic-gate				"forwardable credentials enabled\n"));
22227c478bdstevel@tonic-gate		else
22237c478bdstevel@tonic-gate			(void) printf(gettext(
22247c478bdstevel@tonic-gate				"Credential forwarding enabled\n"));
22257c478bdstevel@tonic-gate	} else
22267c478bdstevel@tonic-gate		(void) printf(gettext("Credential forwarding disabled\n"));
22277c478bdstevel@tonic-gate	return (0);
22287c478bdstevel@tonic-gate}
22297c478bdstevel@tonic-gate
2230740638cbwstatic int
22317c478bdstevel@tonic-gateforw_set(int f_flags)
22327c478bdstevel@tonic-gate{
22337c478bdstevel@tonic-gate	forward_flags = f_flags;
22347c478bdstevel@tonic-gate	return (0);
22357c478bdstevel@tonic-gate}
22367c478bdstevel@tonic-gate
22377c478bdstevel@tonic-gatestatic int
22387c478bdstevel@tonic-gateforw_help(void)
22397c478bdstevel@tonic-gate{
22407c478bdstevel@tonic-gate	struct forwlist *c;
22417c478bdstevel@tonic-gate
22427c478bdstevel@tonic-gate	for (c = ForwList; c->name; c++) {
22437c478bdstevel@tonic-gate		if (c->help) {
22447c478bdstevel@tonic-gate			if (*c->help)
22457c478bdstevel@tonic-gate				(void) printf("%-15s %s\r\n", c->name, c->help);
22467c478bdstevel@tonic-gate			else
22477c478bdstevel@tonic-gate				(void) printf("\n");
22487c478bdstevel@tonic-gate		}
22497c478bdstevel@tonic-gate	}
22507c478bdstevel@tonic-gate	return (0);
22517c478bdstevel@tonic-gate}
22527c478bdstevel@tonic-gate
22537c478bdstevel@tonic-gatestatic int
22547c478bdstevel@tonic-gateforw_cmd(int argc, char *argv[])
22557c478bdstevel@tonic-gate{
22567c478bdstevel@tonic-gate	struct forwlist *c;
22577c478bdstevel@tonic-gate
22587c478bdstevel@tonic-gate	if (argc < 2) {
22597c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(
22607c478bdstevel@tonic-gate			"Need an argument to 'forward' "
22617c478bdstevel@tonic-gate			"command.  'forward ?' for help.\n"));
22627c478bdstevel@tonic-gate		return (0);
22637c478bdstevel@tonic-gate	}
22647c478bdstevel@tonic-gate	c = (struct forwlist *)genget(argv[1], (char **)ForwList,
22657c478bdstevel@tonic-gate		sizeof (struct forwlist));
22667c478bdstevel@tonic-gate	if (c == 0) {
22677c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(
22687c478bdstevel@tonic-gate		    "'%s': unknown argument ('forward ?' for help).\n"),
22697c478bdstevel@tonic-gate		    argv[1]);
22707c478bdstevel@tonic-gate		return (0);
22717c478bdstevel@tonic-gate	}
22727c478bdstevel@tonic-gate	if (Ambiguous(c)) {
22737c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(
22747c478bdstevel@tonic-gate		    "'%s': ambiguous argument ('forward ?' for help).\n"),
22757c478bdstevel@tonic-gate		    argv[1]);
22767c478bdstevel@tonic-gate		return (0);
22777c478bdstevel@tonic-gate	}
22787c478bdstevel@tonic-gate	if (argc != 2) {
22797c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(
22807c478bdstevel@tonic-gate		    "No arguments needed to 'forward %s' command.  "
22817c478bdstevel@tonic-gate		    "'forward ?' for help.\n"), c->name);
22827c478bdstevel@tonic-gate		return (0);
22837c478bdstevel@tonic-gate	}
22847c478bdstevel@tonic-gate	return ((*c->handler) (c->f_flags));
22857c478bdstevel@tonic-gate}
22867c478bdstevel@tonic-gate
22877c478bdstevel@tonic-gate/*
22887c478bdstevel@tonic-gate * The ENCRYPT command.
22897c478bdstevel@tonic-gate */
22907c478bdstevel@tonic-gate
22917c478bdstevel@tonic-gatestruct encryptlist {
22927c478bdstevel@tonic-gate	char	*name;
22937c478bdstevel@tonic-gate	char	*help;
22947c478bdstevel@tonic-gate	int	(*handler)();
22957c478bdstevel@tonic-gate	int	needconnect;
22967c478bdstevel@tonic-gate	int	minarg;
22977c478bdstevel@tonic-gate	int	maxarg;
22987c478bdstevel@tonic-gate};
22997c478bdstevel@tonic-gate
23007c478bdstevel@tonic-gatestatic int EncryptHelp(void);
23017c478bdstevel@tonic-gate
23027c478bdstevel@tonic-gatestatic struct encryptlist EncryptList[] = {
23037c478bdstevel@tonic-gate	{ "enable", "Enable encryption. ('encrypt enable ?' for more)",
23047c478bdstevel@tonic-gate						EncryptEnable, 1, 1, 2 },
23057c478bdstevel@tonic-gate	{ "disable", "Disable encryption. ('encrypt disable ?' for more)",
23067c478bdstevel@tonic-gate						EncryptDisable, 0, 1, 2 },
23077c478bdstevel@tonic-gate	{ "type", "Set encryption type. ('encrypt type ?' for more)",
23087c478bdstevel@tonic-gate						EncryptType, 0, 1, 2 },
23097c478bdstevel@tonic-gate	{ "start", "Start encryption. ('encrypt start ?' for more)",
23107c478bdstevel@tonic-gate						EncryptStart, 1, 0, 1 },
23117c478bdstevel@tonic-gate	{ "stop", "Stop encryption. ('encrypt stop ?' for more)",
23127c478bdstevel@tonic-gate						EncryptStop, 1, 0, 1 },
23137c478bdstevel@tonic-gate	{ "input", "Start encrypting the input stream",
23147c478bdstevel@tonic-gate						EncryptStartInput, 1, 0, 0 },
23157c478bdstevel@tonic-gate	{ "-input", "Stop encrypting the input stream",
23167c478bdstevel@tonic-gate						EncryptStopInput, 1, 0, 0 },
23177c478bdstevel@tonic-gate	{ "output", "Start encrypting the output stream",
23187c478bdstevel@tonic-gate						EncryptStartOutput, 1, 0, 0 },
23197c478bdstevel@tonic-gate	{ "-output", "Stop encrypting the output stream",
23207c478bdstevel@tonic-gate						EncryptStopOutput, 1, 0, 0 },
23217c478bdstevel@tonic-gate
23227c478bdstevel@tonic-gate	{ "status",	"Display current status of encryption information",
23237c478bdstevel@tonic-gate						EncryptStatus,	0, 0, 0 },
23247c478bdstevel@tonic-gate	{ "help",	0,
23257c478bdstevel@tonic-gate						EncryptHelp,	0, 0, 0 },
23267c478bdstevel@tonic-gate	{ "?",	"Print help information",	EncryptHelp,	0, 0, 0 },
23277c478bdstevel@tonic-gate	{ 0 },
23287c478bdstevel@tonic-gate};
23297c478bdstevel@tonic-gate
23307c478bdstevel@tonic-gatestatic int
23317c478bdstevel@tonic-gateEncryptHelp(void)
23327c478bdstevel@tonic-gate{
23337c478bdstevel@tonic-gate	struct encryptlist *c;
23347c478bdstevel@tonic-gate
23357c478bdstevel@tonic-gate	for (c = EncryptList; c->name; c++) {
23367c478bdstevel@tonic-gate		if (c->help) {
23377c478bdstevel@tonic-gate			if (*c->help)
23387c478bdstevel@tonic-gate				(void) printf("%-15s %s\n", c->name, c->help);
23397c478bdstevel@tonic-gate			else
23407c478bdstevel@tonic-gate				(void) printf("\n");
23417c478bdstevel@tonic-gate		}
23427c478bdstevel@tonic-gate	}
23437c478bdstevel@tonic-gate	return (0);
23447c478bdstevel@tonic-gate}
23457c478bdstevel@tonic-gate
23467c478bdstevel@tonic-gatestatic int
23477c478bdstevel@tonic-gateencrypt_cmd(int  argc, char *argv[])
23487c478bdstevel@tonic-gate{
23497c478bdstevel@tonic-gate	struct encryptlist *c;
23507c478bdstevel@tonic-gate
23517c478bdstevel@tonic-gate	if (argc < 2) {
23527c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(
23537c478bdstevel@tonic-gate			"Need an argument to 'encrypt' command.  "
23547c478bdstevel@tonic-gate			"'encrypt ?' for help.\n"));
23557c478bdstevel@tonic-gate		return (0);
23567c478bdstevel@tonic-gate	}
23577c478bdstevel@tonic-gate
23587c478bdstevel@tonic-gate	c = (struct encryptlist *)
23597c478bdstevel@tonic-gate	    genget(argv[1], (char **)EncryptList, sizeof (struct encryptlist));
23607c478bdstevel@tonic-gate	if (c == 0) {
23617c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(
23627c478bdstevel@tonic-gate		    "'%s': unknown argument ('encrypt ?' for help).\n"),
23637c478bdstevel@tonic-gate		    argv[1]);
23647c478bdstevel@tonic-gate		return (0);
23657c478bdstevel@tonic-gate	}
23667c478bdstevel@tonic-gate	if (Ambiguous(c)) {
23677c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(
23687c478bdstevel@tonic-gate		    "'%s': ambiguous argument ('encrypt ?' for help).\n"),
23697c478bdstevel@tonic-gate		    argv[1]);
23707c478bdstevel@tonic-gate		return (0);
23717c478bdstevel@tonic-gate	}
23727c478bdstevel@tonic-gate	argc -= 2;
23737c478bdstevel@tonic-gate	if (argc < c->minarg || argc > c->maxarg) {
23747c478bdstevel@tonic-gate		if (c->minarg == c->maxarg) {
23757c478bdstevel@tonic-gate			(void) fprintf(stderr, gettext("Need %s%d %s "),
23767c478bdstevel@tonic-gate			c->minarg < argc ?
23777c478bdstevel@tonic-gate			gettext("only ") : "", c->minarg,
23787c478bdstevel@tonic-gate			c->minarg == 1 ?
23797c478bdstevel@tonic-gate			gettext("argument") : gettext("arguments"));
23807c478bdstevel@tonic-gate		} else {
23817c478bdstevel@tonic-gate			(void) fprintf(stderr,
23827c478bdstevel@tonic-gate			    gettext("Need %s%d-%d arguments "),
23837c478bdstevel@tonic-gate			c->maxarg < argc ?
23847c478bdstevel@tonic-gate			gettext("only ") : "", c->minarg, c->maxarg);
23857c478bdstevel@tonic-gate		}
23867c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(
23877c478bdstevel@tonic-gate		    "to 'encrypt %s' command.  'encrypt ?' for help.\n"),
23887c478bdstevel@tonic-gate		    c->name);
23897c478bdstevel@tonic-gate		return (0);
23907c478bdstevel@tonic-gate	}
23917c478bdstevel@tonic-gate	if (c->needconnect && !connected) {
23927c478bdstevel@tonic-gate		if (!(argc &&
23937c478bdstevel@tonic-gate		    (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
23947c478bdstevel@tonic-gate			(void) printf(
23957c478bdstevel@tonic-gate			    gettext("?Need to be connected first.\n"));
23967c478bdstevel@tonic-gate			return (0);
23977c478bdstevel@tonic-gate		}
23987c478bdstevel@tonic-gate	}
23997c478bdstevel@tonic-gate	return ((*c->handler)(argc > 0 ? argv[2] : 0,
24007c478bdstevel@tonic-gate	    argc > 1 ? argv[3] : 0, argc > 2 ? argv[4] : 0));
24017c478bdstevel@tonic-gate}
24027c478bdstevel@tonic-gate
24037c478bdstevel@tonic-gate/*
24047c478bdstevel@tonic-gate * Print status about the connection.
24057c478bdstevel@tonic-gate */
2406740638cbwstatic int
24077c478bdstevel@tonic-gatestatus(int argc, char *argv[])
24087c478bdstevel@tonic-gate{
24097c478bdstevel@tonic-gate	if (connected) {
24107c478bdstevel@tonic-gate		(void) printf("Connected to %s.\n", hostname);
24117c478bdstevel@tonic-gate		if ((argc < 2) || strcmp(argv[1], "notmuch")) {
24127c478bdstevel@tonic-gate			int mode = getconnmode();
24137c478bdstevel@tonic-gate
24147c478bdstevel@tonic-gate			if (my_want_state_is_will(TELOPT_LINEMODE)) {
24157c478bdstevel@tonic-gate				(void) printf(
24167c478bdstevel@tonic-gate				    "Operating with LINEMODE option\n");
24177c478bdstevel@tonic-gate				(void) printf(
24187c478bdstevel@tonic-gate				    "%s line editing\n", (mode&MODE_EDIT) ?
24197c478bdstevel@tonic-gate				    "Local" : "No");
24207c478bdstevel@tonic-gate				(void) printf("%s catching of signals\n",
24217c478bdstevel@tonic-gate				    (mode&MODE_TRAPSIG) ? "Local" : "No");
24227c478bdstevel@tonic-gate				slcstate();
24237c478bdstevel@tonic-gate#ifdef	KLUDGELINEMODE
24247c478bdstevel@tonic-gate			} else if (kludgelinemode &&
24257c478bdstevel@tonic-gate			    my_want_state_is_dont(TELOPT_SGA)) {
24267c478bdstevel@tonic-gate				(void) printf(
24277c478bdstevel@tonic-gate				    "Operating in obsolete linemode\n");
24287c478bdstevel@tonic-gate#endif
24297c478bdstevel@tonic-gate			} else {
24307c478bdstevel@tonic-gate				(void) printf(
24317c478bdstevel@tonic-gate					"Operating in single character mode\n");
24327c478bdstevel@tonic-gate				if (localchars)
24337c478bdstevel@tonic-gate					(void) printf(
24347c478bdstevel@tonic-gate					"Catching signals locally\n");
24357c478bdstevel@tonic-gate			}
24367c478bdstevel@tonic-gate			(void) printf("%s character echo\n", (mode&MODE_ECHO) ?
24377c478bdstevel@tonic-gate			    "Local" : "Remote");
24387c478bdstevel@tonic-gate			if (my_want_state_is_will(TELOPT_LFLOW))
24397c478bdstevel@tonic-gate				(void) printf("%s flow control\n",
24407c478bdstevel@tonic-gate				    (mode&MODE_FLOW) ? "Local" : "No");
24417c478bdstevel@tonic-gate
244223a2029Toomas Soome			encrypt_display();
24437c478bdstevel@tonic-gate		}
24447c478bdstevel@tonic-gate	} else {
24457c478bdstevel@tonic-gate		(void) printf("No connection.\n");
24467c478bdstevel@tonic-gate	}
24477c478bdstevel@tonic-gate	if (rlogin != _POSIX_VDISABLE)
24487c478bdstevel@tonic-gate		(void) printf("Escape character is '%s'.\n", control(rlogin));
24497c478bdstevel@tonic-gate	else
24507c478bdstevel@tonic-gate		(void) printf(
24517c478bdstevel@tonic-gate		    "Escape character is '%s'.\n", esc_control(escape));
24527c478bdstevel@tonic-gate	(void) fflush(stdout);
24537c478bdstevel@tonic-gate	return (1);
24547c478bdstevel@tonic-gate}
24557c478bdstevel@tonic-gate
24567c478bdstevel@tonic-gate/*
24577c478bdstevel@tonic-gate * Parse the user input (cmd_line_input) which should:
24587c478bdstevel@tonic-gate * - start with the target host, or with "@" or "!@" followed by at least one
24597c478bdstevel@tonic-gate *   gateway.
24607c478bdstevel@tonic-gate * - each host (can be literal address or hostname) can be separated by ",",
24617c478bdstevel@tonic-gate *   "@", or ",@".
24627c478bdstevel@tonic-gate * Note that the last host is the target, all the others (if any ) are the
24637c478bdstevel@tonic-gate * gateways.
24647c478bdstevel@tonic-gate *
24657c478bdstevel@tonic-gate * Returns:	-1	if a library call fails, too many gateways, or parse
24667c478bdstevel@tonic-gate *			error
24677c478bdstevel@tonic-gate *		num_gw	otherwise
24687c478bdstevel@tonic-gate * On successful return, hostname_list points to a list of hosts (last one being
24697c478bdstevel@tonic-gate * the target, others gateways), src_rtng_type points to the type of source
24707c478bdstevel@tonic-gate * routing (strict vs. loose)
24717c478bdstevel@tonic-gate */
24727c478bdstevel@tonic-gatestatic int
24737c478bdstevel@tonic-gateparse_input(char *cmd_line_input, char **hostname_list, uchar_t *src_rtng_type)
24747c478bdstevel@tonic-gate{
24757c478bdstevel@tonic-gate	char hname[MAXHOSTNAMELEN + 1];
24767c478bdstevel@tonic-gate	char *cp;
24777c478bdstevel@tonic-gate	int gw_count;
24787c478bdstevel@tonic-gate	int i;
24797c478bdstevel@tonic-gate
24807c478bdstevel@tonic-gate	gw_count = 0;
24817c478bdstevel@tonic-gate	cp = cmd_line_input;
24827c478bdstevel@tonic-gate
24837c478bdstevel@tonic-gate	/*
24847c478bdstevel@tonic-gate	 * Defining ICMD generates the Itelnet binary, the special version of
24857c478bdstevel@tonic-gate	 * telnet which is used with firewall proxy.
24867c478bdstevel@tonic-gate	 * If ICMD is defined, parse_input will treat the whole cmd_line_input
24877c478bdstevel@tonic-gate	 * as the target host and set the num_gw to 0. Therefore, none of the
24887c478bdstevel@tonic-gate	 * source routing related code paths will be executed.
24897c478bdstevel@tonic-gate	 */
24907c478bdstevel@tonic-gate#ifndef ICMD
24917c478bdstevel@tonic-gate	if (*cp == '@') {
24927c478bdstevel@tonic-gate		*src_rtng_type = IPOPT_LSRR;
24937c478bdstevel@tonic-gate		cp++;
24947c478bdstevel@tonic-gate	} else if (*cp == '!') {
24957c478bdstevel@tonic-gate		*src_rtng_type = IPOPT_SSRR;
24967c478bdstevel@tonic-gate
24977c478bdstevel@tonic-gate		/* "!" must be followed by '@' */
24987c478bdstevel@tonic-gate		if (*(cp + 1) != '@')
24997c478bdstevel@tonic-gate			goto parse_error;
25007c478bdstevel@tonic-gate		cp += 2;
25017c478bdstevel@tonic-gate	} else {
25027c478bdstevel@tonic-gate#endif	/* ICMD */
25037c478bdstevel@tonic-gate		/* no gateways, just the target */
25047c478bdstevel@tonic-gate		hostname_list[0] = strdup(cp);
25057c478bdstevel@tonic-gate		if (hostname_list[0] == NULL) {
25067c478bdstevel@tonic-gate			perror("telnet: copying host name");
25077c478bdstevel@tonic-gate			return (-1);
25087c478bdstevel@tonic-gate		}
25097c478bdstevel@tonic-gate		return (0);
25107c478bdstevel@tonic-gate#ifndef ICMD
25117c478bdstevel@tonic-gate	}
25127c478bdstevel@tonic-gate
25137c478bdstevel@tonic-gate	while (*cp != '\0') {
25147c478bdstevel@tonic-gate		/*
25157c478bdstevel@tonic-gate		 * Identify each gateway separated by ",", "@" or ",@" and
25167c478bdstevel@tonic-gate		 * store in hname[].
25177c478bdstevel@tonic-gate		 */
25187c478bdstevel@tonic-gate		i = 0;
25197c478bdstevel@tonic-gate		while (*cp != '@' && *cp != ',' && *cp != '\0') {
25207c478bdstevel@tonic-gate			hname[i++] = *cp++;
25217c478bdstevel@tonic-gate			if (i > MAXHOSTNAMELEN)
25227c478bdstevel@tonic-gate				goto parse_error;
25237c478bdstevel@tonic-gate		}
25247c478bdstevel@tonic-gate		hname[i] = '\0';
25257c478bdstevel@tonic-gate
25267c478bdstevel@tonic-gate		/*
25277c478bdstevel@tonic-gate		 * Two consecutive delimiters which result in a 0 length hname
25287c478bdstevel@tonic-gate		 * is a parse error.
25297c478bdstevel@tonic-gate		 */
25307c478bdstevel@tonic-gate		if (i == 0)
25317c478bdstevel@tonic-gate			goto parse_error;
25327c478bdstevel@tonic-gate
25337c478bdstevel@tonic-gate		hostname_list[gw_count] = strdup(hname);
25347c478bdstevel@tonic-gate		if (hostname_list[gw_count] == NULL) {
25357c478bdstevel@tonic-gate			perror("telnet: copying hostname from list");
25367c478bdstevel@tonic-gate			return (-1);
25377c478bdstevel@tonic-gate		}
25387c478bdstevel@tonic-gate
25397c478bdstevel@tonic-gate		if (++gw_count > MAXMAX_GATEWAY) {
25407c478bdstevel@tonic-gate			(void) fprintf(stderr, "telnet: too many gateways\n");
25417c478bdstevel@tonic-gate			return (-1);
25427c478bdstevel@tonic-gate		}
25437c478bdstevel@tonic-gate
25447c478bdstevel@tonic-gate		/* Jump over the next delimiter. */
25457c478bdstevel@tonic-gate		if (*cp != '\0') {
25467c478bdstevel@tonic-gate			/* ...gw1,@gw2... accepted */
25477c478bdstevel@tonic-gate			if (*cp == ',' && *(cp + 1) == '@')
25487c478bdstevel@tonic-gate				cp += 2;
25497c478bdstevel@tonic-gate			else
25507c478bdstevel@tonic-gate				cp++;
25517c478bdstevel@tonic-gate		}
25527c478bdstevel@tonic-gate	}
25537c478bdstevel@tonic-gate
25547c478bdstevel@tonic-gate	/* discount the target */
25557c478bdstevel@tonic-gate	gw_count--;
25567c478bdstevel@tonic-gate
25577c478bdstevel@tonic-gate	/* Any input starting with '!@' or '@' must have at least one gateway */
25587c478bdstevel@tonic-gate	if (gw_count <= 0)
25597c478bdstevel@tonic-gate		goto parse_error;
25607c478bdstevel@tonic-gate
25617c478bdstevel@tonic-gate	return (gw_count);
25627c478bdstevel@tonic-gate
25637c478bdstevel@tonic-gateparse_error:
25647c478bdstevel@tonic-gate	(void) printf("Bad source route option: %s\n", cmd_line_input);
25657c478bdstevel@tonic-gate	return (-1);
25667c478bdstevel@tonic-gate#endif	/* ICMD */
25677c478bdstevel@tonic-gate}
25687c478bdstevel@tonic-gate
25697c478bdstevel@tonic-gate/*
25707c478bdstevel@tonic-gate * Resolves the target and gateway addresses, determines what type of addresses
25717c478bdstevel@tonic-gate * (ALL_ADDRS, ONLY_V6, ONLY_V4) telnet will be trying to connect.
25727c478bdstevel@tonic-gate *
25737c478bdstevel@tonic-gate * Returns:	pointer to resolved target	if name resolutions succeed
25747c478bdstevel@tonic-gate *		NULL				if name resolutions fail or
25757c478bdstevel@tonic-gate *						a library function call fails
25767c478bdstevel@tonic-gate *
25777c478bdstevel@tonic-gate * The last host in the hostname_list is the target. After resolving the target,
25787c478bdstevel@tonic-gate * determines for what type of addresses it should try to resolve gateways. It
25797c478bdstevel@tonic-gate * resolves gateway addresses and picks one address for each desired address
25807c478bdstevel@tonic-gate * type and stores in the array pointed by gw_addrsp. Also, this 'type of
25817c478bdstevel@tonic-gate * addresses' is pointed by addr_type argument on successful return.
25827c478bdstevel@tonic-gate */
25837c478bdstevel@tonic-gatestatic struct addrinfo *
25847c478bdstevel@tonic-gateresolve_hosts(char **hostname_list, int num_gw, struct gateway **gw_addrsp,
25857c478bdstevel@tonic-gate    int *addr_type, const char *portp)
25867c478bdstevel@tonic-gate{
25877c478bdstevel@tonic-gate	struct gateway *gw_addrs = NULL;
25887c478bdstevel@tonic-gate	struct gateway *gw;
25897c478bdstevel@tonic-gate	/* whether we already picked an IPv4 address for the current gateway */
25907c478bdstevel@tonic-gate	boolean_t got_v4_addr;
25917c478bdstevel@tonic-gate	boolean_t got_v6_addr;
25927c478bdstevel@tonic-gate	/* whether we need to get an IPv4 address for the current gateway */
25937c478bdstevel@tonic-gate	boolean_t need_v4_addr = B_FALSE;
25947c478bdstevel@tonic-gate	boolean_t need_v6_addr = B_FALSE;
25957c478bdstevel@tonic-gate	int res_failed_at4;	/* save which gateway failed to resolve */
25967c478bdstevel@tonic-gate	int res_failed_at6;
25977c478bdstevel@tonic-gate	boolean_t is_v4mapped;
25987c478bdstevel@tonic-gate	struct in6_addr *v6addrp;
25997c478bdstevel@tonic-gate	struct in_addr *v4addrp;
26007c478bdstevel@tonic-gate	int error_num;
26017c478bdstevel@tonic-gate	int i;
26027c478bdstevel@tonic-gate	int rc;
26037c478bdstevel@tonic-gate	struct addrinfo *res, *host, *gateway, *addr;
26047c478bdstevel@tonic-gate	struct addrinfo hints;
26057c478bdstevel@tonic-gate
26067c478bdstevel@tonic-gate	*addr_type = ALL_ADDRS;
26077c478bdstevel@tonic-gate
26087c478bdstevel@tonic-gate	memset(&hints, 0, sizeof (hints));
26097c478bdstevel@tonic-gate	hints.ai_flags = AI_CANONNAME; /* used for config files, diags */
26107c478bdstevel@tonic-gate	hints.ai_socktype = SOCK_STREAM;
26117c478bdstevel@tonic-gate	rc = getaddrinfo(hostname_list[num_gw],
26127c478bdstevel@tonic-gate	    (portp != NULL) ? portp : "telnet", &hints, &res);
26137c478bdstevel@tonic-gate	if (rc != 0) {
26147c478bdstevel@tonic-gate		if (hostname_list[num_gw] != NULL &&
26157c478bdstevel@tonic-gate		    *hostname_list[num_gw] != '\0')
26167c478bdstevel@tonic-gate			(void) fprintf(stderr, "%s: ", hostname_list[num_gw]);
26177c478bdstevel@tonic-gate		(void) fprintf(stderr, "%s\n", gai_strerror(rc));
26187c478bdstevel@tonic-gate		return (NULL);
26197c478bdstevel@tonic-gate	}
26207c478bdstevel@tonic-gate
26217c478bdstevel@tonic-gate	/*
26227c478bdstevel@tonic-gate	 * Let's see what type of addresses we got for the target. This
26237c478bdstevel@tonic-gate	 * determines what type of addresses we'd like to resolve gateways
26247c478bdstevel@tonic-gate	 * later.
26257c478bdstevel@tonic-gate	 */
26267c478bdstevel@tonic-gate	for (host = res; host != NULL; host = host->ai_next) {
26277c478bdstevel@tonic-gate		struct sockaddr_in6 *s6;
26287c478bdstevel@tonic-gate
26297c478bdstevel@tonic-gate		s6 = (struct sockaddr_in6 *)host->ai_addr;
26307c478bdstevel@tonic-gate
26317c478bdstevel@tonic-gate		if (host->ai_addr->sa_family == AF_INET ||
26327c478bdstevel@tonic-gate		    IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr))
26337c478bdstevel@tonic-gate			need_v4_addr = B_TRUE;
26347c478bdstevel@tonic-gate		else
26357c478bdstevel@tonic-gate			need_v6_addr = B_TRUE;
26367c478bdstevel@tonic-gate
26377c478bdstevel@tonic-gate		/*
26387c478bdstevel@tonic-gate		 * Let's stop after seeing we need both IPv6 and IPv4.
26397c478bdstevel@tonic-gate		 */
26407c478bdstevel@tonic-gate		if (need_v4_addr && need_v6_addr)
26417c478bdstevel@tonic-gate			break;
26427c478bdstevel@tonic-gate	}
26437c478bdstevel@tonic-gate
26447c478bdstevel@tonic-gate	if (num_gw > 0) {
26457c478bdstevel@tonic-gate		/*
26467c478bdstevel@tonic-gate		 * In the prepare_optbuf(), we'll store the IPv4 address of the
26477c478bdstevel@tonic-gate		 * target in the last slot of gw_addrs array. Therefore we need
26487c478bdstevel@tonic-gate		 * space for num_gw+1 hosts.
26497c478bdstevel@tonic-gate		 */
26507c478bdstevel@tonic-gate		gw_addrs = calloc(num_gw + 1, sizeof (struct gateway));
26517c478bdstevel@tonic-gate		if (gw_addrs == NULL) {
26527c478bdstevel@tonic-gate			perror("telnet: calloc");
26537c478bdstevel@tonic-gate			freeaddrinfo(res);
26547c478bdstevel@tonic-gate			return (NULL);
26557c478bdstevel@tonic-gate		}
26567c478bdstevel@tonic-gate	}
26577c478bdstevel@tonic-gate
26587c478bdstevel@tonic-gate	/*
26597c478bdstevel@tonic-gate	 * Now we'll go through all the gateways and try to resolve them to
26607c478bdstevel@tonic-gate	 * the desired address types.
26617c478bdstevel@tonic-gate	 */
26627c478bdstevel@tonic-gate	gw = gw_addrs;
26637c478bdstevel@tonic-gate
26647c478bdstevel@tonic-gate	/* -1 means 'no address resolution failure yet' */
26657c478bdstevel@tonic-gate	res_failed_at4 = -1;
26667c478bdstevel@tonic-gate	res_failed_at6 = -1;
26677c478bdstevel@tonic-gate	for (i = 0; i < num_gw; i++) {
26687c478bdstevel@tonic-gate		rc = getaddrinfo(hostname_list[i], NULL, NULL, &gateway);
26697c478bdstevel@tonic-gate		if (rc != 0) {
26707c478bdstevel@tonic-gate			if (hostname_list[i] != NULL &&
26717c478bdstevel@tonic-gate			    *hostname_list[i] != '\0')
26727c478bdstevel@tonic-gate				(void) fprintf(stderr, "%s: ",
26737c478bdstevel@tonic-gate				    hostname_list[i]);
26747c478bdstevel@tonic-gate			(void) fprintf(stderr, "bad address\n");
26757c478bdstevel@tonic-gate			return (NULL);
26767c478bdstevel@tonic-gate		}
26777c478bdstevel@tonic-gate
26787c478bdstevel@tonic-gate		/*
26797c478bdstevel@tonic-gate		 * Initially we have no address of any type for this gateway.
26807c478bdstevel@tonic-gate		 */
26817c478bdstevel@tonic-gate		got_v6_addr = B_FALSE;
26827c478bdstevel@tonic-gate		got_v4_addr = B_FALSE;
26837c478bdstevel@tonic-gate
26847c478bdstevel@tonic-gate		/*
26857c478bdstevel@tonic-gate		 * Let's go through all the addresses of this gateway.
26867c478bdstevel@tonic-gate		 * Use the first address which matches the needed family.
26877c478bdstevel@tonic-gate		 */
26887c478bdstevel@tonic-gate		for (addr = gateway; addr != NULL; addr = addr->ai_next) {
26897c478bdstevel@tonic-gate			/*LINTED*/
26907c478bdstevel@tonic-gate			v6addrp = &((struct sockaddr_in6 *)addr->ai_addr)->
26917c478bdstevel@tonic-gate			    sin6_addr;
26927c478bdstevel@tonic-gate			v4addrp = &((struct sockaddr_in *)addr->ai_addr)->
26937c478bdstevel@tonic-gate			    sin_addr;
26947c478bdstevel@tonic-gate
26957c478bdstevel@tonic-gate			if (addr->ai_family == AF_INET6)
26967c478bdstevel@tonic-gate				is_v4mapped = IN6_IS_ADDR_V4MAPPED(v6addrp);
26977c478bdstevel@tonic-gate			else
26987c478bdstevel@tonic-gate				is_v4mapped = B_FALSE;
26997c478bdstevel@tonic-gate
27007c478bdstevel@tonic-gate			/*
27017c478bdstevel@tonic-gate			 * If we need to determine an IPv4 address and haven't
27027c478bdstevel@tonic-gate			 * found one yet and this is a IPv4-mapped IPv6 address,
27037c478bdstevel@tonic-gate			 * then bingo!
27047c478bdstevel@tonic-gate			 */
27057c478bdstevel@tonic-gate			if (need_v4_addr && !got_v4_addr) {
27067c478bdstevel@tonic-gate				if (is_v4mapped) {
27077c478bdstevel@tonic-gate					IN6_V4MAPPED_TO_INADDR(v6addrp,
27087c478bdstevel@tonic-gate					    &gw->gw_addr);
27097c478bdstevel@tonic-gate					got_v4_addr = B_TRUE;
27107c478bdstevel@tonic-gate				} else if (addr->ai_family = AF_INET) {
27117c478bdstevel@tonic-gate					gw->gw_addr = *v4addrp;
27127c478bdstevel@tonic-gate					got_v4_addr = B_TRUE;
27137c478bdstevel@tonic-gate				}
27147c478bdstevel@tonic-gate			}
27157c478bdstevel@tonic-gate
27167c478bdstevel@tonic-gate			if (need_v6_addr && !got_v6_addr &&
27177c478bdstevel@tonic-gate			    addr->ai_family == AF_INET6) {
27187c478bdstevel@tonic-gate				gw->gw_addr6 = *v6addrp;
27197c478bdstevel@tonic-gate				got_v6_addr = B_TRUE;
27207c478bdstevel@tonic-gate			}
27217c478bdstevel@tonic-gate
27227c478bdstevel@tonic-gate			/*
27237c478bdstevel@tonic-gate			 * Let's stop if we got all what we looked for.
27247c478bdstevel@tonic-gate			 */
27257c478bdstevel@tonic-gate			if ((!need_v4_addr || got_v4_addr) &&
27267c478bdstevel@tonic-gate			    (!need_v6_addr || got_v6_addr))
27277c478bdstevel@tonic-gate				break;
27287c478bdstevel@tonic-gate		}
27297c478bdstevel@tonic-gate
27307c478bdstevel@tonic-gate		/*
27317c478bdstevel@tonic-gate		 * We needed an IPv4 address for this gateway but couldn't
27327c478bdstevel@tonic-gate		 * find one.
27337c478bdstevel@tonic-gate		 */
27347c478bdstevel@tonic-gate		if (need_v4_addr && !got_v4_addr) {
27357c478bdstevel@tonic-gate			res_failed_at4 = i;
27367c478bdstevel@tonic-gate			/*
27377c478bdstevel@tonic-gate			 * Since we couldn't resolve a gateway to IPv4 address
27387c478bdstevel@tonic-gate			 * we can't use IPv4 at all. Therefore we no longer
27397c478bdstevel@tonic-gate			 * need IPv4 addresses for any of the gateways.
27407c478bdstevel@tonic-gate			 */
27417c478bdstevel@tonic-gate			need_v4_addr = B_FALSE;
27427c478bdstevel@tonic-gate		}
27437c478bdstevel@tonic-gate
27447c478bdstevel@tonic-gate		if (need_v6_addr && !got_v6_addr) {
27457c478bdstevel@tonic-gate			res_failed_at6 = i;
27467c478bdstevel@tonic-gate			need_v6_addr = B_FALSE;
27477c478bdstevel@tonic-gate		}
27487c478bdstevel@tonic-gate
27497c478bdstevel@tonic-gate		/*
27507c478bdstevel@tonic-gate		 * If some gateways don't resolve to any of the desired
27517c478bdstevel@tonic-gate		 * address types, we fail.
27527c478bdstevel@tonic-gate		 */
27537c478bdstevel@tonic-gate		if (!need_v4_addr && !need_v6_addr) {
27547c478bdstevel@tonic-gate			if (res_failed_at6 != -1) {
27557c478bdstevel@tonic-gate				(void) fprintf(stderr,
27567c478bdstevel@tonic-gate				    "%s: Host doesn't have any IPv6 address\n",
27577c478bdstevel@tonic-gate				    hostname_list[res_failed_at6]);
27587c478bdstevel@tonic-gate			}
27597c478bdstevel@tonic-gate			if (res_failed_at4 != -1) {
27607c478bdstevel@tonic-gate				(void) fprintf(stderr,
27617c478bdstevel@tonic-gate				    "%s: Host doesn't have any IPv4 address\n",
27627c478bdstevel@tonic-gate				    hostname_list[res_failed_at4]);
27637c478bdstevel@tonic-gate			}
27647c478bdstevel@tonic-gate			free(gw_addrs);
27657c478bdstevel@tonic-gate			return (NULL);
27667c478bdstevel@tonic-gate		}
27677c478bdstevel@tonic-gate
27687c478bdstevel@tonic-gate		gw++;
27697c478bdstevel@tonic-gate	}
27707c478bdstevel@tonic-gate
27717c478bdstevel@tonic-gate	*gw_addrsp = gw_addrs;
27727c478bdstevel@tonic-gate
27737c478bdstevel@tonic-gate	/*
27747c478bdstevel@tonic-gate	 * When we get here, need_v4_addr and need_v6_addr have their final
27757c478bdstevel@tonic-gate	 * values based on the name resolution of the target and gateways.
27767c478bdstevel@tonic-gate	 */
27777c478bdstevel@tonic-gate	if (need_v4_addr && need_v6_addr)
27787c478bdstevel@tonic-gate		*addr_type = ALL_ADDRS;
27797c478bdstevel@tonic-gate	else if (need_v4_addr && !need_v6_addr)
27807c478bdstevel@tonic-gate		*addr_type = ONLY_V4;
27817c478bdstevel@tonic-gate	else if (!need_v4_addr && need_v6_addr)
27827c478bdstevel@tonic-gate		*addr_type = ONLY_V6;
27837c478bdstevel@tonic-gate
27847c478bdstevel@tonic-gate	return (res);
27857c478bdstevel@tonic-gate}
27867c478bdstevel@tonic-gate
27877c478bdstevel@tonic-gate
27887c478bdstevel@tonic-gate/*
27897c478bdstevel@tonic-gate * Initializes the buffer pointed by opt_bufpp for a IPv4 option of type
27907c478bdstevel@tonic-gate * src_rtng_type using the gateway addresses stored in gw_addrs. If no buffer
27917c478bdstevel@tonic-gate * is passed, it allocates one. If a buffer is passed, checks if it's big
27927c478bdstevel@tonic-gate * enough.
27937c478bdstevel@tonic-gate * On return opt_buf_len points to the buffer length which we need later for the
27947c478bdstevel@tonic-gate * setsockopt() call, and opt_bufpp points to the newly allocated or already
27957c478bdstevel@tonic-gate * passed buffer. Returns B_FALSE if a library function call fails or passed
27967c478bdstevel@tonic-gate * buffer is not big enough, B_TRUE otherwise.
27977c478bdstevel@tonic-gate */
27987c478bdstevel@tonic-gatestatic boolean_t
27997c478bdstevel@tonic-gateprepare_optbuf(struct gateway *gw_addrs, int num_gw, char **opt_bufpp,
28007c478bdstevel@tonic-gate    size_t *opt_buf_len, struct in_addr *target, uchar_t src_rtng_type)
28017c478bdstevel@tonic-gate{
28027c478bdstevel@tonic-gate	struct ip_sourceroute *sr_opt;
28037c478bdstevel@tonic-gate	size_t needed_buflen;
28047c478bdstevel@tonic-gate	int i;
28057c478bdstevel@tonic-gate
28067c478bdstevel@tonic-gate	/*
28077c478bdstevel@tonic-gate	 * We have (num_gw + 1) IP addresses in the buffer because the number
28087c478bdstevel@tonic-gate	 * of gateway addresses we put in the option buffer includes the target
28097c478bdstevel@tonic-gate	 * address.
28107c478bdstevel@tonic-gate	 * At the time of setsockopt() call, passed option length needs to be
28117c478bdstevel@tonic-gate	 * multiple of 4 bytes. Therefore we need one IPOPT_NOP before (or