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