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