xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 15214)
16460Swnj #ifndef lint
2*15214Sleres static char sccsid[] = "@(#)rwhod.c	4.20 (Berkeley) 83/10/11";
36460Swnj #endif
46460Swnj 
59215Ssam #include <sys/types.h>
69215Ssam #include <sys/socket.h>
79215Ssam #include <sys/stat.h>
89215Ssam #include <sys/ioctl.h>
913187Ssam #include <sys/file.h>
109215Ssam 
1112236Ssam #include <net/if.h>
129215Ssam #include <netinet/in.h>
139215Ssam 
149215Ssam #include <nlist.h>
156460Swnj #include <stdio.h>
166460Swnj #include <signal.h>
176460Swnj #include <errno.h>
186460Swnj #include <utmp.h>
198486Ssam #include <ctype.h>
208486Ssam #include <netdb.h>
2113567Ssam #include "rwhod.h"
226460Swnj 
238486Ssam struct	sockaddr_in sin = { AF_INET };
246460Swnj 
256460Swnj extern	errno;
266460Swnj 
2712236Ssam char	myname[32];
286460Swnj 
296460Swnj struct	nlist nl[] = {
306460Swnj #define	NL_AVENRUN	0
316460Swnj 	{ "_avenrun" },
329215Ssam #define	NL_BOOTTIME	1
339215Ssam 	{ "_boottime" },
346460Swnj 	0
356460Swnj };
366460Swnj 
3712236Ssam /*
3812236Ssam  * We communicate with each neighbor in
3912236Ssam  * a list constructed at the time we're
4012236Ssam  * started up.  Neighbors are currently
4112236Ssam  * directly connected via a hardware interface.
4212236Ssam  */
4312236Ssam struct	neighbor {
4412236Ssam 	struct	neighbor *n_next;
4512236Ssam 	char	*n_name;		/* interface name */
4612236Ssam 	char	*n_addr;		/* who to send to */
4712236Ssam 	int	n_addrlen;		/* size of address */
4812236Ssam 	int	n_flags;		/* should forward?, interface flags */
4912236Ssam };
5012236Ssam 
5112236Ssam struct	neighbor *neighbors;
526460Swnj struct	whod mywd;
5312236Ssam struct	servent *sp;
546460Swnj int	s, utmpf, kmemf = -1;
556460Swnj 
5612236Ssam #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
5712236Ssam #define	RWHODIR		"/usr/spool/rwho"
5811834Ssam 
596460Swnj int	onalrm();
6012236Ssam char	*strcpy(), *sprintf(), *malloc();
616460Swnj long	lseek();
626460Swnj int	getkmem();
6312236Ssam struct	in_addr inet_makeaddr();
646460Swnj 
656460Swnj main()
666460Swnj {
676460Swnj 	struct sockaddr_in from;
686460Swnj 	char path[64];
696460Swnj 	int addr;
7012236Ssam 	struct hostent *hp;
716460Swnj 
728486Ssam 	sp = getservbyname("who", "udp");
738486Ssam 	if (sp == 0) {
748486Ssam 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
758486Ssam 		exit(1);
768486Ssam 	}
776460Swnj #ifndef DEBUG
786460Swnj 	if (fork())
796460Swnj 		exit(0);
806460Swnj 	{ int s;
816460Swnj 	  for (s = 0; s < 10; s++)
826460Swnj 		(void) close(s);
836460Swnj 	  (void) open("/", 0);
846460Swnj 	  (void) dup2(0, 1);
856460Swnj 	  (void) dup2(0, 2);
866460Swnj 	  s = open("/dev/tty", 2);
876460Swnj 	  if (s >= 0) {
886460Swnj 		ioctl(s, TIOCNOTTY, 0);
896460Swnj 		(void) close(s);
906460Swnj 	  }
916460Swnj 	}
926460Swnj #endif
936460Swnj 	(void) chdir("/dev");
946460Swnj 	(void) signal(SIGHUP, getkmem);
956460Swnj 	if (getuid()) {
968486Ssam 		fprintf(stderr, "rwhod: not super user\n");
976460Swnj 		exit(1);
986460Swnj 	}
9912236Ssam 	/*
10012236Ssam 	 * Establish host name as returned by system.
10112236Ssam 	 */
10212236Ssam 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
10312236Ssam 		perror("gethostname");
1046460Swnj 		exit(1);
1056460Swnj 	}
10612236Ssam 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
10713187Ssam 	utmpf = open("/etc/utmp", O_RDONLY);
1086460Swnj 	if (utmpf < 0) {
1096460Swnj 		(void) close(creat("/etc/utmp", 0644));
11013187Ssam 		utmpf = open("/etc/utmp", O_RDONLY);
1116460Swnj 	}
1126460Swnj 	if (utmpf < 0) {
1138486Ssam 		perror("rwhod: /etc/utmp");
1146460Swnj 		exit(1);
1156460Swnj 	}
1166460Swnj 	getkmem();
11713187Ssam 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1188486Ssam 		perror("rwhod: socket");
1199215Ssam 		exit(1);
1206460Swnj 	}
12112236Ssam 	hp = gethostbyname(myname);
12212236Ssam 	if (hp == NULL) {
12312236Ssam 		fprintf(stderr, "%s: don't know my own name\n", myname);
12412236Ssam 		exit(1);
12512236Ssam 	}
12612236Ssam 	sin.sin_family = hp->h_addrtype;
12712236Ssam 	sin.sin_port = sp->s_port;
12813187Ssam 	if (bind(s, &sin, sizeof (sin)) < 0) {
1299215Ssam 		perror("rwhod: bind");
1309215Ssam 		exit(1);
1319215Ssam 	}
13212236Ssam 	if (!configure(s))
13312236Ssam 		exit(1);
13413027Ssam 	signal(SIGALRM, onalrm);
1356460Swnj 	onalrm();
1366460Swnj 	for (;;) {
1376460Swnj 		struct whod wd;
13812236Ssam 		int cc, whod, len = sizeof (from);
1396460Swnj 
14012236Ssam 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
14112236Ssam 			&from, &len);
1426460Swnj 		if (cc <= 0) {
1436460Swnj 			if (cc < 0 && errno != EINTR)
1449215Ssam 				perror("rwhod: recv");
1456460Swnj 			continue;
1466460Swnj 		}
1478486Ssam 		if (from.sin_port != sp->s_port) {
1488486Ssam 			fprintf(stderr, "rwhod: %d: bad from port\n",
1498486Ssam 				ntohs(from.sin_port));
1506460Swnj 			continue;
1516460Swnj 		}
1528486Ssam #ifdef notdef
1538486Ssam 		if (gethostbyname(wd.wd_hostname) == 0) {
1548486Ssam 			fprintf(stderr, "rwhod: %s: unknown host\n",
1558486Ssam 				wd.wd_hostname);
1566460Swnj 			continue;
1576460Swnj 		}
1588486Ssam #endif
15912347Ssam 		if (wd.wd_vers != WHODVERSION)
16012347Ssam 			continue;
16112236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
16212236Ssam 			continue;
1638486Ssam 		if (!verify(wd.wd_hostname)) {
1648486Ssam 			fprintf(stderr, "rwhod: malformed host name from %x\n",
1658486Ssam 				from.sin_addr);
1668486Ssam 			continue;
1678486Ssam 		}
1689914Ssam 		(void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
1696460Swnj 		whod = creat(path, 0666);
1706460Swnj 		if (whod < 0) {
1718486Ssam 			fprintf(stderr, "rwhod: ");
1728486Ssam 			perror(path);
1736460Swnj 			continue;
1746460Swnj 		}
17511834Ssam #if vax || pdp11
17611834Ssam 		{
17713187Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
17811834Ssam 			struct whoent *we;
17911834Ssam 
18011834Ssam 			/* undo header byte swapping before writing to file */
18111834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
18211834Ssam 			for (i = 0; i < 3; i++)
18311834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
18411834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
18511834Ssam 			we = wd.wd_we;
18611834Ssam 			for (i = 0; i < n; i++) {
18711834Ssam 				we->we_idle = ntohl(we->we_idle);
18812872Ssam 				we->we_utmp.out_time =
18912872Ssam 				    ntohl(we->we_utmp.out_time);
19011834Ssam 				we++;
19111834Ssam 			}
19211834Ssam 		}
19311834Ssam #endif
1946460Swnj 		(void) time(&wd.wd_recvtime);
1956460Swnj 		(void) write(whod, (char *)&wd, cc);
1966460Swnj 		(void) close(whod);
1976460Swnj 	}
1986460Swnj }
1996460Swnj 
2008486Ssam /*
2018486Ssam  * Check out host name for unprintables
2028486Ssam  * and other funnies before allowing a file
2038486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2048486Ssam  */
2058486Ssam verify(name)
2068486Ssam 	register char *name;
2078486Ssam {
2088486Ssam 	register int size = 0;
2098486Ssam 
2108486Ssam 	while (*name) {
211*15214Sleres 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
2128486Ssam 			return (0);
2138486Ssam 		name++, size++;
2148486Ssam 	}
2158486Ssam 	return (size > 0);
2168486Ssam }
2178486Ssam 
2186460Swnj int	utmptime;
2196460Swnj int	utmpent;
2206577Ssam struct	utmp utmp[100];
2216460Swnj int	alarmcount;
2226460Swnj 
2236460Swnj onalrm()
2246460Swnj {
2256460Swnj 	register int i;
2266460Swnj 	struct stat stb;
2276577Ssam 	register struct whoent *we = mywd.wd_we, *wlast;
2286460Swnj 	int cc;
2296460Swnj 	double avenrun[3];
2306460Swnj 	time_t now = time(0);
23112236Ssam 	register struct neighbor *np;
2326460Swnj 
2336460Swnj 	if (alarmcount % 10 == 0)
2346460Swnj 		getkmem();
2356460Swnj 	alarmcount++;
2366460Swnj 	(void) fstat(utmpf, &stb);
2376460Swnj 	if (stb.st_mtime != utmptime) {
23813187Ssam 		(void) lseek(utmpf, (long)0, L_SET);
2396460Swnj 		cc = read(utmpf, (char *)utmp, sizeof (utmp));
2406460Swnj 		if (cc < 0) {
2416460Swnj 			perror("/etc/utmp");
24213469Ssam 			goto done;
2436460Swnj 		}
24412236Ssam 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
2456460Swnj 		utmpent = cc / sizeof (struct utmp);
2466460Swnj 		for (i = 0; i < utmpent; i++)
2476460Swnj 			if (utmp[i].ut_name[0]) {
24812807Ssam 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
24912807Ssam 				   sizeof (utmp[i].ut_line));
25012807Ssam 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
25112807Ssam 				   sizeof (utmp[i].ut_name));
25212807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
2536577Ssam 				if (we >= wlast)
2546577Ssam 					break;
2556460Swnj 				we++;
2566460Swnj 			}
2576460Swnj 		utmpent = we - mywd.wd_we;
2586460Swnj 	}
2596460Swnj 	we = mywd.wd_we;
2606460Swnj 	for (i = 0; i < utmpent; i++) {
26112807Ssam 		if (stat(we->we_utmp.out_line, &stb) >= 0)
26212807Ssam 			we->we_idle = htonl(now - stb.st_atime);
2636460Swnj 		we++;
2646460Swnj 	}
26513187Ssam 	(void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
2666460Swnj 	(void) read(kmemf, (char *)avenrun, sizeof (avenrun));
2676460Swnj 	for (i = 0; i < 3; i++)
26812873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
2696460Swnj 	cc = (char *)we - (char *)&mywd;
27012807Ssam 	mywd.wd_sendtime = htonl(time(0));
27112236Ssam 	mywd.wd_vers = WHODVERSION;
27212236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
27312236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
27412236Ssam 		(void) sendto(s, (char *)&mywd, cc, 0,
27512236Ssam 			np->n_addr, np->n_addrlen);
27613469Ssam done:
2776460Swnj 	(void) alarm(60);
2786460Swnj }
2796460Swnj 
2806460Swnj getkmem()
2816460Swnj {
2826460Swnj 	struct nlist *nlp;
2836460Swnj 
2846460Swnj 	if (kmemf >= 0)
2856460Swnj 		(void) close(kmemf);
2866460Swnj loop:
2876460Swnj 	for (nlp = &nl[sizeof (nl) / sizeof (nl[0])]; --nlp >= nl; ) {
2886460Swnj 		nlp->n_value = 0;
2896460Swnj 		nlp->n_type = 0;
2906460Swnj 	}
2916460Swnj 	nlist("/vmunix", nl);
2926460Swnj 	if (nl[0].n_value == 0) {
2936460Swnj 		fprintf(stderr, "/vmunix namelist botch\n");
2946460Swnj 		sleep(300);
2956460Swnj 		goto loop;
2966460Swnj 	}
29713187Ssam 	kmemf = open("/dev/kmem", O_RDONLY);
2986460Swnj 	if (kmemf < 0) {
2996460Swnj 		perror("/dev/kmem");
3006460Swnj 		sleep(300);
3016460Swnj 		goto loop;
3026460Swnj 	}
30313187Ssam 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
30413187Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime,
30513187Ssam 	    sizeof (mywd.wd_boottime));
30612807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
3076460Swnj }
30812236Ssam 
30912236Ssam /*
31012236Ssam  * Figure out device configuration and select
31112236Ssam  * networks which deserve status information.
31212236Ssam  */
31312236Ssam configure(s)
31412236Ssam 	int s;
31512236Ssam {
31612236Ssam 	char buf[BUFSIZ];
31712236Ssam 	struct ifconf ifc;
31812236Ssam 	struct ifreq ifreq, *ifr;
31912236Ssam 	struct sockaddr_in *sin;
32012236Ssam 	register struct neighbor *np;
32113187Ssam 	int n;
32212236Ssam 
32312236Ssam 	ifc.ifc_len = sizeof (buf);
32412236Ssam 	ifc.ifc_buf = buf;
32512236Ssam 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
32612236Ssam 		perror("rwhod: ioctl (get interface configuration)");
32712236Ssam 		return (0);
32812236Ssam 	}
32912236Ssam 	ifr = ifc.ifc_req;
33012236Ssam 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
33112236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
33212236Ssam 			if (np->n_name &&
33312236Ssam 			    strcmp(ifr->ifr_name, np->n_name) == 0)
33412236Ssam 				break;
33512236Ssam 		if (np != NULL)
33612236Ssam 			continue;
33712236Ssam 		ifreq = *ifr;
33812236Ssam 		np = (struct neighbor *)malloc(sizeof (*np));
33912236Ssam 		if (np == NULL)
34012236Ssam 			continue;
34112236Ssam 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
34212236Ssam 		if (np->n_name == NULL) {
34312236Ssam 			free((char *)np);
34412236Ssam 			continue;
34512236Ssam 		}
34612236Ssam 		strcpy(np->n_name, ifr->ifr_name);
34712236Ssam 		np->n_addrlen = sizeof (ifr->ifr_addr);
34812236Ssam 		np->n_addr = malloc(np->n_addrlen);
34912236Ssam 		if (np->n_addr == NULL) {
35012236Ssam 			free(np->n_name);
35112236Ssam 			free((char *)np);
35212236Ssam 			continue;
35312236Ssam 		}
35412236Ssam 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
35512236Ssam 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
35612236Ssam 			perror("rwhod: ioctl (get interface flags)");
35712236Ssam 			free((char *)np);
35812236Ssam 			continue;
35912236Ssam 		}
36012236Ssam 		if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
36112236Ssam 			free((char *)np);
36212236Ssam 			continue;
36312236Ssam 		}
36412236Ssam 		np->n_flags = ifreq.ifr_flags;
36512236Ssam 		if (np->n_flags & IFF_POINTOPOINT) {
36612236Ssam 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
36712236Ssam 				perror("rwhod: ioctl (get dstaddr)");
36812236Ssam 				free((char *)np);
36912236Ssam 				continue;
37012236Ssam 			}
37112236Ssam 			/* we assume addresses are all the same size */
37212236Ssam 			bcopy((char *)&ifreq.ifr_dstaddr,
37312236Ssam 			  np->n_addr, np->n_addrlen);
37412236Ssam 		}
37512236Ssam 		if (np->n_flags & IFF_BROADCAST) {
37612236Ssam 			/* we assume addresses are all the same size */
37712236Ssam 			sin = (struct sockaddr_in *)np->n_addr;
37812236Ssam 			sin->sin_addr =
37912236Ssam 			  inet_makeaddr(inet_netof(sin->sin_addr), INADDR_ANY);
38012236Ssam 		}
38112236Ssam 		/* gag, wish we could get rid of Internet dependencies */
38212236Ssam 		sin = (struct sockaddr_in *)np->n_addr;
38312236Ssam 		sin->sin_port = sp->s_port;
38412236Ssam 		np->n_next = neighbors;
38512236Ssam 		neighbors = np;
38612236Ssam 	}
38712236Ssam 	return (1);
38812236Ssam }
38912807Ssam 
39012807Ssam #ifdef DEBUG
39112807Ssam sendto(s, buf, cc, flags, to, tolen)
39212807Ssam 	int s;
39312807Ssam 	char *buf;
39412807Ssam 	int cc, flags;
39512807Ssam 	char *to;
39612807Ssam 	int tolen;
39712807Ssam {
39812807Ssam 	register struct whod *w = (struct whod *)buf;
39912807Ssam 	register struct whoent *we;
40012807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
40112807Ssam 	char *interval();
40212807Ssam 
40312807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
40412807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
40513187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
40612807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
40713187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
40813187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
40912807Ssam 	cc -= WHDRSIZE;
41012807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
41113187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
41212807Ssam 		printf("%-8.8s %s:%s %.12s",
41313187Ssam 			we->we_utmp.out_name,
41413187Ssam 			w->wd_hostname, we->we_utmp.out_line,
41513187Ssam 			ctime(&t)+4);
41613187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
41712807Ssam 		if (we->we_idle) {
41812807Ssam 			if (we->we_idle >= 100*60)
41912807Ssam 				we->we_idle = 100*60 - 1;
42012807Ssam 			if (we->we_idle >= 60)
42112807Ssam 				printf(" %2d", we->we_idle / 60);
42212807Ssam 			else
42312807Ssam 				printf("   ");
42412807Ssam 			printf(":%02d", we->we_idle % 60);
42512807Ssam 		}
42612807Ssam 		printf("\n");
42712807Ssam 	}
42812807Ssam }
42912807Ssam 
43012807Ssam char *
43112807Ssam interval(time, updown)
43212807Ssam 	int time;
43312807Ssam 	char *updown;
43412807Ssam {
43512807Ssam 	static char resbuf[32];
43612807Ssam 	int days, hours, minutes;
43712807Ssam 
43812807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
43912807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
44012807Ssam 		return (resbuf);
44112807Ssam 	}
44212807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
44312807Ssam 	hours = minutes / 60; minutes %= 60;
44412807Ssam 	days = hours / 24; hours %= 24;
44512807Ssam 	if (days)
44612807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
44712807Ssam 		    updown, days, hours, minutes);
44812807Ssam 	else
44912807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
45012807Ssam 		    updown, hours, minutes);
45112807Ssam 	return (resbuf);
45212807Ssam }
45312807Ssam #endif
454