2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993 The Regents of the University of California.
5 * Copyright (c) 2013 Mariusz Zaborski <oshogbo@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/capsicum.h>
35 #include <sys/ioctl.h>
36 #include <sys/procdesc.h>
37 #include <sys/socket.h>
39 #include <sys/signal.h>
40 #include <sys/sysctl.h>
44 #include <net/if_dl.h>
45 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <protocols/rwhod.h>
51 #include <capsicum_helpers.h>
67 #define UNPRIV_USER "daemon"
68 #define UNPRIV_GROUP "daemon"
70 #define NO_MULTICAST 0 /* multicast modes */
71 #define PER_INTERFACE_MULTICAST 1
72 #define SCOPED_MULTICAST 2
74 #define MAX_MULTICAST_SCOPE 32 /* "site-wide", by convention */
76 #define INADDR_WHOD_GROUP (u_long)0xe0000103 /* 224.0.1.3 */
77 /* (belongs in protocols/rwhod.h) */
81 int iff_flag = IFF_POINTOPOINT;
82 int multicast_mode = NO_MULTICAST;
84 struct sockaddr_in multicast_addr =
85 { sizeof(multicast_addr), AF_INET, 0, { 0 }, { 0 } };
88 * Sleep interval. Don't forget to change the down time check in ruptime
91 #define SL_INTERVAL (3 * 60)
93 char myname[MAXHOSTNAMELEN];
96 * We communicate with each neighbor in a list constructed at the time we're
97 * started up. Neighbors are currently directly connected via a hardware
101 struct neighbor *n_next;
102 char *n_name; /* interface name */
103 struct sockaddr *n_addr; /* who to send to */
104 int n_addrlen; /* size of address */
105 int n_flags; /* should forward?, interface flags */
108 struct neighbor *neighbors;
113 pid_t pid_child_receiver;
115 #define WHDRSIZE (int)(sizeof(mywd) - sizeof(mywd.wd_we))
117 int configure(int so);
118 void getboottime(int signo __unused);
119 void receiver_process(void);
120 void rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo);
121 void run_as(uid_t *uid, gid_t *gid);
122 void quit(const char *msg);
123 void sender_process(void);
124 int verify(char *name, int maxlen);
125 static void usage(void) __dead2;
128 char *interval(int time, char *updown);
129 void Sendto(int s, const void *buf, size_t cc, int flags,
130 const struct sockaddr *to, int tolen);
131 #define sendto Sendto
135 * This version of Berkeley's rwhod has been modified to use IP multicast
136 * datagrams, under control of a new command-line option:
138 * rwhod -m causes rwhod to use IP multicast (instead of
139 * broadcast or unicast) on all interfaces that have
140 * the IFF_MULTICAST flag set in their "ifnet" structs
141 * (excluding the loopback interface). The multicast
142 * reports are sent with a time-to-live of 1, to prevent
143 * forwarding beyond the directly-connected subnet(s).
145 * rwhod -m <ttl> causes rwhod to send IP multicast datagrams with a
146 * time-to-live of <ttl>, via a SINGLE interface rather
147 * than all interfaces. <ttl> must be between 0 and
148 * MAX_MULTICAST_SCOPE, defined below. Note that "-m 1"
149 * is different than "-m", in that "-m 1" specifies
150 * transmission on one interface only.
152 * When "-m" is used without a <ttl> argument, the program accepts multicast
153 * rwhod reports from all multicast-capable interfaces. If a <ttl> argument
154 * is given, it accepts multicast reports from only one interface, the one
155 * on which reports are sent (which may be controlled via the host's routing
156 * table). Regardless of the "-m" option, the program accepts broadcast or
157 * unicast reports from all interfaces. Thus, this program will hear the
158 * reports of old, non-multicasting rwhods, but, if multicasting is used,
159 * those old rwhods won't hear the reports generated by this program.
161 * -- Steve Deering, Stanford University, February 1989
164 main(int argc, char *argv[])
168 struct sockaddr_in soin;
174 errx(1, "not super user");
176 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])) {
185 multicast_mode = SCOPED_MULTICAST;
186 multicast_scope = atoi(*argv);
187 if (multicast_scope > MAX_MULTICAST_SCOPE) {
188 errx(1, "ttl must not exceed %u",
189 MAX_MULTICAST_SCOPE);
192 multicast_mode = PER_INTERFACE_MULTICAST;
194 } else if (strcmp(*argv, "-i") == 0) {
196 } else if (strcmp(*argv, "-l") == 0) {
198 } else if (strcmp(*argv, "-p") == 0) {
211 (void) signal(SIGHUP, getboottime);
212 openlog("rwhod", LOG_PID | LOG_NDELAY, LOG_DAEMON);
213 sp = getservbyname("who", "udp");
215 syslog(LOG_ERR, "who/udp: unknown service");
218 if (chdir(_PATH_RWHODIR) < 0) {
219 syslog(LOG_ERR, "%s: %m", _PATH_RWHODIR);
223 * Establish host name as returned by system.
225 if (gethostname(myname, sizeof(myname) - 1) < 0) {
226 syslog(LOG_ERR, "gethostname: %m");
229 if ((cp = strchr(myname, '.')) != NULL)
231 strlcpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname));
233 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
234 syslog(LOG_ERR, "socket: %m");
237 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
238 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
241 memset(&soin, 0, sizeof(soin));
242 soin.sin_len = sizeof(soin);
243 soin.sin_family = AF_INET;
244 soin.sin_port = sp->s_port;
245 if (bind(s, (struct sockaddr *)&soin, sizeof(soin)) < 0) {
246 syslog(LOG_ERR, "bind: %m");
249 if (setgid(unpriv_gid) != 0) {
250 syslog(LOG_ERR, "setgid: %m");
253 if (setgroups(1, &unpriv_gid) != 0) { /* XXX BOGUS groups[0] = egid */
254 syslog(LOG_ERR, "setgroups: %m");
257 if (setuid(unpriv_uid) != 0) {
258 syslog(LOG_ERR, "setuid: %m");
264 pid_child_receiver = pdfork(&fdp, 0);
265 if (pid_child_receiver == 0) {
267 } else if (pid_child_receiver > 0) {
269 } else if (pid_child_receiver == -1) {
270 if (errno == ENOSYS) {
272 "The pdfork(2) system call is not available - kernel too old.");
274 syslog(LOG_ERR, "pdfork: %m");
287 fprintf(stderr, "usage: rwhod [-i] [-p] [-l] [-m [ttl]]\n");
292 run_as(uid_t *uid, gid_t *gid)
297 pw = getpwnam(UNPRIV_USER);
299 syslog(LOG_ERR, "getpwnam(%s): %m", UNPRIV_USER);
304 gr = getgrnam(UNPRIV_GROUP);
306 syslog(LOG_ERR, "getgrnam(%s): %m", UNPRIV_GROUP);
313 * Check out host name for unprintables
314 * and other funnies before allowing a file
315 * to be created. Sorry, but blanks aren't allowed.
318 verify(char *name, int maxlen)
323 while (*name != '\0' && size < maxlen - 1) {
324 if (!isascii((unsigned char)*name) ||
325 !(isalnum((unsigned char)*name) ||
326 ispunct((unsigned char)*name))) {
337 receiver_process(void)
339 struct sockaddr_in from;
350 dirfd = open(".", O_RDONLY | O_DIRECTORY);
352 syslog(LOG_WARNING, "%s: %m", _PATH_RWHODIR);
355 cap_rights_init(&rights, CAP_CREATE, CAP_FSTAT, CAP_FTRUNCATE,
356 CAP_LOOKUP, CAP_SEEK, CAP_WRITE);
357 if (caph_rights_limit(dirfd, &rights) < 0) {
358 syslog(LOG_WARNING, "cap_rights_limit: %m");
361 if (caph_enter() < 0) {
362 syslog(LOG_ERR, "cap_enter: %m");
366 cc = recvfrom(s, &wd, sizeof(wd), 0, (struct sockaddr *)&from,
369 if (cc < 0 && errno != EINTR)
370 syslog(LOG_WARNING, "recv: %m");
373 if (from.sin_port != sp->s_port && !insecure_mode) {
374 syslog(LOG_WARNING, "%d: bad source port from %s",
375 ntohs(from.sin_port), inet_ntoa(from.sin_addr));
379 syslog(LOG_WARNING, "short packet from %s",
380 inet_ntoa(from.sin_addr));
383 if (wd.wd_vers != WHODVERSION)
385 if (wd.wd_type != WHODTYPE_STATUS)
387 if (!verify(wd.wd_hostname, sizeof(wd.wd_hostname))) {
388 syslog(LOG_WARNING, "malformed host name from %s",
389 inet_ntoa(from.sin_addr));
392 (void) snprintf(path, sizeof(path), "whod.%s", wd.wd_hostname);
394 * Rather than truncating and growing the file each time,
395 * use ftruncate if size is less than previous size.
397 whod = openat(dirfd, path, O_WRONLY | O_CREAT, 0644);
399 syslog(LOG_WARNING, "%s: %m", path);
402 cap_rights_init(&rights, CAP_FSTAT, CAP_FTRUNCATE, CAP_WRITE);
403 if (caph_rights_limit(whod, &rights) < 0) {
404 syslog(LOG_WARNING, "cap_rights_limit: %m");
407 #if ENDIAN != BIG_ENDIAN
412 n = (cc - WHDRSIZE) / sizeof(struct whoent);
413 /* undo header byte swapping before writing to file */
414 wd.wd_sendtime = ntohl(wd.wd_sendtime);
415 for (i = 0; i < 3; i++)
416 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
417 wd.wd_boottime = ntohl(wd.wd_boottime);
419 for (i = 0; i < n; i++) {
420 we->we_idle = ntohl(we->we_idle);
421 we->we_utmp.out_time =
422 ntohl(we->we_utmp.out_time);
428 wd.wd_recvtime = _time_to_int(t);
429 (void) write(whod, (char *)&wd, cc);
430 if (fstat(whod, &st) < 0 || st.st_size > cc)
447 struct whoent *we, *wend;
453 if (sendcount % 10 == 0)
456 wend = &mywd.wd_we[1024 / sizeof(struct whoent)];
458 while ((ut = getutxent()) != NULL && we < wend) {
459 if (ut->ut_type != USER_PROCESS)
461 strncpy(we->we_utmp.out_line, ut->ut_line,
462 sizeof(we->we_utmp.out_line));
463 strncpy(we->we_utmp.out_name, ut->ut_user,
464 sizeof(we->we_utmp.out_name));
465 we->we_utmp.out_time =
466 htonl(_time_to_time32(ut->ut_tv.tv_sec));
471 if (chdir(_PATH_DEV) < 0) {
472 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
476 for (we = mywd.wd_we; we < wend; we++) {
477 if (stat(we->we_utmp.out_line, &stb) >= 0)
478 we->we_idle = htonl(now - stb.st_atime);
480 (void) getloadavg(avenrun,
481 sizeof(avenrun) / sizeof(avenrun[0]));
482 for (i = 0; i < 3; i++)
483 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
484 cc = (char *)wend - (char *)&mywd;
485 mywd.wd_sendtime = htonl(_time_to_time32(time(NULL)));
486 mywd.wd_vers = WHODVERSION;
487 mywd.wd_type = WHODTYPE_STATUS;
488 if (multicast_mode == SCOPED_MULTICAST) {
489 (void) sendto(s, (char *)&mywd, cc, 0,
490 (struct sockaddr *)&multicast_addr,
491 sizeof(multicast_addr));
493 for (np = neighbors; np != NULL; np = np->n_next) {
494 if (multicast_mode == PER_INTERFACE_MULTICAST &&
495 (np->n_flags & IFF_MULTICAST) != 0) {
497 * Select the outgoing interface for the
500 if (setsockopt(s, IPPROTO_IP,
502 &(((struct sockaddr_in *)np->n_addr)->sin_addr),
503 sizeof(struct in_addr)) < 0) {
505 "setsockopt IP_MULTICAST_IF: %m");
508 (void) sendto(s, (char *)&mywd, cc, 0,
509 (struct sockaddr *)&multicast_addr,
510 sizeof(multicast_addr));
512 (void) sendto(s, (char *)&mywd, cc, 0,
513 np->n_addr, np->n_addrlen);
517 if (chdir(_PATH_RWHODIR) < 0) {
518 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
521 if (waitpid(pid_child_receiver, &status, WNOHANG) ==
522 pid_child_receiver) {
530 getboottime(int signo __unused)
537 mib[1] = KERN_BOOTTIME;
539 if (sysctl(mib, nitems(mib), &tm, &size, NULL, 0) == -1) {
540 syslog(LOG_ERR, "cannot get boottime: %m");
543 mywd.wd_boottime = htonl(_time_to_time32(tm.tv_sec));
547 quit(const char *msg)
550 syslog(LOG_ERR, "%s", msg);
555 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
560 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
561 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
562 if ((rtinfo->rti_addrs & (1 << i)) == 0)
564 sa = (struct sockaddr *)cp;
565 rtinfo->rti_info[i] = sa;
571 * Figure out device configuration and select
572 * networks which deserve status information.
578 struct if_msghdr *ifm;
579 struct ifa_msghdr *ifam;
580 struct sockaddr_dl *sdl;
582 int mib[6], flags, lflags, len;
583 char *buf, *lim, *next;
584 struct rt_addrinfo info;
587 if (multicast_mode != NO_MULTICAST) {
588 multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
589 multicast_addr.sin_port = sp->s_port;
592 if (multicast_mode == SCOPED_MULTICAST) {
596 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
597 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
598 if (setsockopt(so, IPPROTO_IP, IP_ADD_MEMBERSHIP,
599 &mreq, sizeof(mreq)) < 0) {
601 "setsockopt IP_ADD_MEMBERSHIP: %m");
604 ttl = multicast_scope;
605 if (setsockopt(so, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
608 "setsockopt IP_MULTICAST_TTL: %m");
618 mib[4] = NET_RT_IFLIST;
620 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
621 quit("route-sysctl-estimate");
622 if ((buf = malloc(needed)) == NULL)
624 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
625 quit("actual retrieval of interface table");
628 sdl = NULL; /* XXX just to keep gcc -Wall happy */
629 for (next = buf; next < lim; next += ifm->ifm_msglen) {
630 ifm = (struct if_msghdr *)next;
631 if (ifm->ifm_type == RTM_IFINFO) {
632 sdl = (struct sockaddr_dl *)(ifm + 1);
633 flags = ifm->ifm_flags;
636 if ((flags & IFF_UP) == 0)
638 lflags = IFF_BROADCAST | iff_flag;
639 if (multicast_mode == PER_INTERFACE_MULTICAST)
640 lflags |= IFF_MULTICAST;
641 if ((flags & lflags) == 0)
643 if (ifm->ifm_type != RTM_NEWADDR)
644 quit("out of sync parsing NET_RT_IFLIST");
645 ifam = (struct ifa_msghdr *)ifm;
646 info.rti_addrs = ifam->ifam_addrs;
647 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
649 /* gag, wish we could get rid of Internet dependencies */
650 #define dstaddr info.rti_info[RTAX_BRD]
651 #define ifaddr info.rti_info[RTAX_IFA]
652 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
653 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
654 if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
656 PORT_SA(dstaddr) = sp->s_port;
657 for (np = neighbors; np != NULL; np = np->n_next) {
658 if (memcmp(sdl->sdl_data, np->n_name,
659 sdl->sdl_nlen) == 0 &&
660 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) {
666 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
669 quit("malloc of neighbor structure");
672 np->n_addr = (struct sockaddr *)(np + 1);
673 np->n_addrlen = dstaddr->sa_len;
674 np->n_name = np->n_addrlen + (char *)np->n_addr;
675 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
676 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
677 if (multicast_mode == PER_INTERFACE_MULTICAST &&
678 (flags & IFF_MULTICAST) != 0 &&
679 (flags & IFF_LOOPBACK) == 0) {
682 memcpy((char *)np->n_addr, (char *)ifaddr,
684 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
685 mreq.imr_interface.s_addr =
686 ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
687 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
688 &mreq, sizeof(mreq)) < 0) {
690 "setsockopt IP_ADD_MEMBERSHIP: %m");
692 /* Fall back to broadcast on this if. */
693 np->n_flags &= ~IFF_MULTICAST;
700 np->n_next = neighbors;
709 Sendto(int s, const void *buf, size_t cc, int flags, const struct sockaddr *to,
714 struct sockaddr_in *sin;
716 w = (struct whod *)buf;
717 sin = (struct sockaddr_in *)to;
718 printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr),
719 ntohs(sin->sin_port));
720 printf("hostname %s %s\n", w->wd_hostname,
721 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
722 printf("load %4.2f, %4.2f, %4.2f\n",
723 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
724 ntohl(w->wd_loadav[2]) / 100.0);
726 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
727 time_t t = _time32_to_time(ntohl(we->we_utmp.out_time));
729 printf("%-8.8s %s:%s %.12s", we->we_utmp.out_name,
730 w->wd_hostname, we->we_utmp.out_line, ctime(&t) + 4);
731 we->we_idle = ntohl(we->we_idle) / 60;
732 if (we->we_idle != 0) {
733 if (we->we_idle >= 100 * 60)
734 we->we_idle = 100 * 60 - 1;
735 if (we->we_idle >= 60)
736 printf(" %2d", we->we_idle / 60);
739 printf(":%02d", we->we_idle % 60);
746 interval(int time, char *updown)
748 static char resbuf[32];
749 int days, hours, minutes;
751 if (time < 0 || time > 3 * 30 * 24 * 60 * 60) {
752 (void) sprintf(resbuf, " %s ??:??", updown);
755 minutes = (time + 59) / 60; /* round to minutes */
756 hours = minutes / 60;
761 (void) sprintf(resbuf, "%s %2d+%02d:%02d",
762 updown, days, hours, minutes);
764 (void) sprintf(resbuf, "%s %2d:%02d",
765 updown, hours, minutes);