xref: /csrg-svn/libexec/rlogind/rlogind.c (revision 11345)
16446Swnj #ifndef lint
2*11345Ssam static char sccsid[] = "@(#)rlogind.c	4.12 83/02/28";
36446Swnj #endif
46446Swnj 
56446Swnj #include <stdio.h>
66446Swnj #include <sys/types.h>
76446Swnj #include <sys/stat.h>
86446Swnj #include <sys/socket.h>
99208Ssam 
109208Ssam #include <netinet/in.h>
119208Ssam 
126446Swnj #include <errno.h>
136446Swnj #include <pwd.h>
146446Swnj #include <wait.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();
23*11345Ssam 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 {
3610417Ssam 	int f, options = SO_KEEPALIVE;
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 #ifdef notdef
8510417Ssam 	if (setsockopt(f, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0)
8610417Ssam 		perror("rlogind: setsocktopt (SO_KEEPALIVE)");
8710417Ssam #endif
889208Ssam 	if (bind(f, &sin, sizeof (sin), 0) < 0) {
899208Ssam 		perror("rlogind: bind");
909208Ssam 		exit(1);
919208Ssam 	}
9210585Ssam 	sigset(SIGCHLD, reapchild);
939208Ssam 	listen(f, 10);
946446Swnj 	for (;;) {
959208Ssam 		int s, len = sizeof (from);
969208Ssam 
979208Ssam 		s = accept(f, &from, &len, 0);
989208Ssam 		if (s < 0) {
9910417Ssam 			if (errno == EINTR)
10010417Ssam 				continue;
1019242Ssam 			perror("rlogind: accept");
1026446Swnj 			continue;
1036446Swnj 		}
10411222Ssam 		if (fork() == 0) {
10511222Ssam 			signal(SIGCHLD, SIG_IGN);
1069208Ssam 			doit(s, &from);
10711222Ssam 		}
1089208Ssam 		close(s);
1096446Swnj 	}
1106446Swnj }
1116446Swnj 
11210417Ssam reapchild()
11310417Ssam {
11410417Ssam 	union wait status;
11510417Ssam 
11610417Ssam 	while (wait3(&status, WNOHANG, 0) > 0)
11710417Ssam 		;
11810417Ssam }
11910417Ssam 
1206446Swnj char	locuser[32], remuser[32];
1216446Swnj char	buf[BUFSIZ];
1226446Swnj int	child;
1236446Swnj int	cleanup();
1246446Swnj int	netf;
1256446Swnj extern	errno;
1266446Swnj char	*line;
1276446Swnj 
1286446Swnj doit(f, fromp)
1296446Swnj 	int f;
1306446Swnj 	struct sockaddr_in *fromp;
1316446Swnj {
1328380Ssam 	char c;
1339242Ssam 	int i, p, cc, t, pid;
1346446Swnj 	int stop = TIOCPKT_DOSTOP;
1358380Ssam 	register struct hostent *hp;
1366446Swnj 
1376446Swnj 	alarm(60);
1386446Swnj 	read(f, &c, 1);
1396446Swnj 	if (c != 0)
1406446Swnj 		exit(1);
1416446Swnj 	alarm(0);
1429961Ssam 	fromp->sin_port = htons((u_short)fromp->sin_port);
1438380Ssam 	hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr),
1448380Ssam 		fromp->sin_family);
145*11345Ssam 	if (hp == 0) {
146*11345Ssam 		char buf[BUFSIZ], *cp = (char *)&fromp->sin_addr;
147*11345Ssam 
148*11345Ssam 		fatal(f, sprintf(buf, "Host name for your address (%s) unknown",
149*11345Ssam 			ntoa(fromp->sin_addr)));
150*11345Ssam 	}
1516446Swnj 	if (fromp->sin_family != AF_INET ||
1526446Swnj 	    fromp->sin_port >= IPPORT_RESERVED ||
1539242Ssam 	    hp == 0)
1549242Ssam 		fatal(f, "Permission denied");
1556446Swnj 	write(f, "", 1);
1566446Swnj 	for (c = 'p'; c <= 's'; c++) {
1576446Swnj 		struct stat stb;
1586446Swnj 		line = "/dev/ptyXX";
1596446Swnj 		line[strlen("/dev/pty")] = c;
1606446Swnj 		line[strlen("/dev/ptyp")] = '0';
1616446Swnj 		if (stat(line, &stb) < 0)
1626446Swnj 			break;
1636446Swnj 		for (i = 0; i < 16; i++) {
1646446Swnj 			line[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
1656446Swnj 			p = open(line, 2);
1666446Swnj 			if (p > 0)
1676446Swnj 				goto gotpty;
1686446Swnj 		}
1696446Swnj 	}
1709242Ssam 	fatal(f, "All network ports in use");
1719242Ssam 	/*NOTREACHED*/
1726446Swnj gotpty:
1736446Swnj 	dup2(f, 0);
1746446Swnj 	line[strlen("/dev/")] = 't';
1756446Swnj #ifdef DEBUG
1766446Swnj 	{ int tt = open("/dev/tty", 2);
1776446Swnj 	  if (tt > 0) {
1786446Swnj 		ioctl(tt, TIOCNOTTY, 0);
1796446Swnj 		close(tt);
1806446Swnj 	  }
1816446Swnj 	}
1826446Swnj #endif
1836446Swnj 	t = open(line, 2);
1849242Ssam 	if (t < 0)
1859242Ssam 		fatalperror(f, line, errno);
1866446Swnj 	{ struct sgttyb b;
1876446Swnj 	  gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b);
1886446Swnj 	}
1899242Ssam 	pid = fork();
1909242Ssam 	if (pid < 0)
1919242Ssam 		fatalperror(f, "", errno);
1929242Ssam 	if (pid) {
1936446Swnj 		char pibuf[1024], fibuf[1024], *pbp, *fbp;
1946446Swnj 		int pcc = 0, fcc = 0, on = 1;
1956446Swnj /* FILE *console = fopen("/dev/console", "w");  */
1966446Swnj /* setbuf(console, 0); */
1976446Swnj 
1986446Swnj /* fprintf(console, "f %d p %d\r\n", f, p); */
1996446Swnj 		ioctl(f, FIONBIO, &on);
2006446Swnj 		ioctl(p, FIONBIO, &on);
2016446Swnj 		ioctl(p, TIOCPKT, &on);
2026446Swnj 		signal(SIGTSTP, SIG_IGN);
2036446Swnj 		sigset(SIGCHLD, cleanup);
2046446Swnj 		for (;;) {
2056446Swnj 			int ibits = 0, obits = 0;
2069242Ssam 
2079242Ssam 			if (fcc)
2089242Ssam 				obits |= (1<<p);
2099242Ssam 			else
2109242Ssam 				ibits |= (1<<f);
2116446Swnj 			if (pcc >= 0)
2129242Ssam 				if (pcc)
2139242Ssam 					obits |= (1<<f);
2149242Ssam 				else
2159242Ssam 					ibits |= (1<<p);
2169242Ssam 			if (fcc < 0 && pcc < 0)
2179242Ssam 				break;
2186446Swnj /* fprintf(console, "ibits from %d obits from %d\r\n", ibits, obits); */
2199208Ssam 			select(16, &ibits, &obits, 0, 0, 0);
2206446Swnj /* fprintf(console, "ibits %d obits %d\r\n", ibits, obits); */
2216446Swnj 			if (ibits == 0 && obits == 0) {
2226446Swnj 				sleep(5);
2236446Swnj 				continue;
2246446Swnj 			}
2256446Swnj 			if (ibits & (1<<f)) {
2266446Swnj 				fcc = read(f, fibuf, sizeof (fibuf));
2276446Swnj /* fprintf(console, "%d from f\r\n", fcc); */
2286446Swnj 				if (fcc < 0 && errno == EWOULDBLOCK)
2296446Swnj 					fcc = 0;
2306446Swnj 				else {
2316446Swnj 					if (fcc <= 0)
2326446Swnj 						break;
2336446Swnj 					fbp = fibuf;
2346446Swnj 				}
2356446Swnj 			}
2366446Swnj 			if (ibits & (1<<p)) {
2376446Swnj 				pcc = read(p, pibuf, sizeof (pibuf));
2386446Swnj /* fprintf(console, "%d from p, buf[0] %x, errno %d\r\n", pcc, buf[0], errno); */
2396446Swnj 				pbp = pibuf;
2406446Swnj 				if (pcc < 0 && errno == EWOULDBLOCK)
2416446Swnj 					pcc = 0;
2426446Swnj 				else if (pcc <= 0)
2436446Swnj 					pcc = -1;
2446446Swnj 				else if (pibuf[0] == 0)
2456446Swnj 					pbp++, pcc--;
2466446Swnj 				else {
2476446Swnj 					if (pibuf[0]&(TIOCPKT_FLUSHWRITE|
2486446Swnj 						      TIOCPKT_NOSTOP|
2496446Swnj 						      TIOCPKT_DOSTOP)) {
2506446Swnj 						int nstop = pibuf[0] &
2516446Swnj 						    (TIOCPKT_NOSTOP|
2526446Swnj 						     TIOCPKT_DOSTOP);
2536446Swnj 						if (nstop)
2546446Swnj 							stop = nstop;
2556446Swnj 						pibuf[0] |= nstop;
2569208Ssam 						send(f,&pibuf[0],1,SOF_OOB);
2576446Swnj 					}
2586446Swnj 					pcc = 0;
2596446Swnj 				}
2606446Swnj 			}
2616446Swnj 			if ((obits & (1<<f)) && pcc > 0) {
2626446Swnj 				cc = write(f, pbp, pcc);
2636446Swnj /* fprintf(console, "%d of %d to f\r\n", cc, pcc); */
2646446Swnj 				if (cc > 0) {
2656446Swnj 					pcc -= cc;
2666446Swnj 					pbp += cc;
2676446Swnj 				}
2686446Swnj 			}
2696446Swnj 			if ((obits & (1<<p)) && fcc > 0) {
2706446Swnj 				cc = write(p, fbp, fcc);
2716446Swnj /* fprintf(console, "%d of %d to p\r\n", cc, fcc); */
2726446Swnj 				if (cc > 0) {
2736446Swnj 					fcc -= cc;
2746446Swnj 					fbp += cc;
2756446Swnj 				}
2766446Swnj 			}
2776446Swnj 		}
2786446Swnj 		cleanup();
2796446Swnj 	}
2806446Swnj 	close(f);
2816446Swnj 	close(p);
2826446Swnj 	dup2(t, 0);
2836446Swnj 	dup2(t, 1);
2846446Swnj 	dup2(t, 2);
2856446Swnj 	close(t);
2868380Ssam 	execl("/bin/login", "login", "-r", hp->h_name, 0);
2879242Ssam 	fatalperror(2, "/bin/login", errno);
2889242Ssam 	/*NOTREACHED*/
2896446Swnj }
2906446Swnj 
2916446Swnj cleanup()
2926446Swnj {
2936446Swnj 
2946446Swnj 	rmut();
29510009Ssam 	vhangup();		/* XXX */
29610192Ssam 	shutdown(netf, 2);
2976446Swnj 	kill(0, SIGKILL);
2986446Swnj 	exit(1);
2996446Swnj }
3006446Swnj 
3019242Ssam fatal(f, msg)
3029242Ssam 	int f;
3039242Ssam 	char *msg;
3049242Ssam {
3059242Ssam 	char buf[BUFSIZ];
3069242Ssam 
3079242Ssam 	buf[0] = '\01';		/* error indicator */
3089242Ssam 	(void) sprintf(buf + 1, "rlogind: %s.\n", msg);
3099242Ssam 	(void) write(f, buf, strlen(buf));
3109242Ssam 	exit(1);
3119242Ssam }
3129242Ssam 
3139242Ssam fatalperror(f, msg, errno)
3149242Ssam 	int f;
3159242Ssam 	char *msg;
3169242Ssam 	int errno;
3179242Ssam {
3189242Ssam 	char buf[BUFSIZ];
3199242Ssam 	extern char *sys_errlist[];
3209242Ssam 
3219242Ssam 	(void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
3229242Ssam 	fatal(f, buf);
3239242Ssam }
3249242Ssam 
3256446Swnj #include <utmp.h>
3266446Swnj 
3276446Swnj struct	utmp wtmp;
3286446Swnj char	wtmpf[]	= "/usr/adm/wtmp";
3296446Swnj char	utmp[] = "/etc/utmp";
3306446Swnj #define SCPYN(a, b)	strncpy(a, b, sizeof(a))
3316446Swnj #define SCMPN(a, b)	strncmp(a, b, sizeof(a))
3326446Swnj 
3336446Swnj rmut()
3346446Swnj {
3356446Swnj 	register f;
3366446Swnj 	int found = 0;
3376446Swnj 
3386446Swnj 	f = open(utmp, 2);
3396446Swnj 	if (f >= 0) {
3406446Swnj 		while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) {
3416446Swnj 			if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0)
3426446Swnj 				continue;
3436446Swnj 			lseek(f, -(long)sizeof(wtmp), 1);
3446446Swnj 			SCPYN(wtmp.ut_name, "");
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, "");
3566446Swnj 			time(&wtmp.ut_time);
3576446Swnj 			lseek(f, (long)0, 2);
3586446Swnj 			write(f, (char *)&wtmp, sizeof(wtmp));
3596446Swnj 			close(f);
3606446Swnj 		}
3616446Swnj 	}
3626446Swnj 	chmod(line, 0666);
3636446Swnj 	chown(line, 0, 0);
3646446Swnj 	line[strlen("/dev/")] = 'p';
3656446Swnj 	chmod(line, 0666);
3666446Swnj 	chown(line, 0, 0);
3676446Swnj }
368*11345Ssam 
369*11345Ssam /*
370*11345Ssam  * Convert network-format internet address
371*11345Ssam  * to base 256 d.d.d.d representation.
372*11345Ssam  */
373*11345Ssam char *
374*11345Ssam ntoa(in)
375*11345Ssam 	struct in_addr in;
376*11345Ssam {
377*11345Ssam 	static char b[18];
378*11345Ssam 	register char *p;
379*11345Ssam 
380*11345Ssam 	p = (char *)&in;
381*11345Ssam #define	UC(b)	(((int)b)&0xff)
382*11345Ssam 	sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
383*11345Ssam 	return (b);
384*11345Ssam }
385