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