Home | History | Annotate | Line # | Download | only in rlogin
rlogin.c revision 1.34
      1 /*	$NetBSD: rlogin.c,v 1.34 2005/01/08 03:23:15 ginsbach Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1993\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)rlogin.c	8.4 (Berkeley) 4/29/95";
     41 #else
     42 __RCSID("$NetBSD: rlogin.c,v 1.34 2005/01/08 03:23:15 ginsbach Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 /*
     47  * rlogin - remote login
     48  */
     49 #include <sys/param.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/socket.h>
     52 #include <sys/time.h>
     53 #include <sys/resource.h>
     54 #include <sys/wait.h>
     55 #include <sys/ioctl.h>
     56 
     57 #include <netinet/in.h>
     58 #include <netinet/in_systm.h>
     59 #include <netinet/ip.h>
     60 
     61 #include <err.h>
     62 #include <errno.h>
     63 #include <fcntl.h>
     64 #include <netdb.h>
     65 #include <pwd.h>
     66 #include <setjmp.h>
     67 #include <signal.h>
     68 #include <stdarg.h>
     69 #include <stdio.h>
     70 #include <stdlib.h>
     71 #include <string.h>
     72 #include <termios.h>
     73 #include <unistd.h>
     74 
     75 #include "getport.h"
     76 
     77 #ifdef KERBEROS
     78 #include <kerberosIV/des.h>
     79 #include <kerberosIV/krb.h>
     80 #include <kerberosIV/kstream.h>
     81 
     82 #include "krb.h"
     83 
     84 CREDENTIALS cred;
     85 Key_schedule schedule;
     86 MSG_DAT msg_data;
     87 struct sockaddr_in local, foreign;
     88 int use_kerberos = 1, doencrypt;
     89 kstream krem;
     90 #endif
     91 
     92 #ifndef TIOCPKT_WINDOW
     93 #define	TIOCPKT_WINDOW	0x80
     94 #endif
     95 
     96 /* concession to Sun */
     97 #ifndef SIGUSR1
     98 #define	SIGUSR1	30
     99 #endif
    100 
    101 #ifndef CCEQ
    102 #define CCEQ(val, c)	(c == val ? val != _POSIX_VDISABLE : 0)
    103 #endif
    104 
    105 int eight, rem;
    106 struct termios deftty;
    107 
    108 int noescape;
    109 u_char escapechar = '~';
    110 
    111 #ifdef OLDSUN
    112 struct winsize {
    113 	unsigned short ws_row, ws_col;
    114 	unsigned short ws_xpixel, ws_ypixel;
    115 };
    116 #else
    117 #define	get_window_size(fd, wp)	ioctl(fd, TIOCGWINSZ, wp)
    118 #endif
    119 struct	winsize winsize;
    120 
    121 void		catch_child(int);
    122 void		copytochild(int);
    123 void		doit(sigset_t *);
    124 void		done(int);
    125 void		echo(int);
    126 u_int		getescape(char *);
    127 void		lostpeer(int);
    128 int		main(int, char **);
    129 void		mode(int);
    130 void		msg(const char *);
    131 void		oob(int);
    132 int		reader(sigset_t *);
    133 void		sendwindow(void);
    134 void		setsignal(int);
    135 void		sigwinch(int);
    136 void		stop(int);
    137 void		usage(void);
    138 void		writer(void);
    139 void		writeroob(int);
    140 
    141 #ifdef	KERBEROS
    142 void		warning(const char *, ...);
    143 #endif
    144 #ifdef OLDSUN
    145 int		get_window_size(int, struct winsize *);
    146 #endif
    147 
    148 int
    149 main(int argc, char *argv[])
    150 {
    151 	struct passwd *pw;
    152 	struct servent *sp;
    153 	struct termios tty;
    154 	sigset_t smask;
    155 	uid_t uid;
    156 	int argoff, ch, dflag, one;
    157 	int i, len, len2;
    158 	char *host, *p, *user, *name, term[1024] = "network";
    159 	speed_t ospeed;
    160 	struct sigaction sa;
    161 	char *service = NULL;
    162 	struct rlimit rlim;
    163 #ifdef KERBEROS
    164 	KTEXT_ST ticket;
    165 	int sock;
    166 	long authopts;
    167 	int through_once = 0;
    168 	extern int _kstream_des_debug_OOB;
    169 	char *dest_realm = NULL;
    170 #endif
    171 
    172 	argoff = dflag = 0;
    173 	one = 1;
    174 	host = user = NULL;
    175 	sp = NULL;
    176 
    177 	if (strcmp(getprogname(), "rlogin") != 0) {
    178 		host = strdup(getprogname());
    179 		if (host == NULL)
    180 			err(1, NULL);
    181 	}
    182 
    183 	/* handle "rlogin host flags" */
    184 	if (!host && argc > 2 && argv[1][0] != '-') {
    185 		host = argv[1];
    186 		argoff = 1;
    187 	}
    188 
    189 #ifdef KERBEROS
    190 #define	OPTIONS	"8EKde:p:k:l:x"
    191 #else
    192 #define	OPTIONS	"8EKde:p:l:"
    193 #endif
    194 	while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
    195 		switch(ch) {
    196 		case '8':
    197 			eight = 1;
    198 			break;
    199 		case 'E':
    200 			noescape = 1;
    201 			break;
    202 #ifdef KERBEROS
    203 		case 'K':
    204 			use_kerberos = 0;
    205 			break;
    206 #endif
    207 		case 'd':
    208 #ifdef KERBEROS
    209 			_kstream_des_debug_OOB = 1;
    210 #endif
    211 			dflag = 1;
    212 			break;
    213 		case 'e':
    214 			noescape = 0;
    215 			escapechar = getescape(optarg);
    216 			break;
    217 #ifdef KERBEROS
    218 		case 'k':
    219 			dest_realm = optarg;
    220 			break;
    221 #endif
    222 		case 'l':
    223 			user = optarg;
    224 			break;
    225 		case 'p':
    226 			sp = getport(service = optarg, "tcp");
    227 			break;
    228 #ifdef CRYPT
    229 #ifdef KERBEROS
    230 		case 'x':
    231 			doencrypt = 1;
    232 			break;
    233 #endif
    234 #endif
    235 		case '?':
    236 		default:
    237 			usage();
    238 		}
    239 	optind += argoff;
    240 	argc -= optind;
    241 	argv += optind;
    242 
    243 	/* if haven't gotten a host yet, do so */
    244 	if (!host && !(host = *argv++))
    245 		usage();
    246 
    247 	if (*argv)
    248 		usage();
    249 
    250 	if (!(pw = getpwuid(uid = getuid())))
    251 		errx(1, "unknown user id.");
    252 	/* Accept user1@host format, though "-l user2" overrides user1 */
    253 	p = strchr(host, '@');
    254 	if (p) {
    255 		*p = '\0';
    256 		if (!user && p > host)
    257 			user = host;
    258 		host = p + 1;
    259 		if (*host == '\0')
    260 			usage();
    261 	}
    262 	if ((name = strdup(pw->pw_name)) == NULL)
    263 		err(1, "malloc");
    264 	if (!user)
    265 		user = name;
    266 
    267 #ifdef KERBEROS
    268 	if (use_kerberos) {
    269 		if (sp == NULL) {
    270 			sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
    271 		}
    272 		if (sp == NULL) {
    273 			use_kerberos = 0;
    274 			warning("can't get entry for %s/tcp service",
    275 			    doencrypt ? "eklogin" : "klogin");
    276 		}
    277 	}
    278 #endif
    279 	if (sp == NULL)
    280 		sp = getservbyname("login", "tcp");
    281 	if (sp == NULL)
    282 		errx(1, "login/tcp: unknown service.");
    283 
    284 	if ((p = getenv("TERM")) != NULL)
    285 		(void)strlcpy(term, p, sizeof(term));
    286 	len = strlen(term);
    287 	if (len < (sizeof(term) - 1) && tcgetattr(0, &tty) == 0) {
    288 		/* start at 2 to include the / */
    289 		for (ospeed = i = cfgetospeed(&tty), len2 = 2; i > 9; len2++)
    290 			i /= 10;
    291 
    292 		if (len + len2 < sizeof(term))
    293 			(void)snprintf(term + len, len2 + 1, "/%d", ospeed);
    294 	}
    295 
    296 	(void)get_window_size(0, &winsize);
    297 
    298 	sigemptyset(&sa.sa_mask);
    299 	sa.sa_flags = SA_RESTART;
    300 	sa.sa_handler = lostpeer;
    301 	(void)sigaction(SIGPIPE, &sa, (struct sigaction *)0);
    302 	/* will use SIGUSR1 for window size hack, so hold it off */
    303 	sigemptyset(&smask);
    304 	sigaddset(&smask, SIGURG);
    305 	sigaddset(&smask, SIGUSR1);
    306 	(void)sigprocmask(SIG_SETMASK, &smask, &smask);
    307 	/*
    308 	 * We set SIGURG and SIGUSR1 below so that an
    309 	 * incoming signal will be held pending rather than being
    310 	 * discarded. Note that these routines will be ready to get
    311 	 * a signal by the time that they are unblocked below.
    312 	 */
    313 	sa.sa_handler = copytochild;
    314 	(void)sigaction(SIGURG, &sa, (struct sigaction *) 0);
    315 	sa.sa_handler = writeroob;
    316 	(void)sigaction(SIGUSR1, &sa, (struct sigaction *) 0);
    317 
    318 	/* don't dump core */
    319 	rlim.rlim_cur = rlim.rlim_max = 0;
    320 	if (setrlimit(RLIMIT_CORE, &rlim) < 0)
    321 		warn("setrlimit");
    322 
    323 #ifdef KERBEROS
    324 try_connect:
    325 	if (use_kerberos) {
    326 		struct hostent *hp;
    327 
    328 		/* Fully qualify hostname (needed for krb_realmofhost). */
    329 		hp = gethostbyname(host);
    330 		if (hp != NULL && !(host = strdup(hp->h_name)))
    331 			errx(1, "%s", strerror(ENOMEM));
    332 
    333 		rem = KSUCCESS;
    334 		errno = 0;
    335 #ifdef CRYPT
    336 		if (doencrypt)
    337 			authopts = KOPT_DO_MUTUAL;
    338 		else
    339 #endif /* CRYPT */
    340 			authopts = 0L;
    341 
    342 		if (dest_realm == NULL) {
    343 			/* default this now, once. */
    344 			if (!(dest_realm = krb_realmofhost (host))) {
    345 				warnx("Unknown realm for host %s.", host);
    346 				use_kerberos = 0;
    347 				if (service != NULL)
    348 					sp = getservbyname("login", "tcp");
    349 				goto try_connect;
    350 			}
    351 		}
    352 
    353 		rem = kcmd(&sock, &host, sp->s_port, name, user,
    354 			   term, 0, &ticket, "rcmd", dest_realm,
    355 			   &cred, schedule, &msg_data, &local, &foreign,
    356 			   authopts);
    357 
    358 		if (rem != KSUCCESS) {
    359 			switch(rem) {
    360 
    361 				case KDC_PR_UNKNOWN:
    362 					warnx("Host %s not registered for %s",
    363 				       	      host, "Kerberos rlogin service");
    364 					use_kerberos = 0;
    365 					if (service != NULL)
    366 						sp = getservbyname("login", "tcp");
    367 					goto try_connect;
    368 				case NO_TKT_FIL:
    369 					if (through_once++) {
    370 						use_kerberos = 0;
    371 						if (service != NULL)
    372 							sp = getservbyname("login", "tcp");
    373 						goto try_connect;
    374 					}
    375 #ifdef notyet
    376 				krb_get_pw_in_tkt(user, krb_realm, "krbtgt",
    377 						  krb_realm,
    378 					          DEFAULT_TKT_LIFE/5, 0);
    379 				goto try_connect;
    380 #endif
    381 			default:
    382 				warnx("Kerberos rcmd failed: %s",
    383 				      (rem == -1) ? "rcmd protocol failure" :
    384 				      krb_err_txt[rem]);
    385 				use_kerberos = 0;
    386 				if (service != NULL)
    387 					sp = getservbyname("login", "tcp");
    388 				goto try_connect;
    389 			}
    390 		}
    391 		rem = sock;
    392 		if (doencrypt)
    393 			krem = kstream_create_rlogin_from_fd(rem, &schedule,
    394 							     &cred.session);
    395 		else
    396 			krem = kstream_create_from_fd(rem, 0, 0);
    397 			kstream_set_buffer_mode(krem, 0);
    398 	} else {
    399 #ifdef CRYPT
    400 		if (doencrypt)
    401 			errx(1, "the -x flag requires Kerberos authentication.");
    402 #endif /* CRYPT */
    403 		rem = rcmd_af(&host, sp->s_port, name, user, term, 0,
    404 		    PF_UNSPEC);
    405 		if (rem < 0)
    406 			exit(1);
    407 	}
    408 #else
    409 	rem = rcmd_af(&host, sp->s_port, name, user, term, 0, PF_UNSPEC);
    410 
    411 #endif /* KERBEROS */
    412 
    413 	if (rem < 0)
    414 		exit(1);
    415 
    416 	if (dflag &&
    417 	    setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
    418 		warn("setsockopt DEBUG (ignored)");
    419     {
    420 	struct sockaddr_storage ss;
    421 	int sslen;
    422 	sslen = sizeof(ss);
    423 	if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0
    424 	 && ((struct sockaddr *)&ss)->sa_family == AF_INET) {
    425 		one = IPTOS_LOWDELAY;
    426 		if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
    427 				sizeof(int)) < 0) {
    428 			warn("setsockopt TOS (ignored)");
    429 		}
    430 	}
    431     }
    432 
    433 	(void)setuid(uid);
    434 	doit(&smask);
    435 	/*NOTREACHED*/
    436 	return (0);
    437 }
    438 
    439 pid_t child;
    440 
    441 void
    442 doit(sigset_t *smask)
    443 {
    444 	struct sigaction sa;
    445 
    446 	sigemptyset(&sa.sa_mask);
    447 	sa.sa_flags = SA_RESTART;
    448 	sa.sa_handler = SIG_IGN;
    449 	(void)sigaction(SIGINT, &sa, (struct sigaction *) 0);
    450 	setsignal(SIGHUP);
    451 	setsignal(SIGQUIT);
    452 	mode(1);
    453 	child = fork();
    454 	if (child == -1) {
    455 		warn("fork");
    456 		done(1);
    457 	}
    458 	if (child == 0) {
    459 		mode(1);
    460 		if (reader(smask) == 0) {
    461 			msg("connection closed.");
    462 			exit(0);
    463 		}
    464 		sleep(1);
    465 		msg("\aconnection closed.");
    466 		exit(1);
    467 	}
    468 
    469 	/*
    470 	 * We may still own the socket, and may have a pending SIGURG (or might
    471 	 * receive one soon) that we really want to send to the reader.  When
    472 	 * one of these comes in, the trap copytochild simply copies such
    473 	 * signals to the child. We can now unblock SIGURG and SIGUSR1
    474 	 * that were set above.
    475 	 */
    476 	(void)sigprocmask(SIG_SETMASK, smask, (sigset_t *) 0);
    477 	sa.sa_handler = catch_child;
    478 	(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
    479 	writer();
    480 	msg("closed connection.");
    481 	done(0);
    482 }
    483 
    484 /* trap a signal, unless it is being ignored. */
    485 void
    486 setsignal(int sig)
    487 {
    488 	struct sigaction sa;
    489 	sigset_t sigs;
    490 
    491 	sigemptyset(&sigs);
    492 	sigaddset(&sigs, sig);
    493 	sigprocmask(SIG_BLOCK, &sigs, &sigs);
    494 
    495 	sigemptyset(&sa.sa_mask);
    496 	sa.sa_handler = exit;
    497 	sa.sa_flags = SA_RESTART;
    498 	(void)sigaction(sig, &sa, &sa);
    499 	if (sa.sa_handler == SIG_IGN)
    500 		(void)sigaction(sig, &sa, (struct sigaction *) 0);
    501 
    502 	(void)sigprocmask(SIG_SETMASK, &sigs, (sigset_t *) 0);
    503 }
    504 
    505 void
    506 done(int status)
    507 {
    508 	pid_t w;
    509 	int wstatus;
    510 	struct sigaction sa;
    511 
    512 	mode(0);
    513 	if (child > 0) {
    514 		/* make sure catch_child does not snap it up */
    515 		sigemptyset(&sa.sa_mask);
    516 		sa.sa_handler = SIG_DFL;
    517 		sa.sa_flags = 0;
    518 		(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
    519 		if (kill(child, SIGKILL) >= 0)
    520 			while ((w = wait(&wstatus)) > 0 && w != child)
    521 				continue;
    522 	}
    523 	exit(status);
    524 }
    525 
    526 int dosigwinch;
    527 
    528 /*
    529  * This is called when the reader process gets the out-of-band (urgent)
    530  * request to turn on the window-changing protocol.
    531  */
    532 void
    533 writeroob(int signo)
    534 {
    535 	struct sigaction sa;
    536 
    537 	if (dosigwinch == 0) {
    538 		sendwindow();
    539 		sigemptyset(&sa.sa_mask);
    540 		sa.sa_handler = sigwinch;
    541 		sa.sa_flags = SA_RESTART;
    542 		(void)sigaction(SIGWINCH, &sa, (struct sigaction *) 0);
    543 	}
    544 	dosigwinch = 1;
    545 }
    546 
    547 void
    548 catch_child(int signo)
    549 {
    550 	int status;
    551 	pid_t pid;
    552 
    553 	for (;;) {
    554 		pid = waitpid(-1, &status, WNOHANG|WUNTRACED);
    555 		if (pid == 0)
    556 			return;
    557 		/* if the child (reader) dies, just quit */
    558 		if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
    559 			done(WEXITSTATUS(status) | WTERMSIG(status));
    560 	}
    561 	/* NOTREACHED */
    562 }
    563 
    564 /*
    565  * writer: write to remote: 0 -> line.
    566  * ~.				terminate
    567  * ~^Z				suspend rlogin process.
    568  * ~<delayed-suspend char>	suspend rlogin process, but leave reader alone.
    569  */
    570 void
    571 writer(void)
    572 {
    573 	int bol, local, n;
    574 	char c;
    575 
    576 	bol = 1;			/* beginning of line */
    577 	local = 0;
    578 	for (;;) {
    579 		n = read(STDIN_FILENO, &c, 1);
    580 		if (n <= 0) {
    581 			if (n < 0 && errno == EINTR)
    582 				continue;
    583 			break;
    584 		}
    585 		/*
    586 		 * If we're at the beginning of the line and recognize a
    587 		 * command character, then we echo locally.  Otherwise,
    588 		 * characters are echo'd remotely.  If the command character
    589 		 * is doubled, this acts as a force and local echo is
    590 		 * suppressed.
    591 		 */
    592 		if (bol) {
    593 			bol = 0;
    594 			if (!noescape && c == escapechar) {
    595 				local = 1;
    596 				continue;
    597 			}
    598 		} else if (local) {
    599 			local = 0;
    600 			if (c == '.' || CCEQ(deftty.c_cc[VEOF], c)) {
    601 				echo((int)c);
    602 				break;
    603 			}
    604 			if (CCEQ(deftty.c_cc[VSUSP], c)) {
    605 				bol = 1;
    606 				echo((int)c);
    607 				stop(1);
    608 				continue;
    609 			}
    610 			if (CCEQ(deftty.c_cc[VDSUSP], c)) {
    611 				bol = 1;
    612 				echo((int)c);
    613 				stop(0);
    614 				continue;
    615 			}
    616 			if (c != escapechar) {
    617 #ifdef KERBEROS
    618 				if (use_kerberos)
    619 					(void)kstream_write(krem,
    620 					    (char *)&escapechar, 1);
    621 				else
    622 #endif
    623 					(void)write(rem, &escapechar, 1);
    624 			}
    625 		}
    626 
    627 #ifdef KERBEROS
    628 		if (use_kerberos) {
    629 			if (kstream_write(krem, &c, 1) == 0) {
    630 					msg("line gone");
    631 					break;
    632 			}
    633 		}
    634 		else
    635 #endif
    636 			if (write(rem, &c, 1) == 0) {
    637 				msg("line gone");
    638 				break;
    639 			}
    640 
    641 		bol = CCEQ(deftty.c_cc[VKILL], c) ||
    642 		    CCEQ(deftty.c_cc[VEOF], c) ||
    643 		    CCEQ(deftty.c_cc[VINTR], c) ||
    644 		    CCEQ(deftty.c_cc[VSUSP], c) ||
    645 		    c == '\r' || c == '\n';
    646 	}
    647 }
    648 
    649 void
    650 echo(int i)
    651 {
    652 	char c = (char)i;
    653 	char *p;
    654 	char buf[8];
    655 
    656 	p = buf;
    657 	c &= 0177;
    658 	*p++ = escapechar;
    659 	if (c < ' ') {
    660 		*p++ = '^';
    661 		*p++ = c + '@';
    662 	} else if (c == 0177) {
    663 		*p++ = '^';
    664 		*p++ = '?';
    665 	} else
    666 		*p++ = c;
    667 	*p++ = '\r';
    668 	*p++ = '\n';
    669 	(void)write(STDOUT_FILENO, buf, p - buf);
    670 }
    671 
    672 void
    673 stop(int all)
    674 {
    675 	struct sigaction sa;
    676 
    677 	mode(0);
    678 	sigemptyset(&sa.sa_mask);
    679 	sa.sa_handler = SIG_IGN;
    680 	sa.sa_flags = SA_RESTART;
    681 	(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
    682 	(void)kill(all ? 0 : getpid(), SIGTSTP);
    683 	sa.sa_handler = catch_child;
    684 	(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
    685 	mode(1);
    686 	sigwinch(0);			/* check for size changes */
    687 }
    688 
    689 void
    690 sigwinch(int signo)
    691 {
    692 	struct winsize ws;
    693 
    694 	if (dosigwinch && get_window_size(0, &ws) == 0 &&
    695 	    memcmp(&ws, &winsize, sizeof(ws))) {
    696 		winsize = ws;
    697 		sendwindow();
    698 	}
    699 }
    700 
    701 /*
    702  * Send the window size to the server via the magic escape
    703  */
    704 void
    705 sendwindow(void)
    706 {
    707 	struct winsize *wp;
    708 	char obuf[4 + sizeof (struct winsize)];
    709 
    710 	wp = (struct winsize *)(obuf+4);
    711 	obuf[0] = 0377;
    712 	obuf[1] = 0377;
    713 	obuf[2] = 's';
    714 	obuf[3] = 's';
    715 	wp->ws_row = htons(winsize.ws_row);
    716 	wp->ws_col = htons(winsize.ws_col);
    717 	wp->ws_xpixel = htons(winsize.ws_xpixel);
    718 	wp->ws_ypixel = htons(winsize.ws_ypixel);
    719 
    720 #ifdef KERBEROS
    721 		if (use_kerberos)
    722 			(void)kstream_write(krem, obuf, sizeof(obuf));
    723 		else
    724 #endif
    725 		(void)write(rem, obuf, sizeof(obuf));
    726 }
    727 
    728 /*
    729  * reader: read from remote: line -> 1
    730  */
    731 #define	READING	1
    732 #define	WRITING	2
    733 
    734 jmp_buf rcvtop;
    735 pid_t ppid;
    736 int rcvcnt, rcvstate;
    737 char rcvbuf[8 * 1024];
    738 
    739 void
    740 oob(int signo)
    741 {
    742 	struct termios tty;
    743 	int atmark, n, rcvd;
    744 	char waste[BUFSIZ], mark;
    745 
    746 	rcvd = 0;
    747 	while (recv(rem, &mark, 1, MSG_OOB) < 0) {
    748 		switch (errno) {
    749 		case EWOULDBLOCK:
    750 			/*
    751 			 * Urgent data not here yet.  It may not be possible
    752 			 * to send it yet if we are blocked for output and
    753 			 * our input buffer is full.
    754 			 */
    755 			if (rcvcnt < sizeof(rcvbuf)) {
    756 				n = read(rem, rcvbuf + rcvcnt,
    757 				    sizeof(rcvbuf) - rcvcnt);
    758 				if (n <= 0)
    759 					return;
    760 				rcvd += n;
    761 			} else {
    762 				n = read(rem, waste, sizeof(waste));
    763 				if (n <= 0)
    764 					return;
    765 			}
    766 			continue;
    767 		default:
    768 			return;
    769 		}
    770 	}
    771 	if (mark & TIOCPKT_WINDOW) {
    772 		/* Let server know about window size changes */
    773 		(void)kill(ppid, SIGUSR1);
    774 	}
    775 	if (!eight && (mark & TIOCPKT_NOSTOP)) {
    776 		(void)tcgetattr(0, &tty);
    777 		tty.c_iflag &= ~IXON;
    778 		(void)tcsetattr(0, TCSANOW, &tty);
    779 	}
    780 	if (!eight && (mark & TIOCPKT_DOSTOP)) {
    781 		(void)tcgetattr(0, &tty);
    782 		tty.c_iflag |= (deftty.c_iflag & IXON);
    783 		(void)tcsetattr(0, TCSANOW, &tty);
    784 	}
    785 	if (mark & TIOCPKT_FLUSHWRITE) {
    786 		(void)tcflush(1, TCIOFLUSH);
    787 		for (;;) {
    788 			if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
    789 				warn("ioctl SIOCATMARK (ignored)");
    790 				break;
    791 			}
    792 			if (atmark)
    793 				break;
    794 			n = read(rem, waste, sizeof (waste));
    795 			if (n <= 0)
    796 				break;
    797 		}
    798 		/*
    799 		 * Don't want any pending data to be output, so clear the recv
    800 		 * buffer.  If we were hanging on a write when interrupted,
    801 		 * don't want it to restart.  If we were reading, restart
    802 		 * anyway.
    803 		 */
    804 		rcvcnt = 0;
    805 		longjmp(rcvtop, 1);
    806 	}
    807 
    808 	/* oob does not do FLUSHREAD (alas!) */
    809 
    810 	/*
    811 	 * If we filled the receive buffer while a read was pending, longjmp
    812 	 * to the top to restart appropriately.  Don't abort a pending write,
    813 	 * however, or we won't know how much was written.
    814 	 */
    815 	if (rcvd && rcvstate == READING)
    816 		longjmp(rcvtop, 1);
    817 }
    818 
    819 /* reader: read from remote: line -> 1 */
    820 int
    821 reader(sigset_t *smask)
    822 {
    823 	pid_t pid;
    824 	int n, remaining;
    825 	char *bufp;
    826 	struct sigaction sa;
    827 
    828 	pid = getpid();		/* modern systems use positives for pid */
    829 	sigemptyset(&sa.sa_mask);
    830 	sa.sa_flags = SA_RESTART;
    831 	sa.sa_handler = SIG_IGN;
    832 	(void)sigaction(SIGTTOU, &sa, (struct sigaction *) 0);
    833 	sa.sa_handler = oob;
    834 	(void)sigaction(SIGURG, &sa, (struct sigaction *) 0);
    835 	ppid = getppid();
    836 	(void)fcntl(rem, F_SETOWN, pid);
    837 	(void)setjmp(rcvtop);
    838 	(void)sigprocmask(SIG_SETMASK, smask, (sigset_t *) 0);
    839 	bufp = rcvbuf;
    840 	for (;;) {
    841 		while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
    842 			rcvstate = WRITING;
    843 			n = write(STDOUT_FILENO, bufp, remaining);
    844 			if (n < 0) {
    845 				if (errno != EINTR)
    846 					return (-1);
    847 				continue;
    848 			}
    849 			bufp += n;
    850 		}
    851 		bufp = rcvbuf;
    852 		rcvcnt = 0;
    853 		rcvstate = READING;
    854 
    855 #ifdef KERBEROS
    856 			if (use_kerberos)
    857 				rcvcnt = kstream_read(krem, rcvbuf, sizeof(rcvbuf));
    858 			else
    859 #endif
    860 			rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
    861 
    862 		if (rcvcnt == 0)
    863 			return (0);
    864 		if (rcvcnt < 0) {
    865 			if (errno == EINTR)
    866 				continue;
    867 			warn("read");
    868 			return (-1);
    869 		}
    870 	}
    871 }
    872 
    873 void
    874 mode(int f)
    875 {
    876 	struct termios tty;
    877 
    878 	switch (f) {
    879 	case 0:
    880 		(void)tcsetattr(0, TCSANOW, &deftty);
    881 		break;
    882 	case 1:
    883 		(void)tcgetattr(0, &deftty);
    884 		tty = deftty;
    885 		/* This is loosely derived from sys/compat/tty_compat.c. */
    886 		tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
    887 		tty.c_iflag &= ~ICRNL;
    888 		tty.c_oflag &= ~OPOST;
    889 		tty.c_cc[VMIN] = 1;
    890 		tty.c_cc[VTIME] = 0;
    891 		if (eight) {
    892 			tty.c_iflag &= IXOFF;
    893 			tty.c_cflag &= ~(CSIZE|PARENB);
    894 			tty.c_cflag |= CS8;
    895 		}
    896 		(void)tcsetattr(0, TCSANOW, &tty);
    897 		break;
    898 
    899 	default:
    900 		return;
    901 	}
    902 }
    903 
    904 void
    905 lostpeer(int signo)
    906 {
    907 	struct sigaction sa;
    908 	sa.sa_flags = SA_RESTART;
    909 	sa.sa_handler = SIG_IGN;
    910 	(void)sigaction(SIGPIPE, &sa, (struct sigaction *)0);
    911 	msg("\aconnection closed.");
    912 	done(1);
    913 }
    914 
    915 /* copy SIGURGs to the child process. */
    916 void
    917 copytochild(int signo)
    918 {
    919 
    920 	(void)kill(child, SIGURG);
    921 }
    922 
    923 void
    924 msg(const char *str)
    925 {
    926 
    927 	(void)fprintf(stderr, "rlogin: %s\r\n", str);
    928 }
    929 
    930 #ifdef KERBEROS
    931 /* VARARGS */
    932 void
    933 warning(const char *fmt, ...)
    934 {
    935 	va_list ap;
    936 
    937 	(void)fprintf(stderr, "rlogin: warning, using standard rlogin: ");
    938 	va_start(ap, fmt);
    939 	vfprintf(stderr, fmt, ap);
    940 	va_end(ap);
    941 	(void)fprintf(stderr, ".\n");
    942 }
    943 #endif
    944 
    945 void
    946 usage(void)
    947 {
    948 	(void)fprintf(stderr,
    949 	    "usage: rlogin [-%s]%s[-e char] [-l username] [-p port] [username@]host\n",
    950 #ifdef KERBEROS
    951 #ifdef CRYPT
    952 	    "8EKdx", " [-k realm] ");
    953 #else
    954 	    "8EKd", " [-k realm] ");
    955 #endif
    956 #else
    957 	    "8Ed", " ");
    958 #endif
    959 	exit(1);
    960 }
    961 
    962 /*
    963  * The following routine provides compatibility (such as it is) between older
    964  * Suns and others.  Suns have only a `ttysize', so we convert it to a winsize.
    965  */
    966 #ifdef OLDSUN
    967 int
    968 get_window_size(fd, wp)
    969 	int fd;
    970 	struct winsize *wp;
    971 {
    972 	struct ttysize ts;
    973 	int error;
    974 
    975 	if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
    976 		return (error);
    977 	wp->ws_row = ts.ts_lines;
    978 	wp->ws_col = ts.ts_cols;
    979 	wp->ws_xpixel = 0;
    980 	wp->ws_ypixel = 0;
    981 	return (0);
    982 }
    983 #endif
    984 
    985 u_int
    986 getescape(char *p)
    987 {
    988 	long val;
    989 	int len;
    990 
    991 	if ((len = strlen(p)) == 1)	/* use any single char, including '\' */
    992 		return ((u_int)*p);
    993 					/* otherwise, \nnn */
    994 	if (*p == '\\' && len >= 2 && len <= 4) {
    995 		val = strtol(++p, NULL, 8);
    996 		for (;;) {
    997 			if (!*++p)
    998 				return ((u_int)val);
    999 			if (*p < '0' || *p > '8')
   1000 				break;
   1001 		}
   1002 	}
   1003 	msg("illegal option value -- e");
   1004 	usage();
   1005 	/* NOTREACHED */
   1006 	return (0);
   1007 }
   1008