xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 12807)
16460Swnj #ifndef lint
2*12807Ssam static char sccsid[] = "@(#)rwhod.c	4.11 (Berkeley) 83/05/28";
36460Swnj #endif
46460Swnj 
59215Ssam #include <sys/types.h>
69215Ssam #include <sys/socket.h>
79215Ssam #include <sys/stat.h>
89215Ssam #include <sys/ioctl.h>
99215Ssam 
1012236Ssam #include <net/if.h>
119215Ssam #include <netinet/in.h>
129215Ssam 
139215Ssam #include <nlist.h>
146460Swnj #include <stdio.h>
156460Swnj #include <signal.h>
166460Swnj #include <errno.h>
176460Swnj #include <utmp.h>
188486Ssam #include <ctype.h>
198486Ssam #include <netdb.h>
2012236Ssam #include <rwhod.h>
216460Swnj 
228486Ssam struct	sockaddr_in sin = { AF_INET };
236460Swnj 
246460Swnj extern	errno;
256460Swnj 
2612236Ssam char	myname[32];
276460Swnj 
286460Swnj struct	nlist nl[] = {
296460Swnj #define	NL_AVENRUN	0
306460Swnj 	{ "_avenrun" },
319215Ssam #define	NL_BOOTTIME	1
329215Ssam 	{ "_boottime" },
336460Swnj 	0
346460Swnj };
356460Swnj 
3612236Ssam /*
3712236Ssam  * We communicate with each neighbor in
3812236Ssam  * a list constructed at the time we're
3912236Ssam  * started up.  Neighbors are currently
4012236Ssam  * directly connected via a hardware interface.
4112236Ssam  */
4212236Ssam struct	neighbor {
4312236Ssam 	struct	neighbor *n_next;
4412236Ssam 	char	*n_name;		/* interface name */
4512236Ssam 	char	*n_addr;		/* who to send to */
4612236Ssam 	int	n_addrlen;		/* size of address */
4712236Ssam 	int	n_flags;		/* should forward?, interface flags */
4812236Ssam };
4912236Ssam 
5012236Ssam struct	neighbor *neighbors;
516460Swnj struct	whod mywd;
5212236Ssam struct	servent *sp;
536460Swnj int	s, utmpf, kmemf = -1;
546460Swnj 
5512236Ssam #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
5612236Ssam #define	RWHODIR		"/usr/spool/rwho"
5711834Ssam 
586460Swnj int	onalrm();
5912236Ssam char	*strcpy(), *sprintf(), *malloc();
606460Swnj long	lseek();
616460Swnj int	getkmem();
6212236Ssam struct	in_addr inet_makeaddr();
636460Swnj 
646460Swnj main()
656460Swnj {
666460Swnj 	struct sockaddr_in from;
676460Swnj 	char path[64];
686460Swnj 	int addr;
6912236Ssam 	struct hostent *hp;
706460Swnj 
718486Ssam 	sp = getservbyname("who", "udp");
728486Ssam 	if (sp == 0) {
738486Ssam 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
748486Ssam 		exit(1);
758486Ssam 	}
766460Swnj #ifndef DEBUG
776460Swnj 	if (fork())
786460Swnj 		exit(0);
796460Swnj 	{ int s;
806460Swnj 	  for (s = 0; s < 10; s++)
816460Swnj 		(void) close(s);
826460Swnj 	  (void) open("/", 0);
836460Swnj 	  (void) dup2(0, 1);
846460Swnj 	  (void) dup2(0, 2);
856460Swnj 	  s = open("/dev/tty", 2);
866460Swnj 	  if (s >= 0) {
876460Swnj 		ioctl(s, TIOCNOTTY, 0);
886460Swnj 		(void) close(s);
896460Swnj 	  }
906460Swnj 	}
916460Swnj #endif
926460Swnj 	(void) chdir("/dev");
936460Swnj 	(void) signal(SIGHUP, getkmem);
946460Swnj 	if (getuid()) {
958486Ssam 		fprintf(stderr, "rwhod: not super user\n");
966460Swnj 		exit(1);
976460Swnj 	}
9812236Ssam 	/*
9912236Ssam 	 * Establish host name as returned by system.
10012236Ssam 	 */
10112236Ssam 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
10212236Ssam 		perror("gethostname");
1036460Swnj 		exit(1);
1046460Swnj 	}
10512236Ssam 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
1066460Swnj 	utmpf = open("/etc/utmp", 0);
1076460Swnj 	if (utmpf < 0) {
1086460Swnj 		(void) close(creat("/etc/utmp", 0644));
1096460Swnj 		utmpf = open("/etc/utmp", 0);
1106460Swnj 	}
1116460Swnj 	if (utmpf < 0) {
1128486Ssam 		perror("rwhod: /etc/utmp");
1136460Swnj 		exit(1);
1146460Swnj 	}
1156460Swnj 	getkmem();
1169261Ssam 	if ((s = socket(AF_INET, SOCK_DGRAM, 0, 0)) < 0) {
1178486Ssam 		perror("rwhod: socket");
1189215Ssam 		exit(1);
1196460Swnj 	}
12012236Ssam 	hp = gethostbyname(myname);
12112236Ssam 	if (hp == NULL) {
12212236Ssam 		fprintf(stderr, "%s: don't know my own name\n", myname);
12312236Ssam 		exit(1);
12412236Ssam 	}
12512236Ssam 	sin.sin_family = hp->h_addrtype;
12612236Ssam 	sin.sin_port = sp->s_port;
1279215Ssam 	if (bind(s, &sin, sizeof (sin), 0) < 0) {
1289215Ssam 		perror("rwhod: bind");
1299215Ssam 		exit(1);
1309215Ssam 	}
13112236Ssam 	if (!configure(s))
13212236Ssam 		exit(1);
1336460Swnj 	sigset(SIGALRM, onalrm);
1346460Swnj 	onalrm();
1356460Swnj 	for (;;) {
1366460Swnj 		struct whod wd;
13712236Ssam 		int cc, whod, len = sizeof (from);
1386460Swnj 
13912236Ssam 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
14012236Ssam 			&from, &len);
1416460Swnj 		if (cc <= 0) {
1426460Swnj 			if (cc < 0 && errno != EINTR)
1439215Ssam 				perror("rwhod: recv");
1446460Swnj 			continue;
1456460Swnj 		}
1468486Ssam 		if (from.sin_port != sp->s_port) {
1478486Ssam 			fprintf(stderr, "rwhod: %d: bad from port\n",
1488486Ssam 				ntohs(from.sin_port));
1496460Swnj 			continue;
1506460Swnj 		}
1518486Ssam #ifdef notdef
1528486Ssam 		if (gethostbyname(wd.wd_hostname) == 0) {
1538486Ssam 			fprintf(stderr, "rwhod: %s: unknown host\n",
1548486Ssam 				wd.wd_hostname);
1556460Swnj 			continue;
1566460Swnj 		}
1578486Ssam #endif
158*12807Ssam #ifdef notyet
15912347Ssam 		if (wd.wd_vers != WHODVERSION)
16012347Ssam 			continue;
16112236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
16212236Ssam 			continue;
163*12807Ssam #endif
1648486Ssam 		if (!verify(wd.wd_hostname)) {
1658486Ssam 			fprintf(stderr, "rwhod: malformed host name from %x\n",
1668486Ssam 				from.sin_addr);
1678486Ssam 			continue;
1688486Ssam 		}
1699914Ssam 		(void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
1706460Swnj 		whod = creat(path, 0666);
1716460Swnj 		if (whod < 0) {
1728486Ssam 			fprintf(stderr, "rwhod: ");
1738486Ssam 			perror(path);
1746460Swnj 			continue;
1756460Swnj 		}
176*12807Ssam #ifdef notyet
17711834Ssam #if vax || pdp11
17811834Ssam 		{
17911834Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct utmp);
18011834Ssam 			struct whoent *we;
18111834Ssam 
18211834Ssam 			/* undo header byte swapping before writing to file */
18311834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
18411834Ssam 			for (i = 0; i < 3; i++)
18511834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
18611834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
18711834Ssam 			we = wd.wd_we;
18811834Ssam 			for (i = 0; i < n; i++) {
18911834Ssam 				we->we_idle = ntohl(we->we_idle);
19011834Ssam 				we->we_utmp.ut_time = ntohl(we->we_utmp.ut_time);
19111834Ssam 				we++;
19211834Ssam 			}
19311834Ssam 		}
19411834Ssam #endif
195*12807Ssam #endif
1966460Swnj 		(void) time(&wd.wd_recvtime);
1976460Swnj 		(void) write(whod, (char *)&wd, cc);
1986460Swnj 		(void) close(whod);
1996460Swnj 	}
2006460Swnj }
2016460Swnj 
2028486Ssam /*
2038486Ssam  * Check out host name for unprintables
2048486Ssam  * and other funnies before allowing a file
2058486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2068486Ssam  */
2078486Ssam verify(name)
2088486Ssam 	register char *name;
2098486Ssam {
2108486Ssam 	register int size = 0;
2118486Ssam 
2128486Ssam 	while (*name) {
2138486Ssam 		if (!isascii(*name) || !isalnum(*name))
2148486Ssam 			return (0);
2158486Ssam 		name++, size++;
2168486Ssam 	}
2178486Ssam 	return (size > 0);
2188486Ssam }
2198486Ssam 
2206460Swnj int	utmptime;
2216460Swnj int	utmpent;
2226577Ssam struct	utmp utmp[100];
2236460Swnj int	alarmcount;
2246460Swnj 
2256460Swnj onalrm()
2266460Swnj {
2276460Swnj 	register int i;
2286460Swnj 	struct stat stb;
2296577Ssam 	register struct whoent *we = mywd.wd_we, *wlast;
2306460Swnj 	int cc;
2316460Swnj 	double avenrun[3];
2326460Swnj 	time_t now = time(0);
23312236Ssam 	register struct neighbor *np;
2346460Swnj 
2356460Swnj 	if (alarmcount % 10 == 0)
2366460Swnj 		getkmem();
2376460Swnj 	alarmcount++;
2386460Swnj 	(void) fstat(utmpf, &stb);
2396460Swnj 	if (stb.st_mtime != utmptime) {
2406460Swnj 		(void) lseek(utmpf, (long)0, 0);
2416460Swnj 		cc = read(utmpf, (char *)utmp, sizeof (utmp));
2426460Swnj 		if (cc < 0) {
2436460Swnj 			perror("/etc/utmp");
2446460Swnj 			return;
2456460Swnj 		}
24612236Ssam 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
2476460Swnj 		utmpent = cc / sizeof (struct utmp);
2486460Swnj 		for (i = 0; i < utmpent; i++)
2496460Swnj 			if (utmp[i].ut_name[0]) {
250*12807Ssam 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
251*12807Ssam 				   sizeof (utmp[i].ut_line));
252*12807Ssam 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
253*12807Ssam 				   sizeof (utmp[i].ut_name));
254*12807Ssam #ifdef notyet
255*12807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
256*12807Ssam #else
257*12807Ssam 				we->we_utmp.out_time = utmp[i].ut_time;
258*12807Ssam #endif
2596577Ssam 				if (we >= wlast)
2606577Ssam 					break;
2616460Swnj 				we++;
2626460Swnj 			}
2636460Swnj 		utmpent = we - mywd.wd_we;
2646460Swnj 	}
2656460Swnj 	we = mywd.wd_we;
2666460Swnj 	for (i = 0; i < utmpent; i++) {
267*12807Ssam 		if (stat(we->we_utmp.out_line, &stb) >= 0)
268*12807Ssam #ifdef notyet
269*12807Ssam 			we->we_idle = htonl(now - stb.st_atime);
270*12807Ssam #else
2716460Swnj 			we->we_idle = now - stb.st_atime;
272*12807Ssam #endif
2736460Swnj 		we++;
2746460Swnj 	}
2756460Swnj 	(void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, 0);
2766460Swnj 	(void) read(kmemf, (char *)avenrun, sizeof (avenrun));
2776460Swnj 	for (i = 0; i < 3; i++)
278*12807Ssam #ifdef notyet
279*12807Ssam 		mywd.wd_loadav[i] = htonl(avenrun[i] * 100);
280*12807Ssam #else
2816460Swnj 		mywd.wd_loadav[i] = avenrun[i] * 100;
282*12807Ssam #endif
2836460Swnj 	cc = (char *)we - (char *)&mywd;
284*12807Ssam #ifdef notyet
285*12807Ssam 	mywd.wd_sendtime = htonl(time(0));
28612236Ssam 	mywd.wd_vers = WHODVERSION;
28712236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
288*12807Ssam #else
289*12807Ssam 	mywd.wd_sendtime = time(0);
290*12807Ssam #endif
29112236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
29212236Ssam 		(void) sendto(s, (char *)&mywd, cc, 0,
29312236Ssam 			np->n_addr, np->n_addrlen);
2946460Swnj 	(void) alarm(60);
2956460Swnj }
2966460Swnj 
2976460Swnj getkmem()
2986460Swnj {
2996460Swnj 	struct nlist *nlp;
3006460Swnj 
3016460Swnj 	signal(SIGHUP, getkmem);
3026460Swnj 	if (kmemf >= 0)
3036460Swnj 		(void) close(kmemf);
3046460Swnj loop:
3056460Swnj 	for (nlp = &nl[sizeof (nl) / sizeof (nl[0])]; --nlp >= nl; ) {
3066460Swnj 		nlp->n_value = 0;
3076460Swnj 		nlp->n_type = 0;
3086460Swnj 	}
3096460Swnj 	nlist("/vmunix", nl);
3106460Swnj 	if (nl[0].n_value == 0) {
3116460Swnj 		fprintf(stderr, "/vmunix namelist botch\n");
3126460Swnj 		sleep(300);
3136460Swnj 		goto loop;
3146460Swnj 	}
3156460Swnj 	kmemf = open("/dev/kmem", 0);
3166460Swnj 	if (kmemf < 0) {
3176460Swnj 		perror("/dev/kmem");
3186460Swnj 		sleep(300);
3196460Swnj 		goto loop;
3206460Swnj 	}
3219215Ssam 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, 0);
3229215Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime, sizeof (mywd.wd_boottime));
323*12807Ssam #ifdef notyet
324*12807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
325*12807Ssam #endif
3266460Swnj }
32712236Ssam 
32812236Ssam /*
32912236Ssam  * Figure out device configuration and select
33012236Ssam  * networks which deserve status information.
33112236Ssam  */
33212236Ssam configure(s)
33312236Ssam 	int s;
33412236Ssam {
33512236Ssam 	char buf[BUFSIZ];
33612236Ssam 	struct ifconf ifc;
33712236Ssam 	struct ifreq ifreq, *ifr;
33812236Ssam 	int n;
33912236Ssam 	struct sockaddr_in *sin;
34012236Ssam 	register struct neighbor *np;
34112236Ssam 
34212236Ssam 	ifc.ifc_len = sizeof (buf);
34312236Ssam 	ifc.ifc_buf = buf;
34412236Ssam 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
34512236Ssam 		perror("rwhod: ioctl (get interface configuration)");
34612236Ssam 		return (0);
34712236Ssam 	}
34812236Ssam 	ifr = ifc.ifc_req;
34912236Ssam 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
35012236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
35112236Ssam 			if (np->n_name &&
35212236Ssam 			    strcmp(ifr->ifr_name, np->n_name) == 0)
35312236Ssam 				break;
35412236Ssam 		if (np != NULL)
35512236Ssam 			continue;
35612236Ssam 		ifreq = *ifr;
35712236Ssam 		np = (struct neighbor *)malloc(sizeof (*np));
35812236Ssam 		if (np == NULL)
35912236Ssam 			continue;
36012236Ssam 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
36112236Ssam 		if (np->n_name == NULL) {
36212236Ssam 			free((char *)np);
36312236Ssam 			continue;
36412236Ssam 		}
36512236Ssam 		strcpy(np->n_name, ifr->ifr_name);
36612236Ssam 		np->n_addrlen = sizeof (ifr->ifr_addr);
36712236Ssam 		np->n_addr = malloc(np->n_addrlen);
36812236Ssam 		if (np->n_addr == NULL) {
36912236Ssam 			free(np->n_name);
37012236Ssam 			free((char *)np);
37112236Ssam 			continue;
37212236Ssam 		}
37312236Ssam 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
37412236Ssam 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
37512236Ssam 			perror("rwhod: ioctl (get interface flags)");
37612236Ssam 			free((char *)np);
37712236Ssam 			continue;
37812236Ssam 		}
37912236Ssam 		if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
38012236Ssam 			free((char *)np);
38112236Ssam 			continue;
38212236Ssam 		}
38312236Ssam 		np->n_flags = ifreq.ifr_flags;
38412236Ssam 		if (np->n_flags & IFF_POINTOPOINT) {
38512236Ssam 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
38612236Ssam 				perror("rwhod: ioctl (get dstaddr)");
38712236Ssam 				free((char *)np);
38812236Ssam 				continue;
38912236Ssam 			}
39012236Ssam 			/* we assume addresses are all the same size */
39112236Ssam 			bcopy((char *)&ifreq.ifr_dstaddr,
39212236Ssam 			  np->n_addr, np->n_addrlen);
39312236Ssam 		}
39412236Ssam 		if (np->n_flags & IFF_BROADCAST) {
39512236Ssam 			/* we assume addresses are all the same size */
39612236Ssam 			sin = (struct sockaddr_in *)np->n_addr;
39712236Ssam 			sin->sin_addr =
39812236Ssam 			  inet_makeaddr(inet_netof(sin->sin_addr), INADDR_ANY);
39912236Ssam 		}
40012236Ssam 		/* gag, wish we could get rid of Internet dependencies */
40112236Ssam 		sin = (struct sockaddr_in *)np->n_addr;
40212236Ssam 		sin->sin_port = sp->s_port;
40312236Ssam 		np->n_next = neighbors;
40412236Ssam 		neighbors = np;
40512236Ssam 	}
40612236Ssam 	return (1);
40712236Ssam }
408*12807Ssam 
409*12807Ssam #ifdef DEBUG
410*12807Ssam sendto(s, buf, cc, flags, to, tolen)
411*12807Ssam 	int s;
412*12807Ssam 	char *buf;
413*12807Ssam 	int cc, flags;
414*12807Ssam 	char *to;
415*12807Ssam 	int tolen;
416*12807Ssam {
417*12807Ssam 	register struct whod *w = (struct whod *)buf;
418*12807Ssam 	register struct whoent *we;
419*12807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
420*12807Ssam 	char *interval();
421*12807Ssam 
422*12807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
423*12807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
424*12807Ssam 	   interval(w->wd_sendtime - w->wd_boottime, "  up"));
425*12807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
426*12807Ssam 	    w->wd_loadav[0] / 100.0, w->wd_loadav[1] / 100.0,
427*12807Ssam 	    w->wd_loadav[2] / 100.0);
428*12807Ssam 	cc -= WHDRSIZE;
429*12807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
430*12807Ssam 		printf("%-8.8s %s:%s %.12s",
431*12807Ssam 			 we->we_utmp.out_name,
432*12807Ssam 			 w->wd_hostname, we->we_utmp.out_line,
433*12807Ssam 			ctime((time_t *)&we->we_utmp.out_time)+4);
434*12807Ssam 		we->we_idle /= 60;
435*12807Ssam 		if (we->we_idle) {
436*12807Ssam 			if (we->we_idle >= 100*60)
437*12807Ssam 				we->we_idle = 100*60 - 1;
438*12807Ssam 			if (we->we_idle >= 60)
439*12807Ssam 				printf(" %2d", we->we_idle / 60);
440*12807Ssam 			else
441*12807Ssam 				printf("   ");
442*12807Ssam 			printf(":%02d", we->we_idle % 60);
443*12807Ssam 		}
444*12807Ssam 		printf("\n");
445*12807Ssam 	}
446*12807Ssam }
447*12807Ssam 
448*12807Ssam char *
449*12807Ssam interval(time, updown)
450*12807Ssam 	int time;
451*12807Ssam 	char *updown;
452*12807Ssam {
453*12807Ssam 	static char resbuf[32];
454*12807Ssam 	int days, hours, minutes;
455*12807Ssam 
456*12807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
457*12807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
458*12807Ssam 		return (resbuf);
459*12807Ssam 	}
460*12807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
461*12807Ssam 	hours = minutes / 60; minutes %= 60;
462*12807Ssam 	days = hours / 24; hours %= 24;
463*12807Ssam 	if (days)
464*12807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
465*12807Ssam 		    updown, days, hours, minutes);
466*12807Ssam 	else
467*12807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
468*12807Ssam 		    updown, hours, minutes);
469*12807Ssam 	return (resbuf);
470*12807Ssam }
471*12807Ssam #endif
472