xref: /csrg-svn/usr.bin/rlogin/rlogin.c (revision 45256)
144346Skarels /*
240858Sbostic  * Copyright (c) 1983, 1990 The Regents of the University of California.
335539Sbostic  * All rights reserved.
435539Sbostic  *
542763Sbostic  * %sccs.include.redist.c%
621595Sdist  */
721595Sdist 
86444Swnj #ifndef lint
921595Sdist char copyright[] =
1040858Sbostic "@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n\
1121595Sdist  All rights reserved.\n";
1235539Sbostic #endif /* not lint */
136444Swnj 
1421595Sdist #ifndef lint
15*45256Smckusick static char sccsid[] = "@(#)rlogin.c	5.31 (Berkeley) 09/27/90";
1635539Sbostic #endif /* not lint */
1721595Sdist 
1812990Ssam /*
1940858Sbostic  * $Source: mit/rlogin/RCS/rlogin.c,v $
2042763Sbostic  * $Header: mit/rlogin/RCS/rlogin.c,v 5.2 89/07/26 12:11:21 kfall
2142763Sbostic  *	Exp Locker: kfall $
2240858Sbostic  */
2340858Sbostic 
2440858Sbostic /*
2512990Ssam  * rlogin - remote login
2612990Ssam  */
2726981Skarels #include <sys/param.h>
2824727Smckusick #include <sys/file.h>
296444Swnj #include <sys/socket.h>
3040858Sbostic #include <sys/signal.h>
3129729Smckusick #include <sys/time.h>
3229729Smckusick #include <sys/resource.h>
3313620Ssam #include <sys/wait.h>
349365Ssam 
359207Ssam #include <netinet/in.h>
3644346Skarels #include <netinet/in_systm.h>
3744346Skarels #include <netinet/ip.h>
3840858Sbostic #include <netdb.h>
399365Ssam 
409365Ssam #include <sgtty.h>
4140858Sbostic #include <setjmp.h>
4245063Sbostic #include <varargs.h>
436444Swnj #include <errno.h>
446444Swnj #include <pwd.h>
4540858Sbostic #include <stdio.h>
4640858Sbostic #include <unistd.h>
4742058Sbostic #include <string.h>
486444Swnj 
4940858Sbostic #ifdef KERBEROS
5041760Skfall #include <kerberosIV/des.h>
5140683Sbostic #include <kerberosIV/krb.h>
5236511Skfall 
5340858Sbostic CREDENTIALS cred;
5440858Sbostic Key_schedule schedule;
5540858Sbostic int use_kerberos = 1, encrypt;
5640858Sbostic char dst_realm_buf[REALM_SZ], *dest_realm = NULL;
5740858Sbostic extern char *krb_realmofhost();
5840858Sbostic #endif
5924726Smckusick 
6040858Sbostic #ifndef TIOCPKT_WINDOW
6140858Sbostic #define	TIOCPKT_WINDOW	0x80
6240858Sbostic #endif
6329729Smckusick 
6440858Sbostic /* concession to Sun */
6540858Sbostic #ifndef SIGUSR1
6640858Sbostic #define	SIGUSR1	30
6726981Skarels #endif
6840858Sbostic 
6940858Sbostic extern int errno;
7040858Sbostic int eight, litout, rem;
7145063Sbostic 
7245063Sbostic int noescape;
7345063Sbostic u_char escapechar = '~';
7445063Sbostic 
7540858Sbostic char *speeds[] = {
7640858Sbostic 	"0", "50", "75", "110", "134", "150", "200", "300", "600", "1200",
7740858Sbostic 	"1800", "2400", "4800", "9600", "19200", "38400"
7840858Sbostic };
7940858Sbostic 
8026981Skarels #ifdef sun
8126981Skarels struct winsize {
8226981Skarels 	unsigned short ws_row, ws_col;
8326981Skarels 	unsigned short ws_xpixel, ws_ypixel;
8426981Skarels };
8540858Sbostic #endif
8618358Ssam struct	winsize winsize;
876444Swnj 
8840858Sbostic #ifndef sun
8940858Sbostic #define	get_window_size(fd, wp)	ioctl(fd, TIOCGWINSZ, wp)
9040858Sbostic #endif
9129729Smckusick 
9240858Sbostic void exit();
9329729Smckusick 
946444Swnj main(argc, argv)
956444Swnj 	int argc;
966444Swnj 	char **argv;
976444Swnj {
9840858Sbostic 	extern char *optarg;
9940858Sbostic 	extern int optind;
10040858Sbostic 	struct passwd *pw;
10140858Sbostic 	struct servent *sp;
1026444Swnj 	struct sgttyb ttyb;
10340858Sbostic 	long omask;
10440858Sbostic 	int argoff, ch, dflag, one, uid;
10540858Sbostic 	char *host, *p, *user, term[1024];
10640858Sbostic 	void lostpeer();
10745063Sbostic 	u_char getescape();
10840858Sbostic 	char *getenv();
1096444Swnj 
11040858Sbostic 	argoff = dflag = 0;
11140858Sbostic 	one = 1;
11240858Sbostic 	host = user = NULL;
11340858Sbostic 
11440858Sbostic 	if (p = rindex(argv[0], '/'))
11540858Sbostic 		++p;
1166444Swnj 	else
11740858Sbostic 		p = argv[0];
11840858Sbostic 
11940858Sbostic 	if (strcmp(p, "rlogin"))
12040858Sbostic 		host = p;
12140858Sbostic 
12240858Sbostic 	/* handle "rlogin host flags" */
12340858Sbostic 	if (!host && argc > 2 && argv[1][0] != '-') {
12440858Sbostic 		host = argv[1];
12540858Sbostic 		argoff = 1;
1266444Swnj 	}
12736511Skfall 
12840858Sbostic #ifdef KERBEROS
12945063Sbostic #define	OPTIONS	"8EKLde:k:l:x"
13040858Sbostic #else
13145063Sbostic #define	OPTIONS	"8EKLde:l:"
13240858Sbostic #endif
13340858Sbostic 	while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != EOF)
13440858Sbostic 		switch(ch) {
13540858Sbostic 		case '8':
13640858Sbostic 			eight = 1;
13740858Sbostic 			break;
13845063Sbostic 		case 'E':
13945063Sbostic 			noescape = 1;
14045063Sbostic 			break;
14140868Sbostic 		case 'K':
14240868Sbostic #ifdef KERBEROS
14340868Sbostic 			use_kerberos = 0;
14440868Sbostic #endif
14540868Sbostic 			break;
14640858Sbostic 		case 'L':
14740858Sbostic 			litout = 1;
14840858Sbostic 			break;
14940858Sbostic 		case 'd':
15040858Sbostic 			dflag = 1;
15140858Sbostic 			break;
15240858Sbostic 		case 'e':
15345063Sbostic 			escapechar = getescape(optarg);
15440858Sbostic 			break;
15540858Sbostic #ifdef KERBEROS
15640858Sbostic 		case 'k':
15740858Sbostic 			dest_realm = dst_realm_buf;
15840858Sbostic 			(void)strncpy(dest_realm, optarg, REALM_SZ);
15940858Sbostic 			break;
16040858Sbostic #endif
16140858Sbostic 		case 'l':
16240858Sbostic 			user = optarg;
16340858Sbostic 			break;
164*45256Smckusick #if defined(KERBEROS) && defined(CRYPT)
16540858Sbostic 		case 'x':
16640858Sbostic 			encrypt = 1;
16740858Sbostic 			des_set_key(cred.session, schedule);
16840858Sbostic 			break;
16940858Sbostic #endif
17040858Sbostic 		case '?':
17140858Sbostic 		default:
17240858Sbostic 			usage();
17336524Skfall 		}
17440858Sbostic 	optind += argoff;
17540858Sbostic 	argc -= optind;
17640858Sbostic 	argv += optind;
17736524Skfall 
17840858Sbostic 	/* if haven't gotten a host yet, do so */
17940858Sbostic 	if (!host && !(host = *argv++))
18040858Sbostic 		usage();
18136511Skfall 
18240858Sbostic 	if (*argv)
18340858Sbostic 		usage();
18440858Sbostic 
18540858Sbostic 	if (!(pw = getpwuid(uid = getuid()))) {
18640858Sbostic 		(void)fprintf(stderr, "rlogin: unknown user id.\n");
1876444Swnj 		exit(1);
1886444Swnj 	}
18940858Sbostic 	if (!user)
19040858Sbostic 		user = pw->pw_name;
19140858Sbostic 
19240868Sbostic 	sp = NULL;
19340858Sbostic #ifdef KERBEROS
19440868Sbostic 	if (use_kerberos) {
19540868Sbostic 		sp = getservbyname((encrypt ? "eklogin" : "klogin"), "tcp");
19640868Sbostic 		if (sp == NULL) {
19740868Sbostic 			use_kerberos = 0;
19840868Sbostic 			warning("can't get entry for %s/tcp service",
19940868Sbostic 			    encrypt ? "eklogin" : "klogin");
20040868Sbostic 		}
20136512Skfall 	}
20236512Skfall #endif
20340868Sbostic 	if (sp == NULL)
20440868Sbostic 		sp = getservbyname("login", "tcp");
20540858Sbostic 	if (sp == NULL) {
20640858Sbostic 		(void)fprintf(stderr, "rlogin: login/tcp: unknown service.\n");
20740858Sbostic 		exit(1);
2089365Ssam 	}
20940858Sbostic 
21040858Sbostic 	(void)strcpy(term, (p = getenv("TERM")) ? p : "network");
21118358Ssam 	if (ioctl(0, TIOCGETP, &ttyb) == 0) {
21240858Sbostic 		(void)strcat(term, "/");
21340858Sbostic 		(void)strcat(term, speeds[ttyb.sg_ospeed]);
2146444Swnj 	}
21540858Sbostic 
21640858Sbostic 	(void)get_window_size(0, &winsize);
21740858Sbostic 
21840858Sbostic 	(void)signal(SIGPIPE, lostpeer);
21929729Smckusick 	/* will use SIGUSR1 for window size hack, so hold it off */
22040858Sbostic 	omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
22136511Skfall 
22240858Sbostic #ifdef KERBEROS
22336512Skfall try_connect:
22440858Sbostic 	if (use_kerberos) {
22536512Skfall 		rem = KSUCCESS;
22640858Sbostic 		errno = 0;
22738728Skfall 		if (dest_realm == NULL)
22838728Skfall 			dest_realm = krb_realmofhost(host);
22938728Skfall 
230*45256Smckusick #ifdef CRYPT
23140858Sbostic 		if (encrypt)
23240858Sbostic 			rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
23340858Sbostic 			    dest_realm, &cred, schedule);
23440858Sbostic 		else
235*45256Smckusick #endif /* CRYPT */
23640858Sbostic 			rem = krcmd(&host, sp->s_port, user, term, 0,
23740858Sbostic 			    dest_realm);
23838728Skfall 		if (rem < 0) {
23936512Skfall 			use_kerberos = 0;
24036628Skfall 			sp = getservbyname("login", "tcp");
24140858Sbostic 			if (sp == NULL) {
24240858Sbostic 				(void)fprintf(stderr,
24340858Sbostic 				    "rlogin: unknown service login/tcp.\n");
24436628Skfall 				exit(1);
24536628Skfall 			}
24638728Skfall 			if (errno == ECONNREFUSED)
24740858Sbostic 				warning("remote host doesn't support Kerberos");
24838728Skfall 			if (errno == ENOENT)
24940858Sbostic 				warning("can't provide Kerberos auth data");
25036512Skfall 			goto try_connect;
25136512Skfall 		}
25236511Skfall 	} else {
253*45256Smckusick #ifdef CRYPT
25440858Sbostic 		if (encrypt) {
25540858Sbostic 			(void)fprintf(stderr,
25640858Sbostic 			    "rlogin: the -x flag requires Kerberos authentication.\n");
25736512Skfall 			exit(1);
25836512Skfall 		}
259*45256Smckusick #endif /* CRYPT */
26040858Sbostic 		rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0);
26136511Skfall 	}
26236512Skfall #else
26340858Sbostic 	rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0);
264*45256Smckusick #endif /* KERBEROS */
26536511Skfall 
26640858Sbostic 	if (rem < 0)
26736511Skfall 		exit(1);
26836511Skfall 
26940858Sbostic 	if (dflag &&
27040858Sbostic 	    setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
27140858Sbostic 		(void)fprintf(stderr, "rlogin: setsockopt: %s.\n",
27240858Sbostic 		    strerror(errno));
27344346Skarels 	one = IPTOS_LOWDELAY;
27444346Skarels 	if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one, sizeof(int)) < 0)
27544346Skarels 		perror("rlogin: setsockopt TOS (ignored)");
27640858Sbostic 
27740858Sbostic 	(void)setuid(uid);
27840858Sbostic 	doit(omask);
2799365Ssam 	/*NOTREACHED*/
2806444Swnj }
2816444Swnj 
28240858Sbostic int child, defflags, deflflags, tabflag;
28340858Sbostic char deferase, defkill;
28440858Sbostic struct tchars deftc;
28540858Sbostic struct ltchars defltc;
28640858Sbostic struct tchars notc = { -1, -1, -1, -1, -1, -1 };
28740858Sbostic struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
2886444Swnj 
28940858Sbostic doit(omask)
29040858Sbostic 	long omask;
2916444Swnj {
29213075Ssam 	struct sgttyb sb;
29340858Sbostic 	void catch_child(), copytochild(), exit(), writeroob();
2946444Swnj 
29540858Sbostic 	(void)ioctl(0, TIOCGETP, (char *)&sb);
29613075Ssam 	defflags = sb.sg_flags;
29712155Ssam 	tabflag = defflags & TBDELAY;
2989962Ssam 	defflags &= ECHO | CRMOD;
29913075Ssam 	deferase = sb.sg_erase;
30013075Ssam 	defkill = sb.sg_kill;
30140858Sbostic 	(void)ioctl(0, TIOCLGET, (char *)&deflflags);
30240858Sbostic 	(void)ioctl(0, TIOCGETC, (char *)&deftc);
30313075Ssam 	notc.t_startc = deftc.t_startc;
30413075Ssam 	notc.t_stopc = deftc.t_stopc;
30540858Sbostic 	(void)ioctl(0, TIOCGLTC, (char *)&defltc);
30640858Sbostic 	(void)signal(SIGINT, SIG_IGN);
30729729Smckusick 	setsignal(SIGHUP, exit);
30829729Smckusick 	setsignal(SIGQUIT, exit);
3099365Ssam 	child = fork();
3109365Ssam 	if (child == -1) {
31140858Sbostic 		(void)fprintf(stderr, "rlogin: fork: %s.\n", strerror(errno));
31225424Skarels 		done(1);
3139365Ssam 	}
3149365Ssam 	if (child == 0) {
31524726Smckusick 		mode(1);
31640858Sbostic 		if (reader(omask) == 0) {
31740858Sbostic 			msg("connection closed.");
31825424Skarels 			exit(0);
31925424Skarels 		}
32012155Ssam 		sleep(1);
32140858Sbostic 		msg("\007connection closed.");
32240858Sbostic 		exit(1);
3236444Swnj 	}
32429729Smckusick 
32529729Smckusick 	/*
32640858Sbostic 	 * We may still own the socket, and may have a pending SIGURG (or might
32740858Sbostic 	 * receive one soon) that we really want to send to the reader.  Set a
32840858Sbostic 	 * trap that simply copies such signals to the child.
32929729Smckusick 	 */
33040858Sbostic 	(void)signal(SIGURG, copytochild);
33140858Sbostic 	(void)signal(SIGUSR1, writeroob);
33240858Sbostic 	(void)sigsetmask(omask);
33340858Sbostic 	(void)signal(SIGCHLD, catch_child);
3349365Ssam 	writer();
33540858Sbostic 	msg("closed connection.");
33625424Skarels 	done(0);
3376444Swnj }
3386444Swnj 
33940858Sbostic /* trap a signal, unless it is being ignored. */
34029729Smckusick setsignal(sig, act)
34140858Sbostic 	int sig;
34240858Sbostic 	void (*act)();
34329729Smckusick {
34429729Smckusick 	int omask = sigblock(sigmask(sig));
34529729Smckusick 
34629729Smckusick 	if (signal(sig, act) == SIG_IGN)
34740858Sbostic 		(void)signal(sig, SIG_IGN);
34840858Sbostic 	(void)sigsetmask(omask);
34929729Smckusick }
35029729Smckusick 
35125424Skarels done(status)
35225424Skarels 	int status;
3536444Swnj {
35429729Smckusick 	int w;
3556444Swnj 
3566444Swnj 	mode(0);
35729729Smckusick 	if (child > 0) {
35840858Sbostic 		/* make sure catch_child does not snap it up */
35940858Sbostic 		(void)signal(SIGCHLD, SIG_DFL);
36029729Smckusick 		if (kill(child, SIGKILL) >= 0)
36140858Sbostic 			while ((w = wait((union wait *)0)) > 0 && w != child);
36229729Smckusick 	}
36325424Skarels 	exit(status);
3646444Swnj }
3656444Swnj 
36640858Sbostic int dosigwinch;
36729729Smckusick 
36829729Smckusick /*
36924726Smckusick  * This is called when the reader process gets the out-of-band (urgent)
37024726Smckusick  * request to turn on the window-changing protocol.
37124726Smckusick  */
37240858Sbostic void
37324726Smckusick writeroob()
37424726Smckusick {
37540858Sbostic 	void sigwinch();
37624726Smckusick 
37725341Smckusick 	if (dosigwinch == 0) {
37824919Smckusick 		sendwindow();
37940858Sbostic 		(void)signal(SIGWINCH, sigwinch);
38025341Smckusick 	}
38124726Smckusick 	dosigwinch = 1;
38224726Smckusick }
38324726Smckusick 
38440858Sbostic void
38540858Sbostic catch_child()
38611803Sedward {
38711803Sedward 	union wait status;
38811803Sedward 	int pid;
38911803Sedward 
39040858Sbostic 	for (;;) {
39140858Sbostic 		pid = wait3(&status, WNOHANG|WUNTRACED, (struct rusage *)0);
39240858Sbostic 		if (pid == 0)
39340858Sbostic 			return;
39440858Sbostic 		/* if the child (reader) dies, just quit */
39540858Sbostic 		if (pid < 0 || pid == child && !WIFSTOPPED(status))
39640858Sbostic 			done((int)(status.w_termsig | status.w_retcode));
39740858Sbostic 	}
39840858Sbostic 	/* NOTREACHED */
39911803Sedward }
40011803Sedward 
4016444Swnj /*
4029365Ssam  * writer: write to remote: 0 -> line.
40345063Sbostic  * ~.				terminate
40445063Sbostic  * ~^Z				suspend rlogin process.
40545063Sbostic  * ~<delayed-suspend char>	suspend rlogin process, but leave reader alone.
4066444Swnj  */
4079365Ssam writer()
4086444Swnj {
40945063Sbostic 	register int bol, local, n;
41023530Sbloom 	char c;
4116444Swnj 
41240858Sbostic 	bol = 1;			/* beginning of line */
41340858Sbostic 	local = 0;
41411803Sedward 	for (;;) {
41540858Sbostic 		n = read(STDIN_FILENO, &c, 1);
41618358Ssam 		if (n <= 0) {
41718358Ssam 			if (n < 0 && errno == EINTR)
41818358Ssam 				continue;
41911803Sedward 			break;
42018358Ssam 		}
4219365Ssam 		/*
42240858Sbostic 		 * If we're at the beginning of the line and recognize a
42340858Sbostic 		 * command character, then we echo locally.  Otherwise,
42440858Sbostic 		 * characters are echo'd remotely.  If the command character
42540858Sbostic 		 * is doubled, this acts as a force and local echo is
42640858Sbostic 		 * suppressed.
4279365Ssam 		 */
42823530Sbloom 		if (bol) {
42923530Sbloom 			bol = 0;
43045063Sbostic 			if (!noescape && c == escapechar) {
43123530Sbloom 				local = 1;
43223530Sbloom 				continue;
4336444Swnj 			}
43423530Sbloom 		} else if (local) {
43523530Sbloom 			local = 0;
43623530Sbloom 			if (c == '.' || c == deftc.t_eofc) {
43723530Sbloom 				echo(c);
43823530Sbloom 				break;
4396444Swnj 			}
44023530Sbloom 			if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
44123530Sbloom 				bol = 1;
44223530Sbloom 				echo(c);
44323530Sbloom 				stop(c);
44423530Sbloom 				continue;
44523530Sbloom 			}
44645063Sbostic 			if (c != escapechar)
447*45256Smckusick #if defined(KERBEROS) && defined(CRYPT)
44845063Sbostic 				if (encrypt)
44945063Sbostic 					(void)des_write(rem, &escapechar, 1);
45045063Sbostic 				else
45136511Skfall #endif
45245063Sbostic 					(void)write(rem, &escapechar, 1);
4536444Swnj 		}
45436511Skfall 
455*45256Smckusick #if defined(KERBEROS) && defined(CRYPT)
45640858Sbostic 		if (encrypt) {
45736511Skfall 			if (des_write(rem, &c, 1) == 0) {
45840858Sbostic 				msg("line gone");
45936511Skfall 				break;
46036511Skfall 			}
46136511Skfall 		} else
46236511Skfall #endif
46336511Skfall 			if (write(rem, &c, 1) == 0) {
46440858Sbostic 				msg("line gone");
46536511Skfall 				break;
46636511Skfall 			}
46723530Sbloom 		bol = c == defkill || c == deftc.t_eofc ||
46825424Skarels 		    c == deftc.t_intrc || c == defltc.t_suspc ||
46923530Sbloom 		    c == '\r' || c == '\n';
4706444Swnj 	}
4716444Swnj }
4726444Swnj 
47323530Sbloom echo(c)
47423530Sbloom register char c;
47523530Sbloom {
47640858Sbostic 	register char *p;
47723530Sbloom 	char buf[8];
47823530Sbloom 
47940858Sbostic 	p = buf;
48023530Sbloom 	c &= 0177;
48145063Sbostic 	*p++ = escapechar;
48223530Sbloom 	if (c < ' ') {
48323530Sbloom 		*p++ = '^';
48423530Sbloom 		*p++ = c + '@';
48523530Sbloom 	} else if (c == 0177) {
48623530Sbloom 		*p++ = '^';
48723530Sbloom 		*p++ = '?';
48823530Sbloom 	} else
48923530Sbloom 		*p++ = c;
49023530Sbloom 	*p++ = '\r';
49123530Sbloom 	*p++ = '\n';
49245063Sbostic 	(void)write(STDOUT_FILENO, buf, p - buf);
49323530Sbloom }
49423530Sbloom 
49518358Ssam stop(cmdc)
49618358Ssam 	char cmdc;
49718358Ssam {
49818358Ssam 	mode(0);
49940858Sbostic 	(void)signal(SIGCHLD, SIG_IGN);
50040858Sbostic 	(void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
50140858Sbostic 	(void)signal(SIGCHLD, catch_child);
50218358Ssam 	mode(1);
50318358Ssam 	sigwinch();			/* check for size changes */
50418358Ssam }
50518358Ssam 
50640858Sbostic void
50718358Ssam sigwinch()
50818358Ssam {
50918358Ssam 	struct winsize ws;
51018358Ssam 
51129729Smckusick 	if (dosigwinch && get_window_size(0, &ws) == 0 &&
51240858Sbostic 	    bcmp(&ws, &winsize, sizeof(ws))) {
51318358Ssam 		winsize = ws;
51424726Smckusick 		sendwindow();
51518358Ssam 	}
51618358Ssam }
51718358Ssam 
51824726Smckusick /*
51924726Smckusick  * Send the window size to the server via the magic escape
52024726Smckusick  */
52124726Smckusick sendwindow()
52224726Smckusick {
52340858Sbostic 	struct winsize *wp;
52424726Smckusick 	char obuf[4 + sizeof (struct winsize)];
52524726Smckusick 
52640858Sbostic 	wp = (struct winsize *)(obuf+4);
52724726Smckusick 	obuf[0] = 0377;
52824726Smckusick 	obuf[1] = 0377;
52924726Smckusick 	obuf[2] = 's';
53024726Smckusick 	obuf[3] = 's';
53124726Smckusick 	wp->ws_row = htons(winsize.ws_row);
53224726Smckusick 	wp->ws_col = htons(winsize.ws_col);
53324726Smckusick 	wp->ws_xpixel = htons(winsize.ws_xpixel);
53424726Smckusick 	wp->ws_ypixel = htons(winsize.ws_ypixel);
53536511Skfall 
536*45256Smckusick #if defined(KERBEROS) && defined(CRYPT)
53736511Skfall 	if(encrypt)
53840858Sbostic 		(void)des_write(rem, obuf, sizeof(obuf));
53936511Skfall 	else
54036511Skfall #endif
54140858Sbostic 		(void)write(rem, obuf, sizeof(obuf));
54224726Smckusick }
54324726Smckusick 
54425424Skarels /*
54525424Skarels  * reader: read from remote: line -> 1
54625424Skarels  */
54725424Skarels #define	READING	1
54825424Skarels #define	WRITING	2
54925424Skarels 
55040858Sbostic jmp_buf rcvtop;
55140858Sbostic int ppid, rcvcnt, rcvstate;
55240858Sbostic char rcvbuf[8 * 1024];
55325424Skarels 
55440858Sbostic void
5556444Swnj oob()
5566444Swnj {
55740858Sbostic 	struct sgttyb sb;
55840858Sbostic 	int atmark, n, out, rcvd;
5599365Ssam 	char waste[BUFSIZ], mark;
5606444Swnj 
56142230Sbostic 	out = O_RDWR;
56240858Sbostic 	rcvd = 0;
56325424Skarels 	while (recv(rem, &mark, 1, MSG_OOB) < 0)
56425424Skarels 		switch (errno) {
56525424Skarels 		case EWOULDBLOCK:
56625424Skarels 			/*
56740858Sbostic 			 * Urgent data not here yet.  It may not be possible
56840858Sbostic 			 * to send it yet if we are blocked for output and
56940858Sbostic 			 * our input buffer is full.
57025424Skarels 			 */
57125424Skarels 			if (rcvcnt < sizeof(rcvbuf)) {
57225424Skarels 				n = read(rem, rcvbuf + rcvcnt,
57340858Sbostic 				    sizeof(rcvbuf) - rcvcnt);
57425424Skarels 				if (n <= 0)
57525424Skarels 					return;
57625424Skarels 				rcvd += n;
57725424Skarels 			} else {
57825424Skarels 				n = read(rem, waste, sizeof(waste));
57925424Skarels 				if (n <= 0)
58025424Skarels 					return;
58125424Skarels 			}
58225424Skarels 			continue;
58325424Skarels 		default:
58425424Skarels 			return;
5856444Swnj 	}
58625424Skarels 	if (mark & TIOCPKT_WINDOW) {
58740858Sbostic 		/* Let server know about window size changes */
58840858Sbostic 		(void)kill(ppid, SIGUSR1);
58924726Smckusick 	}
59025424Skarels 	if (!eight && (mark & TIOCPKT_NOSTOP)) {
59140858Sbostic 		(void)ioctl(0, TIOCGETP, (char *)&sb);
59224726Smckusick 		sb.sg_flags &= ~CBREAK;
59324726Smckusick 		sb.sg_flags |= RAW;
59440858Sbostic 		(void)ioctl(0, TIOCSETN, (char *)&sb);
59513075Ssam 		notc.t_stopc = -1;
59613075Ssam 		notc.t_startc = -1;
59740858Sbostic 		(void)ioctl(0, TIOCSETC, (char *)&notc);
5986444Swnj 	}
59925424Skarels 	if (!eight && (mark & TIOCPKT_DOSTOP)) {
60040858Sbostic 		(void)ioctl(0, TIOCGETP, (char *)&sb);
60124726Smckusick 		sb.sg_flags &= ~RAW;
60224726Smckusick 		sb.sg_flags |= CBREAK;
60340858Sbostic 		(void)ioctl(0, TIOCSETN, (char *)&sb);
60413075Ssam 		notc.t_stopc = deftc.t_stopc;
60513075Ssam 		notc.t_startc = deftc.t_startc;
60640858Sbostic 		(void)ioctl(0, TIOCSETC, (char *)&notc);
6076444Swnj 	}
60825424Skarels 	if (mark & TIOCPKT_FLUSHWRITE) {
60940858Sbostic 		(void)ioctl(1, TIOCFLUSH, (char *)&out);
61025424Skarels 		for (;;) {
61125424Skarels 			if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
61240858Sbostic 				(void)fprintf(stderr, "rlogin: ioctl: %s.\n",
61340858Sbostic 				    strerror(errno));
61425424Skarels 				break;
61525424Skarels 			}
61625424Skarels 			if (atmark)
61725424Skarels 				break;
61825424Skarels 			n = read(rem, waste, sizeof (waste));
61925424Skarels 			if (n <= 0)
62025424Skarels 				break;
62125424Skarels 		}
62225424Skarels 		/*
62340858Sbostic 		 * Don't want any pending data to be output, so clear the recv
62440858Sbostic 		 * buffer.  If we were hanging on a write when interrupted,
62540858Sbostic 		 * don't want it to restart.  If we were reading, restart
62640858Sbostic 		 * anyway.
62725424Skarels 		 */
62825424Skarels 		rcvcnt = 0;
62925424Skarels 		longjmp(rcvtop, 1);
63025424Skarels 	}
63129729Smckusick 
63240858Sbostic 	/* oob does not do FLUSHREAD (alas!) */
63329729Smckusick 
63429729Smckusick 	/*
63540858Sbostic 	 * If we filled the receive buffer while a read was pending, longjmp
63640858Sbostic 	 * to the top to restart appropriately.  Don't abort a pending write,
63740858Sbostic 	 * however, or we won't know how much was written.
63825424Skarels 	 */
63925424Skarels 	if (rcvd && rcvstate == READING)
64025424Skarels 		longjmp(rcvtop, 1);
6416444Swnj }
6426444Swnj 
64340858Sbostic /* reader: read from remote: line -> 1 */
64440858Sbostic reader(omask)
64540858Sbostic 	int omask;
6466444Swnj {
64740858Sbostic 	void oob();
64840858Sbostic 
64926981Skarels #if !defined(BSD) || BSD < 43
65026981Skarels 	int pid = -getpid();
65126981Skarels #else
65225424Skarels 	int pid = getpid();
65326981Skarels #endif
65425424Skarels 	int n, remaining;
65525424Skarels 	char *bufp = rcvbuf;
6566444Swnj 
65740858Sbostic 	(void)signal(SIGTTOU, SIG_IGN);
65840858Sbostic 	(void)signal(SIGURG, oob);
65926981Skarels 	ppid = getppid();
66040858Sbostic 	(void)fcntl(rem, F_SETOWN, pid);
66140858Sbostic 	(void)setjmp(rcvtop);
66240858Sbostic 	(void)sigsetmask(omask);
6636444Swnj 	for (;;) {
66425424Skarels 		while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
66525424Skarels 			rcvstate = WRITING;
66645063Sbostic 			n = write(STDOUT_FILENO, bufp, remaining);
66725424Skarels 			if (n < 0) {
66825424Skarels 				if (errno != EINTR)
66940858Sbostic 					return(-1);
67025424Skarels 				continue;
67125424Skarels 			}
67225424Skarels 			bufp += n;
67325424Skarels 		}
67425424Skarels 		bufp = rcvbuf;
67525424Skarels 		rcvcnt = 0;
67625424Skarels 		rcvstate = READING;
67736511Skfall 
678*45256Smckusick #if defined(KERBEROS) && defined(CRYPT)
67940858Sbostic 		if (encrypt)
68036511Skfall 			rcvcnt = des_read(rem, rcvbuf, sizeof(rcvbuf));
68136511Skfall 		else
68236511Skfall #endif
68336511Skfall 			rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
68425424Skarels 		if (rcvcnt == 0)
68525424Skarels 			return (0);
68625424Skarels 		if (rcvcnt < 0) {
6879365Ssam 			if (errno == EINTR)
6886444Swnj 				continue;
68940858Sbostic 			(void)fprintf(stderr, "rlogin: read: %s.\n",
69040858Sbostic 			    strerror(errno));
69140858Sbostic 			return(-1);
6926444Swnj 		}
6936444Swnj 	}
6946444Swnj }
6956444Swnj 
6966444Swnj mode(f)
6976444Swnj {
69813075Ssam 	struct ltchars *ltc;
69913075Ssam 	struct sgttyb sb;
70040858Sbostic 	struct tchars *tc;
70140858Sbostic 	int lflags;
7029365Ssam 
70340858Sbostic 	(void)ioctl(0, TIOCGETP, (char *)&sb);
70440858Sbostic 	(void)ioctl(0, TIOCLGET, (char *)&lflags);
70540858Sbostic 	switch(f) {
7069962Ssam 	case 0:
70713075Ssam 		sb.sg_flags &= ~(CBREAK|RAW|TBDELAY);
70813075Ssam 		sb.sg_flags |= defflags|tabflag;
7099962Ssam 		tc = &deftc;
71013075Ssam 		ltc = &defltc;
71113075Ssam 		sb.sg_kill = defkill;
71213075Ssam 		sb.sg_erase = deferase;
71321583Sbloom 		lflags = deflflags;
7149962Ssam 		break;
7159962Ssam 	case 1:
71613075Ssam 		sb.sg_flags |= (eight ? RAW : CBREAK);
71713075Ssam 		sb.sg_flags &= ~defflags;
71812155Ssam 		/* preserve tab delays, but turn off XTABS */
71913075Ssam 		if ((sb.sg_flags & TBDELAY) == XTABS)
72013075Ssam 			sb.sg_flags &= ~TBDELAY;
7219962Ssam 		tc = &notc;
72213075Ssam 		ltc = &noltc;
72313075Ssam 		sb.sg_kill = sb.sg_erase = -1;
72421583Sbloom 		if (litout)
72521583Sbloom 			lflags |= LLITOUT;
7269962Ssam 		break;
7279962Ssam 	default:
7289962Ssam 		return;
7296444Swnj 	}
73040858Sbostic 	(void)ioctl(0, TIOCSLTC, (char *)ltc);
73140858Sbostic 	(void)ioctl(0, TIOCSETC, (char *)tc);
73240858Sbostic 	(void)ioctl(0, TIOCSETN, (char *)&sb);
73340858Sbostic 	(void)ioctl(0, TIOCLSET, (char *)&lflags);
7346444Swnj }
7356444Swnj 
73640858Sbostic void
73740858Sbostic lostpeer()
7386444Swnj {
73940858Sbostic 	(void)signal(SIGPIPE, SIG_IGN);
74040858Sbostic 	msg("\007connection closed.");
74140858Sbostic 	done(1);
74240858Sbostic }
74329729Smckusick 
74440858Sbostic /* copy SIGURGs to the child process. */
74540858Sbostic void
74640858Sbostic copytochild()
74740858Sbostic {
74840858Sbostic 	(void)kill(child, SIGURG);
7496444Swnj }
7506444Swnj 
75140858Sbostic msg(str)
75240858Sbostic 	char *str;
7536444Swnj {
75440858Sbostic 	(void)fprintf(stderr, "rlogin: %s\r\n", str);
75540858Sbostic }
75629729Smckusick 
75740858Sbostic #ifdef KERBEROS
75840858Sbostic /* VARARGS */
75940858Sbostic warning(va_alist)
76040858Sbostic va_dcl
76140858Sbostic {
76240858Sbostic 	va_list ap;
76340858Sbostic 	char *fmt;
76440858Sbostic 
76540858Sbostic 	(void)fprintf(stderr, "rlogin: warning, using standard rlogin: ");
76640858Sbostic 	va_start(ap);
76740858Sbostic 	fmt = va_arg(ap, char *);
76840858Sbostic 	vfprintf(stderr, fmt, ap);
76940858Sbostic 	va_end(ap);
77040858Sbostic 	(void)fprintf(stderr, ".\n");
7716444Swnj }
77240858Sbostic #endif
77336512Skfall 
77440858Sbostic usage()
77536512Skfall {
77640858Sbostic 	(void)fprintf(stderr,
77740858Sbostic 	    "usage: rlogin [ -%s]%s[-e char] [ -l username ] host\n",
77840858Sbostic #ifdef KERBEROS
779*45256Smckusick #ifdef CRYPT
78045063Sbostic 	    "8ELx", " [-k realm] ");
78140858Sbostic #else
782*45256Smckusick 	    "8EL", " [-k realm] ");
783*45256Smckusick #endif
784*45256Smckusick #else
78545063Sbostic 	    "8EL", " ");
78640858Sbostic #endif
78740858Sbostic 	exit(1);
78836512Skfall }
78940858Sbostic 
79040858Sbostic /*
79140858Sbostic  * The following routine provides compatibility (such as it is) between 4.2BSD
79240858Sbostic  * Suns and others.  Suns have only a `ttysize', so we convert it to a winsize.
79340858Sbostic  */
79440858Sbostic #ifdef sun
79540858Sbostic get_window_size(fd, wp)
79640858Sbostic 	int fd;
79740858Sbostic 	struct winsize *wp;
79840858Sbostic {
79940858Sbostic 	struct ttysize ts;
80040858Sbostic 	int error;
80140858Sbostic 
80240858Sbostic 	if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
80340858Sbostic 		return(error);
80440858Sbostic 	wp->ws_row = ts.ts_lines;
80540858Sbostic 	wp->ws_col = ts.ts_cols;
80640858Sbostic 	wp->ws_xpixel = 0;
80740858Sbostic 	wp->ws_ypixel = 0;
80840858Sbostic 	return(0);
80940858Sbostic }
81040858Sbostic #endif
81145063Sbostic 
81245063Sbostic u_char
81345063Sbostic getescape(p)
81445063Sbostic 	register char *p;
81545063Sbostic {
81645063Sbostic 	long val;
81745063Sbostic 	int len;
81845063Sbostic 
81945063Sbostic 	if ((len = strlen(p)) == 1)	/* use any single char, including '\' */
82045063Sbostic 		return((u_char)*p);
82145063Sbostic 					/* otherwise, \nnn */
82245063Sbostic 	if (*p == '\\' && len >= 2 && len <= 4) {
82345063Sbostic 		val = strtol(++p, (char **)NULL, 8);
82445063Sbostic 		for (;;) {
82545063Sbostic 			if (!*++p)
82645063Sbostic 				return((u_char)val);
82745063Sbostic 			if (*p < '0' || *p > '8')
82845063Sbostic 				break;
82945063Sbostic 		}
83045063Sbostic 	}
83145063Sbostic 	msg("illegal option value -- e");
83245063Sbostic 	usage();
83345063Sbostic 	/* NOTREACHED */
83445063Sbostic }
835