add confSORT_QUEUE_BY_HOST and confBROKEN_SMTP_PEERS
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
CommitLineData
3b86b0f6 1/*
a34f5dc0
KB
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
86f08f99 4 *
417f7a11 5 * %sccs.include.redist.c%
3b86b0f6
DF
6 */
7
8#ifndef lint
a34f5dc0
KB
9static char copyright[] =
10"@(#) Copyright (c) 1983, 1993\n\
11 The Regents of the University of California. All rights reserved.\n";
86f08f99 12#endif /* not lint */
3b86b0f6 13
52b4fb0c 14#ifndef lint
a34f5dc0 15static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) %G%";
86f08f99 16#endif /* not lint */
52b4fb0c 17
b584098b 18#include <sys/param.h>
52b4fb0c 19#include <sys/socket.h>
52b4fb0c 20#include <sys/stat.h>
17ccb086 21#include <sys/signal.h>
52b4fb0c 22#include <sys/ioctl.h>
d605e71a 23#include <sys/sysctl.h>
de3b21e8 24
7af17b3e 25#include <net/if.h>
0892992b
KS
26#include <net/if_dl.h>
27#include <net/route.h>
de3b21e8 28#include <netinet/in.h>
55ae2764 29#include <protocols/rwhod.h>
de3b21e8 30
a0f59ac1 31#include <ctype.h>
55ae2764
KB
32#include <errno.h>
33#include <fcntl.h>
a0f59ac1 34#include <netdb.h>
55ae2764 35#include <paths.h>
17ccb086 36#include <stdio.h>
55ae2764
KB
37#include <stdlib.h>
38#include <string.h>
39#include <syslog.h>
0f756990 40#include <unistd.h>
55ae2764 41#include <utmp.h>
de3b21e8 42
83361bb3
RC
43/*
44 * Alarm interval. Don't forget to change the down time check in ruptime
45 * if this is changed.
46 */
00cdc205 47#define AL_INTERVAL (3 * 60)
83361bb3 48
b584098b 49char myname[MAXHOSTNAMELEN];
52b4fb0c 50
7af17b3e 51/*
55ae2764
KB
52 * We communicate with each neighbor in a list constructed at the time we're
53 * started up. Neighbors are currently directly connected via a hardware
54 * interface.
7af17b3e
SL
55 */
56struct neighbor {
57 struct neighbor *n_next;
58 char *n_name; /* interface name */
0892992b 59 struct sockaddr *n_addr; /* who to send to */
7af17b3e
SL
60 int n_addrlen; /* size of address */
61 int n_flags; /* should forward?, interface flags */
62};
63
64struct neighbor *neighbors;
52b4fb0c 65struct whod mywd;
7af17b3e 66struct servent *sp;
cf498703 67int s, utmpf;
52b4fb0c 68
5115d8df 69#define WHDRSIZE (sizeof(mywd) - sizeof(mywd.wd_we))
fea387f0 70
55ae2764 71int configure __P((int));
cf498703 72void getboottime __P((int));
55ae2764
KB
73void onalrm __P((int));
74void quit __P((char *));
75void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
76int verify __P((char *));
77#ifdef DEBUG
78char *interval __P((int, char *));
5115d8df
CT
79void Sendto __P((int, char *, int, int, char *, int));
80#define sendto Sendto
55ae2764 81#endif
52b4fb0c 82
55ae2764
KB
83int
84main(argc, argv)
85 int argc;
86 char argv[];
52b4fb0c
BJ
87{
88 struct sockaddr_in from;
0f2d5e68 89 struct stat st;
52b4fb0c 90 char path[64];
221be169 91 int on = 1;
55ae2764 92 char *cp;
5115d8df 93 struct sockaddr_in sin;
52b4fb0c 94
00cdc205
RC
95 if (getuid()) {
96 fprintf(stderr, "rwhod: not super user\n");
97 exit(1);
98 }
a0f59ac1 99 sp = getservbyname("who", "udp");
55ae2764 100 if (sp == NULL) {
a0f59ac1
SL
101 fprintf(stderr, "rwhod: udp/who: unknown service\n");
102 exit(1);
103 }
52b4fb0c 104#ifndef DEBUG
afe907a4 105 daemon(1, 0);
52b4fb0c 106#endif
17ccb086
KB
107 if (chdir(_PATH_RWHODIR) < 0) {
108 (void)fprintf(stderr, "rwhod: %s: %s\n",
109 _PATH_RWHODIR, strerror(errno));
ac82a1ec
KM
110 exit(1);
111 }
cf498703 112 (void) signal(SIGHUP, getboottime);
076ae92c 113 openlog("rwhod", LOG_PID, LOG_DAEMON);
7af17b3e
SL
114 /*
115 * Establish host name as returned by system.
116 */
5115d8df 117 if (gethostname(myname, sizeof(myname) - 1) < 0) {
00cdc205 118 syslog(LOG_ERR, "gethostname: %m");
52b4fb0c
BJ
119 exit(1);
120 }
d2444544
JB
121 if ((cp = index(myname, '.')) != NULL)
122 *cp = '\0';
5115d8df 123 strncpy(mywd.wd_hostname, myname, sizeof(myname) - 1);
17ccb086 124 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
52b4fb0c 125 if (utmpf < 0) {
17ccb086 126 syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
52b4fb0c
BJ
127 exit(1);
128 }
cf498703 129 getboottime(0);
9bd66dcf 130 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00cdc205 131 syslog(LOG_ERR, "socket: %m");
de3b21e8
SL
132 exit(1);
133 }
5115d8df 134 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
b0e1e5f3
MK
135 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
136 exit(1);
137 }
5115d8df 138 memset(&sin, 0, sizeof(sin));
b584098b 139 sin.sin_family = AF_INET;
7af17b3e 140 sin.sin_port = sp->s_port;
5115d8df 141 if (bind(s, (struct sockaddr *)&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);
55ae2764 148 onalrm(0);
52b4fb0c
BJ
149 for (;;) {
150 struct whod wd;
5115d8df 151 int cc, whod, len = sizeof(from);
52b4fb0c 152
5115d8df 153 cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
0df908a9 154 (struct sockaddr *)&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 }
aaddf8a1
SL
165 if (wd.wd_vers != WHODVERSION)
166 continue;
7af17b3e
SL
167 if (wd.wd_type != WHODTYPE_STATUS)
168 continue;
a0f59ac1 169 if (!verify(wd.wd_hostname)) {
00cdc205 170 syslog(LOG_WARNING, "malformed host name from %x",
a0f59ac1 171 from.sin_addr);
52b4fb0c
BJ
172 continue;
173 }
ac82a1ec 174 (void) sprintf(path, "whod.%s", wd.wd_hostname);
0f2d5e68
MK
175 /*
176 * Rather than truncating and growing the file each time,
177 * use ftruncate if size is less than previous size.
178 */
179 whod = open(path, O_WRONLY | O_CREAT, 0644);
52b4fb0c 180 if (whod < 0) {
00cdc205 181 syslog(LOG_WARNING, "%s: %m", path);
52b4fb0c
BJ
182 continue;
183 }
b584098b 184#if ENDIAN != BIG_ENDIAN
fea387f0 185 {
9bd66dcf 186 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
fea387f0
SL
187 struct whoent *we;
188
189 /* undo header byte swapping before writing to file */
190 wd.wd_sendtime = ntohl(wd.wd_sendtime);
191 for (i = 0; i < 3; i++)
192 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
193 wd.wd_boottime = ntohl(wd.wd_boottime);
194 we = wd.wd_we;
195 for (i = 0; i < n; i++) {
196 we->we_idle = ntohl(we->we_idle);
1cccbcb3
SL
197 we->we_utmp.out_time =
198 ntohl(we->we_utmp.out_time);
fea387f0
SL
199 we++;
200 }
201 }
202#endif
0df908a9 203 (void) time((time_t *)&wd.wd_recvtime);
52b4fb0c 204 (void) write(whod, (char *)&wd, cc);
0f2d5e68
MK
205 if (fstat(whod, &st) < 0 || st.st_size > cc)
206 ftruncate(whod, cc);
52b4fb0c
BJ
207 (void) close(whod);
208 }
209}
210
a0f59ac1
SL
211/*
212 * Check out host name for unprintables
213 * and other funnies before allowing a file
214 * to be created. Sorry, but blanks aren't allowed.
215 */
55ae2764 216int
a0f59ac1
SL
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;
4c9b3acd
JB
232int utmpsize = 0;
233struct utmp *utmp;
52b4fb0c
BJ
234int alarmcount;
235
0df908a9 236void
55ae2764
KB
237onalrm(signo)
238 int signo;
52b4fb0c 239{
17ccb086
KB
240 register struct neighbor *np;
241 register struct whoent *we = mywd.wd_we, *wlast;
52b4fb0c
BJ
242 register int i;
243 struct stat stb;
52b4fb0c 244 double avenrun[3];
55ae2764
KB
245 time_t now;
246 int cc;
52b4fb0c 247
55ae2764 248 now = time(NULL);
52b4fb0c 249 if (alarmcount % 10 == 0)
cf498703 250 getboottime(0);
52b4fb0c
BJ
251 alarmcount++;
252 (void) fstat(utmpf, &stb);
4c9b3acd 253 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
02d31241 254 utmptime = stb.st_mtime;
4c9b3acd
JB
255 if (stb.st_size > utmpsize) {
256 utmpsize = stb.st_size + 10 * sizeof(struct utmp);
257 if (utmp)
258 utmp = (struct utmp *)realloc(utmp, utmpsize);
259 else
260 utmp = (struct utmp *)malloc(utmpsize);
261 if (! utmp) {
262 fprintf(stderr, "rwhod: malloc failed\n");
263 utmpsize = 0;
264 goto done;
265 }
266 }
0f756990 267 (void) lseek(utmpf, (off_t)0, L_SET);
4c9b3acd 268 cc = read(utmpf, (char *)utmp, stb.st_size);
52b4fb0c 269 if (cc < 0) {
17ccb086
KB
270 fprintf(stderr, "rwhod: %s: %s\n",
271 _PATH_UTMP, strerror(errno));
08d10f57 272 goto done;
52b4fb0c 273 }
5115d8df
CT
274 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
275 utmpent = cc / sizeof(struct utmp);
52b4fb0c
BJ
276 for (i = 0; i < utmpent; i++)
277 if (utmp[i].ut_name[0]) {
5115d8df
CT
278 memcpy(we->we_utmp.out_line, utmp[i].ut_line,
279 sizeof(utmp[i].ut_line));
280 memcpy(we->we_utmp.out_name, utmp[i].ut_name,
281 sizeof(utmp[i].ut_name));
b1456c48 282 we->we_utmp.out_time = htonl(utmp[i].ut_time);
76886f32
SL
283 if (we >= wlast)
284 break;
52b4fb0c
BJ
285 we++;
286 }
287 utmpent = we - mywd.wd_we;
288 }
221be169
KM
289
290 /*
291 * The test on utmpent looks silly---after all, if no one is
292 * logged on, why worry about efficiency?---but is useful on
293 * (e.g.) compute servers.
294 */
7abf8d65
KB
295 if (utmpent && chdir(_PATH_DEV)) {
296 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
221be169
KM
297 exit(1);
298 }
52b4fb0c
BJ
299 we = mywd.wd_we;
300 for (i = 0; i < utmpent; i++) {
221be169 301 if (stat(we->we_utmp.out_line, &stb) >= 0)
b1456c48 302 we->we_idle = htonl(now - stb.st_atime);
52b4fb0c
BJ
303 we++;
304 }
5115d8df 305 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
52b4fb0c 306 for (i = 0; i < 3; i++)
353c1281 307 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
b1456c48 308 cc = (char *)we - (char *)&mywd;
b1456c48 309 mywd.wd_sendtime = htonl(time(0));
7af17b3e
SL
310 mywd.wd_vers = WHODVERSION;
311 mywd.wd_type = WHODTYPE_STATUS;
312 for (np = neighbors; np != NULL; np = np->n_next)
5115d8df 313 (void)sendto(s, (char *)&mywd, cc, 0,
0892992b 314 np->n_addr, np->n_addrlen);
17ccb086
KB
315 if (utmpent && chdir(_PATH_RWHODIR)) {
316 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
221be169
KM
317 exit(1);
318 }
08d10f57 319done:
83361bb3 320 (void) alarm(AL_INTERVAL);
52b4fb0c
BJ
321}
322
0df908a9 323void
cf498703 324getboottime(signo)
55ae2764 325 int signo;
52b4fb0c 326{
83c99760
CT
327 int mib[2];
328 size_t size;
cf498703 329 struct timeval tm;
52b4fb0c 330
cf498703
KM
331 mib[0] = CTL_KERN;
332 mib[1] = KERN_BOOTTIME;
333 size = sizeof(tm);
334 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
335 syslog(LOG_ERR, "cannot get boottime: %m");
00cdc205 336 exit(1);
52b4fb0c 337 }
cf498703 338 mywd.wd_boottime = htonl(tm.tv_sec);
52b4fb0c 339}
7af17b3e 340
0892992b
KS
341void
342quit(msg)
55ae2764 343 char *msg;
0892992b
KS
344{
345 syslog(LOG_ERR, msg);
346 exit(1);
347}
348
349#define ROUNDUP(a) \
350 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
351#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
352
353void
354rt_xaddrs(cp, cplim, rtinfo)
355 register caddr_t cp, cplim;
356 register struct rt_addrinfo *rtinfo;
357{
358 register struct sockaddr *sa;
359 register int i;
360
5115d8df 361 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
0892992b
KS
362 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
363 if ((rtinfo->rti_addrs & (1 << i)) == 0)
364 continue;
365 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
366 ADVANCE(cp, sa);
367 }
368}
369
7af17b3e
SL
370/*
371 * Figure out device configuration and select
372 * networks which deserve status information.
373 */
55ae2764 374int
7af17b3e
SL
375configure(s)
376 int s;
377{
7af17b3e 378 register struct neighbor *np;
0892992b
KS
379 register struct if_msghdr *ifm;
380 register struct ifa_msghdr *ifam;
0892992b 381 struct sockaddr_dl *sdl;
d605e71a
KM
382 size_t needed;
383 int mib[6], flags = 0, len;
0892992b
KS
384 char *buf, *lim, *next;
385 struct rt_addrinfo info;
7af17b3e 386
d605e71a
KM
387 mib[0] = CTL_NET;
388 mib[1] = PF_ROUTE;
389 mib[2] = 0;
390 mib[3] = AF_INET;
391 mib[4] = NET_RT_IFLIST;
392 mib[5] = 0;
393 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
394 quit("route-sysctl-estimate");
0892992b
KS
395 if ((buf = malloc(needed)) == NULL)
396 quit("malloc");
d605e71a 397 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
0892992b 398 quit("actual retrieval of interface table");
d605e71a 399 lim = buf + needed;
0892992b 400
5115d8df 401 sdl = NULL; /* XXX just to keep gcc -Wall happy */
0892992b
KS
402 for (next = buf; next < lim; next += ifm->ifm_msglen) {
403 ifm = (struct if_msghdr *)next;
404 if (ifm->ifm_type == RTM_IFINFO) {
405 sdl = (struct sockaddr_dl *)(ifm + 1);
406 flags = ifm->ifm_flags;
7af17b3e
SL
407 continue;
408 }
0892992b
KS
409 if ((flags & IFF_UP) == 0 ||
410 (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
7af17b3e 411 continue;
0892992b 412 if (ifm->ifm_type != RTM_NEWADDR)
d605e71a 413 quit("out of sync parsing NET_RT_IFLIST");
0892992b
KS
414 ifam = (struct ifa_msghdr *)ifm;
415 info.rti_addrs = ifam->ifam_addrs;
416 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
417 &info);
418 /* gag, wish we could get rid of Internet dependencies */
419#define dstaddr info.rti_info[RTAX_BRD]
420#define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
421#define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
422 if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
7af17b3e 423 continue;
0892992b
KS
424 PORT_SA(dstaddr) = sp->s_port;
425 for (np = neighbors; np != NULL; np = np->n_next)
5115d8df
CT
426 if (memcmp(sdl->sdl_data, np->n_name,
427 sdl->sdl_nlen) == 0 &&
428 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
0892992b
KS
429 break;
430 if (np != NULL)
7af17b3e 431 continue;
0892992b
KS
432 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
433 np = (struct neighbor *)malloc(len);
434 if (np == NULL)
435 quit("malloc of neighbor structure");
5115d8df 436 memset(np, 0, len);
0892992b
KS
437 np->n_flags = flags;
438 np->n_addr = (struct sockaddr *)(np + 1);
439 np->n_addrlen = dstaddr->sa_len;
440 np->n_name = np->n_addrlen + (char *)np->n_addr;
7af17b3e
SL
441 np->n_next = neighbors;
442 neighbors = np;
5115d8df
CT
443 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
444 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
7af17b3e 445 }
0892992b 446 free(buf);
7af17b3e
SL
447 return (1);
448}
b1456c48
SL
449
450#ifdef DEBUG
55ae2764 451void
5115d8df 452Sendto(s, buf, cc, flags, to, tolen)
b1456c48
SL
453 int s;
454 char *buf;
455 int cc, flags;
456 char *to;
457 int tolen;
458{
459 register struct whod *w = (struct whod *)buf;
460 register struct whoent *we;
461 struct sockaddr_in *sin = (struct sockaddr_in *)to;
b1456c48
SL
462
463 printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
464 printf("hostname %s %s\n", w->wd_hostname,
9bd66dcf 465 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
b1456c48 466 printf("load %4.2f, %4.2f, %4.2f\n",
9bd66dcf
SL
467 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
468 ntohl(w->wd_loadav[2]) / 100.0);
b1456c48 469 cc -= WHDRSIZE;
5115d8df 470 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
9bd66dcf 471 time_t t = ntohl(we->we_utmp.out_time);
b1456c48 472 printf("%-8.8s %s:%s %.12s",
9bd66dcf
SL
473 we->we_utmp.out_name,
474 w->wd_hostname, we->we_utmp.out_line,
475 ctime(&t)+4);
476 we->we_idle = ntohl(we->we_idle) / 60;
b1456c48
SL
477 if (we->we_idle) {
478 if (we->we_idle >= 100*60)
479 we->we_idle = 100*60 - 1;
480 if (we->we_idle >= 60)
481 printf(" %2d", we->we_idle / 60);
482 else
483 printf(" ");
484 printf(":%02d", we->we_idle % 60);
485 }
486 printf("\n");
487 }
488}
489
490char *
491interval(time, updown)
492 int time;
493 char *updown;
494{
495 static char resbuf[32];
496 int days, hours, minutes;
497
498 if (time < 0 || time > 3*30*24*60*60) {
499 (void) sprintf(resbuf, " %s ??:??", updown);
500 return (resbuf);
501 }
502 minutes = (time + 59) / 60; /* round to minutes */
503 hours = minutes / 60; minutes %= 60;
504 days = hours / 24; hours %= 24;
505 if (days)
506 (void) sprintf(resbuf, "%s %2d+%02d:%02d",
507 updown, days, hours, minutes);
508 else
509 (void) sprintf(resbuf, "%s %2d:%02d",
510 updown, hours, minutes);
511 return (resbuf);
512}
513#endif