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