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