rwhod(8): Prevent clobbering the neighboring field by a wrong cast.
[dragonfly.git] / usr.sbin / rwhod / rwhod.c
CommitLineData
984263bc
MD
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.
1de703da
MD
32 *
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 $
9b5ae8ee 36 * $DragonFly: src/usr.sbin/rwhod/rwhod.c,v 1.24 2007/12/27 15:29:40 matthias Exp $
984263bc
MD
37 */
38
984263bc
MD
39#include <sys/param.h>
40#include <sys/socket.h>
41#include <sys/stat.h>
42#include <sys/signal.h>
43#include <sys/ioctl.h>
44#include <sys/sysctl.h>
45
46#include <net/if.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>
52
53#include <ctype.h>
54#include <err.h>
55#include <errno.h>
56#include <fcntl.h>
9259569f 57#include <libutil.h>
984263bc
MD
58#include <netdb.h>
59#include <paths.h>
82ece171 60#include <poll.h>
984263bc
MD
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <syslog.h>
65#include <unistd.h>
66#include <utmp.h>
67#include <pwd.h>
68#include <grp.h>
69
70/*
71 * This version of Berkeley's rwhod has been modified to use IP multicast
72 * datagrams, under control of a new command-line option:
73 *
74 * rwhod -m causes rwhod to use IP multicast (instead of
75 * broadcast or unicast) on all interfaces that have
76 * the IFF_MULTICAST flag set in their "ifnet" structs
77 * (excluding the loopback interface). The multicast
78 * reports are sent with a time-to-live of 1, to prevent
79 * forwarding beyond the directly-connected subnet(s).
80 *
81 * rwhod -m <ttl> causes rwhod to send IP multicast datagrams with a
82 * time-to-live of <ttl>, via a SINGLE interface rather
83 * than all interfaces. <ttl> must be between 0 and
84 * MAX_MULTICAST_SCOPE, defined below. Note that "-m 1"
85 * is different than "-m", in that "-m 1" specifies
86 * transmission on one interface only.
87 *
88 * When "-m" is used without a <ttl> argument, the program accepts multicast
89 * rwhod reports from all multicast-capable interfaces. If a <ttl> argument
90 * is given, it accepts multicast reports from only one interface, the one
91 * on which reports are sent (which may be controlled via the host's routing
92 * table). Regardless of the "-m" option, the program accepts broadcast or
93 * unicast reports from all interfaces. Thus, this program will hear the
94 * reports of old, non-multicasting rwhods, but, if multicasting is used,
95 * those old rwhods won't hear the reports generated by this program.
96 *
97 * -- Steve Deering, Stanford University, February 1989
98 */
99
100#define UNPRIV_USER "daemon"
101#define UNPRIV_GROUP "daemon"
102
35853d73 103#define WITH_ERRNO 1 /* Write to syslog with errno (%m) */
d80a19a0 104#define WITHOUT_ERRNO 0 /* Write to syslog without errno */
35853d73 105
984263bc
MD
106#define NO_MULTICAST 0 /* multicast modes */
107#define PER_INTERFACE_MULTICAST 1
108#define SCOPED_MULTICAST 2
109
110#define MAX_MULTICAST_SCOPE 32 /* "site-wide", by convention */
111
112#define INADDR_WHOD_GROUP (u_long)0xe0000103 /* 224.0.1.3 */
113 /* (belongs in protocols/rwhod.h) */
114
115int insecure_mode;
116int quiet_mode;
117int iff_flag = IFF_POINTOPOINT;
118int multicast_mode = NO_MULTICAST;
119int multicast_scope;
6005b83c 120struct sockaddr_in multicast_addr;
984263bc 121
984263bc
MD
122char myname[MAXHOSTNAMELEN];
123
124/*
125 * We communicate with each neighbor in a list constructed at the time we're
126 * started up. Neighbors are currently directly connected via a hardware
127 * interface.
128 */
129struct neighbor {
130 struct neighbor *n_next;
131 char *n_name; /* interface name */
132 struct sockaddr *n_addr; /* who to send to */
133 int n_addrlen; /* size of address */
134 int n_flags; /* should forward?, interface flags */
135};
136
137struct neighbor *neighbors;
138struct whod mywd;
139struct servent *sp;
140int s, utmpf;
82ece171 141volatile sig_atomic_t onsighup;
984263bc
MD
142
143#define WHDRSIZE (sizeof(mywd) - sizeof(mywd.wd_we))
144
2d8a3be7
EN
145void run_as(uid_t *, gid_t *);
146int configure(int);
6005b83c 147void getboottime(void);
82ece171
LF
148void send_host_information(void);
149void hup(int);
35853d73 150void quit(const char *, int);
2d8a3be7
EN
151void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
152int verify(char *, int);
82ece171 153void handleread(int);
2d8a3be7 154static void usage(void);
82ece171 155void timeadd(struct timeval *, time_t, struct timeval *);
6c8d1328 156
984263bc 157#ifdef DEBUG
62eb0ed9
LF
158char *interval(int, const char *);
159ssize_t Sendto(int, const void *, size_t, int,
2d8a3be7 160 const struct sockaddr *, int);
6c8d1328
LF
161#else
162#define Sendto sendto
984263bc
MD
163#endif
164
165int
320b887a 166main(int argc, char *argv[])
984263bc 167{
82ece171
LF
168 struct pollfd pfd[1];
169 char *ep, *cp;
984263bc 170 int on = 1;
7eea36fc 171 int send_time = 180; /* Default time, 180 seconds (3 minutes) */
f015bdb1 172 struct sockaddr_in m_sin;
984263bc
MD
173 uid_t unpriv_uid;
174 gid_t unpriv_gid;
dba7dc5a 175 long tmp;
82ece171
LF
176 time_t delta = 0;
177 struct timeval next, now;
984263bc
MD
178
179 if (getuid())
180 errx(1, "not super user");
181
182 run_as(&unpriv_uid, &unpriv_gid);
183
184 argv++; argc--;
185 while (argc > 0 && *argv[0] == '-') {
186 if (strcmp(*argv, "-m") == 0) {
dba7dc5a
LF
187 if (argc > 1 && *(argv + 1)[0] != '-') {
188 /* Argument has been given */
984263bc 189 argv++, argc--;
dba7dc5a
LF
190 multicast_mode = SCOPED_MULTICAST;
191 tmp = strtol(*argv, &ep, 10);
192 if (*ep != '\0' || tmp < INT_MIN || tmp > INT_MAX)
193 errx(1, "invalid ttl: %s", *argv);
194 multicast_scope = (int)tmp;
984263bc
MD
195 if (multicast_scope > MAX_MULTICAST_SCOPE)
196 errx(1, "ttl must not exceed %u",
197 MAX_MULTICAST_SCOPE);
198 }
199 else multicast_mode = PER_INTERFACE_MULTICAST;
7eea36fc
LF
200 } else if (strcmp(*argv, "-g") == 0) {
201 if (argc > 1 && *(argv + 1)[0] != '-') {
202 argv++, argc--;
203 send_time = (int)strtol(*argv, &ep, 10);
204 if (send_time <= 0)
205 errx(1, "time must be greater than 0");
206
207 if (ep[0] != '\0') {
208 if (ep[1] != '\0')
209 errx(1, "invalid argument: %s", *argv);
210 if (*ep == 'M' || *ep == 'm') {
211 /* Time in minutes. */
212 send_time *= 60;
213 } else
214 errx(1, "invalid argument: %s", *argv);
215 }
216
217 if (send_time > 180)
218 errx(1, "cannot be greater than 180 seconds (3 minutes)");
219 } else
220 errx(1, "missing argument");
221 } else if (strcmp(*argv, "-i") == 0)
984263bc
MD
222 insecure_mode = 1;
223 else if (strcmp(*argv, "-l") == 0)
224 quiet_mode = 1;
225 else if (strcmp(*argv, "-p") == 0)
226 iff_flag = 0;
227 else
228 usage();
229 argv++, argc--;
230 }
231 if (argc > 0)
232 usage();
233#ifndef DEBUG
234 daemon(1, 0);
9b5ae8ee 235 pidfile(NULL);
984263bc 236#endif
82ece171 237 signal(SIGHUP, hup);
984263bc
MD
238 openlog("rwhod", LOG_PID, LOG_DAEMON);
239 sp = getservbyname("who", "udp");
35853d73
LF
240 if (sp == NULL)
241 quit("udp/who: unknown service", WITHOUT_ERRNO);
242
243 if (chdir(_PATH_RWHODIR) < 0)
244 quit(_PATH_RWHODIR, WITH_ERRNO);
245
984263bc
MD
246 /*
247 * Establish host name as returned by system.
248 */
35853d73
LF
249 if (gethostname(myname, sizeof(myname)) < 0)
250 quit("gethostname", WITH_ERRNO);
251
17b61719 252 if ((cp = strchr(myname, '.')) != NULL)
984263bc 253 *cp = '\0';
c6873906 254 strlcpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname));
984263bc 255 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
35853d73
LF
256 if (utmpf < 0)
257 quit(_PATH_UTMP, WITH_ERRNO);
258
6005b83c 259 getboottime();
35853d73
LF
260 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
261 quit("socket", WITH_ERRNO);
262
263 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
264 quit("setsockopt SO_BROADCAST", WITH_ERRNO);
265
f015bdb1
JS
266 memset(&m_sin, 0, sizeof(m_sin));
267 m_sin.sin_len = sizeof(m_sin);
268 m_sin.sin_family = AF_INET;
269 m_sin.sin_port = sp->s_port;
35853d73
LF
270 if (bind(s, (struct sockaddr *)&m_sin, sizeof(m_sin)) < 0)
271 quit("bind", WITH_ERRNO);
272
984263bc
MD
273 setgid(unpriv_gid);
274 setgroups(1, &unpriv_gid); /* XXX BOGUS groups[0] = egid */
275 setuid(unpriv_uid);
276 if (!configure(s))
277 exit(1);
82ece171 278
984263bc 279 if (!quiet_mode) {
82ece171 280 send_host_information();
7eea36fc 281 delta = send_time;
82ece171
LF
282 gettimeofday(&now, NULL);
283 timeadd(&now, delta, &next);
984263bc 284 }
82ece171
LF
285
286 pfd[0].fd = s;
7eea36fc 287 pfd[0].events = POLLIN;
82ece171 288
984263bc 289 for (;;) {
82ece171 290 int n;
984263bc 291
82ece171
LF
292 n = poll(pfd, 1, 1000);
293
294 if (onsighup) {
295 onsighup = 0;
8c37a827 296 getboottime();
8c37a827
JS
297 }
298
82ece171
LF
299 if (n == 1)
300 handleread(s);
301 if (!quiet_mode) {
302 gettimeofday(&now, NULL);
303 if (now.tv_sec > next.tv_sec) {
304 send_host_information();
305 timeadd(&now, delta, &next);
6005b83c 306 }
984263bc 307 }
82ece171
LF
308 }
309}
310
311void
312timeadd(struct timeval *now, time_t delta, struct timeval *next)
313{
314 (next)->tv_sec = (now)->tv_sec + delta;
315}
316
317void
318handleread(int sock)
319{
320 struct sockaddr_in from;
321 struct stat st;
322 char path[64];
323 struct whod wd;
324 int cc, whod;
325 socklen_t len = sizeof(from);
326
82ece171
LF
327 cc = recvfrom(sock, (char *)&wd, sizeof(struct whod), 0,
328 (struct sockaddr *)&from, &len);
329 if (cc == 0)
330 return;
331
332 if (cc < 0 && errno != EINTR) {
333 syslog(LOG_WARNING, "recv: %m");
334 return;
335 }
336
337 if (from.sin_port != sp->s_port && !insecure_mode) {
338 syslog(LOG_WARNING, "%d: bad source port from %s",
339 ntohs(from.sin_port), inet_ntoa(from.sin_addr));
340 return;
341 }
342 if (cc < (int)WHDRSIZE) {
343 syslog(LOG_WARNING, "short packet from %s",
344 inet_ntoa(from.sin_addr));
345 return;
346 }
347 if (wd.wd_vers != WHODVERSION)
348 return;
349 if (wd.wd_type != WHODTYPE_STATUS)
350 return;
351 if (!verify(wd.wd_hostname, sizeof wd.wd_hostname)) {
352 syslog(LOG_WARNING, "malformed host name from %s",
353 inet_ntoa(from.sin_addr));
354 return;
355 }
356 snprintf(path, sizeof path, "whod.%s", wd.wd_hostname);
357 /*
358 * Rather than truncating and growing the file each time,
359 * use ftruncate if size is less than previous size.
360 */
361 whod = open(path, O_WRONLY | O_CREAT, 0644);
362 if (whod < 0) {
363 syslog(LOG_WARNING, "%s: %m", path);
364 return;
365 }
984263bc 366#if ENDIAN != BIG_ENDIAN
82ece171
LF
367 {
368 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
369 struct whoent *we;
370
371 /* undo header byte swapping before writing to file */
372 wd.wd_sendtime = ntohl(wd.wd_sendtime);
373 for (i = 0; i < 3; i++)
374 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
375 wd.wd_boottime = ntohl(wd.wd_boottime);
376 we = wd.wd_we;
377 for (i = 0; i < n; i++) {
378 we->we_idle = ntohl(we->we_idle);
379 we->we_utmp.out_time =
380 ntohl(we->we_utmp.out_time);
381 we++;
984263bc 382 }
984263bc 383 }
82ece171 384#endif
53aed754 385 wd.wd_recvtime = time(NULL);
82ece171
LF
386 write(whod, (char *)&wd, cc);
387 if (fstat(whod, &st) < 0 || st.st_size > cc)
388 ftruncate(whod, cc);
389 close(whod);
984263bc
MD
390}
391
392static void
320b887a 393usage(void)
984263bc
MD
394{
395 fprintf(stderr, "usage: rwhod [-i] [-p] [-l] [-m [ttl]]\n");
396 exit(1);
397}
398
399void
82ece171
LF
400hup(int signo __unused)
401{
402 onsighup = 1;
403}
404
405void
320b887a 406run_as(uid_t *uid, gid_t *gid)
984263bc
MD
407{
408 struct passwd *pw;
409 struct group *gr;
410
411 pw = getpwnam(UNPRIV_USER);
35853d73
LF
412 if (!pw)
413 quit("getpwnam(daemon)", WITH_ERRNO);
414
984263bc
MD
415 *uid = pw->pw_uid;
416
417 gr = getgrnam(UNPRIV_GROUP);
35853d73
LF
418 if (!gr)
419 quit("getgrnam(daemon)", WITH_ERRNO);
420
984263bc
MD
421 *gid = gr->gr_gid;
422}
423
424/*
425 * Check out host name for unprintables
426 * and other funnies before allowing a file
427 * to be created. Sorry, but blanks aren't allowed.
428 */
429int
320b887a 430verify(char *name, int maxlen)
984263bc 431{
320b887a 432 int size = 0;
984263bc
MD
433
434 while (*name && size < maxlen - 1) {
435 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
436 return (0);
437 name++, size++;
438 }
439 *name = '\0';
440 return (size > 0);
441}
442
443int utmptime;
444int utmpent;
6005b83c 445int utmpsize;
984263bc
MD
446struct utmp *utmp;
447int alarmcount;
448
449void
82ece171 450send_host_information(void)
984263bc 451{
320b887a
EN
452 struct neighbor *np;
453 struct whoent *we = mywd.wd_we, *wlast;
454 int i;
984263bc
MD
455 struct stat stb;
456 double avenrun[3];
457 time_t now;
458 int cc;
459
460 now = time(NULL);
461 if (alarmcount % 10 == 0)
6005b83c 462 getboottime();
984263bc 463 alarmcount++;
71126e33 464 fstat(utmpf, &stb);
984263bc
MD
465 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
466 utmptime = stb.st_mtime;
467 if (stb.st_size > utmpsize) {
468 utmpsize = stb.st_size + 10 * sizeof(struct utmp);
b2d33a7d
LF
469 utmp = reallocf(utmp, utmpsize);
470 if (utmp == NULL) {
35853d73 471 syslog(LOG_WARNING, "malloc failed: %m");
984263bc 472 utmpsize = 0;
82ece171 473 return;
984263bc
MD
474 }
475 }
71126e33 476 lseek(utmpf, (off_t)0, L_SET);
984263bc
MD
477 cc = read(utmpf, (char *)utmp, stb.st_size);
478 if (cc < 0) {
479 syslog(LOG_ERR, "read(%s): %m", _PATH_UTMP);
82ece171 480 return;
984263bc
MD
481 }
482 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
483 utmpent = cc / sizeof(struct utmp);
484 for (i = 0; i < utmpent; i++)
485 if (utmp[i].ut_name[0]) {
486 memcpy(we->we_utmp.out_line, utmp[i].ut_line,
487 sizeof(utmp[i].ut_line));
488 memcpy(we->we_utmp.out_name, utmp[i].ut_name,
489 sizeof(utmp[i].ut_name));
490 we->we_utmp.out_time = htonl(utmp[i].ut_time);
491 if (we >= wlast)
492 break;
493 we++;
494 }
495 utmpent = we - mywd.wd_we;
496 }
497
498 /*
499 * The test on utmpent looks silly---after all, if no one is
500 * logged on, why worry about efficiency?---but is useful on
501 * (e.g.) compute servers.
502 */
503 if (utmpent && chdir(_PATH_DEV)) {
504 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
505 exit(1);
506 }
35853d73 507
984263bc
MD
508 we = mywd.wd_we;
509 for (i = 0; i < utmpent; i++) {
510 if (stat(we->we_utmp.out_line, &stb) >= 0)
511 we->we_idle = htonl(now - stb.st_atime);
512 we++;
513 }
71126e33 514 getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
984263bc
MD
515 for (i = 0; i < 3; i++)
516 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
517 cc = (char *)we - (char *)&mywd;
518 mywd.wd_sendtime = htonl(time(0));
519 mywd.wd_vers = WHODVERSION;
520 mywd.wd_type = WHODTYPE_STATUS;
521 if (multicast_mode == SCOPED_MULTICAST) {
6c8d1328 522 Sendto(s, (char *)&mywd, cc, 0,
984263bc
MD
523 (struct sockaddr *)&multicast_addr,
524 sizeof(multicast_addr));
525 }
526 else for (np = neighbors; np != NULL; np = np->n_next) {
527 if (multicast_mode == PER_INTERFACE_MULTICAST &&
528 np->n_flags & IFF_MULTICAST) {
529 /*
530 * Select the outgoing interface for the multicast.
531 */
532 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
533 &(((struct sockaddr_in *)np->n_addr)->sin_addr),
534 sizeof(struct in_addr)) < 0) {
35853d73 535 quit("setsockopt IP_MULTICAST_IF", WITH_ERRNO);
984263bc 536 }
6c8d1328 537 Sendto(s, (char *)&mywd, cc, 0,
984263bc
MD
538 (struct sockaddr *)&multicast_addr,
539 sizeof(multicast_addr));
6c8d1328 540 } else Sendto(s, (char *)&mywd, cc, 0,
71126e33 541 np->n_addr, np->n_addrlen);
984263bc
MD
542 }
543 if (utmpent && chdir(_PATH_RWHODIR)) {
544 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
545 exit(1);
546 }
984263bc
MD
547}
548
549void
6005b83c 550getboottime(void)
984263bc
MD
551{
552 int mib[2];
553 size_t size;
554 struct timeval tm;
555
556 mib[0] = CTL_KERN;
557 mib[1] = KERN_BOOTTIME;
558 size = sizeof(tm);
35853d73
LF
559 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1)
560 quit("cannot get boottime", WITH_ERRNO);
561
984263bc
MD
562 mywd.wd_boottime = htonl(tm.tv_sec);
563}
564
35853d73
LF
565/*
566 * If wrterrno == WITH_ERRNO, we will print
567 * errno. If not, we leave errno out.
568 */
984263bc 569void
35853d73 570quit(const char *msg, int wrterrno)
984263bc 571{
35853d73
LF
572 if (wrterrno)
573 syslog(LOG_ERR, "%s: %m", msg);
574 else
575 syslog(LOG_ERR, "%s", msg);
984263bc
MD
576 exit(1);
577}
578
579#define ROUNDUP(a) \
580 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
581#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
582
583void
320b887a 584rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
984263bc 585{
320b887a
EN
586 struct sockaddr *sa;
587 int i;
984263bc
MD
588
589 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
590 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
591 if ((rtinfo->rti_addrs & (1 << i)) == 0)
592 continue;
593 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
594 ADVANCE(cp, sa);
595 }
596}
597
598/*
599 * Figure out device configuration and select
600 * networks which deserve status information.
601 */
602int
f015bdb1 603configure(int c_sock)
984263bc 604{
320b887a
EN
605 struct neighbor *np;
606 struct if_msghdr *ifm;
607 struct ifa_msghdr *ifam;
984263bc
MD
608 struct sockaddr_dl *sdl;
609 size_t needed;
610 int mib[6], flags = 0, len;
611 char *buf, *lim, *next;
612 struct rt_addrinfo info;
613
614 if (multicast_mode != NO_MULTICAST) {
f015bdb1
JS
615 multicast_addr.sin_len = sizeof(multicast_addr);
616 multicast_addr.sin_family = AF_INET;
984263bc
MD
617 multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
618 multicast_addr.sin_port = sp->s_port;
619 }
620
621 if (multicast_mode == SCOPED_MULTICAST) {
622 struct ip_mreq mreq;
623 unsigned char ttl;
624
625 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
626 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
f015bdb1 627 if (setsockopt(c_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
984263bc
MD
628 &mreq, sizeof(mreq)) < 0) {
629 syslog(LOG_ERR,
630 "setsockopt IP_ADD_MEMBERSHIP: %m");
631 return(0);
632 }
633 ttl = multicast_scope;
f015bdb1 634 if (setsockopt(c_sock, IPPROTO_IP, IP_MULTICAST_TTL,
984263bc
MD
635 &ttl, sizeof(ttl)) < 0) {
636 syslog(LOG_ERR,
637 "setsockopt IP_MULTICAST_TTL: %m");
638 return(0);
639 }
640 return(1);
641 }
642
643 mib[0] = CTL_NET;
644 mib[1] = PF_ROUTE;
645 mib[2] = 0;
646 mib[3] = AF_INET;
647 mib[4] = NET_RT_IFLIST;
648 mib[5] = 0;
649 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
35853d73 650 quit("route-sysctl-estimate", WITH_ERRNO);
984263bc 651 if ((buf = malloc(needed)) == NULL)
35853d73 652 quit("malloc", WITH_ERRNO);
984263bc 653 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
35853d73 654 quit("actual retrieval of interface table", WITH_ERRNO);
984263bc
MD
655 lim = buf + needed;
656
657 sdl = NULL; /* XXX just to keep gcc -Wall happy */
658 for (next = buf; next < lim; next += ifm->ifm_msglen) {
659 ifm = (struct if_msghdr *)next;
660 if (ifm->ifm_type == RTM_IFINFO) {
661 sdl = (struct sockaddr_dl *)(ifm + 1);
662 flags = ifm->ifm_flags;
663 continue;
664 }
665 if ((flags & IFF_UP) == 0 ||
666 (flags & (((multicast_mode == PER_INTERFACE_MULTICAST) ?
667 IFF_MULTICAST : 0) |
668 IFF_BROADCAST|iff_flag)) == 0)
669 continue;
670 if (ifm->ifm_type != RTM_NEWADDR)
35853d73 671 quit("out of sync parsing NET_RT_IFLIST", WITHOUT_ERRNO);
984263bc
MD
672 ifam = (struct ifa_msghdr *)ifm;
673 info.rti_addrs = ifam->ifam_addrs;
674 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
675 &info);
676 /* gag, wish we could get rid of Internet dependencies */
677#define dstaddr info.rti_info[RTAX_BRD]
678#define ifaddr info.rti_info[RTAX_IFA]
679#define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
680#define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
681 if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
682 continue;
683 PORT_SA(dstaddr) = sp->s_port;
684 for (np = neighbors; np != NULL; np = np->n_next)
685 if (memcmp(sdl->sdl_data, np->n_name,
686 sdl->sdl_nlen) == 0 &&
687 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
688 break;
689 if (np != NULL)
690 continue;
691 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
692 np = (struct neighbor *)malloc(len);
693 if (np == NULL)
35853d73 694 quit("malloc of neighbor structure", WITH_ERRNO);
984263bc
MD
695 memset(np, 0, len);
696 np->n_flags = flags;
697 np->n_addr = (struct sockaddr *)(np + 1);
698 np->n_addrlen = dstaddr->sa_len;
699 np->n_name = np->n_addrlen + (char *)np->n_addr;
700 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
701 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
702 if (multicast_mode == PER_INTERFACE_MULTICAST &&
703 (flags & IFF_MULTICAST) &&
704 !(flags & IFF_LOOPBACK)) {
705 struct ip_mreq mreq;
706
707 memcpy((char *)np->n_addr, (char *)ifaddr,
708 np->n_addrlen);
709 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
710 mreq.imr_interface.s_addr =
711 ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
712 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
713 &mreq, sizeof(mreq)) < 0) {
714 syslog(LOG_ERR,
715 "setsockopt IP_ADD_MEMBERSHIP: %m");
716#if 0
717 /* Fall back to broadcast on this if. */
718 np->n_flags &= ~IFF_MULTICAST;
719#else
720 free((char *)np);
721 continue;
722#endif
723 }
724 }
725 np->n_next = neighbors;
726 neighbors = np;
727 }
728 free(buf);
729 return (1);
730}
731
732#ifdef DEBUG
62eb0ed9
LF
733ssize_t
734Sendto(int s_debug, const void *buf, size_t cc, int flags,
320b887a 735 const struct sockaddr *to, int tolen)
984263bc 736{
62eb0ed9
LF
737 const struct whod *w = (const struct whod *)buf;
738 const struct whoent *we;
739 const struct sockaddr_in *sock_in = (const struct sockaddr_in *)to;
740 ssize_t ret;
741 int idle_time;
742
743 ret = sendto(s_debug, buf, cc, flags, to, tolen);
984263bc 744
dd612bae 745 printf("sendto %s:%d\n", inet_ntoa(sock_in->sin_addr),
62eb0ed9 746 ntohs(sock_in->sin_port));
984263bc
MD
747 printf("hostname %s %s\n", w->wd_hostname,
748 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
749 printf("load %4.2f, %4.2f, %4.2f\n",
750 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
751 ntohl(w->wd_loadav[2]) / 100.0);
752 cc -= WHDRSIZE;
753 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
754 time_t t = ntohl(we->we_utmp.out_time);
62eb0ed9 755 idle_time = we->we_idle;
984263bc
MD
756 printf("%-8.8s %s:%s %.12s",
757 we->we_utmp.out_name,
758 w->wd_hostname, we->we_utmp.out_line,
759 ctime(&t)+4);
62eb0ed9
LF
760 idle_time = ntohl(we->we_idle) / 60;
761 if (idle_time) {
762 if (idle_time >= 100*60)
763 idle_time = 100*60 - 1;
764 if (idle_time >= 60)
765 printf(" %2d", idle_time / 60);
984263bc
MD
766 else
767 printf(" ");
62eb0ed9 768 printf(":%02d", idle_time % 60);
984263bc
MD
769 }
770 printf("\n");
771 }
62eb0ed9 772 return(ret);
984263bc
MD
773}
774
775char *
62eb0ed9 776interval(int inter_time, const char *updown)
984263bc
MD
777{
778 static char resbuf[32];
779 int days, hours, minutes;
780
62eb0ed9 781 if (inter_time < 0 || inter_time > 3*30*24*60*60) {
9c465ab2 782 snprintf(resbuf, sizeof(resbuf), " %s ??:??", updown);
984263bc
MD
783 return (resbuf);
784 }
62eb0ed9 785 minutes = (inter_time + 59) / 60; /* round to minutes */
984263bc
MD
786 hours = minutes / 60; minutes %= 60;
787 days = hours / 24; hours %= 24;
788 if (days)
9c465ab2 789 snprintf(resbuf, sizeof(resbuf), "%s %2d+%02d:%02d",
984263bc
MD
790 updown, days, hours, minutes);
791 else
9c465ab2 792 snprintf(resbuf, sizeof(resbuf), "%s %2d:%02d",
984263bc
MD
793 updown, hours, minutes);
794 return (resbuf);
795}
796#endif