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