xref: /original-bsd/usr.sbin/rwhod/rwhod.c (revision 1612f32f)
1 #ifndef lint
2 static char sccsid[] = "@(#)rwhod.c	4.10 83/05/09";
3 #endif
4 
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <sys/stat.h>
8 #include <sys/ioctl.h>
9 
10 #include <net/if.h>
11 #include <netinet/in.h>
12 
13 #include <nlist.h>
14 #include <stdio.h>
15 #include <signal.h>
16 #include <errno.h>
17 #include <utmp.h>
18 #include <ctype.h>
19 #include <netdb.h>
20 #include <rwhod.h>
21 
22 struct	sockaddr_in sin = { AF_INET };
23 
24 extern	errno;
25 
26 char	myname[32];
27 
28 struct	nlist nl[] = {
29 #define	NL_AVENRUN	0
30 	{ "_avenrun" },
31 #define	NL_BOOTTIME	1
32 	{ "_boottime" },
33 	0
34 };
35 
36 /*
37  * We communicate with each neighbor in
38  * a list constructed at the time we're
39  * started up.  Neighbors are currently
40  * directly connected via a hardware interface.
41  */
42 struct	neighbor {
43 	struct	neighbor *n_next;
44 	char	*n_name;		/* interface name */
45 	char	*n_addr;		/* who to send to */
46 	int	n_addrlen;		/* size of address */
47 	int	n_flags;		/* should forward?, interface flags */
48 };
49 
50 struct	neighbor *neighbors;
51 struct	whod mywd;
52 struct	servent *sp;
53 int	s, utmpf, kmemf = -1;
54 
55 #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
56 #define	RWHODIR		"/usr/spool/rwho"
57 
58 int	onalrm();
59 char	*strcpy(), *sprintf(), *malloc();
60 long	lseek();
61 int	getkmem();
62 struct	in_addr inet_makeaddr();
63 
64 main()
65 {
66 	struct sockaddr_in from;
67 	char path[64];
68 	int addr;
69 	struct hostent *hp;
70 
71 	sp = getservbyname("who", "udp");
72 	if (sp == 0) {
73 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
74 		exit(1);
75 	}
76 #ifndef DEBUG
77 	if (fork())
78 		exit(0);
79 	{ int s;
80 	  for (s = 0; s < 10; s++)
81 		(void) close(s);
82 	  (void) open("/", 0);
83 	  (void) dup2(0, 1);
84 	  (void) dup2(0, 2);
85 	  s = open("/dev/tty", 2);
86 	  if (s >= 0) {
87 		ioctl(s, TIOCNOTTY, 0);
88 		(void) close(s);
89 	  }
90 	}
91 #endif
92 	(void) chdir("/dev");
93 	(void) signal(SIGHUP, getkmem);
94 	if (getuid()) {
95 		fprintf(stderr, "rwhod: not super user\n");
96 		exit(1);
97 	}
98 	/*
99 	 * Establish host name as returned by system.
100 	 */
101 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
102 		perror("gethostname");
103 		exit(1);
104 	}
105 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
106 	utmpf = open("/etc/utmp", 0);
107 	if (utmpf < 0) {
108 		(void) close(creat("/etc/utmp", 0644));
109 		utmpf = open("/etc/utmp", 0);
110 	}
111 	if (utmpf < 0) {
112 		perror("rwhod: /etc/utmp");
113 		exit(1);
114 	}
115 	getkmem();
116 	if ((s = socket(AF_INET, SOCK_DGRAM, 0, 0)) < 0) {
117 		perror("rwhod: socket");
118 		exit(1);
119 	}
120 	hp = gethostbyname(myname);
121 	if (hp == NULL) {
122 		fprintf(stderr, "%s: don't know my own name\n", myname);
123 		exit(1);
124 	}
125 	sin.sin_family = hp->h_addrtype;
126 	sin.sin_port = sp->s_port;
127 	if (bind(s, &sin, sizeof (sin), 0) < 0) {
128 		perror("rwhod: bind");
129 		exit(1);
130 	}
131 	if (!configure(s))
132 		exit(1);
133 	sigset(SIGALRM, onalrm);
134 	onalrm();
135 	for (;;) {
136 		struct whod wd;
137 		int cc, whod, len = sizeof (from);
138 
139 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
140 			&from, &len);
141 		if (cc <= 0) {
142 			if (cc < 0 && errno != EINTR)
143 				perror("rwhod: recv");
144 			continue;
145 		}
146 		if (from.sin_port != sp->s_port) {
147 			fprintf(stderr, "rwhod: %d: bad from port\n",
148 				ntohs(from.sin_port));
149 			continue;
150 		}
151 #ifdef notdef
152 		if (gethostbyname(wd.wd_hostname) == 0) {
153 			fprintf(stderr, "rwhod: %s: unknown host\n",
154 				wd.wd_hostname);
155 			continue;
156 		}
157 #endif
158 		if (wd.wd_vers != WHODVERSION)
159 			continue;
160 		if (wd.wd_type != WHODTYPE_STATUS)
161 			continue;
162 		if (!verify(wd.wd_hostname)) {
163 			fprintf(stderr, "rwhod: malformed host name from %x\n",
164 				from.sin_addr);
165 			continue;
166 		}
167 		(void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
168 		whod = creat(path, 0666);
169 		if (whod < 0) {
170 			fprintf(stderr, "rwhod: ");
171 			perror(path);
172 			continue;
173 		}
174 #if vax || pdp11
175 		{
176 			int i, n = (cc - WHDRSIZE)/sizeof(struct utmp);
177 			struct whoent *we;
178 
179 			/* undo header byte swapping before writing to file */
180 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
181 			for (i = 0; i < 3; i++)
182 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
183 			wd.wd_boottime = ntohl(wd.wd_boottime);
184 			we = wd.wd_we;
185 			for (i = 0; i < n; i++) {
186 				we->we_idle = ntohl(we->we_idle);
187 				we->we_utmp.ut_time = ntohl(we->we_utmp.ut_time);
188 				we++;
189 			}
190 		}
191 #endif
192 		(void) time(&wd.wd_recvtime);
193 		(void) write(whod, (char *)&wd, cc);
194 		(void) close(whod);
195 	}
196 }
197 
198 /*
199  * Check out host name for unprintables
200  * and other funnies before allowing a file
201  * to be created.  Sorry, but blanks aren't allowed.
202  */
203 verify(name)
204 	register char *name;
205 {
206 	register int size = 0;
207 
208 	while (*name) {
209 		if (!isascii(*name) || !isalnum(*name))
210 			return (0);
211 		name++, size++;
212 	}
213 	return (size > 0);
214 }
215 
216 int	utmptime;
217 int	utmpent;
218 struct	utmp utmp[100];
219 int	alarmcount;
220 
221 onalrm()
222 {
223 	register int i;
224 	struct stat stb;
225 	register struct whoent *we = mywd.wd_we, *wlast;
226 	int cc;
227 	double avenrun[3];
228 	time_t now = time(0);
229 	register struct neighbor *np;
230 
231 	if (alarmcount % 10 == 0)
232 		getkmem();
233 	alarmcount++;
234 	(void) fstat(utmpf, &stb);
235 	if (stb.st_mtime != utmptime) {
236 		(void) lseek(utmpf, (long)0, 0);
237 		cc = read(utmpf, (char *)utmp, sizeof (utmp));
238 		if (cc < 0) {
239 			perror("/etc/utmp");
240 			return;
241 		}
242 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
243 		utmpent = cc / sizeof (struct utmp);
244 		for (i = 0; i < utmpent; i++)
245 			if (utmp[i].ut_name[0]) {
246 				we->we_utmp = utmp[i];
247 				if (we >= wlast)
248 					break;
249 				we++;
250 			}
251 		utmpent = we - mywd.wd_we;
252 	}
253 	we = mywd.wd_we;
254 	for (i = 0; i < utmpent; i++) {
255 		if (stat(we->we_utmp.ut_line, &stb) >= 0)
256 			we->we_idle = now - stb.st_atime;
257 		we++;
258 	}
259 	(void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, 0);
260 	(void) read(kmemf, (char *)avenrun, sizeof (avenrun));
261 	for (i = 0; i < 3; i++)
262 		mywd.wd_loadav[i] = avenrun[i] * 100;
263 	cc = (char *)we - (char *)&mywd;
264 	(void) time(&mywd.wd_sendtime);
265 #if vax || pdp11
266 	mywd.wd_sendtime = htonl(mywd.wd_sendtime);
267 	for (i = 0; i < 3; i++)
268 		mywd.wd_loadav[i] = htonl(mywd.wd_loadav[i]);
269 	mywd.wd_boottime = htonl(mywd.wd_boottime);
270 	we = mywd.wd_we;
271 	for (i = 0; i < utmpent; i++) {
272 		we->we_idle = htonl(we->we_idle);
273 		we->we_utmp.ut_time = htonl(we->we_utmp.ut_time);
274 		we++;
275 	}
276 #endif
277 	mywd.wd_vers = WHODVERSION;
278 	mywd.wd_type = WHODTYPE_STATUS;
279 	for (np = neighbors; np != NULL; np = np->n_next)
280 		(void) sendto(s, (char *)&mywd, cc, 0,
281 			np->n_addr, np->n_addrlen);
282 	(void) alarm(60);
283 }
284 
285 getkmem()
286 {
287 	struct nlist *nlp;
288 
289 	signal(SIGHUP, getkmem);
290 	if (kmemf >= 0)
291 		(void) close(kmemf);
292 loop:
293 	for (nlp = &nl[sizeof (nl) / sizeof (nl[0])]; --nlp >= nl; ) {
294 		nlp->n_value = 0;
295 		nlp->n_type = 0;
296 	}
297 	nlist("/vmunix", nl);
298 	if (nl[0].n_value == 0) {
299 		fprintf(stderr, "/vmunix namelist botch\n");
300 		sleep(300);
301 		goto loop;
302 	}
303 	kmemf = open("/dev/kmem", 0);
304 	if (kmemf < 0) {
305 		perror("/dev/kmem");
306 		sleep(300);
307 		goto loop;
308 	}
309 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, 0);
310 	(void) read(kmemf, (char *)&mywd.wd_boottime, sizeof (mywd.wd_boottime));
311 }
312 
313 /*
314  * Figure out device configuration and select
315  * networks which deserve status information.
316  */
317 configure(s)
318 	int s;
319 {
320 	char buf[BUFSIZ];
321 	struct ifconf ifc;
322 	struct ifreq ifreq, *ifr;
323 	int n;
324 	struct sockaddr_in *sin;
325 	register struct neighbor *np;
326 
327 	ifc.ifc_len = sizeof (buf);
328 	ifc.ifc_buf = buf;
329 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
330 		perror("rwhod: ioctl (get interface configuration)");
331 		return (0);
332 	}
333 	ifr = ifc.ifc_req;
334 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
335 		for (np = neighbors; np != NULL; np = np->n_next)
336 			if (np->n_name &&
337 			    strcmp(ifr->ifr_name, np->n_name) == 0)
338 				break;
339 		if (np != NULL)
340 			continue;
341 		ifreq = *ifr;
342 		np = (struct neighbor *)malloc(sizeof (*np));
343 		if (np == NULL)
344 			continue;
345 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
346 		if (np->n_name == NULL) {
347 			free((char *)np);
348 			continue;
349 		}
350 		strcpy(np->n_name, ifr->ifr_name);
351 		np->n_addrlen = sizeof (ifr->ifr_addr);
352 		np->n_addr = malloc(np->n_addrlen);
353 		if (np->n_addr == NULL) {
354 			free(np->n_name);
355 			free((char *)np);
356 			continue;
357 		}
358 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
359 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
360 			perror("rwhod: ioctl (get interface flags)");
361 			free((char *)np);
362 			continue;
363 		}
364 		if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
365 			free((char *)np);
366 			continue;
367 		}
368 		np->n_flags = ifreq.ifr_flags;
369 		if (np->n_flags & IFF_POINTOPOINT) {
370 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
371 				perror("rwhod: ioctl (get dstaddr)");
372 				free((char *)np);
373 				continue;
374 			}
375 			/* we assume addresses are all the same size */
376 			bcopy((char *)&ifreq.ifr_dstaddr,
377 			  np->n_addr, np->n_addrlen);
378 		}
379 		if (np->n_flags & IFF_BROADCAST) {
380 			/* we assume addresses are all the same size */
381 			sin = (struct sockaddr_in *)np->n_addr;
382 			sin->sin_addr =
383 			  inet_makeaddr(inet_netof(sin->sin_addr), INADDR_ANY);
384 		}
385 		/* gag, wish we could get rid of Internet dependencies */
386 		sin = (struct sockaddr_in *)np->n_addr;
387 		sin->sin_port = sp->s_port;
388 		np->n_next = neighbors;
389 		neighbors = np;
390 	}
391 	return (1);
392 }
393