BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
CommitLineData
3b86b0f6 1/*
86f08f99
KB
2 * Copyright (c) 1983 The Regents of the University of California.
3 * All rights reserved.
4 *
af359dea
C
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
3b86b0f6
DF
32 */
33
34#ifndef lint
35char copyright[] =
86f08f99 36"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
3b86b0f6 37 All rights reserved.\n";
86f08f99 38#endif /* not lint */
3b86b0f6 39
52b4fb0c 40#ifndef lint
af359dea 41static char sccsid[] = "@(#)rwhod.c 5.20 (Berkeley) 3/2/91";
86f08f99 42#endif /* not lint */
52b4fb0c 43
b584098b 44#include <sys/param.h>
52b4fb0c 45#include <sys/socket.h>
52b4fb0c 46#include <sys/stat.h>
17ccb086 47#include <sys/signal.h>
52b4fb0c 48#include <sys/ioctl.h>
9bd66dcf 49#include <sys/file.h>
de3b21e8 50
7af17b3e 51#include <net/if.h>
de3b21e8
SL
52#include <netinet/in.h>
53
54#include <nlist.h>
de3b21e8
SL
55#include <errno.h>
56#include <utmp.h>
a0f59ac1
SL
57#include <ctype.h>
58#include <netdb.h>
00cdc205 59#include <syslog.h>
3c13756b 60#include <protocols/rwhod.h>
17ccb086 61#include <stdio.h>
7abf8d65 62#include <paths.h>
de3b21e8 63
83361bb3
RC
64/*
65 * Alarm interval. Don't forget to change the down time check in ruptime
66 * if this is changed.
67 */
00cdc205 68#define AL_INTERVAL (3 * 60)
83361bb3 69
b584098b 70struct sockaddr_in sin;
52b4fb0c 71
b584098b 72char myname[MAXHOSTNAMELEN];
52b4fb0c
BJ
73
74struct nlist nl[] = {
4b8ee582 75#define NL_BOOTTIME 0
de3b21e8 76 { "_boottime" },
52b4fb0c
BJ
77 0
78};
79
7af17b3e
SL
80/*
81 * We communicate with each neighbor in
82 * a list constructed at the time we're
83 * started up. Neighbors are currently
84 * directly connected via a hardware interface.
85 */
86struct neighbor {
87 struct neighbor *n_next;
88 char *n_name; /* interface name */
89 char *n_addr; /* who to send to */
90 int n_addrlen; /* size of address */
91 int n_flags; /* should forward?, interface flags */
92};
93
94struct neighbor *neighbors;
52b4fb0c 95struct whod mywd;
7af17b3e 96struct servent *sp;
52b4fb0c
BJ
97int s, utmpf, kmemf = -1;
98
7af17b3e 99#define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we))
fea387f0 100
17ccb086 101extern int errno;
9bd38ba8 102char *strcpy(), *malloc();
52b4fb0c 103long lseek();
0df908a9 104void getkmem(), onalrm();
7af17b3e 105struct in_addr inet_makeaddr();
52b4fb0c
BJ
106
107main()
108{
109 struct sockaddr_in from;
0f2d5e68 110 struct stat st;
52b4fb0c 111 char path[64];
221be169 112 int on = 1;
17ccb086 113 char *cp, *index(), *strerror();
52b4fb0c 114
00cdc205
RC
115 if (getuid()) {
116 fprintf(stderr, "rwhod: not super user\n");
117 exit(1);
118 }
a0f59ac1
SL
119 sp = getservbyname("who", "udp");
120 if (sp == 0) {
121 fprintf(stderr, "rwhod: udp/who: unknown service\n");
122 exit(1);
123 }
52b4fb0c 124#ifndef DEBUG
afe907a4 125 daemon(1, 0);
52b4fb0c 126#endif
17ccb086
KB
127 if (chdir(_PATH_RWHODIR) < 0) {
128 (void)fprintf(stderr, "rwhod: %s: %s\n",
129 _PATH_RWHODIR, strerror(errno));
ac82a1ec
KM
130 exit(1);
131 }
52b4fb0c 132 (void) signal(SIGHUP, getkmem);
076ae92c 133 openlog("rwhod", LOG_PID, LOG_DAEMON);
7af17b3e
SL
134 /*
135 * Establish host name as returned by system.
136 */
137 if (gethostname(myname, sizeof (myname) - 1) < 0) {
00cdc205 138 syslog(LOG_ERR, "gethostname: %m");
52b4fb0c
BJ
139 exit(1);
140 }
d2444544
JB
141 if ((cp = index(myname, '.')) != NULL)
142 *cp = '\0';
7af17b3e 143 strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
17ccb086 144 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
52b4fb0c 145 if (utmpf < 0) {
17ccb086 146 syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
52b4fb0c
BJ
147 exit(1);
148 }
149 getkmem();
9bd66dcf 150 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00cdc205 151 syslog(LOG_ERR, "socket: %m");
de3b21e8
SL
152 exit(1);
153 }
3de1986a 154 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
b0e1e5f3
MK
155 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
156 exit(1);
157 }
b584098b 158 sin.sin_family = AF_INET;
7af17b3e 159 sin.sin_port = sp->s_port;
0df908a9 160 if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
00cdc205 161 syslog(LOG_ERR, "bind: %m");
de3b21e8 162 exit(1);
52b4fb0c 163 }
7af17b3e
SL
164 if (!configure(s))
165 exit(1);
8a53982e 166 signal(SIGALRM, onalrm);
52b4fb0c
BJ
167 onalrm();
168 for (;;) {
169 struct whod wd;
7af17b3e 170 int cc, whod, len = sizeof (from);
52b4fb0c 171
7af17b3e 172 cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
0df908a9 173 (struct sockaddr *)&from, &len);
52b4fb0c
BJ
174 if (cc <= 0) {
175 if (cc < 0 && errno != EINTR)
00cdc205 176 syslog(LOG_WARNING, "recv: %m");
52b4fb0c
BJ
177 continue;
178 }
a0f59ac1 179 if (from.sin_port != sp->s_port) {
00cdc205 180 syslog(LOG_WARNING, "%d: bad from port",
a0f59ac1 181 ntohs(from.sin_port));
52b4fb0c
BJ
182 continue;
183 }
aaddf8a1
SL
184 if (wd.wd_vers != WHODVERSION)
185 continue;
7af17b3e
SL
186 if (wd.wd_type != WHODTYPE_STATUS)
187 continue;
a0f59ac1 188 if (!verify(wd.wd_hostname)) {
00cdc205 189 syslog(LOG_WARNING, "malformed host name from %x",
a0f59ac1 190 from.sin_addr);
52b4fb0c
BJ
191 continue;
192 }
ac82a1ec 193 (void) sprintf(path, "whod.%s", wd.wd_hostname);
0f2d5e68
MK
194 /*
195 * Rather than truncating and growing the file each time,
196 * use ftruncate if size is less than previous size.
197 */
198 whod = open(path, O_WRONLY | O_CREAT, 0644);
52b4fb0c 199 if (whod < 0) {
00cdc205 200 syslog(LOG_WARNING, "%s: %m", path);
52b4fb0c
BJ
201 continue;
202 }
b584098b 203#if ENDIAN != BIG_ENDIAN
fea387f0 204 {
9bd66dcf 205 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
fea387f0
SL
206 struct whoent *we;
207
208 /* undo header byte swapping before writing to file */
209 wd.wd_sendtime = ntohl(wd.wd_sendtime);
210 for (i = 0; i < 3; i++)
211 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
212 wd.wd_boottime = ntohl(wd.wd_boottime);
213 we = wd.wd_we;
214 for (i = 0; i < n; i++) {
215 we->we_idle = ntohl(we->we_idle);
1cccbcb3
SL
216 we->we_utmp.out_time =
217 ntohl(we->we_utmp.out_time);
fea387f0
SL
218 we++;
219 }
220 }
221#endif
0df908a9 222 (void) time((time_t *)&wd.wd_recvtime);
52b4fb0c 223 (void) write(whod, (char *)&wd, cc);
0f2d5e68
MK
224 if (fstat(whod, &st) < 0 || st.st_size > cc)
225 ftruncate(whod, cc);
52b4fb0c
BJ
226 (void) close(whod);
227 }
228}
229
a0f59ac1
SL
230/*
231 * Check out host name for unprintables
232 * and other funnies before allowing a file
233 * to be created. Sorry, but blanks aren't allowed.
234 */
235verify(name)
236 register char *name;
237{
238 register int size = 0;
239
240 while (*name) {
77650176 241 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
a0f59ac1
SL
242 return (0);
243 name++, size++;
244 }
245 return (size > 0);
246}
247
52b4fb0c
BJ
248int utmptime;
249int utmpent;
4c9b3acd
JB
250int utmpsize = 0;
251struct utmp *utmp;
52b4fb0c
BJ
252int alarmcount;
253
0df908a9 254void
52b4fb0c
BJ
255onalrm()
256{
17ccb086
KB
257 register struct neighbor *np;
258 register struct whoent *we = mywd.wd_we, *wlast;
52b4fb0c
BJ
259 register int i;
260 struct stat stb;
52b4fb0c
BJ
261 int cc;
262 double avenrun[3];
17ccb086
KB
263 time_t now = time((time_t *)NULL);
264 char *strerror();
52b4fb0c
BJ
265
266 if (alarmcount % 10 == 0)
267 getkmem();
268 alarmcount++;
269 (void) fstat(utmpf, &stb);
4c9b3acd 270 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
02d31241 271 utmptime = stb.st_mtime;
4c9b3acd
JB
272 if (stb.st_size > utmpsize) {
273 utmpsize = stb.st_size + 10 * sizeof(struct utmp);
274 if (utmp)
275 utmp = (struct utmp *)realloc(utmp, utmpsize);
276 else
277 utmp = (struct utmp *)malloc(utmpsize);
278 if (! utmp) {
279 fprintf(stderr, "rwhod: malloc failed\n");
280 utmpsize = 0;
281 goto done;
282 }
283 }
9bd66dcf 284 (void) lseek(utmpf, (long)0, L_SET);
4c9b3acd 285 cc = read(utmpf, (char *)utmp, stb.st_size);
52b4fb0c 286 if (cc < 0) {
17ccb086
KB
287 fprintf(stderr, "rwhod: %s: %s\n",
288 _PATH_UTMP, strerror(errno));
08d10f57 289 goto done;
52b4fb0c 290 }
7af17b3e 291 wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
52b4fb0c
BJ
292 utmpent = cc / sizeof (struct utmp);
293 for (i = 0; i < utmpent; i++)
294 if (utmp[i].ut_name[0]) {
b1456c48
SL
295 bcopy(utmp[i].ut_line, we->we_utmp.out_line,
296 sizeof (utmp[i].ut_line));
297 bcopy(utmp[i].ut_name, we->we_utmp.out_name,
298 sizeof (utmp[i].ut_name));
b1456c48 299 we->we_utmp.out_time = htonl(utmp[i].ut_time);
76886f32
SL
300 if (we >= wlast)
301 break;
52b4fb0c
BJ
302 we++;
303 }
304 utmpent = we - mywd.wd_we;
305 }
221be169
KM
306
307 /*
308 * The test on utmpent looks silly---after all, if no one is
309 * logged on, why worry about efficiency?---but is useful on
310 * (e.g.) compute servers.
311 */
7abf8d65
KB
312 if (utmpent && chdir(_PATH_DEV)) {
313 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
221be169
KM
314 exit(1);
315 }
52b4fb0c
BJ
316 we = mywd.wd_we;
317 for (i = 0; i < utmpent; i++) {
221be169 318 if (stat(we->we_utmp.out_line, &stb) >= 0)
b1456c48 319 we->we_idle = htonl(now - stb.st_atime);
52b4fb0c
BJ
320 we++;
321 }
4b8ee582 322 (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
52b4fb0c 323 for (i = 0; i < 3; i++)
353c1281 324 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
b1456c48 325 cc = (char *)we - (char *)&mywd;
b1456c48 326 mywd.wd_sendtime = htonl(time(0));
7af17b3e
SL
327 mywd.wd_vers = WHODVERSION;
328 mywd.wd_type = WHODTYPE_STATUS;
329 for (np = neighbors; np != NULL; np = np->n_next)
330 (void) sendto(s, (char *)&mywd, cc, 0,
0df908a9 331 (struct sockaddr *)np->n_addr, np->n_addrlen);
17ccb086
KB
332 if (utmpent && chdir(_PATH_RWHODIR)) {
333 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
221be169
KM
334 exit(1);
335 }
08d10f57 336done:
83361bb3 337 (void) alarm(AL_INTERVAL);
52b4fb0c
BJ
338}
339
0df908a9 340void
52b4fb0c
BJ
341getkmem()
342{
07cac863
SL
343 static ino_t vmunixino;
344 static time_t vmunixctime;
345 struct stat sb;
52b4fb0c 346
17ccb086 347 if (stat(_PATH_UNIX, &sb) < 0) {
07cac863
SL
348 if (vmunixctime)
349 return;
350 } else {
351 if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
352 return;
353 vmunixctime = sb.st_ctime;
354 vmunixino= sb.st_ino;
355 }
52b4fb0c
BJ
356 if (kmemf >= 0)
357 (void) close(kmemf);
358loop:
17ccb086
KB
359 if (nlist(_PATH_UNIX, nl)) {
360 syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX);
52b4fb0c
BJ
361 sleep(300);
362 goto loop;
363 }
17ccb086 364 kmemf = open(_PATH_KMEM, O_RDONLY, 0);
52b4fb0c 365 if (kmemf < 0) {
17ccb086 366 syslog(LOG_ERR, "%s: %m", _PATH_KMEM);
00cdc205 367 exit(1);
52b4fb0c 368 }
9bd66dcf
SL
369 (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
370 (void) read(kmemf, (char *)&mywd.wd_boottime,
371 sizeof (mywd.wd_boottime));
b1456c48 372 mywd.wd_boottime = htonl(mywd.wd_boottime);
52b4fb0c 373}
7af17b3e
SL
374
375/*
376 * Figure out device configuration and select
377 * networks which deserve status information.
378 */
379configure(s)
380 int s;
381{
25a88e81 382 char buf[BUFSIZ], *cp, *cplim;
7af17b3e
SL
383 struct ifconf ifc;
384 struct ifreq ifreq, *ifr;
7af17b3e
SL
385 struct sockaddr_in *sin;
386 register struct neighbor *np;
387
388 ifc.ifc_len = sizeof (buf);
389 ifc.ifc_buf = buf;
390 if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
00cdc205 391 syslog(LOG_ERR, "ioctl (get interface configuration)");
7af17b3e
SL
392 return (0);
393 }
394 ifr = ifc.ifc_req;
d4a5bf04 395#ifdef AF_LINK
25a88e81
KS
396#define max(a, b) (a > b ? a : b)
397#define size(p) max((p).sa_len, sizeof(p))
398#else
399#define size(p) (sizeof (p))
400#endif
401 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
402 for (cp = buf; cp < cplim;
403 cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
404 ifr = (struct ifreq *)cp;
7af17b3e
SL
405 for (np = neighbors; np != NULL; np = np->n_next)
406 if (np->n_name &&
407 strcmp(ifr->ifr_name, np->n_name) == 0)
408 break;
409 if (np != NULL)
410 continue;
411 ifreq = *ifr;
412 np = (struct neighbor *)malloc(sizeof (*np));
413 if (np == NULL)
414 continue;
415 np->n_name = malloc(strlen(ifr->ifr_name) + 1);
416 if (np->n_name == NULL) {
417 free((char *)np);
418 continue;
419 }
420 strcpy(np->n_name, ifr->ifr_name);
421 np->n_addrlen = sizeof (ifr->ifr_addr);
422 np->n_addr = malloc(np->n_addrlen);
423 if (np->n_addr == NULL) {
424 free(np->n_name);
425 free((char *)np);
426 continue;
427 }
428 bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
429 if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
00cdc205 430 syslog(LOG_ERR, "ioctl (get interface flags)");
7af17b3e
SL
431 free((char *)np);
432 continue;
433 }
64b7ee70
MK
434 if ((ifreq.ifr_flags & IFF_UP) == 0 ||
435 (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
7af17b3e
SL
436 free((char *)np);
437 continue;
438 }
439 np->n_flags = ifreq.ifr_flags;
440 if (np->n_flags & IFF_POINTOPOINT) {
441 if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
00cdc205 442 syslog(LOG_ERR, "ioctl (get dstaddr)");
7af17b3e
SL
443 free((char *)np);
444 continue;
445 }
446 /* we assume addresses are all the same size */
447 bcopy((char *)&ifreq.ifr_dstaddr,
448 np->n_addr, np->n_addrlen);
449 }
450 if (np->n_flags & IFF_BROADCAST) {
64b7ee70
MK
451 if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
452 syslog(LOG_ERR, "ioctl (get broadaddr)");
453 free((char *)np);
454 continue;
455 }
7af17b3e 456 /* we assume addresses are all the same size */
64b7ee70
MK
457 bcopy((char *)&ifreq.ifr_broadaddr,
458 np->n_addr, np->n_addrlen);
7af17b3e
SL
459 }
460 /* gag, wish we could get rid of Internet dependencies */
461 sin = (struct sockaddr_in *)np->n_addr;
462 sin->sin_port = sp->s_port;
463 np->n_next = neighbors;
464 neighbors = np;
465 }
466 return (1);
467}
b1456c48
SL
468
469#ifdef DEBUG
470sendto(s, buf, cc, flags, to, tolen)
471 int s;
472 char *buf;
473 int cc, flags;
474 char *to;
475 int tolen;
476{
477 register struct whod *w = (struct whod *)buf;
478 register struct whoent *we;
479 struct sockaddr_in *sin = (struct sockaddr_in *)to;
480 char *interval();
481
482 printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
483 printf("hostname %s %s\n", w->wd_hostname,
9bd66dcf 484 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
b1456c48 485 printf("load %4.2f, %4.2f, %4.2f\n",
9bd66dcf
SL
486 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
487 ntohl(w->wd_loadav[2]) / 100.0);
b1456c48
SL
488 cc -= WHDRSIZE;
489 for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
9bd66dcf 490 time_t t = ntohl(we->we_utmp.out_time);
b1456c48 491 printf("%-8.8s %s:%s %.12s",
9bd66dcf
SL
492 we->we_utmp.out_name,
493 w->wd_hostname, we->we_utmp.out_line,
494 ctime(&t)+4);
495 we->we_idle = ntohl(we->we_idle) / 60;
b1456c48
SL
496 if (we->we_idle) {
497 if (we->we_idle >= 100*60)
498 we->we_idle = 100*60 - 1;
499 if (we->we_idle >= 60)
500 printf(" %2d", we->we_idle / 60);
501 else
502 printf(" ");
503 printf(":%02d", we->we_idle % 60);
504 }
505 printf("\n");
506 }
507}
508
509char *
510interval(time, updown)
511 int time;
512 char *updown;
513{
514 static char resbuf[32];
515 int days, hours, minutes;
516
517 if (time < 0 || time > 3*30*24*60*60) {
518 (void) sprintf(resbuf, " %s ??:??", updown);
519 return (resbuf);
520 }
521 minutes = (time + 59) / 60; /* round to minutes */
522 hours = minutes / 60; minutes %= 60;
523 days = hours / 24; hours %= 24;
524 if (days)
525 (void) sprintf(resbuf, "%s %2d+%02d:%02d",
526 updown, days, hours, minutes);
527 else
528 (void) sprintf(resbuf, "%s %2d:%02d",
529 updown, hours, minutes);
530 return (resbuf);
531}
532#endif