xref: /csrg-svn/libexec/rlogind/rlogind.c (revision 13554)
16446Swnj #ifndef lint
2*13554Ssam static char sccsid[] = "@(#)rlogind.c	4.18 83/07/01";
36446Swnj #endif
46446Swnj 
56446Swnj #include <stdio.h>
66446Swnj #include <sys/types.h>
76446Swnj #include <sys/stat.h>
86446Swnj #include <sys/socket.h>
9*13554Ssam #include <sys/wait.h>
109208Ssam 
119208Ssam #include <netinet/in.h>
129208Ssam 
136446Swnj #include <errno.h>
146446Swnj #include <pwd.h>
156446Swnj #include <signal.h>
166446Swnj #include <sgtty.h>
176446Swnj #include <stdio.h>
188380Ssam #include <netdb.h>
196446Swnj 
206446Swnj extern	errno;
2110417Ssam int	reapchild();
226446Swnj struct	passwd *getpwnam();
2311345Ssam char	*crypt(), *rindex(), *index(), *malloc(), *ntoa();
248380Ssam struct	sockaddr_in sin = { AF_INET };
256446Swnj /*
266446Swnj  * remote login server:
276446Swnj  *	remuser\0
286446Swnj  *	locuser\0
296446Swnj  *	terminal type\0
306446Swnj  *	data
316446Swnj  */
326446Swnj main(argc, argv)
336446Swnj 	int argc;
346446Swnj 	char **argv;
356446Swnj {
3612216Ssam 	int f, options = 0;
376446Swnj 	struct sockaddr_in from;
388380Ssam 	struct servent *sp;
396446Swnj 
408380Ssam 	sp = getservbyname("login", "tcp");
418380Ssam 	if (sp == 0) {
428380Ssam 		fprintf(stderr, "rlogind: tcp/rlogin: unknown service\n");
438380Ssam 		exit(1);
448380Ssam 	}
456446Swnj #ifndef DEBUG
466446Swnj 	if (fork())
476446Swnj 		exit(0);
486446Swnj 	for (f = 0; f < 10; f++)
496446Swnj 		(void) close(f);
506446Swnj 	(void) open("/", 0);
516446Swnj 	(void) dup2(0, 1);
526446Swnj 	(void) dup2(0, 2);
536446Swnj 	{ int tt = open("/dev/tty", 2);
546446Swnj 	  if (tt > 0) {
556446Swnj 		ioctl(tt, TIOCNOTTY, 0);
566446Swnj 		close(tt);
576446Swnj 	  }
586446Swnj 	}
596446Swnj #endif
609961Ssam 	sin.sin_port = sp->s_port;
616446Swnj 	argc--, argv++;
629208Ssam 	if (argc > 0 && !strcmp(argv[0], "-d")) {
6310417Ssam 		options |= SO_DEBUG;
6410417Ssam 		argc--, argv++;
6510417Ssam 	}
6610417Ssam 	if (argc > 0) {
678380Ssam 		int port = atoi(argv[0]);
688380Ssam 
698380Ssam 		if (port < 0) {
708380Ssam 			fprintf(stderr, "%s: bad port #\n", argv[0]);
718380Ssam 			exit(1);
728380Ssam 		}
739961Ssam 		sin.sin_port = htons((u_short)port);
748380Ssam 		argv++, argc--;
758380Ssam 	}
769259Ssam 	f = socket(AF_INET, SOCK_STREAM, 0, 0);
779208Ssam 	if (f < 0) {
789208Ssam 		perror("rlogind: socket");
799208Ssam 		exit(1);
809208Ssam 	}
8110417Ssam 	if (options & SO_DEBUG)
8210417Ssam 		if (setsockopt(f, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
8310417Ssam 			perror("rlogind: setsockopt (SO_DEBUG)");
8410417Ssam 	if (setsockopt(f, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0)
8512216Ssam 		perror("rlogind: setsockopt (SO_KEEPALIVE)");
869208Ssam 	if (bind(f, &sin, sizeof (sin), 0) < 0) {
879208Ssam 		perror("rlogind: bind");
889208Ssam 		exit(1);
899208Ssam 	}
9013024Ssam 	signal(SIGCHLD, reapchild);
919208Ssam 	listen(f, 10);
926446Swnj 	for (;;) {
939208Ssam 		int s, len = sizeof (from);
949208Ssam 
959208Ssam 		s = accept(f, &from, &len, 0);
969208Ssam 		if (s < 0) {
9710417Ssam 			if (errno == EINTR)
9810417Ssam 				continue;
999242Ssam 			perror("rlogind: accept");
1006446Swnj 			continue;
1016446Swnj 		}
10211222Ssam 		if (fork() == 0) {
10311222Ssam 			signal(SIGCHLD, SIG_IGN);
10413267Ssam 			close(f);
1059208Ssam 			doit(s, &from);
10611222Ssam 		}
1079208Ssam 		close(s);
1086446Swnj 	}
1096446Swnj }
1106446Swnj 
11110417Ssam reapchild()
11210417Ssam {
11310417Ssam 	union wait status;
11410417Ssam 
11510417Ssam 	while (wait3(&status, WNOHANG, 0) > 0)
11610417Ssam 		;
11710417Ssam }
11810417Ssam 
1196446Swnj char	locuser[32], remuser[32];
1206446Swnj char	buf[BUFSIZ];
1216446Swnj int	child;
1226446Swnj int	cleanup();
1236446Swnj int	netf;
1246446Swnj extern	errno;
1256446Swnj char	*line;
1266446Swnj 
1276446Swnj doit(f, fromp)
1286446Swnj 	int f;
1296446Swnj 	struct sockaddr_in *fromp;
1306446Swnj {
1318380Ssam 	char c;
1329242Ssam 	int i, p, cc, t, pid;
1336446Swnj 	int stop = TIOCPKT_DOSTOP;
1348380Ssam 	register struct hostent *hp;
1356446Swnj 
1366446Swnj 	alarm(60);
1376446Swnj 	read(f, &c, 1);
1386446Swnj 	if (c != 0)
1396446Swnj 		exit(1);
1406446Swnj 	alarm(0);
1419961Ssam 	fromp->sin_port = htons((u_short)fromp->sin_port);
1428380Ssam 	hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr),
1438380Ssam 		fromp->sin_family);
14411345Ssam 	if (hp == 0) {
14511345Ssam 		char buf[BUFSIZ], *cp = (char *)&fromp->sin_addr;
14611345Ssam 
14711345Ssam 		fatal(f, sprintf(buf, "Host name for your address (%s) unknown",
14811345Ssam 			ntoa(fromp->sin_addr)));
14911345Ssam 	}
1506446Swnj 	if (fromp->sin_family != AF_INET ||
1516446Swnj 	    fromp->sin_port >= IPPORT_RESERVED ||
1529242Ssam 	    hp == 0)
1539242Ssam 		fatal(f, "Permission denied");
1546446Swnj 	write(f, "", 1);
1556446Swnj 	for (c = 'p'; c <= 's'; c++) {
1566446Swnj 		struct stat stb;
1576446Swnj 		line = "/dev/ptyXX";
1586446Swnj 		line[strlen("/dev/pty")] = c;
1596446Swnj 		line[strlen("/dev/ptyp")] = '0';
1606446Swnj 		if (stat(line, &stb) < 0)
1616446Swnj 			break;
1626446Swnj 		for (i = 0; i < 16; i++) {
1636446Swnj 			line[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
1646446Swnj 			p = open(line, 2);
1656446Swnj 			if (p > 0)
1666446Swnj 				goto gotpty;
1676446Swnj 		}
1686446Swnj 	}
1699242Ssam 	fatal(f, "All network ports in use");
1709242Ssam 	/*NOTREACHED*/
1716446Swnj gotpty:
1726446Swnj 	dup2(f, 0);
1736446Swnj 	line[strlen("/dev/")] = 't';
1746446Swnj #ifdef DEBUG
1756446Swnj 	{ int tt = open("/dev/tty", 2);
1766446Swnj 	  if (tt > 0) {
1776446Swnj 		ioctl(tt, TIOCNOTTY, 0);
1786446Swnj 		close(tt);
1796446Swnj 	  }
1806446Swnj 	}
1816446Swnj #endif
1826446Swnj 	t = open(line, 2);
1839242Ssam 	if (t < 0)
1849242Ssam 		fatalperror(f, line, errno);
1856446Swnj 	{ struct sgttyb b;
1866446Swnj 	  gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b);
1876446Swnj 	}
1889242Ssam 	pid = fork();
1899242Ssam 	if (pid < 0)
1909242Ssam 		fatalperror(f, "", errno);
1919242Ssam 	if (pid) {
1926446Swnj 		char pibuf[1024], fibuf[1024], *pbp, *fbp;
1936446Swnj 		int pcc = 0, fcc = 0, on = 1;
1946446Swnj /* FILE *console = fopen("/dev/console", "w");  */
1956446Swnj /* setbuf(console, 0); */
1966446Swnj 
1976446Swnj /* fprintf(console, "f %d p %d\r\n", f, p); */
1986446Swnj 		ioctl(f, FIONBIO, &on);
1996446Swnj 		ioctl(p, FIONBIO, &on);
2006446Swnj 		ioctl(p, TIOCPKT, &on);
2016446Swnj 		signal(SIGTSTP, SIG_IGN);
20213024Ssam 		signal(SIGCHLD, cleanup);
2036446Swnj 		for (;;) {
2046446Swnj 			int ibits = 0, obits = 0;
2059242Ssam 
2069242Ssam 			if (fcc)
2079242Ssam 				obits |= (1<<p);
2089242Ssam 			else
2099242Ssam 				ibits |= (1<<f);
2106446Swnj 			if (pcc >= 0)
2119242Ssam 				if (pcc)
2129242Ssam 					obits |= (1<<f);
2139242Ssam 				else
2149242Ssam 					ibits |= (1<<p);
2159242Ssam 			if (fcc < 0 && pcc < 0)
2169242Ssam 				break;
2176446Swnj /* fprintf(console, "ibits from %d obits from %d\r\n", ibits, obits); */
2189208Ssam 			select(16, &ibits, &obits, 0, 0, 0);
2196446Swnj /* fprintf(console, "ibits %d obits %d\r\n", ibits, obits); */
2206446Swnj 			if (ibits == 0 && obits == 0) {
2216446Swnj 				sleep(5);
2226446Swnj 				continue;
2236446Swnj 			}
2246446Swnj 			if (ibits & (1<<f)) {
2256446Swnj 				fcc = read(f, fibuf, sizeof (fibuf));
2266446Swnj /* fprintf(console, "%d from f\r\n", fcc); */
2276446Swnj 				if (fcc < 0 && errno == EWOULDBLOCK)
2286446Swnj 					fcc = 0;
2296446Swnj 				else {
2306446Swnj 					if (fcc <= 0)
2316446Swnj 						break;
2326446Swnj 					fbp = fibuf;
2336446Swnj 				}
2346446Swnj 			}
2356446Swnj 			if (ibits & (1<<p)) {
2366446Swnj 				pcc = read(p, pibuf, sizeof (pibuf));
2376446Swnj /* fprintf(console, "%d from p, buf[0] %x, errno %d\r\n", pcc, buf[0], errno); */
2386446Swnj 				pbp = pibuf;
2396446Swnj 				if (pcc < 0 && errno == EWOULDBLOCK)
2406446Swnj 					pcc = 0;
2416446Swnj 				else if (pcc <= 0)
2426446Swnj 					pcc = -1;
2436446Swnj 				else if (pibuf[0] == 0)
2446446Swnj 					pbp++, pcc--;
2456446Swnj 				else {
2466446Swnj 					if (pibuf[0]&(TIOCPKT_FLUSHWRITE|
2476446Swnj 						      TIOCPKT_NOSTOP|
2486446Swnj 						      TIOCPKT_DOSTOP)) {
2496446Swnj 						int nstop = pibuf[0] &
2506446Swnj 						    (TIOCPKT_NOSTOP|
2516446Swnj 						     TIOCPKT_DOSTOP);
2526446Swnj 						if (nstop)
2536446Swnj 							stop = nstop;
2546446Swnj 						pibuf[0] |= nstop;
25512898Ssam 						send(f,&pibuf[0],1,MSG_OOB);
2566446Swnj 					}
2576446Swnj 					pcc = 0;
2586446Swnj 				}
2596446Swnj 			}
2606446Swnj 			if ((obits & (1<<f)) && pcc > 0) {
2616446Swnj 				cc = write(f, pbp, pcc);
2626446Swnj /* fprintf(console, "%d of %d to f\r\n", cc, pcc); */
2636446Swnj 				if (cc > 0) {
2646446Swnj 					pcc -= cc;
2656446Swnj 					pbp += cc;
2666446Swnj 				}
2676446Swnj 			}
2686446Swnj 			if ((obits & (1<<p)) && fcc > 0) {
2696446Swnj 				cc = write(p, fbp, fcc);
2706446Swnj /* fprintf(console, "%d of %d to p\r\n", cc, fcc); */
2716446Swnj 				if (cc > 0) {
2726446Swnj 					fcc -= cc;
2736446Swnj 					fbp += cc;
2746446Swnj 				}
2756446Swnj 			}
2766446Swnj 		}
2776446Swnj 		cleanup();
2786446Swnj 	}
2796446Swnj 	close(f);
2806446Swnj 	close(p);
2816446Swnj 	dup2(t, 0);
2826446Swnj 	dup2(t, 1);
2836446Swnj 	dup2(t, 2);
2846446Swnj 	close(t);
2858380Ssam 	execl("/bin/login", "login", "-r", hp->h_name, 0);
2869242Ssam 	fatalperror(2, "/bin/login", errno);
2879242Ssam 	/*NOTREACHED*/
2886446Swnj }
2896446Swnj 
2906446Swnj cleanup()
2916446Swnj {
2926446Swnj 
2936446Swnj 	rmut();
29410009Ssam 	vhangup();		/* XXX */
29510192Ssam 	shutdown(netf, 2);
2966446Swnj 	kill(0, SIGKILL);
2976446Swnj 	exit(1);
2986446Swnj }
2996446Swnj 
3009242Ssam fatal(f, msg)
3019242Ssam 	int f;
3029242Ssam 	char *msg;
3039242Ssam {
3049242Ssam 	char buf[BUFSIZ];
3059242Ssam 
3069242Ssam 	buf[0] = '\01';		/* error indicator */
307*13554Ssam 	(void) sprintf(buf + 1, "rlogind: %s.\r\n", msg);
3089242Ssam 	(void) write(f, buf, strlen(buf));
3099242Ssam 	exit(1);
3109242Ssam }
3119242Ssam 
3129242Ssam fatalperror(f, msg, errno)
3139242Ssam 	int f;
3149242Ssam 	char *msg;
3159242Ssam 	int errno;
3169242Ssam {
3179242Ssam 	char buf[BUFSIZ];
3189242Ssam 	extern char *sys_errlist[];
3199242Ssam 
3209242Ssam 	(void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
3219242Ssam 	fatal(f, buf);
3229242Ssam }
3239242Ssam 
3246446Swnj #include <utmp.h>
3256446Swnj 
3266446Swnj struct	utmp wtmp;
3276446Swnj char	wtmpf[]	= "/usr/adm/wtmp";
3286446Swnj char	utmp[] = "/etc/utmp";
3296446Swnj #define SCPYN(a, b)	strncpy(a, b, sizeof(a))
3306446Swnj #define SCMPN(a, b)	strncmp(a, b, sizeof(a))
3316446Swnj 
3326446Swnj rmut()
3336446Swnj {
3346446Swnj 	register f;
3356446Swnj 	int found = 0;
3366446Swnj 
3376446Swnj 	f = open(utmp, 2);
3386446Swnj 	if (f >= 0) {
3396446Swnj 		while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) {
3406446Swnj 			if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0)
3416446Swnj 				continue;
3426446Swnj 			lseek(f, -(long)sizeof(wtmp), 1);
3436446Swnj 			SCPYN(wtmp.ut_name, "");
34412681Ssam 			SCPYN(wtmp.ut_host, "");
3456446Swnj 			time(&wtmp.ut_time);
3466446Swnj 			write(f, (char *)&wtmp, sizeof(wtmp));
3476446Swnj 			found++;
3486446Swnj 		}
3496446Swnj 		close(f);
3506446Swnj 	}
3516446Swnj 	if (found) {
3526446Swnj 		f = open(wtmpf, 1);
3536446Swnj 		if (f >= 0) {
3546446Swnj 			SCPYN(wtmp.ut_line, line+5);
3556446Swnj 			SCPYN(wtmp.ut_name, "");
35612681Ssam 			SCPYN(wtmp.ut_host, "");
3576446Swnj 			time(&wtmp.ut_time);
3586446Swnj 			lseek(f, (long)0, 2);
3596446Swnj 			write(f, (char *)&wtmp, sizeof(wtmp));
3606446Swnj 			close(f);
3616446Swnj 		}
3626446Swnj 	}
3636446Swnj 	chmod(line, 0666);
3646446Swnj 	chown(line, 0, 0);
3656446Swnj 	line[strlen("/dev/")] = 'p';
3666446Swnj 	chmod(line, 0666);
3676446Swnj 	chown(line, 0, 0);
3686446Swnj }
36911345Ssam 
37011345Ssam /*
37111345Ssam  * Convert network-format internet address
37211345Ssam  * to base 256 d.d.d.d representation.
37311345Ssam  */
37411345Ssam char *
37511345Ssam ntoa(in)
37611345Ssam 	struct in_addr in;
37711345Ssam {
37811345Ssam 	static char b[18];
37911345Ssam 	register char *p;
38011345Ssam 
38111345Ssam 	p = (char *)&in;
38211345Ssam #define	UC(b)	(((int)b)&0xff)
38311345Ssam 	sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
38411345Ssam 	return (b);
38511345Ssam }
386