xref: /csrg-svn/libexec/rlogind/rlogind.c (revision 24724)
121174Sdist /*
221174Sdist  * Copyright (c) 1983 Regents of the University of California.
321174Sdist  * All rights reserved.  The Berkeley software License Agreement
421174Sdist  * specifies the terms and conditions for redistribution.
521174Sdist  */
621174Sdist 
76446Swnj #ifndef lint
821174Sdist char copyright[] =
921174Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1021174Sdist  All rights reserved.\n";
1121174Sdist #endif not lint
126446Swnj 
1321174Sdist #ifndef lint
14*24724Smckusick static char sccsid[] = "@(#)rlogind.c	5.5 (Berkeley) 09/12/85";
1521174Sdist #endif not lint
1621174Sdist 
1716369Skarels /*
1816369Skarels  * remote login server:
1916369Skarels  *	remuser\0
2016369Skarels  *	locuser\0
2118357Ssam  *	terminal info\0
2216369Skarels  *	data
2316369Skarels  */
2416369Skarels 
256446Swnj #include <stdio.h>
266446Swnj #include <sys/types.h>
276446Swnj #include <sys/stat.h>
286446Swnj #include <sys/socket.h>
2913554Ssam #include <sys/wait.h>
3018357Ssam #include <sys/file.h>
319208Ssam 
329208Ssam #include <netinet/in.h>
339208Ssam 
346446Swnj #include <errno.h>
356446Swnj #include <pwd.h>
366446Swnj #include <signal.h>
376446Swnj #include <sgtty.h>
386446Swnj #include <stdio.h>
398380Ssam #include <netdb.h>
4017187Sralph #include <syslog.h>
4118357Ssam #include <strings.h>
426446Swnj 
4324723Smckusick # ifndef TIOCPKT_WINDOW
4424723Smckusick # define TIOCPKT_WINDOW 0x80
4524723Smckusick # endif TIOCPKT_WINDOW
4624723Smckusick 
476446Swnj extern	errno;
4810417Ssam int	reapchild();
496446Swnj struct	passwd *getpwnam();
5024723Smckusick char	*malloc();
5116369Skarels 
526446Swnj main(argc, argv)
536446Swnj 	int argc;
546446Swnj 	char **argv;
556446Swnj {
5617156Ssam 	int on = 1, options = 0, fromlen;
576446Swnj 	struct sockaddr_in from;
586446Swnj 
5916369Skarels 	fromlen = sizeof (from);
6016369Skarels 	if (getpeername(0, &from, &fromlen) < 0) {
6116369Skarels 		fprintf(stderr, "%s: ", argv[0]);
6216369Skarels 		perror("getpeername");
6316369Skarels 		_exit(1);
648380Ssam 	}
6517156Ssam 	if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
6617187Sralph 		openlog(argv[0], LOG_PID, 0);
6717187Sralph 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
686446Swnj 	}
6916369Skarels 	doit(0, &from);
706446Swnj }
716446Swnj 
726446Swnj int	child;
736446Swnj int	cleanup();
746446Swnj int	netf;
756446Swnj extern	errno;
766446Swnj char	*line;
77*24724Smckusick extern	char	*inet_ntoa();
786446Swnj 
7924723Smckusick 
806446Swnj doit(f, fromp)
816446Swnj 	int f;
826446Swnj 	struct sockaddr_in *fromp;
836446Swnj {
8418357Ssam 	int i, p, t, pid, on = 1;
8518357Ssam 	register struct hostent *hp;
86*24724Smckusick 	struct hostent hostent;
878380Ssam 	char c;
886446Swnj 
896446Swnj 	alarm(60);
906446Swnj 	read(f, &c, 1);
916446Swnj 	if (c != 0)
926446Swnj 		exit(1);
936446Swnj 	alarm(0);
9416227Skarels 	fromp->sin_port = ntohs((u_short)fromp->sin_port);
958380Ssam 	hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr),
968380Ssam 		fromp->sin_family);
9711345Ssam 	if (hp == 0) {
98*24724Smckusick 		/*
99*24724Smckusick 		 * Only the name is used below.
100*24724Smckusick 		 */
101*24724Smckusick 		hp = &hostent;
102*24724Smckusick 		hp->h_name = inet_ntoa(fromp->sin_addr);
10311345Ssam 	}
1046446Swnj 	if (fromp->sin_family != AF_INET ||
10516227Skarels 	    fromp->sin_port >= IPPORT_RESERVED)
1069242Ssam 		fatal(f, "Permission denied");
1076446Swnj 	write(f, "", 1);
1086446Swnj 	for (c = 'p'; c <= 's'; c++) {
1096446Swnj 		struct stat stb;
1106446Swnj 		line = "/dev/ptyXX";
1116446Swnj 		line[strlen("/dev/pty")] = c;
1126446Swnj 		line[strlen("/dev/ptyp")] = '0';
1136446Swnj 		if (stat(line, &stb) < 0)
1146446Swnj 			break;
1156446Swnj 		for (i = 0; i < 16; i++) {
1166446Swnj 			line[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
1176446Swnj 			p = open(line, 2);
1186446Swnj 			if (p > 0)
1196446Swnj 				goto gotpty;
1206446Swnj 		}
1216446Swnj 	}
12224723Smckusick 	fatal(f, "Out of ptys");
1239242Ssam 	/*NOTREACHED*/
1246446Swnj gotpty:
12516227Skarels 	netf = f;
1266446Swnj 	line[strlen("/dev/")] = 't';
1276446Swnj #ifdef DEBUG
1286446Swnj 	{ int tt = open("/dev/tty", 2);
1296446Swnj 	  if (tt > 0) {
1306446Swnj 		ioctl(tt, TIOCNOTTY, 0);
1316446Swnj 		close(tt);
1326446Swnj 	  }
1336446Swnj 	}
1346446Swnj #endif
1356446Swnj 	t = open(line, 2);
1369242Ssam 	if (t < 0)
1379242Ssam 		fatalperror(f, line, errno);
1386446Swnj 	{ struct sgttyb b;
1396446Swnj 	  gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b);
1406446Swnj 	}
1419242Ssam 	pid = fork();
1429242Ssam 	if (pid < 0)
1439242Ssam 		fatalperror(f, "", errno);
14418357Ssam 	if (pid == 0) {
14518357Ssam 		close(f), close(p);
14618357Ssam 		dup2(t, 0), dup2(t, 1), dup2(t, 2);
14716227Skarels 		close(t);
14818357Ssam 		execl("/bin/login", "login", "-r", hp->h_name, 0);
14918357Ssam 		fatalperror(2, "/bin/login", errno);
15018357Ssam 		/*NOTREACHED*/
15118357Ssam 	}
15218357Ssam 	close(t);
15318357Ssam 	ioctl(f, FIONBIO, &on);
15418357Ssam 	ioctl(p, FIONBIO, &on);
15518357Ssam 	ioctl(p, TIOCPKT, &on);
15618357Ssam 	signal(SIGTSTP, SIG_IGN);
15718357Ssam 	signal(SIGCHLD, cleanup);
158*24724Smckusick 	setpgrp(0, 0);
15918357Ssam 	protocol(f, p);
16018357Ssam 	cleanup();
16118357Ssam }
1629242Ssam 
16318357Ssam char	magic[2] = { 0377, 0377 };
16418357Ssam 
16518357Ssam /*
16618357Ssam  * Handle a "control" request (signaled by magic being present)
16718357Ssam  * in the data stream.  For now, we are only willing to handle
16818357Ssam  * window size changes.
16918357Ssam  */
17018357Ssam control(pty, cp, n)
17118357Ssam 	int pty;
17218357Ssam 	char *cp;
17318357Ssam 	int n;
17418357Ssam {
17518357Ssam 	struct winsize *wp;
17618357Ssam 
17718357Ssam 	if (n < 4+sizeof (*wp) || cp[2] != 's' || cp[3] != 's')
17818357Ssam 		return (0);
17918357Ssam 	wp = (struct winsize *)(cp+4);
18018357Ssam 	wp->ws_row = ntohs(wp->ws_row);
18118357Ssam 	wp->ws_col = ntohs(wp->ws_col);
18218357Ssam 	wp->ws_xpixel = ntohs(wp->ws_xpixel);
18318357Ssam 	wp->ws_ypixel = ntohs(wp->ws_ypixel);
18418357Ssam 	(void)ioctl(pty, TIOCSWINSZ, wp);
18518357Ssam 	return (4+sizeof (*wp));
18618357Ssam }
18718357Ssam 
18818357Ssam /*
18918357Ssam  * rlogin "protocol" machine.
19018357Ssam  */
19118357Ssam protocol(f, p)
19218357Ssam 	int f, p;
19318357Ssam {
19418357Ssam 	char pibuf[1024], fibuf[1024], *pbp, *fbp;
19518357Ssam 	register pcc = 0, fcc = 0;
19624723Smckusick 	int cc, stop = TIOCPKT_DOSTOP, wsize;
19724723Smckusick 	static char oob[] = {TIOCPKT_WINDOW};
19818357Ssam 
19918482Ssam 	/*
20018484Ssam 	 * Must ignore SIGTTOU, otherwise we'll stop
20118484Ssam 	 * when we try and set slave pty's window shape
20218484Ssam 	 * (our pgrp is that of the master pty).
20318482Ssam 	 */
20418484Ssam 	(void) signal(SIGTTOU, SIG_IGN);
20524723Smckusick 	send(f, oob, 1, MSG_OOB);	/* indicate new rlogin */
20618357Ssam 	for (;;) {
20718357Ssam 		int ibits = 0, obits = 0;
20818357Ssam 
20918357Ssam 		if (fcc)
21018357Ssam 			obits |= (1<<p);
21118357Ssam 		else
21218357Ssam 			ibits |= (1<<f);
21318357Ssam 		if (pcc >= 0)
21418357Ssam 			if (pcc)
21518357Ssam 				obits |= (1<<f);
2169242Ssam 			else
21718357Ssam 				ibits |= (1<<p);
21818357Ssam 		if (select(16, &ibits, &obits, 0, 0) < 0) {
21918357Ssam 			if (errno == EINTR)
2206446Swnj 				continue;
22118357Ssam 			fatalperror(f, "select", errno);
22218357Ssam 		}
22318357Ssam 		if (ibits == 0 && obits == 0) {
22418357Ssam 			/* shouldn't happen... */
22518357Ssam 			sleep(5);
22618357Ssam 			continue;
22718357Ssam 		}
22818357Ssam 		if (ibits & (1<<f)) {
22918357Ssam 			fcc = read(f, fibuf, sizeof (fibuf));
23018357Ssam 			if (fcc < 0 && errno == EWOULDBLOCK)
23118357Ssam 				fcc = 0;
23218357Ssam 			else {
23318357Ssam 				register char *cp;
23418357Ssam 				int left, n;
23518357Ssam 
23618357Ssam 				if (fcc <= 0)
23716227Skarels 					break;
23818357Ssam 				fbp = fibuf;
23924723Smckusick 
24018357Ssam 			top:
24118357Ssam 				for (cp = fibuf; cp < fibuf+fcc; cp++)
24218357Ssam 					if (cp[0] == magic[0] &&
24318357Ssam 					    cp[1] == magic[1]) {
24418357Ssam 						left = fcc - (cp-fibuf);
24518357Ssam 						n = control(p, cp, left);
24618357Ssam 						if (n) {
24718357Ssam 							left -= n;
24818357Ssam 							if (left > 0)
24918357Ssam 								bcopy(cp, cp+n, left);
25018357Ssam 							fcc -= n;
25118357Ssam 							goto top; /* n^2 */
25224723Smckusick 						} /* if (n) */
25324723Smckusick 					} /* for (cp = ) */
25424723Smckusick 				} /* else */
25524723Smckusick 		} /* if (ibits & (1<<f)) */
25624723Smckusick 
25724723Smckusick 		if ((obits & (1<<p)) && fcc > 0) {
25824723Smckusick 			wsize = fcc;
25924723Smckusick 			do {
26024723Smckusick 				cc = write(p, fbp, wsize);
26124723Smckusick 				wsize /= 2;
26224723Smckusick 			} while (cc<0 && errno==EWOULDBLOCK && wsize);
26324723Smckusick 			if (cc > 0) {
26424723Smckusick 				fcc -= cc;
26524723Smckusick 				fbp += cc;
2666446Swnj 			}
26718357Ssam 		}
26824723Smckusick 
26918357Ssam 		if (ibits & (1<<p)) {
27018357Ssam 			pcc = read(p, pibuf, sizeof (pibuf));
27118357Ssam 			pbp = pibuf;
27218357Ssam 			if (pcc < 0 && errno == EWOULDBLOCK)
27318357Ssam 				pcc = 0;
27418357Ssam 			else if (pcc <= 0)
27518357Ssam 				break;
27618357Ssam 			else if (pibuf[0] == 0)
27718357Ssam 				pbp++, pcc--;
27818357Ssam 			else {
27918357Ssam #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
28024723Smckusick 				int out = FREAD;
28124723Smckusick 
28218357Ssam 				if (pkcontrol(pibuf[0])) {
28318357Ssam 				/* The following 3 lines do nothing. */
28418357Ssam 					int nstop = pibuf[0] &
28518357Ssam 					    (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP);
28618357Ssam 
28718357Ssam 					if (nstop)
28818357Ssam 						stop = nstop;
28918357Ssam 					pibuf[0] |= nstop;
29018357Ssam 					send(f, &pibuf[0], 1, MSG_OOB);
29124723Smckusick 					if (pibuf[0] & TIOCPKT_FLUSHWRITE)
29224723Smckusick 					  ioctl(p, TIOCFLUSH, (char *)&out);
29324723Smckusick 
29416227Skarels 				}
29518357Ssam 				pcc = 0;
2966446Swnj 			}
29718357Ssam 		}
29818357Ssam 		if ((obits & (1<<f)) && pcc > 0) {
29924723Smckusick 			wsize = pcc;
30024723Smckusick 			do {
30124723Smckusick 				cc = write(f, pbp, wsize);
30224723Smckusick 				wsize /= 2;
30324723Smckusick 			} while (cc<0 && errno==EWOULDBLOCK && wsize);
30418357Ssam 			if (cc > 0) {
30518357Ssam 				pcc -= cc;
30618357Ssam 				pbp += cc;
30718357Ssam 			}
3086446Swnj 		}
3096446Swnj 	}
3106446Swnj }
3116446Swnj 
3126446Swnj cleanup()
3136446Swnj {
3146446Swnj 
3156446Swnj 	rmut();
31610009Ssam 	vhangup();		/* XXX */
31710192Ssam 	shutdown(netf, 2);
3186446Swnj 	kill(0, SIGKILL);
3196446Swnj 	exit(1);
3206446Swnj }
3216446Swnj 
3229242Ssam fatal(f, msg)
3239242Ssam 	int f;
3249242Ssam 	char *msg;
3259242Ssam {
3269242Ssam 	char buf[BUFSIZ];
3279242Ssam 
3289242Ssam 	buf[0] = '\01';		/* error indicator */
32913554Ssam 	(void) sprintf(buf + 1, "rlogind: %s.\r\n", msg);
3309242Ssam 	(void) write(f, buf, strlen(buf));
3319242Ssam 	exit(1);
3329242Ssam }
3339242Ssam 
3349242Ssam fatalperror(f, msg, errno)
3359242Ssam 	int f;
3369242Ssam 	char *msg;
3379242Ssam 	int errno;
3389242Ssam {
3399242Ssam 	char buf[BUFSIZ];
34016227Skarels 	extern int sys_nerr;
3419242Ssam 	extern char *sys_errlist[];
3429242Ssam 
34318357Ssam 	if ((unsigned)errno < sys_nerr)
34416227Skarels 		(void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
34516227Skarels 	else
34616227Skarels 		(void) sprintf(buf, "%s: Error %d", msg, errno);
3479242Ssam 	fatal(f, buf);
3489242Ssam }
3499242Ssam 
3506446Swnj #include <utmp.h>
3516446Swnj 
3526446Swnj struct	utmp wtmp;
3536446Swnj char	wtmpf[]	= "/usr/adm/wtmp";
35423566Sbloom char	utmpf[] = "/etc/utmp";
3556446Swnj #define SCPYN(a, b)	strncpy(a, b, sizeof(a))
3566446Swnj #define SCMPN(a, b)	strncmp(a, b, sizeof(a))
3576446Swnj 
3586446Swnj rmut()
3596446Swnj {
3606446Swnj 	register f;
3616446Swnj 	int found = 0;
36223566Sbloom 	struct utmp *u, *utmp;
36323566Sbloom 	int nutmp;
36423566Sbloom 	struct stat statbf;
3656446Swnj 
36623566Sbloom 	f = open(utmpf, O_RDWR);
3676446Swnj 	if (f >= 0) {
36823566Sbloom 		fstat(f, &statbf);
36923566Sbloom 		utmp = (struct utmp *)malloc(statbf.st_size);
37023566Sbloom 		if (!utmp)
37123566Sbloom 			syslog(LOG_ERR, "utmp malloc failed");
37223566Sbloom 		if (statbf.st_size && utmp) {
37323566Sbloom 			nutmp = read(f, utmp, statbf.st_size);
37423566Sbloom 			nutmp /= sizeof(struct utmp);
37523566Sbloom 
37623566Sbloom 			for (u = utmp ; u < &utmp[nutmp] ; u++) {
37723566Sbloom 				if (SCMPN(u->ut_line, line+5) ||
37823566Sbloom 				    u->ut_name[0]==0)
37923566Sbloom 					continue;
38023566Sbloom 				lseek(f, ((long)u)-((long)utmp), L_SET);
38123566Sbloom 				SCPYN(u->ut_name, "");
38223566Sbloom 				SCPYN(u->ut_host, "");
38323566Sbloom 				time(&u->ut_time);
38423566Sbloom 				write(f, (char *)u, sizeof(wtmp));
38523566Sbloom 				found++;
38623566Sbloom 			}
3876446Swnj 		}
3886446Swnj 		close(f);
3896446Swnj 	}
3906446Swnj 	if (found) {
39123566Sbloom 		f = open(wtmpf, O_WRONLY|O_APPEND);
3926446Swnj 		if (f >= 0) {
3936446Swnj 			SCPYN(wtmp.ut_line, line+5);
3946446Swnj 			SCPYN(wtmp.ut_name, "");
39512681Ssam 			SCPYN(wtmp.ut_host, "");
3966446Swnj 			time(&wtmp.ut_time);
3976446Swnj 			write(f, (char *)&wtmp, sizeof(wtmp));
3986446Swnj 			close(f);
3996446Swnj 		}
4006446Swnj 	}
4016446Swnj 	chmod(line, 0666);
4026446Swnj 	chown(line, 0, 0);
4036446Swnj 	line[strlen("/dev/")] = 'p';
4046446Swnj 	chmod(line, 0666);
4056446Swnj 	chown(line, 0, 0);
4066446Swnj }
407