11590Srgrimes/*
21590Srgrimes * Copyright (c) 1983, 1990, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
496196Sdes * Copyright (c) 2002 Networks Associates Technology, Inc.
596196Sdes * All rights reserved.
61590Srgrimes *
796196Sdes * Portions of this software were developed for the FreeBSD Project by
896196Sdes * ThinkSec AS and NAI Labs, the Security Research Division of Network
996196Sdes * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
1096196Sdes * ("CBOSS"), as part of the DARPA CHATS research program.
1196196Sdes *
121590Srgrimes * Redistribution and use in source and binary forms, with or without
131590Srgrimes * modification, are permitted provided that the following conditions
141590Srgrimes * are met:
151590Srgrimes * 1. Redistributions of source code must retain the above copyright
161590Srgrimes *    notice, this list of conditions and the following disclaimer.
171590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
181590Srgrimes *    notice, this list of conditions and the following disclaimer in the
191590Srgrimes *    documentation and/or other materials provided with the distribution.
201590Srgrimes * 3. All advertising materials mentioning features or use of this software
211590Srgrimes *    must display the following acknowledgement:
221590Srgrimes *	This product includes software developed by the University of
231590Srgrimes *	California, Berkeley and its contributors.
241590Srgrimes * 4. Neither the name of the University nor the names of its contributors
251590Srgrimes *    may be used to endorse or promote products derived from this software
261590Srgrimes *    without specific prior written permission.
271590Srgrimes *
281590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
291590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
301590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
311590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
321590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
331590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
341590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
351590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
361590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
371590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381590Srgrimes * SUCH DAMAGE.
391590Srgrimes */
401590Srgrimes
411590Srgrimes#ifndef lint
4227919Scharnierstatic const char copyright[] =
431590Srgrimes"@(#) Copyright (c) 1983, 1990, 1993\n\
441590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
451590Srgrimes#endif /* not lint */
461590Srgrimes
47105235Scharnier#if 0
481590Srgrimes#ifndef lint
4929922Smarkmstatic const char sccsid[] = "@(#)rlogin.c	8.1 (Berkeley) 6/6/93";
501590Srgrimes#endif /* not lint */
51105235Scharnier#endif
521590Srgrimes
53105235Scharnier#include <sys/cdefs.h>
54105235Scharnier__FBSDID("$FreeBSD$");
55105235Scharnier
561590Srgrimes/*
571590Srgrimes * rlogin - remote login
581590Srgrimes */
5995621Smarkm
601590Srgrimes#include <sys/param.h>
61120547Stjr#include <sys/ioctl.h>
621590Srgrimes#include <sys/socket.h>
631590Srgrimes#include <sys/time.h>
641590Srgrimes#include <sys/resource.h>
651590Srgrimes#include <sys/wait.h>
661590Srgrimes
671590Srgrimes#include <netinet/in.h>
681590Srgrimes#include <netinet/in_systm.h>
691590Srgrimes#include <netinet/ip.h>
708232Sdg#include <netinet/tcp.h>
711590Srgrimes
7227919Scharnier#include <err.h>
731590Srgrimes#include <errno.h>
741590Srgrimes#include <fcntl.h>
751590Srgrimes#include <netdb.h>
76200462Sdelphij#include <paths.h>
771590Srgrimes#include <pwd.h>
781590Srgrimes#include <setjmp.h>
79120547Stjr#include <termios.h>
801590Srgrimes#include <signal.h>
811590Srgrimes#include <stdio.h>
821590Srgrimes#include <stdlib.h>
831590Srgrimes#include <string.h>
841590Srgrimes#include <unistd.h>
851590Srgrimes
861590Srgrimes#ifndef TIOCPKT_WINDOW
871590Srgrimes#define	TIOCPKT_WINDOW	0x80
881590Srgrimes#endif
891590Srgrimes
901590Srgrimes/* concession to Sun */
911590Srgrimes#ifndef SIGUSR1
921590Srgrimes#define	SIGUSR1	30
931590Srgrimes#endif
941590Srgrimes
95120547Stjrint eight, rem;
96120547Stjrstruct termios deftty;
97120547Stjr
9857232Sshinint family = PF_UNSPEC;
991590Srgrimes
1001590Srgrimesint noescape;
1011590Srgrimesu_char escapechar = '~';
1021590Srgrimes
1031590Srgrimes#define	get_window_size(fd, wp)	ioctl(fd, TIOCGWINSZ, wp)
1041590Srgrimesstruct	winsize winsize;
1051590Srgrimes
10692921Simpvoid		catch_child(int);
10792921Simpvoid		copytochild(int);
10892921Simpvoid		doit(long) __dead2;
10992921Simpvoid		done(int) __dead2;
11092921Simpvoid		echo(char);
111105268Smarkmu_int		getescape(const char *);
11292921Simpvoid		lostpeer(int);
11392921Simpvoid		mode(int);
11495621Smarkmvoid		msg(const char *);
11592921Simpvoid		oob(int);
11692921Simpint		reader(int);
11792921Simpvoid		sendwindow(void);
11892921Simpvoid		setsignal(int);
11992921Simpvoid		sigwinch(int);
12092921Simpvoid		stop(char);
12192921Simpvoid		usage(void) __dead2;
12292921Simpvoid		writer(void);
12392921Simpvoid		writeroob(int);
1241590Srgrimes
1251590Srgrimesint
12693057Simpmain(int argc, char *argv[])
1271590Srgrimes{
1281590Srgrimes	struct passwd *pw;
1291590Srgrimes	struct servent *sp;
130120547Stjr	struct termios tty;
1311590Srgrimes	long omask;
132105268Smarkm	int argoff, ch, dflag, Dflag, one;
133105268Smarkm	uid_t uid;
13447549Sbde	char *host, *localname, *p, *user, term[1024];
135120547Stjr	speed_t ospeed;
13696196Sdes	struct sockaddr_storage ss;
137148726Sstefanf	socklen_t sslen;
138152397Sdwmalone	size_t len, len2;
139152397Sdwmalone	int i;
1401590Srgrimes
1418232Sdg	argoff = dflag = Dflag = 0;
1421590Srgrimes	one = 1;
14347549Sbde	host = localname = user = NULL;
1441590Srgrimes
14529922Smarkm	if ((p = rindex(argv[0], '/')))
1461590Srgrimes		++p;
1471590Srgrimes	else
1481590Srgrimes		p = argv[0];
1491590Srgrimes
1501590Srgrimes	if (strcmp(p, "rlogin"))
1511590Srgrimes		host = p;
1521590Srgrimes
1531590Srgrimes	/* handle "rlogin host flags" */
1541590Srgrimes	if (!host && argc > 2 && argv[1][0] != '-') {
1551590Srgrimes		host = argv[1];
1561590Srgrimes		argoff = 1;
1571590Srgrimes	}
1581590Srgrimes
159140569Sru#define	OPTIONS	"468DEde:i:l:"
16024360Simp	while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
1611590Srgrimes		switch(ch) {
16257232Sshin		case '4':
16357232Sshin			family = PF_INET;
16457232Sshin			break;
16557232Sshin
16657232Sshin		case '6':
16757232Sshin			family = PF_INET6;
16857232Sshin			break;
16957232Sshin
1701590Srgrimes		case '8':
1711590Srgrimes			eight = 1;
1721590Srgrimes			break;
1738232Sdg		case 'D':
1748232Sdg			Dflag = 1;
1758232Sdg			break;
1761590Srgrimes		case 'E':
1771590Srgrimes			noescape = 1;
1781590Srgrimes			break;
1791590Srgrimes		case 'd':
1801590Srgrimes			dflag = 1;
1811590Srgrimes			break;
1821590Srgrimes		case 'e':
1831590Srgrimes			noescape = 0;
1841590Srgrimes			escapechar = getescape(optarg);
1851590Srgrimes			break;
18647488Speter		case 'i':
18747549Sbde			if (getuid() != 0)
18847549Sbde				errx(1, "-i user: permission denied");
18947488Speter			localname = optarg;
19047488Speter			break;
1911590Srgrimes		case 'l':
1921590Srgrimes			user = optarg;
1931590Srgrimes			break;
1941590Srgrimes		case '?':
1951590Srgrimes		default:
1961590Srgrimes			usage();
1971590Srgrimes		}
1981590Srgrimes	optind += argoff;
1991590Srgrimes
2001590Srgrimes	/* if haven't gotten a host yet, do so */
20134897Smarkm	if (!host && !(host = argv[optind++]))
2021590Srgrimes		usage();
2031590Srgrimes
20434897Smarkm	if (argv[optind])
2051590Srgrimes		usage();
2061590Srgrimes
20727919Scharnier	if (!(pw = getpwuid(uid = getuid())))
20827919Scharnier		errx(1, "unknown user id");
2091590Srgrimes	if (!user)
2101590Srgrimes		user = pw->pw_name;
21147488Speter	if (!localname)
21247488Speter		localname = pw->pw_name;
2131590Srgrimes
2141590Srgrimes	sp = NULL;
215105268Smarkm	sp = getservbyname("login", "tcp");
2161590Srgrimes	if (sp == NULL)
21727919Scharnier		errx(1, "login/tcp: unknown service");
2181590Srgrimes
219120547Stjr	if ((p = getenv("TERM")) != NULL)
220120547Stjr		(void)strlcpy(term, p, sizeof(term));
221120547Stjr	len = strlen(term);
222120547Stjr	if (len < (sizeof(term) - 1) && tcgetattr(0, &tty) == 0) {
223120547Stjr		/* start at 2 to include the / */
224120547Stjr		for (ospeed = i = cfgetospeed(&tty), len2 = 2; i > 9; len2++)
225120547Stjr			i /= 10;
226120547Stjr		if (len + len2 < sizeof(term))
227120547Stjr			(void)snprintf(term + len, len2 + 1, "/%d", ospeed);
2281590Srgrimes	}
2291590Srgrimes
2301590Srgrimes	(void)get_window_size(0, &winsize);
2311590Srgrimes
2321590Srgrimes	(void)signal(SIGPIPE, lostpeer);
2331590Srgrimes	/* will use SIGUSR1 for window size hack, so hold it off */
2341590Srgrimes	omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
2351590Srgrimes	/*
2361590Srgrimes	 * We set SIGURG and SIGUSR1 below so that an
2371590Srgrimes	 * incoming signal will be held pending rather than being
2381590Srgrimes	 * discarded. Note that these routines will be ready to get
2391590Srgrimes	 * a signal by the time that they are unblocked below.
2401590Srgrimes	 */
2411590Srgrimes	(void)signal(SIGURG, copytochild);
2421590Srgrimes	(void)signal(SIGUSR1, writeroob);
2431590Srgrimes
24457232Sshin	rem = rcmd_af(&host, sp->s_port, localname, user, term, 0, family);
2451590Srgrimes
2461590Srgrimes	if (rem < 0)
2471590Srgrimes		exit(1);
2481590Srgrimes
2491590Srgrimes	if (dflag &&
2501590Srgrimes	    setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
25127919Scharnier		warn("setsockopt");
2528232Sdg	if (Dflag &&
2538232Sdg	    setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
25427919Scharnier		warn("setsockopt NODELAY (ignored)");
2558232Sdg
25656590Sshin	sslen = sizeof(ss);
2571590Srgrimes	one = IPTOS_LOWDELAY;
25856590Sshin	if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0 &&
25956590Sshin	    ss.ss_family == AF_INET) {
26056590Sshin		if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
26156590Sshin			       sizeof(int)) < 0)
26256590Sshin			warn("setsockopt TOS (ignored)");
26356590Sshin	} else
26456590Sshin		if (ss.ss_family == AF_INET)
26556590Sshin			warn("setsockopt getsockname failed");
2661590Srgrimes
2671590Srgrimes	(void)setuid(uid);
2681590Srgrimes	doit(omask);
2691590Srgrimes	/*NOTREACHED*/
2701590Srgrimes}
2711590Srgrimes
272120547Stjrint child;
2731590Srgrimes
2741590Srgrimesvoid
27593057Simpdoit(long omask)
2761590Srgrimes{
2771590Srgrimes
2781590Srgrimes	(void)signal(SIGINT, SIG_IGN);
2791590Srgrimes	setsignal(SIGHUP);
2801590Srgrimes	setsignal(SIGQUIT);
281120547Stjr	mode(1);
2821590Srgrimes	child = fork();
2831590Srgrimes	if (child == -1) {
28427919Scharnier		warn("fork");
2851590Srgrimes		done(1);
2861590Srgrimes	}
2871590Srgrimes	if (child == 0) {
2881590Srgrimes		if (reader(omask) == 0) {
289105235Scharnier			msg("connection closed");
2901590Srgrimes			exit(0);
2911590Srgrimes		}
2921590Srgrimes		sleep(1);
293105235Scharnier		msg("\007connection closed");
2941590Srgrimes		exit(1);
2951590Srgrimes	}
2961590Srgrimes
2971590Srgrimes	/*
2981590Srgrimes	 * We may still own the socket, and may have a pending SIGURG (or might
2991590Srgrimes	 * receive one soon) that we really want to send to the reader.  When
3001590Srgrimes	 * one of these comes in, the trap copytochild simply copies such
3011590Srgrimes	 * signals to the child. We can now unblock SIGURG and SIGUSR1
3021590Srgrimes	 * that were set above.
3031590Srgrimes	 */
3041590Srgrimes	(void)sigsetmask(omask);
3051590Srgrimes	(void)signal(SIGCHLD, catch_child);
3061590Srgrimes	writer();
307105235Scharnier	msg("closed connection");
3081590Srgrimes	done(0);
3091590Srgrimes}
3101590Srgrimes
3111590Srgrimes/* trap a signal, unless it is being ignored. */
3121590Srgrimesvoid
31393057Simpsetsignal(int sig)
3141590Srgrimes{
3151590Srgrimes	int omask = sigblock(sigmask(sig));
3161590Srgrimes
3171590Srgrimes	if (signal(sig, exit) == SIG_IGN)
3181590Srgrimes		(void)signal(sig, SIG_IGN);
3191590Srgrimes	(void)sigsetmask(omask);
3201590Srgrimes}
3211590Srgrimes
32218286Sbdevoid
32393057Simpdone(int status)
3241590Srgrimes{
3251590Srgrimes	int w, wstatus;
3261590Srgrimes
3271590Srgrimes	mode(0);
3281590Srgrimes	if (child > 0) {
3291590Srgrimes		/* make sure catch_child does not snap it up */
3301590Srgrimes		(void)signal(SIGCHLD, SIG_DFL);
3311590Srgrimes		if (kill(child, SIGKILL) >= 0)
3321590Srgrimes			while ((w = wait(&wstatus)) > 0 && w != child);
3331590Srgrimes	}
3341590Srgrimes	exit(status);
3351590Srgrimes}
3361590Srgrimes
3371590Srgrimesint dosigwinch;
3381590Srgrimes
3391590Srgrimes/*
3401590Srgrimes * This is called when the reader process gets the out-of-band (urgent)
3411590Srgrimes * request to turn on the window-changing protocol.
3421590Srgrimes */
343105268Smarkm/* ARGSUSED */
3441590Srgrimesvoid
34595621Smarkmwriteroob(int signo __unused)
3461590Srgrimes{
3471590Srgrimes	if (dosigwinch == 0) {
3481590Srgrimes		sendwindow();
3491590Srgrimes		(void)signal(SIGWINCH, sigwinch);
3501590Srgrimes	}
3511590Srgrimes	dosigwinch = 1;
3521590Srgrimes}
3531590Srgrimes
354105268Smarkm/* ARGSUSED */
3551590Srgrimesvoid
35695621Smarkmcatch_child(int signo __unused)
3571590Srgrimes{
35897788Smike	pid_t pid;
35997788Smike	int status;
3601590Srgrimes
3611590Srgrimes	for (;;) {
36297788Smike		pid = wait3(&status, WNOHANG|WUNTRACED, NULL);
3631590Srgrimes		if (pid == 0)
3641590Srgrimes			return;
3651590Srgrimes		/* if the child (reader) dies, just quit */
3661590Srgrimes		if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
36797788Smike			done(WTERMSIG(status) | WEXITSTATUS(status));
3681590Srgrimes	}
3691590Srgrimes	/* NOTREACHED */
3701590Srgrimes}
3711590Srgrimes
3721590Srgrimes/*
3731590Srgrimes * writer: write to remote: 0 -> line.
3741590Srgrimes * ~.				terminate
3751590Srgrimes * ~^Z				suspend rlogin process.
3761590Srgrimes * ~<delayed-suspend char>	suspend rlogin process, but leave reader alone.
3771590Srgrimes */
3781590Srgrimesvoid
37993057Simpwriter(void)
3801590Srgrimes{
38193057Simp	int bol, local, n;
3821590Srgrimes	char c;
3831590Srgrimes
3841590Srgrimes	bol = 1;			/* beginning of line */
3851590Srgrimes	local = 0;
3861590Srgrimes	for (;;) {
3871590Srgrimes		n = read(STDIN_FILENO, &c, 1);
3881590Srgrimes		if (n <= 0) {
3891590Srgrimes			if (n < 0 && errno == EINTR)
3901590Srgrimes				continue;
3911590Srgrimes			break;
3921590Srgrimes		}
3931590Srgrimes		/*
3941590Srgrimes		 * If we're at the beginning of the line and recognize a
3951590Srgrimes		 * command character, then we echo locally.  Otherwise,
3961590Srgrimes		 * characters are echo'd remotely.  If the command character
3971590Srgrimes		 * is doubled, this acts as a force and local echo is
3981590Srgrimes		 * suppressed.
3991590Srgrimes		 */
4001590Srgrimes		if (bol) {
4011590Srgrimes			bol = 0;
4021590Srgrimes			if (!noescape && c == escapechar) {
4031590Srgrimes				local = 1;
4041590Srgrimes				continue;
4051590Srgrimes			}
4061590Srgrimes		} else if (local) {
4071590Srgrimes			local = 0;
408120547Stjr			if (c == '.' || CCEQ(deftty.c_cc[VEOF], c)) {
4091590Srgrimes				echo(c);
4101590Srgrimes				break;
4111590Srgrimes			}
412120547Stjr			if (CCEQ(deftty.c_cc[VSUSP], c) ||
413120547Stjr			    CCEQ(deftty.c_cc[VDSUSP], c)) {
4141590Srgrimes				bol = 1;
4151590Srgrimes				echo(c);
4161590Srgrimes				stop(c);
4171590Srgrimes				continue;
4181590Srgrimes			}
4191590Srgrimes			if (c != escapechar)
420105268Smarkm				(void)write(rem, &escapechar, 1);
4211590Srgrimes		}
4221590Srgrimes
423105268Smarkm		if (write(rem, &c, 1) == 0) {
424105268Smarkm			msg("line gone");
425105268Smarkm			break;
426105268Smarkm		}
427120547Stjr		bol = CCEQ(deftty.c_cc[VKILL], c) ||
428120547Stjr		    CCEQ(deftty.c_cc[VEOF], c) ||
429120547Stjr		    CCEQ(deftty.c_cc[VINTR], c) ||
430120547Stjr		    CCEQ(deftty.c_cc[VSUSP], c) ||
4311590Srgrimes		    c == '\r' || c == '\n';
4321590Srgrimes	}
4331590Srgrimes}
4341590Srgrimes
4351590Srgrimesvoid
43693057Simpecho(char c)
4371590Srgrimes{
43893057Simp	char *p;
4391590Srgrimes	char buf[8];
4401590Srgrimes
4411590Srgrimes	p = buf;
4421590Srgrimes	c &= 0177;
4431590Srgrimes	*p++ = escapechar;
4441590Srgrimes	if (c < ' ') {
4451590Srgrimes		*p++ = '^';
4461590Srgrimes		*p++ = c + '@';
4471590Srgrimes	} else if (c == 0177) {
4481590Srgrimes		*p++ = '^';
4491590Srgrimes		*p++ = '?';
4501590Srgrimes	} else
4511590Srgrimes		*p++ = c;
4521590Srgrimes	*p++ = '\r';
4531590Srgrimes	*p++ = '\n';
4541590Srgrimes	(void)write(STDOUT_FILENO, buf, p - buf);
4551590Srgrimes}
4561590Srgrimes
4571590Srgrimesvoid
4581590Srgrimesstop(char cmdc)
4591590Srgrimes{
4601590Srgrimes	mode(0);
4611590Srgrimes	(void)signal(SIGCHLD, SIG_IGN);
462120547Stjr	(void)kill(CCEQ(deftty.c_cc[VSUSP], cmdc) ? 0 : getpid(), SIGTSTP);
4631590Srgrimes	(void)signal(SIGCHLD, catch_child);
4641590Srgrimes	mode(1);
4651590Srgrimes	sigwinch(0);			/* check for size changes */
4661590Srgrimes}
4671590Srgrimes
468105268Smarkm/* ARGSUSED */
4691590Srgrimesvoid
47095621Smarkmsigwinch(int signo __unused)
4711590Srgrimes{
4721590Srgrimes	struct winsize ws;
4731590Srgrimes
4741590Srgrimes	if (dosigwinch && get_window_size(0, &ws) == 0 &&
4751590Srgrimes	    bcmp(&ws, &winsize, sizeof(ws))) {
4761590Srgrimes		winsize = ws;
4771590Srgrimes		sendwindow();
4781590Srgrimes	}
4791590Srgrimes}
4801590Srgrimes
4811590Srgrimes/*
4821590Srgrimes * Send the window size to the server via the magic escape
4831590Srgrimes */
4841590Srgrimesvoid
48593057Simpsendwindow(void)
4861590Srgrimes{
487221079Sjhb	struct winsize ws;
4881590Srgrimes	char obuf[4 + sizeof (struct winsize)];
4891590Srgrimes
4901590Srgrimes	obuf[0] = 0377;
4911590Srgrimes	obuf[1] = 0377;
4921590Srgrimes	obuf[2] = 's';
4931590Srgrimes	obuf[3] = 's';
494221079Sjhb	ws.ws_row = htons(winsize.ws_row);
495221079Sjhb	ws.ws_col = htons(winsize.ws_col);
496221079Sjhb	ws.ws_xpixel = htons(winsize.ws_xpixel);
497221079Sjhb	ws.ws_ypixel = htons(winsize.ws_ypixel);
498221079Sjhb	bcopy(&ws, obuf + 4, sizeof(ws));
4991590Srgrimes
500105268Smarkm	(void)write(rem, obuf, sizeof(obuf));
5011590Srgrimes}
5021590Srgrimes
5031590Srgrimes/*
5041590Srgrimes * reader: read from remote: line -> 1
5051590Srgrimes */
5061590Srgrimes#define	READING	1
5071590Srgrimes#define	WRITING	2
5081590Srgrimes
5091590Srgrimesjmp_buf rcvtop;
510105268Smarkmint rcvcnt, rcvstate;
511105268Smarkmpid_t ppid;
5121590Srgrimeschar rcvbuf[8 * 1024];
5131590Srgrimes
514105268Smarkm/* ARGSUSED */
5151590Srgrimesvoid
51695621Smarkmoob(int signo __unused)
5171590Srgrimes{
518120547Stjr	struct termios tty;
519120547Stjr	int atmark, n, rcvd;
5201590Srgrimes	char waste[BUFSIZ], mark;
5211590Srgrimes
5221590Srgrimes	rcvd = 0;
5231590Srgrimes	while (recv(rem, &mark, 1, MSG_OOB) < 0) {
5241590Srgrimes		switch (errno) {
5251590Srgrimes		case EWOULDBLOCK:
5261590Srgrimes			/*
5271590Srgrimes			 * Urgent data not here yet.  It may not be possible
5281590Srgrimes			 * to send it yet if we are blocked for output and
5291590Srgrimes			 * our input buffer is full.
5301590Srgrimes			 */
53195621Smarkm			if (rcvcnt < (int)sizeof(rcvbuf)) {
5321590Srgrimes				n = read(rem, rcvbuf + rcvcnt,
5331590Srgrimes				    sizeof(rcvbuf) - rcvcnt);
5341590Srgrimes				if (n <= 0)
5351590Srgrimes					return;
5361590Srgrimes				rcvd += n;
5371590Srgrimes			} else {
5381590Srgrimes				n = read(rem, waste, sizeof(waste));
5391590Srgrimes				if (n <= 0)
5401590Srgrimes					return;
5411590Srgrimes			}
5421590Srgrimes			continue;
5431590Srgrimes		default:
5441590Srgrimes			return;
5451590Srgrimes		}
5461590Srgrimes	}
5471590Srgrimes	if (mark & TIOCPKT_WINDOW) {
5481590Srgrimes		/* Let server know about window size changes */
5491590Srgrimes		(void)kill(ppid, SIGUSR1);
5501590Srgrimes	}
5511590Srgrimes	if (!eight && (mark & TIOCPKT_NOSTOP)) {
552120547Stjr		(void)tcgetattr(0, &tty);
553120547Stjr		tty.c_iflag &= ~IXON;
554120547Stjr		(void)tcsetattr(0, TCSANOW, &tty);
5551590Srgrimes	}
5561590Srgrimes	if (!eight && (mark & TIOCPKT_DOSTOP)) {
557120547Stjr		(void)tcgetattr(0, &tty);
558120547Stjr		tty.c_iflag |= (deftty.c_iflag & IXON);
559120547Stjr		(void)tcsetattr(0, TCSANOW, &tty);
5601590Srgrimes	}
5611590Srgrimes	if (mark & TIOCPKT_FLUSHWRITE) {
562120547Stjr		(void)tcflush(1, TCIOFLUSH);
5631590Srgrimes		for (;;) {
5641590Srgrimes			if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
56527919Scharnier				warn("ioctl");
5661590Srgrimes				break;
5671590Srgrimes			}
5681590Srgrimes			if (atmark)
5691590Srgrimes				break;
5701590Srgrimes			n = read(rem, waste, sizeof (waste));
5711590Srgrimes			if (n <= 0)
5721590Srgrimes				break;
5731590Srgrimes		}
5741590Srgrimes		/*
5751590Srgrimes		 * Don't want any pending data to be output, so clear the recv
5761590Srgrimes		 * buffer.  If we were hanging on a write when interrupted,
5771590Srgrimes		 * don't want it to restart.  If we were reading, restart
5781590Srgrimes		 * anyway.
5791590Srgrimes		 */
5801590Srgrimes		rcvcnt = 0;
5811590Srgrimes		longjmp(rcvtop, 1);
5821590Srgrimes	}
5831590Srgrimes
5841590Srgrimes	/* oob does not do FLUSHREAD (alas!) */
5851590Srgrimes
5861590Srgrimes	/*
5871590Srgrimes	 * If we filled the receive buffer while a read was pending, longjmp
5881590Srgrimes	 * to the top to restart appropriately.  Don't abort a pending write,
5891590Srgrimes	 * however, or we won't know how much was written.
5901590Srgrimes	 */
5911590Srgrimes	if (rcvd && rcvstate == READING)
5921590Srgrimes		longjmp(rcvtop, 1);
5931590Srgrimes}
5941590Srgrimes
5951590Srgrimes/* reader: read from remote: line -> 1 */
5961590Srgrimesint
59793057Simpreader(int omask)
5981590Srgrimes{
599105268Smarkm	int n, remaining;
6001590Srgrimes	char *bufp;
601105268Smarkm	pid_t pid;
6021590Srgrimes
603105268Smarkm	pid = getpid();
6041590Srgrimes	(void)signal(SIGTTOU, SIG_IGN);
6051590Srgrimes	(void)signal(SIGURG, oob);
60691434Sfenner	(void)signal(SIGUSR1, oob); /* When propogating SIGURG from parent */
6071590Srgrimes	ppid = getppid();
6081590Srgrimes	(void)fcntl(rem, F_SETOWN, pid);
6091590Srgrimes	(void)setjmp(rcvtop);
6101590Srgrimes	(void)sigsetmask(omask);
6111590Srgrimes	bufp = rcvbuf;
6121590Srgrimes	for (;;) {
6131590Srgrimes		while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
6141590Srgrimes			rcvstate = WRITING;
6151590Srgrimes			n = write(STDOUT_FILENO, bufp, remaining);
6161590Srgrimes			if (n < 0) {
6171590Srgrimes				if (errno != EINTR)
6181590Srgrimes					return (-1);
6191590Srgrimes				continue;
6201590Srgrimes			}
6211590Srgrimes			bufp += n;
6221590Srgrimes		}
6231590Srgrimes		bufp = rcvbuf;
6241590Srgrimes		rcvcnt = 0;
6251590Srgrimes		rcvstate = READING;
6261590Srgrimes
627105268Smarkm		rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
6281590Srgrimes		if (rcvcnt == 0)
6291590Srgrimes			return (0);
6301590Srgrimes		if (rcvcnt < 0) {
6311590Srgrimes			if (errno == EINTR)
6321590Srgrimes				continue;
63327919Scharnier			warn("read");
6341590Srgrimes			return (-1);
6351590Srgrimes		}
6361590Srgrimes	}
6371590Srgrimes}
6381590Srgrimes
6391590Srgrimesvoid
64093057Simpmode(int f)
6411590Srgrimes{
642120547Stjr	struct termios tty;
6431590Srgrimes
644120547Stjr	switch (f) {
6451590Srgrimes	case 0:
646120547Stjr		(void)tcsetattr(0, TCSANOW, &deftty);
6471590Srgrimes		break;
6481590Srgrimes	case 1:
649120547Stjr		(void)tcgetattr(0, &deftty);
650120547Stjr		tty = deftty;
651120547Stjr		/* This is loosely derived from sys/kern/tty_compat.c. */
652120547Stjr		tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
653120547Stjr		tty.c_iflag &= ~ICRNL;
654120547Stjr		tty.c_oflag &= ~OPOST;
655120547Stjr		tty.c_cc[VMIN] = 1;
656120547Stjr		tty.c_cc[VTIME] = 0;
657120547Stjr		if (eight) {
658120547Stjr			tty.c_iflag &= IXOFF;
659120547Stjr			tty.c_cflag &= ~(CSIZE|PARENB);
660120547Stjr			tty.c_cflag |= CS8;
661120547Stjr		}
662120547Stjr		(void)tcsetattr(0, TCSANOW, &tty);
6631590Srgrimes		break;
6641590Srgrimes	default:
6651590Srgrimes		return;
6661590Srgrimes	}
6671590Srgrimes}
6681590Srgrimes
669105268Smarkm/* ARGSUSED */
6701590Srgrimesvoid
67195621Smarkmlostpeer(int signo __unused)
6721590Srgrimes{
6731590Srgrimes	(void)signal(SIGPIPE, SIG_IGN);
674105235Scharnier	msg("\007connection closed");
6751590Srgrimes	done(1);
6761590Srgrimes}
6771590Srgrimes
67891434Sfenner/* copy SIGURGs to the child process via SIGUSR1. */
679105268Smarkm/* ARGSUSED */
6801590Srgrimesvoid
68195621Smarkmcopytochild(int signo __unused)
6821590Srgrimes{
68391434Sfenner	(void)kill(child, SIGUSR1);
6841590Srgrimes}
6851590Srgrimes
6861590Srgrimesvoid
68795621Smarkmmsg(const char *str)
6881590Srgrimes{
6891590Srgrimes	(void)fprintf(stderr, "rlogin: %s\r\n", str);
6901590Srgrimes}
6911590Srgrimes
6921590Srgrimesvoid
69393057Simpusage(void)
6941590Srgrimes{
6951590Srgrimes	(void)fprintf(stderr,
69657232Sshin	"usage: rlogin [-46%s]%s[-e char] [-i localname] [-l username] host\n",
697120547Stjr	    "8DEd", " ");
6981590Srgrimes	exit(1);
6991590Srgrimes}
7001590Srgrimes
7011590Srgrimesu_int
702105268Smarkmgetescape(const char *p)
7031590Srgrimes{
7041590Srgrimes	long val;
705105268Smarkm	size_t len;
7061590Srgrimes
7071590Srgrimes	if ((len = strlen(p)) == 1)	/* use any single char, including '\' */
7081590Srgrimes		return ((u_int)*p);
7091590Srgrimes					/* otherwise, \nnn */
7101590Srgrimes	if (*p == '\\' && len >= 2 && len <= 4) {
7111590Srgrimes		val = strtol(++p, NULL, 8);
7121590Srgrimes		for (;;) {
7131590Srgrimes			if (!*++p)
7141590Srgrimes				return ((u_int)val);
7151590Srgrimes			if (*p < '0' || *p > '8')
7161590Srgrimes				break;
7171590Srgrimes		}
7181590Srgrimes	}
7191590Srgrimes	msg("illegal option value -- e");
7201590Srgrimes	usage();
7211590Srgrimes	/* NOTREACHED */
7221590Srgrimes}
723