xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/telnet/commands.c (revision 905213bbae11fe989ca214e71ef9a970b403ea0e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1990, 1993
37c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
67c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
77c478bd9Sstevel@tonic-gate  * are met:
87c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
97c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
107c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
117c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
127c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
137c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
147c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
157c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
167c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
177c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
187c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
197c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
327c478bd9Sstevel@tonic-gate  *
33740638c8Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
347c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/param.h>
387c478bd9Sstevel@tonic-gate #include <sys/file.h>
397c478bd9Sstevel@tonic-gate #include <sys/socket.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <signal.h>
447c478bd9Sstevel@tonic-gate #include <netdb.h>
457c478bd9Sstevel@tonic-gate #include <ctype.h>
467c478bd9Sstevel@tonic-gate #include <pwd.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <strings.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <arpa/telnet.h>
517c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include "general.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include "ring.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include "externs.h"
587c478bd9Sstevel@tonic-gate #include "defines.h"
597c478bd9Sstevel@tonic-gate #include "types.h"
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate extern	char *telnet_krb5_realm;
627c478bd9Sstevel@tonic-gate extern	void krb5_profile_get_options(char *, char *,
637c478bd9Sstevel@tonic-gate 		profile_options_boolean*);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <k5-int.h>
667c478bd9Sstevel@tonic-gate #include <profile/prof_int.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate profile_options_boolean config_file_options[] = {
697c478bd9Sstevel@tonic-gate 	{ "forwardable", &forwardable_flag, 0},
707c478bd9Sstevel@tonic-gate 	{ "forward", &forward_flag, 0},
717c478bd9Sstevel@tonic-gate 	{ "encrypt", &encrypt_flag, 0 },
727c478bd9Sstevel@tonic-gate 	{ "autologin", &autologin, 0 },
737c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0}
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Number of maximum IPv4 gateways user can specify. This number is limited by
807c478bd9Sstevel@tonic-gate  * the maximum size of the IPv4 options in the IPv4 header.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate #define	MAX_GATEWAY	8
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Number of maximum IPv6 gateways user can specify. This number is limited by
857c478bd9Sstevel@tonic-gate  * the maximum header extension length of the IPv6 routing header.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate #define	MAX_GATEWAY6	127
887c478bd9Sstevel@tonic-gate #define	MAXMAX_GATEWAY	MAX(MAX_GATEWAY, MAX_GATEWAY6)
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Depending on the address resolutions of the target and gateways,
927c478bd9Sstevel@tonic-gate  * we determine which addresses of the target we'll try connecting to.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate #define	ALL_ADDRS	0	/* try all addrs of target */
957c478bd9Sstevel@tonic-gate #define	ONLY_V4		1	/* try only IPv4 addrs of target */
967c478bd9Sstevel@tonic-gate #define	ONLY_V6		2	/* try only IPv6 addrs of target */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #if defined(USE_TOS)
997c478bd9Sstevel@tonic-gate int tos = -1;
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate char	*hostname;
1037c478bd9Sstevel@tonic-gate static char _hostname[MAXHOSTNAMELEN];
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static int send_tncmd(void (*func)(), char *, char *);
1067c478bd9Sstevel@tonic-gate static void call(int n_ptrs, ...);
1077c478bd9Sstevel@tonic-gate static int cmdrc(char *, char *);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate typedef struct {
1107c478bd9Sstevel@tonic-gate 	char	*name;		/* command name */
1117c478bd9Sstevel@tonic-gate 	char	*help;		/* help string (NULL for no help) */
1127c478bd9Sstevel@tonic-gate 	int	(*handler)();	/* routine which executes command */
1137c478bd9Sstevel@tonic-gate 	int	needconnect;	/* Do we need to be connected to execute? */
1147c478bd9Sstevel@tonic-gate } Command;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * storage for IPv6 and/or IPv4 addresses of gateways
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate struct gateway {
1207c478bd9Sstevel@tonic-gate 	struct in6_addr	gw_addr6;
1217c478bd9Sstevel@tonic-gate 	struct in_addr	gw_addr;
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * IPv4 source routing option.
1267c478bd9Sstevel@tonic-gate  * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs
1277c478bd9Sstevel@tonic-gate  * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr.
1287c478bd9Sstevel@tonic-gate  * If it were defined as "struct in_addr ipsr_addrs[1]", "ipsr_ptr" would be
1297c478bd9Sstevel@tonic-gate  * followed by one byte of padding to avoid misaligned struct in_addr.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate struct ip_sourceroute {
1327c478bd9Sstevel@tonic-gate 	uint8_t ipsr_code;
1337c478bd9Sstevel@tonic-gate 	uint8_t ipsr_len;
1347c478bd9Sstevel@tonic-gate 	uint8_t ipsr_ptr;
1357c478bd9Sstevel@tonic-gate 	/* up to 9 IPv4 addresses */
1367c478bd9Sstevel@tonic-gate 	uint8_t ipsr_addrs[1][sizeof (struct in_addr)];
1377c478bd9Sstevel@tonic-gate };
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static char *line = NULL;
1407c478bd9Sstevel@tonic-gate static unsigned linesize = 0;
1417c478bd9Sstevel@tonic-gate static int margc;
1427c478bd9Sstevel@tonic-gate static char **margv = NULL;
1437c478bd9Sstevel@tonic-gate static unsigned margvlen = 0;
1447c478bd9Sstevel@tonic-gate static int doing_rc = 0;   /* .telnetrc file is being read and processed */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static void
Close(int * fd)1477c478bd9Sstevel@tonic-gate Close(int *fd)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	if (*fd != -1) {
1507c478bd9Sstevel@tonic-gate 		(void) close(*fd);
1517c478bd9Sstevel@tonic-gate 		*fd = -1;
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate static void
Free(char ** p)1567c478bd9Sstevel@tonic-gate Free(char **p)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	if (*p != NULL) {
1597c478bd9Sstevel@tonic-gate 		free(*p);
1607c478bd9Sstevel@tonic-gate 		*p = NULL;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static void
FreeHostnameList(char * list[])1657c478bd9Sstevel@tonic-gate FreeHostnameList(char *list[])
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	unsigned i;
1687c478bd9Sstevel@tonic-gate 	for (i = 0; i <= MAXMAX_GATEWAY && list[i] != NULL; i++)
1697c478bd9Sstevel@tonic-gate 		Free(&list[i]);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #define	MARGV_CHUNK_SIZE 8
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static void
set_argv(str)1757c478bd9Sstevel@tonic-gate set_argv(str)
1767c478bd9Sstevel@tonic-gate char *str;
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	if (margc == margvlen) {
1797c478bd9Sstevel@tonic-gate 		char **newmargv;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		margvlen += MARGV_CHUNK_SIZE;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		if ((newmargv = realloc(margv, margvlen * sizeof (char *)))
1847c478bd9Sstevel@tonic-gate 			== NULL)
1857c478bd9Sstevel@tonic-gate 			ExitString("telnet: no space for arguments",
1867c478bd9Sstevel@tonic-gate 				EXIT_FAILURE);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		margv = newmargv;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	margv[margc] = str;
1927c478bd9Sstevel@tonic-gate 	if (str != NULL)
1937c478bd9Sstevel@tonic-gate 		margc++;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static void
makeargv()1977c478bd9Sstevel@tonic-gate makeargv()
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	char *cp, *cp2, c;
2007c478bd9Sstevel@tonic-gate 	boolean_t shellcmd = B_FALSE;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	margc = 0;
2037c478bd9Sstevel@tonic-gate 	cp = line;
2047c478bd9Sstevel@tonic-gate 	if (*cp == '!') {		/* Special case shell escape */
2057c478bd9Sstevel@tonic-gate 		set_argv("!");		/* No room in string to get this */
2067c478bd9Sstevel@tonic-gate 		cp++;
2077c478bd9Sstevel@tonic-gate 		shellcmd = B_TRUE;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 	while ((c = *cp) != '\0') {
210*905213bbSToomas Soome 		int inquote = 0;
2117c478bd9Sstevel@tonic-gate 		while (isspace(c))
2127c478bd9Sstevel@tonic-gate 			c = *++cp;
2137c478bd9Sstevel@tonic-gate 		if (c == '\0')
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 		set_argv(cp);
2167c478bd9Sstevel@tonic-gate 		/*
2177c478bd9Sstevel@tonic-gate 		 * For the shell escape, put the rest of the line, less
2187c478bd9Sstevel@tonic-gate 		 * leading space, into a single argument, breaking out from
2197c478bd9Sstevel@tonic-gate 		 * the loop to prevent the rest of the line being split up
2207c478bd9Sstevel@tonic-gate 		 * into smaller arguments.
2217c478bd9Sstevel@tonic-gate 		 */
2227c478bd9Sstevel@tonic-gate 		if (shellcmd)
2237c478bd9Sstevel@tonic-gate 			break;
2247c478bd9Sstevel@tonic-gate 		for (cp2 = cp; c != '\0'; c = *++cp) {
2257c478bd9Sstevel@tonic-gate 			if (inquote) {
2267c478bd9Sstevel@tonic-gate 				if (c == inquote) {
2277c478bd9Sstevel@tonic-gate 					inquote = 0;
2287c478bd9Sstevel@tonic-gate 					continue;
2297c478bd9Sstevel@tonic-gate 				}
2307c478bd9Sstevel@tonic-gate 			} else {
2317c478bd9Sstevel@tonic-gate 				if (c == '\\') {
2327c478bd9Sstevel@tonic-gate 					if ((c = *++cp) == '\0')
2337c478bd9Sstevel@tonic-gate 						break;
2347c478bd9Sstevel@tonic-gate 				} else if (c == '"') {
2357c478bd9Sstevel@tonic-gate 					inquote = '"';
2367c478bd9Sstevel@tonic-gate 					continue;
2377c478bd9Sstevel@tonic-gate 				} else if (c == '\'') {
2387c478bd9Sstevel@tonic-gate 					inquote = '\'';
2397c478bd9Sstevel@tonic-gate 					continue;
2407c478bd9Sstevel@tonic-gate 				} else if (isspace(c))
2417c478bd9Sstevel@tonic-gate 					break;
2427c478bd9Sstevel@tonic-gate 			}
2437c478bd9Sstevel@tonic-gate 			*cp2++ = c;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 		*cp2 = '\0';
2467c478bd9Sstevel@tonic-gate 		if (c == '\0')
2477c478bd9Sstevel@tonic-gate 			break;
2487c478bd9Sstevel@tonic-gate 		cp++;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	set_argv((char *)NULL);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * Make a character string into a number.
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1).
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate 
259740638c8Sbw static int
special(char * s)260*905213bbSToomas Soome special(char *s)
2617c478bd9Sstevel@tonic-gate {
262*905213bbSToomas Soome 	char c;
2637c478bd9Sstevel@tonic-gate 	char b;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	switch (*s) {
2667c478bd9Sstevel@tonic-gate 	case '^':
2677c478bd9Sstevel@tonic-gate 		b = *++s;
2687c478bd9Sstevel@tonic-gate 		if (b == '?') {
2697c478bd9Sstevel@tonic-gate 		    c = b | 0x40;		/* DEL */
2707c478bd9Sstevel@tonic-gate 		} else {
2717c478bd9Sstevel@tonic-gate 		    c = b & 0x1f;
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 		break;
2747c478bd9Sstevel@tonic-gate 	default:
2757c478bd9Sstevel@tonic-gate 		c = *s;
2767c478bd9Sstevel@tonic-gate 		break;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	return (c);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate  * Construct a control character sequence
2837c478bd9Sstevel@tonic-gate  * for a special character.
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate static char *
control(cc_t c)286*905213bbSToomas Soome control(cc_t c)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	static char buf[5];
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * The only way I could get the Sun 3.5 compiler
2917c478bd9Sstevel@tonic-gate 	 * to shut up about
2927c478bd9Sstevel@tonic-gate 	 *	if ((unsigned int)c >= 0x80)
2937c478bd9Sstevel@tonic-gate 	 * was to assign "c" to an unsigned int variable...
2947c478bd9Sstevel@tonic-gate 	 * Arggg....
2957c478bd9Sstevel@tonic-gate 	 */
296*905213bbSToomas Soome 	unsigned int uic = (unsigned int)c;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (uic == 0x7f)
2997c478bd9Sstevel@tonic-gate 		return ("^?");
3007c478bd9Sstevel@tonic-gate 	if (c == (cc_t)_POSIX_VDISABLE) {
3017c478bd9Sstevel@tonic-gate 		return ("off");
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 	if (uic >= 0x80) {
3047c478bd9Sstevel@tonic-gate 		buf[0] = '\\';
3057c478bd9Sstevel@tonic-gate 		buf[1] = ((c>>6)&07) + '0';
3067c478bd9Sstevel@tonic-gate 		buf[2] = ((c>>3)&07) + '0';
3077c478bd9Sstevel@tonic-gate 		buf[3] = (c&07) + '0';
3087c478bd9Sstevel@tonic-gate 		buf[4] = 0;
3097c478bd9Sstevel@tonic-gate 	} else if (uic >= 0x20) {
3107c478bd9Sstevel@tonic-gate 		buf[0] = c;
3117c478bd9Sstevel@tonic-gate 		buf[1] = 0;
3127c478bd9Sstevel@tonic-gate 	} else {
3137c478bd9Sstevel@tonic-gate 		buf[0] = '^';
3147c478bd9Sstevel@tonic-gate 		buf[1] = '@'+c;
3157c478bd9Sstevel@tonic-gate 		buf[2] = 0;
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	return (buf);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * Same as control() except that its only used for escape handling, which uses
3227c478bd9Sstevel@tonic-gate  * _POSIX_VDISABLE differently and is aided by the use of the state variable
3237c478bd9Sstevel@tonic-gate  * escape_valid.
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate 	static char *
esc_control(cc_t c)326*905213bbSToomas Soome esc_control(cc_t c)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	static char buf[5];
3297c478bd9Sstevel@tonic-gate 	/*
3307c478bd9Sstevel@tonic-gate 	 * The only way I could get the Sun 3.5 compiler
3317c478bd9Sstevel@tonic-gate 	 * to shut up about
3327c478bd9Sstevel@tonic-gate 	 *	if ((unsigned int)c >= 0x80)
3337c478bd9Sstevel@tonic-gate 	 * was to assign "c" to an unsigned int variable...
3347c478bd9Sstevel@tonic-gate 	 * Arggg....
3357c478bd9Sstevel@tonic-gate 	 */
336*905213bbSToomas Soome 	unsigned int uic = (unsigned int)c;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if (escape_valid == B_FALSE)
3397c478bd9Sstevel@tonic-gate 		return ("off");
3407c478bd9Sstevel@tonic-gate 	if (uic == 0x7f)
3417c478bd9Sstevel@tonic-gate 		return ("^?");
3427c478bd9Sstevel@tonic-gate 	if (uic >= 0x80) {
3437c478bd9Sstevel@tonic-gate 		buf[0] = '\\';
3447c478bd9Sstevel@tonic-gate 		buf[1] = ((c>>6)&07) + '0';
3457c478bd9Sstevel@tonic-gate 		buf[2] = ((c>>3)&07) + '0';
3467c478bd9Sstevel@tonic-gate 		buf[3] = (c&07) + '0';
3477c478bd9Sstevel@tonic-gate 		buf[4] = 0;
3487c478bd9Sstevel@tonic-gate 	} else if (uic >= 0x20) {
3497c478bd9Sstevel@tonic-gate 		buf[0] = c;
3507c478bd9Sstevel@tonic-gate 		buf[1] = 0;
3517c478bd9Sstevel@tonic-gate 	} else {
3527c478bd9Sstevel@tonic-gate 		buf[0] = '^';
3537c478bd9Sstevel@tonic-gate 		buf[1] = '@'+c;
3547c478bd9Sstevel@tonic-gate 		buf[2] = 0;
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 	return (buf);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  *	The following are data structures and routines for
3617c478bd9Sstevel@tonic-gate  *	the "send" command.
3627c478bd9Sstevel@tonic-gate  *
3637c478bd9Sstevel@tonic-gate  */
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate struct sendlist {
3667c478bd9Sstevel@tonic-gate 	char	*name;		/* How user refers to it (case independent) */
3677c478bd9Sstevel@tonic-gate 	char	*help;		/* Help information (0 ==> no help) */
3687c478bd9Sstevel@tonic-gate 	int	needconnect;	/* Need to be connected */
3697c478bd9Sstevel@tonic-gate 	int	narg;		/* Number of arguments */
3707c478bd9Sstevel@tonic-gate 	int	(*handler)();	/* Routine to perform (for special ops) */
3717c478bd9Sstevel@tonic-gate 	int	nbyte;		/* Number of bytes to send this command */
3727c478bd9Sstevel@tonic-gate 	int	what;		/* Character to be sent (<0 ==> special) */
3737c478bd9Sstevel@tonic-gate };
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate static int send_esc(void);
3777c478bd9Sstevel@tonic-gate static int send_help(void);
3787c478bd9Sstevel@tonic-gate static int send_docmd(char *);
3797c478bd9Sstevel@tonic-gate static int send_dontcmd(char *);
3807c478bd9Sstevel@tonic-gate static int send_willcmd(char *);
3817c478bd9Sstevel@tonic-gate static int send_wontcmd(char *);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static struct sendlist Sendlist[] = {
3847c478bd9Sstevel@tonic-gate 	{ "ao",	"Send Telnet Abort output",		1, 0, 0, 2, AO },
3857c478bd9Sstevel@tonic-gate 	{ "ayt", "Send Telnet 'Are You There'",		1, 0, 0, 2, AYT },
3867c478bd9Sstevel@tonic-gate 	{ "b", 0,					1, 0, 0, 2, BREAK },
3877c478bd9Sstevel@tonic-gate 	{ "br", 0,					1, 0, 0, 2, BREAK },
3887c478bd9Sstevel@tonic-gate 	{ "break", 0,					1, 0, 0, 2, BREAK },
3897c478bd9Sstevel@tonic-gate 	{ "brk", "Send Telnet Break",			1, 0, 0, 2, BREAK },
3907c478bd9Sstevel@tonic-gate 	{ "ec",	"Send Telnet Erase Character",		1, 0, 0, 2, EC },
3917c478bd9Sstevel@tonic-gate 	{ "el",	"Send Telnet Erase Line",		1, 0, 0, 2, EL },
3927c478bd9Sstevel@tonic-gate 	{ "escape", "Send current escape character",	1, 0, send_esc, 1, 0 },
3937c478bd9Sstevel@tonic-gate 	{ "ga",	"Send Telnet 'Go Ahead' sequence",	1, 0, 0, 2, GA },
3947c478bd9Sstevel@tonic-gate 	{ "ip",	"Send Telnet Interrupt Process",	1, 0, 0, 2, IP },
3957c478bd9Sstevel@tonic-gate 	{ "intp", 0,					1, 0, 0, 2, IP },
3967c478bd9Sstevel@tonic-gate 	{ "interrupt", 0,				1, 0, 0, 2, IP },
3977c478bd9Sstevel@tonic-gate 	{ "intr",	0,				1, 0, 0, 2, IP },
3987c478bd9Sstevel@tonic-gate 	{ "nop", "Send Telnet 'No operation'",		1, 0, 0, 2, NOP },
3997c478bd9Sstevel@tonic-gate 	{ "eor", "Send Telnet 'End of Record'",		1, 0, 0, 2, EOR },
4007c478bd9Sstevel@tonic-gate 	{ "abort", "Send Telnet 'Abort Process'",	1, 0, 0, 2, ABORT },
4017c478bd9Sstevel@tonic-gate 	{ "susp", "Send Telnet 'Suspend Process'",	1, 0, 0, 2, SUSP },
4027c478bd9Sstevel@tonic-gate 	{ "eof", "Send Telnet End of File Character",	1, 0, 0, 2, xEOF },
4037c478bd9Sstevel@tonic-gate 	{ "synch", "Perform Telnet 'Synch operation'",	1, 0, dosynch, 2, 0 },
4047c478bd9Sstevel@tonic-gate 	{ "getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0 },
4057c478bd9Sstevel@tonic-gate 	{ "?",	"Display send options",			0, 0, send_help, 0, 0 },
4067c478bd9Sstevel@tonic-gate 	{ "help",	0,				0, 0, send_help, 0, 0 },
4077c478bd9Sstevel@tonic-gate 	{ "do",	0,				0, 1, send_docmd, 3, 0 },
4087c478bd9Sstevel@tonic-gate 	{ "dont", 0,				0, 1, send_dontcmd, 3, 0 },
4097c478bd9Sstevel@tonic-gate 	{ "will", 0,				0, 1, send_willcmd, 3, 0 },
4107c478bd9Sstevel@tonic-gate 	{ "wont", 0,				0, 1, send_wontcmd, 3, 0 },
4117c478bd9Sstevel@tonic-gate 	{ 0 }
4127c478bd9Sstevel@tonic-gate };
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate #define	GETSEND(name) ((struct sendlist *)genget(name, (char **)Sendlist, \
4157c478bd9Sstevel@tonic-gate 				sizeof (struct sendlist)))
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate static int
sendcmd(argc,argv)4187c478bd9Sstevel@tonic-gate sendcmd(argc, argv)
4197c478bd9Sstevel@tonic-gate 	int  argc;
4207c478bd9Sstevel@tonic-gate 	char **argv;
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	int count;	/* how many bytes we are going to need to send */
4237c478bd9Sstevel@tonic-gate 	int i;
4247c478bd9Sstevel@tonic-gate 	struct sendlist *s;	/* pointer to current command */
4257c478bd9Sstevel@tonic-gate 	int success = 0;
4267c478bd9Sstevel@tonic-gate 	int needconnect = 0;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	if (argc < 2) {
4297c478bd9Sstevel@tonic-gate 		(void) printf(
4307c478bd9Sstevel@tonic-gate 		    "need at least one argument for 'send' command\n");
4317c478bd9Sstevel@tonic-gate 		(void) printf("'send ?' for help\n");
4327c478bd9Sstevel@tonic-gate 		return (0);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 	/*
4357c478bd9Sstevel@tonic-gate 	 * First, validate all the send arguments.
4367c478bd9Sstevel@tonic-gate 	 * In addition, we see how much space we are going to need, and
4377c478bd9Sstevel@tonic-gate 	 * whether or not we will be doing a "SYNCH" operation (which
4387c478bd9Sstevel@tonic-gate 	 * flushes the network queue).
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 	count = 0;
4417c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
4427c478bd9Sstevel@tonic-gate 		s = GETSEND(argv[i]);
4437c478bd9Sstevel@tonic-gate 		if (s == 0) {
4447c478bd9Sstevel@tonic-gate 			(void) printf("Unknown send argument '%s'\n'send ?' "
4457c478bd9Sstevel@tonic-gate 			    "for help.\n", argv[i]);
4467c478bd9Sstevel@tonic-gate 			return (0);
4477c478bd9Sstevel@tonic-gate 		} else if (Ambiguous(s)) {
4487c478bd9Sstevel@tonic-gate 			(void) printf("Ambiguous send argument '%s'\n'send ?' "
4497c478bd9Sstevel@tonic-gate 			    "for help.\n", argv[i]);
4507c478bd9Sstevel@tonic-gate 			return (0);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 		if (i + s->narg >= argc) {
4537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4547c478bd9Sstevel@tonic-gate 			    "Need %d argument%s to 'send %s' "
4557c478bd9Sstevel@tonic-gate 			    "command.  'send %s ?' for help.\n",
4567c478bd9Sstevel@tonic-gate 			    s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
4577c478bd9Sstevel@tonic-gate 			return (0);
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		count += s->nbyte;
4607c478bd9Sstevel@tonic-gate 		if (s->handler == send_help) {
4617c478bd9Sstevel@tonic-gate 			(void) send_help();
4627c478bd9Sstevel@tonic-gate 			return (0);
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		i += s->narg;
4667c478bd9Sstevel@tonic-gate 		needconnect += s->needconnect;
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	if (!connected && needconnect) {
4697c478bd9Sstevel@tonic-gate 		(void) printf("?Need to be connected first.\n");
4707c478bd9Sstevel@tonic-gate 		(void) printf("'send ?' for help\n");
4717c478bd9Sstevel@tonic-gate 		return (0);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 	/* Now, do we have enough room? */
4747c478bd9Sstevel@tonic-gate 	if (NETROOM() < count) {
4757c478bd9Sstevel@tonic-gate 		(void) printf("There is not enough room in the buffer "
4767c478bd9Sstevel@tonic-gate 		    "TO the network\n");
4777c478bd9Sstevel@tonic-gate 		(void) printf(
4787c478bd9Sstevel@tonic-gate 		    "to process your request.  Nothing will be done.\n");
4797c478bd9Sstevel@tonic-gate 		(void) printf("('send synch' will throw away most "
4807c478bd9Sstevel@tonic-gate 		    "data in the network\n");
4817c478bd9Sstevel@tonic-gate 		(void) printf("buffer, if this might help.)\n");
4827c478bd9Sstevel@tonic-gate 		return (0);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	/* OK, they are all OK, now go through again and actually send */
4857c478bd9Sstevel@tonic-gate 	count = 0;
4867c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
4877c478bd9Sstevel@tonic-gate 		if ((s = GETSEND(argv[i])) == 0) {
4887c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4897c478bd9Sstevel@tonic-gate 			    "Telnet 'send' error - argument disappeared!\n");
4907c478bd9Sstevel@tonic-gate 			(void) quit();
4917c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 		if (s->handler) {
4947c478bd9Sstevel@tonic-gate 			count++;
4957c478bd9Sstevel@tonic-gate 			success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
4967c478bd9Sstevel@tonic-gate 			    (s->narg > 1) ? argv[i+2] : 0);
4977c478bd9Sstevel@tonic-gate 			i += s->narg;
4987c478bd9Sstevel@tonic-gate 		} else {
4997c478bd9Sstevel@tonic-gate 			NET2ADD(IAC, s->what);
5007c478bd9Sstevel@tonic-gate 			printoption("SENT", IAC, s->what);
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 	return (count == success);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate static int
send_esc()5077c478bd9Sstevel@tonic-gate send_esc()
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	NETADD(escape);
5107c478bd9Sstevel@tonic-gate 	return (1);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate static int
send_docmd(name)5147c478bd9Sstevel@tonic-gate send_docmd(name)
5157c478bd9Sstevel@tonic-gate 	char *name;
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	return (send_tncmd(send_do, "do", name));
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate static int
send_dontcmd(name)5217c478bd9Sstevel@tonic-gate send_dontcmd(name)
5227c478bd9Sstevel@tonic-gate 	char *name;
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	return (send_tncmd(send_dont, "dont", name));
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate static int
send_willcmd(name)5287c478bd9Sstevel@tonic-gate send_willcmd(name)
5297c478bd9Sstevel@tonic-gate 	char *name;
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	return (send_tncmd(send_will, "will", name));
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate static int
send_wontcmd(name)5357c478bd9Sstevel@tonic-gate send_wontcmd(name)
5367c478bd9Sstevel@tonic-gate 	char *name;
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	return (send_tncmd(send_wont, "wont", name));
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate int
send_tncmd(void (* func)(),char * cmd,char * name)542*905213bbSToomas Soome send_tncmd(void (*func)(), char *cmd, char *name)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	char **cpp;
5457c478bd9Sstevel@tonic-gate 	extern char *telopts[];
546*905213bbSToomas Soome 	int val = 0;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	if (isprefix(name, "help") || isprefix(name, "?")) {
549*905213bbSToomas Soome 		int col, len;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		(void) printf("Usage: send %s <value|option>\n", cmd);
5527c478bd9Sstevel@tonic-gate 		(void) printf("\"value\" must be from 0 to 255\n");
5537c478bd9Sstevel@tonic-gate 		(void) printf("Valid options are:\n\t");
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		col = 8;
5567c478bd9Sstevel@tonic-gate 		for (cpp = telopts; *cpp; cpp++) {
5577c478bd9Sstevel@tonic-gate 			len = strlen(*cpp) + 3;
5587c478bd9Sstevel@tonic-gate 			if (col + len > 65) {
5597c478bd9Sstevel@tonic-gate 				(void) printf("\n\t");
5607c478bd9Sstevel@tonic-gate 				col = 8;
5617c478bd9Sstevel@tonic-gate 			}
5627c478bd9Sstevel@tonic-gate 			(void) printf(" \"%s\"", *cpp);
5637c478bd9Sstevel@tonic-gate 			col += len;
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 		(void) printf("\n");
5667c478bd9Sstevel@tonic-gate 		return (0);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 	cpp = (char **)genget(name, telopts, sizeof (char *));
5697c478bd9Sstevel@tonic-gate 	if (Ambiguous(cpp)) {
5707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5717c478bd9Sstevel@tonic-gate 		    "'%s': ambiguous argument ('send %s ?' for help).\n",
5727c478bd9Sstevel@tonic-gate 		    name, cmd);
5737c478bd9Sstevel@tonic-gate 		return (0);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 	if (cpp) {
5767c478bd9Sstevel@tonic-gate 		val = cpp - telopts;
5777c478bd9Sstevel@tonic-gate 	} else {
578*905213bbSToomas Soome 		char *cp = name;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		while (*cp >= '0' && *cp <= '9') {
5817c478bd9Sstevel@tonic-gate 			val *= 10;
5827c478bd9Sstevel@tonic-gate 			val += *cp - '0';
5837c478bd9Sstevel@tonic-gate 			cp++;
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 		if (*cp != 0) {
5867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5877c478bd9Sstevel@tonic-gate 			    "'%s': unknown argument ('send %s ?' for help).\n",
5887c478bd9Sstevel@tonic-gate 			    name, cmd);
5897c478bd9Sstevel@tonic-gate 			return (0);
5907c478bd9Sstevel@tonic-gate 		} else if (val < 0 || val > 255) {
5917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5927c478bd9Sstevel@tonic-gate 			    "'%s': bad value ('send %s ?' for help).\n",
5937c478bd9Sstevel@tonic-gate 			    name, cmd);
5947c478bd9Sstevel@tonic-gate 			return (0);
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	if (!connected) {
5987c478bd9Sstevel@tonic-gate 		(void) printf("?Need to be connected first.\n");
5997c478bd9Sstevel@tonic-gate 		return (0);
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 	(*func)(val, 1);
6027c478bd9Sstevel@tonic-gate 	return (1);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate static int
send_help()6067c478bd9Sstevel@tonic-gate send_help()
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	struct sendlist *s;	/* pointer to current command */
6097c478bd9Sstevel@tonic-gate 	for (s = Sendlist; s->name; s++) {
6107c478bd9Sstevel@tonic-gate 		if (s->help)
6117c478bd9Sstevel@tonic-gate 			(void) printf("%-15s %s\n", s->name, s->help);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 	return (0);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * The following are the routines and data structures referred
6187c478bd9Sstevel@tonic-gate  * to by the arguments to the "toggle" command.
6197c478bd9Sstevel@tonic-gate  */
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate static int
lclchars()6227c478bd9Sstevel@tonic-gate lclchars()
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	donelclchars = 1;
6257c478bd9Sstevel@tonic-gate 	return (1);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate static int
togdebug()6297c478bd9Sstevel@tonic-gate togdebug()
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	if (net > 0 &&
6327c478bd9Sstevel@tonic-gate 	    (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
6337c478bd9Sstevel@tonic-gate 		perror("setsockopt (SO_DEBUG)");
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	return (1);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate static int
togcrlf()6407c478bd9Sstevel@tonic-gate togcrlf()
6417c478bd9Sstevel@tonic-gate {
6427c478bd9Sstevel@tonic-gate 	if (crlf) {
6437c478bd9Sstevel@tonic-gate 		(void) printf(
6447c478bd9Sstevel@tonic-gate 		    "Will send carriage returns as telnet <CR><LF>.\n");
6457c478bd9Sstevel@tonic-gate 	} else {
6467c478bd9Sstevel@tonic-gate 		(void) printf(
6477c478bd9Sstevel@tonic-gate 		    "Will send carriage returns as telnet <CR><NUL>.\n");
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	return (1);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate static int binmode;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate static int
togbinary(val)6557c478bd9Sstevel@tonic-gate togbinary(val)
6567c478bd9Sstevel@tonic-gate 	int val;
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	donebinarytoggle = 1;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (val >= 0) {
6617c478bd9Sstevel@tonic-gate 		binmode = val;
6627c478bd9Sstevel@tonic-gate 	} else {
6637c478bd9Sstevel@tonic-gate 		if (my_want_state_is_will(TELOPT_BINARY) &&
6647c478bd9Sstevel@tonic-gate 		    my_want_state_is_do(TELOPT_BINARY)) {
6657c478bd9Sstevel@tonic-gate 			binmode = 1;
6667c478bd9Sstevel@tonic-gate 		} else if (my_want_state_is_wont(TELOPT_BINARY) &&
6677c478bd9Sstevel@tonic-gate 		    my_want_state_is_dont(TELOPT_BINARY)) {
6687c478bd9Sstevel@tonic-gate 			binmode = 0;
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 		val = binmode ? 0 : 1;
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if (val == 1) {
6747c478bd9Sstevel@tonic-gate 		if (my_want_state_is_will(TELOPT_BINARY) &&
6757c478bd9Sstevel@tonic-gate 		    my_want_state_is_do(TELOPT_BINARY)) {
6767c478bd9Sstevel@tonic-gate 			(void) printf("Already operating in binary mode "
6777c478bd9Sstevel@tonic-gate 			    "with remote host.\n");
6787c478bd9Sstevel@tonic-gate 		} else {
6797c478bd9Sstevel@tonic-gate 			(void) printf(
6807c478bd9Sstevel@tonic-gate 			    "Negotiating binary mode with remote host.\n");
6817c478bd9Sstevel@tonic-gate 			tel_enter_binary(3);
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 	} else {
6847c478bd9Sstevel@tonic-gate 		if (my_want_state_is_wont(TELOPT_BINARY) &&
6857c478bd9Sstevel@tonic-gate 		    my_want_state_is_dont(TELOPT_BINARY)) {
6867c478bd9Sstevel@tonic-gate 			(void) printf("Already in network ascii mode "
6877c478bd9Sstevel@tonic-gate 			    "with remote host.\n");
6887c478bd9Sstevel@tonic-gate 		} else {
6897c478bd9Sstevel@tonic-gate 			(void) printf("Negotiating network ascii mode "
6907c478bd9Sstevel@tonic-gate 			    "with remote host.\n");
6917c478bd9Sstevel@tonic-gate 			tel_leave_binary(3);
6927c478bd9Sstevel@tonic-gate 		}
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 	return (1);
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate static int
togrbinary(val)6987c478bd9Sstevel@tonic-gate togrbinary(val)
6997c478bd9Sstevel@tonic-gate 	int val;
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	donebinarytoggle = 1;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	if (val == -1)
7047c478bd9Sstevel@tonic-gate 		val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	if (val == 1) {
7077c478bd9Sstevel@tonic-gate 		if (my_want_state_is_do(TELOPT_BINARY)) {
7087c478bd9Sstevel@tonic-gate 			(void) printf("Already receiving in binary mode.\n");
7097c478bd9Sstevel@tonic-gate 		} else {
7107c478bd9Sstevel@tonic-gate 			(void) printf("Negotiating binary mode on input.\n");
7117c478bd9Sstevel@tonic-gate 			tel_enter_binary(1);
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 	} else {
7147c478bd9Sstevel@tonic-gate 		if (my_want_state_is_dont(TELOPT_BINARY)) {
7157c478bd9Sstevel@tonic-gate 			(void) printf(
7167c478bd9Sstevel@tonic-gate 			    "Already receiving in network ascii mode.\n");
7177c478bd9Sstevel@tonic-gate 		} else {
7187c478bd9Sstevel@tonic-gate 			(void) printf(
7197c478bd9Sstevel@tonic-gate 			    "Negotiating network ascii mode on input.\n");
7207c478bd9Sstevel@tonic-gate 			    tel_leave_binary(1);
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 	return (1);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate static int
togxbinary(val)7277c478bd9Sstevel@tonic-gate togxbinary(val)
7287c478bd9Sstevel@tonic-gate 	int val;
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	donebinarytoggle = 1;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	if (val == -1)
7337c478bd9Sstevel@tonic-gate 		val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	if (val == 1) {
7367c478bd9Sstevel@tonic-gate 		if (my_want_state_is_will(TELOPT_BINARY)) {
7377c478bd9Sstevel@tonic-gate 			(void) printf("Already transmitting in binary mode.\n");
7387c478bd9Sstevel@tonic-gate 		} else {
7397c478bd9Sstevel@tonic-gate 			(void) printf("Negotiating binary mode on output.\n");
7407c478bd9Sstevel@tonic-gate 			tel_enter_binary(2);
7417c478bd9Sstevel@tonic-gate 		}
7427c478bd9Sstevel@tonic-gate 	} else {
7437c478bd9Sstevel@tonic-gate 		if (my_want_state_is_wont(TELOPT_BINARY)) {
7447c478bd9Sstevel@tonic-gate 			(void) printf(
7457c478bd9Sstevel@tonic-gate 			    "Already transmitting in network ascii mode.\n");
7467c478bd9Sstevel@tonic-gate 		} else {
7477c478bd9Sstevel@tonic-gate 			(void) printf(
7487c478bd9Sstevel@tonic-gate 			    "Negotiating network ascii mode on output.\n");
7497c478bd9Sstevel@tonic-gate 			tel_leave_binary(2);
7507c478bd9Sstevel@tonic-gate 		}
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 	return (1);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate static int togglehelp(void);
7577c478bd9Sstevel@tonic-gate extern int auth_togdebug(int);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate struct togglelist {
7607c478bd9Sstevel@tonic-gate 	char	*name;		/* name of toggle */
7617c478bd9Sstevel@tonic-gate 	char	*help;		/* help message */
7627c478bd9Sstevel@tonic-gate 	int	(*handler)();	/* routine to do actual setting */
7637c478bd9Sstevel@tonic-gate 	int	*variable;
7647c478bd9Sstevel@tonic-gate 	char	*actionexplanation;
7657c478bd9Sstevel@tonic-gate };
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate static struct togglelist Togglelist[] = {
7687c478bd9Sstevel@tonic-gate 	{ "autoflush",
7697c478bd9Sstevel@tonic-gate 	"flushing of output when sending interrupt characters",
7707c478bd9Sstevel@tonic-gate 	    0,
7717c478bd9Sstevel@tonic-gate 		&autoflush,
7727c478bd9Sstevel@tonic-gate 		    "flush output when sending interrupt characters" },
7737c478bd9Sstevel@tonic-gate 	{ "autosynch",
7747c478bd9Sstevel@tonic-gate 	"automatic sending of interrupt characters in urgent mode",
7757c478bd9Sstevel@tonic-gate 	    0,
7767c478bd9Sstevel@tonic-gate 		&autosynch,
7777c478bd9Sstevel@tonic-gate 		    "send interrupt characters in urgent mode" },
7787c478bd9Sstevel@tonic-gate 	{ "autologin",
7797c478bd9Sstevel@tonic-gate 	"automatic sending of login and/or authentication info",
7807c478bd9Sstevel@tonic-gate 	    0,
7817c478bd9Sstevel@tonic-gate 		&autologin,
7827c478bd9Sstevel@tonic-gate 		    "send login name and/or authentication information" },
7837c478bd9Sstevel@tonic-gate 	{ "authdebug",
7847c478bd9Sstevel@tonic-gate 	"authentication debugging",
7857c478bd9Sstevel@tonic-gate 	    auth_togdebug,
7867c478bd9Sstevel@tonic-gate 		0,
7877c478bd9Sstevel@tonic-gate 		    "print authentication debugging information" },
7887c478bd9Sstevel@tonic-gate 	{ "autoencrypt",
7897c478bd9Sstevel@tonic-gate 	"automatic encryption of data stream",
7907c478bd9Sstevel@tonic-gate 	    EncryptAutoEnc,
7917c478bd9Sstevel@tonic-gate 		0,
7927c478bd9Sstevel@tonic-gate 		    "automatically encrypt output" },
7937c478bd9Sstevel@tonic-gate 	{ "autodecrypt",
7947c478bd9Sstevel@tonic-gate 	"automatic decryption of data stream",
7957c478bd9Sstevel@tonic-gate 	    EncryptAutoDec,
7967c478bd9Sstevel@tonic-gate 		0,
7977c478bd9Sstevel@tonic-gate 		    "automatically decrypt input" },
7987c478bd9Sstevel@tonic-gate 	{ "verbose_encrypt",
7997c478bd9Sstevel@tonic-gate 	"verbose encryption output",
8007c478bd9Sstevel@tonic-gate 	    EncryptVerbose,
8017c478bd9Sstevel@tonic-gate 		0,
8027c478bd9Sstevel@tonic-gate 		    "print verbose encryption output" },
8037c478bd9Sstevel@tonic-gate 	{ "encdebug",
8047c478bd9Sstevel@tonic-gate 	"encryption debugging",
8057c478bd9Sstevel@tonic-gate 	    EncryptDebug,
8067c478bd9Sstevel@tonic-gate 		0,
8077c478bd9Sstevel@tonic-gate 		    "print encryption debugging information" },
8087c478bd9Sstevel@tonic-gate 	{ "skiprc",
8097c478bd9Sstevel@tonic-gate 	"don't read ~/.telnetrc file",
8107c478bd9Sstevel@tonic-gate 	    0,
8117c478bd9Sstevel@tonic-gate 		&skiprc,
8127c478bd9Sstevel@tonic-gate 		    "skip reading of ~/.telnetrc file" },
8137c478bd9Sstevel@tonic-gate 	{ "binary",
8147c478bd9Sstevel@tonic-gate 	"sending and receiving of binary data",
8157c478bd9Sstevel@tonic-gate 	    togbinary,
8167c478bd9Sstevel@tonic-gate 		0,
8177c478bd9Sstevel@tonic-gate 		    0 },
8187c478bd9Sstevel@tonic-gate 	{ "inbinary",
8197c478bd9Sstevel@tonic-gate 	"receiving of binary data",
8207c478bd9Sstevel@tonic-gate 	    togrbinary,
8217c478bd9Sstevel@tonic-gate 		0,
8227c478bd9Sstevel@tonic-gate 		    0 },
8237c478bd9Sstevel@tonic-gate 	{ "outbinary",
8247c478bd9Sstevel@tonic-gate 	"sending of binary data",
8257c478bd9Sstevel@tonic-gate 	    togxbinary,
8267c478bd9Sstevel@tonic-gate 		0,
8277c478bd9Sstevel@tonic-gate 		    0 },
8287c478bd9Sstevel@tonic-gate 	{ "crlf",
8297c478bd9Sstevel@tonic-gate 	"sending carriage returns as telnet <CR><LF>",
8307c478bd9Sstevel@tonic-gate 	    togcrlf,
8317c478bd9Sstevel@tonic-gate 		&crlf,
8327c478bd9Sstevel@tonic-gate 		    0 },
8337c478bd9Sstevel@tonic-gate 	{ "crmod",
8347c478bd9Sstevel@tonic-gate 	"mapping of received carriage returns",
8357c478bd9Sstevel@tonic-gate 	    0,
8367c478bd9Sstevel@tonic-gate 		&crmod,
8377c478bd9Sstevel@tonic-gate 		    "map carriage return on output" },
8387c478bd9Sstevel@tonic-gate 	{ "localchars",
8397c478bd9Sstevel@tonic-gate 	"local recognition of certain control characters",
8407c478bd9Sstevel@tonic-gate 	    lclchars,
8417c478bd9Sstevel@tonic-gate 		&localchars,
8427c478bd9Sstevel@tonic-gate 		    "recognize certain control characters" },
8437c478bd9Sstevel@tonic-gate 	{ " ", "", 0 },		/* empty line */
8447c478bd9Sstevel@tonic-gate 	{ "debug",
8457c478bd9Sstevel@tonic-gate 	"debugging",
8467c478bd9Sstevel@tonic-gate 	    togdebug,
8477c478bd9Sstevel@tonic-gate 		&debug,
8487c478bd9Sstevel@tonic-gate 		    "turn on socket level debugging" },
8497c478bd9Sstevel@tonic-gate 	{ "netdata",
8507c478bd9Sstevel@tonic-gate 	"printing of hexadecimal network data (debugging)",
8517c478bd9Sstevel@tonic-gate 	    0,
8527c478bd9Sstevel@tonic-gate 		&netdata,
8537c478bd9Sstevel@tonic-gate 		    "print hexadecimal representation of network traffic" },
8547c478bd9Sstevel@tonic-gate 	{ "prettydump",
8557c478bd9Sstevel@tonic-gate 	"output of \"netdata\" to user readable format (debugging)",
8567c478bd9Sstevel@tonic-gate 	    0,
8577c478bd9Sstevel@tonic-gate 		&prettydump,
8587c478bd9Sstevel@tonic-gate 		    "print user readable output for \"netdata\"" },
8597c478bd9Sstevel@tonic-gate 	{ "options",
8607c478bd9Sstevel@tonic-gate 	"viewing of options processing (debugging)",
8617c478bd9Sstevel@tonic-gate 	    0,
8627c478bd9Sstevel@tonic-gate 		&showoptions,
8637c478bd9Sstevel@tonic-gate 		    "show option processing" },
8647c478bd9Sstevel@tonic-gate 	{ "termdata",
8657c478bd9Sstevel@tonic-gate 	"(debugging) toggle printing of hexadecimal terminal data",
8667c478bd9Sstevel@tonic-gate 	    0,
8677c478bd9Sstevel@tonic-gate 		&termdata,
8687c478bd9Sstevel@tonic-gate 		    "print hexadecimal representation of terminal traffic" },
8697c478bd9Sstevel@tonic-gate 	{ "?",
8707c478bd9Sstevel@tonic-gate 	0,
8717c478bd9Sstevel@tonic-gate 	    togglehelp },
8727c478bd9Sstevel@tonic-gate 	{ "help",
8737c478bd9Sstevel@tonic-gate 	0,
8747c478bd9Sstevel@tonic-gate 	    togglehelp },
8757c478bd9Sstevel@tonic-gate 	{ 0 }
8767c478bd9Sstevel@tonic-gate };
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate static int
togglehelp()8797c478bd9Sstevel@tonic-gate togglehelp()
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	struct togglelist *c;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	for (c = Togglelist; c->name; c++) {
8847c478bd9Sstevel@tonic-gate 		if (c->help) {
8857c478bd9Sstevel@tonic-gate 			if (*c->help)
8867c478bd9Sstevel@tonic-gate 				(void) printf(
8877c478bd9Sstevel@tonic-gate 					"%-15s toggle %s\n", c->name, c->help);
8887c478bd9Sstevel@tonic-gate 			else
8897c478bd9Sstevel@tonic-gate 				(void) printf("\n");
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 	(void) printf("\n");
8937c478bd9Sstevel@tonic-gate 	(void) printf("%-15s %s\n", "?", "display help information");
8947c478bd9Sstevel@tonic-gate 	return (0);
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate static void
settogglehelp(set)8987c478bd9Sstevel@tonic-gate settogglehelp(set)
8997c478bd9Sstevel@tonic-gate 	int set;
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	struct togglelist *c;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	for (c = Togglelist; c->name; c++) {
9047c478bd9Sstevel@tonic-gate 		if (c->help) {
9057c478bd9Sstevel@tonic-gate 			if (*c->help)
9067c478bd9Sstevel@tonic-gate 				(void) printf("%-15s %s %s\n", c->name,
9077c478bd9Sstevel@tonic-gate 				    set ? "enable" : "disable", c->help);
9087c478bd9Sstevel@tonic-gate 			else
9097c478bd9Sstevel@tonic-gate 				(void) printf("\n");
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate #define	GETTOGGLE(name) (struct togglelist *) \
9157c478bd9Sstevel@tonic-gate 		genget(name, (char **)Togglelist, sizeof (struct togglelist))
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate static int
toggle(argc,argv)9187c478bd9Sstevel@tonic-gate toggle(argc, argv)
9197c478bd9Sstevel@tonic-gate 	int  argc;
9207c478bd9Sstevel@tonic-gate 	char *argv[];
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate 	int retval = 1;
9237c478bd9Sstevel@tonic-gate 	char *name;
9247c478bd9Sstevel@tonic-gate 	struct togglelist *c;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	if (argc < 2) {
9277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9287c478bd9Sstevel@tonic-gate 		    "Need an argument to 'toggle' command.  "
9297c478bd9Sstevel@tonic-gate 		    "'toggle ?' for help.\n");
9307c478bd9Sstevel@tonic-gate 		return (0);
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 	argc--;
9337c478bd9Sstevel@tonic-gate 	argv++;
9347c478bd9Sstevel@tonic-gate 	while (argc--) {
9357c478bd9Sstevel@tonic-gate 		name = *argv++;
9367c478bd9Sstevel@tonic-gate 		c = GETTOGGLE(name);
9377c478bd9Sstevel@tonic-gate 		if (Ambiguous(c)) {
9387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "'%s': ambiguous argument "
9397c478bd9Sstevel@tonic-gate 			    "('toggle ?' for help).\n", name);
9407c478bd9Sstevel@tonic-gate 			return (0);
9417c478bd9Sstevel@tonic-gate 		} else if (c == 0) {
9427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "'%s': unknown argument "
9437c478bd9Sstevel@tonic-gate 			    "('toggle ?' for help).\n", name);
9447c478bd9Sstevel@tonic-gate 			return (0);
9457c478bd9Sstevel@tonic-gate 		} else {
9467c478bd9Sstevel@tonic-gate 			if (c->variable) {
9477c478bd9Sstevel@tonic-gate 				*c->variable = !*c->variable;	/* invert it */
9487c478bd9Sstevel@tonic-gate 				if (c->actionexplanation) {
9497c478bd9Sstevel@tonic-gate 			(void) printf("%s %s.\n",
9507c478bd9Sstevel@tonic-gate 				*c->variable ? "Will" : "Won't",
9517c478bd9Sstevel@tonic-gate 							c->actionexplanation);
9527c478bd9Sstevel@tonic-gate 				}
9537c478bd9Sstevel@tonic-gate 			}
9547c478bd9Sstevel@tonic-gate 			if (c->handler) {
9557c478bd9Sstevel@tonic-gate 				retval &= (*c->handler)(-1);
9567c478bd9Sstevel@tonic-gate 			}
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 	return (retval);
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate /*
9637c478bd9Sstevel@tonic-gate  * The following perform the "set" command.
9647c478bd9Sstevel@tonic-gate  */
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate #ifdef	USE_TERMIO
9677c478bd9Sstevel@tonic-gate struct termio new_tc = { 0 };
9687c478bd9Sstevel@tonic-gate #endif
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate struct setlist {
9717c478bd9Sstevel@tonic-gate 	char *name;		/* name */
9727c478bd9Sstevel@tonic-gate 	char *help;		/* help information */
9737c478bd9Sstevel@tonic-gate 	void (*handler)();
9747c478bd9Sstevel@tonic-gate 	cc_t *charp;		/* where it is located at */
9757c478bd9Sstevel@tonic-gate };
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate static struct setlist Setlist[] = {
9787c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
9797c478bd9Sstevel@tonic-gate 	{ "echo",  "character to toggle local echoing on/off", 0, &echoc },
9807c478bd9Sstevel@tonic-gate #endif
9817c478bd9Sstevel@tonic-gate 	{ "escape", "character to escape back to telnet command mode", 0,
9827c478bd9Sstevel@tonic-gate 	    &escape },
9837c478bd9Sstevel@tonic-gate 	{ "rlogin", "rlogin escape character", 0, &rlogin },
9847c478bd9Sstevel@tonic-gate 	{ "tracefile", "file to write trace information to", SetNetTrace,
9857c478bd9Sstevel@tonic-gate 	    (cc_t *)NetTraceFile},
9867c478bd9Sstevel@tonic-gate 	{ " ", "" },
9877c478bd9Sstevel@tonic-gate 	{ " ", "The following need 'localchars' to be toggled true", 0, 0 },
9887c478bd9Sstevel@tonic-gate 	{ "flushoutput", "character to cause an Abort Output", 0,
9897c478bd9Sstevel@tonic-gate 	    termFlushCharp },
9907c478bd9Sstevel@tonic-gate 	{ "interrupt", "character to cause an Interrupt Process", 0,
9917c478bd9Sstevel@tonic-gate 	    termIntCharp },
9927c478bd9Sstevel@tonic-gate 	{ "quit", "character to cause an Abort process", 0, termQuitCharp },
9937c478bd9Sstevel@tonic-gate 	{ "eof", "character to cause an EOF ", 0, termEofCharp },
9947c478bd9Sstevel@tonic-gate 	{ " ", "" },
9957c478bd9Sstevel@tonic-gate 	{ " ", "The following are for local editing in linemode", 0, 0 },
9967c478bd9Sstevel@tonic-gate 	{ "erase", "character to use to erase a character", 0, termEraseCharp },
9977c478bd9Sstevel@tonic-gate 	{ "kill", "character to use to erase a line", 0, termKillCharp },
9987c478bd9Sstevel@tonic-gate 	{ "lnext", "character to use for literal next", 0,
9997c478bd9Sstevel@tonic-gate 	    termLiteralNextCharp },
10007c478bd9Sstevel@tonic-gate 	{ "susp", "character to cause a Suspend Process", 0, termSuspCharp },
10017c478bd9Sstevel@tonic-gate 	{ "reprint", "character to use for line reprint", 0, termRprntCharp },
10027c478bd9Sstevel@tonic-gate 	{ "worderase", "character to use to erase a word", 0, termWerasCharp },
10037c478bd9Sstevel@tonic-gate 	{ "start",	"character to use for XON", 0, termStartCharp },
10047c478bd9Sstevel@tonic-gate 	{ "stop",	"character to use for XOFF", 0, termStopCharp },
10057c478bd9Sstevel@tonic-gate 	{ "forw1",	"alternate end of line character", 0, termForw1Charp },
10067c478bd9Sstevel@tonic-gate 	{ "forw2",	"alternate end of line character", 0, termForw2Charp },
10077c478bd9Sstevel@tonic-gate 	{ "ayt",	"alternate AYT character", 0, termAytCharp },
10087c478bd9Sstevel@tonic-gate 	{ 0 }
10097c478bd9Sstevel@tonic-gate };
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate static struct setlist *
getset(name)10127c478bd9Sstevel@tonic-gate getset(name)
10137c478bd9Sstevel@tonic-gate     char *name;
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	return ((struct setlist *)
10167c478bd9Sstevel@tonic-gate 	    genget(name, (char **)Setlist, sizeof (struct setlist)));
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate     void
set_escape_char(s)10207c478bd9Sstevel@tonic-gate set_escape_char(s)
10217c478bd9Sstevel@tonic-gate     char *s;
10227c478bd9Sstevel@tonic-gate {
10237c478bd9Sstevel@tonic-gate 	if (rlogin != _POSIX_VDISABLE) {
10247c478bd9Sstevel@tonic-gate 		rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
10257c478bd9Sstevel@tonic-gate 		(void) printf("Telnet rlogin escape character is '%s'.\n",
10267c478bd9Sstevel@tonic-gate 					control(rlogin));
10277c478bd9Sstevel@tonic-gate 	} else {
10287c478bd9Sstevel@tonic-gate 		escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
10297c478bd9Sstevel@tonic-gate 		(void) printf("Telnet escape character is '%s'.\n",
10307c478bd9Sstevel@tonic-gate 		    esc_control(escape));
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate static int
setcmd(argc,argv)10357c478bd9Sstevel@tonic-gate setcmd(argc, argv)
10367c478bd9Sstevel@tonic-gate 	int  argc;
10377c478bd9Sstevel@tonic-gate 	char *argv[];
10387c478bd9Sstevel@tonic-gate {
10397c478bd9Sstevel@tonic-gate 	int value;
10407c478bd9Sstevel@tonic-gate 	struct setlist *ct;
10417c478bd9Sstevel@tonic-gate 	struct togglelist *c;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	if (argc < 2 || argc > 3) {
10447c478bd9Sstevel@tonic-gate 		(void) printf(
10457c478bd9Sstevel@tonic-gate 			"Format is 'set Name Value'\n'set ?' for help.\n");
10467c478bd9Sstevel@tonic-gate 		return (0);
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 	if ((argc == 2) &&
10497c478bd9Sstevel@tonic-gate 	    (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
10507c478bd9Sstevel@tonic-gate 		for (ct = Setlist; ct->name; ct++)
10517c478bd9Sstevel@tonic-gate 			(void) printf("%-15s %s\n", ct->name, ct->help);
10527c478bd9Sstevel@tonic-gate 		(void) printf("\n");
10537c478bd9Sstevel@tonic-gate 		settogglehelp(1);
10547c478bd9Sstevel@tonic-gate 		(void) printf("%-15s %s\n", "?", "display help information");
10557c478bd9Sstevel@tonic-gate 		return (0);
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	ct = getset(argv[1]);
10597c478bd9Sstevel@tonic-gate 	if (ct == 0) {
10607c478bd9Sstevel@tonic-gate 		c = GETTOGGLE(argv[1]);
10617c478bd9Sstevel@tonic-gate 		if (c == 0) {
10627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "'%s': unknown argument "
10637c478bd9Sstevel@tonic-gate 			    "('set ?' for help).\n", argv[1]);
10647c478bd9Sstevel@tonic-gate 			return (0);
10657c478bd9Sstevel@tonic-gate 		} else if (Ambiguous(c)) {
10667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "'%s': ambiguous argument "
10677c478bd9Sstevel@tonic-gate 			    "('set ?' for help).\n", argv[1]);
10687c478bd9Sstevel@tonic-gate 			return (0);
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 		if (c->variable) {
10717c478bd9Sstevel@tonic-gate 			if ((argc == 2) || (strcmp("on", argv[2]) == 0))
10727c478bd9Sstevel@tonic-gate 				*c->variable = 1;
10737c478bd9Sstevel@tonic-gate 			else if (strcmp("off", argv[2]) == 0)
10747c478bd9Sstevel@tonic-gate 				*c->variable = 0;
10757c478bd9Sstevel@tonic-gate 			else {
10767c478bd9Sstevel@tonic-gate 				(void) printf(
10777c478bd9Sstevel@tonic-gate 				    "Format is 'set togglename [on|off]'\n"
10787c478bd9Sstevel@tonic-gate 				    "'set ?' for help.\n");
10797c478bd9Sstevel@tonic-gate 				return (0);
10807c478bd9Sstevel@tonic-gate 			}
10817c478bd9Sstevel@tonic-gate 			if (c->actionexplanation) {
10827c478bd9Sstevel@tonic-gate 				(void) printf("%s %s.\n",
10837c478bd9Sstevel@tonic-gate 				    *c->variable? "Will" : "Won't",
10847c478bd9Sstevel@tonic-gate 				    c->actionexplanation);
10857c478bd9Sstevel@tonic-gate 			}
10867c478bd9Sstevel@tonic-gate 		}
10877c478bd9Sstevel@tonic-gate 		if (c->handler)
10887c478bd9Sstevel@tonic-gate 			(*c->handler)(1);
10897c478bd9Sstevel@tonic-gate 	} else if (argc != 3) {
10907c478bd9Sstevel@tonic-gate 		(void) printf(
10917c478bd9Sstevel@tonic-gate 			"Format is 'set Name Value'\n'set ?' for help.\n");
10927c478bd9Sstevel@tonic-gate 		return (0);
10937c478bd9Sstevel@tonic-gate 	} else if (Ambiguous(ct)) {
10947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
10957c478bd9Sstevel@tonic-gate 		    "'%s': ambiguous argument ('set ?' for help).\n", argv[1]);
10967c478bd9Sstevel@tonic-gate 		return (0);
10977c478bd9Sstevel@tonic-gate 	} else if (ct->handler) {
10987c478bd9Sstevel@tonic-gate 		(*ct->handler)(argv[2]);
10997c478bd9Sstevel@tonic-gate 		(void) printf(
11007c478bd9Sstevel@tonic-gate 		    "%s set to \"%s\".\n", ct->name, (char *)ct->charp);
11017c478bd9Sstevel@tonic-gate 	} else {
11027c478bd9Sstevel@tonic-gate 		if (strcmp("off", argv[2])) {
11037c478bd9Sstevel@tonic-gate 			value = special(argv[2]);
11047c478bd9Sstevel@tonic-gate 		} else {
11057c478bd9Sstevel@tonic-gate 			value = _POSIX_VDISABLE;
11067c478bd9Sstevel@tonic-gate 		}
11077c478bd9Sstevel@tonic-gate 		*(ct->charp) = (cc_t)value;
11087c478bd9Sstevel@tonic-gate 		(void) printf("%s character is '%s'.\n", ct->name,
11097c478bd9Sstevel@tonic-gate 		    control(*(ct->charp)));
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 	slc_check();
11127c478bd9Sstevel@tonic-gate 	return (1);
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate static int
unsetcmd(int argc,char * argv[])1116*905213bbSToomas Soome unsetcmd(int argc, char *argv[])
11177c478bd9Sstevel@tonic-gate {
11187c478bd9Sstevel@tonic-gate 	struct setlist *ct;
11197c478bd9Sstevel@tonic-gate 	struct togglelist *c;
1120*905213bbSToomas Soome 	char *name;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	if (argc < 2) {
11237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Need an argument to 'unset' command.  "
11247c478bd9Sstevel@tonic-gate 		    "'unset ?' for help.\n");
11257c478bd9Sstevel@tonic-gate 		return (0);
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 	if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
11287c478bd9Sstevel@tonic-gate 		for (ct = Setlist; ct->name; ct++)
11297c478bd9Sstevel@tonic-gate 			(void) printf("%-15s %s\n", ct->name, ct->help);
11307c478bd9Sstevel@tonic-gate 		(void) printf("\n");
11317c478bd9Sstevel@tonic-gate 		settogglehelp(0);
11327c478bd9Sstevel@tonic-gate 		(void) printf("%-15s %s\n", "?", "display help information");
11337c478bd9Sstevel@tonic-gate 		return (0);
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	argc--;
11377c478bd9Sstevel@tonic-gate 	argv++;
11387c478bd9Sstevel@tonic-gate 	while (argc--) {
11397c478bd9Sstevel@tonic-gate 		name = *argv++;
11407c478bd9Sstevel@tonic-gate 		ct = getset(name);
11417c478bd9Sstevel@tonic-gate 		if (ct == 0) {
11427c478bd9Sstevel@tonic-gate 			c = GETTOGGLE(name);
11437c478bd9Sstevel@tonic-gate 			if (c == 0) {
11447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "'%s': unknown argument "
11457c478bd9Sstevel@tonic-gate 				    "('unset ?' for help).\n", name);
11467c478bd9Sstevel@tonic-gate 				return (0);
11477c478bd9Sstevel@tonic-gate 			} else if (Ambiguous(c)) {
11487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11497c478bd9Sstevel@tonic-gate 				    "'%s': ambiguous argument "
11507c478bd9Sstevel@tonic-gate 				    "('unset ?' for help).\n", name);
11517c478bd9Sstevel@tonic-gate 				return (0);
11527c478bd9Sstevel@tonic-gate 			}
11537c478bd9Sstevel@tonic-gate 			if (c->variable) {
11547c478bd9Sstevel@tonic-gate 				*c->variable = 0;
11557c478bd9Sstevel@tonic-gate 				if (c->actionexplanation) {
11567c478bd9Sstevel@tonic-gate 					(void) printf("%s %s.\n",
11577c478bd9Sstevel@tonic-gate 					    *c->variable? "Will" : "Won't",
11587c478bd9Sstevel@tonic-gate 					    c->actionexplanation);
11597c478bd9Sstevel@tonic-gate 				}
11607c478bd9Sstevel@tonic-gate 			}
11617c478bd9Sstevel@tonic-gate 			if (c->handler)
11627c478bd9Sstevel@tonic-gate 				(*c->handler)(0);
11637c478bd9Sstevel@tonic-gate 		} else if (Ambiguous(ct)) {
11647c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "'%s': ambiguous argument "
11657c478bd9Sstevel@tonic-gate 			    "('unset ?' for help).\n", name);
11667c478bd9Sstevel@tonic-gate 			return (0);
11677c478bd9Sstevel@tonic-gate 		} else if (ct->handler) {
11687c478bd9Sstevel@tonic-gate 			(*ct->handler)(0);
11697c478bd9Sstevel@tonic-gate 			(void) printf("%s reset to \"%s\".\n", ct->name,
11707c478bd9Sstevel@tonic-gate 			    (char *)ct->charp);
11717c478bd9Sstevel@tonic-gate 		} else {
11727c478bd9Sstevel@tonic-gate 			*(ct->charp) = _POSIX_VDISABLE;
11737c478bd9Sstevel@tonic-gate 			(void) printf("%s character is '%s'.\n", ct->name,
11747c478bd9Sstevel@tonic-gate 			    control(*(ct->charp)));
11757c478bd9Sstevel@tonic-gate 		}
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 	return (1);
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate /*
11817c478bd9Sstevel@tonic-gate  * The following are the data structures and routines for the
11827c478bd9Sstevel@tonic-gate  * 'mode' command.
11837c478bd9Sstevel@tonic-gate  */
11847c478bd9Sstevel@tonic-gate extern int reqd_linemode;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
11877c478bd9Sstevel@tonic-gate extern int kludgelinemode;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate static int
dokludgemode()11907c478bd9Sstevel@tonic-gate dokludgemode()
11917c478bd9Sstevel@tonic-gate {
11927c478bd9Sstevel@tonic-gate 	kludgelinemode = 1;
11937c478bd9Sstevel@tonic-gate 	send_wont(TELOPT_LINEMODE, 1);
11947c478bd9Sstevel@tonic-gate 	send_dont(TELOPT_SGA, 1);
11957c478bd9Sstevel@tonic-gate 	send_dont(TELOPT_ECHO, 1);
11967c478bd9Sstevel@tonic-gate 	/*
11977c478bd9Sstevel@tonic-gate 	 * If processing the .telnetrc file, keep track of linemode and/or
11987c478bd9Sstevel@tonic-gate 	 * kludgelinemode requests which are processed before initial option
11997c478bd9Sstevel@tonic-gate 	 * negotiations occur.
12007c478bd9Sstevel@tonic-gate 	 */
12017c478bd9Sstevel@tonic-gate 	if (doing_rc)
12027c478bd9Sstevel@tonic-gate 		reqd_linemode = 1;
12037c478bd9Sstevel@tonic-gate 	return (1);
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate #endif
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate static int
dolinemode()12087c478bd9Sstevel@tonic-gate dolinemode()
12097c478bd9Sstevel@tonic-gate {
12107c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
12117c478bd9Sstevel@tonic-gate 	if (kludgelinemode)
12127c478bd9Sstevel@tonic-gate 		send_dont(TELOPT_SGA, 1);
12137c478bd9Sstevel@tonic-gate #endif
12147c478bd9Sstevel@tonic-gate 	send_will(TELOPT_LINEMODE, 1);
12157c478bd9Sstevel@tonic-gate 	send_dont(TELOPT_ECHO, 1);
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	/*
12187c478bd9Sstevel@tonic-gate 	 * If processing the .telnetrc file, keep track of linemode and/or
12197c478bd9Sstevel@tonic-gate 	 * kludgelinemode requests which are processed before initial option
12207c478bd9Sstevel@tonic-gate 	 * negotiations occur.
12217c478bd9Sstevel@tonic-gate 	 */
12227c478bd9Sstevel@tonic-gate 	if (doing_rc)
12237c478bd9Sstevel@tonic-gate 		reqd_linemode = 1;
12247c478bd9Sstevel@tonic-gate 	return (1);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate static int
docharmode()12287c478bd9Sstevel@tonic-gate docharmode()
12297c478bd9Sstevel@tonic-gate {
12307c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
12317c478bd9Sstevel@tonic-gate 	if (kludgelinemode)
12327c478bd9Sstevel@tonic-gate 		send_do(TELOPT_SGA, 1);
12337c478bd9Sstevel@tonic-gate 	else
12347c478bd9Sstevel@tonic-gate #endif
12357c478bd9Sstevel@tonic-gate 		send_wont(TELOPT_LINEMODE, 1);
12367c478bd9Sstevel@tonic-gate 	send_do(TELOPT_ECHO, 1);
12377c478bd9Sstevel@tonic-gate 	reqd_linemode = 0;
12387c478bd9Sstevel@tonic-gate 	return (1);
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate static int
dolmmode(bit,on)12427c478bd9Sstevel@tonic-gate dolmmode(bit, on)
12437c478bd9Sstevel@tonic-gate 	int bit, on;
12447c478bd9Sstevel@tonic-gate {
12457c478bd9Sstevel@tonic-gate 	unsigned char c;
12467c478bd9Sstevel@tonic-gate 	extern int linemode;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	if (my_want_state_is_wont(TELOPT_LINEMODE)) {
12497c478bd9Sstevel@tonic-gate 		(void) printf("?Need to have LINEMODE option enabled first.\n");
12507c478bd9Sstevel@tonic-gate 		(void) printf("'mode ?' for help.\n");
12517c478bd9Sstevel@tonic-gate 		return (0);
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	if (on)
12557c478bd9Sstevel@tonic-gate 		c = (linemode | bit);
12567c478bd9Sstevel@tonic-gate 	else
12577c478bd9Sstevel@tonic-gate 		c = (linemode & ~bit);
12587c478bd9Sstevel@tonic-gate 	lm_mode(&c, 1, 1);
12597c478bd9Sstevel@tonic-gate 	return (1);
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate static int
setmode(int bit)1263*905213bbSToomas Soome setmode(int bit)
12647c478bd9Sstevel@tonic-gate {
12657c478bd9Sstevel@tonic-gate 	return (dolmmode(bit, 1));
12667c478bd9Sstevel@tonic-gate }
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate static int
clearmode(int bit)1269*905213bbSToomas Soome clearmode(int bit)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate 	return (dolmmode(bit, 0));
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate struct modelist {
12757c478bd9Sstevel@tonic-gate 	char	*name;		/* command name */
12767c478bd9Sstevel@tonic-gate 	char	*help;		/* help string */
12777c478bd9Sstevel@tonic-gate 	int	(*handler)();	/* routine which executes command */
12787c478bd9Sstevel@tonic-gate 	int	needconnect;	/* Do we need to be connected to execute? */
12797c478bd9Sstevel@tonic-gate 	int	arg1;
12807c478bd9Sstevel@tonic-gate };
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate static int modehelp();
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate static struct modelist ModeList[] = {
12857c478bd9Sstevel@tonic-gate 	{ "character", "Disable LINEMODE option",	docharmode, 1 },
12867c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
12877c478bd9Sstevel@tonic-gate 	{ "",	"(or disable obsolete line-by-line mode)", 0 },
12887c478bd9Sstevel@tonic-gate #endif
12897c478bd9Sstevel@tonic-gate 	{ "line",	"Enable LINEMODE option",	dolinemode, 1 },
12907c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
12917c478bd9Sstevel@tonic-gate 	{ "",	"(or enable obsolete line-by-line mode)", 0 },
12927c478bd9Sstevel@tonic-gate #endif
12937c478bd9Sstevel@tonic-gate 	{ "", "", 0 },
12947c478bd9Sstevel@tonic-gate 	{ "",	"These require the LINEMODE option to be enabled", 0 },
12957c478bd9Sstevel@tonic-gate 	{ "isig",	"Enable signal trapping", setmode, 1, MODE_TRAPSIG },
12967c478bd9Sstevel@tonic-gate 	{ "+isig",	0,			setmode, 1, MODE_TRAPSIG },
12977c478bd9Sstevel@tonic-gate 	{ "-isig",	"Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
12987c478bd9Sstevel@tonic-gate 	{ "edit",	"Enable character editing",	setmode, 1, MODE_EDIT },
12997c478bd9Sstevel@tonic-gate 	{ "+edit",	0,			setmode, 1, MODE_EDIT },
13007c478bd9Sstevel@tonic-gate 	{ "-edit",	"Disable character editing", clearmode, 1, MODE_EDIT },
13017c478bd9Sstevel@tonic-gate 	{ "softtabs",	"Enable tab expansion",	setmode, 1, MODE_SOFT_TAB },
13027c478bd9Sstevel@tonic-gate 	{ "+softtabs",	0,			setmode, 1, MODE_SOFT_TAB },
13037c478bd9Sstevel@tonic-gate 	{ "-softtabs",	"Disable tab expansion",
13047c478bd9Sstevel@tonic-gate 						clearmode, 1, MODE_SOFT_TAB },
13057c478bd9Sstevel@tonic-gate 	{ "litecho",	"Enable literal character echo",
13067c478bd9Sstevel@tonic-gate 						setmode, 1, MODE_LIT_ECHO },
13077c478bd9Sstevel@tonic-gate 	{ "+litecho",	0,			setmode, 1, MODE_LIT_ECHO },
13087c478bd9Sstevel@tonic-gate 	{ "-litecho",	"Disable literal character echo", clearmode, 1,
13097c478bd9Sstevel@tonic-gate 		MODE_LIT_ECHO },
13107c478bd9Sstevel@tonic-gate 	{ "help",	0,			modehelp, 0 },
13117c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
13127c478bd9Sstevel@tonic-gate 	{ "kludgeline", 0,				dokludgemode, 1 },
13137c478bd9Sstevel@tonic-gate #endif
13147c478bd9Sstevel@tonic-gate 	{ "", "", 0 },
13157c478bd9Sstevel@tonic-gate 	{ "?",	"Print help information",	modehelp, 0 },
13167c478bd9Sstevel@tonic-gate 	{ 0 },
13177c478bd9Sstevel@tonic-gate };
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate static int
modehelp()13217c478bd9Sstevel@tonic-gate modehelp()
13227c478bd9Sstevel@tonic-gate {
13237c478bd9Sstevel@tonic-gate 	struct modelist *mt;
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	(void) printf("format is:  'mode Mode', where 'Mode' is one of:\n\n");
13267c478bd9Sstevel@tonic-gate 	for (mt = ModeList; mt->name; mt++) {
13277c478bd9Sstevel@tonic-gate 		if (mt->help) {
13287c478bd9Sstevel@tonic-gate 			if (*mt->help)
13297c478bd9Sstevel@tonic-gate 				(void) printf("%-15s %s\n", mt->name, mt->help);
13307c478bd9Sstevel@tonic-gate 			else
13317c478bd9Sstevel@tonic-gate 				(void) printf("\n");
13327c478bd9Sstevel@tonic-gate 		}
13337c478bd9Sstevel@tonic-gate 	}
13347c478bd9Sstevel@tonic-gate 	return (0);
13357c478bd9Sstevel@tonic-gate }
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate #define	GETMODECMD(name) (struct modelist *) \
13387c478bd9Sstevel@tonic-gate 		genget(name, (char **)ModeList, sizeof (struct modelist))
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate static int
modecmd(argc,argv)13417c478bd9Sstevel@tonic-gate modecmd(argc, argv)
13427c478bd9Sstevel@tonic-gate 	int  argc;
13437c478bd9Sstevel@tonic-gate 	char *argv[];
13447c478bd9Sstevel@tonic-gate {
13457c478bd9Sstevel@tonic-gate 	struct modelist *mt;
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	if (argc != 2) {
13487c478bd9Sstevel@tonic-gate 		(void) printf("'mode' command requires an argument\n");
13497c478bd9Sstevel@tonic-gate 		(void) printf("'mode ?' for help.\n");
13507c478bd9Sstevel@tonic-gate 	} else if ((mt = GETMODECMD(argv[1])) == 0) {
13517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13527c478bd9Sstevel@tonic-gate 		    "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
13537c478bd9Sstevel@tonic-gate 	} else if (Ambiguous(mt)) {
13547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13557c478bd9Sstevel@tonic-gate 		    "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
13567c478bd9Sstevel@tonic-gate 	} else if (mt->needconnect && !connected) {
13577c478bd9Sstevel@tonic-gate 		(void) printf("?Need to be connected first.\n");
13587c478bd9Sstevel@tonic-gate 		(void) printf("'mode ?' for help.\n");
13597c478bd9Sstevel@tonic-gate 	} else if (mt->handler) {
13607c478bd9Sstevel@tonic-gate 		return (*mt->handler)(mt->arg1);
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 	return (0);
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate /*
13667c478bd9Sstevel@tonic-gate  * The following data structures and routines implement the
13677c478bd9Sstevel@tonic-gate  * "display" command.
13687c478bd9Sstevel@tonic-gate  */
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate static int
display(argc,argv)13717c478bd9Sstevel@tonic-gate display(argc, argv)
13727c478bd9Sstevel@tonic-gate 	int  argc;
13737c478bd9Sstevel@tonic-gate 	char *argv[];
13747c478bd9Sstevel@tonic-gate {
13757c478bd9Sstevel@tonic-gate 	struct togglelist *tl;
13767c478bd9Sstevel@tonic-gate 	struct setlist *sl;
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate #define	dotog(tl)	if (tl->variable && tl->actionexplanation) { \
13797c478bd9Sstevel@tonic-gate 			    if (*tl->variable) { \
13807c478bd9Sstevel@tonic-gate 				(void) printf("will"); \
13817c478bd9Sstevel@tonic-gate 			    } else { \
13827c478bd9Sstevel@tonic-gate 				(void) printf("won't"); \
13837c478bd9Sstevel@tonic-gate 			    } \
13847c478bd9Sstevel@tonic-gate 			    (void) printf(" %s.\n", tl->actionexplanation); \
13857c478bd9Sstevel@tonic-gate 			}
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate #define	doset(sl)   if (sl->name && *sl->name != ' ') { \
13887c478bd9Sstevel@tonic-gate 			if (sl->handler == 0) \
13897c478bd9Sstevel@tonic-gate 			    (void) printf("%-15s [%s]\n", sl->name, \
13907c478bd9Sstevel@tonic-gate 			    control(*sl->charp)); \
13917c478bd9Sstevel@tonic-gate 			else \
13927c478bd9Sstevel@tonic-gate 			    (void) printf("%-15s \"%s\"\n", sl->name, \
13937c478bd9Sstevel@tonic-gate 			    (char *)sl->charp); \
13947c478bd9Sstevel@tonic-gate 		    }
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	if (argc == 1) {
13977c478bd9Sstevel@tonic-gate 		for (tl = Togglelist; tl->name; tl++) {
13987c478bd9Sstevel@tonic-gate 			dotog(tl);
13997c478bd9Sstevel@tonic-gate 		}
14007c478bd9Sstevel@tonic-gate 		(void) printf("\n");
14017c478bd9Sstevel@tonic-gate 		for (sl = Setlist; sl->name; sl++) {
14027c478bd9Sstevel@tonic-gate 			doset(sl);
14037c478bd9Sstevel@tonic-gate 		}
14047c478bd9Sstevel@tonic-gate 	} else {
14057c478bd9Sstevel@tonic-gate 		int i;
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 		for (i = 1; i < argc; i++) {
14087c478bd9Sstevel@tonic-gate 			sl = getset(argv[i]);
14097c478bd9Sstevel@tonic-gate 			tl = GETTOGGLE(argv[i]);
14107c478bd9Sstevel@tonic-gate 			if (Ambiguous(sl) || Ambiguous(tl)) {
14117c478bd9Sstevel@tonic-gate 				(void) printf(
14127c478bd9Sstevel@tonic-gate 				    "?Ambiguous argument '%s'.\n", argv[i]);
14137c478bd9Sstevel@tonic-gate 				return (0);
14147c478bd9Sstevel@tonic-gate 			} else if (!sl && !tl) {
14157c478bd9Sstevel@tonic-gate 				(void) printf(
14167c478bd9Sstevel@tonic-gate 				    "?Unknown argument '%s'.\n", argv[i]);
14177c478bd9Sstevel@tonic-gate 				return (0);
14187c478bd9Sstevel@tonic-gate 			} else {
14197c478bd9Sstevel@tonic-gate 				if (tl) {
14207c478bd9Sstevel@tonic-gate 					dotog(tl);
14217c478bd9Sstevel@tonic-gate 				}
14227c478bd9Sstevel@tonic-gate 				if (sl) {
14237c478bd9Sstevel@tonic-gate 				    doset(sl);
14247c478bd9Sstevel@tonic-gate 				}
14257c478bd9Sstevel@tonic-gate 			}
14267c478bd9Sstevel@tonic-gate 		}
14277c478bd9Sstevel@tonic-gate 	}
14287c478bd9Sstevel@tonic-gate 	optionstatus();
14297c478bd9Sstevel@tonic-gate 	(void) EncryptStatus();
14307c478bd9Sstevel@tonic-gate 	return (1);
14317c478bd9Sstevel@tonic-gate #undef	doset
14327c478bd9Sstevel@tonic-gate #undef	dotog
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate /*
14367c478bd9Sstevel@tonic-gate  * The following are the data structures, and many of the routines,
14377c478bd9Sstevel@tonic-gate  * relating to command processing.
14387c478bd9Sstevel@tonic-gate  */
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate /*
14417c478bd9Sstevel@tonic-gate  * Set the escape character.
14427c478bd9Sstevel@tonic-gate  */
14437c478bd9Sstevel@tonic-gate static int
setescape(int argc,char * argv[])1444*905213bbSToomas Soome setescape(int argc, char *argv[])
14457c478bd9Sstevel@tonic-gate {
1446*905213bbSToomas Soome 	char *arg;
14477c478bd9Sstevel@tonic-gate 	char *buf = NULL;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	if (argc > 2)
14507c478bd9Sstevel@tonic-gate 		arg = argv[1];
14517c478bd9Sstevel@tonic-gate 	else {
14527c478bd9Sstevel@tonic-gate 		(void) printf("new escape character: ");
14537c478bd9Sstevel@tonic-gate 		if (GetString(&buf, NULL, stdin) == NULL) {
14547c478bd9Sstevel@tonic-gate 			if (!feof(stdin)) {
14557c478bd9Sstevel@tonic-gate 				perror("can't set escape character");
14567c478bd9Sstevel@tonic-gate 				goto setescape_exit;
14577c478bd9Sstevel@tonic-gate 			}
14587c478bd9Sstevel@tonic-gate 		}
14597c478bd9Sstevel@tonic-gate 		arg = buf;
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 	/* we place no limitations on what escape can be. */
14627c478bd9Sstevel@tonic-gate 	escape = arg[0];
14637c478bd9Sstevel@tonic-gate 	(void) printf("Escape character is '%s'.\n", esc_control(escape));
14647c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
14657c478bd9Sstevel@tonic-gate setescape_exit:
14667c478bd9Sstevel@tonic-gate 	Free(&buf);
14677c478bd9Sstevel@tonic-gate 	return (1);
14687c478bd9Sstevel@tonic-gate }
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14717c478bd9Sstevel@tonic-gate static int
togcrmod(argc,argv)14727c478bd9Sstevel@tonic-gate togcrmod(argc, argv)
14737c478bd9Sstevel@tonic-gate 	int argc;
14747c478bd9Sstevel@tonic-gate 	char *argv[];
14757c478bd9Sstevel@tonic-gate {
14767c478bd9Sstevel@tonic-gate 	crmod = !crmod;
14777c478bd9Sstevel@tonic-gate 	(void) printf(
14787c478bd9Sstevel@tonic-gate 	    "%s map carriage return on output.\n", crmod ? "Will" : "Won't");
14797c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
14807c478bd9Sstevel@tonic-gate 	return (1);
14817c478bd9Sstevel@tonic-gate }
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14847c478bd9Sstevel@tonic-gate static int
suspend(argc,argv)14857c478bd9Sstevel@tonic-gate suspend(argc, argv)
14867c478bd9Sstevel@tonic-gate 	int argc;
14877c478bd9Sstevel@tonic-gate 	char *argv[];
14887c478bd9Sstevel@tonic-gate {
14897c478bd9Sstevel@tonic-gate 	setcommandmode();
14907c478bd9Sstevel@tonic-gate 	{
14917c478bd9Sstevel@tonic-gate 		unsigned short oldrows, oldcols, newrows, newcols;
14927c478bd9Sstevel@tonic-gate 		int err;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 		err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
14957c478bd9Sstevel@tonic-gate 		(void) kill(0, SIGTSTP);
14967c478bd9Sstevel@tonic-gate 		/*
14977c478bd9Sstevel@tonic-gate 		 * If we didn't get the window size before the SUSPEND, but we
14987c478bd9Sstevel@tonic-gate 		 * can get them now (?), then send the NAWS to make sure that
14997c478bd9Sstevel@tonic-gate 		 * we are set up for the right window size.
15007c478bd9Sstevel@tonic-gate 		 */
15017c478bd9Sstevel@tonic-gate 		if (TerminalWindowSize(&newrows, &newcols) && connected &&
15027c478bd9Sstevel@tonic-gate 		    (err || ((oldrows != newrows) || (oldcols != newcols)))) {
15037c478bd9Sstevel@tonic-gate 			sendnaws();
15047c478bd9Sstevel@tonic-gate 		}
15057c478bd9Sstevel@tonic-gate 	}
15067c478bd9Sstevel@tonic-gate 	/* reget parameters in case they were changed */
15077c478bd9Sstevel@tonic-gate 	TerminalSaveState();
15087c478bd9Sstevel@tonic-gate 	setconnmode(0);
15097c478bd9Sstevel@tonic-gate 	return (1);
15107c478bd9Sstevel@tonic-gate }
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15137c478bd9Sstevel@tonic-gate static int
shell(int argc,char * argv[])1514*905213bbSToomas Soome shell(int argc, char *argv[])
15157c478bd9Sstevel@tonic-gate {
15167c478bd9Sstevel@tonic-gate 	unsigned short oldrows, oldcols, newrows, newcols;
15177c478bd9Sstevel@tonic-gate 	int err;
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	setcommandmode();
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
15227c478bd9Sstevel@tonic-gate 	switch (vfork()) {
15237c478bd9Sstevel@tonic-gate 	case -1:
15247c478bd9Sstevel@tonic-gate 		perror("Fork failed\n");
15257c478bd9Sstevel@tonic-gate 		break;
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	case 0:
15287c478bd9Sstevel@tonic-gate 		{
15297c478bd9Sstevel@tonic-gate 		/*
15307c478bd9Sstevel@tonic-gate 		 * Fire up the shell in the child.
15317c478bd9Sstevel@tonic-gate 		 */
1532*905213bbSToomas Soome 		char *shellp, *shellname;
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 		shellp = getenv("SHELL");
15357c478bd9Sstevel@tonic-gate 		if (shellp == NULL)
15367c478bd9Sstevel@tonic-gate 			shellp = "/bin/sh";
15377c478bd9Sstevel@tonic-gate 		if ((shellname = strrchr(shellp, '/')) == 0)
15387c478bd9Sstevel@tonic-gate 			shellname = shellp;
15397c478bd9Sstevel@tonic-gate 		else
15407c478bd9Sstevel@tonic-gate 			shellname++;
15417c478bd9Sstevel@tonic-gate 		if (argc > 1)
15427c478bd9Sstevel@tonic-gate 			(void) execl(shellp, shellname, "-c", argv[1], 0);
15437c478bd9Sstevel@tonic-gate 		else
15447c478bd9Sstevel@tonic-gate 			(void) execl(shellp, shellname, 0);
15457c478bd9Sstevel@tonic-gate 		perror("Execl");
15467c478bd9Sstevel@tonic-gate 		_exit(EXIT_FAILURE);
15477c478bd9Sstevel@tonic-gate 		}
15487c478bd9Sstevel@tonic-gate 	default:
15497c478bd9Sstevel@tonic-gate 		(void) wait((int *)0);	/* Wait for the shell to complete */
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 		if (TerminalWindowSize(&newrows, &newcols) && connected &&
15527c478bd9Sstevel@tonic-gate 		(err || ((oldrows != newrows) || (oldcols != newcols)))) {
15537c478bd9Sstevel@tonic-gate 			sendnaws();
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 		break;
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 	return (1);
15587c478bd9Sstevel@tonic-gate }
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate static int
bye(argc,argv)15617c478bd9Sstevel@tonic-gate bye(argc, argv)
15627c478bd9Sstevel@tonic-gate 	int  argc;	/* Number of arguments */
15637c478bd9Sstevel@tonic-gate 	char *argv[];	/* arguments */
15647c478bd9Sstevel@tonic-gate {
15657c478bd9Sstevel@tonic-gate 	extern int resettermname;
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	if (connected) {
15687c478bd9Sstevel@tonic-gate 		(void) shutdown(net, 2);
15697c478bd9Sstevel@tonic-gate 		(void) printf("Connection to %.*s closed.\n", MAXHOSTNAMELEN,
15707c478bd9Sstevel@tonic-gate 		    hostname);
15717c478bd9Sstevel@tonic-gate 		Close(&net);
15727c478bd9Sstevel@tonic-gate 		connected = 0;
15737c478bd9Sstevel@tonic-gate 		resettermname = 1;
15747c478bd9Sstevel@tonic-gate 		/* reset options */
15757c478bd9Sstevel@tonic-gate 		(void) tninit();
15767c478bd9Sstevel@tonic-gate 	}
15777c478bd9Sstevel@tonic-gate 	if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
15787c478bd9Sstevel@tonic-gate 		longjmp(toplevel, 1);
15797c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
15807c478bd9Sstevel@tonic-gate 	}
15817c478bd9Sstevel@tonic-gate 	return (1);			/* Keep lint, etc., happy */
15827c478bd9Sstevel@tonic-gate }
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate /*VARARGS*/
15857c478bd9Sstevel@tonic-gate int
quit()15867c478bd9Sstevel@tonic-gate quit()
15877c478bd9Sstevel@tonic-gate {
15887c478bd9Sstevel@tonic-gate 	(void) call(3, bye, "bye", "fromquit");
15897c478bd9Sstevel@tonic-gate 	Exit(EXIT_SUCCESS);
15907c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
15917c478bd9Sstevel@tonic-gate 	return (1);
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15957c478bd9Sstevel@tonic-gate static int
logout(argc,argv)15967c478bd9Sstevel@tonic-gate logout(argc, argv)
15977c478bd9Sstevel@tonic-gate 	int argc;
15987c478bd9Sstevel@tonic-gate 	char *argv[];
15997c478bd9Sstevel@tonic-gate {
16007c478bd9Sstevel@tonic-gate 	send_do(TELOPT_LOGOUT, 1);
16017c478bd9Sstevel@tonic-gate 	(void) netflush();
16027c478bd9Sstevel@tonic-gate 	return (1);
16037c478bd9Sstevel@tonic-gate }
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate /*
16077c478bd9Sstevel@tonic-gate  * The SLC command.
16087c478bd9Sstevel@tonic-gate  */
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate struct slclist {
16117c478bd9Sstevel@tonic-gate 	char	*name;
16127c478bd9Sstevel@tonic-gate 	char	*help;
16137c478bd9Sstevel@tonic-gate 	void	(*handler)();
16147c478bd9Sstevel@tonic-gate 	int	arg;
16157c478bd9Sstevel@tonic-gate };
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate static void slc_help();
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate static struct slclist SlcList[] = {
16207c478bd9Sstevel@tonic-gate 	{ "export",	"Use local special character definitions",
16217c478bd9Sstevel@tonic-gate 						slc_mode_export,	0 },
16227c478bd9Sstevel@tonic-gate 	{ "import",	"Use remote special character definitions",
16237c478bd9Sstevel@tonic-gate 						slc_mode_import,	1 },
16247c478bd9Sstevel@tonic-gate 	{ "check",	"Verify remote special character definitions",
16257c478bd9Sstevel@tonic-gate 						slc_mode_import,	0 },
16267c478bd9Sstevel@tonic-gate 	{ "help",	0,			slc_help,		0 },
16277c478bd9Sstevel@tonic-gate 	{ "?",	"Print help information",	slc_help,		0 },
16287c478bd9Sstevel@tonic-gate 	{ 0 },
16297c478bd9Sstevel@tonic-gate };
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate static void
slc_help()16327c478bd9Sstevel@tonic-gate slc_help()
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate 	struct slclist *c;
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	for (c = SlcList; c->name; c++) {
16377c478bd9Sstevel@tonic-gate 		if (c->help) {
16387c478bd9Sstevel@tonic-gate 			if (*c->help)
16397c478bd9Sstevel@tonic-gate 				(void) printf("%-15s %s\n", c->name, c->help);
16407c478bd9Sstevel@tonic-gate 			else
16417c478bd9Sstevel@tonic-gate 				(void) printf("\n");
16427c478bd9Sstevel@tonic-gate 		}
16437c478bd9Sstevel@tonic-gate 	}
16447c478bd9Sstevel@tonic-gate }
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate static struct slclist *
getslc(name)16477c478bd9Sstevel@tonic-gate getslc(name)
16487c478bd9Sstevel@tonic-gate 	char *name;
16497c478bd9Sstevel@tonic-gate {
16507c478bd9Sstevel@tonic-gate 	return ((struct slclist *)
16517c478bd9Sstevel@tonic-gate 	    genget(name, (char **)SlcList, sizeof (struct slclist)));
16527c478bd9Sstevel@tonic-gate }
16537c478bd9Sstevel@tonic-gate 
1654740638c8Sbw static int
slccmd(argc,argv)16557c478bd9Sstevel@tonic-gate slccmd(argc, argv)
16567c478bd9Sstevel@tonic-gate 	int  argc;
16577c478bd9Sstevel@tonic-gate 	char *argv[];
16587c478bd9Sstevel@tonic-gate {
16597c478bd9Sstevel@tonic-gate 	struct slclist *c;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	if (argc != 2) {
16627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
16637c478bd9Sstevel@tonic-gate 		    "Need an argument to 'slc' command.  'slc ?' for help.\n");
16647c478bd9Sstevel@tonic-gate 		return (0);
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 	c = getslc(argv[1]);
16677c478bd9Sstevel@tonic-gate 	if (c == 0) {
16687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
16697c478bd9Sstevel@tonic-gate 		    "'%s': unknown argument ('slc ?' for help).\n",
16707c478bd9Sstevel@tonic-gate 		    argv[1]);
16717c478bd9Sstevel@tonic-gate 		return (0);
16727c478bd9Sstevel@tonic-gate 	}
16737c478bd9Sstevel@tonic-gate 	if (Ambiguous(c)) {
16747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
16757c478bd9Sstevel@tonic-gate 		    "'%s': ambiguous argument ('slc ?' for help).\n", argv[1]);
16767c478bd9Sstevel@tonic-gate 		return (0);
16777c478bd9Sstevel@tonic-gate 	}
16787c478bd9Sstevel@tonic-gate 	(*c->handler)(c->arg);
16797c478bd9Sstevel@tonic-gate 	slcstate();
16807c478bd9Sstevel@tonic-gate 	return (1);
16817c478bd9Sstevel@tonic-gate }
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate /*
16847c478bd9Sstevel@tonic-gate  * The ENVIRON command.
16857c478bd9Sstevel@tonic-gate  */
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate struct envlist {
16887c478bd9Sstevel@tonic-gate 	char	*name;
16897c478bd9Sstevel@tonic-gate 	char	*help;
16907c478bd9Sstevel@tonic-gate 	void	(*handler)();
16917c478bd9Sstevel@tonic-gate 	int	narg;
16927c478bd9Sstevel@tonic-gate };
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate static struct env_lst *env_define(unsigned char *, unsigned char *);
16957c478bd9Sstevel@tonic-gate static void env_undefine(unsigned char *);
16967c478bd9Sstevel@tonic-gate static void env_export(unsigned char *);
16977c478bd9Sstevel@tonic-gate static void env_unexport(unsigned char *);
16987c478bd9Sstevel@tonic-gate static void env_send(unsigned char *);
16997c478bd9Sstevel@tonic-gate #if defined(OLD_ENVIRON) && defined(ENV_HACK)
17007c478bd9Sstevel@tonic-gate static void env_varval(unsigned char *);
17017c478bd9Sstevel@tonic-gate #endif
17027c478bd9Sstevel@tonic-gate static void env_list(void);
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate static void env_help(void);
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate static struct envlist EnvList[] = {
17077c478bd9Sstevel@tonic-gate 	{ "define",	"Define an environment variable",
17087c478bd9Sstevel@tonic-gate 						(void (*)())env_define,	2 },
17097c478bd9Sstevel@tonic-gate 	{ "undefine", "Undefine an environment variable",
17107c478bd9Sstevel@tonic-gate 						env_undefine,	1 },
17117c478bd9Sstevel@tonic-gate 	{ "export",	"Mark an environment variable for automatic export",
17127c478bd9Sstevel@tonic-gate 						env_export,	1 },
17137c478bd9Sstevel@tonic-gate 	{ "unexport", "Don't mark an environment variable for automatic export",
17147c478bd9Sstevel@tonic-gate 						env_unexport,	1 },
17157c478bd9Sstevel@tonic-gate 	{ "send",	"Send an environment variable", env_send,	1 },
17167c478bd9Sstevel@tonic-gate 	{ "list",	"List the current environment variables",
17177c478bd9Sstevel@tonic-gate 						env_list,	0 },
17187c478bd9Sstevel@tonic-gate #if defined(OLD_ENVIRON) && defined(ENV_HACK)
17197c478bd9Sstevel@tonic-gate 	{ "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
17207c478bd9Sstevel@tonic-gate 						env_varval,    1 },
17217c478bd9Sstevel@tonic-gate #endif
17227c478bd9Sstevel@tonic-gate 	{ "help",	0,			env_help,		0 },
17237c478bd9Sstevel@tonic-gate 	{ "?",	"Print help information",	env_help,		0 },
17247c478bd9Sstevel@tonic-gate 	{ 0 },
17257c478bd9Sstevel@tonic-gate };
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate static void
env_help()17287c478bd9Sstevel@tonic-gate env_help()
17297c478bd9Sstevel@tonic-gate {
17307c478bd9Sstevel@tonic-gate 	struct envlist *c;
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	for (c = EnvList; c->name; c++) {
17337c478bd9Sstevel@tonic-gate 		if (c->help) {
17347c478bd9Sstevel@tonic-gate 			if (*c->help)
17357c478bd9Sstevel@tonic-gate 				(void) printf("%-15s %s\n", c->name, c->help);
17367c478bd9Sstevel@tonic-gate 			else
17377c478bd9Sstevel@tonic-gate 				(void) printf("\n");
17387c478bd9Sstevel@tonic-gate 		}
17397c478bd9Sstevel@tonic-gate 	}
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate static struct envlist *
getenvcmd(name)17437c478bd9Sstevel@tonic-gate getenvcmd(name)
17447c478bd9Sstevel@tonic-gate     char *name;
17457c478bd9Sstevel@tonic-gate {
17467c478bd9Sstevel@tonic-gate 	return ((struct envlist *)
17477c478bd9Sstevel@tonic-gate 	    genget(name, (char **)EnvList, sizeof (struct envlist)));
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate static int
env_cmd(argc,argv)17517c478bd9Sstevel@tonic-gate env_cmd(argc, argv)
17527c478bd9Sstevel@tonic-gate 	int  argc;
17537c478bd9Sstevel@tonic-gate 	char *argv[];
17547c478bd9Sstevel@tonic-gate {
17557c478bd9Sstevel@tonic-gate 	struct envlist *c;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	if (argc < 2) {
17587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
17597c478bd9Sstevel@tonic-gate 		    "Need an argument to 'environ' command.  "
17607c478bd9Sstevel@tonic-gate 		    "'environ ?' for help.\n");
17617c478bd9Sstevel@tonic-gate 		return (0);
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate 	c = getenvcmd(argv[1]);
17647c478bd9Sstevel@tonic-gate 	if (c == 0) {
17657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "'%s': unknown argument "
17667c478bd9Sstevel@tonic-gate 		    "('environ ?' for help).\n", argv[1]);
17677c478bd9Sstevel@tonic-gate 		return (0);
17687c478bd9Sstevel@tonic-gate 	}
17697c478bd9Sstevel@tonic-gate 	if (Ambiguous(c)) {
17707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "'%s': ambiguous argument "
17717c478bd9Sstevel@tonic-gate 		    "('environ ?' for help).\n", argv[1]);
17727c478bd9Sstevel@tonic-gate 		return (0);
17737c478bd9Sstevel@tonic-gate 	}
17747c478bd9Sstevel@tonic-gate 	if (c->narg + 2 != argc) {
17757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
17767c478bd9Sstevel@tonic-gate 		    "Need %s%d argument%s to 'environ %s' command.  "
17777c478bd9Sstevel@tonic-gate 		    "'environ ?' for help.\n",
17787c478bd9Sstevel@tonic-gate 		    c->narg + 2 < argc ? "only " : "",
17797c478bd9Sstevel@tonic-gate 		    c->narg, c->narg == 1 ? "" : "s", c->name);
17807c478bd9Sstevel@tonic-gate 		return (0);
17817c478bd9Sstevel@tonic-gate 	}
17827c478bd9Sstevel@tonic-gate 	(*c->handler)(argv[2], argv[3]);
17837c478bd9Sstevel@tonic-gate 	return (1);
17847c478bd9Sstevel@tonic-gate }
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate struct env_lst {
17877c478bd9Sstevel@tonic-gate 	struct env_lst *next;	/* pointer to next structure */
17887c478bd9Sstevel@tonic-gate 	struct env_lst *prev;	/* pointer to previous structure */
17897c478bd9Sstevel@tonic-gate 	unsigned char *var;	/* pointer to variable name */
17907c478bd9Sstevel@tonic-gate 	unsigned char *value;	/* pointer to variable value */
17917c478bd9Sstevel@tonic-gate 	int export;		/* 1 -> export with default list of variables */
17927c478bd9Sstevel@tonic-gate 	int welldefined;	/* A well defined variable */
17937c478bd9Sstevel@tonic-gate };
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate static struct env_lst envlisthead;
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate static struct env_lst *
env_find(unsigned char * var)1798*905213bbSToomas Soome env_find(unsigned char *var)
17997c478bd9Sstevel@tonic-gate {
1800*905213bbSToomas Soome 	struct env_lst *ep;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	for (ep = envlisthead.next; ep; ep = ep->next) {
18037c478bd9Sstevel@tonic-gate 		if (strcmp((char *)ep->var, (char *)var) == 0)
18047c478bd9Sstevel@tonic-gate 			return (ep);
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 	return (NULL);
18077c478bd9Sstevel@tonic-gate }
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate int
env_init(void)1810*905213bbSToomas Soome env_init(void)
18117c478bd9Sstevel@tonic-gate {
18127c478bd9Sstevel@tonic-gate 	extern char **environ;
18137c478bd9Sstevel@tonic-gate 	char **epp, *cp;
18147c478bd9Sstevel@tonic-gate 	struct env_lst *ep;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	for (epp = environ; *epp; epp++) {
18177c478bd9Sstevel@tonic-gate 		if (cp = strchr(*epp, '=')) {
18187c478bd9Sstevel@tonic-gate 			*cp = '\0';
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 			ep = env_define((unsigned char *)*epp,
18217c478bd9Sstevel@tonic-gate 					(unsigned char *)cp+1);
18227c478bd9Sstevel@tonic-gate 			if (ep == NULL)
18237c478bd9Sstevel@tonic-gate 				return (0);
18247c478bd9Sstevel@tonic-gate 			ep->export = 0;
18257c478bd9Sstevel@tonic-gate 			*cp = '=';
18267c478bd9Sstevel@tonic-gate 		}
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate 	/*
18297c478bd9Sstevel@tonic-gate 	 * Special case for DISPLAY variable.  If it is ":0.0" or
18307c478bd9Sstevel@tonic-gate 	 * "unix:0.0", we have to get rid of "unix" and insert our
18317c478bd9Sstevel@tonic-gate 	 * hostname.
18327c478bd9Sstevel@tonic-gate 	 */
18337c478bd9Sstevel@tonic-gate 	if (((ep = env_find((uchar_t *)"DISPLAY")) != NULL) &&
18347c478bd9Sstevel@tonic-gate 	    ((*ep->value == ':') ||
18357c478bd9Sstevel@tonic-gate 	    (strncmp((char *)ep->value, "unix:", 5) == 0))) {
18367c478bd9Sstevel@tonic-gate 		char hbuf[MAXHOSTNAMELEN];
18377c478bd9Sstevel@tonic-gate 		char *cp2 = strchr((char *)ep->value, ':');
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 		if (gethostname(hbuf, MAXHOSTNAMELEN) == -1) {
18407c478bd9Sstevel@tonic-gate 			perror("telnet: cannot get hostname");
18417c478bd9Sstevel@tonic-gate 			return (0);
18427c478bd9Sstevel@tonic-gate 		}
18437c478bd9Sstevel@tonic-gate 		hbuf[MAXHOSTNAMELEN-1] = '\0';
18447c478bd9Sstevel@tonic-gate 		cp = malloc(strlen(hbuf) + strlen(cp2) + 1);
18457c478bd9Sstevel@tonic-gate 		if (cp == NULL) {
18467c478bd9Sstevel@tonic-gate 			perror("telnet: cannot define DISPLAY variable");
18477c478bd9Sstevel@tonic-gate 			return (0);
18487c478bd9Sstevel@tonic-gate 		}
18497c478bd9Sstevel@tonic-gate 		(void) sprintf((char *)cp, "%s%s", hbuf, cp2);
18507c478bd9Sstevel@tonic-gate 		free(ep->value);
18517c478bd9Sstevel@tonic-gate 		ep->value = (unsigned char *)cp;
18527c478bd9Sstevel@tonic-gate 	}
18537c478bd9Sstevel@tonic-gate 	/*
18547c478bd9Sstevel@tonic-gate 	 * If LOGNAME is defined, but USER is not, then add
18557c478bd9Sstevel@tonic-gate 	 * USER with the value from LOGNAME.  We do this because the "accepted
18567c478bd9Sstevel@tonic-gate 	 * practice" is to always pass USER on the wire, but SVR4 uses
18577c478bd9Sstevel@tonic-gate 	 * LOGNAME by default.
18587c478bd9Sstevel@tonic-gate 	 */
18597c478bd9Sstevel@tonic-gate 	if ((ep = env_find((uchar_t *)"LOGNAME")) != NULL &&
18607c478bd9Sstevel@tonic-gate 		env_find((uchar_t *)"USER") == NULL) {
18617c478bd9Sstevel@tonic-gate 		if (env_define((unsigned char *)"USER", ep->value) != NULL)
18627c478bd9Sstevel@tonic-gate 			env_unexport((unsigned char *)"USER");
18637c478bd9Sstevel@tonic-gate 	}
18647c478bd9Sstevel@tonic-gate 	env_export((unsigned char *)"DISPLAY");
18657c478bd9Sstevel@tonic-gate 	env_export((unsigned char *)"PRINTER");
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	return (1);
18687c478bd9Sstevel@tonic-gate }
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate static struct env_lst *
env_define(unsigned char * var,unsigned char * value)1871*905213bbSToomas Soome env_define(unsigned char *var, unsigned char *value)
18727c478bd9Sstevel@tonic-gate {
18737c478bd9Sstevel@tonic-gate 	unsigned char *tmp_value;
18747c478bd9Sstevel@tonic-gate 	unsigned char *tmp_var;
18757c478bd9Sstevel@tonic-gate 	struct env_lst *ep;
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	/*
18787c478bd9Sstevel@tonic-gate 	 * Allocate copies of arguments first, to make cleanup easier
18797c478bd9Sstevel@tonic-gate 	 * in the case of allocation errors.
18807c478bd9Sstevel@tonic-gate 	 */
18817c478bd9Sstevel@tonic-gate 	tmp_var = (unsigned char *)strdup((char *)var);
18827c478bd9Sstevel@tonic-gate 	if (tmp_var == NULL) {
18837c478bd9Sstevel@tonic-gate 		perror("telnet: can't copy environment variable name");
18847c478bd9Sstevel@tonic-gate 		return (NULL);
18857c478bd9Sstevel@tonic-gate 	}
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 	tmp_value = (unsigned char *)strdup((char *)value);
18887c478bd9Sstevel@tonic-gate 	if (tmp_value == NULL) {
18897c478bd9Sstevel@tonic-gate 		free(tmp_var);
18907c478bd9Sstevel@tonic-gate 		perror("telnet: can't copy environment variable value");
18917c478bd9Sstevel@tonic-gate 		return (NULL);
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	if (ep = env_find(var)) {
18957c478bd9Sstevel@tonic-gate 		if (ep->var)
18967c478bd9Sstevel@tonic-gate 			free(ep->var);
18977c478bd9Sstevel@tonic-gate 		if (ep->value)
18987c478bd9Sstevel@tonic-gate 			free(ep->value);
18997c478bd9Sstevel@tonic-gate 	} else {
19007c478bd9Sstevel@tonic-gate 		ep = malloc(sizeof (struct env_lst));
19017c478bd9Sstevel@tonic-gate 		if (ep == NULL) {
19027c478bd9Sstevel@tonic-gate 			perror("telnet: can't define environment variable");
19037c478bd9Sstevel@tonic-gate 			free(tmp_var);
19047c478bd9Sstevel@tonic-gate 			free(tmp_value);
19057c478bd9Sstevel@tonic-gate 			return (NULL);
19067c478bd9Sstevel@tonic-gate 		}
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 		ep->next = envlisthead.next;
19097c478bd9Sstevel@tonic-gate 		envlisthead.next = ep;
19107c478bd9Sstevel@tonic-gate 		ep->prev = &envlisthead;
19117c478bd9Sstevel@tonic-gate 		if (ep->next)
19127c478bd9Sstevel@tonic-gate 			ep->next->prev = ep;
19137c478bd9Sstevel@tonic-gate 	}
19147c478bd9Sstevel@tonic-gate 	ep->welldefined = opt_welldefined((char *)var);
19157c478bd9Sstevel@tonic-gate 	ep->export = 1;
19167c478bd9Sstevel@tonic-gate 	ep->var = tmp_var;
19177c478bd9Sstevel@tonic-gate 	ep->value = tmp_value;
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	return (ep);
19207c478bd9Sstevel@tonic-gate }
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate static void
env_undefine(unsigned char * var)1923*905213bbSToomas Soome env_undefine(unsigned char *var)
19247c478bd9Sstevel@tonic-gate {
1925*905213bbSToomas Soome 	struct env_lst *ep;
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	if (ep = env_find(var)) {
19287c478bd9Sstevel@tonic-gate 		ep->prev->next = ep->next;
19297c478bd9Sstevel@tonic-gate 		if (ep->next)
19307c478bd9Sstevel@tonic-gate 			ep->next->prev = ep->prev;
19317c478bd9Sstevel@tonic-gate 		if (ep->var)
19327c478bd9Sstevel@tonic-gate 			free(ep->var);
19337c478bd9Sstevel@tonic-gate 		if (ep->value)
19347c478bd9Sstevel@tonic-gate 			free(ep->value);
19357c478bd9Sstevel@tonic-gate 		free(ep);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate }
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate static void
env_export(unsigned char * var)1940*905213bbSToomas Soome env_export(unsigned char *var)
19417c478bd9Sstevel@tonic-gate {
1942*905213bbSToomas Soome 	struct env_lst *ep;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	if (ep = env_find(var))
19457c478bd9Sstevel@tonic-gate 		ep->export = 1;
19467c478bd9Sstevel@tonic-gate }
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate static void
env_unexport(unsigned char * var)1949*905213bbSToomas Soome env_unexport(unsigned char *var)
19507c478bd9Sstevel@tonic-gate {
1951*905213bbSToomas Soome 	struct env_lst *ep;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	if (ep = env_find(var))
19547c478bd9Sstevel@tonic-gate 		ep->export = 0;
19557c478bd9Sstevel@tonic-gate }
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate static void
env_send(unsigned char * var)1958*905213bbSToomas Soome env_send(unsigned char *var)
19597c478bd9Sstevel@tonic-gate {
1960*905213bbSToomas Soome 	struct env_lst *ep;
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	if (my_state_is_wont(TELOPT_NEW_ENVIRON)
19637c478bd9Sstevel@tonic-gate #ifdef	OLD_ENVIRON
19647c478bd9Sstevel@tonic-gate 	    /* old style */ && my_state_is_wont(TELOPT_OLD_ENVIRON)
19657c478bd9Sstevel@tonic-gate #endif
19667c478bd9Sstevel@tonic-gate 		/* no environ */) {
19677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19687c478bd9Sstevel@tonic-gate 		    "Cannot send '%s': Telnet ENVIRON option not enabled\n",
19697c478bd9Sstevel@tonic-gate 									var);
19707c478bd9Sstevel@tonic-gate 		return;
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 	ep = env_find(var);
19737c478bd9Sstevel@tonic-gate 	if (ep == 0) {
19747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19757c478bd9Sstevel@tonic-gate 		    "Cannot send '%s': variable not defined\n", var);
19767c478bd9Sstevel@tonic-gate 		return;
19777c478bd9Sstevel@tonic-gate 	}
19787c478bd9Sstevel@tonic-gate 	env_opt_start_info();
19797c478bd9Sstevel@tonic-gate 	env_opt_add(ep->var);
19807c478bd9Sstevel@tonic-gate 	env_opt_end(0);
19817c478bd9Sstevel@tonic-gate }
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate static void
env_list(void)1984*905213bbSToomas Soome env_list(void)
19857c478bd9Sstevel@tonic-gate {
1986*905213bbSToomas Soome 	struct env_lst *ep;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	for (ep = envlisthead.next; ep; ep = ep->next) {
19897c478bd9Sstevel@tonic-gate 		(void) printf("%c %-20s %s\n", ep->export ? '*' : ' ',
19907c478bd9Sstevel@tonic-gate 					ep->var, ep->value);
19917c478bd9Sstevel@tonic-gate 	}
19927c478bd9Sstevel@tonic-gate }
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate unsigned char *
env_default(int init,int welldefined)1995*905213bbSToomas Soome env_default(int init, int welldefined)
19967c478bd9Sstevel@tonic-gate {
19977c478bd9Sstevel@tonic-gate 	static struct env_lst *nep = NULL;
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	if (init) {
20007c478bd9Sstevel@tonic-gate 		/* return value is not used */
20017c478bd9Sstevel@tonic-gate 		nep = &envlisthead;
20027c478bd9Sstevel@tonic-gate 		return (NULL);
20037c478bd9Sstevel@tonic-gate 	}
20047c478bd9Sstevel@tonic-gate 	if (nep) {
20057c478bd9Sstevel@tonic-gate 		while ((nep = nep->next) != NULL) {
20067c478bd9Sstevel@tonic-gate 			if (nep->export && (nep->welldefined == welldefined))
20077c478bd9Sstevel@tonic-gate 				return (nep->var);
20087c478bd9Sstevel@tonic-gate 		}
20097c478bd9Sstevel@tonic-gate 	}
20107c478bd9Sstevel@tonic-gate 	return (NULL);
20117c478bd9Sstevel@tonic-gate }
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate unsigned char *
env_getvalue(unsigned char * var)2014*905213bbSToomas Soome env_getvalue(unsigned char *var)
20157c478bd9Sstevel@tonic-gate {
2016*905213bbSToomas Soome 	struct env_lst *ep;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	if (ep = env_find(var))
20197c478bd9Sstevel@tonic-gate 		return (ep->value);
20207c478bd9Sstevel@tonic-gate 	return (NULL);
20217c478bd9Sstevel@tonic-gate }
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate #if defined(OLD_ENVIRON) && defined(ENV_HACK)
20247c478bd9Sstevel@tonic-gate static void
env_varval(unsigned char * what)2025*905213bbSToomas Soome env_varval(unsigned char *what)
20267c478bd9Sstevel@tonic-gate {
20277c478bd9Sstevel@tonic-gate 	extern int old_env_var, old_env_value, env_auto;
20287c478bd9Sstevel@tonic-gate 	int len = strlen((char *)what);
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	if (len == 0)
20317c478bd9Sstevel@tonic-gate 		goto unknown;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	if (strncasecmp((char *)what, "status", len) == 0) {
20347c478bd9Sstevel@tonic-gate 		if (env_auto)
20357c478bd9Sstevel@tonic-gate 			(void) printf("%s%s", "VAR and VALUE are/will be ",
20367c478bd9Sstevel@tonic-gate 					"determined automatically\n");
20377c478bd9Sstevel@tonic-gate 		if (old_env_var == OLD_ENV_VAR)
20387c478bd9Sstevel@tonic-gate 			(void) printf(
20397c478bd9Sstevel@tonic-gate 			    "VAR and VALUE set to correct definitions\n");
20407c478bd9Sstevel@tonic-gate 		else
20417c478bd9Sstevel@tonic-gate 			(void) printf(
20427c478bd9Sstevel@tonic-gate 			    "VAR and VALUE definitions are reversed\n");
20437c478bd9Sstevel@tonic-gate 	} else if (strncasecmp((char *)what, "auto", len) == 0) {
20447c478bd9Sstevel@tonic-gate 		env_auto = 1;
20457c478bd9Sstevel@tonic-gate 		old_env_var = OLD_ENV_VALUE;
20467c478bd9Sstevel@tonic-gate 		old_env_value = OLD_ENV_VAR;
20477c478bd9Sstevel@tonic-gate 	} else if (strncasecmp((char *)what, "right", len) == 0) {
20487c478bd9Sstevel@tonic-gate 		env_auto = 0;
20497c478bd9Sstevel@tonic-gate 		old_env_var = OLD_ENV_VAR;
20507c478bd9Sstevel@tonic-gate 		old_env_value = OLD_ENV_VALUE;
20517c478bd9Sstevel@tonic-gate 	} else if (strncasecmp((char *)what, "wrong", len) == 0) {
20527c478bd9Sstevel@tonic-gate 		env_auto = 0;
20537c478bd9Sstevel@tonic-gate 		old_env_var = OLD_ENV_VALUE;
20547c478bd9Sstevel@tonic-gate 		old_env_value = OLD_ENV_VAR;
20557c478bd9Sstevel@tonic-gate 	} else {
20567c478bd9Sstevel@tonic-gate unknown:
20577c478bd9Sstevel@tonic-gate 		(void) printf(
20587c478bd9Sstevel@tonic-gate 		    "Unknown \"varval\" command. (\"auto\", \"right\", "
20597c478bd9Sstevel@tonic-gate 		    "\"wrong\", \"status\")\n");
20607c478bd9Sstevel@tonic-gate 	}
20617c478bd9Sstevel@tonic-gate }
20627c478bd9Sstevel@tonic-gate #endif	/* OLD_ENVIRON && ENV_HACK */
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate /*
20657c478bd9Sstevel@tonic-gate  * The AUTHENTICATE command.
20667c478bd9Sstevel@tonic-gate  */
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate struct authlist {
20697c478bd9Sstevel@tonic-gate 	char	*name;
20707c478bd9Sstevel@tonic-gate 	char	*help;
20717c478bd9Sstevel@tonic-gate 	int	(*handler)();
20727c478bd9Sstevel@tonic-gate 	int	narg;
20737c478bd9Sstevel@tonic-gate };
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate extern int auth_enable(char *);
20767c478bd9Sstevel@tonic-gate extern int auth_disable(char *);
20777c478bd9Sstevel@tonic-gate extern int auth_status(void);
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate static int auth_help(void);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate static struct authlist AuthList[] = {
20827c478bd9Sstevel@tonic-gate 	{ "status",
20837c478bd9Sstevel@tonic-gate 	    "Display current status of authentication information",
20847c478bd9Sstevel@tonic-gate 	    auth_status,	0 },
20857c478bd9Sstevel@tonic-gate 	{ "disable",
20867c478bd9Sstevel@tonic-gate 	    "Disable an authentication type ('auth disable ?' for more)",
20877c478bd9Sstevel@tonic-gate 	    auth_disable,	1 },
20887c478bd9Sstevel@tonic-gate 	{ "enable",
20897c478bd9Sstevel@tonic-gate 	    "Enable an authentication type ('auth enable ?' for more)",
20907c478bd9Sstevel@tonic-gate 	    auth_enable,	1 },
20917c478bd9Sstevel@tonic-gate 	{ "help",	0,			auth_help,		0 },
20927c478bd9Sstevel@tonic-gate 	{ "?",	"Print help information",	auth_help,		0 },
20937c478bd9Sstevel@tonic-gate 	{ 0 },
20947c478bd9Sstevel@tonic-gate };
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate static int
auth_help(void)20977c478bd9Sstevel@tonic-gate auth_help(void)
20987c478bd9Sstevel@tonic-gate {
20997c478bd9Sstevel@tonic-gate 	struct authlist *c;
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	for (c = AuthList; c->name; c++) {
21027c478bd9Sstevel@tonic-gate 		if (c->help) {
21037c478bd9Sstevel@tonic-gate 			if (*c->help)
21047c478bd9Sstevel@tonic-gate 				(void) printf("%-15s %s\n", c->name, c->help);
21057c478bd9Sstevel@tonic-gate 			else
21067c478bd9Sstevel@tonic-gate 				(void) printf("\n");
21077c478bd9Sstevel@tonic-gate 		}
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 	return (0);
21107c478bd9Sstevel@tonic-gate }
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate static int
auth_cmd(int argc,char * argv[])2114*905213bbSToomas Soome auth_cmd(int argc, char *argv[])
21157c478bd9Sstevel@tonic-gate {
21167c478bd9Sstevel@tonic-gate 	struct authlist *c;
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	if (argc < 2) {
21197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Need an argument to 'auth' "
21207c478bd9Sstevel@tonic-gate 			"command.  'auth ?' for help.\n");
21217c478bd9Sstevel@tonic-gate 		return (0);
21227c478bd9Sstevel@tonic-gate 	}
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	c = (struct authlist *)
21257c478bd9Sstevel@tonic-gate 		genget(argv[1], (char **)AuthList, sizeof (struct authlist));
21267c478bd9Sstevel@tonic-gate 	if (c == 0) {
21277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
21287c478bd9Sstevel@tonic-gate 		    "'%s': unknown argument ('auth ?' for help).\n",
21297c478bd9Sstevel@tonic-gate 		    argv[1]);
21307c478bd9Sstevel@tonic-gate 		return (0);
21317c478bd9Sstevel@tonic-gate 	}
21327c478bd9Sstevel@tonic-gate 	if (Ambiguous(c)) {
21337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
21347c478bd9Sstevel@tonic-gate 		    "'%s': ambiguous argument ('auth ?' for help).\n", argv[1]);
21357c478bd9Sstevel@tonic-gate 		return (0);
21367c478bd9Sstevel@tonic-gate 	}
21377c478bd9Sstevel@tonic-gate 	if (c->narg + 2 != argc) {
21387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
21397c478bd9Sstevel@tonic-gate 		    "Need %s%d argument%s to 'auth %s' command."
21407c478bd9Sstevel@tonic-gate 		    " 'auth ?' for help.\n",
21417c478bd9Sstevel@tonic-gate 		    c->narg + 2 < argc ? "only " : "",
21427c478bd9Sstevel@tonic-gate 		    c->narg, c->narg == 1 ? "" : "s", c->name);
21437c478bd9Sstevel@tonic-gate 		return (0);
21447c478bd9Sstevel@tonic-gate 	}
21457c478bd9Sstevel@tonic-gate 	return ((*c->handler)(argv[2], argv[3]));
21467c478bd9Sstevel@tonic-gate }
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate /*
21497c478bd9Sstevel@tonic-gate  * The FORWARD command.
21507c478bd9Sstevel@tonic-gate  */
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate extern int forward_flags;
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate struct forwlist {
21557c478bd9Sstevel@tonic-gate 	char *name;
21567c478bd9Sstevel@tonic-gate 	char *help;
21577c478bd9Sstevel@tonic-gate 	int (*handler)();
21587c478bd9Sstevel@tonic-gate 	int f_flags;
21597c478bd9Sstevel@tonic-gate };
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate static int forw_status(void);
21627c478bd9Sstevel@tonic-gate static int forw_set(int);
21637c478bd9Sstevel@tonic-gate static int forw_help(void);
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate static struct forwlist ForwList[] = {
21667c478bd9Sstevel@tonic-gate 	{"status",
21677c478bd9Sstevel@tonic-gate 		"Display current status of credential forwarding",
21687c478bd9Sstevel@tonic-gate 		forw_status, 0},
21697c478bd9Sstevel@tonic-gate 	{"disable",
21707c478bd9Sstevel@tonic-gate 		"Disable credential forwarding",
21717c478bd9Sstevel@tonic-gate 		forw_set, 0},
21727c478bd9Sstevel@tonic-gate 	{"enable",
21737c478bd9Sstevel@tonic-gate 		"Enable credential forwarding",
21747c478bd9Sstevel@tonic-gate 		forw_set, OPTS_FORWARD_CREDS},
21757c478bd9Sstevel@tonic-gate 	{"forwardable",
21767c478bd9Sstevel@tonic-gate 		"Enable credential forwarding of "
21777c478bd9Sstevel@tonic-gate 				"forwardable credentials",
21787c478bd9Sstevel@tonic-gate 		forw_set, OPTS_FORWARD_CREDS |	OPTS_FORWARDABLE_CREDS},
21797c478bd9Sstevel@tonic-gate 	{"help",
21807c478bd9Sstevel@tonic-gate 		0,
21817c478bd9Sstevel@tonic-gate 		forw_help, 0},
21827c478bd9Sstevel@tonic-gate 	{"?",
21837c478bd9Sstevel@tonic-gate 		"Print help information",
21847c478bd9Sstevel@tonic-gate 		forw_help, 0},
21857c478bd9Sstevel@tonic-gate 	{0},
21867c478bd9Sstevel@tonic-gate };
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate static int
forw_status(void)21897c478bd9Sstevel@tonic-gate forw_status(void)
21907c478bd9Sstevel@tonic-gate {
21917c478bd9Sstevel@tonic-gate 	if (forward_flags & OPTS_FORWARD_CREDS) {
21927c478bd9Sstevel@tonic-gate 		if (forward_flags & OPTS_FORWARDABLE_CREDS)
21937c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
21947c478bd9Sstevel@tonic-gate 				"Credential forwarding of "
21957c478bd9Sstevel@tonic-gate 				"forwardable credentials enabled\n"));
21967c478bd9Sstevel@tonic-gate 		else
21977c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
21987c478bd9Sstevel@tonic-gate 				"Credential forwarding enabled\n"));
21997c478bd9Sstevel@tonic-gate 	} else
22007c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Credential forwarding disabled\n"));
22017c478bd9Sstevel@tonic-gate 	return (0);
22027c478bd9Sstevel@tonic-gate }
22037c478bd9Sstevel@tonic-gate 
2204740638c8Sbw static int
forw_set(int f_flags)22057c478bd9Sstevel@tonic-gate forw_set(int f_flags)
22067c478bd9Sstevel@tonic-gate {
22077c478bd9Sstevel@tonic-gate 	forward_flags = f_flags;
22087c478bd9Sstevel@tonic-gate 	return (0);
22097c478bd9Sstevel@tonic-gate }
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate static int
forw_help(void)22127c478bd9Sstevel@tonic-gate forw_help(void)
22137c478bd9Sstevel@tonic-gate {
22147c478bd9Sstevel@tonic-gate 	struct forwlist *c;
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	for (c = ForwList; c->name; c++) {
22177c478bd9Sstevel@tonic-gate 		if (c->help) {
22187c478bd9Sstevel@tonic-gate 			if (*c->help)
22197c478bd9Sstevel@tonic-gate 				(void) printf("%-15s %s\r\n", c->name, c->help);
22207c478bd9Sstevel@tonic-gate 			else
22217c478bd9Sstevel@tonic-gate 				(void) printf("\n");
22227c478bd9Sstevel@tonic-gate 		}
22237c478bd9Sstevel@tonic-gate 	}
22247c478bd9Sstevel@tonic-gate 	return (0);
22257c478bd9Sstevel@tonic-gate }
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate static int
forw_cmd(int argc,char * argv[])22287c478bd9Sstevel@tonic-gate forw_cmd(int argc, char *argv[])
22297c478bd9Sstevel@tonic-gate {
22307c478bd9Sstevel@tonic-gate 	struct forwlist *c;
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	if (argc < 2) {
22337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
22347c478bd9Sstevel@tonic-gate 			"Need an argument to 'forward' "
22357c478bd9Sstevel@tonic-gate 			"command.  'forward ?' for help.\n"));
22367c478bd9Sstevel@tonic-gate 		return (0);
22377c478bd9Sstevel@tonic-gate 	}
22387c478bd9Sstevel@tonic-gate 	c = (struct forwlist *)genget(argv[1], (char **)ForwList,
22397c478bd9Sstevel@tonic-gate 		sizeof (struct forwlist));
22407c478bd9Sstevel@tonic-gate 	if (c == 0) {
22417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
22427c478bd9Sstevel@tonic-gate 		    "'%s': unknown argument ('forward ?' for help).\n"),
22437c478bd9Sstevel@tonic-gate 		    argv[1]);
22447c478bd9Sstevel@tonic-gate 		return (0);
22457c478bd9Sstevel@tonic-gate 	}
22467c478bd9Sstevel@tonic-gate 	if (Ambiguous(c)) {
22477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
22487c478bd9Sstevel@tonic-gate 		    "'%s': ambiguous argument ('forward ?' for help).\n"),
22497c478bd9Sstevel@tonic-gate 		    argv[1]);
22507c478bd9Sstevel@tonic-gate 		return (0);
22517c478bd9Sstevel@tonic-gate 	}
22527c478bd9Sstevel@tonic-gate 	if (argc != 2) {
22537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
22547c478bd9Sstevel@tonic-gate 		    "No arguments needed to 'forward %s' command.  "
22557c478bd9Sstevel@tonic-gate 		    "'forward ?' for help.\n"), c->name);
22567c478bd9Sstevel@tonic-gate 		return (0);
22577c478bd9Sstevel@tonic-gate 	}
22587c478bd9Sstevel@tonic-gate 	return ((*c->handler) (c->f_flags));
22597c478bd9Sstevel@tonic-gate }
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate /*
22627c478bd9Sstevel@tonic-gate  * The ENCRYPT command.
22637c478bd9Sstevel@tonic-gate  */
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate struct encryptlist {
22667c478bd9Sstevel@tonic-gate 	char	*name;
22677c478bd9Sstevel@tonic-gate 	char	*help;
22687c478bd9Sstevel@tonic-gate 	int	(*handler)();
22697c478bd9Sstevel@tonic-gate 	int	needconnect;
22707c478bd9Sstevel@tonic-gate 	int	minarg;
22717c478bd9Sstevel@tonic-gate 	int	maxarg;
22727c478bd9Sstevel@tonic-gate };
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate static int EncryptHelp(void);
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate static struct encryptlist EncryptList[] = {
22777c478bd9Sstevel@tonic-gate 	{ "enable", "Enable encryption. ('encrypt enable ?' for more)",
22787c478bd9Sstevel@tonic-gate 						EncryptEnable, 1, 1, 2 },
22797c478bd9Sstevel@tonic-gate 	{ "disable", "Disable encryption. ('encrypt disable ?' for more)",
22807c478bd9Sstevel@tonic-gate 						EncryptDisable, 0, 1, 2 },
22817c478bd9Sstevel@tonic-gate 	{ "type", "Set encryption type. ('encrypt type ?' for more)",
22827c478bd9Sstevel@tonic-gate 						EncryptType, 0, 1, 2 },
22837c478bd9Sstevel@tonic-gate 	{ "start", "Start encryption. ('encrypt start ?' for more)",
22847c478bd9Sstevel@tonic-gate 						EncryptStart, 1, 0, 1 },
22857c478bd9Sstevel@tonic-gate 	{ "stop", "Stop encryption. ('encrypt stop ?' for more)",
22867c478bd9Sstevel@tonic-gate 						EncryptStop, 1, 0, 1 },
22877c478bd9Sstevel@tonic-gate 	{ "input", "Start encrypting the input stream",
22887c478bd9Sstevel@tonic-gate 						EncryptStartInput, 1, 0, 0 },
22897c478bd9Sstevel@tonic-gate 	{ "-input", "Stop encrypting the input stream",
22907c478bd9Sstevel@tonic-gate 						EncryptStopInput, 1, 0, 0 },
22917c478bd9Sstevel@tonic-gate 	{ "output", "Start encrypting the output stream",
22927c478bd9Sstevel@tonic-gate 						EncryptStartOutput, 1, 0, 0 },
22937c478bd9Sstevel@tonic-gate 	{ "-output", "Stop encrypting the output stream",
22947c478bd9Sstevel@tonic-gate 						EncryptStopOutput, 1, 0, 0 },
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	{ "status",	"Display current status of encryption information",
22977c478bd9Sstevel@tonic-gate 						EncryptStatus,	0, 0, 0 },
22987c478bd9Sstevel@tonic-gate 	{ "help",	0,
22997c478bd9Sstevel@tonic-gate 						EncryptHelp,	0, 0, 0 },
23007c478bd9Sstevel@tonic-gate 	{ "?",	"Print help information",	EncryptHelp,	0, 0, 0 },
23017c478bd9Sstevel@tonic-gate 	{ 0 },
23027c478bd9Sstevel@tonic-gate };
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate static int
EncryptHelp(void)23057c478bd9Sstevel@tonic-gate EncryptHelp(void)
23067c478bd9Sstevel@tonic-gate {
23077c478bd9Sstevel@tonic-gate 	struct encryptlist *c;
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	for (c = EncryptList; c->name; c++) {
23107c478bd9Sstevel@tonic-gate 		if (c->help) {
23117c478bd9Sstevel@tonic-gate 			if (*c->help)
23127c478bd9Sstevel@tonic-gate 				(void) printf("%-15s %s\n", c->name, c->help);
23137c478bd9Sstevel@tonic-gate 			else
23147c478bd9Sstevel@tonic-gate 				(void) printf("\n");
23157c478bd9Sstevel@tonic-gate 		}
23167c478bd9Sstevel@tonic-gate 	}
23177c478bd9Sstevel@tonic-gate 	return (0);
23187c478bd9Sstevel@tonic-gate }
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate static int
encrypt_cmd(int argc,char * argv[])23217c478bd9Sstevel@tonic-gate encrypt_cmd(int  argc, char *argv[])
23227c478bd9Sstevel@tonic-gate {
23237c478bd9Sstevel@tonic-gate 	struct encryptlist *c;
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	if (argc < 2) {
23267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
23277c478bd9Sstevel@tonic-gate 			"Need an argument to 'encrypt' command.  "
23287c478bd9Sstevel@tonic-gate 			"'encrypt ?' for help.\n"));
23297c478bd9Sstevel@tonic-gate 		return (0);
23307c478bd9Sstevel@tonic-gate 	}
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate 	c = (struct encryptlist *)
23337c478bd9Sstevel@tonic-gate 	    genget(argv[1], (char **)EncryptList, sizeof (struct encryptlist));
23347c478bd9Sstevel@tonic-gate 	if (c == 0) {
23357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
23367c478bd9Sstevel@tonic-gate 		    "'%s': unknown argument ('encrypt ?' for help).\n"),
23377c478bd9Sstevel@tonic-gate 		    argv[1]);
23387c478bd9Sstevel@tonic-gate 		return (0);
23397c478bd9Sstevel@tonic-gate 	}
23407c478bd9Sstevel@tonic-gate 	if (Ambiguous(c)) {
23417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
23427c478bd9Sstevel@tonic-gate 		    "'%s': ambiguous argument ('encrypt ?' for help).\n"),
23437c478bd9Sstevel@tonic-gate 		    argv[1]);
23447c478bd9Sstevel@tonic-gate 		return (0);
23457c478bd9Sstevel@tonic-gate 	}
23467c478bd9Sstevel@tonic-gate 	argc -= 2;
23477c478bd9Sstevel@tonic-gate 	if (argc < c->minarg || argc > c->maxarg) {
23487c478bd9Sstevel@tonic-gate 		if (c->minarg == c->maxarg) {
23497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Need %s%d %s "),
23507c478bd9Sstevel@tonic-gate 			c->minarg < argc ?
23517c478bd9Sstevel@tonic-gate 			gettext("only ") : "", c->minarg,
23527c478bd9Sstevel@tonic-gate 			c->minarg == 1 ?
23537c478bd9Sstevel@tonic-gate 			gettext("argument") : gettext("arguments"));
23547c478bd9Sstevel@tonic-gate 		} else {
23557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
23567c478bd9Sstevel@tonic-gate 			    gettext("Need %s%d-%d arguments "),
23577c478bd9Sstevel@tonic-gate 			c->maxarg < argc ?
23587c478bd9Sstevel@tonic-gate 			gettext("only ") : "", c->minarg, c->maxarg);
23597c478bd9Sstevel@tonic-gate 		}
23607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
23617c478bd9Sstevel@tonic-gate 		    "to 'encrypt %s' command.  'encrypt ?' for help.\n"),
23627c478bd9Sstevel@tonic-gate 		    c->name);
23637c478bd9Sstevel@tonic-gate 		return (0);
23647c478bd9Sstevel@tonic-gate 	}
23657c478bd9Sstevel@tonic-gate 	if (c->needconnect && !connected) {
23667c478bd9Sstevel@tonic-gate 		if (!(argc &&
23677c478bd9Sstevel@tonic-gate 		    (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
23687c478bd9Sstevel@tonic-gate 			(void) printf(
23697c478bd9Sstevel@tonic-gate 			    gettext("?Need to be connected first.\n"));
23707c478bd9Sstevel@tonic-gate 			return (0);
23717c478bd9Sstevel@tonic-gate 		}
23727c478bd9Sstevel@tonic-gate 	}
23737c478bd9Sstevel@tonic-gate 	return ((*c->handler)(argc > 0 ? argv[2] : 0,
23747c478bd9Sstevel@tonic-gate 	    argc > 1 ? argv[3] : 0, argc > 2 ? argv[4] : 0));
23757c478bd9Sstevel@tonic-gate }
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate /*
23787c478bd9Sstevel@tonic-gate  * Print status about the connection.
23797c478bd9Sstevel@tonic-gate  */
2380740638c8Sbw static int
status(int argc,char * argv[])23817c478bd9Sstevel@tonic-gate status(int argc, char *argv[])
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate 	if (connected) {
23847c478bd9Sstevel@tonic-gate 		(void) printf("Connected to %s.\n", hostname);
23857c478bd9Sstevel@tonic-gate 		if ((argc < 2) || strcmp(argv[1], "notmuch")) {
23867c478bd9Sstevel@tonic-gate 			int mode = getconnmode();
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 			if (my_want_state_is_will(TELOPT_LINEMODE)) {
23897c478bd9Sstevel@tonic-gate 				(void) printf(
23907c478bd9Sstevel@tonic-gate 				    "Operating with LINEMODE option\n");
23917c478bd9Sstevel@tonic-gate 				(void) printf(
23927c478bd9Sstevel@tonic-gate 				    "%s line editing\n", (mode&MODE_EDIT) ?
23937c478bd9Sstevel@tonic-gate 				    "Local" : "No");
23947c478bd9Sstevel@tonic-gate 				(void) printf("%s catching of signals\n",
23957c478bd9Sstevel@tonic-gate 				    (mode&MODE_TRAPSIG) ? "Local" : "No");
23967c478bd9Sstevel@tonic-gate 				slcstate();
23977c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
23987c478bd9Sstevel@tonic-gate 			} else if (kludgelinemode &&
23997c478bd9Sstevel@tonic-gate 			    my_want_state_is_dont(TELOPT_SGA)) {
24007c478bd9Sstevel@tonic-gate 				(void) printf(
24017c478bd9Sstevel@tonic-gate 				    "Operating in obsolete linemode\n");
24027c478bd9Sstevel@tonic-gate #endif
24037c478bd9Sstevel@tonic-gate 			} else {
24047c478bd9Sstevel@tonic-gate 				(void) printf(
24057c478bd9Sstevel@tonic-gate 					"Operating in single character mode\n");
24067c478bd9Sstevel@tonic-gate 				if (localchars)
24077c478bd9Sstevel@tonic-gate 					(void) printf(
24087c478bd9Sstevel@tonic-gate 					"Catching signals locally\n");
24097c478bd9Sstevel@tonic-gate 			}
24107c478bd9Sstevel@tonic-gate 			(void) printf("%s character echo\n", (mode&MODE_ECHO) ?
24117c478bd9Sstevel@tonic-gate 			    "Local" : "Remote");
24127c478bd9Sstevel@tonic-gate 			if (my_want_state_is_will(TELOPT_LFLOW))
24137c478bd9Sstevel@tonic-gate 				(void) printf("%s flow control\n",
24147c478bd9Sstevel@tonic-gate 				    (mode&MODE_FLOW) ? "Local" : "No");
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 			encrypt_display();
24177c478bd9Sstevel@tonic-gate 		}
24187c478bd9Sstevel@tonic-gate 	} else {
24197c478bd9Sstevel@tonic-gate 		(void) printf("No connection.\n");
24207c478bd9Sstevel@tonic-gate 	}
24217c478bd9Sstevel@tonic-gate 	if (rlogin != _POSIX_VDISABLE)
24227c478bd9Sstevel@tonic-gate 		(void) printf("Escape character is '%s'.\n", control(rlogin));
24237c478bd9Sstevel@tonic-gate 	else
24247c478bd9Sstevel@tonic-gate 		(void) printf(
24257c478bd9Sstevel@tonic-gate 		    "Escape character is '%s'.\n", esc_control(escape));
24267c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
24277c478bd9Sstevel@tonic-gate 	return (1);
24287c478bd9Sstevel@tonic-gate }
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate /*
24317c478bd9Sstevel@tonic-gate  * Parse the user input (cmd_line_input) which should:
24327c478bd9Sstevel@tonic-gate  * - start with the target host, or with "@" or "!@" followed by at least one
24337c478bd9Sstevel@tonic-gate  *   gateway.
24347c478bd9Sstevel@tonic-gate  * - each host (can be literal address or hostname) can be separated by ",",
24357c478bd9Sstevel@tonic-gate  *   "@", or ",@".
24367c478bd9Sstevel@tonic-gate  * Note that the last host is the target, all the others (if any ) are the
24377c478bd9Sstevel@tonic-gate  * gateways.
24387c478bd9Sstevel@tonic-gate  *
24397c478bd9Sstevel@tonic-gate  * Returns:	-1	if a library call fails, too many gateways, or parse
24407c478bd9Sstevel@tonic-gate  *			error
24417c478bd9Sstevel@tonic-gate  *		num_gw	otherwise
24427c478bd9Sstevel@tonic-gate  * On successful return, hostname_list points to a list of hosts (last one being
24437c478bd9Sstevel@tonic-gate  * the target, others gateways), src_rtng_type points to the type of source
24447c478bd9Sstevel@tonic-gate  * routing (strict vs. loose)
24457c478bd9Sstevel@tonic-gate  */
24467c478bd9Sstevel@tonic-gate static int
parse_input(char * cmd_line_input,char ** hostname_list,uchar_t * src_rtng_type)24477c478bd9Sstevel@tonic-gate parse_input(char *cmd_line_input, char **hostname_list, uchar_t *src_rtng_type)
24487c478bd9Sstevel@tonic-gate {
24497c478bd9Sstevel@tonic-gate 	char hname[MAXHOSTNAMELEN + 1];
24507c478bd9Sstevel@tonic-gate 	char *cp;
24517c478bd9Sstevel@tonic-gate 	int gw_count;
24527c478bd9Sstevel@tonic-gate 	int i;
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 	gw_count = 0;
24557c478bd9Sstevel@tonic-gate 	cp = cmd_line_input;
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 	/*
24587c478bd9Sstevel@tonic-gate 	 * Defining ICMD generates the Itelnet binary, the special version of
24597c478bd9Sstevel@tonic-gate 	 * telnet which is used with firewall proxy.
24607c478bd9Sstevel@tonic-gate 	 * If ICMD is defined, parse_input will treat the whole cmd_line_input
24617c478bd9Sstevel@tonic-gate 	 * as the target host and set the num_gw to 0. Therefore, none of the
24627c478bd9Sstevel@tonic-gate 	 * source routing related code paths will be executed.
24637c478bd9Sstevel@tonic-gate 	 */
24647c478bd9Sstevel@tonic-gate #ifndef ICMD
24657c478bd9Sstevel@tonic-gate 	if (*cp == '@') {
24667c478bd9Sstevel@tonic-gate 		*src_rtng_type = IPOPT_LSRR;
24677c478bd9Sstevel@tonic-gate 		cp++;
24687c478bd9Sstevel@tonic-gate 	} else if (*cp == '!') {
24697c478bd9Sstevel@tonic-gate 		*src_rtng_type = IPOPT_SSRR;
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate 		/* "!" must be followed by '@' */
24727c478bd9Sstevel@tonic-gate 		if (*(cp + 1) != '@')
24737c478bd9Sstevel@tonic-gate 			goto parse_error;
24747c478bd9Sstevel@tonic-gate 		cp += 2;
24757c478bd9Sstevel@tonic-gate 	} else {
24767c478bd9Sstevel@tonic-gate #endif	/* ICMD */
24777c478bd9Sstevel@tonic-gate 		/* no gateways, just the target */
24787c478bd9Sstevel@tonic-gate 		hostname_list[0] = strdup(cp);
24797c478bd9Sstevel@tonic-gate 		if (hostname_list[0] == NULL) {
24807c478bd9Sstevel@tonic-gate 			perror("telnet: copying host name");
24817c478bd9Sstevel@tonic-gate 			return (-1);
24827c478bd9Sstevel@tonic-gate 		}
24837c478bd9Sstevel@tonic-gate 		return (0);
24847c478bd9Sstevel@tonic-gate #ifndef ICMD
24857c478bd9Sstevel@tonic-gate 	}
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	while (*cp != '\0') {
24887c478bd9Sstevel@tonic-gate 		/*
24897c478bd9Sstevel@tonic-gate 		 * Identify each gateway separated by ",", "@" or ",@" and
24907c478bd9Sstevel@tonic-gate 		 * store in hname[].
24917c478bd9Sstevel@tonic-gate 		 */
24927c478bd9Sstevel@tonic-gate 		i = 0;
24937c478bd9Sstevel@tonic-gate 		while (*cp != '@' && *cp != ',' && *cp != '\0') {
24947c478bd9Sstevel@tonic-gate 			hname[i++] = *cp++;
24957c478bd9Sstevel@tonic-gate 			if (i > MAXHOSTNAMELEN)
24967c478bd9Sstevel@tonic-gate 				goto parse_error;
24977c478bd9Sstevel@tonic-gate 		}
24987c478bd9Sstevel@tonic-gate 		hname[i] = '\0';
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 		/*
25017c478bd9Sstevel@tonic-gate 		 * Two consecutive delimiters which result in a 0 length hname
25027c478bd9Sstevel@tonic-gate 		 * is a parse error.
25037c478bd9Sstevel@tonic-gate 		 */
25047c478bd9Sstevel@tonic-gate 		if (i == 0)
25057c478bd9Sstevel@tonic-gate 			goto parse_error;
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 		hostname_list[gw_count] = strdup(hname);
25087c478bd9Sstevel@tonic-gate 		if (hostname_list[gw_count] == NULL) {
25097c478bd9Sstevel@tonic-gate 			perror("telnet: copying hostname from list");
25107c478bd9Sstevel@tonic-gate 			return (-1);
25117c478bd9Sstevel@tonic-gate 		}
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate 		if (++gw_count > MAXMAX_GATEWAY) {
25147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "telnet: too many gateways\n");
25157c478bd9Sstevel@tonic-gate 			return (-1);
25167c478bd9Sstevel@tonic-gate 		}
25177c478bd9Sstevel@tonic-gate 
25187c478bd9Sstevel@tonic-gate 		/* Jump over the next delimiter. */
25197c478bd9Sstevel@tonic-gate 		if (*cp != '\0') {
25207c478bd9Sstevel@tonic-gate 			/* ...gw1,@gw2... accepted */
25217c478bd9Sstevel@tonic-gate 			if (*cp == ',' && *(cp + 1) == '@')
25227c478bd9Sstevel@tonic-gate 				cp += 2;
25237c478bd9Sstevel@tonic-gate 			else
25247c478bd9Sstevel@tonic-gate 				cp++;
25257c478bd9Sstevel@tonic-gate 		}
25267c478bd9Sstevel@tonic-gate 	}
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	/* discount the target */
25297c478bd9Sstevel@tonic-gate 	gw_count--;
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 	/* Any input starting with '!@' or '@' must have at least one gateway */
25327c478bd9Sstevel@tonic-gate 	if (gw_count <= 0)
25337c478bd9Sstevel@tonic-gate 		goto parse_error;
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate 	return (gw_count);
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate parse_error:
25387c478bd9Sstevel@tonic-gate 	(void) printf("Bad source route option: %s\n", cmd_line_input);
25397c478bd9Sstevel@tonic-gate 	return (-1);
25407c478bd9Sstevel@tonic-gate #endif	/* ICMD */
25417c478bd9Sstevel@tonic-gate }
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate /*
25447c478bd9Sstevel@tonic-gate  * Resolves the target and gateway addresses, determines what type of addresses
25457c478bd9Sstevel@tonic-gate  * (ALL_ADDRS, ONLY_V6, ONLY_V4) telnet will be trying to connect.
25467c478bd9Sstevel@tonic-gate  *
25477c478bd9Sstevel@tonic-gate  * Returns:	pointer to resolved target	if name resolutions succeed
25487c478bd9Sstevel@tonic-gate  *		NULL				if name resolutions fail or
25497c478bd9Sstevel@tonic-gate  *						a library function call fails
25507c478bd9Sstevel@tonic-gate  *
25517c478bd9Sstevel@tonic-gate  * The last host in the hostname_list is the target. After resolving the target,
25527c478bd9Sstevel@tonic-gate  * determines for what type of addresses it should try to resolve gateways. It
25537c478bd9Sstevel@tonic-gate  * resolves gateway addresses and picks one address for each desired address
25547c478bd9Sstevel@tonic-gate  * type and stores in the array pointed by gw_addrsp. Also, this 'type of
25557c478bd9Sstevel@tonic-gate  * addresses' is pointed by addr_type argument on successful return.
25567c478bd9Sstevel@tonic-gate  */
25577c478bd9Sstevel@tonic-gate static struct addrinfo *
resolve_hosts(char ** hostname_list,int num_gw,struct gateway ** gw_addrsp,int * addr_type,const char * portp)25587c478bd9Sstevel@tonic-gate resolve_hosts(char **hostname_list, int num_gw, struct gateway **gw_addrsp,
25597c478bd9Sstevel@tonic-gate     int *addr_type, const char *portp)
25607c478bd9Sstevel@tonic-gate {
25617c478bd9Sstevel@tonic-gate 	struct gateway *gw_addrs = NULL;
25627c478bd9Sstevel@tonic-gate 	struct gateway *gw;
25637c478bd9Sstevel@tonic-gate 	/* whether we already picked an IPv4 address for the current gateway */
25647c478bd9Sstevel@tonic-gate 	boolean_t got_v4_addr;
25657c478bd9Sstevel@tonic-gate 	boolean_t got_v6_addr;
25667c478bd9Sstevel@tonic-gate 	/* whether we need to get an IPv4 address for the current gateway */
25677c478bd9Sstevel@tonic-gate 	boolean_t need_v4_addr = B_FALSE;
25687c478bd9Sstevel@tonic-gate 	boolean_t need_v6_addr = B_FALSE;
25697c478bd9Sstevel@tonic-gate 	int res_failed_at4;	/* save which gateway failed to resolve */
25707c478bd9Sstevel@tonic-gate 	int res_failed_at6;
25717c478bd9Sstevel@tonic-gate 	boolean_t is_v4mapped;
25727c478bd9Sstevel@tonic-gate 	struct in6_addr *v6addrp;
25737c478bd9Sstevel@tonic-gate 	struct in_addr *v4addrp;
25747c478bd9Sstevel@tonic-gate 	int error_num;
25757c478bd9Sstevel@tonic-gate 	int i;
25767c478bd9Sstevel@tonic-gate 	int rc;
25777c478bd9Sstevel@tonic-gate 	struct addrinfo *res, *host, *gateway, *addr;
25787c478bd9Sstevel@tonic-gate 	struct addrinfo hints;
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	*addr_type = ALL_ADDRS;
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	memset(&hints, 0, sizeof (hints));
25837c478bd9Sstevel@tonic-gate 	hints.ai_flags = AI_CANONNAME; /* used for config files, diags */
25847c478bd9Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
25857c478bd9Sstevel@tonic-gate 	rc = getaddrinfo(hostname_list[num_gw],
25867c478bd9Sstevel@tonic-gate 	    (portp != NULL) ? portp : "telnet", &hints, &res);
25877c478bd9Sstevel@tonic-gate 	if (rc != 0) {
25887c478bd9Sstevel@tonic-gate 		if (hostname_list[num_gw] != NULL &&
25897c478bd9Sstevel@tonic-gate 		    *hostname_list[num_gw] != '\0')
25907c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", hostname_list[num_gw]);
25917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", gai_strerror(rc));
25927c478bd9Sstevel@tonic-gate 		return (NULL);
25937c478bd9Sstevel@tonic-gate 	}
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	/*
25967c478bd9Sstevel@tonic-gate 	 * Let's see what type of addresses we got for the target. This
25977c478bd9Sstevel@tonic-gate 	 * determines what type of addresses we'd like to resolve gateways
25987c478bd9Sstevel@tonic-gate 	 * later.
25997c478bd9Sstevel@tonic-gate 	 */
26007c478bd9Sstevel@tonic-gate 	for (host = res; host != NULL; host = host->ai_next) {
26017c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *s6;
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 		s6 = (struct sockaddr_in6 *)host->ai_addr;
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 		if (host->ai_addr->sa_family == AF_INET ||
26067c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr))
26077c478bd9Sstevel@tonic-gate 			need_v4_addr = B_TRUE;
26087c478bd9Sstevel@tonic-gate 		else
26097c478bd9Sstevel@tonic-gate 			need_v6_addr = B_TRUE;
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 		/*
26127c478bd9Sstevel@tonic-gate 		 * Let's stop after seeing we need both IPv6 and IPv4.
26137c478bd9Sstevel@tonic-gate 		 */
26147c478bd9Sstevel@tonic-gate 		if (need_v4_addr && need_v6_addr)
26157c478bd9Sstevel@tonic-gate 			break;
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	if (num_gw > 0) {
26197c478bd9Sstevel@tonic-gate 		/*
26207c478bd9Sstevel@tonic-gate 		 * In the prepare_optbuf(), we'll store the IPv4 address of the
26217c478bd9Sstevel@tonic-gate 		 * target in the last slot of gw_addrs array. Therefore we need
26227c478bd9Sstevel@tonic-gate 		 * space for num_gw+1 hosts.
26237c478bd9Sstevel@tonic-gate 		 */
26247c478bd9Sstevel@tonic-gate 		gw_addrs = calloc(num_gw + 1, sizeof (struct gateway));
26257c478bd9Sstevel@tonic-gate 		if (gw_addrs == NULL) {
26267c478bd9Sstevel@tonic-gate 			perror("telnet: calloc");
26277c478bd9Sstevel@tonic-gate 			freeaddrinfo(res);
26287c478bd9Sstevel@tonic-gate 			return (NULL);
26297c478bd9Sstevel@tonic-gate 		}
26307c478bd9Sstevel@tonic-gate 	}
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	/*
26337c478bd9Sstevel@tonic-gate 	 * Now we'll go through all the gateways and try to resolve them to
26347c478bd9Sstevel@tonic-gate 	 * the desired address types.
26357c478bd9Sstevel@tonic-gate 	 */
26367c478bd9Sstevel@tonic-gate 	gw = gw_addrs;
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	/* -1 means 'no address resolution failure yet' */
26397c478bd9Sstevel@tonic-gate 	res_failed_at4 = -1;
26407c478bd9Sstevel@tonic-gate 	res_failed_at6 = -1;
26417c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_gw; i++) {
26427c478bd9Sstevel@tonic-gate 		rc = getaddrinfo(hostname_list[i], NULL, NULL, &gateway);
26437c478bd9Sstevel@tonic-gate 		if (rc != 0) {
26447c478bd9Sstevel@tonic-gate 			if (hostname_list[i] != NULL &&
26457c478bd9Sstevel@tonic-gate 			    *hostname_list[i] != '\0')
26467c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: ",
26477c478bd9Sstevel@tonic-gate 				    hostname_list[i]);
26487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "bad address\n");
26497c478bd9Sstevel@tonic-gate 			return (NULL);
26507c478bd9Sstevel@tonic-gate 		}
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 		/*
26537c478bd9Sstevel@tonic-gate 		 * Initially we have no address of any type for this gateway.
26547c478bd9Sstevel@tonic-gate 		 */
26557c478bd9Sstevel@tonic-gate 		got_v6_addr = B_FALSE;
26567c478bd9Sstevel@tonic-gate 		got_v4_addr = B_FALSE;
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 		/*
26597c478bd9Sstevel@tonic-gate 		 * Let's go through all the addresses of this gateway.
26607c478bd9Sstevel@tonic-gate 		 * Use the first address which matches the needed family.
26617c478bd9Sstevel@tonic-gate 		 */
26627c478bd9Sstevel@tonic-gate 		for (addr = gateway; addr != NULL; addr = addr->ai_next) {
26637c478bd9Sstevel@tonic-gate 			/*LINTED*/
26647c478bd9Sstevel@tonic-gate 			v6addrp = &((struct sockaddr_in6 *)addr->ai_addr)->
26657c478bd9Sstevel@tonic-gate 			    sin6_addr;
26667c478bd9Sstevel@tonic-gate 			v4addrp = &((struct sockaddr_in *)addr->ai_addr)->
26677c478bd9Sstevel@tonic-gate 			    sin_addr;
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 			if (addr->ai_family == AF_INET6)
26707c478bd9Sstevel@tonic-gate 				is_v4mapped = IN6_IS_ADDR_V4MAPPED(v6addrp);
26717c478bd9Sstevel@tonic-gate 			else
26727c478bd9Sstevel@tonic-gate 				is_v4mapped = B_FALSE;
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 			/*
26757c478bd9Sstevel@tonic-gate 			 * If we need to determine an IPv4 address and haven't
26767c478bd9Sstevel@tonic-gate 			 * found one yet and this is a IPv4-mapped IPv6 address,
26777c478bd9Sstevel@tonic-gate 			 * then bingo!
26787c478bd9Sstevel@tonic-gate 			 */
26797c478bd9Sstevel@tonic-gate 			if (need_v4_addr && !got_v4_addr) {
26807c478bd9Sstevel@tonic-gate 				if (is_v4mapped) {
26817c478bd9Sstevel@tonic-gate 					IN6_V4MAPPED_TO_INADDR(v6addrp,
26827c478bd9Sstevel@tonic-gate 					    &gw->gw_addr);
26837c478bd9Sstevel@tonic-gate 					got_v4_addr = B_TRUE;
26847c478bd9Sstevel@tonic-gate 				} else if (addr->ai_family = AF_INET) {
26857c478bd9Sstevel@tonic-gate 					gw->gw_addr = *v4addrp;
26867c478bd9Sstevel@tonic-gate 					got_v4_addr = B_TRUE;
26877c478bd9Sstevel@tonic-gate 				}
26887c478bd9Sstevel@tonic-gate 			}
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 			if (need_v6_addr && !got_v6_addr &&
26917c478bd9Sstevel@tonic-gate 			    addr->ai_family == AF_INET6) {
26927c478bd9Sstevel@tonic-gate 				gw->gw_addr6 = *v6addrp;
26937c478bd9Sstevel@tonic-gate 				got_v6_addr = B_TRUE;
26947c478bd9Sstevel@tonic-gate 			}
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 			/*
26977c478bd9Sstevel@tonic-gate 			 * Let's stop if we got all what we looked for.
26987c478bd9Sstevel@tonic-gate 			 */
26997c478bd9Sstevel@tonic-gate 			if ((!need_v4_addr || got_v4_addr) &&
27007c478bd9Sstevel@tonic-gate 			    (!need_v6_addr || got_v6_addr))
27017c478bd9Sstevel@tonic-gate 				break;
27027c478bd9Sstevel@tonic-gate 		}
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 		/*
27057c478bd9Sstevel@tonic-gate 		 * We needed an IPv4 address for this gateway but couldn't
27067c478bd9Sstevel@tonic-gate 		 * find one.
27077c478bd9Sstevel@tonic-gate 		 */
27087c478bd9Sstevel@tonic-gate 		if (need_v4_addr && !got_v4_addr) {
27097c478bd9Sstevel@tonic-gate 			res_failed_at4 = i;
27107c478bd9Sstevel@tonic-gate 			/*
27117c478bd9Sstevel@tonic-gate 			 * Since we couldn't resolve a gateway to IPv4 address
27127c478bd9Sstevel@tonic-gate 			 * we can't use IPv4 at all. Therefore we no longer
27137c478bd9Sstevel@tonic-gate 			 * need IPv4 addresses for any of the gateways.
27147c478bd9Sstevel@tonic-gate 			 */
27157c478bd9Sstevel@tonic-gate 			need_v4_addr = B_FALSE;
27167c478bd9Sstevel@tonic-gate 		}
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 		if (need_v6_addr && !got_v6_addr) {
27197c478bd9Sstevel@tonic-gate 			res_failed_at6 = i;
27207c478bd9Sstevel@tonic-gate 			need_v6_addr = B_FALSE;
27217c478bd9Sstevel@tonic-gate 		}
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 		/*
27247c478bd9Sstevel@tonic-gate 		 * If some gateways don't resolve to any of the desired
27257c478bd9Sstevel@tonic-gate 		 * address types, we fail.
27267c478bd9Sstevel@tonic-gate 		 */
27277c478bd9Sstevel@tonic-gate 		if (!need_v4_addr && !need_v6_addr) {
27287c478bd9Sstevel@tonic-gate 			if (res_failed_at6 != -1) {
27297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
27307c478bd9Sstevel@tonic-gate 				    "%s: Host doesn't have any IPv6 address\n",
27317c478bd9Sstevel@tonic-gate 				    hostname_list[res_failed_at6]);
27327c478bd9Sstevel@tonic-gate 			}
27337c478bd9Sstevel@tonic-gate 			if (res_failed_at4 != -1) {
27347c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
27357c478bd9Sstevel@tonic-gate 				    "%s: Host doesn't have any IPv4 address\n",
27367c478bd9Sstevel@tonic-gate 				    hostname_list[res_failed_at4]);
27377c478bd9Sstevel@tonic-gate 			}
27387c478bd9Sstevel@tonic-gate 			free(gw_addrs);
27397c478bd9Sstevel@tonic-gate 			return (NULL);
27407c478bd9Sstevel@tonic-gate 		}
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 		gw++;
27437c478bd9Sstevel@tonic-gate 	}
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 	*gw_addrsp = gw_addrs;
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	/*
27487c478bd9Sstevel@tonic-gate 	 * When we get here, need_v4_addr and need_v6_addr have their final
27497c478bd9Sstevel@tonic-gate 	 * values based on the name resolution of the target and gateways.
27507c478bd9Sstevel@tonic-gate 	 */
27517c478bd9Sstevel@tonic-gate 	if (need_v4_addr && need_v6_addr)
27527c478bd9Sstevel@tonic-gate 		*addr_type = ALL_ADDRS;
27537c478bd9Sstevel@tonic-gate 	else if (need_v4_addr && !need_v6_addr)
27547c478bd9Sstevel@tonic-gate 		*addr_type = ONLY_V4;
27557c478bd9Sstevel@tonic-gate 	else if (!need_v4_addr && need_v6_addr)
27567c478bd9Sstevel@tonic-gate 		*addr_type = ONLY_V6;
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	return (res);
27597c478bd9Sstevel@tonic-gate }
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate /*
27637c478bd9Sstevel@tonic-gate  * Initializes the buffer pointed by opt_bufpp for a IPv4 option of type
27647c478bd9Sstevel@tonic-gate  * src_rtng_type using the gateway addresses stored in gw_addrs. If no buffer
27657c478bd9Sstevel@tonic-gate  * is passed, it allocates one. If a buffer is passed, checks if it's big
27667c478bd9Sstevel@tonic-gate  * enough.
27677c478bd9Sstevel@tonic-gate  * On return opt_buf_len points to the buffer length which we need later for the
27687c478bd9Sstevel@tonic-gate  * setsockopt() call, and opt_bufpp points to the newly allocated or already
27697c478bd9Sstevel@tonic-gate  * passed buffer. Returns B_FALSE if a library function call fails or passed
27707c478bd9Sstevel@tonic-gate  * buffer is not big enough, B_TRUE otherwise.
27717c478bd9Sstevel@tonic-gate  */
27727c478bd9Sstevel@tonic-gate static boolean_t
prepare_optbuf(struct gateway * gw_addrs,int num_gw,char ** opt_bufpp,size_t * opt_buf_len,struct in_addr * target,uchar_t src_rtng_type)27737c478bd9Sstevel@tonic-gate prepare_optbuf(struct gateway *gw_addrs, int num_gw, char **opt_bufpp,
27747c478bd9Sstevel@tonic-gate     size_t *opt_buf_len, struct in_addr *target, uchar_t src_rtng_type)
27757c478bd9Sstevel@tonic-gate {
27767c478bd9Sstevel@tonic-gate 	struct ip_sourceroute *sr_opt;
27777c478bd9Sstevel@tonic-gate 	size_t needed_buflen;
27787c478bd9Sstevel@tonic-gate 	int i;
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	/*
27817c478bd9Sstevel@tonic-gate 	 * We have (num_gw + 1) IP addresses in the buffer because the number
27827c478bd9Sstevel@tonic-gate 	 * of gateway addresses we put in the option buffer includes the target
27837c478bd9Sstevel@tonic-gate 	 * address.
27847c478bd9Sstevel@tonic-gate 	 * At the time of setsockopt() call, passed option length needs to be
27857c478bd9Sstevel@tonic-gate 	 * multiple of 4 bytes. Therefore we need one IPOPT_NOP before (or
27867c478bd9Sstevel@tonic-gate 	 * after) IPOPT_LSRR.
27877c478bd9Sstevel@tonic-gate 	 * 1 = preceding 1 byte of IPOPT_NOP
27887c478bd9Sstevel@tonic-gate 	 * 3 = 1 (code) + 1 (len) + 1 (ptr)
27897c478bd9Sstevel@tonic-gate 	 */
27907c478bd9Sstevel@tonic-gate 	needed_buflen = 1 + 3 + (num_gw + 1) * sizeof (struct in_addr);
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	if (*opt_bufpp != NULL) {
27937c478bd9Sstevel@tonic-gate 		/* check if the passed buffer is big enough */
27947c478bd9Sstevel@tonic-gate 		if (*opt_buf_len < needed_buflen) {
27957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
27967c478bd9Sstevel@tonic-gate 			    "telnet: buffer too small for IPv4 source routing "
27977c478bd9Sstevel@tonic-gate 			    "option\n");
27987c478bd9Sstevel@tonic-gate 			return (B_FALSE);
27997c478bd9Sstevel@tonic-gate 		}
28007c478bd9Sstevel@tonic-gate 	} else {
28017c478bd9Sstevel@tonic-gate 		*opt_bufpp = malloc(needed_buflen);
28027c478bd9Sstevel@tonic-gate 		if (*opt_bufpp == NULL) {
28037c478bd9Sstevel@tonic-gate 			perror("telnet: malloc");
28047c478bd9Sstevel@tonic-gate 			return (B_FALSE);
28057c478bd9Sstevel@tonic-gate 		}
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	*opt_buf_len = needed_buflen;
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 	/* final hop is the target */
28117c478bd9Sstevel@tonic-gate 	gw_addrs[num_gw].gw_addr = *target;
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	*opt_bufpp[0] = IPOPT_NOP;
28147c478bd9Sstevel@tonic-gate 	/* IPOPT_LSRR starts right after IPOPT_NOP */
28157c478bd9Sstevel@tonic-gate 	sr_opt = (struct ip_sourceroute *)(*opt_bufpp + 1);
28167c478bd9Sstevel@tonic-gate 	sr_opt->ipsr_code = src_rtng_type;
28177c478bd9Sstevel@tonic-gate 	/* discount the 1 byte of IPOPT_NOP */
28187c478bd9Sstevel@tonic-gate 	sr_opt->ipsr_len = needed_buflen - 1;
28197c478bd9Sstevel@tonic-gate 	sr_opt->ipsr_ptr = IPOPT_MINOFF;
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	/* copy the gateways into the optlist */
28227c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_gw + 1; i++) {
28237c478bd9Sstevel@tonic-gate 		(void) bcopy(&gw_addrs[i].gw_addr, &sr_opt->ipsr_addrs[i],
28247c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
28257c478bd9Sstevel@tonic-gate 	}
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 	return (B_TRUE);
28287c478bd9Sstevel@tonic-gate }
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate /*
28317c478bd9Sstevel@tonic-gate  * Initializes the buffer pointed by opt_bufpp for a IPv6 routing header option
28327c478bd9Sstevel@tonic-gate  * using the gateway addresses stored in gw_addrs. If no buffer is passed, it
28337c478bd9Sstevel@tonic-gate  * allocates one. If a buffer is passed, checks if it's big enough.
28347c478bd9Sstevel@tonic-gate  * On return opt_buf_len points to the buffer length which we need later for the
28357c478bd9Sstevel@tonic-gate  * setsockopt() call, and opt_bufpp points to the newly allocated or already
28367c478bd9Sstevel@tonic-gate  * passed buffer. Returns B_FALSE if a library function call fails or passed
28377c478bd9Sstevel@tonic-gate  * buffer is not big enough, B_TRUE otherwise.
28387c478bd9Sstevel@tonic-gate  */
28397c478bd9Sstevel@tonic-gate static boolean_t
prepare_optbuf6(struct gateway * gw_addrs,int num_gw,char ** opt_bufpp,size_t * opt_buf_len)28407c478bd9Sstevel@tonic-gate prepare_optbuf6(struct gateway *gw_addrs, int num_gw, char **opt_bufpp,
28417c478bd9Sstevel@tonic-gate     size_t *opt_buf_len)
28427c478bd9Sstevel@tonic-gate {
28437c478bd9Sstevel@tonic-gate 	char *opt_bufp;
28447c478bd9Sstevel@tonic-gate 	size_t needed_buflen;
28457c478bd9Sstevel@tonic-gate 	int i;
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	needed_buflen = inet6_rth_space(IPV6_RTHDR_TYPE_0, num_gw);
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 	if (*opt_bufpp != NULL) {
28507c478bd9Sstevel@tonic-gate 		/* check if the passed buffer is big enough */
28517c478bd9Sstevel@tonic-gate 		if (*opt_buf_len < needed_buflen) {
28527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
28537c478bd9Sstevel@tonic-gate 			    "telnet: buffer too small for IPv6 routing "
28547c478bd9Sstevel@tonic-gate 			    "header option\n");
28557c478bd9Sstevel@tonic-gate 			return (B_FALSE);
28567c478bd9Sstevel@tonic-gate 		}
28577c478bd9Sstevel@tonic-gate 	} else {
28587c478bd9Sstevel@tonic-gate 		*opt_bufpp = malloc(needed_buflen);
28597c478bd9Sstevel@tonic-gate 		if (*opt_bufpp == NULL) {
28607c478bd9Sstevel@tonic-gate 			perror("telnet: malloc");
28617c478bd9Sstevel@tonic-gate 			return (B_FALSE);
28627c478bd9Sstevel@tonic-gate 		}
28637c478bd9Sstevel@tonic-gate 	}
28647c478bd9Sstevel@tonic-gate 	*opt_buf_len = needed_buflen;
28657c478bd9Sstevel@tonic-gate 	opt_bufp = *opt_bufpp;
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	/*
28687c478bd9Sstevel@tonic-gate 	 * Initialize the buffer to be used for IPv6 routing header type 0.
28697c478bd9Sstevel@tonic-gate 	 */
28707c478bd9Sstevel@tonic-gate 	if (inet6_rth_init(opt_bufp, needed_buflen, IPV6_RTHDR_TYPE_0,
28717c478bd9Sstevel@tonic-gate 	    num_gw) == NULL) {
28727c478bd9Sstevel@tonic-gate 		perror("telnet: inet6_rth_init");
28737c478bd9Sstevel@tonic-gate 		return (B_FALSE);
28747c478bd9Sstevel@tonic-gate 	}
28757c478bd9Sstevel@tonic-gate 
28767c478bd9Sstevel@tonic-gate 	/*
28777c478bd9Sstevel@tonic-gate 	 * Add gateways one by one.
28787c478bd9Sstevel@tonic-gate 	 */
28797c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_gw; i++) {
28807c478bd9Sstevel@tonic-gate 		if (inet6_rth_add(opt_bufp, &gw_addrs[i].gw_addr6) == -1) {
28817c478bd9Sstevel@tonic-gate 			perror("telnet: inet6_rth_add");
28827c478bd9Sstevel@tonic-gate 			return (B_FALSE);
28837c478bd9Sstevel@tonic-gate 		}
28847c478bd9Sstevel@tonic-gate 	}
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate 	/* successful operation */
28877c478bd9Sstevel@tonic-gate 	return (B_TRUE);
28887c478bd9Sstevel@tonic-gate }
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate int
tn(int argc,char * argv[])2891*905213bbSToomas Soome tn(int argc, char *argv[])
28927c478bd9Sstevel@tonic-gate {
28937c478bd9Sstevel@tonic-gate 	struct addrinfo *host = NULL;
28947c478bd9Sstevel@tonic-gate 	struct addrinfo *h;
28957c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 sin6;
28967c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin;
28977c478bd9Sstevel@tonic-gate 	struct in6_addr addr6;
28987c478bd9Sstevel@tonic-gate 	struct in_addr addr;
28997c478bd9Sstevel@tonic-gate 	void *addrp;
29007c478bd9Sstevel@tonic-gate 	struct gateway *gw_addrs;
29017c478bd9Sstevel@tonic-gate 	char *hostname_list[MAXMAX_GATEWAY + 1] = {NULL};
29027c478bd9Sstevel@tonic-gate 	char *opt_buf6 = NULL;		/* used for IPv6 routing header */
29037c478bd9Sstevel@tonic-gate 	size_t opt_buf_len6 = 0;
29047c478bd9Sstevel@tonic-gate 	uchar_t src_rtng_type;		/* type of IPv4 source routing */
29057c478bd9Sstevel@tonic-gate 	struct servent *sp = 0;
29067c478bd9Sstevel@tonic-gate 	char *opt_buf = NULL;		/* used for IPv4 source routing */
29077c478bd9Sstevel@tonic-gate 	size_t opt_buf_len = 0;
29087c478bd9Sstevel@tonic-gate 	char *cmd;
29097c478bd9Sstevel@tonic-gate 	char *hostp = NULL;
29107c478bd9Sstevel@tonic-gate 	char *portp = NULL;
29117c478bd9Sstevel@tonic-gate 	char *user = NULL;
29127c478bd9Sstevel@tonic-gate #ifdef	ICMD
29137c478bd9Sstevel@tonic-gate 	char *itelnet_host;
29147c478bd9Sstevel@tonic-gate 	char *real_host;
29157c478bd9Sstevel@tonic-gate 	unsigned short dest_port;
29167c478bd9Sstevel@tonic-gate #endif	/* ICMD */
29177c478bd9Sstevel@tonic-gate 	/*
29187c478bd9Sstevel@tonic-gate 	 * The two strings at the end of this function are 24 and 39
29197c478bd9Sstevel@tonic-gate 	 * characters long (minus the %.*s in the format strings).  Add
29207c478bd9Sstevel@tonic-gate 	 * one for the null terminator making the longest print string 40.
29217c478bd9Sstevel@tonic-gate 	 */
29227c478bd9Sstevel@tonic-gate 	char buf[MAXHOSTNAMELEN+40];
29237c478bd9Sstevel@tonic-gate 	/*
29247c478bd9Sstevel@tonic-gate 	 * In the case of ICMD defined, dest_port will contain the real port
29257c478bd9Sstevel@tonic-gate 	 * we are trying to telnet to, and target_port will contain
29267c478bd9Sstevel@tonic-gate 	 * "telnet-passthru" port.
29277c478bd9Sstevel@tonic-gate 	 */
29287c478bd9Sstevel@tonic-gate 	unsigned short target_port;
29297c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
29307c478bd9Sstevel@tonic-gate 	int num_gw;
29317c478bd9Sstevel@tonic-gate 	int ret_val;
29327c478bd9Sstevel@tonic-gate 	boolean_t is_v4mapped;
29337c478bd9Sstevel@tonic-gate 	/*
29347c478bd9Sstevel@tonic-gate 	 * Type of addresses we'll try to connect to (ALL_ADDRS, ONLY_V6,
29357c478bd9Sstevel@tonic-gate 	 * ONLY_V4).
29367c478bd9Sstevel@tonic-gate 	 */
29377c478bd9Sstevel@tonic-gate 	int addr_type;
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate 	/* clear the socket address prior to use */
29407c478bd9Sstevel@tonic-gate 	(void) memset(&sin6, '\0', sizeof (sin6));
29417c478bd9Sstevel@tonic-gate 	sin6.sin6_family = AF_INET6;
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate 	(void) memset(&sin, '\0', sizeof (sin));
29447c478bd9Sstevel@tonic-gate 	sin.sin_family = AF_INET;
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	if (connected) {
29477c478bd9Sstevel@tonic-gate 		(void) printf("?Already connected to %s\n", hostname);
29487c478bd9Sstevel@tonic-gate 		return (0);
29497c478bd9Sstevel@tonic-gate 	}
29507c478bd9Sstevel@tonic-gate #ifdef	ICMD
29517c478bd9Sstevel@tonic-gate 	itelnet_host = getenv("INTERNET_HOST");
29527c478bd9Sstevel@tonic-gate 	if (itelnet_host == NULL || itelnet_host[0] == '\0') {
29537c478bd9Sstevel@tonic-gate 		(void) printf("INTERNET_HOST environment variable undefined\n");
29547c478bd9Sstevel@tonic-gate 		goto tn_exit;
29557c478bd9Sstevel@tonic-gate 	}
29567c478bd9Sstevel@tonic-gate #endif
29577c478bd9Sstevel@tonic-gate 	if (argc < 2) {
29587c478bd9Sstevel@tonic-gate 		(void) printf("(to) ");
29597c478bd9Sstevel@tonic-gate 		if (GetAndAppendString(&line, &linesize, "open ",
29607c478bd9Sstevel@tonic-gate 			stdin) == NULL) {
29617c478bd9Sstevel@tonic-gate 			if (!feof(stdin)) {
29627c478bd9Sstevel@tonic-gate 				perror("telnet");
29637c478bd9Sstevel@tonic-gate 				goto tn_exit;
29647c478bd9Sstevel@tonic-gate 			}
29657c478bd9Sstevel@tonic-gate 		}
29667c478bd9Sstevel@tonic-gate 		makeargv();
29677c478bd9Sstevel@tonic-gate 		argc = margc;
29687c478bd9Sstevel@tonic-gate 		argv = margv;
29697c478bd9Sstevel@tonic-gate 	}
29707c478bd9Sstevel@tonic-gate 	cmd = *argv;
29717c478bd9Sstevel@tonic-gate 	--argc; ++argv;
29727c478bd9Sstevel@tonic-gate 	while (argc) {
29737c478bd9Sstevel@tonic-gate 		if (isprefix(*argv, "help") == 4 || isprefix(*argv, "?") == 1)
29747c478bd9Sstevel@tonic-gate 			goto usage;
29757c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-l") == 0) {
29767c478bd9Sstevel@tonic-gate 			--argc; ++argv;
29777c478bd9Sstevel@tonic-gate 			if (argc == 0)
29787c478bd9Sstevel@tonic-gate 				goto usage;
29797c478bd9Sstevel@tonic-gate 			user = *argv++;
29807c478bd9Sstevel@tonic-gate 			--argc;
29817c478bd9Sstevel@tonic-gate 			continue;
29827c478bd9Sstevel@tonic-gate 		}
29837c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-a") == 0) {
29847c478bd9Sstevel@tonic-gate 			--argc; ++argv;
29857c478bd9Sstevel@tonic-gate 			autologin = autologin_set = 1;
29867c478bd9Sstevel@tonic-gate 			continue;
29877c478bd9Sstevel@tonic-gate 		}
29887c478bd9Sstevel@tonic-gate 		if (hostp == 0) {
29897c478bd9Sstevel@tonic-gate 			hostp = *argv++;
29907c478bd9Sstevel@tonic-gate 			--argc;
29917c478bd9Sstevel@tonic-gate 			continue;
29927c478bd9Sstevel@tonic-gate 		}
29937c478bd9Sstevel@tonic-gate 		if (portp == 0) {
29947c478bd9Sstevel@tonic-gate 			portp = *argv++;
29957c478bd9Sstevel@tonic-gate 			--argc;
29967c478bd9Sstevel@tonic-gate 			/*
29977c478bd9Sstevel@tonic-gate 			 * Do we treat this like a telnet port or raw?
29987c478bd9Sstevel@tonic-gate 			 */
29997c478bd9Sstevel@tonic-gate 			if (*portp == '-') {
30007c478bd9Sstevel@tonic-gate 				portp++;
30017c478bd9Sstevel@tonic-gate 				telnetport = 1;
30027c478bd9Sstevel@tonic-gate 			} else
30037c478bd9Sstevel@tonic-gate 				telnetport = 0;
30047c478bd9Sstevel@tonic-gate 			continue;
30057c478bd9Sstevel@tonic-gate 		}
30067c478bd9Sstevel@tonic-gate usage:
30077c478bd9Sstevel@tonic-gate 		(void) printf(
30087c478bd9Sstevel@tonic-gate 		    "usage: %s [-l user] [-a] host-name [port]\n", cmd);
30097c478bd9Sstevel@tonic-gate 		goto tn_exit;
30107c478bd9Sstevel@tonic-gate 	}
30117c478bd9Sstevel@tonic-gate 	if (hostp == 0)
30127c478bd9Sstevel@tonic-gate 		goto usage;
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate #ifdef ICMD
30157c478bd9Sstevel@tonic-gate 	/*
30167c478bd9Sstevel@tonic-gate 	 * For setup phase treat the relay host as the target host.
30177c478bd9Sstevel@tonic-gate 	 */
30187c478bd9Sstevel@tonic-gate 	real_host = hostp;
30197c478bd9Sstevel@tonic-gate 	hostp = itelnet_host;
30207c478bd9Sstevel@tonic-gate #endif
30217c478bd9Sstevel@tonic-gate 	num_gw = parse_input(hostp, hostname_list, &src_rtng_type);
30227c478bd9Sstevel@tonic-gate 	if (num_gw < 0) {
30237c478bd9Sstevel@tonic-gate 		goto tn_exit;
30247c478bd9Sstevel@tonic-gate 	}
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 	/* Last host in the hostname_list is the target */
30277c478bd9Sstevel@tonic-gate 	hostp = hostname_list[num_gw];
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 	host = resolve_hosts(hostname_list, num_gw, &gw_addrs, &addr_type,
30307c478bd9Sstevel@tonic-gate 	    portp);
30317c478bd9Sstevel@tonic-gate 	if (host == NULL) {
30327c478bd9Sstevel@tonic-gate 		goto tn_exit;
30337c478bd9Sstevel@tonic-gate 	}
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 	/*
30367c478bd9Sstevel@tonic-gate 	 * Check if number of gateways is less than max. available
30377c478bd9Sstevel@tonic-gate 	 */
30387c478bd9Sstevel@tonic-gate 	if ((addr_type == ALL_ADDRS || addr_type == ONLY_V6) &&
30397c478bd9Sstevel@tonic-gate 	    num_gw > MAX_GATEWAY6) {
30407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "telnet: too many IPv6 gateways\n");
30417c478bd9Sstevel@tonic-gate 		goto tn_exit;
30427c478bd9Sstevel@tonic-gate 	}
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 	if ((addr_type == ALL_ADDRS || addr_type == ONLY_V4) &&
30457c478bd9Sstevel@tonic-gate 	    num_gw > MAX_GATEWAY) {
30467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "telnet: too many IPv4 gateways\n");
30477c478bd9Sstevel@tonic-gate 		goto tn_exit;
30487c478bd9Sstevel@tonic-gate 	}
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate 	/*
30517c478bd9Sstevel@tonic-gate 	 * If we pass a literal IPv4 address to getaddrinfo(), in the
30527c478bd9Sstevel@tonic-gate 	 * returned addrinfo structure, hostname is the IPv4-mapped IPv6
30537c478bd9Sstevel@tonic-gate 	 * address string. We prefer to preserve the literal IPv4 address
30547c478bd9Sstevel@tonic-gate 	 * string as the hostname.  Also, if the hostname entered by the
30557c478bd9Sstevel@tonic-gate 	 * user is IPv4-mapped IPv6 address, we'll downgrade it to IPv4
30567c478bd9Sstevel@tonic-gate 	 * address.
30577c478bd9Sstevel@tonic-gate 	 */
30587c478bd9Sstevel@tonic-gate 	if (inet_addr(hostp) != (in_addr_t)-1) {
30597c478bd9Sstevel@tonic-gate 		/* this is a literal IPv4 address */
30607c478bd9Sstevel@tonic-gate 		(void) strlcpy(_hostname, hostp, sizeof (_hostname));
30617c478bd9Sstevel@tonic-gate 	} else if ((inet_pton(AF_INET6, hostp, &addr6) > 0) &&
30627c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED(&addr6)) {
30637c478bd9Sstevel@tonic-gate 		/* this is a IPv4-mapped IPv6 address */
30647c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_INADDR(&addr6, &addr);
30657c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &addr, _hostname, sizeof (_hostname));
30667c478bd9Sstevel@tonic-gate 	} else {
30677c478bd9Sstevel@tonic-gate 		(void) strlcpy(_hostname, host->ai_canonname,
30687c478bd9Sstevel@tonic-gate 		    sizeof (_hostname));
30697c478bd9Sstevel@tonic-gate 	}
30707c478bd9Sstevel@tonic-gate 	hostname = _hostname;
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate 	if (portp == NULL) {
30737c478bd9Sstevel@tonic-gate 		telnetport = 1;
30747c478bd9Sstevel@tonic-gate 	}
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	if (host->ai_family == AF_INET) {
30777c478bd9Sstevel@tonic-gate 		target_port = ((struct sockaddr_in *)(host->ai_addr))->sin_port;
30787c478bd9Sstevel@tonic-gate 	} else {
30797c478bd9Sstevel@tonic-gate 		target_port = ((struct sockaddr_in6 *)(host->ai_addr))
30807c478bd9Sstevel@tonic-gate 		    ->sin6_port;
30817c478bd9Sstevel@tonic-gate 	}
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate #ifdef ICMD
30847c478bd9Sstevel@tonic-gate 	/*
30857c478bd9Sstevel@tonic-gate 	 * Since we pass the port number as an ascii string to the proxy,
30867c478bd9Sstevel@tonic-gate 	 *  we need it in host format.
30877c478bd9Sstevel@tonic-gate 	 */
30887c478bd9Sstevel@tonic-gate 	dest_port = ntohs(target_port);
30897c478bd9Sstevel@tonic-gate 	sp = getservbyname("telnet-passthru", "tcp");
30907c478bd9Sstevel@tonic-gate 	if (sp == 0) {
30917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
30927c478bd9Sstevel@tonic-gate 		    "telnet: tcp/telnet-passthru: unknown service\n");
30937c478bd9Sstevel@tonic-gate 			goto tn_exit;
30947c478bd9Sstevel@tonic-gate 	}
30957c478bd9Sstevel@tonic-gate 	target_port = sp->s_port;
30967c478bd9Sstevel@tonic-gate #endif
30977c478bd9Sstevel@tonic-gate 	h = host;
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate 	/*
31007c478bd9Sstevel@tonic-gate 	 * For IPv6 source routing, we need to initialize option buffer only
31017c478bd9Sstevel@tonic-gate 	 * once.
31027c478bd9Sstevel@tonic-gate 	 */
31037c478bd9Sstevel@tonic-gate 	if (num_gw > 0 && (addr_type == ALL_ADDRS || addr_type == ONLY_V6)) {
31047c478bd9Sstevel@tonic-gate 		if (!prepare_optbuf6(gw_addrs, num_gw, &opt_buf6,
31057c478bd9Sstevel@tonic-gate 		    &opt_buf_len6)) {
31067c478bd9Sstevel@tonic-gate 			goto tn_exit;
31077c478bd9Sstevel@tonic-gate 		}
31087c478bd9Sstevel@tonic-gate 	}
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate 	/*
31117c478bd9Sstevel@tonic-gate 	 * We procure the Kerberos config files options only
31127c478bd9Sstevel@tonic-gate 	 * if the user has choosen Krb5 authentication.
31137c478bd9Sstevel@tonic-gate 	 */
31147c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
31157c478bd9Sstevel@tonic-gate 		krb5_profile_get_options(hostname, telnet_krb5_realm,
31167c478bd9Sstevel@tonic-gate 			config_file_options);
31177c478bd9Sstevel@tonic-gate 	}
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 	if (encrypt_flag) {
31207c478bd9Sstevel@tonic-gate 		extern boolean_t auth_enable_encrypt;
31217c478bd9Sstevel@tonic-gate 		if (krb5_privacy_allowed()) {
31227c478bd9Sstevel@tonic-gate 			encrypt_auto(1);
31237c478bd9Sstevel@tonic-gate 			decrypt_auto(1);
31247c478bd9Sstevel@tonic-gate 			wantencryption = B_TRUE;
31257c478bd9Sstevel@tonic-gate 			autologin = 1;
31267c478bd9Sstevel@tonic-gate 			auth_enable_encrypt = B_TRUE;
31277c478bd9Sstevel@tonic-gate 		} else {
31287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
31297c478bd9Sstevel@tonic-gate 			    "%s:Encryption not supported.\n"), prompt);
31307c478bd9Sstevel@tonic-gate 			exit(1);
31317c478bd9Sstevel@tonic-gate 			}
31327c478bd9Sstevel@tonic-gate 	}
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate 	if (forward_flag && forwardable_flag) {
31357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
31367c478bd9Sstevel@tonic-gate 			"Error in krb5 configuration file. "
31377c478bd9Sstevel@tonic-gate 			"Both forward and forwardable are set.\n"));
31387c478bd9Sstevel@tonic-gate 		exit(1);
31397c478bd9Sstevel@tonic-gate 	}
31407c478bd9Sstevel@tonic-gate 	if (forwardable_flag) {
31417c478bd9Sstevel@tonic-gate 		forward_flags |= OPTS_FORWARD_CREDS | OPTS_FORWARDABLE_CREDS;
31427c478bd9Sstevel@tonic-gate 	} else if (forward_flag)
31437c478bd9Sstevel@tonic-gate 		forward_flags |= OPTS_FORWARD_CREDS;
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate 	do {
31477c478bd9Sstevel@tonic-gate 		/*
31487c478bd9Sstevel@tonic-gate 		 * Search for an address of desired type in the IP address list
31497c478bd9Sstevel@tonic-gate 		 * of the target.
31507c478bd9Sstevel@tonic-gate 		 */
31517c478bd9Sstevel@tonic-gate 		while (h != NULL) {
31527c478bd9Sstevel@tonic-gate 			struct sockaddr_in6 *addr;
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 			addr = (struct sockaddr_in6 *)h->ai_addr;
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 			if (h->ai_family == AF_INET6)
31577c478bd9Sstevel@tonic-gate 				is_v4mapped =
31587c478bd9Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr);
31597c478bd9Sstevel@tonic-gate 			else
31607c478bd9Sstevel@tonic-gate 				is_v4mapped = B_FALSE;
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 			if (addr_type == ALL_ADDRS ||
31637c478bd9Sstevel@tonic-gate 			    (addr_type == ONLY_V6 &&
31647c478bd9Sstevel@tonic-gate 				h->ai_family == AF_INET6) ||
31657c478bd9Sstevel@tonic-gate 			    (addr_type == ONLY_V4 &&
31667c478bd9Sstevel@tonic-gate 				(h->ai_family == AF_INET || is_v4mapped)))
31677c478bd9Sstevel@tonic-gate 				break;
31687c478bd9Sstevel@tonic-gate 
31697c478bd9Sstevel@tonic-gate 			/* skip undesired typed addresses */
31707c478bd9Sstevel@tonic-gate 			h = h->ai_next;
31717c478bd9Sstevel@tonic-gate 		}
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 		if (h == NULL) {
31747c478bd9Sstevel@tonic-gate 			fprintf(stderr,
31757c478bd9Sstevel@tonic-gate 			    "telnet: Unable to connect to remote host");
31767c478bd9Sstevel@tonic-gate 			goto tn_exit;
31777c478bd9Sstevel@tonic-gate 		}
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 		/*
31807c478bd9Sstevel@tonic-gate 		 * We need to open a socket with a family matching the type of
31817c478bd9Sstevel@tonic-gate 		 * address we are trying to connect to. This is because we
31827c478bd9Sstevel@tonic-gate 		 * deal with IPv4 options and IPv6 extension headers.
31837c478bd9Sstevel@tonic-gate 		 */
31847c478bd9Sstevel@tonic-gate 		if (h->ai_family == AF_INET) {
31857c478bd9Sstevel@tonic-gate 			addrp = &((struct sockaddr_in *)(h->ai_addr))->sin_addr;
31867c478bd9Sstevel@tonic-gate 			((struct sockaddr_in *)(h->ai_addr))->sin_port =
31877c478bd9Sstevel@tonic-gate 			    target_port;
31887c478bd9Sstevel@tonic-gate 		} else {
31897c478bd9Sstevel@tonic-gate 			addrp = &((struct sockaddr_in6 *)(h->ai_addr))
31907c478bd9Sstevel@tonic-gate 			    ->sin6_addr;
31917c478bd9Sstevel@tonic-gate 			((struct sockaddr_in6 *)(h->ai_addr))->sin6_port =
31927c478bd9Sstevel@tonic-gate 			    target_port;
31937c478bd9Sstevel@tonic-gate 		}
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 		(void) printf("Trying %s...\n", inet_ntop(h->ai_family,
31967c478bd9Sstevel@tonic-gate 		    addrp, abuf, sizeof (abuf)));
31977c478bd9Sstevel@tonic-gate 
31987c478bd9Sstevel@tonic-gate 		net = socket(h->ai_family, SOCK_STREAM, 0);
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate 		if (net < 0) {
32017c478bd9Sstevel@tonic-gate 			perror("telnet: socket");
32027c478bd9Sstevel@tonic-gate 			goto tn_exit;
32037c478bd9Sstevel@tonic-gate 		}
32047c478bd9Sstevel@tonic-gate #ifndef ICMD
32057c478bd9Sstevel@tonic-gate 		if (num_gw > 0) {
32067c478bd9Sstevel@tonic-gate 			if (h->ai_family == AF_INET || is_v4mapped) {
32077c478bd9Sstevel@tonic-gate 				if (!prepare_optbuf(gw_addrs, num_gw, &opt_buf,
32087c478bd9Sstevel@tonic-gate 				    &opt_buf_len, addrp, src_rtng_type)) {
32097c478bd9Sstevel@tonic-gate 					goto tn_exit;
32107c478bd9Sstevel@tonic-gate 				}
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 				if (setsockopt(net, IPPROTO_IP, IP_OPTIONS,
32137c478bd9Sstevel@tonic-gate 				    opt_buf, opt_buf_len) < 0)
32147c478bd9Sstevel@tonic-gate 					perror("setsockopt (IP_OPTIONS)");
32157c478bd9Sstevel@tonic-gate 			} else {
32167c478bd9Sstevel@tonic-gate 				if (setsockopt(net, IPPROTO_IPV6, IPV6_RTHDR,
32177c478bd9Sstevel@tonic-gate 				    opt_buf6, opt_buf_len6) < 0)
32187c478bd9Sstevel@tonic-gate 					perror("setsockopt (IPV6_RTHDR)");
32197c478bd9Sstevel@tonic-gate 			}
32207c478bd9Sstevel@tonic-gate 		}
32217c478bd9Sstevel@tonic-gate #endif
32227c478bd9Sstevel@tonic-gate #if	defined(USE_TOS)
32237c478bd9Sstevel@tonic-gate 		if (is_v4mapped) {
32247c478bd9Sstevel@tonic-gate 			if (tos < 0)
32257c478bd9Sstevel@tonic-gate 				tos = 020;	/* Low Delay bit */
32267c478bd9Sstevel@tonic-gate 			if (tos &&
32277c478bd9Sstevel@tonic-gate 			    (setsockopt(net, IPPROTO_IP, IP_TOS,
32287c478bd9Sstevel@tonic-gate 			    &tos, sizeof (int)) < 0) &&
32297c478bd9Sstevel@tonic-gate 			    (errno != ENOPROTOOPT))
32307c478bd9Sstevel@tonic-gate 				perror("telnet: setsockopt (IP_TOS) (ignored)");
32317c478bd9Sstevel@tonic-gate 		}
32327c478bd9Sstevel@tonic-gate #endif	/* defined(USE_TOS) */
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 		if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
32357c478bd9Sstevel@tonic-gate 			perror("setsockopt (SO_DEBUG)");
32367c478bd9Sstevel@tonic-gate 		}
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate 		ret_val = connect(net, h->ai_addr, h->ai_addrlen);
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 		/*
32417c478bd9Sstevel@tonic-gate 		 * If failed, try the next address of the target.
32427c478bd9Sstevel@tonic-gate 		 */
32437c478bd9Sstevel@tonic-gate 		if (ret_val < 0) {
32447c478bd9Sstevel@tonic-gate 			Close(&net);
32457c478bd9Sstevel@tonic-gate 			if (h->ai_next != NULL) {
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate 				int oerrno = errno;
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
32507c478bd9Sstevel@tonic-gate 				    "telnet: connect to address %s: ", abuf);
32517c478bd9Sstevel@tonic-gate 				errno = oerrno;
32527c478bd9Sstevel@tonic-gate 				perror((char *)0);
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 				h = h->ai_next;
32557c478bd9Sstevel@tonic-gate 				continue;
32567c478bd9Sstevel@tonic-gate 			}
32577c478bd9Sstevel@tonic-gate 			perror("telnet: Unable to connect to remote host");
32587c478bd9Sstevel@tonic-gate 			goto tn_exit;
32597c478bd9Sstevel@tonic-gate 		}
32607c478bd9Sstevel@tonic-gate 		connected++;
32617c478bd9Sstevel@tonic-gate 	} while (connected == 0);
32627c478bd9Sstevel@tonic-gate 	freeaddrinfo(host);
32637c478bd9Sstevel@tonic-gate 	host = NULL;
32647c478bd9Sstevel@tonic-gate #ifdef ICMD
32657c478bd9Sstevel@tonic-gate 	/*
32667c478bd9Sstevel@tonic-gate 	 * Do initial protocol to connect to farther end...
32677c478bd9Sstevel@tonic-gate 	 */
32687c478bd9Sstevel@tonic-gate 	{
32697c478bd9Sstevel@tonic-gate 		char buf[1024];
32707c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%s %d\n", real_host, (int)dest_port);
32717c478bd9Sstevel@tonic-gate 		write(net, buf, strlen(buf));
32727c478bd9Sstevel@tonic-gate 	}
32737c478bd9Sstevel@tonic-gate #endif
32747c478bd9Sstevel@tonic-gate 	if (cmdrc(hostp, hostname) != 0)
32757c478bd9Sstevel@tonic-gate 		goto tn_exit;
32767c478bd9Sstevel@tonic-gate 	FreeHostnameList(hostname_list);
32777c478bd9Sstevel@tonic-gate 	if (autologin && user == NULL) {
32787c478bd9Sstevel@tonic-gate 		struct passwd *pw;
32797c478bd9Sstevel@tonic-gate 
32807c478bd9Sstevel@tonic-gate 		user = getenv("LOGNAME");
32817c478bd9Sstevel@tonic-gate 		if (user == NULL ||
32827c478bd9Sstevel@tonic-gate 		    ((pw = getpwnam(user)) != NULL) &&
32837c478bd9Sstevel@tonic-gate 		    pw->pw_uid != getuid()) {
32847c478bd9Sstevel@tonic-gate 			if (pw = getpwuid(getuid()))
32857c478bd9Sstevel@tonic-gate 				user = pw->pw_name;
32867c478bd9Sstevel@tonic-gate 			else
32877c478bd9Sstevel@tonic-gate 				user = NULL;
32887c478bd9Sstevel@tonic-gate 		}
32897c478bd9Sstevel@tonic-gate 	}
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 	if (user) {
32927c478bd9Sstevel@tonic-gate 		if (env_define((unsigned char *)"USER", (unsigned char *)user))
32937c478bd9Sstevel@tonic-gate 			env_export((unsigned char *)"USER");
32947c478bd9Sstevel@tonic-gate 		else {
32957c478bd9Sstevel@tonic-gate 			/* Clean up and exit. */
32967c478bd9Sstevel@tonic-gate 			Close(&net);
32977c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf),
32987c478bd9Sstevel@tonic-gate 			    "Connection to %.*s closed.\n",
32997c478bd9Sstevel@tonic-gate 			    MAXHOSTNAMELEN, hostname);
33007c478bd9Sstevel@tonic-gate 			ExitString(buf, EXIT_FAILURE);
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
33037c478bd9Sstevel@tonic-gate 		}
33047c478bd9Sstevel@tonic-gate 	}
33057c478bd9Sstevel@tonic-gate 	(void) call(3, status, "status", "notmuch");
33067c478bd9Sstevel@tonic-gate 	if (setjmp(peerdied) == 0)
33077c478bd9Sstevel@tonic-gate 		telnet(user);
33087c478bd9Sstevel@tonic-gate 
33097c478bd9Sstevel@tonic-gate 	Close(&net);
33107c478bd9Sstevel@tonic-gate 
33117c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),
33127c478bd9Sstevel@tonic-gate 	    "Connection to %.*s closed by foreign host.\n",
33137c478bd9Sstevel@tonic-gate 	    MAXHOSTNAMELEN, hostname);
33147c478bd9Sstevel@tonic-gate 	ExitString(buf, EXIT_FAILURE);
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
33177c478bd9Sstevel@tonic-gate 
33187c478bd9Sstevel@tonic-gate tn_exit:
33197c478bd9Sstevel@tonic-gate 	FreeHostnameList(hostname_list);
33207c478bd9Sstevel@tonic-gate 	Close(&net);
33217c478bd9Sstevel@tonic-gate 	connected = 0;
33227c478bd9Sstevel@tonic-gate 	if (host != NULL)
33237c478bd9Sstevel@tonic-gate 		freeaddrinfo(host);
33247c478bd9Sstevel@tonic-gate 	return (0);
33257c478bd9Sstevel@tonic-gate }
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate #define	HELPINDENT (sizeof ("connect"))
33287c478bd9Sstevel@tonic-gate 
33297c478bd9Sstevel@tonic-gate static char openhelp[] = "connect to a site";
33307c478bd9Sstevel@tonic-gate static char closehelp[] = "close current connection";
33317c478bd9Sstevel@tonic-gate static char logouthelp[] =
33327c478bd9Sstevel@tonic-gate 	    "forcibly logout remote user and close the connection";
33337c478bd9Sstevel@tonic-gate static char quithelp[] = "exit telnet";
33347c478bd9Sstevel@tonic-gate static char statushelp[] = "print status information";
33357c478bd9Sstevel@tonic-gate static char helphelp[] = "print help information";
33367c478bd9Sstevel@tonic-gate static char sendhelp[] =
33377c478bd9Sstevel@tonic-gate 	    "transmit special characters ('send ?' for more)";
33387c478bd9Sstevel@tonic-gate static char sethelp[] =  "set operating parameters ('set ?' for more)";
33397c478bd9Sstevel@tonic-gate static char unsethelp[] = "unset operating parameters ('unset ?' for more)";
33407c478bd9Sstevel@tonic-gate static char togglestring[] =
33417c478bd9Sstevel@tonic-gate 	    "toggle operating parameters ('toggle ?' for more)";
33427c478bd9Sstevel@tonic-gate static char slchelp[] = "change state of special charaters ('slc ?' for more)";
33437c478bd9Sstevel@tonic-gate static char displayhelp[] = "display operating parameters";
33447c478bd9Sstevel@tonic-gate static char authhelp[] =
33457c478bd9Sstevel@tonic-gate 	    "turn on (off) authentication ('auth ?' for more)";
33467c478bd9Sstevel@tonic-gate static char forwardhelp[] =
33477c478bd9Sstevel@tonic-gate 	    "turn on (off) credential forwarding ('forward ?' for more)";
33487c478bd9Sstevel@tonic-gate static char encrypthelp[] =
33497c478bd9Sstevel@tonic-gate 	    "turn on (off) encryption ('encrypt ?' for more)";
33507c478bd9Sstevel@tonic-gate static char zhelp[] = "suspend telnet";
33517c478bd9Sstevel@tonic-gate static char shellhelp[] = "invoke a subshell";
33527c478bd9Sstevel@tonic-gate static char envhelp[] = "change environment variables ('environ ?' for more)";
33537c478bd9Sstevel@tonic-gate static char modestring[] =
33547c478bd9Sstevel@tonic-gate 	    "try to enter line or character mode ('mode ?' for more)";
33557c478bd9Sstevel@tonic-gate 
33567c478bd9Sstevel@tonic-gate static int	help();
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate static Command cmdtab[] = {
33597c478bd9Sstevel@tonic-gate 	{ "close",	closehelp,	bye,		1 },
33607c478bd9Sstevel@tonic-gate 	{ "logout",	logouthelp,	logout,		1 },
33617c478bd9Sstevel@tonic-gate 	{ "display",	displayhelp,	display,	0 },
33627c478bd9Sstevel@tonic-gate 	{ "mode",	modestring,	modecmd,	0 },
33637c478bd9Sstevel@tonic-gate 	{ "open",	openhelp,	tn,		0 },
33647c478bd9Sstevel@tonic-gate 	{ "quit",	quithelp,	quit,		0 },
33657c478bd9Sstevel@tonic-gate 	{ "send",	sendhelp,	sendcmd,	0 },
33667c478bd9Sstevel@tonic-gate 	{ "set",	sethelp,	setcmd,		0 },
33677c478bd9Sstevel@tonic-gate 	{ "unset",	unsethelp,	unsetcmd,	0 },
33687c478bd9Sstevel@tonic-gate 	{ "status",	statushelp,	status,		0 },
33697c478bd9Sstevel@tonic-gate 	{ "toggle",	togglestring,	toggle,		0 },
33707c478bd9Sstevel@tonic-gate 	{ "slc",	slchelp,	slccmd,		0 },
33717c478bd9Sstevel@tonic-gate 	{ "auth",	authhelp,	auth_cmd,	0 },
33727c478bd9Sstevel@tonic-gate 	{ "encrypt",	encrypthelp,	encrypt_cmd,	0 },
33737c478bd9Sstevel@tonic-gate 	{ "forward",	forwardhelp,	forw_cmd,	0 },
33747c478bd9Sstevel@tonic-gate 	{ "z",		zhelp,		suspend,	0 },
33757c478bd9Sstevel@tonic-gate 	{ "!",		shellhelp,	shell,		0 },
33767c478bd9Sstevel@tonic-gate 	{ "environ",	envhelp,	env_cmd,	0 },
33777c478bd9Sstevel@tonic-gate 	{ "?",		helphelp,	help,		0 },
33787c478bd9Sstevel@tonic-gate 	0
33797c478bd9Sstevel@tonic-gate };
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate static Command cmdtab2[] = {
33837c478bd9Sstevel@tonic-gate 	{ "help",	0,		help,		0 },
33847c478bd9Sstevel@tonic-gate 	{ "escape",	0,		setescape,	0 },
33857c478bd9Sstevel@tonic-gate 	{ "crmod",	0,		togcrmod,	0 },
33867c478bd9Sstevel@tonic-gate 	0
33877c478bd9Sstevel@tonic-gate };
33887c478bd9Sstevel@tonic-gate 
33897c478bd9Sstevel@tonic-gate 
33907c478bd9Sstevel@tonic-gate /*
33917c478bd9Sstevel@tonic-gate  * Call routine with argc, argv set from args.
33927c478bd9Sstevel@tonic-gate  * Uses /usr/include/stdarg.h
33937c478bd9Sstevel@tonic-gate  */
33947c478bd9Sstevel@tonic-gate #define	MAXVARGS	100
33957c478bd9Sstevel@tonic-gate /*VARARGS1*/
33967c478bd9Sstevel@tonic-gate static void
call(int n_ptrs,...)33977c478bd9Sstevel@tonic-gate call(int n_ptrs, ...)
33987c478bd9Sstevel@tonic-gate {
33997c478bd9Sstevel@tonic-gate 	va_list ap;
34007c478bd9Sstevel@tonic-gate 	typedef int (*intrtn_t)();
34017c478bd9Sstevel@tonic-gate 	intrtn_t routine;
34027c478bd9Sstevel@tonic-gate 	char *args[MAXVARGS+1];	/* leave 1 for trailing NULL */
34037c478bd9Sstevel@tonic-gate 	int argno = 0;
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 	if (n_ptrs > MAXVARGS)
34067c478bd9Sstevel@tonic-gate 		n_ptrs = MAXVARGS;
3407740638c8Sbw 	va_start(ap, n_ptrs);
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 	routine = (va_arg(ap, intrtn_t)); /* extract the routine's name */
34107c478bd9Sstevel@tonic-gate 	n_ptrs--;
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 	while (argno < n_ptrs)	/* extract the routine's args */
34137c478bd9Sstevel@tonic-gate 		args[argno++] = va_arg(ap, char *);
34147c478bd9Sstevel@tonic-gate 	args[argno] = NULL;	/* NULL terminate for good luck */
34157c478bd9Sstevel@tonic-gate 	va_end(ap);
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 	(*routine)(argno, args);
34187c478bd9Sstevel@tonic-gate }
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate static Command *
getcmd(char * name)3422*905213bbSToomas Soome getcmd(char *name)
34237c478bd9Sstevel@tonic-gate {
34247c478bd9Sstevel@tonic-gate 	Command *cm;
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 	if (cm = (Command *) genget(name, (char **)cmdtab, sizeof (Command)))
34277c478bd9Sstevel@tonic-gate 		return (cm);
34287c478bd9Sstevel@tonic-gate 	return (Command *) genget(name, (char **)cmdtab2, sizeof (Command));
34297c478bd9Sstevel@tonic-gate }
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate void
command(int top,char * tbuf,int cnt)3432*905213bbSToomas Soome command(int top, char *tbuf, int cnt)
34337c478bd9Sstevel@tonic-gate {
34347c478bd9Sstevel@tonic-gate 	Command *c;
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	setcommandmode();
34377c478bd9Sstevel@tonic-gate 	if (!top) {
34387c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
34397c478bd9Sstevel@tonic-gate 	} else {
34407c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_DFL);
34417c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, SIG_DFL);
34427c478bd9Sstevel@tonic-gate 	}
34437c478bd9Sstevel@tonic-gate 	for (;;) {
34447c478bd9Sstevel@tonic-gate 		if (rlogin == _POSIX_VDISABLE)
34457c478bd9Sstevel@tonic-gate 			(void) printf("%s> ", prompt);
34467c478bd9Sstevel@tonic-gate 		if (tbuf) {
34477c478bd9Sstevel@tonic-gate 			char *cp;
34487c478bd9Sstevel@tonic-gate 			if (AllocStringBuffer(&line, &linesize, cnt) == NULL)
34497c478bd9Sstevel@tonic-gate 				goto command_exit;
34507c478bd9Sstevel@tonic-gate 			cp = line;
34517c478bd9Sstevel@tonic-gate 			while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
34527c478bd9Sstevel@tonic-gate 				cnt--;
34537c478bd9Sstevel@tonic-gate 			tbuf = 0;
34547c478bd9Sstevel@tonic-gate 			if (cp == line || *--cp != '\n' || cp == line)
34557c478bd9Sstevel@tonic-gate 				goto getline;
34567c478bd9Sstevel@tonic-gate 			*cp = '\0';
34577c478bd9Sstevel@tonic-gate 			if (rlogin == _POSIX_VDISABLE)
34587c478bd9Sstevel@tonic-gate 				(void) printf("%s\n", line);
34597c478bd9Sstevel@tonic-gate 		} else {
34607c478bd9Sstevel@tonic-gate getline:
34617c478bd9Sstevel@tonic-gate 			if (rlogin != _POSIX_VDISABLE)
34627c478bd9Sstevel@tonic-gate 				(void) printf("%s> ", prompt);
34637c478bd9Sstevel@tonic-gate 			if (GetString(&line, &linesize, stdin) == NULL) {
34647c478bd9Sstevel@tonic-gate 				if (!feof(stdin))
34657c478bd9Sstevel@tonic-gate 					perror("telnet");
34667c478bd9Sstevel@tonic-gate 				(void) quit();
34677c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
34687c478bd9Sstevel@tonic-gate 				break;
34697c478bd9Sstevel@tonic-gate 			}
34707c478bd9Sstevel@tonic-gate 		}
34717c478bd9Sstevel@tonic-gate 		if (line[0] == 0)
34727c478bd9Sstevel@tonic-gate 			break;
34737c478bd9Sstevel@tonic-gate 		makeargv();
34747c478bd9Sstevel@tonic-gate 		if (margv[0] == 0) {
34757c478bd9Sstevel@tonic-gate 			break;
34767c478bd9Sstevel@tonic-gate 		}
34777c478bd9Sstevel@tonic-gate 		c = getcmd(margv[0]);
34787c478bd9Sstevel@tonic-gate 		if (Ambiguous(c)) {
34797c478bd9Sstevel@tonic-gate 			(void) printf("?Ambiguous command\n");
34807c478bd9Sstevel@tonic-gate 			continue;
34817c478bd9Sstevel@tonic-gate 		}
34827c478bd9Sstevel@tonic-gate 		if (c == 0) {
34837c478bd9Sstevel@tonic-gate 			(void) printf("?Invalid command\n");
34847c478bd9Sstevel@tonic-gate 			continue;
34857c478bd9Sstevel@tonic-gate 		}
34867c478bd9Sstevel@tonic-gate 		if (c->needconnect && !connected) {
34877c478bd9Sstevel@tonic-gate 			(void) printf("?Need to be connected first.\n");
34887c478bd9Sstevel@tonic-gate 			continue;
34897c478bd9Sstevel@tonic-gate 		}
34907c478bd9Sstevel@tonic-gate 		if ((*c->handler)(margc, margv)) {
34917c478bd9Sstevel@tonic-gate 			break;
34927c478bd9Sstevel@tonic-gate 		}
34937c478bd9Sstevel@tonic-gate 	}
34947c478bd9Sstevel@tonic-gate command_exit:
34957c478bd9Sstevel@tonic-gate 	if (!top) {
34967c478bd9Sstevel@tonic-gate 		if (!connected) {
34977c478bd9Sstevel@tonic-gate 			longjmp(toplevel, 1);
34987c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
34997c478bd9Sstevel@tonic-gate 		}
35007c478bd9Sstevel@tonic-gate 		setconnmode(0);
35017c478bd9Sstevel@tonic-gate 	}
35027c478bd9Sstevel@tonic-gate }
35037c478bd9Sstevel@tonic-gate 
35047c478bd9Sstevel@tonic-gate /*
35057c478bd9Sstevel@tonic-gate  * Help command.
35067c478bd9Sstevel@tonic-gate  */
3507740638c8Sbw static int
help(int argc,char * argv[])3508*905213bbSToomas Soome help(int argc, char *argv[])
35097c478bd9Sstevel@tonic-gate {
3510*905213bbSToomas Soome 	Command *c;
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 	if (argc == 1) {
35137c478bd9Sstevel@tonic-gate 		(void) printf(
35147c478bd9Sstevel@tonic-gate 		    "Commands may be abbreviated.  Commands are:\n\n");
35157c478bd9Sstevel@tonic-gate 		for (c = cmdtab; c->name; c++)
35167c478bd9Sstevel@tonic-gate 			if (c->help) {
35177c478bd9Sstevel@tonic-gate 				(void) printf("%-*s\t%s\n", HELPINDENT,
35187c478bd9Sstevel@tonic-gate 					c->name, c->help);
35197c478bd9Sstevel@tonic-gate 			}
35207c478bd9Sstevel@tonic-gate 		(void) printf("<return>\tleave command mode\n");
35217c478bd9Sstevel@tonic-gate 		return (0);
35227c478bd9Sstevel@tonic-gate 	}
35237c478bd9Sstevel@tonic-gate 	while (--argc > 0) {
3524*905213bbSToomas Soome 		char *arg;
35257c478bd9Sstevel@tonic-gate 		arg = *++argv;
35267c478bd9Sstevel@tonic-gate 		c = getcmd(arg);
35277c478bd9Sstevel@tonic-gate 		if (Ambiguous(c))
35287c478bd9Sstevel@tonic-gate 			(void) printf("?Ambiguous help command %s\n", arg);
35297c478bd9Sstevel@tonic-gate 		else if (c == (Command *)0)
35307c478bd9Sstevel@tonic-gate 			(void) printf("?Invalid help command %s\n", arg);
35317c478bd9Sstevel@tonic-gate 		else if (c->help) {
35327c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", c->help);
35337c478bd9Sstevel@tonic-gate 		} else  {
35347c478bd9Sstevel@tonic-gate 			(void) printf("No additional help on %s\n", arg);
35357c478bd9Sstevel@tonic-gate 		}
35367c478bd9Sstevel@tonic-gate 	}
35377c478bd9Sstevel@tonic-gate 	return (0);
35387c478bd9Sstevel@tonic-gate }
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate static char *rcname = NULL;
35417c478bd9Sstevel@tonic-gate #define	TELNETRC_NAME "telnetrc"
35427c478bd9Sstevel@tonic-gate #define	TELNETRC_COMP "/." TELNETRC_NAME
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate static int
cmdrc(char * m1,char * m2)35457c478bd9Sstevel@tonic-gate cmdrc(char *m1, char *m2)
35467c478bd9Sstevel@tonic-gate {
35477c478bd9Sstevel@tonic-gate 	Command *c;
35487c478bd9Sstevel@tonic-gate 	FILE *rcfile = NULL;
35497c478bd9Sstevel@tonic-gate 	int gotmachine = 0;
35507c478bd9Sstevel@tonic-gate 	int l1 = strlen(m1);
35517c478bd9Sstevel@tonic-gate 	int l2 = strlen(m2);
35527c478bd9Sstevel@tonic-gate 	char m1save[MAXHOSTNAMELEN];
35537c478bd9Sstevel@tonic-gate 	int ret = 0;
35547c478bd9Sstevel@tonic-gate 	char def[] = "DEFAULT";
35557c478bd9Sstevel@tonic-gate 
35567c478bd9Sstevel@tonic-gate 	if (skiprc)
35577c478bd9Sstevel@tonic-gate 		goto cmdrc_exit;
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate 	doing_rc = 1;
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate 	(void) strlcpy(m1save, m1, sizeof (m1save));
35627c478bd9Sstevel@tonic-gate 	m1 = m1save;
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 	if (rcname == NULL) {
35657c478bd9Sstevel@tonic-gate 		char *homedir;
35667c478bd9Sstevel@tonic-gate 		unsigned rcbuflen;
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 		if ((homedir = getenv("HOME")) == NULL)
35697c478bd9Sstevel@tonic-gate 			homedir = "";
35707c478bd9Sstevel@tonic-gate 
35717c478bd9Sstevel@tonic-gate 		rcbuflen = strlen(homedir) + strlen(TELNETRC_COMP) + 1;
35727c478bd9Sstevel@tonic-gate 		if ((rcname = malloc(rcbuflen)) == NULL) {
35737c478bd9Sstevel@tonic-gate 			perror("telnet: can't process " TELNETRC_NAME);
35747c478bd9Sstevel@tonic-gate 			ret = 1;
35757c478bd9Sstevel@tonic-gate 			goto cmdrc_exit;
35767c478bd9Sstevel@tonic-gate 		}
35777c478bd9Sstevel@tonic-gate 		(void) strcpy(rcname, homedir);
35787c478bd9Sstevel@tonic-gate 		(void) strcat(rcname, TELNETRC_COMP);
35797c478bd9Sstevel@tonic-gate 	}
35807c478bd9Sstevel@tonic-gate 
35817c478bd9Sstevel@tonic-gate 	if ((rcfile = fopen(rcname, "r")) == NULL)
35827c478bd9Sstevel@tonic-gate 		goto cmdrc_exit;
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 	for (;;) {
35857c478bd9Sstevel@tonic-gate 		if (GetString(&line, &linesize, rcfile) == NULL) {
35867c478bd9Sstevel@tonic-gate 			if (!feof(rcfile)) {
35877c478bd9Sstevel@tonic-gate 				perror("telnet: error reading " TELNETRC_NAME);
35887c478bd9Sstevel@tonic-gate 				ret = 1;
35897c478bd9Sstevel@tonic-gate 				goto cmdrc_exit;
35907c478bd9Sstevel@tonic-gate 			}
35917c478bd9Sstevel@tonic-gate 			break;
35927c478bd9Sstevel@tonic-gate 		}
35937c478bd9Sstevel@tonic-gate 		if (line[0] == 0)
35947c478bd9Sstevel@tonic-gate 			continue;
35957c478bd9Sstevel@tonic-gate 		if (line[0] == '#')
35967c478bd9Sstevel@tonic-gate 			continue;
35977c478bd9Sstevel@tonic-gate 		if (gotmachine) {
35987c478bd9Sstevel@tonic-gate 			if (!isspace(line[0]))
35997c478bd9Sstevel@tonic-gate 			gotmachine = 0;
36007c478bd9Sstevel@tonic-gate 		}
36017c478bd9Sstevel@tonic-gate 		if (gotmachine == 0) {
36027c478bd9Sstevel@tonic-gate 			if (isspace(line[0]))
36037c478bd9Sstevel@tonic-gate 				continue;
36047c478bd9Sstevel@tonic-gate 			if (strncasecmp(line, m1, l1) == 0)
36057c478bd9Sstevel@tonic-gate 				(void) strcpy(line, &line[l1]);
36067c478bd9Sstevel@tonic-gate 			else if (strncasecmp(line, m2, l2) == 0)
36077c478bd9Sstevel@tonic-gate 				(void) strcpy(line, &line[l2]);
36087c478bd9Sstevel@tonic-gate 			else if (strncasecmp(line, def, sizeof (def) - 1) == 0)
36097c478bd9Sstevel@tonic-gate 				(void) strcpy(line, &line[sizeof (def) - 1]);
36107c478bd9Sstevel@tonic-gate 			else
36117c478bd9Sstevel@tonic-gate 				continue;
36127c478bd9Sstevel@tonic-gate 			if (line[0] != ' ' && line[0] != '\t' &&
36137c478bd9Sstevel@tonic-gate 			    line[0] != '\n')
36147c478bd9Sstevel@tonic-gate 				continue;
36157c478bd9Sstevel@tonic-gate 			gotmachine = 1;
36167c478bd9Sstevel@tonic-gate 		}
36177c478bd9Sstevel@tonic-gate 		makeargv();
36187c478bd9Sstevel@tonic-gate 		if (margv[0] == 0)
36197c478bd9Sstevel@tonic-gate 			continue;
36207c478bd9Sstevel@tonic-gate 		c = getcmd(margv[0]);
36217c478bd9Sstevel@tonic-gate 		if (Ambiguous(c)) {
36227c478bd9Sstevel@tonic-gate 			(void) printf("?Ambiguous command: %s\n", margv[0]);
36237c478bd9Sstevel@tonic-gate 			continue;
36247c478bd9Sstevel@tonic-gate 		}
36257c478bd9Sstevel@tonic-gate 		if (c == 0) {
36267c478bd9Sstevel@tonic-gate 			(void) printf("?Invalid command: %s\n", margv[0]);
36277c478bd9Sstevel@tonic-gate 			continue;
36287c478bd9Sstevel@tonic-gate 		}
36297c478bd9Sstevel@tonic-gate 		/*
36307c478bd9Sstevel@tonic-gate 		 * This should never happen...
36317c478bd9Sstevel@tonic-gate 		 */
36327c478bd9Sstevel@tonic-gate 		if (c->needconnect && !connected) {
36337c478bd9Sstevel@tonic-gate 			(void) printf("?Need to be connected first for %s.\n",
36347c478bd9Sstevel@tonic-gate 			    margv[0]);
36357c478bd9Sstevel@tonic-gate 			continue;
36367c478bd9Sstevel@tonic-gate 		}
36377c478bd9Sstevel@tonic-gate 		(*c->handler)(margc, margv);
36387c478bd9Sstevel@tonic-gate 	}
36397c478bd9Sstevel@tonic-gate cmdrc_exit:
36407c478bd9Sstevel@tonic-gate 	if (rcfile != NULL)
36417c478bd9Sstevel@tonic-gate 		(void) fclose(rcfile);
36427c478bd9Sstevel@tonic-gate 	doing_rc = 0;
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate 	return (ret);
36457c478bd9Sstevel@tonic-gate }
3646