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