Add copyright
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
... / ...
CommitLineData
1#ifndef lint
2static char sccsid[] = "@(#)rwhod.c 4.28 (Berkeley) 85/02/25";
3#endif
4
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <sys/stat.h>
8#include <sys/ioctl.h>
9#include <sys/file.h>
10
11#include <net/if.h>
12#include <netinet/in.h>
13
14#include <nlist.h>
15#include <stdio.h>
16#include <signal.h>
17#include <errno.h>
18#include <utmp.h>
19#include <ctype.h>
20#include <netdb.h>
21#include <syslog.h>
22#include "rwhod.h"
23
24/*
25 * Alarm interval. Don't forget to change the down time check in ruptime
26 * if this is changed.
27 */
28#define AL_INTERVAL (3 * 60)
29#define MAXTTYS 256 /* Max # of utmp entries examined */
30
31struct sockaddr_in sin = { AF_INET };
32
33extern errno;
34
35char myname[32];
36
37struct nlist nl[] = {
38#define NL_AVENRUN 0
39 { "_avenrun" },
40#define NL_BOOTTIME 1
41 { "_boottime" },
42 0
43};
44
45/*
46 * We communicate with each neighbor in
47 * a list constructed at the time we're
48 * started up. Neighbors are currently
49 * directly connected via a hardware interface.
50 */
51struct neighbor {
52 struct neighbor *n_next;
53 char *n_name; /* interface name */
54 char *n_addr; /* who to send to */
55 int n_addrlen; /* size of address */
56 int n_flags; /* should forward?, interface flags */
57};
58
59struct neighbor *neighbors;
60struct whod mywd;
61struct servent *sp;
62int s, utmpf, kmemf = -1;
63
64#define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we))
65#define RWHODIR "/usr/spool/rwho"
66
67int onalrm();
68char *strcpy(), *sprintf(), *malloc();
69long lseek();
70int getkmem();
71struct in_addr inet_makeaddr();
72
73main()
74{
75 struct sockaddr_in from;
76 char path[64];
77 int addr, on = 1;
78 struct hostent *hp;
79
80 if (getuid()) {
81 fprintf(stderr, "rwhod: not super user\n");
82 exit(1);
83 }
84 sp = getservbyname("who", "udp");
85 if (sp == 0) {
86 fprintf(stderr, "rwhod: udp/who: unknown service\n");
87 exit(1);
88 }
89#ifndef DEBUG
90 if (fork())
91 exit(0);
92 { int s;
93 for (s = 0; s < 10; s++)
94 (void) close(s);
95 (void) open("/", 0);
96 (void) dup2(0, 1);
97 (void) dup2(0, 2);
98 s = open("/dev/tty", 2);
99 if (s >= 0) {
100 ioctl(s, TIOCNOTTY, 0);
101 (void) close(s);
102 }
103 }
104#endif
105 (void) chdir("/dev");
106 (void) signal(SIGHUP, getkmem);
107 openlog("rwhod", LOG_PID, 0);
108 /*
109 * Establish host name as returned by system.
110 */
111 if (gethostname(myname, sizeof (myname) - 1) < 0) {
112 syslog(LOG_ERR, "gethostname: %m");
113 exit(1);
114 }
115 strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
116 utmpf = open("/etc/utmp", O_RDONLY);
117 if (utmpf < 0) {
118 (void) close(creat("/etc/utmp", 0644));
119 utmpf = open("/etc/utmp", O_RDONLY);
120 }
121 if (utmpf < 0) {
122 syslog(LOG_ERR, "/etc/utmp: %m");
123 exit(1);
124 }
125 getkmem();
126 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
127 syslog(LOG_ERR, "socket: %m");
128 exit(1);
129 }
130 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
131 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
132 exit(1);
133 }
134 hp = gethostbyname(myname);
135 if (hp == NULL) {
136 syslog(LOG_ERR, "%s: don't know my own name", myname);
137 exit(1);
138 }
139 sin.sin_family = hp->h_addrtype;
140 sin.sin_port = sp->s_port;
141 if (bind(s, &sin, sizeof (sin)) < 0) {
142 syslog(LOG_ERR, "bind: %m");
143 exit(1);
144 }
145 if (!configure(s))
146 exit(1);
147 signal(SIGALRM, onalrm);
148 onalrm();
149 for (;;) {
150 struct whod wd;
151 int cc, whod, len = sizeof (from);
152
153 cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
154 &from, &len);
155 if (cc <= 0) {
156 if (cc < 0 && errno != EINTR)
157 syslog(LOG_WARNING, "recv: %m");
158 continue;
159 }
160 if (from.sin_port != sp->s_port) {
161 syslog(LOG_WARNING, "%d: bad from port",
162 ntohs(from.sin_port));
163 continue;
164 }
165#ifdef notdef
166 if (gethostbyname(wd.wd_hostname) == 0) {
167 syslog(LOG_WARNING, "%s: unknown host",
168 wd.wd_hostname);
169 continue;
170 }
171#endif
172 if (wd.wd_vers != WHODVERSION)
173 continue;
174 if (wd.wd_type != WHODTYPE_STATUS)
175 continue;
176 if (!verify(wd.wd_hostname)) {
177 syslog(LOG_WARNING, "malformed host name from %x",
178 from.sin_addr);
179 continue;
180 }
181 (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
182 whod = creat(path, 0666);
183 if (whod < 0) {
184 syslog(LOG_WARNING, "%s: %m", path);
185 continue;
186 }
187#if vax || pdp11
188 {
189 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
190 struct whoent *we;
191
192 /* undo header byte swapping before writing to file */
193 wd.wd_sendtime = ntohl(wd.wd_sendtime);
194 for (i = 0; i < 3; i++)
195 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
196 wd.wd_boottime = ntohl(wd.wd_boottime);
197 we = wd.wd_we;
198 for (i = 0; i < n; i++) {
199 we->we_idle = ntohl(we->we_idle);
200 we->we_utmp.out_time =
201 ntohl(we->we_utmp.out_time);
202 we++;
203 }
204 }
205#endif
206 (void) time(&wd.wd_recvtime);
207 (void) write(whod, (char *)&wd, cc);
208 (void) close(whod);
209 }
210}
211
212/*
213 * Check out host name for unprintables
214 * and other funnies before allowing a file
215 * to be created. Sorry, but blanks aren't allowed.
216 */
217verify(name)
218 register char *name;
219{
220 register int size = 0;
221
222 while (*name) {
223 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
224 return (0);
225 name++, size++;
226 }
227 return (size > 0);
228}
229
230int utmptime;
231int utmpent;
232struct utmp utmp[MAXTTYS];
233int alarmcount;
234
235onalrm()
236{
237 register int i;
238 struct stat stb;
239 register struct whoent *we = mywd.wd_we, *wlast;
240 int cc;
241 double avenrun[3];
242 time_t now = time(0);
243 register struct neighbor *np;
244
245 if (alarmcount % 10 == 0)
246 getkmem();
247 alarmcount++;
248 (void) fstat(utmpf, &stb);
249 if (stb.st_mtime != utmptime) {
250 utmptime = stb.st_mtime;
251 (void) lseek(utmpf, (long)0, L_SET);
252 cc = read(utmpf, (char *)utmp, sizeof (utmp));
253 if (cc < 0) {
254 perror("/etc/utmp");
255 goto done;
256 }
257 wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
258 utmpent = cc / sizeof (struct utmp);
259 for (i = 0; i < utmpent; i++)
260 if (utmp[i].ut_name[0]) {
261 bcopy(utmp[i].ut_line, we->we_utmp.out_line,
262 sizeof (utmp[i].ut_line));
263 bcopy(utmp[i].ut_name, we->we_utmp.out_name,
264 sizeof (utmp[i].ut_name));
265 we->we_utmp.out_time = htonl(utmp[i].ut_time);
266 if (we >= wlast)
267 break;
268 we++;
269 }
270 utmpent = we - mywd.wd_we;
271 }
272 we = mywd.wd_we;
273 for (i = 0; i < utmpent; i++) {
274 if (stat(we->we_utmp.out_line, &stb) >= 0)
275 we->we_idle = htonl(now - stb.st_atime);
276 we++;
277 }
278 (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
279 (void) read(kmemf, (char *)avenrun, sizeof (avenrun));
280 for (i = 0; i < 3; i++)
281 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
282 cc = (char *)we - (char *)&mywd;
283 mywd.wd_sendtime = htonl(time(0));
284 mywd.wd_vers = WHODVERSION;
285 mywd.wd_type = WHODTYPE_STATUS;
286 for (np = neighbors; np != NULL; np = np->n_next)
287 (void) sendto(s, (char *)&mywd, cc, 0,
288 np->n_addr, np->n_addrlen);
289done:
290 (void) alarm(AL_INTERVAL);
291}
292
293getkmem()
294{
295 struct nlist *nlp;
296 static ino_t vmunixino;
297 static time_t vmunixctime;
298 struct stat sb;
299
300 if (stat("/vmunix", &sb) < 0) {
301 if (vmunixctime)
302 return;
303 } else {
304 if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
305 return;
306 vmunixctime = sb.st_ctime;
307 vmunixino= sb.st_ino;
308 }
309 if (kmemf >= 0)
310 (void) close(kmemf);
311loop:
312 if (nlist("/vmunix", nl)) {
313 syslog(LOG_WARNING, "/vmunix namelist botch");
314 sleep(300);
315 goto loop;
316 }
317 kmemf = open("/dev/kmem", O_RDONLY);
318 if (kmemf < 0) {
319 syslog(LOG_ERR, "/dev/kmem: %m");
320 exit(1);
321 }
322 (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
323 (void) read(kmemf, (char *)&mywd.wd_boottime,
324 sizeof (mywd.wd_boottime));
325 mywd.wd_boottime = htonl(mywd.wd_boottime);
326}
327
328/*
329 * Figure out device configuration and select
330 * networks which deserve status information.
331 */
332configure(s)
333 int s;
334{
335 char buf[BUFSIZ];
336 struct ifconf ifc;
337 struct ifreq ifreq, *ifr;
338 struct sockaddr_in *sin;
339 register struct neighbor *np;
340 int n;
341
342 ifc.ifc_len = sizeof (buf);
343 ifc.ifc_buf = buf;
344 if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
345 syslog(LOG_ERR, "ioctl (get interface configuration)");
346 return (0);
347 }
348 ifr = ifc.ifc_req;
349 for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
350 for (np = neighbors; np != NULL; np = np->n_next)
351 if (np->n_name &&
352 strcmp(ifr->ifr_name, np->n_name) == 0)
353 break;
354 if (np != NULL)
355 continue;
356 ifreq = *ifr;
357 np = (struct neighbor *)malloc(sizeof (*np));
358 if (np == NULL)
359 continue;
360 np->n_name = malloc(strlen(ifr->ifr_name) + 1);
361 if (np->n_name == NULL) {
362 free((char *)np);
363 continue;
364 }
365 strcpy(np->n_name, ifr->ifr_name);
366 np->n_addrlen = sizeof (ifr->ifr_addr);
367 np->n_addr = malloc(np->n_addrlen);
368 if (np->n_addr == NULL) {
369 free(np->n_name);
370 free((char *)np);
371 continue;
372 }
373 bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
374 if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
375 syslog(LOG_ERR, "ioctl (get interface flags)");
376 free((char *)np);
377 continue;
378 }
379 if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
380 free((char *)np);
381 continue;
382 }
383 np->n_flags = ifreq.ifr_flags;
384 if (np->n_flags & IFF_POINTOPOINT) {
385 if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
386 syslog(LOG_ERR, "ioctl (get dstaddr)");
387 free((char *)np);
388 continue;
389 }
390 /* we assume addresses are all the same size */
391 bcopy((char *)&ifreq.ifr_dstaddr,
392 np->n_addr, np->n_addrlen);
393 }
394 if (np->n_flags & IFF_BROADCAST) {
395 /* we assume addresses are all the same size */
396 sin = (struct sockaddr_in *)np->n_addr;
397 sin->sin_addr = inet_makeaddr((np->n_flags & IFF_LOCAL)?
398 inet_subnetof(sin->sin_addr) :
399 inet_netof(sin->sin_addr),
400 INADDR_ANY);
401 }
402 /* gag, wish we could get rid of Internet dependencies */
403 sin = (struct sockaddr_in *)np->n_addr;
404 sin->sin_port = sp->s_port;
405 np->n_next = neighbors;
406 neighbors = np;
407 }
408 return (1);
409}
410
411/*
412 * Return the possible subnetwork number from an internet address.
413 * If the address is of the form of a subnet address (most significant
414 * bit of the host part is set), believe the subnet exists.
415 * Otherwise, return the network number. Any subnet number is only valid
416 * if this is a ``local'' net.
417 */
418inet_subnetof(in)
419 struct in_addr in;
420{
421 register u_long i = ntohl(in.s_addr);
422
423 if (IN_CLASSA(i)) {
424 if (IN_SUBNETA(i))
425 return ((i & IN_CLASSA_SUBNET) >> IN_CLASSA_SUBNSHIFT);
426 else
427 return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
428 } else if (IN_CLASSB(i)) {
429 if (IN_SUBNETB(i))
430 return ((i & IN_CLASSB_SUBNET) >> IN_CLASSB_SUBNSHIFT);
431 else
432 return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
433 } else
434 return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
435}
436
437#ifdef DEBUG
438sendto(s, buf, cc, flags, to, tolen)
439 int s;
440 char *buf;
441 int cc, flags;
442 char *to;
443 int tolen;
444{
445 register struct whod *w = (struct whod *)buf;
446 register struct whoent *we;
447 struct sockaddr_in *sin = (struct sockaddr_in *)to;
448 char *interval();
449
450 printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
451 printf("hostname %s %s\n", w->wd_hostname,
452 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
453 printf("load %4.2f, %4.2f, %4.2f\n",
454 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
455 ntohl(w->wd_loadav[2]) / 100.0);
456 cc -= WHDRSIZE;
457 for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
458 time_t t = ntohl(we->we_utmp.out_time);
459 printf("%-8.8s %s:%s %.12s",
460 we->we_utmp.out_name,
461 w->wd_hostname, we->we_utmp.out_line,
462 ctime(&t)+4);
463 we->we_idle = ntohl(we->we_idle) / 60;
464 if (we->we_idle) {
465 if (we->we_idle >= 100*60)
466 we->we_idle = 100*60 - 1;
467 if (we->we_idle >= 60)
468 printf(" %2d", we->we_idle / 60);
469 else
470 printf(" ");
471 printf(":%02d", we->we_idle % 60);
472 }
473 printf("\n");
474 }
475}
476
477char *
478interval(time, updown)
479 int time;
480 char *updown;
481{
482 static char resbuf[32];
483 int days, hours, minutes;
484
485 if (time < 0 || time > 3*30*24*60*60) {
486 (void) sprintf(resbuf, " %s ??:??", updown);
487 return (resbuf);
488 }
489 minutes = (time + 59) / 60; /* round to minutes */
490 hours = minutes / 60; minutes %= 60;
491 days = hours / 24; hours %= 24;
492 if (days)
493 (void) sprintf(resbuf, "%s %2d+%02d:%02d",
494 updown, days, hours, minutes);
495 else
496 (void) sprintf(resbuf, "%s %2d:%02d",
497 updown, hours, minutes);
498 return (resbuf);
499}
500#endif