rwhod.c revision 1.8 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.8 1994/05/29 02:53:23 jtc 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 if (!verify(wd.wd_hostname)) {
197 syslog(LOG_WARNING, "malformed host name from %x",
198 from.sin_addr);
199 continue;
200 }
201 (void) sprintf(path, "whod.%s", wd.wd_hostname);
202 /*
203 * Rather than truncating and growing the file each time,
204 * use ftruncate if size is less than previous size.
205 */
206 whod = open(path, O_WRONLY | O_CREAT, 0644);
207 if (whod < 0) {
208 syslog(LOG_WARNING, "%s: %m", path);
209 continue;
210 }
211 #if ENDIAN != BIG_ENDIAN
212 {
213 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
214 struct whoent *we;
215
216 /* undo header byte swapping before writing to file */
217 wd.wd_sendtime = ntohl(wd.wd_sendtime);
218 for (i = 0; i < 3; i++)
219 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
220 wd.wd_boottime = ntohl(wd.wd_boottime);
221 we = wd.wd_we;
222 for (i = 0; i < n; i++) {
223 we->we_idle = ntohl(we->we_idle);
224 we->we_utmp.out_time =
225 ntohl(we->we_utmp.out_time);
226 we++;
227 }
228 }
229 #endif
230 (void) time((time_t *)&wd.wd_recvtime);
231 (void) write(whod, (char *)&wd, cc);
232 if (fstat(whod, &st) < 0 || st.st_size > cc)
233 ftruncate(whod, cc);
234 (void) close(whod);
235 }
236 }
237
238 /*
239 * Check out host name for unprintables
240 * and other funnies before allowing a file
241 * to be created. Sorry, but blanks aren't allowed.
242 */
243 int
244 verify(name)
245 register char *name;
246 {
247 register int size = 0;
248
249 while (*name) {
250 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
251 return (0);
252 name++, size++;
253 }
254 return (size > 0);
255 }
256
257 int utmptime;
258 int utmpent;
259 int utmpsize = 0;
260 struct utmp *utmp;
261 int alarmcount;
262
263 void
264 onalrm(signo)
265 int signo;
266 {
267 register struct neighbor *np;
268 register struct whoent *we = mywd.wd_we, *wlast;
269 register int i;
270 struct stat stb;
271 double avenrun[3];
272 time_t now;
273 int cc;
274
275 now = time(NULL);
276 if (alarmcount % 10 == 0)
277 getboottime(0);
278 alarmcount++;
279 (void) fstat(utmpf, &stb);
280 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
281 utmptime = stb.st_mtime;
282 if (stb.st_size > utmpsize) {
283 utmpsize = stb.st_size + 10 * sizeof(struct utmp);
284 if (utmp)
285 utmp = (struct utmp *)realloc(utmp, utmpsize);
286 else
287 utmp = (struct utmp *)malloc(utmpsize);
288 if (! utmp) {
289 fprintf(stderr, "rwhod: malloc failed\n");
290 utmpsize = 0;
291 goto done;
292 }
293 }
294 (void) lseek(utmpf, (off_t)0, L_SET);
295 cc = read(utmpf, (char *)utmp, stb.st_size);
296 if (cc < 0) {
297 fprintf(stderr, "rwhod: %s: %s\n",
298 _PATH_UTMP, strerror(errno));
299 goto done;
300 }
301 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
302 utmpent = cc / sizeof(struct utmp);
303 for (i = 0; i < utmpent; i++)
304 if (utmp[i].ut_name[0]) {
305 memcpy(we->we_utmp.out_line, utmp[i].ut_line,
306 sizeof(utmp[i].ut_line));
307 memcpy(we->we_utmp.out_name, utmp[i].ut_name,
308 sizeof(utmp[i].ut_name));
309 we->we_utmp.out_time = htonl(utmp[i].ut_time);
310 if (we >= wlast)
311 break;
312 we++;
313 }
314 utmpent = we - mywd.wd_we;
315 }
316
317 /*
318 * The test on utmpent looks silly---after all, if no one is
319 * logged on, why worry about efficiency?---but is useful on
320 * (e.g.) compute servers.
321 */
322 if (utmpent && chdir(_PATH_DEV)) {
323 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
324 exit(1);
325 }
326 we = mywd.wd_we;
327 for (i = 0; i < utmpent; i++) {
328 if (stat(we->we_utmp.out_line, &stb) >= 0)
329 we->we_idle = htonl(now - stb.st_atime);
330 we++;
331 }
332 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
333 for (i = 0; i < 3; i++)
334 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
335 cc = (char *)we - (char *)&mywd;
336 mywd.wd_sendtime = htonl(time(0));
337 mywd.wd_vers = WHODVERSION;
338 mywd.wd_type = WHODTYPE_STATUS;
339 for (np = neighbors; np != NULL; np = np->n_next)
340 (void)sendto(s, (char *)&mywd, cc, 0,
341 np->n_addr, np->n_addrlen);
342 if (utmpent && chdir(_PATH_RWHODIR)) {
343 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
344 exit(1);
345 }
346 done:
347 (void) alarm(AL_INTERVAL);
348 }
349
350 void
351 getboottime(signo)
352 int signo;
353 {
354 int mib[2];
355 size_t size;
356 struct timeval tm;
357
358 mib[0] = CTL_KERN;
359 mib[1] = KERN_BOOTTIME;
360 size = sizeof(tm);
361 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
362 syslog(LOG_ERR, "cannot get boottime: %m");
363 exit(1);
364 }
365 mywd.wd_boottime = htonl(tm.tv_sec);
366 }
367
368 void
369 quit(msg)
370 char *msg;
371 {
372 syslog(LOG_ERR, msg);
373 exit(1);
374 }
375
376 #define ROUNDUP(a) \
377 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
378 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
379
380 void
381 rt_xaddrs(cp, cplim, rtinfo)
382 register caddr_t cp, cplim;
383 register struct rt_addrinfo *rtinfo;
384 {
385 register struct sockaddr *sa;
386 register int i;
387
388 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
389 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
390 if ((rtinfo->rti_addrs & (1 << i)) == 0)
391 continue;
392 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
393 ADVANCE(cp, sa);
394 }
395 }
396
397 /*
398 * Figure out device configuration and select
399 * networks which deserve status information.
400 */
401 int
402 configure(s)
403 int s;
404 {
405 register struct neighbor *np;
406 register struct if_msghdr *ifm;
407 register struct ifa_msghdr *ifam;
408 struct sockaddr_dl *sdl;
409 size_t needed;
410 int mib[6], flags = 0, len;
411 char *buf, *lim, *next;
412 struct rt_addrinfo info;
413
414 mib[0] = CTL_NET;
415 mib[1] = PF_ROUTE;
416 mib[2] = 0;
417 mib[3] = AF_INET;
418 mib[4] = NET_RT_IFLIST;
419 mib[5] = 0;
420 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
421 quit("route-sysctl-estimate");
422 if ((buf = malloc(needed)) == NULL)
423 quit("malloc");
424 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
425 quit("actual retrieval of interface table");
426 lim = buf + needed;
427
428 sdl = NULL; /* XXX just to keep gcc -Wall happy */
429 for (next = buf; next < lim; next += ifm->ifm_msglen) {
430 ifm = (struct if_msghdr *)next;
431 if (ifm->ifm_type == RTM_IFINFO) {
432 sdl = (struct sockaddr_dl *)(ifm + 1);
433 flags = ifm->ifm_flags;
434 continue;
435 }
436 if ((flags & IFF_UP) == 0 ||
437 (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
438 continue;
439 if (ifm->ifm_type != RTM_NEWADDR)
440 quit("out of sync parsing NET_RT_IFLIST");
441 ifam = (struct ifa_msghdr *)ifm;
442 info.rti_addrs = ifam->ifam_addrs;
443 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
444 &info);
445 /* gag, wish we could get rid of Internet dependencies */
446 #define dstaddr info.rti_info[RTAX_BRD]
447 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
448 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
449 if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
450 continue;
451 PORT_SA(dstaddr) = sp->s_port;
452 for (np = neighbors; np != NULL; np = np->n_next)
453 if (memcmp(sdl->sdl_data, np->n_name,
454 sdl->sdl_nlen) == 0 &&
455 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
456 break;
457 if (np != NULL)
458 continue;
459 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
460 np = (struct neighbor *)malloc(len);
461 if (np == NULL)
462 quit("malloc of neighbor structure");
463 memset(np, 0, len);
464 np->n_flags = flags;
465 np->n_addr = (struct sockaddr *)(np + 1);
466 np->n_addrlen = dstaddr->sa_len;
467 np->n_name = np->n_addrlen + (char *)np->n_addr;
468 np->n_next = neighbors;
469 neighbors = np;
470 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
471 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
472 }
473 free(buf);
474 return (1);
475 }
476
477 #ifdef DEBUG
478 void
479 Sendto(s, buf, cc, flags, to, tolen)
480 int s;
481 char *buf;
482 int cc, flags;
483 char *to;
484 int tolen;
485 {
486 register struct whod *w = (struct whod *)buf;
487 register struct whoent *we;
488 struct sockaddr_in *sin = (struct sockaddr_in *)to;
489
490 printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
491 printf("hostname %s %s\n", w->wd_hostname,
492 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
493 printf("load %4.2f, %4.2f, %4.2f\n",
494 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
495 ntohl(w->wd_loadav[2]) / 100.0);
496 cc -= WHDRSIZE;
497 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
498 time_t t = ntohl(we->we_utmp.out_time);
499 printf("%-8.8s %s:%s %.12s",
500 we->we_utmp.out_name,
501 w->wd_hostname, we->we_utmp.out_line,
502 ctime(&t)+4);
503 we->we_idle = ntohl(we->we_idle) / 60;
504 if (we->we_idle) {
505 if (we->we_idle >= 100*60)
506 we->we_idle = 100*60 - 1;
507 if (we->we_idle >= 60)
508 printf(" %2d", we->we_idle / 60);
509 else
510 printf(" ");
511 printf(":%02d", we->we_idle % 60);
512 }
513 printf("\n");
514 }
515 }
516
517 char *
518 interval(time, updown)
519 int time;
520 char *updown;
521 {
522 static char resbuf[32];
523 int days, hours, minutes;
524
525 if (time < 0 || time > 3*30*24*60*60) {
526 (void) sprintf(resbuf, " %s ??:??", updown);
527 return (resbuf);
528 }
529 minutes = (time + 59) / 60; /* round to minutes */
530 hours = minutes / 60; minutes %= 60;
531 days = hours / 24; hours %= 24;
532 if (days)
533 (void) sprintf(resbuf, "%s %2d+%02d:%02d",
534 updown, days, hours, minutes);
535 else
536 (void) sprintf(resbuf, "%s %2d:%02d",
537 updown, hours, minutes);
538 return (resbuf);
539 }
540 #endif
541