xref: /csrg-svn/libexec/rlogind/rlogind.c (revision 28705)
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*28705Smckusick static char sccsid[] = "@(#)rlogind.c	5.11 (Berkeley) 05/23/86";
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 
5924855Seric 	openlog("rlogind", LOG_PID | LOG_AUTH, LOG_AUTH);
6016369Skarels 	fromlen = sizeof (from);
6116369Skarels 	if (getpeername(0, &from, &fromlen) < 0) {
6216369Skarels 		fprintf(stderr, "%s: ", argv[0]);
6316369Skarels 		perror("getpeername");
6416369Skarels 		_exit(1);
658380Ssam 	}
6617156Ssam 	if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 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;
7724724Smckusick extern	char	*inet_ntoa();
786446Swnj 
7924889Smckusick struct winsize win = { 0, 0, 0, 0 };
8024723Smckusick 
8124889Smckusick 
826446Swnj doit(f, fromp)
836446Swnj 	int f;
846446Swnj 	struct sockaddr_in *fromp;
856446Swnj {
8618357Ssam 	int i, p, t, pid, on = 1;
8718357Ssam 	register struct hostent *hp;
8824724Smckusick 	struct hostent hostent;
898380Ssam 	char c;
906446Swnj 
916446Swnj 	alarm(60);
926446Swnj 	read(f, &c, 1);
936446Swnj 	if (c != 0)
946446Swnj 		exit(1);
956446Swnj 	alarm(0);
9616227Skarels 	fromp->sin_port = ntohs((u_short)fromp->sin_port);
978380Ssam 	hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr),
988380Ssam 		fromp->sin_family);
9911345Ssam 	if (hp == 0) {
10024724Smckusick 		/*
10124724Smckusick 		 * Only the name is used below.
10224724Smckusick 		 */
10324724Smckusick 		hp = &hostent;
10424724Smckusick 		hp->h_name = inet_ntoa(fromp->sin_addr);
10511345Ssam 	}
1066446Swnj 	if (fromp->sin_family != AF_INET ||
10716227Skarels 	    fromp->sin_port >= IPPORT_RESERVED)
1089242Ssam 		fatal(f, "Permission denied");
1096446Swnj 	write(f, "", 1);
1106446Swnj 	for (c = 'p'; c <= 's'; c++) {
1116446Swnj 		struct stat stb;
1126446Swnj 		line = "/dev/ptyXX";
1136446Swnj 		line[strlen("/dev/pty")] = c;
1146446Swnj 		line[strlen("/dev/ptyp")] = '0';
1156446Swnj 		if (stat(line, &stb) < 0)
1166446Swnj 			break;
1176446Swnj 		for (i = 0; i < 16; i++) {
1186446Swnj 			line[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
1196446Swnj 			p = open(line, 2);
1206446Swnj 			if (p > 0)
1216446Swnj 				goto gotpty;
1226446Swnj 		}
1236446Swnj 	}
12424723Smckusick 	fatal(f, "Out of ptys");
1259242Ssam 	/*NOTREACHED*/
1266446Swnj gotpty:
12724889Smckusick 	(void) ioctl(p, TIOCSWINSZ, &win);
12816227Skarels 	netf = f;
1296446Swnj 	line[strlen("/dev/")] = 't';
1306446Swnj #ifdef DEBUG
1316446Swnj 	{ int tt = open("/dev/tty", 2);
1326446Swnj 	  if (tt > 0) {
1336446Swnj 		ioctl(tt, TIOCNOTTY, 0);
1346446Swnj 		close(tt);
1356446Swnj 	  }
1366446Swnj 	}
1376446Swnj #endif
1386446Swnj 	t = open(line, 2);
1399242Ssam 	if (t < 0)
1409242Ssam 		fatalperror(f, line, errno);
1416446Swnj 	{ struct sgttyb b;
1426446Swnj 	  gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b);
1436446Swnj 	}
1449242Ssam 	pid = fork();
1459242Ssam 	if (pid < 0)
1469242Ssam 		fatalperror(f, "", errno);
14718357Ssam 	if (pid == 0) {
14818357Ssam 		close(f), close(p);
14918357Ssam 		dup2(t, 0), dup2(t, 1), dup2(t, 2);
15016227Skarels 		close(t);
15118357Ssam 		execl("/bin/login", "login", "-r", hp->h_name, 0);
15218357Ssam 		fatalperror(2, "/bin/login", errno);
15318357Ssam 		/*NOTREACHED*/
15418357Ssam 	}
15518357Ssam 	close(t);
15618357Ssam 	ioctl(f, FIONBIO, &on);
15718357Ssam 	ioctl(p, FIONBIO, &on);
15818357Ssam 	ioctl(p, TIOCPKT, &on);
15918357Ssam 	signal(SIGTSTP, SIG_IGN);
16018357Ssam 	signal(SIGCHLD, cleanup);
16124724Smckusick 	setpgrp(0, 0);
16218357Ssam 	protocol(f, p);
16318357Ssam 	cleanup();
16418357Ssam }
1659242Ssam 
16618357Ssam char	magic[2] = { 0377, 0377 };
16725423Skarels char	oobdata[] = {TIOCPKT_WINDOW};
16818357Ssam 
16918357Ssam /*
17018357Ssam  * Handle a "control" request (signaled by magic being present)
17118357Ssam  * in the data stream.  For now, we are only willing to handle
17218357Ssam  * window size changes.
17318357Ssam  */
17418357Ssam control(pty, cp, n)
17518357Ssam 	int pty;
17618357Ssam 	char *cp;
17718357Ssam 	int n;
17818357Ssam {
179*28705Smckusick 	struct winsize w;
18018357Ssam 
181*28705Smckusick 	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
18218357Ssam 		return (0);
18325423Skarels 	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
184*28705Smckusick 	bcopy(cp+4, (char *)&w, sizeof(w));
185*28705Smckusick 	w.ws_row = ntohs(w.ws_row);
186*28705Smckusick 	w.ws_col = ntohs(w.ws_col);
187*28705Smckusick 	w.ws_xpixel = ntohs(w.ws_xpixel);
188*28705Smckusick 	w.ws_ypixel = ntohs(w.ws_ypixel);
189*28705Smckusick 	(void)ioctl(pty, TIOCSWINSZ, &w);
190*28705Smckusick 	return (4+sizeof (w));
19118357Ssam }
19218357Ssam 
19318357Ssam /*
19418357Ssam  * rlogin "protocol" machine.
19518357Ssam  */
19618357Ssam protocol(f, p)
19718357Ssam 	int f, p;
19818357Ssam {
19918357Ssam 	char pibuf[1024], fibuf[1024], *pbp, *fbp;
20018357Ssam 	register pcc = 0, fcc = 0;
20125423Skarels 	int cc;
20225740Skarels 	char cntl;
20318357Ssam 
20418482Ssam 	/*
20518484Ssam 	 * Must ignore SIGTTOU, otherwise we'll stop
20618484Ssam 	 * when we try and set slave pty's window shape
20725423Skarels 	 * (our controlling tty is the master pty).
20818482Ssam 	 */
20918484Ssam 	(void) signal(SIGTTOU, SIG_IGN);
21025423Skarels 	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
21118357Ssam 	for (;;) {
21225740Skarels 		int ibits, obits, ebits;
21318357Ssam 
21425740Skarels 		ibits = 0;
21525740Skarels 		obits = 0;
21618357Ssam 		if (fcc)
21718357Ssam 			obits |= (1<<p);
21818357Ssam 		else
21918357Ssam 			ibits |= (1<<f);
22018357Ssam 		if (pcc >= 0)
22118357Ssam 			if (pcc)
22218357Ssam 				obits |= (1<<f);
2239242Ssam 			else
22418357Ssam 				ibits |= (1<<p);
22525740Skarels 		ebits = (1<<p);
22625740Skarels 		if (select(16, &ibits, &obits, &ebits, 0) < 0) {
22718357Ssam 			if (errno == EINTR)
2286446Swnj 				continue;
22918357Ssam 			fatalperror(f, "select", errno);
23018357Ssam 		}
23125740Skarels 		if (ibits == 0 && obits == 0 && ebits == 0) {
23218357Ssam 			/* shouldn't happen... */
23318357Ssam 			sleep(5);
23418357Ssam 			continue;
23518357Ssam 		}
23625740Skarels #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
23725740Skarels 		if (ebits & (1<<p)) {
23825740Skarels 			cc = read(p, &cntl, 1);
23925740Skarels 			if (cc == 1 && pkcontrol(cntl)) {
24025740Skarels 				cntl |= oobdata[0];
24125740Skarels 				send(f, &cntl, 1, MSG_OOB);
24225740Skarels 				if (cntl & TIOCPKT_FLUSHWRITE) {
24325740Skarels 					pcc = 0;
24425740Skarels 					ibits &= ~(1<<p);
24525740Skarels 				}
24625740Skarels 			}
24725740Skarels 		}
24818357Ssam 		if (ibits & (1<<f)) {
24918357Ssam 			fcc = read(f, fibuf, sizeof (fibuf));
25018357Ssam 			if (fcc < 0 && errno == EWOULDBLOCK)
25118357Ssam 				fcc = 0;
25218357Ssam 			else {
25318357Ssam 				register char *cp;
25418357Ssam 				int left, n;
25518357Ssam 
25618357Ssam 				if (fcc <= 0)
25716227Skarels 					break;
25818357Ssam 				fbp = fibuf;
25924723Smckusick 
26018357Ssam 			top:
26125423Skarels 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
26218357Ssam 					if (cp[0] == magic[0] &&
26318357Ssam 					    cp[1] == magic[1]) {
26418357Ssam 						left = fcc - (cp-fibuf);
26518357Ssam 						n = control(p, cp, left);
26618357Ssam 						if (n) {
26718357Ssam 							left -= n;
26818357Ssam 							if (left > 0)
26925423Skarels 								bcopy(cp+n, cp, left);
27018357Ssam 							fcc -= n;
27118357Ssam 							goto top; /* n^2 */
27225423Skarels 						}
27325423Skarels 					}
27425423Skarels 			}
27525423Skarels 		}
27624723Smckusick 
27724723Smckusick 		if ((obits & (1<<p)) && fcc > 0) {
27825423Skarels 			cc = write(p, fbp, fcc);
27924723Smckusick 			if (cc > 0) {
28024723Smckusick 				fcc -= cc;
28124723Smckusick 				fbp += cc;
2826446Swnj 			}
28318357Ssam 		}
28424723Smckusick 
28518357Ssam 		if (ibits & (1<<p)) {
28618357Ssam 			pcc = read(p, pibuf, sizeof (pibuf));
28718357Ssam 			pbp = pibuf;
28818357Ssam 			if (pcc < 0 && errno == EWOULDBLOCK)
28918357Ssam 				pcc = 0;
29018357Ssam 			else if (pcc <= 0)
29118357Ssam 				break;
29218357Ssam 			else if (pibuf[0] == 0)
29318357Ssam 				pbp++, pcc--;
29418357Ssam 			else {
29518357Ssam 				if (pkcontrol(pibuf[0])) {
29625423Skarels 					pibuf[0] |= oobdata[0];
29718357Ssam 					send(f, &pibuf[0], 1, MSG_OOB);
29816227Skarels 				}
29918357Ssam 				pcc = 0;
3006446Swnj 			}
30118357Ssam 		}
30218357Ssam 		if ((obits & (1<<f)) && pcc > 0) {
30325423Skarels 			cc = write(f, pbp, pcc);
30425423Skarels 			if (cc < 0 && errno == EWOULDBLOCK) {
30525423Skarels 				/* also shouldn't happen */
30625423Skarels 				sleep(5);
30725423Skarels 				continue;
30825423Skarels 			}
30918357Ssam 			if (cc > 0) {
31018357Ssam 				pcc -= cc;
31118357Ssam 				pbp += cc;
31218357Ssam 			}
3136446Swnj 		}
3146446Swnj 	}
3156446Swnj }
3166446Swnj 
3176446Swnj cleanup()
3186446Swnj {
3196446Swnj 
3206446Swnj 	rmut();
32110009Ssam 	vhangup();		/* XXX */
32210192Ssam 	shutdown(netf, 2);
3236446Swnj 	exit(1);
3246446Swnj }
3256446Swnj 
3269242Ssam fatal(f, msg)
3279242Ssam 	int f;
3289242Ssam 	char *msg;
3299242Ssam {
3309242Ssam 	char buf[BUFSIZ];
3319242Ssam 
3329242Ssam 	buf[0] = '\01';		/* error indicator */
33313554Ssam 	(void) sprintf(buf + 1, "rlogind: %s.\r\n", msg);
3349242Ssam 	(void) write(f, buf, strlen(buf));
3359242Ssam 	exit(1);
3369242Ssam }
3379242Ssam 
3389242Ssam fatalperror(f, msg, errno)
3399242Ssam 	int f;
3409242Ssam 	char *msg;
3419242Ssam 	int errno;
3429242Ssam {
3439242Ssam 	char buf[BUFSIZ];
34416227Skarels 	extern int sys_nerr;
3459242Ssam 	extern char *sys_errlist[];
3469242Ssam 
34718357Ssam 	if ((unsigned)errno < sys_nerr)
34816227Skarels 		(void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
34916227Skarels 	else
35016227Skarels 		(void) sprintf(buf, "%s: Error %d", msg, errno);
3519242Ssam 	fatal(f, buf);
3529242Ssam }
3539242Ssam 
3546446Swnj #include <utmp.h>
3556446Swnj 
3566446Swnj struct	utmp wtmp;
3576446Swnj char	wtmpf[]	= "/usr/adm/wtmp";
35823566Sbloom char	utmpf[] = "/etc/utmp";
3596446Swnj #define SCPYN(a, b)	strncpy(a, b, sizeof(a))
3606446Swnj #define SCMPN(a, b)	strncmp(a, b, sizeof(a))
3616446Swnj 
3626446Swnj rmut()
3636446Swnj {
3646446Swnj 	register f;
3656446Swnj 	int found = 0;
36623566Sbloom 	struct utmp *u, *utmp;
36723566Sbloom 	int nutmp;
36823566Sbloom 	struct stat statbf;
3696446Swnj 
37023566Sbloom 	f = open(utmpf, O_RDWR);
3716446Swnj 	if (f >= 0) {
37223566Sbloom 		fstat(f, &statbf);
37323566Sbloom 		utmp = (struct utmp *)malloc(statbf.st_size);
37423566Sbloom 		if (!utmp)
37523566Sbloom 			syslog(LOG_ERR, "utmp malloc failed");
37623566Sbloom 		if (statbf.st_size && utmp) {
37723566Sbloom 			nutmp = read(f, utmp, statbf.st_size);
37823566Sbloom 			nutmp /= sizeof(struct utmp);
37923566Sbloom 
38023566Sbloom 			for (u = utmp ; u < &utmp[nutmp] ; u++) {
38123566Sbloom 				if (SCMPN(u->ut_line, line+5) ||
38223566Sbloom 				    u->ut_name[0]==0)
38323566Sbloom 					continue;
38423566Sbloom 				lseek(f, ((long)u)-((long)utmp), L_SET);
38523566Sbloom 				SCPYN(u->ut_name, "");
38623566Sbloom 				SCPYN(u->ut_host, "");
38723566Sbloom 				time(&u->ut_time);
38823566Sbloom 				write(f, (char *)u, sizeof(wtmp));
38923566Sbloom 				found++;
39023566Sbloom 			}
3916446Swnj 		}
3926446Swnj 		close(f);
3936446Swnj 	}
3946446Swnj 	if (found) {
39523566Sbloom 		f = open(wtmpf, O_WRONLY|O_APPEND);
3966446Swnj 		if (f >= 0) {
3976446Swnj 			SCPYN(wtmp.ut_line, line+5);
3986446Swnj 			SCPYN(wtmp.ut_name, "");
39912681Ssam 			SCPYN(wtmp.ut_host, "");
4006446Swnj 			time(&wtmp.ut_time);
4016446Swnj 			write(f, (char *)&wtmp, sizeof(wtmp));
4026446Swnj 			close(f);
4036446Swnj 		}
4046446Swnj 	}
4056446Swnj 	chmod(line, 0666);
4066446Swnj 	chown(line, 0, 0);
4076446Swnj 	line[strlen("/dev/")] = 'p';
4086446Swnj 	chmod(line, 0666);
4096446Swnj 	chown(line, 0, 0);
4106446Swnj }
411