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