xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 13027)
16460Swnj #ifndef lint
2*13027Ssam static char sccsid[] = "@(#)rwhod.c	4.15 (Berkeley) 83/06/12";
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);
133*13027Ssam 	signal(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
15812347Ssam 		if (wd.wd_vers != WHODVERSION)
15912347Ssam 			continue;
16012236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
16112236Ssam 			continue;
1628486Ssam 		if (!verify(wd.wd_hostname)) {
1638486Ssam 			fprintf(stderr, "rwhod: malformed host name from %x\n",
1648486Ssam 				from.sin_addr);
1658486Ssam 			continue;
1668486Ssam 		}
1679914Ssam 		(void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
1686460Swnj 		whod = creat(path, 0666);
1696460Swnj 		if (whod < 0) {
1708486Ssam 			fprintf(stderr, "rwhod: ");
1718486Ssam 			perror(path);
1726460Swnj 			continue;
1736460Swnj 		}
17411834Ssam #if vax || pdp11
17511834Ssam 		{
17611834Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct utmp);
17711834Ssam 			struct whoent *we;
17811834Ssam 
17911834Ssam 			/* undo header byte swapping before writing to file */
18011834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
18111834Ssam 			for (i = 0; i < 3; i++)
18211834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
18311834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
18411834Ssam 			we = wd.wd_we;
18511834Ssam 			for (i = 0; i < n; i++) {
18611834Ssam 				we->we_idle = ntohl(we->we_idle);
18712872Ssam 				we->we_utmp.out_time =
18812872Ssam 				    ntohl(we->we_utmp.out_time);
18911834Ssam 				we++;
19011834Ssam 			}
19111834Ssam 		}
19211834Ssam #endif
1936460Swnj 		(void) time(&wd.wd_recvtime);
1946460Swnj 		(void) write(whod, (char *)&wd, cc);
1956460Swnj 		(void) close(whod);
1966460Swnj 	}
1976460Swnj }
1986460Swnj 
1998486Ssam /*
2008486Ssam  * Check out host name for unprintables
2018486Ssam  * and other funnies before allowing a file
2028486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2038486Ssam  */
2048486Ssam verify(name)
2058486Ssam 	register char *name;
2068486Ssam {
2078486Ssam 	register int size = 0;
2088486Ssam 
2098486Ssam 	while (*name) {
2108486Ssam 		if (!isascii(*name) || !isalnum(*name))
2118486Ssam 			return (0);
2128486Ssam 		name++, size++;
2138486Ssam 	}
2148486Ssam 	return (size > 0);
2158486Ssam }
2168486Ssam 
2176460Swnj int	utmptime;
2186460Swnj int	utmpent;
2196577Ssam struct	utmp utmp[100];
2206460Swnj int	alarmcount;
2216460Swnj 
2226460Swnj onalrm()
2236460Swnj {
2246460Swnj 	register int i;
2256460Swnj 	struct stat stb;
2266577Ssam 	register struct whoent *we = mywd.wd_we, *wlast;
2276460Swnj 	int cc;
2286460Swnj 	double avenrun[3];
2296460Swnj 	time_t now = time(0);
23012236Ssam 	register struct neighbor *np;
2316460Swnj 
2326460Swnj 	if (alarmcount % 10 == 0)
2336460Swnj 		getkmem();
2346460Swnj 	alarmcount++;
2356460Swnj 	(void) fstat(utmpf, &stb);
2366460Swnj 	if (stb.st_mtime != utmptime) {
2376460Swnj 		(void) lseek(utmpf, (long)0, 0);
2386460Swnj 		cc = read(utmpf, (char *)utmp, sizeof (utmp));
2396460Swnj 		if (cc < 0) {
2406460Swnj 			perror("/etc/utmp");
2416460Swnj 			return;
2426460Swnj 		}
24312236Ssam 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
2446460Swnj 		utmpent = cc / sizeof (struct utmp);
2456460Swnj 		for (i = 0; i < utmpent; i++)
2466460Swnj 			if (utmp[i].ut_name[0]) {
24712807Ssam 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
24812807Ssam 				   sizeof (utmp[i].ut_line));
24912807Ssam 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
25012807Ssam 				   sizeof (utmp[i].ut_name));
25112807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
2526577Ssam 				if (we >= wlast)
2536577Ssam 					break;
2546460Swnj 				we++;
2556460Swnj 			}
2566460Swnj 		utmpent = we - mywd.wd_we;
2576460Swnj 	}
2586460Swnj 	we = mywd.wd_we;
2596460Swnj 	for (i = 0; i < utmpent; i++) {
26012807Ssam 		if (stat(we->we_utmp.out_line, &stb) >= 0)
26112807Ssam 			we->we_idle = htonl(now - stb.st_atime);
2626460Swnj 		we++;
2636460Swnj 	}
2646460Swnj 	(void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, 0);
2656460Swnj 	(void) read(kmemf, (char *)avenrun, sizeof (avenrun));
2666460Swnj 	for (i = 0; i < 3; i++)
26712873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
2686460Swnj 	cc = (char *)we - (char *)&mywd;
26912807Ssam 	mywd.wd_sendtime = htonl(time(0));
27012236Ssam 	mywd.wd_vers = WHODVERSION;
27112236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
27212236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
27312236Ssam 		(void) sendto(s, (char *)&mywd, cc, 0,
27412236Ssam 			np->n_addr, np->n_addrlen);
2756460Swnj 	(void) alarm(60);
2766460Swnj }
2776460Swnj 
2786460Swnj getkmem()
2796460Swnj {
2806460Swnj 	struct nlist *nlp;
2816460Swnj 
2826460Swnj 	signal(SIGHUP, getkmem);
2836460Swnj 	if (kmemf >= 0)
2846460Swnj 		(void) close(kmemf);
2856460Swnj loop:
2866460Swnj 	for (nlp = &nl[sizeof (nl) / sizeof (nl[0])]; --nlp >= nl; ) {
2876460Swnj 		nlp->n_value = 0;
2886460Swnj 		nlp->n_type = 0;
2896460Swnj 	}
2906460Swnj 	nlist("/vmunix", nl);
2916460Swnj 	if (nl[0].n_value == 0) {
2926460Swnj 		fprintf(stderr, "/vmunix namelist botch\n");
2936460Swnj 		sleep(300);
2946460Swnj 		goto loop;
2956460Swnj 	}
2966460Swnj 	kmemf = open("/dev/kmem", 0);
2976460Swnj 	if (kmemf < 0) {
2986460Swnj 		perror("/dev/kmem");
2996460Swnj 		sleep(300);
3006460Swnj 		goto loop;
3016460Swnj 	}
3029215Ssam 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, 0);
3039215Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime, sizeof (mywd.wd_boottime));
30412807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
3056460Swnj }
30612236Ssam 
30712236Ssam /*
30812236Ssam  * Figure out device configuration and select
30912236Ssam  * networks which deserve status information.
31012236Ssam  */
31112236Ssam configure(s)
31212236Ssam 	int s;
31312236Ssam {
31412236Ssam 	char buf[BUFSIZ];
31512236Ssam 	struct ifconf ifc;
31612236Ssam 	struct ifreq ifreq, *ifr;
31712236Ssam 	int n;
31812236Ssam 	struct sockaddr_in *sin;
31912236Ssam 	register struct neighbor *np;
32012236Ssam 
32112236Ssam 	ifc.ifc_len = sizeof (buf);
32212236Ssam 	ifc.ifc_buf = buf;
32312236Ssam 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
32412236Ssam 		perror("rwhod: ioctl (get interface configuration)");
32512236Ssam 		return (0);
32612236Ssam 	}
32712236Ssam 	ifr = ifc.ifc_req;
32812236Ssam 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
32912236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
33012236Ssam 			if (np->n_name &&
33112236Ssam 			    strcmp(ifr->ifr_name, np->n_name) == 0)
33212236Ssam 				break;
33312236Ssam 		if (np != NULL)
33412236Ssam 			continue;
33512236Ssam 		ifreq = *ifr;
33612236Ssam 		np = (struct neighbor *)malloc(sizeof (*np));
33712236Ssam 		if (np == NULL)
33812236Ssam 			continue;
33912236Ssam 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
34012236Ssam 		if (np->n_name == NULL) {
34112236Ssam 			free((char *)np);
34212236Ssam 			continue;
34312236Ssam 		}
34412236Ssam 		strcpy(np->n_name, ifr->ifr_name);
34512236Ssam 		np->n_addrlen = sizeof (ifr->ifr_addr);
34612236Ssam 		np->n_addr = malloc(np->n_addrlen);
34712236Ssam 		if (np->n_addr == NULL) {
34812236Ssam 			free(np->n_name);
34912236Ssam 			free((char *)np);
35012236Ssam 			continue;
35112236Ssam 		}
35212236Ssam 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
35312236Ssam 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
35412236Ssam 			perror("rwhod: ioctl (get interface flags)");
35512236Ssam 			free((char *)np);
35612236Ssam 			continue;
35712236Ssam 		}
35812236Ssam 		if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
35912236Ssam 			free((char *)np);
36012236Ssam 			continue;
36112236Ssam 		}
36212236Ssam 		np->n_flags = ifreq.ifr_flags;
36312236Ssam 		if (np->n_flags & IFF_POINTOPOINT) {
36412236Ssam 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
36512236Ssam 				perror("rwhod: ioctl (get dstaddr)");
36612236Ssam 				free((char *)np);
36712236Ssam 				continue;
36812236Ssam 			}
36912236Ssam 			/* we assume addresses are all the same size */
37012236Ssam 			bcopy((char *)&ifreq.ifr_dstaddr,
37112236Ssam 			  np->n_addr, np->n_addrlen);
37212236Ssam 		}
37312236Ssam 		if (np->n_flags & IFF_BROADCAST) {
37412236Ssam 			/* we assume addresses are all the same size */
37512236Ssam 			sin = (struct sockaddr_in *)np->n_addr;
37612236Ssam 			sin->sin_addr =
37712236Ssam 			  inet_makeaddr(inet_netof(sin->sin_addr), INADDR_ANY);
37812236Ssam 		}
37912236Ssam 		/* gag, wish we could get rid of Internet dependencies */
38012236Ssam 		sin = (struct sockaddr_in *)np->n_addr;
38112236Ssam 		sin->sin_port = sp->s_port;
38212236Ssam 		np->n_next = neighbors;
38312236Ssam 		neighbors = np;
38412236Ssam 	}
38512236Ssam 	return (1);
38612236Ssam }
38712807Ssam 
38812807Ssam #ifdef DEBUG
38912807Ssam sendto(s, buf, cc, flags, to, tolen)
39012807Ssam 	int s;
39112807Ssam 	char *buf;
39212807Ssam 	int cc, flags;
39312807Ssam 	char *to;
39412807Ssam 	int tolen;
39512807Ssam {
39612807Ssam 	register struct whod *w = (struct whod *)buf;
39712807Ssam 	register struct whoent *we;
39812807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
39912807Ssam 	char *interval();
40012807Ssam 
40112807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
40212807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
40312807Ssam 	   interval(w->wd_sendtime - w->wd_boottime, "  up"));
40412807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
40512807Ssam 	    w->wd_loadav[0] / 100.0, w->wd_loadav[1] / 100.0,
40612807Ssam 	    w->wd_loadav[2] / 100.0);
40712807Ssam 	cc -= WHDRSIZE;
40812807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
40912807Ssam 		printf("%-8.8s %s:%s %.12s",
41012807Ssam 			 we->we_utmp.out_name,
41112807Ssam 			 w->wd_hostname, we->we_utmp.out_line,
41212807Ssam 			ctime((time_t *)&we->we_utmp.out_time)+4);
41312807Ssam 		we->we_idle /= 60;
41412807Ssam 		if (we->we_idle) {
41512807Ssam 			if (we->we_idle >= 100*60)
41612807Ssam 				we->we_idle = 100*60 - 1;
41712807Ssam 			if (we->we_idle >= 60)
41812807Ssam 				printf(" %2d", we->we_idle / 60);
41912807Ssam 			else
42012807Ssam 				printf("   ");
42112807Ssam 			printf(":%02d", we->we_idle % 60);
42212807Ssam 		}
42312807Ssam 		printf("\n");
42412807Ssam 	}
42512807Ssam }
42612807Ssam 
42712807Ssam char *
42812807Ssam interval(time, updown)
42912807Ssam 	int time;
43012807Ssam 	char *updown;
43112807Ssam {
43212807Ssam 	static char resbuf[32];
43312807Ssam 	int days, hours, minutes;
43412807Ssam 
43512807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
43612807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
43712807Ssam 		return (resbuf);
43812807Ssam 	}
43912807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
44012807Ssam 	hours = minutes / 60; minutes %= 60;
44112807Ssam 	days = hours / 24; hours %= 24;
44212807Ssam 	if (days)
44312807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
44412807Ssam 		    updown, days, hours, minutes);
44512807Ssam 	else
44612807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
44712807Ssam 		    updown, hours, minutes);
44812807Ssam 	return (resbuf);
44912807Ssam }
45012807Ssam #endif
451