xref: /csrg-svn/usr.bin/rlogin/rlogin.c (revision 53033)
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*53033Sleres static char sccsid[] = "@(#)rlogin.c	5.34 (Berkeley) 03/20/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 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':
153*53033Sleres 			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;
165*53033Sleres #ifdef CRYPT
166*53033Sleres #ifdef KERBEROS
167*53033Sleres 		case 'x':
168*53033Sleres 			doencrypt = 1;
169*53033Sleres 			des_set_key(cred.session, schedule);
170*53033Sleres 			break;
171*53033Sleres #endif
172*53033Sleres #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) {
22836512Skfall 		rem = KSUCCESS;
22940858Sbostic 		errno = 0;
23038728Skfall 		if (dest_realm == NULL)
23138728Skfall 			dest_realm = krb_realmofhost(host);
23238728Skfall 
233*53033Sleres #ifdef CRYPT
234*53033Sleres 		if (doencrypt)
235*53033Sleres 			rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
236*53033Sleres 			    dest_realm, &cred, schedule);
237*53033Sleres 		else
238*53033Sleres #endif /* CRYPT */
23940858Sbostic 			rem = krcmd(&host, sp->s_port, user, term, 0,
24040858Sbostic 			    dest_realm);
24138728Skfall 		if (rem < 0) {
24236512Skfall 			use_kerberos = 0;
24336628Skfall 			sp = getservbyname("login", "tcp");
24440858Sbostic 			if (sp == NULL) {
24540858Sbostic 				(void)fprintf(stderr,
24640858Sbostic 				    "rlogin: unknown service login/tcp.\n");
24736628Skfall 				exit(1);
24836628Skfall 			}
24938728Skfall 			if (errno == ECONNREFUSED)
25040858Sbostic 				warning("remote host doesn't support Kerberos");
25138728Skfall 			if (errno == ENOENT)
25240858Sbostic 				warning("can't provide Kerberos auth data");
25336512Skfall 			goto try_connect;
25436512Skfall 		}
25536511Skfall 	} else {
256*53033Sleres #ifdef CRYPT
257*53033Sleres 		if (doencrypt) {
258*53033Sleres 			(void)fprintf(stderr,
259*53033Sleres 			    "rlogin: the -x flag requires Kerberos authentication.\n");
260*53033Sleres 			exit(1);
261*53033Sleres 		}
262*53033Sleres #endif /* CRYPT */
26340858Sbostic 		rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0);
26436511Skfall 	}
26536512Skfall #else
26640858Sbostic 	rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0);
26745256Smckusick #endif /* KERBEROS */
26836511Skfall 
26940858Sbostic 	if (rem < 0)
27036511Skfall 		exit(1);
27136511Skfall 
27240858Sbostic 	if (dflag &&
27340858Sbostic 	    setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
27440858Sbostic 		(void)fprintf(stderr, "rlogin: setsockopt: %s.\n",
27540858Sbostic 		    strerror(errno));
27644346Skarels 	one = IPTOS_LOWDELAY;
27744346Skarels 	if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one, sizeof(int)) < 0)
27844346Skarels 		perror("rlogin: setsockopt TOS (ignored)");
27940858Sbostic 
28040858Sbostic 	(void)setuid(uid);
28140858Sbostic 	doit(omask);
2829365Ssam 	/*NOTREACHED*/
2836444Swnj }
2846444Swnj 
28540858Sbostic int child, defflags, deflflags, tabflag;
28640858Sbostic char deferase, defkill;
28740858Sbostic struct tchars deftc;
28840858Sbostic struct ltchars defltc;
28940858Sbostic struct tchars notc = { -1, -1, -1, -1, -1, -1 };
29040858Sbostic struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
2916444Swnj 
29240858Sbostic doit(omask)
29340858Sbostic 	long omask;
2946444Swnj {
29513075Ssam 	struct sgttyb sb;
29640858Sbostic 	void catch_child(), copytochild(), exit(), writeroob();
2976444Swnj 
29840858Sbostic 	(void)ioctl(0, TIOCGETP, (char *)&sb);
29913075Ssam 	defflags = sb.sg_flags;
30012155Ssam 	tabflag = defflags & TBDELAY;
3019962Ssam 	defflags &= ECHO | CRMOD;
30213075Ssam 	deferase = sb.sg_erase;
30313075Ssam 	defkill = sb.sg_kill;
30440858Sbostic 	(void)ioctl(0, TIOCLGET, (char *)&deflflags);
30540858Sbostic 	(void)ioctl(0, TIOCGETC, (char *)&deftc);
30613075Ssam 	notc.t_startc = deftc.t_startc;
30713075Ssam 	notc.t_stopc = deftc.t_stopc;
30840858Sbostic 	(void)ioctl(0, TIOCGLTC, (char *)&defltc);
30940858Sbostic 	(void)signal(SIGINT, SIG_IGN);
31029729Smckusick 	setsignal(SIGHUP, exit);
31129729Smckusick 	setsignal(SIGQUIT, exit);
3129365Ssam 	child = fork();
3139365Ssam 	if (child == -1) {
31440858Sbostic 		(void)fprintf(stderr, "rlogin: fork: %s.\n", strerror(errno));
31525424Skarels 		done(1);
3169365Ssam 	}
3179365Ssam 	if (child == 0) {
31824726Smckusick 		mode(1);
31940858Sbostic 		if (reader(omask) == 0) {
32040858Sbostic 			msg("connection closed.");
32125424Skarels 			exit(0);
32225424Skarels 		}
32312155Ssam 		sleep(1);
32440858Sbostic 		msg("\007connection closed.");
32540858Sbostic 		exit(1);
3266444Swnj 	}
32729729Smckusick 
32829729Smckusick 	/*
32940858Sbostic 	 * We may still own the socket, and may have a pending SIGURG (or might
33040858Sbostic 	 * receive one soon) that we really want to send to the reader.  Set a
33140858Sbostic 	 * trap that simply copies such signals to the child.
33229729Smckusick 	 */
33340858Sbostic 	(void)signal(SIGURG, copytochild);
33440858Sbostic 	(void)signal(SIGUSR1, writeroob);
33540858Sbostic 	(void)sigsetmask(omask);
33640858Sbostic 	(void)signal(SIGCHLD, catch_child);
3379365Ssam 	writer();
33840858Sbostic 	msg("closed connection.");
33925424Skarels 	done(0);
3406444Swnj }
3416444Swnj 
34240858Sbostic /* trap a signal, unless it is being ignored. */
34329729Smckusick setsignal(sig, act)
34440858Sbostic 	int sig;
34540858Sbostic 	void (*act)();
34629729Smckusick {
34729729Smckusick 	int omask = sigblock(sigmask(sig));
34829729Smckusick 
34929729Smckusick 	if (signal(sig, act) == SIG_IGN)
35040858Sbostic 		(void)signal(sig, SIG_IGN);
35140858Sbostic 	(void)sigsetmask(omask);
35229729Smckusick }
35329729Smckusick 
35425424Skarels done(status)
35525424Skarels 	int status;
3566444Swnj {
35746850Sbostic 	int w, wstatus;
3586444Swnj 
3596444Swnj 	mode(0);
36029729Smckusick 	if (child > 0) {
36140858Sbostic 		/* make sure catch_child does not snap it up */
36240858Sbostic 		(void)signal(SIGCHLD, SIG_DFL);
36329729Smckusick 		if (kill(child, SIGKILL) >= 0)
36446850Sbostic 			while ((w = wait(&wstatus)) > 0 && w != child);
36529729Smckusick 	}
36625424Skarels 	exit(status);
3676444Swnj }
3686444Swnj 
36940858Sbostic int dosigwinch;
37046850Sbostic void sigwinch();
37129729Smckusick 
37229729Smckusick /*
37324726Smckusick  * This is called when the reader process gets the out-of-band (urgent)
37424726Smckusick  * request to turn on the window-changing protocol.
37524726Smckusick  */
37640858Sbostic void
37724726Smckusick writeroob()
37824726Smckusick {
37925341Smckusick 	if (dosigwinch == 0) {
38024919Smckusick 		sendwindow();
38140858Sbostic 		(void)signal(SIGWINCH, sigwinch);
38225341Smckusick 	}
38324726Smckusick 	dosigwinch = 1;
38424726Smckusick }
38524726Smckusick 
38640858Sbostic void
38740858Sbostic catch_child()
38811803Sedward {
38911803Sedward 	union wait status;
39011803Sedward 	int pid;
39111803Sedward 
39240858Sbostic 	for (;;) {
39346850Sbostic 		pid = wait3((int *)&status,
39446850Sbostic 		    WNOHANG|WUNTRACED, (struct rusage *)0);
39540858Sbostic 		if (pid == 0)
39640858Sbostic 			return;
39740858Sbostic 		/* if the child (reader) dies, just quit */
39840858Sbostic 		if (pid < 0 || pid == child && !WIFSTOPPED(status))
39940858Sbostic 			done((int)(status.w_termsig | status.w_retcode));
40040858Sbostic 	}
40140858Sbostic 	/* NOTREACHED */
40211803Sedward }
40311803Sedward 
4046444Swnj /*
4059365Ssam  * writer: write to remote: 0 -> line.
40645063Sbostic  * ~.				terminate
40745063Sbostic  * ~^Z				suspend rlogin process.
40845063Sbostic  * ~<delayed-suspend char>	suspend rlogin process, but leave reader alone.
4096444Swnj  */
4109365Ssam writer()
4116444Swnj {
41245063Sbostic 	register int bol, local, n;
41323530Sbloom 	char c;
4146444Swnj 
41540858Sbostic 	bol = 1;			/* beginning of line */
41640858Sbostic 	local = 0;
41711803Sedward 	for (;;) {
41840858Sbostic 		n = read(STDIN_FILENO, &c, 1);
41918358Ssam 		if (n <= 0) {
42018358Ssam 			if (n < 0 && errno == EINTR)
42118358Ssam 				continue;
42211803Sedward 			break;
42318358Ssam 		}
4249365Ssam 		/*
42540858Sbostic 		 * If we're at the beginning of the line and recognize a
42640858Sbostic 		 * command character, then we echo locally.  Otherwise,
42740858Sbostic 		 * characters are echo'd remotely.  If the command character
42840858Sbostic 		 * is doubled, this acts as a force and local echo is
42940858Sbostic 		 * suppressed.
4309365Ssam 		 */
43123530Sbloom 		if (bol) {
43223530Sbloom 			bol = 0;
43345063Sbostic 			if (!noescape && c == escapechar) {
43423530Sbloom 				local = 1;
43523530Sbloom 				continue;
4366444Swnj 			}
43723530Sbloom 		} else if (local) {
43823530Sbloom 			local = 0;
43923530Sbloom 			if (c == '.' || c == deftc.t_eofc) {
44023530Sbloom 				echo(c);
44123530Sbloom 				break;
4426444Swnj 			}
44323530Sbloom 			if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
44423530Sbloom 				bol = 1;
44523530Sbloom 				echo(c);
44623530Sbloom 				stop(c);
44723530Sbloom 				continue;
44823530Sbloom 			}
44945063Sbostic 			if (c != escapechar)
450*53033Sleres #ifdef CRYPT
451*53033Sleres #ifdef KERBEROS
452*53033Sleres 				if (doencrypt)
453*53033Sleres 					(void)des_write(rem, &escapechar, 1);
454*53033Sleres 				else
455*53033Sleres #endif
456*53033Sleres #endif
45745063Sbostic 					(void)write(rem, &escapechar, 1);
4586444Swnj 		}
45936511Skfall 
460*53033Sleres #ifdef CRYPT
461*53033Sleres #ifdef KERBEROS
462*53033Sleres 		if (doencrypt) {
463*53033Sleres 			if (des_write(rem, &c, 1) == 0) {
464*53033Sleres 				msg("line gone");
465*53033Sleres 				break;
466*53033Sleres 			}
467*53033Sleres 		} else
468*53033Sleres #endif
469*53033Sleres #endif
47036511Skfall 			if (write(rem, &c, 1) == 0) {
47140858Sbostic 				msg("line gone");
47236511Skfall 				break;
47336511Skfall 			}
47423530Sbloom 		bol = c == defkill || c == deftc.t_eofc ||
47525424Skarels 		    c == deftc.t_intrc || c == defltc.t_suspc ||
47623530Sbloom 		    c == '\r' || c == '\n';
4776444Swnj 	}
4786444Swnj }
4796444Swnj 
48023530Sbloom echo(c)
48123530Sbloom register char c;
48223530Sbloom {
48340858Sbostic 	register char *p;
48423530Sbloom 	char buf[8];
48523530Sbloom 
48640858Sbostic 	p = buf;
48723530Sbloom 	c &= 0177;
48845063Sbostic 	*p++ = escapechar;
48923530Sbloom 	if (c < ' ') {
49023530Sbloom 		*p++ = '^';
49123530Sbloom 		*p++ = c + '@';
49223530Sbloom 	} else if (c == 0177) {
49323530Sbloom 		*p++ = '^';
49423530Sbloom 		*p++ = '?';
49523530Sbloom 	} else
49623530Sbloom 		*p++ = c;
49723530Sbloom 	*p++ = '\r';
49823530Sbloom 	*p++ = '\n';
49945063Sbostic 	(void)write(STDOUT_FILENO, buf, p - buf);
50023530Sbloom }
50123530Sbloom 
50218358Ssam stop(cmdc)
50318358Ssam 	char cmdc;
50418358Ssam {
50518358Ssam 	mode(0);
50640858Sbostic 	(void)signal(SIGCHLD, SIG_IGN);
50740858Sbostic 	(void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
50840858Sbostic 	(void)signal(SIGCHLD, catch_child);
50918358Ssam 	mode(1);
51018358Ssam 	sigwinch();			/* check for size changes */
51118358Ssam }
51218358Ssam 
51340858Sbostic void
51418358Ssam sigwinch()
51518358Ssam {
51618358Ssam 	struct winsize ws;
51718358Ssam 
51829729Smckusick 	if (dosigwinch && get_window_size(0, &ws) == 0 &&
51940858Sbostic 	    bcmp(&ws, &winsize, sizeof(ws))) {
52018358Ssam 		winsize = ws;
52124726Smckusick 		sendwindow();
52218358Ssam 	}
52318358Ssam }
52418358Ssam 
52524726Smckusick /*
52624726Smckusick  * Send the window size to the server via the magic escape
52724726Smckusick  */
52824726Smckusick sendwindow()
52924726Smckusick {
53040858Sbostic 	struct winsize *wp;
53124726Smckusick 	char obuf[4 + sizeof (struct winsize)];
53224726Smckusick 
53340858Sbostic 	wp = (struct winsize *)(obuf+4);
53424726Smckusick 	obuf[0] = 0377;
53524726Smckusick 	obuf[1] = 0377;
53624726Smckusick 	obuf[2] = 's';
53724726Smckusick 	obuf[3] = 's';
53824726Smckusick 	wp->ws_row = htons(winsize.ws_row);
53924726Smckusick 	wp->ws_col = htons(winsize.ws_col);
54024726Smckusick 	wp->ws_xpixel = htons(winsize.ws_xpixel);
54124726Smckusick 	wp->ws_ypixel = htons(winsize.ws_ypixel);
54236511Skfall 
543*53033Sleres #ifdef CRYPT
544*53033Sleres #ifdef KERBEROS
545*53033Sleres 	if(doencrypt)
546*53033Sleres 		(void)des_write(rem, obuf, sizeof(obuf));
547*53033Sleres 	else
548*53033Sleres #endif
549*53033Sleres #endif
55040858Sbostic 		(void)write(rem, obuf, sizeof(obuf));
55124726Smckusick }
55224726Smckusick 
55325424Skarels /*
55425424Skarels  * reader: read from remote: line -> 1
55525424Skarels  */
55625424Skarels #define	READING	1
55725424Skarels #define	WRITING	2
55825424Skarels 
55940858Sbostic jmp_buf rcvtop;
56040858Sbostic int ppid, rcvcnt, rcvstate;
56140858Sbostic char rcvbuf[8 * 1024];
56225424Skarels 
56340858Sbostic void
5646444Swnj oob()
5656444Swnj {
56640858Sbostic 	struct sgttyb sb;
56740858Sbostic 	int atmark, n, out, rcvd;
5689365Ssam 	char waste[BUFSIZ], mark;
5696444Swnj 
57042230Sbostic 	out = O_RDWR;
57140858Sbostic 	rcvd = 0;
57225424Skarels 	while (recv(rem, &mark, 1, MSG_OOB) < 0)
57325424Skarels 		switch (errno) {
57425424Skarels 		case EWOULDBLOCK:
57525424Skarels 			/*
57640858Sbostic 			 * Urgent data not here yet.  It may not be possible
57740858Sbostic 			 * to send it yet if we are blocked for output and
57840858Sbostic 			 * our input buffer is full.
57925424Skarels 			 */
58025424Skarels 			if (rcvcnt < sizeof(rcvbuf)) {
58125424Skarels 				n = read(rem, rcvbuf + rcvcnt,
58240858Sbostic 				    sizeof(rcvbuf) - rcvcnt);
58325424Skarels 				if (n <= 0)
58425424Skarels 					return;
58525424Skarels 				rcvd += n;
58625424Skarels 			} else {
58725424Skarels 				n = read(rem, waste, sizeof(waste));
58825424Skarels 				if (n <= 0)
58925424Skarels 					return;
59025424Skarels 			}
59125424Skarels 			continue;
59225424Skarels 		default:
59325424Skarels 			return;
5946444Swnj 	}
59525424Skarels 	if (mark & TIOCPKT_WINDOW) {
59640858Sbostic 		/* Let server know about window size changes */
59740858Sbostic 		(void)kill(ppid, SIGUSR1);
59824726Smckusick 	}
59925424Skarels 	if (!eight && (mark & TIOCPKT_NOSTOP)) {
60040858Sbostic 		(void)ioctl(0, TIOCGETP, (char *)&sb);
60124726Smckusick 		sb.sg_flags &= ~CBREAK;
60224726Smckusick 		sb.sg_flags |= RAW;
60340858Sbostic 		(void)ioctl(0, TIOCSETN, (char *)&sb);
60413075Ssam 		notc.t_stopc = -1;
60513075Ssam 		notc.t_startc = -1;
60640858Sbostic 		(void)ioctl(0, TIOCSETC, (char *)&notc);
6076444Swnj 	}
60825424Skarels 	if (!eight && (mark & TIOCPKT_DOSTOP)) {
60940858Sbostic 		(void)ioctl(0, TIOCGETP, (char *)&sb);
61024726Smckusick 		sb.sg_flags &= ~RAW;
61124726Smckusick 		sb.sg_flags |= CBREAK;
61240858Sbostic 		(void)ioctl(0, TIOCSETN, (char *)&sb);
61313075Ssam 		notc.t_stopc = deftc.t_stopc;
61413075Ssam 		notc.t_startc = deftc.t_startc;
61540858Sbostic 		(void)ioctl(0, TIOCSETC, (char *)&notc);
6166444Swnj 	}
61725424Skarels 	if (mark & TIOCPKT_FLUSHWRITE) {
61840858Sbostic 		(void)ioctl(1, TIOCFLUSH, (char *)&out);
61925424Skarels 		for (;;) {
62025424Skarels 			if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
62140858Sbostic 				(void)fprintf(stderr, "rlogin: ioctl: %s.\n",
62240858Sbostic 				    strerror(errno));
62325424Skarels 				break;
62425424Skarels 			}
62525424Skarels 			if (atmark)
62625424Skarels 				break;
62725424Skarels 			n = read(rem, waste, sizeof (waste));
62825424Skarels 			if (n <= 0)
62925424Skarels 				break;
63025424Skarels 		}
63125424Skarels 		/*
63240858Sbostic 		 * Don't want any pending data to be output, so clear the recv
63340858Sbostic 		 * buffer.  If we were hanging on a write when interrupted,
63440858Sbostic 		 * don't want it to restart.  If we were reading, restart
63540858Sbostic 		 * anyway.
63625424Skarels 		 */
63725424Skarels 		rcvcnt = 0;
63825424Skarels 		longjmp(rcvtop, 1);
63925424Skarels 	}
64029729Smckusick 
64140858Sbostic 	/* oob does not do FLUSHREAD (alas!) */
64229729Smckusick 
64329729Smckusick 	/*
64440858Sbostic 	 * If we filled the receive buffer while a read was pending, longjmp
64540858Sbostic 	 * to the top to restart appropriately.  Don't abort a pending write,
64640858Sbostic 	 * however, or we won't know how much was written.
64725424Skarels 	 */
64825424Skarels 	if (rcvd && rcvstate == READING)
64925424Skarels 		longjmp(rcvtop, 1);
6506444Swnj }
6516444Swnj 
65240858Sbostic /* reader: read from remote: line -> 1 */
65340858Sbostic reader(omask)
65440858Sbostic 	int omask;
6556444Swnj {
65640858Sbostic 	void oob();
65740858Sbostic 
65826981Skarels #if !defined(BSD) || BSD < 43
65926981Skarels 	int pid = -getpid();
66026981Skarels #else
66125424Skarels 	int pid = getpid();
66226981Skarels #endif
66325424Skarels 	int n, remaining;
66425424Skarels 	char *bufp = rcvbuf;
6656444Swnj 
66640858Sbostic 	(void)signal(SIGTTOU, SIG_IGN);
66740858Sbostic 	(void)signal(SIGURG, oob);
66826981Skarels 	ppid = getppid();
66940858Sbostic 	(void)fcntl(rem, F_SETOWN, pid);
67040858Sbostic 	(void)setjmp(rcvtop);
67140858Sbostic 	(void)sigsetmask(omask);
6726444Swnj 	for (;;) {
67325424Skarels 		while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
67425424Skarels 			rcvstate = WRITING;
67545063Sbostic 			n = write(STDOUT_FILENO, bufp, remaining);
67625424Skarels 			if (n < 0) {
67725424Skarels 				if (errno != EINTR)
67840858Sbostic 					return(-1);
67925424Skarels 				continue;
68025424Skarels 			}
68125424Skarels 			bufp += n;
68225424Skarels 		}
68325424Skarels 		bufp = rcvbuf;
68425424Skarels 		rcvcnt = 0;
68525424Skarels 		rcvstate = READING;
68636511Skfall 
687*53033Sleres #ifdef CRYPT
688*53033Sleres #ifdef KERBEROS
689*53033Sleres 		if (doencrypt)
690*53033Sleres 			rcvcnt = des_read(rem, rcvbuf, sizeof(rcvbuf));
691*53033Sleres 		else
692*53033Sleres #endif
693*53033Sleres #endif
69436511Skfall 			rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
69525424Skarels 		if (rcvcnt == 0)
69625424Skarels 			return (0);
69725424Skarels 		if (rcvcnt < 0) {
6989365Ssam 			if (errno == EINTR)
6996444Swnj 				continue;
70040858Sbostic 			(void)fprintf(stderr, "rlogin: read: %s.\n",
70140858Sbostic 			    strerror(errno));
70240858Sbostic 			return(-1);
7036444Swnj 		}
7046444Swnj 	}
7056444Swnj }
7066444Swnj 
7076444Swnj mode(f)
7086444Swnj {
70913075Ssam 	struct ltchars *ltc;
71013075Ssam 	struct sgttyb sb;
71140858Sbostic 	struct tchars *tc;
71240858Sbostic 	int lflags;
7139365Ssam 
71440858Sbostic 	(void)ioctl(0, TIOCGETP, (char *)&sb);
71540858Sbostic 	(void)ioctl(0, TIOCLGET, (char *)&lflags);
71640858Sbostic 	switch(f) {
7179962Ssam 	case 0:
71813075Ssam 		sb.sg_flags &= ~(CBREAK|RAW|TBDELAY);
71913075Ssam 		sb.sg_flags |= defflags|tabflag;
7209962Ssam 		tc = &deftc;
72113075Ssam 		ltc = &defltc;
72213075Ssam 		sb.sg_kill = defkill;
72313075Ssam 		sb.sg_erase = deferase;
72421583Sbloom 		lflags = deflflags;
7259962Ssam 		break;
7269962Ssam 	case 1:
72713075Ssam 		sb.sg_flags |= (eight ? RAW : CBREAK);
72813075Ssam 		sb.sg_flags &= ~defflags;
72912155Ssam 		/* preserve tab delays, but turn off XTABS */
73013075Ssam 		if ((sb.sg_flags & TBDELAY) == XTABS)
73113075Ssam 			sb.sg_flags &= ~TBDELAY;
7329962Ssam 		tc = &notc;
73313075Ssam 		ltc = &noltc;
73413075Ssam 		sb.sg_kill = sb.sg_erase = -1;
73521583Sbloom 		if (litout)
73621583Sbloom 			lflags |= LLITOUT;
7379962Ssam 		break;
7389962Ssam 	default:
7399962Ssam 		return;
7406444Swnj 	}
74140858Sbostic 	(void)ioctl(0, TIOCSLTC, (char *)ltc);
74240858Sbostic 	(void)ioctl(0, TIOCSETC, (char *)tc);
74340858Sbostic 	(void)ioctl(0, TIOCSETN, (char *)&sb);
74440858Sbostic 	(void)ioctl(0, TIOCLSET, (char *)&lflags);
7456444Swnj }
7466444Swnj 
74740858Sbostic void
74840858Sbostic lostpeer()
7496444Swnj {
75040858Sbostic 	(void)signal(SIGPIPE, SIG_IGN);
75140858Sbostic 	msg("\007connection closed.");
75240858Sbostic 	done(1);
75340858Sbostic }
75429729Smckusick 
75540858Sbostic /* copy SIGURGs to the child process. */
75640858Sbostic void
75740858Sbostic copytochild()
75840858Sbostic {
75940858Sbostic 	(void)kill(child, SIGURG);
7606444Swnj }
7616444Swnj 
76240858Sbostic msg(str)
76340858Sbostic 	char *str;
7646444Swnj {
76540858Sbostic 	(void)fprintf(stderr, "rlogin: %s\r\n", str);
76640858Sbostic }
76729729Smckusick 
76840858Sbostic #ifdef KERBEROS
76940858Sbostic /* VARARGS */
77040858Sbostic warning(va_alist)
77140858Sbostic va_dcl
77240858Sbostic {
77340858Sbostic 	va_list ap;
77440858Sbostic 	char *fmt;
77540858Sbostic 
77640858Sbostic 	(void)fprintf(stderr, "rlogin: warning, using standard rlogin: ");
77740858Sbostic 	va_start(ap);
77840858Sbostic 	fmt = va_arg(ap, char *);
77940858Sbostic 	vfprintf(stderr, fmt, ap);
78040858Sbostic 	va_end(ap);
78140858Sbostic 	(void)fprintf(stderr, ".\n");
7826444Swnj }
78340858Sbostic #endif
78436512Skfall 
78540858Sbostic usage()
78636512Skfall {
78740858Sbostic 	(void)fprintf(stderr,
78840858Sbostic 	    "usage: rlogin [ -%s]%s[-e char] [ -l username ] host\n",
78940858Sbostic #ifdef KERBEROS
790*53033Sleres #ifdef CRYPT
791*53033Sleres 	    "8ELx", " [-k realm] ");
792*53033Sleres #else
79345256Smckusick 	    "8EL", " [-k realm] ");
794*53033Sleres #endif
79545256Smckusick #else
79645063Sbostic 	    "8EL", " ");
79740858Sbostic #endif
79840858Sbostic 	exit(1);
79936512Skfall }
80040858Sbostic 
80140858Sbostic /*
80240858Sbostic  * The following routine provides compatibility (such as it is) between 4.2BSD
80340858Sbostic  * Suns and others.  Suns have only a `ttysize', so we convert it to a winsize.
80440858Sbostic  */
80540858Sbostic #ifdef sun
80640858Sbostic get_window_size(fd, wp)
80740858Sbostic 	int fd;
80840858Sbostic 	struct winsize *wp;
80940858Sbostic {
81040858Sbostic 	struct ttysize ts;
81140858Sbostic 	int error;
81240858Sbostic 
81340858Sbostic 	if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
81440858Sbostic 		return(error);
81540858Sbostic 	wp->ws_row = ts.ts_lines;
81640858Sbostic 	wp->ws_col = ts.ts_cols;
81740858Sbostic 	wp->ws_xpixel = 0;
81840858Sbostic 	wp->ws_ypixel = 0;
81940858Sbostic 	return(0);
82040858Sbostic }
82140858Sbostic #endif
82245063Sbostic 
82345063Sbostic u_char
82445063Sbostic getescape(p)
82545063Sbostic 	register char *p;
82645063Sbostic {
82745063Sbostic 	long val;
82845063Sbostic 	int len;
82945063Sbostic 
83045063Sbostic 	if ((len = strlen(p)) == 1)	/* use any single char, including '\' */
83145063Sbostic 		return((u_char)*p);
83245063Sbostic 					/* otherwise, \nnn */
83345063Sbostic 	if (*p == '\\' && len >= 2 && len <= 4) {
83445063Sbostic 		val = strtol(++p, (char **)NULL, 8);
83545063Sbostic 		for (;;) {
83645063Sbostic 			if (!*++p)
83745063Sbostic 				return((u_char)val);
83845063Sbostic 			if (*p < '0' || *p > '8')
83945063Sbostic 				break;
84045063Sbostic 		}
84145063Sbostic 	}
84245063Sbostic 	msg("illegal option value -- e");
84345063Sbostic 	usage();
84445063Sbostic 	/* NOTREACHED */
84545063Sbostic }
846