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