xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/rlogin.c (revision 634:263e46dc023d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23473Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * rlogin - remote login
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <sys/param.h>
470Sstevel@tonic-gate #include <sys/errno.h>
480Sstevel@tonic-gate #include <sys/file.h>
490Sstevel@tonic-gate #include <sys/socket.h>
500Sstevel@tonic-gate #include <sys/wait.h>
510Sstevel@tonic-gate #include <sys/stropts.h>
520Sstevel@tonic-gate #include <sys/ttold.h>
530Sstevel@tonic-gate #include <sys/sockio.h>
540Sstevel@tonic-gate #include <sys/tty.h>
550Sstevel@tonic-gate #include <sys/ptyvar.h>
560Sstevel@tonic-gate #include <sys/resource.h>
570Sstevel@tonic-gate #include <sys/select.h>
580Sstevel@tonic-gate #include <sys/time.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <netinet/in.h>
610Sstevel@tonic-gate #include <arpa/inet.h>
620Sstevel@tonic-gate #include <priv_utils.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <stdio.h>
650Sstevel@tonic-gate #include <errno.h>
660Sstevel@tonic-gate #include <pwd.h>
670Sstevel@tonic-gate #include <signal.h>
680Sstevel@tonic-gate #include <setjmp.h>
690Sstevel@tonic-gate #include <netdb.h>
700Sstevel@tonic-gate #include <fcntl.h>
710Sstevel@tonic-gate #include <locale.h>
720Sstevel@tonic-gate #include <stdarg.h>
730Sstevel@tonic-gate #include <stdlib.h>
740Sstevel@tonic-gate #include <string.h>
750Sstevel@tonic-gate #include <unistd.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #include <k5-int.h>
780Sstevel@tonic-gate #include <profile/prof_int.h>
790Sstevel@tonic-gate #include <com_err.h>
800Sstevel@tonic-gate #include <kcmd.h>
810Sstevel@tonic-gate #include <krb5.h>
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /* signal disposition - signal handler or SIG_IGN, SIG_ERR, etc. */
840Sstevel@tonic-gate typedef void (*sigdisp_t)(int);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate extern errcode_t	profile_get_options_boolean(profile_t, char **,
870Sstevel@tonic-gate     profile_options_boolean *);
880Sstevel@tonic-gate extern errcode_t	profile_get_options_string(profile_t, char **,
890Sstevel@tonic-gate     profile_option_strings *);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define	RLOGIN_BUFSIZ	(1024 * 50)
920Sstevel@tonic-gate static char des_inbuf[2 * RLOGIN_BUFSIZ];
930Sstevel@tonic-gate 					/* needs to be > largest read size */
940Sstevel@tonic-gate static char des_outbuf[2 * RLOGIN_BUFSIZ];
950Sstevel@tonic-gate 					/* needs to be > largest write size */
960Sstevel@tonic-gate static krb5_data desinbuf, desoutbuf;
970Sstevel@tonic-gate static krb5_encrypt_block eblock;	/* eblock for encrypt/decrypt */
980Sstevel@tonic-gate static krb5_keyblock *session_key;
990Sstevel@tonic-gate static krb5_creds *cred;
1000Sstevel@tonic-gate static krb5_context bsd_context;
1010Sstevel@tonic-gate static krb5_auth_context auth_context;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static char *krb_realm;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static	int krb5auth_flag;	/* Flag set, when KERBEROS is enabled */
1060Sstevel@tonic-gate static	int fflag, Fflag;	/* Flag set, when option -f / -F used */
1070Sstevel@tonic-gate static	int encrypt_flag;	/* Flag set, when the "-x" option is used */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /* Flag set, if -PN / -PO is specified */
1100Sstevel@tonic-gate static boolean_t rcmdoption_done;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /* Flags set, if corres. cmd line options are turned on */
1130Sstevel@tonic-gate static boolean_t encrypt_done, fwd_done, fwdable_done;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static profile_options_boolean option[] = {
1160Sstevel@tonic-gate 	{ "encrypt", &encrypt_flag, 0 },
1170Sstevel@tonic-gate 	{ "forward", &fflag, 0 },
1180Sstevel@tonic-gate 	{ "forwardable", &Fflag, 0 },
1190Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static char *rcmdproto;
1230Sstevel@tonic-gate static profile_option_strings rcmdversion[] = {
1240Sstevel@tonic-gate 	{ "rcmd_protocol", &rcmdproto, 0 },
1250Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1260Sstevel@tonic-gate };
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static char rlogin[] = "rlogin";
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static char *realmdef[] = { "realms", NULL, rlogin, NULL };
1310Sstevel@tonic-gate static char *appdef[] = { "appdefaults", rlogin, NULL };
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate #ifndef TIOCPKT_WINDOW
1340Sstevel@tonic-gate #define	TIOCPKT_WINDOW 0x80
1350Sstevel@tonic-gate #endif /* TIOCPKT_WINDOW */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #ifndef sigmask
1380Sstevel@tonic-gate #define	sigmask(m)	(1 << ((m)-1))
1390Sstevel@tonic-gate #endif
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #define	set2mask(setp)	((setp)->__sigbits[0])
1420Sstevel@tonic-gate #define	mask2set(mask, setp) \
1430Sstevel@tonic-gate 	((mask) == -1 ? sigfillset(setp) : (((setp)->__sigbits[0]) = (mask)))
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate #ifdef DEBUG
1460Sstevel@tonic-gate #define	DEBUGOPTSTRING	"D:"
1470Sstevel@tonic-gate #else
1480Sstevel@tonic-gate #define	DEBUGOPTSTRING	""
1490Sstevel@tonic-gate #endif	/* DEBUG */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static	boolean_t ttcompat;
1520Sstevel@tonic-gate static	struct termios savetty;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static	char *host;
1550Sstevel@tonic-gate static	int port_number;
1560Sstevel@tonic-gate static	int rem = -1;
1570Sstevel@tonic-gate static	char cmdchar = '~';
1580Sstevel@tonic-gate static	boolean_t nocmdchar;
1590Sstevel@tonic-gate static	boolean_t eight;
1600Sstevel@tonic-gate static	boolean_t litout;
1610Sstevel@tonic-gate static	boolean_t null_local_username;
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Note that this list of speeds is shorter than the list of speeds
1640Sstevel@tonic-gate  * supported by termios.  This is because we can't be sure other rlogind's
1650Sstevel@tonic-gate  * in the world will correctly cope with values other than what 4.2/4.3BSD
1660Sstevel@tonic-gate  * supported.
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate static	char *speeds[] =
1690Sstevel@tonic-gate 	{ "0", "50", "75", "110", "134", "150", "200", "300",
1700Sstevel@tonic-gate 	    "600", "1200", "1800", "2400", "4800", "9600", "19200",
1710Sstevel@tonic-gate 	    "38400" };
1720Sstevel@tonic-gate static	char term[256] = "network";
1730Sstevel@tonic-gate static	void lostpeer(void);
1740Sstevel@tonic-gate static	boolean_t dosigwinch;
1750Sstevel@tonic-gate static	struct winsize winsize;
1760Sstevel@tonic-gate static	void sigwinch(int);
1770Sstevel@tonic-gate static	void oob(void);
1780Sstevel@tonic-gate static	void doit(int);
1790Sstevel@tonic-gate static	sigdisp_t sigdisp(int);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate #define	CRLF "\r\n"
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static	pid_t child;
1840Sstevel@tonic-gate static	void catchild(int);
1850Sstevel@tonic-gate /* LINTED */
1860Sstevel@tonic-gate static	void copytochild(int);
1870Sstevel@tonic-gate static	void writeroob(int);
1880Sstevel@tonic-gate static	void stop(char), echo(char);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate static	int defflags, tabflag;
1910Sstevel@tonic-gate static	int deflflags;
1920Sstevel@tonic-gate static	char deferase, defkill;
1930Sstevel@tonic-gate static	struct tchars deftc;
1940Sstevel@tonic-gate static	struct ltchars defltc;
1950Sstevel@tonic-gate static	struct tchars notc = { (char)-1, (char)-1, (char)-1,
1960Sstevel@tonic-gate 				(char)-1, (char)-1, (char)-1 };
1970Sstevel@tonic-gate static	struct ltchars noltc =	{ (char)-1, (char)-1, (char)-1,
1980Sstevel@tonic-gate 				(char)-1, (char)-1, (char)-1 };
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate static	void done(int);
2010Sstevel@tonic-gate static	void mode(int);
2020Sstevel@tonic-gate static	int reader(int);
2030Sstevel@tonic-gate static	void writer(void);
2040Sstevel@tonic-gate static	void prf(const char *, ...);
2050Sstevel@tonic-gate static	void sendwindow(void);
2060Sstevel@tonic-gate static	int compat_ioctl(int, int, void *);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static void
2090Sstevel@tonic-gate sigsetmask(int mask)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	sigset_t oset;
2120Sstevel@tonic-gate 	sigset_t nset;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	(void) sigprocmask(0, NULL, &nset);
2150Sstevel@tonic-gate 	mask2set(mask, &nset);
2160Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static int
2200Sstevel@tonic-gate sigblock(int mask)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	sigset_t oset;
2230Sstevel@tonic-gate 	sigset_t nset;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	(void) sigprocmask(0, NULL, &nset);
2260Sstevel@tonic-gate 	mask2set(mask, &nset);
2270Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
2280Sstevel@tonic-gate 	return (set2mask(&oset));
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static void
2320Sstevel@tonic-gate pop(int status) {
2330Sstevel@tonic-gate 	if (ttcompat) {
2340Sstevel@tonic-gate 		/*
2350Sstevel@tonic-gate 		 * Pop ttcompat module
2360Sstevel@tonic-gate 		 */
2370Sstevel@tonic-gate 		(void) ioctl(STDIN_FILENO, I_POP, 0);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 	(void) tcsetattr(STDIN_FILENO, TCSANOW, &savetty);
2400Sstevel@tonic-gate 	exit(status);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate static void
2440Sstevel@tonic-gate usage(void) {
2450Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n%s\n",
2460Sstevel@tonic-gate 	    gettext("usage: rlogin [-option] [-option...] "
2470Sstevel@tonic-gate 		"[-k realm] [-l username] host"),
2480Sstevel@tonic-gate 	    gettext("       where option is e, 8, E, L, A, a, x, "
2490Sstevel@tonic-gate 		"PN / PO, f or F"));
2500Sstevel@tonic-gate 	pop(EXIT_FAILURE);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate /* PRINTFLIKE(0) */
2540Sstevel@tonic-gate static void
2550Sstevel@tonic-gate die(const char *format, ...)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	va_list	ap;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	va_start(ap, format);
2600Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
2610Sstevel@tonic-gate 	va_end(ap);
2620Sstevel@tonic-gate 	usage();
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static void
2660Sstevel@tonic-gate usage_forward(void)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	die(gettext("rlogin: Only one of -f and -F allowed.\n"));
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate int
2720Sstevel@tonic-gate main(int argc, char **argv)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	int c;
2750Sstevel@tonic-gate 	char *cp, *cmd, *name = NULL;
2760Sstevel@tonic-gate 	struct passwd *pwd;
2770Sstevel@tonic-gate 	uid_t uid;
2780Sstevel@tonic-gate 	int options = 0, oldmask;
2790Sstevel@tonic-gate 	int on = 1;
2800Sstevel@tonic-gate 	speed_t speed = 0;
2810Sstevel@tonic-gate 	int getattr_ret;
2820Sstevel@tonic-gate 	char *tmp;
2830Sstevel@tonic-gate 	int sock;
2840Sstevel@tonic-gate 	krb5_flags authopts;
2850Sstevel@tonic-gate 	krb5_error_code status;
2860Sstevel@tonic-gate 	enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2910Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
2920Sstevel@tonic-gate #endif
2930Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (__init_suid_priv(0, PRIV_NET_PRIVADDR, NULL) == -1) {
2960Sstevel@tonic-gate 		(void) fprintf(stderr,
2970Sstevel@tonic-gate 		    gettext("Insufficient privileges, "
2980Sstevel@tonic-gate 			"rlogin must be set-uid root\n"));
2990Sstevel@tonic-gate 		exit(1);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	{
3030Sstevel@tonic-gate 		int it;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		if ((getattr_ret = tcgetattr(STDIN_FILENO, &savetty)) < 0)
3060Sstevel@tonic-gate 			perror("tcgetattr");
3070Sstevel@tonic-gate 		it = ioctl(STDIN_FILENO, I_FIND, "ttcompat");
3080Sstevel@tonic-gate 		if (it < 0) {
3090Sstevel@tonic-gate 			perror("ioctl I_FIND ttcompat");
3100Sstevel@tonic-gate 			return (EXIT_FAILURE);
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 		if (it == 0) {
3130Sstevel@tonic-gate 			if (ioctl(STDIN_FILENO, I_PUSH, "ttcompat") < 0) {
3140Sstevel@tonic-gate 				perror("ioctl I_PUSH ttcompat");
3150Sstevel@tonic-gate 				exit(EXIT_FAILURE);
3160Sstevel@tonic-gate 			}
3170Sstevel@tonic-gate 			ttcompat = B_TRUE;
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * Determine command name used to invoke to rlogin(1). Users can
3230Sstevel@tonic-gate 	 * create links named by a host pointing to the binary and type
3240Sstevel@tonic-gate 	 * "hostname" to log into that host afterwards.
3250Sstevel@tonic-gate 	 */
3260Sstevel@tonic-gate 	cmd = strrchr(argv[0], '/');
3270Sstevel@tonic-gate 	cmd = (cmd != NULL) ? (cmd + 1) : argv[0];
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if (strcmp(cmd, rlogin) == 0) {
3300Sstevel@tonic-gate 		if (argc < 2)
3310Sstevel@tonic-gate 			usage();
3320Sstevel@tonic-gate 		if (*argv[1] != '-') {
3330Sstevel@tonic-gate 			host = argv[1];
3340Sstevel@tonic-gate 			argc--;
3350Sstevel@tonic-gate 			argv[1] = argv[0];
3360Sstevel@tonic-gate 			argv++;
3370Sstevel@tonic-gate 		}
3380Sstevel@tonic-gate 	} else {
3390Sstevel@tonic-gate 		host = cmd;
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	while ((c = getopt(argc, argv,
3430Sstevel@tonic-gate 	    DEBUGOPTSTRING "8AEFLP:ade:fk:l:x")) != -1) {
3440Sstevel@tonic-gate 		switch (c) {
3450Sstevel@tonic-gate 		case '8':
3460Sstevel@tonic-gate 			eight = B_TRUE;
3470Sstevel@tonic-gate 			break;
3480Sstevel@tonic-gate 		case 'A':
3490Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
3500Sstevel@tonic-gate 			break;
3510Sstevel@tonic-gate #ifdef DEBUG
3520Sstevel@tonic-gate 		case 'D':
3530Sstevel@tonic-gate 			portnumber = htons(atoi(optarg));
3540Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
3550Sstevel@tonic-gate 			break;
3560Sstevel@tonic-gate #endif /* DEBUG */
3570Sstevel@tonic-gate 		case 'E':
3580Sstevel@tonic-gate 			nocmdchar = B_TRUE;
3590Sstevel@tonic-gate 			break;
3600Sstevel@tonic-gate 		case 'F':
3610Sstevel@tonic-gate 			if (fflag)
3620Sstevel@tonic-gate 				usage_forward();
3630Sstevel@tonic-gate 			Fflag = 1;
3640Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
3650Sstevel@tonic-gate 			fwdable_done = B_TRUE;
3660Sstevel@tonic-gate 			break;
3670Sstevel@tonic-gate 		case 'f':
3680Sstevel@tonic-gate 			if (Fflag)
3690Sstevel@tonic-gate 				usage_forward();
3700Sstevel@tonic-gate 			fflag = 1;
3710Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
3720Sstevel@tonic-gate 			fwd_done = B_TRUE;
3730Sstevel@tonic-gate 			break;
3740Sstevel@tonic-gate 		case 'L':
3750Sstevel@tonic-gate 			litout = B_TRUE;
3760Sstevel@tonic-gate 			break;
3770Sstevel@tonic-gate 		case 'P':
3780Sstevel@tonic-gate 			if (strcmp(optarg, "N") == 0)
3790Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
3800Sstevel@tonic-gate 			else if (strcmp(optarg, "O") == 0)
3810Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
3820Sstevel@tonic-gate 			else
3830Sstevel@tonic-gate 				die(gettext("rlogin: Only -PN or -PO "
3840Sstevel@tonic-gate 				    "allowed.\n"));
3850Sstevel@tonic-gate 			if (rcmdoption_done)
3860Sstevel@tonic-gate 				die(gettext("rlogin: Only one of -PN and -PO "
3870Sstevel@tonic-gate 				    "allowed.\n"));
3880Sstevel@tonic-gate 			rcmdoption_done = B_TRUE;
3890Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
3900Sstevel@tonic-gate 			break;
3910Sstevel@tonic-gate 		case 'a':
3920Sstevel@tonic-gate 		/*
3930Sstevel@tonic-gate 		 * Force the remote host to prompt for a password by sending
3940Sstevel@tonic-gate 		 * a NULL username. This option is mutually exclusive with
3950Sstevel@tonic-gate 		 * the -A, -x, -f, -F, -k <realm> options.
3960Sstevel@tonic-gate 		 */
3970Sstevel@tonic-gate 			null_local_username = B_TRUE;
3980Sstevel@tonic-gate 			break;
3990Sstevel@tonic-gate 		case 'd':
4000Sstevel@tonic-gate 			options |= SO_DEBUG;
4010Sstevel@tonic-gate 			break;
4020Sstevel@tonic-gate 		case 'e': {
4030Sstevel@tonic-gate 			int c;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 			cp = optarg;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 			if ((c = *cp) != '\\') {
4080Sstevel@tonic-gate 				cmdchar = c;
4090Sstevel@tonic-gate 			} else {
4100Sstevel@tonic-gate 				c = cp[1];
4110Sstevel@tonic-gate 				if (c == '\0' || c == '\\') {
4120Sstevel@tonic-gate 					cmdchar = '\\';
4130Sstevel@tonic-gate 				} else if (c >= '0' && c <= '7') {
4140Sstevel@tonic-gate 					long lc;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 					lc = strtol(&cp[1], NULL, 8);
4170Sstevel@tonic-gate 					if (lc < 0 || lc > 255)
4180Sstevel@tonic-gate 						die(gettext("rlogin: octal "
4190Sstevel@tonic-gate 						    "escape character %s too "
4200Sstevel@tonic-gate 						    "large.\n"), cp);
4210Sstevel@tonic-gate 					cmdchar = (char)lc;
4220Sstevel@tonic-gate 				} else {
4230Sstevel@tonic-gate 					die(gettext("rlogin: unrecognized "
4240Sstevel@tonic-gate 					    "escape character option %s.\n"),
4250Sstevel@tonic-gate 					    cp);
4260Sstevel@tonic-gate 				}
4270Sstevel@tonic-gate 			}
4280Sstevel@tonic-gate 			break;
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 		case 'k':
4310Sstevel@tonic-gate 			krb_realm = optarg;
4320Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
4330Sstevel@tonic-gate 			break;
4340Sstevel@tonic-gate 		case 'l':
4350Sstevel@tonic-gate 			name = optarg;
4360Sstevel@tonic-gate 			break;
4370Sstevel@tonic-gate 		case 'x':
4380Sstevel@tonic-gate 			encrypt_flag = 1;
4390Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
4400Sstevel@tonic-gate 			encrypt_done = B_TRUE;
4410Sstevel@tonic-gate 			break;
4420Sstevel@tonic-gate 		default:
4430Sstevel@tonic-gate 			usage();
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	argc -= optind;
4480Sstevel@tonic-gate 	argv += optind;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	if (host == NULL) {
4510Sstevel@tonic-gate 		if (argc == 0)
4520Sstevel@tonic-gate 			usage();
4530Sstevel@tonic-gate 		argc--;
4540Sstevel@tonic-gate 		host = *argv++;
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	if (argc > 0)
4580Sstevel@tonic-gate 		usage();
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	pwd = getpwuid(uid = getuid());
4610Sstevel@tonic-gate 	if (pwd == NULL) {
4620Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("getpwuid(): can not find "
4630Sstevel@tonic-gate 			"password entry for user id %d."), uid);
4640Sstevel@tonic-gate 		return (EXIT_FAILURE);
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 	if (name == NULL)
4670Sstevel@tonic-gate 		name = pwd->pw_name;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	/*
4700Sstevel@tonic-gate 	 * If the `-a' option is issued on the cmd line, we reset all
4710Sstevel@tonic-gate 	 * flags associated with other KRB5 specific options, since
4720Sstevel@tonic-gate 	 * the -a option is mutually exclusive with the rest.
4730Sstevel@tonic-gate 	 */
4740Sstevel@tonic-gate 	if (null_local_username) {
4750Sstevel@tonic-gate 		krb5auth_flag = B_FALSE;
4760Sstevel@tonic-gate 		fflag = Fflag = encrypt_flag = 0;
4770Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Note: The -a option nullifies "
4780Sstevel@tonic-gate 					"all other Kerberos-specific\noptions "
4790Sstevel@tonic-gate 					"you may have used.\n"));
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	if (krb5auth_flag) {
4830Sstevel@tonic-gate 		status = krb5_init_context(&bsd_context);
4840Sstevel@tonic-gate 		if (status) {
4850Sstevel@tonic-gate 			com_err(rlogin, status, gettext("while initializing"
4860Sstevel@tonic-gate 					" krb5"));
4870Sstevel@tonic-gate 			return (EXIT_FAILURE);
4880Sstevel@tonic-gate 		}
4890Sstevel@tonic-gate 		/*
4900Sstevel@tonic-gate 		 * Set up buffers for desread and deswrite.
4910Sstevel@tonic-gate 		 */
4920Sstevel@tonic-gate 		desinbuf.data = des_inbuf;
4930Sstevel@tonic-gate 		desoutbuf.data = des_outbuf;
4940Sstevel@tonic-gate 		desinbuf.length = sizeof (des_inbuf);
4950Sstevel@tonic-gate 		desoutbuf.length = sizeof (des_outbuf);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 		/*
4980Sstevel@tonic-gate 		 * Get our local realm to look up local realm options.
4990Sstevel@tonic-gate 		 */
5000Sstevel@tonic-gate 		status = krb5_get_default_realm(bsd_context, &realmdef[1]);
5010Sstevel@tonic-gate 		if (status) {
5020Sstevel@tonic-gate 			com_err(rlogin, status,
5030Sstevel@tonic-gate 				gettext("while getting default realm"));
5040Sstevel@tonic-gate 			return (EXIT_FAILURE);
5050Sstevel@tonic-gate 		}
5060Sstevel@tonic-gate 		/*
5070Sstevel@tonic-gate 		 * Check the realms section in krb5.conf for encryption,
5080Sstevel@tonic-gate 		 * forward & forwardable info
5090Sstevel@tonic-gate 		 */
5100Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, realmdef,
5110Sstevel@tonic-gate 						option);
5120Sstevel@tonic-gate 		/*
5130Sstevel@tonic-gate 		 * Check the appdefaults section
5140Sstevel@tonic-gate 		 */
5150Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, appdef,
5160Sstevel@tonic-gate 						option);
5170Sstevel@tonic-gate 		profile_get_options_string(bsd_context->profile, appdef,
5180Sstevel@tonic-gate 						rcmdversion);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		/*
5210Sstevel@tonic-gate 		 * Set the *_flag variables, if the corresponding *_done are
5220Sstevel@tonic-gate 		 * set to 1, because we dont want the config file values
5230Sstevel@tonic-gate 		 * overriding the command line options.
5240Sstevel@tonic-gate 		 */
5250Sstevel@tonic-gate 		if (encrypt_done)
5260Sstevel@tonic-gate 			encrypt_flag = 1;
5270Sstevel@tonic-gate 		if (fwd_done) {
5280Sstevel@tonic-gate 			fflag = 1;
5290Sstevel@tonic-gate 			Fflag = 0;
5300Sstevel@tonic-gate 		} else if (fwdable_done) {
5310Sstevel@tonic-gate 			Fflag = 1;
5320Sstevel@tonic-gate 			fflag = 0;
5330Sstevel@tonic-gate 		}
5340Sstevel@tonic-gate 		if (!rcmdoption_done && (rcmdproto != NULL)) {
5350Sstevel@tonic-gate 			if (strncmp(rcmdproto, "rcmdv2", 6) == 0) {
5360Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
5370Sstevel@tonic-gate 			} else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) {
5380Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
5390Sstevel@tonic-gate 			} else {
5400Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Unrecognized "
5410Sstevel@tonic-gate 					"KCMD protocol (%s)"), rcmdproto);
5420Sstevel@tonic-gate 				return (EXIT_FAILURE);
5430Sstevel@tonic-gate 			}
5440Sstevel@tonic-gate 		}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 		if (encrypt_flag && (!krb5_privacy_allowed())) {
5470Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rlogin: "
5480Sstevel@tonic-gate 					"Encryption not supported.\n"));
5490Sstevel@tonic-gate 			return (EXIT_FAILURE);
5500Sstevel@tonic-gate 		}
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	if (port_number == 0) {
5540Sstevel@tonic-gate 		if (krb5auth_flag) {
5550Sstevel@tonic-gate 			struct servent *sp;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 			/*
5580Sstevel@tonic-gate 			 * If the krb5auth_flag is set (via -A, -f, -F, -k) &
5590Sstevel@tonic-gate 			 * if there is an entry in /etc/services for Kerberos
5600Sstevel@tonic-gate 			 * login, attempt to login with Kerberos. If we fail
5610Sstevel@tonic-gate 			 * at any step,  use the standard rlogin
5620Sstevel@tonic-gate 			 */
5630Sstevel@tonic-gate 			sp = getservbyname(encrypt_flag ?
5640Sstevel@tonic-gate 			    "eklogin" : "klogin", "tcp");
5650Sstevel@tonic-gate 			if (sp == NULL) {
5660Sstevel@tonic-gate 				port_number = encrypt_flag ?
5670Sstevel@tonic-gate 				    htons(2105) : htons(543);
5680Sstevel@tonic-gate 			} else {
5690Sstevel@tonic-gate 				port_number = sp->s_port;
5700Sstevel@tonic-gate 			}
5710Sstevel@tonic-gate 		} else {
5720Sstevel@tonic-gate 			port_number = htons(IPPORT_LOGINSERVER);
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	cp = getenv("TERM");
5770Sstevel@tonic-gate 	if (cp) {
5780Sstevel@tonic-gate 		(void) strncpy(term, cp, sizeof (term));
5790Sstevel@tonic-gate 		term[sizeof (term) - 1] = '\0';
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate 	if (getattr_ret == 0) {
5820Sstevel@tonic-gate 		speed = cfgetospeed(&savetty);
5830Sstevel@tonic-gate 		/*
5840Sstevel@tonic-gate 		 * "Be conservative in what we send" -- Only send baud rates
5850Sstevel@tonic-gate 		 * which at least all 4.x BSD derivatives are known to handle
5860Sstevel@tonic-gate 		 * correctly.
5870Sstevel@tonic-gate 		 * NOTE:  This code assumes new termios speed values will
5880Sstevel@tonic-gate 		 * be "higher" speeds.
5890Sstevel@tonic-gate 		 */
5900Sstevel@tonic-gate 		if (speed > B38400)
5910Sstevel@tonic-gate 			speed = B38400;
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	/*
5950Sstevel@tonic-gate 	 * Only put the terminal speed info in if we have room
5960Sstevel@tonic-gate 	 * so we don't overflow the buffer, and only if we have
5970Sstevel@tonic-gate 	 * a speed we recognize.
5980Sstevel@tonic-gate 	 */
5990Sstevel@tonic-gate 	if (speed > 0 && speed < sizeof (speeds)/sizeof (char *) &&
6000Sstevel@tonic-gate 	    strlen(term) + strlen("/") + strlen(speeds[speed]) + 1 <
6010Sstevel@tonic-gate 	    sizeof (term)) {
6020Sstevel@tonic-gate 		(void) strcat(term, "/");
6030Sstevel@tonic-gate 		(void) strcat(term, speeds[speed]);
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 	(void) sigset(SIGPIPE, (sigdisp_t)lostpeer);
6060Sstevel@tonic-gate 	/* will use SIGUSR1 for window size hack, so hold it off */
6070Sstevel@tonic-gate 	oldmask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	/*
6100Sstevel@tonic-gate 	 * Determine if v4 literal address and if so store it to one
6110Sstevel@tonic-gate 	 * side. This is to correct the undesired behaviour of rcmd_af
6120Sstevel@tonic-gate 	 * which converts a passed in v4 literal address to a v4 mapped
6130Sstevel@tonic-gate 	 * v6 literal address. If it was a v4 literal we then re-assign
6140Sstevel@tonic-gate 	 * it to host.
6150Sstevel@tonic-gate 	 */
6160Sstevel@tonic-gate 	tmp = NULL;
6170Sstevel@tonic-gate 	if (inet_addr(host) != (in_addr_t)-1)
6180Sstevel@tonic-gate 		tmp = host;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	if (krb5auth_flag) {
6210Sstevel@tonic-gate 		authopts = AP_OPTS_MUTUAL_REQUIRED;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		/* Piggy-back forwarding flags on top of authopts; */
6240Sstevel@tonic-gate 		/* they will be reset in kcmd */
6250Sstevel@tonic-gate 		if (fflag || Fflag)
6260Sstevel@tonic-gate 			authopts |= OPTS_FORWARD_CREDS;
6270Sstevel@tonic-gate 		if (Fflag)
6280Sstevel@tonic-gate 			authopts |= OPTS_FORWARDABLE_CREDS;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 		status = kcmd(&sock, &host, port_number,
6310Sstevel@tonic-gate 			null_local_username ? "" : pwd->pw_name,
6320Sstevel@tonic-gate 			name, term, NULL,
6330Sstevel@tonic-gate 			"host", krb_realm, bsd_context, &auth_context,
6340Sstevel@tonic-gate 			&cred,
6350Sstevel@tonic-gate 			NULL,		/* No need for sequence number */
6360Sstevel@tonic-gate 			NULL,		/* No need for server seq # */
6370Sstevel@tonic-gate 			authopts,
6380Sstevel@tonic-gate 			0,		/* Not any port # */
6390Sstevel@tonic-gate 			&kcmd_proto);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		if (status != 0) {
6420Sstevel@tonic-gate 			/*
6430Sstevel@tonic-gate 			 * If new protocol requested, we dont fallback to
6440Sstevel@tonic-gate 			 * less secure ones.
6450Sstevel@tonic-gate 			 */
6460Sstevel@tonic-gate 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
6470Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rlogin: kcmdv2 "
6480Sstevel@tonic-gate 					"to host %s failed - %s\n"
6490Sstevel@tonic-gate 					"Fallback to normal rlogin denied."),
6500Sstevel@tonic-gate 					host, error_message(status));
6510Sstevel@tonic-gate 				return (EXIT_FAILURE);
6520Sstevel@tonic-gate 			}
6530Sstevel@tonic-gate 			if (status != -1) {
6540Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rlogin: kcmd "
6550Sstevel@tonic-gate 						"to host %s failed - %s,\n"
6560Sstevel@tonic-gate 						"trying normal rlogin...\n\n"),
6570Sstevel@tonic-gate 						host, error_message(status));
6580Sstevel@tonic-gate 			} else {
6590Sstevel@tonic-gate 				(void) fprintf(stderr,
6600Sstevel@tonic-gate 					gettext("trying normal rlogin...\n"));
6610Sstevel@tonic-gate 			}
6620Sstevel@tonic-gate 			/*
6630Sstevel@tonic-gate 			 * kcmd() failed, so we have to
6640Sstevel@tonic-gate 			 * fallback to normal rlogin
6650Sstevel@tonic-gate 			 */
6660Sstevel@tonic-gate 			port_number = htons(IPPORT_LOGINSERVER);
6670Sstevel@tonic-gate 			krb5auth_flag = B_FALSE;
6680Sstevel@tonic-gate 			fflag = Fflag = encrypt_flag = 0;
6690Sstevel@tonic-gate 			null_local_username = B_FALSE;
6700Sstevel@tonic-gate 		} else {
6710Sstevel@tonic-gate 			(void) fprintf(stderr,
6720Sstevel@tonic-gate 			    gettext("connected with Kerberos V5\n"));
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 			/*
6750Sstevel@tonic-gate 			 * Setup eblock for desread and deswrite.
6760Sstevel@tonic-gate 			 */
6770Sstevel@tonic-gate 			session_key = &cred->keyblock;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
6800Sstevel@tonic-gate 				status = krb5_auth_con_getlocalsubkey(
6810Sstevel@tonic-gate 				    bsd_context,
6820Sstevel@tonic-gate 				    auth_context,
6830Sstevel@tonic-gate 				    &session_key);
6840Sstevel@tonic-gate 				if (status) {
6850Sstevel@tonic-gate 					com_err(rlogin, status,
6860Sstevel@tonic-gate 					    "determining subkey for session");
6870Sstevel@tonic-gate 					return (EXIT_FAILURE);
6880Sstevel@tonic-gate 				}
6890Sstevel@tonic-gate 				if (session_key == NULL) {
6900Sstevel@tonic-gate 					com_err(rlogin, 0,
6910Sstevel@tonic-gate 					    "no subkey negotiated for "
6920Sstevel@tonic-gate 					    "connection");
6930Sstevel@tonic-gate 					return (EXIT_FAILURE);
6940Sstevel@tonic-gate 				}
6950Sstevel@tonic-gate 			}
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 			eblock.crypto_entry = session_key->enctype;
6980Sstevel@tonic-gate 			eblock.key = (krb5_keyblock *)session_key;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 			init_encrypt(encrypt_flag, bsd_context, kcmd_proto,
7010Sstevel@tonic-gate 			    &desinbuf, &desoutbuf, CLIENT, &eblock);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 			rem = sock;
7040Sstevel@tonic-gate 			if (rem < 0)
7050Sstevel@tonic-gate 				pop(EXIT_FAILURE);
7060Sstevel@tonic-gate 		}
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	/*
7100Sstevel@tonic-gate 	 * Don't merge this with the "if" statement above because
7110Sstevel@tonic-gate 	 * "krb5auth_flag" might be set to false inside it.
7120Sstevel@tonic-gate 	 */
7130Sstevel@tonic-gate 	if (!krb5auth_flag) {
7140Sstevel@tonic-gate 		rem = rcmd_af(&host, port_number,
7150Sstevel@tonic-gate 			null_local_username ? "" : pwd->pw_name,
7160Sstevel@tonic-gate 			name, term, NULL, AF_INET6);
7170Sstevel@tonic-gate 		if (rem < 0)
7180Sstevel@tonic-gate 			pop(EXIT_FAILURE);
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/* Never need our privilege again */
7220Sstevel@tonic-gate 	__priv_relinquish();
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	if (tmp != NULL)
7250Sstevel@tonic-gate 		host = tmp;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	if (options & SO_DEBUG &&
7280Sstevel@tonic-gate 	    setsockopt(rem, SOL_SOCKET, SO_DEBUG, (char *)&on,
7290Sstevel@tonic-gate 			    sizeof (on)) < 0)
7300Sstevel@tonic-gate 		perror("rlogin: setsockopt (SO_DEBUG)");
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	{
7330Sstevel@tonic-gate 		int bufsize = 8192;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		(void) setsockopt(rem, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize,
7360Sstevel@tonic-gate 			sizeof (int));
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	doit(oldmask);
740473Sbw 	return (0);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate static void
7440Sstevel@tonic-gate doit(int oldmask)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	struct sgttyb sb;
7470Sstevel@tonic-gate 	int atmark;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
7500Sstevel@tonic-gate 		perror("ioctl TIOCGETP");
7510Sstevel@tonic-gate 	defflags = sb.sg_flags;
7520Sstevel@tonic-gate 	tabflag = defflags & O_TBDELAY;
7530Sstevel@tonic-gate 	defflags &= ECHO | O_CRMOD;
7540Sstevel@tonic-gate 	deferase = sb.sg_erase;
7550Sstevel@tonic-gate 	defkill = sb.sg_kill;
7560Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCLGET, (char *)&deflflags) == -1)
7570Sstevel@tonic-gate 		perror("ioctl TIOCLGET");
7580Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCGETC, (char *)&deftc) == -1)
7590Sstevel@tonic-gate 		perror("ioctl TIOCGETC");
7600Sstevel@tonic-gate 	notc.t_startc = deftc.t_startc;
7610Sstevel@tonic-gate 	notc.t_stopc = deftc.t_stopc;
7620Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCGLTC, (char *)&defltc) == -1)
7630Sstevel@tonic-gate 		perror("ioctl TIOCGLTC");
7640Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
7650Sstevel@tonic-gate 	if (sigdisp(SIGHUP) != SIG_IGN)
7660Sstevel@tonic-gate 		(void) sigset(SIGHUP, exit);
7670Sstevel@tonic-gate 	if (sigdisp(SIGQUIT) != SIG_IGN)
7680Sstevel@tonic-gate 		(void) sigset(SIGQUIT, exit);
7690Sstevel@tonic-gate 	child = fork();
7700Sstevel@tonic-gate 	if (child == (pid_t)-1) {
7710Sstevel@tonic-gate 		perror("rlogin: fork");
7720Sstevel@tonic-gate 		done(EXIT_FAILURE);
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	if (child == 0) {
7750Sstevel@tonic-gate 		mode(1);
7760Sstevel@tonic-gate 		if (reader(oldmask) == 0) {
7770Sstevel@tonic-gate 			prf(gettext("Connection to %.*s closed."),
7780Sstevel@tonic-gate 			    MAXHOSTNAMELEN, host);
7790Sstevel@tonic-gate 			exit(EXIT_SUCCESS);
7800Sstevel@tonic-gate 		}
7810Sstevel@tonic-gate 		(void) sleep(1);
7820Sstevel@tonic-gate 		prf(gettext("\aConnection to %.*s closed."),
7830Sstevel@tonic-gate 		    MAXHOSTNAMELEN, host);
7840Sstevel@tonic-gate 		exit(EXIT_FAILURE);
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	/*
7880Sstevel@tonic-gate 	 * We may still own the socket, and may have a pending SIGURG (or might
7890Sstevel@tonic-gate 	 * receive one soon) that we really want to send to the reader.  Set a
7900Sstevel@tonic-gate 	 * trap that simply copies such signals to the child.
7910Sstevel@tonic-gate 	 */
7920Sstevel@tonic-gate #ifdef F_SETOWN_BUG_FIXED
7930Sstevel@tonic-gate 	(void) sigset(SIGURG, copytochild);
7940Sstevel@tonic-gate #else
7950Sstevel@tonic-gate 	(void) sigset(SIGURG, SIG_IGN);
7960Sstevel@tonic-gate #endif /* F_SETOWN_BUG_FIXED */
7970Sstevel@tonic-gate 	(void) sigset(SIGUSR1, writeroob);
7980Sstevel@tonic-gate 	/*
7990Sstevel@tonic-gate 	 * Of course, if the urgent byte already arrived, allowing SIGURG
8000Sstevel@tonic-gate 	 * won't get us notification.  So, we check to see if we've got
8010Sstevel@tonic-gate 	 * an urgent byte.  If so, force a call to writeroob() to pretend
8020Sstevel@tonic-gate 	 * we got SIGURG.
8030Sstevel@tonic-gate 	 */
8040Sstevel@tonic-gate 	if (ioctl(rem, SIOCATMARK, &atmark) >= 0) {
8050Sstevel@tonic-gate 		if (atmark)
8060Sstevel@tonic-gate 			writeroob(0);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 	sigsetmask(oldmask);
8090Sstevel@tonic-gate 	(void) sigset(SIGCHLD, catchild);
8100Sstevel@tonic-gate 	writer();
8110Sstevel@tonic-gate 	prf(gettext("Closed connection to %.*s."), MAXHOSTNAMELEN, host);
8120Sstevel@tonic-gate 	done(EXIT_SUCCESS);
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate /*
8160Sstevel@tonic-gate  * Get signal disposition (or signal handler) for a given signal
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate static sigdisp_t
8190Sstevel@tonic-gate sigdisp(int sig)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate 	struct sigaction act;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	act.sa_handler = NULL;
8240Sstevel@tonic-gate 	act.sa_flags = 0;
8250Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
8260Sstevel@tonic-gate 	(void) sigaction(sig, NULL, &act);
8270Sstevel@tonic-gate 	return (act.sa_handler);
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate static void
8310Sstevel@tonic-gate done(int status)
8320Sstevel@tonic-gate {
8330Sstevel@tonic-gate 	pid_t w;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	mode(0);
8360Sstevel@tonic-gate 	if (child > 0) {
8370Sstevel@tonic-gate 		/* make sure catchild does not snap it up */
8380Sstevel@tonic-gate 		(void) sigset(SIGCHLD, SIG_DFL);
8390Sstevel@tonic-gate 		if (kill(child, SIGKILL) >= 0)
8400Sstevel@tonic-gate 			while ((w = wait(0)) > (pid_t)0 && w != child)
8410Sstevel@tonic-gate 				/* void */;
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 	pop(status);
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate  * Copy SIGURGs to the child process.
8480Sstevel@tonic-gate  */
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate /* ARGSUSED */
8510Sstevel@tonic-gate static void
8520Sstevel@tonic-gate copytochild(int signum)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	(void) kill(child, SIGURG);
8560Sstevel@tonic-gate }
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate /*
8590Sstevel@tonic-gate  * This is called when the reader process gets the out-of-band (urgent)
8600Sstevel@tonic-gate  * request to turn on the window-changing protocol.
8610Sstevel@tonic-gate  */
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate /* ARGSUSED */
8640Sstevel@tonic-gate static void
8650Sstevel@tonic-gate writeroob(int signum)
8660Sstevel@tonic-gate {
8670Sstevel@tonic-gate 	int mask;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	if (!dosigwinch) {
8700Sstevel@tonic-gate 		/*
8710Sstevel@tonic-gate 		 * Start tracking window size.  It doesn't matter which
8720Sstevel@tonic-gate 		 * order the next two are in, because we'll be unconditionally
8730Sstevel@tonic-gate 		 * sending a size notification in a moment.
8740Sstevel@tonic-gate 		 */
8750Sstevel@tonic-gate 		(void) sigset(SIGWINCH, sigwinch);
8760Sstevel@tonic-gate 		dosigwinch = B_TRUE;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 		/*
8790Sstevel@tonic-gate 		 * It would be bad if a SIGWINCH came in between the ioctl
8800Sstevel@tonic-gate 		 * and sending the data.  It could result in the SIGWINCH
8810Sstevel@tonic-gate 		 * handler sending a good message, and then us sending an
8820Sstevel@tonic-gate 		 * outdated or inconsistent message.
8830Sstevel@tonic-gate 		 *
8840Sstevel@tonic-gate 		 * Instead, if the change is made before the
8850Sstevel@tonic-gate 		 * ioctl, the sigwinch handler will send a size message
8860Sstevel@tonic-gate 		 * and we'll send another, identical, one.  If the change
8870Sstevel@tonic-gate 		 * is made after the ioctl, we'll send a message with the
8880Sstevel@tonic-gate 		 * old value, and then the sigwinch handler will send
8890Sstevel@tonic-gate 		 * a revised, correct one.
8900Sstevel@tonic-gate 		 */
8910Sstevel@tonic-gate 		mask = sigblock(sigmask(SIGWINCH));
8920Sstevel@tonic-gate 		if (ioctl(STDIN_FILENO, TIOCGWINSZ, &winsize) == 0)
8930Sstevel@tonic-gate 			sendwindow();
8940Sstevel@tonic-gate 		sigsetmask(mask);
8950Sstevel@tonic-gate 	}
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate /* ARGSUSED */
8990Sstevel@tonic-gate static void
9000Sstevel@tonic-gate catchild(int signum)
9010Sstevel@tonic-gate {
9020Sstevel@tonic-gate 	int options;
9030Sstevel@tonic-gate 	siginfo_t	info;
9040Sstevel@tonic-gate 	int error;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	for (;;) {
9070Sstevel@tonic-gate 		options = WNOHANG | WEXITED;
9080Sstevel@tonic-gate 		error = waitid(P_ALL, 0, &info, options);
9090Sstevel@tonic-gate 		if (error != 0)
9100Sstevel@tonic-gate 			return;
9110Sstevel@tonic-gate 		if (info.si_pid == 0)
9120Sstevel@tonic-gate 			return;
9130Sstevel@tonic-gate 		if (info.si_code == CLD_TRAPPED)
9140Sstevel@tonic-gate 			continue;
9150Sstevel@tonic-gate 		if (info.si_code == CLD_STOPPED)
9160Sstevel@tonic-gate 			continue;
9170Sstevel@tonic-gate 		done(info.si_status);
9180Sstevel@tonic-gate 	}
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate /*
9220Sstevel@tonic-gate  * writer: write to remote: 0 -> line.
9230Sstevel@tonic-gate  * ~.	terminate
9240Sstevel@tonic-gate  * ~^Z	suspend rlogin process.
9250Sstevel@tonic-gate  * ~^Y  suspend rlogin process, but leave reader alone.
9260Sstevel@tonic-gate  */
9270Sstevel@tonic-gate static void
9280Sstevel@tonic-gate writer(void)
9290Sstevel@tonic-gate {
9300Sstevel@tonic-gate 	char c;
9310Sstevel@tonic-gate 	int n;
9320Sstevel@tonic-gate 	boolean_t bol = B_TRUE;		/* beginning of line */
9330Sstevel@tonic-gate 	boolean_t local = B_FALSE;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	for (;;) {
9360Sstevel@tonic-gate 		n = read(STDIN_FILENO, &c, 1);
9370Sstevel@tonic-gate 		if (n <= 0) {
9380Sstevel@tonic-gate 			if (n == 0)
9390Sstevel@tonic-gate 				break;
9400Sstevel@tonic-gate 			if (errno == EINTR)
9410Sstevel@tonic-gate 				continue;
9420Sstevel@tonic-gate 			else {
9430Sstevel@tonic-gate 				prf(gettext("Read error from terminal: %s"),
944*634Sdp 				    strerror(errno));
9450Sstevel@tonic-gate 				break;
9460Sstevel@tonic-gate 			}
9470Sstevel@tonic-gate 		}
9480Sstevel@tonic-gate 		/*
9490Sstevel@tonic-gate 		 * If we're at the beginning of the line
9500Sstevel@tonic-gate 		 * and recognize a command character, then
9510Sstevel@tonic-gate 		 * we echo locally.  Otherwise, characters
9520Sstevel@tonic-gate 		 * are echo'd remotely.  If the command
9530Sstevel@tonic-gate 		 * character is doubled, this acts as a
9540Sstevel@tonic-gate 		 * force and local echo is suppressed.
9550Sstevel@tonic-gate 		 */
9560Sstevel@tonic-gate 		if (bol && !nocmdchar) {
9570Sstevel@tonic-gate 			bol = B_FALSE;
9580Sstevel@tonic-gate 			if (c == cmdchar) {
9590Sstevel@tonic-gate 				local = B_TRUE;
9600Sstevel@tonic-gate 				continue;
9610Sstevel@tonic-gate 			}
9620Sstevel@tonic-gate 		} else if (local) {
9630Sstevel@tonic-gate 			local = B_FALSE;
9640Sstevel@tonic-gate 			if (c == '.' || c == deftc.t_eofc) {
9650Sstevel@tonic-gate 				echo(c);
9660Sstevel@tonic-gate 				break;
9670Sstevel@tonic-gate 			}
9680Sstevel@tonic-gate 			if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
9690Sstevel@tonic-gate 				bol = B_TRUE;
9700Sstevel@tonic-gate 				echo(c);
9710Sstevel@tonic-gate 				stop(c);
9720Sstevel@tonic-gate 				continue;
9730Sstevel@tonic-gate 			}
9740Sstevel@tonic-gate 			if (c != cmdchar) {
9750Sstevel@tonic-gate 				if (deswrite(rem, &cmdchar, 1, 0) < 0) {
9760Sstevel@tonic-gate 					prf(gettext(
9770Sstevel@tonic-gate 					    "Write error to network: %s"),
978*634Sdp 					    strerror(errno));
9790Sstevel@tonic-gate 					break;
9800Sstevel@tonic-gate 				}
9810Sstevel@tonic-gate 			}
9820Sstevel@tonic-gate 		}
9830Sstevel@tonic-gate 		if ((n = deswrite(rem, &c, 1, 0)) <= 0) {
9840Sstevel@tonic-gate 			if (n == 0)
9850Sstevel@tonic-gate 				prf(gettext("line gone"));
9860Sstevel@tonic-gate 			else
9870Sstevel@tonic-gate 				prf(gettext("Write error to network: %s"),
988*634Sdp 				    strerror(errno));
9890Sstevel@tonic-gate 			break;
9900Sstevel@tonic-gate 		}
9910Sstevel@tonic-gate 		bol = c == defkill || c == deftc.t_eofc ||
9920Sstevel@tonic-gate 		    c == deftc.t_intrc || c == defltc.t_suspc ||
9930Sstevel@tonic-gate 		    c == '\r' || c == '\n';
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate static void
9980Sstevel@tonic-gate echo(char c)
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate 	char buf[8];
10010Sstevel@tonic-gate 	char *p = buf;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	c &= 0177;
10040Sstevel@tonic-gate 	*p++ = cmdchar;
10050Sstevel@tonic-gate 	if (c < ' ') {
10060Sstevel@tonic-gate 		*p++ = '^';
10070Sstevel@tonic-gate 		*p++ = c + '@';
10080Sstevel@tonic-gate 	} else if (c == 0177) {
10090Sstevel@tonic-gate 		*p++ = '^';
10100Sstevel@tonic-gate 		*p++ = '?';
10110Sstevel@tonic-gate 	} else
10120Sstevel@tonic-gate 		*p++ = c;
10130Sstevel@tonic-gate 	*p++ = '\r';
10140Sstevel@tonic-gate 	*p++ = '\n';
10150Sstevel@tonic-gate 	if (write(STDOUT_FILENO, buf, p - buf) < 0)
1016*634Sdp 		prf(gettext("Write error to terminal: %s"), strerror(errno));
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate static void
10200Sstevel@tonic-gate stop(char cmdc)
10210Sstevel@tonic-gate {
10220Sstevel@tonic-gate 	mode(0);
10230Sstevel@tonic-gate 	(void) sigset(SIGCHLD, SIG_IGN);
10240Sstevel@tonic-gate 	(void) kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
10250Sstevel@tonic-gate 	(void) sigset(SIGCHLD, catchild);
10260Sstevel@tonic-gate 	mode(1);
10270Sstevel@tonic-gate 	sigwinch(0);			/* check for size changes */
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate /* ARGSUSED */
10310Sstevel@tonic-gate static void
10320Sstevel@tonic-gate sigwinch(int signum)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	struct winsize ws;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	if (dosigwinch && ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0 &&
10370Sstevel@tonic-gate 	    memcmp(&winsize, &ws, sizeof (ws)) != 0) {
10380Sstevel@tonic-gate 		winsize = ws;
10390Sstevel@tonic-gate 		sendwindow();
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate /*
10440Sstevel@tonic-gate  * Send the window size to the server via the magic escape.
10450Sstevel@tonic-gate  * Note:  SIGWINCH should be blocked when this is called, lest
10460Sstevel@tonic-gate  * winsize change underneath us and chaos result.
10470Sstevel@tonic-gate  */
10480Sstevel@tonic-gate static void
10490Sstevel@tonic-gate sendwindow(void)
10500Sstevel@tonic-gate {
10510Sstevel@tonic-gate 	char obuf[4 + sizeof (struct winsize)];
10520Sstevel@tonic-gate 	struct winsize *wp = (struct winsize *)(void *)(obuf+4);
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	obuf[0] = -1;
10550Sstevel@tonic-gate 	obuf[1] = -1;
10560Sstevel@tonic-gate 	obuf[2] = 's';
10570Sstevel@tonic-gate 	obuf[3] = 's';
10580Sstevel@tonic-gate 	wp->ws_row = htons(winsize.ws_row);
10590Sstevel@tonic-gate 	wp->ws_col = htons(winsize.ws_col);
10600Sstevel@tonic-gate 	wp->ws_xpixel = htons(winsize.ws_xpixel);
10610Sstevel@tonic-gate 	wp->ws_ypixel = htons(winsize.ws_ypixel);
10620Sstevel@tonic-gate 	if (deswrite(rem, obuf, sizeof (obuf), 0) < 0)
1063*634Sdp 		prf(gettext("Write error to network: %s"), strerror(errno));
10640Sstevel@tonic-gate }
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate /*
10680Sstevel@tonic-gate  * reader: read from remote: remote -> stdout
10690Sstevel@tonic-gate  */
10700Sstevel@tonic-gate #define	READING	1
10710Sstevel@tonic-gate #define	WRITING	2
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate static	char rcvbuf[8 * 1024];
10740Sstevel@tonic-gate static	int rcvcnt;
10750Sstevel@tonic-gate static	int rcvstate;
10760Sstevel@tonic-gate static	pid_t ppid;
10770Sstevel@tonic-gate static	jmp_buf rcvtop;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate static void
10800Sstevel@tonic-gate oob(void)
10810Sstevel@tonic-gate {
10820Sstevel@tonic-gate 	int out = FWRITE, atmark, n;
10830Sstevel@tonic-gate 	int rcvd = 0;
10840Sstevel@tonic-gate 	char waste[4*BUFSIZ], mark;
10850Sstevel@tonic-gate 	struct sgttyb sb;
10860Sstevel@tonic-gate 	fd_set exceptfds;
10870Sstevel@tonic-gate 	struct timeval tv;
10880Sstevel@tonic-gate 	int ret;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	FD_ZERO(&exceptfds);
10910Sstevel@tonic-gate 	FD_SET(rem, &exceptfds);
10920Sstevel@tonic-gate 	timerclear(&tv);
10930Sstevel@tonic-gate 	ret = select(rem+1, NULL, NULL, &exceptfds, &tv);
10940Sstevel@tonic-gate 	/*
10950Sstevel@tonic-gate 	 * We may get an extra signal at start up time since we are trying
10960Sstevel@tonic-gate 	 * to take all precautions not to miss the urgent byte. This
10970Sstevel@tonic-gate 	 * means we may get here without any urgent data to process, in which
10980Sstevel@tonic-gate 	 * case we do nothing and just return.
10990Sstevel@tonic-gate 	 */
11000Sstevel@tonic-gate 	if (ret <= 0)
11010Sstevel@tonic-gate 		return;
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	do {
11040Sstevel@tonic-gate 		if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
11050Sstevel@tonic-gate 			break;
11060Sstevel@tonic-gate 		}
11070Sstevel@tonic-gate 		if (!atmark) {
11080Sstevel@tonic-gate 			/*
11090Sstevel@tonic-gate 			 * Urgent data not here yet.
11100Sstevel@tonic-gate 			 * It may not be possible to send it yet
11110Sstevel@tonic-gate 			 * if we are blocked for output
11120Sstevel@tonic-gate 			 * and our input buffer is full.
11130Sstevel@tonic-gate 			 */
11140Sstevel@tonic-gate 			if (rcvcnt < sizeof (rcvbuf)) {
11150Sstevel@tonic-gate 				n = desread(rem, rcvbuf + rcvcnt,
11160Sstevel@tonic-gate 					sizeof (rcvbuf) - rcvcnt, 0);
11170Sstevel@tonic-gate 				if (n <= 0)
11180Sstevel@tonic-gate 					return;
11190Sstevel@tonic-gate 				rcvd += n;
11200Sstevel@tonic-gate 				rcvcnt += n;
11210Sstevel@tonic-gate 			} else {
11220Sstevel@tonic-gate 				/*
11230Sstevel@tonic-gate 				 * We still haven't gotten to the urgent mark
11240Sstevel@tonic-gate 				 * and we're out of buffer space.  Since we
11250Sstevel@tonic-gate 				 * must clear our receive window to allow it
11260Sstevel@tonic-gate 				 * to arrive, we will have to throw away
11270Sstevel@tonic-gate 				 * these bytes.
11280Sstevel@tonic-gate 				 */
11290Sstevel@tonic-gate 				n = desread(rem, waste, sizeof (waste), 0);
11300Sstevel@tonic-gate 				if (n <= 0)
11310Sstevel@tonic-gate 					return;
11320Sstevel@tonic-gate 			}
11330Sstevel@tonic-gate 		}
11340Sstevel@tonic-gate 	} while (atmark == 0);
11350Sstevel@tonic-gate 	while (recv(rem, &mark, 1, MSG_OOB) < 0) {
11360Sstevel@tonic-gate 		switch (errno) {
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 		case EWOULDBLOCK:
11390Sstevel@tonic-gate 			/*
11400Sstevel@tonic-gate 			 * We've reached the urgent mark, so the next
11410Sstevel@tonic-gate 			 * data to arrive will be the urgent, but it must
11420Sstevel@tonic-gate 			 * not have arrived yet.
11430Sstevel@tonic-gate 			 */
11440Sstevel@tonic-gate 			(void) sleep(1);
11450Sstevel@tonic-gate 			continue;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 		default:
11480Sstevel@tonic-gate 			return;
11490Sstevel@tonic-gate 		}
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 	if (mark & TIOCPKT_WINDOW) {
11520Sstevel@tonic-gate 		/*
11530Sstevel@tonic-gate 		 * Let server know about window size changes
11540Sstevel@tonic-gate 		 */
11550Sstevel@tonic-gate 		(void) kill(ppid, SIGUSR1);
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 	if (!eight && (mark & TIOCPKT_NOSTOP)) {
11580Sstevel@tonic-gate 		if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
11590Sstevel@tonic-gate 			perror("ioctl TIOCGETP");
11600Sstevel@tonic-gate 		sb.sg_flags &= ~O_CBREAK;
11610Sstevel@tonic-gate 		sb.sg_flags |= O_RAW;
11620Sstevel@tonic-gate 		if (compat_ioctl(STDIN_FILENO, TIOCSETP, &sb) == -1)
11630Sstevel@tonic-gate 			perror("ioctl TIOCSETP 1");
11640Sstevel@tonic-gate 		notc.t_stopc = -1;
11650Sstevel@tonic-gate 		notc.t_startc = -1;
11660Sstevel@tonic-gate 		if (compat_ioctl(STDIN_FILENO, TIOCSETC, &notc) == -1)
11670Sstevel@tonic-gate 			perror("ioctl TIOCSETC");
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 	if (!eight && (mark & TIOCPKT_DOSTOP)) {
11700Sstevel@tonic-gate 		if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
11710Sstevel@tonic-gate 			perror("ioctl TIOCGETP");
11720Sstevel@tonic-gate 		sb.sg_flags &= ~O_RAW;
11730Sstevel@tonic-gate 		sb.sg_flags |= O_CBREAK;
11740Sstevel@tonic-gate 		if (compat_ioctl(STDIN_FILENO, TIOCSETP, &sb) == -1)
11750Sstevel@tonic-gate 			perror("ioctl TIOCSETP 2");
11760Sstevel@tonic-gate 		notc.t_stopc = deftc.t_stopc;
11770Sstevel@tonic-gate 		notc.t_startc = deftc.t_startc;
11780Sstevel@tonic-gate 		if (compat_ioctl(STDIN_FILENO, TIOCSETC, &notc) == -1)
11790Sstevel@tonic-gate 			perror("ioctl TIOCSETC");
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 	if (mark & TIOCPKT_FLUSHWRITE) {
11820Sstevel@tonic-gate 		if (ioctl(STDOUT_FILENO, TIOCFLUSH, (char *)&out) == -1)
11830Sstevel@tonic-gate 			perror("ioctl TIOCFLUSH");
11840Sstevel@tonic-gate 		for (;;) {
11850Sstevel@tonic-gate 			if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
11860Sstevel@tonic-gate 				perror("ioctl SIOCATMARK");
11870Sstevel@tonic-gate 				break;
11880Sstevel@tonic-gate 			}
11890Sstevel@tonic-gate 			if (atmark)
11900Sstevel@tonic-gate 				break;
11910Sstevel@tonic-gate 			n = desread(rem, waste, sizeof (waste), 0);
11920Sstevel@tonic-gate 			if (n <= 0) {
11930Sstevel@tonic-gate 				if (n < 0)
11940Sstevel@tonic-gate 					prf(gettext(
11950Sstevel@tonic-gate 					    "Read error from network: %s"),
1196*634Sdp 					    strerror(errno));
11970Sstevel@tonic-gate 				break;
11980Sstevel@tonic-gate 			}
11990Sstevel@tonic-gate 		}
12000Sstevel@tonic-gate 		/*
12010Sstevel@tonic-gate 		 * Don't want any pending data to be output,
12020Sstevel@tonic-gate 		 * so clear the recv buffer.
12030Sstevel@tonic-gate 		 * If we were hanging on a write when interrupted,
12040Sstevel@tonic-gate 		 * don't want it to restart.  If we were reading,
12050Sstevel@tonic-gate 		 * restart anyway.
12060Sstevel@tonic-gate 		 */
12070Sstevel@tonic-gate 		rcvcnt = 0;
12080Sstevel@tonic-gate 		longjmp(rcvtop, 1);
12090Sstevel@tonic-gate 	}
12100Sstevel@tonic-gate 	/*
12110Sstevel@tonic-gate 	 * If we filled the receive buffer while a read was pending,
12120Sstevel@tonic-gate 	 * longjmp to the top to restart appropriately.  Don't abort
12130Sstevel@tonic-gate 	 * a pending write, however, or we won't know how much was written.
12140Sstevel@tonic-gate 	 */
12150Sstevel@tonic-gate 	if (rcvd && rcvstate == READING)
12160Sstevel@tonic-gate 		longjmp(rcvtop, 1);
12170Sstevel@tonic-gate }
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate /*
12200Sstevel@tonic-gate  * reader: read from remote: line -> 1
12210Sstevel@tonic-gate  */
12220Sstevel@tonic-gate static int
12230Sstevel@tonic-gate reader(int oldmask)
12240Sstevel@tonic-gate {
12250Sstevel@tonic-gate 	/*
12260Sstevel@tonic-gate 	 * 4.3bsd or later and SunOS 4.0 or later use the posiitive
12270Sstevel@tonic-gate 	 * pid; otherwise use the negative.
12280Sstevel@tonic-gate 	 */
12290Sstevel@tonic-gate 	pid_t pid = getpid();
12300Sstevel@tonic-gate 	int n, remaining;
12310Sstevel@tonic-gate 	char *bufp = rcvbuf;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	(void) sigset(SIGTTOU, SIG_IGN);
12340Sstevel@tonic-gate 	(void) sigset(SIGURG, (void (*)())oob);
12350Sstevel@tonic-gate 	ppid = getppid();
12360Sstevel@tonic-gate 	if (fcntl(rem, F_SETOWN, pid) == -1)
12370Sstevel@tonic-gate 		perror("fcntl F_SETOWN");
12380Sstevel@tonic-gate 	/*
12390Sstevel@tonic-gate 	 * A SIGURG may have been posted before we were completely forked,
12400Sstevel@tonic-gate 	 * which means we may not have received it. To insure we do not miss
12410Sstevel@tonic-gate 	 * any urgent data, we force the signal. The signal hander will be
12420Sstevel@tonic-gate 	 * able to determine if in fact there is urgent data or not.
12430Sstevel@tonic-gate 	 */
12440Sstevel@tonic-gate 	(void) kill(pid, SIGURG);
12450Sstevel@tonic-gate 	(void) setjmp(rcvtop);
12460Sstevel@tonic-gate 	sigsetmask(oldmask);
12470Sstevel@tonic-gate 	for (;;) {
12480Sstevel@tonic-gate 		while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
12490Sstevel@tonic-gate 			rcvstate = WRITING;
12500Sstevel@tonic-gate 			n = write(STDOUT_FILENO, bufp, remaining);
12510Sstevel@tonic-gate 			if (n < 0) {
12520Sstevel@tonic-gate 				if (errno != EINTR) {
12530Sstevel@tonic-gate 					prf(gettext(
12540Sstevel@tonic-gate 					    "Write error to terminal: %s"),
1255*634Sdp 					    strerror(errno));
12560Sstevel@tonic-gate 					return (-1);
12570Sstevel@tonic-gate 				}
12580Sstevel@tonic-gate 				continue;
12590Sstevel@tonic-gate 			}
12600Sstevel@tonic-gate 			bufp += n;
12610Sstevel@tonic-gate 		}
12620Sstevel@tonic-gate 		bufp = rcvbuf;
12630Sstevel@tonic-gate 		rcvcnt = 0;
12640Sstevel@tonic-gate 		rcvstate = READING;
12650Sstevel@tonic-gate 		rcvcnt = desread(rem, rcvbuf, sizeof (rcvbuf), 0);
12660Sstevel@tonic-gate 		if (rcvcnt == 0)
12670Sstevel@tonic-gate 			return (0);
12680Sstevel@tonic-gate 		if (rcvcnt < 0) {
12690Sstevel@tonic-gate 			if (errno == EINTR)
12700Sstevel@tonic-gate 				continue;
12710Sstevel@tonic-gate 			prf(gettext("Read error from network: %s"),
1272*634Sdp 			    strerror(errno));
12730Sstevel@tonic-gate 			return (-1);
12740Sstevel@tonic-gate 		}
12750Sstevel@tonic-gate 	}
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate static void
12790Sstevel@tonic-gate mode(int f)
12800Sstevel@tonic-gate {
12810Sstevel@tonic-gate 	struct tchars *tc;
12820Sstevel@tonic-gate 	struct ltchars *ltc;
12830Sstevel@tonic-gate 	struct sgttyb sb;
12840Sstevel@tonic-gate 	int	lflags;
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
12870Sstevel@tonic-gate 		perror("ioctl TIOCGETP");
12880Sstevel@tonic-gate 	if (ioctl(STDIN_FILENO, TIOCLGET, (char *)&lflags) == -1)
12890Sstevel@tonic-gate 		perror("ioctl TIOCLGET");
12900Sstevel@tonic-gate 	switch (f) {
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	case 0:
12930Sstevel@tonic-gate 		sb.sg_flags &= ~(O_CBREAK|O_RAW|O_TBDELAY);
12940Sstevel@tonic-gate 		sb.sg_flags |= defflags|tabflag;
12950Sstevel@tonic-gate 		tc = &deftc;
12960Sstevel@tonic-gate 		ltc = &defltc;
12970Sstevel@tonic-gate 		sb.sg_kill = defkill;
12980Sstevel@tonic-gate 		sb.sg_erase = deferase;
12990Sstevel@tonic-gate 		lflags = deflflags;
13000Sstevel@tonic-gate 		break;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	case 1:
13030Sstevel@tonic-gate 		sb.sg_flags |= (eight ? O_RAW : O_CBREAK);
13040Sstevel@tonic-gate 		sb.sg_flags &= ~defflags;
13050Sstevel@tonic-gate 		/* preserve tab delays, but turn off XTABS */
13060Sstevel@tonic-gate 		if ((sb.sg_flags & O_TBDELAY) == O_XTABS)
13070Sstevel@tonic-gate 			sb.sg_flags &= ~O_TBDELAY;
13080Sstevel@tonic-gate 		tc = &notc;
13090Sstevel@tonic-gate 		ltc = &noltc;
13100Sstevel@tonic-gate 		sb.sg_kill = sb.sg_erase = -1;
13110Sstevel@tonic-gate 		if (litout)
13120Sstevel@tonic-gate 			lflags |= LLITOUT;
13130Sstevel@tonic-gate 		break;
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	default:
13160Sstevel@tonic-gate 		/*NOTREACHED*/
13170Sstevel@tonic-gate 		return;
13180Sstevel@tonic-gate 	}
13190Sstevel@tonic-gate 	if (compat_ioctl(STDIN_FILENO, TIOCSLTC, ltc) == -1)
13200Sstevel@tonic-gate 		perror("ioctl TIOCSLTC");
13210Sstevel@tonic-gate 	if (compat_ioctl(STDIN_FILENO, TIOCSETC, tc) == -1)
13220Sstevel@tonic-gate 		perror("ioctl TIOCSETC");
13230Sstevel@tonic-gate 	if (compat_ioctl(STDIN_FILENO, TIOCSETP, &sb) == -1)
13240Sstevel@tonic-gate 		perror("ioctl TIOCSETP 3");
13250Sstevel@tonic-gate 	if (compat_ioctl(STDIN_FILENO, TIOCLSET, &lflags) == -1)
13260Sstevel@tonic-gate 		perror("ioctl TIOCLSET");
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate /* PRINTFLIKE(0) */
13300Sstevel@tonic-gate static void
13310Sstevel@tonic-gate prf(const char *format, ...)
13320Sstevel@tonic-gate {
13330Sstevel@tonic-gate 	va_list	ap;
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	va_start(ap, format);
13360Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
13370Sstevel@tonic-gate 	va_end(ap);
13380Sstevel@tonic-gate 	(void) fputs(CRLF, stderr);
13390Sstevel@tonic-gate }
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate static void
13420Sstevel@tonic-gate lostpeer(void)
13430Sstevel@tonic-gate {
13440Sstevel@tonic-gate 	(void) sigset(SIGPIPE, SIG_IGN);
13450Sstevel@tonic-gate 	prf(gettext("\aConnection to %.*s closed."), MAXHOSTNAMELEN, host);
13460Sstevel@tonic-gate 	done(EXIT_FAILURE);
13470Sstevel@tonic-gate }
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate static int
13500Sstevel@tonic-gate compat_ioctl(int des, int request, void *arg)
13510Sstevel@tonic-gate {
13520Sstevel@tonic-gate 	struct termios	tb;
13530Sstevel@tonic-gate 	boolean_t	flag = B_FALSE;
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	if (ioctl(des, request, arg) < 0)
13560Sstevel@tonic-gate 		return (-1);
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	if (tcgetattr(des, &tb) < 0)
13590Sstevel@tonic-gate 		return (-1);
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	if (cfgetispeed(&tb) != cfgetispeed(&savetty)) {
13620Sstevel@tonic-gate 		(void) cfsetispeed(&tb, cfgetispeed(&savetty));
13630Sstevel@tonic-gate 		flag = B_TRUE;
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate 	if (cfgetospeed(&tb) != cfgetospeed(&savetty)) {
13660Sstevel@tonic-gate 		(void) cfsetospeed(&tb, cfgetospeed(&savetty));
13670Sstevel@tonic-gate 		flag = B_TRUE;
13680Sstevel@tonic-gate 	}
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	return (flag ? tcsetattr(des, TCSANOW, &tb) : 0);
13710Sstevel@tonic-gate }
1372