2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)rwhod.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.sbin/rwhod/rwhod.c,v 1.13.2.2 2000/12/23 15:28:12 iedowse Exp $
36 * $DragonFly: src/usr.sbin/rwhod/rwhod.c,v 1.10 2005/03/21 19:20:57 joerg Exp $
39 #include <sys/param.h>
40 #include <sys/socket.h>
42 #include <sys/signal.h>
43 #include <sys/ioctl.h>
44 #include <sys/sysctl.h>
47 #include <net/if_dl.h>
48 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <protocols/rwhod.h>
69 * This version of Berkeley's rwhod has been modified to use IP multicast
70 * datagrams, under control of a new command-line option:
72 * rwhod -m causes rwhod to use IP multicast (instead of
73 * broadcast or unicast) on all interfaces that have
74 * the IFF_MULTICAST flag set in their "ifnet" structs
75 * (excluding the loopback interface). The multicast
76 * reports are sent with a time-to-live of 1, to prevent
77 * forwarding beyond the directly-connected subnet(s).
79 * rwhod -m <ttl> causes rwhod to send IP multicast datagrams with a
80 * time-to-live of <ttl>, via a SINGLE interface rather
81 * than all interfaces. <ttl> must be between 0 and
82 * MAX_MULTICAST_SCOPE, defined below. Note that "-m 1"
83 * is different than "-m", in that "-m 1" specifies
84 * transmission on one interface only.
86 * When "-m" is used without a <ttl> argument, the program accepts multicast
87 * rwhod reports from all multicast-capable interfaces. If a <ttl> argument
88 * is given, it accepts multicast reports from only one interface, the one
89 * on which reports are sent (which may be controlled via the host's routing
90 * table). Regardless of the "-m" option, the program accepts broadcast or
91 * unicast reports from all interfaces. Thus, this program will hear the
92 * reports of old, non-multicasting rwhods, but, if multicasting is used,
93 * those old rwhods won't hear the reports generated by this program.
95 * -- Steve Deering, Stanford University, February 1989
98 #define UNPRIV_USER "daemon"
99 #define UNPRIV_GROUP "daemon"
101 #define NO_MULTICAST 0 /* multicast modes */
102 #define PER_INTERFACE_MULTICAST 1
103 #define SCOPED_MULTICAST 2
105 #define MAX_MULTICAST_SCOPE 32 /* "site-wide", by convention */
107 #define INADDR_WHOD_GROUP (u_long)0xe0000103 /* 224.0.1.3 */
108 /* (belongs in protocols/rwhod.h) */
112 int iff_flag = IFF_POINTOPOINT;
113 int multicast_mode = NO_MULTICAST;
115 struct sockaddr_in multicast_addr;
118 * Alarm interval. Don't forget to change the down time check in ruptime
119 * if this is changed.
121 #define AL_INTERVAL (3 * 60)
123 char myname[MAXHOSTNAMELEN];
126 * We communicate with each neighbor in a list constructed at the time we're
127 * started up. Neighbors are currently directly connected via a hardware
131 struct neighbor *n_next;
132 char *n_name; /* interface name */
133 struct sockaddr *n_addr; /* who to send to */
134 int n_addrlen; /* size of address */
135 int n_flags; /* should forward?, interface flags */
138 struct neighbor *neighbors;
142 volatile sig_atomic_t got_sigalrm, got_sighup;
144 #define WHDRSIZE (sizeof(mywd) - sizeof(mywd.wd_we))
146 void run_as(uid_t *, gid_t *);
148 void getboottime(void);
151 void quit(const char *);
152 void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
153 int verify(char *, int);
154 static void usage(void);
156 char *interval(int, char *);
157 void Sendto(int, const void *, size_t, int,
158 const struct sockaddr *, int);
159 #define sendto Sendto
163 main(int argc, char *argv[])
165 struct sockaddr_in from;
170 struct sockaddr_in m_sin;
175 errx(1, "not super user");
177 run_as(&unpriv_uid, &unpriv_gid);
180 while (argc > 0 && *argv[0] == '-') {
181 if (strcmp(*argv, "-m") == 0) {
182 if (argc > 1 && isdigit(*(argv + 1)[0])) {
184 multicast_mode = SCOPED_MULTICAST;
185 multicast_scope = atoi(*argv);
186 if (multicast_scope > MAX_MULTICAST_SCOPE)
187 errx(1, "ttl must not exceed %u",
188 MAX_MULTICAST_SCOPE);
190 else multicast_mode = PER_INTERFACE_MULTICAST;
192 else if (strcmp(*argv, "-i") == 0)
194 else if (strcmp(*argv, "-l") == 0)
196 else if (strcmp(*argv, "-p") == 0)
207 signal(SIGHUP, onsignal);
208 openlog("rwhod", LOG_PID, LOG_DAEMON);
209 sp = getservbyname("who", "udp");
211 syslog(LOG_ERR, "udp/who: unknown service");
214 if (chdir(_PATH_RWHODIR) < 0) {
215 syslog(LOG_ERR, "%s: %m", _PATH_RWHODIR);
219 * Establish host name as returned by system.
221 if (gethostname(myname, sizeof(myname)) < 0) {
222 syslog(LOG_ERR, "gethostname: %m");
225 if ((cp = strchr(myname, '.')) != NULL)
227 strlcpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname));
228 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
230 syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
234 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
235 syslog(LOG_ERR, "socket: %m");
238 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
239 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
242 memset(&m_sin, 0, sizeof(m_sin));
243 m_sin.sin_len = sizeof(m_sin);
244 m_sin.sin_family = AF_INET;
245 m_sin.sin_port = sp->s_port;
246 if (bind(s, (struct sockaddr *)&m_sin, sizeof(m_sin)) < 0) {
247 syslog(LOG_ERR, "bind: %m");
251 setgroups(1, &unpriv_gid); /* XXX BOGUS groups[0] = egid */
256 signal(SIGALRM, onsignal);
261 int cc, whod, len = sizeof(from);
263 if (got_sigalrm == 1) {
267 else if (got_sighup == 1) {
272 cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
273 (struct sockaddr *)&from, &len);
277 if (errno == EINTR) {
278 if (got_sigalrm == 1) {
282 else if (got_sighup == 1) {
287 syslog(LOG_WARNING, "recv: %m");
291 if (from.sin_port != sp->s_port && !insecure_mode) {
292 syslog(LOG_WARNING, "%d: bad source port from %s",
293 ntohs(from.sin_port), inet_ntoa(from.sin_addr));
296 if (cc < (int)WHDRSIZE) {
297 syslog(LOG_WARNING, "short packet from %s",
298 inet_ntoa(from.sin_addr));
301 if (wd.wd_vers != WHODVERSION)
303 if (wd.wd_type != WHODTYPE_STATUS)
305 if (!verify(wd.wd_hostname, sizeof wd.wd_hostname)) {
306 syslog(LOG_WARNING, "malformed host name from %s",
307 inet_ntoa(from.sin_addr));
310 snprintf(path, sizeof path, "whod.%s", wd.wd_hostname);
312 * Rather than truncating and growing the file each time,
313 * use ftruncate if size is less than previous size.
315 whod = open(path, O_WRONLY | O_CREAT, 0644);
317 syslog(LOG_WARNING, "%s: %m", path);
320 #if ENDIAN != BIG_ENDIAN
322 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
325 /* undo header byte swapping before writing to file */
326 wd.wd_sendtime = ntohl(wd.wd_sendtime);
327 for (i = 0; i < 3; i++)
328 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
329 wd.wd_boottime = ntohl(wd.wd_boottime);
331 for (i = 0; i < n; i++) {
332 we->we_idle = ntohl(we->we_idle);
333 we->we_utmp.out_time =
334 ntohl(we->we_utmp.out_time);
339 time((time_t *)&wd.wd_recvtime);
340 write(whod, (char *)&wd, cc);
341 if (fstat(whod, &st) < 0 || st.st_size > cc)
350 fprintf(stderr, "usage: rwhod [-i] [-p] [-l] [-m [ttl]]\n");
355 run_as(uid_t *uid, gid_t *gid)
360 pw = getpwnam(UNPRIV_USER);
362 syslog(LOG_ERR, "getpwnam(%s): %m", UNPRIV_USER);
367 gr = getgrnam(UNPRIV_GROUP);
369 syslog(LOG_ERR, "getgrnam(%s): %m", UNPRIV_GROUP);
376 * Check out host name for unprintables
377 * and other funnies before allowing a file
378 * to be created. Sorry, but blanks aren't allowed.
381 verify(char *name, int maxlen)
385 while (*name && size < maxlen - 1) {
386 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
397 if (signo == SIGALRM)
413 struct whoent *we = mywd.wd_we, *wlast;
421 if (alarmcount % 10 == 0)
425 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
426 utmptime = stb.st_mtime;
427 if (stb.st_size > utmpsize) {
428 utmpsize = stb.st_size + 10 * sizeof(struct utmp);
430 utmp = (struct utmp *)realloc(utmp, utmpsize);
432 utmp = (struct utmp *)malloc(utmpsize);
434 syslog(LOG_WARNING, "malloc failed");
439 lseek(utmpf, (off_t)0, L_SET);
440 cc = read(utmpf, (char *)utmp, stb.st_size);
442 syslog(LOG_ERR, "read(%s): %m", _PATH_UTMP);
445 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
446 utmpent = cc / sizeof(struct utmp);
447 for (i = 0; i < utmpent; i++)
448 if (utmp[i].ut_name[0]) {
449 memcpy(we->we_utmp.out_line, utmp[i].ut_line,
450 sizeof(utmp[i].ut_line));
451 memcpy(we->we_utmp.out_name, utmp[i].ut_name,
452 sizeof(utmp[i].ut_name));
453 we->we_utmp.out_time = htonl(utmp[i].ut_time);
458 utmpent = we - mywd.wd_we;
462 * The test on utmpent looks silly---after all, if no one is
463 * logged on, why worry about efficiency?---but is useful on
464 * (e.g.) compute servers.
466 if (utmpent && chdir(_PATH_DEV)) {
467 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
471 for (i = 0; i < utmpent; i++) {
472 if (stat(we->we_utmp.out_line, &stb) >= 0)
473 we->we_idle = htonl(now - stb.st_atime);
476 getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
477 for (i = 0; i < 3; i++)
478 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
479 cc = (char *)we - (char *)&mywd;
480 mywd.wd_sendtime = htonl(time(0));
481 mywd.wd_vers = WHODVERSION;
482 mywd.wd_type = WHODTYPE_STATUS;
483 if (multicast_mode == SCOPED_MULTICAST) {
484 sendto(s, (char *)&mywd, cc, 0,
485 (struct sockaddr *)&multicast_addr,
486 sizeof(multicast_addr));
488 else for (np = neighbors; np != NULL; np = np->n_next) {
489 if (multicast_mode == PER_INTERFACE_MULTICAST &&
490 np->n_flags & IFF_MULTICAST) {
492 * Select the outgoing interface for the multicast.
494 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
495 &(((struct sockaddr_in *)np->n_addr)->sin_addr),
496 sizeof(struct in_addr)) < 0) {
498 "setsockopt IP_MULTICAST_IF: %m");
501 sendto(s, (char *)&mywd, cc, 0,
502 (struct sockaddr *)&multicast_addr,
503 sizeof(multicast_addr));
504 } else sendto(s, (char *)&mywd, cc, 0,
505 np->n_addr, np->n_addrlen);
507 if (utmpent && chdir(_PATH_RWHODIR)) {
508 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
523 mib[1] = KERN_BOOTTIME;
525 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
526 syslog(LOG_ERR, "cannot get boottime: %m");
529 mywd.wd_boottime = htonl(tm.tv_sec);
533 quit(const char *msg)
536 syslog(LOG_ERR, "%s", msg);
541 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
542 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
545 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
550 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
551 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
552 if ((rtinfo->rti_addrs & (1 << i)) == 0)
554 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
560 * Figure out device configuration and select
561 * networks which deserve status information.
564 configure(int c_sock)
567 struct if_msghdr *ifm;
568 struct ifa_msghdr *ifam;
569 struct sockaddr_dl *sdl;
571 int mib[6], flags = 0, len;
572 char *buf, *lim, *next;
573 struct rt_addrinfo info;
575 if (multicast_mode != NO_MULTICAST) {
576 multicast_addr.sin_len = sizeof(multicast_addr);
577 multicast_addr.sin_family = AF_INET;
578 multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
579 multicast_addr.sin_port = sp->s_port;
582 if (multicast_mode == SCOPED_MULTICAST) {
586 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
587 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
588 if (setsockopt(c_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
589 &mreq, sizeof(mreq)) < 0) {
591 "setsockopt IP_ADD_MEMBERSHIP: %m");
594 ttl = multicast_scope;
595 if (setsockopt(c_sock, IPPROTO_IP, IP_MULTICAST_TTL,
596 &ttl, sizeof(ttl)) < 0) {
598 "setsockopt IP_MULTICAST_TTL: %m");
608 mib[4] = NET_RT_IFLIST;
610 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
611 quit("route-sysctl-estimate");
612 if ((buf = malloc(needed)) == NULL)
614 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
615 quit("actual retrieval of interface table");
618 sdl = NULL; /* XXX just to keep gcc -Wall happy */
619 for (next = buf; next < lim; next += ifm->ifm_msglen) {
620 ifm = (struct if_msghdr *)next;
621 if (ifm->ifm_type == RTM_IFINFO) {
622 sdl = (struct sockaddr_dl *)(ifm + 1);
623 flags = ifm->ifm_flags;
626 if ((flags & IFF_UP) == 0 ||
627 (flags & (((multicast_mode == PER_INTERFACE_MULTICAST) ?
629 IFF_BROADCAST|iff_flag)) == 0)
631 if (ifm->ifm_type != RTM_NEWADDR)
632 quit("out of sync parsing NET_RT_IFLIST");
633 ifam = (struct ifa_msghdr *)ifm;
634 info.rti_addrs = ifam->ifam_addrs;
635 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
637 /* gag, wish we could get rid of Internet dependencies */
638 #define dstaddr info.rti_info[RTAX_BRD]
639 #define ifaddr info.rti_info[RTAX_IFA]
640 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
641 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
642 if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
644 PORT_SA(dstaddr) = sp->s_port;
645 for (np = neighbors; np != NULL; np = np->n_next)
646 if (memcmp(sdl->sdl_data, np->n_name,
647 sdl->sdl_nlen) == 0 &&
648 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
652 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
653 np = (struct neighbor *)malloc(len);
655 quit("malloc of neighbor structure");
658 np->n_addr = (struct sockaddr *)(np + 1);
659 np->n_addrlen = dstaddr->sa_len;
660 np->n_name = np->n_addrlen + (char *)np->n_addr;
661 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
662 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
663 if (multicast_mode == PER_INTERFACE_MULTICAST &&
664 (flags & IFF_MULTICAST) &&
665 !(flags & IFF_LOOPBACK)) {
668 memcpy((char *)np->n_addr, (char *)ifaddr,
670 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
671 mreq.imr_interface.s_addr =
672 ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
673 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
674 &mreq, sizeof(mreq)) < 0) {
676 "setsockopt IP_ADD_MEMBERSHIP: %m");
678 /* Fall back to broadcast on this if. */
679 np->n_flags &= ~IFF_MULTICAST;
686 np->n_next = neighbors;
695 Sendto(int s, const void *buf, size_t cc, int flags,
696 const struct sockaddr *to, int tolen)
698 struct whod *w = (struct whod *)buf;
700 struct sockaddr_in *sin = (struct sockaddr_in *)to;
702 printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr),
703 ntohs(sin->sin_port));
704 printf("hostname %s %s\n", w->wd_hostname,
705 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
706 printf("load %4.2f, %4.2f, %4.2f\n",
707 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
708 ntohl(w->wd_loadav[2]) / 100.0);
710 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
711 time_t t = ntohl(we->we_utmp.out_time);
712 printf("%-8.8s %s:%s %.12s",
713 we->we_utmp.out_name,
714 w->wd_hostname, we->we_utmp.out_line,
716 we->we_idle = ntohl(we->we_idle) / 60;
718 if (we->we_idle >= 100*60)
719 we->we_idle = 100*60 - 1;
720 if (we->we_idle >= 60)
721 printf(" %2d", we->we_idle / 60);
724 printf(":%02d", we->we_idle % 60);
731 interval(int time, char *updown)
733 static char resbuf[32];
734 int days, hours, minutes;
736 if (time < 0 || time > 3*30*24*60*60) {
737 sprintf(resbuf, " %s ??:??", updown);
740 minutes = (time + 59) / 60; /* round to minutes */
741 hours = minutes / 60; minutes %= 60;
742 days = hours / 24; hours %= 24;
744 sprintf(resbuf, "%s %2d+%02d:%02d",
745 updown, days, hours, minutes);
747 sprintf(resbuf, "%s %2d:%02d",
748 updown, hours, minutes);