Home | History | Annotate | Line # | Download | only in rlogin
rlogin.c revision 1.22
      1 /*	$NetBSD: rlogin.c,v 1.22 1999/04/30 15:44:21 christos 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) 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.4 (Berkeley) 4/29/95";
     45 #else
     46 __RCSID("$NetBSD: rlogin.c,v 1.22 1999/04/30 15:44:21 christos 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 #include <sys/ioctl.h>
     60 
     61 #include <netinet/in.h>
     62 #include <netinet/in_systm.h>
     63 #include <netinet/ip.h>
     64 
     65 #include <err.h>
     66 #include <errno.h>
     67 #include <fcntl.h>
     68 #include <netdb.h>
     69 #include <pwd.h>
     70 #include <setjmp.h>
     71 #include <termios.h>
     72 #include <signal.h>
     73 #include <stdio.h>
     74 #include <stdlib.h>
     75 #include <string.h>
     76 #include <unistd.h>
     77 
     78 #ifdef __STDC__
     79 #include <stdarg.h>
     80 #else
     81 #include <varargs.h>
     82 #endif
     83 
     84 #ifdef KERBEROS
     85 #include <kerberosIV/des.h>
     86 #include <kerberosIV/krb.h>
     87 #include <kerberosIV/kstream.h>
     88 
     89 #include "krb.h"
     90 
     91 CREDENTIALS cred;
     92 Key_schedule schedule;
     93 MSG_DAT msg_data;
     94 struct sockaddr_in local, foreign;
     95 int use_kerberos = 1, doencrypt;
     96 kstream krem;
     97 #endif
     98 
     99 #ifndef TIOCPKT_WINDOW
    100 #define	TIOCPKT_WINDOW	0x80
    101 #endif
    102 
    103 /* concession to Sun */
    104 #ifndef SIGUSR1
    105 #define	SIGUSR1	30
    106 #endif
    107 
    108 #ifndef CCEQ
    109 #define CCEQ(val, c)	(c == val ? val != _POSIX_VDISABLE : 0)
    110 #endif
    111 
    112 int eight, rem;
    113 struct termios deftty;
    114 
    115 int noescape;
    116 u_char escapechar = '~';
    117 
    118 #ifdef OLDSUN
    119 struct winsize {
    120 	unsigned short ws_row, ws_col;
    121 	unsigned short ws_xpixel, ws_ypixel;
    122 };
    123 #else
    124 #define	get_window_size(fd, wp)	ioctl(fd, TIOCGWINSZ, wp)
    125 #endif
    126 struct	winsize winsize;
    127 
    128 void		catch_child __P((int));
    129 void		copytochild __P((int));
    130 void		doit __P((sigset_t *));
    131 void		done __P((int));
    132 void		echo __P((int));
    133 u_int		getescape __P((char *));
    134 void		lostpeer __P((int));
    135 int		main __P((int, char **));
    136 void		mode __P((int));
    137 void		msg __P((char *));
    138 void		oob __P((int));
    139 int		reader __P((sigset_t *));
    140 void		sendwindow __P((void));
    141 void		setsignal __P((int));
    142 int		speed __P((int));
    143 void		sigwinch __P((int));
    144 void		stop __P((int));
    145 void		usage __P((void));
    146 void		writer __P((void));
    147 void		writeroob __P((int));
    148 
    149 #ifdef	KERBEROS
    150 void		warning __P((const char *, ...));
    151 #endif
    152 #ifdef OLDSUN
    153 int		get_window_size __P((int, struct winsize *));
    154 #endif
    155 
    156 extern char *__progname;
    157 
    158 int
    159 main(argc, argv)
    160 	int argc;
    161 	char *argv[];
    162 {
    163 	struct passwd *pw;
    164 	struct servent *sp;
    165 	struct termios tty;
    166 	sigset_t smask;
    167 	int argoff, ch, dflag, one, uid;
    168 	int i, len, len2;
    169 	char *host, *p, *user, *name, term[1024] = "network";
    170 	speed_t ospeed;
    171 	struct sigaction sa;
    172 	struct rlimit rlim;
    173 #ifdef KERBEROS
    174 	KTEXT_ST ticket;
    175 	char **orig_argv = argv;
    176 	int sock;
    177 	long authopts;
    178 	int through_once = 0;
    179 	char *cp = (char *) NULL;
    180 	extern int _kstream_des_debug_OOB;
    181 	char *dest_realm = NULL;
    182 #endif
    183 
    184 	argoff = dflag = 0;
    185 	one = 1;
    186 	host = user = NULL;
    187 
    188 	if (strcmp(__progname, "rlogin") != 0)
    189 		host = __progname;
    190 
    191 	/* handle "rlogin host flags" */
    192 	if (!host && argc > 2 && argv[1][0] != '-') {
    193 		host = argv[1];
    194 		argoff = 1;
    195 	}
    196 
    197 #ifdef KERBEROS
    198 #define	OPTIONS	"8EKLde:k:l:x"
    199 #else
    200 #define	OPTIONS	"8EKLde:l:"
    201 #endif
    202 	while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
    203 		switch(ch) {
    204 		case '8':
    205 			eight = 1;
    206 			break;
    207 		case 'E':
    208 			noescape = 1;
    209 			break;
    210 #ifdef KERBEROS
    211 		case 'K':
    212 			use_kerberos = 0;
    213 			break;
    214 #endif
    215 		case 'd':
    216 #ifdef KERBEROS
    217 			_kstream_des_debug_OOB = 1;
    218 #endif
    219 			dflag = 1;
    220 			break;
    221 		case 'e':
    222 			noescape = 0;
    223 			escapechar = getescape(optarg);
    224 			break;
    225 #ifdef KERBEROS
    226 		case 'k':
    227 			dest_realm = optarg;
    228 			break;
    229 #endif
    230 		case 'l':
    231 			user = optarg;
    232 			break;
    233 #ifdef CRYPT
    234 #ifdef KERBEROS
    235 		case 'x':
    236 			doencrypt = 1;
    237 			break;
    238 #endif
    239 #endif
    240 		case '?':
    241 		default:
    242 			usage();
    243 		}
    244 	optind += argoff;
    245 	argc -= optind;
    246 	argv += optind;
    247 
    248 	/* if haven't gotten a host yet, do so */
    249 	if (!host && !(host = *argv++))
    250 		usage();
    251 
    252 	if (*argv)
    253 		usage();
    254 
    255 	if (!(pw = getpwuid(uid = getuid())))
    256 		errx(1, "unknown user id.");
    257 	/* Accept user1@host format, though "-l user2" overrides user1 */
    258 	p = strchr(host, '@');
    259 	if (p) {
    260 		*p = '\0';
    261 		if (!user && p > host)
    262 			user = host;
    263 		host = p + 1;
    264 		if (*host == '\0')
    265 			usage();
    266 	}
    267 	if ((name = strdup(pw->pw_name)) == NULL)
    268 		err(1, "malloc");
    269 	if (!user)
    270 		user = name;
    271 
    272 #ifdef KERBEROS
    273 	sp = NULL;
    274 	if (use_kerberos) {
    275 		sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
    276 		if (sp == NULL) {
    277 			use_kerberos = 0;
    278 			warning("can't get entry for %s/tcp service",
    279 			    doencrypt ? "eklogin" : "klogin");
    280 		}
    281 	}
    282 	if (sp == NULL)
    283 #endif
    284 		sp = getservbyname("login", "tcp");
    285 	if (sp == NULL)
    286 		errx(1, "login/tcp: unknown service.");
    287 
    288 	if ((p = getenv("TERM")) != NULL) {
    289 		(void)strncpy(term, p, sizeof(term) - 1);
    290 		term[sizeof(term) - 1] = '\0';
    291 	}
    292 	len = strlen(term);
    293 	if (len < (sizeof(term) - 1) && tcgetattr(0, &tty) == 0) {
    294 		/* start at 2 to include the / */
    295 		for (ospeed = i = cfgetospeed(&tty), len2 = 2; i > 9; len2++)
    296 			i /= 10;
    297 
    298 		if (len + len2 < sizeof(term))
    299 			(void)snprintf(term + len, len2 + 1, "/%d", ospeed);
    300 	}
    301 
    302 	(void)get_window_size(0, &winsize);
    303 
    304 	sigemptyset(&sa.sa_mask);
    305 	sa.sa_flags = SA_RESTART;
    306 	sa.sa_handler = lostpeer;
    307 	(void)sigaction(SIGPIPE, &sa, (struct sigaction *)0);
    308 	/* will use SIGUSR1 for window size hack, so hold it off */
    309 	sigemptyset(&smask);
    310 	sigaddset(&smask, SIGURG);
    311 	sigaddset(&smask, SIGUSR1);
    312 	(void)sigprocmask(SIG_SETMASK, &smask, &smask);
    313 	/*
    314 	 * We set SIGURG and SIGUSR1 below so that an
    315 	 * incoming signal will be held pending rather than being
    316 	 * discarded. Note that these routines will be ready to get
    317 	 * a signal by the time that they are unblocked below.;
    318 	 */
    319 	sa.sa_handler = copytochild;
    320 	(void)sigaction(SIGURG, &sa, (struct sigaction *) 0);
    321 	sa.sa_handler = writeroob;
    322 	(void)sigaction(SIGUSR1, &sa, (struct sigaction *) 0);
    323 
    324 	/* don't dump core */
    325 	rlim.rlim_cur = rlim.rlim_max = 0;
    326 	if (setrlimit(RLIMIT_CORE, &rlim) < 0)
    327 		warn("setrlimit");
    328 
    329 #ifdef KERBEROS
    330 try_connect:
    331 	if (use_kerberos) {
    332 		struct hostent *hp;
    333 
    334 		/* Fully qualify hostname (needed for krb_realmofhost). */
    335 		hp = gethostbyname(host);
    336 		if (hp != NULL && !(host = strdup(hp->h_name)))
    337 			errx(1, "%s", strerror(ENOMEM));
    338 
    339 		rem = KSUCCESS;
    340 		errno = 0;
    341 #ifdef CRYPT
    342 		if (doencrypt)
    343 			authopts = KOPT_DO_MUTUAL;
    344 		else
    345 #endif /* CRYPT */
    346 			authopts = 0L;
    347 
    348 		if (dest_realm == NULL) {
    349 			/* default this now, once. */
    350 			if (!(dest_realm = krb_realmofhost (host))) {
    351 				warnx("Unknown realm for host %s.", host);
    352 				use_kerberos = 0;
    353 				sp = getservbyname("login", "tcp");
    354 				goto try_connect;
    355 			}
    356 		}
    357 
    358 		rem = kcmd(&sock, &host, sp->s_port, name, user,
    359 			   term, 0, &ticket, "rcmd", dest_realm,
    360 			   &cred, schedule, &msg_data, &local, &foreign,
    361 			   authopts);
    362 
    363 		if (rem != KSUCCESS) {
    364 			switch(rem) {
    365 
    366 				case KDC_PR_UNKNOWN:
    367 					warnx("Host %s not registered for %s",
    368 				       	      host, "Kerberos rlogin service");
    369 					use_kerberos = 0;
    370 					sp = getservbyname("login", "tcp");
    371 					goto try_connect;
    372 				case NO_TKT_FIL:
    373 					if (through_once++) {
    374 						use_kerberos = 0;
    375 						sp = getservbyname("login", "tcp");
    376 						goto try_connect;
    377 					}
    378 #ifdef notyet
    379 				krb_get_pw_in_tkt(user, krb_realm, "krbtgt",
    380 						  krb_realm,
    381 					          DEFAULT_TKT_LIFE/5, 0);
    382 				goto try_connect;
    383 #endif
    384 			default:
    385 				warnx("Kerberos rcmd failed: %s",
    386 				      (rem == -1) ? "rcmd protocol failure" :
    387 				      krb_err_txt[rem]);
    388 				use_kerberos = 0;
    389 				sp = getservbyname("login", "tcp");
    390 				goto try_connect;
    391 			}
    392 		}
    393 		rem = sock;
    394 		if (doencrypt)
    395 			krem = kstream_create_rlogin_from_fd(rem, &schedule,
    396 							     &cred.session);
    397 		else
    398 			krem = kstream_create_from_fd(rem, 0, 0);
    399 			kstream_set_buffer_mode(krem, 0);
    400 	} else {
    401 #ifdef CRYPT
    402 		if (doencrypt)
    403 			errx(1, "the -x flag requires Kerberos authentication.");
    404 #endif /* CRYPT */
    405 		rem = rcmd(&host, sp->s_port, name, user, term, 0);
    406 		if (rem < 0)
    407 			exit(1);
    408 	}
    409 #else
    410 	rem = rcmd(&host, sp->s_port, name, user, term, 0);
    411 
    412 #endif /* KERBEROS */
    413 
    414 	if (rem < 0)
    415 		exit(1);
    416 
    417 	if (dflag &&
    418 	    setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
    419 		warn("setsockopt DEBUG (ignored)");
    420 	one = IPTOS_LOWDELAY;
    421 	if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one, sizeof(int)) < 0)
    422 		warn("setsockopt TOS (ignored)");
    423 
    424 	(void)setuid(uid);
    425 	doit(&smask);
    426 	/*NOTREACHED*/
    427 	return (0);
    428 }
    429 
    430 int
    431 speed(fd)
    432 	int fd;
    433 {
    434 	struct termios tt;
    435 
    436 	(void)tcgetattr(fd, &tt);
    437 
    438 	return ((int)cfgetispeed(&tt));
    439 }
    440 
    441 pid_t child;
    442 struct termios deftt;
    443 struct termios nott;
    444 
    445 void
    446 doit(smask)
    447 	sigset_t *smask;
    448 {
    449 	int i;
    450 	struct sigaction sa;
    451 
    452 	for (i = 0; i < NCCS; i++)
    453 		nott.c_cc[i] = _POSIX_VDISABLE;
    454 	tcgetattr(0, &deftt);
    455 	nott.c_cc[VSTART] = deftt.c_cc[VSTART];
    456 	nott.c_cc[VSTOP] = deftt.c_cc[VSTOP];
    457 	sigemptyset(&sa.sa_mask);
    458 	sa.sa_flags = SA_RESTART;
    459 	sa.sa_handler = SIG_IGN;
    460 	(void)sigaction(SIGINT, &sa, (struct sigaction *) 0);
    461 	setsignal(SIGHUP);
    462 	setsignal(SIGQUIT);
    463 	mode(1);
    464 	child = fork();
    465 	if (child == -1) {
    466 		warn("fork");
    467 		done(1);
    468 	}
    469 	if (child == 0) {
    470 		mode(1);
    471 		if (reader(smask) == 0) {
    472 			msg("connection closed.");
    473 			exit(0);
    474 		}
    475 		sleep(1);
    476 		msg("\aconnection closed.");
    477 		exit(1);
    478 	}
    479 
    480 	/*
    481 	 * We may still own the socket, and may have a pending SIGURG (or might
    482 	 * receive one soon) that we really want to send to the reader.  When
    483 	 * one of these comes in, the trap copytochild simply copies such
    484 	 * signals to the child. We can now unblock SIGURG and SIGUSR1
    485 	 * that were set above.
    486 	 */
    487 	(void)sigprocmask(SIG_SETMASK, smask, (sigset_t *) 0);
    488 	sa.sa_handler = catch_child;
    489 	(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
    490 	writer();
    491 	msg("closed connection.");
    492 	done(0);
    493 }
    494 
    495 /* trap a signal, unless it is being ignored. */
    496 void
    497 setsignal(sig)
    498 	int sig;
    499 {
    500 	struct sigaction sa;
    501 	sigset_t sigs;
    502 
    503 	sigemptyset(&sigs);
    504 	sigaddset(&sigs, sig);
    505 	sigprocmask(SIG_BLOCK, &sigs, &sigs);
    506 
    507 	sigemptyset(&sa.sa_mask);
    508 	sa.sa_handler = exit;
    509 	sa.sa_flags = SA_RESTART;
    510 	(void)sigaction(sig, &sa, &sa);
    511 	if (sa.sa_handler == SIG_IGN)
    512 		(void)sigaction(sig, &sa, (struct sigaction *) 0);
    513 
    514 	(void)sigprocmask(SIG_SETMASK, &sigs, (sigset_t *) 0);
    515 }
    516 
    517 void
    518 done(status)
    519 	int status;
    520 {
    521 	pid_t w;
    522 	int wstatus;
    523 	struct sigaction sa;
    524 
    525 	mode(0);
    526 	if (child > 0) {
    527 		/* make sure catch_child does not snap it up */
    528 		sigemptyset(&sa.sa_mask);
    529 		sa.sa_handler = SIG_DFL;
    530 		sa.sa_flags = 0;
    531 		(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
    532 		if (kill(child, SIGKILL) >= 0)
    533 			while ((w = wait(&wstatus)) > 0 && w != child)
    534 				continue;
    535 	}
    536 	exit(status);
    537 }
    538 
    539 int dosigwinch;
    540 
    541 /*
    542  * This is called when the reader process gets the out-of-band (urgent)
    543  * request to turn on the window-changing protocol.
    544  */
    545 void
    546 writeroob(signo)
    547 	int signo;
    548 {
    549 	struct sigaction sa;
    550 
    551 	if (dosigwinch == 0) {
    552 		sendwindow();
    553 		sigemptyset(&sa.sa_mask);
    554 		sa.sa_handler = sigwinch;
    555 		sa.sa_flags = SA_RESTART;
    556 		(void)sigaction(SIGWINCH, &sa, (struct sigaction *) 0);
    557 	}
    558 	dosigwinch = 1;
    559 }
    560 
    561 void
    562 catch_child(signo)
    563 	int signo;
    564 {
    565 	int status;
    566 	pid_t pid;
    567 
    568 	for (;;) {
    569 		pid = waitpid(-1, &status, WNOHANG|WUNTRACED);
    570 		if (pid == 0)
    571 			return;
    572 		/* if the child (reader) dies, just quit */
    573 		if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
    574 			done(WEXITSTATUS(status) | WTERMSIG(status));
    575 	}
    576 	/* NOTREACHED */
    577 }
    578 
    579 /*
    580  * writer: write to remote: 0 -> line.
    581  * ~.				terminate
    582  * ~^Z				suspend rlogin process.
    583  * ~<delayed-suspend char>	suspend rlogin process, but leave reader alone.
    584  */
    585 void
    586 writer()
    587 {
    588 	int bol, local, n;
    589 	char c;
    590 
    591 	bol = 1;			/* beginning of line */
    592 	local = 0;
    593 	for (;;) {
    594 		n = read(STDIN_FILENO, &c, 1);
    595 		if (n <= 0) {
    596 			if (n < 0 && errno == EINTR)
    597 				continue;
    598 			break;
    599 		}
    600 		/*
    601 		 * If we're at the beginning of the line and recognize a
    602 		 * command character, then we echo locally.  Otherwise,
    603 		 * characters are echo'd remotely.  If the command character
    604 		 * is doubled, this acts as a force and local echo is
    605 		 * suppressed.
    606 		 */
    607 		if (bol) {
    608 			bol = 0;
    609 			if (!noescape && c == escapechar) {
    610 				local = 1;
    611 				continue;
    612 			}
    613 		} else if (local) {
    614 			local = 0;
    615 			if (c == '.' || CCEQ(deftty.c_cc[VEOF], c)) {
    616 				echo((int)c);
    617 				break;
    618 			}
    619 			if (CCEQ(deftty.c_cc[VSUSP], c)) {
    620 				bol = 1;
    621 				echo((int)c);
    622 				stop(1);
    623 				continue;
    624 			}
    625 			if (CCEQ(deftty.c_cc[VDSUSP], c)) {
    626 				bol = 1;
    627 				echo((int)c);
    628 				stop(0);
    629 				continue;
    630 			}
    631 			if (c != escapechar)
    632 #ifdef KERBEROS
    633 				if (use_kerberos) {
    634 					(void)kstream_write(krem,
    635 				    	(char *)&escapechar, 1);
    636 				}
    637 				else
    638 #endif
    639 				(void)write(rem, &escapechar, 1);
    640 		}
    641 
    642 #ifdef KERBEROS
    643 		if (use_kerberos) {
    644 			if (kstream_write(krem, &c, 1) == 0) {
    645 					msg("line gone");
    646 					break;
    647 			}
    648 		}
    649 		else
    650 #endif
    651 			if (write(rem, &c, 1) == 0) {
    652 				msg("line gone");
    653 				break;
    654 			}
    655 
    656 		bol = CCEQ(deftty.c_cc[VKILL], c) ||
    657 		    CCEQ(deftty.c_cc[VEOF], c) ||
    658 		    CCEQ(deftty.c_cc[VINTR], c) ||
    659 		    CCEQ(deftty.c_cc[VSUSP], c) ||
    660 		    c == '\r' || c == '\n';
    661 	}
    662 }
    663 
    664 void
    665 echo(i)
    666 	int i;
    667 {
    668 	char c = (char)i;
    669 	char *p;
    670 	char buf[8];
    671 
    672 	p = buf;
    673 	c &= 0177;
    674 	*p++ = escapechar;
    675 	if (c < ' ') {
    676 		*p++ = '^';
    677 		*p++ = c + '@';
    678 	} else if (c == 0177) {
    679 		*p++ = '^';
    680 		*p++ = '?';
    681 	} else
    682 		*p++ = c;
    683 	*p++ = '\r';
    684 	*p++ = '\n';
    685 	(void)write(STDOUT_FILENO, buf, p - buf);
    686 }
    687 
    688 void
    689 stop(all)
    690 	int all;
    691 {
    692 	struct sigaction sa;
    693 
    694 	mode(0);
    695 	sigemptyset(&sa.sa_mask);
    696 	sa.sa_handler = SIG_IGN;
    697 	sa.sa_flags = SA_RESTART;
    698 	(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
    699 	(void)kill(all ? 0 : getpid(), SIGTSTP);
    700 	sa.sa_handler = catch_child;
    701 	(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
    702 	mode(1);
    703 	sigwinch(0);			/* check for size changes */
    704 }
    705 
    706 void
    707 sigwinch(signo)
    708 	int signo;
    709 {
    710 	struct winsize ws;
    711 
    712 	if (dosigwinch && get_window_size(0, &ws) == 0 &&
    713 	    memcmp(&ws, &winsize, sizeof(ws))) {
    714 		winsize = ws;
    715 		sendwindow();
    716 	}
    717 }
    718 
    719 /*
    720  * Send the window size to the server via the magic escape
    721  */
    722 void
    723 sendwindow()
    724 {
    725 	struct winsize *wp;
    726 	char obuf[4 + sizeof (struct winsize)];
    727 
    728 	wp = (struct winsize *)(obuf+4);
    729 	obuf[0] = 0377;
    730 	obuf[1] = 0377;
    731 	obuf[2] = 's';
    732 	obuf[3] = 's';
    733 	wp->ws_row = htons(winsize.ws_row);
    734 	wp->ws_col = htons(winsize.ws_col);
    735 	wp->ws_xpixel = htons(winsize.ws_xpixel);
    736 	wp->ws_ypixel = htons(winsize.ws_ypixel);
    737 
    738 #ifdef KERBEROS
    739 		if (use_kerberos)
    740 			(void)kstream_write(krem, obuf, sizeof(obuf));
    741 		else
    742 #endif
    743 		(void)write(rem, obuf, sizeof(obuf));
    744 }
    745 
    746 /*
    747  * reader: read from remote: line -> 1
    748  */
    749 #define	READING	1
    750 #define	WRITING	2
    751 
    752 jmp_buf rcvtop;
    753 pid_t ppid;
    754 int rcvcnt, rcvstate;
    755 char rcvbuf[8 * 1024];
    756 
    757 void
    758 oob(signo)
    759 	int signo;
    760 {
    761 	struct termios tty;
    762 	int atmark, n, rcvd;
    763 	char waste[BUFSIZ], mark;
    764 
    765 	rcvd = 0;
    766 	while (recv(rem, &mark, 1, MSG_OOB) < 0) {
    767 		switch (errno) {
    768 		case EWOULDBLOCK:
    769 			/*
    770 			 * Urgent data not here yet.  It may not be possible
    771 			 * to send it yet if we are blocked for output and
    772 			 * our input buffer is full.
    773 			 */
    774 			if (rcvcnt < sizeof(rcvbuf)) {
    775 				n = read(rem, rcvbuf + rcvcnt,
    776 				    sizeof(rcvbuf) - rcvcnt);
    777 				if (n <= 0)
    778 					return;
    779 				rcvd += n;
    780 			} else {
    781 				n = read(rem, waste, sizeof(waste));
    782 				if (n <= 0)
    783 					return;
    784 			}
    785 			continue;
    786 		default:
    787 			return;
    788 		}
    789 	}
    790 	if (mark & TIOCPKT_WINDOW) {
    791 		/* Let server know about window size changes */
    792 		(void)kill(ppid, SIGUSR1);
    793 	}
    794 	if (!eight && (mark & TIOCPKT_NOSTOP)) {
    795 		(void)tcgetattr(0, &tty);
    796 		tty.c_iflag &= ~IXON;
    797 		(void)tcsetattr(0, TCSANOW, &tty);
    798 	}
    799 	if (!eight && (mark & TIOCPKT_DOSTOP)) {
    800 		(void)tcgetattr(0, &tty);
    801 		tty.c_iflag |= (deftty.c_iflag & IXON);
    802 		(void)tcsetattr(0, TCSANOW, &tty);
    803 	}
    804 	if (mark & TIOCPKT_FLUSHWRITE) {
    805 		(void)tcflush(1, TCIOFLUSH);
    806 		for (;;) {
    807 			if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
    808 				warn("ioctl SIOCATMARK (ignored)");
    809 				break;
    810 			}
    811 			if (atmark)
    812 				break;
    813 			n = read(rem, waste, sizeof (waste));
    814 			if (n <= 0)
    815 				break;
    816 		}
    817 		/*
    818 		 * Don't want any pending data to be output, so clear the recv
    819 		 * buffer.  If we were hanging on a write when interrupted,
    820 		 * don't want it to restart.  If we were reading, restart
    821 		 * anyway.
    822 		 */
    823 		rcvcnt = 0;
    824 		longjmp(rcvtop, 1);
    825 	}
    826 
    827 	/* oob does not do FLUSHREAD (alas!) */
    828 
    829 	/*
    830 	 * If we filled the receive buffer while a read was pending, longjmp
    831 	 * to the top to restart appropriately.  Don't abort a pending write,
    832 	 * however, or we won't know how much was written.
    833 	 */
    834 	if (rcvd && rcvstate == READING)
    835 		longjmp(rcvtop, 1);
    836 }
    837 
    838 /* reader: read from remote: line -> 1 */
    839 int
    840 reader(smask)
    841 	sigset_t *smask;
    842 {
    843 	pid_t pid;
    844 	int n, remaining;
    845 	char *bufp;
    846 	struct sigaction sa;
    847 
    848 	pid = getpid();		/* modern systems use positives for pid */
    849 	sigemptyset(&sa.sa_mask);
    850 	sa.sa_flags = SA_RESTART;
    851 	sa.sa_handler = SIG_IGN;
    852 	(void)sigaction(SIGTTOU, &sa, (struct sigaction *) 0);
    853 	sa.sa_handler = oob;
    854 	(void)sigaction(SIGURG, &sa, (struct sigaction *) 0);
    855 	ppid = getppid();
    856 	(void)fcntl(rem, F_SETOWN, pid);
    857 	(void)setjmp(rcvtop);
    858 	(void)sigprocmask(SIG_SETMASK, smask, (sigset_t *) 0);
    859 	bufp = rcvbuf;
    860 	for (;;) {
    861 		while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
    862 			rcvstate = WRITING;
    863 			n = write(STDOUT_FILENO, bufp, remaining);
    864 			if (n < 0) {
    865 				if (errno != EINTR)
    866 					return (-1);
    867 				continue;
    868 			}
    869 			bufp += n;
    870 		}
    871 		bufp = rcvbuf;
    872 		rcvcnt = 0;
    873 		rcvstate = READING;
    874 
    875 #ifdef KERBEROS
    876 			if (use_kerberos)
    877 				rcvcnt = kstream_read(krem, rcvbuf, sizeof(rcvbuf));
    878 			else
    879 #endif
    880 			rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
    881 
    882 		if (rcvcnt == 0)
    883 			return (0);
    884 		if (rcvcnt < 0) {
    885 			if (errno == EINTR)
    886 				continue;
    887 			warn("read");
    888 			return (-1);
    889 		}
    890 	}
    891 }
    892 
    893 void
    894 mode(f)
    895 	int f;
    896 {
    897 	struct termios tty;
    898 
    899 	switch (f) {
    900 	case 0:
    901 		(void)tcsetattr(0, TCSANOW, &deftty);
    902 		break;
    903 	case 1:
    904 		(void)tcgetattr(0, &deftty);
    905 		tty = deftty;
    906 		/* This is loosely derived from sys/compat/tty_compat.c. */
    907 		tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
    908 		tty.c_iflag &= ~ICRNL;
    909 		tty.c_oflag &= ~OPOST;
    910 		tty.c_cc[VMIN] = 1;
    911 		tty.c_cc[VTIME] = 0;
    912 		if (eight) {
    913 			tty.c_iflag &= IXOFF;
    914 			tty.c_cflag &= ~(CSIZE|PARENB);
    915 			tty.c_cflag |= CS8;
    916 		}
    917 		(void)tcsetattr(0, TCSANOW, &tty);
    918 		break;
    919 
    920 	default:
    921 		return;
    922 	}
    923 }
    924 
    925 void
    926 lostpeer(signo)
    927 	int signo;
    928 {
    929 	struct sigaction sa;
    930 	sa.sa_flags = SA_RESTART;
    931 	sa.sa_handler = SIG_IGN;
    932 	(void)sigaction(SIGPIPE, &sa, (struct sigaction *)0);
    933 	msg("\aconnection closed.");
    934 	done(1);
    935 }
    936 
    937 /* copy SIGURGs to the child process. */
    938 void
    939 copytochild(signo)
    940 	int signo;
    941 {
    942 
    943 	(void)kill(child, SIGURG);
    944 }
    945 
    946 void
    947 msg(str)
    948 	char *str;
    949 {
    950 
    951 	(void)fprintf(stderr, "rlogin: %s\r\n", str);
    952 }
    953 
    954 #ifdef KERBEROS
    955 /* VARARGS */
    956 void
    957 #if __STDC__
    958 warning(const char *fmt, ...)
    959 #else
    960 warning(fmt, va_alist)
    961 	char *fmt;
    962 	va_dcl
    963 #endif
    964 {
    965 	va_list ap;
    966 
    967 	(void)fprintf(stderr, "rlogin: warning, using standard rlogin: ");
    968 #ifdef __STDC__
    969 	va_start(ap, fmt);
    970 #else
    971 	va_start(ap);
    972 #endif
    973 	vfprintf(stderr, fmt, ap);
    974 	va_end(ap);
    975 	(void)fprintf(stderr, ".\n");
    976 }
    977 #endif
    978 
    979 void
    980 usage()
    981 {
    982 	(void)fprintf(stderr,
    983 	    "usage: rlogin [ -%s]%s[-e char] [ -l username ] [username@]host\n",
    984 #ifdef KERBEROS
    985 #ifdef CRYPT
    986 	    "8EKLdx", " [-k realm] ");
    987 #else
    988 	    "8EKLd", " [-k realm] ");
    989 #endif
    990 #else
    991 	    "8ELd", " ");
    992 #endif
    993 	exit(1);
    994 }
    995 
    996 /*
    997  * The following routine provides compatibility (such as it is) between older
    998  * Suns and others.  Suns have only a `ttysize', so we convert it to a winsize.
    999  */
   1000 #ifdef OLDSUN
   1001 int
   1002 get_window_size(fd, wp)
   1003 	int fd;
   1004 	struct winsize *wp;
   1005 {
   1006 	struct ttysize ts;
   1007 	int error;
   1008 
   1009 	if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
   1010 		return (error);
   1011 	wp->ws_row = ts.ts_lines;
   1012 	wp->ws_col = ts.ts_cols;
   1013 	wp->ws_xpixel = 0;
   1014 	wp->ws_ypixel = 0;
   1015 	return (0);
   1016 }
   1017 #endif
   1018 
   1019 u_int
   1020 getescape(p)
   1021 	char *p;
   1022 {
   1023 	long val;
   1024 	int len;
   1025 
   1026 	if ((len = strlen(p)) == 1)	/* use any single char, including '\' */
   1027 		return ((u_int)*p);
   1028 					/* otherwise, \nnn */
   1029 	if (*p == '\\' && len >= 2 && len <= 4) {
   1030 		val = strtol(++p, NULL, 8);
   1031 		for (;;) {
   1032 			if (!*++p)
   1033 				return ((u_int)val);
   1034 			if (*p < '0' || *p > '8')
   1035 				break;
   1036 		}
   1037 	}
   1038 	msg("illegal option value -- e");
   1039 	usage();
   1040 	/* NOTREACHED */
   1041 	return (0);
   1042 }
   1043