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