Add copyright
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
CommitLineData
52b4fb0c 1#ifndef lint
e48cb7b7 2static char sccsid[] = "@(#)rwhod.c 4.28 (Berkeley) 85/02/25";
52b4fb0c
BJ
3#endif
4
52b4fb0c 5#include <sys/types.h>
52b4fb0c 6#include <sys/socket.h>
52b4fb0c 7#include <sys/stat.h>
52b4fb0c 8#include <sys/ioctl.h>
9bd66dcf 9#include <sys/file.h>
de3b21e8 10
7af17b3e 11#include <net/if.h>
de3b21e8
SL
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>
a0f59ac1
SL
19#include <ctype.h>
20#include <netdb.h>
00cdc205 21#include <syslog.h>
86fcf481 22#include "rwhod.h"
de3b21e8 23
83361bb3
RC
24/*
25 * Alarm interval. Don't forget to change the down time check in ruptime
26 * if this is changed.
27 */
00cdc205 28#define AL_INTERVAL (3 * 60)
e48cb7b7 29#define MAXTTYS 256 /* Max # of utmp entries examined */
83361bb3 30
a0f59ac1 31struct sockaddr_in sin = { AF_INET };
52b4fb0c
BJ
32
33extern errno;
34
7af17b3e 35char myname[32];
52b4fb0c
BJ
36
37struct nlist nl[] = {
38#define NL_AVENRUN 0
39 { "_avenrun" },
de3b21e8
SL
40#define NL_BOOTTIME 1
41 { "_boottime" },
52b4fb0c
BJ
42 0
43};
44
7af17b3e
SL
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;
52b4fb0c 60struct whod mywd;
7af17b3e 61struct servent *sp;
52b4fb0c
BJ
62int s, utmpf, kmemf = -1;
63
7af17b3e
SL
64#define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we))
65#define RWHODIR "/usr/spool/rwho"
fea387f0 66
52b4fb0c 67int onalrm();
7af17b3e 68char *strcpy(), *sprintf(), *malloc();
52b4fb0c
BJ
69long lseek();
70int getkmem();
7af17b3e 71struct in_addr inet_makeaddr();
52b4fb0c
BJ
72
73main()
74{
75 struct sockaddr_in from;
76 char path[64];
3de1986a 77 int addr, on = 1;
7af17b3e 78 struct hostent *hp;
52b4fb0c 79
00cdc205
RC
80 if (getuid()) {
81 fprintf(stderr, "rwhod: not super user\n");
82 exit(1);
83 }
a0f59ac1
SL
84 sp = getservbyname("who", "udp");
85 if (sp == 0) {
86 fprintf(stderr, "rwhod: udp/who: unknown service\n");
87 exit(1);
88 }
52b4fb0c
BJ
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);
00cdc205 107 openlog("rwhod", LOG_PID, 0);
7af17b3e
SL
108 /*
109 * Establish host name as returned by system.
110 */
111 if (gethostname(myname, sizeof (myname) - 1) < 0) {
00cdc205 112 syslog(LOG_ERR, "gethostname: %m");
52b4fb0c
BJ
113 exit(1);
114 }
7af17b3e 115 strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
9bd66dcf 116 utmpf = open("/etc/utmp", O_RDONLY);
52b4fb0c
BJ
117 if (utmpf < 0) {
118 (void) close(creat("/etc/utmp", 0644));
9bd66dcf 119 utmpf = open("/etc/utmp", O_RDONLY);
52b4fb0c
BJ
120 }
121 if (utmpf < 0) {
00cdc205 122 syslog(LOG_ERR, "/etc/utmp: %m");
52b4fb0c
BJ
123 exit(1);
124 }
125 getkmem();
9bd66dcf 126 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00cdc205 127 syslog(LOG_ERR, "socket: %m");
de3b21e8
SL
128 exit(1);
129 }
3de1986a 130 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
b0e1e5f3
MK
131 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
132 exit(1);
133 }
7af17b3e
SL
134 hp = gethostbyname(myname);
135 if (hp == NULL) {
00cdc205 136 syslog(LOG_ERR, "%s: don't know my own name", myname);
7af17b3e
SL
137 exit(1);
138 }
139 sin.sin_family = hp->h_addrtype;
140 sin.sin_port = sp->s_port;
9bd66dcf 141 if (bind(s, &sin, sizeof (sin)) < 0) {
00cdc205 142 syslog(LOG_ERR, "bind: %m");
de3b21e8 143 exit(1);
52b4fb0c 144 }
7af17b3e
SL
145 if (!configure(s))
146 exit(1);
8a53982e 147 signal(SIGALRM, onalrm);
52b4fb0c
BJ
148 onalrm();
149 for (;;) {
150 struct whod wd;
7af17b3e 151 int cc, whod, len = sizeof (from);
52b4fb0c 152
7af17b3e
SL
153 cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
154 &from, &len);
52b4fb0c
BJ
155 if (cc <= 0) {
156 if (cc < 0 && errno != EINTR)
00cdc205 157 syslog(LOG_WARNING, "recv: %m");
52b4fb0c
BJ
158 continue;
159 }
a0f59ac1 160 if (from.sin_port != sp->s_port) {
00cdc205 161 syslog(LOG_WARNING, "%d: bad from port",
a0f59ac1 162 ntohs(from.sin_port));
52b4fb0c
BJ
163 continue;
164 }
a0f59ac1
SL
165#ifdef notdef
166 if (gethostbyname(wd.wd_hostname) == 0) {
00cdc205 167 syslog(LOG_WARNING, "%s: unknown host",
a0f59ac1
SL
168 wd.wd_hostname);
169 continue;
170 }
171#endif
aaddf8a1
SL
172 if (wd.wd_vers != WHODVERSION)
173 continue;
7af17b3e
SL
174 if (wd.wd_type != WHODTYPE_STATUS)
175 continue;
a0f59ac1 176 if (!verify(wd.wd_hostname)) {
00cdc205 177 syslog(LOG_WARNING, "malformed host name from %x",
a0f59ac1 178 from.sin_addr);
52b4fb0c
BJ
179 continue;
180 }
a82aa56a 181 (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
52b4fb0c
BJ
182 whod = creat(path, 0666);
183 if (whod < 0) {
00cdc205 184 syslog(LOG_WARNING, "%s: %m", path);
52b4fb0c
BJ
185 continue;
186 }
fea387f0
SL
187#if vax || pdp11
188 {
9bd66dcf 189 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
fea387f0
SL
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);
1cccbcb3
SL
200 we->we_utmp.out_time =
201 ntohl(we->we_utmp.out_time);
fea387f0
SL
202 we++;
203 }
204 }
205#endif
52b4fb0c
BJ
206 (void) time(&wd.wd_recvtime);
207 (void) write(whod, (char *)&wd, cc);
208 (void) close(whod);
209 }
210}
211
a0f59ac1
SL
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) {
77650176 223 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
a0f59ac1
SL
224 return (0);
225 name++, size++;
226 }
227 return (size > 0);
228}
229
52b4fb0c
BJ
230int utmptime;
231int utmpent;
e48cb7b7 232struct utmp utmp[MAXTTYS];
52b4fb0c
BJ
233int alarmcount;
234
235onalrm()
236{
237 register int i;
238 struct stat stb;
76886f32 239 register struct whoent *we = mywd.wd_we, *wlast;
52b4fb0c
BJ
240 int cc;
241 double avenrun[3];
242 time_t now = time(0);
7af17b3e 243 register struct neighbor *np;
52b4fb0c
BJ
244
245 if (alarmcount % 10 == 0)
246 getkmem();
247 alarmcount++;
248 (void) fstat(utmpf, &stb);
249 if (stb.st_mtime != utmptime) {
02d31241 250 utmptime = stb.st_mtime;
9bd66dcf 251 (void) lseek(utmpf, (long)0, L_SET);
52b4fb0c
BJ
252 cc = read(utmpf, (char *)utmp, sizeof (utmp));
253 if (cc < 0) {
254 perror("/etc/utmp");
08d10f57 255 goto done;
52b4fb0c 256 }
7af17b3e 257 wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
52b4fb0c
BJ
258 utmpent = cc / sizeof (struct utmp);
259 for (i = 0; i < utmpent; i++)
260 if (utmp[i].ut_name[0]) {
b1456c48
SL
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));
b1456c48 265 we->we_utmp.out_time = htonl(utmp[i].ut_time);
76886f32
SL
266 if (we >= wlast)
267 break;
52b4fb0c
BJ
268 we++;
269 }
270 utmpent = we - mywd.wd_we;
271 }
272 we = mywd.wd_we;
273 for (i = 0; i < utmpent; i++) {
b1456c48 274 if (stat(we->we_utmp.out_line, &stb) >= 0)
b1456c48 275 we->we_idle = htonl(now - stb.st_atime);
52b4fb0c
BJ
276 we++;
277 }
9bd66dcf 278 (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
52b4fb0c
BJ
279 (void) read(kmemf, (char *)avenrun, sizeof (avenrun));
280 for (i = 0; i < 3; i++)
353c1281 281 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
b1456c48 282 cc = (char *)we - (char *)&mywd;
b1456c48 283 mywd.wd_sendtime = htonl(time(0));
7af17b3e
SL
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);
08d10f57 289done:
83361bb3 290 (void) alarm(AL_INTERVAL);
52b4fb0c
BJ
291}
292
293getkmem()
294{
295 struct nlist *nlp;
07cac863
SL
296 static ino_t vmunixino;
297 static time_t vmunixctime;
298 struct stat sb;
52b4fb0c 299
07cac863
SL
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 }
52b4fb0c
BJ
309 if (kmemf >= 0)
310 (void) close(kmemf);
311loop:
00cdc205
RC
312 if (nlist("/vmunix", nl)) {
313 syslog(LOG_WARNING, "/vmunix namelist botch");
52b4fb0c
BJ
314 sleep(300);
315 goto loop;
316 }
9bd66dcf 317 kmemf = open("/dev/kmem", O_RDONLY);
52b4fb0c 318 if (kmemf < 0) {
00cdc205
RC
319 syslog(LOG_ERR, "/dev/kmem: %m");
320 exit(1);
52b4fb0c 321 }
9bd66dcf
SL
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));
b1456c48 325 mywd.wd_boottime = htonl(mywd.wd_boottime);
52b4fb0c 326}
7af17b3e
SL
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;
7af17b3e
SL
338 struct sockaddr_in *sin;
339 register struct neighbor *np;
9bd66dcf 340 int n;
7af17b3e
SL
341
342 ifc.ifc_len = sizeof (buf);
343 ifc.ifc_buf = buf;
344 if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
00cdc205 345 syslog(LOG_ERR, "ioctl (get interface configuration)");
7af17b3e
SL
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) {
00cdc205 375 syslog(LOG_ERR, "ioctl (get interface flags)");
7af17b3e
SL
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) {
00cdc205 386 syslog(LOG_ERR, "ioctl (get dstaddr)");
7af17b3e
SL
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;
8e50836a
MK
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);
7af17b3e
SL
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}
b1456c48 410
8e50836a
MK
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
b1456c48
SL
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,
9bd66dcf 452 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
b1456c48 453 printf("load %4.2f, %4.2f, %4.2f\n",
9bd66dcf
SL
454 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
455 ntohl(w->wd_loadav[2]) / 100.0);
b1456c48
SL
456 cc -= WHDRSIZE;
457 for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
9bd66dcf 458 time_t t = ntohl(we->we_utmp.out_time);
b1456c48 459 printf("%-8.8s %s:%s %.12s",
9bd66dcf
SL
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;
b1456c48
SL
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