xref: /csrg-svn/libexec/rlogind/rlogind.c (revision 10009)
16446Swnj #ifndef lint
2*10009Ssam static char sccsid[] = "@(#)rlogind.c	4.7 82/12/29";
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;
216446Swnj struct	passwd *getpwnam();
228380Ssam char	*crypt(), *rindex(), *index(), *malloc();
236446Swnj int	options = SO_ACCEPTCONN|SO_KEEPALIVE;
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 {
366446Swnj 	union wait status;
376446Swnj 	int f, debug = 0;
386446Swnj 	struct sockaddr_in from;
398380Ssam 	struct servent *sp;
406446Swnj 
418380Ssam 	sp = getservbyname("login", "tcp");
428380Ssam 	if (sp == 0) {
438380Ssam 		fprintf(stderr, "rlogind: tcp/rlogin: unknown service\n");
448380Ssam 		exit(1);
458380Ssam 	}
466446Swnj #ifndef DEBUG
476446Swnj 	if (fork())
486446Swnj 		exit(0);
496446Swnj 	for (f = 0; f < 10; f++)
506446Swnj 		(void) close(f);
516446Swnj 	(void) open("/", 0);
526446Swnj 	(void) dup2(0, 1);
536446Swnj 	(void) dup2(0, 2);
546446Swnj 	{ int tt = open("/dev/tty", 2);
556446Swnj 	  if (tt > 0) {
566446Swnj 		ioctl(tt, TIOCNOTTY, 0);
576446Swnj 		close(tt);
586446Swnj 	  }
596446Swnj 	}
606446Swnj #endif
619961Ssam 	sin.sin_port = sp->s_port;
626446Swnj 	argc--, argv++;
639208Ssam 	if (argc > 0 && !strcmp(argv[0], "-d")) {
648380Ssam 		int port = atoi(argv[0]);
658380Ssam 
668380Ssam 		if (port < 0) {
678380Ssam 			fprintf(stderr, "%s: bad port #\n", argv[0]);
688380Ssam 			exit(1);
698380Ssam 		}
709961Ssam 		sin.sin_port = htons((u_short)port);
718380Ssam 		argv++, argc--;
728380Ssam 	}
739259Ssam 	f = socket(AF_INET, SOCK_STREAM, 0, 0);
749208Ssam 	if (f < 0) {
759208Ssam 		perror("rlogind: socket");
769208Ssam 		exit(1);
779208Ssam 	}
789208Ssam 	if (bind(f, &sin, sizeof (sin), 0) < 0) {
799208Ssam 		perror("rlogind: bind");
809208Ssam 		exit(1);
819208Ssam 	}
829208Ssam 	listen(f, 10);
836446Swnj 	for (;;) {
849208Ssam 		int s, len = sizeof (from);
859208Ssam 
869208Ssam 		s = accept(f, &from, &len, 0);
879208Ssam 		if (s < 0) {
889242Ssam 			perror("rlogind: accept");
896446Swnj 			sleep(1);
906446Swnj 			continue;
916446Swnj 		}
926446Swnj 		if (fork() == 0)
939208Ssam 			doit(s, &from);
949208Ssam 		close(s);
956446Swnj 		while (wait3(status, WNOHANG, 0) > 0)
966446Swnj 			continue;
976446Swnj 	}
986446Swnj }
996446Swnj 
1006446Swnj char	locuser[32], remuser[32];
1016446Swnj char	buf[BUFSIZ];
1026446Swnj int	child;
1036446Swnj int	cleanup();
1046446Swnj int	netf;
1056446Swnj extern	errno;
1066446Swnj char	*line;
1076446Swnj 
1086446Swnj doit(f, fromp)
1096446Swnj 	int f;
1106446Swnj 	struct sockaddr_in *fromp;
1116446Swnj {
1128380Ssam 	char c;
1139242Ssam 	int i, p, cc, t, pid;
1146446Swnj 	int stop = TIOCPKT_DOSTOP;
1158380Ssam 	register struct hostent *hp;
1166446Swnj 
1176446Swnj 	alarm(60);
1186446Swnj 	read(f, &c, 1);
1196446Swnj 	if (c != 0)
1206446Swnj 		exit(1);
1216446Swnj 	alarm(0);
1229961Ssam 	fromp->sin_port = htons((u_short)fromp->sin_port);
1238380Ssam 	hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr),
1248380Ssam 		fromp->sin_family);
1259242Ssam 	if (hp == 0)
1269242Ssam 		fatal(f, "Host name for your address unknown");
1276446Swnj 	if (fromp->sin_family != AF_INET ||
1286446Swnj 	    fromp->sin_port >= IPPORT_RESERVED ||
1299242Ssam 	    hp == 0)
1309242Ssam 		fatal(f, "Permission denied");
1316446Swnj 	write(f, "", 1);
1326446Swnj 	for (c = 'p'; c <= 's'; c++) {
1336446Swnj 		struct stat stb;
1346446Swnj 		line = "/dev/ptyXX";
1356446Swnj 		line[strlen("/dev/pty")] = c;
1366446Swnj 		line[strlen("/dev/ptyp")] = '0';
1376446Swnj 		if (stat(line, &stb) < 0)
1386446Swnj 			break;
1396446Swnj 		for (i = 0; i < 16; i++) {
1406446Swnj 			line[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
1416446Swnj 			p = open(line, 2);
1426446Swnj 			if (p > 0)
1436446Swnj 				goto gotpty;
1446446Swnj 		}
1456446Swnj 	}
1469242Ssam 	fatal(f, "All network ports in use");
1479242Ssam 	/*NOTREACHED*/
1486446Swnj gotpty:
1496446Swnj 	dup2(f, 0);
1506446Swnj 	line[strlen("/dev/")] = 't';
1516446Swnj #ifdef DEBUG
1526446Swnj 	{ int tt = open("/dev/tty", 2);
1536446Swnj 	  if (tt > 0) {
1546446Swnj 		ioctl(tt, TIOCNOTTY, 0);
1556446Swnj 		close(tt);
1566446Swnj 	  }
1576446Swnj 	}
1586446Swnj #endif
1596446Swnj 	t = open(line, 2);
1609242Ssam 	if (t < 0)
1619242Ssam 		fatalperror(f, line, errno);
1626446Swnj 	{ struct sgttyb b;
1636446Swnj 	  gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b);
1646446Swnj 	}
1659242Ssam 	pid = fork();
1669242Ssam 	if (pid < 0)
1679242Ssam 		fatalperror(f, "", errno);
1689242Ssam 	if (pid) {
1696446Swnj 		char pibuf[1024], fibuf[1024], *pbp, *fbp;
1706446Swnj 		int pcc = 0, fcc = 0, on = 1;
1716446Swnj /* FILE *console = fopen("/dev/console", "w");  */
1726446Swnj /* setbuf(console, 0); */
1736446Swnj 
1746446Swnj /* fprintf(console, "f %d p %d\r\n", f, p); */
1756446Swnj 		ioctl(f, FIONBIO, &on);
1766446Swnj 		ioctl(p, FIONBIO, &on);
1776446Swnj 		ioctl(p, TIOCPKT, &on);
1786446Swnj 		signal(SIGTSTP, SIG_IGN);
1796446Swnj 		sigset(SIGCHLD, cleanup);
1806446Swnj 		for (;;) {
1816446Swnj 			int ibits = 0, obits = 0;
1829242Ssam 
1839242Ssam 			if (fcc)
1849242Ssam 				obits |= (1<<p);
1859242Ssam 			else
1869242Ssam 				ibits |= (1<<f);
1876446Swnj 			if (pcc >= 0)
1889242Ssam 				if (pcc)
1899242Ssam 					obits |= (1<<f);
1909242Ssam 				else
1919242Ssam 					ibits |= (1<<p);
1929242Ssam 			if (fcc < 0 && pcc < 0)
1939242Ssam 				break;
1946446Swnj /* fprintf(console, "ibits from %d obits from %d\r\n", ibits, obits); */
1959208Ssam 			select(16, &ibits, &obits, 0, 0, 0);
1966446Swnj /* fprintf(console, "ibits %d obits %d\r\n", ibits, obits); */
1976446Swnj 			if (ibits == 0 && obits == 0) {
1986446Swnj 				sleep(5);
1996446Swnj 				continue;
2006446Swnj 			}
2016446Swnj 			if (ibits & (1<<f)) {
2026446Swnj 				fcc = read(f, fibuf, sizeof (fibuf));
2036446Swnj /* fprintf(console, "%d from f\r\n", fcc); */
2046446Swnj 				if (fcc < 0 && errno == EWOULDBLOCK)
2056446Swnj 					fcc = 0;
2066446Swnj 				else {
2076446Swnj 					if (fcc <= 0)
2086446Swnj 						break;
2096446Swnj 					fbp = fibuf;
2106446Swnj 				}
2116446Swnj 			}
2126446Swnj 			if (ibits & (1<<p)) {
2136446Swnj 				pcc = read(p, pibuf, sizeof (pibuf));
2146446Swnj /* fprintf(console, "%d from p, buf[0] %x, errno %d\r\n", pcc, buf[0], errno); */
2156446Swnj 				pbp = pibuf;
2166446Swnj 				if (pcc < 0 && errno == EWOULDBLOCK)
2176446Swnj 					pcc = 0;
2186446Swnj 				else if (pcc <= 0)
2196446Swnj 					pcc = -1;
2206446Swnj 				else if (pibuf[0] == 0)
2216446Swnj 					pbp++, pcc--;
2226446Swnj 				else {
2236446Swnj 					if (pibuf[0]&(TIOCPKT_FLUSHWRITE|
2246446Swnj 						      TIOCPKT_NOSTOP|
2256446Swnj 						      TIOCPKT_DOSTOP)) {
2266446Swnj 						int nstop = pibuf[0] &
2276446Swnj 						    (TIOCPKT_NOSTOP|
2286446Swnj 						     TIOCPKT_DOSTOP);
2296446Swnj 						if (nstop)
2306446Swnj 							stop = nstop;
2316446Swnj 						pibuf[0] |= nstop;
2329208Ssam 						send(f,&pibuf[0],1,SOF_OOB);
2336446Swnj 					}
2346446Swnj 					pcc = 0;
2356446Swnj 				}
2366446Swnj 			}
2376446Swnj 			if ((obits & (1<<f)) && pcc > 0) {
2386446Swnj 				cc = write(f, pbp, pcc);
2396446Swnj /* fprintf(console, "%d of %d to f\r\n", cc, pcc); */
2406446Swnj 				if (cc > 0) {
2416446Swnj 					pcc -= cc;
2426446Swnj 					pbp += cc;
2436446Swnj 				}
2446446Swnj 			}
2456446Swnj 			if ((obits & (1<<p)) && fcc > 0) {
2466446Swnj 				cc = write(p, fbp, fcc);
2476446Swnj /* fprintf(console, "%d of %d to p\r\n", cc, fcc); */
2486446Swnj 				if (cc > 0) {
2496446Swnj 					fcc -= cc;
2506446Swnj 					fbp += cc;
2516446Swnj 				}
2526446Swnj 			}
2536446Swnj 		}
2546446Swnj 		cleanup();
2556446Swnj 	}
2566446Swnj 	close(f);
2576446Swnj 	close(p);
2586446Swnj 	dup2(t, 0);
2596446Swnj 	dup2(t, 1);
2606446Swnj 	dup2(t, 2);
2616446Swnj 	close(t);
2628380Ssam 	execl("/bin/login", "login", "-r", hp->h_name, 0);
2639242Ssam 	fatalperror(2, "/bin/login", errno);
2649242Ssam 	/*NOTREACHED*/
2656446Swnj }
2666446Swnj 
2676446Swnj cleanup()
2686446Swnj {
2696446Swnj 	int how = 2;
2706446Swnj 
2716446Swnj 	rmut();
272*10009Ssam 	vhangup();		/* XXX */
2736446Swnj 	ioctl(netf, SIOCDONE, &how);
2746446Swnj 	kill(0, SIGKILL);
2756446Swnj 	exit(1);
2766446Swnj }
2776446Swnj 
2789242Ssam fatal(f, msg)
2799242Ssam 	int f;
2809242Ssam 	char *msg;
2819242Ssam {
2829242Ssam 	char buf[BUFSIZ];
2839242Ssam 
2849242Ssam 	buf[0] = '\01';		/* error indicator */
2859242Ssam 	(void) sprintf(buf + 1, "rlogind: %s.\n", msg);
2869242Ssam 	(void) write(f, buf, strlen(buf));
2879242Ssam 	exit(1);
2889242Ssam }
2899242Ssam 
2909242Ssam fatalperror(f, msg, errno)
2919242Ssam 	int f;
2929242Ssam 	char *msg;
2939242Ssam 	int errno;
2949242Ssam {
2959242Ssam 	char buf[BUFSIZ];
2969242Ssam 	extern char *sys_errlist[];
2979242Ssam 
2989242Ssam 	(void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
2999242Ssam 	fatal(f, buf);
3009242Ssam }
3019242Ssam 
3026446Swnj #include <utmp.h>
3036446Swnj 
3046446Swnj struct	utmp wtmp;
3056446Swnj char	wtmpf[]	= "/usr/adm/wtmp";
3066446Swnj char	utmp[] = "/etc/utmp";
3076446Swnj #define SCPYN(a, b)	strncpy(a, b, sizeof(a))
3086446Swnj #define SCMPN(a, b)	strncmp(a, b, sizeof(a))
3096446Swnj 
3106446Swnj rmut()
3116446Swnj {
3126446Swnj 	register f;
3136446Swnj 	int found = 0;
3146446Swnj 
3156446Swnj 	f = open(utmp, 2);
3166446Swnj 	if (f >= 0) {
3176446Swnj 		while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) {
3186446Swnj 			if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0)
3196446Swnj 				continue;
3206446Swnj 			lseek(f, -(long)sizeof(wtmp), 1);
3216446Swnj 			SCPYN(wtmp.ut_name, "");
3226446Swnj 			time(&wtmp.ut_time);
3236446Swnj 			write(f, (char *)&wtmp, sizeof(wtmp));
3246446Swnj 			found++;
3256446Swnj 		}
3266446Swnj 		close(f);
3276446Swnj 	}
3286446Swnj 	if (found) {
3296446Swnj 		f = open(wtmpf, 1);
3306446Swnj 		if (f >= 0) {
3316446Swnj 			SCPYN(wtmp.ut_line, line+5);
3326446Swnj 			SCPYN(wtmp.ut_name, "");
3336446Swnj 			time(&wtmp.ut_time);
3346446Swnj 			lseek(f, (long)0, 2);
3356446Swnj 			write(f, (char *)&wtmp, sizeof(wtmp));
3366446Swnj 			close(f);
3376446Swnj 		}
3386446Swnj 	}
3396446Swnj 	chmod(line, 0666);
3406446Swnj 	chown(line, 0, 0);
3416446Swnj 	line[strlen("/dev/")] = 'p';
3426446Swnj 	chmod(line, 0666);
3436446Swnj 	chown(line, 0, 0);
3446446Swnj }
345