]> git.saurik.com Git - apple/network_cmds.git/blob - rwhod.tproj/rwhod.c
network_cmds-245.1.3.tar.gz
[apple/network_cmds.git] / rwhod.tproj / rwhod.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1983, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56 #ifndef lint
57 static char copyright[] =
58 "@(#) Copyright (c) 1983, 1993\n\
59 The Regents of the University of California. All rights reserved.\n";
60 #endif /* not lint */
61
62 #ifndef lint
63 static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93";
64 #endif /* not lint */
65
66 #include <sys/param.h>
67 #include <sys/socket.h>
68 #include <sys/stat.h>
69 #include <sys/signal.h>
70 #include <sys/ioctl.h>
71 #include <sys/sysctl.h>
72
73 #include <net/if.h>
74 #include <net/if_dl.h>
75 #include <net/route.h>
76 #include <netinet/in.h>
77 #include <arpa/inet.h>
78 #include <protocols/rwhod.h>
79
80 #include <ctype.h>
81 #include <errno.h>
82 #include <fcntl.h>
83 #include <netdb.h>
84 #include <paths.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 #include <syslog.h>
89 #include <unistd.h>
90 #include <utmp.h>
91
92 /*
93 * Alarm interval. Don't forget to change the down time check in ruptime
94 * if this is changed.
95 */
96 #define AL_INTERVAL (3 * 60)
97
98 char myname[MAXHOSTNAMELEN];
99
100 /*
101 * We communicate with each neighbor in a list constructed at the time we're
102 * started up. Neighbors are currently directly connected via a hardware
103 * interface.
104 */
105 struct neighbor {
106 struct neighbor *n_next;
107 char *n_name; /* interface name */
108 struct sockaddr *n_addr; /* who to send to */
109 int n_addrlen; /* size of address */
110 int n_flags; /* should forward?, interface flags */
111 };
112
113 struct neighbor *neighbors;
114 struct whod mywd;
115 struct servent *sp;
116 int s, utmpf;
117
118 #define WHDRSIZE (sizeof(mywd) - sizeof(mywd.wd_we))
119
120 int configure __P((int));
121 void getboottime __P((int));
122 void onalrm __P((int));
123 void quit __P((char *));
124 void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
125 int verify __P((char *));
126 #ifdef DEBUG
127 char *interval __P((int, char *));
128 void Sendto __P((int, char *, int, int, char *, int));
129 #define sendto Sendto
130 #endif
131
132 int
133 main(argc, argv)
134 int argc;
135 char argv[];
136 {
137 struct sockaddr_in from;
138 struct stat st;
139 char path[64];
140 int on = 1;
141 char *cp;
142 struct sockaddr_in sin;
143
144 if (getuid()) {
145 fprintf(stderr, "rwhod: not super user\n");
146 exit(1);
147 }
148 sp = getservbyname("who", "udp");
149 if (sp == NULL) {
150 fprintf(stderr, "rwhod: udp/who: unknown service\n");
151 exit(1);
152 }
153 #ifndef DEBUG
154 daemon(1, 0);
155 #endif
156 if (chdir(_PATH_RWHODIR) < 0) {
157 (void)fprintf(stderr, "rwhod: %s: %s\n",
158 _PATH_RWHODIR, strerror(errno));
159 exit(1);
160 }
161 (void) signal(SIGHUP, getboottime);
162 openlog("rwhod", LOG_PID, LOG_DAEMON);
163 /*
164 * Establish host name as returned by system.
165 */
166 if (gethostname(myname, sizeof(myname) - 1) < 0) {
167 syslog(LOG_ERR, "gethostname: %m");
168 exit(1);
169 }
170 if ((cp = index(myname, '.')) != NULL)
171 *cp = '\0';
172 strncpy(mywd.wd_hostname, myname, sizeof(myname) - 1);
173 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
174 if (utmpf < 0) {
175 syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
176 exit(1);
177 }
178 getboottime(0);
179 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
180 syslog(LOG_ERR, "socket: %m");
181 exit(1);
182 }
183 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
184 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
185 exit(1);
186 }
187 memset(&sin, 0, sizeof(sin));
188 sin.sin_family = AF_INET;
189 sin.sin_port = sp->s_port;
190 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
191 syslog(LOG_ERR, "bind: %m");
192 exit(1);
193 }
194 if (!configure(s))
195 exit(1);
196 signal(SIGALRM, onalrm);
197 onalrm(0);
198 for (;;) {
199 struct whod wd;
200 int cc, whod, len = sizeof(from);
201
202 cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
203 (struct sockaddr *)&from, &len);
204 if (cc <= 0) {
205 if (cc < 0 && errno != EINTR)
206 syslog(LOG_WARNING, "recv: %m");
207 continue;
208 }
209 if (from.sin_port != sp->s_port) {
210 syslog(LOG_WARNING, "%d: bad source port from %s",
211 ntohs(from.sin_port), inet_ntoa(from.sin_addr));
212 continue;
213 }
214 if (cc < WHDRSIZE) {
215 syslog(LOG_WARNING, "short packet from %s",
216 inet_ntoa(from.sin_addr));
217 continue;
218 }
219
220 if (wd.wd_vers != WHODVERSION)
221 continue;
222 if (wd.wd_type != WHODTYPE_STATUS)
223 continue;
224 if (!verify(wd.wd_hostname)) {
225 syslog(LOG_WARNING, "malformed host name from %s",
226 inet_ntoa(from.sin_addr));
227 continue;
228 }
229 (void) sprintf(path, "whod.%s", wd.wd_hostname);
230 /*
231 * Rather than truncating and growing the file each time,
232 * use ftruncate if size is less than previous size.
233 */
234 whod = open(path, O_WRONLY | O_CREAT, 0644);
235 if (whod < 0) {
236 syslog(LOG_WARNING, "%s: %m", path);
237 continue;
238 }
239 #if ENDIAN != BIG_ENDIAN
240 {
241 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
242 struct whoent *we;
243
244 /* undo header byte swapping before writing to file */
245 wd.wd_sendtime = ntohl(wd.wd_sendtime);
246 for (i = 0; i < 3; i++)
247 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
248 wd.wd_boottime = ntohl(wd.wd_boottime);
249 we = wd.wd_we;
250 for (i = 0; i < n; i++) {
251 we->we_idle = ntohl(we->we_idle);
252 we->we_utmp.out_time =
253 ntohl(we->we_utmp.out_time);
254 we++;
255 }
256 }
257 #endif
258 (void) time((time_t *)&wd.wd_recvtime);
259 (void) write(whod, (char *)&wd, cc);
260 if (fstat(whod, &st) < 0 || st.st_size > cc)
261 ftruncate(whod, cc);
262 (void) close(whod);
263 }
264 }
265
266 /*
267 * Check out host name for unprintables
268 * and other funnies before allowing a file
269 * to be created. Sorry, but blanks aren't allowed.
270 */
271 int
272 verify(name)
273 register char *name;
274 {
275 register int size = 0;
276
277 while (*name) {
278 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
279 return (0);
280 name++, size++;
281 }
282 return (size > 0);
283 }
284
285 int utmptime;
286 int utmpent;
287 int utmpsize = 0;
288 struct utmp *utmp;
289 int alarmcount;
290
291 void
292 onalrm(signo)
293 int signo;
294 {
295 register struct neighbor *np;
296 register struct whoent *we = mywd.wd_we, *wlast;
297 register int i;
298 struct stat stb;
299 double avenrun[3];
300 time_t now;
301 int cc;
302
303 now = time(NULL);
304 if (alarmcount % 10 == 0)
305 getboottime(0);
306 alarmcount++;
307 (void) fstat(utmpf, &stb);
308 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
309 utmptime = stb.st_mtime;
310 if (stb.st_size > utmpsize) {
311 utmpsize = stb.st_size + 10 * sizeof(struct utmp);
312 if (utmp)
313 utmp = (struct utmp *)realloc(utmp, utmpsize);
314 else
315 utmp = (struct utmp *)malloc(utmpsize);
316 if (! utmp) {
317 fprintf(stderr, "rwhod: malloc failed\n");
318 utmpsize = 0;
319 goto done;
320 }
321 }
322 (void) lseek(utmpf, (off_t)0, L_SET);
323 cc = read(utmpf, (char *)utmp, stb.st_size);
324 if (cc < 0) {
325 fprintf(stderr, "rwhod: %s: %s\n",
326 _PATH_UTMP, strerror(errno));
327 goto done;
328 }
329 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
330 utmpent = cc / sizeof(struct utmp);
331 for (i = 0; i < utmpent; i++)
332 if (utmp[i].ut_name[0]) {
333 memcpy(we->we_utmp.out_line, utmp[i].ut_line,
334 sizeof(utmp[i].ut_line));
335 memcpy(we->we_utmp.out_name, utmp[i].ut_name,
336 sizeof(utmp[i].ut_name));
337 we->we_utmp.out_time = htonl(utmp[i].ut_time);
338 if (we >= wlast)
339 break;
340 we++;
341 }
342 utmpent = we - mywd.wd_we;
343 }
344
345 /*
346 * The test on utmpent looks silly---after all, if no one is
347 * logged on, why worry about efficiency?---but is useful on
348 * (e.g.) compute servers.
349 */
350 if (utmpent && chdir(_PATH_DEV)) {
351 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
352 exit(1);
353 }
354 we = mywd.wd_we;
355 for (i = 0; i < utmpent; i++) {
356 if (stat(we->we_utmp.out_line, &stb) >= 0)
357 we->we_idle = htonl(now - stb.st_atime);
358 we++;
359 }
360 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
361 for (i = 0; i < 3; i++)
362 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
363 cc = (char *)we - (char *)&mywd;
364 mywd.wd_sendtime = htonl(time(0));
365 mywd.wd_vers = WHODVERSION;
366 mywd.wd_type = WHODTYPE_STATUS;
367 for (np = neighbors; np != NULL; np = np->n_next)
368 (void)sendto(s, (char *)&mywd, cc, 0,
369 np->n_addr, np->n_addrlen);
370 if (utmpent && chdir(_PATH_RWHODIR)) {
371 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
372 exit(1);
373 }
374 done:
375 (void) alarm(AL_INTERVAL);
376 }
377
378 void
379 getboottime(signo)
380 int signo;
381 {
382 int mib[2];
383 size_t size;
384 struct timeval tm;
385
386 mib[0] = CTL_KERN;
387 mib[1] = KERN_BOOTTIME;
388 size = sizeof(tm);
389 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
390 syslog(LOG_ERR, "cannot get boottime: %m");
391 exit(1);
392 }
393 mywd.wd_boottime = htonl(tm.tv_sec);
394 }
395
396 void
397 quit(msg)
398 char *msg;
399 {
400 syslog(LOG_ERR, msg);
401 exit(1);
402 }
403
404 #define ROUNDUP(a) \
405 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
406 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
407
408 void
409 rt_xaddrs(cp, cplim, rtinfo)
410 register caddr_t cp, cplim;
411 register struct rt_addrinfo *rtinfo;
412 {
413 register struct sockaddr *sa;
414 register int i;
415
416 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
417 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
418 if ((rtinfo->rti_addrs & (1 << i)) == 0)
419 continue;
420 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
421 ADVANCE(cp, sa);
422 }
423 }
424
425 /*
426 * Figure out device configuration and select
427 * networks which deserve status information.
428 */
429 int
430 configure(s)
431 int s;
432 {
433 register struct neighbor *np;
434 register struct if_msghdr *ifm;
435 register struct ifa_msghdr *ifam;
436 struct sockaddr_dl *sdl;
437 size_t needed;
438 int mib[6], flags = 0, len;
439 char *buf, *lim, *next;
440 struct rt_addrinfo info;
441
442 mib[0] = CTL_NET;
443 mib[1] = PF_ROUTE;
444 mib[2] = 0;
445 mib[3] = AF_INET;
446 mib[4] = NET_RT_IFLIST;
447 mib[5] = 0;
448 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
449 quit("route-sysctl-estimate");
450 if ((buf = malloc(needed)) == NULL)
451 quit("malloc");
452 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
453 quit("actual retrieval of interface table");
454 lim = buf + needed;
455
456 sdl = NULL; /* XXX just to keep gcc -Wall happy */
457 for (next = buf; next < lim; next += ifm->ifm_msglen) {
458 ifm = (struct if_msghdr *)next;
459 if (ifm->ifm_type == RTM_IFINFO) {
460 sdl = (struct sockaddr_dl *)(ifm + 1);
461 flags = ifm->ifm_flags;
462 continue;
463 }
464 if ((flags & IFF_UP) == 0 ||
465 (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
466 continue;
467 if (ifm->ifm_type != RTM_NEWADDR)
468 quit("out of sync parsing NET_RT_IFLIST");
469 ifam = (struct ifa_msghdr *)ifm;
470 info.rti_addrs = ifam->ifam_addrs;
471 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
472 &info);
473 /* gag, wish we could get rid of Internet dependencies */
474 #define dstaddr info.rti_info[RTAX_BRD]
475 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
476 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
477 if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
478 continue;
479 PORT_SA(dstaddr) = sp->s_port;
480 for (np = neighbors; np != NULL; np = np->n_next)
481 if (memcmp(sdl->sdl_data, np->n_name,
482 sdl->sdl_nlen) == 0 &&
483 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
484 break;
485 if (np != NULL)
486 continue;
487 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
488 np = (struct neighbor *)malloc(len);
489 if (np == NULL)
490 quit("malloc of neighbor structure");
491 memset(np, 0, len);
492 np->n_flags = flags;
493 np->n_addr = (struct sockaddr *)(np + 1);
494 np->n_addrlen = dstaddr->sa_len;
495 np->n_name = np->n_addrlen + (char *)np->n_addr;
496 np->n_next = neighbors;
497 neighbors = np;
498 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
499 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
500 }
501 free(buf);
502 return (1);
503 }
504
505 #ifdef DEBUG
506 void
507 Sendto(s, buf, cc, flags, to, tolen)
508 int s;
509 char *buf;
510 int cc, flags;
511 char *to;
512 int tolen;
513 {
514 register struct whod *w = (struct whod *)buf;
515 register struct whoent *we;
516 struct sockaddr_in *sin = (struct sockaddr_in *)to;
517
518 printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
519 printf("hostname %s %s\n", w->wd_hostname,
520 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
521 printf("load %4.2f, %4.2f, %4.2f\n",
522 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
523 ntohl(w->wd_loadav[2]) / 100.0);
524 cc -= WHDRSIZE;
525 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
526 time_t t = ntohl(we->we_utmp.out_time);
527 printf("%-8.8s %s:%s %.12s",
528 we->we_utmp.out_name,
529 w->wd_hostname, we->we_utmp.out_line,
530 ctime(&t)+4);
531 we->we_idle = ntohl(we->we_idle) / 60;
532 if (we->we_idle) {
533 if (we->we_idle >= 100*60)
534 we->we_idle = 100*60 - 1;
535 if (we->we_idle >= 60)
536 printf(" %2d", we->we_idle / 60);
537 else
538 printf(" ");
539 printf(":%02d", we->we_idle % 60);
540 }
541 printf("\n");
542 }
543 }
544
545 char *
546 interval(time, updown)
547 int time;
548 char *updown;
549 {
550 static char resbuf[32];
551 int days, hours, minutes;
552
553 if (time < 0 || time > 3*30*24*60*60) {
554 (void) sprintf(resbuf, " %s ??:??", updown);
555 return (resbuf);
556 }
557 minutes = (time + 59) / 60; /* round to minutes */
558 hours = minutes / 60; minutes %= 60;
559 days = hours / 24; hours %= 24;
560 if (days)
561 (void) sprintf(resbuf, "%s %2d+%02d:%02d",
562 updown, days, hours, minutes);
563 else
564 (void) sprintf(resbuf, "%s %2d:%02d",
565 updown, hours, minutes);
566 return (resbuf);
567 }
568 #endif