122549Sdist /* 222549Sdist * Copyright (c) 1983 Regents of the University of California. 322549Sdist * All rights reserved. The Berkeley software License Agreement 422549Sdist * specifies the terms and conditions for redistribution. 522549Sdist */ 622549Sdist 76460Swnj #ifndef lint 822549Sdist char copyright[] = 922549Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 1022549Sdist All rights reserved.\n"; 1122549Sdist #endif not lint 126460Swnj 1322549Sdist #ifndef lint 14*26016Skarels static char sccsid[] = "@(#)rwhod.c 5.8 (Berkeley) 02/01/86"; 1522549Sdist #endif not lint 1622549Sdist 179215Ssam #include <sys/types.h> 189215Ssam #include <sys/socket.h> 199215Ssam #include <sys/stat.h> 209215Ssam #include <sys/ioctl.h> 2113187Ssam #include <sys/file.h> 229215Ssam 2312236Ssam #include <net/if.h> 249215Ssam #include <netinet/in.h> 259215Ssam 269215Ssam #include <nlist.h> 276460Swnj #include <stdio.h> 286460Swnj #include <signal.h> 296460Swnj #include <errno.h> 306460Swnj #include <utmp.h> 318486Ssam #include <ctype.h> 328486Ssam #include <netdb.h> 3316673Sralph #include <syslog.h> 3423535Smckusick #include <protocols/rwhod.h> 356460Swnj 3615541Sralph /* 3715541Sralph * Alarm interval. Don't forget to change the down time check in ruptime 3815541Sralph * if this is changed. 3915541Sralph */ 4016673Sralph #define AL_INTERVAL (3 * 60) 4115541Sralph 428486Ssam struct sockaddr_in sin = { AF_INET }; 436460Swnj 446460Swnj extern errno; 456460Swnj 4612236Ssam char myname[32]; 476460Swnj 486460Swnj struct nlist nl[] = { 496460Swnj #define NL_AVENRUN 0 506460Swnj { "_avenrun" }, 519215Ssam #define NL_BOOTTIME 1 529215Ssam { "_boottime" }, 536460Swnj 0 546460Swnj }; 556460Swnj 5612236Ssam /* 5712236Ssam * We communicate with each neighbor in 5812236Ssam * a list constructed at the time we're 5912236Ssam * started up. Neighbors are currently 6012236Ssam * directly connected via a hardware interface. 6112236Ssam */ 6212236Ssam struct neighbor { 6312236Ssam struct neighbor *n_next; 6412236Ssam char *n_name; /* interface name */ 6512236Ssam char *n_addr; /* who to send to */ 6612236Ssam int n_addrlen; /* size of address */ 6712236Ssam int n_flags; /* should forward?, interface flags */ 6812236Ssam }; 6912236Ssam 7012236Ssam struct neighbor *neighbors; 716460Swnj struct whod mywd; 7212236Ssam struct servent *sp; 736460Swnj int s, utmpf, kmemf = -1; 746460Swnj 7512236Ssam #define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we)) 7612236Ssam #define RWHODIR "/usr/spool/rwho" 7711834Ssam 786460Swnj int onalrm(); 7912236Ssam char *strcpy(), *sprintf(), *malloc(); 806460Swnj long lseek(); 816460Swnj int getkmem(); 8212236Ssam struct in_addr inet_makeaddr(); 836460Swnj 846460Swnj main() 856460Swnj { 866460Swnj struct sockaddr_in from; 87*26016Skarels struct stat st; 886460Swnj char path[64]; 8917155Ssam int addr, on = 1; 9024745Sbloom char *cp; 9124745Sbloom extern char *index(); 926460Swnj 9316673Sralph if (getuid()) { 9416673Sralph fprintf(stderr, "rwhod: not super user\n"); 9516673Sralph exit(1); 9616673Sralph } 978486Ssam sp = getservbyname("who", "udp"); 988486Ssam if (sp == 0) { 998486Ssam fprintf(stderr, "rwhod: udp/who: unknown service\n"); 1008486Ssam exit(1); 1018486Ssam } 1026460Swnj #ifndef DEBUG 1036460Swnj if (fork()) 1046460Swnj exit(0); 1056460Swnj { int s; 1066460Swnj for (s = 0; s < 10; s++) 1076460Swnj (void) close(s); 1086460Swnj (void) open("/", 0); 1096460Swnj (void) dup2(0, 1); 1106460Swnj (void) dup2(0, 2); 1116460Swnj s = open("/dev/tty", 2); 1126460Swnj if (s >= 0) { 1136460Swnj ioctl(s, TIOCNOTTY, 0); 1146460Swnj (void) close(s); 1156460Swnj } 1166460Swnj } 1176460Swnj #endif 11826011Smckusick if (chdir(RWHODIR) < 0) { 11926011Smckusick perror(RWHODIR); 12026011Smckusick exit(1); 12126011Smckusick } 1226460Swnj (void) signal(SIGHUP, getkmem); 12324853Seric openlog("rwhod", LOG_PID, LOG_DAEMON); 12412236Ssam /* 12512236Ssam * Establish host name as returned by system. 12612236Ssam */ 12712236Ssam if (gethostname(myname, sizeof (myname) - 1) < 0) { 12816673Sralph syslog(LOG_ERR, "gethostname: %m"); 1296460Swnj exit(1); 1306460Swnj } 13124745Sbloom if ((cp = index(myname, '.')) != NULL) 13224745Sbloom *cp = '\0'; 13312236Ssam strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1); 13413187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1356460Swnj if (utmpf < 0) { 1366460Swnj (void) close(creat("/etc/utmp", 0644)); 13713187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1386460Swnj } 1396460Swnj if (utmpf < 0) { 14016673Sralph syslog(LOG_ERR, "/etc/utmp: %m"); 1416460Swnj exit(1); 1426460Swnj } 1436460Swnj getkmem(); 14413187Ssam if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 14516673Sralph syslog(LOG_ERR, "socket: %m"); 1469215Ssam exit(1); 1476460Swnj } 14817155Ssam if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 14917055Skarels syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 15017055Skarels exit(1); 15117055Skarels } 15212236Ssam sin.sin_port = sp->s_port; 15313187Ssam if (bind(s, &sin, sizeof (sin)) < 0) { 15416673Sralph syslog(LOG_ERR, "bind: %m"); 1559215Ssam exit(1); 1569215Ssam } 15712236Ssam if (!configure(s)) 15812236Ssam exit(1); 15913027Ssam signal(SIGALRM, onalrm); 1606460Swnj onalrm(); 1616460Swnj for (;;) { 1626460Swnj struct whod wd; 16312236Ssam int cc, whod, len = sizeof (from); 1646460Swnj 16512236Ssam cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0, 16612236Ssam &from, &len); 1676460Swnj if (cc <= 0) { 1686460Swnj if (cc < 0 && errno != EINTR) 16916673Sralph syslog(LOG_WARNING, "recv: %m"); 1706460Swnj continue; 1716460Swnj } 1728486Ssam if (from.sin_port != sp->s_port) { 17316673Sralph syslog(LOG_WARNING, "%d: bad from port", 1748486Ssam ntohs(from.sin_port)); 1756460Swnj continue; 1766460Swnj } 1778486Ssam #ifdef notdef 1788486Ssam if (gethostbyname(wd.wd_hostname) == 0) { 17916673Sralph syslog(LOG_WARNING, "%s: unknown host", 1808486Ssam wd.wd_hostname); 1816460Swnj continue; 1826460Swnj } 1838486Ssam #endif 18412347Ssam if (wd.wd_vers != WHODVERSION) 18512347Ssam continue; 18612236Ssam if (wd.wd_type != WHODTYPE_STATUS) 18712236Ssam continue; 1888486Ssam if (!verify(wd.wd_hostname)) { 18916673Sralph syslog(LOG_WARNING, "malformed host name from %x", 1908486Ssam from.sin_addr); 1918486Ssam continue; 1928486Ssam } 19326011Smckusick (void) sprintf(path, "whod.%s", wd.wd_hostname); 194*26016Skarels /* 195*26016Skarels * Rather than truncating and growing the file each time, 196*26016Skarels * use ftruncate if size is less than previous size. 197*26016Skarels */ 198*26016Skarels whod = open(path, O_WRONLY | O_CREAT, 0644); 1996460Swnj if (whod < 0) { 20016673Sralph syslog(LOG_WARNING, "%s: %m", path); 2016460Swnj continue; 2026460Swnj } 20311834Ssam #if vax || pdp11 20411834Ssam { 20513187Ssam int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 20611834Ssam struct whoent *we; 20711834Ssam 20811834Ssam /* undo header byte swapping before writing to file */ 20911834Ssam wd.wd_sendtime = ntohl(wd.wd_sendtime); 21011834Ssam for (i = 0; i < 3; i++) 21111834Ssam wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 21211834Ssam wd.wd_boottime = ntohl(wd.wd_boottime); 21311834Ssam we = wd.wd_we; 21411834Ssam for (i = 0; i < n; i++) { 21511834Ssam we->we_idle = ntohl(we->we_idle); 21612872Ssam we->we_utmp.out_time = 21712872Ssam ntohl(we->we_utmp.out_time); 21811834Ssam we++; 21911834Ssam } 22011834Ssam } 22111834Ssam #endif 2226460Swnj (void) time(&wd.wd_recvtime); 2236460Swnj (void) write(whod, (char *)&wd, cc); 224*26016Skarels if (fstat(whod, &st) < 0 || st.st_size > cc) 225*26016Skarels ftruncate(whod, cc); 2266460Swnj (void) close(whod); 2276460Swnj } 2286460Swnj } 2296460Swnj 2308486Ssam /* 2318486Ssam * Check out host name for unprintables 2328486Ssam * and other funnies before allowing a file 2338486Ssam * to be created. Sorry, but blanks aren't allowed. 2348486Ssam */ 2358486Ssam verify(name) 2368486Ssam register char *name; 2378486Ssam { 2388486Ssam register int size = 0; 2398486Ssam 2408486Ssam while (*name) { 24115214Sleres if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 2428486Ssam return (0); 2438486Ssam name++, size++; 2448486Ssam } 2458486Ssam return (size > 0); 2468486Ssam } 2478486Ssam 2486460Swnj int utmptime; 2496460Swnj int utmpent; 25022546Sbloom int utmpsize = 0; 25122546Sbloom struct utmp *utmp; 2526460Swnj int alarmcount; 2536460Swnj 2546460Swnj onalrm() 2556460Swnj { 2566460Swnj register int i; 2576460Swnj struct stat stb; 2586577Ssam register struct whoent *we = mywd.wd_we, *wlast; 2596460Swnj int cc; 2606460Swnj double avenrun[3]; 2616460Swnj time_t now = time(0); 26212236Ssam register struct neighbor *np; 2636460Swnj 2646460Swnj if (alarmcount % 10 == 0) 2656460Swnj getkmem(); 2666460Swnj alarmcount++; 2676460Swnj (void) fstat(utmpf, &stb); 26822546Sbloom if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 26917303Stef utmptime = stb.st_mtime; 27022546Sbloom if (stb.st_size > utmpsize) { 27122546Sbloom utmpsize = stb.st_size + 10 * sizeof(struct utmp); 27222546Sbloom if (utmp) 27322546Sbloom utmp = (struct utmp *)realloc(utmp, utmpsize); 27422546Sbloom else 27522546Sbloom utmp = (struct utmp *)malloc(utmpsize); 27622546Sbloom if (! utmp) { 27722546Sbloom fprintf(stderr, "rwhod: malloc failed\n"); 27822546Sbloom utmpsize = 0; 27922546Sbloom goto done; 28022546Sbloom } 28122546Sbloom } 28213187Ssam (void) lseek(utmpf, (long)0, L_SET); 28322546Sbloom cc = read(utmpf, (char *)utmp, stb.st_size); 2846460Swnj if (cc < 0) { 2856460Swnj perror("/etc/utmp"); 28613469Ssam goto done; 2876460Swnj } 28812236Ssam wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1]; 2896460Swnj utmpent = cc / sizeof (struct utmp); 2906460Swnj for (i = 0; i < utmpent; i++) 2916460Swnj if (utmp[i].ut_name[0]) { 29212807Ssam bcopy(utmp[i].ut_line, we->we_utmp.out_line, 29312807Ssam sizeof (utmp[i].ut_line)); 29412807Ssam bcopy(utmp[i].ut_name, we->we_utmp.out_name, 29512807Ssam sizeof (utmp[i].ut_name)); 29612807Ssam we->we_utmp.out_time = htonl(utmp[i].ut_time); 2976577Ssam if (we >= wlast) 2986577Ssam break; 2996460Swnj we++; 3006460Swnj } 3016460Swnj utmpent = we - mywd.wd_we; 3026460Swnj } 3036460Swnj we = mywd.wd_we; 3046460Swnj for (i = 0; i < utmpent; i++) { 30526011Smckusick char path[64]; 30626011Smckusick (void) sprintf(path, "/dev/%s", we->we_utmp.out_line); 30726011Smckusick if (stat(path, &stb) >= 0) 30812807Ssam we->we_idle = htonl(now - stb.st_atime); 3096460Swnj we++; 3106460Swnj } 31113187Ssam (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET); 3126460Swnj (void) read(kmemf, (char *)avenrun, sizeof (avenrun)); 3136460Swnj for (i = 0; i < 3; i++) 31412873Ssam mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 3156460Swnj cc = (char *)we - (char *)&mywd; 31612807Ssam mywd.wd_sendtime = htonl(time(0)); 31712236Ssam mywd.wd_vers = WHODVERSION; 31812236Ssam mywd.wd_type = WHODTYPE_STATUS; 31912236Ssam for (np = neighbors; np != NULL; np = np->n_next) 32012236Ssam (void) sendto(s, (char *)&mywd, cc, 0, 32112236Ssam np->n_addr, np->n_addrlen); 32213469Ssam done: 32315541Sralph (void) alarm(AL_INTERVAL); 3246460Swnj } 3256460Swnj 3266460Swnj getkmem() 3276460Swnj { 3286460Swnj struct nlist *nlp; 32916663Ssam static ino_t vmunixino; 33016663Ssam static time_t vmunixctime; 33116663Ssam struct stat sb; 3326460Swnj 33316663Ssam if (stat("/vmunix", &sb) < 0) { 33416663Ssam if (vmunixctime) 33516663Ssam return; 33616663Ssam } else { 33716663Ssam if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino) 33816663Ssam return; 33916663Ssam vmunixctime = sb.st_ctime; 34016663Ssam vmunixino= sb.st_ino; 34116663Ssam } 3426460Swnj if (kmemf >= 0) 3436460Swnj (void) close(kmemf); 3446460Swnj loop: 34516673Sralph if (nlist("/vmunix", nl)) { 34616673Sralph syslog(LOG_WARNING, "/vmunix namelist botch"); 3476460Swnj sleep(300); 3486460Swnj goto loop; 3496460Swnj } 35013187Ssam kmemf = open("/dev/kmem", O_RDONLY); 3516460Swnj if (kmemf < 0) { 35216673Sralph syslog(LOG_ERR, "/dev/kmem: %m"); 35316673Sralph exit(1); 3546460Swnj } 35513187Ssam (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET); 35613187Ssam (void) read(kmemf, (char *)&mywd.wd_boottime, 35713187Ssam sizeof (mywd.wd_boottime)); 35812807Ssam mywd.wd_boottime = htonl(mywd.wd_boottime); 3596460Swnj } 36012236Ssam 36112236Ssam /* 36212236Ssam * Figure out device configuration and select 36312236Ssam * networks which deserve status information. 36412236Ssam */ 36512236Ssam configure(s) 36612236Ssam int s; 36712236Ssam { 36812236Ssam char buf[BUFSIZ]; 36912236Ssam struct ifconf ifc; 37012236Ssam struct ifreq ifreq, *ifr; 37112236Ssam struct sockaddr_in *sin; 37212236Ssam register struct neighbor *np; 37313187Ssam int n; 37412236Ssam 37512236Ssam ifc.ifc_len = sizeof (buf); 37612236Ssam ifc.ifc_buf = buf; 37712236Ssam if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 37816673Sralph syslog(LOG_ERR, "ioctl (get interface configuration)"); 37912236Ssam return (0); 38012236Ssam } 38112236Ssam ifr = ifc.ifc_req; 38212236Ssam for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) { 38312236Ssam for (np = neighbors; np != NULL; np = np->n_next) 38412236Ssam if (np->n_name && 38512236Ssam strcmp(ifr->ifr_name, np->n_name) == 0) 38612236Ssam break; 38712236Ssam if (np != NULL) 38812236Ssam continue; 38912236Ssam ifreq = *ifr; 39012236Ssam np = (struct neighbor *)malloc(sizeof (*np)); 39112236Ssam if (np == NULL) 39212236Ssam continue; 39312236Ssam np->n_name = malloc(strlen(ifr->ifr_name) + 1); 39412236Ssam if (np->n_name == NULL) { 39512236Ssam free((char *)np); 39612236Ssam continue; 39712236Ssam } 39812236Ssam strcpy(np->n_name, ifr->ifr_name); 39912236Ssam np->n_addrlen = sizeof (ifr->ifr_addr); 40012236Ssam np->n_addr = malloc(np->n_addrlen); 40112236Ssam if (np->n_addr == NULL) { 40212236Ssam free(np->n_name); 40312236Ssam free((char *)np); 40412236Ssam continue; 40512236Ssam } 40612236Ssam bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen); 40712236Ssam if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 40816673Sralph syslog(LOG_ERR, "ioctl (get interface flags)"); 40912236Ssam free((char *)np); 41012236Ssam continue; 41112236Ssam } 41221852Skarels if ((ifreq.ifr_flags & IFF_UP) == 0 || 41321852Skarels (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) { 41412236Ssam free((char *)np); 41512236Ssam continue; 41612236Ssam } 41712236Ssam np->n_flags = ifreq.ifr_flags; 41812236Ssam if (np->n_flags & IFF_POINTOPOINT) { 41912236Ssam if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) { 42016673Sralph syslog(LOG_ERR, "ioctl (get dstaddr)"); 42112236Ssam free((char *)np); 42212236Ssam continue; 42312236Ssam } 42412236Ssam /* we assume addresses are all the same size */ 42512236Ssam bcopy((char *)&ifreq.ifr_dstaddr, 42612236Ssam np->n_addr, np->n_addrlen); 42712236Ssam } 42812236Ssam if (np->n_flags & IFF_BROADCAST) { 42921852Skarels if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { 43021852Skarels syslog(LOG_ERR, "ioctl (get broadaddr)"); 43121852Skarels free((char *)np); 43221852Skarels continue; 43321852Skarels } 43412236Ssam /* we assume addresses are all the same size */ 43521852Skarels bcopy((char *)&ifreq.ifr_broadaddr, 43621852Skarels np->n_addr, np->n_addrlen); 43712236Ssam } 43812236Ssam /* gag, wish we could get rid of Internet dependencies */ 43912236Ssam sin = (struct sockaddr_in *)np->n_addr; 44012236Ssam sin->sin_port = sp->s_port; 44112236Ssam np->n_next = neighbors; 44212236Ssam neighbors = np; 44312236Ssam } 44412236Ssam return (1); 44512236Ssam } 44612807Ssam 44712807Ssam #ifdef DEBUG 44812807Ssam sendto(s, buf, cc, flags, to, tolen) 44912807Ssam int s; 45012807Ssam char *buf; 45112807Ssam int cc, flags; 45212807Ssam char *to; 45312807Ssam int tolen; 45412807Ssam { 45512807Ssam register struct whod *w = (struct whod *)buf; 45612807Ssam register struct whoent *we; 45712807Ssam struct sockaddr_in *sin = (struct sockaddr_in *)to; 45812807Ssam char *interval(); 45912807Ssam 46012807Ssam printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 46112807Ssam printf("hostname %s %s\n", w->wd_hostname, 46213187Ssam interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 46312807Ssam printf("load %4.2f, %4.2f, %4.2f\n", 46413187Ssam ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 46513187Ssam ntohl(w->wd_loadav[2]) / 100.0); 46612807Ssam cc -= WHDRSIZE; 46712807Ssam for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) { 46813187Ssam time_t t = ntohl(we->we_utmp.out_time); 46912807Ssam printf("%-8.8s %s:%s %.12s", 47013187Ssam we->we_utmp.out_name, 47113187Ssam w->wd_hostname, we->we_utmp.out_line, 47213187Ssam ctime(&t)+4); 47313187Ssam we->we_idle = ntohl(we->we_idle) / 60; 47412807Ssam if (we->we_idle) { 47512807Ssam if (we->we_idle >= 100*60) 47612807Ssam we->we_idle = 100*60 - 1; 47712807Ssam if (we->we_idle >= 60) 47812807Ssam printf(" %2d", we->we_idle / 60); 47912807Ssam else 48012807Ssam printf(" "); 48112807Ssam printf(":%02d", we->we_idle % 60); 48212807Ssam } 48312807Ssam printf("\n"); 48412807Ssam } 48512807Ssam } 48612807Ssam 48712807Ssam char * 48812807Ssam interval(time, updown) 48912807Ssam int time; 49012807Ssam char *updown; 49112807Ssam { 49212807Ssam static char resbuf[32]; 49312807Ssam int days, hours, minutes; 49412807Ssam 49512807Ssam if (time < 0 || time > 3*30*24*60*60) { 49612807Ssam (void) sprintf(resbuf, " %s ??:??", updown); 49712807Ssam return (resbuf); 49812807Ssam } 49912807Ssam minutes = (time + 59) / 60; /* round to minutes */ 50012807Ssam hours = minutes / 60; minutes %= 60; 50112807Ssam days = hours / 24; hours %= 24; 50212807Ssam if (days) 50312807Ssam (void) sprintf(resbuf, "%s %2d+%02d:%02d", 50412807Ssam updown, days, hours, minutes); 50512807Ssam else 50612807Ssam (void) sprintf(resbuf, "%s %2d:%02d", 50712807Ssam updown, hours, minutes); 50812807Ssam return (resbuf); 50912807Ssam } 51012807Ssam #endif 511