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