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