xref: /titanic_52/usr/src/cmd/cmd-inet/usr.bin/rlogin.c (revision 740638c8fa5a1c03618a9c75c3161dba57259a17)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*740638c8Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * rlogin - remote login
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/param.h>
477c478bd9Sstevel@tonic-gate #include <sys/errno.h>
487c478bd9Sstevel@tonic-gate #include <sys/file.h>
497c478bd9Sstevel@tonic-gate #include <sys/socket.h>
507c478bd9Sstevel@tonic-gate #include <sys/wait.h>
517c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
527c478bd9Sstevel@tonic-gate #include <sys/ttold.h>
537c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
547c478bd9Sstevel@tonic-gate #include <sys/tty.h>
557c478bd9Sstevel@tonic-gate #include <sys/ptyvar.h>
567c478bd9Sstevel@tonic-gate #include <sys/resource.h>
577c478bd9Sstevel@tonic-gate #include <sys/select.h>
587c478bd9Sstevel@tonic-gate #include <sys/time.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <netinet/in.h>
617c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
627c478bd9Sstevel@tonic-gate #include <priv_utils.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <stdio.h>
657c478bd9Sstevel@tonic-gate #include <errno.h>
667c478bd9Sstevel@tonic-gate #include <pwd.h>
677c478bd9Sstevel@tonic-gate #include <signal.h>
687c478bd9Sstevel@tonic-gate #include <setjmp.h>
697c478bd9Sstevel@tonic-gate #include <netdb.h>
707c478bd9Sstevel@tonic-gate #include <fcntl.h>
717c478bd9Sstevel@tonic-gate #include <locale.h>
727c478bd9Sstevel@tonic-gate #include <stdarg.h>
737c478bd9Sstevel@tonic-gate #include <stdlib.h>
747c478bd9Sstevel@tonic-gate #include <string.h>
757c478bd9Sstevel@tonic-gate #include <unistd.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #include <k5-int.h>
787c478bd9Sstevel@tonic-gate #include <profile/prof_int.h>
797c478bd9Sstevel@tonic-gate #include <com_err.h>
807c478bd9Sstevel@tonic-gate #include <kcmd.h>
817c478bd9Sstevel@tonic-gate #include <krb5.h>
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /* signal disposition - signal handler or SIG_IGN, SIG_ERR, etc. */
847c478bd9Sstevel@tonic-gate typedef void (*sigdisp_t)(int);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate extern errcode_t	profile_get_options_boolean(profile_t, char **,
877c478bd9Sstevel@tonic-gate     profile_options_boolean *);
887c478bd9Sstevel@tonic-gate extern errcode_t	profile_get_options_string(profile_t, char **,
897c478bd9Sstevel@tonic-gate     profile_option_strings *);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	RLOGIN_BUFSIZ	(1024 * 50)
927c478bd9Sstevel@tonic-gate static char des_inbuf[2 * RLOGIN_BUFSIZ];
937c478bd9Sstevel@tonic-gate 					/* needs to be > largest read size */
947c478bd9Sstevel@tonic-gate static char des_outbuf[2 * RLOGIN_BUFSIZ];
957c478bd9Sstevel@tonic-gate 					/* needs to be > largest write size */
967c478bd9Sstevel@tonic-gate static krb5_data desinbuf, desoutbuf;
977c478bd9Sstevel@tonic-gate static krb5_encrypt_block eblock;	/* eblock for encrypt/decrypt */
987c478bd9Sstevel@tonic-gate static krb5_keyblock *session_key;
997c478bd9Sstevel@tonic-gate static krb5_creds *cred;
1007c478bd9Sstevel@tonic-gate static krb5_context bsd_context;
1017c478bd9Sstevel@tonic-gate static krb5_auth_context auth_context;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static char *krb_realm;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static	int krb5auth_flag;	/* Flag set, when KERBEROS is enabled */
1067c478bd9Sstevel@tonic-gate static	int fflag, Fflag;	/* Flag set, when option -f / -F used */
1077c478bd9Sstevel@tonic-gate static	int encrypt_flag;	/* Flag set, when the "-x" option is used */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /* Flag set, if -PN / -PO is specified */
1107c478bd9Sstevel@tonic-gate static boolean_t rcmdoption_done;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /* Flags set, if corres. cmd line options are turned on */
1137c478bd9Sstevel@tonic-gate static boolean_t encrypt_done, fwd_done, fwdable_done;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static profile_options_boolean option[] = {
1167c478bd9Sstevel@tonic-gate 	{ "encrypt", &encrypt_flag, 0 },
1177c478bd9Sstevel@tonic-gate 	{ "forward", &fflag, 0 },
1187c478bd9Sstevel@tonic-gate 	{ "forwardable", &Fflag, 0 },
1197c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static char *rcmdproto;
1237c478bd9Sstevel@tonic-gate static profile_option_strings rcmdversion[] = {
1247c478bd9Sstevel@tonic-gate 	{ "rcmd_protocol", &rcmdproto, 0 },
1257c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1267c478bd9Sstevel@tonic-gate };
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static char rlogin[] = "rlogin";
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static char *realmdef[] = { "realms", NULL, rlogin, NULL };
1317c478bd9Sstevel@tonic-gate static char *appdef[] = { "appdefaults", rlogin, NULL };
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #ifndef TIOCPKT_WINDOW
1347c478bd9Sstevel@tonic-gate #define	TIOCPKT_WINDOW 0x80
1357c478bd9Sstevel@tonic-gate #endif /* TIOCPKT_WINDOW */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #ifndef sigmask
1387c478bd9Sstevel@tonic-gate #define	sigmask(m)	(1 << ((m)-1))
1397c478bd9Sstevel@tonic-gate #endif
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate #define	set2mask(setp)	((setp)->__sigbits[0])
1427c478bd9Sstevel@tonic-gate #define	mask2set(mask, setp) \
1437c478bd9Sstevel@tonic-gate 	((mask) == -1 ? sigfillset(setp) : (((setp)->__sigbits[0]) = (mask)))
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #ifdef DEBUG
1467c478bd9Sstevel@tonic-gate #define	DEBUGOPTSTRING	"D:"
1477c478bd9Sstevel@tonic-gate #else
1487c478bd9Sstevel@tonic-gate #define	DEBUGOPTSTRING	""
1497c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static	boolean_t ttcompat;
1527c478bd9Sstevel@tonic-gate static	struct termios savetty;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static	char *errmsg(int);
1557c478bd9Sstevel@tonic-gate static	char *host;
1567c478bd9Sstevel@tonic-gate static	int port_number;
1577c478bd9Sstevel@tonic-gate static	int rem = -1;
1587c478bd9Sstevel@tonic-gate static	char cmdchar = '~';
1597c478bd9Sstevel@tonic-gate static	boolean_t nocmdchar;
1607c478bd9Sstevel@tonic-gate static	boolean_t eight;
1617c478bd9Sstevel@tonic-gate static	boolean_t litout;
1627c478bd9Sstevel@tonic-gate static	boolean_t null_local_username;
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * Note that this list of speeds is shorter than the list of speeds
1657c478bd9Sstevel@tonic-gate  * supported by termios.  This is because we can't be sure other rlogind's
1667c478bd9Sstevel@tonic-gate  * in the world will correctly cope with values other than what 4.2/4.3BSD
1677c478bd9Sstevel@tonic-gate  * supported.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate static	char *speeds[] =
1707c478bd9Sstevel@tonic-gate 	{ "0", "50", "75", "110", "134", "150", "200", "300",
1717c478bd9Sstevel@tonic-gate 	    "600", "1200", "1800", "2400", "4800", "9600", "19200",
1727c478bd9Sstevel@tonic-gate 	    "38400" };
1737c478bd9Sstevel@tonic-gate static	char term[256] = "network";
1747c478bd9Sstevel@tonic-gate static	void lostpeer(void);
1757c478bd9Sstevel@tonic-gate static	boolean_t dosigwinch;
1767c478bd9Sstevel@tonic-gate static	struct winsize winsize;
1777c478bd9Sstevel@tonic-gate static	void sigwinch(int);
1787c478bd9Sstevel@tonic-gate static	void oob(void);
1797c478bd9Sstevel@tonic-gate static	void doit(int);
1807c478bd9Sstevel@tonic-gate static	sigdisp_t sigdisp(int);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #define	CRLF "\r\n"
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static	pid_t child;
1857c478bd9Sstevel@tonic-gate static	void catchild(int);
1867c478bd9Sstevel@tonic-gate /* LINTED */
1877c478bd9Sstevel@tonic-gate static	void copytochild(int);
1887c478bd9Sstevel@tonic-gate static	void writeroob(int);
1897c478bd9Sstevel@tonic-gate static	void stop(char), echo(char);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static	int defflags, tabflag;
1927c478bd9Sstevel@tonic-gate static	int deflflags;
1937c478bd9Sstevel@tonic-gate static	char deferase, defkill;
1947c478bd9Sstevel@tonic-gate static	struct tchars deftc;
1957c478bd9Sstevel@tonic-gate static	struct ltchars defltc;
1967c478bd9Sstevel@tonic-gate static	struct tchars notc = { (char)-1, (char)-1, (char)-1,
1977c478bd9Sstevel@tonic-gate 				(char)-1, (char)-1, (char)-1 };
1987c478bd9Sstevel@tonic-gate static	struct ltchars noltc =	{ (char)-1, (char)-1, (char)-1,
1997c478bd9Sstevel@tonic-gate 				(char)-1, (char)-1, (char)-1 };
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static	void done(int);
2027c478bd9Sstevel@tonic-gate static	void mode(int);
2037c478bd9Sstevel@tonic-gate static	int reader(int);
2047c478bd9Sstevel@tonic-gate static	void writer(void);
2057c478bd9Sstevel@tonic-gate static	void prf(const char *, ...);
2067c478bd9Sstevel@tonic-gate static	void sendwindow(void);
2077c478bd9Sstevel@tonic-gate static	int compat_ioctl(int, int, void *);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate static void
2107c478bd9Sstevel@tonic-gate sigsetmask(int mask)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	sigset_t oset;
2137c478bd9Sstevel@tonic-gate 	sigset_t nset;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, NULL, &nset);
2167c478bd9Sstevel@tonic-gate 	mask2set(mask, &nset);
2177c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate static int
2217c478bd9Sstevel@tonic-gate sigblock(int mask)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	sigset_t oset;
2247c478bd9Sstevel@tonic-gate 	sigset_t nset;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, NULL, &nset);
2277c478bd9Sstevel@tonic-gate 	mask2set(mask, &nset);
2287c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
2297c478bd9Sstevel@tonic-gate 	return (set2mask(&oset));
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static void
2337c478bd9Sstevel@tonic-gate pop(int status) {
2347c478bd9Sstevel@tonic-gate 	if (ttcompat) {
2357c478bd9Sstevel@tonic-gate 		/*
2367c478bd9Sstevel@tonic-gate 		 * Pop ttcompat module
2377c478bd9Sstevel@tonic-gate 		 */
2387c478bd9Sstevel@tonic-gate 		(void) ioctl(STDIN_FILENO, I_POP, 0);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	(void) tcsetattr(STDIN_FILENO, TCSANOW, &savetty);
2417c478bd9Sstevel@tonic-gate 	exit(status);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static void
2457c478bd9Sstevel@tonic-gate usage(void) {
2467c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n%s\n",
2477c478bd9Sstevel@tonic-gate 	    gettext("usage: rlogin [-option] [-option...] "
2487c478bd9Sstevel@tonic-gate 		"[-k realm] [-l username] host"),
2497c478bd9Sstevel@tonic-gate 	    gettext("       where option is e, 8, E, L, A, a, x, "
2507c478bd9Sstevel@tonic-gate 		"PN / PO, f or F"));
2517c478bd9Sstevel@tonic-gate 	pop(EXIT_FAILURE);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /* PRINTFLIKE(0) */
2557c478bd9Sstevel@tonic-gate static void
2567c478bd9Sstevel@tonic-gate die(const char *format, ...)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	va_list	ap;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	va_start(ap, format);
2617c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
2627c478bd9Sstevel@tonic-gate 	va_end(ap);
2637c478bd9Sstevel@tonic-gate 	usage();
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate static void
2677c478bd9Sstevel@tonic-gate usage_forward(void)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	die(gettext("rlogin: Only one of -f and -F allowed.\n"));
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate int
2737c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	int c;
2767c478bd9Sstevel@tonic-gate 	char *cp, *cmd, *name = NULL;
2777c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
2787c478bd9Sstevel@tonic-gate 	uid_t uid;
2797c478bd9Sstevel@tonic-gate 	int options = 0, oldmask;
2807c478bd9Sstevel@tonic-gate 	int on = 1;
2817c478bd9Sstevel@tonic-gate 	speed_t speed = 0;
2827c478bd9Sstevel@tonic-gate 	int getattr_ret;
2837c478bd9Sstevel@tonic-gate 	char *tmp;
2847c478bd9Sstevel@tonic-gate 	int sock;
2857c478bd9Sstevel@tonic-gate 	krb5_flags authopts;
2867c478bd9Sstevel@tonic-gate 	krb5_error_code status;
2877c478bd9Sstevel@tonic-gate 	enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2927c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
2937c478bd9Sstevel@tonic-gate #endif
2947c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (__init_suid_priv(0, PRIV_NET_PRIVADDR, NULL) == -1) {
2977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2987c478bd9Sstevel@tonic-gate 		    gettext("Insufficient privileges, "
2997c478bd9Sstevel@tonic-gate 			"rlogin must be set-uid root\n"));
3007c478bd9Sstevel@tonic-gate 		exit(1);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	{
3047c478bd9Sstevel@tonic-gate 		int it;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		if ((getattr_ret = tcgetattr(STDIN_FILENO, &savetty)) < 0)
3077c478bd9Sstevel@tonic-gate 			perror("tcgetattr");
3087c478bd9Sstevel@tonic-gate 		it = ioctl(STDIN_FILENO, I_FIND, "ttcompat");
3097c478bd9Sstevel@tonic-gate 		if (it < 0) {
3107c478bd9Sstevel@tonic-gate 			perror("ioctl I_FIND ttcompat");
3117c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 		if (it == 0) {
3147c478bd9Sstevel@tonic-gate 			if (ioctl(STDIN_FILENO, I_PUSH, "ttcompat") < 0) {
3157c478bd9Sstevel@tonic-gate 				perror("ioctl I_PUSH ttcompat");
3167c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
3177c478bd9Sstevel@tonic-gate 			}
3187c478bd9Sstevel@tonic-gate 			ttcompat = B_TRUE;
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * Determine command name used to invoke to rlogin(1). Users can
3247c478bd9Sstevel@tonic-gate 	 * create links named by a host pointing to the binary and type
3257c478bd9Sstevel@tonic-gate 	 * "hostname" to log into that host afterwards.
3267c478bd9Sstevel@tonic-gate 	 */
3277c478bd9Sstevel@tonic-gate 	cmd = strrchr(argv[0], '/');
3287c478bd9Sstevel@tonic-gate 	cmd = (cmd != NULL) ? (cmd + 1) : argv[0];
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (strcmp(cmd, rlogin) == 0) {
3317c478bd9Sstevel@tonic-gate 		if (argc < 2)
3327c478bd9Sstevel@tonic-gate 			usage();
3337c478bd9Sstevel@tonic-gate 		if (*argv[1] != '-') {
3347c478bd9Sstevel@tonic-gate 			host = argv[1];
3357c478bd9Sstevel@tonic-gate 			argc--;
3367c478bd9Sstevel@tonic-gate 			argv[1] = argv[0];
3377c478bd9Sstevel@tonic-gate 			argv++;
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 	} else {
3407c478bd9Sstevel@tonic-gate 		host = cmd;
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv,
3447c478bd9Sstevel@tonic-gate 	    DEBUGOPTSTRING "8AEFLP:ade:fk:l:x")) != -1) {
3457c478bd9Sstevel@tonic-gate 		switch (c) {
3467c478bd9Sstevel@tonic-gate 		case '8':
3477c478bd9Sstevel@tonic-gate 			eight = B_TRUE;
3487c478bd9Sstevel@tonic-gate 			break;
3497c478bd9Sstevel@tonic-gate 		case 'A':
3507c478bd9Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
3517c478bd9Sstevel@tonic-gate 			break;
3527c478bd9Sstevel@tonic-gate #ifdef DEBUG
3537c478bd9Sstevel@tonic-gate 		case 'D':
3547c478bd9Sstevel@tonic-gate 			portnumber = htons(atoi(optarg));
3557c478bd9Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
3567c478bd9Sstevel@tonic-gate 			break;
3577c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3587c478bd9Sstevel@tonic-gate 		case 'E':
3597c478bd9Sstevel@tonic-gate 			nocmdchar = B_TRUE;
3607c478bd9Sstevel@tonic-gate 			break;
3617c478bd9Sstevel@tonic-gate 		case 'F':
3627c478bd9Sstevel@tonic-gate 			if (fflag)
3637c478bd9Sstevel@tonic-gate 				usage_forward();
3647c478bd9Sstevel@tonic-gate 			Fflag = 1;
3657c478bd9Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
3667c478bd9Sstevel@tonic-gate 			fwdable_done = B_TRUE;
3677c478bd9Sstevel@tonic-gate 			break;
3687c478bd9Sstevel@tonic-gate 		case 'f':
3697c478bd9Sstevel@tonic-gate 			if (Fflag)
3707c478bd9Sstevel@tonic-gate 				usage_forward();
3717c478bd9Sstevel@tonic-gate 			fflag = 1;
3727c478bd9Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
3737c478bd9Sstevel@tonic-gate 			fwd_done = B_TRUE;
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 		case 'L':
3767c478bd9Sstevel@tonic-gate 			litout = B_TRUE;
3777c478bd9Sstevel@tonic-gate 			break;
3787c478bd9Sstevel@tonic-gate 		case 'P':
3797c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "N") == 0)
3807c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
3817c478bd9Sstevel@tonic-gate 			else if (strcmp(optarg, "O") == 0)
3827c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
3837c478bd9Sstevel@tonic-gate 			else
3847c478bd9Sstevel@tonic-gate 				die(gettext("rlogin: Only -PN or -PO "
3857c478bd9Sstevel@tonic-gate 				    "allowed.\n"));
3867c478bd9Sstevel@tonic-gate 			if (rcmdoption_done)
3877c478bd9Sstevel@tonic-gate 				die(gettext("rlogin: Only one of -PN and -PO "
3887c478bd9Sstevel@tonic-gate 				    "allowed.\n"));
3897c478bd9Sstevel@tonic-gate 			rcmdoption_done = B_TRUE;
3907c478bd9Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
3917c478bd9Sstevel@tonic-gate 			break;
3927c478bd9Sstevel@tonic-gate 		case 'a':
3937c478bd9Sstevel@tonic-gate 		/*
3947c478bd9Sstevel@tonic-gate 		 * Force the remote host to prompt for a password by sending
3957c478bd9Sstevel@tonic-gate 		 * a NULL username. This option is mutually exclusive with
3967c478bd9Sstevel@tonic-gate 		 * the -A, -x, -f, -F, -k <realm> options.
3977c478bd9Sstevel@tonic-gate 		 */
3987c478bd9Sstevel@tonic-gate 			null_local_username = B_TRUE;
3997c478bd9Sstevel@tonic-gate 			break;
4007c478bd9Sstevel@tonic-gate 		case 'd':
4017c478bd9Sstevel@tonic-gate 			options |= SO_DEBUG;
4027c478bd9Sstevel@tonic-gate 			break;
4037c478bd9Sstevel@tonic-gate 		case 'e': {
4047c478bd9Sstevel@tonic-gate 			int c;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 			cp = optarg;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 			if ((c = *cp) != '\\') {
4097c478bd9Sstevel@tonic-gate 				cmdchar = c;
4107c478bd9Sstevel@tonic-gate 			} else {
4117c478bd9Sstevel@tonic-gate 				c = cp[1];
4127c478bd9Sstevel@tonic-gate 				if (c == '\0' || c == '\\') {
4137c478bd9Sstevel@tonic-gate 					cmdchar = '\\';
4147c478bd9Sstevel@tonic-gate 				} else if (c >= '0' && c <= '7') {
4157c478bd9Sstevel@tonic-gate 					long lc;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 					lc = strtol(&cp[1], NULL, 8);
4187c478bd9Sstevel@tonic-gate 					if (lc < 0 || lc > 255)
4197c478bd9Sstevel@tonic-gate 						die(gettext("rlogin: octal "
4207c478bd9Sstevel@tonic-gate 						    "escape character %s too "
4217c478bd9Sstevel@tonic-gate 						    "large.\n"), cp);
4227c478bd9Sstevel@tonic-gate 					cmdchar = (char)lc;
4237c478bd9Sstevel@tonic-gate 				} else {
4247c478bd9Sstevel@tonic-gate 					die(gettext("rlogin: unrecognized "
4257c478bd9Sstevel@tonic-gate 					    "escape character option %s.\n"),
4267c478bd9Sstevel@tonic-gate 					    cp);
4277c478bd9Sstevel@tonic-gate 				}
4287c478bd9Sstevel@tonic-gate 			}
4297c478bd9Sstevel@tonic-gate 			break;
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 		case 'k':
4327c478bd9Sstevel@tonic-gate 			krb_realm = optarg;
4337c478bd9Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
4347c478bd9Sstevel@tonic-gate 			break;
4357c478bd9Sstevel@tonic-gate 		case 'l':
4367c478bd9Sstevel@tonic-gate 			name = optarg;
4377c478bd9Sstevel@tonic-gate 			break;
4387c478bd9Sstevel@tonic-gate 		case 'x':
4397c478bd9Sstevel@tonic-gate 			encrypt_flag = 1;
4407c478bd9Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
4417c478bd9Sstevel@tonic-gate 			encrypt_done = B_TRUE;
4427c478bd9Sstevel@tonic-gate 			break;
4437c478bd9Sstevel@tonic-gate 		default:
4447c478bd9Sstevel@tonic-gate 			usage();
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	argc -= optind;
4497c478bd9Sstevel@tonic-gate 	argv += optind;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (host == NULL) {
4527c478bd9Sstevel@tonic-gate 		if (argc == 0)
4537c478bd9Sstevel@tonic-gate 			usage();
4547c478bd9Sstevel@tonic-gate 		argc--;
4557c478bd9Sstevel@tonic-gate 		host = *argv++;
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (argc > 0)
4597c478bd9Sstevel@tonic-gate 		usage();
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	pwd = getpwuid(uid = getuid());
4627c478bd9Sstevel@tonic-gate 	if (pwd == NULL) {
4637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("getpwuid(): can not find "
4647c478bd9Sstevel@tonic-gate 			"password entry for user id %d."), uid);
4657c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 	if (name == NULL)
4687c478bd9Sstevel@tonic-gate 		name = pwd->pw_name;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * If the `-a' option is issued on the cmd line, we reset all
4727c478bd9Sstevel@tonic-gate 	 * flags associated with other KRB5 specific options, since
4737c478bd9Sstevel@tonic-gate 	 * the -a option is mutually exclusive with the rest.
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	if (null_local_username) {
4767c478bd9Sstevel@tonic-gate 		krb5auth_flag = B_FALSE;
4777c478bd9Sstevel@tonic-gate 		fflag = Fflag = encrypt_flag = 0;
4787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Note: The -a option nullifies "
4797c478bd9Sstevel@tonic-gate 					"all other Kerberos-specific\noptions "
4807c478bd9Sstevel@tonic-gate 					"you may have used.\n"));
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (krb5auth_flag) {
4847c478bd9Sstevel@tonic-gate 		status = krb5_init_context(&bsd_context);
4857c478bd9Sstevel@tonic-gate 		if (status) {
4867c478bd9Sstevel@tonic-gate 			com_err(rlogin, status, gettext("while initializing"
4877c478bd9Sstevel@tonic-gate 					" krb5"));
4887c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 		/*
4917c478bd9Sstevel@tonic-gate 		 * Set up buffers for desread and deswrite.
4927c478bd9Sstevel@tonic-gate 		 */
4937c478bd9Sstevel@tonic-gate 		desinbuf.data = des_inbuf;
4947c478bd9Sstevel@tonic-gate 		desoutbuf.data = des_outbuf;
4957c478bd9Sstevel@tonic-gate 		desinbuf.length = sizeof (des_inbuf);
4967c478bd9Sstevel@tonic-gate 		desoutbuf.length = sizeof (des_outbuf);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		/*
4997c478bd9Sstevel@tonic-gate 		 * Get our local realm to look up local realm options.
5007c478bd9Sstevel@tonic-gate 		 */
5017c478bd9Sstevel@tonic-gate 		status = krb5_get_default_realm(bsd_context, &realmdef[1]);
5027c478bd9Sstevel@tonic-gate 		if (status) {
5037c478bd9Sstevel@tonic-gate 			com_err(rlogin, status,
5047c478bd9Sstevel@tonic-gate 				gettext("while getting default realm"));
5057c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 		/*
5087c478bd9Sstevel@tonic-gate 		 * Check the realms section in krb5.conf for encryption,
5097c478bd9Sstevel@tonic-gate 		 * forward & forwardable info
5107c478bd9Sstevel@tonic-gate 		 */
5117c478bd9Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, realmdef,
5127c478bd9Sstevel@tonic-gate 						option);
5137c478bd9Sstevel@tonic-gate 		/*
5147c478bd9Sstevel@tonic-gate 		 * Check the appdefaults section
5157c478bd9Sstevel@tonic-gate 		 */
5167c478bd9Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, appdef,
5177c478bd9Sstevel@tonic-gate 						option);
5187c478bd9Sstevel@tonic-gate 		profile_get_options_string(bsd_context->profile, appdef,
5197c478bd9Sstevel@tonic-gate 						rcmdversion);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		/*
5227c478bd9Sstevel@tonic-gate 		 * Set the *_flag variables, if the corresponding *_done are
5237c478bd9Sstevel@tonic-gate 		 * set to 1, because we dont want the config file values
5247c478bd9Sstevel@tonic-gate 		 * overriding the command line options.
5257c478bd9Sstevel@tonic-gate 		 */
5267c478bd9Sstevel@tonic-gate 		if (encrypt_done)
5277c478bd9Sstevel@tonic-gate 			encrypt_flag = 1;
5287c478bd9Sstevel@tonic-gate 		if (fwd_done) {
5297c478bd9Sstevel@tonic-gate 			fflag = 1;
5307c478bd9Sstevel@tonic-gate 			Fflag = 0;
5317c478bd9Sstevel@tonic-gate 		} else if (fwdable_done) {
5327c478bd9Sstevel@tonic-gate 			Fflag = 1;
5337c478bd9Sstevel@tonic-gate 			fflag = 0;
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 		if (!rcmdoption_done && (rcmdproto != NULL)) {
5367c478bd9Sstevel@tonic-gate 			if (strncmp(rcmdproto, "rcmdv2", 6) == 0) {
5377c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
5387c478bd9Sstevel@tonic-gate 			} else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) {
5397c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
5407c478bd9Sstevel@tonic-gate 			} else {
5417c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Unrecognized "
5427c478bd9Sstevel@tonic-gate 					"KCMD protocol (%s)"), rcmdproto);
5437c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
5447c478bd9Sstevel@tonic-gate 			}
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		if (encrypt_flag && (!krb5_privacy_allowed())) {
5487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rlogin: "
5497c478bd9Sstevel@tonic-gate 					"Encryption not supported.\n"));
5507c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (port_number == 0) {
5557c478bd9Sstevel@tonic-gate 		if (krb5auth_flag) {
5567c478bd9Sstevel@tonic-gate 			struct servent *sp;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 			/*
5597c478bd9Sstevel@tonic-gate 			 * If the krb5auth_flag is set (via -A, -f, -F, -k) &
5607c478bd9Sstevel@tonic-gate 			 * if there is an entry in /etc/services for Kerberos
5617c478bd9Sstevel@tonic-gate 			 * login, attempt to login with Kerberos. If we fail
5627c478bd9Sstevel@tonic-gate 			 * at any step,  use the standard rlogin
5637c478bd9Sstevel@tonic-gate 			 */
5647c478bd9Sstevel@tonic-gate 			sp = getservbyname(encrypt_flag ?
5657c478bd9Sstevel@tonic-gate 			    "eklogin" : "klogin", "tcp");
5667c478bd9Sstevel@tonic-gate 			if (sp == NULL) {
5677c478bd9Sstevel@tonic-gate 				port_number = encrypt_flag ?
5687c478bd9Sstevel@tonic-gate 				    htons(2105) : htons(543);
5697c478bd9Sstevel@tonic-gate 			} else {
5707c478bd9Sstevel@tonic-gate 				port_number = sp->s_port;
5717c478bd9Sstevel@tonic-gate 			}
5727c478bd9Sstevel@tonic-gate 		} else {
5737c478bd9Sstevel@tonic-gate 			port_number = htons(IPPORT_LOGINSERVER);
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	cp = getenv("TERM");
5787c478bd9Sstevel@tonic-gate 	if (cp) {
5797c478bd9Sstevel@tonic-gate 		(void) strncpy(term, cp, sizeof (term));
5807c478bd9Sstevel@tonic-gate 		term[sizeof (term) - 1] = '\0';
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 	if (getattr_ret == 0) {
5837c478bd9Sstevel@tonic-gate 		speed = cfgetospeed(&savetty);
5847c478bd9Sstevel@tonic-gate 		/*
5857c478bd9Sstevel@tonic-gate 		 * "Be conservative in what we send" -- Only send baud rates
5867c478bd9Sstevel@tonic-gate 		 * which at least all 4.x BSD derivatives are known to handle
5877c478bd9Sstevel@tonic-gate 		 * correctly.
5887c478bd9Sstevel@tonic-gate 		 * NOTE:  This code assumes new termios speed values will
5897c478bd9Sstevel@tonic-gate 		 * be "higher" speeds.
5907c478bd9Sstevel@tonic-gate 		 */
5917c478bd9Sstevel@tonic-gate 		if (speed > B38400)
5927c478bd9Sstevel@tonic-gate 			speed = B38400;
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/*
5967c478bd9Sstevel@tonic-gate 	 * Only put the terminal speed info in if we have room
5977c478bd9Sstevel@tonic-gate 	 * so we don't overflow the buffer, and only if we have
5987c478bd9Sstevel@tonic-gate 	 * a speed we recognize.
5997c478bd9Sstevel@tonic-gate 	 */
6007c478bd9Sstevel@tonic-gate 	if (speed > 0 && speed < sizeof (speeds)/sizeof (char *) &&
6017c478bd9Sstevel@tonic-gate 	    strlen(term) + strlen("/") + strlen(speeds[speed]) + 1 <
6027c478bd9Sstevel@tonic-gate 	    sizeof (term)) {
6037c478bd9Sstevel@tonic-gate 		(void) strcat(term, "/");
6047c478bd9Sstevel@tonic-gate 		(void) strcat(term, speeds[speed]);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 	(void) sigset(SIGPIPE, (sigdisp_t)lostpeer);
6077c478bd9Sstevel@tonic-gate 	/* will use SIGUSR1 for window size hack, so hold it off */
6087c478bd9Sstevel@tonic-gate 	oldmask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/*
6117c478bd9Sstevel@tonic-gate 	 * Determine if v4 literal address and if so store it to one
6127c478bd9Sstevel@tonic-gate 	 * side. This is to correct the undesired behaviour of rcmd_af
6137c478bd9Sstevel@tonic-gate 	 * which converts a passed in v4 literal address to a v4 mapped
6147c478bd9Sstevel@tonic-gate 	 * v6 literal address. If it was a v4 literal we then re-assign
6157c478bd9Sstevel@tonic-gate 	 * it to host.
6167c478bd9Sstevel@tonic-gate 	 */
6177c478bd9Sstevel@tonic-gate 	tmp = NULL;
6187c478bd9Sstevel@tonic-gate 	if (inet_addr(host) != (in_addr_t)-1)
6197c478bd9Sstevel@tonic-gate 		tmp = host;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if (krb5auth_flag) {
6227c478bd9Sstevel@tonic-gate 		authopts = AP_OPTS_MUTUAL_REQUIRED;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		/* Piggy-back forwarding flags on top of authopts; */
6257c478bd9Sstevel@tonic-gate 		/* they will be reset in kcmd */
6267c478bd9Sstevel@tonic-gate 		if (fflag || Fflag)
6277c478bd9Sstevel@tonic-gate 			authopts |= OPTS_FORWARD_CREDS;
6287c478bd9Sstevel@tonic-gate 		if (Fflag)
6297c478bd9Sstevel@tonic-gate 			authopts |= OPTS_FORWARDABLE_CREDS;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 		status = kcmd(&sock, &host, port_number,
6327c478bd9Sstevel@tonic-gate 			null_local_username ? "" : pwd->pw_name,
6337c478bd9Sstevel@tonic-gate 			name, term, NULL,
6347c478bd9Sstevel@tonic-gate 			"host", krb_realm, bsd_context, &auth_context,
6357c478bd9Sstevel@tonic-gate 			&cred,
6367c478bd9Sstevel@tonic-gate 			NULL,		/* No need for sequence number */
6377c478bd9Sstevel@tonic-gate 			NULL,		/* No need for server seq # */
6387c478bd9Sstevel@tonic-gate 			authopts,
6397c478bd9Sstevel@tonic-gate 			0,		/* Not any port # */
6407c478bd9Sstevel@tonic-gate 			&kcmd_proto);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		if (status != 0) {
6437c478bd9Sstevel@tonic-gate 			/*
6447c478bd9Sstevel@tonic-gate 			 * If new protocol requested, we dont fallback to
6457c478bd9Sstevel@tonic-gate 			 * less secure ones.
6467c478bd9Sstevel@tonic-gate 			 */
6477c478bd9Sstevel@tonic-gate 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
6487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rlogin: kcmdv2 "
6497c478bd9Sstevel@tonic-gate 					"to host %s failed - %s\n"
6507c478bd9Sstevel@tonic-gate 					"Fallback to normal rlogin denied."),
6517c478bd9Sstevel@tonic-gate 					host, error_message(status));
6527c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
6537c478bd9Sstevel@tonic-gate 			}
6547c478bd9Sstevel@tonic-gate 			if (status != -1) {
6557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rlogin: kcmd "
6567c478bd9Sstevel@tonic-gate 						"to host %s failed - %s,\n"
6577c478bd9Sstevel@tonic-gate 						"trying normal rlogin...\n\n"),
6587c478bd9Sstevel@tonic-gate 						host, error_message(status));
6597c478bd9Sstevel@tonic-gate 			} else {
6607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6617c478bd9Sstevel@tonic-gate 					gettext("trying normal rlogin...\n"));
6627c478bd9Sstevel@tonic-gate 			}
6637c478bd9Sstevel@tonic-gate 			/*
6647c478bd9Sstevel@tonic-gate 			 * kcmd() failed, so we have to
6657c478bd9Sstevel@tonic-gate 			 * fallback to normal rlogin
6667c478bd9Sstevel@tonic-gate 			 */
6677c478bd9Sstevel@tonic-gate 			port_number = htons(IPPORT_LOGINSERVER);
6687c478bd9Sstevel@tonic-gate 			krb5auth_flag = B_FALSE;
6697c478bd9Sstevel@tonic-gate 			fflag = Fflag = encrypt_flag = 0;
6707c478bd9Sstevel@tonic-gate 			null_local_username = B_FALSE;
6717c478bd9Sstevel@tonic-gate 		} else {
6727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6737c478bd9Sstevel@tonic-gate 			    gettext("connected with Kerberos V5\n"));
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 			/*
6767c478bd9Sstevel@tonic-gate 			 * Setup eblock for desread and deswrite.
6777c478bd9Sstevel@tonic-gate 			 */
6787c478bd9Sstevel@tonic-gate 			session_key = &cred->keyblock;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
6817c478bd9Sstevel@tonic-gate 				status = krb5_auth_con_getlocalsubkey(
6827c478bd9Sstevel@tonic-gate 				    bsd_context,
6837c478bd9Sstevel@tonic-gate 				    auth_context,
6847c478bd9Sstevel@tonic-gate 				    &session_key);
6857c478bd9Sstevel@tonic-gate 				if (status) {
6867c478bd9Sstevel@tonic-gate 					com_err(rlogin, status,
6877c478bd9Sstevel@tonic-gate 					    "determining subkey for session");
6887c478bd9Sstevel@tonic-gate 					return (EXIT_FAILURE);
6897c478bd9Sstevel@tonic-gate 				}
6907c478bd9Sstevel@tonic-gate 				if (session_key == NULL) {
6917c478bd9Sstevel@tonic-gate 					com_err(rlogin, 0,
6927c478bd9Sstevel@tonic-gate 					    "no subkey negotiated for "
6937c478bd9Sstevel@tonic-gate 					    "connection");
6947c478bd9Sstevel@tonic-gate 					return (EXIT_FAILURE);
6957c478bd9Sstevel@tonic-gate 				}
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 			eblock.crypto_entry = session_key->enctype;
6997c478bd9Sstevel@tonic-gate 			eblock.key = (krb5_keyblock *)session_key;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			init_encrypt(encrypt_flag, bsd_context, kcmd_proto,
7027c478bd9Sstevel@tonic-gate 			    &desinbuf, &desoutbuf, CLIENT, &eblock);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 			rem = sock;
7057c478bd9Sstevel@tonic-gate 			if (rem < 0)
7067c478bd9Sstevel@tonic-gate 				pop(EXIT_FAILURE);
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * Don't merge this with the "if" statement above because
7127c478bd9Sstevel@tonic-gate 	 * "krb5auth_flag" might be set to false inside it.
7137c478bd9Sstevel@tonic-gate 	 */
7147c478bd9Sstevel@tonic-gate 	if (!krb5auth_flag) {
7157c478bd9Sstevel@tonic-gate 		rem = rcmd_af(&host, port_number,
7167c478bd9Sstevel@tonic-gate 			null_local_username ? "" : pwd->pw_name,
7177c478bd9Sstevel@tonic-gate 			name, term, NULL, AF_INET6);
7187c478bd9Sstevel@tonic-gate 		if (rem < 0)
7197c478bd9Sstevel@tonic-gate 			pop(EXIT_FAILURE);
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/* Never need our privilege again */
7237c478bd9Sstevel@tonic-gate 	__priv_relinquish();
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	if (tmp != NULL)
7267c478bd9Sstevel@tonic-gate 		host = tmp;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	if (options & SO_DEBUG &&
7297c478bd9Sstevel@tonic-gate 	    setsockopt(rem, SOL_SOCKET, SO_DEBUG, (char *)&on,
7307c478bd9Sstevel@tonic-gate 			    sizeof (on)) < 0)
7317c478bd9Sstevel@tonic-gate 		perror("rlogin: setsockopt (SO_DEBUG)");
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	{
7347c478bd9Sstevel@tonic-gate 		int bufsize = 8192;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		(void) setsockopt(rem, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize,
7377c478bd9Sstevel@tonic-gate 			sizeof (int));
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	doit(oldmask);
741*740638c8Sbw 	return (0);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate static void
7457c478bd9Sstevel@tonic-gate doit(int oldmask)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	struct sgttyb sb;
7487c478bd9Sstevel@tonic-gate 	int atmark;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
7517c478bd9Sstevel@tonic-gate 		perror("ioctl TIOCGETP");
7527c478bd9Sstevel@tonic-gate 	defflags = sb.sg_flags;
7537c478bd9Sstevel@tonic-gate 	tabflag = defflags & O_TBDELAY;
7547c478bd9Sstevel@tonic-gate 	defflags &= ECHO | O_CRMOD;
7557c478bd9Sstevel@tonic-gate 	deferase = sb.sg_erase;
7567c478bd9Sstevel@tonic-gate 	defkill = sb.sg_kill;
7577c478bd9Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCLGET, (char *)&deflflags) == -1)
7587c478bd9Sstevel@tonic-gate 		perror("ioctl TIOCLGET");
7597c478bd9Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCGETC, (char *)&deftc) == -1)
7607c478bd9Sstevel@tonic-gate 		perror("ioctl TIOCGETC");
7617c478bd9Sstevel@tonic-gate 	notc.t_startc = deftc.t_startc;
7627c478bd9Sstevel@tonic-gate 	notc.t_stopc = deftc.t_stopc;
7637c478bd9Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCGLTC, (char *)&defltc) == -1)
7647c478bd9Sstevel@tonic-gate 		perror("ioctl TIOCGLTC");
7657c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
7667c478bd9Sstevel@tonic-gate 	if (sigdisp(SIGHUP) != SIG_IGN)
7677c478bd9Sstevel@tonic-gate 		(void) sigset(SIGHUP, exit);
7687c478bd9Sstevel@tonic-gate 	if (sigdisp(SIGQUIT) != SIG_IGN)
7697c478bd9Sstevel@tonic-gate 		(void) sigset(SIGQUIT, exit);
7707c478bd9Sstevel@tonic-gate 	child = fork();
7717c478bd9Sstevel@tonic-gate 	if (child == (pid_t)-1) {
7727c478bd9Sstevel@tonic-gate 		perror("rlogin: fork");
7737c478bd9Sstevel@tonic-gate 		done(EXIT_FAILURE);
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 	if (child == 0) {
7767c478bd9Sstevel@tonic-gate 		mode(1);
7777c478bd9Sstevel@tonic-gate 		if (reader(oldmask) == 0) {
7787c478bd9Sstevel@tonic-gate 			prf(gettext("Connection to %.*s closed."),
7797c478bd9Sstevel@tonic-gate 			    MAXHOSTNAMELEN, host);
7807c478bd9Sstevel@tonic-gate 			exit(EXIT_SUCCESS);
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 		(void) sleep(1);
7837c478bd9Sstevel@tonic-gate 		prf(gettext("\aConnection to %.*s closed."),
7847c478bd9Sstevel@tonic-gate 		    MAXHOSTNAMELEN, host);
7857c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	/*
7897c478bd9Sstevel@tonic-gate 	 * We may still own the socket, and may have a pending SIGURG (or might
7907c478bd9Sstevel@tonic-gate 	 * receive one soon) that we really want to send to the reader.  Set a
7917c478bd9Sstevel@tonic-gate 	 * trap that simply copies such signals to the child.
7927c478bd9Sstevel@tonic-gate 	 */
7937c478bd9Sstevel@tonic-gate #ifdef F_SETOWN_BUG_FIXED
7947c478bd9Sstevel@tonic-gate 	(void) sigset(SIGURG, copytochild);
7957c478bd9Sstevel@tonic-gate #else
7967c478bd9Sstevel@tonic-gate 	(void) sigset(SIGURG, SIG_IGN);
7977c478bd9Sstevel@tonic-gate #endif /* F_SETOWN_BUG_FIXED */
7987c478bd9Sstevel@tonic-gate 	(void) sigset(SIGUSR1, writeroob);
7997c478bd9Sstevel@tonic-gate 	/*
8007c478bd9Sstevel@tonic-gate 	 * Of course, if the urgent byte already arrived, allowing SIGURG
8017c478bd9Sstevel@tonic-gate 	 * won't get us notification.  So, we check to see if we've got
8027c478bd9Sstevel@tonic-gate 	 * an urgent byte.  If so, force a call to writeroob() to pretend
8037c478bd9Sstevel@tonic-gate 	 * we got SIGURG.
8047c478bd9Sstevel@tonic-gate 	 */
8057c478bd9Sstevel@tonic-gate 	if (ioctl(rem, SIOCATMARK, &atmark) >= 0) {
8067c478bd9Sstevel@tonic-gate 		if (atmark)
8077c478bd9Sstevel@tonic-gate 			writeroob(0);
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 	sigsetmask(oldmask);
8107c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, catchild);
8117c478bd9Sstevel@tonic-gate 	writer();
8127c478bd9Sstevel@tonic-gate 	prf(gettext("Closed connection to %.*s."), MAXHOSTNAMELEN, host);
8137c478bd9Sstevel@tonic-gate 	done(EXIT_SUCCESS);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate /*
8177c478bd9Sstevel@tonic-gate  * Get signal disposition (or signal handler) for a given signal
8187c478bd9Sstevel@tonic-gate  */
8197c478bd9Sstevel@tonic-gate static sigdisp_t
8207c478bd9Sstevel@tonic-gate sigdisp(int sig)
8217c478bd9Sstevel@tonic-gate {
8227c478bd9Sstevel@tonic-gate 	struct sigaction act;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	act.sa_handler = NULL;
8257c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
8267c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
8277c478bd9Sstevel@tonic-gate 	(void) sigaction(sig, NULL, &act);
8287c478bd9Sstevel@tonic-gate 	return (act.sa_handler);
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate static void
8327c478bd9Sstevel@tonic-gate done(int status)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	pid_t w;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	mode(0);
8377c478bd9Sstevel@tonic-gate 	if (child > 0) {
8387c478bd9Sstevel@tonic-gate 		/* make sure catchild does not snap it up */
8397c478bd9Sstevel@tonic-gate 		(void) sigset(SIGCHLD, SIG_DFL);
8407c478bd9Sstevel@tonic-gate 		if (kill(child, SIGKILL) >= 0)
8417c478bd9Sstevel@tonic-gate 			while ((w = wait(0)) > (pid_t)0 && w != child)
8427c478bd9Sstevel@tonic-gate 				/* void */;
8437c478bd9Sstevel@tonic-gate 	}
8447c478bd9Sstevel@tonic-gate 	pop(status);
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate /*
8487c478bd9Sstevel@tonic-gate  * Copy SIGURGs to the child process.
8497c478bd9Sstevel@tonic-gate  */
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate /* ARGSUSED */
8527c478bd9Sstevel@tonic-gate static void
8537c478bd9Sstevel@tonic-gate copytochild(int signum)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	(void) kill(child, SIGURG);
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate /*
8607c478bd9Sstevel@tonic-gate  * This is called when the reader process gets the out-of-band (urgent)
8617c478bd9Sstevel@tonic-gate  * request to turn on the window-changing protocol.
8627c478bd9Sstevel@tonic-gate  */
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate /* ARGSUSED */
8657c478bd9Sstevel@tonic-gate static void
8667c478bd9Sstevel@tonic-gate writeroob(int signum)
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate 	int mask;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	if (!dosigwinch) {
8717c478bd9Sstevel@tonic-gate 		/*
8727c478bd9Sstevel@tonic-gate 		 * Start tracking window size.  It doesn't matter which
8737c478bd9Sstevel@tonic-gate 		 * order the next two are in, because we'll be unconditionally
8747c478bd9Sstevel@tonic-gate 		 * sending a size notification in a moment.
8757c478bd9Sstevel@tonic-gate 		 */
8767c478bd9Sstevel@tonic-gate 		(void) sigset(SIGWINCH, sigwinch);
8777c478bd9Sstevel@tonic-gate 		dosigwinch = B_TRUE;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		/*
8807c478bd9Sstevel@tonic-gate 		 * It would be bad if a SIGWINCH came in between the ioctl
8817c478bd9Sstevel@tonic-gate 		 * and sending the data.  It could result in the SIGWINCH
8827c478bd9Sstevel@tonic-gate 		 * handler sending a good message, and then us sending an
8837c478bd9Sstevel@tonic-gate 		 * outdated or inconsistent message.
8847c478bd9Sstevel@tonic-gate 		 *
8857c478bd9Sstevel@tonic-gate 		 * Instead, if the change is made before the
8867c478bd9Sstevel@tonic-gate 		 * ioctl, the sigwinch handler will send a size message
8877c478bd9Sstevel@tonic-gate 		 * and we'll send another, identical, one.  If the change
8887c478bd9Sstevel@tonic-gate 		 * is made after the ioctl, we'll send a message with the
8897c478bd9Sstevel@tonic-gate 		 * old value, and then the sigwinch handler will send
8907c478bd9Sstevel@tonic-gate 		 * a revised, correct one.
8917c478bd9Sstevel@tonic-gate 		 */
8927c478bd9Sstevel@tonic-gate 		mask = sigblock(sigmask(SIGWINCH));
8937c478bd9Sstevel@tonic-gate 		if (ioctl(STDIN_FILENO, TIOCGWINSZ, &winsize) == 0)
8947c478bd9Sstevel@tonic-gate 			sendwindow();
8957c478bd9Sstevel@tonic-gate 		sigsetmask(mask);
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate /* ARGSUSED */
9007c478bd9Sstevel@tonic-gate static void
9017c478bd9Sstevel@tonic-gate catchild(int signum)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	int options;
9047c478bd9Sstevel@tonic-gate 	siginfo_t	info;
9057c478bd9Sstevel@tonic-gate 	int error;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	for (;;) {
9087c478bd9Sstevel@tonic-gate 		options = WNOHANG | WEXITED;
9097c478bd9Sstevel@tonic-gate 		error = waitid(P_ALL, 0, &info, options);
9107c478bd9Sstevel@tonic-gate 		if (error != 0)
9117c478bd9Sstevel@tonic-gate 			return;
9127c478bd9Sstevel@tonic-gate 		if (info.si_pid == 0)
9137c478bd9Sstevel@tonic-gate 			return;
9147c478bd9Sstevel@tonic-gate 		if (info.si_code == CLD_TRAPPED)
9157c478bd9Sstevel@tonic-gate 			continue;
9167c478bd9Sstevel@tonic-gate 		if (info.si_code == CLD_STOPPED)
9177c478bd9Sstevel@tonic-gate 			continue;
9187c478bd9Sstevel@tonic-gate 		done(info.si_status);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate /*
9237c478bd9Sstevel@tonic-gate  * writer: write to remote: 0 -> line.
9247c478bd9Sstevel@tonic-gate  * ~.	terminate
9257c478bd9Sstevel@tonic-gate  * ~^Z	suspend rlogin process.
9267c478bd9Sstevel@tonic-gate  * ~^Y  suspend rlogin process, but leave reader alone.
9277c478bd9Sstevel@tonic-gate  */
9287c478bd9Sstevel@tonic-gate static void
9297c478bd9Sstevel@tonic-gate writer(void)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 	char c;
9327c478bd9Sstevel@tonic-gate 	int n;
9337c478bd9Sstevel@tonic-gate 	boolean_t bol = B_TRUE;		/* beginning of line */
9347c478bd9Sstevel@tonic-gate 	boolean_t local = B_FALSE;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	for (;;) {
9377c478bd9Sstevel@tonic-gate 		n = read(STDIN_FILENO, &c, 1);
9387c478bd9Sstevel@tonic-gate 		if (n <= 0) {
9397c478bd9Sstevel@tonic-gate 			if (n == 0)
9407c478bd9Sstevel@tonic-gate 				break;
9417c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
9427c478bd9Sstevel@tonic-gate 				continue;
9437c478bd9Sstevel@tonic-gate 			else {
9447c478bd9Sstevel@tonic-gate 				prf(gettext("Read error from terminal: %s"),
9457c478bd9Sstevel@tonic-gate 				    errmsg(errno));
9467c478bd9Sstevel@tonic-gate 				break;
9477c478bd9Sstevel@tonic-gate 			}
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 		/*
9507c478bd9Sstevel@tonic-gate 		 * If we're at the beginning of the line
9517c478bd9Sstevel@tonic-gate 		 * and recognize a command character, then
9527c478bd9Sstevel@tonic-gate 		 * we echo locally.  Otherwise, characters
9537c478bd9Sstevel@tonic-gate 		 * are echo'd remotely.  If the command
9547c478bd9Sstevel@tonic-gate 		 * character is doubled, this acts as a
9557c478bd9Sstevel@tonic-gate 		 * force and local echo is suppressed.
9567c478bd9Sstevel@tonic-gate 		 */
9577c478bd9Sstevel@tonic-gate 		if (bol && !nocmdchar) {
9587c478bd9Sstevel@tonic-gate 			bol = B_FALSE;
9597c478bd9Sstevel@tonic-gate 			if (c == cmdchar) {
9607c478bd9Sstevel@tonic-gate 				local = B_TRUE;
9617c478bd9Sstevel@tonic-gate 				continue;
9627c478bd9Sstevel@tonic-gate 			}
9637c478bd9Sstevel@tonic-gate 		} else if (local) {
9647c478bd9Sstevel@tonic-gate 			local = B_FALSE;
9657c478bd9Sstevel@tonic-gate 			if (c == '.' || c == deftc.t_eofc) {
9667c478bd9Sstevel@tonic-gate 				echo(c);
9677c478bd9Sstevel@tonic-gate 				break;
9687c478bd9Sstevel@tonic-gate 			}
9697c478bd9Sstevel@tonic-gate 			if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
9707c478bd9Sstevel@tonic-gate 				bol = B_TRUE;
9717c478bd9Sstevel@tonic-gate 				echo(c);
9727c478bd9Sstevel@tonic-gate 				stop(c);
9737c478bd9Sstevel@tonic-gate 				continue;
9747c478bd9Sstevel@tonic-gate 			}
9757c478bd9Sstevel@tonic-gate 			if (c != cmdchar) {
9767c478bd9Sstevel@tonic-gate 				if (deswrite(rem, &cmdchar, 1, 0) < 0) {
9777c478bd9Sstevel@tonic-gate 					prf(gettext(
9787c478bd9Sstevel@tonic-gate 					    "Write error to network: %s"),
9797c478bd9Sstevel@tonic-gate 					    errmsg(errno));
9807c478bd9Sstevel@tonic-gate 					break;
9817c478bd9Sstevel@tonic-gate 				}
9827c478bd9Sstevel@tonic-gate 			}
9837c478bd9Sstevel@tonic-gate 		}
9847c478bd9Sstevel@tonic-gate 		if ((n = deswrite(rem, &c, 1, 0)) <= 0) {
9857c478bd9Sstevel@tonic-gate 			if (n == 0)
9867c478bd9Sstevel@tonic-gate 				prf(gettext("line gone"));
9877c478bd9Sstevel@tonic-gate 			else
9887c478bd9Sstevel@tonic-gate 				prf(gettext("Write error to network: %s"),
9897c478bd9Sstevel@tonic-gate 				    errmsg(errno));
9907c478bd9Sstevel@tonic-gate 			break;
9917c478bd9Sstevel@tonic-gate 		}
9927c478bd9Sstevel@tonic-gate 		bol = c == defkill || c == deftc.t_eofc ||
9937c478bd9Sstevel@tonic-gate 		    c == deftc.t_intrc || c == defltc.t_suspc ||
9947c478bd9Sstevel@tonic-gate 		    c == '\r' || c == '\n';
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate static void
9997c478bd9Sstevel@tonic-gate echo(char c)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	char buf[8];
10027c478bd9Sstevel@tonic-gate 	char *p = buf;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	c &= 0177;
10057c478bd9Sstevel@tonic-gate 	*p++ = cmdchar;
10067c478bd9Sstevel@tonic-gate 	if (c < ' ') {
10077c478bd9Sstevel@tonic-gate 		*p++ = '^';
10087c478bd9Sstevel@tonic-gate 		*p++ = c + '@';
10097c478bd9Sstevel@tonic-gate 	} else if (c == 0177) {
10107c478bd9Sstevel@tonic-gate 		*p++ = '^';
10117c478bd9Sstevel@tonic-gate 		*p++ = '?';
10127c478bd9Sstevel@tonic-gate 	} else
10137c478bd9Sstevel@tonic-gate 		*p++ = c;
10147c478bd9Sstevel@tonic-gate 	*p++ = '\r';
10157c478bd9Sstevel@tonic-gate 	*p++ = '\n';
10167c478bd9Sstevel@tonic-gate 	if (write(STDOUT_FILENO, buf, p - buf) < 0)
10177c478bd9Sstevel@tonic-gate 		prf(gettext("Write error to terminal: %s"), errmsg(errno));
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate static void
10217c478bd9Sstevel@tonic-gate stop(char cmdc)
10227c478bd9Sstevel@tonic-gate {
10237c478bd9Sstevel@tonic-gate 	mode(0);
10247c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, SIG_IGN);
10257c478bd9Sstevel@tonic-gate 	(void) kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
10267c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, catchild);
10277c478bd9Sstevel@tonic-gate 	mode(1);
10287c478bd9Sstevel@tonic-gate 	sigwinch(0);			/* check for size changes */
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate /* ARGSUSED */
10327c478bd9Sstevel@tonic-gate static void
10337c478bd9Sstevel@tonic-gate sigwinch(int signum)
10347c478bd9Sstevel@tonic-gate {
10357c478bd9Sstevel@tonic-gate 	struct winsize ws;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	if (dosigwinch && ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0 &&
10387c478bd9Sstevel@tonic-gate 	    memcmp(&winsize, &ws, sizeof (ws)) != 0) {
10397c478bd9Sstevel@tonic-gate 		winsize = ws;
10407c478bd9Sstevel@tonic-gate 		sendwindow();
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate /*
10457c478bd9Sstevel@tonic-gate  * Send the window size to the server via the magic escape.
10467c478bd9Sstevel@tonic-gate  * Note:  SIGWINCH should be blocked when this is called, lest
10477c478bd9Sstevel@tonic-gate  * winsize change underneath us and chaos result.
10487c478bd9Sstevel@tonic-gate  */
10497c478bd9Sstevel@tonic-gate static void
10507c478bd9Sstevel@tonic-gate sendwindow(void)
10517c478bd9Sstevel@tonic-gate {
10527c478bd9Sstevel@tonic-gate 	char obuf[4 + sizeof (struct winsize)];
10537c478bd9Sstevel@tonic-gate 	struct winsize *wp = (struct winsize *)(void *)(obuf+4);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	obuf[0] = -1;
10567c478bd9Sstevel@tonic-gate 	obuf[1] = -1;
10577c478bd9Sstevel@tonic-gate 	obuf[2] = 's';
10587c478bd9Sstevel@tonic-gate 	obuf[3] = 's';
10597c478bd9Sstevel@tonic-gate 	wp->ws_row = htons(winsize.ws_row);
10607c478bd9Sstevel@tonic-gate 	wp->ws_col = htons(winsize.ws_col);
10617c478bd9Sstevel@tonic-gate 	wp->ws_xpixel = htons(winsize.ws_xpixel);
10627c478bd9Sstevel@tonic-gate 	wp->ws_ypixel = htons(winsize.ws_ypixel);
10637c478bd9Sstevel@tonic-gate 	if (deswrite(rem, obuf, sizeof (obuf), 0) < 0)
10647c478bd9Sstevel@tonic-gate 		prf(gettext("Write error to network: %s"), errmsg(errno));
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate /*
10697c478bd9Sstevel@tonic-gate  * reader: read from remote: remote -> stdout
10707c478bd9Sstevel@tonic-gate  */
10717c478bd9Sstevel@tonic-gate #define	READING	1
10727c478bd9Sstevel@tonic-gate #define	WRITING	2
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate static	char rcvbuf[8 * 1024];
10757c478bd9Sstevel@tonic-gate static	int rcvcnt;
10767c478bd9Sstevel@tonic-gate static	int rcvstate;
10777c478bd9Sstevel@tonic-gate static	pid_t ppid;
10787c478bd9Sstevel@tonic-gate static	jmp_buf rcvtop;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate static void
10817c478bd9Sstevel@tonic-gate oob(void)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	int out = FWRITE, atmark, n;
10847c478bd9Sstevel@tonic-gate 	int rcvd = 0;
10857c478bd9Sstevel@tonic-gate 	char waste[4*BUFSIZ], mark;
10867c478bd9Sstevel@tonic-gate 	struct sgttyb sb;
10877c478bd9Sstevel@tonic-gate 	fd_set exceptfds;
10887c478bd9Sstevel@tonic-gate 	struct timeval tv;
10897c478bd9Sstevel@tonic-gate 	int ret;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	FD_ZERO(&exceptfds);
10927c478bd9Sstevel@tonic-gate 	FD_SET(rem, &exceptfds);
10937c478bd9Sstevel@tonic-gate 	timerclear(&tv);
10947c478bd9Sstevel@tonic-gate 	ret = select(rem+1, NULL, NULL, &exceptfds, &tv);
10957c478bd9Sstevel@tonic-gate 	/*
10967c478bd9Sstevel@tonic-gate 	 * We may get an extra signal at start up time since we are trying
10977c478bd9Sstevel@tonic-gate 	 * to take all precautions not to miss the urgent byte. This
10987c478bd9Sstevel@tonic-gate 	 * means we may get here without any urgent data to process, in which
10997c478bd9Sstevel@tonic-gate 	 * case we do nothing and just return.
11007c478bd9Sstevel@tonic-gate 	 */
11017c478bd9Sstevel@tonic-gate 	if (ret <= 0)
11027c478bd9Sstevel@tonic-gate 		return;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	do {
11057c478bd9Sstevel@tonic-gate 		if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
11067c478bd9Sstevel@tonic-gate 			break;
11077c478bd9Sstevel@tonic-gate 		}
11087c478bd9Sstevel@tonic-gate 		if (!atmark) {
11097c478bd9Sstevel@tonic-gate 			/*
11107c478bd9Sstevel@tonic-gate 			 * Urgent data not here yet.
11117c478bd9Sstevel@tonic-gate 			 * It may not be possible to send it yet
11127c478bd9Sstevel@tonic-gate 			 * if we are blocked for output
11137c478bd9Sstevel@tonic-gate 			 * and our input buffer is full.
11147c478bd9Sstevel@tonic-gate 			 */
11157c478bd9Sstevel@tonic-gate 			if (rcvcnt < sizeof (rcvbuf)) {
11167c478bd9Sstevel@tonic-gate 				n = desread(rem, rcvbuf + rcvcnt,
11177c478bd9Sstevel@tonic-gate 					sizeof (rcvbuf) - rcvcnt, 0);
11187c478bd9Sstevel@tonic-gate 				if (n <= 0)
11197c478bd9Sstevel@tonic-gate 					return;
11207c478bd9Sstevel@tonic-gate 				rcvd += n;
11217c478bd9Sstevel@tonic-gate 				rcvcnt += n;
11227c478bd9Sstevel@tonic-gate 			} else {
11237c478bd9Sstevel@tonic-gate 				/*
11247c478bd9Sstevel@tonic-gate 				 * We still haven't gotten to the urgent mark
11257c478bd9Sstevel@tonic-gate 				 * and we're out of buffer space.  Since we
11267c478bd9Sstevel@tonic-gate 				 * must clear our receive window to allow it
11277c478bd9Sstevel@tonic-gate 				 * to arrive, we will have to throw away
11287c478bd9Sstevel@tonic-gate 				 * these bytes.
11297c478bd9Sstevel@tonic-gate 				 */
11307c478bd9Sstevel@tonic-gate 				n = desread(rem, waste, sizeof (waste), 0);
11317c478bd9Sstevel@tonic-gate 				if (n <= 0)
11327c478bd9Sstevel@tonic-gate 					return;
11337c478bd9Sstevel@tonic-gate 			}
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 	} while (atmark == 0);
11367c478bd9Sstevel@tonic-gate 	while (recv(rem, &mark, 1, MSG_OOB) < 0) {
11377c478bd9Sstevel@tonic-gate 		switch (errno) {
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 		case EWOULDBLOCK:
11407c478bd9Sstevel@tonic-gate 			/*
11417c478bd9Sstevel@tonic-gate 			 * We've reached the urgent mark, so the next
11427c478bd9Sstevel@tonic-gate 			 * data to arrive will be the urgent, but it must
11437c478bd9Sstevel@tonic-gate 			 * not have arrived yet.
11447c478bd9Sstevel@tonic-gate 			 */
11457c478bd9Sstevel@tonic-gate 			(void) sleep(1);
11467c478bd9Sstevel@tonic-gate 			continue;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 		default:
11497c478bd9Sstevel@tonic-gate 			return;
11507c478bd9Sstevel@tonic-gate 		}
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 	if (mark & TIOCPKT_WINDOW) {
11537c478bd9Sstevel@tonic-gate 		/*
11547c478bd9Sstevel@tonic-gate 		 * Let server know about window size changes
11557c478bd9Sstevel@tonic-gate 		 */
11567c478bd9Sstevel@tonic-gate 		(void) kill(ppid, SIGUSR1);
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 	if (!eight && (mark & TIOCPKT_NOSTOP)) {
11597c478bd9Sstevel@tonic-gate 		if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
11607c478bd9Sstevel@tonic-gate 			perror("ioctl TIOCGETP");
11617c478bd9Sstevel@tonic-gate 		sb.sg_flags &= ~O_CBREAK;
11627c478bd9Sstevel@tonic-gate 		sb.sg_flags |= O_RAW;
11637c478bd9Sstevel@tonic-gate 		if (compat_ioctl(STDIN_FILENO, TIOCSETP, &sb) == -1)
11647c478bd9Sstevel@tonic-gate 			perror("ioctl TIOCSETP 1");
11657c478bd9Sstevel@tonic-gate 		notc.t_stopc = -1;
11667c478bd9Sstevel@tonic-gate 		notc.t_startc = -1;
11677c478bd9Sstevel@tonic-gate 		if (compat_ioctl(STDIN_FILENO, TIOCSETC, &notc) == -1)
11687c478bd9Sstevel@tonic-gate 			perror("ioctl TIOCSETC");
11697c478bd9Sstevel@tonic-gate 	}
11707c478bd9Sstevel@tonic-gate 	if (!eight && (mark & TIOCPKT_DOSTOP)) {
11717c478bd9Sstevel@tonic-gate 		if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
11727c478bd9Sstevel@tonic-gate 			perror("ioctl TIOCGETP");
11737c478bd9Sstevel@tonic-gate 		sb.sg_flags &= ~O_RAW;
11747c478bd9Sstevel@tonic-gate 		sb.sg_flags |= O_CBREAK;
11757c478bd9Sstevel@tonic-gate 		if (compat_ioctl(STDIN_FILENO, TIOCSETP, &sb) == -1)
11767c478bd9Sstevel@tonic-gate 			perror("ioctl TIOCSETP 2");
11777c478bd9Sstevel@tonic-gate 		notc.t_stopc = deftc.t_stopc;
11787c478bd9Sstevel@tonic-gate 		notc.t_startc = deftc.t_startc;
11797c478bd9Sstevel@tonic-gate 		if (compat_ioctl(STDIN_FILENO, TIOCSETC, &notc) == -1)
11807c478bd9Sstevel@tonic-gate 			perror("ioctl TIOCSETC");
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	if (mark & TIOCPKT_FLUSHWRITE) {
11837c478bd9Sstevel@tonic-gate 		if (ioctl(STDOUT_FILENO, TIOCFLUSH, (char *)&out) == -1)
11847c478bd9Sstevel@tonic-gate 			perror("ioctl TIOCFLUSH");
11857c478bd9Sstevel@tonic-gate 		for (;;) {
11867c478bd9Sstevel@tonic-gate 			if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
11877c478bd9Sstevel@tonic-gate 				perror("ioctl SIOCATMARK");
11887c478bd9Sstevel@tonic-gate 				break;
11897c478bd9Sstevel@tonic-gate 			}
11907c478bd9Sstevel@tonic-gate 			if (atmark)
11917c478bd9Sstevel@tonic-gate 				break;
11927c478bd9Sstevel@tonic-gate 			n = desread(rem, waste, sizeof (waste), 0);
11937c478bd9Sstevel@tonic-gate 			if (n <= 0) {
11947c478bd9Sstevel@tonic-gate 				if (n < 0)
11957c478bd9Sstevel@tonic-gate 					prf(gettext(
11967c478bd9Sstevel@tonic-gate 					    "Read error from network: %s"),
11977c478bd9Sstevel@tonic-gate 					    errmsg(errno));
11987c478bd9Sstevel@tonic-gate 				break;
11997c478bd9Sstevel@tonic-gate 			}
12007c478bd9Sstevel@tonic-gate 		}
12017c478bd9Sstevel@tonic-gate 		/*
12027c478bd9Sstevel@tonic-gate 		 * Don't want any pending data to be output,
12037c478bd9Sstevel@tonic-gate 		 * so clear the recv buffer.
12047c478bd9Sstevel@tonic-gate 		 * If we were hanging on a write when interrupted,
12057c478bd9Sstevel@tonic-gate 		 * don't want it to restart.  If we were reading,
12067c478bd9Sstevel@tonic-gate 		 * restart anyway.
12077c478bd9Sstevel@tonic-gate 		 */
12087c478bd9Sstevel@tonic-gate 		rcvcnt = 0;
12097c478bd9Sstevel@tonic-gate 		longjmp(rcvtop, 1);
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 	/*
12127c478bd9Sstevel@tonic-gate 	 * If we filled the receive buffer while a read was pending,
12137c478bd9Sstevel@tonic-gate 	 * longjmp to the top to restart appropriately.  Don't abort
12147c478bd9Sstevel@tonic-gate 	 * a pending write, however, or we won't know how much was written.
12157c478bd9Sstevel@tonic-gate 	 */
12167c478bd9Sstevel@tonic-gate 	if (rcvd && rcvstate == READING)
12177c478bd9Sstevel@tonic-gate 		longjmp(rcvtop, 1);
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate /*
12217c478bd9Sstevel@tonic-gate  * reader: read from remote: line -> 1
12227c478bd9Sstevel@tonic-gate  */
12237c478bd9Sstevel@tonic-gate static int
12247c478bd9Sstevel@tonic-gate reader(int oldmask)
12257c478bd9Sstevel@tonic-gate {
12267c478bd9Sstevel@tonic-gate 	/*
12277c478bd9Sstevel@tonic-gate 	 * 4.3bsd or later and SunOS 4.0 or later use the posiitive
12287c478bd9Sstevel@tonic-gate 	 * pid; otherwise use the negative.
12297c478bd9Sstevel@tonic-gate 	 */
12307c478bd9Sstevel@tonic-gate 	pid_t pid = getpid();
12317c478bd9Sstevel@tonic-gate 	int n, remaining;
12327c478bd9Sstevel@tonic-gate 	char *bufp = rcvbuf;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTTOU, SIG_IGN);
12357c478bd9Sstevel@tonic-gate 	(void) sigset(SIGURG, (void (*)())oob);
12367c478bd9Sstevel@tonic-gate 	ppid = getppid();
12377c478bd9Sstevel@tonic-gate 	if (fcntl(rem, F_SETOWN, pid) == -1)
12387c478bd9Sstevel@tonic-gate 		perror("fcntl F_SETOWN");
12397c478bd9Sstevel@tonic-gate 	/*
12407c478bd9Sstevel@tonic-gate 	 * A SIGURG may have been posted before we were completely forked,
12417c478bd9Sstevel@tonic-gate 	 * which means we may not have received it. To insure we do not miss
12427c478bd9Sstevel@tonic-gate 	 * any urgent data, we force the signal. The signal hander will be
12437c478bd9Sstevel@tonic-gate 	 * able to determine if in fact there is urgent data or not.
12447c478bd9Sstevel@tonic-gate 	 */
12457c478bd9Sstevel@tonic-gate 	(void) kill(pid, SIGURG);
12467c478bd9Sstevel@tonic-gate 	(void) setjmp(rcvtop);
12477c478bd9Sstevel@tonic-gate 	sigsetmask(oldmask);
12487c478bd9Sstevel@tonic-gate 	for (;;) {
12497c478bd9Sstevel@tonic-gate 		while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
12507c478bd9Sstevel@tonic-gate 			rcvstate = WRITING;
12517c478bd9Sstevel@tonic-gate 			n = write(STDOUT_FILENO, bufp, remaining);
12527c478bd9Sstevel@tonic-gate 			if (n < 0) {
12537c478bd9Sstevel@tonic-gate 				if (errno != EINTR) {
12547c478bd9Sstevel@tonic-gate 					prf(gettext(
12557c478bd9Sstevel@tonic-gate 					    "Write error to terminal: %s"),
12567c478bd9Sstevel@tonic-gate 					    errmsg(errno));
12577c478bd9Sstevel@tonic-gate 					return (-1);
12587c478bd9Sstevel@tonic-gate 				}
12597c478bd9Sstevel@tonic-gate 				continue;
12607c478bd9Sstevel@tonic-gate 			}
12617c478bd9Sstevel@tonic-gate 			bufp += n;
12627c478bd9Sstevel@tonic-gate 		}
12637c478bd9Sstevel@tonic-gate 		bufp = rcvbuf;
12647c478bd9Sstevel@tonic-gate 		rcvcnt = 0;
12657c478bd9Sstevel@tonic-gate 		rcvstate = READING;
12667c478bd9Sstevel@tonic-gate 		rcvcnt = desread(rem, rcvbuf, sizeof (rcvbuf), 0);
12677c478bd9Sstevel@tonic-gate 		if (rcvcnt == 0)
12687c478bd9Sstevel@tonic-gate 			return (0);
12697c478bd9Sstevel@tonic-gate 		if (rcvcnt < 0) {
12707c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
12717c478bd9Sstevel@tonic-gate 				continue;
12727c478bd9Sstevel@tonic-gate 			prf(gettext("Read error from network: %s"),
12737c478bd9Sstevel@tonic-gate 			    errmsg(errno));
12747c478bd9Sstevel@tonic-gate 			return (-1);
12757c478bd9Sstevel@tonic-gate 		}
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate static void
12807c478bd9Sstevel@tonic-gate mode(int f)
12817c478bd9Sstevel@tonic-gate {
12827c478bd9Sstevel@tonic-gate 	struct tchars *tc;
12837c478bd9Sstevel@tonic-gate 	struct ltchars *ltc;
12847c478bd9Sstevel@tonic-gate 	struct sgttyb sb;
12857c478bd9Sstevel@tonic-gate 	int	lflags;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
12887c478bd9Sstevel@tonic-gate 		perror("ioctl TIOCGETP");
12897c478bd9Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCLGET, (char *)&lflags) == -1)
12907c478bd9Sstevel@tonic-gate 		perror("ioctl TIOCLGET");
12917c478bd9Sstevel@tonic-gate 	switch (f) {
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	case 0:
12947c478bd9Sstevel@tonic-gate 		sb.sg_flags &= ~(O_CBREAK|O_RAW|O_TBDELAY);
12957c478bd9Sstevel@tonic-gate 		sb.sg_flags |= defflags|tabflag;
12967c478bd9Sstevel@tonic-gate 		tc = &deftc;
12977c478bd9Sstevel@tonic-gate 		ltc = &defltc;
12987c478bd9Sstevel@tonic-gate 		sb.sg_kill = defkill;
12997c478bd9Sstevel@tonic-gate 		sb.sg_erase = deferase;
13007c478bd9Sstevel@tonic-gate 		lflags = deflflags;
13017c478bd9Sstevel@tonic-gate 		break;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	case 1:
13047c478bd9Sstevel@tonic-gate 		sb.sg_flags |= (eight ? O_RAW : O_CBREAK);
13057c478bd9Sstevel@tonic-gate 		sb.sg_flags &= ~defflags;
13067c478bd9Sstevel@tonic-gate 		/* preserve tab delays, but turn off XTABS */
13077c478bd9Sstevel@tonic-gate 		if ((sb.sg_flags & O_TBDELAY) == O_XTABS)
13087c478bd9Sstevel@tonic-gate 			sb.sg_flags &= ~O_TBDELAY;
13097c478bd9Sstevel@tonic-gate 		tc = &notc;
13107c478bd9Sstevel@tonic-gate 		ltc = &noltc;
13117c478bd9Sstevel@tonic-gate 		sb.sg_kill = sb.sg_erase = -1;
13127c478bd9Sstevel@tonic-gate 		if (litout)
13137c478bd9Sstevel@tonic-gate 			lflags |= LLITOUT;
13147c478bd9Sstevel@tonic-gate 		break;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	default:
13177c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
13187c478bd9Sstevel@tonic-gate 		return;
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 	if (compat_ioctl(STDIN_FILENO, TIOCSLTC, ltc) == -1)
13217c478bd9Sstevel@tonic-gate 		perror("ioctl TIOCSLTC");
13227c478bd9Sstevel@tonic-gate 	if (compat_ioctl(STDIN_FILENO, TIOCSETC, tc) == -1)
13237c478bd9Sstevel@tonic-gate 		perror("ioctl TIOCSETC");
13247c478bd9Sstevel@tonic-gate 	if (compat_ioctl(STDIN_FILENO, TIOCSETP, &sb) == -1)
13257c478bd9Sstevel@tonic-gate 		perror("ioctl TIOCSETP 3");
13267c478bd9Sstevel@tonic-gate 	if (compat_ioctl(STDIN_FILENO, TIOCLSET, &lflags) == -1)
13277c478bd9Sstevel@tonic-gate 		perror("ioctl TIOCLSET");
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate /* PRINTFLIKE(0) */
13317c478bd9Sstevel@tonic-gate static void
13327c478bd9Sstevel@tonic-gate prf(const char *format, ...)
13337c478bd9Sstevel@tonic-gate {
13347c478bd9Sstevel@tonic-gate 	va_list	ap;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	va_start(ap, format);
13377c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
13387c478bd9Sstevel@tonic-gate 	va_end(ap);
13397c478bd9Sstevel@tonic-gate 	(void) fputs(CRLF, stderr);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate static void
13437c478bd9Sstevel@tonic-gate lostpeer(void)
13447c478bd9Sstevel@tonic-gate {
13457c478bd9Sstevel@tonic-gate 	(void) sigset(SIGPIPE, SIG_IGN);
13467c478bd9Sstevel@tonic-gate 	prf(gettext("\aConnection to %.*s closed."), MAXHOSTNAMELEN, host);
13477c478bd9Sstevel@tonic-gate 	done(EXIT_FAILURE);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate static char *
13517c478bd9Sstevel@tonic-gate errmsg(int errcode)
13527c478bd9Sstevel@tonic-gate {
13537c478bd9Sstevel@tonic-gate 	extern int sys_nerr;
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	if (errcode < 0 || errcode > sys_nerr)
13567c478bd9Sstevel@tonic-gate 		return (gettext("Unknown error"));
13577c478bd9Sstevel@tonic-gate 	else
13587c478bd9Sstevel@tonic-gate 		return (strerror(errcode));
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate static int
13627c478bd9Sstevel@tonic-gate compat_ioctl(int des, int request, void *arg)
13637c478bd9Sstevel@tonic-gate {
13647c478bd9Sstevel@tonic-gate 	struct termios	tb;
13657c478bd9Sstevel@tonic-gate 	boolean_t	flag = B_FALSE;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	if (ioctl(des, request, arg) < 0)
13687c478bd9Sstevel@tonic-gate 		return (-1);
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	if (tcgetattr(des, &tb) < 0)
13717c478bd9Sstevel@tonic-gate 		return (-1);
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	if (cfgetispeed(&tb) != cfgetispeed(&savetty)) {
13747c478bd9Sstevel@tonic-gate 		(void) cfsetispeed(&tb, cfgetispeed(&savetty));
13757c478bd9Sstevel@tonic-gate 		flag = B_TRUE;
13767c478bd9Sstevel@tonic-gate 	}
13777c478bd9Sstevel@tonic-gate 	if (cfgetospeed(&tb) != cfgetospeed(&savetty)) {
13787c478bd9Sstevel@tonic-gate 		(void) cfsetospeed(&tb, cfgetospeed(&savetty));
13797c478bd9Sstevel@tonic-gate 		flag = B_TRUE;
13807c478bd9Sstevel@tonic-gate 	}
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	return (flag ? tcsetattr(des, TCSANOW, &tb) : 0);
13837c478bd9Sstevel@tonic-gate }
1384