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