Home | History | Annotate | Line # | Download | only in rlogin
rlogin.c revision 1.7
      1 /*	$NetBSD: rlogin.c,v 1.7 1995/05/17 13:35:50 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.7 1995/05/17 13:35:50 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((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)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 				bol = 1;
    498 				echo(c);
    499 				stop(1);
    500 				continue;
    501 			}
    502 			if (CCEQ(deftty.c_cc[VDSUSP], c)) {
    503 				bol = 1;
    504 				echo(c);
    505 				stop(0);
    506 				continue;
    507 			}
    508 			if (c != escapechar)
    509 #ifdef CRYPT
    510 #ifdef KERBEROS
    511 				if (doencrypt)
    512 					(void)des_write(rem,
    513 					    (char *)&escapechar, 1);
    514 				else
    515 #endif
    516 #endif
    517 					(void)write(rem, &escapechar, 1);
    518 		}
    519 
    520 #ifdef CRYPT
    521 #ifdef KERBEROS
    522 		if (doencrypt) {
    523 			if (des_write(rem, &c, 1) == 0) {
    524 				msg("line gone");
    525 				break;
    526 			}
    527 		} else
    528 #endif
    529 #endif
    530 			if (write(rem, &c, 1) == 0) {
    531 				msg("line gone");
    532 				break;
    533 			}
    534 		bol = CCEQ(deftty.c_cc[VKILL], c) ||
    535 		    CCEQ(deftty.c_cc[VEOF], c) ||
    536 		    CCEQ(deftty.c_cc[VINTR], c) ||
    537 		    CCEQ(deftty.c_cc[VSUSP], c) ||
    538 		    c == '\r' || c == '\n';
    539 	}
    540 }
    541 
    542 void
    543 #if __STDC__
    544 echo(register char c)
    545 #else
    546 echo(c)
    547 	register char c;
    548 #endif
    549 {
    550 	register char *p;
    551 	char buf[8];
    552 
    553 	p = buf;
    554 	c &= 0177;
    555 	*p++ = escapechar;
    556 	if (c < ' ') {
    557 		*p++ = '^';
    558 		*p++ = c + '@';
    559 	} else if (c == 0177) {
    560 		*p++ = '^';
    561 		*p++ = '?';
    562 	} else
    563 		*p++ = c;
    564 	*p++ = '\r';
    565 	*p++ = '\n';
    566 	(void)write(STDOUT_FILENO, buf, p - buf);
    567 }
    568 
    569 void
    570 stop(all)
    571 	int all;
    572 {
    573 	mode(0);
    574 	(void)signal(SIGCHLD, SIG_IGN);
    575 	(void)kill(all ? 0 : getpid(), SIGTSTP);
    576 	(void)signal(SIGCHLD, catch_child);
    577 	mode(1);
    578 	sigwinch(0);			/* check for size changes */
    579 }
    580 
    581 void
    582 sigwinch(signo)
    583 	int signo;
    584 {
    585 	struct winsize ws;
    586 
    587 	if (dosigwinch && get_window_size(0, &ws) == 0 &&
    588 	    bcmp(&ws, &winsize, sizeof(ws))) {
    589 		winsize = ws;
    590 		sendwindow();
    591 	}
    592 }
    593 
    594 /*
    595  * Send the window size to the server via the magic escape
    596  */
    597 void
    598 sendwindow()
    599 {
    600 	struct winsize *wp;
    601 	char obuf[4 + sizeof (struct winsize)];
    602 
    603 	wp = (struct winsize *)(obuf+4);
    604 	obuf[0] = 0377;
    605 	obuf[1] = 0377;
    606 	obuf[2] = 's';
    607 	obuf[3] = 's';
    608 	wp->ws_row = htons(winsize.ws_row);
    609 	wp->ws_col = htons(winsize.ws_col);
    610 	wp->ws_xpixel = htons(winsize.ws_xpixel);
    611 	wp->ws_ypixel = htons(winsize.ws_ypixel);
    612 
    613 #ifdef CRYPT
    614 #ifdef KERBEROS
    615 	if(doencrypt)
    616 		(void)des_write(rem, obuf, sizeof(obuf));
    617 	else
    618 #endif
    619 #endif
    620 		(void)write(rem, obuf, sizeof(obuf));
    621 }
    622 
    623 /*
    624  * reader: read from remote: line -> 1
    625  */
    626 #define	READING	1
    627 #define	WRITING	2
    628 
    629 jmp_buf rcvtop;
    630 int ppid, rcvcnt, rcvstate;
    631 char rcvbuf[8 * 1024];
    632 
    633 void
    634 oob(signo)
    635 	int signo;
    636 {
    637 	struct termios tty;
    638 	int atmark, n, out, rcvd;
    639 	char waste[BUFSIZ], mark;
    640 
    641 	out = O_RDWR;
    642 	rcvd = 0;
    643 	while (recv(rem, &mark, 1, MSG_OOB) < 0) {
    644 		switch (errno) {
    645 		case EWOULDBLOCK:
    646 			/*
    647 			 * Urgent data not here yet.  It may not be possible
    648 			 * to send it yet if we are blocked for output and
    649 			 * our input buffer is full.
    650 			 */
    651 			if (rcvcnt < sizeof(rcvbuf)) {
    652 				n = read(rem, rcvbuf + rcvcnt,
    653 				    sizeof(rcvbuf) - rcvcnt);
    654 				if (n <= 0)
    655 					return;
    656 				rcvd += n;
    657 			} else {
    658 				n = read(rem, waste, sizeof(waste));
    659 				if (n <= 0)
    660 					return;
    661 			}
    662 			continue;
    663 		default:
    664 			return;
    665 		}
    666 	}
    667 	if (mark & TIOCPKT_WINDOW) {
    668 		/* Let server know about window size changes */
    669 		(void)kill(ppid, SIGUSR1);
    670 	}
    671 	if (!eight && (mark & TIOCPKT_NOSTOP)) {
    672 		(void)tcgetattr(0, &tty);
    673 		tty.c_iflag &= ~IXON;
    674 		(void)tcsetattr(0, TCSANOW, &tty);
    675 	}
    676 	if (!eight && (mark & TIOCPKT_DOSTOP)) {
    677 		(void)tcgetattr(0, &tty);
    678 		tty.c_iflag |= (deftty.c_iflag & IXON);
    679 		(void)tcsetattr(0, TCSANOW, &tty);
    680 	}
    681 	if (mark & TIOCPKT_FLUSHWRITE) {
    682 		(void)ioctl(1, TIOCFLUSH, (char *)&out);
    683 		for (;;) {
    684 			if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
    685 				(void)fprintf(stderr, "rlogin: ioctl: %s.\n",
    686 				    strerror(errno));
    687 				break;
    688 			}
    689 			if (atmark)
    690 				break;
    691 			n = read(rem, waste, sizeof (waste));
    692 			if (n <= 0)
    693 				break;
    694 		}
    695 		/*
    696 		 * Don't want any pending data to be output, so clear the recv
    697 		 * buffer.  If we were hanging on a write when interrupted,
    698 		 * don't want it to restart.  If we were reading, restart
    699 		 * anyway.
    700 		 */
    701 		rcvcnt = 0;
    702 		longjmp(rcvtop, 1);
    703 	}
    704 
    705 	/* oob does not do FLUSHREAD (alas!) */
    706 
    707 	/*
    708 	 * If we filled the receive buffer while a read was pending, longjmp
    709 	 * to the top to restart appropriately.  Don't abort a pending write,
    710 	 * however, or we won't know how much was written.
    711 	 */
    712 	if (rcvd && rcvstate == READING)
    713 		longjmp(rcvtop, 1);
    714 }
    715 
    716 /* reader: read from remote: line -> 1 */
    717 int
    718 reader(omask)
    719 	int omask;
    720 {
    721 	int pid, n, remaining;
    722 	char *bufp;
    723 
    724 #if BSD >= 43 || defined(SUNOS4)
    725 	pid = getpid();		/* modern systems use positives for pid */
    726 #else
    727 	pid = -getpid();	/* old broken systems use negatives */
    728 #endif
    729 	(void)signal(SIGTTOU, SIG_IGN);
    730 	(void)signal(SIGURG, oob);
    731 	ppid = getppid();
    732 	(void)fcntl(rem, F_SETOWN, pid);
    733 	(void)setjmp(rcvtop);
    734 	(void)sigsetmask(omask);
    735 	bufp = rcvbuf;
    736 	for (;;) {
    737 		while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
    738 			rcvstate = WRITING;
    739 			n = write(STDOUT_FILENO, bufp, remaining);
    740 			if (n < 0) {
    741 				if (errno != EINTR)
    742 					return (-1);
    743 				continue;
    744 			}
    745 			bufp += n;
    746 		}
    747 		bufp = rcvbuf;
    748 		rcvcnt = 0;
    749 		rcvstate = READING;
    750 
    751 #ifdef CRYPT
    752 #ifdef KERBEROS
    753 		if (doencrypt)
    754 			rcvcnt = des_read(rem, rcvbuf, sizeof(rcvbuf));
    755 		else
    756 #endif
    757 #endif
    758 			rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
    759 		if (rcvcnt == 0)
    760 			return (0);
    761 		if (rcvcnt < 0) {
    762 			if (errno == EINTR)
    763 				continue;
    764 			(void)fprintf(stderr, "rlogin: read: %s.\n",
    765 			    strerror(errno));
    766 			return (-1);
    767 		}
    768 	}
    769 }
    770 
    771 void
    772 mode(f)
    773 	int f;
    774 {
    775 	struct termios tty;
    776 
    777 	switch (f) {
    778 	case 0:
    779 		(void)tcsetattr(0, TCSANOW, &deftty);
    780 		break;
    781 	case 1:
    782 		(void)tcgetattr(0, &deftty);
    783 		tty = deftty;
    784 		/* This is loosely derived from sys/compat/tty_compat.c. */
    785 		tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
    786 		tty.c_iflag &= ~ICRNL;
    787 		tty.c_oflag &= ~OPOST;
    788 		if (eight) {
    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