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