Home | History | Annotate | Download | only in usr.bin
      1     0  stevel /*
      2     0  stevel  * CDDL HEADER START
      3     0  stevel  *
      4     0  stevel  * The contents of this file are subject to the terms of the
      5  8175   Peter  * Common Development and Distribution License (the "License").
      6  8175   Peter  * You may not use this file except in compliance with the License.
      7     0  stevel  *
      8     0  stevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9     0  stevel  * or http://www.opensolaris.org/os/licensing.
     10     0  stevel  * See the License for the specific language governing permissions
     11     0  stevel  * and limitations under the License.
     12     0  stevel  *
     13     0  stevel  * When distributing Covered Code, include this CDDL HEADER in each
     14     0  stevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15     0  stevel  * If applicable, add the following below this CDDL HEADER, with the
     16     0  stevel  * fields enclosed by brackets "[]" replaced with your own identifying
     17     0  stevel  * information: Portions Copyright [yyyy] [name of copyright owner]
     18     0  stevel  *
     19     0  stevel  * CDDL HEADER END
     20     0  stevel  */
     21     0  stevel /*
     22  8175   Peter  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     23     0  stevel  * Use is subject to license terms.
     24     0  stevel  */
     25     0  stevel 
     26     0  stevel /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
     27     0  stevel /*	  All Rights Reserved  	*/
     28     0  stevel 
     29     0  stevel /*
     30     0  stevel  * University Copyright- Copyright (c) 1982, 1986, 1988
     31     0  stevel  * The Regents of the University of California
     32     0  stevel  * All Rights Reserved
     33     0  stevel  *
     34     0  stevel  * University Acknowledgment- Portions of this document are derived from
     35     0  stevel  * software developed by the University of California, Berkeley, and its
     36     0  stevel  * contributors.
     37     0  stevel  */
     38     0  stevel 
     39     0  stevel /*
     40     0  stevel  * rlogin - remote login
     41     0  stevel  */
     42     0  stevel #include <sys/types.h>
     43     0  stevel #include <sys/param.h>
     44     0  stevel #include <sys/errno.h>
     45     0  stevel #include <sys/file.h>
     46     0  stevel #include <sys/socket.h>
     47     0  stevel #include <sys/wait.h>
     48     0  stevel #include <sys/stropts.h>
     49     0  stevel #include <sys/ttold.h>
     50     0  stevel #include <sys/sockio.h>
     51     0  stevel #include <sys/tty.h>
     52     0  stevel #include <sys/ptyvar.h>
     53     0  stevel #include <sys/resource.h>
     54     0  stevel #include <sys/select.h>
     55     0  stevel #include <sys/time.h>
     56     0  stevel 
     57     0  stevel #include <netinet/in.h>
     58     0  stevel #include <arpa/inet.h>
     59     0  stevel #include <priv_utils.h>
     60     0  stevel 
     61     0  stevel #include <stdio.h>
     62     0  stevel #include <errno.h>
     63     0  stevel #include <pwd.h>
     64     0  stevel #include <signal.h>
     65     0  stevel #include <setjmp.h>
     66     0  stevel #include <netdb.h>
     67     0  stevel #include <fcntl.h>
     68     0  stevel #include <locale.h>
     69     0  stevel #include <stdarg.h>
     70     0  stevel #include <stdlib.h>
     71     0  stevel #include <string.h>
     72     0  stevel #include <unistd.h>
     73     0  stevel 
     74     0  stevel #include <k5-int.h>
     75     0  stevel #include <profile/prof_int.h>
     76     0  stevel #include <com_err.h>
     77     0  stevel #include <kcmd.h>
     78     0  stevel #include <krb5.h>
     79     0  stevel 
     80     0  stevel /* signal disposition - signal handler or SIG_IGN, SIG_ERR, etc. */
     81     0  stevel typedef void (*sigdisp_t)(int);
     82     0  stevel 
     83     0  stevel extern errcode_t	profile_get_options_boolean(profile_t, char **,
     84     0  stevel     profile_options_boolean *);
     85     0  stevel extern errcode_t	profile_get_options_string(profile_t, char **,
     86     0  stevel     profile_option_strings *);
     87     0  stevel 
     88     0  stevel #define	RLOGIN_BUFSIZ	(1024 * 50)
     89     0  stevel static char des_inbuf[2 * RLOGIN_BUFSIZ];
     90     0  stevel 					/* needs to be > largest read size */
     91     0  stevel static char des_outbuf[2 * RLOGIN_BUFSIZ];
     92     0  stevel 					/* needs to be > largest write size */
     93     0  stevel static krb5_data desinbuf, desoutbuf;
     94     0  stevel static krb5_encrypt_block eblock;	/* eblock for encrypt/decrypt */
     95     0  stevel static krb5_keyblock *session_key;
     96     0  stevel static krb5_creds *cred;
     97  8175   Peter static krb5_context bsd_context = NULL;
     98     0  stevel static krb5_auth_context auth_context;
     99     0  stevel 
    100     0  stevel static char *krb_realm;
    101     0  stevel 
    102     0  stevel static	int krb5auth_flag;	/* Flag set, when KERBEROS is enabled */
    103  8175   Peter static profile_options_boolean autologin_option[] = {
    104  8175   Peter 	{ "autologin", &krb5auth_flag, 0 },
    105  8175   Peter 	{ NULL, NULL, 0 }
    106  8175   Peter };
    107  8175   Peter 
    108     0  stevel static	int fflag, Fflag;	/* Flag set, when option -f / -F used */
    109     0  stevel static	int encrypt_flag;	/* Flag set, when the "-x" option is used */
    110     0  stevel 
    111     0  stevel /* Flag set, if -PN / -PO is specified */
    112     0  stevel static boolean_t rcmdoption_done;
    113     0  stevel 
    114     0  stevel /* Flags set, if corres. cmd line options are turned on */
    115     0  stevel static boolean_t encrypt_done, fwd_done, fwdable_done;
    116     0  stevel 
    117     0  stevel static profile_options_boolean option[] = {
    118     0  stevel 	{ "encrypt", &encrypt_flag, 0 },
    119     0  stevel 	{ "forward", &fflag, 0 },
    120     0  stevel 	{ "forwardable", &Fflag, 0 },
    121     0  stevel 	{ NULL, NULL, 0 }
    122     0  stevel };
    123     0  stevel 
    124     0  stevel static char *rcmdproto;
    125     0  stevel static profile_option_strings rcmdversion[] = {
    126     0  stevel 	{ "rcmd_protocol", &rcmdproto, 0 },
    127     0  stevel 	{ NULL, NULL, 0 }
    128     0  stevel };
    129     0  stevel 
    130     0  stevel static char rlogin[] = "rlogin";
    131     0  stevel 
    132     0  stevel static char *realmdef[] = { "realms", NULL, rlogin, NULL };
    133     0  stevel static char *appdef[] = { "appdefaults", rlogin, NULL };
    134     0  stevel 
    135     0  stevel #ifndef TIOCPKT_WINDOW
    136     0  stevel #define	TIOCPKT_WINDOW 0x80
    137     0  stevel #endif /* TIOCPKT_WINDOW */
    138     0  stevel 
    139     0  stevel #ifndef sigmask
    140     0  stevel #define	sigmask(m)	(1 << ((m)-1))
    141     0  stevel #endif
    142     0  stevel 
    143     0  stevel #define	set2mask(setp)	((setp)->__sigbits[0])
    144     0  stevel #define	mask2set(mask, setp) \
    145     0  stevel 	((mask) == -1 ? sigfillset(setp) : (((setp)->__sigbits[0]) = (mask)))
    146     0  stevel 
    147     0  stevel #ifdef DEBUG
    148     0  stevel #define	DEBUGOPTSTRING	"D:"
    149     0  stevel #else
    150     0  stevel #define	DEBUGOPTSTRING	""
    151     0  stevel #endif	/* DEBUG */
    152     0  stevel 
    153     0  stevel static	boolean_t ttcompat;
    154     0  stevel static	struct termios savetty;
    155     0  stevel 
    156     0  stevel static	char *host;
    157     0  stevel static	int port_number;
    158     0  stevel static	int rem = -1;
    159     0  stevel static	char cmdchar = '~';
    160     0  stevel static	boolean_t nocmdchar;
    161     0  stevel static	boolean_t eight;
    162     0  stevel static	boolean_t litout;
    163     0  stevel static	boolean_t null_local_username;
    164     0  stevel /*
    165     0  stevel  * Note that this list of speeds is shorter than the list of speeds
    166     0  stevel  * supported by termios.  This is because we can't be sure other rlogind's
    167     0  stevel  * in the world will correctly cope with values other than what 4.2/4.3BSD
    168     0  stevel  * supported.
    169     0  stevel  */
    170     0  stevel static	char *speeds[] =
    171     0  stevel 	{ "0", "50", "75", "110", "134", "150", "200", "300",
    172     0  stevel 	    "600", "1200", "1800", "2400", "4800", "9600", "19200",
    173     0  stevel 	    "38400" };
    174     0  stevel static	char term[256] = "network";
    175     0  stevel static	void lostpeer(void);
    176     0  stevel static	boolean_t dosigwinch;
    177     0  stevel static	struct winsize winsize;
    178     0  stevel static	void sigwinch(int);
    179     0  stevel static	void oob(void);
    180     0  stevel static	void doit(int);
    181     0  stevel static	sigdisp_t sigdisp(int);
    182     0  stevel 
    183     0  stevel #define	CRLF "\r\n"
    184     0  stevel 
    185     0  stevel static	pid_t child;
    186     0  stevel static	void catchild(int);
    187     0  stevel /* LINTED */
    188     0  stevel static	void copytochild(int);
    189     0  stevel static	void writeroob(int);
    190     0  stevel static	void stop(char), echo(char);
    191     0  stevel 
    192     0  stevel static	int defflags, tabflag;
    193     0  stevel static	int deflflags;
    194     0  stevel static	char deferase, defkill;
    195     0  stevel static	struct tchars deftc;
    196     0  stevel static	struct ltchars defltc;
    197     0  stevel static	struct tchars notc = { (char)-1, (char)-1, (char)-1,
    198     0  stevel 				(char)-1, (char)-1, (char)-1 };
    199     0  stevel static	struct ltchars noltc =	{ (char)-1, (char)-1, (char)-1,
    200     0  stevel 				(char)-1, (char)-1, (char)-1 };
    201     0  stevel 
    202     0  stevel static	void done(int);
    203     0  stevel static	void mode(int);
    204     0  stevel static	int reader(int);
    205     0  stevel static	void writer(void);
    206     0  stevel static	void prf(const char *, ...);
    207     0  stevel static	void sendwindow(void);
    208     0  stevel static	int compat_ioctl(int, int, void *);
    209     0  stevel 
    210     0  stevel static void
    211     0  stevel sigsetmask(int mask)
    212     0  stevel {
    213     0  stevel 	sigset_t oset;
    214     0  stevel 	sigset_t nset;
    215     0  stevel 
    216     0  stevel 	(void) sigprocmask(0, NULL, &nset);
    217     0  stevel 	mask2set(mask, &nset);
    218     0  stevel 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
    219     0  stevel }
    220     0  stevel 
    221     0  stevel static int
    222     0  stevel sigblock(int mask)
    223     0  stevel {
    224     0  stevel 	sigset_t oset;
    225     0  stevel 	sigset_t nset;
    226     0  stevel 
    227     0  stevel 	(void) sigprocmask(0, NULL, &nset);
    228     0  stevel 	mask2set(mask, &nset);
    229     0  stevel 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
    230     0  stevel 	return (set2mask(&oset));
    231     0  stevel }
    232     0  stevel 
    233     0  stevel static void
    234     0  stevel pop(int status) {
    235     0  stevel 	if (ttcompat) {
    236     0  stevel 		/*
    237     0  stevel 		 * Pop ttcompat module
    238     0  stevel 		 */
    239     0  stevel 		(void) ioctl(STDIN_FILENO, I_POP, 0);
    240     0  stevel 	}
    241     0  stevel 	(void) tcsetattr(STDIN_FILENO, TCSANOW, &savetty);
    242     0  stevel 	exit(status);
    243     0  stevel }
    244     0  stevel 
    245     0  stevel static void
    246     0  stevel usage(void) {
    247     0  stevel 	(void) fprintf(stderr, "%s\n%s\n",
    248     0  stevel 	    gettext("usage: rlogin [-option] [-option...] "
    249     0  stevel 		"[-k realm] [-l username] host"),
    250  8175   Peter 	    gettext("       where option is e, 8, E, L, A, a, K, x, "
    251     0  stevel 		"PN / PO, f or F"));
    252     0  stevel 	pop(EXIT_FAILURE);
    253     0  stevel }
    254     0  stevel 
    255     0  stevel /* PRINTFLIKE(0) */
    256     0  stevel static void
    257     0  stevel die(const char *format, ...)
    258     0  stevel {
    259     0  stevel 	va_list	ap;
    260     0  stevel 
    261     0  stevel 	va_start(ap, format);
    262     0  stevel 	(void) vfprintf(stderr, format, ap);
    263     0  stevel 	va_end(ap);
    264     0  stevel 	usage();
    265     0  stevel }
    266     0  stevel 
    267     0  stevel static void
    268     0  stevel usage_forward(void)
    269     0  stevel {
    270     0  stevel 	die(gettext("rlogin: Only one of -f and -F allowed.\n"));
    271     0  stevel }
    272     0  stevel 
    273     0  stevel int
    274     0  stevel main(int argc, char **argv)
    275     0  stevel {
    276     0  stevel 	int c;
    277     0  stevel 	char *cp, *cmd, *name = NULL;
    278     0  stevel 	struct passwd *pwd;
    279     0  stevel 	uid_t uid;
    280     0  stevel 	int options = 0, oldmask;
    281     0  stevel 	int on = 1;
    282     0  stevel 	speed_t speed = 0;
    283     0  stevel 	int getattr_ret;
    284     0  stevel 	char *tmp;
    285     0  stevel 	int sock;
    286     0  stevel 	krb5_flags authopts;
    287     0  stevel 	krb5_error_code status;
    288     0  stevel 	enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL;
    289     0  stevel 
    290     0  stevel 	(void) setlocale(LC_ALL, "");
    291     0  stevel 
    292     0  stevel #if !defined(TEXT_DOMAIN)
    293     0  stevel #define	TEXT_DOMAIN "SYS_TEST"
    294     0  stevel #endif
    295     0  stevel 	(void) textdomain(TEXT_DOMAIN);
    296     0  stevel 
    297     0  stevel 	if (__init_suid_priv(0, PRIV_NET_PRIVADDR, NULL) == -1) {
    298     0  stevel 		(void) fprintf(stderr,
    299     0  stevel 		    gettext("Insufficient privileges, "
    300     0  stevel 			"rlogin must be set-uid root\n"));
    301     0  stevel 		exit(1);
    302     0  stevel 	}
    303     0  stevel 
    304     0  stevel 	{
    305     0  stevel 		int it;
    306     0  stevel 
    307     0  stevel 		if ((getattr_ret = tcgetattr(STDIN_FILENO, &savetty)) < 0)
    308     0  stevel 			perror("tcgetattr");
    309     0  stevel 		it = ioctl(STDIN_FILENO, I_FIND, "ttcompat");
    310     0  stevel 		if (it < 0) {
    311     0  stevel 			perror("ioctl I_FIND ttcompat");
    312     0  stevel 			return (EXIT_FAILURE);
    313     0  stevel 		}
    314     0  stevel 		if (it == 0) {
    315     0  stevel 			if (ioctl(STDIN_FILENO, I_PUSH, "ttcompat") < 0) {
    316     0  stevel 				perror("ioctl I_PUSH ttcompat");
    317     0  stevel 				exit(EXIT_FAILURE);
    318     0  stevel 			}
    319     0  stevel 			ttcompat = B_TRUE;
    320     0  stevel 		}
    321     0  stevel 	}
    322     0  stevel 
    323     0  stevel 	/*
    324     0  stevel 	 * Determine command name used to invoke to rlogin(1). Users can
    325     0  stevel 	 * create links named by a host pointing to the binary and type
    326     0  stevel 	 * "hostname" to log into that host afterwards.
    327     0  stevel 	 */
    328     0  stevel 	cmd = strrchr(argv[0], '/');
    329     0  stevel 	cmd = (cmd != NULL) ? (cmd + 1) : argv[0];
    330     0  stevel 
    331     0  stevel 	if (strcmp(cmd, rlogin) == 0) {
    332     0  stevel 		if (argc < 2)
    333     0  stevel 			usage();
    334     0  stevel 		if (*argv[1] != '-') {
    335     0  stevel 			host = argv[1];
    336     0  stevel 			argc--;
    337     0  stevel 			argv[1] = argv[0];
    338     0  stevel 			argv++;
    339     0  stevel 		}
    340     0  stevel 	} else {
    341     0  stevel 		host = cmd;
    342     0  stevel 	}
    343     0  stevel 
    344     0  stevel 	while ((c = getopt(argc, argv,
    345  8175   Peter 	    DEBUGOPTSTRING "8AEFLP:aKde:fk:l:x")) != -1) {
    346     0  stevel 		switch (c) {
    347     0  stevel 		case '8':
    348     0  stevel 			eight = B_TRUE;
    349     0  stevel 			break;
    350     0  stevel 		case 'A':
    351  8175   Peter 			krb5auth_flag++;
    352     0  stevel 			break;
    353     0  stevel #ifdef DEBUG
    354     0  stevel 		case 'D':
    355     0  stevel 			portnumber = htons(atoi(optarg));
    356  8175   Peter 			krb5auth_flag++;
    357     0  stevel 			break;
    358     0  stevel #endif /* DEBUG */
    359     0  stevel 		case 'E':
    360     0  stevel 			nocmdchar = B_TRUE;
    361     0  stevel 			break;
    362     0  stevel 		case 'F':
    363     0  stevel 			if (fflag)
    364     0  stevel 				usage_forward();
    365     0  stevel 			Fflag = 1;
    366  8175   Peter 			krb5auth_flag++;
    367     0  stevel 			fwdable_done = B_TRUE;
    368     0  stevel 			break;
    369     0  stevel 		case 'f':
    370     0  stevel 			if (Fflag)
    371     0  stevel 				usage_forward();
    372     0  stevel 			fflag = 1;
    373  8175   Peter 			krb5auth_flag++;
    374     0  stevel 			fwd_done = B_TRUE;
    375     0  stevel 			break;
    376     0  stevel 		case 'L':
    377     0  stevel 			litout = B_TRUE;
    378     0  stevel 			break;
    379     0  stevel 		case 'P':
    380     0  stevel 			if (strcmp(optarg, "N") == 0)
    381     0  stevel 				kcmd_proto = KCMD_NEW_PROTOCOL;
    382     0  stevel 			else if (strcmp(optarg, "O") == 0)
    383     0  stevel 				kcmd_proto = KCMD_OLD_PROTOCOL;
    384     0  stevel 			else
    385     0  stevel 				die(gettext("rlogin: Only -PN or -PO "
    386     0  stevel 				    "allowed.\n"));
    387     0  stevel 			if (rcmdoption_done)
    388     0  stevel 				die(gettext("rlogin: Only one of -PN and -PO "
    389     0  stevel 				    "allowed.\n"));
    390     0  stevel 			rcmdoption_done = B_TRUE;
    391  8175   Peter 			krb5auth_flag++;
    392     0  stevel 			break;
    393     0  stevel 		case 'a':
    394  8175   Peter 		case 'K':
    395     0  stevel 		/*
    396     0  stevel 		 * Force the remote host to prompt for a password by sending
    397  8175   Peter 		 * a NULL username. These options are mutually exclusive with
    398     0  stevel 		 * the -A, -x, -f, -F, -k <realm> options.
    399     0  stevel 		 */
    400     0  stevel 			null_local_username = B_TRUE;
    401     0  stevel 			break;
    402     0  stevel 		case 'd':
    403     0  stevel 			options |= SO_DEBUG;
    404     0  stevel 			break;
    405     0  stevel 		case 'e': {
    406     0  stevel 			int c;
    407     0  stevel 
    408     0  stevel 			cp = optarg;
    409     0  stevel 
    410     0  stevel 			if ((c = *cp) != '\\') {
    411     0  stevel 				cmdchar = c;
    412     0  stevel 			} else {
    413     0  stevel 				c = cp[1];
    414     0  stevel 				if (c == '\0' || c == '\\') {
    415     0  stevel 					cmdchar = '\\';
    416     0  stevel 				} else if (c >= '0' && c <= '7') {
    417     0  stevel 					long lc;
    418     0  stevel 
    419     0  stevel 					lc = strtol(&cp[1], NULL, 8);
    420     0  stevel 					if (lc < 0 || lc > 255)
    421     0  stevel 						die(gettext("rlogin: octal "
    422     0  stevel 						    "escape character %s too "
    423     0  stevel 						    "large.\n"), cp);
    424     0  stevel 					cmdchar = (char)lc;
    425     0  stevel 				} else {
    426     0  stevel 					die(gettext("rlogin: unrecognized "
    427     0  stevel 					    "escape character option %s.\n"),
    428     0  stevel 					    cp);
    429     0  stevel 				}
    430     0  stevel 			}
    431     0  stevel 			break;
    432     0  stevel 		}
    433     0  stevel 		case 'k':
    434     0  stevel 			krb_realm = optarg;
    435  8175   Peter 			krb5auth_flag++;
    436     0  stevel 			break;
    437     0  stevel 		case 'l':
    438     0  stevel 			name = optarg;
    439     0  stevel 			break;
    440     0  stevel 		case 'x':
    441     0  stevel 			encrypt_flag = 1;
    442  8175   Peter 			krb5auth_flag++;
    443     0  stevel 			encrypt_done = B_TRUE;
    444     0  stevel 			break;
    445     0  stevel 		default:
    446     0  stevel 			usage();
    447     0  stevel 		}
    448     0  stevel 	}
    449     0  stevel 
    450     0  stevel 	argc -= optind;
    451     0  stevel 	argv += optind;
    452     0  stevel 
    453     0  stevel 	if (host == NULL) {
    454     0  stevel 		if (argc == 0)
    455     0  stevel 			usage();
    456     0  stevel 		argc--;
    457     0  stevel 		host = *argv++;
    458     0  stevel 	}
    459     0  stevel 
    460     0  stevel 	if (argc > 0)
    461     0  stevel 		usage();
    462     0  stevel 
    463     0  stevel 	pwd = getpwuid(uid = getuid());
    464     0  stevel 	if (pwd == NULL) {
    465     0  stevel 		(void) fprintf(stderr, gettext("getpwuid(): can not find "
    466     0  stevel 			"password entry for user id %d."), uid);
    467     0  stevel 		return (EXIT_FAILURE);
    468     0  stevel 	}
    469     0  stevel 	if (name == NULL)
    470     0  stevel 		name = pwd->pw_name;
    471     0  stevel 
    472     0  stevel 	/*
    473  8175   Peter 	 * If the `-a or -K' options are issued on the cmd line, we reset
    474  8175   Peter 	 * all flags associated with other KRB5 specific options, since
    475  8175   Peter 	 * these options are mutually exclusive with the rest.
    476     0  stevel 	 */
    477     0  stevel 	if (null_local_username) {
    478  8175   Peter 		krb5auth_flag = 0;
    479     0  stevel 		fflag = Fflag = encrypt_flag = 0;
    480  8175   Peter 		(void) fprintf(stderr,
    481  8175   Peter 				gettext("Note: The -a (or -K) option nullifies "
    482     0  stevel 					"all other Kerberos-specific\noptions "
    483     0  stevel 					"you may have used.\n"));
    484  8175   Peter 	} else if (!krb5auth_flag) {
    485  8175   Peter 		/* is autologin set in krb5.conf? */
    486  8175   Peter 		status = krb5_init_context(&bsd_context);
    487  8175   Peter 		/* don't sweat failure here */
    488  8175   Peter 		if (!status) {
    489  8175   Peter 			/*
    490  8175   Peter 			 * note that the call to profile_get_options_boolean
    491  8175   Peter 			 * with autologin_option can affect value of
    492  8175   Peter 			 * krb5auth_flag
    493  8175   Peter 			 */
    494  8175   Peter 			profile_get_options_boolean(bsd_context->profile,
    495  8175   Peter 						appdef,
    496  8175   Peter 						autologin_option);
    497  8175   Peter 		}
    498     0  stevel 	}
    499     0  stevel 
    500     0  stevel 	if (krb5auth_flag) {
    501  8175   Peter 		if (!bsd_context) {
    502  8175   Peter 			status = krb5_init_context(&bsd_context);
    503  8175   Peter 			if (status) {
    504  8175   Peter 				com_err(rlogin, status,
    505  8175   Peter 				    gettext("while initializing krb5"));
    506  8175   Peter 				return (EXIT_FAILURE);
    507  8175   Peter 			}
    508     0  stevel 		}
    509     0  stevel 		/*
    510     0  stevel 		 * Set up buffers for desread and deswrite.
    511     0  stevel 		 */
    512     0  stevel 		desinbuf.data = des_inbuf;
    513     0  stevel 		desoutbuf.data = des_outbuf;
    514     0  stevel 		desinbuf.length = sizeof (des_inbuf);
    515     0  stevel 		desoutbuf.length = sizeof (des_outbuf);
    516     0  stevel 
    517     0  stevel 		/*
    518     0  stevel 		 * Get our local realm to look up local realm options.
    519     0  stevel 		 */
    520     0  stevel 		status = krb5_get_default_realm(bsd_context, &realmdef[1]);
    521     0  stevel 		if (status) {
    522     0  stevel 			com_err(rlogin, status,
    523     0  stevel 				gettext("while getting default realm"));
    524     0  stevel 			return (EXIT_FAILURE);
    525     0  stevel 		}
    526     0  stevel 		/*
    527     0  stevel 		 * Check the realms section in krb5.conf for encryption,
    528     0  stevel 		 * forward & forwardable info
    529     0  stevel 		 */
    530     0  stevel 		profile_get_options_boolean(bsd_context->profile, realmdef,
    531     0  stevel 						option);
    532     0  stevel 		/*
    533     0  stevel 		 * Check the appdefaults section
    534     0  stevel 		 */
    535     0  stevel 		profile_get_options_boolean(bsd_context->profile, appdef,
    536     0  stevel 						option);
    537     0  stevel 		profile_get_options_string(bsd_context->profile, appdef,
    538     0  stevel 						rcmdversion);
    539     0  stevel 
    540     0  stevel 		/*
    541     0  stevel 		 * Set the *_flag variables, if the corresponding *_done are
    542     0  stevel 		 * set to 1, because we dont want the config file values
    543     0  stevel 		 * overriding the command line options.
    544     0  stevel 		 */
    545     0  stevel 		if (encrypt_done)
    546     0  stevel 			encrypt_flag = 1;
    547     0  stevel 		if (fwd_done) {
    548     0  stevel 			fflag = 1;
    549     0  stevel 			Fflag = 0;
    550     0  stevel 		} else if (fwdable_done) {
    551     0  stevel 			Fflag = 1;
    552     0  stevel 			fflag = 0;
    553     0  stevel 		}
    554     0  stevel 		if (!rcmdoption_done && (rcmdproto != NULL)) {
    555     0  stevel 			if (strncmp(rcmdproto, "rcmdv2", 6) == 0) {
    556     0  stevel 				kcmd_proto = KCMD_NEW_PROTOCOL;
    557     0  stevel 			} else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) {
    558     0  stevel 				kcmd_proto = KCMD_OLD_PROTOCOL;
    559     0  stevel 			} else {
    560     0  stevel 				(void) fprintf(stderr, gettext("Unrecognized "
    561     0  stevel 					"KCMD protocol (%s)"), rcmdproto);
    562     0  stevel 				return (EXIT_FAILURE);
    563     0  stevel 			}
    564     0  stevel 		}
    565     0  stevel 
    566     0  stevel 		if (encrypt_flag && (!krb5_privacy_allowed())) {
    567     0  stevel 			(void) fprintf(stderr, gettext("rlogin: "
    568     0  stevel 					"Encryption not supported.\n"));
    569     0  stevel 			return (EXIT_FAILURE);
    570     0  stevel 		}
    571     0  stevel 	}
    572     0  stevel 
    573     0  stevel 	if (port_number == 0) {
    574     0  stevel 		if (krb5auth_flag) {
    575     0  stevel 			struct servent *sp;
    576     0  stevel 
    577     0  stevel 			/*
    578     0  stevel 			 * If the krb5auth_flag is set (via -A, -f, -F, -k) &
    579     0  stevel 			 * if there is an entry in /etc/services for Kerberos
    580     0  stevel 			 * login, attempt to login with Kerberos. If we fail
    581     0  stevel 			 * at any step,  use the standard rlogin
    582     0  stevel 			 */
    583     0  stevel 			sp = getservbyname(encrypt_flag ?
    584     0  stevel 			    "eklogin" : "klogin", "tcp");
    585     0  stevel 			if (sp == NULL) {
    586     0  stevel 				port_number = encrypt_flag ?
    587     0  stevel 				    htons(2105) : htons(543);
    588     0  stevel 			} else {
    589     0  stevel 				port_number = sp->s_port;
    590     0  stevel 			}
    591     0  stevel 		} else {
    592     0  stevel 			port_number = htons(IPPORT_LOGINSERVER);
    593     0  stevel 		}
    594     0  stevel 	}
    595     0  stevel 
    596     0  stevel 	cp = getenv("TERM");
    597     0  stevel 	if (cp) {
    598     0  stevel 		(void) strncpy(term, cp, sizeof (term));
    599     0  stevel 		term[sizeof (term) - 1] = '\0';
    600     0  stevel 	}
    601     0  stevel 	if (getattr_ret == 0) {
    602     0  stevel 		speed = cfgetospeed(&savetty);
    603     0  stevel 		/*
    604     0  stevel 		 * "Be conservative in what we send" -- Only send baud rates
    605     0  stevel 		 * which at least all 4.x BSD derivatives are known to handle
    606     0  stevel 		 * correctly.
    607     0  stevel 		 * NOTE:  This code assumes new termios speed values will
    608     0  stevel 		 * be "higher" speeds.
    609     0  stevel 		 */
    610     0  stevel 		if (speed > B38400)
    611     0  stevel 			speed = B38400;
    612     0  stevel 	}
    613     0  stevel 
    614     0  stevel 	/*
    615     0  stevel 	 * Only put the terminal speed info in if we have room
    616     0  stevel 	 * so we don't overflow the buffer, and only if we have
    617     0  stevel 	 * a speed we recognize.
    618     0  stevel 	 */
    619     0  stevel 	if (speed > 0 && speed < sizeof (speeds)/sizeof (char *) &&
    620     0  stevel 	    strlen(term) + strlen("/") + strlen(speeds[speed]) + 1 <
    621     0  stevel 	    sizeof (term)) {
    622     0  stevel 		(void) strcat(term, "/");
    623     0  stevel 		(void) strcat(term, speeds[speed]);
    624     0  stevel 	}
    625     0  stevel 	(void) sigset(SIGPIPE, (sigdisp_t)lostpeer);
    626     0  stevel 	/* will use SIGUSR1 for window size hack, so hold it off */
    627     0  stevel 	oldmask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
    628     0  stevel 
    629     0  stevel 	/*
    630     0  stevel 	 * Determine if v4 literal address and if so store it to one
    631     0  stevel 	 * side. This is to correct the undesired behaviour of rcmd_af
    632     0  stevel 	 * which converts a passed in v4 literal address to a v4 mapped
    633     0  stevel 	 * v6 literal address. If it was a v4 literal we then re-assign
    634     0  stevel 	 * it to host.
    635     0  stevel 	 */
    636     0  stevel 	tmp = NULL;
    637     0  stevel 	if (inet_addr(host) != (in_addr_t)-1)
    638     0  stevel 		tmp = host;
    639     0  stevel 
    640     0  stevel 	if (krb5auth_flag) {
    641     0  stevel 		authopts = AP_OPTS_MUTUAL_REQUIRED;
    642     0  stevel 
    643     0  stevel 		/* Piggy-back forwarding flags on top of authopts; */
    644     0  stevel 		/* they will be reset in kcmd */
    645     0  stevel 		if (fflag || Fflag)
    646     0  stevel 			authopts |= OPTS_FORWARD_CREDS;
    647     0  stevel 		if (Fflag)
    648     0  stevel 			authopts |= OPTS_FORWARDABLE_CREDS;
    649     0  stevel 
    650     0  stevel 		status = kcmd(&sock, &host, port_number,
    651     0  stevel 			null_local_username ? "" : pwd->pw_name,
    652     0  stevel 			name, term, NULL,
    653     0  stevel 			"host", krb_realm, bsd_context, &auth_context,
    654     0  stevel 			&cred,
    655     0  stevel 			NULL,		/* No need for sequence number */
    656     0  stevel 			NULL,		/* No need for server seq # */
    657     0  stevel 			authopts,
    658     0  stevel 			0,		/* Not any port # */
    659     0  stevel 			&kcmd_proto);
    660     0  stevel 
    661     0  stevel 		if (status != 0) {
    662     0  stevel 			/*
    663     0  stevel 			 * If new protocol requested, we dont fallback to
    664     0  stevel 			 * less secure ones.
    665     0  stevel 			 */
    666     0  stevel 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
    667     0  stevel 				(void) fprintf(stderr, gettext("rlogin: kcmdv2 "
    668     0  stevel 					"to host %s failed - %s\n"
    669     0  stevel 					"Fallback to normal rlogin denied."),
    670     0  stevel 					host, error_message(status));
    671     0  stevel 				return (EXIT_FAILURE);
    672     0  stevel 			}
    673     0  stevel 			if (status != -1) {
    674     0  stevel 				(void) fprintf(stderr, gettext("rlogin: kcmd "
    675     0  stevel 						"to host %s failed - %s,\n"
    676     0  stevel 						"trying normal rlogin...\n\n"),
    677     0  stevel 						host, error_message(status));
    678     0  stevel 			} else {
    679     0  stevel 				(void) fprintf(stderr,
    680     0  stevel 					gettext("trying normal rlogin...\n"));
    681     0  stevel 			}
    682     0  stevel 			/*
    683     0  stevel 			 * kcmd() failed, so we have to
    684     0  stevel 			 * fallback to normal rlogin
    685     0  stevel 			 */
    686     0  stevel 			port_number = htons(IPPORT_LOGINSERVER);
    687  8175   Peter 			krb5auth_flag = 0;
    688     0  stevel 			fflag = Fflag = encrypt_flag = 0;
    689     0  stevel 			null_local_username = B_FALSE;
    690     0  stevel 		} else {
    691     0  stevel 			(void) fprintf(stderr,
    692     0  stevel 			    gettext("connected with Kerberos V5\n"));
    693     0  stevel 
    694     0  stevel 			/*
    695     0  stevel 			 * Setup eblock for desread and deswrite.
    696     0  stevel 			 */
    697     0  stevel 			session_key = &cred->keyblock;
    698     0  stevel 
    699     0  stevel 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
    700     0  stevel 				status = krb5_auth_con_getlocalsubkey(
    701     0  stevel 				    bsd_context,
    702     0  stevel 				    auth_context,
    703     0  stevel 				    &session_key);
    704     0  stevel 				if (status) {
    705     0  stevel 					com_err(rlogin, status,
    706     0  stevel 					    "determining subkey for session");
    707     0  stevel 					return (EXIT_FAILURE);
    708     0  stevel 				}
    709     0  stevel 				if (session_key == NULL) {
    710     0  stevel 					com_err(rlogin, 0,
    711     0  stevel 					    "no subkey negotiated for "
    712     0  stevel 					    "connection");
    713     0  stevel 					return (EXIT_FAILURE);
    714     0  stevel 				}
    715     0  stevel 			}
    716     0  stevel 
    717     0  stevel 			eblock.crypto_entry = session_key->enctype;
    718     0  stevel 			eblock.key = (krb5_keyblock *)session_key;
    719     0  stevel 
    720     0  stevel 			init_encrypt(encrypt_flag, bsd_context, kcmd_proto,
    721     0  stevel 			    &desinbuf, &desoutbuf, CLIENT, &eblock);
    722     0  stevel 
    723     0  stevel 			rem = sock;
    724     0  stevel 			if (rem < 0)
    725     0  stevel 				pop(EXIT_FAILURE);
    726     0  stevel 		}
    727     0  stevel 	}
    728     0  stevel 
    729     0  stevel 	/*
    730     0  stevel 	 * Don't merge this with the "if" statement above because
    731     0  stevel 	 * "krb5auth_flag" might be set to false inside it.
    732     0  stevel 	 */
    733     0  stevel 	if (!krb5auth_flag) {
    734     0  stevel 		rem = rcmd_af(&host, port_number,
    735     0  stevel 			null_local_username ? "" : pwd->pw_name,
    736     0  stevel 			name, term, NULL, AF_INET6);
    737     0  stevel 		if (rem < 0)
    738     0  stevel 			pop(EXIT_FAILURE);
    739     0  stevel 	}
    740     0  stevel 
    741     0  stevel 	/* Never need our privilege again */
    742     0  stevel 	__priv_relinquish();
    743     0  stevel 
    744     0  stevel 	if (tmp != NULL)
    745     0  stevel 		host = tmp;
    746     0  stevel 
    747     0  stevel 	if (options & SO_DEBUG &&
    748     0  stevel 	    setsockopt(rem, SOL_SOCKET, SO_DEBUG, (char *)&on,
    749     0  stevel 			    sizeof (on)) < 0)
    750     0  stevel 		perror("rlogin: setsockopt (SO_DEBUG)");
    751     0  stevel 
    752     0  stevel 	{
    753     0  stevel 		int bufsize = 8192;
    754     0  stevel 
    755     0  stevel 		(void) setsockopt(rem, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize,
    756     0  stevel 			sizeof (int));
    757     0  stevel 	}
    758     0  stevel 
    759     0  stevel 	doit(oldmask);
    760   473      bw 	return (0);
    761     0  stevel }
    762     0  stevel 
    763     0  stevel static void
    764     0  stevel doit(int oldmask)
    765     0  stevel {
    766     0  stevel 	struct sgttyb sb;
    767     0  stevel 	int atmark;
    768     0  stevel 
    769     0  stevel 	if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
    770     0  stevel 		perror("ioctl TIOCGETP");
    771     0  stevel 	defflags = sb.sg_flags;
    772     0  stevel 	tabflag = defflags & O_TBDELAY;
    773     0  stevel 	defflags &= ECHO | O_CRMOD;
    774     0  stevel 	deferase = sb.sg_erase;
    775     0  stevel 	defkill = sb.sg_kill;
    776     0  stevel 	if (ioctl(STDIN_FILENO, TIOCLGET, (char *)&deflflags) == -1)
    777     0  stevel 		perror("ioctl TIOCLGET");
    778     0  stevel 	if (ioctl(STDIN_FILENO, TIOCGETC, (char *)&deftc) == -1)
    779     0  stevel 		perror("ioctl TIOCGETC");
    780     0  stevel 	notc.t_startc = deftc.t_startc;
    781     0  stevel 	notc.t_stopc = deftc.t_stopc;
    782     0  stevel 	if (ioctl(STDIN_FILENO, TIOCGLTC, (char *)&defltc) == -1)
    783     0  stevel 		perror("ioctl TIOCGLTC");
    784     0  stevel 	(void) sigset(SIGINT, SIG_IGN);
    785     0  stevel 	if (sigdisp(SIGHUP) != SIG_IGN)
    786     0  stevel 		(void) sigset(SIGHUP, exit);
    787     0  stevel 	if (sigdisp(SIGQUIT) != SIG_IGN)
    788     0  stevel 		(void) sigset(SIGQUIT, exit);
    789     0  stevel 	child = fork();
    790     0  stevel 	if (child == (pid_t)-1) {
    791     0  stevel 		perror("rlogin: fork");
    792     0  stevel 		done(EXIT_FAILURE);
    793     0  stevel 	}
    794     0  stevel 	if (child == 0) {
    795     0  stevel 		mode(1);
    796     0  stevel 		if (reader(oldmask) == 0) {
    797     0  stevel 			prf(gettext("Connection to %.*s closed."),
    798     0  stevel 			    MAXHOSTNAMELEN, host);
    799     0  stevel 			exit(EXIT_SUCCESS);
    800     0  stevel 		}
    801     0  stevel 		(void) sleep(1);
    802     0  stevel 		prf(gettext("\aConnection to %.*s closed."),
    803     0  stevel 		    MAXHOSTNAMELEN, host);
    804     0  stevel 		exit(EXIT_FAILURE);
    805     0  stevel 	}
    806     0  stevel 
    807     0  stevel 	/*
    808     0  stevel 	 * We may still own the socket, and may have a pending SIGURG (or might
    809     0  stevel 	 * receive one soon) that we really want to send to the reader.  Set a
    810     0  stevel 	 * trap that simply copies such signals to the child.
    811     0  stevel 	 */
    812     0  stevel #ifdef F_SETOWN_BUG_FIXED
    813     0  stevel 	(void) sigset(SIGURG, copytochild);
    814     0  stevel #else
    815     0  stevel 	(void) sigset(SIGURG, SIG_IGN);
    816     0  stevel #endif /* F_SETOWN_BUG_FIXED */
    817     0  stevel 	(void) sigset(SIGUSR1, writeroob);
    818     0  stevel 	/*
    819     0  stevel 	 * Of course, if the urgent byte already arrived, allowing SIGURG
    820     0  stevel 	 * won't get us notification.  So, we check to see if we've got
    821     0  stevel 	 * an urgent byte.  If so, force a call to writeroob() to pretend
    822     0  stevel 	 * we got SIGURG.
    823     0  stevel 	 */
    824     0  stevel 	if (ioctl(rem, SIOCATMARK, &atmark) >= 0) {
    825     0  stevel 		if (atmark)
    826     0  stevel 			writeroob(0);
    827     0  stevel 	}
    828     0  stevel 	sigsetmask(oldmask);
    829     0  stevel 	(void) sigset(SIGCHLD, catchild);
    830     0  stevel 	writer();
    831     0  stevel 	prf(gettext("Closed connection to %.*s."), MAXHOSTNAMELEN, host);
    832     0  stevel 	done(EXIT_SUCCESS);
    833     0  stevel }
    834     0  stevel 
    835     0  stevel /*
    836     0  stevel  * Get signal disposition (or signal handler) for a given signal
    837     0  stevel  */
    838     0  stevel static sigdisp_t
    839     0  stevel sigdisp(int sig)
    840     0  stevel {
    841     0  stevel 	struct sigaction act;
    842     0  stevel 
    843     0  stevel 	act.sa_handler = NULL;
    844     0  stevel 	act.sa_flags = 0;
    845     0  stevel 	(void) sigemptyset(&act.sa_mask);
    846     0  stevel 	(void) sigaction(sig, NULL, &act);
    847     0  stevel 	return (act.sa_handler);
    848     0  stevel }
    849     0  stevel 
    850     0  stevel static void
    851     0  stevel done(int status)
    852     0  stevel {
    853     0  stevel 	pid_t w;
    854     0  stevel 
    855     0  stevel 	mode(0);
    856     0  stevel 	if (child > 0) {
    857     0  stevel 		/* make sure catchild does not snap it up */
    858     0  stevel 		(void) sigset(SIGCHLD, SIG_DFL);
    859     0  stevel 		if (kill(child, SIGKILL) >= 0)
    860     0  stevel 			while ((w = wait(0)) > (pid_t)0 && w != child)
    861     0  stevel 				/* void */;
    862     0  stevel 	}
    863     0  stevel 	pop(status);
    864     0  stevel }
    865     0  stevel 
    866     0  stevel /*
    867     0  stevel  * Copy SIGURGs to the child process.
    868     0  stevel  */
    869     0  stevel 
    870     0  stevel /* ARGSUSED */
    871     0  stevel static void
    872     0  stevel copytochild(int signum)
    873     0  stevel {
    874     0  stevel 
    875     0  stevel 	(void) kill(child, SIGURG);
    876     0  stevel }
    877     0  stevel 
    878     0  stevel /*
    879     0  stevel  * This is called when the reader process gets the out-of-band (urgent)
    880     0  stevel  * request to turn on the window-changing protocol.
    881     0  stevel  */
    882     0  stevel 
    883     0  stevel /* ARGSUSED */
    884     0  stevel static void
    885     0  stevel writeroob(int signum)
    886     0  stevel {
    887     0  stevel 	int mask;
    888     0  stevel 
    889     0  stevel 	if (!dosigwinch) {
    890     0  stevel 		/*
    891     0  stevel 		 * Start tracking window size.  It doesn't matter which
    892     0  stevel 		 * order the next two are in, because we'll be unconditionally
    893     0  stevel 		 * sending a size notification in a moment.
    894     0  stevel 		 */
    895     0  stevel 		(void) sigset(SIGWINCH, sigwinch);
    896     0  stevel 		dosigwinch = B_TRUE;
    897     0  stevel 
    898     0  stevel 		/*
    899     0  stevel 		 * It would be bad if a SIGWINCH came in between the ioctl
    900     0  stevel 		 * and sending the data.  It could result in the SIGWINCH
    901     0  stevel 		 * handler sending a good message, and then us sending an
    902     0  stevel 		 * outdated or inconsistent message.
    903     0  stevel 		 *
    904     0  stevel 		 * Instead, if the change is made before the
    905     0  stevel 		 * ioctl, the sigwinch handler will send a size message
    906     0  stevel 		 * and we'll send another, identical, one.  If the change
    907     0  stevel 		 * is made after the ioctl, we'll send a message with the
    908     0  stevel 		 * old value, and then the sigwinch handler will send
    909     0  stevel 		 * a revised, correct one.
    910     0  stevel 		 */
    911     0  stevel 		mask = sigblock(sigmask(SIGWINCH));
    912     0  stevel 		if (ioctl(STDIN_FILENO, TIOCGWINSZ, &winsize) == 0)
    913     0  stevel 			sendwindow();
    914     0  stevel 		sigsetmask(mask);
    915     0  stevel 	}
    916     0  stevel }
    917     0  stevel 
    918     0  stevel /* ARGSUSED */
    919     0  stevel static void
    920     0  stevel catchild(int signum)
    921     0  stevel {
    922     0  stevel 	int options;
    923     0  stevel 	siginfo_t	info;
    924     0  stevel 	int error;
    925     0  stevel 
    926     0  stevel 	for (;;) {
    927     0  stevel 		options = WNOHANG | WEXITED;
    928     0  stevel 		error = waitid(P_ALL, 0, &info, options);
    929     0  stevel 		if (error != 0)
    930     0  stevel 			return;
    931     0  stevel 		if (info.si_pid == 0)
    932     0  stevel 			return;
    933     0  stevel 		if (info.si_code == CLD_TRAPPED)
    934     0  stevel 			continue;
    935     0  stevel 		if (info.si_code == CLD_STOPPED)
    936     0  stevel 			continue;
    937     0  stevel 		done(info.si_status);
    938     0  stevel 	}
    939     0  stevel }
    940     0  stevel 
    941     0  stevel /*
    942     0  stevel  * writer: write to remote: 0 -> line.
    943     0  stevel  * ~.	terminate
    944     0  stevel  * ~^Z	suspend rlogin process.
    945     0  stevel  * ~^Y  suspend rlogin process, but leave reader alone.
    946     0  stevel  */
    947     0  stevel static void
    948     0  stevel writer(void)
    949     0  stevel {
    950     0  stevel 	char c;
    951     0  stevel 	int n;
    952     0  stevel 	boolean_t bol = B_TRUE;		/* beginning of line */
    953     0  stevel 	boolean_t local = B_FALSE;
    954     0  stevel 
    955     0  stevel 	for (;;) {
    956     0  stevel 		n = read(STDIN_FILENO, &c, 1);
    957     0  stevel 		if (n <= 0) {
    958     0  stevel 			if (n == 0)
    959     0  stevel 				break;
    960     0  stevel 			if (errno == EINTR)
    961     0  stevel 				continue;
    962     0  stevel 			else {
    963     0  stevel 				prf(gettext("Read error from terminal: %s"),
    964   634      dp 				    strerror(errno));
    965     0  stevel 				break;
    966     0  stevel 			}
    967     0  stevel 		}
    968     0  stevel 		/*
    969     0  stevel 		 * If we're at the beginning of the line
    970     0  stevel 		 * and recognize a command character, then
    971     0  stevel 		 * we echo locally.  Otherwise, characters
    972     0  stevel 		 * are echo'd remotely.  If the command
    973     0  stevel 		 * character is doubled, this acts as a
    974     0  stevel 		 * force and local echo is suppressed.
    975     0  stevel 		 */
    976     0  stevel 		if (bol && !nocmdchar) {
    977     0  stevel 			bol = B_FALSE;
    978     0  stevel 			if (c == cmdchar) {
    979     0  stevel 				local = B_TRUE;
    980     0  stevel 				continue;
    981     0  stevel 			}
    982     0  stevel 		} else if (local) {
    983     0  stevel 			local = B_FALSE;
    984     0  stevel 			if (c == '.' || c == deftc.t_eofc) {
    985     0  stevel 				echo(c);
    986     0  stevel 				break;
    987     0  stevel 			}
    988     0  stevel 			if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
    989     0  stevel 				bol = B_TRUE;
    990     0  stevel 				echo(c);
    991     0  stevel 				stop(c);
    992     0  stevel 				continue;
    993     0  stevel 			}
    994     0  stevel 			if (c != cmdchar) {
    995     0  stevel 				if (deswrite(rem, &cmdchar, 1, 0) < 0) {
    996     0  stevel 					prf(gettext(
    997     0  stevel 					    "Write error to network: %s"),
    998   634      dp 					    strerror(errno));
    999     0  stevel 					break;
   1000     0  stevel 				}
   1001     0  stevel 			}
   1002     0  stevel 		}
   1003     0  stevel 		if ((n = deswrite(rem, &c, 1, 0)) <= 0) {
   1004     0  stevel 			if (n == 0)
   1005     0  stevel 				prf(gettext("line gone"));
   1006     0  stevel 			else
   1007     0  stevel 				prf(gettext("Write error to network: %s"),
   1008   634      dp 				    strerror(errno));
   1009     0  stevel 			break;
   1010     0  stevel 		}
   1011     0  stevel 		bol = c == defkill || c == deftc.t_eofc ||
   1012     0  stevel 		    c == deftc.t_intrc || c == defltc.t_suspc ||
   1013     0  stevel 		    c == '\r' || c == '\n';
   1014     0  stevel 	}
   1015     0  stevel }
   1016     0  stevel 
   1017     0  stevel static void
   1018     0  stevel echo(char c)
   1019     0  stevel {
   1020     0  stevel 	char buf[8];
   1021     0  stevel 	char *p = buf;
   1022     0  stevel 
   1023     0  stevel 	c &= 0177;
   1024     0  stevel 	*p++ = cmdchar;
   1025     0  stevel 	if (c < ' ') {
   1026     0  stevel 		*p++ = '^';
   1027     0  stevel 		*p++ = c + '@';
   1028     0  stevel 	} else if (c == 0177) {
   1029     0  stevel 		*p++ = '^';
   1030     0  stevel 		*p++ = '?';
   1031     0  stevel 	} else
   1032     0  stevel 		*p++ = c;
   1033     0  stevel 	*p++ = '\r';
   1034     0  stevel 	*p++ = '\n';
   1035     0  stevel 	if (write(STDOUT_FILENO, buf, p - buf) < 0)
   1036   634      dp 		prf(gettext("Write error to terminal: %s"), strerror(errno));
   1037     0  stevel }
   1038     0  stevel 
   1039     0  stevel static void
   1040     0  stevel stop(char cmdc)
   1041     0  stevel {
   1042     0  stevel 	mode(0);
   1043     0  stevel 	(void) sigset(SIGCHLD, SIG_IGN);
   1044     0  stevel 	(void) kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
   1045     0  stevel 	(void) sigset(SIGCHLD, catchild);
   1046     0  stevel 	mode(1);
   1047     0  stevel 	sigwinch(0);			/* check for size changes */
   1048     0  stevel }
   1049     0  stevel 
   1050     0  stevel /* ARGSUSED */
   1051     0  stevel static void
   1052     0  stevel sigwinch(int signum)
   1053     0  stevel {
   1054     0  stevel 	struct winsize ws;
   1055     0  stevel 
   1056     0  stevel 	if (dosigwinch && ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0 &&
   1057     0  stevel 	    memcmp(&winsize, &ws, sizeof (ws)) != 0) {
   1058     0  stevel 		winsize = ws;
   1059     0  stevel 		sendwindow();
   1060     0  stevel 	}
   1061     0  stevel }
   1062     0  stevel 
   1063     0  stevel /*
   1064     0  stevel  * Send the window size to the server via the magic escape.
   1065     0  stevel  * Note:  SIGWINCH should be blocked when this is called, lest
   1066     0  stevel  * winsize change underneath us and chaos result.
   1067     0  stevel  */
   1068     0  stevel static void
   1069     0  stevel sendwindow(void)
   1070     0  stevel {
   1071     0  stevel 	char obuf[4 + sizeof (struct winsize)];
   1072     0  stevel 	struct winsize *wp = (struct winsize *)(void *)(obuf+4);
   1073     0  stevel 
   1074     0  stevel 	obuf[0] = -1;
   1075     0  stevel 	obuf[1] = -1;
   1076     0  stevel 	obuf[2] = 's';
   1077     0  stevel 	obuf[3] = 's';
   1078     0  stevel 	wp->ws_row = htons(winsize.ws_row);
   1079     0  stevel 	wp->ws_col = htons(winsize.ws_col);
   1080     0  stevel 	wp->ws_xpixel = htons(winsize.ws_xpixel);
   1081     0  stevel 	wp->ws_ypixel = htons(winsize.ws_ypixel);
   1082     0  stevel 	if (deswrite(rem, obuf, sizeof (obuf), 0) < 0)
   1083   634      dp 		prf(gettext("Write error to network: %s"), strerror(errno));
   1084     0  stevel }
   1085     0  stevel 
   1086     0  stevel 
   1087     0  stevel /*
   1088     0  stevel  * reader: read from remote: remote -> stdout
   1089     0  stevel  */
   1090     0  stevel #define	READING	1
   1091     0  stevel #define	WRITING	2
   1092     0  stevel 
   1093     0  stevel static	char rcvbuf[8 * 1024];
   1094     0  stevel static	int rcvcnt;
   1095     0  stevel static	int rcvstate;
   1096     0  stevel static	pid_t ppid;
   1097     0  stevel static	jmp_buf rcvtop;
   1098     0  stevel 
   1099     0  stevel static void
   1100     0  stevel oob(void)
   1101     0  stevel {
   1102     0  stevel 	int out = FWRITE, atmark, n;
   1103     0  stevel 	int rcvd = 0;
   1104     0  stevel 	char waste[4*BUFSIZ], mark;
   1105     0  stevel 	struct sgttyb sb;
   1106     0  stevel 	fd_set exceptfds;
   1107     0  stevel 	struct timeval tv;
   1108     0  stevel 	int ret;
   1109     0  stevel 
   1110     0  stevel 	FD_ZERO(&exceptfds);
   1111     0  stevel 	FD_SET(rem, &exceptfds);
   1112     0  stevel 	timerclear(&tv);
   1113     0  stevel 	ret = select(rem+1, NULL, NULL, &exceptfds, &tv);
   1114     0  stevel 	/*
   1115     0  stevel 	 * We may get an extra signal at start up time since we are trying
   1116     0  stevel 	 * to take all precautions not to miss the urgent byte. This
   1117     0  stevel 	 * means we may get here without any urgent data to process, in which
   1118     0  stevel 	 * case we do nothing and just return.
   1119     0  stevel 	 */
   1120     0  stevel 	if (ret <= 0)
   1121     0  stevel 		return;
   1122     0  stevel 
   1123     0  stevel 	do {
   1124     0  stevel 		if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
   1125     0  stevel 			break;
   1126     0  stevel 		}
   1127     0  stevel 		if (!atmark) {
   1128     0  stevel 			/*
   1129     0  stevel 			 * Urgent data not here yet.
   1130     0  stevel 			 * It may not be possible to send it yet
   1131     0  stevel 			 * if we are blocked for output
   1132     0  stevel 			 * and our input buffer is full.
   1133     0  stevel 			 */
   1134     0  stevel 			if (rcvcnt < sizeof (rcvbuf)) {
   1135     0  stevel 				n = desread(rem, rcvbuf + rcvcnt,
   1136     0  stevel 					sizeof (rcvbuf) - rcvcnt, 0);
   1137     0  stevel 				if (n <= 0)
   1138     0  stevel 					return;
   1139     0  stevel 				rcvd += n;
   1140     0  stevel 				rcvcnt += n;
   1141     0  stevel 			} else {
   1142     0  stevel 				/*
   1143     0  stevel 				 * We still haven't gotten to the urgent mark
   1144     0  stevel 				 * and we're out of buffer space.  Since we
   1145     0  stevel 				 * must clear our receive window to allow it
   1146     0  stevel 				 * to arrive, we will have to throw away
   1147     0  stevel 				 * these bytes.
   1148     0  stevel 				 */
   1149     0  stevel 				n = desread(rem, waste, sizeof (waste), 0);
   1150     0  stevel 				if (n <= 0)
   1151     0  stevel 					return;
   1152     0  stevel 			}
   1153     0  stevel 		}
   1154     0  stevel 	} while (atmark == 0);
   1155     0  stevel 	while (recv(rem, &mark, 1, MSG_OOB) < 0) {
   1156     0  stevel 		switch (errno) {
   1157     0  stevel 
   1158     0  stevel 		case EWOULDBLOCK:
   1159     0  stevel 			/*
   1160     0  stevel 			 * We've reached the urgent mark, so the next
   1161     0  stevel 			 * data to arrive will be the urgent, but it must
   1162     0  stevel 			 * not have arrived yet.
   1163     0  stevel 			 */
   1164     0  stevel 			(void) sleep(1);
   1165     0  stevel 			continue;
   1166     0  stevel 
   1167     0  stevel 		default:
   1168     0  stevel 			return;
   1169     0  stevel 		}
   1170     0  stevel 	}
   1171     0  stevel 	if (mark & TIOCPKT_WINDOW) {
   1172     0  stevel 		/*
   1173     0  stevel 		 * Let server know about window size changes
   1174     0  stevel 		 */
   1175     0  stevel 		(void) kill(ppid, SIGUSR1);
   1176     0  stevel 	}
   1177     0  stevel 	if (!eight && (mark & TIOCPKT_NOSTOP)) {
   1178     0  stevel 		if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
   1179     0  stevel 			perror("ioctl TIOCGETP");
   1180     0  stevel 		sb.sg_flags &= ~O_CBREAK;
   1181     0  stevel 		sb.sg_flags |= O_RAW;
   1182     0  stevel 		if (compat_ioctl(STDIN_FILENO, TIOCSETP, &sb) == -1)
   1183     0  stevel 			perror("ioctl TIOCSETP 1");
   1184     0  stevel 		notc.t_stopc = -1;
   1185     0  stevel 		notc.t_startc = -1;
   1186     0  stevel 		if (compat_ioctl(STDIN_FILENO, TIOCSETC, &notc) == -1)
   1187     0  stevel 			perror("ioctl TIOCSETC");
   1188     0  stevel 	}
   1189     0  stevel 	if (!eight && (mark & TIOCPKT_DOSTOP)) {
   1190     0  stevel 		if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
   1191     0  stevel 			perror("ioctl TIOCGETP");
   1192     0  stevel 		sb.sg_flags &= ~O_RAW;
   1193     0  stevel 		sb.sg_flags |= O_CBREAK;
   1194     0  stevel 		if (compat_ioctl(STDIN_FILENO, TIOCSETP, &sb) == -1)
   1195     0  stevel 			perror("ioctl TIOCSETP 2");
   1196     0  stevel 		notc.t_stopc = deftc.t_stopc;
   1197     0  stevel 		notc.t_startc = deftc.t_startc;
   1198     0  stevel 		if (compat_ioctl(STDIN_FILENO, TIOCSETC, &notc) == -1)
   1199     0  stevel 			perror("ioctl TIOCSETC");
   1200     0  stevel 	}
   1201     0  stevel 	if (mark & TIOCPKT_FLUSHWRITE) {
   1202     0  stevel 		if (ioctl(STDOUT_FILENO, TIOCFLUSH, (char *)&out) == -1)
   1203     0  stevel 			perror("ioctl TIOCFLUSH");
   1204     0  stevel 		for (;;) {
   1205     0  stevel 			if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
   1206     0  stevel 				perror("ioctl SIOCATMARK");
   1207     0  stevel 				break;
   1208     0  stevel 			}
   1209     0  stevel 			if (atmark)
   1210     0  stevel 				break;
   1211     0  stevel 			n = desread(rem, waste, sizeof (waste), 0);
   1212     0  stevel 			if (n <= 0) {
   1213     0  stevel 				if (n < 0)
   1214     0  stevel 					prf(gettext(
   1215     0  stevel 					    "Read error from network: %s"),
   1216   634      dp 					    strerror(errno));
   1217     0  stevel 				break;
   1218     0  stevel 			}
   1219     0  stevel 		}
   1220     0  stevel 		/*
   1221     0  stevel 		 * Don't want any pending data to be output,
   1222     0  stevel 		 * so clear the recv buffer.
   1223     0  stevel 		 * If we were hanging on a write when interrupted,
   1224     0  stevel 		 * don't want it to restart.  If we were reading,
   1225     0  stevel 		 * restart anyway.
   1226     0  stevel 		 */
   1227     0  stevel 		rcvcnt = 0;
   1228     0  stevel 		longjmp(rcvtop, 1);
   1229     0  stevel 	}
   1230     0  stevel 	/*
   1231     0  stevel 	 * If we filled the receive buffer while a read was pending,
   1232     0  stevel 	 * longjmp to the top to restart appropriately.  Don't abort
   1233     0  stevel 	 * a pending write, however, or we won't know how much was written.
   1234     0  stevel 	 */
   1235     0  stevel 	if (rcvd && rcvstate == READING)
   1236     0  stevel 		longjmp(rcvtop, 1);
   1237     0  stevel }
   1238     0  stevel 
   1239     0  stevel /*
   1240     0  stevel  * reader: read from remote: line -> 1
   1241     0  stevel  */
   1242     0  stevel static int
   1243     0  stevel reader(int oldmask)
   1244     0  stevel {
   1245     0  stevel 	/*
   1246     0  stevel 	 * 4.3bsd or later and SunOS 4.0 or later use the posiitive
   1247     0  stevel 	 * pid; otherwise use the negative.
   1248     0  stevel 	 */
   1249     0  stevel 	pid_t pid = getpid();
   1250     0  stevel 	int n, remaining;
   1251     0  stevel 	char *bufp = rcvbuf;
   1252     0  stevel 
   1253     0  stevel 	(void) sigset(SIGTTOU, SIG_IGN);
   1254     0  stevel 	(void) sigset(SIGURG, (void (*)())oob);
   1255     0  stevel 	ppid = getppid();
   1256     0  stevel 	if (fcntl(rem, F_SETOWN, pid) == -1)
   1257     0  stevel 		perror("fcntl F_SETOWN");
   1258     0  stevel 	/*
   1259     0  stevel 	 * A SIGURG may have been posted before we were completely forked,
   1260     0  stevel 	 * which means we may not have received it. To insure we do not miss
   1261     0  stevel 	 * any urgent data, we force the signal. The signal hander will be
   1262     0  stevel 	 * able to determine if in fact there is urgent data or not.
   1263     0  stevel 	 */
   1264     0  stevel 	(void) kill(pid, SIGURG);
   1265     0  stevel 	(void) setjmp(rcvtop);
   1266     0  stevel 	sigsetmask(oldmask);
   1267     0  stevel 	for (;;) {
   1268     0  stevel 		while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
   1269     0  stevel 			rcvstate = WRITING;
   1270     0  stevel 			n = write(STDOUT_FILENO, bufp, remaining);
   1271     0  stevel 			if (n < 0) {
   1272     0  stevel 				if (errno != EINTR) {
   1273     0  stevel 					prf(gettext(
   1274     0  stevel 					    "Write error to terminal: %s"),
   1275   634      dp 					    strerror(errno));
   1276     0  stevel 					return (-1);
   1277     0  stevel 				}
   1278     0  stevel 				continue;
   1279     0  stevel 			}
   1280     0  stevel 			bufp += n;
   1281     0  stevel 		}
   1282     0  stevel 		bufp = rcvbuf;
   1283     0  stevel 		rcvcnt = 0;
   1284     0  stevel 		rcvstate = READING;
   1285     0  stevel 		rcvcnt = desread(rem, rcvbuf, sizeof (rcvbuf), 0);
   1286     0  stevel 		if (rcvcnt == 0)
   1287     0  stevel 			return (0);
   1288     0  stevel 		if (rcvcnt < 0) {
   1289     0  stevel 			if (errno == EINTR)
   1290     0  stevel 				continue;
   1291     0  stevel 			prf(gettext("Read error from network: %s"),
   1292   634      dp 			    strerror(errno));
   1293     0  stevel 			return (-1);
   1294     0  stevel 		}
   1295     0  stevel 	}
   1296     0  stevel }
   1297     0  stevel 
   1298     0  stevel static void
   1299     0  stevel mode(int f)
   1300     0  stevel {
   1301     0  stevel 	struct tchars *tc;
   1302     0  stevel 	struct ltchars *ltc;
   1303     0  stevel 	struct sgttyb sb;
   1304     0  stevel 	int	lflags;
   1305     0  stevel 
   1306     0  stevel 	if (ioctl(STDIN_FILENO, TIOCGETP, (char *)&sb) == -1)
   1307     0  stevel 		perror("ioctl TIOCGETP");
   1308     0  stevel 	if (ioctl(STDIN_FILENO, TIOCLGET, (char *)&lflags) == -1)
   1309     0  stevel 		perror("ioctl TIOCLGET");
   1310     0  stevel 	switch (f) {
   1311     0  stevel 
   1312     0  stevel 	case 0:
   1313     0  stevel 		sb.sg_flags &= ~(O_CBREAK|O_RAW|O_TBDELAY);
   1314     0  stevel 		sb.sg_flags |= defflags|tabflag;
   1315     0  stevel 		tc = &deftc;
   1316     0  stevel 		ltc = &defltc;
   1317     0  stevel 		sb.sg_kill = defkill;
   1318     0  stevel 		sb.sg_erase = deferase;
   1319     0  stevel 		lflags = deflflags;
   1320     0  stevel 		break;
   1321     0  stevel 
   1322     0  stevel 	case 1:
   1323     0  stevel 		sb.sg_flags |= (eight ? O_RAW : O_CBREAK);
   1324     0  stevel 		sb.sg_flags &= ~defflags;
   1325     0  stevel 		/* preserve tab delays, but turn off XTABS */
   1326     0  stevel 		if ((sb.sg_flags & O_TBDELAY) == O_XTABS)
   1327     0  stevel 			sb.sg_flags &= ~O_TBDELAY;
   1328     0  stevel 		tc = &notc;
   1329     0  stevel 		ltc = &noltc;
   1330     0  stevel 		sb.sg_kill = sb.sg_erase = -1;
   1331     0  stevel 		if (litout)
   1332     0  stevel 			lflags |= LLITOUT;
   1333     0  stevel 		break;
   1334     0  stevel 
   1335     0  stevel 	default:
   1336     0  stevel 		/*NOTREACHED*/
   1337     0  stevel 		return;
   1338     0  stevel 	}
   1339     0  stevel 	if (compat_ioctl(STDIN_FILENO, TIOCSLTC, ltc) == -1)
   1340     0  stevel 		perror("ioctl TIOCSLTC");
   1341     0  stevel 	if (compat_ioctl(STDIN_FILENO, TIOCSETC, tc) == -1)
   1342     0  stevel 		perror("ioctl TIOCSETC");
   1343     0  stevel 	if (compat_ioctl(STDIN_FILENO, TIOCSETP, &sb) == -1)
   1344     0  stevel 		perror("ioctl TIOCSETP 3");
   1345     0  stevel 	if (compat_ioctl(STDIN_FILENO, TIOCLSET, &lflags) == -1)
   1346     0  stevel 		perror("ioctl TIOCLSET");
   1347     0  stevel }
   1348     0  stevel 
   1349     0  stevel /* PRINTFLIKE(0) */
   1350     0  stevel static void
   1351     0  stevel prf(const char *format, ...)
   1352     0  stevel {
   1353     0  stevel 	va_list	ap;
   1354     0  stevel 
   1355     0  stevel 	va_start(ap, format);
   1356     0  stevel 	(void) vfprintf(stderr, format, ap);
   1357     0  stevel 	va_end(ap);
   1358     0  stevel 	(void) fputs(CRLF, stderr);
   1359     0  stevel }
   1360     0  stevel 
   1361     0  stevel static void
   1362     0  stevel lostpeer(void)
   1363     0  stevel {
   1364     0  stevel 	(void) sigset(SIGPIPE, SIG_IGN);
   1365     0  stevel 	prf(gettext("\aConnection to %.*s closed."), MAXHOSTNAMELEN, host);
   1366     0  stevel 	done(EXIT_FAILURE);
   1367     0  stevel }
   1368     0  stevel 
   1369     0  stevel static int
   1370     0  stevel compat_ioctl(int des, int request, void *arg)
   1371     0  stevel {
   1372     0  stevel 	struct termios	tb;
   1373     0  stevel 	boolean_t	flag = B_FALSE;
   1374     0  stevel 
   1375     0  stevel 	if (ioctl(des, request, arg) < 0)
   1376     0  stevel 		return (-1);
   1377     0  stevel 
   1378     0  stevel 	if (tcgetattr(des, &tb) < 0)
   1379     0  stevel 		return (-1);
   1380     0  stevel 
   1381     0  stevel 	if (cfgetispeed(&tb) != cfgetispeed(&savetty)) {
   1382     0  stevel 		(void) cfsetispeed(&tb, cfgetispeed(&savetty));
   1383     0  stevel 		flag = B_TRUE;
   1384     0  stevel 	}
   1385     0  stevel 	if (cfgetospeed(&tb) != cfgetospeed(&savetty)) {
   1386     0  stevel 		(void) cfsetospeed(&tb, cfgetospeed(&savetty));
   1387     0  stevel 		flag = B_TRUE;
   1388     0  stevel 	}
   1389     0  stevel 
   1390     0  stevel 	return (flag ? tcsetattr(des, TCSANOW, &tb) : 0);
   1391     0  stevel }
   1392