xref: /csrg-svn/usr.bin/rlogin/rlogin.c (revision 56960)
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*56960Seric static char sccsid[] = "@(#)rlogin.c	5.36 (Berkeley) 12/02/92";
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;
5546850Sbostic int use_kerberos = 1, doencrypt;
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 int eight, litout, rem;
7045063Sbostic 
7145063Sbostic int noescape;
7245063Sbostic u_char escapechar = '~';
7345063Sbostic 
7440858Sbostic char *speeds[] = {
7540858Sbostic 	"0", "50", "75", "110", "134", "150", "200", "300", "600", "1200",
7640858Sbostic 	"1800", "2400", "4800", "9600", "19200", "38400"
7740858Sbostic };
7840858Sbostic 
7926981Skarels #ifdef sun
8026981Skarels struct winsize {
8126981Skarels 	unsigned short ws_row, ws_col;
8226981Skarels 	unsigned short ws_xpixel, ws_ypixel;
8326981Skarels };
8440858Sbostic #endif
8518358Ssam struct	winsize winsize;
866444Swnj 
8740858Sbostic #ifndef sun
8840858Sbostic #define	get_window_size(fd, wp)	ioctl(fd, TIOCGWINSZ, wp)
8940858Sbostic #endif
9029729Smckusick 
9140858Sbostic void exit();
9229729Smckusick 
936444Swnj main(argc, argv)
946444Swnj 	int argc;
956444Swnj 	char **argv;
966444Swnj {
9740858Sbostic 	extern char *optarg;
9840858Sbostic 	extern int optind;
9940858Sbostic 	struct passwd *pw;
10040858Sbostic 	struct servent *sp;
101*56960Seric 	struct hostent *hp;
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':
15353033Sleres 			noescape = 0;
15445063Sbostic 			escapechar = getescape(optarg);
15540858Sbostic 			break;
15640858Sbostic #ifdef KERBEROS
15740858Sbostic 		case 'k':
15840858Sbostic 			dest_realm = dst_realm_buf;
15940858Sbostic 			(void)strncpy(dest_realm, optarg, REALM_SZ);
16040858Sbostic 			break;
16140858Sbostic #endif
16240858Sbostic 		case 'l':
16340858Sbostic 			user = optarg;
16440858Sbostic 			break;
16553033Sleres #ifdef CRYPT
16653033Sleres #ifdef KERBEROS
16753033Sleres 		case 'x':
16853033Sleres 			doencrypt = 1;
16953033Sleres 			des_set_key(cred.session, schedule);
17053033Sleres 			break;
17153033Sleres #endif
17253033Sleres #endif
17340858Sbostic 		case '?':
17440858Sbostic 		default:
17540858Sbostic 			usage();
17636524Skfall 		}
17740858Sbostic 	optind += argoff;
17840858Sbostic 	argc -= optind;
17940858Sbostic 	argv += optind;
18036524Skfall 
18140858Sbostic 	/* if haven't gotten a host yet, do so */
18240858Sbostic 	if (!host && !(host = *argv++))
18340858Sbostic 		usage();
18436511Skfall 
18540858Sbostic 	if (*argv)
18640858Sbostic 		usage();
18740858Sbostic 
18840858Sbostic 	if (!(pw = getpwuid(uid = getuid()))) {
18940858Sbostic 		(void)fprintf(stderr, "rlogin: unknown user id.\n");
1906444Swnj 		exit(1);
1916444Swnj 	}
19240858Sbostic 	if (!user)
19340858Sbostic 		user = pw->pw_name;
19440858Sbostic 
19540868Sbostic 	sp = NULL;
19640858Sbostic #ifdef KERBEROS
19740868Sbostic 	if (use_kerberos) {
19846850Sbostic 		sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
19940868Sbostic 		if (sp == NULL) {
20040868Sbostic 			use_kerberos = 0;
20140868Sbostic 			warning("can't get entry for %s/tcp service",
20246850Sbostic 			    doencrypt ? "eklogin" : "klogin");
20340868Sbostic 		}
20436512Skfall 	}
20536512Skfall #endif
20640868Sbostic 	if (sp == NULL)
20740868Sbostic 		sp = getservbyname("login", "tcp");
20840858Sbostic 	if (sp == NULL) {
20940858Sbostic 		(void)fprintf(stderr, "rlogin: login/tcp: unknown service.\n");
21040858Sbostic 		exit(1);
2119365Ssam 	}
21240858Sbostic 
21340858Sbostic 	(void)strcpy(term, (p = getenv("TERM")) ? p : "network");
21418358Ssam 	if (ioctl(0, TIOCGETP, &ttyb) == 0) {
21540858Sbostic 		(void)strcat(term, "/");
21640858Sbostic 		(void)strcat(term, speeds[ttyb.sg_ospeed]);
2176444Swnj 	}
21840858Sbostic 
21940858Sbostic 	(void)get_window_size(0, &winsize);
22040858Sbostic 
22140858Sbostic 	(void)signal(SIGPIPE, lostpeer);
22229729Smckusick 	/* will use SIGUSR1 for window size hack, so hold it off */
22340858Sbostic 	omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
22436511Skfall 
22540858Sbostic #ifdef KERBEROS
22636512Skfall try_connect:
22740858Sbostic 	if (use_kerberos) {
228*56960Seric 		/* fully qualify hostname (needed for krb_realmofhost) */
229*56960Seric 		hp = gethostbyname(host);
230*56960Seric 		if (hp != NULL && !(host = strdup(hp->h_name))) {
231*56960Seric 			(void)fprintf(stderr, "rlogin: %s.\n", strerror(ENOMEM));
232*56960Seric 			exit(1);
233*56960Seric 		}
234*56960Seric 
23536512Skfall 		rem = KSUCCESS;
23640858Sbostic 		errno = 0;
23738728Skfall 		if (dest_realm == NULL)
23838728Skfall 			dest_realm = krb_realmofhost(host);
23938728Skfall 
24053033Sleres #ifdef CRYPT
24153033Sleres 		if (doencrypt)
24253033Sleres 			rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
24353033Sleres 			    dest_realm, &cred, schedule);
24453033Sleres 		else
24553033Sleres #endif /* CRYPT */
24640858Sbostic 			rem = krcmd(&host, sp->s_port, user, term, 0,
24740858Sbostic 			    dest_realm);
24838728Skfall 		if (rem < 0) {
24936512Skfall 			use_kerberos = 0;
25036628Skfall 			sp = getservbyname("login", "tcp");
25140858Sbostic 			if (sp == NULL) {
25240858Sbostic 				(void)fprintf(stderr,
25340858Sbostic 				    "rlogin: unknown service login/tcp.\n");
25436628Skfall 				exit(1);
25536628Skfall 			}
25638728Skfall 			if (errno == ECONNREFUSED)
25740858Sbostic 				warning("remote host doesn't support Kerberos");
25838728Skfall 			if (errno == ENOENT)
25940858Sbostic 				warning("can't provide Kerberos auth data");
26036512Skfall 			goto try_connect;
26136512Skfall 		}
26236511Skfall 	} else {
26353033Sleres #ifdef CRYPT
26453033Sleres 		if (doencrypt) {
26553033Sleres 			(void)fprintf(stderr,
26653033Sleres 			    "rlogin: the -x flag requires Kerberos authentication.\n");
26753033Sleres 			exit(1);
26853033Sleres 		}
26953033Sleres #endif /* CRYPT */
27040858Sbostic 		rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0);
27136511Skfall 	}
27236512Skfall #else
27340858Sbostic 	rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0);
27445256Smckusick #endif /* KERBEROS */
27536511Skfall 
27640858Sbostic 	if (rem < 0)
27736511Skfall 		exit(1);
27836511Skfall 
27940858Sbostic 	if (dflag &&
28040858Sbostic 	    setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
28140858Sbostic 		(void)fprintf(stderr, "rlogin: setsockopt: %s.\n",
28240858Sbostic 		    strerror(errno));
28344346Skarels 	one = IPTOS_LOWDELAY;
28444346Skarels 	if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one, sizeof(int)) < 0)
28544346Skarels 		perror("rlogin: setsockopt TOS (ignored)");
28640858Sbostic 
28740858Sbostic 	(void)setuid(uid);
28840858Sbostic 	doit(omask);
2899365Ssam 	/*NOTREACHED*/
2906444Swnj }
2916444Swnj 
29240858Sbostic int child, defflags, deflflags, tabflag;
29340858Sbostic char deferase, defkill;
29440858Sbostic struct tchars deftc;
29540858Sbostic struct ltchars defltc;
29640858Sbostic struct tchars notc = { -1, -1, -1, -1, -1, -1 };
29740858Sbostic struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
2986444Swnj 
29940858Sbostic doit(omask)
30040858Sbostic 	long omask;
3016444Swnj {
30213075Ssam 	struct sgttyb sb;
30340858Sbostic 	void catch_child(), copytochild(), exit(), writeroob();
3046444Swnj 
30540858Sbostic 	(void)ioctl(0, TIOCGETP, (char *)&sb);
30613075Ssam 	defflags = sb.sg_flags;
30712155Ssam 	tabflag = defflags & TBDELAY;
3089962Ssam 	defflags &= ECHO | CRMOD;
30913075Ssam 	deferase = sb.sg_erase;
31013075Ssam 	defkill = sb.sg_kill;
31140858Sbostic 	(void)ioctl(0, TIOCLGET, (char *)&deflflags);
31240858Sbostic 	(void)ioctl(0, TIOCGETC, (char *)&deftc);
31313075Ssam 	notc.t_startc = deftc.t_startc;
31413075Ssam 	notc.t_stopc = deftc.t_stopc;
31540858Sbostic 	(void)ioctl(0, TIOCGLTC, (char *)&defltc);
31640858Sbostic 	(void)signal(SIGINT, SIG_IGN);
31729729Smckusick 	setsignal(SIGHUP, exit);
31829729Smckusick 	setsignal(SIGQUIT, exit);
3199365Ssam 	child = fork();
3209365Ssam 	if (child == -1) {
32140858Sbostic 		(void)fprintf(stderr, "rlogin: fork: %s.\n", strerror(errno));
32225424Skarels 		done(1);
3239365Ssam 	}
3249365Ssam 	if (child == 0) {
32524726Smckusick 		mode(1);
32640858Sbostic 		if (reader(omask) == 0) {
32740858Sbostic 			msg("connection closed.");
32825424Skarels 			exit(0);
32925424Skarels 		}
33012155Ssam 		sleep(1);
33140858Sbostic 		msg("\007connection closed.");
33240858Sbostic 		exit(1);
3336444Swnj 	}
33429729Smckusick 
33529729Smckusick 	/*
33640858Sbostic 	 * We may still own the socket, and may have a pending SIGURG (or might
33740858Sbostic 	 * receive one soon) that we really want to send to the reader.  Set a
33840858Sbostic 	 * trap that simply copies such signals to the child.
33929729Smckusick 	 */
34040858Sbostic 	(void)signal(SIGURG, copytochild);
34140858Sbostic 	(void)signal(SIGUSR1, writeroob);
34240858Sbostic 	(void)sigsetmask(omask);
34340858Sbostic 	(void)signal(SIGCHLD, catch_child);
3449365Ssam 	writer();
34540858Sbostic 	msg("closed connection.");
34625424Skarels 	done(0);
3476444Swnj }
3486444Swnj 
34940858Sbostic /* trap a signal, unless it is being ignored. */
35029729Smckusick setsignal(sig, act)
35140858Sbostic 	int sig;
35240858Sbostic 	void (*act)();
35329729Smckusick {
35429729Smckusick 	int omask = sigblock(sigmask(sig));
35529729Smckusick 
35629729Smckusick 	if (signal(sig, act) == SIG_IGN)
35740858Sbostic 		(void)signal(sig, SIG_IGN);
35840858Sbostic 	(void)sigsetmask(omask);
35929729Smckusick }
36029729Smckusick 
36125424Skarels done(status)
36225424Skarels 	int status;
3636444Swnj {
36446850Sbostic 	int w, wstatus;
3656444Swnj 
3666444Swnj 	mode(0);
36729729Smckusick 	if (child > 0) {
36840858Sbostic 		/* make sure catch_child does not snap it up */
36940858Sbostic 		(void)signal(SIGCHLD, SIG_DFL);
37029729Smckusick 		if (kill(child, SIGKILL) >= 0)
37146850Sbostic 			while ((w = wait(&wstatus)) > 0 && w != child);
37229729Smckusick 	}
37325424Skarels 	exit(status);
3746444Swnj }
3756444Swnj 
37640858Sbostic int dosigwinch;
37746850Sbostic void sigwinch();
37829729Smckusick 
37929729Smckusick /*
38024726Smckusick  * This is called when the reader process gets the out-of-band (urgent)
38124726Smckusick  * request to turn on the window-changing protocol.
38224726Smckusick  */
38340858Sbostic void
38424726Smckusick writeroob()
38524726Smckusick {
38625341Smckusick 	if (dosigwinch == 0) {
38724919Smckusick 		sendwindow();
38840858Sbostic 		(void)signal(SIGWINCH, sigwinch);
38925341Smckusick 	}
39024726Smckusick 	dosigwinch = 1;
39124726Smckusick }
39224726Smckusick 
39340858Sbostic void
39440858Sbostic catch_child()
39511803Sedward {
39611803Sedward 	union wait status;
39711803Sedward 	int pid;
39811803Sedward 
39940858Sbostic 	for (;;) {
40054125Sbostic 		pid = wait3((int *)&status, WNOHANG|WUNTRACED, NULL);
40140858Sbostic 		if (pid == 0)
40240858Sbostic 			return;
40340858Sbostic 		/* if the child (reader) dies, just quit */
40440858Sbostic 		if (pid < 0 || pid == child && !WIFSTOPPED(status))
40540858Sbostic 			done((int)(status.w_termsig | status.w_retcode));
40640858Sbostic 	}
40740858Sbostic 	/* NOTREACHED */
40811803Sedward }
40911803Sedward 
4106444Swnj /*
4119365Ssam  * writer: write to remote: 0 -> line.
41245063Sbostic  * ~.				terminate
41345063Sbostic  * ~^Z				suspend rlogin process.
41445063Sbostic  * ~<delayed-suspend char>	suspend rlogin process, but leave reader alone.
4156444Swnj  */
4169365Ssam writer()
4176444Swnj {
41845063Sbostic 	register int bol, local, n;
41923530Sbloom 	char c;
4206444Swnj 
42140858Sbostic 	bol = 1;			/* beginning of line */
42240858Sbostic 	local = 0;
42311803Sedward 	for (;;) {
42440858Sbostic 		n = read(STDIN_FILENO, &c, 1);
42518358Ssam 		if (n <= 0) {
42618358Ssam 			if (n < 0 && errno == EINTR)
42718358Ssam 				continue;
42811803Sedward 			break;
42918358Ssam 		}
4309365Ssam 		/*
43140858Sbostic 		 * If we're at the beginning of the line and recognize a
43240858Sbostic 		 * command character, then we echo locally.  Otherwise,
43340858Sbostic 		 * characters are echo'd remotely.  If the command character
43440858Sbostic 		 * is doubled, this acts as a force and local echo is
43540858Sbostic 		 * suppressed.
4369365Ssam 		 */
43723530Sbloom 		if (bol) {
43823530Sbloom 			bol = 0;
43945063Sbostic 			if (!noescape && c == escapechar) {
44023530Sbloom 				local = 1;
44123530Sbloom 				continue;
4426444Swnj 			}
44323530Sbloom 		} else if (local) {
44423530Sbloom 			local = 0;
44523530Sbloom 			if (c == '.' || c == deftc.t_eofc) {
44623530Sbloom 				echo(c);
44723530Sbloom 				break;
4486444Swnj 			}
44923530Sbloom 			if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
45023530Sbloom 				bol = 1;
45123530Sbloom 				echo(c);
45223530Sbloom 				stop(c);
45323530Sbloom 				continue;
45423530Sbloom 			}
45545063Sbostic 			if (c != escapechar)
45653033Sleres #ifdef CRYPT
45753033Sleres #ifdef KERBEROS
45853033Sleres 				if (doencrypt)
45953033Sleres 					(void)des_write(rem, &escapechar, 1);
46053033Sleres 				else
46153033Sleres #endif
46253033Sleres #endif
46345063Sbostic 					(void)write(rem, &escapechar, 1);
4646444Swnj 		}
46536511Skfall 
46653033Sleres #ifdef CRYPT
46753033Sleres #ifdef KERBEROS
46853033Sleres 		if (doencrypt) {
46953033Sleres 			if (des_write(rem, &c, 1) == 0) {
47053033Sleres 				msg("line gone");
47153033Sleres 				break;
47253033Sleres 			}
47353033Sleres 		} else
47453033Sleres #endif
47553033Sleres #endif
47636511Skfall 			if (write(rem, &c, 1) == 0) {
47740858Sbostic 				msg("line gone");
47836511Skfall 				break;
47936511Skfall 			}
48023530Sbloom 		bol = c == defkill || c == deftc.t_eofc ||
48125424Skarels 		    c == deftc.t_intrc || c == defltc.t_suspc ||
48223530Sbloom 		    c == '\r' || c == '\n';
4836444Swnj 	}
4846444Swnj }
4856444Swnj 
48623530Sbloom echo(c)
48723530Sbloom register char c;
48823530Sbloom {
48940858Sbostic 	register char *p;
49023530Sbloom 	char buf[8];
49123530Sbloom 
49240858Sbostic 	p = buf;
49323530Sbloom 	c &= 0177;
49445063Sbostic 	*p++ = escapechar;
49523530Sbloom 	if (c < ' ') {
49623530Sbloom 		*p++ = '^';
49723530Sbloom 		*p++ = c + '@';
49823530Sbloom 	} else if (c == 0177) {
49923530Sbloom 		*p++ = '^';
50023530Sbloom 		*p++ = '?';
50123530Sbloom 	} else
50223530Sbloom 		*p++ = c;
50323530Sbloom 	*p++ = '\r';
50423530Sbloom 	*p++ = '\n';
50545063Sbostic 	(void)write(STDOUT_FILENO, buf, p - buf);
50623530Sbloom }
50723530Sbloom 
50818358Ssam stop(cmdc)
50918358Ssam 	char cmdc;
51018358Ssam {
51118358Ssam 	mode(0);
51240858Sbostic 	(void)signal(SIGCHLD, SIG_IGN);
51340858Sbostic 	(void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
51440858Sbostic 	(void)signal(SIGCHLD, catch_child);
51518358Ssam 	mode(1);
51618358Ssam 	sigwinch();			/* check for size changes */
51718358Ssam }
51818358Ssam 
51940858Sbostic void
52018358Ssam sigwinch()
52118358Ssam {
52218358Ssam 	struct winsize ws;
52318358Ssam 
52429729Smckusick 	if (dosigwinch && get_window_size(0, &ws) == 0 &&
52540858Sbostic 	    bcmp(&ws, &winsize, sizeof(ws))) {
52618358Ssam 		winsize = ws;
52724726Smckusick 		sendwindow();
52818358Ssam 	}
52918358Ssam }
53018358Ssam 
53124726Smckusick /*
53224726Smckusick  * Send the window size to the server via the magic escape
53324726Smckusick  */
53424726Smckusick sendwindow()
53524726Smckusick {
53640858Sbostic 	struct winsize *wp;
53724726Smckusick 	char obuf[4 + sizeof (struct winsize)];
53824726Smckusick 
53940858Sbostic 	wp = (struct winsize *)(obuf+4);
54024726Smckusick 	obuf[0] = 0377;
54124726Smckusick 	obuf[1] = 0377;
54224726Smckusick 	obuf[2] = 's';
54324726Smckusick 	obuf[3] = 's';
54424726Smckusick 	wp->ws_row = htons(winsize.ws_row);
54524726Smckusick 	wp->ws_col = htons(winsize.ws_col);
54624726Smckusick 	wp->ws_xpixel = htons(winsize.ws_xpixel);
54724726Smckusick 	wp->ws_ypixel = htons(winsize.ws_ypixel);
54836511Skfall 
54953033Sleres #ifdef CRYPT
55053033Sleres #ifdef KERBEROS
55153033Sleres 	if(doencrypt)
55253033Sleres 		(void)des_write(rem, obuf, sizeof(obuf));
55353033Sleres 	else
55453033Sleres #endif
55553033Sleres #endif
55640858Sbostic 		(void)write(rem, obuf, sizeof(obuf));
55724726Smckusick }
55824726Smckusick 
55925424Skarels /*
56025424Skarels  * reader: read from remote: line -> 1
56125424Skarels  */
56225424Skarels #define	READING	1
56325424Skarels #define	WRITING	2
56425424Skarels 
56540858Sbostic jmp_buf rcvtop;
56640858Sbostic int ppid, rcvcnt, rcvstate;
56740858Sbostic char rcvbuf[8 * 1024];
56825424Skarels 
56940858Sbostic void
5706444Swnj oob()
5716444Swnj {
57240858Sbostic 	struct sgttyb sb;
57340858Sbostic 	int atmark, n, out, rcvd;
5749365Ssam 	char waste[BUFSIZ], mark;
5756444Swnj 
57642230Sbostic 	out = O_RDWR;
57740858Sbostic 	rcvd = 0;
57825424Skarels 	while (recv(rem, &mark, 1, MSG_OOB) < 0)
57925424Skarels 		switch (errno) {
58025424Skarels 		case EWOULDBLOCK:
58125424Skarels 			/*
58240858Sbostic 			 * Urgent data not here yet.  It may not be possible
58340858Sbostic 			 * to send it yet if we are blocked for output and
58440858Sbostic 			 * our input buffer is full.
58525424Skarels 			 */
58625424Skarels 			if (rcvcnt < sizeof(rcvbuf)) {
58725424Skarels 				n = read(rem, rcvbuf + rcvcnt,
58840858Sbostic 				    sizeof(rcvbuf) - rcvcnt);
58925424Skarels 				if (n <= 0)
59025424Skarels 					return;
59125424Skarels 				rcvd += n;
59225424Skarels 			} else {
59325424Skarels 				n = read(rem, waste, sizeof(waste));
59425424Skarels 				if (n <= 0)
59525424Skarels 					return;
59625424Skarels 			}
59725424Skarels 			continue;
59825424Skarels 		default:
59925424Skarels 			return;
6006444Swnj 	}
60125424Skarels 	if (mark & TIOCPKT_WINDOW) {
60240858Sbostic 		/* Let server know about window size changes */
60340858Sbostic 		(void)kill(ppid, SIGUSR1);
60424726Smckusick 	}
60525424Skarels 	if (!eight && (mark & TIOCPKT_NOSTOP)) {
60640858Sbostic 		(void)ioctl(0, TIOCGETP, (char *)&sb);
60724726Smckusick 		sb.sg_flags &= ~CBREAK;
60824726Smckusick 		sb.sg_flags |= RAW;
60940858Sbostic 		(void)ioctl(0, TIOCSETN, (char *)&sb);
61013075Ssam 		notc.t_stopc = -1;
61113075Ssam 		notc.t_startc = -1;
61240858Sbostic 		(void)ioctl(0, TIOCSETC, (char *)&notc);
6136444Swnj 	}
61425424Skarels 	if (!eight && (mark & TIOCPKT_DOSTOP)) {
61540858Sbostic 		(void)ioctl(0, TIOCGETP, (char *)&sb);
61624726Smckusick 		sb.sg_flags &= ~RAW;
61724726Smckusick 		sb.sg_flags |= CBREAK;
61840858Sbostic 		(void)ioctl(0, TIOCSETN, (char *)&sb);
61913075Ssam 		notc.t_stopc = deftc.t_stopc;
62013075Ssam 		notc.t_startc = deftc.t_startc;
62140858Sbostic 		(void)ioctl(0, TIOCSETC, (char *)&notc);
6226444Swnj 	}
62325424Skarels 	if (mark & TIOCPKT_FLUSHWRITE) {
62440858Sbostic 		(void)ioctl(1, TIOCFLUSH, (char *)&out);
62525424Skarels 		for (;;) {
62625424Skarels 			if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
62740858Sbostic 				(void)fprintf(stderr, "rlogin: ioctl: %s.\n",
62840858Sbostic 				    strerror(errno));
62925424Skarels 				break;
63025424Skarels 			}
63125424Skarels 			if (atmark)
63225424Skarels 				break;
63325424Skarels 			n = read(rem, waste, sizeof (waste));
63425424Skarels 			if (n <= 0)
63525424Skarels 				break;
63625424Skarels 		}
63725424Skarels 		/*
63840858Sbostic 		 * Don't want any pending data to be output, so clear the recv
63940858Sbostic 		 * buffer.  If we were hanging on a write when interrupted,
64040858Sbostic 		 * don't want it to restart.  If we were reading, restart
64140858Sbostic 		 * anyway.
64225424Skarels 		 */
64325424Skarels 		rcvcnt = 0;
64425424Skarels 		longjmp(rcvtop, 1);
64525424Skarels 	}
64629729Smckusick 
64740858Sbostic 	/* oob does not do FLUSHREAD (alas!) */
64829729Smckusick 
64929729Smckusick 	/*
65040858Sbostic 	 * If we filled the receive buffer while a read was pending, longjmp
65140858Sbostic 	 * to the top to restart appropriately.  Don't abort a pending write,
65240858Sbostic 	 * however, or we won't know how much was written.
65325424Skarels 	 */
65425424Skarels 	if (rcvd && rcvstate == READING)
65525424Skarels 		longjmp(rcvtop, 1);
6566444Swnj }
6576444Swnj 
65840858Sbostic /* reader: read from remote: line -> 1 */
65940858Sbostic reader(omask)
66040858Sbostic 	int omask;
6616444Swnj {
66240858Sbostic 	void oob();
66340858Sbostic 
66426981Skarels #if !defined(BSD) || BSD < 43
66526981Skarels 	int pid = -getpid();
66626981Skarels #else
66725424Skarels 	int pid = getpid();
66826981Skarels #endif
66925424Skarels 	int n, remaining;
67025424Skarels 	char *bufp = rcvbuf;
6716444Swnj 
67240858Sbostic 	(void)signal(SIGTTOU, SIG_IGN);
67340858Sbostic 	(void)signal(SIGURG, oob);
67426981Skarels 	ppid = getppid();
67540858Sbostic 	(void)fcntl(rem, F_SETOWN, pid);
67640858Sbostic 	(void)setjmp(rcvtop);
67740858Sbostic 	(void)sigsetmask(omask);
6786444Swnj 	for (;;) {
67925424Skarels 		while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
68025424Skarels 			rcvstate = WRITING;
68145063Sbostic 			n = write(STDOUT_FILENO, bufp, remaining);
68225424Skarels 			if (n < 0) {
68325424Skarels 				if (errno != EINTR)
68440858Sbostic 					return(-1);
68525424Skarels 				continue;
68625424Skarels 			}
68725424Skarels 			bufp += n;
68825424Skarels 		}
68925424Skarels 		bufp = rcvbuf;
69025424Skarels 		rcvcnt = 0;
69125424Skarels 		rcvstate = READING;
69236511Skfall 
69353033Sleres #ifdef CRYPT
69453033Sleres #ifdef KERBEROS
69553033Sleres 		if (doencrypt)
69653033Sleres 			rcvcnt = des_read(rem, rcvbuf, sizeof(rcvbuf));
69753033Sleres 		else
69853033Sleres #endif
69953033Sleres #endif
70036511Skfall 			rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
70125424Skarels 		if (rcvcnt == 0)
70225424Skarels 			return (0);
70325424Skarels 		if (rcvcnt < 0) {
7049365Ssam 			if (errno == EINTR)
7056444Swnj 				continue;
70640858Sbostic 			(void)fprintf(stderr, "rlogin: read: %s.\n",
70740858Sbostic 			    strerror(errno));
70840858Sbostic 			return(-1);
7096444Swnj 		}
7106444Swnj 	}
7116444Swnj }
7126444Swnj 
7136444Swnj mode(f)
7146444Swnj {
71513075Ssam 	struct ltchars *ltc;
71613075Ssam 	struct sgttyb sb;
71740858Sbostic 	struct tchars *tc;
71840858Sbostic 	int lflags;
7199365Ssam 
72040858Sbostic 	(void)ioctl(0, TIOCGETP, (char *)&sb);
72140858Sbostic 	(void)ioctl(0, TIOCLGET, (char *)&lflags);
72240858Sbostic 	switch(f) {
7239962Ssam 	case 0:
72413075Ssam 		sb.sg_flags &= ~(CBREAK|RAW|TBDELAY);
72513075Ssam 		sb.sg_flags |= defflags|tabflag;
7269962Ssam 		tc = &deftc;
72713075Ssam 		ltc = &defltc;
72813075Ssam 		sb.sg_kill = defkill;
72913075Ssam 		sb.sg_erase = deferase;
73021583Sbloom 		lflags = deflflags;
7319962Ssam 		break;
7329962Ssam 	case 1:
73313075Ssam 		sb.sg_flags |= (eight ? RAW : CBREAK);
73413075Ssam 		sb.sg_flags &= ~defflags;
73512155Ssam 		/* preserve tab delays, but turn off XTABS */
73613075Ssam 		if ((sb.sg_flags & TBDELAY) == XTABS)
73713075Ssam 			sb.sg_flags &= ~TBDELAY;
7389962Ssam 		tc = &notc;
73913075Ssam 		ltc = &noltc;
74013075Ssam 		sb.sg_kill = sb.sg_erase = -1;
74121583Sbloom 		if (litout)
74221583Sbloom 			lflags |= LLITOUT;
7439962Ssam 		break;
7449962Ssam 	default:
7459962Ssam 		return;
7466444Swnj 	}
74740858Sbostic 	(void)ioctl(0, TIOCSLTC, (char *)ltc);
74840858Sbostic 	(void)ioctl(0, TIOCSETC, (char *)tc);
74940858Sbostic 	(void)ioctl(0, TIOCSETN, (char *)&sb);
75040858Sbostic 	(void)ioctl(0, TIOCLSET, (char *)&lflags);
7516444Swnj }
7526444Swnj 
75340858Sbostic void
75440858Sbostic lostpeer()
7556444Swnj {
75640858Sbostic 	(void)signal(SIGPIPE, SIG_IGN);
75740858Sbostic 	msg("\007connection closed.");
75840858Sbostic 	done(1);
75940858Sbostic }
76029729Smckusick 
76140858Sbostic /* copy SIGURGs to the child process. */
76240858Sbostic void
76340858Sbostic copytochild()
76440858Sbostic {
76540858Sbostic 	(void)kill(child, SIGURG);
7666444Swnj }
7676444Swnj 
76840858Sbostic msg(str)
76940858Sbostic 	char *str;
7706444Swnj {
77140858Sbostic 	(void)fprintf(stderr, "rlogin: %s\r\n", str);
77240858Sbostic }
77329729Smckusick 
77440858Sbostic #ifdef KERBEROS
77540858Sbostic /* VARARGS */
77640858Sbostic warning(va_alist)
77740858Sbostic va_dcl
77840858Sbostic {
77940858Sbostic 	va_list ap;
78040858Sbostic 	char *fmt;
78140858Sbostic 
78240858Sbostic 	(void)fprintf(stderr, "rlogin: warning, using standard rlogin: ");
78340858Sbostic 	va_start(ap);
78440858Sbostic 	fmt = va_arg(ap, char *);
78540858Sbostic 	vfprintf(stderr, fmt, ap);
78640858Sbostic 	va_end(ap);
78740858Sbostic 	(void)fprintf(stderr, ".\n");
7886444Swnj }
78940858Sbostic #endif
79036512Skfall 
79140858Sbostic usage()
79236512Skfall {
79340858Sbostic 	(void)fprintf(stderr,
79440858Sbostic 	    "usage: rlogin [ -%s]%s[-e char] [ -l username ] host\n",
79540858Sbostic #ifdef KERBEROS
79653033Sleres #ifdef CRYPT
79754125Sbostic 	    "8EKLx", " [-k realm] ");
79853033Sleres #else
79954125Sbostic 	    "8EKL", " [-k realm] ");
80053033Sleres #endif
80145256Smckusick #else
80245063Sbostic 	    "8EL", " ");
80340858Sbostic #endif
80440858Sbostic 	exit(1);
80536512Skfall }
80640858Sbostic 
80740858Sbostic /*
80840858Sbostic  * The following routine provides compatibility (such as it is) between 4.2BSD
80940858Sbostic  * Suns and others.  Suns have only a `ttysize', so we convert it to a winsize.
81040858Sbostic  */
81140858Sbostic #ifdef sun
81240858Sbostic get_window_size(fd, wp)
81340858Sbostic 	int fd;
81440858Sbostic 	struct winsize *wp;
81540858Sbostic {
81640858Sbostic 	struct ttysize ts;
81740858Sbostic 	int error;
81840858Sbostic 
81940858Sbostic 	if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
82040858Sbostic 		return(error);
82140858Sbostic 	wp->ws_row = ts.ts_lines;
82240858Sbostic 	wp->ws_col = ts.ts_cols;
82340858Sbostic 	wp->ws_xpixel = 0;
82440858Sbostic 	wp->ws_ypixel = 0;
82540858Sbostic 	return(0);
82640858Sbostic }
82740858Sbostic #endif
82845063Sbostic 
82945063Sbostic u_char
83045063Sbostic getescape(p)
83145063Sbostic 	register char *p;
83245063Sbostic {
83345063Sbostic 	long val;
83445063Sbostic 	int len;
83545063Sbostic 
83645063Sbostic 	if ((len = strlen(p)) == 1)	/* use any single char, including '\' */
83745063Sbostic 		return((u_char)*p);
83845063Sbostic 					/* otherwise, \nnn */
83945063Sbostic 	if (*p == '\\' && len >= 2 && len <= 4) {
84045063Sbostic 		val = strtol(++p, (char **)NULL, 8);
84145063Sbostic 		for (;;) {
84245063Sbostic 			if (!*++p)
84345063Sbostic 				return((u_char)val);
84445063Sbostic 			if (*p < '0' || *p > '8')
84545063Sbostic 				break;
84645063Sbostic 		}
84745063Sbostic 	}
84845063Sbostic 	msg("illegal option value -- e");
84945063Sbostic 	usage();
85045063Sbostic 	/* NOTREACHED */
85145063Sbostic }
852